mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
The fallback keeps the catalog position but takes heading, field of view and pitch from the request again, as before, clamped as before. A catalog camera with no pose (every world-cameras row on main) otherwise asked Google for heading 0, pitch 0, FOV 80 while the browser draws it at the id-hash heading, pitch -17 and FOV 74, and a calibrated camera lost its calibration. Cost and the per-IP budget are unchanged. - The Street View budget is built per proxy, so tests no longer depend on GEV_RATELIMIT_GOOGLE_PER_MIN in the shell; the fixture pins it. - New tests: a pose-less catalog camera, and the per-IP budget (no call without a key, the cap, a separate budget per address). - The client no longer sends lat/lon on /frame. - CURRENT-STATE, the changelog fragment and SECURITY.md say the fallback has its own per-client limit, and that unlisted (seed) cameras get the placeholder.
78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
import {
|
|
ACTIVE_FRAME_REFRESH_MS,
|
|
FRAME_ENDPOINT,
|
|
MEDIA_ENDPOINT,
|
|
} from './sourcePolicy.js';
|
|
const FRAME_SOURCE_KINDS = new Set([
|
|
'upstream-image',
|
|
'hls-pull',
|
|
'streetview',
|
|
'synthetic',
|
|
]);
|
|
function safeNumber(value, fallback = NaN) {
|
|
const n = Number(value);
|
|
return Number.isFinite(n) ? n : fallback;
|
|
}
|
|
function frameUrlFor(camera, refreshMs = ACTIVE_FRAME_REFRESH_MS) {
|
|
const cadenceMs = Math.max(
|
|
1000,
|
|
safeNumber(refreshMs, ACTIVE_FRAME_REFRESH_MS),
|
|
);
|
|
const tick = Math.floor(Date.now() / cadenceMs);
|
|
const params = new URLSearchParams({
|
|
label: camera.name,
|
|
city: camera.city,
|
|
heading: String(Math.round(camera.headingDeg)),
|
|
fov: String(Math.round(camera.fovDeg)),
|
|
pitch: String(Math.round(camera.pitchDeg || -10)),
|
|
ts: String(tick),
|
|
});
|
|
return `${FRAME_ENDPOINT}/${encodeURIComponent(camera.id)}?${params.toString()}`;
|
|
}
|
|
function mediaUrlFor(camera) {
|
|
return `${MEDIA_ENDPOINT}/${encodeURIComponent(camera.id)}?ts=${Math.floor(Date.now() / 15000)}`;
|
|
}
|
|
/** Supply catalog/health records and the existing registered camera URL families. */
|
|
export function createCctvSource({
|
|
fetchImpl = (...args) => globalThis.fetch(...args),
|
|
} = {}) {
|
|
async function read(path, key, { signal } = {}) {
|
|
signal?.throwIfAborted();
|
|
const response = await fetchImpl(path, { cache: 'no-store', signal });
|
|
if (!response.ok) throw new Error('Camera source HTTP ' + response.status);
|
|
const payload = await response.json();
|
|
signal?.throwIfAborted();
|
|
if (!Array.isArray(payload?.[key]))
|
|
throw new Error('Malformed camera ' + key + ' snapshot');
|
|
return payload;
|
|
}
|
|
return {
|
|
getCatalog(options) {
|
|
return read('/api/cctv/sources', 'sources', options);
|
|
},
|
|
getHealth(options) {
|
|
return read('/api/cctv/health', 'cameras', options);
|
|
},
|
|
getFrameUrl: frameUrlFor,
|
|
getMediaUrl: mediaUrlFor,
|
|
/** Read a registered frame and its provenance, including fallback frames. */
|
|
async getFrame(camera, { signal } = {}) {
|
|
signal?.throwIfAborted();
|
|
const response = await fetchImpl(frameUrlFor(camera), {
|
|
cache: 'no-store',
|
|
signal,
|
|
});
|
|
if (!response.ok) throw new Error('Camera frame HTTP ' + response.status);
|
|
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
signal?.throwIfAborted();
|
|
const type = response.headers.get('content-type') || '';
|
|
const sourceKind = response.headers.get('x-cctv-source');
|
|
return {
|
|
contentType: type.split(';')[0].trim().toLowerCase(),
|
|
bytes,
|
|
sourceKind: FRAME_SOURCE_KINDS.has(sourceKind) ? sourceKind : null,
|
|
};
|
|
},
|
|
};
|
|
}
|