@zilijonas/foliark 0.0.0-stage → 0.1.1
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/COMMERCIAL-LICENSING.md +33 -0
- package/LICENSE +235 -0
- package/LICENSE_FOXIT +27 -0
- package/LICENSING.md +55 -0
- package/NOTICE +14 -0
- package/README.md +130 -2
- package/SUPPORT_MATRIX.md +126 -0
- package/THIRD_PARTY_NOTICES.md +3603 -0
- package/cmaps/83pv-RKSJ-H.cmap +312 -0
- package/cmaps/90ms-RKSJ-H.cmap +257 -0
- package/cmaps/90ms-RKSJ-V.cmap +154 -0
- package/cmaps/90msp-RKSJ-H.cmap +255 -0
- package/cmaps/90msp-RKSJ-V.cmap +153 -0
- package/cmaps/90pv-RKSJ-H.cmap +353 -0
- package/cmaps/Add-RKSJ-H.cmap +736 -0
- package/cmaps/Add-RKSJ-V.cmap +133 -0
- package/cmaps/Adobe-CNS1-UCS2.cmap +18303 -0
- package/cmaps/Adobe-GB1-UCS2.cmap +14268 -0
- package/cmaps/Adobe-Japan1-UCS2.cmap +19160 -0
- package/cmaps/Adobe-Korea1-UCS2.cmap +9265 -0
- package/cmaps/B5pc-H.cmap +335 -0
- package/cmaps/B5pc-V.cmap +88 -0
- package/cmaps/CNS-EUC-H.cmap +488 -0
- package/cmaps/CNS-EUC-V.cmap +536 -0
- package/cmaps/ETen-B5-H.cmap +341 -0
- package/cmaps/ETen-B5-V.cmap +89 -0
- package/cmaps/ETenms-B5-H.cmap +77 -0
- package/cmaps/ETenms-B5-V.cmap +97 -0
- package/cmaps/EUC-H.cmap +205 -0
- package/cmaps/EUC-V.cmap +103 -0
- package/cmaps/Ext-RKSJ-H.cmap +766 -0
- package/cmaps/Ext-RKSJ-V.cmap +115 -0
- package/cmaps/GB-EUC-H.cmap +171 -0
- package/cmaps/GB-EUC-V.cmap +96 -0
- package/cmaps/GBK-EUC-H.cmap +4271 -0
- package/cmaps/GBK-EUC-V.cmap +95 -0
- package/cmaps/GBK2K-H.cmap +5517 -0
- package/cmaps/GBK2K-V.cmap +116 -0
- package/cmaps/GBKp-EUC-H.cmap +4270 -0
- package/cmaps/GBKp-EUC-V.cmap +95 -0
- package/cmaps/GBpc-EUC-H.cmap +173 -0
- package/cmaps/GBpc-EUC-V.cmap +96 -0
- package/cmaps/H.cmap +198 -0
- package/cmaps/HKscs-B5-H.cmap +1329 -0
- package/cmaps/HKscs-B5-V.cmap +88 -0
- package/cmaps/KSC-EUC-H.cmap +560 -0
- package/cmaps/KSC-EUC-V.cmap +92 -0
- package/cmaps/KSCms-UHC-H.cmap +774 -0
- package/cmaps/KSCms-UHC-HW-H.cmap +773 -0
- package/cmaps/KSCms-UHC-HW-V.cmap +91 -0
- package/cmaps/KSCms-UHC-V.cmap +92 -0
- package/cmaps/KSCpc-EUC-H.cmap +606 -0
- package/cmaps/LICENSE.txt +28 -0
- package/cmaps/MANIFEST.json +258 -0
- package/cmaps/UniCNS-UCS2-H.cmap +16990 -0
- package/cmaps/UniCNS-UCS2-V.cmap +88 -0
- package/cmaps/UniCNS-UTF16-H.cmap +19146 -0
- package/cmaps/UniCNS-UTF16-V.cmap +92 -0
- package/cmaps/UniGB-UCS2-H.cmap +14319 -0
- package/cmaps/UniGB-UCS2-V.cmap +99 -0
- package/cmaps/UniGB-UTF16-H.cmap +14567 -0
- package/cmaps/UniGB-UTF16-V.cmap +102 -0
- package/cmaps/UniJIS-UCS2-H.cmap +8868 -0
- package/cmaps/UniJIS-UCS2-HW-H.cmap +79 -0
- package/cmaps/UniJIS-UCS2-HW-V.cmap +277 -0
- package/cmaps/UniJIS-UCS2-V.cmap +273 -0
- package/cmaps/UniJIS-UTF16-H.cmap +14483 -0
- package/cmaps/UniJIS-UTF16-V.cmap +305 -0
- package/cmaps/UniKS-UCS2-H.cmap +8723 -0
- package/cmaps/UniKS-UCS2-V.cmap +93 -0
- package/cmaps/UniKS-UTF16-H.cmap +8893 -0
- package/cmaps/UniKS-UTF16-V.cmap +97 -0
- package/cmaps/V.cmap +103 -0
- package/dist/client.d.ts +48 -0
- package/dist/client.js +200 -0
- package/dist/document.d.ts +100 -0
- package/dist/document.js +108 -0
- package/dist/generated/AnnotationPolicy.d.ts +1 -0
- package/dist/generated/AnnotationPolicy.js +2 -0
- package/dist/generated/Capabilities.d.ts +9 -0
- package/dist/generated/Capabilities.js +1 -0
- package/dist/generated/Capability.d.ts +9 -0
- package/dist/generated/Capability.js +1 -0
- package/dist/generated/CapabilityReason.d.ts +5 -0
- package/dist/generated/CapabilityReason.js +1 -0
- package/dist/generated/CapabilityStatus.d.ts +1 -0
- package/dist/generated/CapabilityStatus.js +2 -0
- package/dist/generated/ChangeResult.d.ts +12 -0
- package/dist/generated/ChangeResult.js +1 -0
- package/dist/generated/ChangeSet.d.ts +16 -0
- package/dist/generated/ChangeSet.js +1 -0
- package/dist/generated/Cluster.d.ts +12 -0
- package/dist/generated/Cluster.js +1 -0
- package/dist/generated/Collateral.d.ts +5 -0
- package/dist/generated/Collateral.js +1 -0
- package/dist/generated/Ctl.d.ts +3 -0
- package/dist/generated/Ctl.js +1 -0
- package/dist/generated/CtlCancel.d.ts +8 -0
- package/dist/generated/CtlCancel.js +1 -0
- package/dist/generated/CtlEvent.d.ts +1 -0
- package/dist/generated/CtlEvent.js +2 -0
- package/dist/generated/CtlEventMsg.d.ts +8 -0
- package/dist/generated/CtlEventMsg.js +1 -0
- package/dist/generated/DocumentFeatures.d.ts +13 -0
- package/dist/generated/DocumentFeatures.js +2 -0
- package/dist/generated/DocumentInfo.d.ts +12 -0
- package/dist/generated/DocumentInfo.js +1 -0
- package/dist/generated/EditLayout.d.ts +11 -0
- package/dist/generated/EditLayout.js +1 -0
- package/dist/generated/EditPreview.d.ts +35 -0
- package/dist/generated/EditPreview.js +1 -0
- package/dist/generated/EditorFace.d.ts +7 -0
- package/dist/generated/EditorFace.js +2 -0
- package/dist/generated/EditsReport.d.ts +4 -0
- package/dist/generated/EditsReport.js +2 -0
- package/dist/generated/ErrorCode.d.ts +4 -0
- package/dist/generated/ErrorCode.js +2 -0
- package/dist/generated/ErrorDetails.d.ts +20 -0
- package/dist/generated/ErrorDetails.js +2 -0
- package/dist/generated/ErrorWire.d.ts +11 -0
- package/dist/generated/ErrorWire.js +1 -0
- package/dist/generated/ExportMode.d.ts +1 -0
- package/dist/generated/ExportMode.js +2 -0
- package/dist/generated/ExportPlan.d.ts +13 -0
- package/dist/generated/ExportPlan.js +1 -0
- package/dist/generated/ExportPolicy.d.ts +13 -0
- package/dist/generated/ExportPolicy.js +1 -0
- package/dist/generated/ExportReport.d.ts +20 -0
- package/dist/generated/ExportReport.js +1 -0
- package/dist/generated/ExtractOptions.d.ts +7 -0
- package/dist/generated/ExtractOptions.js +2 -0
- package/dist/generated/Flattened.d.ts +5 -0
- package/dist/generated/Flattened.js +2 -0
- package/dist/generated/FoliarkError.d.ts +10 -0
- package/dist/generated/FoliarkError.js +1 -0
- package/dist/generated/FontMatchRule.d.ts +7 -0
- package/dist/generated/FontMatchRule.js +2 -0
- package/dist/generated/FontResolution.d.ts +12 -0
- package/dist/generated/FontResolution.js +1 -0
- package/dist/generated/FontRule.d.ts +1 -0
- package/dist/generated/FontRule.js +2 -0
- package/dist/generated/FontSource.d.ts +10 -0
- package/dist/generated/FontSource.js +1 -0
- package/dist/generated/FontStyle.d.ts +1 -0
- package/dist/generated/FontStyle.js +2 -0
- package/dist/generated/GeneratedOrigin.d.ts +4 -0
- package/dist/generated/GeneratedOrigin.js +2 -0
- package/dist/generated/Hit.d.ts +7 -0
- package/dist/generated/Hit.js +1 -0
- package/dist/generated/InstancePath.d.ts +5 -0
- package/dist/generated/InstancePath.js +1 -0
- package/dist/generated/InstanceStep.d.ts +9 -0
- package/dist/generated/InstanceStep.js +1 -0
- package/dist/generated/InvisibleTextPolicy.d.ts +1 -0
- package/dist/generated/InvisibleTextPolicy.js +2 -0
- package/dist/generated/KeptReport.d.ts +4 -0
- package/dist/generated/KeptReport.js +2 -0
- package/dist/generated/LayoutRun.d.ts +6 -0
- package/dist/generated/LayoutRun.js +1 -0
- package/dist/generated/Limits.d.ts +18 -0
- package/dist/generated/Limits.js +2 -0
- package/dist/generated/Loss.d.ts +6 -0
- package/dist/generated/Loss.js +1 -0
- package/dist/generated/LossCode.d.ts +1 -0
- package/dist/generated/LossCode.js +2 -0
- package/dist/generated/Matrix.d.ts +4 -0
- package/dist/generated/Matrix.js +2 -0
- package/dist/generated/ObjectId.d.ts +10 -0
- package/dist/generated/ObjectId.js +2 -0
- package/dist/generated/OkFalse.d.ts +1 -0
- package/dist/generated/OkFalse.js +2 -0
- package/dist/generated/OkTrue.d.ts +1 -0
- package/dist/generated/OkTrue.js +2 -0
- package/dist/generated/OpName.d.ts +4 -0
- package/dist/generated/OpName.js +2 -0
- package/dist/generated/Origin.d.ts +12 -0
- package/dist/generated/Origin.js +1 -0
- package/dist/generated/PageGeometry.d.ts +24 -0
- package/dist/generated/PageGeometry.js +1 -0
- package/dist/generated/ParagraphInfo.d.ts +26 -0
- package/dist/generated/ParagraphInfo.js +1 -0
- package/dist/generated/Phase.d.ts +1 -0
- package/dist/generated/Phase.js +2 -0
- package/dist/generated/Point.d.ts +4 -0
- package/dist/generated/Point.js +2 -0
- package/dist/generated/PreviewGlyph.d.ts +13 -0
- package/dist/generated/PreviewGlyph.js +2 -0
- package/dist/generated/PreviewLine.d.ts +10 -0
- package/dist/generated/PreviewLine.js +2 -0
- package/dist/generated/Prog.d.ts +7 -0
- package/dist/generated/Prog.js +1 -0
- package/dist/generated/Progress.d.ts +6 -0
- package/dist/generated/Progress.js +1 -0
- package/dist/generated/Quad.d.ts +5 -0
- package/dist/generated/Quad.js +1 -0
- package/dist/generated/RasterPolicy.d.ts +4 -0
- package/dist/generated/RasterPolicy.js +2 -0
- package/dist/generated/Rect.d.ts +9 -0
- package/dist/generated/Rect.js +2 -0
- package/dist/generated/Redaction.d.ts +18 -0
- package/dist/generated/Redaction.js +1 -0
- package/dist/generated/RedactionReport.d.ts +16 -0
- package/dist/generated/RedactionReport.js +1 -0
- package/dist/generated/RegisteredFont.d.ts +9 -0
- package/dist/generated/RegisteredFont.js +1 -0
- package/dist/generated/RejectedChange.d.ts +7 -0
- package/dist/generated/RejectedChange.js +1 -0
- package/dist/generated/RenderWarning.d.ts +5 -0
- package/dist/generated/RenderWarning.js +1 -0
- package/dist/generated/RenderWarningCode.d.ts +1 -0
- package/dist/generated/RenderWarningCode.js +2 -0
- package/dist/generated/RenderedPageHeader.d.ts +17 -0
- package/dist/generated/RenderedPageHeader.js +1 -0
- package/dist/generated/Req.d.ts +13 -0
- package/dist/generated/Req.js +1 -0
- package/dist/generated/Res.d.ts +3 -0
- package/dist/generated/Res.js +1 -0
- package/dist/generated/ResErr.d.ts +9 -0
- package/dist/generated/ResErr.js +1 -0
- package/dist/generated/ResOk.d.ts +8 -0
- package/dist/generated/ResOk.js +1 -0
- package/dist/generated/RunSource.d.ts +1 -0
- package/dist/generated/RunSource.js +2 -0
- package/dist/generated/SanitizedItem.d.ts +1 -0
- package/dist/generated/SanitizedItem.js +2 -0
- package/dist/generated/SearchHit.d.ts +13 -0
- package/dist/generated/SearchHit.js +1 -0
- package/dist/generated/SearchOptions.d.ts +10 -0
- package/dist/generated/SearchOptions.js +2 -0
- package/dist/generated/SharedKind.d.ts +1 -0
- package/dist/generated/SharedKind.js +2 -0
- package/dist/generated/SharedSurvivor.d.ts +6 -0
- package/dist/generated/SharedSurvivor.js +1 -0
- package/dist/generated/SignedInputPolicy.d.ts +1 -0
- package/dist/generated/SignedInputPolicy.js +2 -0
- package/dist/generated/SourceLoc.d.ts +9 -0
- package/dist/generated/SourceLoc.js +1 -0
- package/dist/generated/StyleFlags.d.ts +6 -0
- package/dist/generated/StyleFlags.js +2 -0
- package/dist/generated/StyleRun.d.ts +18 -0
- package/dist/generated/StyleRun.js +1 -0
- package/dist/generated/TextEdit.d.ts +11 -0
- package/dist/generated/TextEdit.js +1 -0
- package/dist/generated/TextPage.d.ts +17 -0
- package/dist/generated/TextPage.js +1 -0
- package/dist/generated/TextRange.d.ts +11 -0
- package/dist/generated/TextRange.js +1 -0
- package/dist/generated/TextStats.d.ts +9 -0
- package/dist/generated/TextStats.js +2 -0
- package/dist/generated/V1.d.ts +1 -0
- package/dist/generated/V1.js +2 -0
- package/dist/generated/Word.d.ts +27 -0
- package/dist/generated/Word.js +1 -0
- package/dist/generated/WordSource.d.ts +1 -0
- package/dist/generated/WordSource.js +2 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.js +3 -0
- package/dist/inline.d.ts +18 -0
- package/dist/inline.js +33 -0
- package/dist/layout.d.ts +5 -0
- package/dist/layout.js +3 -0
- package/dist/lifecycle.d.ts +7 -0
- package/dist/lifecycle.js +28 -0
- package/dist/protocol.d.ts +162 -0
- package/dist/protocol.js +302 -0
- package/dist/worker-core.d.ts +15 -0
- package/dist/worker-core.js +244 -0
- package/dist/worker-supervisor.d.ts +23 -0
- package/dist/worker-supervisor.js +41 -0
- package/dist/worker.d.ts +1 -0
- package/dist/worker.js +9 -0
- package/package.json +77 -3
- package/wasm/foliark_wasm.d.ts +120 -0
- package/wasm/foliark_wasm.js +1050 -0
- package/wasm/foliark_wasm_bg.wasm +0 -0
- package/wasm/foliark_wasm_bg.wasm.d.ts +32 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
export { createFoliark } from "./client.js";
|
|
2
|
+
export type { Foliark, FoliarkOptions, OpenOptions, ResetReason } from "./client.js";
|
|
3
|
+
export type { DocumentHandle, ExportOptions, ExtractTextOptions, RenderedPage, RenderOptions, SearchOptions, } from "./document.js";
|
|
4
|
+
export { isCurrentPreview } from "./layout.js";
|
|
5
|
+
export type { EditPreview } from "./generated/EditPreview.js";
|
|
6
|
+
export type { PreviewGlyph } from "./generated/PreviewGlyph.js";
|
|
7
|
+
export type { PreviewLine } from "./generated/PreviewLine.js";
|
|
8
|
+
export { FoliarkError } from "./protocol.js";
|
|
9
|
+
export type { Capabilities } from "./generated/Capabilities.js";
|
|
10
|
+
export type { ChangeResult } from "./generated/ChangeResult.js";
|
|
11
|
+
export type { ChangeSet } from "./generated/ChangeSet.js";
|
|
12
|
+
export type { TextEdit } from "./generated/TextEdit.js";
|
|
13
|
+
export type { Redaction } from "./generated/Redaction.js";
|
|
14
|
+
export type { RejectedChange } from "./generated/RejectedChange.js";
|
|
15
|
+
export type { EditLayout } from "./generated/EditLayout.js";
|
|
16
|
+
export type { ExportReport } from "./generated/ExportReport.js";
|
|
17
|
+
export type { Loss } from "./generated/Loss.js";
|
|
18
|
+
export type { LossCode } from "./generated/LossCode.js";
|
|
19
|
+
export type { SanitizedItem } from "./generated/SanitizedItem.js";
|
|
20
|
+
export type { RenderWarning } from "./generated/RenderWarning.js";
|
|
21
|
+
export type { Cluster } from "./generated/Cluster.js";
|
|
22
|
+
export type { DocumentInfo } from "./generated/DocumentInfo.js";
|
|
23
|
+
export type { ErrorCode } from "./generated/ErrorCode.js";
|
|
24
|
+
export type { ErrorDetails } from "./generated/ErrorDetails.js";
|
|
25
|
+
export type { ExportPlan } from "./generated/ExportPlan.js";
|
|
26
|
+
export type { ExportPolicy } from "./generated/ExportPolicy.js";
|
|
27
|
+
export type { FontMatchRule } from "./generated/FontMatchRule.js";
|
|
28
|
+
export type { FontSource } from "./generated/FontSource.js";
|
|
29
|
+
export type { Hit } from "./generated/Hit.js";
|
|
30
|
+
export type { Limits } from "./generated/Limits.js";
|
|
31
|
+
export type { Matrix } from "./generated/Matrix.js";
|
|
32
|
+
export type { PageGeometry } from "./generated/PageGeometry.js";
|
|
33
|
+
export type { ParagraphInfo } from "./generated/ParagraphInfo.js";
|
|
34
|
+
export type { Point } from "./generated/Point.js";
|
|
35
|
+
export type { Quad } from "./generated/Quad.js";
|
|
36
|
+
export type { Rect } from "./generated/Rect.js";
|
|
37
|
+
export type { RegisteredFont } from "./generated/RegisteredFont.js";
|
|
38
|
+
export type { SearchHit } from "./generated/SearchHit.js";
|
|
39
|
+
export type { StyleRun } from "./generated/StyleRun.js";
|
|
40
|
+
export type { TextPage } from "./generated/TextPage.js";
|
|
41
|
+
export type { TextRange } from "./generated/TextRange.js";
|
|
42
|
+
export type { Word } from "./generated/Word.js";
|
package/dist/index.js
ADDED
package/dist/inline.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { type Foliark, type FoliarkOptions } from "./client.js";
|
|
2
|
+
export interface InlineOptions extends Omit<FoliarkOptions, "wasmUrl" | "workerUrl" | "worker" | "hangTimeoutMs"> {
|
|
3
|
+
/** Where to fetch the wasm (the `@zilijonas/foliark/wasm` URL). Not needed when `wasm` is given. */
|
|
4
|
+
wasmUrl?: string | URL;
|
|
5
|
+
/** Already-fetched bytes or a compiled module (tests, prefetched or cached assets): nothing is fetched. */
|
|
6
|
+
wasm?: BufferSource | WebAssembly.Module;
|
|
7
|
+
/**
|
|
8
|
+
* Bytes of one CMap pack file (a `<cmapsUrl>/<name>.cmap` URL). Default `fetch`, which is right in a browser worker.
|
|
9
|
+
* Node's `fetch` cannot read `file:` URLs (the default `cmapsUrl` is one there), so a Node host passes a reader.
|
|
10
|
+
*/
|
|
11
|
+
fetchCMap?: (url: string) => Promise<Uint8Array>;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Like `createFoliark` without a worker. Hang supervision belongs to the host: a stuck wasm call blocks the host
|
|
15
|
+
* worker itself. After any `onReset` the host must treat its worker as dead and replace it, because a wasm trap or
|
|
16
|
+
* out-of-memory cannot be healed inside the thread that owns the instance.
|
|
17
|
+
*/
|
|
18
|
+
export declare function createInlineFoliark(o: InlineOptions): Promise<Foliark>;
|
package/dist/inline.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// In-worker entry (`@zilijonas/foliark/inline`): the same `Foliark` client and DTOs, but the engine runs in the CALLING
|
|
2
|
+
// thread instead of a nested worker. For hosts that already own a dedicated worker with its own generation and
|
|
3
|
+
// recovery (HDDN): they keep their protocol and state machine and import this inside that worker. Same worker
|
|
4
|
+
// core and the same validated envelope as the module worker, over a real MessageChannel, so buffers still transfer.
|
|
5
|
+
import init from "../wasm/foliark_wasm.js";
|
|
6
|
+
import { createFoliark } from "./client.js";
|
|
7
|
+
import { createWorkerCore } from "./worker-core.js";
|
|
8
|
+
/** `setTimeout` caps at 2^31-1 ms; a same-thread engine cannot be watched from outside, its host supervises hangs. */
|
|
9
|
+
const NO_HANG_TIMER = 2 ** 31 - 1;
|
|
10
|
+
/**
|
|
11
|
+
* Like `createFoliark` without a worker. Hang supervision belongs to the host: a stuck wasm call blocks the host
|
|
12
|
+
* worker itself. After any `onReset` the host must treat its worker as dead and replace it, because a wasm trap or
|
|
13
|
+
* out-of-memory cannot be healed inside the thread that owns the instance.
|
|
14
|
+
*/
|
|
15
|
+
export function createInlineFoliark(o) {
|
|
16
|
+
const { wasm, wasmUrl, fetchCMap, ...rest } = o;
|
|
17
|
+
return createFoliark({
|
|
18
|
+
...rest,
|
|
19
|
+
wasmUrl: wasmUrl ?? "inline:",
|
|
20
|
+
hangTimeoutMs: NO_HANG_TIMER,
|
|
21
|
+
worker: () => {
|
|
22
|
+
const { port1, port2 } = new MessageChannel();
|
|
23
|
+
const core = createWorkerCore({
|
|
24
|
+
post: (m, transfer = []) => port2.postMessage(m, { transfer }),
|
|
25
|
+
loadWasm: (url) => init({ module_or_path: wasm ?? url }).then(() => undefined),
|
|
26
|
+
cmapsUrl: new URL("../cmaps/", import.meta.url).href,
|
|
27
|
+
fetchCMap,
|
|
28
|
+
});
|
|
29
|
+
port2.onmessage = (e) => core.onMessage(e.data);
|
|
30
|
+
return Object.assign(port1, { terminate: () => (port1.close(), port2.close()) });
|
|
31
|
+
},
|
|
32
|
+
});
|
|
33
|
+
}
|
package/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { DocumentHandle } from "./document.js";
|
|
2
|
+
import type { EditPreview } from "./generated/EditPreview.js";
|
|
3
|
+
/** True while `p` was laid out on `doc`'s current revision. A font registered since (`fontRegistryVersion`
|
|
4
|
+
* moves) also changes the layout: call `layoutEdit` again after `registerFonts`. */
|
|
5
|
+
export declare const isCurrentPreview: (p: EditPreview, doc: DocumentHandle) => boolean;
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
/** True while `p` was laid out on `doc`'s current revision. A font registered since (`fontRegistryVersion`
|
|
2
|
+
* moves) also changes the layout: call `layoutEdit` again after `registerFonts`. */
|
|
3
|
+
export const isCurrentPreview = (p, doc) => p.docId === doc.id && p.revision === doc.revision;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare class Lifecycle {
|
|
2
|
+
private readonly owned;
|
|
3
|
+
/** Own a resource until it is released. Returns the early release (`close()`): it runs the hook once. */
|
|
4
|
+
own(release: () => void): () => void;
|
|
5
|
+
/** Release everything owned; a hook that throws does not stop the others. */
|
|
6
|
+
releaseAll(): void;
|
|
7
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
// What the client owns on behalf of its handles (spec 1, 10.2): one registry, so `close()`, a worker reset and
|
|
2
|
+
// `dispose()` release the same things the same way. A handle registers a hook that drops its JS-side caches and
|
|
3
|
+
// marks it gone; the client releases every hook of a dead worker generation at reset and all of them at dispose.
|
|
4
|
+
// This frees JS memory only. Wasm memory never shrinks (a new worker returns it) and nothing here is a wipe.
|
|
5
|
+
export class Lifecycle {
|
|
6
|
+
owned = new Set();
|
|
7
|
+
/** Own a resource until it is released. Returns the early release (`close()`): it runs the hook once. */
|
|
8
|
+
own(release) {
|
|
9
|
+
this.owned.add(release);
|
|
10
|
+
return () => {
|
|
11
|
+
if (this.owned.delete(release))
|
|
12
|
+
release();
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
/** Release everything owned; a hook that throws does not stop the others. */
|
|
16
|
+
releaseAll() {
|
|
17
|
+
const all = [...this.owned];
|
|
18
|
+
this.owned.clear();
|
|
19
|
+
for (const release of all) {
|
|
20
|
+
try {
|
|
21
|
+
release();
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
// releasing is best effort per resource; the rest must still go
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import type { Ctl } from "./generated/Ctl.js";
|
|
2
|
+
import type { ErrorCode } from "./generated/ErrorCode.js";
|
|
3
|
+
import type { ErrorDetails } from "./generated/ErrorDetails.js";
|
|
4
|
+
import type { ErrorWire } from "./generated/ErrorWire.js";
|
|
5
|
+
import type { OpName } from "./generated/OpName.js";
|
|
6
|
+
import type { PageGeometry } from "./generated/PageGeometry.js";
|
|
7
|
+
import type { Prog } from "./generated/Prog.js";
|
|
8
|
+
import type { Req } from "./generated/Req.js";
|
|
9
|
+
import type { Res } from "./generated/Res.js";
|
|
10
|
+
export declare const PROTOCOL_VERSION = 1;
|
|
11
|
+
/** Default cap for one transferred buffer: spec 9 `maxOutputBytes`. Callers pass their `Limits` value. */
|
|
12
|
+
export declare const DEFAULT_MAX_BUFFER_BYTES: number;
|
|
13
|
+
export declare const ERROR_CODES: {
|
|
14
|
+
readonly INVALID_ARGUMENT: true;
|
|
15
|
+
readonly MALFORMED_PDF: true;
|
|
16
|
+
readonly REPAIRED_PDF: true;
|
|
17
|
+
readonly DECODE_FAILED: true;
|
|
18
|
+
readonly PASSWORD_REQUIRED: true;
|
|
19
|
+
readonly PASSWORD_INCORRECT: true;
|
|
20
|
+
readonly ENCRYPTION_UNSUPPORTED: true;
|
|
21
|
+
readonly LIMIT_EXCEEDED: true;
|
|
22
|
+
readonly OUT_OF_MEMORY: true;
|
|
23
|
+
readonly ASSET_LOAD_FAILED: true;
|
|
24
|
+
readonly FONT_UNSUPPORTED: true;
|
|
25
|
+
readonly EDIT_NO_STYLE: true;
|
|
26
|
+
readonly EDIT_ROTATED_TEXT: true;
|
|
27
|
+
readonly EDIT_NO_FONT: true;
|
|
28
|
+
readonly EDIT_NO_FIT: true;
|
|
29
|
+
readonly EDIT_UNSUPPORTED_SCRIPT: true;
|
|
30
|
+
readonly STALE_REVISION: true;
|
|
31
|
+
readonly STALE_PLAN: true;
|
|
32
|
+
readonly PLAN_CONSUMED: true;
|
|
33
|
+
readonly FORM_APPEARANCE_MISSING: true;
|
|
34
|
+
readonly XFA_UNSUPPORTED: true;
|
|
35
|
+
readonly OPTIONAL_CONTENT_UNSUPPORTED: true;
|
|
36
|
+
readonly SIGNED_INPUT: true;
|
|
37
|
+
readonly REDACT_PATTERN_UNSUPPORTED: true;
|
|
38
|
+
readonly REDACT_VECTOR_UNSUPPORTED: true;
|
|
39
|
+
readonly REDACT_VERIFY_FAILED: true;
|
|
40
|
+
readonly RENDER_INCOMPLETE: true;
|
|
41
|
+
readonly CONSENT_REQUIRED: true;
|
|
42
|
+
readonly CANCELLED: true;
|
|
43
|
+
readonly WORKER_RESET: true;
|
|
44
|
+
readonly DOCUMENT_GONE: true;
|
|
45
|
+
readonly DOCUMENT_CLOSED: true;
|
|
46
|
+
readonly CLIENT_DISPOSED: true;
|
|
47
|
+
readonly PROTOCOL_ERROR: true;
|
|
48
|
+
readonly INTERNAL: true;
|
|
49
|
+
};
|
|
50
|
+
/** Spec 12 `FoliarkError`. `recoverable` is decided by the side that raises it. */
|
|
51
|
+
export declare class FoliarkError extends Error {
|
|
52
|
+
readonly code: ErrorCode;
|
|
53
|
+
readonly details?: ErrorDetails;
|
|
54
|
+
docId?: string;
|
|
55
|
+
revision?: number;
|
|
56
|
+
readonly recoverable: boolean;
|
|
57
|
+
constructor(code: ErrorCode, message: string, o?: {
|
|
58
|
+
details?: ErrorDetails;
|
|
59
|
+
recoverable?: boolean;
|
|
60
|
+
});
|
|
61
|
+
toWire(): ErrorWire;
|
|
62
|
+
static fromWire(w: ErrorWire): FoliarkError;
|
|
63
|
+
}
|
|
64
|
+
/** Anything thrown that is not already a `FoliarkError` is an engine bug (spec 12 INTERNAL). */
|
|
65
|
+
export declare const asFoliarkError: (e: unknown) => FoliarkError;
|
|
66
|
+
/** `needsDoc`: whether the op addresses an open document through `Req.doc`. */
|
|
67
|
+
export declare const OPS: {
|
|
68
|
+
readonly init: false;
|
|
69
|
+
readonly open: false;
|
|
70
|
+
readonly info: true;
|
|
71
|
+
readonly pages: true;
|
|
72
|
+
readonly capabilities: true;
|
|
73
|
+
readonly extractText: true;
|
|
74
|
+
readonly hitTest: true;
|
|
75
|
+
readonly select: true;
|
|
76
|
+
readonly quadsFor: true;
|
|
77
|
+
readonly search: true;
|
|
78
|
+
readonly renderPage: true;
|
|
79
|
+
readonly prepareParagraph: true;
|
|
80
|
+
readonly layoutEdit: true;
|
|
81
|
+
readonly applyChanges: true;
|
|
82
|
+
readonly prepareExport: true;
|
|
83
|
+
readonly export: true;
|
|
84
|
+
readonly registerFonts: false;
|
|
85
|
+
readonly close: true;
|
|
86
|
+
};
|
|
87
|
+
export declare const isErrorCode: (x: unknown) => x is "INVALID_ARGUMENT" | "MALFORMED_PDF" | "REPAIRED_PDF" | "DECODE_FAILED" | "PASSWORD_REQUIRED" | "PASSWORD_INCORRECT" | "ENCRYPTION_UNSUPPORTED" | "LIMIT_EXCEEDED" | "OUT_OF_MEMORY" | "ASSET_LOAD_FAILED" | "FONT_UNSUPPORTED" | "EDIT_NO_STYLE" | "EDIT_ROTATED_TEXT" | "EDIT_NO_FONT" | "EDIT_NO_FIT" | "EDIT_UNSUPPORTED_SCRIPT" | "STALE_REVISION" | "STALE_PLAN" | "PLAN_CONSUMED" | "FORM_APPEARANCE_MISSING" | "XFA_UNSUPPORTED" | "OPTIONAL_CONTENT_UNSUPPORTED" | "SIGNED_INPUT" | "REDACT_PATTERN_UNSUPPORTED" | "REDACT_VECTOR_UNSUPPORTED" | "REDACT_VERIFY_FAILED" | "RENDER_INCOMPLETE" | "CONSENT_REQUIRED" | "CANCELLED" | "WORKER_RESET" | "DOCUMENT_GONE" | "DOCUMENT_CLOSED" | "CLIENT_DISPOSED" | "PROTOCOL_ERROR" | "INTERNAL";
|
|
88
|
+
export declare const isOpName: (x: unknown) => x is "init" | "open" | "info" | "pages" | "capabilities" | "extractText" | "hitTest" | "select" | "quadsFor" | "search" | "renderPage" | "prepareParagraph" | "layoutEdit" | "applyChanges" | "prepareExport" | "export" | "registerFonts" | "close";
|
|
89
|
+
export declare const isErrorWire: (x: unknown) => x is ErrorWire;
|
|
90
|
+
export declare const isPageGeometry: (x: unknown) => x is PageGeometry;
|
|
91
|
+
/**
|
|
92
|
+
* A transferable byte buffer: a real, attached `ArrayBuffer` (never a view, never shared) with
|
|
93
|
+
* `min <= byteLength <= max`. `min: 1` rejects the empty or detached stand-in for a missing buffer.
|
|
94
|
+
*/
|
|
95
|
+
export declare function isBuffer(x: unknown, max?: number, min?: number): x is ArrayBuffer;
|
|
96
|
+
/** Wrapper that makes `postMessage` ownership explicit: listed buffers must be valid, unique, and detached afterwards. */
|
|
97
|
+
export declare function postTransfer(port: {
|
|
98
|
+
postMessage(message: unknown, options: {
|
|
99
|
+
transfer: Transferable[];
|
|
100
|
+
}): void;
|
|
101
|
+
}, message: unknown, buffers: ArrayBuffer[], maxBytes?: number): void;
|
|
102
|
+
type Guard = (x: unknown) => boolean;
|
|
103
|
+
export declare const argsGuards: (maxBytes?: number) => Partial<Record<OpName, Guard>>;
|
|
104
|
+
export declare const resultGuards: (maxBytes?: number) => Partial<Record<OpName, Guard>>;
|
|
105
|
+
export declare const isReq: (x: unknown) => x is Req<unknown>;
|
|
106
|
+
export declare const isProg: (x: unknown) => x is Prog;
|
|
107
|
+
export declare const isCtl: (x: unknown) => x is Ctl;
|
|
108
|
+
/**
|
|
109
|
+
* Worker side: validate one raw request and check it belongs to this worker generation.
|
|
110
|
+
* Failure carries the id (when readable) so the worker can answer, else the worker must report `fatal`.
|
|
111
|
+
*/
|
|
112
|
+
export declare function checkRequest(raw: unknown, generation: number | undefined, maxBytes?: number): {
|
|
113
|
+
ok: true;
|
|
114
|
+
req: Req<unknown>;
|
|
115
|
+
} | {
|
|
116
|
+
ok: false;
|
|
117
|
+
id?: number;
|
|
118
|
+
error: FoliarkError;
|
|
119
|
+
};
|
|
120
|
+
export declare const okRes: (id: number, result: unknown) => Res<unknown>;
|
|
121
|
+
export declare const errRes: (id: number, e: FoliarkError) => Res<unknown>;
|
|
122
|
+
export type DropReason = "malformed" | "unknown-id" | "duplicate" | "stale-generation" | "stale-event";
|
|
123
|
+
export type Inbound<T> = {
|
|
124
|
+
kind: "result";
|
|
125
|
+
ctx: T;
|
|
126
|
+
value: unknown;
|
|
127
|
+
} | {
|
|
128
|
+
kind: "failure";
|
|
129
|
+
ctx: T;
|
|
130
|
+
error: FoliarkError;
|
|
131
|
+
} | {
|
|
132
|
+
kind: "progress";
|
|
133
|
+
ctx: T;
|
|
134
|
+
progress: Prog["progress"];
|
|
135
|
+
} | {
|
|
136
|
+
kind: "event";
|
|
137
|
+
event: "ready" | "fatal";
|
|
138
|
+
reason?: string;
|
|
139
|
+
} | {
|
|
140
|
+
kind: "dropped";
|
|
141
|
+
reason: DropReason;
|
|
142
|
+
};
|
|
143
|
+
/**
|
|
144
|
+
* Request ids are monotonic across worker generations, so a response id classifies by position alone:
|
|
145
|
+
* below this generation's first id = stale, pending = answer, otherwise issued-and-settled = duplicate, else unknown.
|
|
146
|
+
* Every `dropped` outcome is a protocol violation the caller must report (reason only, never content).
|
|
147
|
+
*/
|
|
148
|
+
export declare class Correlator<T> {
|
|
149
|
+
private readonly maxBytes;
|
|
150
|
+
generation: number;
|
|
151
|
+
private last;
|
|
152
|
+
private firstOfGeneration;
|
|
153
|
+
private readonly pending;
|
|
154
|
+
constructor(maxBytes?: number);
|
|
155
|
+
/** Requests sent and not yet answered. */
|
|
156
|
+
get size(): number;
|
|
157
|
+
register(op: OpName, ctx: T): number;
|
|
158
|
+
/** Start a new generation: returns the contexts still waiting so the caller rejects them `WORKER_RESET`. */
|
|
159
|
+
reset(): T[];
|
|
160
|
+
receive(raw: unknown): Inbound<T>;
|
|
161
|
+
}
|
|
162
|
+
export {};
|
package/dist/protocol.js
ADDED
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
export const PROTOCOL_VERSION = 1;
|
|
2
|
+
/** Default cap for one transferred buffer: spec 9 `maxOutputBytes`. Callers pass their `Limits` value. */
|
|
3
|
+
export const DEFAULT_MAX_BUFFER_BYTES = 256 * 1024 * 1024;
|
|
4
|
+
// ---- errors -------------------------------------------------------------------------------------
|
|
5
|
+
export const ERROR_CODES = {
|
|
6
|
+
INVALID_ARGUMENT: true, MALFORMED_PDF: true, REPAIRED_PDF: true, DECODE_FAILED: true, PASSWORD_REQUIRED: true,
|
|
7
|
+
PASSWORD_INCORRECT: true, ENCRYPTION_UNSUPPORTED: true, LIMIT_EXCEEDED: true, OUT_OF_MEMORY: true,
|
|
8
|
+
ASSET_LOAD_FAILED: true, FONT_UNSUPPORTED: true, EDIT_NO_STYLE: true, EDIT_ROTATED_TEXT: true, EDIT_NO_FONT: true,
|
|
9
|
+
EDIT_NO_FIT: true, EDIT_UNSUPPORTED_SCRIPT: true, STALE_REVISION: true, STALE_PLAN: true,
|
|
10
|
+
PLAN_CONSUMED: true, FORM_APPEARANCE_MISSING: true, XFA_UNSUPPORTED: true, OPTIONAL_CONTENT_UNSUPPORTED: true,
|
|
11
|
+
SIGNED_INPUT: true, REDACT_PATTERN_UNSUPPORTED: true, REDACT_VECTOR_UNSUPPORTED: true, REDACT_VERIFY_FAILED: true, RENDER_INCOMPLETE: true,
|
|
12
|
+
CONSENT_REQUIRED: true, CANCELLED: true, WORKER_RESET: true, DOCUMENT_GONE: true, DOCUMENT_CLOSED: true,
|
|
13
|
+
CLIENT_DISPOSED: true, PROTOCOL_ERROR: true, INTERNAL: true,
|
|
14
|
+
};
|
|
15
|
+
/** Spec 12 `FoliarkError`. `recoverable` is decided by the side that raises it. */
|
|
16
|
+
export class FoliarkError extends Error {
|
|
17
|
+
code;
|
|
18
|
+
details;
|
|
19
|
+
docId;
|
|
20
|
+
revision;
|
|
21
|
+
recoverable;
|
|
22
|
+
constructor(code, message, o = {}) {
|
|
23
|
+
super(message);
|
|
24
|
+
this.name = "FoliarkError";
|
|
25
|
+
this.code = code;
|
|
26
|
+
this.details = o.details;
|
|
27
|
+
this.recoverable = o.recoverable ?? false;
|
|
28
|
+
}
|
|
29
|
+
toWire() {
|
|
30
|
+
const w = { code: this.code, message: this.message, recoverable: this.recoverable };
|
|
31
|
+
if (this.details !== undefined)
|
|
32
|
+
w.details = this.details;
|
|
33
|
+
return w;
|
|
34
|
+
}
|
|
35
|
+
static fromWire(w) {
|
|
36
|
+
return new FoliarkError(w.code, w.message, { details: w.details, recoverable: w.recoverable });
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Anything thrown that is not already a `FoliarkError` is an engine bug (spec 12 INTERNAL). */
|
|
40
|
+
export const asFoliarkError = (e) => e instanceof FoliarkError ? e : new FoliarkError("INTERNAL", e instanceof Error ? e.message : String(e));
|
|
41
|
+
const protocolError = (message) => new FoliarkError("PROTOCOL_ERROR", message);
|
|
42
|
+
const isRec = (x) => typeof x === "object" && x !== null && !Array.isArray(x);
|
|
43
|
+
const isStr = (x) => typeof x === "string";
|
|
44
|
+
const isNum = (x) => typeof x === "number" && Number.isFinite(x);
|
|
45
|
+
const isU32 = (x) => Number.isInteger(x) && x >= 0 && x <= 0xffff_ffff;
|
|
46
|
+
const opt = (g) => (x) => x === undefined || g(x);
|
|
47
|
+
const arrayOf = (g) => (x) => Array.isArray(x) && x.every(g);
|
|
48
|
+
const tuple = (n) => (x) => Array.isArray(x) && x.length === n && x.every(isNum);
|
|
49
|
+
/** Every listed field must satisfy its guard (use `opt` for optional ones). Extra fields are ignored (v1 is additive). */
|
|
50
|
+
const shape = (fields) => (x) => isRec(x) && Object.entries(fields).every(([k, g]) => g(x[k]));
|
|
51
|
+
const oneOf = (table) => (x) => isStr(x) && Object.hasOwn(table, x);
|
|
52
|
+
const PHASES = { open: true, extract: true, render: true, layout: true, export: true, verify: true };
|
|
53
|
+
/** `needsDoc`: whether the op addresses an open document through `Req.doc`. */
|
|
54
|
+
export const OPS = {
|
|
55
|
+
init: false, open: false, info: true, pages: true, capabilities: true, extractText: true, hitTest: true, select: true,
|
|
56
|
+
quadsFor: true, search: true, renderPage: true, prepareParagraph: true, layoutEdit: true, applyChanges: true, prepareExport: true,
|
|
57
|
+
export: true, registerFonts: false, close: true,
|
|
58
|
+
};
|
|
59
|
+
export const isErrorCode = oneOf(ERROR_CODES);
|
|
60
|
+
export const isOpName = oneOf(OPS);
|
|
61
|
+
const isPhase = oneOf(PHASES);
|
|
62
|
+
const isErrorDetails = shape({
|
|
63
|
+
limit: opt(isStr), actual: opt(isNum), max: opt(isNum), feature: opt(isStr), pages: opt(arrayOf(isU32)),
|
|
64
|
+
fields: opt(arrayOf(isStr)), editId: opt(isStr), chars: opt(isStr), neededLines: opt(isU32), maxLines: opt(isU32),
|
|
65
|
+
objRef: opt(isStr), ids: opt(arrayOf(isStr)),
|
|
66
|
+
});
|
|
67
|
+
export const isErrorWire = shape({
|
|
68
|
+
code: isErrorCode, message: isStr, details: opt(isErrorDetails), recoverable: (x) => typeof x === "boolean",
|
|
69
|
+
});
|
|
70
|
+
const isRect = shape({ x: isNum, y: isNum, width: isNum, height: isNum });
|
|
71
|
+
export const isPageGeometry = shape({
|
|
72
|
+
index: isU32, width: isNum, height: isNum, rotation: (x) => x === 0 || x === 90 || x === 180 || x === 270,
|
|
73
|
+
userUnit: (x) => isNum(x) && x > 0, mediaBox: tuple(4), cropBox: tuple(4), userToDisplay: tuple(6), displayToUser: tuple(6),
|
|
74
|
+
});
|
|
75
|
+
const isRenderedHeader = shape({
|
|
76
|
+
width: isU32, height: isU32, scale: (x) => isNum(x) && x > 0, clip: isRect, complete: (x) => typeof x === "boolean",
|
|
77
|
+
warnings: arrayOf(shape({ code: isStr, detail: isStr })),
|
|
78
|
+
});
|
|
79
|
+
// ---- buffers ------------------------------------------------------------------------------------
|
|
80
|
+
const isDetached = (b) => b.detached === true;
|
|
81
|
+
/**
|
|
82
|
+
* A transferable byte buffer: a real, attached `ArrayBuffer` (never a view, never shared) with
|
|
83
|
+
* `min <= byteLength <= max`. `min: 1` rejects the empty or detached stand-in for a missing buffer.
|
|
84
|
+
*/
|
|
85
|
+
export function isBuffer(x, max = DEFAULT_MAX_BUFFER_BYTES, min = 0) {
|
|
86
|
+
return (Object.prototype.toString.call(x) === "[object ArrayBuffer]" &&
|
|
87
|
+
!isDetached(x) &&
|
|
88
|
+
x.byteLength >= min &&
|
|
89
|
+
x.byteLength <= max);
|
|
90
|
+
}
|
|
91
|
+
/** Wrapper that makes `postMessage` ownership explicit: listed buffers must be valid, unique, and detached afterwards. */
|
|
92
|
+
export function postTransfer(port, message, buffers, maxBytes = DEFAULT_MAX_BUFFER_BYTES) {
|
|
93
|
+
if (new Set(buffers).size !== buffers.length)
|
|
94
|
+
throw protocolError("buffer listed twice for transfer");
|
|
95
|
+
if (!buffers.every((b) => isBuffer(b, maxBytes)))
|
|
96
|
+
throw protocolError("transfer list holds a missing, detached, shared or oversized buffer");
|
|
97
|
+
port.postMessage(message, { transfer: buffers });
|
|
98
|
+
// Still attached means the host did not move it: the sender would keep a live alias of bytes the receiver owns.
|
|
99
|
+
if (buffers.some((b) => b.byteLength !== 0 && !isDetached(b)))
|
|
100
|
+
throw new FoliarkError("INTERNAL", "buffer was not detached by transfer");
|
|
101
|
+
}
|
|
102
|
+
const isNull = (x) => x === null;
|
|
103
|
+
const isBool = (x) => typeof x === "boolean";
|
|
104
|
+
const isEmptyArgs = isRec;
|
|
105
|
+
const isPoint = shape({ x: isNum, y: isNum });
|
|
106
|
+
const isQuad = (x) => Array.isArray(x) && x.length === 4 && x.every(isPoint);
|
|
107
|
+
const isRgb = tuple(3);
|
|
108
|
+
const isDocumentInfo = shape({
|
|
109
|
+
pageCount: isU32, pdfVersion: isStr, encrypted: isBool, repaired: isBool, requiredAssets: arrayOf(isStr),
|
|
110
|
+
features: shape({
|
|
111
|
+
forms: isBool, xfa: isBool, signatures: isBool, optionalContent: isBool, javascript: isBool, embeddedFiles: isBool,
|
|
112
|
+
annotations: isU32, structTree: isBool, outlines: isBool, thumbnails: isBool, metadata: isBool,
|
|
113
|
+
}),
|
|
114
|
+
});
|
|
115
|
+
const isWord = shape({
|
|
116
|
+
text: isStr, start: isU32, end: isU32, box: isRect, blockIndex: isU32, lineIndex: isU32, wordIndex: isU32,
|
|
117
|
+
styleIndex: isU32, invisible: isBool, ocgHidden: opt((x) => x === true), source: (x) => x === "native",
|
|
118
|
+
});
|
|
119
|
+
const isStyleRun = shape({
|
|
120
|
+
fontName: isStr, fontRef: isStr, size: isNum, color: isRgb, renderMode: isU32,
|
|
121
|
+
flags: shape({ bold: isBool, italic: isBool, serif: isBool, mono: isBool }),
|
|
122
|
+
});
|
|
123
|
+
const isCluster = shape({ start: isU32, end: isU32, quad: isQuad, styleIndex: isU32, glyphs: isU32, flags: isU32 });
|
|
124
|
+
const isTextPage = shape({
|
|
125
|
+
docId: isStr, revision: isU32, pageIndex: isU32, text: isStr, words: arrayOf(isWord), styles: arrayOf(isStyleRun),
|
|
126
|
+
clusters: opt(arrayOf(isCluster)),
|
|
127
|
+
stats: shape({ visibleClusters: isU32, invisibleClusters: isU32, unmappedGlyphs: isU32, imageCoverage: isNum }),
|
|
128
|
+
});
|
|
129
|
+
const isHit = shape({ start: isU32, end: isU32, wordIndex: isU32, quad: isQuad });
|
|
130
|
+
const isTextRange = shape({ start: isU32, end: isU32, text: isStr, quads: arrayOf(isQuad), partialCluster: isBool });
|
|
131
|
+
const isSearchHit = (x) => isTextRange(x) && shape({ pageIndex: isU32, revision: isU32 })(x);
|
|
132
|
+
const isCapability = shape({
|
|
133
|
+
status: (x) => x === "supported" || x === "unsupported" || x === "unknown",
|
|
134
|
+
reasons: arrayOf(shape({ code: isErrorCode, pages: opt(arrayOf(isU32)) })),
|
|
135
|
+
});
|
|
136
|
+
const isCapabilities = shape({
|
|
137
|
+
extract: isCapability, render: isCapability, edit: isCapability, redact: isCapability, exportNative: isCapability,
|
|
138
|
+
exportRaster: isCapability,
|
|
139
|
+
});
|
|
140
|
+
const isRegisteredFont = shape({
|
|
141
|
+
id: isStr, family: isStr, weight: isU32, style: (x) => x === "normal" || x === "italic", postscriptName: isStr, glyphCount: isU32,
|
|
142
|
+
});
|
|
143
|
+
const isMode = (x) => x === "native" || x === "raster";
|
|
144
|
+
/** Rust serializes an absent `Option` as null. */
|
|
145
|
+
const optNull = (g) => (x) => x === undefined || x === null || g(x);
|
|
146
|
+
const isTextEdit = shape({ id: isStr, pageIndex: isU32, rects: arrayOf(isRect), text: isStr, font: optNull(isStr) });
|
|
147
|
+
const isRedaction = (x) => shape({ id: isStr, pageIndex: isU32, fill: optNull(isRgb) })(x) &&
|
|
148
|
+
(x.kind === "region" ? isRect(x.rect) : x.kind === "text" && isU32(x.start) && isU32(x.end));
|
|
149
|
+
const isChangeSet = shape({ baseRevision: isU32, textEdits: opt(arrayOf(isTextEdit)), redactions: opt(arrayOf(isRedaction)) });
|
|
150
|
+
const isLayoutRun = shape({ source: (x) => x === "embedded" || x === "registered", font: isStr, clusters: isU32 });
|
|
151
|
+
const isEditLayout = shape({
|
|
152
|
+
id: isStr, lines: isU32, approximate: isBool, fingerprint: isStr, runs: arrayOf(isLayoutRun),
|
|
153
|
+
});
|
|
154
|
+
const isEditPreview = shape({
|
|
155
|
+
docId: isStr, revision: isU32, fontRegistryVersion: isU32, fontSize: isNum,
|
|
156
|
+
lines: arrayOf(shape({ text: isStr, baseline: isNum, x: isNum, width: isNum })),
|
|
157
|
+
neededLines: isU32, maxLines: isU32, fits: isBool, approximate: isBool, runs: arrayOf(isLayoutRun), fingerprint: isStr,
|
|
158
|
+
glyphs: opt(arrayOf(shape({ line: isU32, font: isStr, gid: isU32, start: isU32, end: isU32, x: isNum, width: isNum }))),
|
|
159
|
+
});
|
|
160
|
+
const isChangeResult = shape({
|
|
161
|
+
accepted: isBool, revision: isU32,
|
|
162
|
+
rejected: arrayOf(shape({ id: isStr, code: isErrorCode, details: optNull(isRec) })),
|
|
163
|
+
changedPages: arrayOf(isU32), layouts: arrayOf(isEditLayout),
|
|
164
|
+
});
|
|
165
|
+
export const argsGuards = (maxBytes = DEFAULT_MAX_BUFFER_BYTES) => ({
|
|
166
|
+
init: shape({ wasmUrl: isStr, cmapsUrl: opt(isStr), limits: opt(isRec), fontMatch: opt(arrayOf(shape({ pattern: isStr, family: isStr }))) }),
|
|
167
|
+
open: shape({ bytes: (x) => isBuffer(x, maxBytes, 1), password: opt(isStr), allowRepaired: opt(isBool) }),
|
|
168
|
+
info: isEmptyArgs,
|
|
169
|
+
pages: isEmptyArgs,
|
|
170
|
+
capabilities: shape({ deep: opt(isBool) }),
|
|
171
|
+
extractText: shape({ page: isU32, chars: opt(isBool), includeOcgHidden: opt(isBool) }),
|
|
172
|
+
hitTest: shape({ page: isU32, point: isPoint, tolerance: opt((x) => isNum(x) && x >= 0) }),
|
|
173
|
+
select: shape({ page: isU32, from: isPoint, to: isPoint }),
|
|
174
|
+
quadsFor: shape({ page: isU32, start: isU32, end: isU32 }),
|
|
175
|
+
search: shape({
|
|
176
|
+
query: isStr, pages: opt(arrayOf(isU32)), caseSensitive: opt(isBool), diacriticSensitive: opt(isBool), wholeWord: opt(isBool),
|
|
177
|
+
maxHits: opt(isU32),
|
|
178
|
+
}),
|
|
179
|
+
renderPage: shape({
|
|
180
|
+
page: isU32, scale: opt((x) => isNum(x) && x > 0), clip: opt(isRect), annotations: opt(isBool), background: opt(tuple(4)),
|
|
181
|
+
}),
|
|
182
|
+
prepareParagraph: shape({ page: isU32, rects: arrayOf(isRect), font: opt(isStr), editorFace: opt(isBool) }),
|
|
183
|
+
layoutEdit: shape({ page: isU32, rects: arrayOf(isRect), text: isStr, font: opt(isStr), glyphs: opt(isBool) }),
|
|
184
|
+
applyChanges: shape({ changes: isChangeSet }),
|
|
185
|
+
prepareExport: shape({ mode: isMode, policy: opt(isRec) }),
|
|
186
|
+
export: shape({ planId: isStr }),
|
|
187
|
+
registerFonts: shape({
|
|
188
|
+
fonts: arrayOf(shape({
|
|
189
|
+
bytes: (x) => isBuffer(x, maxBytes, 1), family: isStr, weight: (x) => x === 400 || x === 700,
|
|
190
|
+
style: (x) => x === "normal" || x === "italic",
|
|
191
|
+
})),
|
|
192
|
+
}),
|
|
193
|
+
close: isEmptyArgs,
|
|
194
|
+
});
|
|
195
|
+
export const resultGuards = (maxBytes = DEFAULT_MAX_BUFFER_BYTES) => ({
|
|
196
|
+
init: isNull,
|
|
197
|
+
open: shape({ docId: isStr, info: isDocumentInfo }),
|
|
198
|
+
info: isDocumentInfo,
|
|
199
|
+
pages: arrayOf(isPageGeometry),
|
|
200
|
+
capabilities: isCapabilities,
|
|
201
|
+
extractText: isTextPage,
|
|
202
|
+
hitTest: (x) => x === null || isHit(x),
|
|
203
|
+
select: (x) => x === null || isTextRange(x),
|
|
204
|
+
quadsFor: isTextRange,
|
|
205
|
+
search: arrayOf(isSearchHit),
|
|
206
|
+
renderPage: (x) => isRenderedHeader(x) &&
|
|
207
|
+
isBuffer(x.data, maxBytes, 1) &&
|
|
208
|
+
x.data.byteLength === x.width * x.height * 4,
|
|
209
|
+
prepareParagraph: shape({ docId: isStr, revision: isU32, pageIndex: isU32, bounds: isRect, font: isRec }),
|
|
210
|
+
layoutEdit: isEditPreview,
|
|
211
|
+
applyChanges: isChangeResult,
|
|
212
|
+
prepareExport: shape({
|
|
213
|
+
planId: isStr, docId: isStr, generation: isU32, revision: isU32, mode: isMode, policyHash: isStr,
|
|
214
|
+
fontRegistryVersion: isU32, byteLength: isU32, report: isRec,
|
|
215
|
+
}),
|
|
216
|
+
export: (x) => isBuffer(x, maxBytes, 1),
|
|
217
|
+
registerFonts: arrayOf(isRegisteredFont),
|
|
218
|
+
close: isNull,
|
|
219
|
+
});
|
|
220
|
+
// ---- messages -----------------------------------------------------------------------------------
|
|
221
|
+
export const isReq = (x) => isRec(x) && x.v === PROTOCOL_VERSION && isU32(x.id) && isU32(x.gen) && isOpName(x.op) && opt(isStr)(x.doc) && "args" in x &&
|
|
222
|
+
(x.doc !== undefined) === OPS[x.op];
|
|
223
|
+
export const isProg = shape({
|
|
224
|
+
v: (x) => x === PROTOCOL_VERSION, id: isU32,
|
|
225
|
+
progress: shape({ phase: isPhase, done: isU32, total: isU32 }),
|
|
226
|
+
});
|
|
227
|
+
export const isCtl = (x) => isRec(x) && x.v === PROTOCOL_VERSION &&
|
|
228
|
+
(isU32(x.cancel) || ((x.event === "ready" || x.event === "fatal") && isU32(x.gen) && opt(isStr)(x.reason)));
|
|
229
|
+
const isResEnvelope = (x) => isRec(x) && x.v === PROTOCOL_VERSION && isU32(x.id) && ((x.ok === true && "result" in x) || (x.ok === false && isErrorWire(x.error)));
|
|
230
|
+
/**
|
|
231
|
+
* Worker side: validate one raw request and check it belongs to this worker generation.
|
|
232
|
+
* Failure carries the id (when readable) so the worker can answer, else the worker must report `fatal`.
|
|
233
|
+
*/
|
|
234
|
+
export function checkRequest(raw, generation, maxBytes = DEFAULT_MAX_BUFFER_BYTES) {
|
|
235
|
+
const id = isRec(raw) && isU32(raw.id) ? raw.id : undefined;
|
|
236
|
+
if (!isReq(raw))
|
|
237
|
+
return { ok: false, id, error: protocolError("malformed request") };
|
|
238
|
+
if (generation !== undefined && raw.gen !== generation) {
|
|
239
|
+
return { ok: false, id, error: new FoliarkError("WORKER_RESET", "request from a previous worker generation", { recoverable: true }) };
|
|
240
|
+
}
|
|
241
|
+
if (!(argsGuards(maxBytes)[raw.op] ?? (() => true))(raw.args))
|
|
242
|
+
return { ok: false, id, error: protocolError(`malformed ${raw.op} arguments`) };
|
|
243
|
+
return { ok: true, req: raw };
|
|
244
|
+
}
|
|
245
|
+
export const okRes = (id, result) => ({ v: 1, id, ok: true, result });
|
|
246
|
+
export const errRes = (id, e) => ({ v: 1, id, ok: false, error: e.toWire() });
|
|
247
|
+
/**
|
|
248
|
+
* Request ids are monotonic across worker generations, so a response id classifies by position alone:
|
|
249
|
+
* below this generation's first id = stale, pending = answer, otherwise issued-and-settled = duplicate, else unknown.
|
|
250
|
+
* Every `dropped` outcome is a protocol violation the caller must report (reason only, never content).
|
|
251
|
+
*/
|
|
252
|
+
export class Correlator {
|
|
253
|
+
maxBytes;
|
|
254
|
+
generation = 1;
|
|
255
|
+
last = 0;
|
|
256
|
+
firstOfGeneration = 1;
|
|
257
|
+
pending = new Map();
|
|
258
|
+
constructor(maxBytes = DEFAULT_MAX_BUFFER_BYTES) {
|
|
259
|
+
this.maxBytes = maxBytes;
|
|
260
|
+
}
|
|
261
|
+
/** Requests sent and not yet answered. */
|
|
262
|
+
get size() {
|
|
263
|
+
return this.pending.size;
|
|
264
|
+
}
|
|
265
|
+
register(op, ctx) {
|
|
266
|
+
this.pending.set(++this.last, { op, ctx });
|
|
267
|
+
return this.last;
|
|
268
|
+
}
|
|
269
|
+
/** Start a new generation: returns the contexts still waiting so the caller rejects them `WORKER_RESET`. */
|
|
270
|
+
reset() {
|
|
271
|
+
const orphans = [...this.pending.values()].map((p) => p.ctx);
|
|
272
|
+
this.pending.clear();
|
|
273
|
+
this.generation++;
|
|
274
|
+
this.firstOfGeneration = this.last + 1;
|
|
275
|
+
return orphans;
|
|
276
|
+
}
|
|
277
|
+
receive(raw) {
|
|
278
|
+
if (isCtl(raw)) {
|
|
279
|
+
if ("event" in raw && raw.gen !== this.generation)
|
|
280
|
+
return { kind: "dropped", reason: "stale-event" };
|
|
281
|
+
return "event" in raw ? { kind: "event", event: raw.event, reason: raw.reason } : { kind: "dropped", reason: "malformed" };
|
|
282
|
+
}
|
|
283
|
+
const id = isRec(raw) && isU32(raw.id) ? raw.id : undefined;
|
|
284
|
+
if (id === undefined)
|
|
285
|
+
return { kind: "dropped", reason: "malformed" };
|
|
286
|
+
const entry = this.pending.get(id);
|
|
287
|
+
if (!entry)
|
|
288
|
+
return { kind: "dropped", reason: id < this.firstOfGeneration ? "stale-generation" : id <= this.last ? "duplicate" : "unknown-id" };
|
|
289
|
+
if (isProg(raw))
|
|
290
|
+
return { kind: "progress", ctx: entry.ctx, progress: raw.progress };
|
|
291
|
+
// From here the request is answered one way or another; a bad answer still settles it, as PROTOCOL_ERROR.
|
|
292
|
+
this.pending.delete(id);
|
|
293
|
+
if (!isResEnvelope(raw))
|
|
294
|
+
return { kind: "failure", ctx: entry.ctx, error: protocolError("malformed response") };
|
|
295
|
+
if (!raw.ok)
|
|
296
|
+
return { kind: "failure", ctx: entry.ctx, error: FoliarkError.fromWire(raw.error) };
|
|
297
|
+
const guard = resultGuards(this.maxBytes)[entry.op];
|
|
298
|
+
if (guard && !guard(raw.result))
|
|
299
|
+
return { kind: "failure", ctx: entry.ctx, error: protocolError(`malformed ${entry.op} result`) };
|
|
300
|
+
return { kind: "result", ctx: entry.ctx, value: raw.result };
|
|
301
|
+
}
|
|
302
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/** Spec 10.1: work units per step. */
|
|
2
|
+
export declare const STEP_BUDGET = 100000;
|
|
3
|
+
export interface WorkerHost {
|
|
4
|
+
post(message: unknown, transfer?: ArrayBuffer[]): void;
|
|
5
|
+
/** Instantiates the wasm module (fetch in the real worker, bytes in tests). */
|
|
6
|
+
loadWasm(url: string): Promise<void>;
|
|
7
|
+
/** Where the CMap pack lives when `init` names no `cmapsUrl`: the `cmaps/` folder next to this package's `dist/`. */
|
|
8
|
+
cmapsUrl?: string;
|
|
9
|
+
/** Bytes of one pack file (default `fetch`; tests read the folder). Rejects when it cannot be had. */
|
|
10
|
+
fetchCMap?(url: string): Promise<Uint8Array>;
|
|
11
|
+
}
|
|
12
|
+
export declare function createWorkerCore(host: WorkerHost, stepBudget?: number): {
|
|
13
|
+
/** Entry point for every message from the main thread. */
|
|
14
|
+
onMessage(raw: unknown): void;
|
|
15
|
+
};
|