import type { WorkerCommand, WorkerCommandMap } from './protocol.js'; import { type PdfPageRenderCancellationToken } from './render-queue.js'; import { type PdfWorkerLike, type PdfWorkerUrls } from './worker-host.js'; /** Low-level worker options used by {@link PdfrxEngineOptions}. */ export interface WorkerCommunicatorOptions { /** * Base URL of the directory that contains `pdfium_worker.js` and `pdfium.wasm`, * resolved against {@link baseUrl} when relative. * * Required in a browser, where those files have to be served. Everywhere else * it defaults to the `assets/` directory shipped inside this package, so there * is nothing to pass — unless the package's files are not on disk as published * (a bundled server build, say), in which case point this at wherever they went. */ wasmModulesUrl?: string; /** * What relative URLs — {@link wasmModulesUrl} and the ones passed to * `PdfrxEngine.openUrl` — resolve against. Defaults to `document.baseURI` in a * browser and to the current working directory elsewhere. */ baseUrl?: string; /** Extra headers sent when the worker fetches `pdfium.wasm`. */ headers?: Record; /** Whether the worker's `pdfium.wasm` fetch includes credentials. */ withCredentials?: boolean; /** * Escape hatch for starting the worker yourself, for a host the engine does * not already know (it handles browsers, Node, Bun and Deno on its own) or one * that needs its worker configured — permissions, resource limits, a * self-hosted copy of `pdfium_worker.js`. Return anything that behaves like a * Web Worker; see {@link PdfWorkerLike} and {@link PdfWorkerUrls}. */ createWorker?: (urls: PdfWorkerUrls) => PdfWorkerLike | Promise; } /** * Owns the rendering worker and speaks its raw command protocol. The worker is * started the way the host runs workers — a Web Worker in a browser, the * platform equivalent on Node, Bun and Deno — unless * {@link WorkerCommunicatorOptions.createWorker} says otherwise. */ export declare class WorkerCommunicator { /** * The worker, which some hosts can only produce asynchronously. Commands are * posted from `.then` callbacks on it, which run in call order, so awaiting it * per command does not reorder them. */ private readonly workerPromise; /** The worker once it exists, so {@link dispose} can terminate it right away. */ private worker; /** What relative URLs resolve against; see {@link WorkerCommunicatorOptions.baseUrl}. */ readonly baseUrl: string; private readonly pending; private readonly callbacks; private requestId; private callbackId; private readonly initPromise; private disposed; /** Renders are queued here rather than in the worker, so they stay cancellable. */ private readonly renderQueue; /** * Spawns the worker and kicks off engine initialization. The worker starts * fetching `pdfium.wasm` immediately; await {@link ready} before relying on it. */ constructor(options: WorkerCommunicatorOptions); /** Resolves when the worker has loaded and initialized the WASM engine. */ get ready(): Promise; /** * Dispatches a {@link WorkerMessage}: resolves/rejects the matching pending * request, or routes an unsolicited `callback`/`error`/`ready` notification. * @internal */ private onMessage; /** * Sends a typed command to the worker and resolves with its typed result. * * Every command except `init` waits for `ready` first. Pass `transfer` * to hand ownership of `ArrayBuffer`s (e.g. document/JPEG/font bytes) to the * worker without copying. * * @param transfer Transferable objects to move (not copy) to the worker. */ sendCommand(command: C, parameters: WorkerCommandMap[C]['params'], transfer?: Transferable[]): Promise; /** * Posts a command with a fresh request id and returns a promise settled by * the worker's reply. Rejects immediately if the communicator is disposed. * @internal */ private sendCommandRaw; /** * Runs `send` under the worker's render queue: it waits for a free slot and * resolves to `null` if `token` is cancelled first. Used by * {@link PdfPage.render} so a page that scrolls out of view can drop its * pending render instead of blocking the pages now on screen. * @internal */ enqueueRender(send: () => Promise, token?: PdfPageRenderCancellationToken): Promise; /** Renders waiting for a worker slot (not counting the one being rendered). */ get pendingRenderCount(): number; /** * Registers a callback the worker can invoke by id (e.g. download progress), * and returns that id to pass along in a command's parameters. * Remember to {@link unregisterCallback} it when done to avoid leaks. */ registerCallback(callback: (...args: never[]) => void): number; /** Removes a callback previously added with {@link registerCallback}. */ unregisterCallback(id: number): void; /** Terminates the worker. All documents opened through it become unusable. */ dispose(): void; } //# sourceMappingURL=communicator.d.ts.map