Merge main into codex/fix-cctv-session-admission

Main's Street View budget tests in the media feed-type file now mount the
provider through the shared CCTV test harness, which also resets the Google
rate-limit setting.
This commit is contained in:
German Khokhlov committed 2026-11-20 17:39:46 +04:00
commit 23efd3b343
196 files changed
+21064 -562

No files matched your search

+35 -13
View File
@@ -21,7 +21,11 @@ import { PROXY_SIGNALS } from '../../src/localRequestGate.mjs';
import { createHlsPuller, HlsCapacityError } from './cctv/stream.js';
import { isMlitRiverAdjustmentImage } from './cctv/worldCameras.js';
import { ottawaAuthorizedFrameUrl } from './cctv/ottawa.js';
import { googleServerApiKey } from './places/google-key.js';
import {
GOOGLE_DEFAULT_PER_MIN,
googleServerApiKey,
} from './places/google-key.js';
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. */
@@ -203,17 +207,31 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
};
};
let streetViewBudget; // undefined = not built yet; null = the explicit 0 opt-out
/**
* Fetch a Google Street View static image as a fallback frame. Server-side
* call, never reaches the browser — prefers GOOGLE_MAPS_SERVER_API_KEY
* (#33: a key scoped to Street View Static/Places, restricted by server IP
* rather than HTTP referrer) and falls back to the browser-exposed
* GOOGLE_MAPS_API_KEY for setups that haven't split the two yet.
* Each IP (or IPv6 /64) has its own GEV_RATELIMIT_GOOGLE_PER_MIN window,
* separate from Places. Setting it to 0 disables the limit.
*/
const streetViewFallback = async ({ lat, lon, heading, fov, pitch }) => {
const streetViewFallback = async (
client,
{ lat, lon, heading, fov, pitch },
) => {
const streetViewKey = googleServerApiKey();
if (!streetViewKey || !Number.isFinite(lat) || !Number.isFinite(lon))
return null;
// Built on first use, after the standalone env has loaded.
if (streetViewBudget === undefined)
streetViewBudget = makeCostRateLimiter(
process.env.GEV_RATELIMIT_GOOGLE_PER_MIN,
GOOGLE_DEFAULT_PER_MIN,
);
if (streetViewBudget && !streetViewBudget(client)) return null;
try {
const sv = new URL('https://maps.googleapis.com/maps/api/streetview');
sv.searchParams.set('size', '960x540');
@@ -269,6 +287,8 @@ export function cctvProxy({ sourceRoot = process.cwd() } = {}) {
// keyless gate keeps the routes reachable behind a reverse proxy; the
// key-bearing Street View fallback below has its own check.
if (admitSameSiteKeyless(req, res)) return;
// Read before any await: a socket the client has closed has no address.
const client = clientNetworkKey(req);
const sources = await getCctvSources();
const sourceById = new Map(
sources.map((source) => [source.id, source]),
@@ -639,8 +659,7 @@ 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);
// The query sets only the view, the pose the browser draws.
const heading = Number(
url.searchParams.get('heading') || source?.headingDeg,
);
@@ -742,18 +761,21 @@ 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 also answers only for catalog cameras, at the catalog position;
// the query never sets the location.
const forwarded = PROXY_SIGNALS.some(
(name) => req.headers?.[name] !== undefined,
);
const sv = forwarded
? null
: await streetViewFallback({
lat,
lon,
heading,
fov,
pitch,
});
const sv =
forwarded || !source
? null
: await streetViewFallback(client, {
lat: source.lat,
lon: source.lon,
heading,
fov,
pitch,
});
if (sv?.ok) {
if (!keepHealth)
setHealth(cameraId, {
+53
View File
@@ -0,0 +1,53 @@
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 or
* parsed) 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,
* parse?: (text: string) => unknown}} options
* @returns {Promise<unknown[]>} The parsed bodies (texts without `parse`), in
* feed order.
*/
export async function readCatalogsTogether(
feeds,
{ accept, maxBytes, timeoutMs, parse = (text) => text },
) {
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 parse(await readResponseTextCapped(response, maxBytes, signal));
} catch (error) {
sibling.abort();
await response?.body?.cancel().catch(() => {});
throw error;
}
}),
);
}
+13 -38
View File
@@ -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,21 @@ 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,
parse: JSON.parse,
},
);
if (!Array.isArray(rows) || !Array.isArray(layers)) return [];
const features = layers.find((layer) => layer?.layer === 'VKR')?.features;
if (!Array.isArray(features)) return [];
+92
View File
@@ -127,3 +127,95 @@ 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);
});
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);
});
+34 -70
View File
@@ -43,6 +43,7 @@ import {
TARKTEE_IMAGE_ORIGIN,
DEFAULT_TARKTEE_MAX_SOURCES,
TARKTEE_MAX_CATALOG_BYTES,
TARKTEE_FETCH_TIMEOUT_MS,
TARKTEE_ANCHORS,
DEFAULT_WARENDORF_SOURCE_FILE,
WARENDORF_IMAGE_ORIGINS,
@@ -82,7 +83,6 @@ import {
HK_ANCHORS,
HK_MAX_CATALOG_BYTES,
CCTV_SOURCE_FETCH_TIMEOUT_MS,
TARKTEE_FETCH_TIMEOUT_MS,
DELDOT_CCTV_URL,
DEFAULT_DELDOT_MAX_SOURCES,
DELDOT_ANCHORS,
@@ -172,6 +172,7 @@ import {
pinnedCameraUrl,
} from './normalize.js';
import { directionToHeading } from '../../../src/data/directionText.js';
import { readCatalogsTogether } from './catalogPair.js';
import {
readResponseBytesCapped,
readResponseJsonCapped,
@@ -561,6 +562,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
@@ -593,6 +610,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) {
@@ -604,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();
@@ -658,7 +677,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,9 +696,10 @@ export async function loadOntarioSourcesFromOpenData(
);
return prioritized;
} 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:',
error?.message || error,
`[CCTV] Ontario 511 camera download error (${ontarioFailureKind(error)})`,
);
return [];
}
@@ -1279,73 +1299,17 @@ export function parseTarkteeDatexImages(xml) {
*/
export async function loadTarkteeSourcesFromDatex() {
try {
const responses = await Promise.allSettled([
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),
}),
]);
const [locResp, imgResp] = responses.map((result) =>
result.status === 'fulfilled' ? result.value : undefined,
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,
},
);
// 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 [];
};
const rejected = responses.find((result) => result.status === 'rejected');
if (rejected) {
await discardBoth();
throw rejected.reason;
}
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) {
+2
View File
@@ -1,6 +1,7 @@
import path from 'node:path';
import { openSkyProxy } from './aircraft/opensky.js';
import { celestrakProxy, rocketLaunchesProxy } from './space.js';
import { issTelemetryProxy } from './space/issTelemetry.js';
import { tomtomProxy } from './traffic.js';
import { firmsProxy } from './firms.js';
import { terrainHeightsProxy } from './terrain.js';
@@ -58,6 +59,7 @@ function localProviderPlugins({
return [
openSkyProxy(),
celestrakProxy(),
issTelemetryProxy(),
tomtomProxy(),
firmsProxy(),
rocketLaunchesProxy(),
+8
View File
@@ -1,5 +1,13 @@
import { resolveGoogleServerKey } from '../../../scripts/google-server-key.mjs';
/**
* Default GEV_RATELIMIT_GOOGLE_PER_MIN for the Places and Geocoding window
* and the separate CCTV Street View window, in requests per minute per IP.
* The Pinokio build sets the same default in pinokio/_ENVIRONMENT.
* `.env.example` suggests 60 for hosts other than localhost.
*/
export const GOOGLE_DEFAULT_PER_MIN = 120;
/**
* Optional Google place context is an empty capability when no key is present,
* not a server outage. Returning 200 keeps a deliberately keyless session out
+29 -25
View File
@@ -1,4 +1,5 @@
import {
GOOGLE_DEFAULT_PER_MIN,
googleServerApiKey,
keylessGooglePlacesResponse,
keylessGoogleGeocodeResponse,
@@ -15,17 +16,7 @@ import {
// undefined = not built yet; null = the explicit 0 opt-out; fn = active limiter
let _googleRateLimiter;
/**
* Requests/min/IP applied when GEV_RATELIMIT_GOOGLE_PER_MIN is unset. This is
* the value the Pinokio build already ships (pinokio/_ENVIRONMENT), so the
* packaged app keeps behaving exactly as it does today and only an
* unconfigured server changes — from unlimited to what the product already
* runs with. Places calls are user-driven (a search, a nearby lookup, an
* installation probe) rather than polled, so this sits far above what the app
* generates and bites only a caller enumerating the endpoint. `.env.example`
* suggests a tighter 60 when the host is not localhost; that still applies.
*/
export const GOOGLE_DEFAULT_PER_MIN = 120;
export { GOOGLE_DEFAULT_PER_MIN };
/** Google cost endpoints (nearby-places, text-search, geocoding). Null only when set to 0. */
function googleRateLimiter() {
@@ -71,6 +62,8 @@ const GEOCODE_UPSTREAM_TIMEOUT_MS = 10_000;
const GEOCODE_MAX_RESPONSE_BYTES = 256 * 1024;
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();
@@ -114,6 +107,10 @@ 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;
// Default to no-store. The browser caches only the 200 answer below.
// A cached refusal (bad key, API off, quota) would outlive the key fix
// by five minutes.
res.setHeader('Cache-Control', 'no-store');
if (req.method !== 'GET') {
res.statusCode = 405;
res.setHeader('Content-Type', 'application/json');
@@ -129,7 +126,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;
}
@@ -194,18 +190,21 @@ 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)
throw new Error('Google Places request failed');
const places = projectNearbyPlaces(data || {}, latitude, longitude);
res.statusCode = response.ok ? 200 : response.status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'private, max-age=300');
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) {
@@ -222,11 +221,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.
// a view-biased Text Search lands on the actual feature. Same key, 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');
@@ -242,7 +242,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;
}
@@ -315,18 +314,20 @@ export function googlePlacesContextProxy({
}),
},
);
const data = await response.json().catch(() => ({}));
const places = projectTextSearchPlaces(data, latitude, longitude);
const data = await response.json().catch(() => null);
if (response.ok && !data)
throw new Error('Google Places request failed');
const places = projectTextSearchPlaces(data || {}, latitude, longitude);
res.statusCode = response.ok ? 200 : response.status;
res.setHeader('Content-Type', 'application/json; charset=utf-8');
res.setHeader('Cache-Control', 'private, max-age=300');
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) {
@@ -366,6 +367,7 @@ 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;
res.setHeader('Cache-Control', 'no-store');
if (req.method !== 'GET') {
refuse(405, 'Method not allowed');
return;
@@ -379,7 +381,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;
}
@@ -446,7 +447,10 @@ 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.
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
+468
View File
@@ -0,0 +1,468 @@
import { createRequire } from 'node:module';
import { clientNetworkKey, makeRateLimiter } from '../common/rate-limit.js';
import { admitSameSiteKeyless } from '../common/same-site.js';
/**
* NASA's public ISS telemetry (the ISSLive feed), relayed as a small snapshot.
*
* The feed is a push stream served by Lightstreamer for NASA. This module
* holds ONE connection to it for the whole server and gives browsers the
* latest value of each joint item at `/api/iss-telemetry`, so many viewers
* cost the public server one subscriber.
*
* Off by default: the feed publishes no terms of use, so nothing here
* contacts it unless the operator sets `ISS_TELEMETRY_ENABLED=1`. Without the
* flag the route answers `{ configured: false }` and the browser stops asking.
* With it, the connection opens on the first request and closes again once
* nobody has asked for a while.
*/
/** The route browsers poll. */
export const ISS_TELEMETRY_ROUTE = '/api/iss-telemetry';
const FEED_URL = 'wss://push.lightstreamer.com/lightstreamer';
/** Lightstreamer's text protocol (TLCP), spoken directly over the socket. */
const FEED_PROTOCOL = 'TLCP-2.4.0.lightstreamer.com';
const FEED_ADAPTER_SET = 'ISSLIVE';
/** The client id Lightstreamer assigns to custom TLCP clients. */
const FEED_CLIENT_ID = 'mgQkwtwdysogQz2BJ4Ji%20kOj2Bg';
/** The station clock; it ticks only while the ground has signal. */
const SIGNAL_ITEM = 'TIME_000001';
/** `Status.Class` of an item received with signal. */
const SIGNAL_CLASS = '24';
/**
* The items relayed: the joints the ISS model can turn, plus the station
* clock that says whether the feed has signal.
*/
export const ISS_TELEMETRY_ITEMS = Object.freeze([
// Alpha joints (SARJ): port, starboard.
'S0000004',
'S0000003',
// Array beta joints (BGA): 2A, 4A, 2B, 4B, 1A, 3A, 1B, 3B.
'P4000007',
'P4000008',
'P6000008',
'P6000007',
'S4000007',
'S4000008',
'S6000008',
'S6000007',
// Radiator joints (TRRJ): port, starboard.
'S0000002',
'S0000001',
// Ku-band dish: elevation, cross-elevation, transmitting.
'Z1000014',
'Z1000015',
'Z1000013',
// Canadarm2: shoulder roll, yaw, pitch; elbow; wrist pitch, yaw, roll.
'CSASSRMS004',
'CSASSRMS005',
'CSASSRMS006',
'CSASSRMS007',
'CSASSRMS008',
'CSASSRMS009',
'CSASSRMS010',
SIGNAL_ITEM,
]);
const FEED_FIELDS = Object.freeze(['TimeStamp', 'Value', 'Status.Class']);
/** No station clock tick for this long means the signal is gone. */
const SIGNAL_STALE_MS = 20_000;
/** No frame at all for this long (the server probes every 5 s) recycles the socket. */
const SILENCE_MS = 30_000;
/** Nobody asking for this long closes the connection. */
const IDLE_MS = 120_000;
/** Waits between connection attempts, the last one repeating. */
const RETRY_LADDER_MS = Object.freeze([5_000, 15_000, 60_000, 300_000]);
/**
* A session that has run this long has proved itself: its updates bring the
* retry ladder back to its first step. One that ends sooner (a snapshot and
* then `END`, over and over) keeps climbing the ladder.
*/
const STABLE_SESSION_MS = 60_000;
/** The opening handshake with the feed gives up after this. */
const HANDSHAKE_TIMEOUT_MS = 10_000;
/** A frame larger than this is not telemetry; it is dropped unread. */
const MAX_FRAME_BYTES = 64 * 1024;
const TICK_MS = 5_000;
/**
* Whether the operator switched the relay on. Only "1" does.
* @param {Record<string, string|undefined>} [env]
* @returns {boolean}
*/
export function issTelemetryEnabled(env = process.env) {
return String(env.ISS_TELEMETRY_ENABLED || '').trim() === '1';
}
/**
* The feed's time stamp as epoch milliseconds. The feed counts hours from the
* start of the day before January 1st (day of year × 24 + hours of the day),
* with no year; the year is taken from `nowMs`, stepping back one when that
* would put the stamp in the future (a value last sent in December, read in
* January).
* @param {number} hours
* @param {number} nowMs
* @returns {number|null}
*/
export function issTelemetryTimeMs(hours, nowMs) {
if (!Number.isFinite(hours) || hours < 0) return null;
const year = new Date(nowMs).getUTCFullYear();
const at = Date.UTC(year, 0, 0) + hours * 3_600_000;
// A day of slack for clock differences.
return at > nowMs + 86_400_000
? Date.UTC(year - 1, 0, 0) + hours * 3_600_000
: at;
}
/**
* Decode one TLCP update line, `U,<subscription>,<item>,<fields>`, against
* the item's previous field values. Fields are `|`-separated and
* percent-encoded: an empty field is unchanged, `#` is null, `$` is the empty
* string and `^n` stands for n unchanged fields.
* @param {string} line
* @param {(item: number) => Array<string|null>|undefined} previousOf
* @param {number} fieldCount
* @returns {{item: number, fields: Array<string|null>}|null} Null for a line
* that is not a well-formed update.
*/
export function parseTlcpUpdate(line, previousOf, fieldCount) {
if (!line.startsWith('U,')) return null;
const second = line.indexOf(',', 2);
const third = second < 0 ? -1 : line.indexOf(',', second + 1);
if (third < 0) return null;
const item = Number(line.slice(second + 1, third));
if (!Number.isInteger(item) || item < 1) return null;
const previous = previousOf(item) ?? [];
const fields = [];
for (const raw of line.slice(third + 1).split('|')) {
if (fields.length >= fieldCount) return null;
if (raw === '') fields.push(previous[fields.length] ?? null);
else if (raw === '#') fields.push(null);
else if (raw === '$') fields.push('');
else if (raw[0] === '^') {
const run = Number(raw.slice(1));
if (!Number.isInteger(run) || run < 1 || fields.length + run > fieldCount)
return null;
for (let i = 0; i < run; i++)
fields.push(previous[fields.length] ?? null);
} else {
try {
fields.push(decodeURIComponent(raw));
} catch {
return null;
}
}
}
return fields.length === fieldCount ? { item, fields } : null;
}
/**
* The relay: the connection's lifecycle and the snapshot it serves. It owns
* no timers; the caller drives `tick()` every few seconds.
* @param {{createSocket: () => object|null, now?: () => number}} options
* `createSocket` opens the feed and returns a `ws`-style socket (`on`,
* `send`, `terminate`), or null when no socket can be made.
*/
export function createIssTelemetryRelay({ createSocket, now = Date.now }) {
/** @type {Map<string, {value: number, at: number|null}>} */
const values = new Map();
/** Per item index, the raw field values the next update is decoded against. */
const fields = new Map();
let socket = null;
/** Bumped on every connect and dispose, so a dead socket's events are ignored. */
let generation = 0;
let lastDemandAt = null;
let lastFrameAt = null;
let signalAt = null;
let signalClass = null;
let failures = 0;
let retryAt = 0;
let unavailable = false;
/** When the live socket was opened, or null without one. */
let connectedAt = null;
function drop() {
generation += 1;
const old = socket;
socket = null;
connectedAt = null;
fields.clear();
// A replacement session must rebuild both its joint snapshot and signal
// credit; a new clock tick cannot make the old session's angles live.
values.clear();
signalAt = null;
signalClass = null;
try {
old?.terminate?.();
} catch {
// A socket that will not terminate is abandoned; the generation guard
// keeps its events out.
}
}
function fail() {
drop();
const wait =
RETRY_LADDER_MS[Math.min(failures, RETRY_LADDER_MS.length - 1)];
failures += 1;
retryAt = now() + wait;
}
function onLine(line) {
if (line.startsWith('CONOK,')) {
socket?.send(
`control\r\nLS_reqId=1&LS_op=add&LS_subId=1&LS_mode=MERGE&LS_group=${encodeURIComponent(
ISS_TELEMETRY_ITEMS.join(' '),
)}&LS_schema=${encodeURIComponent(FEED_FIELDS.join(' '))}&LS_snapshot=true`,
);
return;
}
// The session was refused, ended, or asks to be re-bound: start over.
if (/^(CONERR|END|LOOP|REQERR),/.test(line)) {
fail();
return;
}
const update = parseTlcpUpdate(
line,
(item) => fields.get(item),
FEED_FIELDS.length,
);
if (!update) return;
const name = ISS_TELEMETRY_ITEMS[update.item - 1];
if (!name) return;
fields.set(update.item, update.fields);
if (connectedAt !== null && now() - connectedAt >= STABLE_SESSION_MS)
failures = 0;
const [stamp, raw, statusClass] = update.fields;
const at = now();
if (name === SIGNAL_ITEM) {
signalAt = at;
signalClass = statusClass;
return;
}
const value = raw === null || raw === '' ? Number.NaN : Number(raw);
if (!Number.isFinite(value)) return;
values.set(name, {
value,
at:
stamp === null || stamp === ''
? null
: issTelemetryTimeMs(Number(stamp), at),
});
}
function connect() {
let opened;
try {
opened = createSocket();
} catch {
opened = null;
}
if (!opened) {
unavailable = true;
return;
}
unavailable = false;
generation += 1;
const mine = generation;
socket = opened;
connectedAt = now();
lastFrameAt = now();
opened.on('open', () => {
if (mine !== generation) return;
opened.send(
`create_session\r\nLS_adapter_set=${FEED_ADAPTER_SET}&LS_cid=${FEED_CLIENT_ID}&LS_send_sync=false&LS_cause=api`,
);
});
opened.on('message', (data) => {
if (mine !== generation) return;
// Nothing the feed sends may take the server down.
try {
if (data?.length > MAX_FRAME_BYTES) return;
lastFrameAt = now();
for (const line of String(data).split('\r\n')) {
if (mine !== generation) return;
if (line) onLine(line);
}
} catch {
fail();
}
});
const lost = () => {
if (mine === generation) fail();
};
opened.on('error', lost);
opened.on('close', lost);
}
/** Whether anyone asked recently enough to keep the connection. */
function wanted() {
return lastDemandAt !== null && now() - lastDemandAt < IDLE_MS;
}
/**
* Housekeeping, every few seconds: close when idle, recycle a silent
* socket, retry a failed one.
* @returns {boolean} False once there is nothing left to watch.
*/
function tick() {
if (!wanted()) {
dispose();
return false;
}
if (socket && now() - lastFrameAt > SILENCE_MS) fail();
if (!socket && !unavailable && now() >= retryAt) connect();
return true;
}
/** Record that a browser asked, opening the connection if it is closed. */
function demand() {
lastDemandAt = now();
if (!socket && !unavailable && now() >= retryAt) connect();
}
function status() {
if (unavailable) return 'unavailable';
if (!socket) return 'reconnecting';
if (signalAt === null) return 'connecting';
return signalClass === SIGNAL_CLASS && now() - signalAt < SIGNAL_STALE_MS
? 'live'
: 'no-signal';
}
/** The body served to browsers: each item's `[value, epoch ms it was true at]`. */
function snapshot() {
const items = {};
for (const [name, { value, at }] of values) items[name] = [value, at];
return { configured: true, status: status(), generatedAt: now(), items };
}
/** Close the connection and forget what it said. */
function dispose() {
drop();
values.clear();
lastDemandAt = null;
signalAt = null;
signalClass = null;
failures = 0;
retryAt = 0;
unavailable = false;
}
return { demand, tick, snapshot, dispose };
}
let webSocketImpl;
/**
* Load `ws` once, lazily: the vessel feed's socket library, so a missing
* optional dependency turns this relay off instead of breaking the server.
* @returns {Function|null}
*/
function loadWebSocket() {
if (webSocketImpl !== undefined) return webSocketImpl;
try {
webSocketImpl = createRequire(import.meta.url)('ws');
} catch {
webSocketImpl = null;
console.warn(
'[ISS telemetry] `ws` is unavailable; the telemetry relay is off.',
);
}
return webSocketImpl;
}
function json(res, status, body) {
res.writeHead(status, {
'Content-Type': 'application/json',
'Cache-Control': 'no-store',
});
res.end(JSON.stringify(body));
}
/**
* The Vite plugin that mounts the relay's route.
* @param {{env?: Record<string, string|undefined>, createSocket?: () => object|null,
* now?: () => number}} [options] Test seams; the defaults read the process
* environment at request time and open the real feed.
*/
export function issTelemetryProxy({
env,
createSocket = () => {
const WebSocket = loadWebSocket();
// `ws` buffers a whole frame before handing it over, up to 100 MiB by
// default: the frame cap applies on the socket, not only after it.
return WebSocket
? new WebSocket(FEED_URL, FEED_PROTOCOL, {
maxPayload: MAX_FRAME_BYTES,
handshakeTimeout: HANDSHAKE_TIMEOUT_MS,
})
: null;
},
now = Date.now,
} = {}) {
const relay = createIssTelemetryRelay({ createSocket, now });
// A browser polls every few seconds; this leaves room for several tabs.
const allow = makeRateLimiter({ windowMs: 60_000, max: 60, globalMax: 1200 });
let timer = null;
function stop() {
if (timer) clearInterval(timer);
timer = null;
relay.dispose();
}
function watch() {
if (timer) return;
timer = setInterval(() => {
if (relay.tick()) return;
clearInterval(timer);
timer = null;
}, TICK_MS);
timer.unref?.();
}
function handle(req, res) {
if (admitSameSiteKeyless(req, res)) return;
// The route is the one path: anything under it is not found, as JSON.
const path = String(req.url ?? '/').split('?')[0];
if (path !== '/' && path !== '') {
json(res, 404, { error: 'Not Found' });
return;
}
if (req.method !== 'GET' && req.method !== 'HEAD') {
json(res, 405, { error: 'Method Not Allowed' });
return;
}
// Read per request: the environment is loaded after this module is.
if (!issTelemetryEnabled(env ?? process.env)) {
json(res, 200, { configured: false });
return;
}
// IPv6 clients fold to their /64, so one host cannot spread across it.
if (!allow(clientNetworkKey(req))) {
json(res, 429, { error: 'Too Many Requests' });
return;
}
relay.demand();
watch();
json(res, 200, relay.snapshot());
}
function install(server) {
server.middlewares.use(ISS_TELEMETRY_ROUTE, handle);
// Vite restarts the server in-process while this module's state lives on.
server.httpServer?.on('close', stop);
}
return {
name: 'iss-telemetry-proxy',
configureServer: install,
configurePreviewServer: install,
// Middleware mode has no httpServer to hang 'close' on.
closeBundle: stop,
};
}
@@ -0,0 +1,430 @@
import assert from 'node:assert/strict';
import test from 'node:test';
import {
ISS_TELEMETRY_ITEMS,
ISS_TELEMETRY_ROUTE,
createIssTelemetryRelay,
issTelemetryEnabled,
issTelemetryProxy,
issTelemetryTimeMs,
parseTlcpUpdate,
} from './issTelemetry.js';
/** A `ws`-shaped socket the test drives by hand. */
function fakeSocket() {
const handlers = {};
return {
sent: [],
terminated: false,
on(event, handler) {
handlers[event] = handler;
},
send(text) {
this.sent.push(text);
},
terminate() {
this.terminated = true;
},
emit(event, payload) {
handlers[event]?.(payload);
},
};
}
const index = (name) => ISS_TELEMETRY_ITEMS.indexOf(name) + 1;
/** A relay on a hand-moved clock, with every socket it opened. */
function relayAt(startMs) {
const clock = { ms: startMs };
const sockets = [];
const relay = createIssTelemetryRelay({
createSocket: () => {
const socket = fakeSocket();
sockets.push(socket);
return socket;
},
now: () => clock.ms,
});
return { relay, sockets, clock };
}
const NOV_13 = Date.UTC(2026, 10, 13, 11, 0, 0);
/** The feed's stamp for a moment: day of year × 24 + hours of that day. */
const stampOf = (ms) =>
(ms - Date.UTC(new Date(ms).getUTCFullYear(), 0, 0)) / 3_600_000;
test('only ISS_TELEMETRY_ENABLED=1 switches the relay on', () => {
assert.equal(issTelemetryEnabled({}), false);
assert.equal(issTelemetryEnabled({ ISS_TELEMETRY_ENABLED: '' }), false);
assert.equal(issTelemetryEnabled({ ISS_TELEMETRY_ENABLED: '0' }), false);
assert.equal(issTelemetryEnabled({ ISS_TELEMETRY_ENABLED: 'true' }), false);
assert.equal(issTelemetryEnabled({ ISS_TELEMETRY_ENABLED: ' 1 ' }), true);
});
test("the feed's stamp is hours from the day before January 1st", () => {
// 13 November 2026 is day 317: 317 × 24 + 11 hours.
assert.equal(issTelemetryTimeMs(317 * 24 + 11, NOV_13), NOV_13);
assert.equal(
issTelemetryTimeMs(stampOf(NOV_13 - 5000), NOV_13),
NOV_13 - 5000,
);
// A value last sent on 30 December, read on 2 January, belongs to last year.
const january = Date.UTC(2027, 0, 2, 0, 0, 0);
assert.equal(
issTelemetryTimeMs(364 * 24, january),
Date.UTC(2026, 11, 30, 0, 0, 0),
);
assert.equal(issTelemetryTimeMs(Number.NaN, NOV_13), null);
assert.equal(issTelemetryTimeMs(-1, NOV_13), null);
});
test('an update line decodes against the previous field values', () => {
const previous = new Map([[3, ['7000.5', '10.25', '24']]]);
const of = (item) => previous.get(item);
assert.deepEqual(parseTlcpUpdate('U,1,3,7000.6|11.5|24', of, 3), {
item: 3,
fields: ['7000.6', '11.5', '24'],
});
// Empty = unchanged, ^n = n unchanged, # = null, $ = empty string.
assert.deepEqual(parseTlcpUpdate('U,1,3,7000.7||', of, 3).fields, [
'7000.7',
'10.25',
'24',
]);
assert.deepEqual(parseTlcpUpdate('U,1,3,7000.8|^2', of, 3).fields, [
'7000.8',
'10.25',
'24',
]);
assert.deepEqual(parseTlcpUpdate('U,1,3,#|$|24', of, 3).fields, [
null,
'',
'24',
]);
assert.deepEqual(parseTlcpUpdate('U,1,3,a%7Cb|1|24', of, 3).fields[0], 'a|b');
// Not updates, or malformed ones.
for (const line of [
'PROBE',
'SUBOK,1,23,3',
'U,1',
'U,1,x,1|2|3',
'U,1,0,1|2|3',
'U,1,3,1|2',
'U,1,3,1|2|3|4',
'U,1,3,^9',
'U,1,3,%E0%A4%A|1|24',
])
assert.equal(parseTlcpUpdate(line, of, 3), null, line);
});
test('the relay opens on the first request, subscribes, and reports live values', () => {
const { relay, sockets, clock } = relayAt(NOV_13);
assert.equal(sockets.length, 0, 'nothing is contacted before a request');
relay.demand();
assert.equal(sockets.length, 1);
assert.equal(relay.snapshot().status, 'connecting');
const socket = sockets[0];
socket.emit('open');
assert.match(socket.sent[0], /^create_session\r\nLS_adapter_set=ISSLIVE&/);
socket.emit(
'message',
'CONOK,S1,50000,5000,*\r\nSERVNAME,x\r\nCLIENTIP,192.0.2.1',
);
assert.match(
socket.sent[1],
/^control\r\n.*LS_mode=MERGE.*LS_schema=TimeStamp%20Value%20Status\.Class/,
);
assert.ok(socket.sent[1].includes('S0000004%20S0000003'));
const stamp = stampOf(NOV_13 - 2000);
socket.emit(
'message',
`U,1,${index('S0000004')},${stamp}|346.12|24\r\nU,1,${index('TIME_000001')},${stamp}|123|24`,
);
let body = relay.snapshot();
assert.equal(body.configured, true);
assert.equal(body.status, 'live');
assert.deepEqual(body.items.S0000004, [346.12, NOV_13 - 2000]);
assert.equal(body.items.TIME_000001, undefined, 'the clock is not a joint');
assert.equal(JSON.stringify(body).includes('192.0.2.1'), false);
// A later update with an unchanged stamp field keeps the old stamp.
clock.ms += 1000;
socket.emit('message', `U,1,${index('S0000004')},|346.5|`);
body = relay.snapshot();
assert.deepEqual(body.items.S0000004, [346.5, NOV_13 - 2000]);
});
test('no station clock tick for 20 s, or a clock without signal, is no-signal', () => {
const { relay, sockets, clock } = relayAt(NOV_13);
relay.demand();
const socket = sockets[0];
socket.emit('open');
socket.emit('message', 'CONOK,S1,50000,5000,*');
const clockLine = (cls) =>
`U,1,${index('TIME_000001')},${stampOf(clock.ms)}|1|${cls}`;
socket.emit('message', clockLine('24'));
assert.equal(relay.snapshot().status, 'live');
clock.ms += 19_000;
socket.emit('message', 'PROBE');
assert.equal(relay.snapshot().status, 'live');
clock.ms += 2_000;
assert.equal(relay.snapshot().status, 'no-signal');
socket.emit('message', clockLine('1'));
assert.equal(relay.snapshot().status, 'no-signal');
socket.emit('message', clockLine('24'));
assert.equal(relay.snapshot().status, 'live');
});
test('a lost or silent connection is retried on a ladder, and stale events are ignored', () => {
const { relay, sockets, clock } = relayAt(NOV_13);
relay.demand();
const first = sockets[0];
first.emit('open');
first.emit('close');
assert.equal(first.terminated, true);
assert.equal(relay.snapshot().status, 'reconnecting');
// Not before 5 s.
clock.ms += 4_000;
relay.demand();
relay.tick();
assert.equal(sockets.length, 1);
clock.ms += 1_000;
relay.demand();
assert.equal(sockets.length, 2);
// The dead socket's late events change nothing.
first.emit('message', `U,1,${index('S0000004')},1|999|24`);
assert.equal(relay.snapshot().items.S0000004, undefined);
// A second failure waits 15 s.
sockets[1].emit('error', new Error('x'));
clock.ms += 14_000;
relay.demand();
assert.equal(sockets.length, 2);
clock.ms += 1_000;
relay.demand();
assert.equal(sockets.length, 3);
// A socket that goes silent for 30 s is recycled by the tick.
const third = sockets[2];
third.emit('open');
third.emit('message', 'CONOK,S1,50000,5000,*');
clock.ms += 29_000;
relay.demand();
relay.tick();
assert.equal(third.terminated, false);
clock.ms += 2_000;
relay.demand();
relay.tick();
assert.equal(third.terminated, true);
});
test("a reconnect cannot mark the previous socket's joint samples live", () => {
const { relay, sockets, clock } = relayAt(NOV_13);
relay.demand();
const first = sockets[0];
first.emit('open');
first.emit('message', 'CONOK,S1,50000,5000,*');
const stamp = stampOf(clock.ms);
first.emit(
'message',
`U,1,${index('S0000004')},${stamp}|17|24\r\n` +
`U,1,${index('TIME_000001')},${stamp}|1|24`,
);
assert.equal(relay.snapshot().status, 'live');
assert.deepEqual(relay.snapshot().items.S0000004, [17, clock.ms]);
first.emit('close');
assert.equal(relay.snapshot().status, 'reconnecting');
clock.ms += 5_000;
relay.demand();
assert.equal(sockets.length, 2);
assert.equal(relay.snapshot().status, 'connecting');
assert.deepEqual(relay.snapshot().items, {});
const second = sockets[1];
second.emit('open');
second.emit('message', 'CONOK,S2,50000,5000,*');
// The clock can return before any new joint snapshot is delivered.
second.emit(
'message',
`U,1,${index('TIME_000001')},${stampOf(clock.ms)}|1|24`,
);
assert.equal(relay.snapshot().status, 'live');
assert.deepEqual(
relay.snapshot().items,
{},
'the new clock must not revive old joint angles',
);
second.emit('message', `U,1,${index('S0000004')},${stampOf(clock.ms)}|23|24`);
assert.deepEqual(relay.snapshot().items.S0000004, [23, clock.ms]);
});
test('a refused or ended session starts over', () => {
for (const line of ['CONERR,2,x', 'END,31,x', 'LOOP,0', 'REQERR,1,17,x']) {
const { relay, sockets } = relayAt(NOV_13);
relay.demand();
sockets[0].emit('open');
sockets[0].emit('message', line);
assert.equal(sockets[0].terminated, true, line);
assert.equal(relay.snapshot().status, 'reconnecting', line);
}
});
test('with nobody asking for two minutes the connection closes and the values go', () => {
const { relay, sockets, clock } = relayAt(NOV_13);
relay.demand();
const socket = sockets[0];
socket.emit('open');
socket.emit('message', 'CONOK,S1,50000,5000,*');
socket.emit('message', `U,1,${index('S0000003')},${stampOf(clock.ms)}|14|24`);
clock.ms += 119_000;
socket.emit('message', 'PROBE');
assert.equal(relay.tick(), true);
assert.equal(socket.terminated, false);
clock.ms += 2_000;
assert.equal(relay.tick(), false);
assert.equal(socket.terminated, true);
assert.deepEqual(relay.snapshot().items, {});
// The next request opens a fresh connection.
relay.demand();
assert.equal(sockets.length, 2);
});
test('an oversized or malformed frame is dropped without taking the relay down', () => {
const { relay, sockets } = relayAt(NOV_13);
relay.demand();
const socket = sockets[0];
socket.emit('open');
socket.emit('message', 'CONOK,S1,50000,5000,*');
socket.emit('message', { length: 10_000_000 });
socket.emit('message', `U,1,${index('S0000004')},1|not-a-number|24`);
socket.emit('message', 'U,1,999,1|2|24');
assert.deepEqual(relay.snapshot().items, {});
assert.equal(socket.terminated, false);
});
test('without a socket library the relay says unavailable', () => {
const relay = createIssTelemetryRelay({ createSocket: () => null });
relay.demand();
assert.equal(relay.snapshot().status, 'unavailable');
});
/** Mount the plugin on a fake Connect stack and return a request function. */
function install(plugin, preview = false) {
const routes = new Map();
plugin[preview ? 'configurePreviewServer' : 'configureServer']({
middlewares: { use: (route, handler) => routes.set(route, handler) },
});
return (method = 'GET', headers = {}, url = '/') => {
const res = {
headers: {},
writeHead(status, headers) {
Object.assign(this, { status, headers });
},
setHeader(name, value) {
this.headers[name] = value;
},
end(body) {
this.body = body;
this.status ??= this.statusCode;
},
};
routes.get(ISS_TELEMETRY_ROUTE)({ url, method, headers, socket: {} }, res);
return res;
};
}
test('the route is off by default and never opens the feed', () => {
let opened = 0;
const plugin = issTelemetryProxy({
env: {},
createSocket: () => {
opened += 1;
return fakeSocket();
},
});
assert.equal(typeof plugin.configurePreviewServer, 'function');
for (const preview of [false, true]) {
const res = install(plugin, preview)();
assert.equal(res.status, 200);
assert.deepEqual(JSON.parse(res.body), { configured: false });
assert.equal(res.headers['Cache-Control'], 'no-store');
}
assert.equal(opened, 0);
});
test('switched on, the route opens the feed once and serves the snapshot', () => {
const sockets = [];
const plugin = issTelemetryProxy({
env: { ISS_TELEMETRY_ENABLED: '1' },
createSocket: () => {
const socket = fakeSocket();
sockets.push(socket);
return socket;
},
now: () => NOV_13,
});
const request = install(plugin);
try {
const first = JSON.parse(request().body);
assert.equal(first.configured, true);
assert.equal(first.status, 'connecting');
request();
assert.equal(sockets.length, 1, 'one connection for every request');
assert.equal(request('POST').status, 405);
const unknown = request('GET', {}, '/anything');
assert.equal(unknown.status, 404);
assert.deepEqual(JSON.parse(unknown.body), { error: 'Not Found' });
assert.equal(request('GET', {}, '/?t=1').status, 200, 'a query is fine');
assert.equal(
request('GET', { 'sec-fetch-site': 'cross-site' }).status,
403,
);
// Sixty requests a minute from one client, then 429.
let limited = 0;
for (let i = 0; i < 70; i++) if (request().status === 429) limited += 1;
assert.ok(limited > 0);
} finally {
plugin.closeBundle();
}
assert.equal(sockets[0].terminated, true);
});
test('a session that keeps ending soon after its snapshot climbs the retry ladder', () => {
const { relay, sockets, clock } = relayAt(NOV_13);
relay.demand();
/** The current socket opens, sends a snapshot update, then ends. */
const shortSession = () => {
const socket = sockets.at(-1);
socket.emit('open');
socket.emit('message', 'CONOK,S1,50000,5000,*');
socket.emit(
'message',
`U,1,${index('S0000004')},${stampOf(clock.ms)}|17|24`,
);
socket.emit('message', 'END,41,session closed');
};
// Each wait is the next step: 5 s, 15 s, 60 s, then 5 minutes.
for (const wait of [5_000, 15_000, 60_000, 300_000, 300_000]) {
shortSession();
const before = sockets.length;
clock.ms += wait - 1_000;
relay.demand();
assert.equal(sockets.length, before, `not before ${wait / 1000} s`);
clock.ms += 1_000;
relay.demand();
assert.equal(sockets.length, before + 1, `again after ${wait / 1000} s`);
}
// A session that runs a minute before its updates brings the ladder down.
const socket = sockets.at(-1);
socket.emit('open');
socket.emit('message', 'CONOK,S1,50000,5000,*');
clock.ms += 60_000;
relay.demand();
socket.emit('message', `U,1,${index('S0000004')},${stampOf(clock.ms)}|17|24`);
socket.emit('message', 'END,41,session closed');
const before = sockets.length;
clock.ms += 5_000;
relay.demand();
assert.equal(sockets.length, before + 1, 'back to 5 s');
});