@rentnerkev/pdf 1.0.0 → 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 +23 -7
- package/THIRD_PARTY_NOTICES.md +1 -1
- package/dist/ai.d.ts +53 -0
- package/dist/ai.d.ts.map +1 -0
- package/dist/ai.js +144 -0
- package/dist/ai.js.map +1 -0
- 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 +1606 -0
- package/package.json +9 -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/abort.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"abort.js","sourceRoot":"","sources":["../../web/src/engine/abort.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,4EAA4E;AAC5E,2CAA2C;AAC3C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAA;AAE7C,8EAA8E;AAC9E,MAAM,CAAC,MAAM,YAAY,GAAG,CAAC,MAAe,EAAmB,EAAE,CAC7D,MAAM,YAAY,eAAe,IAAI,MAAM,CAAC,IAAI,KAAK,SAAS;IAC1D,CAAC,CAAC,MAAM;IACR,CAAC,CAAC,IAAI,eAAe,CAAC,SAAS,EAAE,wBAAwB,EAAE,SAAS,EAAE;QAChE,KAAK,EAAE,MAAM;KAChB,CAAC,CAAA;AAEZ,4DAA4D;AAC5D,MAAM,UAAU,cAAc,CAAC,MAA+B;IAC1D,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI;QAAE,MAAM,YA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,OAAO,CAAC,CAAA;IAC3E,CAAC;AACL,CAAC;AASD;;;;GAIG;AACH,MAAM,UAAU,QAAQ,CACpB,MAA+B,EAC/B,SAA6B,EAC7B,OAAO,GAAG,YAAY;IAEtB,MAAM,UAAU,GAAG,IAAI,eAAe,EAAE,CAAA;IACxC,MAAM,KAAK,GAAG,CAAC,MAAe,EAAQ,EAAE;QACpC,IAAI,CAAC,UAAU,CAAC,MAAM,CAAC,OAAO;YAAE,UAAU,CAAC,KAAK,CAAC,MAAM,CAAC,CAAA;IAC5D,CAAC,CAAA;IACD,MAAM,OAAO,GAAG,GAAS,EAAE,CAAC,KAAK,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IACjD,IAAI,MAAM,EAAE,OAAO,KAAK,IAAI;QAAE,KAAK,CAAC,MAAM,CAAC,MAAM,CAAC,CAAA;;QAC7C,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAA;IAC/D,MAAM,KAAK,GACP,SAAS,KAAK,SAAS;QACnB,CAAC,CAAC,SAAS;QACX,CAAC,CAAC,UAAU,CAAC,GAAG,EAAE;YACZ,KAAK,CACD,IAAI,eAAe,CACf,SAAS,EACT,GAAG,OAAO,0BAA0B,SAAS,iBAAiB,CACjE,CACJ,CAAA;QACL,CAAC,EAAE,SAAS,CAAC,CAAA;IACvB,OAAO;QACH,MAAM,EAAE,UAAU,CAAC,MAAM;QACzB,KAAK,EAAE,GAAG,EAAE;YACR,IAAI,KAAK,KAAK,SAAS;gBAAE,YAAY,CAAC,KAAK,CAAC,CAAA;YAC5C,MAAM,EAAE,mBAAmB,CAAC,OAAO,EAAE,OAAO,CAAC,CAAA;QACjD,CAAC;KACJ,CAAA;AACL,CAAC","sourcesContent":["// Cancellation for renders (concurrency model): an aborted signal rejects\n// at once with `ABORTED`, while the native engine stops at its next page or\n// paragraph on its own (see the backends).\nimport { RentnerPdfError } from './errors.ts'\n\n/** The `ABORTED` error for an abort `reason`; an `ABORTED` reason is kept. */\nexport const abortedError = (reason: unknown): RentnerPdfError =>\n reason instanceof RentnerPdfError && reason.code === 'ABORTED'\n ? reason\n : new RentnerPdfError('ABORTED', 'the render was aborted', undefined, {\n cause: reason,\n })\n\n/** Throws the `ABORTED` error when `signal` has aborted. */\nexport function throwIfAborted(signal: AbortSignal | undefined): void {\n if (signal?.aborted === true) throw abortedError(signal.reason)\n}\n\n/**\n * Settles like `promise`, but rejects with `ABORTED` as soon as `signal`\n * aborts. The work behind `promise` must stop on its own.\n */\nexport async function raceAbort<T>(\n promise: Promise<T>,\n signal: AbortSignal | undefined,\n): Promise<T> {\n if (signal === undefined) return promise\n if (signal.aborted) {\n // The work stops on its own; its rejection is expected.\n promise.catch(() => undefined)\n throw abortedError(signal.reason)\n }\n let onAbort: (() => void) | undefined\n const aborted = new Promise<never>((_, reject) => {\n onAbort = () => reject(abortedError(signal.reason))\n signal.addEventListener('abort', onAbort, { once: true })\n })\n try {\n return await Promise.race([promise, aborted])\n } finally {\n if (onAbort !== undefined) signal.removeEventListener('abort', onAbort)\n }\n}\n\n/** A signal that aborts with `signal` or after `timeoutMs` milliseconds. */\nexport interface Deadline {\n readonly signal: AbortSignal\n /** Stops the timer and the link to the outer signal. */\n clear(): void\n}\n\n/**\n * Links an optional outer `signal` and an optional timeout into one signal.\n * The timer is ref'd: a render that never finishes fails instead of\n * letting the process exit with an unsettled promise.\n */\nexport function deadline(\n signal: AbortSignal | undefined,\n timeoutMs: number | undefined,\n subject = 'the render',\n): Deadline {\n const controller = new AbortController()\n const abort = (reason: unknown): void => {\n if (!controller.signal.aborted) controller.abort(reason)\n }\n const onAbort = (): void => abort(signal?.reason)\n if (signal?.aborted === true) abort(signal.reason)\n else signal?.addEventListener('abort', onAbort, { once: true })\n const timer =\n timeoutMs === undefined\n ? undefined\n : setTimeout(() => {\n abort(\n new RentnerPdfError(\n 'ABORTED',\n `${subject} did not finish within ${timeoutMs} ms (timeoutMs)`,\n ),\n )\n }, timeoutMs)\n return {\n signal: controller.signal,\n clear: () => {\n if (timer !== undefined) clearTimeout(timer)\n signal?.removeEventListener('abort', onAbort)\n },\n }\n}\n"]}
|
package/dist/engine/assets.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AttachmentSource, ImageSource, SvgSource } from '../ir/Types/input.
|
|
1
|
+
import type { AttachmentSource, ImageSource, SvgSource } from '../ir/Types/input.js';
|
|
2
2
|
/** Whether `value` is file bytes: a `Uint8Array` (or `Buffer`) or an `ArrayBuffer`. */
|
|
3
3
|
export declare const isImageSource: (value: unknown) => value is ImageSource;
|
|
4
4
|
/** Whether `value` is an attachment's content: bytes or a string. */
|
|
@@ -10,9 +10,10 @@ export declare const isAttachmentSource: (value: unknown) => value is Attachment
|
|
|
10
10
|
export declare class Assets {
|
|
11
11
|
#private;
|
|
12
12
|
readonly buffers: Uint8Array[];
|
|
13
|
+
constructor(transfer?: ArrayBuffer[]);
|
|
13
14
|
index(source: SvgSource): number;
|
|
14
|
-
/** Adds a buffer that
|
|
15
|
-
|
|
15
|
+
/** Adds a fresh encoder buffer that can be transferred to a worker. */
|
|
16
|
+
pushOwned(buffer: Uint8Array<ArrayBuffer>): number;
|
|
16
17
|
/**
|
|
17
18
|
* Adds an attachment's content as a buffer of its own: the engine
|
|
18
19
|
* embeds it as it is, even when a node shows the same bytes.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../../web/src/engine/assets.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACR,gBAAgB,EAChB,WAAW,EACX,SAAS,EACZ,MAAM,sBAAsB,CAAA;AAI7B,uFAAuF;AACvF,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,KAAK,IAAI,WACO,CAAA;AAE/D,qEAAqE;AACrE,eAAO,MAAM,kBAAkB,UAAW,OAAO,KAAG,KAAK,IAAI,gBACR,CAAA;
|
|
1
|
+
{"version":3,"file":"assets.d.ts","sourceRoot":"","sources":["../../web/src/engine/assets.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACR,gBAAgB,EAChB,WAAW,EACX,SAAS,EACZ,MAAM,sBAAsB,CAAA;AAI7B,uFAAuF;AACvF,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,KAAK,IAAI,WACO,CAAA;AAE/D,qEAAqE;AACrE,eAAO,MAAM,kBAAkB,UAAW,OAAO,KAAG,KAAK,IAAI,gBACR,CAAA;AAErD;;;GAGG;AACH,qBAAa,MAAM;;IACf,QAAQ,CAAC,OAAO,EAAE,UAAU,EAAE,CAAK;IAInC,YAAY,QAAQ,CAAC,EAAE,WAAW,EAAE,EAEnC;IAED,KAAK,CAAC,MAAM,EAAE,SAAS,GAAG,MAAM,CAO/B;IAED,uEAAuE;IACvE,SAAS,CAAC,MAAM,EAAE,UAAU,CAAC,WAAW,CAAC,GAAG,MAAM,CAIjD;IAcD;;;OAGG;IACH,UAAU,CAAC,MAAM,EAAE,gBAAgB,GAAG,MAAM,CAE3C;CACJ"}
|
package/dist/engine/assets.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
let utf8;
|
|
2
2
|
/** Whether `value` is file bytes: a `Uint8Array` (or `Buffer`) or an `ArrayBuffer`. */
|
|
3
3
|
export const isImageSource = (value) => value instanceof Uint8Array || value instanceof ArrayBuffer;
|
|
4
4
|
/** Whether `value` is an attachment's content: bytes or a string. */
|
|
5
5
|
export const isAttachmentSource = (value) => typeof value === 'string' || isImageSource(value);
|
|
6
|
-
const bytesOf = (source) => typeof source === 'string'
|
|
7
|
-
? utf8.encode(source)
|
|
8
|
-
: source instanceof Uint8Array
|
|
9
|
-
? source
|
|
10
|
-
: new Uint8Array(source);
|
|
11
6
|
/**
|
|
12
7
|
* Collects asset buffers; the same buffer object, or the same SVG string,
|
|
13
8
|
* gets one index.
|
|
@@ -15,26 +10,39 @@ const bytesOf = (source) => typeof source === 'string'
|
|
|
15
10
|
export class Assets {
|
|
16
11
|
buffers = [];
|
|
17
12
|
#indices = new Map();
|
|
13
|
+
#transfer;
|
|
14
|
+
constructor(transfer) {
|
|
15
|
+
this.#transfer = transfer;
|
|
16
|
+
}
|
|
18
17
|
index(source) {
|
|
19
18
|
let index = this.#indices.get(source);
|
|
20
19
|
if (index === undefined) {
|
|
21
|
-
index = this
|
|
22
|
-
this.buffers.push(bytesOf(source));
|
|
20
|
+
index = this.#pushSource(source);
|
|
23
21
|
this.#indices.set(source, index);
|
|
24
22
|
}
|
|
25
23
|
return index;
|
|
26
24
|
}
|
|
27
|
-
/** Adds a buffer that
|
|
28
|
-
|
|
25
|
+
/** Adds a fresh encoder buffer that can be transferred to a worker. */
|
|
26
|
+
pushOwned(buffer) {
|
|
27
|
+
this.#transfer?.push(buffer.buffer);
|
|
29
28
|
this.buffers.push(buffer);
|
|
30
29
|
return this.buffers.length - 1;
|
|
31
30
|
}
|
|
31
|
+
#pushSource(source) {
|
|
32
|
+
if (typeof source === 'string') {
|
|
33
|
+
return this.pushOwned((utf8 ??= new TextEncoder()).encode(source));
|
|
34
|
+
}
|
|
35
|
+
// Keep caller-owned bytes out of the transfer list, including views
|
|
36
|
+
// that cover the entire buffer.
|
|
37
|
+
this.buffers.push(source instanceof Uint8Array ? source : new Uint8Array(source));
|
|
38
|
+
return this.buffers.length - 1;
|
|
39
|
+
}
|
|
32
40
|
/**
|
|
33
41
|
* Adds an attachment's content as a buffer of its own: the engine
|
|
34
42
|
* embeds it as it is, even when a node shows the same bytes.
|
|
35
43
|
*/
|
|
36
44
|
attachment(source) {
|
|
37
|
-
return this
|
|
45
|
+
return this.#pushSource(source);
|
|
38
46
|
}
|
|
39
47
|
}
|
|
40
48
|
//# sourceMappingURL=assets.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"assets.js","sourceRoot":"","sources":["../../web/src/engine/assets.ts"],"names":[],"mappings":"AAUA,
|
|
1
|
+
{"version":3,"file":"assets.js","sourceRoot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,CAAC,IAAI,KAAK,IAAI,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAA;QACtE,CAAC;QACD,oEAAoE;QACpE,gCAAgC;QAChC,IAAI,CAAC,OAAO,CAAC,IAAI,CACb,MAAM,YAAY,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CACjE,CAAA;QACD,OAAO,IAAI,CAAC,OAAO,CAAC,MAAM,GAAG,CAAC,CAAA;IAClC,CAAC;IAED;;;OAGG;IACH,UAAU,CAAC,MAAwB;QAC/B,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAA;IACnC,CAAC;CACJ","sourcesContent":["// The asset buffers of a render (D-06): image bytes, SVG markup, letterhead\n// PDFs and attachments travel next to the document, which references them\n// by index. Both the IR encoder and the React path's per-render registry\n// collect them here.\nimport type {\n AttachmentSource,\n ImageSource,\n SvgSource,\n} from '../ir/Types/input.ts'\n\nlet utf8: TextEncoder | undefined\n\n/** Whether `value` is file bytes: a `Uint8Array` (or `Buffer`) or an `ArrayBuffer`. */\nexport const isImageSource = (value: unknown): value is ImageSource =>\n value instanceof Uint8Array || value instanceof ArrayBuffer\n\n/** Whether `value` is an attachment's content: bytes or a string. */\nexport const isAttachmentSource = (value: unknown): value is AttachmentSource =>\n typeof value === 'string' || isImageSource(value)\n\n/**\n * Collects asset buffers; the same buffer object, or the same SVG string,\n * gets one index.\n */\nexport class Assets {\n readonly buffers: Uint8Array[] = []\n readonly #indices = new Map<SvgSource, number>()\n readonly #transfer?: ArrayBuffer[]\n\n constructor(transfer?: ArrayBuffer[]) {\n this.#transfer = transfer\n }\n\n index(source: SvgSource): number {\n let index = this.#indices.get(source)\n if (index === undefined) {\n index = this.#pushSource(source)\n this.#indices.set(source, index)\n }\n return index\n }\n\n /** Adds a fresh encoder buffer that can be transferred to a worker. */\n pushOwned(buffer: Uint8Array<ArrayBuffer>): number {\n this.#transfer?.push(buffer.buffer)\n this.buffers.push(buffer)\n return this.buffers.length - 1\n }\n\n #pushSource(source: SvgSource): number {\n if (typeof source === 'string') {\n return this.pushOwned((utf8 ??= new TextEncoder()).encode(source))\n }\n // Keep caller-owned bytes out of the transfer list, including views\n // that cover the entire buffer.\n this.buffers.push(\n source instanceof Uint8Array ? source : new Uint8Array(source),\n )\n return this.buffers.length - 1\n }\n\n /**\n * Adds an attachment's content as a buffer of its own: the engine\n * embeds it as it is, even when a node shows the same bytes.\n */\n attachment(source: AttachmentSource): number {\n return this.#pushSource(source)\n }\n}\n"]}
|
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,E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ACpB,MAAM,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QAC3C,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;IACxD,CAAC;IACD,IAAI,MAAqB,CAAA;IACzB,IAAI,CAAC;QACD,MAAM,GAAG,OAAO,CAAC,MAAM,EAAE,CAAA;IAC7B,CAAC;IAAC,OAAO,WAAW,EAAE,CAAC;QACnB,IAAI,MAAM,KAAK,QAAQ;YAAE,MAAM,WAAW,CAAA;QAC1C,IAAI,IAAiB,CAAA;QACrB,IAAI,CAAC;YACD,IAAI,GAAG,MAAM,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,CAAA;QACzC,CAAC;QAAC,OAAO,SAAS,EAAE,CAAC;YACjB,MAAM,IAAI,cAAc,CACpB,CAAC,WAAW,EAAE,SAAS,CAAC,EACxB,6DAA6D,EAC7D,EAAE,KAAK,EAAE,SAAS,EAAE,CACvB,CAAA;QACL,CAAC;QACD,OAAO,CAAC,OAAO,EAAE,EAAE;YACf,IAAI,CAAC,gBAAgB,EAAE,CAAC;gBACpB,gBAAgB,GAAG,IAAI,CAAA;gBACvB,OAAO,CAAC,IAAI,CACR,yFAAyF,MAAM,CAAC,WAAW,CAAC,EAAE,CACjH,CAAA;YACL,CAAC;YACD,OAAO,iBAAiB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAA;QAC3C,CAAC,CAAA;IACL,CAAC;IACD,OAAO,CAAC,OAAO,EAAE,EAAE,CAAC,mBAAmB,CAAC,MAAM,EAAE,OAAO,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,I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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,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,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Aa,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
|
};
|