import { normalizeFirePerimeterSnapshot } from '../../src/layers/perimeters/records.js'; import { readResponseJsonCapped, readResponseTextCapped, coalesceProxyRequest, } from './common/http.js'; import { makeRateLimiter, clientKey } from './common/rate-limit.js'; // NIFC WFIGS current interagency fire perimeters (public, keyless). // maxAllowableOffset trades ~100 m of boundary fidelity for a payload small // enough to refresh continuously (~1 MB for a typical fire season). const API_URL = 'https://services3.arcgis.com/T4QMspbfLg3qTGWY/arcgis/rest/services/' + 'WFIGS_Interagency_Perimeters_Current/FeatureServer/0/query?' + new URLSearchParams({ where: '1=1', outFields: [ 'poly_IncidentName', 'attr_UniqueFireIdentifier', 'attr_IncidentSize', 'attr_PercentContained', 'attr_POOState', 'attr_IncidentTypeCategory', 'attr_FireDiscoveryDateTime', 'poly_DateCurrent', 'attr_FireCause', 'attr_FireBehaviorGeneral', 'attr_TotalIncidentPersonnel', 'attr_POOCounty', 'attr_EstimatedCostToDate', 'attr_IncidentComplexityLevel', 'attr_CpxName', ].join(','), maxAllowableOffset: '0.001', outSR: '4326', f: 'geojson', }).toString(); // The service caps a single response at its maxRecordCount (2000); a peak // season can exceed that, so follow exceededTransferLimit with offset pages. const MAX_PAGES = 5; const exceededTransferLimit = (payload) => payload?.exceededTransferLimit === true || payload?.properties?.exceededTransferLimit === true; const MIB = 1024 * 1024; /** Fixed-origin, bounded WFIGS and InciWeb routes for dev and preview. */ export function firePerimetersProxy({ fetchImpl = (...args) => globalThis.fetch(...args), now = () => Date.now(), } = {}) { const cache = new Map(); const publications = new Map(); const inFlight = new Map(); const allow = makeRateLimiter({ windowMs: 60_000, max: 60, globalMax: 1200 }); async function upstream(url, cap, timeout, options = {}) { const signal = AbortSignal.timeout(timeout); const response = await fetchImpl(url, { ...options, signal, redirect: 'error', }); if (!response.ok) { await response.body?.cancel(); throw new Error('upstream_unavailable'); } return readResponseJsonCapped(response, cap, signal); } async function fetchPerimeters() { const features = []; for (let page = 0; page < MAX_PAGES; page++) { const url = page === 0 ? API_URL : `${API_URL}&resultOffset=${features.length}`; const payload = await upstream(url, 16 * MIB, 30_000); if (!Array.isArray(payload?.features)) throw new Error('invalid_snapshot'); features.push(...payload.features); if (!exceededTransferLimit(payload) || !payload.features.length) break; } const rows = normalizeFirePerimeterSnapshot({ features }); return { fetchedAt: now(), rows }; } async function fetchIndex() { const rows = await upstream( 'https://inciweb.wildfire.gov/api/single-publication/', 4 * MIB, 20_000, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ title: '' }), }, ); if (!Array.isArray(rows)) throw new Error('invalid_index'); return rows; } // InciWeb's publication API was retired; incident pages carry origin and update times. async function fetchPublication(id) { const signal = AbortSignal.timeout(15_000); let response = await fetchImpl(`https://inciweb.wildfire.gov/node/${id}`, { signal, redirect: 'manual', }); if ([301, 302, 303, 307, 308].includes(response.status)) { await response.body?.cancel(); const location = response.headers.get('location'); if (!location || /\.\.|[?%\\]/.test(location)) throw new Error('invalid_redirect'); const url = new URL(location, 'https://inciweb.wildfire.gov/'); if ( url.hostname !== 'inciweb.wildfire.gov' || !/^\/[a-z0-9-]+(?:\/[a-z0-9-]+)?$/.test(url.pathname) || !['http:', 'https:'].includes(url.protocol) || url.port || url.username || url.password ) throw new Error('invalid_redirect'); url.protocol = 'https:'; url.search = ''; url.hash = ''; response = await fetchImpl(url.href, { signal, redirect: 'error' }); } if (response.status !== 200) { await response.body?.cancel(); throw new Error('upstream_unavailable'); } const html = await readResponseTextCapped(response, 2 * MIB, signal); const changed = Date.parse( html.match(/\s*