|
| 1 | +// @deltic/translator — the packaged translator wasm plus its per-platform |
| 2 | +// loader (issue #16 delivery design note, item 3). |
| 3 | +// |
| 4 | +// Why a separate package: the translator is a versioned peer of the |
| 5 | +// runtime (plan-format coupling), so it ships inside the same release — |
| 6 | +// but embedders that translate at BUILD time (tools/translate, |
| 7 | +// embedder-api A4) deploy no translator at all, and keeping the ~1.85 MB |
| 8 | +// asset out of @deltic/runtime keeps their production graphs clean. |
| 9 | +// |
| 10 | +// The asset (`translator_shim.wasm`, sibling to this module) is copied |
| 11 | +// from the cargo build by `just shim` and is gitignored — run `just shim` |
| 12 | +// once in a fresh checkout. Publish tooling will pin the exact asset (and |
| 13 | +// its digest) into the released package when #16's packaging lands. |
| 14 | + |
| 15 | +import { Translator } from "@deltic/runtime/shim"; |
| 16 | + |
| 17 | +let singleton: Promise<Translator> | undefined; |
| 18 | + |
| 19 | +/** |
| 20 | + * The packaged translator, loaded lazily and cached for the realm. |
| 21 | + * |
| 22 | + * Platform paths, in order of preference: |
| 23 | + * |
| 24 | + * * **Deno** — native wasm-module import: stable, permission-free, and |
| 25 | + * delivery/caching ride the module cache. The shim imports nothing, |
| 26 | + * so the ESM integration instantiates it trivially and |
| 27 | + * `Translator.fromExports` wraps the namespace with no compile and no |
| 28 | + * copy. (`buildHash` is unrecoverable from an instance, so the |
| 29 | + * artifact cache keys without translator identity on this path — |
| 30 | + * see Translator.buildHash.) |
| 31 | + * * **Node** — `node:fs` read of the packaged asset (Node's wasm-module |
| 32 | + * imports are still experimental; don't build on them). |
| 33 | + * * **Browser / workers** — `fetch` of the packaged asset URL (bundlers |
| 34 | + * understand the `new URL(…, import.meta.url)` pattern and carry the |
| 35 | + * asset). |
| 36 | + * |
| 37 | + * Pass the result to `instantiate({ componentBytes, translator })` |
| 38 | + * (embedder-api A3), or call `.translate()` directly. |
| 39 | + */ |
| 40 | +export function defaultTranslator(): Promise<Translator> { |
| 41 | + return singleton ??= load(); |
| 42 | +} |
| 43 | + |
| 44 | +async function load(): Promise<Translator> { |
| 45 | + const url = new URL("./translator_shim.wasm", import.meta.url); |
| 46 | + try { |
| 47 | + if (typeof Deno !== "undefined") { |
| 48 | + // String-literal dynamic import: statically analyzable, so the wasm |
| 49 | + // rides the module graph permission-free; still lazy, and non-Deno |
| 50 | + // platforms never evaluate the Deno-only module (see its header). |
| 51 | + const { ns } = await import("./shim_asset_deno.ts"); |
| 52 | + return Translator.fromExports(ns); |
| 53 | + } |
| 54 | + const proc = (globalThis as { process?: { versions?: { node?: string } } }) |
| 55 | + .process; |
| 56 | + if (proc?.versions?.node) { |
| 57 | + const { readFile } = await import("node:fs/promises"); |
| 58 | + return await Translator.create(new Uint8Array(await readFile(url))); |
| 59 | + } |
| 60 | + const res = await fetch(url); |
| 61 | + if (!res.ok) { |
| 62 | + throw new Error(`fetching the translator asset failed: ${res.status}`); |
| 63 | + } |
| 64 | + return await Translator.create(new Uint8Array(await res.arrayBuffer())); |
| 65 | + } catch (e) { |
| 66 | + // The overwhelmingly likely in-repo cause is the missing gitignored |
| 67 | + // asset; say so instead of leaking a bare module-resolution error. |
| 68 | + throw new Error( |
| 69 | + `@deltic/translator: could not load ${url}: ${e}\n` + |
| 70 | + `(in a repo checkout, run \`just shim\` to build and place the asset)`, |
| 71 | + { cause: e }, |
| 72 | + ); |
| 73 | + } |
| 74 | +} |
0 commit comments