mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-04 10:24:02 +00:00
style: format terrain traffic fire and bike-share modules
This commit is contained in:
1 parent
f4054469d5
commit
a2d6461b52
13 files changed
+638
-213
No files matched your search
+43
-11
@@ -49,10 +49,16 @@ export function firmsProxy() {
|
||||
diskChecked = true;
|
||||
try {
|
||||
const parsed = JSON.parse(await fsp.readFile(CACHE_PATH, 'utf8'));
|
||||
if (Number.isFinite(parsed?.at) && Array.isArray(parsed?.sources) && Array.isArray(parsed?.fires)) {
|
||||
if (
|
||||
Number.isFinite(parsed?.at) &&
|
||||
Array.isArray(parsed?.sources) &&
|
||||
Array.isArray(parsed?.fires)
|
||||
) {
|
||||
mem = parsed;
|
||||
}
|
||||
} catch { /* no disk cache yet */ }
|
||||
} catch {
|
||||
/* no disk cache yet */
|
||||
}
|
||||
}
|
||||
|
||||
async function writeDisk(entry) {
|
||||
@@ -97,7 +103,10 @@ export function firmsProxy() {
|
||||
for (const record of records) fires.push(record);
|
||||
sources.push({ source, count: records.length, ok: true });
|
||||
} catch (err) {
|
||||
console.warn(`[firms-proxy] ${source} fetch failed:`, err?.message || err);
|
||||
console.warn(
|
||||
`[firms-proxy] ${source} fetch failed:`,
|
||||
err?.message || err,
|
||||
);
|
||||
sources.push({ source, count: 0, ok: false });
|
||||
}
|
||||
}
|
||||
@@ -136,9 +145,14 @@ export function firmsProxy() {
|
||||
const body = await res.json();
|
||||
const used = Number(body?.current_transactions);
|
||||
const limit = Number(body?.transaction_limit);
|
||||
return Number.isFinite(used) && Number.isFinite(limit) ? { used, limit } : null;
|
||||
return Number.isFinite(used) && Number.isFinite(limit)
|
||||
? { used, limit }
|
||||
: null;
|
||||
} catch (err) {
|
||||
console.warn('[firms-proxy] mapkey status failed:', err?.message || err);
|
||||
console.warn(
|
||||
'[firms-proxy] mapkey status failed:',
|
||||
err?.message || err,
|
||||
);
|
||||
return null;
|
||||
}
|
||||
})()
|
||||
@@ -146,7 +160,9 @@ export function firmsProxy() {
|
||||
statusCache = { at: Date.now(), transactions };
|
||||
return transactions;
|
||||
})
|
||||
.finally(() => { statusInflight = null; });
|
||||
.finally(() => {
|
||||
statusInflight = null;
|
||||
});
|
||||
}
|
||||
return statusInflight;
|
||||
}
|
||||
@@ -157,7 +173,10 @@ export function firmsProxy() {
|
||||
server.middlewares.use('/api/firms', async (req, res) => {
|
||||
const sendJson = (status, obj) => {
|
||||
if (res.headersSent) return;
|
||||
res.writeHead(status, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(status, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify(obj));
|
||||
};
|
||||
try {
|
||||
@@ -167,7 +186,14 @@ export function firmsProxy() {
|
||||
|
||||
if (subPath === '/status') {
|
||||
if (!key) {
|
||||
sendJson(200, { hasKey: false, lastFetch: null, count: null, stale: false, ttlMs: TTL_MS, transactions: null });
|
||||
sendJson(200, {
|
||||
hasKey: false,
|
||||
lastFetch: null,
|
||||
count: null,
|
||||
stale: false,
|
||||
ttlMs: TTL_MS,
|
||||
transactions: null,
|
||||
});
|
||||
return;
|
||||
}
|
||||
const transactions = await getTransactions(key);
|
||||
@@ -203,10 +229,14 @@ export function firmsProxy() {
|
||||
return fresh;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(`[firms-proxy] refresh failed (${err?.message || err}) — serving cache if any`);
|
||||
console.warn(
|
||||
`[firms-proxy] refresh failed (${err?.message || err}) — serving cache if any`,
|
||||
);
|
||||
return null;
|
||||
})
|
||||
.finally(() => { inflight = null; });
|
||||
.finally(() => {
|
||||
inflight = null;
|
||||
});
|
||||
}
|
||||
const pending = inflight;
|
||||
const fresh = await pending;
|
||||
@@ -215,7 +245,9 @@ export function firmsProxy() {
|
||||
} else if (entry) {
|
||||
sendJson(200, buildPayload(entry, true)); // upstream down — stale beats empty
|
||||
} else {
|
||||
sendJson(502, { error: 'firms fetch failed and no cache available' });
|
||||
sendJson(502, {
|
||||
error: 'firms fetch failed and no cache available',
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[firms-proxy] error:', err?.message || err);
|
||||
|
||||
+74
-19
@@ -1,4 +1,8 @@
|
||||
import { isAllowedGbfsHost, isAllowedGbfsPath, gbfsCacheControl } from '../../src/data/gbfsSource.js';
|
||||
import {
|
||||
isAllowedGbfsHost,
|
||||
isAllowedGbfsPath,
|
||||
gbfsCacheControl,
|
||||
} from '../../src/data/gbfsSource.js';
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// GBFS (General Bikeshare Feed Specification) proxy constants
|
||||
@@ -23,7 +27,10 @@ export function gbfsProxy() {
|
||||
server.middlewares.use('/api/gbfs', async (req, res) => {
|
||||
try {
|
||||
if (req.method !== 'GET') {
|
||||
res.writeHead(405, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(405, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'Method Not Allowed' }));
|
||||
return;
|
||||
}
|
||||
@@ -31,7 +38,10 @@ export function gbfsProxy() {
|
||||
const url = new URL(req.url || '/', 'http://localhost');
|
||||
const encodedTarget = url.pathname.replace(/^\/+/, '');
|
||||
if (!encodedTarget) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(400, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'Missing GBFS upstream target' }));
|
||||
return;
|
||||
}
|
||||
@@ -40,7 +50,10 @@ export function gbfsProxy() {
|
||||
try {
|
||||
decodedTarget = decodeURIComponent(encodedTarget);
|
||||
} catch {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(400, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'Invalid GBFS target encoding' }));
|
||||
return;
|
||||
}
|
||||
@@ -49,31 +62,53 @@ export function gbfsProxy() {
|
||||
try {
|
||||
upstreamUrl = new URL(decodedTarget);
|
||||
} catch {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(400, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'Invalid GBFS upstream URL' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (upstreamUrl.protocol !== 'https:') {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.end(JSON.stringify({ error: 'Only https GBFS targets are allowed' }));
|
||||
res.writeHead(400, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(
|
||||
JSON.stringify({ error: 'Only https GBFS targets are allowed' }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isAllowedGbfsHost(upstreamUrl.hostname)) {
|
||||
res.writeHead(403, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(403, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'GBFS host not allowed' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isAllowedGbfsPath(upstreamUrl.pathname)) {
|
||||
res.writeHead(400, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.end(JSON.stringify({ error: 'Only station_information/station_status endpoints are allowed' }));
|
||||
res.writeHead(400, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
error:
|
||||
'Only station_information/station_status endpoints are allowed',
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const controller = new AbortController();
|
||||
const timeoutId = setTimeout(() => controller.abort(), GBFS_PROXY_TIMEOUT_MS);
|
||||
const timeoutId = setTimeout(
|
||||
() => controller.abort(),
|
||||
GBFS_PROXY_TIMEOUT_MS,
|
||||
);
|
||||
let upstream;
|
||||
try {
|
||||
upstream = await fetch(upstreamUrl.toString(), {
|
||||
@@ -91,18 +126,32 @@ export function gbfsProxy() {
|
||||
// Limit response size to prevent memory exhaustion from malicious upstream
|
||||
const GBFS_MAX_BODY_BYTES = 5 * 1024 * 1024; // 5 MB
|
||||
const contentLength = Number(upstream.headers.get('content-length'));
|
||||
if (Number.isFinite(contentLength) && contentLength > GBFS_MAX_BODY_BYTES) {
|
||||
res.writeHead(502, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.end(JSON.stringify({ error: 'GBFS upstream response too large' }));
|
||||
if (
|
||||
Number.isFinite(contentLength) &&
|
||||
contentLength > GBFS_MAX_BODY_BYTES
|
||||
) {
|
||||
res.writeHead(502, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(
|
||||
JSON.stringify({ error: 'GBFS upstream response too large' }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const body = await upstream.text();
|
||||
if (Buffer.byteLength(body, 'utf8') > GBFS_MAX_BODY_BYTES) {
|
||||
res.writeHead(502, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.end(JSON.stringify({ error: 'GBFS upstream response too large' }));
|
||||
res.writeHead(502, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(
|
||||
JSON.stringify({ error: 'GBFS upstream response too large' }),
|
||||
);
|
||||
return;
|
||||
}
|
||||
const contentType = upstream.headers.get('content-type') || 'application/json';
|
||||
const contentType =
|
||||
upstream.headers.get('content-type') || 'application/json';
|
||||
res.writeHead(upstream.status, {
|
||||
'Content-Type': contentType,
|
||||
'Cache-Control': gbfsCacheControl(upstreamUrl.pathname),
|
||||
@@ -112,12 +161,18 @@ export function gbfsProxy() {
|
||||
res.end(body);
|
||||
} catch (error) {
|
||||
if (error?.name === 'AbortError') {
|
||||
res.writeHead(504, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(504, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'GBFS upstream timeout' }));
|
||||
return;
|
||||
}
|
||||
console.error('[GBFS Proxy]', error?.message || String(error));
|
||||
res.writeHead(502, { 'Content-Type': 'application/json', 'Cache-Control': 'no-store' });
|
||||
res.writeHead(502, {
|
||||
'Content-Type': 'application/json',
|
||||
'Cache-Control': 'no-store',
|
||||
});
|
||||
res.end(JSON.stringify({ error: 'GBFS proxy error' }));
|
||||
}
|
||||
});
|
||||
|
||||
+37
-16
@@ -39,12 +39,19 @@ export function terrainHeightsProxy() {
|
||||
diskLoaded = true;
|
||||
try {
|
||||
const parsed = JSON.parse(await fsp.readFile(CACHE_PATH, 'utf8'));
|
||||
const pointEntries = parsed?.version === 2 && parsed.points && typeof parsed.points === 'object'
|
||||
? parsed.points
|
||||
: null;
|
||||
const pointEntries =
|
||||
parsed?.version === 2 &&
|
||||
parsed.points &&
|
||||
typeof parsed.points === 'object'
|
||||
? parsed.points
|
||||
: null;
|
||||
if (pointEntries) {
|
||||
for (const [key, entry] of Object.entries(pointEntries)) {
|
||||
if (entry && Number.isFinite(entry.at) && validTerrainResult(entry.result)) {
|
||||
if (
|
||||
entry &&
|
||||
Number.isFinite(entry.at) &&
|
||||
validTerrainResult(entry.result)
|
||||
) {
|
||||
mem.set(key, entry);
|
||||
}
|
||||
}
|
||||
@@ -53,18 +60,27 @@ export function terrainHeightsProxy() {
|
||||
// positionally present results; an absent value never becomes height 0.
|
||||
for (const [rawPoints, entry] of Object.entries(parsed)) {
|
||||
const points = parseTerrainPoints(rawPoints);
|
||||
if (!points || !entry || !Number.isFinite(entry.at) || !Array.isArray(entry.results)) continue;
|
||||
if (
|
||||
!points ||
|
||||
!entry ||
|
||||
!Number.isFinite(entry.at) ||
|
||||
!Array.isArray(entry.results)
|
||||
)
|
||||
continue;
|
||||
for (let i = 0; i < points.length; i += 1) {
|
||||
const result = entry.results[i];
|
||||
if (!validTerrainResult(result)) continue;
|
||||
const key = terrainPointKey(points[i]);
|
||||
const existing = mem.get(key);
|
||||
if (!existing || entry.at > existing.at) mem.set(key, { at: entry.at, result });
|
||||
if (!existing || entry.at > existing.at)
|
||||
mem.set(key, { at: entry.at, result });
|
||||
}
|
||||
}
|
||||
diskDirty = mem.size > 0;
|
||||
}
|
||||
} catch { /* no disk cache yet */ }
|
||||
} catch {
|
||||
/* no disk cache yet */
|
||||
}
|
||||
// Periodic flush, same shape as adsbdbProxy: coalesce writes instead of
|
||||
// hitting disk on every request.
|
||||
setInterval(async () => {
|
||||
@@ -97,7 +113,8 @@ export function terrainHeightsProxy() {
|
||||
const chunkResults = await fetchTerrainChunkWithRetry(chunk);
|
||||
// Keep later chunks aligned even if a malformed upstream response omits
|
||||
// trailing positions. The resolver will reject each null individually.
|
||||
for (let j = 0; j < chunk.length; j += 1) results.push(chunkResults[j] ?? null);
|
||||
for (let j = 0; j < chunk.length; j += 1)
|
||||
results.push(chunkResults[j] ?? null);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
@@ -106,10 +123,9 @@ export function terrainHeightsProxy() {
|
||||
function fetchMissingSingleFlight(points) {
|
||||
const key = points.map(terrainPointKey).join(';');
|
||||
if (!inflight.has(key)) {
|
||||
const request = fetchUpstreamAll(points)
|
||||
.finally(() => {
|
||||
if (inflight.get(key) === request) inflight.delete(key);
|
||||
});
|
||||
const request = fetchUpstreamAll(points).finally(() => {
|
||||
if (inflight.get(key) === request) inflight.delete(key);
|
||||
});
|
||||
inflight.set(key, request);
|
||||
}
|
||||
return inflight.get(key);
|
||||
@@ -130,11 +146,16 @@ export function terrainHeightsProxy() {
|
||||
const rawPoints = parsedUrl.searchParams.get('points');
|
||||
const points = parseTerrainPoints(rawPoints);
|
||||
if (!points) {
|
||||
send(400, { error: 'invalid points parameter — expected "lon,lat;lon,lat;…" with finite numbers' });
|
||||
send(400, {
|
||||
error:
|
||||
'invalid points parameter — expected "lon,lat;lon,lat;…" with finite numbers',
|
||||
});
|
||||
return;
|
||||
}
|
||||
if (points.length > MAX_POINTS) {
|
||||
send(500, { error: `too many points (${points.length}); max ${MAX_POINTS} per request` });
|
||||
send(500, {
|
||||
error: `too many points (${points.length}); max ${MAX_POINTS} per request`,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -147,8 +168,8 @@ export function terrainHeightsProxy() {
|
||||
if (outcome.cacheChanged) diskDirty = true;
|
||||
if (outcome.upstreamError) {
|
||||
console.warn(
|
||||
'[terrain-heights-proxy] refresh incomplete'
|
||||
+ ' — serving stale points when available'
|
||||
'[terrain-heights-proxy] refresh incomplete' +
|
||||
' — serving stale points when available',
|
||||
);
|
||||
}
|
||||
send(outcome.status, outcome.body);
|
||||
|
||||
+46
-21
@@ -60,10 +60,16 @@ export function tomtomProxy() {
|
||||
budgetLoaded = true;
|
||||
try {
|
||||
const parsed = JSON.parse(await fsp.readFile(BUDGET_PATH, 'utf8'));
|
||||
if (parsed && typeof parsed.date === 'string' && Number.isFinite(parsed.count)) {
|
||||
if (
|
||||
parsed &&
|
||||
typeof parsed.date === 'string' &&
|
||||
Number.isFinite(parsed.count)
|
||||
) {
|
||||
budget = parsed;
|
||||
}
|
||||
} catch { /* no budget file yet */ }
|
||||
} catch {
|
||||
/* no budget file yet */
|
||||
}
|
||||
}
|
||||
|
||||
async function persistBudget() {
|
||||
@@ -87,7 +93,8 @@ export function tomtomProxy() {
|
||||
void persistBudget();
|
||||
}
|
||||
|
||||
const tilePath = (key) => path.join(CACHE_DIR, `flow-${key.replaceAll('/', '-')}.pbf`);
|
||||
const tilePath = (key) =>
|
||||
path.join(CACHE_DIR, `flow-${key.replaceAll('/', '-')}.pbf`);
|
||||
|
||||
/** Disk-cache read; tile age comes from the file's mtime. */
|
||||
async function readDiskTile(key) {
|
||||
@@ -97,7 +104,9 @@ export function tomtomProxy() {
|
||||
fsp.readFile(tilePath(key)),
|
||||
]);
|
||||
return { at: stat.mtimeMs, buf };
|
||||
} catch { return null; }
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async function writeDiskTile(key, buf) {
|
||||
@@ -105,7 +114,10 @@ export function tomtomProxy() {
|
||||
await fsp.mkdir(CACHE_DIR, { recursive: true });
|
||||
await fsp.writeFile(tilePath(key), buf);
|
||||
} catch (err) {
|
||||
console.warn(`[tomtom-proxy] tile cache write failed for ${key}:`, err?.message || err);
|
||||
console.warn(
|
||||
`[tomtom-proxy] tile cache write failed for ${key}:`,
|
||||
err?.message || err,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -119,10 +131,13 @@ export function tomtomProxy() {
|
||||
}
|
||||
|
||||
async function fetchUpstream(z, x, y) {
|
||||
const url = 'https://api.tomtom.com/traffic/map/4/tile/flow/relative/'
|
||||
+ `${z}/${x}/${y}.pbf?key=${encodeURIComponent(process.env.TOMTOM_API_KEY)}`;
|
||||
const url =
|
||||
'https://api.tomtom.com/traffic/map/4/tile/flow/relative/' +
|
||||
`${z}/${x}/${y}.pbf?key=${encodeURIComponent(process.env.TOMTOM_API_KEY)}`;
|
||||
recordUpstreamFetch(); // attempts count — upstream bills the request either way
|
||||
const res = await fetch(url, { signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS) });
|
||||
const res = await fetch(url, {
|
||||
signal: AbortSignal.timeout(UPSTREAM_TIMEOUT_MS),
|
||||
});
|
||||
if (!res.ok) throw new Error(`HTTP ${res.status}`);
|
||||
const buf = Buffer.from(await res.arrayBuffer());
|
||||
if (buf.length === 0) throw new Error('empty tile body');
|
||||
@@ -161,7 +176,12 @@ export function tomtomProxy() {
|
||||
if (urlPath === '/status') {
|
||||
const hasKey = Boolean(process.env.TOMTOM_API_KEY);
|
||||
const b = currentBudget();
|
||||
sendJson(200, { hasKey, dailyCount: b.count, budget: dailyBudgetLimit(), date: b.date });
|
||||
sendJson(200, {
|
||||
hasKey,
|
||||
dailyCount: b.count,
|
||||
budget: dailyBudgetLimit(),
|
||||
date: b.date,
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -208,18 +228,23 @@ export function tomtomProxy() {
|
||||
|
||||
// Stale or missing → refresh, single-flight per tile.
|
||||
if (!inflight.has(key)) {
|
||||
inflight.set(key, fetchUpstream(z, x, y)
|
||||
.then(async (buf) => {
|
||||
const fresh = { at: Date.now(), buf };
|
||||
memSet(key, fresh);
|
||||
await writeDiskTile(key, buf);
|
||||
return fresh;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(`[tomtom-proxy] ${key} fetch failed (${err?.message || err}) — serving stale if any`);
|
||||
return null;
|
||||
})
|
||||
.finally(() => inflight.delete(key)));
|
||||
inflight.set(
|
||||
key,
|
||||
fetchUpstream(z, x, y)
|
||||
.then(async (buf) => {
|
||||
const fresh = { at: Date.now(), buf };
|
||||
memSet(key, fresh);
|
||||
await writeDiskTile(key, buf);
|
||||
return fresh;
|
||||
})
|
||||
.catch((err) => {
|
||||
console.warn(
|
||||
`[tomtom-proxy] ${key} fetch failed (${err?.message || err}) — serving stale if any`,
|
||||
);
|
||||
return null;
|
||||
})
|
||||
.finally(() => inflight.delete(key)),
|
||||
);
|
||||
}
|
||||
const fresh = await inflight.get(key);
|
||||
if (fresh) {
|
||||
|
||||
Reference in new issue
Block a user