mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-20 18:22:25 +00:00
Parse Lithuania catalogs inside the shared reader so bad JSON cancels the sibling
This commit is contained in:
1 parent
b6d5d7373f
commit
247452c202
3 files changed
+42
-38
No files matched your search
@@ -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<string[]>} The body texts, in feed order.
|
||||
* @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 },
|
||||
{ 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(() => {});
|
||||
|
||||
@@ -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 [];
|
||||
|
||||
@@ -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('<html>error page</html>')],
|
||||
]) {
|
||||
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 = [];
|
||||
|
||||
Reference in new issue
Block a user