From 1a0b9c8b0f05f8b6d7d0b892efa39e0ef76e9a2c Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Wed, 14 Oct 2026 13:08:35 +0100 Subject: [PATCH 01/23] fix(qa-cctv-v2): keep long headful waits under puppeteer's protocolTimeout MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The headful drain budget is uncapped (camCount * 800 + 30000 ms, ~50 min at N≈3,700), but each page.waitForFunction is one CDP call that puppeteer aborts after its 180 s default protocolTimeout. The .catch then recorded "timed out waiting for drain on the real-GPU sign-off path" after ~3 min. - Poll the drain in <=120 s slices (waitForFunctionSliced) so no single CDP call outlives protocolTimeout, whatever the catalog size. - Launch with protocolTimeout 240 s, headroom above the largest slice and the 60 s fixed waits. - Bound the sibling-owner 3-frame postRender wait at 10 s and report the frame count, so a renderer that stops producing frames fails with evidence instead of hanging the evaluate. - List --disable-backgrounding-occluded-windows explicitly. Puppeteer 25.10 already passes it by default, so this documents the headful contract rather than changing behaviour. Co-Authored-By: Claude Opus 5.5 --- scripts/qa-cctv-v2.mjs | 76 +++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 16 deletions(-) diff --git a/scripts/qa-cctv-v2.mjs b/scripts/qa-cctv-v2.mjs index 08995603fa..9234093bef 100644 --- a/scripts/qa-cctv-v2.mjs +++ b/scripts/qa-cctv-v2.mjs @@ -139,6 +139,41 @@ function record(name, ok, detail) { const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); +// Every page.waitForFunction / page.evaluate is a single CDP call that +// puppeteer aborts after `protocolTimeout` (default 180s). The headful drain +// budget is uncapped and reaches ~50 minutes on the full ~3,900-camera +// catalog, so long waits are polled in slices of at most WAIT_SLICE_MS and +// protocolTimeout is set with headroom above the largest slice. +const WAIT_SLICE_MS = 120000; +const PROTOCOL_TIMEOUT_MS = 2 * WAIT_SLICE_MS; + +/** + * page.waitForFunction with an overall budget that may exceed + * protocolTimeout: waits in WAIT_SLICE_MS slices until the predicate holds or + * the budget runs out. + * @param {import('puppeteer').Page} page + * @param {Function} predicate - In-page predicate. + * @param {number} budgetMs - Overall budget. + * @returns {Promise} True if the predicate held within the budget. + */ +async function waitForFunctionSliced(page, predicate, budgetMs) { + const deadline = Date.now() + budgetMs; + for (;;) { + const remaining = deadline - Date.now(); + if (remaining <= 0) return false; + const ok = await page + .waitForFunction(predicate, { + timeout: Math.min(remaining, WAIT_SLICE_MS), + }) + .then(() => true) + .catch((err) => { + if (err?.name === 'TimeoutError') return false; + throw err; + }); + if (ok) return true; + } +} + // SwiftShader can spend tens of seconds servicing the hover drillPick emitted // by page.mouse.move(), before the LEFT_DOWN under test even runs. Position the // pointer while ADJUST is disabled, then re-enable it and explicitly render a @@ -364,6 +399,7 @@ async function main() { const chromeExecutable = findChromeExecutable(); const browser = await puppeteer.launch({ headless: HEADFUL ? false : 'new', + protocolTimeout: PROTOCOL_TIMEOUT_MS, ...(chromeExecutable ? { executablePath: chromeExecutable } : {}), args: [ '--no-sandbox', @@ -373,6 +409,9 @@ async function main() { '--disable-web-security', '--disable-background-timer-throttling', '--disable-renderer-backgrounding', + // Puppeteer's defaults already include this; listed so the headful + // run keeps rendering when covered even if those defaults change. + '--disable-backgrounding-occluded-windows', '--window-size=1280,800', ], }); @@ -475,18 +514,15 @@ async function main() { console.log( `Waiting for geometry-load queue to drain (N=${camCount}, budget ${Math.round(drainBudgetMs / 1000)}s)...`, ); - const drained = await page - .waitForFunction( - () => { - const mod = - window.__godsEyeView.dataManager.layers.get('cctv').module; - const ui = mod.getUIState(); - return ui.loading && ui.loading.active === false; - }, - { timeout: drainBudgetMs }, - ) - .then(() => true) - .catch(() => false); + const drained = await waitForFunctionSliced( + page, + () => { + const mod = window.__godsEyeView.dataManager.layers.get('cctv').module; + const ui = mod.getUIState(); + return ui.loading && ui.loading.active === false; + }, + drainBudgetMs, + ); record( `geometry-load queue drains within ${Math.round(drainBudgetMs / 1000)}s (N=${camCount})`, drained ? true : HEADFUL ? false : null, @@ -1358,16 +1394,23 @@ async function main() { }, }); window.__qaCctvSiblingOwner = owner; - await new Promise((resolve) => { + // Bounded: if the browser stops producing frames (headful window + // asleep or hidden), report it instead of hanging the CDP call. + const framesRendered = await new Promise((resolve) => { let frames = 0; + const finish = () => { + clearTimeout(timer); + remove(); + resolve(frames); + }; const remove = scene.postRender.addEventListener(() => { if (++frames < 3) { scene.requestRender(); return; } - remove(); - resolve(); + finish(); }); + const timer = setTimeout(finish, 10000); scene.requestRender(); }); const rect = scene.canvas.getBoundingClientRect(); @@ -1382,6 +1425,7 @@ async function main() { typeof pickedId === 'string' ? pickedId : (pickedId?.id ?? null), ownerId: owner.id, ownsExactObject: picked?.id === owner, + framesRendered, projectionDelta: Math.hypot( projected.x - canvasPoint.x, projected.y - canvasPoint.y, @@ -1395,7 +1439,7 @@ async function main() { siblingTarget.ownsExactObject : null, siblingTarget - ? `picked=${siblingTarget.pickedId} exactOwner=${siblingTarget.ownsExactObject} projectionΔ=${siblingTarget.projectionDelta}` + ? `picked=${siblingTarget.pickedId} exactOwner=${siblingTarget.ownsExactObject} projectionΔ=${siblingTarget.projectionDelta} frames=${siblingTarget.framesRendered}/3` : 'no previously verified empty canvas ray was available', ); if ( From 7cdaf40fd036c2cf43ce5d7f89fd1583c0816538 Mon Sep 17 00:00:00 2001 From: Said Date: Mon, 19 Oct 2026 14:00:35 +0100 Subject: [PATCH 02/23] docs(cctv): retain Ontario licence attribution in batch candidate --- src/data/dataCredits.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/data/dataCredits.js b/src/data/dataCredits.js index 23f317e669..747ed18d18 100644 --- a/src/data/dataCredits.js +++ b/src/data/dataCredits.js @@ -238,9 +238,9 @@ export const DATA_CREDITS = [ key: 'ontario-511-cctv', html: 'CCTV cameras & frames (Ontario): ' + - 'Ontario 511 ' + - '(Open Government Licence - Ontario) ' + - '· provided as is, without warranty; actual road conditions may differ', + 'Ontario 511. ' + + 'Contains information licensed under the Open Government Licence – Ontario. ' + + 'Provided as is, without warranty; actual road conditions may differ.', }, { key: 'fintraffic-cctv', From 5b4f5ed4a0caa1941ba97a62edb4f6f7e2e4dcf6 Mon Sep 17 00:00:00 2001 From: Said Date: Mon, 19 Oct 2026 23:05:37 +0100 Subject: [PATCH 03/23] Fix CCTV provider cancellation and credential-safe errors --- changelog.d/1228-cctv-ontario-511-key.md | 2 + .../1233-tarktee-datex-catalog-hardening.md | 3 +- docs/CURRENT-STATE.md | 7 +- server/providers/cctv/ontario511.test.mjs | 40 +++++++ server/providers/cctv/sources.js | 107 ++++++++--------- src/data/cctvEstonia.test.mjs | 113 +++++++++++++++++- src/layers/cctv/frames.test.mjs | 31 +++++ src/layers/cctv/rendering.test.mjs | 53 ++++++++ 8 files changed, 293 insertions(+), 63 deletions(-) diff --git a/changelog.d/1228-cctv-ontario-511-key.md b/changelog.d/1228-cctv-ontario-511-key.md index d9dc7986f7..cbc3194d41 100644 --- a/changelog.d/1228-cctv-ontario-511-key.md +++ b/changelog.d/1228-cctv-ontario-511-key.md @@ -2,3 +2,5 @@ free developer key; set `ONTARIO_511_API_KEY` to enable the pack, which is otherwise skipped with one log line saying where to get a key (quanticsoul4772, #1228). +- Refuse Ontario catalog redirects and keep credential-bearing URLs and provider + exception text out of failure logs; retain the Ontario licence attribution. diff --git a/changelog.d/1233-tarktee-datex-catalog-hardening.md b/changelog.d/1233-tarktee-datex-catalog-hardening.md index e8fec8b34e..f2b2781af9 100644 --- a/changelog.d/1233-tarktee-datex-catalog-hardening.md +++ b/changelog.d/1233-tarktee-datex-catalog-hardening.md @@ -1,4 +1,5 @@ - Harden the Tarktee (Estonia) DATEX2 camera catalog fetches: refuse redirects, - cancel both response streams when either request fails or overflows the 4 MB + abort pending siblings and cancel received or late response streams when either + request fails before headers or overflows the 4 MB byte cap, retain the shared 30 s dual-XML fetch deadline, and deduplicate camera IDs before per-pack prioritization (yaka0007, #1233). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index e9b6c1df5e..a7ed660912 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -4446,7 +4446,7 @@ silently demoting every later lookup for the session. requires a developer key, so the pack needs `ONTARIO_511_API_KEY` and is skipped without one (#1228). All sources are RAW PRIOR poses, and the layer is stills-first. - sources. Fintraffic Finland road weather cameras (2026-09-13; cap 300, `CCTV_FINTRAFFIC_MAX_SOURCES`, + Fintraffic Finland road weather cameras (2026-09-13; cap 300, `CCTV_FINTRAFFIC_MAX_SOURCES`, kill switch `CCTV_FINTRAFFIC_ENABLED=0`) are the fourth pack: one keyless GeoJSON station list covering the whole country, where one _preset_ (a station's fixed view) is one camera — 806 GATHERING stations carry 2,256 in-collection presets, prioritized to 300 against seven anchors @@ -4459,6 +4459,11 @@ silently demoting every later lookup for the session. empty keeps the old catalog and is retried after another 15 minutes, not on the next request. Estonia's Tarktee DATEX feeds, which answer in 2 to 16.6 s, get 30 s (`TARKTEE_FETCH_TIMEOUT_MS`) where other packs get 15 s. + Ontario catalog requests refuse redirects and log only a fixed diagnostic for + transport/JSON failures, so reflected request URLs or keys cannot reach logs. + Tarktee owns both XML requests with one 30-second deadline and shared abort; + a failed header or body read cancels its sibling, including a response arriving + after cleanup. - **CCTV v3 UX — viewshed + calibration gizmo** (built 2026-07-05 and field validated 2026-07-21): the COVERAGE toggle is a tri-state cycle `OFF → ON → VIEWSHED`; viewshed mode renders each visible camera's frustum diff --git a/server/providers/cctv/ontario511.test.mjs b/server/providers/cctv/ontario511.test.mjs index 51334b3cba..9a8ba0e643 100644 --- a/server/providers/cctv/ontario511.test.mjs +++ b/server/providers/cctv/ontario511.test.mjs @@ -127,3 +127,43 @@ for (const status of [429, 500, 503]) { assert.doesNotMatch(lines[0], /secret-key-value/); }); } + +for (const failure of ['transport', 'invalid-json']) { + test(`Ontario ${failure} failures cannot log reflected keys or request URLs`, async (t) => { + const lines = captureConsole(t); + const key = 'synthetic secret&/=%value'; + t.mock.method(globalThis, 'fetch', async (url) => { + if (failure === 'transport') + throw new Error(`request failed: ${url} ${key}`); + return new Response(`not JSON: ${url} ${key}`); + }); + assert.deepEqual( + await loadOntarioSourcesFromOpenData({}, { ONTARIO_511_API_KEY: key }), + [], + ); + assert.equal(lines.length, 1); + assert.match(lines[0], /Ontario 511 camera download error/); + assert.equal(lines[0].includes(key), false); + assert.equal(lines[0].includes('synthetic'), false); + assert.equal(lines[0].includes('https://'), false); + }); +} + +test('Ontario encodes reserved key characters and refuses credential-bearing redirects', async (t) => { + const lines = captureConsole(t); + const key = 'synthetic &/=% value'; + let request; + t.mock.method(globalThis, 'fetch', async (url, init) => { + request = { url: new URL(url), init }; + return Response.json([ROW]); + }); + const cameras = await loadOntarioSourcesFromOpenData( + {}, + { ONTARIO_511_API_KEY: key }, + ); + assert.equal(request.url.searchParams.get('key'), key); + assert.equal(request.init.redirect, 'error'); + assert.equal(cameras.length, 1); + assert.equal(JSON.stringify(cameras).includes('synthetic'), false); + assert.equal(lines.join(' ').includes('synthetic'), false); +}); diff --git a/server/providers/cctv/sources.js b/server/providers/cctv/sources.js index 56d712898b..9681c63620 100644 --- a/server/providers/cctv/sources.js +++ b/server/providers/cctv/sources.js @@ -563,6 +563,7 @@ export async function loadOntarioSourcesFromOpenData( url.searchParams.set('key', key); const resp = await fetch(url, { headers: { Accept: 'application/json' }, + redirect: 'error', signal: AbortSignal.timeout(CCTV_SOURCE_FETCH_TIMEOUT_MS), }); if (!resp.ok) { @@ -646,10 +647,11 @@ export async function loadOntarioSourcesFromOpenData( `[CCTV] Loaded Ontario 511 camera sources: ${unique.length} enabled (using nearest ${prioritized.length})`, ); return prioritized; - } catch (error) { + } catch { + // Transport and JSON errors may reflect the credential-bearing request. + // Do not log upstream exception text, response bodies or URLs. console.warn( - '[CCTV] Ontario 511 camera download error:', - error?.message || error, + '[CCTV] Ontario 511 camera download error (network or invalid response)', ); return []; } @@ -1246,66 +1248,50 @@ export function parseTarkteeDatexImages(xml) { * @returns {Promise>} Normalized camera source objects. */ export async function loadTarkteeSourcesFromDatex() { + // Own both transports before either can reject. A response that arrives + // after cleanup must also be discarded instead of escaping Promise.all. + const abort = new AbortController(); + const signal = AbortSignal.any([ + abort.signal, + AbortSignal.timeout(TARKTEE_FETCH_TIMEOUT_MS), + ]); + const responses = []; + const discardBoth = async () => { + abort.abort(); + await Promise.allSettled( + responses.map((response) => response.body?.cancel()), + ); + return []; + }; + const fetchXml = async (url) => { + const response = await fetch(url, { + headers: { Accept: 'application/xml,text/xml,*/*' }, + redirect: 'manual', + signal, + }); + if (signal.aborted) { + await Promise.allSettled([response.body?.cancel()]); + signal.throwIfAborted(); + } + responses.push(response); + if (!response.ok) { + throw new Error( + response.status >= 300 && response.status < 400 + ? 'catalog redirected; redirects are not followed' + : `catalog HTTP ${response.status}`, + ); + } + return response; + }; try { const [locResp, imgResp] = await Promise.all([ - fetch(TARKTEE_LOCATIONS_URL, { - headers: { Accept: 'application/xml,text/xml,*/*' }, - redirect: 'manual', - signal: AbortSignal.timeout(TARKTEE_FETCH_TIMEOUT_MS), - }), - fetch(TARKTEE_IMAGES_URL, { - headers: { Accept: 'application/xml,text/xml,*/*' }, - redirect: 'manual', - signal: AbortSignal.timeout(TARKTEE_FETCH_TIMEOUT_MS), - }), + fetchXml(TARKTEE_LOCATIONS_URL), + fetchXml(TARKTEE_IMAGES_URL), + ]); + const [locXml, imgXml] = await Promise.all([ + readResponseTextCapped(locResp, TARKTEE_MAX_CATALOG_BYTES, signal), + readResponseTextCapped(imgResp, TARKTEE_MAX_CATALOG_BYTES, signal), ]); - // Both responses are fetched in parallel; if either is rejected or overflows - // its byte cap, both bodies must be released so neither transport leaks. - const discardBoth = async () => { - await Promise.allSettled([ - locResp?.body?.cancel(), - imgResp?.body?.cancel(), - ]); - return []; - }; - if ( - (locResp.status >= 300 && locResp.status < 400) || - (imgResp.status >= 300 && imgResp.status < 400) - ) { - console.warn( - '[CCTV] Tarktee DATEX catalog redirected; redirects are not followed', - ); - return discardBoth(); - } - if (!locResp.ok) { - console.warn('[CCTV] Tarktee locations download failed:', locResp.status); - return discardBoth(); - } - if (!imgResp.ok) { - console.warn('[CCTV] Tarktee images download failed:', imgResp.status); - return discardBoth(); - } - let locXml; - let imgXml; - const readAbort = new AbortController(); - try { - [locXml, imgXml] = await Promise.all([ - readResponseTextCapped( - locResp, - TARKTEE_MAX_CATALOG_BYTES, - readAbort.signal, - ), - readResponseTextCapped( - imgResp, - TARKTEE_MAX_CATALOG_BYTES, - readAbort.signal, - ), - ]); - } catch (error) { - readAbort.abort(); - await discardBoth(); - throw error; - } const locations = parseTarkteeDatexLocations(locXml); const images = parseTarkteeDatexImages(imgXml); if (!locations.size || !images.size) { @@ -1366,6 +1352,7 @@ export async function loadTarkteeSourcesFromDatex() { ); return prioritized; } catch (error) { + await discardBoth(); console.warn( '[CCTV] Tarktee DATEX download error:', error?.message || error, diff --git a/src/data/cctvEstonia.test.mjs b/src/data/cctvEstonia.test.mjs index 9ffeb6744b..7ded2cc66e 100644 --- a/src/data/cctvEstonia.test.mjs +++ b/src/data/cctvEstonia.test.mjs @@ -4,6 +4,7 @@ import { TARKTEE_IMAGES_URL, TARKTEE_LOCATIONS_URL, TARKTEE_MAX_CATALOG_BYTES, + TARKTEE_FETCH_TIMEOUT_MS, } from '../../server/providers/cctv/constants.js'; import { loadTallinnSourcesFromCatalog, @@ -116,7 +117,7 @@ test('Tarktee DATEX requests wait 30 s, past the slow answers measured live', as ), ); const cameras = await loadTarkteeSourcesFromDatex(); - assert.deepEqual(timeouts, [30_000, 30_000]); + assert.deepEqual(timeouts, [30_000]); assert.equal(cameras.length, 1); }); @@ -246,3 +247,113 @@ test('Tarktee loader cancels the sibling body when one feed fails or overflows t 'sibling stream is cancelled when the other feed exceeds the byte cap', ); }); + +for (const failingUrl of [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL]) { + for (const lateSibling of [false, true]) { + test(`Tarktee cancels a ${lateSibling ? 'late' : 'received'} sibling when ${failingUrl} rejects before headers`, async (t) => { + quiet(t); + let cancelled = false; + let siblingSignal; + let release; + const gate = new Promise((resolve) => { + release = resolve; + }); + t.mock.method(globalThis, 'fetch', async (url, init) => { + if (String(url) === failingUrl) throw new Error('headers unavailable'); + siblingSignal = init.signal; + if (lateSibling) await gate; + return new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('')); + }, + cancel() { + cancelled = true; + }, + }), + ); + }); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + // Also exercise a response arriving after abort, as a queued transport + // completion can race the rejection owner even when fetch honors abort. + if (lateSibling) { + release(); + await new Promise((resolve) => setImmediate(resolve)); + } + assert.equal(cancelled, true, 'every obtained body must be cancelled'); + assert.equal( + siblingSignal.aborted, + true, + 'a pending transport is aborted immediately', + ); + }); + } + test(`Tarktee aborts a pending sibling when ${failingUrl} rejects`, async (t) => { + quiet(t); + let aborted = false; + t.mock.method(globalThis, 'fetch', async (url, init) => { + if (String(url) === failingUrl) throw new Error('headers unavailable'); + return new Promise((resolve, reject) => { + init.signal.addEventListener( + 'abort', + () => { + aborted = true; + reject(init.signal.reason); + }, + { once: true }, + ); + }); + }); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + assert.equal(aborted, true, 'do not wait for the 30-second deadline'); + }); +} + +test('Tarktee cancels both lengthless streams when one exceeds the byte cap', async (t) => { + quiet(t); + const cancelled = []; + t.mock.method( + globalThis, + 'fetch', + async (url) => + new Response( + new ReadableStream({ + start(controller) { + const oversized = String(url) === TARKTEE_LOCATIONS_URL; + controller.enqueue( + new Uint8Array(oversized ? TARKTEE_MAX_CATALOG_BYTES + 1 : 1), + ); + }, + cancel() { + cancelled.push(String(url)); + }, + }), + ), + ); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + assert.deepEqual( + cancelled.sort(), + [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL].sort(), + ); +}); + +test('Tarktee aborts pending headers as soon as its sibling returns an HTTP error', async (t) => { + quiet(t); + let aborted = false; + t.mock.method(globalThis, 'fetch', async (url, init) => { + if (String(url) === TARKTEE_LOCATIONS_URL) + return new Response('unavailable', { status: 503 }); + return new Promise((resolve, reject) => { + init.signal.addEventListener( + 'abort', + () => { + aborted = true; + reject(init.signal.reason); + }, + { once: true }, + ); + }); + }); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + assert.equal(aborted, true); +}); diff --git a/src/layers/cctv/frames.test.mjs b/src/layers/cctv/frames.test.mjs index 0d03e94576..a0f14169e8 100644 --- a/src/layers/cctv/frames.test.mjs +++ b/src/layers/cctv/frames.test.mjs @@ -139,3 +139,34 @@ test('same pixels at a new aspect redraw at the new fit', () => { ]); assert.equal(runtime.canvasStamp, 2); }); + +test('same pixels at a new resolution with the same fit skip the redraw', () => { + const { runtime, load } = stillHarness(); + load(640, 480); + assert.deepEqual(load(1280, 960), []); + assert.equal(runtime.canvasStamp, 1); + assert.equal(runtime.drawnImageStamp, 2); +}); + +test('changed pixels redraw even when the fitted rectangle is unchanged', () => { + const { runtime, load } = stillHarness(); + load(640, 480); + const changedPixels = new Uint8ClampedArray( + FRAME_SIGNATURE_W * FRAME_SIGNATURE_H * 4, + ).fill(90); + runtime.signatureCtx.getImageData = () => ({ data: changedPixels }); + const calls = load(640, 480); + assert.equal(calls[0][0], 'fillRect'); + assert.deepEqual(calls[1], ['drawImage', 240, 0, 1440, 1080]); + assert.equal(runtime.canvasStamp, 2); +}); + +test('a failed pixel signature redraws rather than assuming equal content', () => { + const { runtime, load } = stillHarness(); + load(640, 480); + runtime.signatureCtx.getImageData = () => { + throw new Error('tainted canvas'); + }; + assert.deepEqual(load(640, 480).at(-1), ['drawImage', 240, 0, 1440, 1080]); + assert.equal(runtime.canvasStamp, 2); +}); diff --git a/src/layers/cctv/rendering.test.mjs b/src/layers/cctv/rendering.test.mjs index c4b490dd2b..e82650c4b2 100644 --- a/src/layers/cctv/rendering.test.mjs +++ b/src/layers/cctv/rendering.test.mjs @@ -71,3 +71,56 @@ test('focus de-emphasis fades each icon from its own color', () => { ); assert.equal(state._activeFocusStyleCount, 3); }); + +test('focus release restores video hue and alpha after the style throttle', () => { + let target = {}; + const records = ['mp4', 'webm'].map((feedType) => ({ + camera: { id: feedType, feedType }, + billboard: { show: true, color: LIVE_CAMERA_COLOR }, + })); + const state = { + _enabled: true, + _viewer: { scene: {}, camera: {} }, + _records: records, + _activeCameraId: null, + _activeFocusStyleCount: 0, + _lastFocusStyleAt: -Infinity, + }; + const focus = { + focusNowMs: (nowMs) => nowMs, + getFocusTarget: () => target, + focusPassIsNeeded: (value, count) => !!value || count > 0, + advanceSpriteFocus: () => ({ + factor: target ? FOCUS_FACTOR : 1, + active: !!target, + transitioning: false, + }), + focusAlphaNeedsWrite: (before, after) => before !== after, + }; + const { refreshCctvFocusStyles } = createRendering({ + state, + services: { focus }, + parts: { model: { isVideoFeedType } }, + source: {}, + }); + + refreshCctvFocusStyles(1000); + for (const record of records) { + assertFadedFrom( + record.billboard.color, + LIVE_CAMERA_COLOR, + record.camera.id, + ); + } + const dimmed = records.map((record) => record.billboard.color); + target = null; + refreshCctvFocusStyles(1050); + for (let i = 0; i < records.length; i += 1) { + assert.equal(records[i].billboard.color, dimmed[i]); + } + refreshCctvFocusStyles(1100); + for (const record of records) { + assert.deepEqual(record.billboard.color, LIVE_CAMERA_COLOR); + } + assert.equal(state._activeFocusStyleCount, 0); +}); From ea09a59877f1c7627f342c6928f6dcd92e698c78 Mon Sep 17 00:00:00 2001 From: Said Date: Mon, 19 Oct 2026 23:05:38 +0100 Subject: [PATCH 04/23] Validate CCTV terrain regime and frame calibration drag fixture --- .../1133-cctv-qa-terrain-drag-fixture.md | 3 + scripts/qa-cctv-v2.mjs | 75 +++++++++++++++---- 2 files changed, 62 insertions(+), 16 deletions(-) create mode 100644 changelog.d/1133-cctv-qa-terrain-drag-fixture.md diff --git a/changelog.d/1133-cctv-qa-terrain-drag-fixture.md b/changelog.d/1133-cctv-qa-terrain-drag-fixture.md new file mode 100644 index 0000000000..e997fc1fbb --- /dev/null +++ b/changelog.d/1133-cctv-qa-terrain-drag-fixture.md @@ -0,0 +1,3 @@ +- Make CCTV QA ground-sampling checks respect the active terrain regime and + frame the actual calibration gizmo for the drag regression, preserving the + photoreal completion floor and all drag invariants. diff --git a/scripts/qa-cctv-v2.mjs b/scripts/qa-cctv-v2.mjs index 2c121e0d6b..8b69ac83db 100644 --- a/scripts/qa-cctv-v2.mjs +++ b/scripts/qa-cctv-v2.mjs @@ -658,24 +658,39 @@ async function main() { // (existence, not just ceiling). The wait below tolerates the update-tick // latency of the completion pass (update interval is 10s). // ----------------------------------------------------------------------- - console.log('Waiting for tiles + the one-shot completion pass...'); - const tilesSeenLoaded = await waitForTilesLoaded(page, 45000); - let sampleFloorOk = false; - if (tilesSeenLoaded) { - sampleFloorOk = await waitForPredicate( - page, - (base) => window.__qaCounters.sampleHeight - base >= 1, - { timeoutMs: 30000, args: [c0.sampleHeight] }, + // Globe terrain uses the cached DEM/prior and intentionally never calls + // scene.sampleHeight. Only the photoreal regime owns a one-shot mesh snap. + // Keep the mesh completion floor strict when that regime is active. + const photorealRegime = await page.evaluate( + () => window.__godsEyeView.viewer.scene.globe?.show === false, + ); + if (photorealRegime) { + console.log('Waiting for tiles + the one-shot completion pass...'); + const tilesSeenLoaded = await waitForTilesLoaded(page, 45000); + let sampleFloorOk = false; + if (tilesSeenLoaded) { + sampleFloorOk = await waitForPredicate( + page, + (base) => window.__qaCounters.sampleHeight - base >= 1, + { timeoutMs: 30000, args: [c0.sampleHeight] }, + ); + } + const cFloor = await readCounters(); + record( + '>=1 real ground sample once tilesLoaded observed (one-shot snap completed)', + tilesSeenLoaded ? sampleFloorOk : null, + tilesSeenLoaded + ? `total real samples since enable=${cFloor.sampleHeight - c0.sampleHeight}` + : 'tilesLoaded never observed within 45s — floor not assessable this run', + ); + } else { + const cFloor = await readCounters(); + record( + 'terrain-globe uses its DEM/prior without photoreal mesh sampling', + cFloor.sampleHeight === c0.sampleHeight, + `mesh samples since enable=${cFloor.sampleHeight - c0.sampleHeight}`, ); } - const cFloor = await readCounters(); - record( - '>=1 real ground sample once tilesLoaded observed (one-shot snap completed)', - tilesSeenLoaded ? sampleFloorOk : null, - tilesSeenLoaded - ? `total real samples since enable=${cFloor.sampleHeight - c0.sampleHeight}` - : 'tilesLoaded never observed within 45s — floor not assessable this run', - ); // Let the completion pass finish before the steady-state window: require // the sampleHeight counter quiet for 12s (> the 10s update interval, so @@ -1732,6 +1747,34 @@ async function main() { gizmoStates.map((s, i) => `${GIZMO_PARTS[i]}=${s}`).join(' '), ); + // Frame the actual gizmo for the drag fixture. Default autofocus can leave + // its shaft below the 10px interaction threshold; that distant view is not + // the drag-usability scenario this regression exercises. + const dragFraming = await page.evaluate(async () => { + const viewer = window.__godsEyeView.viewer; + const e = viewer.entities.getById('cctv-gizmo-move-east'); + if (!e) return null; + const pts = e.polyline.positions.getValue(viewer.clock.currentTime); + const target = pts?.[0]; + if (!target) return null; + const measure = () => { + const a = viewer.scene.cartesianToCanvasCoordinates(pts[0]); + const b = viewer.scene.cartesianToCanvasCoordinates(pts[pts.length - 1]); + return { + shaftPx: a && b ? Math.hypot(b.x - a.x, b.y - a.y) : null, + rangeM: target.constructor.distance(viewer.camera.positionWC, target), + }; + }; + const before = measure(); + viewer.camera.cancelFlight(); + viewer.camera.lookAt(target, new target.constructor(0, -200, 180)); + viewer.camera.lookAtTransform(viewer.camera.transform.constructor.IDENTITY); + viewer.scene.requestRender(); + const frames = await window.__qaRenderFrames(3, 10000); + return { before, after: measure(), frames }; + }); + console.log('Drag fixture framing:', JSON.stringify(dragFraming)); + // Synthetic CDP drag on the EAST arrow — the owner's field-test // regression. Sample effective elevation after every mouse move: it must // stay frozen until release, with no transient sampleHeight calls. From f927ef288a28a37fe99235b1dbed2826dcc6c279 Mon Sep 17 00:00:00 2001 From: Said Date: Mon, 19 Oct 2026 23:16:02 +0100 Subject: [PATCH 05/23] Check tracking rotation on the first visible frame within fleet cadence --- .../1133-cctv-qa-terrain-drag-fixture.md | 3 + scripts/track-regression.mjs | 79 +++++++++++++------ 2 files changed, 56 insertions(+), 26 deletions(-) diff --git a/changelog.d/1133-cctv-qa-terrain-drag-fixture.md b/changelog.d/1133-cctv-qa-terrain-drag-fixture.md index e997fc1fbb..789df9291c 100644 --- a/changelog.d/1133-cctv-qa-terrain-drag-fixture.md +++ b/changelog.d/1133-cctv-qa-terrain-drag-fixture.md @@ -1,3 +1,6 @@ - Make CCTV QA ground-sampling checks respect the active terrain regime and frame the actual calibration gizmo for the drag regression, preserving the photoreal completion floor and all drag invariants. +- Measure the tracking arrival regression on the first visible frame within + 250 ms, accounting for the 80 ms fleet cadence while rejecting a stale + first-visible orientation instead of retrying until it is correct. diff --git a/scripts/track-regression.mjs b/scripts/track-regression.mjs index 756daccfc3..e05309efaf 100644 --- a/scripts/track-regression.mjs +++ b/scripts/track-regression.mjs @@ -2696,9 +2696,9 @@ async function main() { // Staged deterministically: park the camera nadir over the synthetic // cluster, let rotations settle, TAMPER a billboard's rotation, then // trigger each path with the camera idle. The idle-drift catch-up - // (ROTATION_REFRESH_MS = 1 s) can coincidentally mask a regression in a - // ~300 ms SwiftShader frame window, so a regression shows as an - // intermittent (not guaranteed) failure — never a false failure. + // (ROTATION_REFRESH_MS = 1 s) must not repair a stale first-visible nose + // before we inspect it: capture that first postRender, with a 250 ms + // deadline that admits the 80 ms fleet cadence without waiting a second. // ============================================================ console.log('\nChange 4 — arrival rotation: moveEnd settle pass + horizon-reveal pass'); const arrival = await evalPage(async () => { @@ -2762,6 +2762,43 @@ async function main() { }); await nextFrames(3); const angDiff = (a, b) => Math.abs(Math.atan2(Math.sin(a - b), Math.cos(a - b))); + // Render frames are not fleet ticks: at 60/120 Hz four frames can + // precede the next 80 ms fleet pass. Observe elapsed time instead, and + // freeze the angle on the FIRST visible frame, even when it is wrong. + const observeFirstReveal = (bb, course, budgetMs = 250) => new Promise((resolve) => { + const startedAt = performance.now(); + let frames = 0; + let settled = false; + const finish = (shown, elapsedMs) => { + if (settled) return; + settled = true; + clearTimeout(timer); + remove(); + const expected = shown + ? screenProjectedRotation(v.scene, bb.position, course, null) + : null; + resolve({ + shown, + flipFrame: shown ? frames : -1, + revealElapsedMs: elapsedMs, + revealWithinBudget: shown && elapsedMs <= budgetMs, + dReveal: shown && Number.isFinite(expected) ? angDiff(bb.rotation, expected) : null, + }); + }; + const remove = v.scene.postRender.addEventListener(() => { + frames += 1; + const elapsedMs = performance.now() - startedAt; + if (bb.show) { + finish(true, elapsedMs); + } else if (elapsedMs >= budgetMs) { + finish(false, elapsedMs); + } else { + v.scene.requestRender(); + } + }); + const timer = setTimeout(() => finish(false, performance.now() - startedAt), budgetMs); + v.scene.requestRender(); + }); const probe = async (id, course) => { const bb = findBB(id); if (!bb || !bb.show) return { error: `${id} missing/hidden` }; @@ -2779,25 +2816,17 @@ async function main() { const expectedMoveEnd = screenProjectedRotation(v.scene, bb.position, course, null); // (b) reveal pass: tamper + hide — the next fleet tick must flip it // visible AND correct the nose in that same tick (camera still idle, - // no moveEnd raised). Diagnostic fields (round 5): record WHICH frame - // the flip landed on (flipFrame; -1 = never within the extended - // window) and whether a 3D model owns this id — distinguishes "tick - // slow under load" from "occluder says hidden" from "model handoff". + // no moveEnd raised). Record frame count as diagnostics only: the + // first visible postRender must already have the corrected rotation. bb.rotation = r0 + 1.5; bb.show = false; - let flipFrame = -1; - for (let f = 1; f <= 6; f++) { - await nextFrames(1); - if (bb.show && flipFrame === -1) flipFrame = f; - if (f >= 2 && flipFrame !== -1) break; - } - const expectedReveal = screenProjectedRotation(v.scene, bb.position, course, null); + const reveal = await observeFirstReveal(bb, course); const hasModel = !!(window.__g3dFindModel && window.__g3dFindModel(id)); return { r0, dMoveEnd: angDiff(afterMoveEnd, expectedMoveEnd), - dReveal: angDiff(bb.rotation, expectedReveal), - shown: bb.show, flipFrame, hasModel, + ...reveal, + hasModel, }; }; const flightCourse = window.__SYNTH.flights.find((f) => f.icao === 'aaa002')?.track ?? 0; @@ -2808,20 +2837,18 @@ async function main() { }; }); const fmtArr = (a) => (!a ? `no result (${arrival?.error || 'phase error'})` : a.error ? a.error - : `settle-err=${(a.dMoveEnd * 180 / Math.PI).toFixed(1)}° reveal-err=${(a.dReveal * 180 / Math.PI).toFixed(1)}° shown=${a.shown} flipFrame=${a.flipFrame} hasModel=${a.hasModel}`); + : `settle-err=${(a.dMoveEnd * 180 / Math.PI).toFixed(1)}° reveal-err=${a.dReveal === null ? 'unobserved' : (a.dReveal * 180 / Math.PI).toFixed(1) + '°'} shown=${a.shown} flipFrame=${a.flipFrame} revealMs=${a.revealElapsedMs.toFixed(1)} hasModel=${a.hasModel}`); const TAMPER_DEG = 1.5 * 180 / Math.PI; // ≈ 86° — an uncorrected nose stays this far off - // Reveal budget re-pinned 2 → 4 frames (round 5): the diagnostic probe - // shows the military flip consistently lands on frame 2 — the exact edge - // of the old budget — so any load hiccup flaked it (flights lands frame - // 1). The regression this protects against is a reveal that waits for - // the 1 s idle catch-up (~30-60 frames) or never re-aims; 4 frames still - // catches that with a wide margin, with zero-headroom flakes gone. + // 250 ms spans multiple 80 ms fleet opportunities while remaining well + // below the 1 s idle catch-up. Never retry until the angle becomes correct: + // dReveal is the immutable first-visible-frame observation above. const arrOk = (a) => !!a && !a.error && a.shown - && (a.flipFrame === -1 ? false : a.flipFrame <= 4) + && a.revealWithinBudget && a.revealElapsedMs <= 250 + && a.dReveal !== null && a.dMoveEnd * 180 / Math.PI < 10 && a.dReveal * 180 / Math.PI < 10; - record(`arrival: moveEnd + reveal both re-aim within 4 frames, flights (tamper was ${TAMPER_DEG.toFixed(0)}°)`, + record(`arrival: moveEnd re-aims and first reveal is correct within 250 ms, flights (tamper was ${TAMPER_DEG.toFixed(0)}°)`, arrOk(arrival.flights), fmtArr(arrival.flights)); - record(`arrival: moveEnd + reveal both re-aim within 4 frames, military (tamper was ${TAMPER_DEG.toFixed(0)}°)`, + record(`arrival: moveEnd re-aims and first reveal is correct within 250 ms, military (tamper was ${TAMPER_DEG.toFixed(0)}°)`, arrOk(arrival.military), fmtArr(arrival.military)); // ============================================================ From 2e2f7564680d142f3c9701cd0077b3ccc62b089b Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Wed, 4 Nov 2026 22:13:01 -0500 Subject: [PATCH 06/23] fix(places): don't let the browser cache a refused Google answer The Places and Geocoding proxies sent Cache-Control: private, max-age=300 whatever Google answered. A 403 from a key without the Places API, or a geocode REQUEST_DENIED / OVER_QUERY_LIMIT (which Google sends as HTTP 200), was then replayed from the browser cache for five minutes after the key was fixed. Refusals are now no-store. --- docs/CURRENT-STATE.md | 1 + server/providers/places/google.js | 26 +++++++++++++++++--- src/tooling/placeProviders.test.mjs | 38 +++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index f1244947d4..ece994cab5 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -5996,6 +5996,7 @@ apply to `public/` assets or built-preview serving. - `AISSTREAM_API_KEY` is server-side only; the browser reads the same-origin `/api/vessels` cache. - `/api/google/nearby-places` keeps the Google key out of Places requests issued for voice scene context. - `/api/google/text-search` keeps the Google key server-side for view-biased Places recovery used by annotation resolution. +- The two Places routes and both geocode routes let the browser keep an answer for five minutes (`private, max-age=300`), but a refusal is `no-store`: a Places error status, or a Geocoding status other than `OK` or `ZERO_RESULTS` (Google sends `REQUEST_DENIED` and `OVER_QUERY_LIMIT` as HTTP 200). A denied or exhausted key is not replayed from the browser cache after it is fixed. - `/api/overpass` is bounded by body/response caps, per-client/global rate limits, concurrency limits, operator-configured upstreams, in-flight dedupe, cache bounds, and static validation that every selector is spatially bounded. - `/api/military-installations` uses an independent limiter with the same 90-per-client/300-global one-minute bounds, so viewport installation refreshes never consume `/api/overpass` annotation capacity. - `/api/route` proxies bounded OSRM route requests for annotation routes, with profile allowlisting, distance caps, response caps, caching, and sanitized "no route found" errors. diff --git a/server/providers/places/google.js b/server/providers/places/google.js index 1ddaa95939..e20efa899d 100644 --- a/server/providers/places/google.js +++ b/server/providers/places/google.js @@ -71,6 +71,9 @@ const GEOCODE_UPSTREAM_TIMEOUT_MS = 10_000; const GEOCODE_MAX_RESPONSE_BYTES = 256 * 1024; +/** Geocoding statuses that answer the query; the rest report a failure. */ +const GEOCODE_ANSWER_STATUSES = new Set(['OK', 'ZERO_RESULTS']); + /** Validate a free-text geocoder query before consuming request quota. */ export function validateGeocodeAddress(searchParams) { const address = String(searchParams.get('address') || '').trim(); @@ -199,7 +202,12 @@ export function googlePlacesContextProxy({ res.statusCode = response.ok ? 200 : response.status; res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.setHeader('Cache-Control', 'private, max-age=300'); + // A refusal (bad key, API not enabled, quota) is not kept: the browser + // would replay it for five minutes after the key is fixed. + res.setHeader( + 'Cache-Control', + response.ok ? 'private, max-age=300' : 'no-store', + ); res.end( JSON.stringify({ places, @@ -320,7 +328,12 @@ export function googlePlacesContextProxy({ res.statusCode = response.ok ? 200 : response.status; res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.setHeader('Cache-Control', 'private, max-age=300'); + // A refusal (bad key, API not enabled, quota) is not kept: the browser + // would replay it for five minutes after the key is fixed. + res.setHeader( + 'Cache-Control', + response.ok ? 'private, max-age=300' : 'no-store', + ); res.end( JSON.stringify({ places, @@ -446,7 +459,14 @@ export function googlePlacesContextProxy({ res.statusCode = response.ok ? 200 : response.status; res.setHeader('Content-Type', 'application/json; charset=utf-8'); - res.setHeader('Cache-Control', 'private, max-age=300'); + // Geocoding reports a denied key or spent quota as HTTP 200 with a + // status of its own; only an answer is worth keeping. + res.setHeader( + 'Cache-Control', + response.ok && GEOCODE_ANSWER_STATUSES.has(data.status) + ? 'private, max-age=300' + : 'no-store', + ); res.end(JSON.stringify(data)); } catch { // Fixed public text: a transport, URL or runtime message would diff --git a/src/tooling/placeProviders.test.mjs b/src/tooling/placeProviders.test.mjs index d533b7fb9a..a6a3457536 100644 --- a/src/tooling/placeProviders.test.mjs +++ b/src/tooling/placeProviders.test.mjs @@ -185,6 +185,44 @@ for (const preview of [false, true]) { }); } +test('a refused Places or Geocoding answer is not kept by the browser', async (t) => { + const plugin = googlePlacesContextProxy({ + resolveApiKey: () => 'fixture-server-key', + }); + const request = install((middlewares) => + plugin.configureServer({ middlewares }), + ); + t.mock.method(globalThis, 'fetch', async () => + Response.json({ error: { message: 'API key not valid' } }, { status: 403 }), + ); + for (const url of [ + ['/api/google/nearby-places', '?lat=30&lon=-97'], + ['/api/google/text-search', '?q=museum&lat=30&lon=-97'], + ]) { + const refused = await request(...url); + assert.equal(refused.statusCode, 403); + assert.equal(refused.headers['cache-control'], 'no-store'); + } + + // Geocoding reports a denied key as HTTP 200 with a status of its own. + let status = 'REQUEST_DENIED'; + t.mock.method(globalThis, 'fetch', async () => + Response.json({ status, results: [] }), + ); + for (const denied of ['REQUEST_DENIED', 'OVER_QUERY_LIMIT']) { + status = denied; + const answer = await request('/api/google/geocode', '?address=Austin'); + assert.equal(answer.statusCode, 200); + assert.equal(answer.body.status, denied); + assert.equal(answer.headers['cache-control'], 'no-store', denied); + } + for (const answered of ['OK', 'ZERO_RESULTS']) { + status = answered; + const answer = await request('/api/google/geocode', '?address=Austin'); + assert.equal(answer.headers['cache-control'], 'private, max-age=300'); + } +}); + test('OSRM routing preserves aliases, cache, span guards and upstream failure behavior', async (t) => { const request = install(installRouteMiddleware); let calls = 0; From f22701ae7064392ace46afd8f5188531bf4c79ee Mon Sep 17 00:00:00 2001 From: Rudy Celekli Date: Wed, 4 Nov 2026 22:25:40 -0500 Subject: [PATCH 07/23] docs(changelog): add the fragment for #1570 --- changelog.d/1570-places-no-store-on-errors.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/1570-places-no-store-on-errors.md diff --git a/changelog.d/1570-places-no-store-on-errors.md b/changelog.d/1570-places-no-store-on-errors.md new file mode 100644 index 0000000000..459258ad0b --- /dev/null +++ b/changelog.d/1570-places-no-store-on-errors.md @@ -0,0 +1,4 @@ +- Stop the browser caching a refused Google Places or Geocoding answer. The + proxies sent `private, max-age=300` on a 403 and on a geocode + `REQUEST_DENIED` or `OVER_QUERY_LIMIT`, so a fixed key kept failing for five + minutes; refusals are now `no-store` (Rudy Celekli, @rudycelekli, #1570). From 7af0453f3460bc5b4c48f05df88dfda78ac34a7f Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Fri, 6 Nov 2026 01:05:50 +0000 Subject: [PATCH 08/23] CCTV Street View fallback: catalog cameras only, at their catalog position, inside the Google budget --- server/providers/cctv.js | 33 +++++++++++++++-------------- server/providers/places/google.js | 4 ++-- src/data/cctvMediaFeedType.test.mjs | 27 +++++++++++++++++++++++ 3 files changed, 46 insertions(+), 18 deletions(-) diff --git a/server/providers/cctv.js b/server/providers/cctv.js index a977f4e753..64605b9852 100644 --- a/server/providers/cctv.js +++ b/server/providers/cctv.js @@ -19,6 +19,8 @@ import { admitSameSiteKeyless } from './common/same-site.js'; import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs'; import { createHlsPuller } from './cctv/stream.js'; import { googleServerApiKey } from './places/google-key.js'; +import { googleRateLimiter } from './places/google.js'; +import { clientKey } from './common/rate-limit.js'; export { CCTV_FRAME_FETCH_TIMEOUT_MS, fetchCctvImageFromUpstream }; /** * Vite plugin: CCTV camera proxy with source registry, frame/media serving, @@ -509,13 +511,6 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { const source = sourceById.get(cameraId); const label = url.searchParams.get('label') || source?.name || cameraId; const city = url.searchParams.get('city') || source?.city || ''; - const lat = Number(url.searchParams.get('lat') || source?.lat); - const lon = Number(url.searchParams.get('lon') || source?.lon); - const heading = Number( - url.searchParams.get('heading') || source?.headingDeg, - ); - const fov = Number(url.searchParams.get('fov') || source?.fovDeg); - const pitch = Number(url.searchParams.get('pitch') || source?.pitchDeg); // Only use server-registered upstream URLs — never accept client-supplied URLs // (prevents SSRF via ?upstream= query parameter) @@ -549,18 +544,24 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { // request (whose client this server cannot see) gets the placeholder, // as if no key were configured. Any forwarding header counts, even an // empty one. + // It is also only for catalog cameras, framed from the catalog (never + // a spot the query names), within the per-IP Google budget. const forwarded = PROXY_SIGNALS.some( (name) => req.headers?.[name] !== undefined, ); - const sv = forwarded - ? null - : await streetViewFallback({ - lat, - lon, - heading, - fov, - pitch, - }); + const googleBudget = googleServerApiKey() ? googleRateLimiter() : null; + const sv = + forwarded || + !source || + (googleBudget && !googleBudget(clientKey(req))) + ? null + : await streetViewFallback({ + lat: Number(source.lat), + lon: Number(source.lon), + heading: Number(source.headingDeg), + fov: Number(source.fovDeg), + pitch: Number(source.pitchDeg ?? -10), + }); if (sv?.ok) { setHealth(cameraId, { status: 'degraded', diff --git a/server/providers/places/google.js b/server/providers/places/google.js index 1ddaa95939..5b034e7064 100644 --- a/server/providers/places/google.js +++ b/server/providers/places/google.js @@ -27,8 +27,8 @@ let _googleRateLimiter; */ export const GOOGLE_DEFAULT_PER_MIN = 120; -/** Google cost endpoints (nearby-places, text-search, geocoding). Null only when set to 0. */ -function googleRateLimiter() { +/** Google cost endpoints (nearby-places, text-search, geocoding, the CCTV Street View fallback). Null only when set to 0. */ +export function googleRateLimiter() { if (_googleRateLimiter === undefined) _googleRateLimiter = makeCostRateLimiter( process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, diff --git a/src/data/cctvMediaFeedType.test.mjs b/src/data/cctvMediaFeedType.test.mjs index 04dd77e284..0523c95f39 100644 --- a/src/data/cctvMediaFeedType.test.mjs +++ b/src/data/cctvMediaFeedType.test.mjs @@ -528,3 +528,30 @@ test('a forwarded same-origin request reaches every CCTV route but not the Stree assert.equal(direct.body, 'street-view'); assert.equal(streetViewCalls(), 1); }); + +test('the Street View fallback frames catalog cameras only, from the catalog position', async (t) => { + const app = mount(t, ({ url }) => + url.startsWith('https://maps.googleapis.com/') + ? trackedUpstream({ + headers: { 'content-type': 'image/jpeg' }, + body: 'street-view', + }).response + : upstreamResponse({ status: 503 }), + ); + process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key'; + const streetView = () => + app.requests.filter(({ url }) => + url.startsWith('https://maps.googleapis.com/'), + ); + + const unknown = await app.call('/frame/not-a-camera?lat=48.85&lon=2.29'); + assert.equal(unknown.headers['X-CCTV-Source'], 'synthetic'); + assert.deepEqual(streetView(), [], 'no Google call for an unknown id'); + + const steered = await app.call( + `/frame/${STILL.id}?lat=48.85&lon=2.29&heading=90`, + ); + assert.equal(steered.headers['X-CCTV-Source'], 'streetview'); + const location = new URL(streetView()[0].url).searchParams.get('location'); + assert.equal(location, `${STILL.lat},${STILL.lon}`); +}); From 078a6c0db0288df57bd373ae28ca9948d94016aa Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Fri, 6 Nov 2026 01:09:11 +0000 Subject: [PATCH 09/23] CCTV Street View fallback: own per-IP Google budget inside the cctv package boundary --- scripts/package-boundaries.json | 4 +++- server/providers/cctv.js | 21 ++++++++++++++++++--- server/providers/places/google.js | 4 ++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index 20f6c0b85d..b764667f07 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -377,7 +377,9 @@ "src/data/cctvFootprint.js", "src/sources/cctvTypes.js", "src/sources/httpBody.js", - "src/i18n/marker.js" + "src/i18n/marker.js", + "server/providers/common/rate-limit.js", + "src/sources/rateLimit.js" ], "external": [] }, diff --git a/server/providers/cctv.js b/server/providers/cctv.js index 64605b9852..e8e18ad51e 100644 --- a/server/providers/cctv.js +++ b/server/providers/cctv.js @@ -19,9 +19,22 @@ import { admitSameSiteKeyless } from './common/same-site.js'; import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs'; import { createHlsPuller } from './cctv/stream.js'; import { googleServerApiKey } from './places/google-key.js'; -import { googleRateLimiter } from './places/google.js'; -import { clientKey } from './common/rate-limit.js'; +import { clientKey, makeCostRateLimiter } from './common/rate-limit.js'; export { CCTV_FRAME_FETCH_TIMEOUT_MS, fetchCctvImageFromUpstream }; +let _streetViewRateLimiter; +/** + * Per-IP budget for the Street View fallback, on the same setting as the + * other Google routes (GEV_RATELIMIT_GOOGLE_PER_MIN; 0 disables it). + */ +function streetViewRateLimiter() { + if (_streetViewRateLimiter === undefined) + _streetViewRateLimiter = makeCostRateLimiter( + process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, + 120, + ); + return _streetViewRateLimiter; +} + /** * Vite plugin: CCTV camera proxy with source registry, frame/media serving, * fallback chain (upstream -> Street View -> synthetic SVG), and health tracking. @@ -549,7 +562,9 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { const forwarded = PROXY_SIGNALS.some( (name) => req.headers?.[name] !== undefined, ); - const googleBudget = googleServerApiKey() ? googleRateLimiter() : null; + const googleBudget = googleServerApiKey() + ? streetViewRateLimiter() + : null; const sv = forwarded || !source || diff --git a/server/providers/places/google.js b/server/providers/places/google.js index 5b034e7064..1ddaa95939 100644 --- a/server/providers/places/google.js +++ b/server/providers/places/google.js @@ -27,8 +27,8 @@ let _googleRateLimiter; */ export const GOOGLE_DEFAULT_PER_MIN = 120; -/** Google cost endpoints (nearby-places, text-search, geocoding, the CCTV Street View fallback). Null only when set to 0. */ -export function googleRateLimiter() { +/** Google cost endpoints (nearby-places, text-search, geocoding). Null only when set to 0. */ +function googleRateLimiter() { if (_googleRateLimiter === undefined) _googleRateLimiter = makeCostRateLimiter( process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, From f0d3db31646be8dd5b504b4f49922d71c07116bd Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Fri, 6 Nov 2026 11:57:58 +0000 Subject: [PATCH 10/23] Docs: the CCTV Street View fallback serves catalog cameras only, inside the Google budget --- docs/CURRENT-STATE.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 8ad23493a4..019e25704e 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -1276,7 +1276,10 @@ and follows redirects only on the camera's origin. Catalog image URLs, and redirects on those hosts, are pinned by parsed URL to the agency's image directory. Every `/api/cctv` route refuses cross-site browser requests but accepts forwarding headers, so cameras work behind a reverse proxy; a forwarded -request skips the key-bearing Street View fallback and gets the placeholder. +request skips the key-bearing Street View fallback and gets the placeholder. The +Street View fallback serves only catalog cameras, at the catalog position, +heading, field of view and pitch, never coordinates a request supplies, and it +shares the per-client Google budget (`GEV_RATELIMIT_GOOGLE_PER_MIN`, default 120). The CCTV layer draws only the cameras on or near the screen. Zoomed out, it keeps one camera per ground cell sized to the view, preferring a healthy feed, From f579b23f520a51e69b158222207088baa3f1b205 Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Fri, 6 Nov 2026 11:59:32 +0000 Subject: [PATCH 11/23] Changelog: 1602 fragment --- changelog.d/1602-cctv-streetview-fallback-catalog.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog.d/1602-cctv-streetview-fallback-catalog.md diff --git a/changelog.d/1602-cctv-streetview-fallback-catalog.md b/changelog.d/1602-cctv-streetview-fallback-catalog.md new file mode 100644 index 0000000000..04574b9142 --- /dev/null +++ b/changelog.d/1602-cctv-streetview-fallback-catalog.md @@ -0,0 +1,5 @@ +- The CCTV Street View fallback serves only catalog cameras, at their catalog + position, heading, field of view and pitch, and ignores coordinates a + request supplies. It spends from the per-client Google budget + (`GEV_RATELIMIT_GOOGLE_PER_MIN`, default 120), so the Google key can no + longer be used as an unmetered Street View service (asturksever, #1602). From b65a7aaed8e16c29108bfece126806b3cca92ccd Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Fri, 13 Nov 2026 18:47:30 +0400 Subject: [PATCH 12/23] Keep an explicit CCTV pick of the camera a move re-opened Until someone picks a camera, a settled move re-opens CCTV on the camera nearest the view and keeps the opening pick provisional. setActiveCamera ends that opening pick, but for a camera that is already active and activated it returns UNCHANGED before clearing the flag. So pressing NEAREST on the camera a move had just re-opened (or selecting it from the list, or through setParams) left the pick provisional, and the landing of NEAREST's own flight, or the next pan, switched to another camera. Clear the provisional flag on the UNCHANGED path too. reopenNearestCamera only activates a different camera, so it is unaffected, and the same-camera re-select still skips the activation work. --- src/data/cctvExplicitPickEndsOpening.test.mjs | 129 ++++++++++++++++++ src/layers/cctv/selection.js | 3 + 2 files changed, 132 insertions(+) create mode 100644 src/data/cctvExplicitPickEndsOpening.test.mjs diff --git a/src/data/cctvExplicitPickEndsOpening.test.mjs b/src/data/cctvExplicitPickEndsOpening.test.mjs new file mode 100644 index 0000000000..cfe6498880 --- /dev/null +++ b/src/data/cctvExplicitPickEndsOpening.test.mjs @@ -0,0 +1,129 @@ +// An explicit pick of the camera that a settled move just re-opened must end +// the opening pick. That pick goes through setActiveCamera's UNCHANGED path +// (same camera, already activated); if that path keeps the provisional flag, +// the next settled move (a pan, or the landing of NEAREST's own flight) +// switches the camera away from the one the user chose. +import assert from 'node:assert/strict'; +import test from 'node:test'; +import * as Cesium from 'cesium'; +import cctvLayer, { + _setCctvCoverageStateForTest, + _setCctvOverlayHostForTest, + reopenNearestCamera, +} from './cctv.js'; + +const BASE = { + headingDeg: 41, + pitchDeg: -5, + fovDeg: 56, + rangeM: 210, + mountHeightM: 100, + groundElevationM: 0, + city: 'London', +}; + +function record(id, lat, lon) { + return { + camera: { ...BASE, id, name: `Camera ${id}`, lat, lon }, + coverageEntities: [], + projection: null, + activationDone: true, + groundResolved: {}, + groundSamples: {}, + groundPrior: 0, + frustumGeometry: null, + frustumPositions: null, + probeClampRangeM: null, + position: Cesium.Cartesian3.fromDegrees(lon, lat, 100), + }; +} + +function viewerAt(lat, lon) { + return { + entities: new Cesium.EntityCollection(), + isDestroyed: () => false, + trackedEntity: null, + camera: { + positionWC: Cesium.Cartesian3.fromDegrees(lon, lat, 300), + positionCartographic: Cesium.Cartographic.fromDegrees(lon, lat, 300), + heading: 0.4, + pitch: -0.7, + roll: 0, + transform: Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY), + flyToBoundingSphere() {}, + }, + scene: { + canvas: { clientWidth: 1200, clientHeight: 800 }, + cartesianToCanvasCoordinates: () => undefined, + primitives: { add: (p) => p, remove: () => true }, + requestRender() {}, + }, + }; +} + +function moveViewer(viewer, lat, lon) { + viewer.camera.positionCartographic = Cesium.Cartographic.fromDegrees( + lon, + lat, + 300, + ); +} + +function withReopenedLayer(fn) { + _setCctvOverlayHostForTest({ + setEntries() {}, + setVisible() {}, + clearSource() {}, + }); + // Activation builds the projection canvas and frame image. + const originalDocument = globalThis.document; + const originalImage = globalThis.Image; + globalThis.document = { createElement: () => ({ getContext: () => null }) }; + globalThis.Image = class {}; + try { + const trafalgar = record('a-trafalgar', 51.508, -0.128); + const cambridge = record('b-cambridge', 51.513, -0.129); + // enable() assigned the opening camera without activating it. + trafalgar.activationDone = false; + const viewer = viewerAt(51.5081, -0.1281); + _setCctvCoverageStateForTest({ + viewer, + records: [trafalgar, cambridge], + activeCameraId: 'a-trafalgar', + enabled: true, + openingCameraProvisional: true, + }); + // A settled move next to Cambridge Circus re-opens there, still as the + // opening pick. + moveViewer(viewer, 51.5131, -0.1291); + assert.equal(reopenNearestCamera(), true); + assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge'); + fn(viewer); + } finally { + globalThis.document = originalDocument; + globalThis.Image = originalImage; + _setCctvOverlayHostForTest(); + _setCctvCoverageStateForTest({ enabled: false }); + } +} + +for (const [name, pick] of [ + ['NEAREST (focusNearest)', () => cctvLayer.focusNearest({ focus: false })], + ['selectCamera', () => cctvLayer.selectCamera('b-cambridge')], + [ + 'setParams({ selectedCameraId })', + () => cctvLayer.setParams({ selectedCameraId: 'b-cambridge' }), + ], +]) { + test(`an explicit ${name} pick of the re-opened camera ends the opening pick`, () => { + withReopenedLayer((viewer) => { + pick(); + assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge'); + // A later settled move (a pan, or the landing of the pick's own flight) + // leaves the explicit choice alone. + moveViewer(viewer, 51.5081, -0.1281); + assert.equal(reopenNearestCamera(), false); + assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge'); + }); + }); +} diff --git a/src/layers/cctv/selection.js b/src/layers/cctv/selection.js index 834b2f583c..1a5c1c1613 100644 --- a/src/layers/cctv/selection.js +++ b/src/layers/cctv/selection.js @@ -79,6 +79,9 @@ export function createSelection({ record, ) ) { + // Picking the camera a settled move re-opened is still a choice: later + // settled moves must leave it alone. + layerState._openingCameraProvisional = false; // Browser-only feeds have a separate player; a reselect retries it // without re-running activation or touching the projection geometry. if (parts.model.isBrowserOverlayCamera(record.camera)) { From 707a6a664a97e0f61304d13775747b7796171042 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Fri, 13 Nov 2026 18:52:37 +0400 Subject: [PATCH 13/23] Changelog fragment for #1702 --- changelog.d/1702-cctv-nearest-same-camera.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 changelog.d/1702-cctv-nearest-same-camera.md diff --git a/changelog.d/1702-cctv-nearest-same-camera.md b/changelog.d/1702-cctv-nearest-same-camera.md new file mode 100644 index 0000000000..63833e6ca6 --- /dev/null +++ b/changelog.d/1702-cctv-nearest-same-camera.md @@ -0,0 +1,4 @@ +- Keep the CCTV camera picked with NEAREST, from the camera list or by a scene + when it is the camera the view just moved to. The pick did not stick: + NEAREST's own flight, or the next pan, switched to another camera, so the + panel showed a camera nobody had picked (gekh, #1702). From 31b6b25d8bb16f0b1b42c8f1bde8e190578cc3a3 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Thu, 19 Nov 2026 18:29:24 +0400 Subject: [PATCH 14/23] fix(places): make no-store the default and refuse an unreadable Places 200 Every Google route now sets no-store right after the same-site gate and switches to private, max-age=300 only on a confirmed answer, so the proxy's own 400, 405, 429 and 502 replies are no longer left without a header. Both Places routes answer an unreadable 200 body with 502 instead of caching an empty result, as the geocode routes already did. The test pins the allowlist with OVER_DAILY_LIMIT and UNKNOWN_ERROR and covers the unreadable and empty Places bodies; the changelog fragment is shorter. --- changelog.d/1570-places-no-store-on-errors.md | 6 +- docs/CURRENT-STATE.md | 2 +- server/providers/places/google.js | 71 +++++++++++-------- src/tooling/placeProviders.test.mjs | 59 ++++++++++++++- 4 files changed, 103 insertions(+), 35 deletions(-) diff --git a/changelog.d/1570-places-no-store-on-errors.md b/changelog.d/1570-places-no-store-on-errors.md index 459258ad0b..4753638588 100644 --- a/changelog.d/1570-places-no-store-on-errors.md +++ b/changelog.d/1570-places-no-store-on-errors.md @@ -1,4 +1,2 @@ -- Stop the browser caching a refused Google Places or Geocoding answer. The - proxies sent `private, max-age=300` on a 403 and on a geocode - `REQUEST_DENIED` or `OVER_QUERY_LIMIT`, so a fixed key kept failing for five - minutes; refusals are now `no-store` (Rudy Celekli, @rudycelekli, #1570). +- Stop the browser caching a refused Google Places or Geocoding answer, so a + fixed key works at once (Rudy Celekli, @rudycelekli, #1570). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index ece994cab5..42ff3ff9c2 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -5996,7 +5996,7 @@ apply to `public/` assets or built-preview serving. - `AISSTREAM_API_KEY` is server-side only; the browser reads the same-origin `/api/vessels` cache. - `/api/google/nearby-places` keeps the Google key out of Places requests issued for voice scene context. - `/api/google/text-search` keeps the Google key server-side for view-biased Places recovery used by annotation resolution. -- The two Places routes and both geocode routes let the browser keep an answer for five minutes (`private, max-age=300`), but a refusal is `no-store`: a Places error status, or a Geocoding status other than `OK` or `ZERO_RESULTS` (Google sends `REQUEST_DENIED` and `OVER_QUERY_LIMIT` as HTTP 200). A denied or exhausted key is not replayed from the browser cache after it is fixed. +- The two Places routes and both geocode routes let the browser keep an answer for five minutes (`private, max-age=300`), and everything else is `no-store`: a Places error status or unreadable body, a Geocoding status other than `OK` or `ZERO_RESULTS` (Google sends `REQUEST_DENIED` and `OVER_QUERY_LIMIT` as HTTP 200), and the proxy's own 400, 405, 429 and 502 replies. A denied or exhausted key is not replayed from the browser cache after it is fixed. - `/api/overpass` is bounded by body/response caps, per-client/global rate limits, concurrency limits, operator-configured upstreams, in-flight dedupe, cache bounds, and static validation that every selector is spatially bounded. - `/api/military-installations` uses an independent limiter with the same 90-per-client/300-global one-minute bounds, so viewport installation refreshes never consume `/api/overpass` annotation capacity. - `/api/route` proxies bounded OSRM route requests for annotation routes, with profile allowlisting, distance caps, response caps, caching, and sanitized "no route found" errors. diff --git a/server/providers/places/google.js b/server/providers/places/google.js index e20efa899d..c321dc0c4b 100644 --- a/server/providers/places/google.js +++ b/server/providers/places/google.js @@ -71,7 +71,6 @@ const GEOCODE_UPSTREAM_TIMEOUT_MS = 10_000; const GEOCODE_MAX_RESPONSE_BYTES = 256 * 1024; -/** Geocoding statuses that answer the query; the rest report a failure. */ const GEOCODE_ANSWER_STATUSES = new Set(['OK', 'ZERO_RESULTS']); /** Validate a free-text geocoder query before consuming request quota. */ @@ -117,6 +116,9 @@ export function googlePlacesContextProxy({ // Gate first, like the OpenAI routes: a cross-site caller learns nothing // about this endpoint's method surface. if (admitSameSite(req, res)) return; + // Nothing but a confirmed answer is kept: a refusal (bad key, API not + // enabled, quota) would be replayed for five minutes after the key is fixed. + res.setHeader('Cache-Control', 'no-store'); if (req.method !== 'GET') { res.statusCode = 405; res.setHeader('Content-Type', 'application/json'); @@ -132,7 +134,6 @@ export function googlePlacesContextProxy({ if (keyless) { res.statusCode = keyless.statusCode; res.setHeader('Content-Type', 'application/json'); - res.setHeader('Cache-Control', 'no-store'); res.end(JSON.stringify(keyless.payload)); return; } @@ -197,23 +198,30 @@ export function googlePlacesContextProxy({ }), }, ); - const data = await response.json().catch(() => ({})); - const places = projectNearbyPlaces(data, latitude, longitude); + const data = await response.json().catch(() => null); + // A 200 we could not read is an upstream failure, not an empty answer. + if (response.ok && !data) { + res.statusCode = 502; + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.end( + JSON.stringify({ + error: 'Google Places request failed', + places: [], + }), + ); + return; + } + const places = projectNearbyPlaces(data || {}, latitude, longitude); res.statusCode = response.ok ? 200 : response.status; res.setHeader('Content-Type', 'application/json; charset=utf-8'); - // A refusal (bad key, API not enabled, quota) is not kept: the browser - // would replay it for five minutes after the key is fixed. - res.setHeader( - 'Cache-Control', - response.ok ? 'private, max-age=300' : 'no-store', - ); + if (response.ok) res.setHeader('Cache-Control', 'private, max-age=300'); res.end( JSON.stringify({ places, error: response.ok ? null - : data.error?.message || 'Google Places request failed', + : data?.error?.message || 'Google Places request failed', }), ); } catch (error) { @@ -231,10 +239,12 @@ export function googlePlacesContextProxy({ // Text Search: resolve a named landmark/POI to a real coordinate, biased to // the view. Geocoding scatters obscure monument/POI names across the city; // a view-biased Text Search lands on the actual feature. Same key, field - // mask, throttle, and `places: []` error contract as nearby-places above. + // mask, throttle, cache rule, and `places: []` error contract as + // nearby-places above. middlewares.use('/api/google/text-search', async (req, res) => { // Paid like nearby-places, so gated the same way, first. if (admitSameSite(req, res)) return; + res.setHeader('Cache-Control', 'no-store'); if (req.method !== 'GET') { res.statusCode = 405; res.setHeader('Content-Type', 'application/json'); @@ -250,7 +260,6 @@ export function googlePlacesContextProxy({ if (keyless) { res.statusCode = keyless.statusCode; res.setHeader('Content-Type', 'application/json'); - res.setHeader('Cache-Control', 'no-store'); res.end(JSON.stringify(keyless.payload)); return; } @@ -323,23 +332,30 @@ export function googlePlacesContextProxy({ }), }, ); - const data = await response.json().catch(() => ({})); - const places = projectTextSearchPlaces(data, latitude, longitude); + const data = await response.json().catch(() => null); + // A 200 we could not read is an upstream failure, not an empty answer. + if (response.ok && !data) { + res.statusCode = 502; + res.setHeader('Content-Type', 'application/json; charset=utf-8'); + res.end( + JSON.stringify({ + error: 'Google Places request failed', + places: [], + }), + ); + return; + } + const places = projectTextSearchPlaces(data || {}, latitude, longitude); res.statusCode = response.ok ? 200 : response.status; res.setHeader('Content-Type', 'application/json; charset=utf-8'); - // A refusal (bad key, API not enabled, quota) is not kept: the browser - // would replay it for five minutes after the key is fixed. - res.setHeader( - 'Cache-Control', - response.ok ? 'private, max-age=300' : 'no-store', - ); + if (response.ok) res.setHeader('Cache-Control', 'private, max-age=300'); res.end( JSON.stringify({ places, error: response.ok ? null - : data.error?.message || 'Google Places request failed', + : data?.error?.message || 'Google Places request failed', }), ); } catch (error) { @@ -379,6 +395,8 @@ export function googlePlacesContextProxy({ // Gate first, like the Places routes above: a cross-site caller learns // nothing about this endpoint's method surface. if (admitSameSite(req, res)) return; + // As in the Places routes, only the confirmed answer below is kept. + res.setHeader('Cache-Control', 'no-store'); if (req.method !== 'GET') { refuse(405, 'Method not allowed'); return; @@ -392,7 +410,6 @@ export function googlePlacesContextProxy({ if (keyless) { res.statusCode = keyless.statusCode; res.setHeader('Content-Type', 'application/json'); - res.setHeader('Cache-Control', 'no-store'); res.end(JSON.stringify(keyless.payload)); return; } @@ -461,12 +478,8 @@ export function googlePlacesContextProxy({ res.setHeader('Content-Type', 'application/json; charset=utf-8'); // Geocoding reports a denied key or spent quota as HTTP 200 with a // status of its own; only an answer is worth keeping. - res.setHeader( - 'Cache-Control', - response.ok && GEOCODE_ANSWER_STATUSES.has(data.status) - ? 'private, max-age=300' - : 'no-store', - ); + if (response.ok && GEOCODE_ANSWER_STATUSES.has(data.status)) + res.setHeader('Cache-Control', 'private, max-age=300'); res.end(JSON.stringify(data)); } catch { // Fixed public text: a transport, URL or runtime message would diff --git a/src/tooling/placeProviders.test.mjs b/src/tooling/placeProviders.test.mjs index a6a3457536..2a9cf16daa 100644 --- a/src/tooling/placeProviders.test.mjs +++ b/src/tooling/placeProviders.test.mjs @@ -209,7 +209,12 @@ test('a refused Places or Geocoding answer is not kept by the browser', async (t t.mock.method(globalThis, 'fetch', async () => Response.json({ status, results: [] }), ); - for (const denied of ['REQUEST_DENIED', 'OVER_QUERY_LIMIT']) { + for (const denied of [ + 'REQUEST_DENIED', + 'OVER_QUERY_LIMIT', + 'OVER_DAILY_LIMIT', + 'UNKNOWN_ERROR', + ]) { status = denied; const answer = await request('/api/google/geocode', '?address=Austin'); assert.equal(answer.statusCode, 200); @@ -223,6 +228,58 @@ test('a refused Places or Geocoding answer is not kept by the browser', async (t } }); +const ROUTES = [ + ['/api/google/nearby-places', '?lat=30&lon=-97'], + ['/api/google/text-search', '?q=museum&lat=30&lon=-97'], +]; + +const truncated = () => + new Response( + new ReadableStream({ + start(c) { + c.enqueue(new TextEncoder().encode('{"places":[{"displayName"')); + c.error(new TypeError('terminated')); + }, + }), + { status: 200 }, + ); + +for (const [name, body] of [ + ['non-JSON', () => new Response('not json', { status: 200 })], + ['cut mid-body', truncated], +]) { + test(`an unreadable Places 200 (${name}) is a failure, not a cached empty answer`, async () => { + const request = install((middlewares) => + googlePlacesContextProxy({ + resolveApiKey: () => 'fixture-server-key', + fetchImpl: async () => body(), + }).configureServer({ middlewares }), + ); + for (const [route, query] of ROUTES) { + const res = await request(route, query); + assert.equal(res.statusCode, 502, route); + assert.equal(res.headers['cache-control'], 'no-store', route); + assert.deepEqual(res.body.places, [], route); + assert.ok(res.body.error, route); + } + }); +} + +test('a real empty Places answer ({}) stays cacheable', async () => { + const request = install((middlewares) => + googlePlacesContextProxy({ + resolveApiKey: () => 'fixture-server-key', + fetchImpl: async () => Response.json({}), + }).configureServer({ middlewares }), + ); + for (const [route, query] of ROUTES) { + const res = await request(route, query); + assert.equal(res.statusCode, 200, route); + assert.equal(res.headers['cache-control'], 'private, max-age=300', route); + assert.equal(res.body.error, null, route); + } +}); + test('OSRM routing preserves aliases, cache, span guards and upstream failure behavior', async (t) => { const request = install(installRouteMiddleware); let calls = 0; From bdd92663d63ce0a56aa6776b8905da6e0eafb70e Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Thu, 19 Nov 2026 18:59:04 +0400 Subject: [PATCH 15/23] Cancel the Tarktee sibling on a body failure and settle review notes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Each Tarktee DATEX feed now reads its body as soon as its headers arrive, so a failed or oversized body aborts the other request at once instead of waiting for its headers up to the shared 30 s deadline. Status and redirect errors name the feed again. Ontario failures log the error class and system code, never the key-bearing URL or upstream text. DATA_SOURCES.md and the camera licence field use the Open Government Licence – Ontario statement the credit shows, and a test pins that credit. The focus-release rendering test now uses main's icon renderer and its isVideoFeedCamera contract. The Tarktee cancellation tests disable the 30 s deadline and time out after 2 s, so a regression fails fast instead of passing late or hanging; a duplicated older test and an unused import are gone. This PR's notes move into changelog.d/1266-cctv-shared-fix-batch.md: the 1133 fragment described main's tracking probe and terrain check, not this branch. --- DATA_SOURCES.md | 4 +- .../1133-cctv-qa-terrain-drag-fixture.md | 6 - changelog.d/1266-cctv-shared-fix-batch.md | 11 + docs/CURRENT-STATE.md | 18 +- server/providers/cctv/sources.js | 92 ++++---- src/data/cctvEstonia.test.mjs | 214 +++++++++--------- src/data/cctvEstoniaBodyPhase.test.mjs | 92 ++++++++ src/data/dataCredits.test.mjs | 12 + src/layers/cctv/rendering.test.mjs | 73 +++--- 9 files changed, 325 insertions(+), 197 deletions(-) delete mode 100644 changelog.d/1133-cctv-qa-terrain-drag-fixture.md create mode 100644 changelog.d/1266-cctv-shared-fix-batch.md create mode 100644 src/data/cctvEstoniaBodyPhase.test.mjs diff --git a/DATA_SOURCES.md b/DATA_SOURCES.md index f8da1e26c7..79ca2646f5 100644 --- a/DATA_SOURCES.md +++ b/DATA_SOURCES.md @@ -68,7 +68,7 @@ How to read this: | **Caltrans (cwwp2.dot.ca.gov)** | CCTV camera catalogs + frames, California districts | Public Caltrans traffic camera data | "Caltrans — cwwp2.dot.ca.gov" (courtesy) | | **DelDOT (video.deldot.gov)** | CCTV camera catalog (`tmc.deldot.gov/json/videocamera.json`, cached for 15 minutes) + live HTTPS HLS video, Delaware. Keyless | Public DelDOT traffic camera streams via the [DelDOT traffic map](https://deldot.gov/map/) | "DelDOT — Delaware Department of Transportation" (courtesy) | | **TfL Open Data (JamCams)** | CCTV camera catalog + frames, London | [TfL Open Data terms](https://tfl.gov.uk/info-for/open-data-users/) — attribution REQUIRED | "Powered by TfL Open Data. Contains OS data © Crown copyright and database rights" | -| **Ontario 511** | CCTV camera catalog + frames, Ontario highways including Kitchener-area routes | [Open Government Licence – Ontario](https://www.ontario.ca/page/open-government-licence-ontario) | "Ontario 511" plus Open Government Licence - Ontario | +| **Ontario 511** | CCTV camera catalog + frames, Ontario highways including Kitchener-area routes | [Open Government Licence – Ontario](https://www.ontario.ca/page/open-government-licence-ontario) | "Ontario 511. Contains information licensed under the Open Government Licence – Ontario" | | **Fintraffic / Digitraffic (weathercams)** | CCTV camera catalog + frames, Finland | [CC BY 4.0](https://www.digitraffic.fi/en/terms-of-service/) — attribution REQUIRED | "Fintraffic / digitraffic.fi, license CC BY 4.0" | | **DriveBC** (Government of British Columbia) | CCTV camera catalog + frames, British Columbia highways | [Open Government Licence – British Columbia](https://www2.gov.bc.ca/gov/content/data/open-data/open-government-licence-bc) — attribution REQUIRED | "DriveBC. Contains information licensed under the Open Government Licence – British Columbia" | | **Open Calgary** (`data.calgary.ca`) | CCTV camera catalog + frames, Calgary | [Open Government Licence – City of Calgary](https://data.calgary.ca/stories/s/Open-Calgary-Terms-of-Use/u45n-7awa) — attribution REQUIRED | "Contains information licensed under the Open Government Licence – City of Calgary" | @@ -247,7 +247,7 @@ The optional Local ADS-B layer and the broadcast-FM receiver use radio you recei - **Open Vessel Photos.** When you select a vessel with a nine-digit MMSI, the browser asks `https://openvesselphotos.org/v1/mmsi/{mmsi}` directly (the API is keyless and answers cross-origin, so no key and no server proxy are involved; account credentials are never sent) for that vessel only, once per MMSI per session, with an 8-second timeout. A miss (a 404 or a vessel without photos) is remembered for the session; a failed request may be asked again after five minutes. The first photo whose image is an `https://openvesselphotos.org` URL and which names a licence is shown, at the 800 px size, in the CONTEXT panel's CONTACTS view under the vessel's name and MMSI (`src/ui/views/panels/AwarenessPanel.vue`), and nowhere else (`src/data/openVesselPhoto.js`, `src/layers/vessels/photos.js`). The image is hotlinked from the URL the API gives (never copied or re-hosted); the photographer and licence are visible text next to it ("Photo: {photographer} · {licence}", or "Photo · {licence}" when the answer names no one), and the photo and credit link to the vessel's Open Vessel Photos page with `target="_blank" rel="noopener"`. If the image fails to load, the photo and its credit are hidden. - **Launch Library 2.** `/api/launches` makes a server-side rolling-30-day query against the supported v2.3 detailed launch endpoint, caches successful responses for 15 minutes in memory and on disk, and serves the last successful response during a throttle or transient outage. Anonymous access is limited to 15 calls/hour; deployments can provide `LL2_API_TOKEN` for authenticated access. The Space Devs' published terms permit using and sharing the API data in any form, ask users not to forward it without adding value, disclaim complete accuracy, and encourage—but do not require—attribution. This app keeps a courtesy credit. Payload and stage/recovery records are shown only when supplied. Failed launches expose their source status and never receive fallback orbit geometry or a live/estimated marker. LL2 supplies launch context and event timing, not continuous ascent telemetry or live orbital state. - **TfL JamCams.** The camera list comes from the keyless `api.tfl.gov.uk` endpoint (an optional `TFL_APP_KEY` raises its rate limit); frames come from TfL's public S3 bucket. The "Powered by TfL Open Data" attribution is required by TfL's terms and is registered in the Data attribution popover. -- **Ontario 511 CCTV.** The camera list comes from the `511on.ca/api/v2/get/cameras` endpoint, which now requires a free developer key (`ONTARIO_511_API_KEY`: sign up at 511on.ca/my511/register, then request it while logged in at 511on.ca/developers/doc; throttled to 10 calls a minute). Without the key the pack is skipped. Frames are server-registered still images under `511on.ca/map/Cctv/`. The source is live-fetched at runtime and attributed under the Open Government Licence - Ontario. Access is governed by Ontario 511's Developer's Access Agreement (511on.ca/developers/resources): the data stays under the Open Government Licence - Ontario unless otherwise specified; key requests are reviewed case by case and ask for the intended use and any third parties that will receive the data; attribution by link to 511on.ca is encouraged, and the Ontario 511 logo may not be altered or used to imply endorsement (this app links and shows no logo); and the data is provided "as is", a disclaimer distributors should pass on, which the credit does. +- **Ontario 511 CCTV.** The camera list comes from the `511on.ca/api/v2/get/cameras` endpoint, which now requires a free developer key (`ONTARIO_511_API_KEY`: sign up at 511on.ca/my511/register, then request it while logged in at 511on.ca/developers/doc; throttled to 10 calls a minute). Without the key the pack is skipped. Frames are server-registered still images under `511on.ca/map/Cctv/`. The source is live-fetched at runtime and attributed under the Open Government Licence – Ontario. Access is governed by Ontario 511's Developer's Access Agreement (511on.ca/developers/resources): the data stays under the Open Government Licence – Ontario unless otherwise specified; key requests are reviewed case by case and ask for the intended use and any third parties that will receive the data; attribution by link to 511on.ca is encouraged, and the Ontario 511 logo may not be altered or used to imply endorsement (this app links and shows no logo); and the data is provided "as is", a disclaimer distributors should pass on, which the credit does. - **DelDOT live video cameras.** The keyless `tmc.deldot.gov/json/videocamera.json` catalog supplies active cameras and official `https://video.deldot.gov/live/…/playlist.m3u8` links. The local server transiently buffers bounded MPEG-TS segments in memory and deletes them on session release or idle expiry. No recordings are retained. `CCTV_DELDOT_ENABLED=0` disables the pack. Public availability is not a blanket redistribution license; deployment beyond personal local viewing needs confirmation of applicable DelDOT terms. - **Fintraffic weathercams.** The camera list is one keyless GeoJSON request to `tie.digitraffic.fi/api/weathercam/v1/stations` (809 stations / 2,275 presets), sent with the `Digitraffic-User: gods-eye-view` header the service asks clients to identify themselves with and with gzip; frames are stills on `weathercam.digitraffic.fi`, refreshed on each station's 600 s collection interval, which is also the client's ambient-still refresh cadence for this pack (the active camera keeps the usual 10-second refresh). One preset (a fixed camera view) is one camera; stations not `GATHERING` and presets not `inCollection` are dropped. Attribution is required by the CC BY 4.0 licence and is registered in the Data attribution popover. `CCTV_FINTRAFFIC_ENABLED=0` is the kill switch. - **DriveBC highway cameras.** The camera list comes from the keyless `https://www.drivebc.ca/api/webcams/` endpoint served by the DriveBC.ca site ([bcgov/DriveBC.ca](https://github.com/bcgov/DriveBC.ca)); frames come from `https://www.drivebc.ca/images/.jpg`, built from the numeric camera id. DataBC's [DriveBC HighwayCams](https://catalogue.data.gov.bc.ca/dataset/bc-highwaycams) catalogue entry publishes the same cameras under the Open Government Licence – British Columbia, but its CSV still lists the retired `images.drivebc.ca` frame URLs, which now return a placeholder. The licence's attribution statement is required and is registered in the Data attribution popover. By default the 250 cameras nearest Vancouver and Victoria load; `CCTV_DRIVEBC_MAX_SOURCES` changes the cap and `CCTV_DRIVEBC_ENABLED=0` turns the pack off. Some cameras in the feed are supplied by partners (TransLink, the City of Vancouver, the City of Surrey, Parks Canada / Alberta Motor Association and others); the feed's per-camera credit is carried onto the camera record and shown beside the provider in the CCTV panel. diff --git a/changelog.d/1133-cctv-qa-terrain-drag-fixture.md b/changelog.d/1133-cctv-qa-terrain-drag-fixture.md deleted file mode 100644 index 789df9291c..0000000000 --- a/changelog.d/1133-cctv-qa-terrain-drag-fixture.md +++ /dev/null @@ -1,6 +0,0 @@ -- Make CCTV QA ground-sampling checks respect the active terrain regime and - frame the actual calibration gizmo for the drag regression, preserving the - photoreal completion floor and all drag invariants. -- Measure the tracking arrival regression on the first visible frame within - 250 ms, accounting for the 80 ms fleet cadence while rejecting a stale - first-visible orientation instead of retrying until it is correct. diff --git a/changelog.d/1266-cctv-shared-fix-batch.md b/changelog.d/1266-cctv-shared-fix-batch.md new file mode 100644 index 0000000000..898606fafd --- /dev/null +++ b/changelog.d/1266-cctv-shared-fix-batch.md @@ -0,0 +1,11 @@ +- Refuse Ontario 511 catalog redirects and keep the key-bearing request URL + and upstream error text out of failure logs, which now name only the error + class and system code. Credit Ontario 511 with the Open Government + Licence – Ontario attribution statement (asturksever, #1266). +- Abort the other Tarktee (Estonia) DATEX request as soon as one fails, before + its headers or while its body is read, instead of waiting out the shared + 30 s deadline; a response that arrives after the failure is released too + (asturksever, #1266). +- Frame the calibration gizmo before the CCTV QA drag regression drags it; + default autofocus could leave the arrow shorter than the 10 px grab + threshold (asturksever, #1266). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 91c0d7bd0e..661e962e15 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -7540,16 +7540,14 @@ silently demoting every later lookup for the session. empty keeps the old catalog and is retried after another 15 minutes, not on the next request. Estonia's Tarktee DATEX feeds, which answer in 2 to 16.6 s, get 30 s (`TARKTEE_FETCH_TIMEOUT_MS`) where other packs get 15 s. - Ontario catalog requests refuse redirects and log only a fixed diagnostic for - transport/JSON failures, so reflected request URLs or keys cannot reach logs. - Tarktee owns both XML requests with one 30-second deadline and shared abort; - a failed header or body read cancels its sibling, including a response arriving - after cleanup. - Both Tarktee XML catalogs refuse redirects and have a 4 MiB per-document - limit, enforced against declared length and streamed bytes. A rejected fetch, - unsuccessful response, or body-read failure releases both available response - bodies; read failures also cancel an active sibling reader. Cameras sharing - an image number are deduplicated before the Tarktee pack cap is applied. + Ontario catalog requests refuse redirects and log only the error class and + system code for transport/JSON failures, never the request URL or key. + Both Tarktee XML catalogs share that 30 s deadline, refuse redirects and + have a 4 MiB per-document limit, enforced against declared length and + streamed bytes. A failed request, unsuccessful response or body read aborts + the other feed at once and releases every response body, including one that + arrives after the failure. Cameras sharing an image number are deduplicated + before the Tarktee pack cap is applied. - **CCTV v3 UX — viewshed + calibration gizmo** (built 2026-07-05 and field validated 2026-07-21): the COVERAGE toggle is a tri-state cycle `OFF → ON → VIEWSHED`; viewshed mode renders each visible camera's frustum diff --git a/server/providers/cctv/sources.js b/server/providers/cctv/sources.js index 86e8aa2ea9..43cd0f1f3f 100644 --- a/server/providers/cctv/sources.js +++ b/server/providers/cctv/sources.js @@ -561,6 +561,22 @@ export function _resetOntarioKeyWarningForTest() { ontarioKeyMissingLogged = false; } +/** + * What failed in an Ontario download, without upstream text: the error class, + * plus the system code (`ENOTFOUND`) or a refused redirect when there is one. + * @param {unknown} error + * @returns {string} + */ +function ontarioFailureKind(error) { + const name = /^[A-Za-z]{1,40}$/.test(error?.name) ? error.name : 'Error'; + if (error?.cause?.message === 'unexpected redirect') + return `${name}, redirect refused`; + const code = error?.cause?.code; + return typeof code === 'string' && /^[A-Z][A-Z0-9_]{1,40}$/.test(code) + ? `${name} ${code}` + : name; +} + /** * Fetch Ontario 511 CCTV cameras. The camera list comes from the 511 API, * which now requires a free developer key (511on.ca/developers/doc, "Requires a @@ -659,7 +675,7 @@ export async function loadOntarioSourcesFromOpenData( url: view.url, snapshotUrl: view.url, sourceKind: 'ontario-511-open-data', - license: 'Open Government Licence - Ontario', + license: 'Open Government Licence – Ontario', }); } @@ -677,11 +693,11 @@ export async function loadOntarioSourcesFromOpenData( `[CCTV] Loaded Ontario 511 camera sources: ${unique.length} enabled (using nearest ${prioritized.length})`, ); return prioritized; - } catch { - // Transport and JSON errors may reflect the credential-bearing request. - // Do not log upstream exception text, response bodies or URLs. + } catch (error) { + // Transport and JSON errors may reflect the credential-bearing request, + // so only the error class and a system code are logged, never the text. console.warn( - '[CCTV] Ontario 511 camera download error (network or invalid response)', + `[CCTV] Ontario 511 camera download error (${ontarioFailureKind(error)})`, ); return []; } @@ -1280,49 +1296,46 @@ export function parseTarkteeDatexImages(xml) { * @returns {Promise>} Normalized camera source objects. */ export async function loadTarkteeSourcesFromDatex() { - // Own both transports before either can reject. A response that arrives - // after cleanup must also be discarded instead of escaping Promise.all. - const abort = new AbortController(); + // One deadline for both feeds. Whichever read fails first aborts the other, + // so a stalled sibling is not waited on until the deadline. + const sibling = new AbortController(); const signal = AbortSignal.any([ - abort.signal, + sibling.signal, AbortSignal.timeout(TARKTEE_FETCH_TIMEOUT_MS), ]); - const responses = []; - const discardBoth = async () => { - abort.abort(); - await Promise.allSettled( - responses.map((response) => response.body?.cancel()), - ); - return []; - }; - const fetchXml = async (url) => { - const response = await fetch(url, { - headers: { Accept: 'application/xml,text/xml,*/*' }, - redirect: 'manual', - signal, - }); - if (signal.aborted) { - await Promise.allSettled([response.body?.cancel()]); + const readXml = async (url, feed) => { + let response; + try { + response = await fetch(url, { + headers: { Accept: 'application/xml,text/xml,*/*' }, + redirect: 'manual', + signal, + }); signal.throwIfAborted(); - } - responses.push(response); - if (!response.ok) { - throw new Error( - response.status >= 300 && response.status < 400 - ? 'catalog redirected; redirects are not followed' - : `catalog HTTP ${response.status}`, + if (!response.ok) { + throw new Error( + response.status >= 300 && response.status < 400 + ? `${feed} catalog redirected; redirects are not followed` + : `${feed} download failed: ${response.status}`, + ); + } + return await readResponseTextCapped( + response, + TARKTEE_MAX_CATALOG_BYTES, + signal, ); + } catch (error) { + // The join needs both feeds: stop the other one now, and release this + // body, including a response that arrived after the other failed. + sibling.abort(); + await response?.body?.cancel().catch(() => {}); + throw error; } - return response; }; try { - const [locResp, imgResp] = await Promise.all([ - fetchXml(TARKTEE_LOCATIONS_URL), - fetchXml(TARKTEE_IMAGES_URL), - ]); const [locXml, imgXml] = await Promise.all([ - readResponseTextCapped(locResp, TARKTEE_MAX_CATALOG_BYTES, signal), - readResponseTextCapped(imgResp, TARKTEE_MAX_CATALOG_BYTES, signal), + readXml(TARKTEE_LOCATIONS_URL, 'locations'), + readXml(TARKTEE_IMAGES_URL, 'images'), ]); const locations = parseTarkteeDatexLocations(locXml); const images = parseTarkteeDatexImages(imgXml); @@ -1384,7 +1397,6 @@ export async function loadTarkteeSourcesFromDatex() { ); return prioritized; } catch (error) { - await discardBoth(); console.warn( '[CCTV] Tarktee DATEX download error:', error?.message || error, diff --git a/src/data/cctvEstonia.test.mjs b/src/data/cctvEstonia.test.mjs index 945cf189f8..91ca74b898 100644 --- a/src/data/cctvEstonia.test.mjs +++ b/src/data/cctvEstonia.test.mjs @@ -4,7 +4,6 @@ import { TARKTEE_IMAGES_URL, TARKTEE_LOCATIONS_URL, TARKTEE_MAX_CATALOG_BYTES, - TARKTEE_FETCH_TIMEOUT_MS, } from '../../server/providers/cctv/constants.js'; import { loadTallinnSourcesFromCatalog, @@ -13,6 +12,14 @@ import { parseTarkteeDatexLocations, } from '../../server/providers/cctv/sources.js'; +/** + * Disarm the 30 s deadline, so a loader that only stops at that deadline + * fails at the test timeout instead of passing late. + */ +function withoutDeadline(t) { + t.mock.method(AbortSignal, 'timeout', () => new AbortController().signal); +} + /** Silence the loaders' progress and failure logging. */ function quiet(t) { t.mock.method(console, 'log', () => {}); @@ -192,48 +199,6 @@ test('Tarktee loader refuses a redirect and cancels both response bodies', async assert.equal(cancelled.images, true, 'sibling 200 body is also cancelled'); }); -test('Tarktee loader cancels fulfilled bodies when either fetch rejects', async (t) => { - for (const failedUrl of [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL]) { - for (const delayedSibling of [false, true]) { - await t.test( - `${failedUrl}, delayed sibling: ${delayedSibling}`, - async (t) => { - quiet(t); - let cancelled = false; - const response = new Response( - new ReadableStream({ - start(controller) { - controller.enqueue(new TextEncoder().encode('')); - }, - cancel() { - cancelled = true; - }, - }), - ); - t.mock.method(globalThis, 'fetch', async (url) => { - if (String(url) === failedUrl) throw new TypeError('fetch failed'); - if (delayedSibling) await new Promise(setImmediate); - return response; - }); - - try { - assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); - // An aborted mock transport may still deliver its queued response. - if (delayedSibling) await new Promise(setImmediate); - assert.equal( - cancelled, - true, - 'fulfilled sibling body is cancelled', - ); - } finally { - await response.body.cancel(); - } - }, - ); - } - } -}); - test('Tarktee loader cancels the sibling body when one feed fails or overflows the byte cap', async (t) => { quiet(t); let siblingCancelled = false; @@ -292,63 +257,81 @@ test('Tarktee loader cancels the sibling body when one feed fails or overflows t for (const failingUrl of [TARKTEE_LOCATIONS_URL, TARKTEE_IMAGES_URL]) { for (const lateSibling of [false, true]) { - test(`Tarktee cancels a ${lateSibling ? 'late' : 'received'} sibling when ${failingUrl} rejects before headers`, async (t) => { + test( + `Tarktee cancels a ${lateSibling ? 'late' : 'received'} sibling when ${failingUrl} rejects before headers`, + { timeout: 2000 }, + async (t) => { + quiet(t); + withoutDeadline(t); + let cancelled = false; + let siblingSignal; + let release; + const gate = new Promise((resolve) => { + release = resolve; + }); + t.mock.method(globalThis, 'fetch', async (url, init) => { + if (String(url) === failingUrl) + throw new Error('headers unavailable'); + siblingSignal = init.signal; + // A late transport answers once the sibling is aborted, or on release. + if (lateSibling) + await Promise.race([ + gate, + new Promise((resolve) => + init.signal.addEventListener('abort', resolve, { once: true }), + ), + ]); + return new Response( + new ReadableStream({ + start(controller) { + controller.enqueue(new TextEncoder().encode('')); + }, + cancel() { + cancelled = true; + }, + }), + ); + }); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + // Also exercise a response arriving after abort, as a queued transport + // completion can race the rejection owner even when fetch honors abort. + if (lateSibling) { + release(); + await new Promise((resolve) => setImmediate(resolve)); + } + assert.equal(cancelled, true, 'every obtained body must be cancelled'); + assert.equal( + siblingSignal.aborted, + true, + 'a pending transport is aborted immediately', + ); + }, + ); + } + test( + `Tarktee aborts a pending sibling when ${failingUrl} rejects`, + { timeout: 2000 }, + async (t) => { quiet(t); - let cancelled = false; - let siblingSignal; - let release; - const gate = new Promise((resolve) => { - release = resolve; - }); + withoutDeadline(t); + let aborted = false; t.mock.method(globalThis, 'fetch', async (url, init) => { if (String(url) === failingUrl) throw new Error('headers unavailable'); - siblingSignal = init.signal; - if (lateSibling) await gate; - return new Response( - new ReadableStream({ - start(controller) { - controller.enqueue(new TextEncoder().encode('')); + return new Promise((resolve, reject) => { + init.signal.addEventListener( + 'abort', + () => { + aborted = true; + reject(init.signal.reason); }, - cancel() { - cancelled = true; - }, - }), - ); + { once: true }, + ); + }); }); assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); - // Also exercise a response arriving after abort, as a queued transport - // completion can race the rejection owner even when fetch honors abort. - if (lateSibling) { - release(); - await new Promise((resolve) => setImmediate(resolve)); - } - assert.equal(cancelled, true, 'every obtained body must be cancelled'); - assert.equal( - siblingSignal.aborted, - true, - 'a pending transport is aborted immediately', - ); - }); - } - test(`Tarktee aborts a pending sibling when ${failingUrl} rejects`, async (t) => { - quiet(t); - let aborted = false; - t.mock.method(globalThis, 'fetch', async (url, init) => { - if (String(url) === failingUrl) throw new Error('headers unavailable'); - return new Promise((resolve, reject) => { - init.signal.addEventListener( - 'abort', - () => { - aborted = true; - reject(init.signal.reason); - }, - { once: true }, - ); - }); - }); - assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); - assert.equal(aborted, true, 'do not wait for the 30-second deadline'); - }); + assert.equal(aborted, true, 'do not wait for the 30-second deadline'); + }, + ); } test('Tarktee cancels both lengthless streams when one exceeds the byte cap', async (t) => { @@ -379,23 +362,28 @@ test('Tarktee cancels both lengthless streams when one exceeds the byte cap', as ); }); -test('Tarktee aborts pending headers as soon as its sibling returns an HTTP error', async (t) => { - quiet(t); - let aborted = false; - t.mock.method(globalThis, 'fetch', async (url, init) => { - if (String(url) === TARKTEE_LOCATIONS_URL) - return new Response('unavailable', { status: 503 }); - return new Promise((resolve, reject) => { - init.signal.addEventListener( - 'abort', - () => { - aborted = true; - reject(init.signal.reason); - }, - { once: true }, - ); +test( + 'Tarktee aborts pending headers as soon as its sibling returns an HTTP error', + { timeout: 2000 }, + async (t) => { + quiet(t); + withoutDeadline(t); + let aborted = false; + t.mock.method(globalThis, 'fetch', async (url, init) => { + if (String(url) === TARKTEE_LOCATIONS_URL) + return new Response('unavailable', { status: 503 }); + return new Promise((resolve, reject) => { + init.signal.addEventListener( + 'abort', + () => { + aborted = true; + reject(init.signal.reason); + }, + { once: true }, + ); + }); }); - }); - assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); - assert.equal(aborted, true); -}); + assert.deepEqual(await loadTarkteeSourcesFromDatex(), []); + assert.equal(aborted, true); + }, +); diff --git a/src/data/cctvEstoniaBodyPhase.test.mjs b/src/data/cctvEstoniaBodyPhase.test.mjs new file mode 100644 index 0000000000..92c3c80d8a --- /dev/null +++ b/src/data/cctvEstoniaBodyPhase.test.mjs @@ -0,0 +1,92 @@ +// A body-phase failure of one Tarktee DATEX feed must abort the sibling that is +// still waiting for headers instead of leaving it until the 30 s deadline. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; +import http from 'node:http'; +import { + TARKTEE_IMAGES_URL, + TARKTEE_LOCATIONS_URL, + TARKTEE_MAX_CATALOG_BYTES, +} from '../../server/providers/cctv/constants.js'; +import { loadTarkteeSourcesFromDatex } from '../../server/providers/cctv/sources.js'; + +/** The sibling sends its headers this late; the loader must not wait for it. */ +const SIBLING_HEADER_DELAY_MS = 2000; +/** Generous bound for "at once" on a loaded CI machine. */ +const FAIL_FAST_MS = 1000; + +function startServer(handlers) { + const server = http.createServer((req, res) => handlers[req.url](req, res)); + return new Promise((resolve) => { + server.listen(0, '127.0.0.1', () => resolve(server)); + }); +} + +/** Send the two DATEX URLs to the local server through the real fetch. */ +function routeFetchTo(t, port) { + const realFetch = globalThis.fetch; + t.mock.method(globalThis, 'fetch', (url, init) => { + const path = + String(url) === TARKTEE_LOCATIONS_URL + ? '/loc' + : String(url) === TARKTEE_IMAGES_URL + ? '/img' + : null; + assert.ok(path, `unexpected url ${url}`); + return realFetch(`http://127.0.0.1:${port}${path}`, init); + }); +} + +const slowHeaders = (req, res) => { + const timer = setTimeout(() => { + res.writeHead(200, { 'content-type': 'application/xml' }); + res.end(''); + }, SIBLING_HEADER_DELAY_MS); + res.on('close', () => clearTimeout(timer)); +}; + +const failingBodies = { + 'declares a body over the byte cap': (req, res) => { + res.writeHead(200, { + 'content-type': 'application/xml', + 'content-length': String(TARKTEE_MAX_CATALOG_BYTES + 1), + }); + res.write(''); + }, + 'resets the connection mid-body': (req, res) => { + res.writeHead(200, { 'content-type': 'application/xml' }); + res.write(''); + setTimeout(() => res.socket.destroy(), 20); + }, +}; + +for (const [label, failingFeed] of Object.entries(failingBodies)) { + for (const [failing, pending] of [ + ['/loc', '/img'], + ['/img', '/loc'], + ]) { + test(`Tarktee aborts ${pending} pending headers when ${failing} ${label}`, { timeout: 10_000 }, async (t) => { + t.mock.method(console, 'log', () => {}); + t.mock.method(console, 'warn', () => {}); + const server = await startServer({ + [failing]: failingFeed, + [pending]: slowHeaders, + }); + t.after(() => { + server.closeAllConnections(); + server.close(); + }); + routeFetchTo(t, server.address().port); + + const started = Date.now(); + const cameras = await loadTarkteeSourcesFromDatex(); + const elapsed = Date.now() - started; + + assert.deepEqual(cameras, []); + assert.ok( + elapsed < FAIL_FAST_MS, + `loader waited ${elapsed} ms for the sibling's headers instead of aborting it`, + ); + }); + } +} diff --git a/src/data/dataCredits.test.mjs b/src/data/dataCredits.test.mjs index 8036413793..56fdffc6db 100644 --- a/src/data/dataCredits.test.mjs +++ b/src/data/dataCredits.test.mjs @@ -173,6 +173,18 @@ test('the NYC camera credit carries NYC DOT’s requested disclaimer', () => { assert.match(credit.html, /observe posted signage/); }); +test('the Ontario 511 camera credit carries the licence attribution statement', () => { + const credit = DATA_CREDITS.find((entry) => entry.key === 'ontario-511-cctv'); + assert.ok(credit, 'Ontario 511 camera frames must be credited'); + assert.ok( + credit.html.includes( + 'Contains information licensed under the Open Government Licence – Ontario<\/a>/); + assert.match(credit.html, /Provided as is, without warranty/); +}); + test("the Houston camera credit names TranStar's terms and the permission they require", () => { const credit = DATA_CREDITS.find((entry) => entry.key === 'houston-cctv'); assert.ok(credit, 'Houston TranStar camera frames must be credited'); diff --git a/src/layers/cctv/rendering.test.mjs b/src/layers/cctv/rendering.test.mjs index 307d05050c..7bd0a84d7c 100644 --- a/src/layers/cctv/rendering.test.mjs +++ b/src/layers/cctv/rendering.test.mjs @@ -378,14 +378,22 @@ test('a pose with no perspective field of view is projected by the renderer', () }); test('focus release restores video hue and alpha after the style throttle', () => { + const viewer = {}; + const camera = fakeCameraPort({ + pose: { position: { x: 6378137 + 1000, y: 0, z: 0 } }, + }); + const { unbind } = bindFakeDraw(viewer, { + camera: camera.port, + query: { project: () => ({ x: 10, y: 20 }) }, + }); let target = {}; const records = ['mp4', 'webm'].map((feedType) => ({ camera: { id: feedType, feedType }, - billboard: { show: true, color: LIVE_CAMERA_COLOR }, + position: { x: 6378137, y: 0, z: 0 }, })); const state = { _enabled: true, - _viewer: { scene: {}, camera: {} }, + _viewer: viewer, _records: records, _activeCameraId: null, _activeFocusStyleCount: 0, @@ -402,30 +410,43 @@ test('focus release restores video hue and alpha after the style throttle', () = }), focusAlphaNeedsWrite: (before, after) => before !== after, }; - const { refreshCctvFocusStyles } = createRendering({ - state, - services: { focus }, - parts: { model: { isVideoFeedType } }, - source: {}, - }); + const { createCameraIcons, addCameraIcon, refreshCctvFocusStyles } = + createRendering({ + state, + services: { focus }, + parts: { + model: { + isVideoFeedCamera: (camera) => isVideoFeedType(camera.feedType), + }, + }, + source: {}, + }); + try { + createCameraIcons(viewer); + for (const record of records) addCameraIcon(record); + const icons = state._icons; - refreshCctvFocusStyles(1000); - for (const record of records) { - assertFadedFrom( - record.billboard.color, - LIVE_CAMERA_COLOR, - record.camera.id, - ); + refreshCctvFocusStyles(1000); + for (const record of records) { + assertKeepsColor( + icons, + record.camera.id, + LIVE_CAMERA_COLOR, + record.camera.id, + ); + } + target = null; + refreshCctvFocusStyles(1050); + for (const record of records) { + assert.equal(icons.focus(record.camera.id), FOCUS_FACTOR, 'throttled'); + } + refreshCctvFocusStyles(1100); + for (const record of records) { + assert.deepEqual(icons.item(record.camera.id).color, LIVE_CAMERA_COLOR); + assert.equal(icons.focus(record.camera.id), 1); + } + assert.equal(state._activeFocusStyleCount, 0); + } finally { + unbind(); } - const dimmed = records.map((record) => record.billboard.color); - target = null; - refreshCctvFocusStyles(1050); - for (let i = 0; i < records.length; i += 1) { - assert.equal(records[i].billboard.color, dimmed[i]); - } - refreshCctvFocusStyles(1100); - for (const record of records) { - assert.deepEqual(record.billboard.color, LIVE_CAMERA_COLOR); - } - assert.equal(state._activeFocusStyleCount, 0); }); From 11e1c9d26d879110ac87a24d4394e805f74ca714 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Thu, 19 Nov 2026 19:06:53 +0400 Subject: [PATCH 16/23] CCTV Street View fallback: the browser's pose at the catalog position 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. --- SECURITY.md | 4 +- .../1602-cctv-streetview-fallback-catalog.md | 10 +- docs/CURRENT-STATE.md | 11 +- server/providers/cctv.js | 51 ++++--- src/data/cctvMediaFeedType.test.mjs | 48 +++++- src/data/cctvStreetViewPose.test.mjs | 141 ++++++++++++++++++ src/layers/cctv/source.js | 2 - src/layers/cctv/source.test.mjs | 4 +- 8 files changed, 235 insertions(+), 36 deletions(-) create mode 100644 src/data/cctvStreetViewPose.test.mjs diff --git a/SECURITY.md b/SECURITY.md index 7b894c346f..fb2890e174 100644 --- a/SECURITY.md +++ b/SECURITY.md @@ -68,7 +68,7 @@ browser-key fallback for existing single-key setups. The data proxies under `server/providers/` are written so the browser cannot turn the server into an open relay: -- **No arbitrary-URL fetching.** The CCTV frame proxy fetches only server-registered camera/frame URLs — clients cannot pass an upstream URL to fetch (SSRF mitigation). Other proxies target fixed upstream hosts. +- **No arbitrary-URL fetching.** The CCTV frame proxy fetches only server-registered camera/frame URLs — clients cannot pass an upstream URL to fetch (SSRF mitigation). The key-bearing Street View fallback likewise answers only for a registered camera, always at its catalog position: a request sets only the view direction, field of view (20–120°) and pitch (−40–20°), so it cannot buy Street View of a place of its choosing. Other proxies target fixed upstream hosts. - **Live HLS is bounded and origin-pinned.** Registered HTTP(S) HLS sources use a Node puller that rejects redirects and off-origin playlist references, bounds playlists and segment bodies while streaming, and aborts timed-out or released sessions. At most two sessions retain 12 segments/24 MiB each, plus bounded download buffers. Stores are memory-only and idle sessions expire after 15 seconds. RTMP/ffmpeg execution is not enabled. Locally configured source URLs remain an operator trust boundary. - **Radio is not an audio relay.** `/api/radio/stations` contacts only allowlisted Radio Browser HTTPS hosts and paths, rejects redirects, rejects any hostname with a loopback/private/link-local/metadata/non-public A or AAAA result, and pins each TLS connection to a validated address. It returns normalized public HTTPS stream URLs; `/api/radio/click/:uuid` applies the same destination policy and accepts only station IDs from the current bounded catalog. The browser then connects directly to the broadcaster after an explicit playback action, so the broadcaster sees the listener's IP address. GEV never proxies, caches, records, or redistributes audio. - **No verbatim client headers upstream.** The CCTV media route is the one route that relays a request header (`Range`, for video seeking). It is parsed and canonicalized before it is forwarded: one `bytes=` range only, with every accepted form — explicit span, open-ended and suffix — bounded to the same 64 MiB ceiling the relay applies to a declared response body. Multi-range, malformed, inverted and non-`bytes` values are dropped and the request proceeds without a `Range`, as RFC 7233 §3.1 prescribes. Bounding the request bounds what is asked for: a response that declares no length — live streamed media, or a chunked body from an upstream that ignores the `Range` — has no ceiling. An upstream request the browser has stopped waiting for is cancelled rather than left running, whether the viewer leaves before the headers arrive or during the body. @@ -95,7 +95,7 @@ The dev server is a **key broker**: every server-side key above is spendable by - **Local-only by default.** `./scripts/dev-fresh.sh` (and the Vite config itself) bind to `localhost`, so only your machine can reach the server — and only local names are accepted (`allowedHosts` stays restricted, which also blunts DNS-rebinding tricks). Binding to all interfaces keeps that restriction: IP addresses and `localhost` work, and LAN hostnames must be listed in `GEV_ALLOWED_HOSTS` (for example `GEV_ALLOWED_HOSTS=globe.lan`); suffix and wildcard entries are ignored. - **LAN exposure is an explicit opt-in**: `HOST=0.0.0.0 ./scripts/dev-fresh.sh`. The launcher prints a prominent warning plus your LAN URL. Understand what opting in means: **every device on that network can drive the proxies and spend your OpenAI / Google / OpenSky / AISStream / TomTom / FIRMS quota** for as long as the server runs. Do this only on networks you trust. -- **App-level throttles (on by default):** `GEV_RATELIMIT_OPENAI_PER_MIN` (default `30`) and `GEV_RATELIMIT_GOOGLE_PER_MIN` (default `120`) — the values the Pinokio build already ships — cap the cost-bearing endpoints per client IP per minute (over-limit requests receive a sanitized `429`). Typed chat keeps its own per-IP window, set by `GEV_RATELIMIT_OPENAI_CHAT_PER_MIN` and `GEV_RATELIMIT_GEMINI_CHAT_PER_MIN`; left unset, each takes its provider's resolved cap (`GEV_RATELIMIT_OPENAI_PER_MIN`, or `GEV_RATELIMIT_GEMINI_PER_MIN`, default `30`), so lowering a provider throttle lowers chat too, while an explicit chat value, including `0`, wins. Both defaults sit well above what the app itself generates; set either to exactly `0` to run unthrottled. They are **per-IP, process-local, in-memory guards** — they reset on restart, a caller with several addresses gets a bucket per address, and they are **not billing caps**. +- **App-level throttles (on by default):** `GEV_RATELIMIT_OPENAI_PER_MIN` (default `30`) and `GEV_RATELIMIT_GOOGLE_PER_MIN` (default `120`) — the values the Pinokio build already ships — cap the cost-bearing endpoints per client IP per minute (over-limit requests receive a sanitized `429`). The CCTV Street View fallback has its own per-IP window on `GEV_RATELIMIT_GOOGLE_PER_MIN`, separate from the Places and Geocoding one; over it, a frame gets the placeholder instead of a `429`. Typed chat keeps its own per-IP window, set by `GEV_RATELIMIT_OPENAI_CHAT_PER_MIN` and `GEV_RATELIMIT_GEMINI_CHAT_PER_MIN`; left unset, each takes its provider's resolved cap (`GEV_RATELIMIT_OPENAI_PER_MIN`, or `GEV_RATELIMIT_GEMINI_PER_MIN`, default `30`), so lowering a provider throttle lowers chat too, while an explicit chat value, including `0`, wins. Both defaults sit well above what the app itself generates; set either to exactly `0` to run unthrottled. They are **per-IP, process-local, in-memory guards** — they reset on restart, a caller with several addresses gets a bucket per address, and they are **not billing caps**. - **Docker trusts one address, and only because the port is on loopback.** Docker delivers the host's own browser to the container from the container network's gateway, never from `127.0.0.1`, so a loopback-only Provider diff --git a/changelog.d/1602-cctv-streetview-fallback-catalog.md b/changelog.d/1602-cctv-streetview-fallback-catalog.md index 04574b9142..194923b264 100644 --- a/changelog.d/1602-cctv-streetview-fallback-catalog.md +++ b/changelog.d/1602-cctv-streetview-fallback-catalog.md @@ -1,5 +1,5 @@ -- The CCTV Street View fallback serves only catalog cameras, at their catalog - position, heading, field of view and pitch, and ignores coordinates a - request supplies. It spends from the per-client Google budget - (`GEV_RATELIMIT_GOOGLE_PER_MIN`, default 120), so the Google key can no - longer be used as an unmetered Street View service (asturksever, #1602). +- The CCTV Street View fallback serves only catalog cameras, always at their + catalog position, and ignores coordinates a request supplies. It has its own + per-client limit on the Google setting (`GEV_RATELIMIT_GOOGLE_PER_MIN`, + default 120), so the Google key can no longer be used as an unmetered Street + View service (asturksever, #1602). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 019e25704e..d7f60479ed 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -1277,9 +1277,14 @@ redirects on those hosts, are pinned by parsed URL to the agency's image directory. Every `/api/cctv` route refuses cross-site browser requests but accepts forwarding headers, so cameras work behind a reverse proxy; a forwarded request skips the key-bearing Street View fallback and gets the placeholder. The -Street View fallback serves only catalog cameras, at the catalog position, -heading, field of view and pitch, never coordinates a request supplies, and it -shares the per-client Google budget (`GEV_RATELIMIT_GOOGLE_PER_MIN`, default 120). +Street View fallback serves only catalog cameras, always at the catalog +position; coordinates a request supplies are ignored. Heading, field of view +and pitch come from the request, the pose the browser draws with calibration +included, and fall back to the catalog pose. A camera the server does not list, +such as a bundled seed camera the browser shows when the server catalog is +empty, gets the placeholder. The fallback has its own per-client limit on the +Google setting (`GEV_RATELIMIT_GOOGLE_PER_MIN`, default 120, `0` disables it); +a request over it gets the placeholder. The CCTV layer draws only the cameras on or near the screen. Zoomed out, it keeps one camera per ground cell sized to the view, preferring a healthy feed, diff --git a/server/providers/cctv.js b/server/providers/cctv.js index e8e18ad51e..3eb94dbb2f 100644 --- a/server/providers/cctv.js +++ b/server/providers/cctv.js @@ -21,19 +21,6 @@ import { createHlsPuller } from './cctv/stream.js'; import { googleServerApiKey } from './places/google-key.js'; import { clientKey, makeCostRateLimiter } from './common/rate-limit.js'; export { CCTV_FRAME_FETCH_TIMEOUT_MS, fetchCctvImageFromUpstream }; -let _streetViewRateLimiter; -/** - * Per-IP budget for the Street View fallback, on the same setting as the - * other Google routes (GEV_RATELIMIT_GOOGLE_PER_MIN; 0 disables it). - */ -function streetViewRateLimiter() { - if (_streetViewRateLimiter === undefined) - _streetViewRateLimiter = makeCostRateLimiter( - process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, - 120, - ); - return _streetViewRateLimiter; -} /** * Vite plugin: CCTV camera proxy with source registry, frame/media serving, @@ -96,6 +83,22 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { }; }; + /** + * Per-IP budget for the Street View fallback, one per proxy, on the same + * setting as the other Google routes (GEV_RATELIMIT_GOOGLE_PER_MIN; 0 + * disables it). Built on first use, after the standalone env has loaded. + * undefined = not built yet; null = the explicit 0 opt-out. + */ + let streetViewBudget; + const streetViewRateLimiter = () => { + if (streetViewBudget === undefined) + streetViewBudget = makeCostRateLimiter( + process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, + 120, + ); + return streetViewBudget; + }; + /** * Fetch a Google Street View static image as a fallback frame. Server-side * call, never reaches the browser — prefers GOOGLE_MAPS_SERVER_API_KEY @@ -524,6 +527,13 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { const source = sourceById.get(cameraId); const label = url.searchParams.get('label') || source?.name || cameraId; const city = url.searchParams.get('city') || source?.city || ''; + // The view follows the pose the browser draws (calibration included); + // the position never comes from the query (see the Street View fallback). + const heading = Number( + url.searchParams.get('heading') || source?.headingDeg, + ); + const fov = Number(url.searchParams.get('fov') || source?.fovDeg); + const pitch = Number(url.searchParams.get('pitch') || source?.pitchDeg); // Only use server-registered upstream URLs — never accept client-supplied URLs // (prevents SSRF via ?upstream= query parameter) @@ -557,8 +567,9 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { // request (whose client this server cannot see) gets the placeholder, // as if no key were configured. Any forwarding header counts, even an // empty one. - // It is also only for catalog cameras, framed from the catalog (never - // a spot the query names), within the per-IP Google budget. + // It is also only for catalog cameras, at the catalog position (never a + // spot the query names; the query sets only the view), within the per-IP + // Google budget. const forwarded = PROXY_SIGNALS.some( (name) => req.headers?.[name] !== undefined, ); @@ -571,11 +582,11 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) { (googleBudget && !googleBudget(clientKey(req))) ? null : await streetViewFallback({ - lat: Number(source.lat), - lon: Number(source.lon), - heading: Number(source.headingDeg), - fov: Number(source.fovDeg), - pitch: Number(source.pitchDeg ?? -10), + lat: source.lat, + lon: source.lon, + heading, + fov, + pitch, }); if (sv?.ok) { setHealth(cameraId, { diff --git a/src/data/cctvMediaFeedType.test.mjs b/src/data/cctvMediaFeedType.test.mjs index 0523c95f39..191b3015ed 100644 --- a/src/data/cctvMediaFeedType.test.mjs +++ b/src/data/cctvMediaFeedType.test.mjs @@ -111,6 +111,7 @@ function mount(t, respond) { austin: process.env.CCTV_FORCE_AUSTIN, serverKey: process.env.GOOGLE_MAPS_SERVER_API_KEY, browserKey: process.env.GOOGLE_MAPS_API_KEY, + rateLimit: process.env.GEV_RATELIMIT_GOOGLE_PER_MIN, }; process.env.CCTV_SOURCES_JSON = JSON.stringify([ STILL, @@ -125,6 +126,8 @@ function mount(t, respond) { // than a network call this file does not model. delete process.env.GOOGLE_MAPS_SERVER_API_KEY; delete process.env.GOOGLE_MAPS_API_KEY; + // The default Google budget, whatever the shell sets. + delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN; const nativeFetch = globalThis.fetch; const requests = []; globalThis.fetch = async (url, init = {}) => { @@ -139,6 +142,7 @@ function mount(t, respond) { ['CCTV_FORCE_AUSTIN', before.austin], ['GOOGLE_MAPS_SERVER_API_KEY', before.serverKey], ['GOOGLE_MAPS_API_KEY', before.browserKey], + ['GEV_RATELIMIT_GOOGLE_PER_MIN', before.rateLimit], ]) { if (value === undefined) delete process.env[name]; else process.env[name] = value; @@ -155,9 +159,9 @@ function mount(t, respond) { }); assert.ok(handler, 'the CCTV provider must mount a middleware'); - const call = async (path, headers = {}) => { + const call = async (path, headers = {}, socket) => { const { res, done } = recordingResponse(); - await handler({ url: path, headers, method: 'GET', on() {} }, res); + await handler({ url: path, headers, method: 'GET', on() {}, socket }, res); await done; return res; }; @@ -554,4 +558,44 @@ test('the Street View fallback frames catalog cameras only, from the catalog pos assert.equal(steered.headers['X-CCTV-Source'], 'streetview'); const location = new URL(streetView()[0].url).searchParams.get('location'); assert.equal(location, `${STILL.lat},${STILL.lon}`); + assert.equal(new URL(streetView()[0].url).searchParams.get('heading'), '90'); +}); + +test('the Street View fallback spends a per-IP Google budget, and only with a key', async (t) => { + const app = mount(t, ({ url }) => + url.startsWith('https://maps.googleapis.com/') + ? trackedUpstream({ + headers: { 'content-type': 'image/jpeg' }, + body: 'street-view', + }).response + : upstreamResponse({ status: 503 }), + ); + // Read when the first keyed fallback builds the limiter, so after mount(). + process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = '2'; + const streetViewCalls = () => + app.requests.filter(({ url }) => + url.startsWith('https://maps.googleapis.com/'), + ).length; + const first = { remoteAddress: '203.0.113.7' }; + const frameSource = async (socket) => + (await app.call(`/frame/${STILL.id}`, {}, socket)).headers['X-CCTV-Source']; + + // Without a key nothing is spent. + for (let i = 0; i < 3; i++) + assert.equal(await frameSource(first), 'synthetic'); + assert.equal(streetViewCalls(), 0); + + process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key'; + assert.deepEqual( + [await frameSource(first), await frameSource(first), await frameSource(first)], + ['streetview', 'streetview', 'synthetic'], + ); + assert.equal(streetViewCalls(), 2, 'no Google call over the budget'); + + // Another address has a budget of its own. + assert.equal( + await frameSource({ remoteAddress: '203.0.113.8' }), + 'streetview', + ); + assert.equal(streetViewCalls(), 3); }); diff --git a/src/data/cctvStreetViewPose.test.mjs b/src/data/cctvStreetViewPose.test.mjs new file mode 100644 index 0000000000..584fe8629d --- /dev/null +++ b/src/data/cctvStreetViewPose.test.mjs @@ -0,0 +1,141 @@ +// When a catalog camera has no pose, the browser sends /frame a heading derived +// from the camera ID, a pitch of -17°, and a field of view of 74°. Street View +// uses that pose at the camera's catalog coordinates. +import { test } from 'node:test'; +import assert from 'node:assert/strict'; + +import { cctvProxy } from '../../server/providers/cctv.js'; + +// Same shape as a world-cameras row: an image URL, position, and no +// headingDeg / pitchDeg / fovDeg. +const POSELESS = { + id: 'world-or-fixture', + name: 'Pose-less fixture', + lat: 44.05, + lon: -123.09, + feedType: 'image', + url: 'https://cams.upstream.invalid/still.jpg', +}; + +/** The browser's id-hash heading (src/layers/cctv/model.js headingFromId). */ +function browserHeadingFromId(id) { + let acc = 0; + for (let i = 0; i < id.length; i++) acc = (acc * 33 + id.charCodeAt(i)) >>> 0; + return ((((acc % 16) * 22.5) % 360) + 360) % 360; +} + +function response({ status = 200, headers = {}, body = '' }) { + return new Response(status === 204 ? null : String(body), { + status, + headers, + }); +} + +function recording() { + const chunks = []; + let finished; + const done = new Promise((resolve) => (finished = resolve)); + const res = { + statusCode: 0, + headers: {}, + writableEnded: false, + writeHead(status, headers) { + res.statusCode = status; + res.headers = { ...res.headers, ...headers }; + }, + setHeader(name, value) { + res.headers[name] = value; + }, + write(chunk) { + chunks.push(Buffer.from(chunk)); + return true; + }, + end(chunk) { + if (chunk) chunks.push(Buffer.from(chunk)); + res.writableEnded = true; + res.body = Buffer.concat(chunks).toString('utf8'); + finished(); + }, + on() {}, + once() {}, + emit() {}, + destroy() {}, + }; + return { res, done }; +} + +test('the Street View fallback frames a pose-less catalog camera the way the browser draws it', async (t) => { + const saved = Object.fromEntries( + [ + 'CCTV_SOURCES_JSON', + 'CCTV_SOURCES_FILE', + 'CCTV_FORCE_AUSTIN', + 'GOOGLE_MAPS_SERVER_API_KEY', + 'GOOGLE_MAPS_API_KEY', + 'GEV_RATELIMIT_GOOGLE_PER_MIN', + ].map((name) => [name, process.env[name]]), + ); + process.env.CCTV_SOURCES_JSON = JSON.stringify([POSELESS]); + process.env.CCTV_SOURCES_FILE = 'absent-source-file.json'; + process.env.CCTV_FORCE_AUSTIN = '0'; + process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key'; + delete process.env.GOOGLE_MAPS_API_KEY; + delete process.env.GEV_RATELIMIT_GOOGLE_PER_MIN; + const nativeFetch = globalThis.fetch; + const streetView = []; + globalThis.fetch = async (url) => { + const href = String(url); + if (href.startsWith('https://maps.googleapis.com/')) { + streetView.push(new URL(href)); + return response({ + headers: { 'content-type': 'image/jpeg' }, + body: 'street-view', + }); + } + return response({ status: 503 }); // the camera's upstream is down + }; + t.after(() => { + globalThis.fetch = nativeFetch; + for (const [name, value] of Object.entries(saved)) { + if (value === undefined) delete process.env[name]; + else process.env[name] = value; + } + }); + + let handler; + cctvProxy().configureServer({ + middlewares: { use: (_route, fn) => (handler = fn) }, + }); + + // What src/layers/cctv/source.js sends for this camera with no calibration. + const heading = Math.round(browserHeadingFromId(POSELESS.id)); + const params = new URLSearchParams({ + label: POSELESS.name, + city: '', + lat: POSELESS.lat.toFixed(6), + lon: POSELESS.lon.toFixed(6), + heading: String(heading), + fov: '74', + pitch: '-17', + ts: '1', + }); + const { res, done } = recording(); + await handler( + { + url: `/frame/${POSELESS.id}?${params}`, + headers: {}, + method: 'GET', + on() {}, + }, + res, + ); + await done; + + assert.equal(res.headers['X-CCTV-Source'], 'streetview'); + assert.equal(streetView.length, 1); + const sv = streetView[0].searchParams; + assert.equal(sv.get('location'), `${POSELESS.lat},${POSELESS.lon}`); + assert.equal(Number(sv.get('heading')), heading, 'heading the browser draws'); + assert.equal(Number(sv.get('fov')), 74, 'FOV the browser draws'); + assert.equal(Number(sv.get('pitch')), -17, 'pitch the browser draws'); +}); diff --git a/src/layers/cctv/source.js b/src/layers/cctv/source.js index 3b628954b4..78fd5cb194 100644 --- a/src/layers/cctv/source.js +++ b/src/layers/cctv/source.js @@ -22,8 +22,6 @@ function frameUrlFor(camera, refreshMs = ACTIVE_FRAME_REFRESH_MS) { const params = new URLSearchParams({ label: camera.name, city: camera.city, - lat: camera.lat.toFixed(6), - lon: camera.lon.toFixed(6), heading: String(Math.round(camera.headingDeg)), fov: String(Math.round(camera.fovDeg)), pitch: String(Math.round(camera.pitchDeg || -10)), diff --git a/src/layers/cctv/source.test.mjs b/src/layers/cctv/source.test.mjs index a4b276d213..61e36c3321 100644 --- a/src/layers/cctv/source.test.mjs +++ b/src/layers/cctv/source.test.mjs @@ -81,8 +81,8 @@ test('frame and media URLs preserve registered camera identity and encoded metad ); assert.equal(frame.searchParams.get('label'), camera.name); assert.equal(frame.searchParams.get('city'), camera.city); - assert.equal(frame.searchParams.get('lat'), '30.267000'); - assert.equal(frame.searchParams.get('lon'), '-97.744000'); + assert.equal(frame.searchParams.has('lat'), false); + assert.equal(frame.searchParams.has('lon'), false); assert.equal(frame.searchParams.get('heading'), '45'); assert.equal(frame.searchParams.get('pitch'), '-12'); assert.deepEqual([...media.searchParams.keys()], ['ts']); From 75f1a3c1814903d022980dd165e6789d006005f1 Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Thu, 19 Nov 2026 18:06:58 +0000 Subject: [PATCH 17/23] Frame the port-drawn gizmo in the CCTV QA drag fixture and share the catalog-pair reader The drag-fixture framing looked the gizmo up in viewer.entities, which main no longer populates, so it did nothing and five drag checks stayed inconclusive. It now reads the arrow from the layer's drawn state and records the framed shaft length as a check. Tarktee and Lithuania read their two catalogs through one helper, readCatalogsTogether, so both get the shared deadline, sibling abort and the release of a response that arrives after the other feed failed. An Ontario error response has its body released, and tests pin the refused redirect and system-code log labels. --- changelog.d/1266-cctv-shared-fix-batch.md | 8 ++-- docs/CURRENT-STATE.md | 7 ++- scripts/format-scope.json | 1 + scripts/package-boundaries.json | 1 + scripts/qa-cctv-v2.mjs | 27 ++++++++---- server/providers/cctv/catalogPair.js | 51 ++++++++++++++++++++++ server/providers/cctv/lithuania.js | 52 ++++++---------------- server/providers/cctv/ontario511.test.mjs | 52 ++++++++++++++++++++++ server/providers/cctv/sources.js | 53 ++++++----------------- 9 files changed, 160 insertions(+), 92 deletions(-) create mode 100644 server/providers/cctv/catalogPair.js diff --git a/changelog.d/1266-cctv-shared-fix-batch.md b/changelog.d/1266-cctv-shared-fix-batch.md index 898606fafd..f35f3ac82c 100644 --- a/changelog.d/1266-cctv-shared-fix-batch.md +++ b/changelog.d/1266-cctv-shared-fix-batch.md @@ -1,10 +1,12 @@ - Refuse Ontario 511 catalog redirects and keep the key-bearing request URL and upstream error text out of failure logs, which now name only the error - class and system code. Credit Ontario 511 with the Open Government - Licence – Ontario attribution statement (asturksever, #1266). + class and system code; release the body of an error response. Credit + Ontario 511 with the Open Government Licence – Ontario attribution statement + (asturksever, #1266). - Abort the other Tarktee (Estonia) DATEX request as soon as one fails, before its headers or while its body is read, instead of waiting out the shared - 30 s deadline; a response that arrives after the failure is released too + 30 s deadline; a response that arrives after the failure is released too. + The Lithuania pack reads its two catalogs through the same helper (asturksever, #1266). - Frame the calibration gizmo before the CCTV QA drag regression drags it; default autofocus could leave the arrow shorter than the 10 px grab diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 661e962e15..d0c06f38c8 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -7546,8 +7546,11 @@ silently demoting every later lookup for the session. have a 4 MiB per-document limit, enforced against declared length and streamed bytes. A failed request, unsuccessful response or body read aborts the other feed at once and releases every response body, including one that - arrives after the failure. Cameras sharing an image number are deduplicated - before the Tarktee pack cap is applied. + arrives after the failure. The Lithuania pack reads its two catalogs + through the same helper (`readCatalogsTogether`, + `server/providers/cctv/catalogPair.js`) with its 15 s deadline. An Ontario + error response has its body released. Cameras sharing an image number are + deduplicated before the Tarktee pack cap is applied. - **CCTV v3 UX — viewshed + calibration gizmo** (built 2026-07-05 and field validated 2026-07-21): the COVERAGE toggle is a tri-state cycle `OFF → ON → VIEWSHED`; viewshed mode renders each visible camera's frustum diff --git a/scripts/format-scope.json b/scripts/format-scope.json index e86ae6c4ea..3c846d449b 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -148,6 +148,7 @@ "server/providers/cctv/groundHeights.js", "server/providers/cctv/catalog.js", "server/providers/cctv/lithuania.js", + "server/providers/cctv/catalogPair.js", "src/data/cctvLithuania.test.mjs", "server/providers/cctv/taiwan.js", "src/data/cctvTaiwan.test.mjs", diff --git a/scripts/package-boundaries.json b/scripts/package-boundaries.json index 31245c7cf2..d600ec7446 100644 --- a/scripts/package-boundaries.json +++ b/scripts/package-boundaries.json @@ -563,6 +563,7 @@ "server/providers/cctv/groundHeights.js", "server/providers/cctv/catalog.js", "server/providers/cctv/lithuania.js", + "server/providers/cctv/catalogPair.js", "server/providers/cctv/taiwan.js", "server/providers/cctv/quebec.js", "server/providers/cctv/ottawa.js", diff --git a/scripts/qa-cctv-v2.mjs b/scripts/qa-cctv-v2.mjs index 734fb98fd6..652de38b7d 100644 --- a/scripts/qa-cctv-v2.mjs +++ b/scripts/qa-cctv-v2.mjs @@ -1792,31 +1792,40 @@ async function main() { // Frame the actual gizmo for the drag fixture. Default autofocus can leave // its shaft below the 10px interaction threshold; that distant view is not - // the drag-usability scenario this regression exercises. + // the drag-usability scenario this regression exercises. The gizmo is drawn + // through the renderer ports, so its arrow is read from the layer's drawn + // state (plain {x,y,z} points), not from viewer.entities. const dragFraming = await page.evaluate(async () => { const viewer = window.__godsEyeView.viewer; - const e = viewer.entities.getById('cctv-gizmo-move-east'); - if (!e) return null; - const pts = e.polyline.positions.getValue(viewer.clock.currentTime); - const target = pts?.[0]; - if (!target) return null; + const mod = window.__godsEyeView.dataManager.layers.get('cctv').module; + const arrow = () => + mod._cctvDrawnForTest().gizmo['cctv-gizmo-move-east']?.positions; + if (!arrow()?.length) return null; + const Cartesian3 = viewer.camera.positionWC.constructor; const measure = () => { + const pts = arrow(); + if (!pts?.length) return { shaftPx: null, rangeM: null }; const a = viewer.scene.cartesianToCanvasCoordinates(pts[0]); const b = viewer.scene.cartesianToCanvasCoordinates(pts[pts.length - 1]); return { shaftPx: a && b ? Math.hypot(b.x - a.x, b.y - a.y) : null, - rangeM: target.constructor.distance(viewer.camera.positionWC, target), + rangeM: Cartesian3.distance(viewer.camera.positionWC, pts[0]), }; }; const before = measure(); + const target = Cartesian3.clone(arrow()[0]); viewer.camera.cancelFlight(); - viewer.camera.lookAt(target, new target.constructor(0, -200, 180)); + viewer.camera.lookAt(target, new Cartesian3(0, -200, 180)); viewer.camera.lookAtTransform(viewer.camera.transform.constructor.IDENTITY); viewer.scene.requestRender(); const frames = await window.__qaRenderFrames(3, 10000); return { before, after: measure(), frames }; }); - console.log('Drag fixture framing:', JSON.stringify(dragFraming)); + record( + 'drag fixture frames the east arrow above the 10px grab threshold', + dragFraming?.after?.shaftPx >= 10, + JSON.stringify(dragFraming), + ); // Synthetic CDP drag on the EAST arrow — the owner's field-test // regression. Sample effective elevation after every mouse move: it must diff --git a/server/providers/cctv/catalogPair.js b/server/providers/cctv/catalogPair.js new file mode 100644 index 0000000000..b6f0ffa901 --- /dev/null +++ b/server/providers/cctv/catalogPair.js @@ -0,0 +1,51 @@ +import { readResponseTextCapped } from '../common/http.js'; + +/** + * Read catalogs that are only useful together, because the pack joins them. + * + * The requests share one deadline and refuse redirects. Whichever fails first + * (before its headers, on its status, or while its capped body is read) aborts + * the others at once, so a stalled feed is not waited on until the deadline. + * Every response body is released, including one that arrives after another + * feed has already failed. + * + * @param {Array<{url: string, label: string}>} feeds `label` names the feed + * in the error ("locations download failed: 503"). + * @param {{accept: string, maxBytes: number, timeoutMs: number}} options + * @returns {Promise} The body texts, in feed order. + */ +export async function readCatalogsTogether( + feeds, + { accept, maxBytes, timeoutMs }, +) { + const sibling = new AbortController(); + const signal = AbortSignal.any([ + sibling.signal, + AbortSignal.timeout(timeoutMs), + ]); + return Promise.all( + feeds.map(async ({ url, label }) => { + let response; + try { + response = await fetch(url, { + headers: { Accept: accept }, + redirect: 'manual', + signal, + }); + signal.throwIfAborted(); + if (!response.ok) { + throw new Error( + response.status >= 300 && response.status < 400 + ? `${label} catalog redirected; redirects are not followed` + : `${label} download failed: ${response.status}`, + ); + } + return await readResponseTextCapped(response, maxBytes, signal); + } catch (error) { + sibling.abort(); + await response?.body?.cancel().catch(() => {}); + throw error; + } + }), + ); +} diff --git a/server/providers/cctv/lithuania.js b/server/providers/cctv/lithuania.js index 840e68c6cf..5ce67d411b 100644 --- a/server/providers/cctv/lithuania.js +++ b/server/providers/cctv/lithuania.js @@ -4,7 +4,7 @@ import { fallbackHeadingFromId, prioritizeSources, } from './normalize.js'; -import { readResponseJsonCapped } from '../common/http.js'; +import { readCatalogsTogether } from './catalogPair.js'; const BASE = 'https://eismoinfo.lt/eismoinfo-backend'; export const LITHUANIA_CAMERAS_URL = `${BASE}/camera-info-table`; @@ -81,46 +81,22 @@ export function lithuaniaCameraToSource(row, feature) { }; } -async function readCatalog(url, sibling) { - const signal = AbortSignal.any([ - AbortSignal.timeout(CCTV_SOURCE_FETCH_TIMEOUT_MS), - sibling.signal, - ]); - try { - const response = await fetch(url, { - headers: { Accept: 'application/json' }, - redirect: 'manual', - signal, - }); - if (!response.ok) { - await response.body?.cancel().catch(() => {}); - throw new Error( - response.status >= 300 && response.status < 400 - ? 'catalog redirected; redirects are not followed' - : `Catalog HTTP ${response.status}`, - ); - } - return await readResponseJsonCapped( - response, - LITHUANIA_MAX_CATALOG_BYTES, - signal, - ); - } catch (error) { - // The join needs both catalogs; release the other request at once - // rather than leaving its body open until the timeout. - sibling.abort(); - throw error; - } -} - /** Two bounded requests per shared catalog refresh, no API key required. */ export async function loadLithuaniaSources() { try { - const sibling = new AbortController(); - const [rows, layers] = await Promise.all([ - readCatalog(LITHUANIA_CAMERAS_URL, sibling), - readCatalog(LITHUANIA_LOCATIONS_URL, sibling), - ]); + const [rows, layers] = ( + await readCatalogsTogether( + [ + { url: LITHUANIA_CAMERAS_URL, label: 'cameras' }, + { url: LITHUANIA_LOCATIONS_URL, label: 'locations' }, + ], + { + accept: 'application/json', + maxBytes: LITHUANIA_MAX_CATALOG_BYTES, + timeoutMs: CCTV_SOURCE_FETCH_TIMEOUT_MS, + }, + ) + ).map((text) => JSON.parse(text)); if (!Array.isArray(rows) || !Array.isArray(layers)) return []; const features = layers.find((layer) => layer?.layer === 'VKR')?.features; if (!Array.isArray(features)) return []; diff --git a/server/providers/cctv/ontario511.test.mjs b/server/providers/cctv/ontario511.test.mjs index 9a8ba0e643..2f24388df9 100644 --- a/server/providers/cctv/ontario511.test.mjs +++ b/server/providers/cctv/ontario511.test.mjs @@ -167,3 +167,55 @@ test('Ontario encodes reserved key characters and refuses credential-bearing red assert.equal(JSON.stringify(cameras).includes('synthetic'), false); assert.equal(lines.join(' ').includes('synthetic'), false); }); + +test('Ontario names a refused redirect and a system code without the request text', async (t) => { + const lines = captureConsole(t); + const key = 'synthetic-secret'; + const failures = [ + // The shape Node's fetch gives a refused redirect and a DNS failure. + new TypeError('fetch failed', { + cause: new Error('unexpected redirect'), + }), + new TypeError('fetch failed', { + cause: Object.assign(new Error(`getaddrinfo ENOTFOUND ${key}`), { + code: 'ENOTFOUND', + }), + }), + ]; + t.mock.method(globalThis, 'fetch', async () => { + throw failures.shift(); + }); + for (let i = 0; i < 2; i++) { + assert.deepEqual( + await loadOntarioSourcesFromOpenData({}, { ONTARIO_511_API_KEY: key }), + [], + ); + } + assert.deepEqual(lines, [ + '[CCTV] Ontario 511 camera download error (TypeError, redirect refused)', + '[CCTV] Ontario 511 camera download error (TypeError ENOTFOUND)', + ]); +}); + +test('Ontario releases the body of an error response', async (t) => { + captureConsole(t); + let cancelled = false; + t.mock.method( + globalThis, + 'fetch', + async () => + new Response( + new ReadableStream({ + cancel() { + cancelled = true; + }, + }), + { status: 503 }, + ), + ); + assert.deepEqual( + await loadOntarioSourcesFromOpenData({}, { ONTARIO_511_API_KEY: 'k' }), + [], + ); + assert.equal(cancelled, true); +}); diff --git a/server/providers/cctv/sources.js b/server/providers/cctv/sources.js index 43cd0f1f3f..d7df49530a 100644 --- a/server/providers/cctv/sources.js +++ b/server/providers/cctv/sources.js @@ -172,6 +172,7 @@ import { pinnedCameraUrl, } from './normalize.js'; import { directionToHeading } from '../../../src/data/directionText.js'; +import { readCatalogsTogether } from './catalogPair.js'; import { readResponseBytesCapped, readResponseJsonCapped, @@ -621,6 +622,7 @@ export async function loadOntarioSourcesFromOpenData( console.warn( `[CCTV] Ontario 511 camera download failed: ${resp.status}${keyHint}`, ); + await resp.body?.cancel().catch(() => {}); return []; } const rows = await resp.json(); @@ -1296,47 +1298,18 @@ export function parseTarkteeDatexImages(xml) { * @returns {Promise>} Normalized camera source objects. */ export async function loadTarkteeSourcesFromDatex() { - // One deadline for both feeds. Whichever read fails first aborts the other, - // so a stalled sibling is not waited on until the deadline. - const sibling = new AbortController(); - const signal = AbortSignal.any([ - sibling.signal, - AbortSignal.timeout(TARKTEE_FETCH_TIMEOUT_MS), - ]); - const readXml = async (url, feed) => { - let response; - try { - response = await fetch(url, { - headers: { Accept: 'application/xml,text/xml,*/*' }, - redirect: 'manual', - signal, - }); - signal.throwIfAborted(); - if (!response.ok) { - throw new Error( - response.status >= 300 && response.status < 400 - ? `${feed} catalog redirected; redirects are not followed` - : `${feed} download failed: ${response.status}`, - ); - } - return await readResponseTextCapped( - response, - TARKTEE_MAX_CATALOG_BYTES, - signal, - ); - } catch (error) { - // The join needs both feeds: stop the other one now, and release this - // body, including a response that arrived after the other failed. - sibling.abort(); - await response?.body?.cancel().catch(() => {}); - throw error; - } - }; try { - const [locXml, imgXml] = await Promise.all([ - readXml(TARKTEE_LOCATIONS_URL, 'locations'), - readXml(TARKTEE_IMAGES_URL, 'images'), - ]); + const [locXml, imgXml] = await readCatalogsTogether( + [ + { url: TARKTEE_LOCATIONS_URL, label: 'locations' }, + { url: TARKTEE_IMAGES_URL, label: 'images' }, + ], + { + accept: 'application/xml,text/xml,*/*', + maxBytes: TARKTEE_MAX_CATALOG_BYTES, + timeoutMs: TARKTEE_FETCH_TIMEOUT_MS, + }, + ); const locations = parseTarkteeDatexLocations(locXml); const images = parseTarkteeDatexImages(imgXml); if (!locations.size || !images.size) { From a6eecee1570f9d31468d3485096c3a7c14ffea91 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Fri, 20 Nov 2026 01:10:42 +0400 Subject: [PATCH 18/23] Let the camera picker pick the opening CCTV camera The picker preselected the active camera, and a native select fires no change for the option it already shows. So while CCTV was still following the view, choosing the camera the picker showed did nothing, and the next pan switched to another camera. The layer now reports whether the active camera is the opening one (activeCameraProvisional). While it is, the picker shows that camera from a hidden, disabled first option, so choosing it from the list is a change and ends automatic selection; closing the list keeps it. A same-camera pick that ends the opening pick notifies listeners so the picker drops the hidden option, and reopenNearestCamera passes { opening: true } so its own notification already carries the opening state. docs/CURRENT-STATE.md now says when automatic selection ends. Tests cover the hidden option, confirming and dismissing the picker before a pan, the notification, and focusNearestToPoint, which is now on main. --- changelog.d/1702-cctv-nearest-same-camera.md | 3 +- docs/CURRENT-STATE.md | 12 + src/data/cctvExplicitPickEndsOpening.test.mjs | 37 ++-- src/layers/cctv/navigation.js | 5 +- src/layers/cctv/presentation.js | 4 + src/layers/cctv/selection.js | 14 +- src/layers/cctv/selection.test.mjs | 13 ++ src/ui/cctv/CctvPicker.vue | 11 +- src/ui/cctvPicker.test.mjs | 24 ++ src/ui/cctvPickerOpening.test.mjs | 205 ++++++++++++++++++ src/ui/cctvPresentation.js | 12 +- src/ui/stores/cctv.js | 5 +- 12 files changed, 315 insertions(+), 30 deletions(-) create mode 100644 src/ui/cctvPickerOpening.test.mjs diff --git a/changelog.d/1702-cctv-nearest-same-camera.md b/changelog.d/1702-cctv-nearest-same-camera.md index 63833e6ca6..660f59c589 100644 --- a/changelog.d/1702-cctv-nearest-same-camera.md +++ b/changelog.d/1702-cctv-nearest-same-camera.md @@ -1,4 +1,5 @@ - Keep the CCTV camera picked with NEAREST, from the camera list or by a scene when it is the camera the view just moved to. The pick did not stick: NEAREST's own flight, or the next pan, switched to another camera, so the - panel showed a camera nobody had picked (gekh, #1702). + panel showed a camera nobody had picked. Choosing the camera the list already + shows now counts as a pick too (gekh, #1702). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 51dd971fe0..96484b78b8 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2000,6 +2000,18 @@ The view pass reruns while the camera moves, paced by its own cost. The camera picker lists the cameras in view, active camera first, and closes with the count of cameras outside the view. +Turning CCTV on opens on the camera nearest the view. Until someone picks a +camera, each settled move, such as a shared view's flight landing, moves it to +the camera then nearest the view. Automatic selection ends at the first pick: +NEAREST, PREV or NEXT, a camera chosen in the picker, a click on a camera, a +click on empty ground that deselects, an auto-hop step, or a call to +`selectCamera`, `focusNearest`, `focusNearestToPoint` or +`setParams({ selectedCameraId })`, which voice and scenes use. Picking the +camera that is already active counts. While the camera is the automatic one, +the picker shows it from a hidden first option, so choosing that camera from +the list is a change and ends automatic selection; closing the list without a +choice keeps it. + Traffic owns its rendered-surface observer only while enabled, removes it on disable/destroy, and rebuilds no cached heights on frames where the surface identity is unchanged. Its dots and heat-lines are two renderer draw sets, it animates on the frame port's preRender, and it follows the camera through the camera port at its own 5% threshold rather than the shared `percentageChanged` ledger. The surface identity is the globe port's `surfaceVersion()` and tile readiness its `isSettled('surface')`, so the layer no longer reaches Cesium. Heat-lines are always asked for draped: where the Cesium renderer cannot drape (no depth textures) its draw port draws them (and every other draped line) over the surface, in their colour where the surface hides them and hidden past the horizon, recolouring a line in place when only its colour changes, rather than the layer dropping them. Traffic and bikeshare expose factories through `./layers/traffic` and diff --git a/src/data/cctvExplicitPickEndsOpening.test.mjs b/src/data/cctvExplicitPickEndsOpening.test.mjs index cfe6498880..fdb207e4c9 100644 --- a/src/data/cctvExplicitPickEndsOpening.test.mjs +++ b/src/data/cctvExplicitPickEndsOpening.test.mjs @@ -11,6 +11,7 @@ import cctvLayer, { _setCctvOverlayHostForTest, reopenNearestCamera, } from './cctv.js'; +import { bindFakeDraw } from '../testSupport/fakeDrawPort.mjs'; const BASE = { headingDeg: 41, @@ -39,19 +40,9 @@ function record(id, lat, lon) { } function viewerAt(lat, lon) { - return { - entities: new Cesium.EntityCollection(), + const viewer = { isDestroyed: () => false, - trackedEntity: null, - camera: { - positionWC: Cesium.Cartesian3.fromDegrees(lon, lat, 300), - positionCartographic: Cesium.Cartographic.fromDegrees(lon, lat, 300), - heading: 0.4, - pitch: -0.7, - roll: 0, - transform: Cesium.Matrix4.clone(Cesium.Matrix4.IDENTITY), - flyToBoundingSphere() {}, - }, + eye: { lat, lon, height: 300 }, scene: { canvas: { clientWidth: 1200, clientHeight: 800 }, cartesianToCanvasCoordinates: () => undefined, @@ -59,14 +50,20 @@ function viewerAt(lat, lon) { requestRender() {}, }, }; + // The layer reads the eye and flies through the renderer's camera port. + viewer.draw = bindFakeDraw(viewer, { + camera: { + pose: () => ({ ...viewer.eye }), + on: () => () => {}, + followed: null, + flyToBounds() {}, + }, + }).draw; + return viewer; } function moveViewer(viewer, lat, lon) { - viewer.camera.positionCartographic = Cesium.Cartographic.fromDegrees( - lon, - lat, - 300, - ); + viewer.eye = { lat, lon, height: 300 }; } function withReopenedLayer(fn) { @@ -98,6 +95,7 @@ function withReopenedLayer(fn) { moveViewer(viewer, 51.5131, -0.1291); assert.equal(reopenNearestCamera(), true); assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge'); + assert.equal(cctvLayer.getUIState().activeCameraProvisional, true); fn(viewer); } finally { globalThis.document = originalDocument; @@ -114,11 +112,16 @@ for (const [name, pick] of [ 'setParams({ selectedCameraId })', () => cctvLayer.setParams({ selectedCameraId: 'b-cambridge' }), ], + [ + 'focusNearestToPoint', + () => cctvLayer.focusNearestToPoint(51.513, -0.129, { focus: false }), + ], ]) { test(`an explicit ${name} pick of the re-opened camera ends the opening pick`, () => { withReopenedLayer((viewer) => { pick(); assert.equal(cctvLayer.getUIState().activeCameraId, 'b-cambridge'); + assert.equal(cctvLayer.getUIState().activeCameraProvisional, false); // A later settled move (a pan, or the landing of the pick's own flight) // leaves the explicit choice alone. moveViewer(viewer, 51.5081, -0.1281); diff --git a/src/layers/cctv/navigation.js b/src/layers/cctv/navigation.js index 6b2ba96aaf..7f99142839 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -73,9 +73,8 @@ export function createNavigation({ return false; const nearest = nearestCameraIdToViewer(); if (!nearest || nearest === layerState._activeCameraId) return false; - parts.selection.setActiveCamera(nearest); - // setActiveCamera ends the opening pick; this is still that pick. - layerState._openingCameraProvisional = true; + // Still the opening pick: its notification tells the picker so. + parts.selection.setActiveCamera(nearest, { opening: true }); return true; } diff --git a/src/layers/cctv/presentation.js b/src/layers/cctv/presentation.js index f23c007334..cc398ea5f1 100644 --- a/src/layers/cctv/presentation.js +++ b/src/layers/cctv/presentation.js @@ -286,6 +286,10 @@ export function createPresentation({ hoverId: layerState._hoverCardId, }, activeCameraId: activeId, + // The camera CCTV opened on, following the view until someone picks one. + activeCameraProvisional: Boolean( + activeId && layerState._openingCameraProvisional, + ), activeCameraReselection: layerState._activeCameraReselection, activeCamera: active ? getPublicCameraState(active, activeId) : null, cameraIndex: cameraIndex(), diff --git a/src/layers/cctv/selection.js b/src/layers/cctv/selection.js index 7ec4e90166..7e147834a3 100644 --- a/src/layers/cctv/selection.js +++ b/src/layers/cctv/selection.js @@ -49,10 +49,13 @@ export function createSelection({ * Sets the active camera by ID, initializes its projection runtime, refreshes * its frame, and updates styles. * @param {string} cameraId - ID of the camera to activate. + * @param {Object} [options] + * @param {boolean} [options.opening=false] - The opening camera following + * the view, not someone's pick: settled moves may still replace it. * @returns {'activated'|'unchanged'|'not-found'} Discriminated activation result. */ - function setActiveCamera(cameraId) { + function setActiveCamera(cameraId, { opening = false } = {}) { if (!cameraId || !layerState._recordById.has(cameraId)) return CCTV_ACTIVATION_RESULT.NOT_FOUND; const record = layerState._recordById.get(cameraId); @@ -71,19 +74,22 @@ export function createSelection({ ) ) { // Picking the camera a settled move re-opened is still a choice: later - // settled moves must leave it alone. - layerState._openingCameraProvisional = false; + // settled moves must leave it alone, and the picker learns it. + const endsOpening = layerState._openingCameraProvisional && !opening; + if (endsOpening) layerState._openingCameraProvisional = false; // Browser-only feeds have a separate player; a reselect retries it // without re-running activation or touching the projection geometry. if (parts.model.isBrowserOverlayCamera(record.camera)) { layerState._activeCameraReselection += 1; parts.presentation.notifyListeners(); + } else if (endsOpening) { + parts.presentation.notifyListeners(); } return CCTV_ACTIVATION_RESULT.UNCHANGED; } layerState._activeCameraId = cameraId; layerState._autoHopSuspended = false; - layerState._openingCameraProvisional = false; + layerState._openingCameraProvisional = opening; // A real activation creates projection work — wake the self-stopping loop. parts.projection.startProjectionLoop(); if (previousActiveRecord && previousActiveRecord !== record) { diff --git a/src/layers/cctv/selection.test.mjs b/src/layers/cctv/selection.test.mjs index 92ee536706..96be823b0a 100644 --- a/src/layers/cctv/selection.test.mjs +++ b/src/layers/cctv/selection.test.mjs @@ -52,3 +52,16 @@ test('same-camera selection notifies browser-overlay feeds so playback can retry assert.equal(fixture.state._activeCameraReselection, 1); assert.equal(fixture.notifications(), 1); }); + +test('same-camera selection ends the opening pick and tells the panel once', () => { + const fixture = makeSelection('tripcheck-odot'); + fixture.state._openingCameraProvisional = true; + + assert.equal( + fixture.selection.setActiveCamera('same-camera'), + CCTV_ACTIVATION_RESULT.UNCHANGED, + ); + assert.equal(fixture.state._openingCameraProvisional, false); + assert.equal(fixture.state._activeCameraReselection, 0); + assert.equal(fixture.notifications(), 1); +}); diff --git a/src/ui/cctv/CctvPicker.vue b/src/ui/cctv/CctvPicker.vue index a4b1a077cb..efe611e50b 100644 --- a/src/ui/cctv/CctvPicker.vue +++ b/src/ui/cctv/CctvPicker.vue @@ -5,7 +5,9 @@ import { useCctvStore } from '../stores/cctv.js'; // The camera picker: the cameras in view and a closing note for those the // view leaves out. It renders only when CctvControls publishes a picker, -// which re-applies the active camera, as on every layer notification. +// which re-applies the active camera, as on every layer notification. The +// opening camera, which follows the view until someone picks one, shows in a +// hidden first option: choosing that camera from the list is a change. const { picker } = storeToRefs(useCctvStore()); // The selection, applied once the options are in place. @@ -21,6 +23,13 @@ const vPick = { mounted: pick, updated: pick };