@rentnerkev/pdf 1.0.1 → 1.0.2
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 +6 -6
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/ai.d.ts.map +1 -1
- package/dist/ai.js +8 -3
- package/dist/ai.js.map +1 -1
- package/dist/components/Types/props.d.ts +2 -2
- package/dist/components/components.d.ts +5 -5
- package/dist/components/components.d.ts.map +1 -1
- package/dist/components/components.js +16 -16
- package/dist/components/components.js.map +1 -1
- package/dist/components/host.d.ts +2 -2
- package/dist/engine/Types/backend.d.ts +5 -5
- package/dist/engine/Types/engine.d.ts +8 -8
- package/dist/engine/Types/errors.d.ts +2 -2
- package/dist/engine/Types/render-options.d.ts +1 -1
- package/dist/engine/Types/render-result.d.ts +1 -1
- package/dist/engine/abort.d.ts +2 -2
- package/dist/engine/abort.d.ts.map +1 -1
- package/dist/engine/abort.js +2 -2
- package/dist/engine/abort.js.map +1 -1
- package/dist/engine/assets.d.ts +4 -3
- package/dist/engine/assets.d.ts.map +1 -1
- package/dist/engine/assets.js +19 -11
- package/dist/engine/assets.js.map +1 -1
- package/dist/engine/backend.d.ts +5 -5
- package/dist/engine/backend.d.ts.map +1 -1
- package/dist/engine/backend.js +12 -9
- package/dist/engine/backend.js.map +1 -1
- package/dist/engine/backends/native.d.ts +2 -2
- package/dist/engine/backends/wasm.d.ts +2 -2
- package/dist/engine/backends/wasm.d.ts.map +1 -1
- package/dist/engine/backends/wasm.js +12 -4
- package/dist/engine/backends/wasm.js.map +1 -1
- package/dist/engine/channel.d.ts +5 -8
- package/dist/engine/channel.d.ts.map +1 -1
- package/dist/engine/channel.js +71 -43
- package/dist/engine/channel.js.map +1 -1
- package/dist/engine/create-engine.d.ts +1 -1
- package/dist/engine/create-engine.d.ts.map +1 -1
- package/dist/engine/create-engine.js +11 -5
- package/dist/engine/create-engine.js.map +1 -1
- package/dist/engine/default-fonts.d.ts.map +1 -1
- package/dist/engine/default-fonts.js +24 -17
- package/dist/engine/default-fonts.js.map +1 -1
- package/dist/engine/encode.d.ts +5 -4
- package/dist/engine/encode.d.ts.map +1 -1
- package/dist/engine/encode.js +15 -10
- package/dist/engine/encode.js.map +1 -1
- package/dist/engine/errors.d.ts +3 -3
- package/dist/engine/fonts.d.ts +1 -1
- package/dist/engine/markup.d.ts +3 -3
- package/dist/engine/options.d.ts +2 -2
- package/dist/engine/prepare.d.ts +4 -4
- package/dist/engine/render-many.d.ts +3 -3
- package/dist/engine/render-result.d.ts +9 -3
- package/dist/engine/render-result.d.ts.map +1 -1
- package/dist/engine/render-result.js +8 -1
- package/dist/engine/render-result.js.map +1 -1
- package/dist/engine/render-stream.d.ts +1 -1
- package/dist/engine/render.d.ts +3 -3
- package/dist/index.d.ts +24 -24
- package/dist/ir/Types/input.d.ts +1 -1
- package/dist/ir/factories.d.ts +3 -3
- package/dist/ir/factories.d.ts.map +1 -1
- package/dist/ir/factories.js +8 -3
- package/dist/ir/factories.js.map +1 -1
- package/dist/ir/page-variants.d.ts +3 -0
- package/dist/ir/page-variants.d.ts.map +1 -0
- package/dist/ir/page-variants.js +3 -0
- package/dist/ir/page-variants.js.map +1 -0
- package/dist/jsx/Types/context.d.ts +1 -1
- package/dist/jsx/Types/evaluate.d.ts +4 -4
- package/dist/jsx/Types/jsx.d.ts +1 -1
- package/dist/jsx/context.d.ts +1 -1
- package/dist/jsx/element.d.ts +1 -1
- package/dist/jsx/evaluate.d.ts +3 -3
- package/dist/jsx/evaluate.d.ts.map +1 -1
- package/dist/jsx/evaluate.js +20 -52
- package/dist/jsx/evaluate.js.map +1 -1
- package/dist/jsx/source.d.ts +1 -1
- package/dist/jsx-dev-runtime.d.ts +3 -3
- package/dist/jsx-runtime.d.ts +3 -3
- package/dist/native/loader.d.ts.map +1 -1
- package/dist/native/loader.js +33 -27
- package/dist/native/loader.js.map +1 -1
- package/dist/native/rentnerpdf.linux-arm64-gnu.node +0 -0
- package/dist/native/rentnerpdf.linux-arm64-musl.node +0 -0
- package/dist/native/rentnerpdf.linux-x64-gnu.node +0 -0
- package/dist/native/rentnerpdf.linux-x64-musl.node +0 -0
- package/dist/native/rentnerpdf.win32-arm64-msvc.node +0 -0
- package/dist/native/rentnerpdf.win32-x64-msvc.node +0 -0
- package/dist/node.d.ts +2 -2
- package/dist/react/Types/preview.d.ts +16 -11
- package/dist/react/Types/preview.d.ts.map +1 -1
- package/dist/react/Types/preview.js.map +1 -1
- package/dist/react/Types/render.d.ts +1 -1
- package/dist/react/components.d.ts +4 -4
- package/dist/react/components.d.ts.map +1 -1
- package/dist/react/components.js +23 -23
- package/dist/react/components.js.map +1 -1
- package/dist/react/pdf-preview.d.ts +3 -2
- package/dist/react/pdf-preview.d.ts.map +1 -1
- package/dist/react/pdf-preview.js +2 -1
- package/dist/react/pdf-preview.js.map +1 -1
- package/dist/react/pdf-render.d.ts +4 -4
- package/dist/react/pdf-render.d.ts.map +1 -1
- package/dist/react/pdf-render.js +20 -12
- package/dist/react/pdf-render.js.map +1 -1
- package/dist/react/registry.d.ts +3 -3
- package/dist/react/registry.d.ts.map +1 -1
- package/dist/react/registry.js +6 -3
- package/dist/react/registry.js.map +1 -1
- package/dist/react/render.d.ts +4 -4
- package/dist/react/render.d.ts.map +1 -1
- package/dist/react/render.js +36 -54
- package/dist/react/render.js.map +1 -1
- package/dist/react/same-document.d.ts +2 -1
- package/dist/react/same-document.d.ts.map +1 -1
- package/dist/react/same-document.js +7 -1
- package/dist/react/same-document.js.map +1 -1
- package/dist/react/static.d.ts +5 -0
- package/dist/react/static.d.ts.map +1 -1
- package/dist/react/static.js +20 -16
- package/dist/react/static.js.map +1 -1
- package/dist/react/use-pdf.d.ts +5 -5
- package/dist/react/use-pdf.d.ts.map +1 -1
- package/dist/react/use-pdf.js +81 -14
- package/dist/react/use-pdf.js.map +1 -1
- package/dist/react.d.ts +8 -8
- package/dist/tailwind-merge.d.ts +2 -2
- package/dist/theme/Types/theme.d.ts +8 -1
- package/dist/theme/Types/theme.d.ts.map +1 -1
- package/dist/theme/Types/theme.js.map +1 -1
- package/dist/theme/css-imports.d.ts +1 -1
- package/dist/theme/normalize.d.ts +2 -2
- package/dist/theme/package-imports.d.ts +1 -8
- package/dist/theme/package-imports.d.ts.map +1 -1
- package/dist/theme/package-imports.js +4 -11
- package/dist/theme/package-imports.js.map +1 -1
- package/dist/theme/paths.d.ts +6 -0
- package/dist/theme/paths.d.ts.map +1 -0
- package/dist/theme/paths.js +13 -0
- package/dist/theme/paths.js.map +1 -0
- package/dist/theme/theme-from-css-file.d.ts +1 -3
- package/dist/theme/theme-from-css-file.d.ts.map +1 -1
- package/dist/theme/theme-from-css-file.js +16 -21
- package/dist/theme/theme-from-css-file.js.map +1 -1
- package/dist/theme/theme-from-css.d.ts +1 -1
- package/dist/theme/warnings.d.ts +1 -4
- package/dist/theme/warnings.d.ts.map +1 -1
- package/dist/theme/warnings.js +1 -1
- package/dist/theme/warnings.js.map +1 -1
- package/dist/vite/plugin.d.ts +3 -6
- package/dist/vite/plugin.d.ts.map +1 -1
- package/dist/vite/plugin.js +17 -7
- package/dist/vite/plugin.js.map +1 -1
- package/dist/vite.d.ts +2 -2
- package/dist/wasm/binding.generated.d.ts +19 -14
- package/dist/wasm/loader.d.ts.map +1 -1
- package/dist/wasm/loader.js +11 -3
- package/dist/wasm/loader.js.map +1 -1
- package/dist/wasm/node-path.d.ts +3 -0
- package/dist/wasm/node-path.d.ts.map +1 -0
- package/dist/wasm/node-path.js +4 -0
- package/dist/wasm/node-path.js.map +1 -0
- package/dist/wasm/pkg/rentnerpdf_wasm.d.ts +19 -14
- package/dist/wasm/pkg/rentnerpdf_wasm.js +39 -75
- package/dist/wasm/pkg/rentnerpdf_wasm_bg.wasm +0 -0
- package/dist/wasm/pkg/rentnerpdf_wasm_bg.wasm.d.ts +8 -7
- package/dist/worker/Types/connection.d.ts +1 -1
- package/dist/worker/Types/protocol.d.ts +2 -2
- package/dist/worker/Types/worker-engine.d.ts +3 -3
- package/dist/worker/client.d.ts +1 -1
- package/dist/worker/client.d.ts.map +1 -1
- package/dist/worker/client.js +10 -10
- package/dist/worker/client.js.map +1 -1
- package/dist/worker/connection.d.ts +2 -2
- package/dist/worker/protocol.d.ts +2 -2
- package/dist/worker/protocol.js.map +1 -1
- package/dist/worker/serve.d.ts +3 -5
- package/dist/worker/serve.d.ts.map +1 -1
- package/dist/worker/serve.js +1 -1
- package/dist/worker/serve.js.map +1 -1
- package/dist/worker.d.ts +3 -3
- package/docs/usage.md +51 -49
- package/package.json +4 -1
- package/dist/engine/pdf-bytes.d.ts +0 -8
- package/dist/engine/pdf-bytes.d.ts.map +0 -1
- package/dist/engine/pdf-bytes.js +0 -9
- package/dist/engine/pdf-bytes.js.map +0 -1
- package/dist/theme/Types/warnings.d.ts +0 -9
- package/dist/theme/Types/warnings.d.ts.map +0 -1
- package/dist/theme/Types/warnings.js +0 -2
- package/dist/theme/Types/warnings.js.map +0 -1
package/dist/engine/backend.d.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { type NativeBinding } from '../native/loader.
|
|
2
|
-
import { type WasmBinding, type WasmModuleSource } from '../wasm/loader.
|
|
3
|
-
import type { Backend, BackendChoice, BackendOptions } from './Types/backend.
|
|
1
|
+
import { type NativeBinding } from '../native/loader.js';
|
|
2
|
+
import { type WasmBinding, type WasmModuleSource } from '../wasm/loader.js';
|
|
3
|
+
import type { Backend, BackendChoice, BackendOptions } from './Types/backend.js';
|
|
4
4
|
/** How the bindings are loaded; tests replace them. */
|
|
5
5
|
export interface BackendLoaders {
|
|
6
6
|
readonly native: () => NativeBinding;
|
|
7
7
|
readonly wasm: (module?: WasmModuleSource) => Promise<WasmBinding>;
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
|
-
*
|
|
10
|
+
* Loads the chosen backend before constructing its engine. `auto` uses the native addon
|
|
11
11
|
* when it loads and otherwise falls back to WebAssembly, with a one-time
|
|
12
12
|
* console hint. Errors of the engine itself (such as an unsupported font)
|
|
13
13
|
* never cause a fallback.
|
|
14
14
|
*/
|
|
15
|
-
export declare function
|
|
15
|
+
export declare function loadBackend(choice: BackendChoice, wasmModule?: WasmModuleSource, loaders?: BackendLoaders): Promise<(options: BackendOptions) => Backend>;
|
|
16
16
|
//# sourceMappingURL=backend.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../web/src/engine/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAiB,MAAM,qBAAqB,CAAA;AACvE,OAAO,EAEH,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACxB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEhF,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,aAAa,CAAA;IACpC,QAAQ,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,WAAW,CAAC,CAAA;CACrE;AAYD;;;;;GAKG;AACH,wBAAsB,
|
|
1
|
+
{"version":3,"file":"backend.d.ts","sourceRoot":"","sources":["../../web/src/engine/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,aAAa,EAAiB,MAAM,qBAAqB,CAAA;AACvE,OAAO,EAEH,KAAK,WAAW,EAChB,KAAK,gBAAgB,EACxB,MAAM,mBAAmB,CAAA;AAG1B,OAAO,KAAK,EAAE,OAAO,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AAEhF,uDAAuD;AACvD,MAAM,WAAW,cAAc;IAC3B,QAAQ,CAAC,MAAM,EAAE,MAAM,aAAa,CAAA;IACpC,QAAQ,CAAC,IAAI,EAAE,CAAC,MAAM,CAAC,EAAE,gBAAgB,KAAK,OAAO,CAAC,WAAW,CAAC,CAAA;CACrE;AAYD;;;;;GAKG;AACH,wBAAsB,WAAW,CAC7B,MAAM,EAAE,aAAa,EACrB,UAAU,CAAC,EAAE,gBAAgB,EAC7B,OAAO,GAAE,cAA+B,GACzC,OAAO,CAAC,CAAC,OAAO,EAAE,cAAc,KAAK,OAAO,CAAC,CA+B/C"}
|
package/dist/engine/backend.js
CHANGED
|
@@ -9,14 +9,15 @@ const defaultLoaders = {
|
|
|
9
9
|
let fallbackReported = false;
|
|
10
10
|
const reason = (error) => error instanceof Error ? error.message : String(error);
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* Loads the chosen backend before constructing its engine. `auto` uses the native addon
|
|
13
13
|
* when it loads and otherwise falls back to WebAssembly, with a one-time
|
|
14
14
|
* console hint. Errors of the engine itself (such as an unsupported font)
|
|
15
15
|
* never cause a fallback.
|
|
16
16
|
*/
|
|
17
|
-
export async function
|
|
17
|
+
export async function loadBackend(choice, wasmModule, loaders = defaultLoaders) {
|
|
18
18
|
if (choice === 'wasm') {
|
|
19
|
-
|
|
19
|
+
const wasm = await loaders.wasm(wasmModule);
|
|
20
|
+
return (options) => createWasmBackend(wasm, options);
|
|
20
21
|
}
|
|
21
22
|
let native;
|
|
22
23
|
try {
|
|
@@ -32,12 +33,14 @@ export async function createBackend(choice, options, wasmModule, loaders = defau
|
|
|
32
33
|
catch (wasmError) {
|
|
33
34
|
throw new AggregateError([nativeError, wasmError], 'RentnerPDF could not load its native or WebAssembly engine.', { cause: wasmError });
|
|
34
35
|
}
|
|
35
|
-
|
|
36
|
-
fallbackReported
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
36
|
+
return (options) => {
|
|
37
|
+
if (!fallbackReported) {
|
|
38
|
+
fallbackReported = true;
|
|
39
|
+
console.warn(`RentnerPDF falls back to its WebAssembly engine, which renders on the calling thread: ${reason(nativeError)}`);
|
|
40
|
+
}
|
|
41
|
+
return createWasmBackend(wasm, options);
|
|
42
|
+
};
|
|
40
43
|
}
|
|
41
|
-
return createNativeBackend(native, options);
|
|
44
|
+
return (options) => createNativeBackend(native, options);
|
|
42
45
|
}
|
|
43
46
|
//# sourceMappingURL=backend.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../web/src/engine/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACvE,OAAO,EACH,eAAe,GAGlB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAStD,MAAM,cAAc,GAAmB;IACnC,MAAM,EAAE,aAAa;IACrB,IAAI,EAAE,eAAe;CACxB,CAAA;AAED,IAAI,gBAAgB,GAAG,KAAK,CAAA;AAE5B,MAAM,MAAM,GAAG,CAAC,KAAc,EAAU,EAAE,CACtC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAA;AAE1D;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,
|
|
1
|
+
{"version":3,"file":"backend.js","sourceRoot":"","sources":["../../web/src/engine/backend.ts"],"names":[],"mappings":"AAAA,OAAO,EAAsB,aAAa,EAAE,MAAM,qBAAqB,CAAA;AACvE,OAAO,EACH,eAAe,GAGlB,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,sBAAsB,CAAA;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAA;AAStD,MAAM,cAAc,GAAmB;IACnC,MAAM,EAAE,aAAa;IACrB,IAAI,EAAE,eAAe;CACxB,CAAA;AAED,IAAI,gBAAgB,GAAG,KAAK,CAAA;AAE5B,MAAM,MAAM,GAAG,CAAC,KAAc,EAAU,EAAE,CACtC,KAAK,YAAY,KAAK,CA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,CAAC,CAAA;AAC5D,CAAC","sourcesContent":["import { type NativeBinding, nativeBinding } from '../native/loader.ts'\nimport {\n loadWasmBinding,\n type WasmBinding,\n type WasmModuleSource,\n} from '../wasm/loader.ts'\nimport { createNativeBackend } from './backends/native.ts'\nimport { createWasmBackend } from './backends/wasm.ts'\nimport type { Backend, BackendChoice, BackendOptions } from './Types/backend.ts'\n\n/** How the bindings are loaded; tests replace them. */\nexport interface BackendLoaders {\n readonly native: () => NativeBinding\n readonly wasm: (module?: WasmModuleSource) => Promise<WasmBinding>\n}\n\nconst defaultLoaders: BackendLoaders = {\n native: nativeBinding,\n wasm: loadWasmBinding,\n}\n\nlet fallbackReported = false\n\nconst reason = (error: unknown): string =>\n error instanceof Error ? error.message : String(error)\n\n/**\n * Loads the chosen backend before constructing its engine. `auto` uses the native addon\n * when it loads and otherwise falls back to WebAssembly, with a one-time\n * console hint. Errors of the engine itself (such as an unsupported font)\n * never cause a fallback.\n */\nexport async function loadBackend(\n choice: BackendChoice,\n wasmModule?: WasmModuleSource,\n loaders: BackendLoaders = defaultLoaders,\n): Promise<(options: BackendOptions) => Backend> {\n if (choice === 'wasm') {\n const wasm = await loaders.wasm(wasmModule)\n return (options) => createWasmBackend(wasm, options)\n }\n let native: NativeBinding\n try {\n native = loaders.native()\n } catch (nativeError) {\n if (choice === 'native') throw nativeError\n let wasm: WasmBinding\n try {\n wasm = await loaders.wasm(wasmModule)\n } catch (wasmError) {\n throw new AggregateError(\n [nativeError, wasmError],\n 'RentnerPDF could not load its native or WebAssembly engine.',\n { cause: wasmError },\n )\n }\n return (options) => {\n if (!fallbackReported) {\n fallbackReported = true\n console.warn(\n `RentnerPDF falls back to its WebAssembly engine, which renders on the calling thread: ${reason(nativeError)}`,\n )\n }\n return createWasmBackend(wasm, options)\n }\n }\n return (options) => createNativeBackend(native, options)\n}\n"]}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { NativeBinding } from '../../native/loader.
|
|
2
|
-
import type { Backend, BackendOptions } from '../Types/backend.
|
|
1
|
+
import type { NativeBinding } from '../../native/loader.js';
|
|
2
|
+
import type { Backend, BackendOptions } from '../Types/backend.js';
|
|
3
3
|
/**
|
|
4
4
|
* The native engine: renders off the event loop on the engine's own render
|
|
5
5
|
* threads (at most `threads` at a time); `renderSync` renders on the
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { WasmBinding } from '../../wasm/loader.
|
|
2
|
-
import type { Backend, BackendOptions } from '../Types/backend.
|
|
1
|
+
import type { WasmBinding } from '../../wasm/loader.js';
|
|
2
|
+
import type { Backend, BackendOptions } from '../Types/backend.js';
|
|
3
3
|
/**
|
|
4
4
|
* The WebAssembly engine. It renders on the calling thread: `render` and
|
|
5
5
|
* `renderDetailed` return promises for API parity, but block the thread
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm.d.ts","sourceRoot":"","sources":["../../../web/src/engine/backends/wasm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;
|
|
1
|
+
{"version":3,"file":"wasm.d.ts","sourceRoot":"","sources":["../../../web/src/engine/backends/wasm.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,sBAAsB,CAAA;AACvD,OAAO,KAAK,EAAE,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAA;AA8GlE;;;;;;;;;;;GAWG;AACH,wBAAgB,iBAAiB,CAC7B,OAAO,EAAE,WAAW,EACpB,OAAO,EAAE,cAAc,GACxB,OAAO,CAkIT"}
|
|
@@ -47,6 +47,12 @@ const isTrap = (error) => error instanceof Error &&
|
|
|
47
47
|
const stoppedError = (trap) => new Error(`INTERNAL: the WebAssembly engine stopped: ${trap.message}`, {
|
|
48
48
|
cause: trap,
|
|
49
49
|
});
|
|
50
|
+
/** Rejects an oversized font before passing its bytes through the binding. */
|
|
51
|
+
function checkFontSize(font, limit) {
|
|
52
|
+
if (font.byteLength > limit) {
|
|
53
|
+
throw new Error(`LIMIT_EXCEEDED: font has ${font.byteLength} bytes, the limit is ${limit}`);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
50
56
|
/** Creates an engine with its options and registers `fonts` in order. */
|
|
51
57
|
function createInstance(binding, { strict, imageCacheBytes, shapedRunsMax, limits, theme }, fonts) {
|
|
52
58
|
const options = new binding.EngineOptions();
|
|
@@ -69,6 +75,7 @@ function createInstance(binding, { strict, imageCacheBytes, shapedRunsMax, limit
|
|
|
69
75
|
const engine = new binding.Engine(options);
|
|
70
76
|
try {
|
|
71
77
|
for (const [bytes, kind] of fonts) {
|
|
78
|
+
checkFontSize(bytes, engine.maxFontBytes);
|
|
72
79
|
if (kind === 'user')
|
|
73
80
|
engine.registerFont(bytes);
|
|
74
81
|
else
|
|
@@ -118,7 +125,7 @@ export function createWasmBackend(binding, options) {
|
|
|
118
125
|
}
|
|
119
126
|
let generation = restartsOf(binding);
|
|
120
127
|
// Engines created again get the same options, so the same limits.
|
|
121
|
-
const { maxInputBytes, maxAssetBytes } = engine;
|
|
128
|
+
const { maxInputBytes, maxAssetBytes, maxFontBytes } = engine;
|
|
122
129
|
// Why the module could not be instantiated again; the engine refuses
|
|
123
130
|
// every further call then.
|
|
124
131
|
let stopped;
|
|
@@ -194,10 +201,11 @@ export function createWasmBackend(binding, options) {
|
|
|
194
201
|
? []
|
|
195
202
|
: guard(() => binding.markupDebugPaths(markup)),
|
|
196
203
|
stats: () => guard((target) => target.stats()),
|
|
197
|
-
registerFont: (font) =>
|
|
198
|
-
|
|
204
|
+
registerFont: (font) => {
|
|
205
|
+
checkFontSize(font, maxFontBytes);
|
|
206
|
+
guard((target) => target.registerFont(font));
|
|
199
207
|
fonts.push([font, 'user']);
|
|
200
|
-
}
|
|
208
|
+
},
|
|
201
209
|
close: () => {
|
|
202
210
|
// An engine of an earlier or trapped instance is left to the
|
|
203
211
|
// collector: freeing it could free memory of the current one.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../../../web/src/engine/backends/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AASrD;;;;GAIG;AACH,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAsB,CAAA;AAElD,wEAAwE;AACxE,MAAM,OAAO,GAAG,CAAC,OAAoB,EAA4B,EAAE;IAC/D,uEAAuE;IACvE,MAAM,KAAK,GAAG,OAAO,CAAC,iBAAiB,CAAA;IACvC,OAAO,OAAO,KAAK,KAAK,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,SAAS,CAAA;AAC1D,CAAC,CAAA;AAED,MAAM,UAAU,GAAG,CAAC,OAAoB,EAAU,EAAE;IAChD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC9B,OAAO,KAAK,KAAK,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAA;AAC/D,CAAC,CAAA;AAED;;;;GAIG;AACH,SAAS,WAAW,CAAC,OAAoB,EAAE,IAAW;IAClD,MAAM,KAAK,GAAG,OAAO,CAAC,OAAO,CAAC,CAAA;IAC9B,IAAI,KAAK,KAAK,SAAS;QAAE,OAAO,IAAI,CAAA;IACpC,IAAI,CAAC;QACD,KAAK,EAAE,CAAA;IACX,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,KAAK,CAAA;IAChB,CAAC;IACD,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAA;IAC5C,OAAO,SAAS,CAAA;AACpB,CAAC;AAED;;;;;;GAMG;AACH,MAAM,MAAM,GAAG,CAAC,KAAc,EAAkB,EAA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AK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC9C,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE,CACnB,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE;YACb,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;YACzB,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA;QAC9B,CAAC,CAAC;QACN,KAAK,EAAE,GAAG,EAAE;YACR,6DAA6D;YAC7D,8DAA8D;YAC9D,IAAI,OAAO,KAAK,SAAS,IAAI,UAAU,KAAK,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC9D,OAAM;YACV,CAAC;YACD,IAAI,CAAC;gBACD,MAAM,CAAC,IAAI,EAAE,CAAA;YACjB,CAAC;YAAC,MAAM,CAAC;gBACL,oDAAoD;YACxD,CAAC;QACL,CAAC;KACJ,CAAA;AACL,CAAC","sourcesContent":["import { assertIrVersion } from '../../ir/version.ts'\nimport type { WasmBinding } from '../../wasm/loader.ts'\nimport type { Backend, BackendOptions } from '../Types/backend.ts'\n\ntype WasmEngine = InstanceType<WasmBinding['Engine']>\n\n/** A font as the engine registered it: a user font or a default font. */\ntype RegisteredFont = readonly [bytes: Uint8Array, kind: 'user' | 'default']\n\n/**\n * How often each loaded module was instantiated again, keyed by its\n * `__wbg_reset_state`: the engines of a realm share one instance, so a\n * reset after a trap in one engine retires the engines of all of them.\n */\nconst restarts = new WeakMap<() => void, number>()\n\n/** The reset function of the glue; builds without it cannot recover. */\nconst resetOf = (binding: WasmBinding): (() => void) | undefined => {\n // oxlint-disable-next-line no-underscore-dangle -- wasm-bindgen's name\n const reset = binding.__wbg_reset_state\n return typeof reset === 'function' ? reset : undefined\n}\n\nconst restartsOf = (binding: WasmBinding): number => {\n const reset = resetOf(binding)\n return reset === undefined ? 0 : (restarts.get(reset) ?? 0)\n}\n\n/**\n * Instantiates the module again after `trap` and returns why that failed,\n * or `undefined`. Engines of the old instance must never be called or\n * freed afterwards: their pointers refer to its memory.\n */\nfunction resetModule(binding: WasmBinding, trap: Error): unknown {\n const reset = resetOf(binding)\n if (reset === undefined) return trap\n try {\n reset()\n } catch (error) {\n return error\n }\n restarts.set(reset, restartsOf(binding) + 1)\n return undefined\n}\n\n/**\n * Whether an error means the module trapped: `WebAssembly.RuntimeError`\n * (a Rust panic, `panic = abort`, or an out-of-bounds access) or a stack\n * overflow (`RangeError` in V8 and JavaScriptCore, `InternalError` in\n * SpiderMonkey). By name: no DOM types needed. The engine's own errors are\n * plain `Error`s with a code prefix.\n */\nconst isTrap = (error: unknown): error is Error =>\n error instanceof Error &&\n (error.name === 'RuntimeError' ||\n error.name === 'RangeError' ||\n error.name === 'InternalError')\n\nconst stoppedError = (trap: Error): Error =>\n new Error(`INTERNAL: the WebAssembly engine stopped: ${trap.message}`, {\n cause: trap,\n })\n\n/** Creates an engine with its options and registers `fonts` in order. */\nfunction createInstance(\n binding: WasmBinding,\n { strict, imageCacheBytes, shapedRunsMax, limits, theme }: BackendOptions,\n fonts: readonly RegisteredFont[],\n): WasmEngine {\n const options = new binding.EngineOptions()\n options.strict = strict\n options.imageCacheBytes = imageCacheBytes\n options.shapedRunsMax = shapedRunsMax\n for (const [name, value] of Object.entries(limits ?? {})) {\n if (value !== undefined) options.setLimit(name, value)\n }\n if (theme !== undefined) {\n for (const { css, scopes, source } of theme.stylesheets) {\n options.addThemeStylesheet(css, [...scopes], source)\n }\n for (const [name, value] of theme.variables) {\n options.addThemeVariable(name, value)\n }\n options.rootFontSize = theme.rootFontSize\n }\n const engine = new binding.Engine(options)\n try {\n for (const [bytes, kind] of fonts) {\n if (kind === 'user') engine.registerFont(bytes)\n else engine.registerDefaultFont(bytes)\n }\n } catch (error) {\n try {\n engine.free()\n } catch {\n // A trapped module may fail to free; the error below says why.\n }\n throw error\n }\n return engine\n}\n\n/**\n * The WebAssembly engine. It renders on the calling thread: `render` and\n * `renderDetailed` return promises for API parity, but block the thread\n * while the engine works, so a signal cannot stop a running render (the\n * engine layer checks it before each render). It has no threads.\n *\n * Documents are checked against `maxInputBytes` and `maxAssetBytes` before\n * they are copied into WebAssembly memory, which never shrinks. A trap (a\n * Rust panic aborts the module) fails its call with `INTERNAL` and\n * instantiates the module again; the next call creates the engine anew\n * with the same options and every font registered so far.\n */\nexport function createWasmBackend(\n binding: WasmBinding,\n options: BackendOptions,\n): Backend {\n assertIrVersion('WebAssembly', binding.irVersion())\n // Every font in registration order; the same buffers are registered\n // again after a reset.\n const fonts: RegisteredFont[] = [\n ...options.fonts.map((font) => [font, 'user'] as const),\n ...options.defaultFonts.map((font) => [font, 'default'] as const),\n ]\n let engine: WasmEngine\n try {\n engine = createInstance(binding, options, fonts)\n } catch (error) {\n if (!isTrap(error)) throw error\n resetModule(binding, error)\n throw stoppedError(error)\n }\n let generation = restartsOf(binding)\n // Engines created again get the same options, so the same limits.\n const { maxInputBytes, maxAssetBytes } = engine\n // Why the module could not be instantiated again; the engine refuses\n // every further call then.\n let stopped: unknown\n\n /** This engine on the current instance, created again after a reset. */\n const current = (): WasmEngine => {\n if (stopped !== undefined) {\n throw new Error(\n 'INTERNAL: this WebAssembly engine stopped after an internal error, and its module could not be instantiated again',\n { cause: stopped },\n )\n }\n const latest = restartsOf(binding)\n if (generation !== latest) {\n try {\n engine = createInstance(binding, options, fonts)\n } catch (error) {\n stopped = error\n if (isTrap(error)) resetModule(binding, error)\n throw new Error(\n `INTERNAL: the WebAssembly engine could not be created again: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n )\n }\n generation = latest\n }\n return engine\n }\n\n const guard = <T>(run: (engine: WasmEngine) => T): T => {\n const target = current()\n try {\n return run(target)\n } catch (error) {\n if (!isTrap(error)) throw error\n stopped = resetModule(binding, error)\n throw stoppedError(error)\n }\n }\n\n // The engine's own first checks (decode.rs), with its messages.\n const checkSizes = (input: Uint8Array, assets: readonly Uint8Array[]) => {\n const assetBytes = assets.reduce(\n (total, asset) => total + asset.byteLength,\n 0,\n )\n if (assetBytes > maxAssetBytes) {\n throw new Error(\n `LIMIT_EXCEEDED: assets have ${assetBytes} bytes, the limit is ${maxAssetBytes}`,\n )\n }\n if (input.byteLength > maxInputBytes) {\n throw new Error(\n `LIMIT_EXCEEDED: input has ${input.byteLength} bytes, the limit is ${maxInputBytes}`,\n )\n }\n }\n\n return {\n name: 'wasm',\n version: binding.engineVersion(),\n themeWarnings: engine.themeWarnings(),\n threads: 1,\n render: async ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.render(ir, assets))\n },\n renderDetailed: async ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.renderDetailed(ir, assets))\n },\n renderSync: ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.render(ir, assets))\n },\n renderMarkup: async ({ markup, assets, channel }) => {\n checkSizes(markup, assets)\n return guard((target) =>\n target.renderMarkup(markup, assets, channel),\n )\n },\n renderMarkupDetailed: async ({ markup, assets, channel }) => {\n checkSizes(markup, assets)\n return guard((target) =>\n target.renderMarkupDetailed(markup, assets, channel),\n )\n },\n // Larger markup has no paths: the engine refused to render it.\n markupDebugPaths: (markup) =>\n markup.byteLength > maxInputBytes\n ? []\n : guard(() => binding.markupDebugPaths(markup)),\n stats: () => guard((target) => target.stats()),\n registerFont: (font) =>\n guard((target) => {\n target.registerFont(font)\n fonts.push([font, 'user'])\n }),\n close: () => {\n // An engine of an earlier or trapped instance is left to the\n // collector: freeing it could free memory of the current one.\n if (stopped !== undefined || generation !== restartsOf(binding)) {\n return\n }\n try {\n engine.free()\n } catch {\n // Nothing refers to the engine any more either way.\n }\n },\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"wasm.js","sourceRoot":"","sources":["../../../web/src/engine/backends/wasm.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAA;AASrD;;;;GAIG;AACH,MAA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AK,CAAC,GAAG,EAAE,CAAC,OAAO,CAAC,gBAAgB,CAAC,MAAM,CAAC,CAAC;QACvD,KAAK,EAAE,GAAG,EAAE,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QAC9C,YAAY,EAAE,CAAC,IAAI,EAAE,EAAE;YACnB,aAAa,CAAC,IAAI,EAAE,YAAY,CAAC,CAAA;YACjC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAC,CAAA;YAC5C,KAAK,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,MAAM,CAAC,CAAC,CAAA;QAC9B,CAAC;QACD,KAAK,EAAE,GAAG,EAAE;YACR,6DAA6D;YAC7D,8DAA8D;YAC9D,IAAI,OAAO,KAAK,SAAS,IAAI,UAAU,KAAK,UAAU,CAAC,OAAO,CAAC,EAAE,CAAC;gBAC9D,OAAM;YACV,CAAC;YACD,IAAI,CAAC;gBACD,MAAM,CAAC,IAAI,EAAE,CAAA;YACjB,CAAC;YAAC,MAAM,CAAC;gBACL,oDAAoD;YACxD,CAAC;QACL,CAAC;KACJ,CAAA;AACL,CAAC","sourcesContent":["import { assertIrVersion } from '../../ir/version.ts'\nimport type { WasmBinding } from '../../wasm/loader.ts'\nimport type { Backend, BackendOptions } from '../Types/backend.ts'\n\ntype WasmEngine = InstanceType<WasmBinding['Engine']>\n\n/** A font as the engine registered it: a user font or a default font. */\ntype RegisteredFont = readonly [bytes: Uint8Array, kind: 'user' | 'default']\n\n/**\n * How often each loaded module was instantiated again, keyed by its\n * `__wbg_reset_state`: the engines of a realm share one instance, so a\n * reset after a trap in one engine retires the engines of all of them.\n */\nconst restarts = new WeakMap<() => void, number>()\n\n/** The reset function of the glue; builds without it cannot recover. */\nconst resetOf = (binding: WasmBinding): (() => void) | undefined => {\n // oxlint-disable-next-line no-underscore-dangle -- wasm-bindgen's name\n const reset = binding.__wbg_reset_state\n return typeof reset === 'function' ? reset : undefined\n}\n\nconst restartsOf = (binding: WasmBinding): number => {\n const reset = resetOf(binding)\n return reset === undefined ? 0 : (restarts.get(reset) ?? 0)\n}\n\n/**\n * Instantiates the module again after `trap` and returns why that failed,\n * or `undefined`. Engines of the old instance must never be called or\n * freed afterwards: their pointers refer to its memory.\n */\nfunction resetModule(binding: WasmBinding, trap: Error): unknown {\n const reset = resetOf(binding)\n if (reset === undefined) return trap\n try {\n reset()\n } catch (error) {\n return error\n }\n restarts.set(reset, restartsOf(binding) + 1)\n return undefined\n}\n\n/**\n * Whether an error means the module trapped: `WebAssembly.RuntimeError`\n * (a Rust panic, `panic = abort`, or an out-of-bounds access) or a stack\n * overflow (`RangeError` in V8 and JavaScriptCore, `InternalError` in\n * SpiderMonkey). By name: no DOM types needed. The engine's own errors are\n * plain `Error`s with a code prefix.\n */\nconst isTrap = (error: unknown): error is Error =>\n error instanceof Error &&\n (error.name === 'RuntimeError' ||\n error.name === 'RangeError' ||\n error.name === 'InternalError')\n\nconst stoppedError = (trap: Error): Error =>\n new Error(`INTERNAL: the WebAssembly engine stopped: ${trap.message}`, {\n cause: trap,\n })\n\n/** Rejects an oversized font before passing its bytes through the binding. */\nfunction checkFontSize(font: Uint8Array, limit: number): void {\n if (font.byteLength > limit) {\n throw new Error(\n `LIMIT_EXCEEDED: font has ${font.byteLength} bytes, the limit is ${limit}`,\n )\n }\n}\n\n/** Creates an engine with its options and registers `fonts` in order. */\nfunction createInstance(\n binding: WasmBinding,\n { strict, imageCacheBytes, shapedRunsMax, limits, theme }: BackendOptions,\n fonts: readonly RegisteredFont[],\n): WasmEngine {\n const options = new binding.EngineOptions()\n options.strict = strict\n options.imageCacheBytes = imageCacheBytes\n options.shapedRunsMax = shapedRunsMax\n for (const [name, value] of Object.entries(limits ?? {})) {\n if (value !== undefined) options.setLimit(name, value)\n }\n if (theme !== undefined) {\n for (const { css, scopes, source } of theme.stylesheets) {\n options.addThemeStylesheet(css, [...scopes], source)\n }\n for (const [name, value] of theme.variables) {\n options.addThemeVariable(name, value)\n }\n options.rootFontSize = theme.rootFontSize\n }\n const engine = new binding.Engine(options)\n try {\n for (const [bytes, kind] of fonts) {\n checkFontSize(bytes, engine.maxFontBytes)\n if (kind === 'user') engine.registerFont(bytes)\n else engine.registerDefaultFont(bytes)\n }\n } catch (error) {\n try {\n engine.free()\n } catch {\n // A trapped module may fail to free; the error below says why.\n }\n throw error\n }\n return engine\n}\n\n/**\n * The WebAssembly engine. It renders on the calling thread: `render` and\n * `renderDetailed` return promises for API parity, but block the thread\n * while the engine works, so a signal cannot stop a running render (the\n * engine layer checks it before each render). It has no threads.\n *\n * Documents are checked against `maxInputBytes` and `maxAssetBytes` before\n * they are copied into WebAssembly memory, which never shrinks. A trap (a\n * Rust panic aborts the module) fails its call with `INTERNAL` and\n * instantiates the module again; the next call creates the engine anew\n * with the same options and every font registered so far.\n */\nexport function createWasmBackend(\n binding: WasmBinding,\n options: BackendOptions,\n): Backend {\n assertIrVersion('WebAssembly', binding.irVersion())\n // Every font in registration order; the same buffers are registered\n // again after a reset.\n const fonts: RegisteredFont[] = [\n ...options.fonts.map((font) => [font, 'user'] as const),\n ...options.defaultFonts.map((font) => [font, 'default'] as const),\n ]\n let engine: WasmEngine\n try {\n engine = createInstance(binding, options, fonts)\n } catch (error) {\n if (!isTrap(error)) throw error\n resetModule(binding, error)\n throw stoppedError(error)\n }\n let generation = restartsOf(binding)\n // Engines created again get the same options, so the same limits.\n const { maxInputBytes, maxAssetBytes, maxFontBytes } = engine\n // Why the module could not be instantiated again; the engine refuses\n // every further call then.\n let stopped: unknown\n\n /** This engine on the current instance, created again after a reset. */\n const current = (): WasmEngine => {\n if (stopped !== undefined) {\n throw new Error(\n 'INTERNAL: this WebAssembly engine stopped after an internal error, and its module could not be instantiated again',\n { cause: stopped },\n )\n }\n const latest = restartsOf(binding)\n if (generation !== latest) {\n try {\n engine = createInstance(binding, options, fonts)\n } catch (error) {\n stopped = error\n if (isTrap(error)) resetModule(binding, error)\n throw new Error(\n `INTERNAL: the WebAssembly engine could not be created again: ${error instanceof Error ? error.message : String(error)}`,\n { cause: error },\n )\n }\n generation = latest\n }\n return engine\n }\n\n const guard = <T>(run: (engine: WasmEngine) => T): T => {\n const target = current()\n try {\n return run(target)\n } catch (error) {\n if (!isTrap(error)) throw error\n stopped = resetModule(binding, error)\n throw stoppedError(error)\n }\n }\n\n // The engine's own first checks (decode.rs), with its messages.\n const checkSizes = (input: Uint8Array, assets: readonly Uint8Array[]) => {\n const assetBytes = assets.reduce(\n (total, asset) => total + asset.byteLength,\n 0,\n )\n if (assetBytes > maxAssetBytes) {\n throw new Error(\n `LIMIT_EXCEEDED: assets have ${assetBytes} bytes, the limit is ${maxAssetBytes}`,\n )\n }\n if (input.byteLength > maxInputBytes) {\n throw new Error(\n `LIMIT_EXCEEDED: input has ${input.byteLength} bytes, the limit is ${maxInputBytes}`,\n )\n }\n }\n\n return {\n name: 'wasm',\n version: binding.engineVersion(),\n themeWarnings: engine.themeWarnings(),\n threads: 1,\n render: async ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.render(ir, assets))\n },\n renderDetailed: async ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.renderDetailed(ir, assets))\n },\n renderSync: ({ ir, assets }) => {\n checkSizes(ir, assets)\n return guard((target) => target.render(ir, assets))\n },\n renderMarkup: async ({ markup, assets, channel }) => {\n checkSizes(markup, assets)\n return guard((target) =>\n target.renderMarkup(markup, assets, channel),\n )\n },\n renderMarkupDetailed: async ({ markup, assets, channel }) => {\n checkSizes(markup, assets)\n return guard((target) =>\n target.renderMarkupDetailed(markup, assets, channel),\n )\n },\n // Larger markup has no paths: the engine refused to render it.\n markupDebugPaths: (markup) =>\n markup.byteLength > maxInputBytes\n ? []\n : guard(() => binding.markupDebugPaths(markup)),\n stats: () => guard((target) => target.stats()),\n registerFont: (font) => {\n checkFontSize(font, maxFontBytes)\n guard((target) => target.registerFont(font))\n fonts.push([font, 'user'])\n },\n close: () => {\n // An engine of an earlier or trapped instance is left to the\n // collector: freeing it could free memory of the current one.\n if (stopped !== undefined || generation !== restartsOf(binding)) {\n return\n }\n try {\n engine.free()\n } catch {\n // Nothing refers to the engine any more either way.\n }\n },\n }\n}\n"]}
|
package/dist/engine/channel.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type { Document, TableColumn } from '../ir/ir.generated.
|
|
2
|
-
import type { TableColumnInput } from '../ir/Types/input.
|
|
1
|
+
import type { Document, TableColumn } from '../ir/ir.generated.js';
|
|
2
|
+
import type { TableColumnInput } from '../ir/Types/input.js';
|
|
3
3
|
export type ChannelRange = NonNullable<TableColumn['text']>;
|
|
4
4
|
export type DataChannel = NonNullable<Document['channel']>;
|
|
5
5
|
/** Where a data table's column cells go: the channel, or a cache in front of it. */
|
|
@@ -7,8 +7,6 @@ export interface ChannelCells {
|
|
|
7
7
|
text(rows: readonly object[], key: string, path: string): ChannelRange;
|
|
8
8
|
values(rows: readonly object[], key: string, path: string): ChannelRange;
|
|
9
9
|
}
|
|
10
|
-
/** Whether a data table column holds numbers in minor units (`format` or `sum: true`). */
|
|
11
|
-
export declare const isNumberColumn: (column: TableColumnInput) => boolean;
|
|
12
10
|
/**
|
|
13
11
|
* Writes a data table's cells to the channel, column by column: the row
|
|
14
12
|
* count and the columns with their cell ranges, as the IR has them. `path`
|
|
@@ -22,9 +20,8 @@ export declare function encodeDataTable(data: unknown, columns: readonly TableCo
|
|
|
22
20
|
export declare class ColumnarChannel implements ChannelCells {
|
|
23
21
|
#private;
|
|
24
22
|
/**
|
|
25
|
-
* Adds the `key` cells
|
|
26
|
-
*
|
|
27
|
-
* `undefined` as empty cells.
|
|
23
|
+
* Adds the `key` cells as text. Numbers and bigints use `String()`.
|
|
24
|
+
* Null and undefined produce empty cells.
|
|
28
25
|
*/
|
|
29
26
|
text(rows: readonly object[], key: string, path: string): ChannelRange;
|
|
30
27
|
/**
|
|
@@ -36,6 +33,6 @@ export declare class ColumnarChannel implements ChannelCells {
|
|
|
36
33
|
* Encodes the buffers and adds them with `add`, which returns their
|
|
37
34
|
* asset index; `undefined` if no data table added a column.
|
|
38
35
|
*/
|
|
39
|
-
finish(add: (buffer: Uint8Array) => number): DataChannel | undefined;
|
|
36
|
+
finish(add: (buffer: Uint8Array<ArrayBuffer>) => number): DataChannel | undefined;
|
|
40
37
|
}
|
|
41
38
|
//# sourceMappingURL=channel.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../web/src/engine/channel.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;AAC3D,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAA;
|
|
1
|
+
{"version":3,"file":"channel.d.ts","sourceRoot":"","sources":["../../web/src/engine/channel.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,uBAAuB,CAAA;AAClE,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,sBAAsB,CAAA;AAE5D,MAAM,MAAM,YAAY,GAAG,WAAW,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,CAAA;AAC3D,MAAM,MAAM,WAAW,GAAG,WAAW,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC,CAAA;AAuE1D,oFAAoF;AACpF,MAAM,WAAW,YAAY;IACzB,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAAA;IACtE,MAAM,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAAA;CAC3E;AAMD;;;;GAIG;AACH,wBAAgB,eAAe,CAC3B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,SAAS,gBAAgB,EAAE,EACpC,KAAK,EAAE,YAAY,EACnB,IAAI,EAAE,MAAM,GACb;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,WAAW,EAAE,CAAA;CAAE,CAqB1C;AAED,sDAAsD;AACtD,qBAAa,eAAgB,YAAW,YAAY;;IAUhD;;;OAGG;IACH,IAAI,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CAoCrE;IAED;;;OAGG;IACH,MAAM,CAAC,IAAI,EAAE,SAAS,MAAM,EAAE,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,YAAY,CA8BvE;IAED;;;OAGG;IACH,MAAM,CACF,GAAG,EAAE,CAAC,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,KAAK,MAAM,GACjD,WAAW,GAAG,SAAS,CAwBzB;CACJ"}
|
package/dist/engine/channel.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
let utf8;
|
|
2
2
|
/** Typed arrays use the platform's byte order; the engine reads little-endian. */
|
|
3
|
-
|
|
3
|
+
let littleEndian;
|
|
4
4
|
const invalid = (message, path) =>
|
|
5
5
|
// The engine's error format, so the JSX source is added.
|
|
6
6
|
new Error(`INVALID_DOCUMENT: ${message} (at ${path})`);
|
|
@@ -13,20 +13,41 @@ const describe = (value) => value instanceof Date
|
|
|
13
13
|
: typeof value === 'function'
|
|
14
14
|
? 'a function'
|
|
15
15
|
: `a ${typeof value}`;
|
|
16
|
-
/**
|
|
17
|
-
|
|
18
|
-
if (
|
|
19
|
-
return
|
|
16
|
+
/** Reserve a complete column while keeping growth amortized across tables. */
|
|
17
|
+
function reserve(array, length, allocate) {
|
|
18
|
+
if (array.length >= length)
|
|
19
|
+
return array;
|
|
20
|
+
const next = allocate(Math.max(length, array.length * 2));
|
|
21
|
+
next.set(array);
|
|
22
|
+
return next;
|
|
23
|
+
}
|
|
24
|
+
const endOffsets = (length) => new Uint32Array(length);
|
|
25
|
+
const minorUnits = (length) => new Float64Array(length);
|
|
26
|
+
/** The exact bytes of a typed array, little-endian on every platform. */
|
|
27
|
+
const bytesOf = (array, length, write) => {
|
|
28
|
+
if ((littleEndian ??= new Uint8Array(new Uint32Array([1]).buffer)[0] === 1)) {
|
|
29
|
+
const exact = array.length === length ? array : array.slice(0, length);
|
|
30
|
+
return new Uint8Array(exact.buffer);
|
|
20
31
|
}
|
|
21
|
-
const bytes = new Uint8Array(array.
|
|
32
|
+
const bytes = new Uint8Array(length * array.BYTES_PER_ELEMENT);
|
|
22
33
|
const view = new DataView(bytes.buffer);
|
|
23
|
-
for (const [index, value] of array.entries()) {
|
|
34
|
+
for (const [index, value] of array.subarray(0, length).entries()) {
|
|
24
35
|
write(view, index * array.BYTES_PER_ELEMENT, value);
|
|
25
36
|
}
|
|
26
37
|
return bytes;
|
|
27
38
|
};
|
|
39
|
+
function encodeText(text) {
|
|
40
|
+
const encoder = (utf8 ??= new TextEncoder());
|
|
41
|
+
if (/\P{ASCII}/u.test(text))
|
|
42
|
+
return encoder.encode(text);
|
|
43
|
+
// An ASCII code unit takes exactly one byte. Encode directly into the
|
|
44
|
+
// buffer the worker will transfer.
|
|
45
|
+
const bytes = new Uint8Array(text.length);
|
|
46
|
+
encoder.encodeInto(text, bytes);
|
|
47
|
+
return bytes;
|
|
48
|
+
}
|
|
28
49
|
/** Whether a data table column holds numbers in minor units (`format` or `sum: true`). */
|
|
29
|
-
|
|
50
|
+
const isNumberColumn = (column) => column.format !== undefined || column.sum === true;
|
|
30
51
|
/**
|
|
31
52
|
* Writes a data table's cells to the channel, column by column: the row
|
|
32
53
|
* count and the columns with their cell ranges, as the IR has them. `path`
|
|
@@ -51,26 +72,35 @@ export function encodeDataTable(data, columns, cells, path) {
|
|
|
51
72
|
/** Collects the cells of a document's data tables. */
|
|
52
73
|
export class ColumnarChannel {
|
|
53
74
|
#texts = [];
|
|
54
|
-
#ends =
|
|
55
|
-
#
|
|
56
|
-
#
|
|
75
|
+
#ends = endOffsets(0);
|
|
76
|
+
#values = minorUnits(0);
|
|
77
|
+
#textCells = 0;
|
|
78
|
+
#numberCells = 0;
|
|
57
79
|
/** Whether any column added cells (or an empty column was added). */
|
|
58
80
|
#used = false;
|
|
59
81
|
/**
|
|
60
|
-
* Adds the `key` cells
|
|
61
|
-
*
|
|
62
|
-
* `undefined` as empty cells.
|
|
82
|
+
* Adds the `key` cells as text. Numbers and bigints use `String()`.
|
|
83
|
+
* Null and undefined produce empty cells.
|
|
63
84
|
*/
|
|
64
85
|
text(rows, key, path) {
|
|
65
86
|
this.#used = true;
|
|
66
|
-
const at = this.#
|
|
67
|
-
|
|
87
|
+
const at = this.#textCells;
|
|
88
|
+
const length = rows.length;
|
|
89
|
+
const ends = (this.#ends = reserve(this.#ends, at + length, endOffsets));
|
|
90
|
+
const texts = this.#texts;
|
|
91
|
+
let end = ends[at - 1] ?? 0;
|
|
92
|
+
for (let index = 0; index < length; index++) {
|
|
68
93
|
const value = rows[index][key];
|
|
69
94
|
let text;
|
|
70
95
|
if (typeof value === 'string') {
|
|
71
|
-
//
|
|
72
|
-
//
|
|
73
|
-
|
|
96
|
+
// Repair a trailing high surrogate before joining cells so it
|
|
97
|
+
// cannot pair with the next cell's low surrogate. UTF-8 encoding
|
|
98
|
+
// repairs the remaining lone surrogates.
|
|
99
|
+
const last = value.charCodeAt(value.length - 1);
|
|
100
|
+
text =
|
|
101
|
+
last >= 0xd800 && last <= 0xdbff
|
|
102
|
+
? value.toWellFormed()
|
|
103
|
+
: value;
|
|
74
104
|
}
|
|
75
105
|
else if (typeof value === 'number' || typeof value === 'bigint') {
|
|
76
106
|
text = String(value);
|
|
@@ -81,11 +111,13 @@ export class ColumnarChannel {
|
|
|
81
111
|
else {
|
|
82
112
|
throw invalid(`column "${key}" shows strings and numbers; row ${index} has ${describe(value)}, format it as a string first`, `${path}.data[${index}].${key}`);
|
|
83
113
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
114
|
+
texts[at + index] = text;
|
|
115
|
+
end += text.length;
|
|
116
|
+
ends[at + index] = end;
|
|
87
117
|
}
|
|
88
|
-
|
|
118
|
+
this.#textCells += length;
|
|
119
|
+
texts.length = this.#textCells;
|
|
120
|
+
return { at, len: length };
|
|
89
121
|
}
|
|
90
122
|
/**
|
|
91
123
|
* Adds the `key` cells of `rows` as numbers: safe integers in minor
|
|
@@ -93,11 +125,13 @@ export class ColumnarChannel {
|
|
|
93
125
|
*/
|
|
94
126
|
values(rows, key, path) {
|
|
95
127
|
this.#used = true;
|
|
96
|
-
const at = this.#
|
|
97
|
-
|
|
128
|
+
const at = this.#numberCells;
|
|
129
|
+
const length = rows.length;
|
|
130
|
+
const values = (this.#values = reserve(this.#values, at + length, minorUnits));
|
|
131
|
+
for (let index = 0; index < length; index++) {
|
|
98
132
|
const value = rows[index][key];
|
|
99
133
|
if (value === null || value === undefined) {
|
|
100
|
-
|
|
134
|
+
values[at + index] = Number.NaN;
|
|
101
135
|
continue;
|
|
102
136
|
}
|
|
103
137
|
const number = typeof value === 'bigint' ? Number(value) : value;
|
|
@@ -106,9 +140,10 @@ export class ColumnarChannel {
|
|
|
106
140
|
(typeof value === 'bigint' && BigInt(number) !== value)) {
|
|
107
141
|
throw invalid(`column "${key}" holds integers in minor units (cents) within ±Number.MAX_SAFE_INTEGER; row ${index} has ${typeof value === 'number' || typeof value === 'bigint' ? String(value) : describe(value)}`, `${path}.data[${index}].${key}`);
|
|
108
142
|
}
|
|
109
|
-
|
|
143
|
+
values[at + index] = number;
|
|
110
144
|
}
|
|
111
|
-
|
|
145
|
+
this.#numberCells += length;
|
|
146
|
+
return { at, len: length };
|
|
112
147
|
}
|
|
113
148
|
/**
|
|
114
149
|
* Encodes the buffers and adds them with `add`, which returns their
|
|
@@ -117,21 +152,14 @@ export class ColumnarChannel {
|
|
|
117
152
|
finish(add) {
|
|
118
153
|
if (!this.#used)
|
|
119
154
|
return undefined;
|
|
120
|
-
const
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const grown = new Uint8Array(written + (joined.length - first.read) * 3);
|
|
127
|
-
grown.set(text.subarray(0, written));
|
|
128
|
-
written += utf8.encodeInto(joined.slice(first.read), grown.subarray(written)).written;
|
|
129
|
-
text = grown;
|
|
130
|
-
}
|
|
131
|
-
const ends = bytesOf(Uint32Array.from(this.#ends), (view, offset, value) => view.setUint32(offset, value, true));
|
|
132
|
-
const values = bytesOf(Float64Array.from(this.#values), (view, offset, value) => view.setFloat64(offset, value, true));
|
|
155
|
+
const texts = this.#texts;
|
|
156
|
+
const text = encodeText((texts.length === this.#textCells
|
|
157
|
+
? texts
|
|
158
|
+
: texts.slice(0, this.#textCells)).join(''));
|
|
159
|
+
const ends = bytesOf(this.#ends, this.#textCells, (view, offset, value) => view.setUint32(offset, value, true));
|
|
160
|
+
const values = bytesOf(this.#values, this.#numberCells, (view, offset, value) => view.setFloat64(offset, value, true));
|
|
133
161
|
return {
|
|
134
|
-
text: add(text
|
|
162
|
+
text: add(text),
|
|
135
163
|
ends: add(ends),
|
|
136
164
|
values: add(values),
|
|
137
165
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../../web/src/engine/channel.ts"],"names":[],"mappings":"AAaA,MAAM,IAAI,GAAG,IAAI,WAAW,EAAE,CAAA;AAE9B,kFAAkF;AAClF,MAAM,YAAY,GAAG,IAAI,UAAU,CAAC,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAA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AC/B,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAChE,CAAA;QACD,OAAO;YACH,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC;YACpC,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;YACf,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;SACtB,CAAA;IACL,CAAC;CACJ","sourcesContent":["// The columnar channel (D-06): the cells of data-driven tables travel as\n// three buffers next to the IR JSON instead of inside it. Text cells are\n// joined and written with one `encodeInto`, with their UTF-16 end offsets\n// in a `Uint32Array`; number cells (integer minor units) go into a\n// `Float64Array`, NaN for empty cells. Columns reference their cells as\n// `{ at, len }`. Rust validates everything again; the checks here give\n// errors with the row and key instead of a channel offset.\nimport type { Document, TableColumn } from '../ir/ir.generated.ts'\nimport type { TableColumnInput } from '../ir/Types/input.ts'\n\nexport type ChannelRange = NonNullable<TableColumn['text']>\nexport type DataChannel = NonNullable<Document['channel']>\n\nconst utf8 = new TextEncoder()\n\n/** Typed arrays use the platform's byte order; the engine reads little-endian. */\nconst littleEndian = new Uint8Array(new Uint32Array([1]).buffer)[0] === 1\n\nconst invalid = (message: string, path: string): Error =>\n // The engine's error format, so the JSX source is added.\n new Error(`INVALID_DOCUMENT: ${message} (at ${path})`)\n\nconst describe = (value: unknown): string =>\n value instanceof Date\n ? 'a Date'\n : Array.isArray(value)\n ? 'an array'\n : typeof value === 'object'\n ? 'an object'\n : typeof value === 'function'\n ? 'a function'\n : `a ${typeof value}`\n\n/** The bytes of a typed array, little-endian on every platform. */\nconst bytesOf = (\n array: Uint32Array | Float64Array,\n write: (view: DataView, offset: number, value: number) => void,\n): Uint8Array => {\n if (littleEndian) {\n return new Uint8Array(array.buffer, array.byteOffset, array.byteLength)\n }\n const bytes = new Uint8Array(array.byteLength)\n const view = new DataView(bytes.buffer)\n for (const [index, value] of array.entries()) {\n write(view, index * array.BYTES_PER_ELEMENT, value)\n }\n return bytes\n}\n\n/** Where a data table's column cells go: the channel, or a cache in front of it. */\nexport interface ChannelCells {\n text(rows: readonly object[], key: string, path: string): ChannelRange\n values(rows: readonly object[], key: string, path: string): ChannelRange\n}\n\n/** Whether a data table column holds numbers in minor units (`format` or `sum: true`). */\nexport const isNumberColumn = (column: TableColumnInput): boolean =>\n column.format !== undefined || column.sum === true\n\n/**\n * Writes a data table's cells to the channel, column by column: the row\n * count and the columns with their cell ranges, as the IR has them. `path`\n * is the table's IR path for error messages.\n */\nexport function encodeDataTable(\n data: unknown,\n columns: readonly TableColumnInput[],\n cells: ChannelCells,\n path: string,\n): { rows: number; columns: TableColumn[] } {\n if (!Array.isArray(data)) {\n throw invalid(\n 'table data must be an array of row objects',\n `${path}.data`,\n )\n }\n const rows = data as readonly object[]\n const encoded = columns.map((column, index): TableColumn => {\n const { key } = column\n if (typeof key !== 'string') {\n throw invalid(\n 'a column of a data table needs the `key` of the row property it shows',\n `${path}.columns[${index}].key`,\n )\n }\n return isNumberColumn(column)\n ? { ...column, values: cells.values(rows, key, path) }\n : { ...column, text: cells.text(rows, key, path) }\n })\n return { rows: rows.length, columns: encoded }\n}\n\n/** Collects the cells of a document's data tables. */\nexport class ColumnarChannel implements ChannelCells {\n readonly #texts: string[] = []\n readonly #ends: number[] = []\n #end = 0\n readonly #values: number[] = []\n\n /** Whether any column added cells (or an empty column was added). */\n #used = false\n\n /**\n * Adds the `key` cells of `rows` as text: strings (malformed UTF-16\n * repaired), numbers and bigints with `String()`, `null` and\n * `undefined` as empty cells.\n */\n text(rows: readonly object[], key: string, path: string): ChannelRange {\n this.#used = true\n const at = this.#ends.length\n for (let index = 0; index < rows.length; index++) {\n const value = (rows[index] as Record<string, unknown>)[key]\n let text: string\n if (typeof value === 'string') {\n // A lone surrogate would merge with its neighbour cell's when\n // the cells are joined; U+FFFD keeps the cell's length.\n text = value.isWellFormed() ? value : value.toWellFormed()\n } else if (typeof value === 'number' || typeof value === 'bigint') {\n text = String(value)\n } else if (value === null || value === undefined) {\n text = ''\n } else {\n throw invalid(\n `column \"${key}\" shows strings and numbers; row ${index} has ${describe(value)}, format it as a string first`,\n `${path}.data[${index}].${key}`,\n )\n }\n this.#texts.push(text)\n this.#end += text.length\n this.#ends.push(this.#end)\n }\n return { at, len: rows.length }\n }\n\n /**\n * Adds the `key` cells of `rows` as numbers: safe integers in minor\n * units (cents), `null` and `undefined` as empty cells.\n */\n values(rows: readonly object[], key: string, path: string): ChannelRange {\n this.#used = true\n const at = this.#values.length\n for (let index = 0; index < rows.length; index++) {\n const value = (rows[index] as Record<string, unknown>)[key]\n if (value === null || value === undefined) {\n this.#values.push(Number.NaN)\n continue\n }\n const number = typeof value === 'bigint' ? Number(value) : value\n if (\n typeof number !== 'number' ||\n !Number.isSafeInteger(number) ||\n (typeof value === 'bigint' && BigInt(number) !== value)\n ) {\n throw invalid(\n `column \"${key}\" holds integers in minor units (cents) within ±Number.MAX_SAFE_INTEGER; row ${index} has ${typeof value === 'number' || typeof value === 'bigint' ? String(value) : describe(value)}`,\n `${path}.data[${index}].${key}`,\n )\n }\n this.#values.push(number)\n }\n return { at, len: rows.length }\n }\n\n /**\n * Encodes the buffers and adds them with `add`, which returns their\n * asset index; `undefined` if no data table added a column.\n */\n finish(add: (buffer: Uint8Array) => number): DataChannel | undefined {\n if (!this.#used) return undefined\n const joined = this.#texts.join('')\n // One byte per code unit is exact for ASCII; grow once otherwise.\n let text = new Uint8Array(joined.length)\n const first = utf8.encodeInto(joined, text)\n let { written } = first\n if (first.read < joined.length) {\n const grown = new Uint8Array(\n written + (joined.length - first.read) * 3,\n )\n grown.set(text.subarray(0, written))\n written += utf8.encodeInto(\n joined.slice(first.read),\n grown.subarray(written),\n ).written\n text = grown\n }\n const ends = bytesOf(\n Uint32Array.from(this.#ends),\n (view, offset, value) => view.setUint32(offset, value, true),\n )\n const values = bytesOf(\n Float64Array.from(this.#values),\n (view, offset, value) => view.setFloat64(offset, value, true),\n )\n return {\n text: add(text.subarray(0, written)),\n ends: add(ends),\n values: add(values),\n }\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"channel.js","sourceRoot":"","sources":["../../web/src/engine/channel.ts"],"names":[],"mappings":"AAaA,IAAI,IAA6B,CAAA;AAEjC,kFAAkF;AAClF,IAAI,YAAiC,CAAA;AAErC,MAAM,OAAO,GAAG,CAAC,OAAe,EAAE,IAAY,EAAS,EAAE;AACrD,yDAAyD;AACzD,IAAI,KAAK,CAAC,qBAAqB,OAAO,QAAQ,IAAI,GAAG,CAAC,CAAA;AAE1D,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAU,EAAE,CACxC,KAAK,YAAY,IAAI;IACjB,CAAC,CAAC,QAAQ;IACV,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;QACpB,CAAC,CAAC,UAAU;QACZ,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ;YACzB,CAAC,CAAC,WAAW;YACb,CAAC,CAAC,OAAO,KAAK,KAAK,UAAU;gBAC3B,CAAC,CAAC,YAAY;gBACd,CAAC,CAAC,KAAK,OAAO,KAAK,EAAE,CAAA;AAInC,8EAA8E;AAC9E,SAAS,OAAO,CACZ,KAAQ,EACR,MAAc,EACd,QAA+B;IAE/B,IAAI,KAAK,CAAC,MAAM,IAAI,MAAM;QAAE,OAAO,KAAK,CAAA;IACxC,MAAM,IAAI,GAAG,QAAQ,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,EAAE,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAA;IACzD,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,CAAA;IACf,OAAO,IAAI,CAAA;AACf,CAAC;AAED,MAAM,UAAU,GAAG,CAAC,MAAc,EAA4B,EAAE,CAC5D,IAAI,WAAW,CAAC,MAAM,CAAC,CAAA;AAC3B,MAAM,UAAU,GAAG,CAAC,MAAc,EA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U;YAC7B,CAAC,CAAC,KAAK;YACP,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CACpC,CAAC,IAAI,CAAC,EAAE,CAAC,CACb,CAAA;QACD,MAAM,IAAI,GAAG,OAAO,CAChB,IAAI,CAAC,KAAK,EACV,IAAI,CAAC,UAAU,EACf,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAC/D,CAAA;QACD,MAAM,MAAM,GAAG,OAAO,CAClB,IAAI,CAAC,OAAO,EACZ,IAAI,CAAC,YAAY,EACjB,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,KAAK,EAAE,IAAI,CAAC,CAChE,CAAA;QACD,OAAO;YACH,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;YACf,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;YACf,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;SACtB,CAAA;IACL,CAAC;CACJ","sourcesContent":["// The columnar channel (D-06): the cells of data-driven tables travel as\n// three buffers next to the IR JSON instead of inside it. Text cells are\n// joined and UTF-8 encoded, with their UTF-16 end offsets\n// in a `Uint32Array`; number cells (integer minor units) go into a\n// `Float64Array`, NaN for empty cells. Columns reference their cells as\n// `{ at, len }`. Rust validates everything again; the checks here give\n// errors with the row and key instead of a channel offset.\nimport type { Document, TableColumn } from '../ir/ir.generated.ts'\nimport type { TableColumnInput } from '../ir/Types/input.ts'\n\nexport type ChannelRange = NonNullable<TableColumn['text']>\nexport type DataChannel = NonNullable<Document['channel']>\n\nlet utf8: TextEncoder | undefined\n\n/** Typed arrays use the platform's byte order; the engine reads little-endian. */\nlet littleEndian: boolean | undefined\n\nconst invalid = (message: string, path: string): Error =>\n // The engine's error format, so the JSX source is added.\n new Error(`INVALID_DOCUMENT: ${message} (at ${path})`)\n\nconst describe = (value: unknown): string =>\n value instanceof Date\n ? 'a Date'\n : Array.isArray(value)\n ? 'an array'\n : typeof value === 'object'\n ? 'an object'\n : typeof value === 'function'\n ? 'a function'\n : `a ${typeof value}`\n\ntype NumericCells = Uint32Array<ArrayBuffer> | Float64Array<ArrayBuffer>\n\n/** Reserve a complete column while keeping growth amortized across tables. */\nfunction reserve<T extends NumericCells>(\n array: T,\n length: number,\n allocate: (length: number) => T,\n): T {\n if (array.length >= length) return array\n const next = allocate(Math.max(length, array.length * 2))\n next.set(array)\n return next\n}\n\nconst endOffsets = (length: number): Uint32Array<ArrayBuffer> =>\n new Uint32Array(length)\nconst minorUnits = (length: number): Float64Array<ArrayBuffer> =>\n new Float64Array(length)\n\n/** The exact bytes of a typed array, little-endian on every platform. */\nconst bytesOf = (\n array: NumericCells,\n length: number,\n write: (view: DataView, offset: number, value: number) => void,\n): Uint8Array<ArrayBuffer> => {\n if (\n (littleEndian ??= new Uint8Array(new Uint32Array([1]).buffer)[0] === 1)\n ) {\n const exact = array.length === length ? array : array.slice(0, length)\n return new Uint8Array(exact.buffer)\n }\n const bytes = new Uint8Array(length * array.BYTES_PER_ELEMENT)\n const view = new DataView(bytes.buffer)\n for (const [index, value] of array.subarray(0, length).entries()) {\n write(view, index * array.BYTES_PER_ELEMENT, value)\n }\n return bytes\n}\n\nfunction encodeText(text: string): Uint8Array<ArrayBuffer> {\n const encoder = (utf8 ??= new TextEncoder())\n if (/\\P{ASCII}/u.test(text)) return encoder.encode(text)\n // An ASCII code unit takes exactly one byte. Encode directly into the\n // buffer the worker will transfer.\n const bytes = new Uint8Array(text.length)\n encoder.encodeInto(text, bytes)\n return bytes\n}\n\n/** Where a data table's column cells go: the channel, or a cache in front of it. */\nexport interface ChannelCells {\n text(rows: readonly object[], key: string, path: string): ChannelRange\n values(rows: readonly object[], key: string, path: string): ChannelRange\n}\n\n/** Whether a data table column holds numbers in minor units (`format` or `sum: true`). */\nconst isNumberColumn = (column: TableColumnInput): boolean =>\n column.format !== undefined || column.sum === true\n\n/**\n * Writes a data table's cells to the channel, column by column: the row\n * count and the columns with their cell ranges, as the IR has them. `path`\n * is the table's IR path for error messages.\n */\nexport function encodeDataTable(\n data: unknown,\n columns: readonly TableColumnInput[],\n cells: ChannelCells,\n path: string,\n): { rows: number; columns: TableColumn[] } {\n if (!Array.isArray(data)) {\n throw invalid(\n 'table data must be an array of row objects',\n `${path}.data`,\n )\n }\n const rows = data as readonly object[]\n const encoded = columns.map((column, index): TableColumn => {\n const { key } = column\n if (typeof key !== 'string') {\n throw invalid(\n 'a column of a data table needs the `key` of the row property it shows',\n `${path}.columns[${index}].key`,\n )\n }\n return isNumberColumn(column)\n ? { ...column, values: cells.values(rows, key, path) }\n : { ...column, text: cells.text(rows, key, path) }\n })\n return { rows: rows.length, columns: encoded }\n}\n\n/** Collects the cells of a document's data tables. */\nexport class ColumnarChannel implements ChannelCells {\n readonly #texts: string[] = []\n #ends = endOffsets(0)\n #values = minorUnits(0)\n #textCells = 0\n #numberCells = 0\n\n /** Whether any column added cells (or an empty column was added). */\n #used = false\n\n /**\n * Adds the `key` cells as text. Numbers and bigints use `String()`.\n * Null and undefined produce empty cells.\n */\n text(rows: readonly object[], key: string, path: string): ChannelRange {\n this.#used = true\n const at = this.#textCells\n const length = rows.length\n const ends = (this.#ends = reserve(this.#ends, at + length, endOffsets))\n const texts = this.#texts\n let end = ends[at - 1] ?? 0\n for (let index = 0; index < length; index++) {\n const value = (rows[index] as Record<string, unknown>)[key]\n let text: string\n if (typeof value === 'string') {\n // Repair a trailing high surrogate before joining cells so it\n // cannot pair with the next cell's low surrogate. UTF-8 encoding\n // repairs the remaining lone surrogates.\n const last = value.charCodeAt(value.length - 1)\n text =\n last >= 0xd800 && last <= 0xdbff\n ? value.toWellFormed()\n : value\n } else if (typeof value === 'number' || typeof value === 'bigint') {\n text = String(value)\n } else if (value === null || value === undefined) {\n text = ''\n } else {\n throw invalid(\n `column \"${key}\" shows strings and numbers; row ${index} has ${describe(value)}, format it as a string first`,\n `${path}.data[${index}].${key}`,\n )\n }\n texts[at + index] = text\n end += text.length\n ends[at + index] = end\n }\n this.#textCells += length\n texts.length = this.#textCells\n return { at, len: length }\n }\n\n /**\n * Adds the `key` cells of `rows` as numbers: safe integers in minor\n * units (cents), `null` and `undefined` as empty cells.\n */\n values(rows: readonly object[], key: string, path: string): ChannelRange {\n this.#used = true\n const at = this.#numberCells\n const length = rows.length\n const values = (this.#values = reserve(\n this.#values,\n at + length,\n minorUnits,\n ))\n for (let index = 0; index < length; index++) {\n const value = (rows[index] as Record<string, unknown>)[key]\n if (value === null || value === undefined) {\n values[at + index] = Number.NaN\n continue\n }\n const number = typeof value === 'bigint' ? Number(value) : value\n if (\n typeof number !== 'number' ||\n !Number.isSafeInteger(number) ||\n (typeof value === 'bigint' && BigInt(number) !== value)\n ) {\n throw invalid(\n `column \"${key}\" holds integers in minor units (cents) within ±Number.MAX_SAFE_INTEGER; row ${index} has ${typeof value === 'number' || typeof value === 'bigint' ? String(value) : describe(value)}`,\n `${path}.data[${index}].${key}`,\n )\n }\n values[at + index] = number\n }\n this.#numberCells += length\n return { at, len: length }\n }\n\n /**\n * Encodes the buffers and adds them with `add`, which returns their\n * asset index; `undefined` if no data table added a column.\n */\n finish(\n add: (buffer: Uint8Array<ArrayBuffer>) => number,\n ): DataChannel | undefined {\n if (!this.#used) return undefined\n const texts = this.#texts\n const text = encodeText(\n (texts.length === this.#textCells\n ? texts\n : texts.slice(0, this.#textCells)\n ).join(''),\n )\n const ends = bytesOf(\n this.#ends,\n this.#textCells,\n (view, offset, value) => view.setUint32(offset, value, true),\n )\n const values = bytesOf(\n this.#values,\n this.#numberCells,\n (view, offset, value) => view.setFloat64(offset, value, true),\n )\n return {\n text: add(text),\n ends: add(ends),\n values: add(values),\n }\n }\n}\n"]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Engine, EngineOptions } from './Types/engine.
|
|
1
|
+
import type { Engine, EngineOptions } from './Types/engine.js';
|
|
2
2
|
/**
|
|
3
3
|
* Creates an engine with its fonts; reuse it for every render. The engine
|
|
4
4
|
* runs natively where the addon loads and on WebAssembly otherwise (see
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"create-engine.d.ts","sourceRoot":"","sources":["../../web/src/engine/create-engine.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"create-engine.d.ts","sourceRoot":"","sources":["../../web/src/engine/create-engine.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,MAAM,EAAE,aAAa,EAAe,MAAM,mBAAmB,CAAA;AAa3E;;;;GAIG;AACH,wBAAsB,YAAY,CAC9B,OAAO,GAAE,aAAkB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAqIjB"}
|
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import { raceAbort, throwIfAborted } from './abort.js';
|
|
2
|
-
import {
|
|
2
|
+
import { loadBackend } from './backend.js';
|
|
3
3
|
import { loadDefaultFonts } from './default-fonts.js';
|
|
4
4
|
import { encodeDocument } from './encode.js';
|
|
5
5
|
import { toRentnerPdfError } from './errors.js';
|
|
6
6
|
import { fontBytes } from './fonts.js';
|
|
7
7
|
import { ASYNC_DISPOSE, DISPOSE, EngineLifecycle } from './lifecycle.js';
|
|
8
8
|
import { registerBackend } from './markup.js';
|
|
9
|
-
import { pdfBytes } from './pdf-bytes.js';
|
|
10
9
|
import { engineThemeWarnings, prepareEngineOptions, prepareInput as prepare, prepareInputSync as prepareSync, } from './prepare.js';
|
|
11
10
|
import { renderBatch } from './render-many.js';
|
|
12
|
-
import { toRenderResult } from './render-result.js';
|
|
11
|
+
import { pdfBytes, toRenderResult } from './render-result.js';
|
|
13
12
|
import { pdfStream } from './render-stream.js';
|
|
14
13
|
const rethrow = (error, locate) => {
|
|
15
14
|
throw toRentnerPdfError(error, locate);
|
|
@@ -21,12 +20,19 @@ const rethrow = (error, locate) => {
|
|
|
21
20
|
*/
|
|
22
21
|
export async function createEngine(options = {}) {
|
|
23
22
|
const prepared = prepareEngineOptions(options);
|
|
24
|
-
const
|
|
23
|
+
const loadingBackend = loadBackend(options.backend ?? 'auto', options.wasmModule);
|
|
24
|
+
const loadingFonts = prepared.defaultFont ? loadDefaultFonts() : [];
|
|
25
25
|
let backend;
|
|
26
26
|
try {
|
|
27
|
-
|
|
27
|
+
const [defaultFonts, createBackend] = await Promise.all([
|
|
28
|
+
loadingFonts,
|
|
29
|
+
loadingBackend,
|
|
30
|
+
]);
|
|
31
|
+
backend = createBackend({ ...prepared.settings, defaultFonts });
|
|
28
32
|
}
|
|
29
33
|
catch (error) {
|
|
34
|
+
// Font read errors take precedence even when the binding fails first.
|
|
35
|
+
await loadingFonts;
|
|
30
36
|
return rethrow(error);
|
|
31
37
|
}
|
|
32
38
|
const themeWarnings = engineThemeWarnings(prepared.themeWarnings, backend.themeWarnings);
|