/** * Where does `embedpdf.wasm` come from? * * The 6 MB binary is fetched at runtime from a plain URL, resolved here on * the main thread (the worker never guesses) in a fixed precedence order: * * 1. `wasmBinary` — caller-supplied bytes, zero network (air-gapped). * 2. `wasmUrl` — exact URL. * 3. `assetsUrl` — base directory; `embedpdf.wasm` is appended. * 4. the default — depends on how the worker itself is delivered: * - the bundled worker file or a blob worker (the zero-config paths): * the bundler-resolved URL from `@embedpdf/engine-runtime-wasm32/wasm-url` * (the wasm ships inside the consumer's own build). Neither worker has * the wasm beside it (a bundler renames the files; a blob URL has no * location), so the URL is resolved here and must be absolute. A dev * server that prebundled the package without its files has no such * URL: there the bytes come from `@embedpdf/engine/dev-wasm`. * - a configured worker URL / caller-built worker: nothing is sent, and * the Emscripten glue resolves `embedpdf.wasm` as a sibling of the * worker script. Copying `embedpdf-worker.js` and `embedpdf.wasm` into * one directory is a complete self-host setup, and bundler-emitted * workers (Vite `?worker`) keep their bundler-managed asset next to * the worker chunk. */ import { unpackInlineWasm } from './inline-wasm'; import { prebundledDevWasm } from './worker-files'; /** * How to deliver the engine's Web Worker: * - omitted (default): this package's `workers/embedpdf-worker.js`, emitted * by the consumer's bundler as a file of their build, so `worker-src 'self'` * covers it. Where the scripts are on another origin than the page (the * viewer from a CDN), a browser refuses that worker, so it starts from a * blob: URL instead (`worker-src blob:`). So it does where a dev server * prebundled the package without the file (Vite 7 and older). * - `'inline'`: always the blob: URL, built from the worker source string * shipped inside this package — for toolchains that cannot emit the file * (`@embedpdf/engine/portable` passes it). * - a URL string: a same-origin static worker file, copied from this * package's `workers/` directory. * - a `Worker` or `() => Worker`: full control (custom worker builds, shared * lifecycles). */ export type WorkerSource = 'inline' | string | Worker | (() => Worker); export interface WasmSourceOptions { /** Exact URL of `embedpdf.wasm` (absolute, or relative to the page). */ wasmUrl?: string; /** Pre-fetched `embedpdf.wasm` bytes — no network request is made. */ wasmBinary?: ArrayBuffer | Uint8Array; /** Base directory for self-hosted runtime assets; `embedpdf.wasm` is appended. */ assetsUrl?: string; /** * The bytes, produced on demand at boot — what `@embedpdf/engine/portable` * passes: the binary carried through the module graph as a lazy chunk of * the app. Explicit like the three above (it never falls back to anything * else), but costs nothing until the engine actually boots. */ wasmLoader?: () => Promise; } /** The wire shape sent to the worker's init message. */ export interface ResolvedWasmSource { wasmUrl?: string; wasmBinary?: ArrayBuffer; } /** * Resolve the caller's wasm options into what the worker init carries. * Explicit sources only — the default for the package's own worker * deliveries lives in {@link resolveDefaultWasmSource}; a configured or * caller-built worker self-resolves the wasm as a sibling of the worker * script when no explicit source is given (hence the empty result). A * `wasmLoader` is explicit too, but asynchronous: see {@link resolveWasmSourceAsync}. */ export function resolveWasmSource(options: WasmSourceOptions): ResolvedWasmSource { if (options.wasmBinary !== undefined) { return { wasmBinary: toStandaloneBuffer(options.wasmBinary) }; } if (options.wasmUrl !== undefined) { return { wasmUrl: toAbsoluteUrl(options.wasmUrl) }; } if (options.assetsUrl !== undefined) { const base = options.assetsUrl.endsWith('/') ? options.assetsUrl : `${options.assetsUrl}/`; return { wasmUrl: toAbsoluteUrl(`${base}embedpdf.wasm`) }; } return {}; } /** {@link resolveWasmSource} plus the asynchronous explicit source, `wasmLoader`. */ export async function resolveWasmSourceAsync( options: WasmSourceOptions, ): Promise { const explicit = resolveWasmSource(options); if (explicit.wasmUrl !== undefined || explicit.wasmBinary !== undefined) return explicit; if (options.wasmLoader) return { wasmBinary: toStandaloneBuffer(await options.wasmLoader()) }; return {}; } /** * Resolve the wasm source for the package's own worker deliveries (the * bundled file and the blob), which cannot self-resolve: a bundler renames * the worker file, and a blob URL has no meaningful location. Explicit * options win; otherwise the default is the sibling the consumer's bundler * emitted: * `@embedpdf/engine-runtime-wasm32/wasm-url`, a static * `new URL('./lib/embedpdf.wasm', import.meta.url)` that webpack, Vite, * Rspack, Parcel, and Turbopack resolve at build time, shipping the wasm * inside the consumer's own build — served from their origin, compiled * streaming by the worker. * * One build has no such sibling and still starts: a dev server that * prebundled the package without `embedpdf.wasm` gets the copy its build * carries for that (see {@link prebundledDevWasm}). * * There is deliberately nothing after that. A toolchain that cannot carry * the asset (Angular's application builder, plain esbuild) fails here with * the two fixes named: `@embedpdf/engine/portable`, which carries the wasm * through the module graph instead, or `assetsUrl` to a self-hosted copy. * No CDN is ever contacted: a request that leaves the app's origin is * something the app configures, never something the engine decides. */ export async function resolveDefaultWasmSource( options: WasmSourceOptions, ): Promise { const explicit = resolveWasmSource(options); if (explicit.wasmUrl !== undefined || explicit.wasmBinary !== undefined) return explicit; if (options.wasmLoader) return { wasmBinary: toStandaloneBuffer(await options.wasmLoader()) }; // The sibling's URL is wrong where the worker files' are: both were left behind. const devWasm = await prebundledDevWasm(); if (devWasm !== null) return { wasmBinary: await unpackInlineWasm(devWasm) }; const sibling = await siblingWasmUrl(); if (sibling === null) throw new Error(NO_SIBLING_MESSAGE); // Not always absolute: webpack's RelativeURL runtime yields a root-relative // href like `/_next/static/media/embedpdf..wasm`, which a blob: // worker cannot resolve (see toAbsoluteUrl). return { wasmUrl: toAbsoluteUrl(sibling) }; } const NO_SIBLING_MESSAGE = 'embedpdf.wasm has no default location in this build: the bundler-resolved ' + '`@embedpdf/engine-runtime-wasm32/wasm-url` module is unavailable. Either import ' + '`localEngine` from `@embedpdf/engine/portable` (the wasm travels inside your build as ' + 'a lazy chunk — works with every bundler), or self-host the file and pass `assetsUrl` / ' + '`wasmUrl` / `wasmBinary`. See https://www.embedpdf.com/docs/viewer/self-hosting'; /** * The bundler-resolved sibling URL, or null when this build has none. The * module cannot fail to resolve under a bundler (that is a build-time error, * as it should be); it can fail to evaluate — an output format where * `import.meta.url` is undefined makes its `new URL()` throw — or export a * non-string where a bundled artifact aliased it away. Both mean "no * sibling", never "crash the engine". */ async function siblingWasmUrl(): Promise { try { const href: unknown = (await import('@embedpdf/engine-runtime-wasm32/wasm-url')).default; return typeof href === 'string' && href.length > 0 ? href : null; } catch { return null; } } /** * Resolve against the page now: a relative URL like `/assets/embedpdf.wasm` * cannot be resolved inside a `blob:` worker (blob URLs are not hierarchical), * so the absolute form must cross the postMessage boundary. */ export function toAbsoluteUrl(url: string): string { if (typeof document !== 'undefined') return new URL(url, document.baseURI).href; if (typeof location !== 'undefined') return new URL(url, location.href).href; return url; } /** * Copy to a standalone ArrayBuffer: the init message transfers the buffer to * the worker, and neutering the caller's copy would break a second engine * created from the same options (or a larger buffer the caller still owns). */ function toStandaloneBuffer(input: ArrayBuffer | Uint8Array): ArrayBuffer { if (input instanceof Uint8Array) { const copy = new ArrayBuffer(input.byteLength); new Uint8Array(copy).set(input); return copy; } return input.slice(0); }