The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { PreviewError } from "./previewFailure"; |
| 2 | |
| 3 | export async function readBoundedDocument( |
| 4 | src: string, |
| 5 | signal: AbortSignal, |
| 6 | limit: number, |
| 7 | cache: RequestCache = "default", |
| 8 | ) { |
| 9 | const response = await fetch(src, { signal, cache }); |
| 10 | if (!response.ok || Number(response.headers.get("content-length")) > limit) |
| 11 | throw new PreviewError({ code: "document-unavailable-or-too-large" }); |
| 12 | const reader = response.body?.getReader(); |
| 13 | if (!reader) throw new PreviewError({ code: "document-unavailable" }); |
| 14 | const chunks: Uint8Array<ArrayBuffer>[] = []; |
| 15 | let size = 0; |
| 16 | try { |
| 17 | while (true) { |
| 18 | const { done, value } = await reader.read(); |
| 19 | if (done) break; |
| 20 | size += value.length; |
| 21 | if (size > limit) throw new PreviewError({ code: "document-too-large" }); |
| 22 | chunks.push(value); |
| 23 | } |
| 24 | } finally { |
| 25 | await reader.cancel(); |
| 26 | } |
| 27 | return new Blob(chunks).arrayBuffer(); |
| 28 | } |