Skip to content

feat(engines): add workerUrl and encoderWorkerUrl for CSP-friendly workers#634

Open
IBRAHIMDANS wants to merge 1 commit into
embedpdf:mainfrom
IBRAHIMDANS:feature/worker-url-csp
Open

feat(engines): add workerUrl and encoderWorkerUrl for CSP-friendly workers#634
IBRAHIMDANS wants to merge 1 commit into
embedpdf:mainfrom
IBRAHIMDANS:feature/worker-url-csp

Conversation

@IBRAHIMDANS
Copy link
Copy Markdown

@IBRAHIMDANS IBRAHIMDANS commented May 15, 2026

Summary

Closes: #628

Add workerUrl and encoderWorkerUrl options to CreatePdfiumEngineOptions so that the PDFium worker and the image encoder worker pool can be loaded from a static URL instead of a blob: object URL.

  • workerUrl — URL to the PDFium worker script
  • encoderWorkerUrl — URL to the image encoder worker script
  • Both options are propagated through the React/shared, Svelte, and Vue hooks
  • When omitted, the existing blob: fallback is preserved (no breaking change)

Motivation

Apps with a strict Content Security Policy that omit blob: from worker-src (or child-src) are currently blocked when using usePdfiumEngine({ worker: true }):

Refused to create a worker from 'blob:...' because it violates the CSP directive: "worker-src 'self'"

This is a blocker for banking, healthcare, and defense environments where compliance frameworks flag worker-src blob:. Libraries like pdf.js and monaco-editor already expose the worker as a configurable URL — this PR brings EmbedPDF in line with that pattern.

Usage

// Vite — bundle the worker as a static asset
usePdfiumEngine({
  wasmUrl: '/assets/pdfium.wasm',
  workerUrl: new URL('@embedpdf/engines/pdfium-worker-engine', import.meta.url).href,
  encoderWorkerUrl: '/assets/embedpdf-encoder-worker.js',
});

// Or serve pre-copied static files
usePdfiumEngine({
  wasmUrl: '/assets/pdfium.wasm',
  workerUrl: '/assets/embedpdf-worker.js',
  encoderWorkerUrl: '/assets/embedpdf-encoder-worker.js',
});

CSP header can then be simply:

worker-src 'self'

Notes

  • No tests added: @embedpdf/engines has no test infrastructure today. The branching logic (workerUrl ?? createObjectURL(...)) is minimal enough that the diff is self-explanatory.
  • The /docs/snippet/airgapped page would benefit from a short section on these two options — happy to contribute that separately if the maintainers agree on the API shape.

…endly workers

Allow configuring the PDFium worker and image encoder worker via static
URLs instead of blob: object URLs, enabling strict Content Security
Policies without requiring `worker-src blob:`.

When the option is omitted, the existing blob: fallback is preserved
for full backward compatibility.

Closes embedpdf#628
Copilot AI review requested due to automatic review settings May 15, 2026 06:35
@vercel
Copy link
Copy Markdown

vercel Bot commented May 15, 2026

Someone is attempting to deploy a commit to the OpenBook Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds CSP-friendly worker URL configuration for the PDFium worker engine and propagates the new options through framework hooks.

Changes:

  • Added workerUrl and encoderWorkerUrl options to PDFium engine creation options.
  • Used provided worker URLs instead of generated blob: URLs when available.
  • Propagated the new options through React/Preact shared, Svelte, and Vue hooks.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
packages/engines/src/lib/pdfium/web/worker-engine.ts Adds worker URL options and uses them when constructing PDFium and encoder workers.
packages/engines/src/lib/pdfium/web/direct-engine.ts Extends the shared options type with worker URL fields for compatibility.
packages/engines/src/shared/hooks/use-pdfium-engine.ts Passes worker URL options through the React/Preact hook.
packages/engines/src/svelte/hooks/use-pdfium-engine.svelte.ts Passes worker URL options through the Svelte hook.
packages/engines/src/vue/composables/use-pdfium-engine.ts Passes worker URL options through the Vue composable.
Comments suppressed due to low confidence (2)

packages/engines/src/svelte/hooks/use-pdfium-engine.svelte.ts:45

  • This newly added call is not formatted according to the repo's Prettier configuration (prettier/prettier is enforced in ESLint with a 100-column print width). Please run the formatter so this line is wrapped consistently.
          const pdfEngine = await createPdfiumEngine(wasmUrl, { logger, fontFallback, workerUrl, encoderWorkerUrl });

packages/engines/src/vue/composables/use-pdfium-engine.ts:58

  • This newly added call is not formatted according to the repo's Prettier configuration (prettier/prettier is enforced in ESLint with a 100-column print width). Please run the formatter so this line is wrapped consistently.
      const pdfEngine = await createPdfiumEngine(wasmUrl, { logger, fontFallback, workerUrl, encoderWorkerUrl });

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

}, ignore);
};
}, [wasmUrl, worker, logger, fontFallback]);
}, [wasmUrl, worker, logger, fontFallback, workerUrl, encoderWorkerUrl]);
*/
export function usePdfiumEngine(props: UsePdfiumEngineProps = {}): UsePdfiumEngineResult {
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback } = props;
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback, workerUrl, encoderWorkerUrl } = props;
Comment on lines +38 to +41
* // Vite
* workerUrl: new URL('@embedpdf/engines/pdfium-worker', import.meta.url).href
* // Or a static asset
* workerUrl: '/assets/embedpdf-worker.js'

export function usePdfiumEngine(config?: UsePdfiumEngineProps) {
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback } = config ?? {};
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback, workerUrl, encoderWorkerUrl } = config ?? {};
*/
export function usePdfiumEngine(props: UsePdfiumEngineProps = {}): UsePdfiumEngineResult {
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback } = props;
const { wasmUrl = defaultWasmUrl, worker = true, logger, fontFallback, workerUrl, encoderWorkerUrl } = props;
@IBRAHIMDANS
Copy link
Copy Markdown
Author

@copilot apply changes based on the comments in this thread

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow configuring the PDFium worker as an external asset URL (CSP-friendly, no blob:)

2 participants