Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

officePreview.worker.ts

116 lines4.4 KB

1import { toTsv } from "@betteroffice/xlsx/headless";
2import { isCompoundOfficeFile } from "./officeEncryptedStreams";
3import { previewUnlock } from "./previewUnlock";
4import { boundedArchive, prepareOffice } from "./officeArchive";
5import { PREVIEW_PAGE_ROWS } from "./largeTextProtocol";
6import type { OfficeWorkerRequest, OfficeWorkerResult } from "./officePreviewProtocol";
7import type { openEpub } from "./epubPreview";
8import type { ParsedOdsWorkbook } from "./extendedDocumentPreview";
9import { PreviewError, previewFailure } from "./previewFailure";
10​
11let epub: Awaited<ReturnType<typeof openEpub>> | undefined;
12let pendingEpubPage: number | undefined;
13let renderingEpub = false;
14let epubAssets: string[] = [];
15let workbook: ParsedOdsWorkbook["sheets"] = [];
16function reply(result: OfficeWorkerResult) {
17 self.postMessage(result, { transfer: "bytes" in result ? [result.bytes] : [] });
19async function handle(input: Exclude<OfficeWorkerRequest, { type: "unlock" }>, password?: string) {
20 if (input.type === "epub-page") {
21 const book = epub;
22 if (!book) throw new PreviewError({ code: "epub-no-chapters" });
23 pendingEpubPage = input.index;
24 if (renderingEpub) return;
25 renderingEpub = true;
26 try {
27 while (pendingEpubPage !== undefined) {
28 const index = pendingEpubPage;
29 pendingEpubPage = undefined;
30 const result = await book
31 .page(index)
32 .catch((cause: unknown) => previewFailure(cause, "parse-failed"));
33 if (pendingEpubPage !== undefined && pendingEpubPage !== index) {
34 if ("assets" in result) for (const url of result.assets) URL.revokeObjectURL(url);
35 continue;
36 }
37 for (const url of epubAssets) URL.revokeObjectURL(url);
38 if ("assets" in result) {
39 const { assets, ...page } = result;
40 epubAssets = assets;
41 reply({ type: "epub-page", index, result: page });
42 } else {
43 epubAssets = [];
44 reply({ type: "epub-page", index, result });
45 }
46 pendingEpubPage = undefined;
47 }
48 } finally {
49 renderingEpub = false;
50 }
51 return;
52 }
53 if (input.type === "sheet-page" || input.type === "sheet-copy") {
54 const sheet = workbook[input.sheet];
55 if (!sheet) throw new PreviewError({ code: "sheet-unavailable" });
56 if (input.type === "sheet-copy") {
57 const cells = sheet.rows.map((row, index) => {
58 const signedColumns = sheet.signedNumberColumns[index];
59 return row.map((input, column) => ({
60 input,
61 isFormula: signedColumns?.includes(column) ?? false,
62 }));
63 });
64 reply({ type: "sheet-copy", sheet: input.sheet, text: toTsv(cells) });
65 return;
66 }
67 reply({
68 type: "sheet-page",
69 sheet: input.sheet,
70 index: input.index,
71 rows: sheet.rows.slice(
72 input.index * PREVIEW_PAGE_ROWS,
73 (input.index + 1) * PREVIEW_PAGE_ROWS,
74 ),
75 });
76 return;
77 }
78 if (input.kind === "docx" || input.kind === "xlsx" || input.kind === "pptx") {
79 const bytes = isCompoundOfficeFile(input.bytes)
80 ? (await import("./officeEncryption")).decryptOffice(input.bytes, password)
81 : input.bytes;
82 reply({ type: input.kind, ...(await prepareOffice(bytes, input.kind, password)) });
83 return;
84 }
85 if (input.kind === "epub") {
86 const { openEpub } = await import("./epubPreview");
87 const { archive } = await boundedArchive(input, password);
88 const { page, ...book } = await openEpub(archive);
89 epub = { page, ...book };
90 reply({ type: "epub", ...book });
91 return;
92 }
93 const { openDocument, openNotebook, openRtf } = await import("./extendedDocumentPreview");
94 if (input.kind === "ipynb" || input.kind === "rtf") {
95 reply(input.kind === "ipynb" ? openNotebook(input.bytes) : openRtf(input.bytes));
96 return;
97 }
98 const { archive } = await boundedArchive(input, password);
99 const { unlockOpenDocument } = await import("./openDocumentEncryption");
100 await unlockOpenDocument(archive, password);
101 const result = await openDocument(archive, input.kind);
102 if (result.type === "ods") {
103 workbook = result.sheets;
104 reply({
105 ...result,
106 sheets: result.sheets.map(({ signedNumberColumns: _signedNumberColumns, ...sheet }) => ({
107 ...sheet,
108 rows: sheet.rows.slice(0, PREVIEW_PAGE_ROWS),
109 })),
110 });
111 } else reply(result);
113const receive = previewUnlock(handle, reply, "parse-failed");
114self.addEventListener("message", (event: MessageEvent<OfficeWorkerRequest>) => {
115 void receive(event.data);
116});