fix(cctv): release snapshot readers and verify provider error paths

This commit is contained in:
Sameh Khamis committed 2026-09-12 16:37:04 -07:00
1 parent 3d956177ab
commit 24d2582516
5 files changed
+138 -19

No files matched your search

+26 -17
View File
@@ -157,19 +157,7 @@ export async function proxyMediaResponse(
stream.pipe(res); stream.pipe(res);
} }
/** /** Read a snapshot incrementally, retaining at most maxBytes of owned chunks. */
* Fetch one upstream CCTV image within the frame-refresh budget.
*
* A timeout is treated like every other upstream miss so the caller can
* continue through the Street View and synthetic fallback chain. `fetchImpl`
* and `timeoutMs` are injectable only to keep the timeout contract unit-testable.
*
* @param {string} url - Server-registered upstream image URL.
* @param {object} [options]
* @param {typeof fetch} [options.fetchImpl=fetch] - Fetch implementation.
* @param {number} [options.timeoutMs=CCTV_FRAME_FETCH_TIMEOUT_MS] - Abort timeout.
* @returns {Promise<{ok:true,body:Buffer,contentType:string}|null>}
*/
async function readCappedResponseBytes(upstream, maxBytes) { async function readCappedResponseBytes(upstream, maxBytes) {
const declared = Number(upstream.headers.get('content-length')); const declared = Number(upstream.headers.get('content-length'));
if (Number.isFinite(declared) && declared > maxBytes) { if (Number.isFinite(declared) && declared > maxBytes) {
@@ -213,6 +201,7 @@ async function readCappedResponseBytes(upstream, maxBytes) {
? upstream.body.getReader() ? upstream.body.getReader()
: null; : null;
if (!reader) return null; if (!reader) return null;
try {
for (;;) { for (;;) {
const { done, value } = await reader.read(); const { done, value } = await reader.read();
if (done) break; if (done) break;
@@ -220,14 +209,18 @@ async function readCappedResponseBytes(upstream, maxBytes) {
try { try {
await reader.cancel(); await reader.cancel();
} catch { } catch {
/* no-op */ /* already closed */
} }
return null; return null;
} }
} }
return Buffer.concat(chunks, total); return Buffer.concat(chunks, total);
} finally {
reader.releaseLock();
}
} }
/** Open registered media within a header deadline; leave timely live bodies running. */
export async function fetchCctvMediaUpstream( export async function fetchCctvMediaUpstream(
url, url,
{ {
@@ -245,14 +238,26 @@ export async function fetchCctvMediaUpstream(
} }
} }
/**
* Fetch one upstream CCTV image within the frame-refresh budget.
*
* A timeout is treated like every other upstream miss so the caller can
* continue through the Street View and synthetic fallback chain. `fetchImpl`
* and `timeoutMs` are injectable only to keep the timeout contract unit-testable.
*
* @param {string} url - Server-registered upstream image URL.
* @param {object} [options]
* @param {typeof fetch} [options.fetchImpl=fetch] - Fetch implementation.
* @param {number} [options.timeoutMs=CCTV_FRAME_FETCH_TIMEOUT_MS] - Abort timeout.
* @param {number} [options.maxBytes=CCTV_FRAME_MAX_BODY_BYTES] - Snapshot byte cap.
* @returns {Promise<{ok:true,body:Buffer,contentType:string}|null>}
*/
export async function fetchCctvImageFromUpstream( export async function fetchCctvImageFromUpstream(
url, url,
{ {
fetchImpl = fetch, fetchImpl = fetch,
timeoutMs = CCTV_FRAME_FETCH_TIMEOUT_MS, timeoutMs = CCTV_FRAME_FETCH_TIMEOUT_MS,
maxBytes = CCTV_FRAME_MAX_BODY_BYTES, maxBytes = CCTV_FRAME_MAX_BODY_BYTES,
CCTV_MEDIA_FETCH_TIMEOUT_MS,
CCTV_MEDIA_MAX_BODY_BYTES,
} = {}, } = {},
) { ) {
if (!url || !/^https?:\/\//i.test(url)) return null; if (!url || !/^https?:\/\//i.test(url)) return null;
@@ -268,7 +273,10 @@ export async function fetchCctvImageFromUpstream(
signal: controller.signal, signal: controller.signal,
}); });
const contentType = upstream.headers.get('content-type') || ''; const contentType = upstream.headers.get('content-type') || '';
if (!upstream.ok || !contentType.startsWith('image/')) return null; if (!upstream.ok || !contentType.startsWith('image/')) {
controller.abort();
return null;
}
const body = await readCappedResponseBytes(upstream, maxBytes); const body = await readCappedResponseBytes(upstream, maxBytes);
if (!body) return null; if (!body) return null;
return { ok: true, body, contentType }; return { ok: true, body, contentType };
@@ -276,5 +284,6 @@ export async function fetchCctvImageFromUpstream(
return null; return null;
} finally { } finally {
clearTimeout(timeoutId); clearTimeout(timeoutId);
controller.abort();
} }
} }
+1
View File
@@ -21,6 +21,7 @@ function googleRateLimiter() {
return _googleRateLimiter; return _googleRateLimiter;
} }
/** Validate raw lat/lon presence and WGS84 bounds before consuming request quota. */
export function validatePlacesCoordinates(searchParams) { export function validatePlacesCoordinates(searchParams) {
const rawLat = searchParams.get('lat'); const rawLat = searchParams.get('lat');
const rawLon = searchParams.get('lon'); const rawLon = searchParams.get('lon');
+47
View File
@@ -212,3 +212,50 @@ test('CCTV upstream frame fetch refuses a body it cannot stream instead of buffe
}); });
assert.equal(result, null); assert.equal(result, null);
}); });
test('reader-only snapshots release their lock on success, overflow and read failure', async () => {
for (const mode of ['success', 'overflow', 'failure']) {
const body = new ReadableStream({
pull(controller) {
if (mode === 'failure') controller.error(new Error('read failed'));
else { controller.enqueue(new Uint8Array(4)); controller.close(); }
},
});
Object.defineProperty(body, Symbol.asyncIterator, { value: undefined });
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
maxBytes: mode === 'overflow' ? 2 : 8,
fetchImpl: async () => ({ ok: true, headers: new Headers({ 'content-type': 'image/jpeg' }), body }),
});
assert.equal(body.locked, false, mode);
assert.equal(result?.body.length ?? null, mode === 'success' ? 4 : null);
}
});
test('the snapshot deadline remains active after headers while reading a stalled body', async () => {
let signal;
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
timeoutMs: 20,
fetchImpl: async (_url, init) => {
signal = init.signal;
return new Response(new ReadableStream({ start(controller) {
signal.addEventListener('abort', () => controller.error(signal.reason), { once: true });
}}), { headers: { 'content-type': 'image/jpeg' } });
},
});
assert.equal(result, null);
assert.equal(signal.aborted, true);
});
test('rejected snapshot responses abort the upstream download', async () => {
for (const [status, contentType] of [[503, 'image/jpeg'], [200, 'text/html']]) {
let signal;
const result = await fetchCctvImageFromUpstream('https://example.com/frame.jpg', {
fetchImpl: async (_url, init) => {
signal = init.signal;
return new Response('rejected', { status, headers: { 'content-type': contentType } });
},
});
assert.equal(result, null);
assert.equal(signal.aborted, true);
}
});
+15
View File
@@ -89,3 +89,18 @@ test('both Places routes answer bad coordinates with a 400 before the limiter an
else process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = previousLimit; else process.env.GEV_RATELIMIT_GOOGLE_PER_MIN = previousLimit;
} }
}); });
test('preview validates both Places routes and keeps the keyless response', async () => {
for (const key of ['', 'test-key']) {
const routes = new Map();
googlePlacesContextProxy({ resolveApiKey: () => key }).configurePreviewServer({
middlewares: { use: (path, handler) => routes.set(path, handler) },
});
for (const name of ['nearby-places', 'text-search']) {
const result = await invokeRoute(routes.get(`/api/google/${name}`), { url: '/?q=capitol&lat=&lon=', remoteAddress: 'preview-test' });
assert.equal(result.statusCode, key ? 400 : 200);
assert.deepEqual(result.body.places, []);
if (!key) assert.equal(result.body.configured, false);
}
}
});
+49 -2
View File
@@ -7,9 +7,9 @@ import { cctvProxy } from '../../server/providers/cctv.js';
import { radioBrowserProxy } from '../../server/providers/radio.js'; import { radioBrowserProxy } from '../../server/providers/radio.js';
import { localProviderPlugins } from '../../server/providers/local.js'; import { localProviderPlugins } from '../../server/providers/local.js';
function install(plugin) { function install(plugin, hook = 'configureServer') {
let handler; let handler;
plugin.configureServer({ plugin[hook]({
middlewares: { middlewares: {
use(_route, fn) { use(_route, fn) {
handler = fn; handler = fn;
@@ -102,3 +102,50 @@ test('composition creates exactly one CCTV and radio provider without acquisitio
assert.equal(plugins.filter((p) => p.name === factory().name).length, 1); assert.equal(plugins.filter((p) => p.name === factory().name).length, 1);
} }
}); });
for (const hook of ['configureServer', 'configurePreviewServer']) {
test(`CCTV ${hook} maps header timeouts and cancels upstream error bodies`, async (t) => {
isolate(t);
process.env.CCTV_SOURCES_JSON = JSON.stringify([
{
id: 'bounded',
name: 'Test',
lat: 30,
lon: -97,
feedType: 'video',
url: 'https://camera.example.org/live.mp4',
},
]);
const request = install(
cctvProxy({ sourceRoot: fixture(t, 'unused') }),
hook,
);
// Resolve the catalog before substituting transport behavior.
await request('/sources');
t.mock.method(globalThis, 'fetch', async () => {
throw new DOMException('timeout', 'AbortError');
});
const timeout = await request('/media/bounded');
assert.equal(timeout.status, 504);
assert.deepEqual(JSON.parse(timeout.body), {
error: 'Upstream media timeout',
});
let cancelled = false;
t.mock.method(
globalThis,
'fetch',
async () =>
new Response(
new ReadableStream({
cancel() {
cancelled = true;
},
}),
{ status: 503 },
),
);
const error = await request('/media/bounded');
assert.equal(error.status, 503);
assert.equal(cancelled, true);
});
}