fix(cctv): keep an HLS camera's health through the refused viewer's frame

A viewer refused for capacity falls back to /frame after hls.js gives up.
For a pullable HLS camera without a snapshot, /frame only has a
placeholder or a Street View frame, which says nothing about the stream,
yet it wrote degraded over the health that /media owns. /frame now
leaves that health as it is; a dead upstream is still reported through
/media.

Tests: the middleware suite covers both fallbacks, a real upstream
failure that still degrades, and cross-site segment and release refusals
on a live HLS lease; capacity checks use HLS_LIMITS and HlsCapacityError;
the three CCTV suites share one provider harness in src/testSupport.
CURRENT-STATE and the changelog fragment describe the refusal.
This commit is contained in:
German Khokhlov committed 2026-11-20 11:48:22 +04:00
1 parent f5f5f04b5d
commit ed38a1ab3f
10 files changed
+283 -321

No files matched your search

+30 -20
View File
@@ -23,6 +23,11 @@ import { isMlitRiverAdjustmentImage } from './cctv/worldCameras.js';
import { ottawaAuthorizedFrameUrl } from './cctv/ottawa.js';
import { googleServerApiKey } from './places/google-key.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.
@@ -386,10 +391,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({
@@ -450,8 +452,7 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
});
res.end(playlist);
} catch (error) {
// A full lease or session budget says nothing about the source:
// leave the health its other viewers see untouched.
// A capacity refusal says nothing about the source: keep its health.
if (!(error instanceof HlsCapacityError))
setHealth(cameraId, {
status: 'degraded',
@@ -653,6 +654,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'
@@ -747,12 +755,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',
@@ -771,14 +780,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',