@deepseek-ai/libreoffice-kit 0.0.2-rc7 → 0.0.2-rc9
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/README.md +1 -1
- package/README.zh.md +1 -1
- package/lib/browser/assets.json +2 -2
- package/lib/browser/index.js +297 -162
- package/lib/browser/types/connection.d.ts +28 -6
- package/lib/browser/types/font-channel.d.ts +16 -1
- package/lib/browser/types/index.d.ts +2 -2
- package/lib/browser/types/office-engine.d.ts +10 -2
- package/lib/browser/types/office-types.d.ts +60 -1
- package/lib/browser/types/office.d.ts +18 -3
- package/lib/browser/types/protocol.d.ts +32 -5
- package/lib/browser/types/types.d.ts +2 -0
- package/lib/browser/worker.js +286 -105
- package/lib/browser-assets.js +1 -1
- package/lib/cli.js +2 -2
- package/lib/index.js +2 -2
- package/lib/types/engine.d.ts +1 -1
- package/package.json +2 -2
- package/sources/browser/packages/browser/package.json +1 -1
- package/sources/browser/packages/browser/src/connection.ts +103 -37
- package/sources/browser/packages/browser/src/font-channel.ts +15 -6
- package/sources/browser/packages/browser/src/index.ts +2 -2
- package/sources/browser/packages/browser/src/office-engine.ts +80 -6
- package/sources/browser/packages/browser/src/office-types.ts +52 -1
- package/sources/browser/packages/browser/src/office.ts +118 -52
- package/sources/browser/packages/browser/src/protocol.ts +18 -4
- package/sources/browser/packages/browser/src/types.ts +3 -0
- package/sources/browser/packages/browser/src/worker.ts +132 -53
- package/sources/browser/scripts/build-browser.mjs +6 -2
package/README.md
CHANGED
|
@@ -60,7 +60,7 @@ PDFium is an experimental direct raster path, separate from PDF.js and Office re
|
|
|
60
60
|
|
|
61
61
|
## Browser and font entries
|
|
62
62
|
|
|
63
|
-
`@deepseek-ai/libreoffice-kit/browser` provides
|
|
63
|
+
`@deepseek-ai/libreoffice-kit/browser` provides read-only Office sessions through `openOfficeDocument` and reusable one-document-at-a-time engines through `openOfficeRuntime`. Documents can capture and restore typed reading state while applications retain any display snapshot. Writer paginated/continuous layouts, selection/copy and standalone PDFium viewing remain available. The browser API has no editing, snapshot-save or browser-capture operation; Node/CLI conversion and recalculation retain their explicit output APIs. `@deepseek-ai/libreoffice-kit/browser-assets` resolves the local browser Worker and validates the assets in the exact-version WASM dependency. Host asset servers expose these resources as opaque URLs; the main package does not duplicate the LibreOffice payload.
|
|
64
64
|
|
|
65
65
|
`./fonts` provides the Host font service described above. Browser-safe `./font-config` and `./document-inspection` share font configuration and input validation with the Node API. `./internal/*` exports are implementation details used by the bundled browser Worker.
|
|
66
66
|
|
package/README.zh.md
CHANGED
|
@@ -49,7 +49,7 @@ PDFium 直接图片渲染仍为实验能力,与 PDF.js 和 Office 阅读会话
|
|
|
49
49
|
|
|
50
50
|
## 浏览器与字体入口
|
|
51
51
|
|
|
52
|
-
`@deepseek-ai/libreoffice-kit/browser` 通过 `openOfficeDocument`
|
|
52
|
+
`@deepseek-ai/libreoffice-kit/browser` 通过 `openOfficeDocument` 提供只读 Office 文档,并通过 `openOfficeRuntime` 提供一次只打开一个文档、可复用的运行时。文档可以捕获和恢复类型化阅读状态,显示快照仍由应用持有。Writer 分页/连续重排、选择复制和独立 PDFium 查看继续可用。浏览器 API 不提供编辑、OOXML 保存快照或浏览器画面捕获;Node/CLI 的转换和重算仍保留显式输出能力。Node 端 `./browser-assets` 校验并解析主包中的 Worker,以及精确版本 WASM 依赖中的资源。Host 将其映射成资源 URL;主包不复制 LibreOffice 大文件。
|
|
53
53
|
|
|
54
54
|
`./fonts` 提供上文的字体服务。`./font-config` 与 `./document-inspection` 为浏览器和 Node 共用的纯逻辑;`./internal/*` 仅用于内部实现。字体子集采用 Unicode 17 数据和 HarfBuzz 布局闭包,内存缓存淘汰后从未变化的原字体重新生成。
|
|
55
55
|
|
package/lib/browser/assets.json
CHANGED
|
@@ -3,8 +3,8 @@
|
|
|
3
3
|
"files": {
|
|
4
4
|
"worker": {
|
|
5
5
|
"path": "lib/browser/worker.js",
|
|
6
|
-
"bytes":
|
|
7
|
-
"sha256": "
|
|
6
|
+
"bytes": 127167,
|
|
7
|
+
"sha256": "eb76230173a37f65af3dda31c515199de6520c534242c7fdffab4958c5fbf17b"
|
|
8
8
|
}
|
|
9
9
|
}
|
|
10
10
|
}
|
package/lib/browser/index.js
CHANGED
|
@@ -97,14 +97,15 @@ function preparedBrowserModule(prepared, assets) {
|
|
|
97
97
|
}
|
|
98
98
|
//#endregion
|
|
99
99
|
//#region lib/types/connection.js
|
|
100
|
-
/** Browser
|
|
100
|
+
/** Browser Worker ownership, document lifetime, cancellation and asynchronous Host font delivery. */
|
|
101
101
|
/**
|
|
102
|
-
*
|
|
103
|
-
* @param options -
|
|
104
|
-
* @param signal -
|
|
105
|
-
* @
|
|
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.
|
|
106
107
|
*/
|
|
107
|
-
async function
|
|
108
|
+
async function openBrowserWorker(options, signal, onEvent) {
|
|
108
109
|
signal?.throwIfAborted();
|
|
109
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.");
|
|
110
111
|
if (![
|
|
@@ -113,14 +114,6 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
113
114
|
options.maxArchiveEntries,
|
|
114
115
|
options.maxUncompressedBytes
|
|
115
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.");
|
|
116
|
-
if (!(office ? [
|
|
117
|
-
"doc",
|
|
118
|
-
"docx",
|
|
119
|
-
"xls",
|
|
120
|
-
"xlsx",
|
|
121
|
-
"ppt",
|
|
122
|
-
"pptx"
|
|
123
|
-
] : ["pdf"]).includes(options.extension) || options.data.byteLength === 0) throw new BrowserRenderError("invalid-document", "The document is empty or has an unsupported extension.");
|
|
124
117
|
if (options.preparedEngine !== void 0) preparedBrowserModule(options.preparedEngine, options.assets);
|
|
125
118
|
const worker = new Worker(options.assets.workerUrl, { name: "libreoffice-document" });
|
|
126
119
|
const channel = new SharedArrayBuffer(1048584);
|
|
@@ -131,7 +124,8 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
131
124
|
let closed = false;
|
|
132
125
|
let disposal;
|
|
133
126
|
let frames;
|
|
134
|
-
let
|
|
127
|
+
let resolveFonts;
|
|
128
|
+
let onMissingFonts;
|
|
135
129
|
function failPending(error) {
|
|
136
130
|
for (const entry of pending.values()) entry.reject(error);
|
|
137
131
|
pending.clear();
|
|
@@ -172,7 +166,11 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
172
166
|
return;
|
|
173
167
|
case "font":
|
|
174
168
|
if (closed) return;
|
|
175
|
-
|
|
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) => {
|
|
176
174
|
if (closed) return;
|
|
177
175
|
frames = fontFrames(result, message.known);
|
|
178
176
|
pumpFonts();
|
|
@@ -182,7 +180,7 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
182
180
|
pumpFonts();
|
|
183
181
|
return;
|
|
184
182
|
case "missing-fonts":
|
|
185
|
-
if (!closed)
|
|
183
|
+
if (!closed) onMissingFonts?.(message.families);
|
|
186
184
|
return;
|
|
187
185
|
default: {
|
|
188
186
|
const entry = pending.get(message.id);
|
|
@@ -259,6 +257,8 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
259
257
|
signal?.removeEventListener("abort", aborted);
|
|
260
258
|
lifetime.abort();
|
|
261
259
|
frames = void 0;
|
|
260
|
+
resolveFonts = void 0;
|
|
261
|
+
onMissingFonts = void 0;
|
|
262
262
|
Atomics.store(control, 0, 5);
|
|
263
263
|
Atomics.notify(control, 0);
|
|
264
264
|
failPending(new BrowserRenderError("disposed", "The document is closed."));
|
|
@@ -281,173 +281,308 @@ async function openBrowserConnection(options, signal, office = false, onEvent) {
|
|
|
281
281
|
dispose();
|
|
282
282
|
}
|
|
283
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
|
+
};
|
|
284
309
|
try {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
data,
|
|
288
|
-
extension: options.extension,
|
|
289
|
-
assets: options.assets,
|
|
290
|
-
...options.preparedEngine === void 0 ? {} : { preparedEngine: options.preparedEngine },
|
|
291
|
-
timeoutMs: options.timeoutMs,
|
|
292
|
-
maxLoadedFontBytes: options.maxLoadedFontBytes,
|
|
293
|
-
fontFallbacks: options.fontFallbacks,
|
|
294
|
-
maxArchiveEntries: options.maxArchiveEntries,
|
|
295
|
-
maxUncompressedBytes: options.maxUncompressedBytes
|
|
296
|
-
};
|
|
297
|
-
const result = await post({
|
|
298
|
-
type: "open",
|
|
310
|
+
if ((await post({
|
|
311
|
+
type: "start",
|
|
299
312
|
id: ++sequence,
|
|
300
313
|
options: engineOptions,
|
|
301
|
-
channel
|
|
302
|
-
|
|
303
|
-
}, [data.buffer]);
|
|
304
|
-
if (result.type !== "opened") throw new BrowserRenderError("invalid-document", "LibreOffice returned an unexpected load response.");
|
|
305
|
-
opened = result;
|
|
306
|
-
if (opened.type !== "opened" || opened.pages.length === 0) throw new BrowserRenderError("invalid-document", "LibreOffice returned no document pages.");
|
|
314
|
+
channel
|
|
315
|
+
})).type !== "started") throw new BrowserRenderError("unavailable", "LibreOffice returned an unexpected start response.");
|
|
307
316
|
signal?.throwIfAborted();
|
|
308
|
-
} catch (
|
|
317
|
+
} catch (failure) {
|
|
309
318
|
await dispose();
|
|
310
319
|
signal?.throwIfAborted();
|
|
311
|
-
throw
|
|
320
|
+
throw failure;
|
|
312
321
|
}
|
|
313
322
|
return {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
if (
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
+
}
|
|
330
364
|
},
|
|
365
|
+
request,
|
|
331
366
|
dispose
|
|
332
367
|
};
|
|
333
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
|
+
}
|
|
334
396
|
//#endregion
|
|
335
397
|
//#region lib/types/office.js
|
|
336
|
-
/** Browser owner for
|
|
337
|
-
/**
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
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 {
|
|
344
411
|
listener(event);
|
|
345
412
|
} catch (error) {
|
|
346
413
|
console.error("Office document observer failed", error);
|
|
347
414
|
}
|
|
348
415
|
});
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
const
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
try {
|
|
370
|
-
const response = await operate({
|
|
371
|
-
type: "layout",
|
|
372
|
-
request: next.request
|
|
373
|
-
});
|
|
374
|
-
if (!response.layout) throw new BrowserRenderError("render-failed", "LibreOffice returned no layout result.");
|
|
375
|
-
for (const waiter of next.waiters) waiter.resolve(response.layout);
|
|
376
|
-
} catch (error) {
|
|
377
|
-
for (const waiter of next.waiters) waiter.reject(error);
|
|
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;
|
|
378
436
|
}
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
subscribe(listener) {
|
|
387
|
-
listeners.add(listener);
|
|
388
|
-
return () => {
|
|
389
|
-
listeners.delete(listener);
|
|
390
|
-
};
|
|
391
|
-
},
|
|
392
|
-
async pointer(event) {
|
|
393
|
-
await operate({
|
|
394
|
-
type: "pointer",
|
|
395
|
-
event
|
|
396
|
-
});
|
|
397
|
-
},
|
|
398
|
-
async navigate(event) {
|
|
399
|
-
await operate({
|
|
400
|
-
type: "navigate",
|
|
401
|
-
event
|
|
402
|
-
});
|
|
403
|
-
},
|
|
404
|
-
async selectAll() {
|
|
405
|
-
await operate({ type: "select-all" });
|
|
406
|
-
},
|
|
407
|
-
async copy() {
|
|
408
|
-
return (await operate({ type: "copy" })).text ?? "";
|
|
409
|
-
},
|
|
410
|
-
async goToCell(address) {
|
|
411
|
-
await operate({
|
|
412
|
-
type: "cell",
|
|
413
|
-
address
|
|
414
|
-
});
|
|
415
|
-
},
|
|
416
|
-
async setPart(part) {
|
|
417
|
-
await operate({
|
|
418
|
-
type: "part",
|
|
419
|
-
part
|
|
420
|
-
});
|
|
421
|
-
},
|
|
422
|
-
async setViewport(rectangle, scale) {
|
|
423
|
-
await operate({
|
|
424
|
-
type: "viewport",
|
|
425
|
-
rectangle,
|
|
426
|
-
scale
|
|
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
|
|
427
444
|
});
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
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;
|
|
449
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
|
+
};
|
|
450
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
|
+
}
|
|
451
586
|
}
|
|
452
587
|
//#endregion
|
|
453
588
|
//#region lib/types/index.js
|
|
@@ -488,4 +623,4 @@ async function openDocument(options, signal) {
|
|
|
488
623
|
};
|
|
489
624
|
}
|
|
490
625
|
//#endregion
|
|
491
|
-
export { BrowserRenderError, openDocument, openOfficeDocument, prepareOfficeBrowser };
|
|
626
|
+
export { BrowserRenderError, openDocument, openOfficeDocument, openOfficeRuntime, prepareOfficeBrowser };
|
|
@@ -1,20 +1,42 @@
|
|
|
1
|
-
import type { OwnerRequest, WorkerMessage } from './protocol.ts';
|
|
2
|
-
import type { BrowserDocumentOptions } from './types.ts';
|
|
3
|
-
import type { OfficeDocumentEvent
|
|
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
|
+
}
|
|
4
17
|
/**
|
|
5
|
-
*
|
|
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.
|
|
6
27
|
* @param options - Resources, limits, source bytes and asynchronous Host font provider.
|
|
7
28
|
* @param signal - Document lifetime; abort closes its Worker and all pending work.
|
|
29
|
+
* @param onEvent - Reading notifications, including fatal Worker failures.
|
|
8
30
|
* @returns Initial geometry, ordered Worker requests and joined disposal.
|
|
9
31
|
*/
|
|
10
|
-
export declare function openBrowserConnection(options: BrowserDocumentOptions
|
|
32
|
+
export declare function openBrowserConnection(options: BrowserDocumentOptions, signal?: AbortSignal, onEvent?: (event: OfficeDocumentEvent) => void): Promise<{
|
|
11
33
|
opened: {
|
|
12
34
|
readonly type: "opened";
|
|
13
35
|
readonly id: number;
|
|
14
36
|
readonly pages: readonly import("./types.ts").BrowserPage[];
|
|
15
37
|
readonly office?: import("./office-types.ts").OfficeDocumentState;
|
|
16
38
|
};
|
|
17
|
-
request(message: OwnerRequest, operation?: AbortSignal)
|
|
39
|
+
request: (message: OwnerRequest, operation?: AbortSignal, transfer?: Transferable[]) => Promise<WorkerMessage>;
|
|
18
40
|
dispose: () => Promise<void>;
|
|
19
41
|
}>;
|
|
20
42
|
//# sourceMappingURL=connection.d.ts.map
|
|
@@ -16,6 +16,21 @@ export interface EngineFontFace {
|
|
|
16
16
|
readonly path: string;
|
|
17
17
|
readonly family: string;
|
|
18
18
|
}
|
|
19
|
+
/** Fonts and internal aliases retained for the lifetime of one engine Worker. */
|
|
20
|
+
export interface EngineFontCache {
|
|
21
|
+
readonly installed: Map<string, {
|
|
22
|
+
face: EngineFontFace;
|
|
23
|
+
bytes: number;
|
|
24
|
+
format: FontIdentity['format'];
|
|
25
|
+
}>;
|
|
26
|
+
readonly aliases: Map<string, {
|
|
27
|
+
id: string;
|
|
28
|
+
family: string;
|
|
29
|
+
}>;
|
|
30
|
+
loadedBytes: number;
|
|
31
|
+
}
|
|
32
|
+
/** @returns Empty runtime-owned font state shared by successive documents. */
|
|
33
|
+
export declare function createEngineFontCache(): EngineFontCache;
|
|
19
34
|
/** The synchronous reader installs immutable font assets before returning their MEMFS paths and aliases. */
|
|
20
|
-
export declare function createFontReader(channel: SharedArrayBuffer, timeoutMs: number, maxLoadedFontBytes: number, declaredFamilies: ReadonlyMap<string, string>, request: (request: BrowserFontRequest, known: readonly FontIdentity[]) => void, next: () => void, install: (path: string, bytes: Uint8Array) => void, missing: (families: readonly string[]) => void, documentCodePoints?: readonly number[]): (request: BrowserFontRequest) => EngineFontFace[];
|
|
35
|
+
export declare function createFontReader(channel: SharedArrayBuffer, timeoutMs: number, maxLoadedFontBytes: number, declaredFamilies: ReadonlyMap<string, string>, request: (request: BrowserFontRequest, known: readonly FontIdentity[]) => void, next: () => void, install: (path: string, bytes: Uint8Array) => void, missing: (families: readonly string[]) => void, documentCodePoints?: readonly number[], cache?: EngineFontCache): (request: BrowserFontRequest) => EngineFontFace[];
|
|
21
36
|
//# sourceMappingURL=font-channel.d.ts.map
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import type { BrowserDocument, BrowserDocumentOptions } from './types.ts';
|
|
2
2
|
export { BrowserRenderError } from './types.ts';
|
|
3
|
-
export { openOfficeDocument } from './office.ts';
|
|
3
|
+
export { openOfficeDocument, openOfficeRuntime } from './office.ts';
|
|
4
4
|
export { prepareOfficeBrowser } from './preparation.ts';
|
|
5
5
|
export type * from './office-types.ts';
|
|
6
|
-
export type { BrowserDocument, BrowserDocumentOptions, BrowserEngineAssets, BrowserFontIdentity, BrowserFontRequest, BrowserFontResult, BrowserPage, BrowserRenderErrorCode, BrowserResolvedFont, BrowserTile, BrowserTileRequest, PreparedBrowserEngine } from './types.ts';
|
|
6
|
+
export type { BrowserDocument, BrowserDocumentOptions, BrowserEngineAssets, BrowserFontIdentity, BrowserFontRequest, BrowserFontResult, BrowserPage, BrowserRenderErrorCode, BrowserResolvedFont, BrowserRuntimeOptions, BrowserTile, BrowserTileRequest, PreparedBrowserEngine } from './types.ts';
|
|
7
7
|
/**
|
|
8
8
|
* Open a read-only document; each call owns an independent Worker.
|
|
9
9
|
* @param options - Source bytes, version-matched resources and Host fonts.
|