@deepseek-ai/libreoffice-kit 0.0.2-rc9 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/NOTICE +0 -240
- package/README.md +70 -43
- package/README.zh.md +75 -35
- package/lib/cli.js +859 -137
- package/lib/font-snapshot-worker.js +454 -0
- package/lib/index.js +893 -138
- package/lib/types/engine.d.ts +5 -5
- package/lib/types/font-loader.d.ts +18 -16
- package/lib/types/font-metadata-cache.d.ts +11 -0
- package/lib/types/font-snapshot-state.d.ts +15 -0
- package/lib/types/font-snapshot-validation.d.ts +4 -0
- package/lib/types/font-snapshot-worker.d.ts +9 -0
- package/lib/types/font-snapshot.d.ts +11 -0
- package/lib/types/fonts.d.ts +45 -22
- package/lib/types/image-operations.d.ts +1 -1
- package/lib/types/index.d.ts +17 -7
- package/lib/types/native-image-renderer.d.ts +19 -0
- package/lib/types/native.d.ts +1 -1
- package/lib/types/options.d.ts +5 -1
- package/lib/types/wasm.d.ts +4 -1
- package/lib/types/worker.d.ts +5 -3
- package/lib/worker.js +257 -1020
- package/package.json +14 -55
- package/assets/font-subset.json +0 -70
- package/assets/font-subset.wasm +0 -0
- package/lib/browser/assets.json +0 -10
- package/lib/browser/index.js +0 -626
- package/lib/browser/types/connection.d.ts +0 -42
- package/lib/browser/types/engine-rendering.d.ts +0 -3
- package/lib/browser/types/engine-types.d.ts +0 -17
- package/lib/browser/types/font-channel.d.ts +0 -36
- package/lib/browser/types/index.d.ts +0 -14
- package/lib/browser/types/office-engine.d.ts +0 -56
- package/lib/browser/types/office-types.d.ts +0 -166
- package/lib/browser/types/office.d.ts +0 -19
- package/lib/browser/types/preparation.d.ts +0 -13
- package/lib/browser/types/protocol.d.ts +0 -137
- package/lib/browser/types/rendering.d.ts +0 -4
- package/lib/browser/types/types.d.ts +0 -115
- package/lib/browser/types/worker.d.ts +0 -2
- package/lib/browser/worker.js +0 -3796
- package/lib/browser-assets.js +0 -119
- package/lib/document.js +0 -178
- package/lib/engine-rendering.js +0 -120
- package/lib/font-config.js +0 -46
- package/lib/font-source.js +0 -395
- package/lib/font-worker.js +0 -1481
- package/lib/rendering.js +0 -99
- package/lib/sheet-geometry.js +0 -111
- package/lib/types/browser-assets.d.ts +0 -15
- package/lib/types/default-fonts.d.ts +0 -3
- package/lib/types/font-config.d.ts +0 -24
- package/lib/types/font-preferences-data.d.ts +0 -16
- package/lib/types/font-preferences.d.ts +0 -29
- package/lib/types/font-request.d.ts +0 -12
- package/lib/types/font-source-types.d.ts +0 -95
- package/lib/types/font-source.d.ts +0 -10
- package/lib/types/font-subsets.d.ts +0 -39
- package/lib/types/font-worker.d.ts +0 -2
- package/lib/types/harfbuzz-subset.d.ts +0 -11
- package/lib/types/unicode-scripts.d.ts +0 -18
- package/licenses/font-subset/harfbuzz-COPYING +0 -42
- package/licenses/font-subset/harfbuzzjs-LICENSE +0 -19
- package/licenses/javascript/fflate-LICENSE +0 -21
- package/licenses/javascript/saxes-LICENSE +0 -64
- package/licenses/javascript/xmlchars-LICENSE +0 -18
- package/sources/browser/packages/browser/package.json +0 -22
- package/sources/browser/packages/browser/src/connection.ts +0 -237
- package/sources/browser/packages/browser/src/engine-rendering.ts +0 -2
- package/sources/browser/packages/browser/src/engine-types.ts +0 -14
- package/sources/browser/packages/browser/src/font-channel.ts +0 -169
- package/sources/browser/packages/browser/src/index.ts +0 -40
- package/sources/browser/packages/browser/src/office-engine.ts +0 -397
- package/sources/browser/packages/browser/src/office-types.ts +0 -128
- package/sources/browser/packages/browser/src/office.ts +0 -128
- package/sources/browser/packages/browser/src/preparation.ts +0 -41
- package/sources/browser/packages/browser/src/protocol.ts +0 -55
- package/sources/browser/packages/browser/src/rendering.ts +0 -3
- package/sources/browser/packages/browser/src/types.ts +0 -118
- package/sources/browser/packages/browser/src/worker.ts +0 -260
- package/sources/browser/packages/browser/tsconfig.json +0 -5
- package/sources/browser/packages/browser/tsdown.config.ts +0 -7
- package/sources/browser/scripts/build-browser.mjs +0 -86
- package/sources/font-subset/engine/font-subset/README.md +0 -23
- package/sources/font-subset/engine/font-subset/README.zh.md +0 -23
- package/sources/font-subset/engine/font-subset/source.json +0 -16
- package/sources/font-subset/harfbuzzjs/config-override-subset.h +0 -7
- package/sources/font-subset/harfbuzzjs/harfbuzz-subset.symbols +0 -34
- package/sources/font-subset/scripts/build-font-subset.mjs +0 -64
- package/sources/font-subset/scripts/pack-utils.mjs +0 -69
- package/sources/font-subset/scripts/platform-matrix.mjs +0 -100
- package/sources/font-subset/scripts/verify-artifacts.mjs +0 -260
package/lib/browser/index.js
DELETED
|
@@ -1,626 +0,0 @@
|
|
|
1
|
-
//#region lib/types/protocol.js
|
|
2
|
-
const FONT_CHUNK_BYTES = 1048576;
|
|
3
|
-
//#endregion
|
|
4
|
-
//#region lib/types/types.js
|
|
5
|
-
/** Browser-facing document rendering and host font interfaces. */
|
|
6
|
-
/** A rendering failure with a stable machine-readable category. */
|
|
7
|
-
var BrowserRenderError = class extends Error {
|
|
8
|
-
code;
|
|
9
|
-
constructor(code, message) {
|
|
10
|
-
super(message);
|
|
11
|
-
this.code = code;
|
|
12
|
-
this.name = "BrowserRenderError";
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
//#endregion
|
|
16
|
-
//#region lib/types/font-channel.js
|
|
17
|
-
/** Views shared by the owner and engine; only the engine uses Atomics.wait. */
|
|
18
|
-
function fontViews(channel) {
|
|
19
|
-
return {
|
|
20
|
-
control: new Int32Array(channel, 0, 2),
|
|
21
|
-
bytes: new Uint8Array(channel, 8)
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
/** Only uninstalled content crosses the channel; each family retains its own alias. */
|
|
25
|
-
function* fontFrames(result, known = []) {
|
|
26
|
-
const identities = new Map(known.map((font) => [font.id, font]));
|
|
27
|
-
const header = {
|
|
28
|
-
fonts: result.fonts.map((font) => {
|
|
29
|
-
const size = font.data === void 0 ? font.bytes : font.data.byteLength;
|
|
30
|
-
const identity = {
|
|
31
|
-
id: font.id,
|
|
32
|
-
bytes: size,
|
|
33
|
-
...font.format ? { format: font.format } : {}
|
|
34
|
-
};
|
|
35
|
-
const prior = identities.get(font.id);
|
|
36
|
-
if (prior && (prior.bytes !== identity.bytes || prior.format !== identity.format)) throw new BrowserRenderError("font-unavailable", "Font identity has conflicting bytes or formats.");
|
|
37
|
-
if (!prior && font.data === void 0) throw new BrowserRenderError("font-unavailable", "Font reference has not been installed.");
|
|
38
|
-
identities.set(font.id, identity);
|
|
39
|
-
return {
|
|
40
|
-
...identity,
|
|
41
|
-
family: font.family,
|
|
42
|
-
alias: font.alias,
|
|
43
|
-
...prior ? { reference: true } : {}
|
|
44
|
-
};
|
|
45
|
-
}),
|
|
46
|
-
...result.missingFamily === void 0 ? {} : { missingFamily: result.missingFamily }
|
|
47
|
-
};
|
|
48
|
-
const encoded = new TextEncoder().encode(JSON.stringify(header));
|
|
49
|
-
if (encoded.length > 1048576) throw new BrowserRenderError("font-limit", "Font response metadata exceeds the transfer limit.");
|
|
50
|
-
yield {
|
|
51
|
-
state: 1,
|
|
52
|
-
bytes: encoded
|
|
53
|
-
};
|
|
54
|
-
for (const [index, font] of result.fonts.entries()) {
|
|
55
|
-
if (header.fonts[index].reference) continue;
|
|
56
|
-
/* v8 ignore next -- header validation rejects an unknown data-less font before iteration. */
|
|
57
|
-
if (font.data === void 0) throw new BrowserRenderError("font-unavailable", "Font reference has not been installed.");
|
|
58
|
-
for (let offset = 0; offset < font.data.byteLength; offset += FONT_CHUNK_BYTES) yield {
|
|
59
|
-
state: 2,
|
|
60
|
-
bytes: font.data.subarray(offset, offset + FONT_CHUNK_BYTES)
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
yield {
|
|
64
|
-
state: 3,
|
|
65
|
-
bytes: /* @__PURE__ */ new Uint8Array()
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
//#endregion
|
|
69
|
-
//#region lib/types/preparation.js
|
|
70
|
-
/** Browser-side WASM compilation without creating an Office Worker or memory. */
|
|
71
|
-
/**
|
|
72
|
-
* Compile the version-matched LibreOffice WASM module for later document Workers.
|
|
73
|
-
* @param assets - Browser resources whose WASM URL identifies the compiled module.
|
|
74
|
-
* @param signal - Cancels the resource request; compilation may finish before cancellation is observed.
|
|
75
|
-
* @returns an opaque, structured-cloneable engine handle that allocates no WASM memory.
|
|
76
|
-
*/
|
|
77
|
-
async function prepareOfficeBrowser(assets, signal) {
|
|
78
|
-
signal?.throwIfAborted();
|
|
79
|
-
if (typeof WebAssembly.compileStreaming !== "function") throw new BrowserRenderError("unavailable", "Streaming WebAssembly compilation is unavailable.");
|
|
80
|
-
const response = await fetch(assets.wasmUrl, {
|
|
81
|
-
credentials: "same-origin",
|
|
82
|
-
...signal === void 0 ? {} : { signal }
|
|
83
|
-
});
|
|
84
|
-
if (!response.ok) throw new BrowserRenderError("unavailable", `LibreOffice WASM request failed: ${response.status}`);
|
|
85
|
-
const module = await WebAssembly.compileStreaming(Promise.resolve(response));
|
|
86
|
-
signal?.throwIfAborted();
|
|
87
|
-
return Object.freeze({
|
|
88
|
-
wasmUrl: assets.wasmUrl,
|
|
89
|
-
module
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
/** Return the compiled module after validating the Worker-bound asset identity. */
|
|
93
|
-
function preparedBrowserModule(prepared, assets) {
|
|
94
|
-
const record = prepared;
|
|
95
|
-
if (record.wasmUrl !== assets.wasmUrl || !(record.module instanceof WebAssembly.Module)) throw new TypeError("The prepared browser engine does not match the requested WASM asset.");
|
|
96
|
-
return record.module;
|
|
97
|
-
}
|
|
98
|
-
//#endregion
|
|
99
|
-
//#region lib/types/connection.js
|
|
100
|
-
/** Browser Worker ownership, document lifetime, cancellation and asynchronous Host font delivery. */
|
|
101
|
-
/**
|
|
102
|
-
* Start one LibreOffice Worker without loading a document.
|
|
103
|
-
* @param options - Engine resources, limits and asynchronous Host font provider.
|
|
104
|
-
* @param signal - Engine lifetime; abort closes its Worker and all pending work.
|
|
105
|
-
* @param onEvent - Reading notifications, including fatal Worker failures.
|
|
106
|
-
* @returns Document loading, Worker requests and joined disposal.
|
|
107
|
-
*/
|
|
108
|
-
async function openBrowserWorker(options, signal, onEvent) {
|
|
109
|
-
signal?.throwIfAborted();
|
|
110
|
-
if (!globalThis.crossOriginIsolated || typeof SharedArrayBuffer === "undefined" || typeof Worker === "undefined") throw new BrowserRenderError("unavailable", "LibreOffice requires an isolated secure browser context with Worker and SharedArrayBuffer support.");
|
|
111
|
-
if (![
|
|
112
|
-
options.timeoutMs,
|
|
113
|
-
options.maxLoadedFontBytes,
|
|
114
|
-
options.maxArchiveEntries,
|
|
115
|
-
options.maxUncompressedBytes
|
|
116
|
-
].every((value) => Number.isSafeInteger(value) && value > 0) || options.timeoutMs > 2147483647) throw new TypeError("Document limits must be positive integers and timeoutMs must fit a browser timer.");
|
|
117
|
-
if (options.preparedEngine !== void 0) preparedBrowserModule(options.preparedEngine, options.assets);
|
|
118
|
-
const worker = new Worker(options.assets.workerUrl, { name: "libreoffice-document" });
|
|
119
|
-
const channel = new SharedArrayBuffer(1048584);
|
|
120
|
-
const { control, bytes } = fontViews(channel);
|
|
121
|
-
const lifetime = new AbortController();
|
|
122
|
-
const pending = /* @__PURE__ */ new Map();
|
|
123
|
-
let sequence = 0;
|
|
124
|
-
let closed = false;
|
|
125
|
-
let disposal;
|
|
126
|
-
let frames;
|
|
127
|
-
let resolveFonts;
|
|
128
|
-
let onMissingFonts;
|
|
129
|
-
function failPending(error) {
|
|
130
|
-
for (const entry of pending.values()) entry.reject(error);
|
|
131
|
-
pending.clear();
|
|
132
|
-
}
|
|
133
|
-
function sendFontError(error) {
|
|
134
|
-
if (closed) return;
|
|
135
|
-
frames = void 0;
|
|
136
|
-
const encoded = new TextEncoder().encode(error instanceof Error ? error.message : String(error)).subarray(0, FONT_CHUNK_BYTES);
|
|
137
|
-
bytes.set(encoded);
|
|
138
|
-
Atomics.store(control, 1, encoded.length);
|
|
139
|
-
Atomics.store(control, 0, 4);
|
|
140
|
-
Atomics.notify(control, 0);
|
|
141
|
-
}
|
|
142
|
-
function pumpFonts() {
|
|
143
|
-
if (closed || !frames) return;
|
|
144
|
-
try {
|
|
145
|
-
const frame = frames.next();
|
|
146
|
-
if (frame.done) {
|
|
147
|
-
frames = void 0;
|
|
148
|
-
return;
|
|
149
|
-
}
|
|
150
|
-
bytes.set(frame.value.bytes);
|
|
151
|
-
Atomics.store(control, 1, frame.value.bytes.length);
|
|
152
|
-
Atomics.store(control, 0, frame.value.state);
|
|
153
|
-
Atomics.notify(control, 0);
|
|
154
|
-
} catch (error) {
|
|
155
|
-
sendFontError(error);
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
worker.onmessage = (event) => {
|
|
159
|
-
const message = event.data;
|
|
160
|
-
switch (message.type) {
|
|
161
|
-
case "office-event":
|
|
162
|
-
if (!closed) {
|
|
163
|
-
onEvent?.(message.event);
|
|
164
|
-
if (message.event.type === "error") dispose();
|
|
165
|
-
}
|
|
166
|
-
return;
|
|
167
|
-
case "font":
|
|
168
|
-
if (closed) return;
|
|
169
|
-
if (!resolveFonts) {
|
|
170
|
-
sendFontError(new BrowserRenderError("font-unavailable", "No document font resolver is active."));
|
|
171
|
-
return;
|
|
172
|
-
}
|
|
173
|
-
Promise.resolve().then(() => resolveFonts(message.request, message.known, lifetime.signal)).then((result) => {
|
|
174
|
-
if (closed) return;
|
|
175
|
-
frames = fontFrames(result, message.known);
|
|
176
|
-
pumpFonts();
|
|
177
|
-
}, sendFontError);
|
|
178
|
-
return;
|
|
179
|
-
case "font-next":
|
|
180
|
-
pumpFonts();
|
|
181
|
-
return;
|
|
182
|
-
case "missing-fonts":
|
|
183
|
-
if (!closed) onMissingFonts?.(message.families);
|
|
184
|
-
return;
|
|
185
|
-
default: {
|
|
186
|
-
const entry = pending.get(message.id);
|
|
187
|
-
if (!entry) return;
|
|
188
|
-
pending.delete(message.id);
|
|
189
|
-
if (message.type === "error") {
|
|
190
|
-
entry.reject(new BrowserRenderError(message.code, message.message));
|
|
191
|
-
if (message.fatal || message.code === "font-limit" || message.code === "font-unavailable" || message.code === "timeout") {
|
|
192
|
-
onEvent?.({
|
|
193
|
-
type: "error",
|
|
194
|
-
code: message.code,
|
|
195
|
-
message: message.message
|
|
196
|
-
});
|
|
197
|
-
dispose();
|
|
198
|
-
}
|
|
199
|
-
} else entry.resolve(message);
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
};
|
|
203
|
-
worker.onerror = (event) => {
|
|
204
|
-
const error = new BrowserRenderError("render-failed", event.message || "LibreOffice Worker failed.");
|
|
205
|
-
onEvent?.({
|
|
206
|
-
type: "error",
|
|
207
|
-
code: error.code,
|
|
208
|
-
message: error.message
|
|
209
|
-
});
|
|
210
|
-
failPending(error);
|
|
211
|
-
dispose();
|
|
212
|
-
};
|
|
213
|
-
worker.onmessageerror = () => {
|
|
214
|
-
const error = new BrowserRenderError("render-failed", "LibreOffice Worker returned an unreadable response.");
|
|
215
|
-
onEvent?.({
|
|
216
|
-
type: "error",
|
|
217
|
-
code: error.code,
|
|
218
|
-
message: error.message
|
|
219
|
-
});
|
|
220
|
-
failPending(error);
|
|
221
|
-
dispose();
|
|
222
|
-
};
|
|
223
|
-
function post(message, transfer = []) {
|
|
224
|
-
return new Promise((resolve, reject) => {
|
|
225
|
-
const timer = setTimeout(() => {
|
|
226
|
-
pending.delete(message.id);
|
|
227
|
-
const error = new BrowserRenderError("timeout", "LibreOffice operation timed out.");
|
|
228
|
-
reject(error);
|
|
229
|
-
if (!closed) onEvent?.({
|
|
230
|
-
type: "error",
|
|
231
|
-
code: error.code,
|
|
232
|
-
message: error.message
|
|
233
|
-
});
|
|
234
|
-
dispose();
|
|
235
|
-
}, options.timeoutMs);
|
|
236
|
-
pending.set(message.id, {
|
|
237
|
-
resolve(value) {
|
|
238
|
-
clearTimeout(timer);
|
|
239
|
-
resolve(value);
|
|
240
|
-
},
|
|
241
|
-
reject(error) {
|
|
242
|
-
clearTimeout(timer);
|
|
243
|
-
reject(error);
|
|
244
|
-
}
|
|
245
|
-
});
|
|
246
|
-
try {
|
|
247
|
-
worker.postMessage(message, transfer);
|
|
248
|
-
} catch (error) {
|
|
249
|
-
pending.get(message.id)?.reject(error);
|
|
250
|
-
pending.delete(message.id);
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
}
|
|
254
|
-
function dispose() {
|
|
255
|
-
if (disposal) return disposal;
|
|
256
|
-
closed = true;
|
|
257
|
-
signal?.removeEventListener("abort", aborted);
|
|
258
|
-
lifetime.abort();
|
|
259
|
-
frames = void 0;
|
|
260
|
-
resolveFonts = void 0;
|
|
261
|
-
onMissingFonts = void 0;
|
|
262
|
-
Atomics.store(control, 0, 5);
|
|
263
|
-
Atomics.notify(control, 0);
|
|
264
|
-
failPending(new BrowserRenderError("disposed", "The document is closed."));
|
|
265
|
-
disposal = new Promise((resolve) => {
|
|
266
|
-
const deadline = setTimeout(resolve, 1e3);
|
|
267
|
-
post({
|
|
268
|
-
type: "dispose",
|
|
269
|
-
id: ++sequence
|
|
270
|
-
}).then(() => resolve(), () => resolve()).finally(() => clearTimeout(deadline));
|
|
271
|
-
}).finally(() => {
|
|
272
|
-
worker.terminate();
|
|
273
|
-
failPending(new BrowserRenderError("disposed", "The document is closed."));
|
|
274
|
-
worker.onmessage = null;
|
|
275
|
-
worker.onerror = null;
|
|
276
|
-
worker.onmessageerror = null;
|
|
277
|
-
});
|
|
278
|
-
return disposal;
|
|
279
|
-
}
|
|
280
|
-
function aborted() {
|
|
281
|
-
dispose();
|
|
282
|
-
}
|
|
283
|
-
signal?.addEventListener("abort", aborted, { once: true });
|
|
284
|
-
function request(message, operation, transfer = []) {
|
|
285
|
-
if (closed) return Promise.reject(new BrowserRenderError("disposed", "The document is closed."));
|
|
286
|
-
if (operation?.aborted) return Promise.reject(operation.reason);
|
|
287
|
-
const task = post({
|
|
288
|
-
...message,
|
|
289
|
-
id: ++sequence
|
|
290
|
-
}, transfer);
|
|
291
|
-
if (!operation) return task;
|
|
292
|
-
return new Promise((resolve, reject) => {
|
|
293
|
-
const abort = () => {
|
|
294
|
-
reject(operation.reason);
|
|
295
|
-
};
|
|
296
|
-
operation.addEventListener("abort", abort, { once: true });
|
|
297
|
-
task.then(resolve, reject).finally(() => operation.removeEventListener("abort", abort));
|
|
298
|
-
});
|
|
299
|
-
}
|
|
300
|
-
const engineOptions = {
|
|
301
|
-
assets: options.assets,
|
|
302
|
-
...options.preparedEngine === void 0 ? {} : { preparedEngine: options.preparedEngine },
|
|
303
|
-
timeoutMs: options.timeoutMs,
|
|
304
|
-
maxLoadedFontBytes: options.maxLoadedFontBytes,
|
|
305
|
-
fontFallbacks: options.fontFallbacks,
|
|
306
|
-
maxArchiveEntries: options.maxArchiveEntries,
|
|
307
|
-
maxUncompressedBytes: options.maxUncompressedBytes
|
|
308
|
-
};
|
|
309
|
-
try {
|
|
310
|
-
if ((await post({
|
|
311
|
-
type: "start",
|
|
312
|
-
id: ++sequence,
|
|
313
|
-
options: engineOptions,
|
|
314
|
-
channel
|
|
315
|
-
})).type !== "started") throw new BrowserRenderError("unavailable", "LibreOffice returned an unexpected start response.");
|
|
316
|
-
signal?.throwIfAborted();
|
|
317
|
-
} catch (failure) {
|
|
318
|
-
await dispose();
|
|
319
|
-
signal?.throwIfAborted();
|
|
320
|
-
throw failure;
|
|
321
|
-
}
|
|
322
|
-
return {
|
|
323
|
-
async openDocument(documentOptions, documentSignal) {
|
|
324
|
-
if (closed) throw new BrowserRenderError("disposed", "The document is closed.");
|
|
325
|
-
if (![
|
|
326
|
-
"pdf",
|
|
327
|
-
"doc",
|
|
328
|
-
"docx",
|
|
329
|
-
"xls",
|
|
330
|
-
"xlsx",
|
|
331
|
-
"ppt",
|
|
332
|
-
"pptx"
|
|
333
|
-
].includes(documentOptions.extension) || documentOptions.data.byteLength === 0) throw new BrowserRenderError("invalid-document", "The document is empty or has an unsupported extension.");
|
|
334
|
-
const data = new Uint8Array(documentOptions.data);
|
|
335
|
-
resolveFonts = documentOptions.resolveFonts;
|
|
336
|
-
onMissingFonts = documentOptions.onMissingFonts;
|
|
337
|
-
try {
|
|
338
|
-
const result = await request({
|
|
339
|
-
type: "open",
|
|
340
|
-
options: {
|
|
341
|
-
data,
|
|
342
|
-
extension: documentOptions.extension,
|
|
343
|
-
...documentOptions.restore === void 0 ? {} : { restore: documentOptions.restore }
|
|
344
|
-
}
|
|
345
|
-
}, documentSignal, [data.buffer]);
|
|
346
|
-
if (result.type !== "opened") throw new BrowserRenderError("invalid-document", "LibreOffice returned an unexpected load response.");
|
|
347
|
-
if (result.pages.length === 0) throw new BrowserRenderError("invalid-document", "LibreOffice returned no document pages.");
|
|
348
|
-
documentSignal?.throwIfAborted();
|
|
349
|
-
return result;
|
|
350
|
-
} catch (failure) {
|
|
351
|
-
await dispose();
|
|
352
|
-
documentSignal?.throwIfAborted();
|
|
353
|
-
throw failure;
|
|
354
|
-
}
|
|
355
|
-
},
|
|
356
|
-
async closeDocument() {
|
|
357
|
-
if (closed) return;
|
|
358
|
-
try {
|
|
359
|
-
if ((await request({ type: "close-document" })).type !== "closed-document") throw new BrowserRenderError("render-failed", "LibreOffice returned an unexpected close response.");
|
|
360
|
-
} finally {
|
|
361
|
-
resolveFonts = void 0;
|
|
362
|
-
onMissingFonts = void 0;
|
|
363
|
-
}
|
|
364
|
-
},
|
|
365
|
-
request,
|
|
366
|
-
dispose
|
|
367
|
-
};
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* Load original PDF bytes in a dedicated Worker without Draw import.
|
|
371
|
-
* @param options - Resources, limits, source bytes and asynchronous Host font provider.
|
|
372
|
-
* @param signal - Document lifetime; abort closes its Worker and all pending work.
|
|
373
|
-
* @param onEvent - Reading notifications, including fatal Worker failures.
|
|
374
|
-
* @returns Initial geometry, ordered Worker requests and joined disposal.
|
|
375
|
-
*/
|
|
376
|
-
async function openBrowserConnection(options, signal, onEvent) {
|
|
377
|
-
signal?.throwIfAborted();
|
|
378
|
-
if (options.extension !== "pdf" || options.data.byteLength === 0) throw new BrowserRenderError("invalid-document", "The document is empty or has an unsupported extension.");
|
|
379
|
-
const connected = await openBrowserWorker(options, signal, onEvent);
|
|
380
|
-
try {
|
|
381
|
-
return {
|
|
382
|
-
opened: await connected.openDocument({
|
|
383
|
-
data: options.data,
|
|
384
|
-
extension: options.extension,
|
|
385
|
-
resolveFonts: options.resolveFonts,
|
|
386
|
-
...options.onMissingFonts === void 0 ? {} : { onMissingFonts: options.onMissingFonts }
|
|
387
|
-
}, signal),
|
|
388
|
-
request: connected.request,
|
|
389
|
-
dispose: connected.dispose
|
|
390
|
-
};
|
|
391
|
-
} catch (error) {
|
|
392
|
-
await connected.dispose();
|
|
393
|
-
throw error;
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
//#endregion
|
|
397
|
-
//#region lib/types/office.js
|
|
398
|
-
/** Browser owner for one shared engine Worker and its read-only Office documents. */
|
|
399
|
-
/**
|
|
400
|
-
* Start one read-only Office engine that opens documents one at a time.
|
|
401
|
-
* @param options - Engine resources and limits shared by every document.
|
|
402
|
-
* @param signal - Runtime lifetime; abort closes the Worker and its pthreads.
|
|
403
|
-
* @returns Sequential document loading and joined disposal.
|
|
404
|
-
*/
|
|
405
|
-
async function openOfficeRuntime(options, signal) {
|
|
406
|
-
let current;
|
|
407
|
-
let disposed = false;
|
|
408
|
-
const worker = await openBrowserWorker(options, signal, (event) => {
|
|
409
|
-
if (event.type === "state") current?.update(event.state);
|
|
410
|
-
for (const listener of current?.listeners ?? []) try {
|
|
411
|
-
listener(event);
|
|
412
|
-
} catch (error) {
|
|
413
|
-
console.error("Office document observer failed", error);
|
|
414
|
-
}
|
|
415
|
-
});
|
|
416
|
-
const open = async (openOptions, documentSignal) => {
|
|
417
|
-
if (disposed) throw new BrowserRenderError("disposed", "The Office runtime is closed.");
|
|
418
|
-
if (current && !current.closed) current.closed = true;
|
|
419
|
-
current?.listeners.clear();
|
|
420
|
-
current = void 0;
|
|
421
|
-
await worker.closeDocument();
|
|
422
|
-
const opened = await worker.openDocument({
|
|
423
|
-
data: openOptions.data,
|
|
424
|
-
extension: openOptions.extension,
|
|
425
|
-
resolveFonts: openOptions.resolveFonts,
|
|
426
|
-
...openOptions.onMissingFonts === void 0 ? {} : { onMissingFonts: openOptions.onMissingFonts },
|
|
427
|
-
...openOptions.restore === void 0 ? {} : { restore: openOptions.restore }
|
|
428
|
-
}, documentSignal);
|
|
429
|
-
if (!opened.office) throw new BrowserRenderError("invalid-document", "LibreOffice returned no reading state.");
|
|
430
|
-
let reading = opened.office;
|
|
431
|
-
const record = {
|
|
432
|
-
closed: false,
|
|
433
|
-
listeners: /* @__PURE__ */ new Set(),
|
|
434
|
-
update(state) {
|
|
435
|
-
reading = state;
|
|
436
|
-
}
|
|
437
|
-
};
|
|
438
|
-
current = record;
|
|
439
|
-
async function operate(operation) {
|
|
440
|
-
if (record.closed) throw new BrowserRenderError("disposed", "Office document is closed.");
|
|
441
|
-
const response = await worker.request({
|
|
442
|
-
type: "office-operation",
|
|
443
|
-
operation
|
|
444
|
-
});
|
|
445
|
-
if (response.type !== "office-result") throw new BrowserRenderError("render-failed", "LibreOffice returned an unexpected reading response.");
|
|
446
|
-
return response;
|
|
447
|
-
}
|
|
448
|
-
let layoutRunning = false;
|
|
449
|
-
let pendingLayout;
|
|
450
|
-
async function applyLayouts() {
|
|
451
|
-
layoutRunning = true;
|
|
452
|
-
while (pendingLayout) {
|
|
453
|
-
const next = pendingLayout;
|
|
454
|
-
pendingLayout = void 0;
|
|
455
|
-
try {
|
|
456
|
-
const response = await operate({
|
|
457
|
-
type: "layout",
|
|
458
|
-
request: next.request
|
|
459
|
-
});
|
|
460
|
-
if (!response.layout) throw new BrowserRenderError("render-failed", "LibreOffice returned no layout result.");
|
|
461
|
-
for (const waiter of next.waiters) waiter.resolve(response.layout);
|
|
462
|
-
} catch (error) {
|
|
463
|
-
for (const waiter of next.waiters) waiter.reject(error);
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
layoutRunning = false;
|
|
467
|
-
}
|
|
468
|
-
const close = async () => {
|
|
469
|
-
if (record.closed) return;
|
|
470
|
-
record.closed = true;
|
|
471
|
-
record.listeners.clear();
|
|
472
|
-
current = void 0;
|
|
473
|
-
await worker.closeDocument();
|
|
474
|
-
};
|
|
475
|
-
let disposal;
|
|
476
|
-
return {
|
|
477
|
-
get state() {
|
|
478
|
-
return reading;
|
|
479
|
-
},
|
|
480
|
-
subscribe(listener) {
|
|
481
|
-
if (record.closed) return () => {};
|
|
482
|
-
record.listeners.add(listener);
|
|
483
|
-
return () => {
|
|
484
|
-
record.listeners.delete(listener);
|
|
485
|
-
};
|
|
486
|
-
},
|
|
487
|
-
async pointer(event) {
|
|
488
|
-
await operate({
|
|
489
|
-
type: "pointer",
|
|
490
|
-
event
|
|
491
|
-
});
|
|
492
|
-
},
|
|
493
|
-
async navigate(event) {
|
|
494
|
-
await operate({
|
|
495
|
-
type: "navigate",
|
|
496
|
-
event
|
|
497
|
-
});
|
|
498
|
-
},
|
|
499
|
-
async selectAll() {
|
|
500
|
-
await operate({ type: "select-all" });
|
|
501
|
-
},
|
|
502
|
-
async copy() {
|
|
503
|
-
return (await operate({ type: "copy" })).text ?? "";
|
|
504
|
-
},
|
|
505
|
-
async goToCell(address) {
|
|
506
|
-
await operate({
|
|
507
|
-
type: "cell",
|
|
508
|
-
address
|
|
509
|
-
});
|
|
510
|
-
},
|
|
511
|
-
async setPart(part) {
|
|
512
|
-
await operate({
|
|
513
|
-
type: "part",
|
|
514
|
-
part
|
|
515
|
-
});
|
|
516
|
-
},
|
|
517
|
-
async setViewport(rectangle, scale) {
|
|
518
|
-
await operate({
|
|
519
|
-
type: "viewport",
|
|
520
|
-
rectangle,
|
|
521
|
-
scale
|
|
522
|
-
});
|
|
523
|
-
},
|
|
524
|
-
setLayout(request) {
|
|
525
|
-
const waiter = Promise.withResolvers();
|
|
526
|
-
pendingLayout = {
|
|
527
|
-
request,
|
|
528
|
-
waiters: [...pendingLayout?.waiters ?? [], waiter]
|
|
529
|
-
};
|
|
530
|
-
if (!layoutRunning) applyLayouts();
|
|
531
|
-
return waiter.promise;
|
|
532
|
-
},
|
|
533
|
-
async renderTile(request, tileSignal) {
|
|
534
|
-
if (record.closed) throw new BrowserRenderError("disposed", "Office document is closed.");
|
|
535
|
-
const response = await worker.request({
|
|
536
|
-
type: "office-tile",
|
|
537
|
-
request
|
|
538
|
-
}, tileSignal);
|
|
539
|
-
if (response.type !== "tile") throw new BrowserRenderError("render-failed", "LibreOffice returned an unexpected tile response.");
|
|
540
|
-
return response.tile;
|
|
541
|
-
},
|
|
542
|
-
async captureReadingState() {
|
|
543
|
-
if (record.closed) throw new BrowserRenderError("disposed", "Office document is closed.");
|
|
544
|
-
const response = await worker.request({ type: "reading-state" });
|
|
545
|
-
if (response.type !== "reading-state") throw new BrowserRenderError("render-failed", "LibreOffice returned an unexpected reading state.");
|
|
546
|
-
return response.state;
|
|
547
|
-
},
|
|
548
|
-
close,
|
|
549
|
-
dispose() {
|
|
550
|
-
return disposal ??= close().then(disposeRuntime);
|
|
551
|
-
}
|
|
552
|
-
};
|
|
553
|
-
};
|
|
554
|
-
const disposeRuntime = async () => {
|
|
555
|
-
if (disposed) return;
|
|
556
|
-
disposed = true;
|
|
557
|
-
current?.listeners.clear();
|
|
558
|
-
if (current && !current.closed) current.closed = true;
|
|
559
|
-
await worker.dispose();
|
|
560
|
-
};
|
|
561
|
-
return {
|
|
562
|
-
open,
|
|
563
|
-
dispose: disposeRuntime
|
|
564
|
-
};
|
|
565
|
-
}
|
|
566
|
-
/**
|
|
567
|
-
* Open one read-only Office document in its own engine; the runtime ends with the document.
|
|
568
|
-
* @param options - Source bytes, engine resources and Host fonts.
|
|
569
|
-
* @param signal - Document lifetime; abort closes its Worker and pthreads.
|
|
570
|
-
* @returns Immutable reading state and disposal.
|
|
571
|
-
*/
|
|
572
|
-
async function openOfficeDocument(options, signal) {
|
|
573
|
-
const { data, extension, resolveFonts, onMissingFonts, ...runtime } = options;
|
|
574
|
-
const office = await openOfficeRuntime(runtime, signal);
|
|
575
|
-
try {
|
|
576
|
-
return await office.open({
|
|
577
|
-
data,
|
|
578
|
-
extension,
|
|
579
|
-
resolveFonts,
|
|
580
|
-
...onMissingFonts === void 0 ? {} : { onMissingFonts }
|
|
581
|
-
}, signal);
|
|
582
|
-
} catch (error) {
|
|
583
|
-
await office.dispose();
|
|
584
|
-
throw error;
|
|
585
|
-
}
|
|
586
|
-
}
|
|
587
|
-
//#endregion
|
|
588
|
-
//#region lib/types/index.js
|
|
589
|
-
/** Browser Office preview and retained reading entry points. */
|
|
590
|
-
/**
|
|
591
|
-
* Open a read-only document; each call owns an independent Worker.
|
|
592
|
-
* @param options - Source bytes, version-matched resources and Host fonts.
|
|
593
|
-
* @param signal - Document lifetime; abort awaits engine teardown.
|
|
594
|
-
* @returns Immutable geometry, serialized tile rendering and disposal.
|
|
595
|
-
*/
|
|
596
|
-
async function openDocument(options, signal) {
|
|
597
|
-
const connection = await openBrowserConnection(options, signal);
|
|
598
|
-
let queue = Promise.resolve();
|
|
599
|
-
return {
|
|
600
|
-
pages: Object.freeze(connection.opened.pages.map((page) => Object.freeze(page))),
|
|
601
|
-
renderTile(request, tileSignal) {
|
|
602
|
-
if (tileSignal?.aborted) return Promise.reject(tileSignal.reason);
|
|
603
|
-
const task = queue.catch(() => void 0).then(async () => {
|
|
604
|
-
tileSignal?.throwIfAborted();
|
|
605
|
-
const response = await connection.request({
|
|
606
|
-
type: "tile",
|
|
607
|
-
request
|
|
608
|
-
}, tileSignal);
|
|
609
|
-
if (response.type !== "tile") throw new BrowserRenderError("render-failed", "LibreOffice returned an unexpected tile response.");
|
|
610
|
-
return response.tile;
|
|
611
|
-
});
|
|
612
|
-
queue = task;
|
|
613
|
-
if (!tileSignal) return task;
|
|
614
|
-
return new Promise((resolve, reject) => {
|
|
615
|
-
const abort = () => {
|
|
616
|
-
reject(tileSignal.reason);
|
|
617
|
-
};
|
|
618
|
-
tileSignal.addEventListener("abort", abort, { once: true });
|
|
619
|
-
task.then(resolve, reject).finally(() => tileSignal.removeEventListener("abort", abort));
|
|
620
|
-
});
|
|
621
|
-
},
|
|
622
|
-
dispose: connection.dispose
|
|
623
|
-
};
|
|
624
|
-
}
|
|
625
|
-
//#endregion
|
|
626
|
-
export { BrowserRenderError, openDocument, openOfficeDocument, openOfficeRuntime, prepareOfficeBrowser };
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
import type { OwnerRequest, WorkerDocumentOptions, WorkerMessage } from './protocol.ts';
|
|
2
|
-
import type { BrowserDocumentOptions, BrowserRuntimeOptions } from './types.ts';
|
|
3
|
-
import type { OfficeDocumentEvent } from './office-types.ts';
|
|
4
|
-
/** One engine Worker that owns a LibreOffice instance and opens one document at a time. */
|
|
5
|
-
export interface BrowserWorker {
|
|
6
|
-
/** Load a document, replacing any document this Worker still holds. */
|
|
7
|
-
openDocument(options: WorkerDocumentOptions & Pick<BrowserDocumentOptions, 'resolveFonts' | 'onMissingFonts'>, signal?: AbortSignal): Promise<Extract<WorkerMessage, {
|
|
8
|
-
type: 'opened';
|
|
9
|
-
}>>;
|
|
10
|
-
/** Release the open document; the engine and its pthreads stay initialized. */
|
|
11
|
-
closeDocument(): Promise<void>;
|
|
12
|
-
/** Send one typed request owned by the open document. */
|
|
13
|
-
request(message: OwnerRequest, operation?: AbortSignal, transfer?: Transferable[]): Promise<WorkerMessage>;
|
|
14
|
-
/** Close the document and terminate the Worker. */
|
|
15
|
-
dispose(): Promise<void>;
|
|
16
|
-
}
|
|
17
|
-
/**
|
|
18
|
-
* Start one LibreOffice Worker without loading a document.
|
|
19
|
-
* @param options - Engine resources, limits and asynchronous Host font provider.
|
|
20
|
-
* @param signal - Engine lifetime; abort closes its Worker and all pending work.
|
|
21
|
-
* @param onEvent - Reading notifications, including fatal Worker failures.
|
|
22
|
-
* @returns Document loading, Worker requests and joined disposal.
|
|
23
|
-
*/
|
|
24
|
-
export declare function openBrowserWorker(options: BrowserRuntimeOptions, signal?: AbortSignal, onEvent?: (event: OfficeDocumentEvent) => void): Promise<BrowserWorker>;
|
|
25
|
-
/**
|
|
26
|
-
* Load original PDF bytes in a dedicated Worker without Draw import.
|
|
27
|
-
* @param options - Resources, limits, source bytes and asynchronous Host font provider.
|
|
28
|
-
* @param signal - Document lifetime; abort closes its Worker and all pending work.
|
|
29
|
-
* @param onEvent - Reading notifications, including fatal Worker failures.
|
|
30
|
-
* @returns Initial geometry, ordered Worker requests and joined disposal.
|
|
31
|
-
*/
|
|
32
|
-
export declare function openBrowserConnection(options: BrowserDocumentOptions, signal?: AbortSignal, onEvent?: (event: OfficeDocumentEvent) => void): Promise<{
|
|
33
|
-
opened: {
|
|
34
|
-
readonly type: "opened";
|
|
35
|
-
readonly id: number;
|
|
36
|
-
readonly pages: readonly import("./types.ts").BrowserPage[];
|
|
37
|
-
readonly office?: import("./office-types.ts").OfficeDocumentState;
|
|
38
|
-
};
|
|
39
|
-
request: (message: OwnerRequest, operation?: AbortSignal, transfer?: Transferable[]) => Promise<WorkerMessage>;
|
|
40
|
-
dispose: () => Promise<void>;
|
|
41
|
-
}>;
|
|
42
|
-
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
/** Emscripten operations used by the single owning engine Worker. */
|
|
2
|
-
export interface EmscriptenModule {
|
|
3
|
-
readonly FS: {
|
|
4
|
-
mkdirTree(path: string): void;
|
|
5
|
-
writeFile(path: string, data: Uint8Array): void;
|
|
6
|
-
readFile(path: string): Uint8Array<ArrayBuffer>;
|
|
7
|
-
unlink(path: string): void;
|
|
8
|
-
};
|
|
9
|
-
readonly ENV: Record<string, string>;
|
|
10
|
-
readonly HEAPU32: Uint32Array;
|
|
11
|
-
readonly PThread: {
|
|
12
|
-
terminateAllThreads(): void;
|
|
13
|
-
};
|
|
14
|
-
ccall(name: string, returnType: string | null, argTypes: string[], args: unknown[]): number;
|
|
15
|
-
UTF8ToString(pointer: number): string;
|
|
16
|
-
}
|
|
17
|
-
//# sourceMappingURL=engine-types.d.ts.map
|