Open History to see the last commit
1import type { Database } from "sql.js";
2
3import { boundedZip } from "@t3tools/client-runtime/zip/bounded-zip";
4import {
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
15let database: Database | null = null;
16let tables: string[] = [];
17// Entries over the preview limit keep only their size, so their payload is never copied.
18let files = new Map<string, Uint8Array<ArrayBuffer> | number>();
19const reply = (result: BinaryWorkerResult) => self.postMessage(result, { transfer: [] });
20const quote = (name: string) => `"${name.replaceAll('"', '""')}"`;
21
22async function gunzip(bytes: ArrayBuffer) {
23 const reader = new Blob([bytes])
24 .stream()
25 .pipeThrough(new DecompressionStream("gzip"))
26 .getReader();
27 const chunks: Uint8Array<ArrayBuffer>[] = [];
28 let length = 0;
29 try {
30 while (true) {
31 const { value, done } = await reader.read();
32 if (done) break;
33 length += value.length;
34 if (length > BINARY_PREVIEW_MAX_BYTES || length > bytes.byteLength * ARCHIVE_MAX_RATIO)
35 throw new Error(
36 "The expanded archive exceeds the preview size or compression ratio limit.",
37 );
38 chunks.push(value);
39 }
40 } finally {
41 await reader.cancel();
42 }
43 return new Uint8Array(await new Blob(chunks).arrayBuffer());
44}
45
46function tarEntries(bytes: Uint8Array<ArrayBuffer>) {
47 const entries: ArchiveEntry[] = [];
48 const text = (offset: number, length: number) =>
49 new TextDecoder().decode(bytes.subarray(offset, offset + length)).split("\0", 1)[0] ?? "";
50 const number = (offset: number, length: number) => {
51 const value = text(offset, length).trim();
52 if (!/^[0-7]*$/.test(value)) throw new Error("This TAR uses an unsupported numeric format.");
53 return Number.parseInt(value || "0", 8);
54 };
55 let longPath: string | null = null;
56 let longLink: string | null = null;
57 const globalPax: Record<string, string> = {};
58 let localPax: Record<string, string> = {};
59 let offset = 0;
60 while (offset + 512 <= bytes.length) {
61 if (bytes.subarray(offset, offset + 512).every((byte) => byte === 0)) break;
62 if (entries.length >= ARCHIVE_MAX_ENTRIES)
63 throw new Error("This archive contains more than 2,048 entries.");
64 let checksum = 0;
65 for (let index = 0; index < 512; index++)
66 checksum += index >= 148 && index < 156 ? 32 : bytes[offset + index]!;
67 if (checksum !== number(offset + 148, 8)) throw new Error("The TAR header is 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 Error("An archive entry is too large or 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 Error("An archive entry is too large or 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 Error("Archive metadata is 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 Error("The TAR metadata is 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 Error("The TAR metadata is damaged.");
114 const record = new TextDecoder().decode(content.subarray(separator + 1, end - 1));
115 const equals = record.indexOf("=");
116 if (equals < 1) throw new Error("The TAR metadata is 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 Error("The TAR metadata is 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 Error("This archive has no readable entries.");
155 return entries;
156}
157
158async function handle(input: BinaryWorkerRequest) {
159 if (input.type === "entry") {
160 const bytes = files.get(input.path);
161 if (bytes === undefined) throw new Error("This archive entry is unavailable.");
162 if (typeof bytes === "number" || bytes.length > ARCHIVE_ENTRY_MAX_BYTES)
163 throw new Error("Entry previews are limited to 10 MB.");
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 Error("This table is 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 Error("Preview limited to files under 50 MB.");
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 Error("Database previews are limited to 100 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
259self.addEventListener("message", (event: MessageEvent<BinaryWorkerRequest>) => {
260 void handle(event.data).catch((cause: unknown) =>
261 reply({
262 type: "error",
263 message: cause instanceof Error ? cause.message : "Could not preview this file.",
264 }),
265 );
266});