src/read-capped.ts
19 lines and 1 functions in DrDustinEdwards/capsid, at commit daa875b on 2026-10-24. This file on GitHub.
Functions
| Function | Lines | Complexity | Covered | CRAP | Mutation |
|---|---|---|---|---|---|
| readCapped | 4 to 20 | 5 | 100% | OkCRAP 5 | No data: mutation testing has not run on this repo yet |
Imports
It imports no other module of this repo. Imported by src/live-checks.ts, src/ops-snapshot.ts.
Source
// No imports: the explorer build loads this through ops-snapshot.ts (.github/workflows/explorer.yml).
/** The body as text, read no further than `max` bytes. */
export async function readCapped(res: Response, max: number): Promise<{ text: string; truncated: boolean }> {
if (!res.body) return { text: await res.text(), truncated: false };
const reader = res.body.getReader();
const decoder = new TextDecoder();
let text = "";
let size = 0;
for (;;) {
const { done, value } = await reader.read();
if (done) return { text: text + decoder.decode(), truncated: false };
size += value.byteLength;
if (size > max) {
await reader.cancel();
return { text, truncated: true };
}
text += decoder.decode(value, { stream: true });
}
}