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 { BINARY_PREVIEW_MAX_BYTES } from "./binaryPreviewProtocol"; |
| 2 | import { PreviewError } from "./previewFailure"; |
| 3 | |
| 4 | export async function readBinaryPreview(src: string, signal: AbortSignal) { |
| 5 | const response = await fetch(src, { signal }); |
| 6 | if (!response.ok) throw new PreviewError({ code: "file-load-failed" }); |
| 7 | if (Number(response.headers.get("content-length")) > BINARY_PREVIEW_MAX_BYTES) |
| 8 | throw new PreviewError({ code: "file-too-large" }); |
| 9 | const reader = response.body?.getReader(); |
| 10 | if (!reader) throw new PreviewError({ code: "file-unavailable" }); |
| 11 | const chunks: Uint8Array<ArrayBuffer>[] = []; |
| 12 | let size = 0; |
| 13 | try { |
| 14 | while (true) { |
| 15 | const { value, done } = await reader.read(); |
| 16 | if (done) break; |
| 17 | size += value.length; |
| 18 | if (size > BINARY_PREVIEW_MAX_BYTES) throw new PreviewError({ code: "file-too-large" }); |
| 19 | chunks.push(value); |
| 20 | } |
| 21 | } finally { |
| 22 | await reader.cancel(); |
| 23 | } |
| 24 | return new Blob(chunks).arrayBuffer(); |
| 25 | } |