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 JSZip from "jszip"; |
| 2 | import { checkZipDirectory } from "./zipDirectory.ts"; |
| 3 | import { ZipError } from "./zipError.ts"; |
| 4 | |
| 5 | export const ARCHIVE_MAX_BYTES = 50 * 1024 * 1024; |
| 6 | export const ARCHIVE_ENTRY_MAX_BYTES = 10 * 1024 * 1024; |
| 7 | export const ARCHIVE_MAX_ENTRIES = 2048; |
| 8 | export const ARCHIVE_MAX_RATIO = 200; |
| 9 | |
| 10 | export async function boundedZip( |
| 11 | bytes: ArrayBuffer, |
| 12 | entryLimit = ARCHIVE_MAX_BYTES, |
| 13 | limits: { |
| 14 | totalLimit?: number; |
| 15 | xmlLimit?: number; |
| 16 | ratioFloor?: number; |
| 17 | markupLimit?: number; |
| 18 | } = {}, |
| 19 | ) { |
| 20 | if (bytes.byteLength > ARCHIVE_MAX_BYTES) throw new ZipError({ code: "file-too-large" }); |
| 21 | checkZipDirectory(bytes, ARCHIVE_MAX_ENTRIES); |
| 22 | const zip = await JSZip.loadAsync(bytes); |
| 23 | const entries = Object.values(zip.files); |
| 24 | if (entries.length > ARCHIVE_MAX_ENTRIES) |
| 25 | throw new ZipError({ code: "archive-too-many-entries" }); |
| 26 | const files = new Map< |
| 27 | string, |
| 28 | { bytes: Uint8Array<ArrayBuffer>; date: Date; directory: boolean } |
| 29 | >(); |
| 30 | let total = 0; |
| 31 | let markup = 0; |
| 32 | for (const entry of entries) { |
| 33 | if (entry.name.length > 1024) throw new ZipError({ code: "archive-path-too-long" }); |
| 34 | const chunks: Uint8Array<ArrayBuffer>[] = []; |
| 35 | let length = 0; |
| 36 | await new Promise<void>((resolve, reject) => { |
| 37 | const stream = ( |
| 38 | entry as JSZip.JSZipObject & { |
| 39 | internalStream(type: "uint8array"): JSZip.JSZipStreamHelper<Uint8Array<ArrayBuffer>>; |
| 40 | } |
| 41 | ).internalStream("uint8array"); |
| 42 | stream |
| 43 | .on("data", (chunk) => { |
| 44 | total += chunk.length; |
| 45 | length += chunk.length; |
| 46 | // Word relationship targets can use arbitrary extensions, so count every part. |
| 47 | if (limits.markupLimit !== undefined) |
| 48 | for (const byte of chunk) if (byte === 0x3c) markup++; |
| 49 | if ( |
| 50 | markup > (limits.markupLimit ?? Infinity) || |
| 51 | total > (limits.totalLimit ?? ARCHIVE_MAX_BYTES) || |
| 52 | total > |
| 53 | Math.max(limits.ratioFloor ?? 0, Math.max(1, bytes.byteLength) * ARCHIVE_MAX_RATIO) || |
| 54 | length > entryLimit || |
| 55 | (entry.name.endsWith(".xml") && length > (limits.xmlLimit ?? 8 * 1024 * 1024)) |
| 56 | ) { |
| 57 | stream.pause(); |
| 58 | reject(new ZipError({ code: "archive-expanded-too-large" })); |
| 59 | } else chunks.push(chunk); |
| 60 | }) |
| 61 | .on("error", reject) |
| 62 | .on("end", resolve) |
| 63 | .resume(); |
| 64 | }); |
| 65 | const content = new Uint8Array(length); |
| 66 | let offset = 0; |
| 67 | for (const chunk of chunks) { |
| 68 | content.set(chunk, offset); |
| 69 | offset += chunk.length; |
| 70 | } |
| 71 | files.set(entry.name, { bytes: content, date: entry.date, directory: entry.dir }); |
| 72 | } |
| 73 | return files; |
| 74 | } |