mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 02:14:01 +00:00
Merge branch 'main' into codex/fix-overpass-cache-singleflight
This commit is contained in:
90 files changed
+6131
-288
No files matched your search
@@ -7,10 +7,9 @@ import { readCappedResponseText } from '../common/http.js';
|
||||
* own credit bucket, 4 credits per call on the free tier). OAuth via the
|
||||
* shared coalesced token. 60s per-icao cache; 404/429 forwarded so the
|
||||
* client can fall back to its accumulated trail silently.
|
||||
* /api/military/track?hex=<hex> — adsb.lol tar1090 readsb trace
|
||||
* (undocumented but live; no browser CORS, hence this proxy). Up to ~24h
|
||||
* of real history per aircraft. Treat as best-effort; data is ODbL —
|
||||
* credit "adsb.lol (ODbL)" in the UI.
|
||||
* /api/military/track?hex=<hex> — compatibility refusal for the retired,
|
||||
* undocumented ADSB.lol trace route. The standalone military source uses
|
||||
* locally accumulated trails; provider-supplied history remains optional.
|
||||
*/
|
||||
export function trackBackfillProxies() {
|
||||
const TRACK_CACHE_MS = 60000;
|
||||
@@ -107,10 +106,16 @@ export function trackBackfillProxies() {
|
||||
);
|
||||
return;
|
||||
}
|
||||
await proxyJson(
|
||||
res,
|
||||
`lol:${hex}`,
|
||||
`https://adsb.lol/data/traces/${hex.slice(-2)}/trace_full_${hex}.json`,
|
||||
res.statusCode = 503;
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.setHeader('Cache-Control', 'no-store');
|
||||
res.setHeader('Retry-After', '1800');
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
error:
|
||||
'Military history unavailable; locally accumulated trails remain available',
|
||||
code: 'history_unavailable',
|
||||
}),
|
||||
);
|
||||
} catch (error) {
|
||||
res.statusCode = 502;
|
||||
|
||||
@@ -16,6 +16,7 @@ function realtimeInstructions(
|
||||
'For "what is this aircraft?", answer with get_entity_context\'s identityLine: callsign, operator, type and route from returned fields only, airport codes verbatim. Other questions about the selection answer from its properties. Never infer them from the callsign.',
|
||||
'While a camera motion or route flight is active, a bare "stop" means move_camera{motion:stop} — NOT control_scene and NOT stop_tracking (those need explicit words like "stop the scene" / "stop tracking"). If move_camera stop returns stopped:false and an entity is being tracked, call stop_tracking next — the user means "stop whatever is moving". Flying somewhere while tracking stops the tracking.',
|
||||
'For camera-motion requests — "orbit around this", "pan left", "tilt up", "stop moving" — call move_camera. For "fly the route" over a drawn route, call fly_route. Confirm with the RESULTING state ("Orbiting slowly", "Flying the route").',
|
||||
'Globe spin: plain Rotate, including a repeat after a refusal, calls only move_camera motion rotate_globe; the app checks fit. For rotation, call zoom_to_globe only for this turn\'s explicit user Reset/Show Globe View request, never from your own plan, an earlier refusal or a bare yes/ok. On globe-view-required say "Globe rotation isn’t available from this view. You can say ‘Reset to Globe View and rotate the globe.’" Do not ask a question or call more tools for that request. Explicit reset-and-rotate: await reset success before rotate_globe; never both together. After a failed reset, do not retry for that request. Local turn/look left/right: move_camera motion rotate in that direction, not pan.',
|
||||
'analyst_query ANSWERS questions; it never moves the camera or starts tracking. For requests to FOLLOW or TRACK a specific aircraft/ship, call track_entity (get_entity_context first when the target is ambiguous), never analyst_query as the final or only action. For "follow/track the nearest aircraft", first call analyst_query with the aircraft layer(s), sortBy=distance, and limit=1, then call track_entity with the returned aircraft identity in the same turn. The lookup alone does not fulfill a follow/track command.',
|
||||
'For a request to enable an aircraft layer and SELECT or FIND the nearest/closest aircraft near a named place — for example, "Turn on flights and select the closest aircraft to Austin" — call select_nearest_aircraft once. It atomically turns on the requested aircraft layer first, waits for location arrival, refreshes that layer for the destination viewport, filters out landed/on-ground records, and selects the nearest airborne result. A healthy fallback feed is valid data, never an enable failure. Do not also call fly_to_location, set_layer_visibility, analyst_query, track_entity, set_context_mode, or control_cockpit for the same request. SELECT/FIND never implies Contacts or Cockpit unless the user explicitly asks for either mode.',
|
||||
'For ANALYTICAL questions about loaded layer data — how many / which / fastest / highest / biggest / nearest / next flights, ships, fires, earthquakes, satellites, launches, storms, transit, bikes, bases, datacenters or dams ("how many flights over Texas", "anything above 40,000 feet") — call analyst_query, not get_entity_context. Always set scope by WHERE: "around here" or "near this" while pointing is kind pointer, never view. Filter on the attribute the user names ("MODIS" → sensor eq MODIS). Say the count with its scope in a few words ("37 loaded flights over Texas") plus one or two examples by name. If the needed layer is off, say so and offer to turn it on. "Which of THOSE…" about the last answer → followUp=true with only the new filter/sort.',
|
||||
|
||||
@@ -511,18 +511,18 @@ export const ACTION_DESCRIPTIONS = {
|
||||
},
|
||||
move_camera: {
|
||||
description:
|
||||
'Direct the camera like a drone operator: orbit the current view target, pan, tilt, or rotate — one bounded nudge (mode=once) or continuous motion until stopped (mode=continuous). Continuous motion also stops on any manual camera input or when a navigation tool runs. Say the RESULTING state when confirming ("Orbiting slowly").',
|
||||
'Direct the camera like a drone operator: orbit the current view target, pan, tilt, or rotate — one bounded nudge (mode=once) or continuous motion until stopped (mode=continuous). Continuous motion also stops on any manual camera input or when a navigation tool runs. Say the RESULTING state when confirming ("Orbiting slowly"). Globe rotation (rotate_globe) spins Earth right continuously by default; explicit direction, mode and speed take precedence. It checks fit and may refuse.',
|
||||
$position: 1,
|
||||
parameters: {
|
||||
properties: {
|
||||
direction: {
|
||||
description:
|
||||
'Required except for orbit (defaults right/clockwise) and stop.',
|
||||
'Required for pan/tilt/rotate; orbit/rotate_globe default right.',
|
||||
$position: 2,
|
||||
},
|
||||
mode: {
|
||||
description:
|
||||
'once = bounded eased nudge (default); continuous = until stop/manual input.',
|
||||
'once = bounded eased nudge; continuous = until stop/manual input. rotate_globe defaults continuous; other motions default once.',
|
||||
$position: 2,
|
||||
},
|
||||
},
|
||||
|
||||
@@ -5,7 +5,7 @@ import { readResponseTextCapped } from '../common/http.js';
|
||||
import {
|
||||
normalizeRadioBrowserStation,
|
||||
publicRadioStation,
|
||||
cleanRadioText,
|
||||
normalizeRadioTag,
|
||||
} from './stations.js';
|
||||
import {
|
||||
radioMirrorOrigin,
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
RADIO_USER_AGENT,
|
||||
RADIO_UUID_RE,
|
||||
RADIO_FALLBACK_MIRRORS,
|
||||
RADIO_REJECTED_STATION_COUNT_MAX,
|
||||
} from './constants.js';
|
||||
async function mapRadioConcurrent(values, concurrency, mapper) {
|
||||
const results = new Array(values.length);
|
||||
@@ -177,29 +178,25 @@ export function createRadioProxyMiddleware({
|
||||
const rows = await fetchPath(`/json/stations/search?${params}`);
|
||||
if (!Array.isArray(rows))
|
||||
throw new Error('Radio Browser catalog payload was not an array');
|
||||
if (
|
||||
!rows.every(
|
||||
(row) =>
|
||||
row &&
|
||||
typeof row === 'object' &&
|
||||
!Array.isArray(row) &&
|
||||
typeof row.stationuuid === 'string' &&
|
||||
typeof row.name === 'string' &&
|
||||
(typeof row.url_resolved === 'string' ||
|
||||
typeof row.url === 'string'),
|
||||
)
|
||||
)
|
||||
throw new Error(
|
||||
'Radio Browser catalog contained a malformed station row',
|
||||
);
|
||||
const stations = rows
|
||||
const structuralRows = rows.filter(
|
||||
(row) =>
|
||||
row &&
|
||||
typeof row === 'object' &&
|
||||
!Array.isArray(row) &&
|
||||
typeof row.stationuuid === 'string' &&
|
||||
typeof row.name === 'string' &&
|
||||
(typeof row.url_resolved === 'string' ||
|
||||
typeof row.url === 'string'),
|
||||
);
|
||||
// Malformed rows are skipped individually, but counted so the
|
||||
// catalog keeps a diagnostic for upstream schema drift. Policy
|
||||
// rejections in normalization (offline, non-HTTPS, codec) are normal
|
||||
// filtering and are not counted.
|
||||
const rejectedRows = rows.length - structuralRows.length;
|
||||
const stations = structuralRows
|
||||
.map(normalizeRadioBrowserStation)
|
||||
.filter(Boolean);
|
||||
const requestedTag = cleanRadioText(tag, 80)
|
||||
.toLocaleLowerCase()
|
||||
.replace(/[_-]+/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.trim();
|
||||
const requestedTag = normalizeRadioTag(tag);
|
||||
const requestedTagCovered =
|
||||
!requestedTag ||
|
||||
stations.some((station) =>
|
||||
@@ -215,9 +212,10 @@ export function createRadioProxyMiddleware({
|
||||
// also contain an accepted station tagged for the requested category.
|
||||
succeeded: stations.length > 0 && requestedTagCovered,
|
||||
stations,
|
||||
rejectedRows,
|
||||
};
|
||||
} catch {
|
||||
return { succeeded: false, stations: [] };
|
||||
return { succeeded: false, stations: [], rejectedRows: 0 };
|
||||
}
|
||||
},
|
||||
);
|
||||
@@ -257,6 +255,10 @@ export function createRadioProxyMiddleware({
|
||||
if (selected.length < RADIO_CATALOG_HEALTHY_MIN_STATIONS)
|
||||
healthReasons.push('station-coverage-below-policy');
|
||||
const degraded = healthReasons.length > 0;
|
||||
const rejectedStationCount = Math.min(
|
||||
RADIO_REJECTED_STATION_COUNT_MAX,
|
||||
outcomes.reduce((total, outcome) => total + outcome.rejectedRows, 0),
|
||||
);
|
||||
const coverage = {
|
||||
successfulQueries,
|
||||
totalQueries: queries.length,
|
||||
@@ -271,6 +273,9 @@ export function createRadioProxyMiddleware({
|
||||
degraded,
|
||||
degradedReason: degraded ? healthReasons.join(',') : null,
|
||||
coverage,
|
||||
// Diagnostic only: it never fails health policy, so a usable generation
|
||||
// with a few skipped rows is still admitted. The client reports it.
|
||||
rejectedStationCount,
|
||||
};
|
||||
if (degraded && catalogCache) {
|
||||
const error = new Error(
|
||||
@@ -360,6 +365,7 @@ export function createRadioProxyMiddleware({
|
||||
coverage: catalog.coverage || null,
|
||||
acceptedGeneration: catalog.acceptedGeneration ?? null,
|
||||
catalogInstance,
|
||||
rejectedStationCount: catalog.rejectedStationCount ?? 0,
|
||||
});
|
||||
} catch (error) {
|
||||
sendJson(res, 503, {
|
||||
|
||||
@@ -11,6 +11,9 @@ export const RADIO_CATALOG_HEALTHY_MIN_STATIONS = Math.ceil(
|
||||
export const RADIO_USER_AGENT =
|
||||
'GodsEyeView/1.0 (Radio Browser directory client)';
|
||||
export { RADIO_UUID_RE } from '../../../src/sources/radioBrowser.js';
|
||||
// Upper bound for the structurally malformed upstream rows reported in the
|
||||
// /stations envelope. The client layer validates against the same bound.
|
||||
export const RADIO_REJECTED_STATION_COUNT_MAX = 10_000;
|
||||
export const RADIO_FALLBACK_MIRRORS = Object.freeze([
|
||||
'https://de1.api.radio-browser.info',
|
||||
'https://de2.api.radio-browser.info',
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
export {
|
||||
cleanRadioText,
|
||||
normalizeRadioTag,
|
||||
isNonGlobalIpv4,
|
||||
publicRadioHttpsUrl,
|
||||
normalizeRadioBrowserStation,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import path from 'node:path';
|
||||
import { promises as fsp } from 'node:fs';
|
||||
import { celestrakTleUrl } from '../../../src/data/spaceProviderRequests.js';
|
||||
import { providerCooldownMs } from './cooldown.js';
|
||||
import { admitSameSiteKeyless } from '../common/same-site.js';
|
||||
import { readResponseTextCapped } from '../common/http.js';
|
||||
|
||||
/**
|
||||
@@ -24,6 +26,18 @@ import { readResponseTextCapped } from '../common/http.js';
|
||||
export function celestrakProxy() {
|
||||
const TLE_TTL_MS = 6 * 3600_000;
|
||||
const CACHE_DIR = path.join(process.cwd(), '.gev-cache');
|
||||
const groups = new Set([
|
||||
'stations',
|
||||
'visual',
|
||||
'gps-ops',
|
||||
'glo-ops',
|
||||
'galileo',
|
||||
'geo',
|
||||
'starlink',
|
||||
// Space Missions (`getActiveTle`) reads the full active catalog.
|
||||
'active',
|
||||
]);
|
||||
let cooldownUntil = 0;
|
||||
const mem = new Map(); // group -> { at: epochMs, body: string }
|
||||
const inflight = new Map(); // group -> Promise<{at, body}|null>
|
||||
|
||||
@@ -64,7 +78,16 @@ export function celestrakProxy() {
|
||||
'gods-eye-view-celestrak-proxy/1.0 (+https://github.com/bilawalsidhu/gods-eye-view)',
|
||||
},
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
if (!res.ok) {
|
||||
// Group refreshes run in parallel: a later failure without Retry-After
|
||||
// must never shorten a longer deadline an earlier one already set.
|
||||
cooldownUntil = Math.max(
|
||||
cooldownUntil,
|
||||
Date.now() + providerCooldownMs(res),
|
||||
);
|
||||
void res.body?.cancel().catch(() => {});
|
||||
throw new Error(`HTTP ${res.status}`);
|
||||
}
|
||||
const body = await readResponseTextCapped(
|
||||
res,
|
||||
CELESTRAK_MAX_RESPONSE_BYTES,
|
||||
@@ -80,12 +103,20 @@ export function celestrakProxy() {
|
||||
const group = String(req.url || '')
|
||||
.replace(/^\//, '')
|
||||
.split('?')[0];
|
||||
if (!/^[a-z0-9-]+$/i.test(group)) {
|
||||
// Keyless and needed to render the satellites layer: refuse cross-site
|
||||
// browsers but keep the documented reverse-proxy deployment working.
|
||||
if (admitSameSiteKeyless(req, res)) return;
|
||||
if (req.method !== 'GET') {
|
||||
res.writeHead(405);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
if (!groups.has(group)) {
|
||||
res.writeHead(400, { 'Content-Type': 'text/plain' });
|
||||
res.end('invalid group');
|
||||
return;
|
||||
}
|
||||
const send = (status, body, cacheStatus) => {
|
||||
const send = (status, body, cacheStatus, entry) => {
|
||||
// Guard against a double-send (e.g. a throw AFTER a response already
|
||||
// went out routing into the catch's send): writeHead after headersSent
|
||||
// throws "Cannot set headers after they are sent".
|
||||
@@ -93,6 +124,17 @@ export function celestrakProxy() {
|
||||
res.writeHead(status, {
|
||||
'Content-Type': 'text/plain',
|
||||
'x-tle-cache': cacheStatus,
|
||||
'Cache-Control': 'no-store',
|
||||
...(entry
|
||||
? { 'X-TLE-Cached-At': new Date(entry.at).toISOString() }
|
||||
: {}),
|
||||
...(cooldownUntil > 0 && Date.now() < cooldownUntil
|
||||
? {
|
||||
'Retry-After': String(
|
||||
Math.ceil((cooldownUntil - Date.now()) / 1000),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
});
|
||||
res.end(body);
|
||||
};
|
||||
@@ -104,7 +146,16 @@ export function celestrakProxy() {
|
||||
if (entry) mem.set(group, entry);
|
||||
}
|
||||
if (entry && now - entry.at < TLE_TTL_MS) {
|
||||
send(200, entry.body, 'HIT');
|
||||
send(200, entry.body, 'HIT', entry);
|
||||
return;
|
||||
}
|
||||
if (Date.now() < cooldownUntil) {
|
||||
send(
|
||||
entry ? 200 : 502,
|
||||
entry?.body || 'celestrak upstream cooling down',
|
||||
entry ? 'STALE-ERROR' : 'NONE',
|
||||
entry,
|
||||
);
|
||||
return;
|
||||
}
|
||||
// Stale or missing → refresh, single-flight per group.
|
||||
@@ -118,6 +169,8 @@ export function celestrakProxy() {
|
||||
return fresh;
|
||||
})
|
||||
.catch((err) => {
|
||||
if (Date.now() >= cooldownUntil)
|
||||
cooldownUntil = Date.now() + 60000;
|
||||
console.warn(
|
||||
'[celestrak-proxy] refresh failed — serving cache if any',
|
||||
);
|
||||
@@ -128,9 +181,9 @@ export function celestrakProxy() {
|
||||
}
|
||||
const fresh = await inflight.get(group);
|
||||
if (fresh) {
|
||||
send(200, fresh.body, 'MISS');
|
||||
send(200, fresh.body, 'MISS', fresh);
|
||||
} else if (entry) {
|
||||
send(200, entry.body, 'STALE-ERROR'); // upstream down — stale beats empty
|
||||
send(200, entry.body, 'STALE-ERROR', entry); // upstream down — stale beats empty
|
||||
} else {
|
||||
send(502, 'celestrak fetch failed and no cache available', 'NONE');
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
/** Bound an upstream Retry-After delay (seconds or HTTP date). */
|
||||
export function providerCooldownMs(response, now = Date.now()) {
|
||||
const raw = response?.headers?.get('retry-after')?.trim();
|
||||
const seconds = raw && /^\d+(?:\.\d+)?$/.test(raw) ? Number(raw) : NaN;
|
||||
const deadline = raw && !Number.isFinite(seconds) ? Date.parse(raw) : NaN;
|
||||
const delay = Number.isFinite(seconds)
|
||||
? seconds * 1000
|
||||
: Number.isFinite(deadline)
|
||||
? deadline - now
|
||||
: 60000;
|
||||
return Math.min(30 * 60000, Math.max(5000, delay));
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import path from 'node:path';
|
||||
import { providerCooldownMs } from './cooldown.js';
|
||||
import { promises as fsp } from 'node:fs';
|
||||
import {
|
||||
readResponseTextCapped,
|
||||
@@ -30,6 +31,8 @@ export function rocketLaunchesProxy() {
|
||||
'launch-library-2-v2.3.json',
|
||||
);
|
||||
let cache = null;
|
||||
let cooldownUntil = 0;
|
||||
let cooldownStatus = 502;
|
||||
let diskLoaded = false;
|
||||
const inFlight = new Map();
|
||||
|
||||
@@ -62,7 +65,18 @@ export function rocketLaunchesProxy() {
|
||||
function send(res, status, body, cacheState) {
|
||||
res.writeHead(status, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': status === 200 ? 'public, max-age=900' : 'no-store',
|
||||
'Cache-Control':
|
||||
status === 200 && cacheState !== 'STALE-ERROR'
|
||||
? 'public, max-age=900'
|
||||
: 'no-store',
|
||||
...(cache ? { 'X-GEV-Cached-At': new Date(cache.at).toISOString() } : {}),
|
||||
...(Date.now() < cooldownUntil
|
||||
? {
|
||||
'Retry-After': String(
|
||||
Math.ceil((cooldownUntil - Date.now()) / 1000),
|
||||
),
|
||||
}
|
||||
: {}),
|
||||
'X-GEV-Cache': cacheState,
|
||||
});
|
||||
res.end(body);
|
||||
@@ -75,17 +89,25 @@ export function rocketLaunchesProxy() {
|
||||
signal: AbortSignal.timeout(20000),
|
||||
headers: launchLibraryRequestHeaders(),
|
||||
});
|
||||
const body = await readResponseTextCapped(upstream, maxResponseBytes);
|
||||
if (!upstream.ok) {
|
||||
const deadline = Date.now() + providerCooldownMs(upstream);
|
||||
// Never shorten a longer cooldown that is already in force.
|
||||
if (deadline >= cooldownUntil) {
|
||||
cooldownUntil = deadline;
|
||||
cooldownStatus = upstream.status;
|
||||
}
|
||||
void upstream.body?.cancel().catch(() => {});
|
||||
const error = new Error(`upstream HTTP ${upstream.status}`);
|
||||
error.upstreamStatus = upstream.status;
|
||||
throw error;
|
||||
}
|
||||
const body = await readResponseTextCapped(upstream, maxResponseBytes);
|
||||
const parsed = JSON.parse(body);
|
||||
if (!Array.isArray(parsed?.results))
|
||||
throw new Error('malformed upstream response');
|
||||
const fresh = { at: Date.now(), body };
|
||||
cache = fresh;
|
||||
cooldownUntil = 0;
|
||||
void saveDiskCache(fresh);
|
||||
return fresh;
|
||||
}
|
||||
@@ -103,6 +125,16 @@ export function rocketLaunchesProxy() {
|
||||
return;
|
||||
}
|
||||
const stale = cache;
|
||||
if (now < cooldownUntil) {
|
||||
send(
|
||||
res,
|
||||
stale ? 200 : cooldownStatus,
|
||||
stale?.body ||
|
||||
JSON.stringify({ error: 'Launch Library 2 unavailable' }),
|
||||
stale ? 'STALE-ERROR' : 'NONE',
|
||||
);
|
||||
return;
|
||||
}
|
||||
const request = coalesceProxyRequest(
|
||||
inFlight,
|
||||
'recent-launches',
|
||||
@@ -112,6 +144,10 @@ export function rocketLaunchesProxy() {
|
||||
const fresh = await request.promise;
|
||||
send(res, 200, fresh.body, request.shared ? 'INFLIGHT' : 'MISS');
|
||||
} catch (error) {
|
||||
if (Date.now() >= cooldownUntil) {
|
||||
cooldownUntil = Date.now() + 60000;
|
||||
cooldownStatus = 502;
|
||||
}
|
||||
// Log only a bounded status, never upstream bodies, URLs, or credentials.
|
||||
const status = Number.isInteger(error?.upstreamStatus)
|
||||
? error.upstreamStatus
|
||||
|
||||
Reference in new issue
Block a user