diff --git a/server/providers/cctv/catalogPair.js b/server/providers/cctv/catalogPair.js index b6f0ffa901..d52ab75969 100644 --- a/server/providers/cctv/catalogPair.js +++ b/server/providers/cctv/catalogPair.js @@ -4,19 +4,21 @@ import { readResponseTextCapped } from '../common/http.js'; * Read catalogs that are only useful together, because the pack joins them. * * The requests share one deadline and refuse redirects. Whichever fails first - * (before its headers, on its status, or while its capped body is read) aborts - * the others at once, so a stalled feed is not waited on until the deadline. - * Every response body is released, including one that arrives after another - * feed has already failed. + * (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}} options - * @returns {Promise} The body texts, in feed order. + * @param {{accept: string, maxBytes: number, timeoutMs: number, + * parse?: (text: string) => unknown}} options + * @returns {Promise} The parsed bodies (texts without `parse`), in + * feed order. */ export async function readCatalogsTogether( feeds, - { accept, maxBytes, timeoutMs }, + { accept, maxBytes, timeoutMs, parse = (text) => text }, ) { const sibling = new AbortController(); const signal = AbortSignal.any([ @@ -40,7 +42,7 @@ export async function readCatalogsTogether( : `${label} download failed: ${response.status}`, ); } - return await readResponseTextCapped(response, maxBytes, signal); + return parse(await readResponseTextCapped(response, maxBytes, signal)); } catch (error) { sibling.abort(); await response?.body?.cancel().catch(() => {}); diff --git a/server/providers/cctv/lithuania.js b/server/providers/cctv/lithuania.js index 5ce67d411b..c977749a20 100644 --- a/server/providers/cctv/lithuania.js +++ b/server/providers/cctv/lithuania.js @@ -84,19 +84,18 @@ export function lithuaniaCameraToSource(row, feature) { /** Two bounded requests per shared catalog refresh, no API key required. */ export async function loadLithuaniaSources() { try { - const [rows, layers] = ( - await readCatalogsTogether( - [ - { url: LITHUANIA_CAMERAS_URL, label: 'cameras' }, - { url: LITHUANIA_LOCATIONS_URL, label: 'locations' }, - ], - { - accept: 'application/json', - maxBytes: LITHUANIA_MAX_CATALOG_BYTES, - timeoutMs: CCTV_SOURCE_FETCH_TIMEOUT_MS, - }, - ) - ).map((text) => JSON.parse(text)); + 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 []; diff --git a/src/data/cctvLithuania.test.mjs b/src/data/cctvLithuania.test.mjs index af28a51f60..a59cea16b0 100644 --- a/src/data/cctvLithuania.test.mjs +++ b/src/data/cctvLithuania.test.mjs @@ -140,25 +140,28 @@ for (const [label, flag] of [ }); } -test('a failed catalog cancels the sibling request instead of waiting for the timeout', async (t) => { - t.mock.method(console, 'warn', () => {}); - let siblingSignal; - t.mock.method(globalThis, 'fetch', async (url, options) => { - if (url === LITHUANIA_CAMERAS_URL) { - return new Response('', { status: 503 }); - } - siblingSignal = options.signal; - // Never settles on its own; only an abort ends it. - return new Promise((_, reject) => { - options.signal.addEventListener('abort', () => - reject(options.signal.reason), - ); +for (const [failure, camerasResponse] of [ + ['failed', () => new Response('', { status: 503 })], + ['malformed', () => new Response('error page')], +]) { + test(`a ${failure} catalog cancels the sibling request instead of waiting for the timeout`, async (t) => { + t.mock.method(console, 'warn', () => {}); + let siblingSignal; + t.mock.method(globalThis, 'fetch', async (url, options) => { + if (url === LITHUANIA_CAMERAS_URL) return camerasResponse(); + siblingSignal = options.signal; + // Never settles on its own; only an abort ends it. + return new Promise((_, reject) => { + options.signal.addEventListener('abort', () => + reject(options.signal.reason), + ); + }); }); + assert.deepEqual(await loadLithuaniaSources(), []); + assert.equal(siblingSignal?.aborted, true); + assert.notEqual(siblingSignal.reason?.name, 'TimeoutError'); }); - assert.deepEqual(await loadLithuaniaSources(), []); - assert.equal(siblingSignal?.aborted, true); - assert.notEqual(siblingSignal.reason?.name, 'TimeoutError'); -}); +} test('a redirected catalog is reported as not followed', async (t) => { const warnings = [];