@deepseek-ai/libreoffice-kit 0.0.1 → 0.0.2-rc4

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.
Files changed (90) hide show
  1. package/NOTICE +2 -0
  2. package/README.md +43 -47
  3. package/README.zh.md +35 -50
  4. package/assets/font-subset.json +70 -0
  5. package/assets/font-subset.wasm +0 -0
  6. package/lib/browser/assets.json +10 -0
  7. package/lib/browser/index.js +435 -0
  8. package/lib/browser/types/connection.d.ts +20 -0
  9. package/lib/browser/types/editor-engine.d.ts +50 -0
  10. package/lib/browser/types/editor-types.d.ts +141 -0
  11. package/lib/browser/types/editor.d.ts +9 -0
  12. package/lib/browser/types/engine-rendering.d.ts +3 -0
  13. package/lib/browser/types/engine-types.d.ts +17 -0
  14. package/lib/browser/types/font-channel.d.ts +20 -0
  15. package/lib/browser/types/index.d.ts +13 -0
  16. package/lib/browser/types/protocol.d.ts +117 -0
  17. package/lib/browser/types/rendering.d.ts +4 -0
  18. package/lib/browser/types/types.d.ts +90 -0
  19. package/lib/browser/types/worker.d.ts +2 -0
  20. package/lib/browser/worker.js +3711 -0
  21. package/lib/browser-assets.js +119 -0
  22. package/lib/cli.js +1303 -0
  23. package/lib/document.js +178 -0
  24. package/lib/engine-rendering.js +120 -0
  25. package/lib/font-config.js +46 -0
  26. package/lib/font-source.js +344 -0
  27. package/lib/font-worker.js +712 -0
  28. package/lib/index.js +319 -109
  29. package/lib/rendering.js +99 -0
  30. package/lib/sheet-geometry.js +111 -0
  31. package/lib/types/browser-assets.d.ts +15 -0
  32. package/lib/types/cli.d.ts +3 -0
  33. package/lib/types/document.d.ts +1 -1
  34. package/lib/types/engine-rendering.d.ts +15 -0
  35. package/lib/types/engine.d.ts +5 -5
  36. package/lib/types/font-config.d.ts +24 -0
  37. package/lib/types/font-full.d.ts +13 -0
  38. package/lib/types/font-loader.d.ts +1 -7
  39. package/lib/types/font-request.d.ts +12 -0
  40. package/lib/types/font-source-types.d.ts +82 -0
  41. package/lib/types/font-source.d.ts +10 -0
  42. package/lib/types/font-subsets.d.ts +35 -0
  43. package/lib/types/font-worker.d.ts +2 -0
  44. package/lib/types/fonts.d.ts +9 -29
  45. package/lib/types/harfbuzz-subset.d.ts +11 -0
  46. package/lib/types/image-operations.d.ts +58 -0
  47. package/lib/types/image-renderer.d.ts +6 -0
  48. package/lib/types/index.d.ts +25 -2
  49. package/lib/types/native.d.ts +4 -2
  50. package/lib/types/operations.d.ts +41 -0
  51. package/lib/types/png.d.ts +2 -0
  52. package/lib/types/profile.d.ts +8 -0
  53. package/lib/types/rendering.d.ts +41 -0
  54. package/lib/types/runtime.d.ts +14 -0
  55. package/lib/types/sheet-geometry.d.ts +38 -0
  56. package/lib/types/unicode-scripts.d.ts +18 -0
  57. package/lib/types/wasm.d.ts +76 -1
  58. package/lib/types/worker.d.ts +3 -0
  59. package/lib/worker.js +1012 -237
  60. package/licenses/font-subset/harfbuzz-COPYING +42 -0
  61. package/licenses/font-subset/harfbuzzjs-LICENSE +19 -0
  62. package/licenses/javascript/fflate-LICENSE +21 -0
  63. package/licenses/javascript/saxes-LICENSE +64 -0
  64. package/licenses/javascript/xmlchars-LICENSE +18 -0
  65. package/package.json +59 -12
  66. package/sources/browser/packages/browser/package.json +22 -0
  67. package/sources/browser/packages/browser/src/connection.ts +168 -0
  68. package/sources/browser/packages/browser/src/editor-engine.ts +382 -0
  69. package/sources/browser/packages/browser/src/editor-types.ts +106 -0
  70. package/sources/browser/packages/browser/src/editor.ts +55 -0
  71. package/sources/browser/packages/browser/src/engine-rendering.ts +2 -0
  72. package/sources/browser/packages/browser/src/engine-types.ts +14 -0
  73. package/sources/browser/packages/browser/src/font-channel.ts +99 -0
  74. package/sources/browser/packages/browser/src/index.ts +39 -0
  75. package/sources/browser/packages/browser/src/protocol.ts +40 -0
  76. package/sources/browser/packages/browser/src/rendering.ts +3 -0
  77. package/sources/browser/packages/browser/src/types.ts +92 -0
  78. package/sources/browser/packages/browser/src/worker.ts +171 -0
  79. package/sources/browser/packages/browser/tsconfig.json +5 -0
  80. package/sources/browser/packages/browser/tsdown.config.ts +7 -0
  81. package/sources/browser/scripts/build-browser.mjs +74 -0
  82. package/sources/font-subset/engine/font-subset/README.md +23 -0
  83. package/sources/font-subset/engine/font-subset/README.zh.md +23 -0
  84. package/sources/font-subset/engine/font-subset/source.json +16 -0
  85. package/sources/font-subset/harfbuzzjs/config-override-subset.h +7 -0
  86. package/sources/font-subset/harfbuzzjs/harfbuzz-subset.symbols +34 -0
  87. package/sources/font-subset/scripts/build-font-subset.mjs +64 -0
  88. package/sources/font-subset/scripts/pack-utils.mjs +69 -0
  89. package/sources/font-subset/scripts/platform-matrix.mjs +100 -0
  90. package/sources/font-subset/scripts/verify-artifacts.mjs +260 -0
@@ -0,0 +1,42 @@
1
+ HarfBuzz is licensed under the so-called "Old MIT" license. Details follow.
2
+ For parts of HarfBuzz that are licensed under different licenses see individual
3
+ files names COPYING in subdirectories where applicable.
4
+
5
+ Copyright © 2010-2022 Google, Inc.
6
+ Copyright © 2015-2020 Ebrahim Byagowi
7
+ Copyright © 2019,2020 Facebook, Inc.
8
+ Copyright © 2012,2015 Mozilla Foundation
9
+ Copyright © 2011 Codethink Limited
10
+ Copyright © 2008,2010 Nokia Corporation and/or its subsidiary(-ies)
11
+ Copyright © 2009 Keith Stribley
12
+ Copyright © 2011 Martin Hosken and SIL International
13
+ Copyright © 2007 Chris Wilson
14
+ Copyright © 2005,2006,2020,2021,2022,2023 Behdad Esfahbod
15
+ Copyright © 2004,2007,2008,2009,2010,2013,2021,2022,2023 Red Hat, Inc.
16
+ Copyright © 1998-2005 David Turner and Werner Lemberg
17
+ Copyright © 2016 Igalia S.L.
18
+ Copyright © 2022 Matthias Clasen
19
+ Copyright © 2018,2021 Khaled Hosny
20
+ Copyright © 2018,2019,2020 Adobe, Inc
21
+ Copyright © 2013-2015 Alexei Podtelezhnikov
22
+
23
+ For full copyright notices consult the individual files in the package.
24
+
25
+
26
+ Permission is hereby granted, without written agreement and without
27
+ license or royalty fees, to use, copy, modify, and distribute this
28
+ software and its documentation for any purpose, provided that the
29
+ above copyright notice and the following two paragraphs appear in
30
+ all copies of this software.
31
+
32
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
33
+ DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
34
+ ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
35
+ IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
36
+ DAMAGE.
37
+
38
+ THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
39
+ BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
40
+ FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
41
+ ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
42
+ PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
@@ -0,0 +1,19 @@
1
+ Copyright (c) 2019-2026 The harfbuzzjs project authors
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19
+ SOFTWARE.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Arjun Barrett
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,64 @@
1
+ The ISC License
2
+
3
+ Copyright (c) Contributors
4
+
5
+ Permission to use, copy, modify, and/or distribute this software for any
6
+ purpose with or without fee is hereby granted, provided that the above
7
+ copyright notice and this permission notice appear in all copies.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15
+ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
+
17
+ ====
18
+
19
+ The following license is the one that governed sax, from which saxes
20
+ was forked. Isaac Schlueter is not *directly* involved with saxes so
21
+ don't go bugging him for saxes issues.
22
+
23
+ The ISC License
24
+
25
+ Copyright (c) Isaac Z. Schlueter and Contributors
26
+
27
+ Permission to use, copy, modify, and/or distribute this software for any
28
+ purpose with or without fee is hereby granted, provided that the above
29
+ copyright notice and this permission notice appear in all copies.
30
+
31
+ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
32
+ WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
33
+ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
34
+ ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35
+ WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
36
+ ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
37
+ IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38
+
39
+ ====
40
+
41
+ `String.fromCodePoint` by Mathias Bynens is no longer used, but it can
42
+ still be found in old commits. It was once used according to terms of
43
+ MIT License, as follows:
44
+
45
+ Copyright Mathias Bynens <https://mathiasbynens.be/>
46
+
47
+ Permission is hereby granted, free of charge, to any person obtaining
48
+ a copy of this software and associated documentation files (the
49
+ "Software"), to deal in the Software without restriction, including
50
+ without limitation the rights to use, copy, modify, merge, publish,
51
+ distribute, sublicense, and/or sell copies of the Software, and to
52
+ permit persons to whom the Software is furnished to do so, subject to
53
+ the following conditions:
54
+
55
+ The above copyright notice and this permission notice shall be
56
+ included in all copies or substantial portions of the Software.
57
+
58
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
59
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
60
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
61
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
62
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
63
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
64
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,18 @@
1
+ Copyright Louis-Dominique Dubeau and contributors to xmlchars
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
4
+ this software and associated documentation files (the "Software"), to deal in
5
+ the Software without restriction, including without limitation the rights to
6
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7
+ the Software, and to permit persons to whom the Software is furnished to do so,
8
+ subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in all
11
+ copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@deepseek-ai/libreoffice-kit",
3
- "description": "Convert on-disk Office documents to PDF with prebuilt LibreOffice",
4
- "version": "0.0.1",
3
+ "description": "WASM Office editing, direct document images, and conversion for Node and browsers",
4
+ "version": "0.0.2-rc4",
5
5
  "publishConfig": {
6
6
  "access": "public"
7
7
  },
@@ -22,25 +22,72 @@
22
22
  "types": "./lib/types/index.d.ts",
23
23
  "default": "./lib/index.js"
24
24
  },
25
- "./package.json": "./package.json"
25
+ "./package.json": "./package.json",
26
+ "./fonts": {
27
+ "types": "./lib/types/font-source.d.ts",
28
+ "default": "./lib/font-source.js"
29
+ },
30
+ "./font-config": {
31
+ "types": "./lib/types/font-config.d.ts",
32
+ "default": "./lib/font-config.js"
33
+ },
34
+ "./document-inspection": {
35
+ "types": "./lib/types/document.d.ts",
36
+ "default": "./lib/document.js"
37
+ },
38
+ "./cli": "./lib/cli.js",
39
+ "./browser-assets": {
40
+ "types": "./lib/types/browser-assets.d.ts",
41
+ "default": "./lib/browser-assets.js"
42
+ },
43
+ "./internal/rendering": {
44
+ "types": "./lib/types/rendering.d.ts",
45
+ "default": "./lib/rendering.js"
46
+ },
47
+ "./internal/engine-rendering": {
48
+ "types": "./lib/types/engine-rendering.d.ts",
49
+ "default": "./lib/engine-rendering.js"
50
+ },
51
+ "./internal/sheet-geometry": {
52
+ "types": "./lib/types/sheet-geometry.d.ts",
53
+ "default": "./lib/sheet-geometry.js"
54
+ },
55
+ "./browser": {
56
+ "types": "./lib/browser/types/index.d.ts",
57
+ "import": "./lib/browser/index.js"
58
+ }
26
59
  },
27
60
  "files": [
28
61
  "README.zh.md",
29
62
  "lib/index.js",
63
+ "lib/cli.js",
30
64
  "lib/worker.js",
31
65
  "NOTICE",
32
- "lib/types/**/*.d.ts"
66
+ "lib/types/**/*.d.ts",
67
+ "lib/font-source.js",
68
+ "lib/font-worker.js",
69
+ "lib/font-config.js",
70
+ "lib/document.js",
71
+ "assets/font-subset.wasm",
72
+ "assets/font-subset.json",
73
+ "sources/font-subset/",
74
+ "licenses/font-subset/",
75
+ "lib/browser-assets.js",
76
+ "lib/rendering.js",
77
+ "lib/engine-rendering.js",
78
+ "lib/sheet-geometry.js",
79
+ "lib/browser/",
80
+ "sources/browser/",
81
+ "licenses/javascript/"
33
82
  ],
34
83
  "dependencies": {
84
+ "@unicode/unicode-17.0.0": "2.0.7",
35
85
  "fflate": "0.8.2",
36
86
  "fontkit": "2.0.4",
37
- "saxes": "6.0.0"
87
+ "saxes": "6.0.0",
88
+ "@deepseek-ai/libreoffice-kit-wasm": "0.0.2-rc4"
38
89
  },
39
- "optionalDependencies": {
40
- "@deepseek-ai/libreoffice-kit-darwin-x64": "0.0.1",
41
- "@deepseek-ai/libreoffice-kit-win32-arm64": "0.0.1",
42
- "@deepseek-ai/libreoffice-kit-win32-x64": "0.0.1",
43
- "@deepseek-ai/libreoffice-kit-wasm": "0.0.1",
44
- "@deepseek-ai/libreoffice-kit-darwin-arm64": "0.0.1"
90
+ "bin": {
91
+ "libreoffice-kit": "./lib/cli.js"
45
92
  }
46
- }
93
+ }
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "@deepseek-ai/libreoffice-kit-browser-internal",
3
+ "version": "0.0.2-rc4",
4
+ "type": "module",
5
+ "license": "MPL-2.0",
6
+ "description": "Browser Worker LibreOffice rendering and persistent OOXML editing",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/deepseek-harness/libreoffice-kit.git",
10
+ "directory": "packages/browser"
11
+ },
12
+ "exports": {
13
+ ".": {
14
+ "types": "./lib/types/index.d.ts",
15
+ "import": "./lib/index.js"
16
+ }
17
+ },
18
+ "devDependencies": {
19
+ "@deepseek-ai/libreoffice-kit": "workspace:*"
20
+ },
21
+ "private": true
22
+ }
@@ -0,0 +1,168 @@
1
+ /** Browser document ownership, cancellation and asynchronous Host font delivery. */
2
+ import { FONT_CHUNK_BYTES, FONT_HEADER_BYTES, FontState } from './protocol.ts'
3
+ import type { OwnerMessage, OwnerRequest, WorkerMessage, WorkerOptions } from './protocol.ts'
4
+ import { fontFrames, fontViews } from './font-channel.ts'
5
+ import { BrowserRenderError } from './types.ts'
6
+ import type { BrowserDocumentOptions } from './types.ts'
7
+ import type { BrowserEditorEvent, BrowserEditorOptions } from './editor-types.ts'
8
+
9
+ interface Pending {
10
+ resolve(message: WorkerMessage): void
11
+ reject(error: unknown): void
12
+ }
13
+
14
+ /**
15
+ * Load original Office bytes in a dedicated Worker with no intermediate PDF.
16
+ * @param options - Resources, limits, source bytes and asynchronous Host font provider.
17
+ * @param signal - Document lifetime; abort closes its Worker and all pending work.
18
+ * @returns Initial geometry, ordered Worker requests and joined disposal.
19
+ */
20
+ export async function openBrowserConnection(options: BrowserDocumentOptions | BrowserEditorOptions, signal?: AbortSignal,
21
+ editing = false, onEvent?: (event: BrowserEditorEvent) => void) {
22
+ signal?.throwIfAborted()
23
+ 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.')
24
+ if (![options.timeoutMs, options.maxLoadedFontBytes, options.maxArchiveEntries, options.maxUncompressedBytes].every(value => Number.isSafeInteger(value) && value > 0) || options.timeoutMs > 0x7fffffff) throw new TypeError('Document limits must be positive integers and timeoutMs must fit a browser timer.')
25
+ if (!(editing ? ['doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx'] : ['pdf']).includes(options.extension) || options.data.byteLength === 0) throw new BrowserRenderError('invalid-document', 'The document is empty or has an unsupported extension.')
26
+ if (['doc', 'xls', 'ppt'].includes(options.extension) && !('readOnly' in options && options.readOnly === true)) throw new BrowserRenderError('invalid-document', 'Legacy Office documents require readOnly mode.')
27
+ const worker = new Worker(options.assets.workerUrl, { name: 'libreoffice-document' })
28
+ const channel = new SharedArrayBuffer(FONT_HEADER_BYTES + FONT_CHUNK_BYTES)
29
+ const { control, bytes } = fontViews(channel)
30
+ const lifetime = new AbortController()
31
+ const pending = new Map<number, Pending>()
32
+ let sequence = 0
33
+ let closed = false
34
+ let disposal: Promise<void> | undefined
35
+ let frames: ReturnType<typeof fontFrames> | undefined
36
+ let opened: Extract<WorkerMessage, { type: 'opened' }> | undefined
37
+
38
+ function failPending(error: unknown): void {
39
+ for (const entry of pending.values()) entry.reject(error)
40
+ pending.clear()
41
+ }
42
+ function sendFontError(error: unknown): void {
43
+ if (closed) return
44
+ frames = undefined
45
+ const encoded = new TextEncoder().encode(error instanceof Error ? error.message : String(error)).subarray(0, FONT_CHUNK_BYTES)
46
+ bytes.set(encoded)
47
+ Atomics.store(control, 1, encoded.length)
48
+ Atomics.store(control, 0, FontState.Error)
49
+ Atomics.notify(control, 0)
50
+ }
51
+ function pumpFonts(): void {
52
+ if (closed || !frames) return
53
+ try {
54
+ const frame = frames.next()
55
+ if (frame.done) { frames = undefined; return }
56
+ bytes.set(frame.value.bytes)
57
+ Atomics.store(control, 1, frame.value.bytes.length)
58
+ Atomics.store(control, 0, frame.value.state)
59
+ Atomics.notify(control, 0)
60
+ } catch (error) { sendFontError(error) }
61
+ }
62
+ worker.onmessage = (event: MessageEvent<WorkerMessage>) => {
63
+ const message = event.data
64
+ switch (message.type) {
65
+ case 'editor-event': if (!closed) { onEvent?.(message.event); if (message.event.type === 'error') void dispose() }; return
66
+ case 'font':
67
+ if (closed) return
68
+ void Promise.resolve().then(() => options.resolveFonts(message.request, lifetime.signal)).then(result => {
69
+ if (closed) return
70
+ frames = fontFrames(result)
71
+ pumpFonts()
72
+ }, sendFontError)
73
+ return
74
+ case 'font-next': pumpFonts(); return
75
+ case 'missing-fonts': if (!closed) options.onMissingFonts?.(message.families); return
76
+ default: {
77
+ const entry = pending.get(message.id)
78
+ if (!entry) return
79
+ pending.delete(message.id)
80
+ if (message.type === 'error') {
81
+ entry.reject(new BrowserRenderError(message.code, message.message))
82
+ if (message.fatal || message.code === 'font-limit' || message.code === 'font-unavailable' || message.code === 'timeout') {
83
+ onEvent?.({ type: 'error', code: message.code, message: message.message })
84
+ void dispose()
85
+ }
86
+ } else entry.resolve(message)
87
+ }
88
+ }
89
+ }
90
+ worker.onerror = (event) => {
91
+ const error = new BrowserRenderError('render-failed', event.message || 'LibreOffice Worker failed.')
92
+ onEvent?.({ type: 'error', code: error.code, message: error.message })
93
+ failPending(error)
94
+ void dispose()
95
+ }
96
+ worker.onmessageerror = () => {
97
+ const error = new BrowserRenderError('render-failed', 'LibreOffice Worker returned an unreadable response.')
98
+ onEvent?.({ type: 'error', code: error.code, message: error.message })
99
+ failPending(error)
100
+ void dispose()
101
+ }
102
+
103
+ function post(message: OwnerMessage, transfer: Transferable[] = []): Promise<WorkerMessage> {
104
+ return new Promise((resolve, reject) => {
105
+ const timer = setTimeout(() => {
106
+ pending.delete(message.id)
107
+ const error = new BrowserRenderError('timeout', 'LibreOffice operation timed out.')
108
+ reject(error)
109
+ if (!closed) onEvent?.({ type: 'error', code: error.code, message: error.message })
110
+ void dispose()
111
+ }, options.timeoutMs)
112
+ pending.set(message.id, {
113
+ resolve(value) { clearTimeout(timer); resolve(value) },
114
+ reject(error) { clearTimeout(timer); reject(error) },
115
+ })
116
+ try { worker.postMessage(message, transfer) } catch (error) { pending.get(message.id)?.reject(error); pending.delete(message.id) }
117
+ })
118
+ }
119
+ function dispose(): Promise<void> {
120
+ if (disposal) return disposal
121
+ closed = true
122
+ signal?.removeEventListener('abort', aborted)
123
+ lifetime.abort()
124
+ frames = undefined
125
+ Atomics.store(control, 0, FontState.Cancelled)
126
+ Atomics.notify(control, 0)
127
+ failPending(new BrowserRenderError('disposed', 'The document is closed.'))
128
+ disposal = new Promise<void>((resolve) => {
129
+ // This handshake only releases owned workers; document processing has its own caller deadline.
130
+ const deadline = setTimeout(resolve, 1_000)
131
+ void post({ type: 'dispose', id: ++sequence }).then(() => resolve(), () => resolve()).finally(() => clearTimeout(deadline))
132
+ }).finally(() => { worker.terminate(); failPending(new BrowserRenderError('disposed', 'The document is closed.')); worker.onmessage = null; worker.onerror = null; worker.onmessageerror = null })
133
+ return disposal
134
+ }
135
+ function aborted(): void { void dispose() }
136
+ signal?.addEventListener('abort', aborted, { once: true })
137
+
138
+ try {
139
+ const data = new Uint8Array(options.data)
140
+ const engineOptions: WorkerOptions = { data, extension: options.extension, assets: options.assets, timeoutMs: options.timeoutMs, maxLoadedFontBytes: options.maxLoadedFontBytes, fontFallbacks: options.fontFallbacks, maxArchiveEntries: options.maxArchiveEntries, maxUncompressedBytes: options.maxUncompressedBytes,
141
+ ...('readOnly' in options && options.readOnly !== undefined ? { readOnly: options.readOnly } : {}) }
142
+ const result = await post({ type: 'open', id: ++sequence, options: engineOptions, channel, ...(editing ? { editing: true } : {}) }, [data.buffer])
143
+ if (result.type !== 'opened') throw new BrowserRenderError('invalid-document', 'LibreOffice returned an unexpected load response.')
144
+ opened = result
145
+ if (opened.type !== 'opened' || opened.pages.length === 0) throw new BrowserRenderError('invalid-document', 'LibreOffice returned no document pages.')
146
+ signal?.throwIfAborted()
147
+ } catch (error) {
148
+ await dispose()
149
+ signal?.throwIfAborted()
150
+ throw error
151
+ }
152
+
153
+ return {
154
+ opened,
155
+ request(message: OwnerRequest, operation?: AbortSignal): Promise<WorkerMessage> {
156
+ if (closed) return Promise.reject(new BrowserRenderError('disposed', 'The document is closed.'))
157
+ if (operation?.aborted) return Promise.reject(operation.reason)
158
+ const task = post({ ...message, id: ++sequence })
159
+ if (!operation) return task
160
+ return new Promise((resolve, reject) => {
161
+ const abort = (): void => { reject(operation.reason) }
162
+ operation.addEventListener('abort', abort, { once: true })
163
+ void task.then(resolve, reject).finally(() => operation.removeEventListener('abort', abort))
164
+ })
165
+ },
166
+ dispose,
167
+ }
168
+ }