The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import type { Database } from "sql.js"; |
| 2 | |
| 3 | import { boundedZip } from "@t3tools/client-runtime/zip/bounded-zip"; |
| 4 | import { |
| 5 | ARCHIVE_ENTRY_MAX_BYTES, |
| 6 | ARCHIVE_MAX_ENTRIES, |
| 7 | ARCHIVE_MAX_RATIO, |
| 8 | BINARY_PREVIEW_MAX_BYTES, |
| 9 | SQLITE_PAGE_SIZE, |
| 10 | type ArchiveEntry, |
| 11 | type BinaryWorkerRequest, |
| 12 | type BinaryWorkerResult, |
| 13 | } from "./binaryPreviewProtocol"; |
| 14 | import { PreviewError, previewFailure } from "./previewFailure"; |
| 15 | |
| 16 | let database: Database | null = null; |
| 17 | let tables: string[] = []; |
| 18 | // Entries over the preview limit keep only their size, so their payload is never copied. |
| 19 | let files = new Map<string, Uint8Array<ArrayBuffer> | number>(); |
| 20 | const reply = (result: BinaryWorkerResult) => self.postMessage(result, { transfer: [] }); |
| 21 | const quote = (name: string) => `"${name.replaceAll('"', '""')}"`; |
| 22 | |
| 23 | async function gunzip(bytes: ArrayBuffer) { |
| 24 | const reader = new Blob([bytes]) |
| 25 | .stream() |
| 26 | .pipeThrough(new DecompressionStream("gzip")) |
| 27 | .getReader(); |
| 28 | const chunks: Uint8Array<ArrayBuffer>[] = []; |
| 29 | let length = 0; |
| 30 | try { |
| 31 | while (true) { |
| 32 | const { value, done } = await reader.read(); |
| 33 | if (done) break; |
| 34 | length += value.length; |
| 35 | if (length > BINARY_PREVIEW_MAX_BYTES || length > bytes.byteLength * ARCHIVE_MAX_RATIO) |
| 36 | throw new PreviewError({ code: "archive-expanded-too-large" }); |
| 37 | chunks.push(value); |
| 38 | } |
| 39 | } finally { |
| 40 | await reader.cancel(); |
| 41 | } |
| 42 | return new Uint8Array(await new Blob(chunks).arrayBuffer()); |
| 43 | } |
| 44 | |
| 45 | function tarEntries(bytes: Uint8Array<ArrayBuffer>) { |
| 46 | const entries: ArchiveEntry[] = []; |
| 47 | const text = (offset: number, length: number) => |
| 48 | new TextDecoder().decode(bytes.subarray(offset, offset + length)).split("\0", 1)[0] ?? ""; |
| 49 | const number = (offset: number, length: number) => { |
| 50 | const value = text(offset, length).trim(); |
| 51 | if (!/^[0-7]*$/.test(value)) throw new PreviewError({ code: "tar-numeric-format" }); |
| 52 | return Number.parseInt(value || "0", 8); |
| 53 | }; |
| 54 | let longPath: string | null = null; |
| 55 | let longLink: string | null = null; |
| 56 | const globalPax: Record<string, string> = {}; |
| 57 | let localPax: Record<string, string> = {}; |
| 58 | let offset = 0; |
| 59 | while (offset + 512 <= bytes.length) { |
| 60 | if (bytes.subarray(offset, offset + 512).every((byte) => byte === 0)) break; |
| 61 | if (entries.length >= ARCHIVE_MAX_ENTRIES) |
| 62 | throw new PreviewError({ code: "archive-too-many-entries" }); |
| 63 | let checksum = 0; |
| 64 | for (let index = 0; index < 512; index++) |
| 65 | checksum += index >= 148 && index < 156 ? 32 : bytes[offset + index]!; |
| 66 | if (checksum !== number(offset + 148, 8)) |
| 67 | throw new PreviewError({ code: "tar-header-damaged" }); |
| 68 | const type = text(offset + 156, 1); |
| 69 | const metadata = type === "L" || type === "K" || type === "x" || type === "g"; |
| 70 | const attributes: Record<string, string> = metadata ? {} : { ...globalPax, ...localPax }; |
| 71 | const size = attributes.size ? Number(attributes.size) : number(offset + 124, 12); |
| 72 | if (attributes.size && !/^\d+$/.test(attributes.size)) |
| 73 | throw new PreviewError({ code: "archive-entry-damaged" }); |
| 74 | const prefix = |
| 75 | text(offset + 257, 6) === "ustar" && text(offset + 263, 2) === "00" |
| 76 | ? text(offset + 345, 155) |
| 77 | : ""; |
| 78 | const path = |
| 79 | attributes.path || longPath || [prefix, text(offset, 100)].filter(Boolean).join("/"); |
| 80 | const linkpath = attributes.linkpath ?? longLink ?? text(offset + 157, 100); |
| 81 | if (!metadata) { |
| 82 | longLink = null; |
| 83 | longPath = null; |
| 84 | localPax = {}; |
| 85 | } |
| 86 | if ( |
| 87 | path.length > 1024 || |
| 88 | !Number.isSafeInteger(size) || |
| 89 | size < 0 || |
| 90 | size > BINARY_PREVIEW_MAX_BYTES || |
| 91 | offset + 512 + size > bytes.length |
| 92 | ) |
| 93 | throw new PreviewError({ code: "archive-entry-damaged" }); |
| 94 | const content = bytes.subarray(offset + 512, offset + 512 + size); |
| 95 | if (type === "L") longPath = new TextDecoder().decode(content).replace(/\0.*$/s, ""); |
| 96 | else if (type === "K") longLink = new TextDecoder().decode(content).replace(/\0.*$/s, ""); |
| 97 | else if (type === "x" || type === "g") { |
| 98 | if (size > 65536) throw new PreviewError({ code: "archive-metadata-too-large" }); |
| 99 | for (let cursor = 0; cursor < content.length;) { |
| 100 | const separator = content.indexOf(32, cursor); |
| 101 | if (separator < cursor || separator - cursor > 10) |
| 102 | throw new PreviewError({ code: "tar-metadata-damaged" }); |
| 103 | const lengthText = new TextDecoder().decode(content.subarray(cursor, separator)); |
| 104 | const length = Number(lengthText); |
| 105 | const end = cursor + length; |
| 106 | if ( |
| 107 | !/^[1-9]\d*$/.test(lengthText) || |
| 108 | !Number.isSafeInteger(length) || |
| 109 | end > content.length || |
| 110 | end <= separator + 1 || |
| 111 | content[end - 1] !== 10 |
| 112 | ) |
| 113 | throw new PreviewError({ code: "tar-metadata-damaged" }); |
| 114 | const record = new TextDecoder().decode(content.subarray(separator + 1, end - 1)); |
| 115 | const equals = record.indexOf("="); |
| 116 | if (equals < 1) throw new PreviewError({ code: "tar-metadata-damaged" }); |
| 117 | const key = record.slice(0, equals); |
| 118 | const value = record.slice(equals + 1); |
| 119 | if (["path", "linkpath", "size", "mtime"].includes(key)) { |
| 120 | if (type === "x") localPax[key] = value; |
| 121 | else if (value || key === "linkpath") globalPax[key] = value; |
| 122 | else delete globalPax[key]; |
| 123 | } |
| 124 | cursor = end; |
| 125 | } |
| 126 | } else if ( |
| 127 | type === "" || |
| 128 | type === "0" || |
| 129 | type === "5" || |
| 130 | // A hard link shares an earlier member's bytes; one without that member is skipped. |
| 131 | (type === "1" && linkpath && files.has(linkpath)) |
| 132 | ) { |
| 133 | if (attributes.mtime && !/^[+-]?\d+(?:\.\d+)?$/.test(attributes.mtime)) |
| 134 | throw new PreviewError({ code: "tar-metadata-damaged" }); |
| 135 | const modified = |
| 136 | (attributes.mtime ? Number(attributes.mtime) : number(offset + 136, 12)) * 1000; |
| 137 | const date = new Date(modified); |
| 138 | const linked = type === "1" ? files.get(linkpath) : undefined; |
| 139 | entries.push({ |
| 140 | path, |
| 141 | size: typeof linked === "number" ? linked : (linked?.length ?? size), |
| 142 | directory: type === "5", |
| 143 | modified: Number.isNaN(date.getTime()) ? null : date.toISOString(), |
| 144 | }); |
| 145 | if (type === "5") { |
| 146 | files.delete(path); |
| 147 | files.delete(path.replace(/\/+$/, "")); |
| 148 | } else files.set(path, linked ?? (size > ARCHIVE_ENTRY_MAX_BYTES ? size : content.slice())); |
| 149 | } else { |
| 150 | files.delete(path); |
| 151 | } |
| 152 | offset += 512 + Math.ceil(size / 512) * 512; |
| 153 | } |
| 154 | if (entries.length === 0) throw new PreviewError({ code: "archive-empty" }); |
| 155 | return entries; |
| 156 | } |
| 157 | |
| 158 | async function handle(input: BinaryWorkerRequest) { |
| 159 | if (input.type === "entry") { |
| 160 | const bytes = files.get(input.path); |
| 161 | if (bytes === undefined) throw new PreviewError({ code: "archive-entry-unavailable" }); |
| 162 | if (typeof bytes === "number" || bytes.length > ARCHIVE_ENTRY_MAX_BYTES) |
| 163 | throw new PreviewError({ code: "archive-entry-too-large" }); |
| 164 | reply({ type: "entry", path: input.path, bytes }); |
| 165 | return; |
| 166 | } |
| 167 | if (input.type === "rows") { |
| 168 | if ( |
| 169 | !database || |
| 170 | !tables.includes(input.table) || |
| 171 | !Number.isSafeInteger(input.page) || |
| 172 | input.page < 0 |
| 173 | ) |
| 174 | throw new PreviewError({ code: "table-unavailable" }); |
| 175 | const columns = |
| 176 | database |
| 177 | .exec(`PRAGMA table_xinfo(${quote(input.table)})`)[0] |
| 178 | ?.values.filter((row) => row[6] !== 1) |
| 179 | .map((row) => String(row[1])) |
| 180 | .slice(0, 50) ?? []; |
| 181 | const fields = columns.map((name) => { |
| 182 | const field = quote(name); |
| 183 | return `CASE WHEN typeof(${field}) = 'blob' THEN '[blob: ' || length(${field}) || ' bytes]' ELSE substr(CAST(${field} AS TEXT), 1, 1024) END`; |
| 184 | }); |
| 185 | const result = database.exec( |
| 186 | `SELECT ${fields.join(",")} FROM ${quote(input.table)} LIMIT ${SQLITE_PAGE_SIZE} OFFSET ${input.page * SQLITE_PAGE_SIZE}`, |
| 187 | )[0]; |
| 188 | reply({ |
| 189 | type: "rows", |
| 190 | table: input.table, |
| 191 | page: input.page, |
| 192 | columns, |
| 193 | rows: |
| 194 | result?.values.map((row) => row.map((cell) => (cell === null ? "NULL" : String(cell)))) ?? |
| 195 | [], |
| 196 | }); |
| 197 | return; |
| 198 | } |
| 199 | if (input.bytes.byteLength > BINARY_PREVIEW_MAX_BYTES) |
| 200 | throw new PreviewError({ code: "file-too-large" }); |
| 201 | if (input.kind === "sqlite") { |
| 202 | const [{ default: initSqlJs }, { default: wasmUrl }] = await Promise.all([ |
| 203 | import("sql.js"), |
| 204 | import("sql.js/dist/sql-wasm.wasm?url"), |
| 205 | ]); |
| 206 | const SQL = await initSqlJs({ locateFile: () => wasmUrl }); |
| 207 | database = new SQL.Database(new Uint8Array(input.bytes)); |
| 208 | database.run("PRAGMA query_only=ON; PRAGMA trusted_schema=OFF;"); |
| 209 | tables = |
| 210 | database |
| 211 | .exec( |
| 212 | "SELECT name FROM sqlite_schema WHERE type='table' AND substr(name, 1, 7) != 'sqlite_' AND sql NOT LIKE 'CREATE VIRTUAL TABLE%' ORDER BY name LIMIT 101", |
| 213 | )[0] |
| 214 | ?.values.map((row) => String(row[0])) ?? []; |
| 215 | if (tables.length > 100) throw new PreviewError({ code: "database-too-many-tables" }); |
| 216 | reply({ |
| 217 | type: "sqlite", |
| 218 | tables: tables.map((name) => ({ |
| 219 | name, |
| 220 | count: Number( |
| 221 | database!.exec(`SELECT count(*) FROM ${quote(name)}`)[0]?.values[0]?.[0] ?? 0, |
| 222 | ), |
| 223 | })), |
| 224 | }); |
| 225 | } else { |
| 226 | files = new Map(); |
| 227 | let entries: ArchiveEntry[]; |
| 228 | if ( |
| 229 | /\.(zip|jar)$/i.test(input.name) || |
| 230 | (input.bytes.byteLength >= 4 && |
| 231 | [0x04034b50, 0x06054b50].includes(new DataView(input.bytes).getUint32(0, true))) |
| 232 | ) { |
| 233 | const archive = await boundedZip(input.bytes, BINARY_PREVIEW_MAX_BYTES); |
| 234 | entries = [...archive].map(([path, file]) => { |
| 235 | files.set(path, file.bytes); |
| 236 | return { |
| 237 | path, |
| 238 | size: file.bytes.length, |
| 239 | directory: file.directory, |
| 240 | modified: Number.isNaN(file.date.getTime()) ? null : file.date.toISOString(), |
| 241 | }; |
| 242 | }); |
| 243 | } else if (/\.(gz|tgz)$/i.test(input.name) || new Uint8Array(input.bytes)[0] === 0x1f) { |
| 244 | const bytes = await gunzip(input.bytes); |
| 245 | if (/\.(tar\.gz|tgz)$/i.test(input.name)) entries = tarEntries(bytes); |
| 246 | else { |
| 247 | const path = input.name.replace(/\.gz$/i, "") || "contents"; |
| 248 | if (/\.tar$/i.test(path)) entries = tarEntries(bytes); |
| 249 | else { |
| 250 | files.set(path, bytes); |
| 251 | entries = [{ path, size: bytes.length, directory: false, modified: null }]; |
| 252 | } |
| 253 | } |
| 254 | } else entries = tarEntries(new Uint8Array(input.bytes)); |
| 255 | reply({ type: "archive", entries }); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | self.addEventListener("message", (event: MessageEvent<BinaryWorkerRequest>) => { |
| 260 | void handle(event.data).catch((cause: unknown) => |
| 261 | reply({ type: "error", ...previewFailure(cause, "preview-failed") }), |
| 262 | ); |
| 263 | }); |