@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 1,420 lines 56 KB
Raw
1 import JSZip from "jszip";
2 import { checkZipDirectory } from "@t3tools/client-runtime/zip/directory";
3 import { SaxesParser } from "saxes";
4 import { externalUrl } from "./office/links";
5
6 import {
7 OFFICE_PREVIEW_MAX_BYTES,
8 OFFICE_PREVIEW_MAX_COLUMNS,
9 OFFICE_PREVIEW_MAX_ROWS,
10 type OfficeWorkerRequest,
11 } from "./officePreviewProtocol";
12 import { PreviewError } from "./previewFailure";
13
14 // Word limits, measured against the @betteroffice/docx engine, which lays out in its own worker.
15 export const WORD_LIMITS = {
16 /** document.xml read and kept, about 700 pages of prose; the engine opens it in about 1 s. */
17 bodyBytes: 4 * 1024 * 1024,
18 bodyElements: 50_000,
19 bodyTags: 1_000_000,
20 /** A table is laid out in one go: 100,000 tags is a 4,000-row table, about 2 s. */
21 elementTags: 100_000,
22 /** Line breaking grows with the square of a paragraph's text: 40K characters take about 2 s. */
23 paragraphText: 128 * 1024,
24 styles: 8_192,
25 styleBytes: 16 * 1024 * 1024,
26 styleUnits: 400_000,
27 styleVisits: 131_072,
28 stylePropertyVisits: 2_000_000,
29 numberingReferences: 20_000,
30 numberingBytes: 32 * 1024 * 1024,
31 numberingMarkup: 400_000,
32 auxiliaryPartBytes: 8 * 1024 * 1024,
33 auxiliaryMarkup: 1_000_000,
34 /** Body, styles, lists and notes, with headers and footers counted once per page. */
35 expandedBytes: 64 * 1024 * 1024,
36 expandedMarkup: 4_000_000,
37 };
38
39 function xmlParser() {
40 const parser = new SaxesParser({ xmlns: true });
41 parser.on("doctype", () => {
42 throw new PreviewError({ code: "office-xml-declarations" });
43 });
44 return parser;
45 }
46
47 function officeXmlDecoder(bytes: Uint8Array) {
48 if ((bytes[0] === 0xff && bytes[1] === 0xfe) || (bytes[0] === 0x3c && bytes[1] === 0))
49 return new TextDecoder("utf-16le");
50 if ((bytes[0] === 0xfe && bytes[1] === 0xff) || (bytes[0] === 0 && bytes[1] === 0x3c))
51 return new TextDecoder("utf-16be");
52 const declaration = new TextDecoder().decode(bytes.subarray(0, 256));
53 return new TextDecoder(/^<\?xml\s[^?]*\bencoding\s*=\s*["']([^"']+)["']/i.exec(declaration)?.[1]);
54 }
55
56 function officeXml(bytes: Uint8Array) {
57 // Repacked XML is UTF-8, regardless of the source part's encoding.
58 return officeXmlDecoder(bytes)
59 .decode(bytes)
60 .replace(/^(<\?xml\s[^?]*\bencoding\s*=\s*)["'][^"']+["']/i, '$1"UTF-8"');
61 }
62
63 // Section properties terminate the section they describe, so the kept prefix
64 // may depend on a paragraph or body section beyond the retained input window.
65 function wordSections() {
66 const parser = xmlParser();
67 const names: string[] = [];
68 const namespaceScopes: Record<string, string>[] = [];
69 const sections: { end: number; xml: string; body: boolean }[] = [];
70 let window = "";
71 let offset = 0;
72 let capture: {
73 start: number;
74 insert: number;
75 namespaces: string;
76 body: boolean;
77 depth: number;
78 } | null = null;
79 let bytes = 0;
80 let tags = 0;
81 let nextParagraph = false;
82 let paragraphSection = false;
83 const escape = (value: string) =>
84 value.replaceAll("&", "&amp;").replaceAll('"', "&quot;").replaceAll("<", "&lt;");
85 parser.on("opentag", (tag) => {
86 names.push(tag.local);
87 namespaceScopes.push(tag.ns);
88 if (names.length > 64) throw new PreviewError({ code: "word-xml-nesting" });
89 const body = names.length === 3 && names[1] === "body";
90 if (
91 tag.local === "sectPr" &&
92 tag.uri === "http://schemas.openxmlformats.org/wordprocessingml/2006/main" &&
93 (body || (names.length === 5 && names[2] === "p" && names[3] === "pPr" && !nextParagraph))
94 ) {
95 let namespaces = "";
96 const scope = Object.assign({}, ...namespaceScopes) as Record<string, string>;
97 for (const prefix in scope) {
98 const name = prefix ? `xmlns:${prefix}` : "xmlns";
99 if (prefix !== "xml" && !tag.attributes[name])
100 namespaces += ` ${name}="${escape(scope[prefix]!)}"`;
101 }
102 const start = window.lastIndexOf("<", parser.position - offset - 1);
103 if (start < 0) throw new PreviewError({ code: "word-section-markup" });
104 capture = {
105 start: start + offset,
106 insert: parser.position - (tag.isSelfClosing ? 2 : 1),
107 namespaces,
108 body,
109 depth: names.length,
110 };
111 }
112 if (capture && ++tags > 2_000) throw new PreviewError({ code: "word-limited-section-markup" });
113 });
114 parser.on("closetag", (tag) => {
115 if (capture && tag.local === "sectPr" && names.length === capture.depth) {
116 const xml =
117 window.slice(capture.start - offset, capture.insert - offset) +
118 capture.namespaces +
119 window.slice(capture.insert - offset, parser.position - offset);
120 bytes += xml.length;
121 if (bytes > 128 * 1024) throw new PreviewError({ code: "word-limited-section-markup" });
122 sections.push({ end: parser.position, xml, body: capture.body });
123 paragraphSection = !capture.body;
124 if (!capture.body && parser.position > 4 * 1024 * 1024) nextParagraph = true;
125 capture = null;
126 }
127 if (names.length === 3 && tag.local === "p" && paragraphSection) {
128 sections[sections.length - 1]!.end = parser.position;
129 paragraphSection = false;
130 }
131 names.pop();
132 namespaceScopes.pop();
133 });
134 return {
135 write(chunk: string) {
136 window += chunk;
137 parser.write(chunk);
138 if (capture && parser.position - capture.start > 128 * 1024)
139 throw new PreviewError({ code: "word-limited-section-markup" });
140 const keep = capture?.start ?? Math.max(offset, parser.position - 128 * 1024);
141 window = window.slice(keep - offset);
142 offset = keep;
143 },
144 finish() {
145 parser.close();
146 return sections;
147 },
148 };
149 }
150
151 // Keep whole body elements so every prefix and render batch is valid Word XML.
152 // Individual tables/paragraphs are bounded too: one giant element must not
153 // bypass the budget between yields on the main thread.
154 function wordDocuments(
155 xml: string,
156 incomplete: boolean,
157 sections: ReturnType<ReturnType<typeof wordSections>["finish"]>,
158 ) {
159 const parser = xmlParser();
160 const names: string[] = [];
161 const batches: { content: string; end: number }[] = [];
162 let prefix = "";
163 let suffix = "";
164 let boundary = 0;
165 let batchStart = 0;
166 let tags = 0;
167 let elementTags = 0;
168 let batchTags = 0;
169 let elements = 0;
170 // The text each open paragraph lays out (its w:t runs); a nested paragraph counts apart.
171 const paragraphText: number[] = [];
172 let inText = false;
173 let truncated = incomplete;
174 const limit = Symbol("Word preview limit");
175 const countText = (text: string) => {
176 if (!inText || paragraphText.length === 0) return;
177 const length = paragraphText.at(-1)! + text.length;
178 if (length > WORD_LIMITS.paragraphText) throw limit;
179 paragraphText[paragraphText.length - 1] = length;
180 };
181 parser.on("text", countText);
182 parser.on("cdata", countText);
183 parser.on("opentag", (tag) => {
184 names.push(tag.name);
185 if (prefix && tag.local === "p") paragraphText.push(0);
186 if (tag.local === "t") inText = true;
187 if (names.length === 2 && tag.local === "body") {
188 prefix = xml.slice(0, parser.position).replace(/\/>$/, ">");
189 suffix = `</${tag.name}></${names[0]}>`;
190 boundary = batchStart = parser.position;
191 } else if (prefix && names.length > 2) {
192 if (names.length === 3 && ++elements > WORD_LIMITS.bodyElements) throw limit;
193 tags++;
194 elementTags++;
195 batchTags++;
196 if (tags > WORD_LIMITS.bodyTags || elementTags > WORD_LIMITS.elementTags) throw limit;
197 }
198 if (names.length > 64) throw limit;
199 });
200 parser.on("closetag", (tag) => {
201 if (prefix && tag.local === "p") paragraphText.pop();
202 if (tag.local === "t") inText = false;
203 if (prefix && names.length === 3 && tag.local !== "sectPr") {
204 if (parser.position - prefix.length > WORD_LIMITS.bodyBytes) throw limit;
205 boundary = parser.position;
206 elementTags = 0;
207 if (batchTags >= 500) {
208 batches.push({ content: xml.slice(batchStart, boundary), end: boundary });
209 batchStart = boundary;
210 batchTags = 0;
211 }
212 }
213 names.pop();
214 });
215 try {
216 for (let offset = 0; offset < xml.length; offset += 16 * 1024) {
217 parser.write(xml.slice(offset, offset + 16 * 1024));
218 }
219 if (!incomplete) parser.close();
220 } catch (cause) {
221 if (cause !== limit) throw cause;
222 truncated = true;
223 }
224 if (!prefix) throw new PreviewError({ code: "word-no-body" });
225 if (boundary > batchStart || batches.length === 0)
226 batches.push({ content: xml.slice(batchStart, boundary), end: boundary });
227 const documents: string[] = [];
228 let length = 0;
229 let markup = 0;
230 for (const batch of batches) {
231 const section = sections.find((section) => section.body || section.end >= batch.end);
232 const document = prefix + batch.content + (section?.xml ?? "") + suffix;
233 length += batch.content.length;
234 markup += batch.content.match(/<(?![!?/])/g)?.length ?? 0;
235 if (length > WORD_LIMITS.bodyBytes || markup > WORD_LIMITS.bodyTags) {
236 truncated = true;
237 break;
238 }
239 documents.push(document);
240 }
241 if (documents.length === 0) throw new PreviewError({ code: "word-section-markup" });
242 return { documents, truncated };
243 }
244
245 function boundedWorksheet(
246 xml: string,
247 strings: readonly string[],
248 budget: { remaining: number; cells: number },
249 ) {
250 const parser = xmlParser();
251 const rows = new Map<number, Map<number, string>>();
252 const rowTags = new Map<number, string>();
253 const metadataAttributes = new Map<
254 string,
255 {
256 ref: string | undefined;
257 min: string | undefined;
258 max: string | undefined;
259 hidden: string | undefined;
260 }
261 >();
262 let dataStart = 0;
263 let dataTag = 0;
264 let dataPrefix = "";
265 let dataNamespace = "";
266 let dataEnd = 0;
267 let defaultWidth: number | undefined;
268 let gridlines: boolean | undefined;
269 const sizedColumns: { min: number; max: number }[] = [];
270 let emptyData = false;
271 let depth = 0;
272 let row = 0;
273 let explicitRow = false;
274 let rowTag = "";
275 let column = 0;
276 let maxRow = 0;
277 let maxColumn = 0;
278 let cells = new Map<number, string>();
279 let cell: {
280 start: number;
281 opening: string;
282 name: string;
283 prefix: string;
284 selfClosing: boolean;
285 text: string;
286 value: string;
287 reading: string;
288 shared: boolean;
289 } | null = null;
290 let truncated = false;
291 const coordinate = (ref: string) => {
292 const match = /^([A-Z]{1,3})([1-9]\d{0,6})$/.exec(ref);
293 if (!match) throw new PreviewError({ code: "workbook-cell-coordinates" });
294 let col = 0;
295 for (const letter of match[1]!) col = col * 26 + letter.charCodeAt(0) - 64;
296 const line = Number(match[2]);
297 if (col > 16384 || line > 1048576) throw new PreviewError({ code: "workbook-dimensions" });
298 return { col, line };
299 };
300 parser.on("opentag", (tag) => {
301 depth++;
302 if (depth > 64) throw new PreviewError({ code: "workbook-xml-nesting" });
303 if (
304 (tag.local === "row" && depth !== 3) ||
305 (tag.local === "c" && depth !== 4) ||
306 ((tag.local === "dimension" || tag.local === "sheetData") && depth !== 2)
307 )
308 throw new PreviewError({ code: "workbook-structure" });
309 const attribute = (name: string) => tag.attributes[name]?.value;
310 if (["dimension", "mergeCell", "col"].includes(tag.local))
311 metadataAttributes.set(
312 xml.slice(xml.lastIndexOf("<", parser.position - 1), parser.position),
313 {
314 ref: attribute("ref"),
315 min: attribute("min"),
316 max: attribute("max"),
317 hidden: attribute("hidden"),
318 },
319 );
320 // The engine draws gridlines on every sheet, so the first view's choice travels beside the bytes.
321 if (depth === 3 && tag.local === "sheetView" && gridlines === undefined) {
322 const shown = attribute("showGridLines");
323 gridlines = shown !== "0" && shown !== "false";
324 }
325 if (depth === 2 && tag.local === "sheetFormatPr") {
326 const width = Number(attribute("defaultColWidth") ?? Number.NaN);
327 if (width >= 0 && width <= 255) defaultWidth = width;
328 }
329 if (depth === 3 && tag.local === "col") {
330 const hidden = attribute("hidden");
331 const min = Number(attribute("min") ?? 1);
332 const max = Number(attribute("max") ?? min);
333 if (
334 (attribute("width") !== undefined || hidden === "1" || hidden === "true") &&
335 Number.isInteger(min) &&
336 Number.isInteger(max)
337 )
338 sizedColumns.push({ min, max });
339 }
340 if (depth === 2 && tag.local === "sheetData") {
341 dataStart = parser.position;
342 dataTag = xml.lastIndexOf("<", parser.position - 1);
343 dataPrefix = tag.prefix ? `${tag.prefix}:` : "";
344 dataNamespace = tag.uri
345 .replaceAll("&", "&amp;")
346 .replaceAll('"', "&quot;")
347 .replaceAll("<", "&lt;");
348 emptyData = tag.isSelfClosing;
349 }
350 // A dimension past the limits loses nothing by itself; the cells past them say whether content is lost.
351 if (depth === 2 && tag.local === "dimension") {
352 for (const ref of (attribute("ref") ?? "").split(":")) coordinate(ref);
353 } else if (depth === 3 && tag.local === "row") {
354 explicitRow = attribute("r") !== undefined;
355 row = attribute("r") === undefined ? row + 1 : Number(attribute("r"));
356 if (!Number.isInteger(row) || row < 1 || row > 1048576)
357 throw new PreviewError({ code: "workbook-row-coordinates" });
358 rowTag = xml
359 .slice(xml.lastIndexOf("<", parser.position - 1), parser.position)
360 .replace(/\/>$/, ">");
361 column = 0;
362 cells = rows.get(row) ?? new Map<number, string>();
363 if (row > OFFICE_PREVIEW_MAX_ROWS) throw new PreviewError({ code: "workbook-too-many-rows" });
364 } else if (depth === 4 && tag.local === "c") {
365 const ref = attribute("r");
366 const position = ref ? coordinate(ref) : { col: column + 1, line: row };
367 if (!explicitRow && column === 0) row = position.line;
368 column = position.col;
369 if (row < 1 || position.line !== row)
370 throw new PreviewError({ code: "workbook-inconsistent-rows" });
371 if (row > OFFICE_PREVIEW_MAX_ROWS || column > OFFICE_PREVIEW_MAX_COLUMNS) {
372 truncated = true;
373 return;
374 }
375 maxRow = Math.max(maxRow, row);
376 maxColumn = Math.max(maxColumn, column);
377 if (++budget.cells > 2_000_000 || maxRow * maxColumn > 8_000_000)
378 throw new PreviewError({ code: "workbook-too-many-cells" });
379 // Explicit coordinates also bound sheets whose rows/cells omit r.
380 let letters = "";
381 for (let value = column; value > 0; value = Math.floor((value - 1) / 26))
382 letters = String.fromCharCode(65 + ((value - 1) % 26)) + letters;
383 const style = attribute("s");
384 const type = attribute("t");
385 if ((style && !/^\d{1,6}$/.test(style)) || (type && !/^[a-zA-Z]+$/.test(type)))
386 throw new PreviewError({ code: "workbook-cell-metadata" });
387 cell = {
388 start: parser.position,
389 opening: xml
390 .slice(xml.lastIndexOf("<", parser.position - 1), parser.position)
391 .replace(/\/?>$/, `${ref ? "" : ` r="${letters}${row}"`}>`),
392 name: tag.name,
393 prefix: tag.prefix ? `${tag.prefix}:` : "",
394 selfClosing: tag.isSelfClosing,
395 text: "",
396 value: "",
397 reading: "",
398 shared: type === "s",
399 };
400 }
401 if (cell && (tag.local === "t" || tag.local === "v")) cell.reading = tag.local;
402 });
403 const readCellText = (chunk: string) => {
404 if (cell?.reading === "t") cell.text += chunk;
405 if (cell?.reading === "v") cell.value += chunk;
406 };
407 parser.on("text", readCellText);
408 parser.on("cdata", readCellText);
409 parser.on("closetag", (tag) => {
410 if (depth === 4 && tag.local === "c" && cell) {
411 const end = cell.selfClosing ? cell.start : xml.lastIndexOf("<", parser.position - 1);
412 const raw = cell.opening + xml.slice(cell.start, end) + `</${cell.name}>`;
413 const shown = cell.shared ? (strings[Number(cell.value)] ?? "") : cell.text || cell.value;
414 const kept = shown.slice(0, Math.min(4096, budget.remaining));
415 budget.remaining -= kept.length;
416 if (kept.length < shown.length) {
417 truncated = true;
418 const escape = (s: string) =>
419 s.replaceAll("&", "&amp;").replaceAll("<", "&lt;").replaceAll(">", "&gt;");
420 cells.set(
421 column,
422 cell.opening
423 .replace(/\s+t\s*=\s*(?:"[^"]*"|'[^']*')/, "")
424 .replace(/>$/, ' t="inlineStr">') +
425 `<${cell.prefix}is><${cell.prefix}t>` +
426 escape(kept) +
427 `</${cell.prefix}t></${cell.prefix}is></${cell.name}>`,
428 );
429 } else cells.set(column, raw);
430 cell = null;
431 } else if (depth === 3 && tag.local === "row" && row <= OFFICE_PREVIEW_MAX_ROWS) {
432 rows.set(row, cells);
433 rowTags.set(row, rowTag);
434 }
435 if (depth === 2 && tag.local === "sheetData")
436 dataEnd = emptyData ? parser.position : xml.lastIndexOf("<", parser.position - 1);
437 if (cell?.reading === tag.local) cell.reading = "";
438 depth--;
439 });
440 parser.write(xml).close();
441 if (!dataStart) throw new PreviewError({ code: "workbook-no-data" });
442 const boundRange = (ref: string) => {
443 const [a, b = a] = ref.split(":");
444 const first = coordinate(a!);
445 const last = coordinate(b!);
446 if (first.col > OFFICE_PREVIEW_MAX_COLUMNS || first.line > OFFICE_PREVIEW_MAX_ROWS) return "";
447 let letters = "";
448 for (
449 let col = Math.min(OFFICE_PREVIEW_MAX_COLUMNS, last.col);
450 col > 0;
451 col = Math.floor((col - 1) / 26)
452 )
453 letters = String.fromCharCode(65 + ((col - 1) % 26)) + letters;
454 return a + ":" + letters + Math.min(OFFICE_PREVIEW_MAX_ROWS, last.line);
455 };
456 const metadata = (part: string) =>
457 part.replace(
458 /(<((?:\w+:)?(?:dimension|mergeCell|col))\b(?:[^"'<>]|"[^"]*"|'[^']*')*>)(?:\s*<\/\2\s*>)?/g,
459 (tag: string, opening: string) => {
460 const attributes = metadataAttributes.get(opening);
461 if (!attributes) return tag;
462 const replace = (name: string, value: string) => {
463 const pattern = new RegExp(`\\s${name}\\s*=\\s*(?:"[^"]*"|'[^']*')`);
464 tag = pattern.test(tag)
465 ? tag.replace(pattern, ` ${name}="${value}"`)
466 : tag.replace(/\/?>/, (end) => ` ${name}="${value}"${end}`);
467 };
468 if (attributes.ref !== undefined) {
469 const bounded = boundRange(attributes.ref);
470 if (!bounded) return "";
471 replace("ref", bounded);
472 }
473 if (/<(?:\w+:)?col\b/.test(opening)) {
474 const min = Math.max(1, Number(attributes.min ?? 1));
475 // Hidden columns keep their full span, or the columns past the limit would show again.
476 const hidden = attributes.hidden === "1" || attributes.hidden === "true";
477 const max = Math.min(
478 hidden ? 16384 : OFFICE_PREVIEW_MAX_COLUMNS,
479 Number(attributes.max ?? 16384),
480 );
481 if (!Number.isInteger(min) || !Number.isInteger(max) || min > max) return "";
482 replace("min", String(min));
483 replace("max", String(max));
484 }
485 return tag;
486 },
487 );
488 // The engine sizes columns without a width at Excel's 8.43 characters and ignores `defaultColWidth`, so the
489 // columns it covers get that width spelled out. A second `<cols>` is valid: the schema allows any number.
490 let defaultColumns = "";
491 if (defaultWidth !== undefined) {
492 let next = 1;
493 for (const span of sizedColumns.toSorted((a, b) => a.min - b.min)) {
494 if (span.min > next)
495 defaultColumns += `<${dataPrefix}col min="${next}" max="${Math.min(span.min - 1, OFFICE_PREVIEW_MAX_COLUMNS)}" width="${defaultWidth}"/>`;
496 next = Math.max(next, span.max + 1);
497 if (next > OFFICE_PREVIEW_MAX_COLUMNS) break;
498 }
499 if (next <= OFFICE_PREVIEW_MAX_COLUMNS)
500 defaultColumns += `<${dataPrefix}col min="${next}" max="${OFFICE_PREVIEW_MAX_COLUMNS}" width="${defaultWidth}"/>`;
501 if (defaultColumns)
502 defaultColumns = `<${dataPrefix}cols xmlns${dataPrefix ? `:${dataPrefix.slice(0, -1)}` : ""}="${dataNamespace}">${defaultColumns}</${dataPrefix}cols>`;
503 }
504 const head = (part: string) =>
505 metadata(part.slice(0, dataTag) + defaultColumns + part.slice(dataTag));
506 return {
507 xml: emptyData
508 ? head(xml)
509 : `${head(xml.slice(0, dataStart))}${[...rows]
510 .sort(([a], [b]) => a - b)
511 .map(
512 ([line, values]) =>
513 `${rowTags.get(line) ?? `<row r="${line}">`}${[...values]
514 .sort(([a], [b]) => a - b)
515 .map(([, value]) => value)
516 .join("")}</${/^<([^\s>]+)/.exec(rowTags.get(line) ?? "<row>")![1]}>`,
517 )
518 .join("")}${metadata(xml.slice(dataEnd))}`,
519 truncated,
520 gridlines: gridlines ?? true,
521 };
522 }
523
524 /**
525 * A stylesheet as Excel reads it, in the terms the engine understands. The engine takes a number format id under 164 as
526 * Excel's built-in one, though workbooks define their own there (Excel writes its locale formats 5 to 8 that way), so
527 * those move to free ids. It also reads `<u val="…">` as a boolean, which loses every named underline but the bare one.
528 */
529 function engineStyles(xml: string) {
530 const name = (local: string) => `<((?:[\\w.-]+:)?)${local}\\b`;
531 const blocks = new Map<string, { start: number; end: number }>();
532 const parser = xmlParser();
533 let depth = 0;
534 parser.on("opentag", (tag) => {
535 if (++depth > 64) throw new PreviewError({ code: "workbook-xml-nesting" });
536 if (depth !== 2 || !["numFmts", "cellStyleXfs", "cellXfs"].includes(tag.local)) return;
537 if (blocks.has(tag.local)) throw new PreviewError({ code: "workbook-duplicate-styles" });
538 blocks.set(tag.local, {
539 start: xml.lastIndexOf("<", parser.position - 1),
540 end: parser.position,
541 });
542 });
543 parser.on("closetag", (tag) => {
544 if (depth === 2) {
545 const block = blocks.get(tag.local);
546 if (block) block.end = parser.position;
547 }
548 depth--;
549 });
550 parser.write(xml).close();
551 const id = /(numFmtId\s*=\s*)(["'])(\d+)\2/g;
552 const moved = new Map<number, number>();
553 const formatBlock = blocks.get("numFmts");
554 const formats = formatBlock ? xml.slice(formatBlock.start, formatBlock.end) : "";
555 const used = new Set([...formats.matchAll(id)].map((match) => Number(match[3])));
556 let next = 164;
557 const renumber = (part: string, assign: boolean) =>
558 part.replace(id, (attribute: string, key: string, quote: string, value: string) => {
559 const number = Number(value);
560 if (assign && number < 164 && !moved.has(number)) {
561 while (used.has(next)) next++;
562 if (next > 65535) throw new PreviewError({ code: "workbook-number-formats" });
563 moved.set(number, next++);
564 }
565 const to = moved.get(number);
566 return to === undefined ? attribute : `${key}${quote}${to}${quote}`;
567 });
568 const numberedFormats = renumber(formats, true);
569 const parts: string[] = [];
570 let offset = 0;
571 for (const [local, block] of blocks) {
572 parts.push(
573 xml.slice(offset, block.start),
574 local === "numFmts" ? numberedFormats : renumber(xml.slice(block.start, block.end), false),
575 );
576 offset = block.end;
577 }
578 parts.push(xml.slice(offset));
579 return parts
580 .join("")
581 .replace(
582 new RegExp(
583 `${name("u")}\\s+val\\s*=\\s*(["'])(?:single|double|singleAccounting|doubleAccounting)\\2\\s*(?:/>|>\\s*</\\1u\\s*>)`,
584 "g",
585 ),
586 "<$1u/>",
587 );
588 }
589
590 async function boundWorkbook(archive: JSZip) {
591 const paths = new Map<string, string>();
592 // Chart sheets and other non-grid sheets hold no cells to bound.
593 const otherSheets = new Set<string>();
594 let sharedPath = "xl/sharedStrings.xml";
595 let stylesPath = "xl/styles.xml";
596 let themePath = "xl/theme/theme1.xml";
597 const relationships = xmlParser();
598 relationships.on("opentag", (tag) => {
599 if (tag.local !== "Relationship") return;
600 const type = tag.attributes.Type?.value;
601 const path = new URL(
602 tag.attributes.Target?.value ?? "",
603 "https://docx.invalid/xl/workbook.xml",
604 ).pathname.slice(1);
605 if (type?.endsWith("/worksheet")) paths.set(tag.attributes.Id?.value ?? "", path);
606 else if (/\/(?:chartsheet|dialogsheet|xlMacrosheet|xlIntlMacrosheet)$/.test(type ?? ""))
607 otherSheets.add(tag.attributes.Id?.value ?? "");
608 if (type?.endsWith("/sharedStrings")) sharedPath = path;
609 if (type?.endsWith("/styles")) stylesPath = path;
610 if (type?.endsWith("/theme")) themePath = path;
611 });
612 relationships
613 .write((await archive.file("xl/_rels/workbook.xml.rels")?.async("string")) ?? "")
614 .close();
615 const sheetPaths: string[] = [];
616 // Every sheet in workbook order, as the engine indexes them; chart sheets have no worksheet path.
617 const sheetOrder: (string | undefined)[] = [];
618 const workbook = xmlParser();
619 workbook.on("opentag", (tag) => {
620 if (tag.local !== "sheet") return;
621 const id = Object.values(tag.attributes).find((attribute) => attribute.local === "id")?.value;
622 if (otherSheets.has(id ?? "")) {
623 sheetOrder.push(undefined);
624 return;
625 }
626 const path = paths.get(id ?? "");
627 if (!path || sheetPaths.length === 100)
628 throw new PreviewError({ code: "workbook-sheet-count" });
629 sheetPaths.push(path);
630 sheetOrder.push(path);
631 });
632 workbook.write((await archive.file("xl/workbook.xml")?.async("string")) ?? "").close();
633 const theme = (await archive.file(themePath)?.async("string")) ?? "";
634 const typeface = (font: string, fallback: string) =>
635 new RegExp(
636 `<(?:[\\w.-]+:)?${font}\\b[^>]*>\\s*<(?:[\\w.-]+:)?latin\\b[^>]*\\btypeface\\s*=\\s*["']([^"']+)["']`,
637 ).exec(theme)?.[1] ?? fallback;
638 const themeFonts = {
639 major: typeface("majorFont", "Calibri Light"),
640 minor: typeface("minorFont", "Calibri"),
641 };
642 const styles = await archive.file(stylesPath)?.async("string");
643 if (styles) archive.file(stylesPath, engineStyles(styles));
644 const strings: string[] = [];
645 const shared = await archive.file(sharedPath)?.async("string");
646 if (shared) {
647 const parser = xmlParser();
648 let text = "";
649 let reading = false;
650 parser.on("opentag", (tag) => {
651 if (tag.local === "si") text = "";
652 if (tag.local === "t") reading = true;
653 });
654 const readSharedText = (chunk: string) => {
655 if (reading) text += chunk;
656 };
657 parser.on("text", readSharedText);
658 parser.on("cdata", readSharedText);
659 parser.on("closetag", (tag) => {
660 if (tag.local === "t") reading = false;
661 if (tag.local === "si") strings.push(text);
662 });
663 parser.write(shared).close();
664 }
665 const budget = { remaining: 1024 * 1024, cells: 0 };
666 const bounded = new Map<string, { truncated: boolean; gridlines: boolean }>();
667 for (const path of new Set(sheetPaths)) {
668 const file = archive.file(path);
669 if (!file) throw new PreviewError({ code: "workbook-missing-sheet" });
670 const sheet = boundedWorksheet(await file.async("string"), strings, budget);
671 archive.file(path, sheet.xml);
672 bounded.set(path, sheet);
673 }
674 return {
675 sheets: sheetOrder.map(
676 (path) => (path && bounded.get(path)) || { truncated: false, gridlines: true },
677 ),
678 themeFonts,
679 };
680 }
681
682 const WORD_RELATIONSHIP = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/";
683 const WORD_EXPANDED_PARTS = new Set(["footnotes", "endnotes", "comments", "header", "footer"]);
684 const WORD_DISABLED_PARTS = new Set(["commentsExtended", "fontTable", "aFChunk"]);
685 const WORD_BINARY_PARTS = new Set([
686 "image",
687 "font",
688 "thumbnail",
689 "audio",
690 "video",
691 "oleObject",
692 "package",
693 "printerSettings",
694 "vbaProject",
695 "origin",
696 "certificate",
697 // Package metadata can point to binary payloads; explicit XML still wins below.
698 "core-properties",
699 "extended-properties",
700 "custom-properties",
701 "signature",
702 ]);
703
704 function wordPartTypes() {
705 const defaults = new Map<string, string>();
706 const overrides = new Map<string, string>();
707 const xmlParts = new Set([
708 "word/document.xml",
709 "docProps/app.xml",
710 "docProps/core.xml",
711 "docProps/custom.xml",
712 ]);
713 const roles = new Map<string, Set<string>>();
714 const relationships = new Map<string, Map<string, string>>();
715 return {
716 roles,
717 relationships,
718 binary(path: string) {
719 const extension = path.split(".").at(-1)?.toLowerCase() ?? "";
720 const type = overrides.get(path) ?? defaults.get(extension) ?? "";
721 // An XML relationship always wins over a misleading media content type.
722 if (
723 path === "[Content_Types].xml" ||
724 extension === "rels" ||
725 extension === "xml" ||
726 xmlParts.has(path) ||
727 /(?:\+xml|\/xml)$/i.test(type)
728 )
729 return false;
730 return (
731 [...(roles.get(path) ?? [])].some((role) => WORD_BINARY_PARTS.has(role)) ||
732 /^(?:image|audio|video|font)\//i.test(type) ||
733 /^(?:png|jpe?g|gif|webp|bmp|tiff?|ico|bin|sigs|cer|ttf|otf|odttf|woff2?)$/.test(
734 extension,
735 ) ||
736 type === "application/octet-stream" ||
737 type === "application/vnd.openxmlformats-package.digital-signature-origin"
738 );
739 },
740 metadata(path: string, xml: string) {
741 const parser = xmlParser();
742 const removed: { start: number; end: number }[] = [];
743 let start = -1;
744 parser.on("opentag", (tag) => {
745 const attr = (name: string) => tag.attributes[name]?.value ?? "";
746 if (path === "[Content_Types].xml") {
747 if (tag.local === "Default")
748 defaults.set(attr("Extension").toLowerCase(), attr("ContentType"));
749 if (tag.local === "Override")
750 overrides.set(attr("PartName").replace(/^\//, ""), attr("ContentType"));
751 }
752 if (tag.local !== "Relationship" || !path.endsWith(".rels")) return;
753 const type = attr("Type");
754 const role = type.slice(type.lastIndexOf("/") + 1);
755 const external =
756 attr("TargetMode").toLowerCase() === "external" ||
757 /^(?:[a-z][a-z\d+.-]*:|\/\/)/i.test(attr("Target"));
758 const folder = path === "_rels/.rels" ? "" : path.replace(/_rels\/[^/]+\.rels$/, "");
759 // Preserve archive URL resolution, including literal query/fragment
760 // characters in ZIP names rather than silently classifying another file.
761 const target = new URL(attr("Target"), `https://docx.invalid/${folder}`)
762 .toString()
763 .slice("https://docx.invalid/".length);
764 if (!external) {
765 if (
766 !WORD_BINARY_PARTS.has(role) &&
767 role !== "hyperlink" &&
768 (type.startsWith(WORD_RELATIONSHIP) ||
769 role === "core-properties" ||
770 role === "signature")
771 )
772 xmlParts.add(target);
773 const types = roles.get(target) ?? new Set<string>();
774 types.add(role);
775 roles.set(target, types);
776 const owner = path === "_rels/.rels" ? "" : path.replace(/_rels\/([^/]+)\.rels$/, "$1");
777 const refs = relationships.get(owner) ?? new Map<string, string>();
778 if (!refs.has(attr("Id"))) refs.set(attr("Id"), target);
779 relationships.set(owner, refs);
780 if (target === "word/document.xml" && role !== "officeDocument")
781 throw new PreviewError({ code: "word-limited-aliased-part" });
782 if (type === WORD_RELATIONSHIP + "officeDocument" && target !== "word/document.xml")
783 throw new PreviewError({ code: "word-limited-relationship" });
784 }
785 if (
786 (external && !(role === "hyperlink" && externalUrl(attr("Target")))) ||
787 WORD_DISABLED_PARTS.has(role)
788 ) {
789 start = xml.lastIndexOf("<", parser.position - 1);
790 }
791 });
792 parser.on("closetag", (tag) => {
793 if (tag.local === "Relationship" && start >= 0) {
794 removed.push({ start, end: parser.position });
795 start = -1;
796 }
797 });
798 parser.write(xml).close();
799 for (const range of removed.toReversed())
800 xml = xml.slice(0, range.start) + xml.slice(range.end);
801 return xml;
802 },
803 };
804 }
805
806 // Bound inherited style and numbering expansion before the engine loads it.
807 // Picture bullets can duplicate embedded images, so `pictures` locates those definitions to omit.
808 function wordXmlCost(xml: string, roles: Set<string> | undefined) {
809 const parser = xmlParser();
810 let depth = 0;
811 let markup = 0;
812 const limit = Symbol("Word style budget");
813 const exceeded = {
814 bytes: Infinity,
815 markup: Infinity,
816 pictures: [] as { start: number; end: number }[],
817 };
818 let styleCount = 0;
819 let ownUnits = 0;
820 const styles = new Map<
821 string,
822 { properties: Map<string, number>; base: string; linked: string }
823 >();
824 let style: { id: string; base: string; linked: string; properties: Map<string, number> } | null =
825 null;
826 const names: string[] = [];
827 const definitions = new Map<string, { bytes: number; markup: number }>();
828 const references: { id: string; selector: number }[] = [];
829 let definition: { id: string; start: number; markup: number } | null = null;
830 let reference: { id: string; selector: number } | null = null;
831 const pictures: { start: number; end: number }[] = [];
832 let picture: { start: number; depth: number } | null = null;
833 parser.on("opentag", (tag) => {
834 if (++depth > 64) throw new PreviewError({ code: "word-limited-xml-nesting" });
835 markup++;
836 const attr = (name: string) =>
837 Object.values(tag.attributes).find((a) => a.local === name)?.value ?? "";
838 names.push(tag.local === "tblStylePr" ? `${tag.local}:${attr("type")}` : tag.local);
839 if (roles?.has("styles") && (tag.local === "style" || tag.local === "docDefaults")) {
840 if (++styleCount > WORD_LIMITS.styles || attr("styleId").length > 256) throw limit;
841 style = { id: attr("styleId"), properties: new Map(), base: "", linked: "" };
842 }
843 if (style) {
844 if (tag.local === "basedOn") style.base = attr("val");
845 else if (tag.local === "link") style.linked = attr("val");
846 else if (names.length > 2 && tag.local !== "name") {
847 const key = names.slice(2).join("/");
848 const size = new TextEncoder().encode(
849 xml.slice(xml.lastIndexOf("<", parser.position - 1), parser.position),
850 ).length;
851 if (++ownUnits > WORD_LIMITS.styleUnits) throw limit;
852 style.properties.set(key, size);
853 }
854 }
855 if (roles?.has("numbering")) {
856 if (tag.local === "abstractNum")
857 definition = {
858 id: attr("abstractNumId"),
859 start: xml.lastIndexOf("<", parser.position - 1),
860 markup: markup - 1,
861 };
862 if (tag.local === "num") {
863 if (references.length >= WORD_LIMITS.numberingReferences) throw limit;
864 reference = { id: "", selector: attr("numId").length + 32 };
865 references.push(reference);
866 }
867 if (tag.local === "abstractNumId" && reference) reference.id = attr("val");
868 }
869 if (!picture && (tag.local === "numPicBullet" || tag.local === "lvlPicBulletId"))
870 picture = { start: xml.lastIndexOf("<", parser.position - 1), depth };
871 });
872 parser.on("closetag", (tag) => {
873 if (picture?.depth === depth) {
874 pictures.push({ start: picture.start, end: parser.position });
875 picture = null;
876 }
877 if (definition && tag.local === "abstractNum") {
878 const previous = definitions.get(definition.id);
879 definitions.set(definition.id, {
880 bytes:
881 (previous?.bytes ?? 0) +
882 new TextEncoder().encode(xml.slice(definition.start, parser.position)).length,
883 markup: (previous?.markup ?? 0) + markup - definition.markup,
884 });
885 definition = null;
886 }
887 if (tag.local === "num") reference = null;
888 if (style && (tag.local === "style" || tag.local === "docDefaults")) {
889 styles.set(style.id, style);
890 style = null;
891 }
892 names.pop();
893 depth--;
894 });
895 try {
896 parser.write(xml).close();
897 } catch (cause) {
898 if (cause !== limit) throw cause;
899 return exceeded;
900 }
901 const rawBytes = new TextEncoder().encode(xml).length;
902 if (roles?.has("styles")) {
903 // Immutable parent pointers match processStyles' ordered merges (including
904 // forward links and cycles) without copying an expanding property map.
905 type Resolved = {
906 properties: Map<string, number>;
907 parent?: Resolved | undefined;
908 depth: number;
909 };
910 const resolved = new Map<string, Resolved>();
911 for (const [id, style] of styles) resolved.set(id, { properties: style.properties, depth: 1 });
912 for (const [id, style] of styles) {
913 const parent = style.base ? resolved.get(style.base) : undefined;
914 if (parent && parent.depth >= 32) return exceeded;
915 resolved.set(id, { properties: style.properties, parent, depth: (parent?.depth ?? 0) + 1 });
916 }
917 let bytes = 0;
918 let expandedMarkup = 0;
919 let visited = 0;
920 let propertyVisits = 0;
921 for (const [id, style] of styles) {
922 for (const root of [
923 resolved.get(id),
924 style.linked ? resolved.get(style.linked) : undefined,
925 ]) {
926 const seen = new Set<string>();
927 for (let node = root; node; node = node.parent) {
928 if (++visited > WORD_LIMITS.styleVisits) return exceeded;
929 for (const [key, size] of node.properties) {
930 if (++propertyVisits > WORD_LIMITS.stylePropertyVisits) return exceeded;
931 if (seen.has(key)) continue;
932 // Latent styles never enter properties. Charge each own property
933 // once per resolved style, including linked and default selectors.
934 const copies = id ? 1 : 2;
935 bytes += (size * 2 + id.length + 32) * copies;
936 expandedMarkup += copies;
937 if (bytes > WORD_LIMITS.styleBytes || expandedMarkup > WORD_LIMITS.styleUnits)
938 return exceeded;
939 seen.add(key);
940 }
941 }
942 }
943 }
944 return { bytes, markup: expandedMarkup, pictures };
945 }
946 let bytes = rawBytes;
947 if (roles?.has("numbering")) {
948 for (const ref of references) {
949 const cost = definitions.get(ref.id);
950 if (!cost) continue;
951 bytes += cost.bytes * 2 + cost.markup * ref.selector;
952 markup += cost.markup;
953 if (bytes > WORD_LIMITS.numberingBytes || markup > WORD_LIMITS.numberingMarkup)
954 return exceeded;
955 }
956 }
957 return { bytes, markup, pictures };
958 }
959
960 // Layout can repeat notes at references and headers/footers across pages.
961 // Charge those copies, not just the XML that defines them.
962 async function wordExpansion(
963 archive: JSZip,
964 parts: ReturnType<typeof wordPartTypes>,
965 mediaSizes: Map<string, number>,
966 auxiliaryCosts: Map<string, { bytes: number; markup: number }>,
967 documents: string[],
968 ) {
969 type Cost = { bytes: number; markup: number; media: number; images: Set<string> };
970 const empty = (): Cost => ({ bytes: 0, markup: 0, media: 0, images: new Set() });
971 const nodes = new Map<string, { xml: string; path: string }>();
972 const optional = new Set<string>();
973 const noteParts = new Map<string, string>();
974 for (const [path, roles] of parts.roles) {
975 if (![...roles].some((role) => WORD_EXPANDED_PARTS.has(role))) continue;
976 const xml = await archive.file(path)?.async("string");
977 if (!xml) continue;
978 optional.add(path);
979 if (roles.has("header") || roles.has("footer")) nodes.set(path, { xml, path });
980 for (const role of ["footnotes", "endnotes", "comments"]) {
981 if (!roles.has(role)) continue;
982 noteParts.set(role.slice(0, -1), path);
983 const parser = xmlParser();
984 let start = 0;
985 let id = "";
986 let depth = 0;
987 let prefix = "";
988 let suffix = "";
989 parser.on("opentag", (tag) => {
990 if (++depth === 1) {
991 prefix = xml.slice(0, parser.position);
992 suffix = `</${tag.name}>`;
993 }
994 if (tag.local !== role.slice(0, -1)) return;
995 start = xml.lastIndexOf("<", parser.position - 1);
996 id = Object.values(tag.attributes).find((attr) => attr.local === "id")?.value ?? "";
997 });
998 parser.on("closetag", (tag) => {
999 if (tag.local === role.slice(0, -1))
1000 nodes.set(`${path}#${id}`, {
1001 xml: prefix + xml.slice(start, parser.position) + suffix,
1002 path,
1003 });
1004 depth--;
1005 });
1006 parser.write(xml).close();
1007 }
1008 }
1009 const used = new Set<string>();
1010 const memo = new Map<string, Cost>();
1011 let expandedXmlBytes = 0;
1012 const scan = (xml: string, path: string, chain: Set<string>, pages = 1, expand = true): Cost => {
1013 const cost = empty();
1014 const refs = new Map<string, number>();
1015 const headers = new Set<string>();
1016 const parser = xmlParser();
1017 const add = (key: string) => refs.set(key, (refs.get(key) ?? 0) + 1);
1018 parser.on("opentag", (tag) => {
1019 const attr = (name: string) =>
1020 Object.values(tag.attributes).find((a) => a.local === name)?.value ?? "";
1021 const note = /^(footnote|endnote|comment)Reference$/.exec(tag.local)?.[1];
1022 if (note && noteParts.has(note)) add(`${noteParts.get(note)}#${attr("id")}`);
1023 if (tag.local === "headerReference" || tag.local === "footerReference") {
1024 const target = parts.relationships.get(path)?.get(attr("id"));
1025 if (target) headers.add(target);
1026 }
1027 if (tag.local === "altChunk") cost.bytes = Infinity;
1028 if (tag.local !== "blip" && tag.local !== "imagedata") return;
1029 const target = parts.relationships.get(path)?.get(attr("embed") || attr("id"));
1030 if (target) {
1031 cost.media += mediaSizes.get(target) ?? OFFICE_PREVIEW_MAX_BYTES + 1;
1032 cost.images.add(target);
1033 }
1034 });
1035 parser.write(xml).close();
1036 for (const header of headers) refs.set(header, pages);
1037 if (!expand) return cost;
1038 for (const [key, copies] of refs) {
1039 const node = nodes.get(key);
1040 if (!node) continue;
1041 used.add(node.path);
1042 if (chain.has(key) || chain.size >= 64) {
1043 cost.bytes = Infinity;
1044 break;
1045 }
1046 let expanded = memo.get(key);
1047 if (!expanded) {
1048 expandedXmlBytes += node.xml.length * 2;
1049 if (expandedXmlBytes > WORD_LIMITS.expandedBytes) {
1050 cost.bytes = Infinity;
1051 break;
1052 }
1053 const own = wordXmlCost(node.xml, undefined);
1054 expanded = scan(node.xml, node.path, new Set([...chain, key]));
1055 expanded.bytes += own.bytes;
1056 expanded.markup += own.markup;
1057 memo.set(key, expanded);
1058 }
1059 cost.bytes += expanded.bytes * copies;
1060 cost.markup += expanded.markup * copies;
1061 cost.media += expanded.media * copies;
1062 for (const image of expanded.images) cost.images.add(image);
1063 }
1064 return cost;
1065 };
1066 const costs = new Map<string, Cost>();
1067 let bytes = 0;
1068 let markup = 0;
1069 let media = 0;
1070 // Styles may introduce a page break before each styled paragraph. The page
1071 // bound also includes explicit breaks and section transitions in each batch.
1072 let stylePageBreaks = false;
1073 for (const [path, roles] of parts.roles)
1074 if (
1075 roles.has("styles") &&
1076 /<(?:\w+:)?pageBreakBefore\b/.test((await archive.file(path)?.async("string")) ?? "")
1077 )
1078 stylePageBreaks = true;
1079 for (const document of documents) {
1080 const pages = Math.max(
1081 1,
1082 document.match(
1083 stylePageBreaks ? /<(?:\w+:)?p\b/g : /<(?:\w+:)?(?:br|pageBreakBefore|sectPr)\b/g,
1084 )?.length ?? 0,
1085 );
1086 const expanded = scan(document, "word/document.xml", new Set(), pages);
1087 const body = wordXmlCost(document, undefined);
1088 bytes += body.bytes + expanded.bytes;
1089 markup += body.markup + expanded.markup;
1090 media += expanded.media;
1091 costs.set(document, expanded);
1092 }
1093 for (const [path, cost] of auxiliaryCosts) {
1094 if (optional.has(path) && !used.has(path)) continue;
1095 bytes += cost.bytes;
1096 markup += cost.markup;
1097 }
1098 const overBudget =
1099 bytes > WORD_LIMITS.expandedBytes ||
1100 markup > WORD_LIMITS.expandedMarkup ||
1101 media > OFFICE_PREVIEW_MAX_BYTES;
1102 const truncated = (used.size > 0 && overBudget) || bytes === Infinity;
1103 const omitted = new Set([...optional].filter((path) => truncated || !used.has(path)));
1104 for (const path of omitted) archive.remove(path);
1105 if (truncated)
1106 for (const document of documents)
1107 costs.set(document, scan(document, "word/document.xml", new Set(), 1, false));
1108 // Remove relationships too: related XML is parsed even when not rendered.
1109 for (const [owner, refs] of parts.relationships) {
1110 const ids = new Set([...refs].filter(([, path]) => omitted.has(path)).map(([id]) => id));
1111 if (ids.size === 0) continue;
1112 const relPath = owner ? owner.replace(/([^/]+)$/, "_rels/$1.rels") : "_rels/.rels";
1113 let xml = await archive.file(relPath)?.async("string");
1114 if (!xml) continue;
1115 const removed: { start: number; end: number }[] = [];
1116 const parser = xmlParser();
1117 let start = -1;
1118 parser.on("opentag", (tag) => {
1119 if (tag.local === "Relationship" && ids.has(tag.attributes.Id?.value ?? ""))
1120 start = xml!.lastIndexOf("<", parser.position - 1);
1121 });
1122 parser.on("closetag", (tag) => {
1123 if (tag.local === "Relationship" && start >= 0) {
1124 removed.push({ start, end: parser.position });
1125 start = -1;
1126 }
1127 });
1128 parser.write(xml).close();
1129 for (const range of removed.toReversed())
1130 xml = xml.slice(0, range.start) + xml.slice(range.end);
1131 archive.file(relPath, xml);
1132 }
1133 return { costs, truncated, omitted };
1134 }
1135
1136 // Stream each entry before handing it to an engine. A tiny compressed archive
1137 // must not turn into an unbounded allocation on the UI thread.
1138 export async function boundedArchive(input: Extract<OfficeWorkerRequest, { type: "open" }>) {
1139 const { bytes } = input;
1140 const spreadsheet = input.kind === "xlsx";
1141 if (bytes.byteLength > OFFICE_PREVIEW_MAX_BYTES)
1142 throw new PreviewError({ code: "file-too-large" });
1143 checkZipDirectory(bytes);
1144 const source = await JSZip.loadAsync(bytes).catch(() => {
1145 throw new PreviewError({ code: "office-invalid" });
1146 });
1147 const allEntries = Object.values(source.files);
1148 if (allEntries.length > 2048) throw new PreviewError({ code: "office-too-many-entries" });
1149 const entries = allEntries.filter((entry) => !entry.dir);
1150 if (
1151 input.kind === "docx" &&
1152 entries.some((entry) => entry.name.includes("\\") || entry.name.startsWith("/"))
1153 )
1154 throw new PreviewError({ code: "word-limited-archive-paths" });
1155 const archive = new JSZip();
1156 const partTypes = wordPartTypes();
1157 // Metadata must be bounded and classified before any payload gets a media exemption.
1158 if (input.kind === "docx")
1159 entries.sort(
1160 (a, b) =>
1161 Number(b.name === "[Content_Types].xml" || b.name.endsWith(".rels")) -
1162 Number(a.name === "[Content_Types].xml" || a.name.endsWith(".rels")),
1163 );
1164 const costs = new Map<string, { bytes: number; markup: number }>();
1165 const mediaSizes = new Map<string, number>();
1166 let total = 0;
1167 let wordAuxiliaryMarkup = 0;
1168 let wordInputTruncated = false;
1169 let word: ReturnType<typeof wordDocuments> | null = null;
1170 for (const entry of entries) {
1171 const document = input.kind === "docx" && entry.name === "word/document.xml";
1172 const media = input.kind === "docx" && partTypes.binary(entry.name);
1173 const chunks: Uint8Array[] = [];
1174 const sections = document ? wordSections() : null;
1175 let decoder: TextDecoder | undefined;
1176 let length = 0;
1177 await new Promise<void>((resolve, reject) => {
1178 // JSZip exposes this streaming API at runtime but omits it from its types.
1179 const stream = (
1180 entry as JSZip.JSZipObject & {
1181 internalStream(type: "uint8array"): JSZip.JSZipStreamHelper<Uint8Array>;
1182 }
1183 ).internalStream("uint8array");
1184 stream
1185 .on("data", (chunk: Uint8Array) => {
1186 total += chunk.length;
1187 length += chunk.length;
1188 if (total > (spreadsheet ? 128 * 1024 * 1024 : OFFICE_PREVIEW_MAX_BYTES)) {
1189 stream.pause();
1190 reject(new PreviewError({ code: "office-too-large-expanded" }));
1191 return;
1192 }
1193 try {
1194 if (sections) {
1195 decoder ??= officeXmlDecoder(chunk);
1196 sections.write(decoder.decode(chunk, { stream: true }));
1197 }
1198 } catch (cause) {
1199 stream.pause();
1200 reject(cause);
1201 return;
1202 }
1203 if (document && length > 4 * 1024 * 1024) {
1204 const kept = Math.max(0, chunk.length - (length - 4 * 1024 * 1024));
1205 if (kept) chunks.push(chunk.subarray(0, kept));
1206 wordInputTruncated = true;
1207 return;
1208 }
1209 // Media is bounded independently. Even renamed auxiliary XML must
1210 // stay small before the engine parses it.
1211 if (input.kind === "docx" && !document && !media) {
1212 for (const byte of chunk) if (byte === 0x3c) wordAuxiliaryMarkup++;
1213 if (
1214 length > WORD_LIMITS.auxiliaryPartBytes ||
1215 wordAuxiliaryMarkup > WORD_LIMITS.auxiliaryMarkup
1216 ) {
1217 stream.pause();
1218 reject(new PreviewError({ code: "word-limited-auxiliary-markup" }));
1219 return;
1220 }
1221 }
1222 if (
1223 total > (spreadsheet ? 128 * 1024 * 1024 : OFFICE_PREVIEW_MAX_BYTES) ||
1224 (spreadsheet && total > Math.max(bytes.byteLength * 200, 8 * 1024 * 1024)) ||
1225 (!document &&
1226 (spreadsheet || media || entry.name.endsWith(".xml")) &&
1227 length > (spreadsheet ? 64 * 1024 * 1024 : 8 * 1024 * 1024))
1228 ) {
1229 stream.pause();
1230 reject(new PreviewError({ code: "office-too-large-expanded" }));
1231 } else chunks.push(chunk);
1232 })
1233 .on("error", reject)
1234 .on("end", resolve)
1235 .resume();
1236 });
1237 const content = new Uint8Array(document ? Math.min(length, 4 * 1024 * 1024) : length);
1238 let offset = 0;
1239 for (const chunk of chunks) {
1240 content.set(chunk, offset);
1241 offset += chunk.length;
1242 }
1243 if (document) {
1244 sections!.write(decoder?.decode() ?? "");
1245 word = wordDocuments(officeXml(content), wordInputTruncated, sections!.finish());
1246 archive.file(entry.name, word.documents[0]!);
1247 } else if (input.kind === "docx" && !media) {
1248 let xml = officeXml(content);
1249 if (entry.name === "[Content_Types].xml" || entry.name.endsWith(".rels"))
1250 xml = partTypes.metadata(entry.name, xml);
1251 const roles = partTypes.roles.get(entry.name);
1252 let cost = wordXmlCost(xml, roles);
1253 // The renderer parses related parts even when their render option is off.
1254 if (roles && [...roles].every((role) => WORD_DISABLED_PARTS.has(role))) continue;
1255 // Picture bullets come out, and their list levels keep the text bullets Word falls back to.
1256 if (roles?.has("numbering") && cost.pictures.length > 0) {
1257 for (const range of cost.pictures.toReversed())
1258 xml = xml.slice(0, range.start) + xml.slice(range.end);
1259 cost = wordXmlCost(xml, roles);
1260 wordInputTruncated = true;
1261 }
1262 if (
1263 (roles?.has("styles") &&
1264 (cost.bytes > WORD_LIMITS.styleBytes || cost.markup > WORD_LIMITS.styleUnits)) ||
1265 (roles?.has("numbering") &&
1266 (cost.bytes > WORD_LIMITS.numberingBytes || cost.markup > WORD_LIMITS.numberingMarkup))
1267 ) {
1268 wordInputTruncated = true;
1269 continue;
1270 }
1271 costs.set(entry.name, cost);
1272 archive.file(entry.name, xml);
1273 } else {
1274 if (input.kind === "docx" && partTypes.roles.get(entry.name)?.has("font")) {
1275 continue;
1276 }
1277 if (media) mediaSizes.set(entry.name, Math.ceil((content.length * 4) / 3));
1278 archive.file(entry.name, /\.(?:xml|rels)$/i.test(entry.name) ? officeXml(content) : content);
1279 }
1280 }
1281 if (word) {
1282 const expansion = await wordExpansion(archive, partTypes, mediaSizes, costs, word.documents);
1283 wordInputTruncated ||= expansion.truncated;
1284 for (const path of expansion.omitted) {
1285 archive.remove(path);
1286 costs.delete(path);
1287 }
1288 let mediaBytes = 0;
1289 const usedImages = new Set<string>();
1290 const auxiliary = [...costs.values()].reduce(
1291 (sum, cost) => ({ bytes: sum.bytes + cost.bytes, markup: sum.markup + cost.markup }),
1292 { bytes: 0, markup: 0 },
1293 );
1294 // The worker engine parses shared parts once, rather than once per legacy render batch.
1295 let bytes = auxiliary.bytes;
1296 let markup = auxiliary.markup;
1297 const documents: string[] = [];
1298 for (const document of word.documents) {
1299 const rawCost = wordXmlCost(document, undefined);
1300 const extra = expansion.costs.get(document)!;
1301 const cost = { bytes: rawCost.bytes + extra.bytes, markup: rawCost.markup + extra.markup };
1302 const images = extra.media;
1303 const batchImages = new Set(extra.images);
1304 if (
1305 bytes + cost.bytes > WORD_LIMITS.expandedBytes ||
1306 markup + cost.markup > WORD_LIMITS.expandedMarkup ||
1307 mediaBytes + images > OFFICE_PREVIEW_MAX_BYTES
1308 )
1309 break;
1310 bytes += cost.bytes;
1311 markup += cost.markup;
1312 mediaBytes += images;
1313 for (const path of batchImages) usedImages.add(path);
1314 documents.push(document);
1315 }
1316 if (documents.length === 0) throw new PreviewError({ code: "word-limited-expanded-markup" });
1317 word.truncated ||= wordInputTruncated || documents.length < word.documents.length;
1318 word.documents = documents;
1319 for (const path of mediaSizes.keys()) if (!usedImages.has(path)) archive.remove(path);
1320 archive.file("word/document.xml", documents[0]!);
1321 }
1322 const workbook = input.kind === "xlsx" ? await boundWorkbook(archive) : undefined;
1323 return { archive, word, workbook };
1324 }
1325
1326 export async function prepareOffice(
1327 bytes: ArrayBuffer,
1328 kind: Extract<OfficeWorkerRequest, { type: "open" }>["kind"],
1329 ) {
1330 const result = await boundedArchive({ type: "open", kind, bytes });
1331 const root =
1332 kind === "docx"
1333 ? "word/document.xml"
1334 : kind === "xlsx"
1335 ? "xl/workbook.xml"
1336 : "ppt/presentation.xml";
1337 if (!result.archive.file(root)) throw new PreviewError({ code: "office-not-open-xml" });
1338 if (result.word) {
1339 const references = new Map<string, string>();
1340 const relationships = xmlParser();
1341 relationships.on("opentag", (tag) => {
1342 if (tag.local !== "Relationship") return;
1343 const type = tag.attributes.Type?.value ?? "";
1344 const target = tag.attributes.Target?.value ?? "";
1345 const path = new URL(target, "https://docx.invalid/word/").pathname.slice(1);
1346 if (result.archive.file(path))
1347 references.set(tag.attributes.Id?.value ?? "", type.slice(type.lastIndexOf("/") + 1));
1348 });
1349 const relationshipXml = await result.archive
1350 .file("word/_rels/document.xml.rels")
1351 ?.async("string");
1352 if (relationshipXml) relationships.write(relationshipXml).close();
1353 let xml = joinWordBatches(result.word.documents);
1354 const removed: { start: number; end: number }[] = [];
1355 const parser = xmlParser();
1356 let start = -1;
1357 // Bounded parts can be omitted; dangling story references otherwise reject the entire body in the engine.
1358 parser.on("opentag", (tag) => {
1359 if (tag.local !== "headerReference" && tag.local !== "footerReference") return;
1360 const id = Object.values(tag.attributes).find((attr) => attr.local === "id")?.value ?? "";
1361 if (references.get(id) !== tag.local.replace("Reference", ""))
1362 start = xml.lastIndexOf("<", parser.position - 1);
1363 });
1364 parser.on("closetag", (tag) => {
1365 if (start >= 0 && (tag.local === "headerReference" || tag.local === "footerReference")) {
1366 removed.push({ start, end: parser.position });
1367 start = -1;
1368 }
1369 });
1370 parser.write(xml).close();
1371 for (const range of removed.toReversed())
1372 xml = xml.slice(0, range.start) + xml.slice(range.end);
1373 result.archive.file(root, xml);
1374 }
1375 return {
1376 bytes: await result.archive.generateAsync({ type: "arraybuffer", compression: "STORE" }),
1377 truncated:
1378 result.word?.truncated || result.workbook?.sheets.some((sheet) => sheet.truncated) || false,
1379 view: result.workbook && {
1380 hiddenGridlines: result.workbook.sheets.flatMap((sheet, index) =>
1381 sheet.gridlines ? [] : [index],
1382 ),
1383 themeFonts: result.workbook.themeFonts,
1384 },
1385 };
1386 }
1387
1388 function joinWordBatches(documents: string[]) {
1389 let prefix = "";
1390 let suffix = "";
1391 let section = "";
1392 const bodies: string[] = [];
1393 for (const xml of documents) {
1394 const parser = xmlParser();
1395 let depth = 0;
1396 let start = 0;
1397 let end = 0;
1398 let sectionStart = 0;
1399 parser.on("opentag", (tag) => {
1400 depth++;
1401 if (depth === 2 && tag.local === "body") {
1402 start = parser.position;
1403 if (!prefix) prefix = xml.slice(0, start);
1404 }
1405 if (depth === 3 && tag.local === "sectPr")
1406 sectionStart = xml.lastIndexOf("<", parser.position - 1);
1407 });
1408 parser.on("closetag", (tag) => {
1409 if (depth === 2 && tag.local === "body") {
1410 end = xml.lastIndexOf("<", parser.position - 1);
1411 suffix = xml.slice(end);
1412 }
1413 depth--;
1414 });
1415 parser.write(xml).close();
1416 bodies.push(xml.slice(start, sectionStart || end));
1417 section = sectionStart ? xml.slice(sectionStart, end) : "";
1418 }
1419 return prefix + bodies.join("") + section + suffix;
1420 }