Merge pull request #449 from trails-cool/chore/remove-eslint-disables
chore(ts): drop the last 2 `eslint-disable` comments in prod code
This commit is contained in:
commit
c54f0361cf
2 changed files with 12 additions and 22 deletions
|
|
@ -79,22 +79,27 @@ export async function readBodyWithCap(response: Response, maxBytes: number): Pro
|
||||||
if (Number.isFinite(declared) && declared > maxBytes) {
|
if (Number.isFinite(declared) && declared > maxBytes) {
|
||||||
throw new BRouterError(`response too large (${declared} bytes)`, 502);
|
throw new BRouterError(`response too large (${declared} bytes)`, 502);
|
||||||
}
|
}
|
||||||
// Stream the body, abort once we've seen `maxBytes`.
|
// Stream the body, abort once we've seen `maxBytes`. The read +
|
||||||
|
// done-check live in the for header so the loop reads as "iterate
|
||||||
|
// over reads until done"; TS narrows `chunk.value` to Uint8Array
|
||||||
|
// inside the body because the false-done branch rules out
|
||||||
|
// `{ done: true, value: undefined }`.
|
||||||
const reader = response.body?.getReader();
|
const reader = response.body?.getReader();
|
||||||
if (!reader) return await response.text();
|
if (!reader) return await response.text();
|
||||||
const decoder = new TextDecoder();
|
const decoder = new TextDecoder();
|
||||||
let received = 0;
|
let received = 0;
|
||||||
let out = "";
|
let out = "";
|
||||||
// eslint-disable-next-line no-constant-condition
|
for (
|
||||||
while (true) {
|
let chunk = await reader.read();
|
||||||
const { value, done } = await reader.read();
|
!chunk.done;
|
||||||
if (done) break;
|
chunk = await reader.read()
|
||||||
received += value.byteLength;
|
) {
|
||||||
|
received += chunk.value.byteLength;
|
||||||
if (received > maxBytes) {
|
if (received > maxBytes) {
|
||||||
try { await reader.cancel(); } catch { /* ignore */ }
|
try { await reader.cancel(); } catch { /* ignore */ }
|
||||||
throw new BRouterError(`response exceeded ${maxBytes} bytes`, 502);
|
throw new BRouterError(`response exceeded ${maxBytes} bytes`, 502);
|
||||||
}
|
}
|
||||||
out += decoder.decode(value, { stream: true });
|
out += decoder.decode(chunk.value, { stream: true });
|
||||||
}
|
}
|
||||||
out += decoder.decode();
|
out += decoder.decode();
|
||||||
return out;
|
return out;
|
||||||
|
|
|
||||||
|
|
@ -15,21 +15,6 @@ async function getDOMParser(): Promise<typeof DOMParser> {
|
||||||
return _LinkedDOMParser;
|
return _LinkedDOMParser;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function parseGpx(xml: string): GpxData {
|
|
||||||
// Synchronous path for browser
|
|
||||||
if (typeof DOMParser !== "undefined") {
|
|
||||||
return parseGpxWithParser(new DOMParser(), xml);
|
|
||||||
}
|
|
||||||
// Fallback: try linkedom synchronously (works in Node with top-level await or CJS)
|
|
||||||
try {
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
||||||
const { DOMParser: LP } = require("linkedom");
|
|
||||||
return parseGpxWithParser(new LP() as unknown as DOMParser, xml);
|
|
||||||
} catch {
|
|
||||||
throw new Error("DOMParser not available — install linkedom");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function parseGpxAsync(xml: string): Promise<GpxData> {
|
export async function parseGpxAsync(xml: string): Promise<GpxData> {
|
||||||
const Parser = await getDOMParser();
|
const Parser = await getDOMParser();
|
||||||
return parseGpxWithParser(new Parser(), xml);
|
return parseGpxWithParser(new Parser(), xml);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue