@junejs/pdf-typst-wasm 0.2.0 → 0.2.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/dist/{renderer-N0qDEY0c.d.ts → assets-0jc0W7Pm.d.ts} +11 -1
- package/dist/{fonts-GfZSxDhh.js → assets-rrgfvCKs.js} +16 -5
- package/dist/node.d.ts +2 -2
- package/dist/node.js +2 -2
- package/dist/typst.wasm.ref.json +9 -0
- package/dist/unbundled.d.ts +11 -0
- package/dist/unbundled.js +22 -0
- package/dist/workerd.d.ts +2 -2
- package/dist/workerd.js +2 -2
- package/package.json +10 -4
- package/src/assets.ts +38 -0
- package/src/node.ts +1 -0
- package/src/renderer.ts +9 -6
- package/src/unbundled.ts +57 -0
- package/src/workerd.ts +1 -0
|
@@ -56,4 +56,14 @@ interface TypstWasmRenderer extends PdfRenderer {
|
|
|
56
56
|
readonly egress: readonly FontEgress[];
|
|
57
57
|
}
|
|
58
58
|
//#endregion
|
|
59
|
-
|
|
59
|
+
//#region src/assets.d.ts
|
|
60
|
+
type AssetRef = {
|
|
61
|
+
name: string;
|
|
62
|
+
sha256: string;
|
|
63
|
+
size: number;
|
|
64
|
+
urls: string[];
|
|
65
|
+
};
|
|
66
|
+
type AssetLoader = (ref: AssetRef) => Promise<Uint8Array | ArrayBuffer>;
|
|
67
|
+
declare function loadVerified(ref: AssetRef, load: AssetLoader): Promise<Uint8Array>;
|
|
68
|
+
//#endregion
|
|
69
|
+
export { WasmSource as _, TypstWasmRendererOptions as a, FontProvider as c, googleFontSubsets as d, localFonts as f, TypstOutput as g, TypstEngine as h, TypstWasmRenderer as i, FontSource as l, TypstCompileError as m, AssetRef as n, DocumentFonts as o, FontScope as p, loadVerified as r, FontEgress as s, AssetLoader as t, GoogleFont as u, createEngine as v };
|
|
@@ -43,7 +43,7 @@ async function createEngine(wasm) {
|
|
|
43
43
|
//#endregion
|
|
44
44
|
//#region src/renderer.ts
|
|
45
45
|
const MISSING_SHOWN = 10;
|
|
46
|
-
function createTypstWasmRenderer(defaultWasm, opts = {}) {
|
|
46
|
+
function createTypstWasmRenderer(defaultWasm, opts = {}, engineEgress = []) {
|
|
47
47
|
const providers = [...opts.fonts ?? []];
|
|
48
48
|
let engine;
|
|
49
49
|
const ready = () => engine ??= (async () => {
|
|
@@ -57,7 +57,7 @@ function createTypstWasmRenderer(defaultWasm, opts = {}) {
|
|
|
57
57
|
});
|
|
58
58
|
return {
|
|
59
59
|
name: "typst-wasm",
|
|
60
|
-
egress: uniqueEgress(providers),
|
|
60
|
+
egress: uniqueEgress([...engineEgress, ...providers.flatMap((p) => p.egress)]),
|
|
61
61
|
async render(doc) {
|
|
62
62
|
const source = typstSource(doc, { fonts: opts.fontFamilies });
|
|
63
63
|
const [e, documentFonts] = await Promise.all([ready(), fontsFor(doc, providers)]);
|
|
@@ -82,9 +82,9 @@ function createTypstWasmRenderer(defaultWasm, opts = {}) {
|
|
|
82
82
|
}
|
|
83
83
|
};
|
|
84
84
|
}
|
|
85
|
-
function uniqueEgress(
|
|
85
|
+
function uniqueEgress(all) {
|
|
86
86
|
const seen = /* @__PURE__ */ new Map();
|
|
87
|
-
for (const e of
|
|
87
|
+
for (const e of all) if (!seen.get(e.host) || e.sends === "document-characters") seen.set(e.host, {
|
|
88
88
|
host: e.host,
|
|
89
89
|
sends: e.sends
|
|
90
90
|
});
|
|
@@ -183,4 +183,15 @@ function googleSubset(cache, family, weight, text, fetcher) {
|
|
|
183
183
|
return load;
|
|
184
184
|
}
|
|
185
185
|
//#endregion
|
|
186
|
-
|
|
186
|
+
//#region src/assets.ts
|
|
187
|
+
async function loadVerified(ref, load) {
|
|
188
|
+
const got = await load(ref);
|
|
189
|
+
const bytes = got instanceof Uint8Array ? got.buffer instanceof SharedArrayBuffer ? got.slice() : got : new Uint8Array(got);
|
|
190
|
+
if (bytes.byteLength !== ref.size) throw new Error(`${ref.name}: the loader returned ${bytes.byteLength} bytes, expected ${ref.size} (sha256 ${ref.sha256})`);
|
|
191
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
192
|
+
const hex = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
193
|
+
if (hex !== ref.sha256) throw new Error(`${ref.name}: the loader returned bytes with sha256 ${hex}, expected ${ref.sha256}`);
|
|
194
|
+
return bytes;
|
|
195
|
+
}
|
|
196
|
+
//#endregion
|
|
197
|
+
export { TypstCompileError as a, createTypstWasmRenderer as i, googleFontSubsets as n, createEngine as o, localFonts as r, loadVerified as t };
|
package/dist/node.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { _ as WasmSource, a as TypstWasmRendererOptions, c as FontProvider, d as googleFontSubsets, f as localFonts, g as TypstOutput, h as TypstEngine, i as TypstWasmRenderer, l as FontSource, m as TypstCompileError, n as AssetRef, o as DocumentFonts, p as FontScope, r as loadVerified, s as FontEgress, t as AssetLoader, u as GoogleFont, v as createEngine } from "./assets-0jc0W7Pm.js";
|
|
2
2
|
//#region src/node.d.ts
|
|
3
3
|
export declare const WASM_URL: URL;
|
|
4
4
|
export declare function typstWasmRenderer(opts?: TypstWasmRendererOptions): TypstWasmRenderer;
|
|
5
5
|
//#endregion
|
|
6
|
-
export { type DocumentFonts, type FontEgress, type FontProvider, type FontScope, type FontSource, type GoogleFont, TypstCompileError, type TypstEngine, type TypstOutput, type TypstWasmRenderer, type TypstWasmRendererOptions, type WasmSource, createEngine, googleFontSubsets, localFonts };
|
|
6
|
+
export { type AssetLoader, type AssetRef, type DocumentFonts, type FontEgress, type FontProvider, type FontScope, type FontSource, type GoogleFont, TypstCompileError, type TypstEngine, type TypstOutput, type TypstWasmRenderer, type TypstWasmRendererOptions, type WasmSource, createEngine, googleFontSubsets, loadVerified, localFonts };
|
package/dist/node.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as TypstCompileError, i as createTypstWasmRenderer, n as googleFontSubsets, o as createEngine, r as localFonts, t as loadVerified } from "./assets-rrgfvCKs.js";
|
|
2
2
|
//#region src/node.ts
|
|
3
3
|
const WASM_URL = new URL("../dist/typst.wasm", import.meta.url);
|
|
4
4
|
let compiled;
|
|
@@ -21,4 +21,4 @@ function typstWasmRenderer(opts = {}) {
|
|
|
21
21
|
return createTypstWasmRenderer(loadWasm, opts);
|
|
22
22
|
}
|
|
23
23
|
//#endregion
|
|
24
|
-
export { TypstCompileError, WASM_URL, createEngine, googleFontSubsets, localFonts, typstWasmRenderer };
|
|
24
|
+
export { TypstCompileError, WASM_URL, createEngine, googleFontSubsets, loadVerified, localFonts, typstWasmRenderer };
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "typst.wasm",
|
|
3
|
+
"sha256": "65482a22d5c7fa257bce3f0bf9531548f1f553427d29b783ff84b346f678517f",
|
|
4
|
+
"size": 30456380,
|
|
5
|
+
"urls": [
|
|
6
|
+
"https://cdn.jsdelivr.net/npm/@junejs/pdf-typst-wasm@0.2.1/dist/typst.wasm",
|
|
7
|
+
"https://unpkg.com/@junejs/pdf-typst-wasm@0.2.1/dist/typst.wasm"
|
|
8
|
+
]
|
|
9
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { _ as WasmSource, a as TypstWasmRendererOptions, c as FontProvider, d as googleFontSubsets, f as localFonts, g as TypstOutput, h as TypstEngine, i as TypstWasmRenderer, l as FontSource, m as TypstCompileError, n as AssetRef, o as DocumentFonts, p as FontScope, r as loadVerified, s as FontEgress, t as AssetLoader, u as GoogleFont, v as createEngine } from "./assets-0jc0W7Pm.js";
|
|
2
|
+
//#region src/unbundled.d.ts
|
|
3
|
+
export declare function engineUrls(version: string): string[];
|
|
4
|
+
export declare function typstWasmAsset(): Promise<AssetRef>;
|
|
5
|
+
export type UnbundledRendererOptions = Omit<TypstWasmRendererOptions, "wasm"> & {
|
|
6
|
+
load: AssetLoader;
|
|
7
|
+
engineEgress?: FontEgress[];
|
|
8
|
+
};
|
|
9
|
+
export declare function typstWasmRenderer(opts: UnbundledRendererOptions): TypstWasmRenderer;
|
|
10
|
+
//#endregion
|
|
11
|
+
export { type AssetLoader, type AssetRef, type DocumentFonts, type FontEgress, type FontProvider, type FontScope, type FontSource, type GoogleFont, TypstCompileError, type TypstEngine, type TypstOutput, type TypstWasmRenderer, type WasmSource, createEngine, googleFontSubsets, loadVerified, localFonts };
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { a as TypstCompileError, i as createTypstWasmRenderer, n as googleFontSubsets, o as createEngine, r as localFonts, t as loadVerified } from "./assets-rrgfvCKs.js";
|
|
2
|
+
//#region src/unbundled.ts
|
|
3
|
+
function engineUrls(version) {
|
|
4
|
+
return [`https://cdn.jsdelivr.net/npm/@junejs/pdf-typst-wasm@${version}/dist/typst.wasm`, `https://unpkg.com/@junejs/pdf-typst-wasm@${version}/dist/typst.wasm`];
|
|
5
|
+
}
|
|
6
|
+
const ENGINE_HOSTS = engineUrls("0").map((url) => ({
|
|
7
|
+
host: new URL(url).host,
|
|
8
|
+
sends: "nothing"
|
|
9
|
+
}));
|
|
10
|
+
async function typstWasmAsset() {
|
|
11
|
+
try {
|
|
12
|
+
return (await import("../dist/typst.wasm.ref.json", { with: { type: "json" } })).default;
|
|
13
|
+
} catch (err) {
|
|
14
|
+
throw new Error("@junejs/pdf-typst-wasm/unbundled: dist/typst.wasm.ref.json is missing — in the June repo, run `bun run build` in packages/pdf-typst-wasm", { cause: err });
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
function typstWasmRenderer(opts) {
|
|
18
|
+
const { load, engineEgress, ...rest } = opts;
|
|
19
|
+
return createTypstWasmRenderer(async () => loadVerified(await typstWasmAsset(), load), rest, engineEgress ?? ENGINE_HOSTS);
|
|
20
|
+
}
|
|
21
|
+
//#endregion
|
|
22
|
+
export { TypstCompileError, createEngine, engineUrls, googleFontSubsets, loadVerified, localFonts, typstWasmAsset, typstWasmRenderer };
|
package/dist/workerd.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { _ as WasmSource, a as TypstWasmRendererOptions, c as FontProvider, d as googleFontSubsets, f as localFonts, g as TypstOutput, h as TypstEngine, i as TypstWasmRenderer, l as FontSource, m as TypstCompileError, n as AssetRef, o as DocumentFonts, p as FontScope, r as loadVerified, s as FontEgress, t as AssetLoader, u as GoogleFont, v as createEngine } from "./assets-0jc0W7Pm.js";
|
|
2
2
|
//#region src/workerd.d.ts
|
|
3
3
|
export declare function typstWasmRenderer(opts?: TypstWasmRendererOptions): TypstWasmRenderer;
|
|
4
4
|
//#endregion
|
|
5
|
-
export { type DocumentFonts, type FontEgress, type FontProvider, type FontScope, type FontSource, type GoogleFont, TypstCompileError, type TypstEngine, type TypstOutput, type TypstWasmRenderer, type TypstWasmRendererOptions, type WasmSource, createEngine, googleFontSubsets, localFonts };
|
|
5
|
+
export { type AssetLoader, type AssetRef, type DocumentFonts, type FontEgress, type FontProvider, type FontScope, type FontSource, type GoogleFont, TypstCompileError, type TypstEngine, type TypstOutput, type TypstWasmRenderer, type TypstWasmRendererOptions, type WasmSource, createEngine, googleFontSubsets, loadVerified, localFonts };
|
package/dist/workerd.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { a as
|
|
1
|
+
import { a as TypstCompileError, i as createTypstWasmRenderer, n as googleFontSubsets, o as createEngine, r as localFonts, t as loadVerified } from "./assets-rrgfvCKs.js";
|
|
2
2
|
import wasm from "../dist/typst.wasm";
|
|
3
3
|
//#region src/workerd.ts
|
|
4
4
|
function typstWasmRenderer(opts = {}) {
|
|
5
5
|
return createTypstWasmRenderer(async () => wasm, opts);
|
|
6
6
|
}
|
|
7
7
|
//#endregion
|
|
8
|
-
export { TypstCompileError, createEngine, googleFontSubsets, localFonts, typstWasmRenderer };
|
|
8
|
+
export { TypstCompileError, createEngine, googleFontSubsets, loadVerified, localFonts, typstWasmRenderer };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@junejs/pdf-typst-wasm",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "June — a PdfRenderer on a Typst to PDF engine compiled to WebAssembly: no browser, no native binary, and it runs on Cloudflare Workers.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -25,6 +25,12 @@
|
|
|
25
25
|
"default": "./dist/node.js"
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
|
+
"./unbundled": {
|
|
29
|
+
"source": "./src/unbundled.ts",
|
|
30
|
+
"bun": "./src/unbundled.ts",
|
|
31
|
+
"types": "./dist/unbundled.d.ts",
|
|
32
|
+
"default": "./dist/unbundled.js"
|
|
33
|
+
},
|
|
28
34
|
"./typst.wasm": "./dist/typst.wasm"
|
|
29
35
|
},
|
|
30
36
|
"files": [
|
|
@@ -37,11 +43,11 @@
|
|
|
37
43
|
"test": "bun test"
|
|
38
44
|
},
|
|
39
45
|
"dependencies": {
|
|
40
|
-
"@junejs/pdf": "^0.2.
|
|
46
|
+
"@junejs/pdf": "^0.2.1"
|
|
41
47
|
},
|
|
42
48
|
"devDependencies": {
|
|
43
|
-
"@junejs/core": "^0.2.
|
|
44
|
-
"@junejs/server": "^0.2.
|
|
49
|
+
"@junejs/core": "^0.2.1",
|
|
50
|
+
"@junejs/server": "^0.2.1"
|
|
45
51
|
},
|
|
46
52
|
"keywords": [
|
|
47
53
|
"june",
|
package/src/assets.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// Large files a package needs but doesn't embed (#383). The `unbundled` entries never reference
|
|
2
|
+
// their files, so `bun build --compile` leaves ~41 MB of engine and fonts out of a binary that
|
|
3
|
+
// may never render a PDF; the app supplies a loader instead (a cache on a volume, a download,
|
|
4
|
+
// a file it shipped next to the binary).
|
|
5
|
+
//
|
|
6
|
+
// The package, not the loader, is what's trusted: every ref pins a size and a sha256, and
|
|
7
|
+
// loadVerified checks the loader's bytes against them before they are used, so a naive loader
|
|
8
|
+
// (one fetch, no checks) is as safe as a careful one. The URLs are where the file can be
|
|
9
|
+
// fetched; any of them may be down, and none of them needs to be trusted.
|
|
10
|
+
//
|
|
11
|
+
// Nothing here depends on PDF types: another package with a large optional file can use it.
|
|
12
|
+
|
|
13
|
+
export type AssetRef = {
|
|
14
|
+
// The file's name, for errors and for a cache directory: "typst.wasm".
|
|
15
|
+
name: string;
|
|
16
|
+
// Hex SHA-256 of the file's bytes: the only thing trusted.
|
|
17
|
+
sha256: string;
|
|
18
|
+
// Bytes. A loader may use it to cut a download off.
|
|
19
|
+
size: number;
|
|
20
|
+
// Where the file can be fetched, in order of preference.
|
|
21
|
+
urls: string[];
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
// The app's way to get a file's bytes: from a cache, a download, a pre-seeded directory.
|
|
25
|
+
export type AssetLoader = (ref: AssetRef) => Promise<Uint8Array | ArrayBuffer>;
|
|
26
|
+
|
|
27
|
+
// The loader's bytes, after checking them against the ref. Throws an error naming the file and
|
|
28
|
+
// both digests when they don't match: a wrong engine or font is never used.
|
|
29
|
+
export async function loadVerified(ref: AssetRef, load: AssetLoader): Promise<Uint8Array> {
|
|
30
|
+
const got = await load(ref);
|
|
31
|
+
// WebCrypto refuses a view on a SharedArrayBuffer: copy one into a plain buffer first.
|
|
32
|
+
const bytes = got instanceof Uint8Array ? (got.buffer instanceof SharedArrayBuffer ? got.slice() : got) : new Uint8Array(got);
|
|
33
|
+
if (bytes.byteLength !== ref.size) throw new Error(`${ref.name}: the loader returned ${bytes.byteLength} bytes, expected ${ref.size} (sha256 ${ref.sha256})`);
|
|
34
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes as Uint8Array<ArrayBuffer>);
|
|
35
|
+
const hex = [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
36
|
+
if (hex !== ref.sha256) throw new Error(`${ref.name}: the loader returned bytes with sha256 ${hex}, expected ${ref.sha256}`);
|
|
37
|
+
return bytes;
|
|
38
|
+
}
|
package/src/node.ts
CHANGED
|
@@ -11,6 +11,7 @@ import { createTypstWasmRenderer, type TypstWasmRenderer, type TypstWasmRenderer
|
|
|
11
11
|
|
|
12
12
|
export { type TypstWasmRenderer, type TypstWasmRendererOptions } from "./renderer";
|
|
13
13
|
export { googleFontSubsets, localFonts, type DocumentFonts, type FontEgress, type FontProvider, type FontSource, type GoogleFont } from "./fonts";
|
|
14
|
+
export { loadVerified, type AssetLoader, type AssetRef } from "./assets";
|
|
14
15
|
export { createEngine, TypstCompileError, type FontScope, type TypstEngine, type TypstOutput, type WasmSource } from "./engine";
|
|
15
16
|
|
|
16
17
|
export const WASM_URL = new URL("../dist/typst.wasm", import.meta.url);
|
package/src/renderer.ts
CHANGED
|
@@ -41,15 +41,18 @@ export type TypstWasmRendererOptions = {
|
|
|
41
41
|
};
|
|
42
42
|
|
|
43
43
|
export interface TypstWasmRenderer extends PdfRenderer {
|
|
44
|
-
// Every host the
|
|
45
|
-
//
|
|
44
|
+
// Every host the renderer may contact, and what each is sent: the font providers', plus,
|
|
45
|
+
// with the unbundled entry, where loading the engine goes (`engineEgress`). Empty means it
|
|
46
|
+
// works offline. Declared, not observed (see fonts.ts).
|
|
46
47
|
readonly egress: readonly FontEgress[];
|
|
47
48
|
}
|
|
48
49
|
|
|
49
50
|
// How many missing characters a message names before it counts the rest.
|
|
50
51
|
const MISSING_SHOWN = 10;
|
|
51
52
|
|
|
52
|
-
|
|
53
|
+
// `engineEgress`: what getting the engine itself contacts, for an entry whose engine is fetched
|
|
54
|
+
// (unbundled.ts); the bundled entries read it from the package and contact nothing.
|
|
55
|
+
export function createTypstWasmRenderer(defaultWasm: () => Promise<WasmSource>, opts: TypstWasmRendererOptions = {}, engineEgress: readonly FontEgress[] = []): TypstWasmRenderer {
|
|
53
56
|
const providers = [...(opts.fonts ?? [])];
|
|
54
57
|
let engine: Promise<TypstEngine> | undefined;
|
|
55
58
|
|
|
@@ -67,7 +70,7 @@ export function createTypstWasmRenderer(defaultWasm: () => Promise<WasmSource>,
|
|
|
67
70
|
|
|
68
71
|
return {
|
|
69
72
|
name: "typst-wasm",
|
|
70
|
-
egress: uniqueEgress(providers),
|
|
73
|
+
egress: uniqueEgress([...engineEgress, ...providers.flatMap((p) => p.egress)]),
|
|
71
74
|
async render(doc: PdfDocument): Promise<Uint8Array> {
|
|
72
75
|
const source = typstSource(doc, { fonts: opts.fontFamilies });
|
|
73
76
|
const [e, documentFonts] = await Promise.all([ready(), fontsFor(doc, providers)]);
|
|
@@ -94,9 +97,9 @@ export function createTypstWasmRenderer(defaultWasm: () => Promise<WasmSource>,
|
|
|
94
97
|
};
|
|
95
98
|
}
|
|
96
99
|
|
|
97
|
-
function uniqueEgress(
|
|
100
|
+
function uniqueEgress(all: readonly FontEgress[]): FontEgress[] {
|
|
98
101
|
const seen = new Map<string, FontEgress>();
|
|
99
|
-
for (const e of
|
|
102
|
+
for (const e of all) {
|
|
100
103
|
const prior = seen.get(e.host);
|
|
101
104
|
// A host sent the document's characters by any provider is sent them, full stop.
|
|
102
105
|
if (!prior || e.sends === "document-characters") seen.set(e.host, { host: e.host, sends: e.sends });
|
package/src/unbundled.ts
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
// @junejs/pdf-typst-wasm/unbundled — the renderer without its engine file (#383). The default
|
|
2
|
+
// entries embed typst.wasm (bun.ts imports it as a file, workerd.ts as a module), so a
|
|
3
|
+
// `bun build --compile` binary carries its 29 MiB whether or not it ever renders. This entry
|
|
4
|
+
// never references the file: the app passes `load`, and the engine is fetched, read or taken
|
|
5
|
+
// from a cache only on the first render.
|
|
6
|
+
//
|
|
7
|
+
// import { typstWasmRenderer } from "@junejs/pdf-typst-wasm/unbundled";
|
|
8
|
+
// typstWasmRenderer({ load: (ref) => myCache.get(ref) });
|
|
9
|
+
//
|
|
10
|
+
// The engine's ref (size, sha256, URLs) is generated by scripts/build-wasm.ts from the file
|
|
11
|
+
// being published, since a local build's bytes differ from CI's. loadVerified checks the
|
|
12
|
+
// loader's bytes against it, so an engine that doesn't match the release is never run. In a
|
|
13
|
+
// checkout without a build there is no ref, and the first render says so.
|
|
14
|
+
//
|
|
15
|
+
// Runtime-neutral: no host imports, so the same file serves Node, Bun, Deno and workerd.
|
|
16
|
+
|
|
17
|
+
import { loadVerified, type AssetLoader, type AssetRef } from "./assets";
|
|
18
|
+
import type { FontEgress } from "./fonts";
|
|
19
|
+
import { createTypstWasmRenderer, type TypstWasmRenderer, type TypstWasmRendererOptions } from "./renderer";
|
|
20
|
+
|
|
21
|
+
export { loadVerified, type AssetLoader, type AssetRef } from "./assets";
|
|
22
|
+
export { googleFontSubsets, localFonts, type DocumentFonts, type FontEgress, type FontProvider, type FontSource, type GoogleFont } from "./fonts";
|
|
23
|
+
export { createEngine, TypstCompileError, type FontScope, type TypstEngine, type TypstOutput, type WasmSource } from "./engine";
|
|
24
|
+
export type { TypstWasmRenderer } from "./renderer";
|
|
25
|
+
|
|
26
|
+
// Where a release's engine can be fetched: the npm tarball's own file, through two CDNs that
|
|
27
|
+
// serve npm packages. The sha256 makes either as good as the other.
|
|
28
|
+
export function engineUrls(version: string): string[] {
|
|
29
|
+
return [`https://cdn.jsdelivr.net/npm/@junejs/pdf-typst-wasm@${version}/dist/typst.wasm`, `https://unpkg.com/@junejs/pdf-typst-wasm@${version}/dist/typst.wasm`];
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const ENGINE_HOSTS: FontEgress[] = engineUrls("0").map((url) => ({ host: new URL(url).host, sends: "nothing" }));
|
|
33
|
+
|
|
34
|
+
// The ref of the engine this release ships. Rejects in a checkout that hasn't run
|
|
35
|
+
// `bun run build`.
|
|
36
|
+
export async function typstWasmAsset(): Promise<AssetRef> {
|
|
37
|
+
try {
|
|
38
|
+
// @ts-ignore — written by scripts/build-wasm.ts; absent until the engine is built.
|
|
39
|
+
return (await import("../dist/typst.wasm.ref.json", { with: { type: "json" } })).default as AssetRef;
|
|
40
|
+
} catch (err) {
|
|
41
|
+
throw new Error("@junejs/pdf-typst-wasm/unbundled: dist/typst.wasm.ref.json is missing — in the June repo, run `bun run build` in packages/pdf-typst-wasm", { cause: err });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export type UnbundledRendererOptions = Omit<TypstWasmRendererOptions, "wasm"> & {
|
|
46
|
+
// Gets the engine's bytes for its ref, on the first render. They are checked against the
|
|
47
|
+
// ref's size and sha256 before they run.
|
|
48
|
+
load: AssetLoader;
|
|
49
|
+
// What `load` contacts, for renderer.egress. Default: the ref's URL hosts, sending nothing
|
|
50
|
+
// (the engine may be downloaded). [] when the app pre-seeds it and never fetches.
|
|
51
|
+
engineEgress?: FontEgress[];
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
export function typstWasmRenderer(opts: UnbundledRendererOptions): TypstWasmRenderer {
|
|
55
|
+
const { load, engineEgress, ...rest } = opts;
|
|
56
|
+
return createTypstWasmRenderer(async () => loadVerified(await typstWasmAsset(), load), rest, engineEgress ?? ENGINE_HOSTS);
|
|
57
|
+
}
|
package/src/workerd.ts
CHANGED
|
@@ -13,6 +13,7 @@ import { createTypstWasmRenderer, type TypstWasmRenderer, type TypstWasmRenderer
|
|
|
13
13
|
|
|
14
14
|
export { type TypstWasmRenderer, type TypstWasmRendererOptions } from "./renderer";
|
|
15
15
|
export { googleFontSubsets, localFonts, type DocumentFonts, type FontEgress, type FontProvider, type FontSource, type GoogleFont } from "./fonts";
|
|
16
|
+
export { loadVerified, type AssetLoader, type AssetRef } from "./assets";
|
|
16
17
|
export { createEngine, TypstCompileError, type FontScope, type TypstEngine, type TypstOutput, type WasmSource } from "./engine";
|
|
17
18
|
|
|
18
19
|
export function typstWasmRenderer(opts: TypstWasmRendererOptions = {}): TypstWasmRenderer {
|