mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
Merge pull request #1267 from bilawalsidhu/codex/fix-cctv-session-admission
fix(cctv): keep camera health on HLS capacity refusals and test leases through the middleware
This commit is contained in:
12 files changed
+499
-247
No files matched your search
@@ -0,0 +1,5 @@
|
||||
- Stop reporting a live HLS camera as degraded when a viewer is refused because
|
||||
every viewer or session slot is taken: the refusal answers 503 with
|
||||
`Retry-After`, the frame shown in its place leaves the camera's health
|
||||
as it was, and that viewer's panel labels the frame `FALLBACK · DEGRADED`
|
||||
rather than live (asturksever, gekh, #1267).
|
||||
@@ -9923,7 +9923,12 @@ its paint loop. Camera deactivation destroys the decoder, and fatal playback
|
||||
errors revert to the labeled frame fallback. Camera changes clear the old panel
|
||||
frame. hls.js is imported only when an HLS camera starts. Two live sessions may
|
||||
coexist with at most eight independent consumer leases each; no-access lease
|
||||
expiry is 15 seconds. Closing one consumer leaves other consumers running. Lease
|
||||
expiry is 15 seconds. A viewer refused because every lease or session slot is
|
||||
taken gets 503 with `Retry-After: 2` and leaves the camera's health unchanged;
|
||||
so does its frame fallback, since an HLS camera without a snapshot has no frame
|
||||
of its own. That viewer's panel badge reads FALLBACK · DEGRADED and its meta
|
||||
line drops the live health message, since the shared live health does not
|
||||
describe the frame it shows. Closing one consumer leaves other consumers running. Lease
|
||||
ids use `crypto.randomUUID` where the page is a secure context and an equivalent
|
||||
v4 id from `crypto.getRandomValues` on plain-HTTP LAN pages. Encrypted/fMP4/byte-range playlists fail
|
||||
closed. Performance and live agency reliability require recorded validation.
|
||||
|
||||
+38
-26
@@ -18,7 +18,7 @@ import {
|
||||
import { sanitizeCctvRangeHeader } from './cctv/range.js';
|
||||
import { admitSameSiteKeyless } from './common/same-site.js';
|
||||
import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs';
|
||||
import { createHlsPuller } from './cctv/stream.js';
|
||||
import { createHlsPuller, HlsCapacityError } from './cctv/stream.js';
|
||||
import { isMlitRiverAdjustmentImage } from './cctv/worldCameras.js';
|
||||
import { ottawaAuthorizedFrameUrl } from './cctv/ottawa.js';
|
||||
import {
|
||||
@@ -28,6 +28,10 @@ import {
|
||||
import { clientNetworkKey, makeCostRateLimiter } from './common/rate-limit.js';
|
||||
export { CCTV_FRAME_FETCH_TIMEOUT_MS, fetchCctvImageFromUpstream };
|
||||
|
||||
/** An http(s) .m3u8 URL: what the HLS puller behind /media can play. */
|
||||
const isPullableHlsUrl = (url) =>
|
||||
/^https?:\/\//i.test(url) && /\.m3u8(?:\?|$)/i.test(url);
|
||||
|
||||
/**
|
||||
* Vite plugin: CCTV camera proxy with source registry, frame/media serving,
|
||||
* fallback chain (upstream -> Street View -> synthetic SVG), and health tracking.
|
||||
@@ -407,10 +411,7 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
return;
|
||||
}
|
||||
if (feedType === 'hls') {
|
||||
if (
|
||||
!/^https?:\/\//i.test(mediaUrl) ||
|
||||
!/\.m3u8(?:\?|$)/i.test(mediaUrl)
|
||||
) {
|
||||
if (!isPullableHlsUrl(mediaUrl)) {
|
||||
res.writeHead(503, { 'Content-Type': 'application/json' });
|
||||
res.end(
|
||||
JSON.stringify({
|
||||
@@ -470,13 +471,15 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
'X-CCTV-Session': entry.token,
|
||||
});
|
||||
res.end(playlist);
|
||||
} catch {
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'fallback',
|
||||
label: source?.provider || 'Configured source',
|
||||
message: 'Live HLS unavailable',
|
||||
});
|
||||
} catch (error) {
|
||||
// A capacity refusal says nothing about the source: keep its health.
|
||||
if (!(error instanceof HlsCapacityError))
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'fallback',
|
||||
label: source?.provider || 'Configured source',
|
||||
message: 'Live HLS unavailable',
|
||||
});
|
||||
if (!downstream.closed) {
|
||||
res.writeHead(503, {
|
||||
'Content-Type': 'application/json',
|
||||
@@ -670,6 +673,13 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
(!isVideoFeedType(normalizeFeedType(source?.feedType))
|
||||
? source?.url
|
||||
: '');
|
||||
// /media reports a pullable HLS camera's health. Without a snapshot of
|
||||
// its own, the placeholder or Street View frame here says nothing about
|
||||
// its stream, so it leaves that health as it is.
|
||||
const keepHealth =
|
||||
normalizeFeedType(source?.feedType) === 'hls' &&
|
||||
!source?.snapshotUrl &&
|
||||
isPullableHlsUrl(source?.url || '');
|
||||
|
||||
const upstreamImage =
|
||||
source?.pack === 'world-cameras'
|
||||
@@ -767,12 +777,13 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
pitch,
|
||||
});
|
||||
if (sv?.ok) {
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'streetview',
|
||||
label: 'Google Street View',
|
||||
message: 'Fallback Street View frame',
|
||||
});
|
||||
if (!keepHealth)
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'streetview',
|
||||
label: 'Google Street View',
|
||||
message: 'Fallback Street View frame',
|
||||
});
|
||||
res.writeHead(200, {
|
||||
'Content-Type': sv.contentType,
|
||||
'Cache-Control': 'no-store',
|
||||
@@ -791,14 +802,15 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
|
||||
: 'NO UPSTREAM CONFIGURED',
|
||||
});
|
||||
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'synthetic',
|
||||
label: source?.provider || 'Synthetic fallback',
|
||||
message: source?.url
|
||||
? 'Upstream unavailable'
|
||||
: 'No source configured',
|
||||
});
|
||||
if (!keepHealth)
|
||||
setHealth(cameraId, {
|
||||
status: 'degraded',
|
||||
sourceKind: 'synthetic',
|
||||
label: source?.provider || 'Synthetic fallback',
|
||||
message: source?.url
|
||||
? 'Upstream unavailable'
|
||||
: 'No source configured',
|
||||
});
|
||||
|
||||
res.writeHead(200, {
|
||||
'Content-Type': 'image/svg+xml',
|
||||
|
||||
@@ -16,6 +16,9 @@ export const HLS_LIMITS = Object.freeze({
|
||||
readyMs: 12000,
|
||||
});
|
||||
|
||||
/** Every lease or session slot is taken, or the viewer's lease ended mid-request: the source itself has not failed. */
|
||||
export class HlsCapacityError extends Error {}
|
||||
|
||||
/** Refuse redirects; count actual streamed bytes, including chunked responses. */
|
||||
export async function fetchHlsBytes(
|
||||
url,
|
||||
@@ -152,7 +155,7 @@ export function createHlsPuller({
|
||||
let lease = entry.leases.get(leaseId);
|
||||
if (!lease) {
|
||||
if (!create || entry.leases.size >= limits.leasesPerSession)
|
||||
throw new Error('HLS lease capacity reached');
|
||||
throw new HlsCapacityError('HLS lease capacity reached');
|
||||
lease = {};
|
||||
entry.leases.set(leaseId, lease);
|
||||
}
|
||||
@@ -264,7 +267,7 @@ export function createHlsPuller({
|
||||
}
|
||||
// Reserve synchronously, before any await: concurrent creation is singleflight.
|
||||
if (active.size >= limits.sessions)
|
||||
throw new Error('HLS session capacity reached');
|
||||
throw new HlsCapacityError('HLS session capacity reached');
|
||||
entry = {
|
||||
cameraId,
|
||||
url,
|
||||
|
||||
@@ -0,0 +1,187 @@
|
||||
// Live HLS through the real /api/cctv middleware. Leases are random
|
||||
// per-playback ids, not bound to the socket address, and a viewer refused for
|
||||
// capacity leaves the camera's health as it was.
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { HLS_LIMITS } from '../../server/providers/cctv/stream.js';
|
||||
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
|
||||
|
||||
const perSession = HLS_LIMITS.leasesPerSession;
|
||||
const SAME_ORIGIN = { host: 'localhost:4173', 'sec-fetch-site': 'same-origin' };
|
||||
const camera = (id) => ({
|
||||
id,
|
||||
name: id,
|
||||
lat: 30.27,
|
||||
lon: -97.74,
|
||||
feedType: 'hls',
|
||||
url: `https://cams.upstream.invalid/${id}/list.m3u8`,
|
||||
});
|
||||
const PLAYLIST =
|
||||
'#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
|
||||
const lease = (index) =>
|
||||
`00000000-0000-4000-8000-${String(index).padStart(12, '0')}`;
|
||||
|
||||
/** Mount the provider over three HLS cameras; `failing` upstreams answer 500. */
|
||||
function mount(t, failing = []) {
|
||||
const upstream = [];
|
||||
const app = mountCctvProvider(t, {
|
||||
cameras: ['cam-a', 'cam-b', 'cam-c'].map(camera),
|
||||
fetch: async (url) => {
|
||||
upstream.push(String(url));
|
||||
if (failing.some((id) => String(url).includes(`/${id}/`)))
|
||||
return new Response('', { status: 500 });
|
||||
return new Response(String(url).endsWith('.m3u8') ? PLAYLIST : 'abc');
|
||||
},
|
||||
});
|
||||
return { ...app, upstream };
|
||||
}
|
||||
|
||||
/** Open a playlist and return the first segment path it lists. */
|
||||
async function openPlaylist(call, cameraId, leaseId, remoteAddress) {
|
||||
const res = await call(
|
||||
`/media/${cameraId}?lease=${leaseId}`,
|
||||
SAME_ORIGIN,
|
||||
'GET',
|
||||
remoteAddress,
|
||||
);
|
||||
assert.equal(res.statusCode, 200, `playlist ${cameraId} ${leaseId}`);
|
||||
const segment = res.body
|
||||
.split('\n')
|
||||
.find((line) => line.startsWith('/api/cctv/media/'));
|
||||
assert.ok(segment, 'the playlist lists a segment');
|
||||
return segment.replace('/api/cctv', '');
|
||||
}
|
||||
|
||||
test('viewers behind one shared address get every slot, and no more', async (t) => {
|
||||
assert.equal(HLS_LIMITS.sessions, 2, 'the test fills both sessions');
|
||||
const { call, health } = mount(t);
|
||||
const shared = '::ffff:172.17.0.1';
|
||||
let segment;
|
||||
for (let index = 0; index < 2 * perSession; index++)
|
||||
segment = await openPlaylist(
|
||||
call,
|
||||
index < perSession ? 'cam-a' : 'cam-b',
|
||||
lease(index),
|
||||
shared,
|
||||
);
|
||||
assert.equal(
|
||||
(await call(segment, SAME_ORIGIN, 'GET', shared)).statusCode,
|
||||
200,
|
||||
);
|
||||
const before = await health('cam-a');
|
||||
// A ninth viewer of a full session and a third session are both refused,
|
||||
// without reporting a camera whose upstream is fine as degraded.
|
||||
for (const cameraId of ['cam-a', 'cam-c']) {
|
||||
const refused = await call(
|
||||
`/media/${cameraId}?lease=${lease(2 * perSession)}`,
|
||||
SAME_ORIGIN,
|
||||
'GET',
|
||||
shared,
|
||||
);
|
||||
assert.equal(refused.statusCode, 503, cameraId);
|
||||
assert.equal(refused.headers['Retry-After'], '2');
|
||||
}
|
||||
assert.deepEqual(await health('cam-a'), before);
|
||||
assert.equal(await health('cam-c'), undefined);
|
||||
// A released slot is free for the next viewer.
|
||||
await call(`/media/cam-a?lease=${lease(0)}`, SAME_ORIGIN, 'DELETE', shared);
|
||||
await openPlaylist(call, 'cam-a', lease(2 * perSession), shared);
|
||||
});
|
||||
|
||||
test('a lease keeps working after its viewer reconnects from another address', async (t) => {
|
||||
const { call } = mount(t);
|
||||
await openPlaylist(call, 'cam-a', lease(0), '192.168.1.20');
|
||||
const segment = await openPlaylist(call, 'cam-a', lease(0), '192.168.1.21');
|
||||
assert.equal(
|
||||
(await call(segment, SAME_ORIGIN, 'GET', '192.168.1.21')).statusCode,
|
||||
200,
|
||||
);
|
||||
// A second viewer keeps the session alive, so the 404 below is the released
|
||||
// lease and not a stopped session.
|
||||
const other = await openPlaylist(call, 'cam-a', lease(1), '192.168.1.30');
|
||||
const release = await call(
|
||||
`/media/cam-a?lease=${lease(0)}`,
|
||||
SAME_ORIGIN,
|
||||
'DELETE',
|
||||
'192.168.1.21',
|
||||
);
|
||||
assert.equal(release.statusCode, 204);
|
||||
assert.equal(
|
||||
(await call(segment, SAME_ORIGIN, 'GET', '192.168.1.21')).statusCode,
|
||||
404,
|
||||
);
|
||||
assert.equal(
|
||||
(await call(other, SAME_ORIGIN, 'GET', '192.168.1.30')).statusCode,
|
||||
200,
|
||||
);
|
||||
});
|
||||
|
||||
test('ninth viewer frame fallback preserves active camera health', async (t) => {
|
||||
const { call, health } = mount(t);
|
||||
let segment;
|
||||
for (let index = 0; index < perSession; index++)
|
||||
segment = await openPlaylist(call, 'cam-a', lease(index));
|
||||
const before = await health('cam-a');
|
||||
assert.equal(before.status, 'ok');
|
||||
const refused = await call(
|
||||
`/media/cam-a?lease=${lease(perSession)}`,
|
||||
SAME_ORIGIN,
|
||||
);
|
||||
assert.equal(refused.statusCode, 503);
|
||||
const frame = await call(
|
||||
'/frame/cam-a?label=cam-a&lat=30.27&lon=-97.74',
|
||||
SAME_ORIGIN,
|
||||
);
|
||||
assert.equal(frame.statusCode, 200);
|
||||
assert.equal(
|
||||
(await call(segment, SAME_ORIGIN)).statusCode,
|
||||
200,
|
||||
'admitted viewer still plays',
|
||||
);
|
||||
assert.deepEqual(await health('cam-a'), before);
|
||||
});
|
||||
|
||||
test('third camera frame fallback keeps absent health', async (t) => {
|
||||
const { call, health, upstream } = mount(t);
|
||||
await openPlaylist(call, 'cam-a', lease(0));
|
||||
await openPlaylist(call, 'cam-b', lease(1));
|
||||
const refused = await call(`/media/cam-c?lease=${lease(2)}`, SAME_ORIGIN);
|
||||
assert.equal(refused.statusCode, 503);
|
||||
assert.equal(await health('cam-c'), undefined);
|
||||
const frame = await call(
|
||||
'/frame/cam-c?label=cam-c&lat=30.27&lon=-97.74',
|
||||
SAME_ORIGIN,
|
||||
);
|
||||
assert.equal(frame.statusCode, 200);
|
||||
assert.equal(upstream.filter((url) => url.includes('/cam-c/')).length, 0);
|
||||
assert.equal(
|
||||
await health('cam-c'),
|
||||
undefined,
|
||||
'the fallback frame wrote cam-c health',
|
||||
);
|
||||
});
|
||||
|
||||
test('a failing HLS upstream with free slots is still reported degraded', async (t) => {
|
||||
const { call, health } = mount(t, ['cam-c']);
|
||||
const res = await call(`/media/cam-c?lease=${lease(0)}`, SAME_ORIGIN);
|
||||
assert.equal(res.statusCode, 503);
|
||||
assert.equal(res.headers['Retry-After'], '2');
|
||||
const row = await health('cam-c');
|
||||
assert.deepEqual(
|
||||
{ status: row.status, message: row.message },
|
||||
{ status: 'degraded', message: 'Live HLS unavailable' },
|
||||
);
|
||||
});
|
||||
|
||||
test('a cross-site request can neither read nor release a live HLS lease', async (t) => {
|
||||
const { call } = mount(t);
|
||||
const segment = await openPlaylist(call, 'cam-a', lease(0));
|
||||
const crossSite = { host: 'localhost:4173', 'sec-fetch-site': 'cross-site' };
|
||||
assert.equal((await call(segment, crossSite)).statusCode, 403);
|
||||
assert.equal(
|
||||
(await call(`/media/cam-a?lease=${lease(0)}`, crossSite, 'DELETE'))
|
||||
.statusCode,
|
||||
403,
|
||||
);
|
||||
assert.equal((await call(segment, SAME_ORIGIN)).statusCode, 200);
|
||||
});
|
||||
@@ -5,6 +5,7 @@ import {
|
||||
fetchHlsBytes,
|
||||
parseHlsMedia,
|
||||
HLS_LIMITS,
|
||||
HlsCapacityError,
|
||||
} from '../../server/providers/cctv/stream.js';
|
||||
const playlist =
|
||||
'#EXTM3U\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
|
||||
@@ -144,7 +145,7 @@ test('one session is reserved before await; disk-free cache and capacity remain
|
||||
manager.ensure('a', base),
|
||||
]);
|
||||
assert.equal(a, b);
|
||||
await assert.rejects(manager.ensure('b', base));
|
||||
await assert.rejects(manager.ensure('b', base), HlsCapacityError);
|
||||
assert.equal(await manager.waitReady(a), true);
|
||||
assert.deepEqual(manager.stats(), { sessions: 1, bytes: 6 });
|
||||
const text = await manager.buildPlaylist(a, 'a');
|
||||
@@ -261,7 +262,10 @@ test('two consumers share downloads but release and abandoned expiry are indepen
|
||||
await manager.waitReady(a);
|
||||
assert.equal(downloads, 4); // One manifest and three segments, not per consumer.
|
||||
assert.equal(a.leases.size, 2);
|
||||
await assert.rejects(manager.ensure('camera', base, 'viewer-c'));
|
||||
await assert.rejects(
|
||||
manager.ensure('camera', base, 'viewer-c'),
|
||||
HlsCapacityError,
|
||||
);
|
||||
manager.release('camera', 'viewer-a');
|
||||
assert.equal(manager.stats().sessions, 1);
|
||||
assert.equal(manager.getSegment('camera', a.token, 0, 'viewer-a'), null);
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { Readable } from 'node:stream';
|
||||
import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
|
||||
|
||||
const STILL = {
|
||||
id: 'still-fixture',
|
||||
@@ -63,113 +63,17 @@ function upstreamResponse({ status = 200, headers = {}, body = 'payload' }) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Collects what a handler wrote, the way a Node ServerResponse would. */
|
||||
function recordingResponse() {
|
||||
const chunks = [];
|
||||
let finished;
|
||||
const done = new Promise((resolve) => {
|
||||
finished = resolve;
|
||||
});
|
||||
const res = {
|
||||
statusCode: 0,
|
||||
headers: {},
|
||||
writableEnded: false,
|
||||
body: '',
|
||||
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 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Mount the real provider over the fixture cameras and answer every upstream
|
||||
* request from the supplied function.
|
||||
*/
|
||||
/** Mount the real provider over the fixture cameras, recording upstream requests. */
|
||||
function mount(t, respond) {
|
||||
const before = {
|
||||
json: process.env.CCTV_SOURCES_JSON,
|
||||
file: process.env.CCTV_SOURCES_FILE,
|
||||
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,
|
||||
STILL_ALIAS,
|
||||
VIDEO,
|
||||
WEBM,
|
||||
MJPEG,
|
||||
]);
|
||||
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
|
||||
process.env.CCTV_FORCE_AUSTIN = '0';
|
||||
// No Street View key: a /frame miss lands on the synthetic billboard rather
|
||||
// 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 = {}) => {
|
||||
requests.push({ url: String(url), redirect: init.redirect });
|
||||
return respond({ url: String(url), init });
|
||||
};
|
||||
t.after(() => {
|
||||
globalThis.fetch = nativeFetch;
|
||||
for (const [name, value] of [
|
||||
['CCTV_SOURCES_JSON', before.json],
|
||||
['CCTV_SOURCES_FILE', before.file],
|
||||
['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;
|
||||
}
|
||||
});
|
||||
|
||||
let handler = null;
|
||||
cctvProxy().configureServer({
|
||||
middlewares: {
|
||||
use: (_route, fn) => {
|
||||
handler = fn;
|
||||
},
|
||||
const app = mountCctvProvider(t, {
|
||||
cameras: [STILL, STILL_ALIAS, VIDEO, WEBM, MJPEG],
|
||||
fetch: async (url, init = {}) => {
|
||||
requests.push({ url: String(url), redirect: init.redirect });
|
||||
return respond({ url: String(url), init });
|
||||
},
|
||||
});
|
||||
assert.ok(handler, 'the CCTV provider must mount a middleware');
|
||||
|
||||
const call = async (path, headers = {}, socket) => {
|
||||
const { res, done } = recordingResponse();
|
||||
await handler({ url: path, headers, method: 'GET', on() {}, socket }, res);
|
||||
await done;
|
||||
return res;
|
||||
};
|
||||
const health = async (id) => {
|
||||
const res = await call('/health');
|
||||
return JSON.parse(res.body).cameras.find((entry) => entry.id === id);
|
||||
};
|
||||
return { call, health, requests };
|
||||
return { ...app, requests };
|
||||
}
|
||||
|
||||
test('a still camera is refused by /media without any upstream request', async (t) => {
|
||||
@@ -405,24 +309,35 @@ test('every CCTV route refuses cross-site requests and admits the app itself', a
|
||||
}),
|
||||
);
|
||||
const host = 'localhost:5173';
|
||||
const paths = [
|
||||
'/sources',
|
||||
'/health',
|
||||
`/stream/${STILL.id}`,
|
||||
`/frame/${STILL.id}`,
|
||||
`/media/${VIDEO.id}`,
|
||||
const lease = '00000000-0000-4000-8000-000000000000';
|
||||
const routes = [
|
||||
['GET', '/sources'],
|
||||
['GET', '/health'],
|
||||
['GET', `/stream/${STILL.id}`],
|
||||
['GET', `/frame/${STILL.id}`],
|
||||
['GET', `/media/${VIDEO.id}`],
|
||||
['GET', `/media/${VIDEO.id}/seg_0.ts?lease=${lease}`],
|
||||
['DELETE', `/media/${VIDEO.id}?lease=${lease}`],
|
||||
];
|
||||
// A catalog refresh would fetch the live packs: a refused request must not
|
||||
// start one.
|
||||
process.env.CCTV_FORCE_AUSTIN = '1';
|
||||
for (const headers of [
|
||||
{ host, 'sec-fetch-site': 'cross-site' },
|
||||
{ host, 'sec-fetch-site': 'same-site' },
|
||||
{ host, origin: 'https://elsewhere.example' },
|
||||
{ host, origin: 'null' },
|
||||
])
|
||||
for (const path of paths) {
|
||||
const res = await app.call(path, headers);
|
||||
assert.equal(res.statusCode, 403, `${path} ${JSON.stringify(headers)}`);
|
||||
for (const [method, path] of routes) {
|
||||
const res = await app.call(path, headers, method);
|
||||
assert.equal(
|
||||
res.statusCode,
|
||||
403,
|
||||
`${method} ${path} ${JSON.stringify(headers)}`,
|
||||
);
|
||||
}
|
||||
assert.deepEqual(app.requests, [], 'a refused request reached an upstream');
|
||||
process.env.CCTV_FORCE_AUSTIN = '0';
|
||||
for (const headers of [
|
||||
{ host, 'sec-fetch-site': 'same-origin' },
|
||||
{ host, origin: `http://${host}` },
|
||||
@@ -578,9 +493,11 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
|
||||
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'];
|
||||
const first = '203.0.113.7';
|
||||
const frameSource = async (address) =>
|
||||
(await app.call(`/frame/${STILL.id}`, {}, 'GET', address)).headers[
|
||||
'X-CCTV-Source'
|
||||
];
|
||||
|
||||
// Without a key nothing is spent.
|
||||
for (let i = 0; i < 3; i++)
|
||||
@@ -590,7 +507,9 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
|
||||
process.env.GOOGLE_MAPS_SERVER_API_KEY = 'fixture-key';
|
||||
// Neither an unknown id nor a forwarded request spends the window.
|
||||
assert.equal(
|
||||
(await app.call('/frame/not-a-camera', {}, first)).headers['X-CCTV-Source'],
|
||||
(await app.call('/frame/not-a-camera', {}, 'GET', first)).headers[
|
||||
'X-CCTV-Source'
|
||||
],
|
||||
'synthetic',
|
||||
);
|
||||
assert.equal(
|
||||
@@ -598,6 +517,7 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
|
||||
await app.call(
|
||||
`/frame/${STILL.id}`,
|
||||
{ 'x-forwarded-for': '203.0.113.9' },
|
||||
'GET',
|
||||
first,
|
||||
)
|
||||
).headers['X-CCTV-Source'],
|
||||
@@ -614,14 +534,11 @@ test('the Street View fallback spends a per-IP Google budget, and only with a ke
|
||||
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(await frameSource('203.0.113.8'), 'streetview');
|
||||
assert.equal(streetViewCalls(), 3);
|
||||
|
||||
// Addresses within one IPv6 /64 share a budget.
|
||||
const v6 = (remoteAddress) => frameSource({ remoteAddress });
|
||||
const v6 = frameSource;
|
||||
assert.deepEqual(
|
||||
[await v6('2001:db8::1'), await v6('2001:db8::2'), await v6('2001:db8::3')],
|
||||
['streetview', 'streetview', 'synthetic'],
|
||||
|
||||
@@ -9,7 +9,7 @@ import assert from 'node:assert/strict';
|
||||
import http from 'node:http';
|
||||
import net from 'node:net';
|
||||
import { Readable, Writable } from 'node:stream';
|
||||
import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
|
||||
import { proxyMediaResponse } from '../../server/providers/cctv/media.js';
|
||||
import { sanitizeCctvRangeHeader } from '../../server/providers/cctv/range.js';
|
||||
import { CCTV_MEDIA_MAX_BODY_BYTES as CAP } from '../../server/providers/cctv/constants.js';
|
||||
@@ -131,99 +131,21 @@ function upstreamResponse({ status = 206, headers = {}, body = 'payload' }) {
|
||||
};
|
||||
}
|
||||
|
||||
/** Collects what a handler wrote, the way a Node ServerResponse would. */
|
||||
function recordingResponse() {
|
||||
const chunks = [];
|
||||
let finished;
|
||||
const done = new Promise((resolve) => {
|
||||
finished = resolve;
|
||||
});
|
||||
const res = {
|
||||
statusCode: 0,
|
||||
headers: {},
|
||||
writableEnded: false,
|
||||
body: '',
|
||||
writeHead(status, headers) {
|
||||
res.statusCode = status;
|
||||
res.headers = headers || {};
|
||||
},
|
||||
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 };
|
||||
}
|
||||
|
||||
/**
|
||||
* Mount the real provider, serve one camera, and answer its upstream from the
|
||||
* supplied function.
|
||||
*/
|
||||
/** Mount the real provider over the fixture cameras, recording upstream requests. */
|
||||
function mount(t, respond) {
|
||||
const before = {
|
||||
json: process.env.CCTV_SOURCES_JSON,
|
||||
file: process.env.CCTV_SOURCES_FILE,
|
||||
austin: process.env.CCTV_FORCE_AUSTIN,
|
||||
};
|
||||
process.env.CCTV_SOURCES_JSON = JSON.stringify([CAMERA]);
|
||||
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
|
||||
process.env.CCTV_FORCE_AUSTIN = '0';
|
||||
const nativeFetch = globalThis.fetch;
|
||||
const requests = [];
|
||||
globalThis.fetch = async (url, init = {}) => {
|
||||
requests.push({
|
||||
url: String(url),
|
||||
headers: init.headers || {},
|
||||
signal: init.signal || null,
|
||||
});
|
||||
return respond({ url: String(url), init });
|
||||
};
|
||||
t.after(() => {
|
||||
globalThis.fetch = nativeFetch;
|
||||
for (const [name, value] of [
|
||||
['CCTV_SOURCES_JSON', before.json],
|
||||
['CCTV_SOURCES_FILE', before.file],
|
||||
['CCTV_FORCE_AUSTIN', before.austin],
|
||||
]) {
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
}
|
||||
});
|
||||
|
||||
let handler = null;
|
||||
const plugin = cctvProxy();
|
||||
plugin.configureServer({
|
||||
middlewares: {
|
||||
use: (_route, fn) => {
|
||||
handler = fn;
|
||||
},
|
||||
const app = mountCctvProvider(t, {
|
||||
cameras: [CAMERA],
|
||||
fetch: async (url, init = {}) => {
|
||||
requests.push({
|
||||
url: String(url),
|
||||
headers: init.headers || {},
|
||||
signal: init.signal || null,
|
||||
});
|
||||
return respond({ url: String(url), init });
|
||||
},
|
||||
});
|
||||
assert.ok(handler, 'the CCTV provider must mount a middleware');
|
||||
|
||||
/** Drive one request through the mounted handler. */
|
||||
const call = async (path, headers = {}) => {
|
||||
const { res, done } = recordingResponse();
|
||||
await handler({ url: path, headers, method: 'GET', on() {} }, res);
|
||||
await done;
|
||||
return res;
|
||||
};
|
||||
/** Read the camera's current health entry. */
|
||||
const health = async () => {
|
||||
const res = await call('/health');
|
||||
return JSON.parse(res.body).cameras.find(({ id }) => id === CAMERA.id);
|
||||
};
|
||||
return { call, health, requests, handler: (req, res) => handler(req, res) };
|
||||
return { ...app, requests, health: () => app.health(CAMERA.id) };
|
||||
}
|
||||
|
||||
test('a canonical range reaches the upstream and its 206 is passed through', async (t) => {
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Reproduction: a viewer refused for HLS capacity is shown its fallback frame
|
||||
// under a badge that still reads the shared live health ("LIVE · OK").
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { HLS_LIMITS } from '../../server/providers/cctv/stream.js';
|
||||
import { mountCctvProvider } from '../testSupport/cctvProviderHarness.mjs';
|
||||
import { presentCctvSourceBadge } from '../ui/cctvFrames.js';
|
||||
import { presentCctvPanel } from '../ui/cctvPresentation.js';
|
||||
|
||||
const SAME_ORIGIN = { host: 'localhost:4173', 'sec-fetch-site': 'same-origin' };
|
||||
const PLAYLIST =
|
||||
'#EXTM3U\n#EXT-X-TARGETDURATION:2\n#EXT-X-MEDIA-SEQUENCE:12\n#EXTINF:2,\na.ts\n#EXTINF:2,\nb.ts\n#EXTINF:2,\nc.ts\n';
|
||||
const lease = (index) =>
|
||||
`00000000-0000-4000-8000-${String(index).padStart(12, '0')}`;
|
||||
const camera = (id) => ({
|
||||
id,
|
||||
name: id,
|
||||
lat: 30.27,
|
||||
lon: -97.74,
|
||||
feedType: 'hls',
|
||||
url: `https://cams.upstream.invalid/${id}/list.m3u8`,
|
||||
});
|
||||
|
||||
test('a capacity-refused viewer is not shown its fallback frame as live and ok', async (t) => {
|
||||
assert.equal(HLS_LIMITS.sessions, 2);
|
||||
const { call, health } = mountCctvProvider(t, {
|
||||
cameras: ['cam-a', 'cam-b', 'cam-c'].map(camera),
|
||||
fetch: async (url) =>
|
||||
new Response(String(url).endsWith('.m3u8') ? PLAYLIST : 'abc'),
|
||||
});
|
||||
const open = (id, index) =>
|
||||
call(`/media/${id}?lease=${lease(index)}`, SAME_ORIGIN);
|
||||
// cam-c played earlier and was released: its row is live/ok.
|
||||
assert.equal((await open('cam-c', 0)).statusCode, 200);
|
||||
assert.equal(
|
||||
(await call(`/media/cam-c?lease=${lease(0)}`, SAME_ORIGIN, 'DELETE'))
|
||||
.statusCode,
|
||||
204,
|
||||
);
|
||||
// Two other viewers now hold both camera sessions.
|
||||
assert.equal((await open('cam-a', 1)).statusCode, 200);
|
||||
assert.equal((await open('cam-b', 2)).statusCode, 200);
|
||||
// The cam-c viewer is refused and falls back to the frame.
|
||||
const refused = await open('cam-c', 3);
|
||||
assert.equal(refused.statusCode, 503);
|
||||
const frame = await call('/frame/cam-c', SAME_ORIGIN);
|
||||
assert.equal(frame.statusCode, 200);
|
||||
assert.equal(frame.headers['X-CCTV-Source'], 'synthetic');
|
||||
assert.match(frame.body, /UPSTREAM UNAVAILABLE/);
|
||||
const row = await health('cam-c');
|
||||
// The badge the panel derives for this viewer, in image (fallback) mode.
|
||||
const shown = {
|
||||
feedType: 'hls',
|
||||
isVideo: false,
|
||||
sourceKind: row?.sourceKind || 'configured',
|
||||
sourceStatus: row?.status || 'unknown',
|
||||
sourceMessage: row?.message || '',
|
||||
};
|
||||
const badge = presentCctvSourceBadge(shown, true, {
|
||||
loading: false,
|
||||
error: false,
|
||||
hasFrame: true,
|
||||
});
|
||||
assert.notEqual(
|
||||
badge.label,
|
||||
'LIVE · OK',
|
||||
`fallback frame (${frame.headers['X-CCTV-Source']}) labelled ${badge.label}; health message "${row?.message}"`,
|
||||
);
|
||||
const { meta } = presentCctvPanel(
|
||||
{
|
||||
enabled: true,
|
||||
activeCameraId: 'cam-c',
|
||||
activeCamera: {
|
||||
...shown,
|
||||
id: 'cam-c',
|
||||
city: 'Austin',
|
||||
headingDeg: 0,
|
||||
fovDeg: 60,
|
||||
rangeM: 100,
|
||||
},
|
||||
},
|
||||
true,
|
||||
);
|
||||
assert.doesNotMatch(meta, /Live HLS connected/);
|
||||
});
|
||||
@@ -0,0 +1,94 @@
|
||||
import { cctvProxy } from '../../server/providers/cctv.js';
|
||||
|
||||
/** Collects what a handler wrote, the way a Node ServerResponse would. */
|
||||
export function recordingResponse() {
|
||||
const chunks = [];
|
||||
let finished;
|
||||
const done = new Promise((resolve) => {
|
||||
finished = resolve;
|
||||
});
|
||||
const res = {
|
||||
statusCode: 0,
|
||||
headers: {},
|
||||
writableEnded: false,
|
||||
body: '',
|
||||
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 };
|
||||
}
|
||||
|
||||
export function mountCctvProvider(t, { cameras, fetch }) {
|
||||
const saved = {};
|
||||
for (const name of [
|
||||
'CCTV_SOURCES_JSON',
|
||||
'CCTV_SOURCES_FILE',
|
||||
'CCTV_FORCE_AUSTIN',
|
||||
'GOOGLE_MAPS_SERVER_API_KEY',
|
||||
'GOOGLE_MAPS_API_KEY',
|
||||
'GEV_RATELIMIT_GOOGLE_PER_MIN',
|
||||
])
|
||||
saved[name] = process.env[name];
|
||||
|
||||
const nativeFetch = globalThis.fetch;
|
||||
const closers = [];
|
||||
t.after(() => {
|
||||
for (const close of closers) close();
|
||||
globalThis.fetch = nativeFetch;
|
||||
for (const [name, value] of Object.entries(saved)) {
|
||||
if (value === undefined) delete process.env[name];
|
||||
else process.env[name] = value;
|
||||
}
|
||||
});
|
||||
|
||||
process.env.CCTV_SOURCES_JSON = JSON.stringify(cameras);
|
||||
process.env.CCTV_SOURCES_FILE = 'absent-source-file.json';
|
||||
process.env.CCTV_FORCE_AUSTIN = '0';
|
||||
// No Street View key: a /frame miss lands on the synthetic billboard.
|
||||
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;
|
||||
// The HLS puller binds fetch when cctvProxy() creates it, so mock first.
|
||||
globalThis.fetch = fetch;
|
||||
let handler;
|
||||
cctvProxy().configureServer({
|
||||
httpServer: { on: (_event, fn) => closers.push(fn) },
|
||||
middlewares: { use: (_route, fn) => (handler = fn) },
|
||||
});
|
||||
if (!handler) throw new Error('the CCTV provider must mount a middleware');
|
||||
|
||||
const call = async (path, headers = {}, method = 'GET', remoteAddress) => {
|
||||
const { res, done } = recordingResponse();
|
||||
await handler(
|
||||
{ url: path, headers, method, socket: { remoteAddress }, on() {} },
|
||||
res,
|
||||
);
|
||||
await done;
|
||||
return res;
|
||||
};
|
||||
const health = async (id) => {
|
||||
const res = await call('/health');
|
||||
return JSON.parse(res.body).cameras.find((entry) => entry.id === id);
|
||||
};
|
||||
return { call, health, handler };
|
||||
}
|
||||
@@ -186,6 +186,14 @@ export function _settleCctvFrame(token, src, ok) {
|
||||
);
|
||||
}
|
||||
|
||||
/** Shared live health describes the stream even when this viewer shows a frame. */
|
||||
export function isCctvLiveFrameFallback(activeCamera) {
|
||||
return (
|
||||
activeCamera?.isVideo === false &&
|
||||
String(activeCamera.sourceKind || '').toLowerCase() === 'live'
|
||||
);
|
||||
}
|
||||
|
||||
/** The source badge's label and frame state for the camera and preview. */
|
||||
export function presentCctvSourceBadge(activeCamera, enabled, frame) {
|
||||
if (!enabled || !activeCamera)
|
||||
@@ -194,6 +202,11 @@ export function presentCctvSourceBadge(activeCamera, enabled, frame) {
|
||||
return { label: t('FRAME · LOADING'), state: 'loading' };
|
||||
if (frame.error && !frame.hasFrame)
|
||||
return { label: t('FRAME · UNAVAILABLE'), state: 'error' };
|
||||
if (isCctvLiveFrameFallback(activeCamera))
|
||||
return {
|
||||
label: `${cctvSourceKindLabel('fallback')} · ${cctvSourceStatusLabel('degraded')}`,
|
||||
state: 'ready',
|
||||
};
|
||||
const kind = cctvSourceKindLabel(
|
||||
activeCamera.sourceKind || activeCamera.feedType || 'unknown',
|
||||
);
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import { getLocale, keepCodesWithValues, msg, t, tn } from '../i18n/index.js';
|
||||
import { cctvSourceLabelText, cctvSourceMessageText } from './cctvFrames.js';
|
||||
import {
|
||||
cctvSourceLabelText,
|
||||
cctvSourceMessageText,
|
||||
isCctvLiveFrameFallback,
|
||||
} from './cctvFrames.js';
|
||||
|
||||
import { createCctvVideoSurface } from './cctvVideo.js';
|
||||
import { presentCctvCalibration } from './cctvCalibration.js';
|
||||
@@ -96,9 +100,10 @@ function metaText(state, enabled, activeCamera, catalogCount) {
|
||||
const provider = cctvSourceLabelText(
|
||||
activeCamera.sourceLabel || activeCamera.provider || 'Configured Source',
|
||||
);
|
||||
const statusMsg = activeCamera.sourceMessage
|
||||
? ` · ${cctvSourceMessageText(activeCamera.sourceMessage)}`
|
||||
: '';
|
||||
const statusMsg =
|
||||
activeCamera.sourceMessage && !isCctvLiveFrameFallback(activeCamera)
|
||||
? ` · ${cctvSourceMessageText(activeCamera.sourceMessage)}`
|
||||
: '';
|
||||
// A partner-supplied feed inside a pack names its owner here.
|
||||
const credit = activeCamera.credit ? ` · ${activeCamera.credit}` : '';
|
||||
const calBadge = activeCamera.calBadge
|
||||
|
||||
Reference in new issue
Block a user