mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 02:14:01 +00:00
The source dropped the proxy's stale flag and fetch time, so the layer row read nominal and freshly updated through an outage, and the proxy served its last snapshot indefinitely. Carry stale and fetchedAt through the source as Fire Perimeters does, date a stale snapshot by its fetch, and stop serving a snapshot more than 24 hours old.
39 lines
1.3 KiB
JavaScript
39 lines
1.3 KiB
JavaScript
import { readResponseJsonCapped } from '../../sources/httpBody.js';
|
|
|
|
/** Request a normalized snapshot through the bounded, same-origin FAA proxy. */
|
|
export function createDroneAirspaceSource({
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
} = {}) {
|
|
/**
|
|
* The rows with how fresh they are: the proxy answers from its last
|
|
* snapshot, marked stale, while the FAA service is unreachable.
|
|
* @returns {Promise<{rows: object[], stale: boolean, fetchedAt: number|null}>}
|
|
*/
|
|
async function getSnapshotDetail({ signal } = {}) {
|
|
signal?.throwIfAborted();
|
|
const response = await fetchImpl('/api/drone-airspace', { signal });
|
|
if (!response.ok) throw new Error(`FAA SUA HTTP ${response.status}`);
|
|
const payload = await readResponseJsonCapped(
|
|
response,
|
|
80 * 1024 * 1024,
|
|
signal,
|
|
);
|
|
signal?.throwIfAborted();
|
|
if (!Array.isArray(payload?.rows))
|
|
throw new Error('Malformed drone airspace snapshot');
|
|
return {
|
|
rows: payload.rows,
|
|
stale: payload.stale === true,
|
|
fetchedAt: Number.isFinite(payload.fetchedAt) ? payload.fetchedAt : null,
|
|
};
|
|
}
|
|
|
|
return {
|
|
/** The zone rows (see getSnapshotDetail for staleness). */
|
|
async getSnapshot(options = {}) {
|
|
return (await getSnapshotDetail(options)).rows;
|
|
},
|
|
getSnapshotDetail,
|
|
};
|
|
}
|