Skip to content

Fix WASM factory blob URL loading (#1527, #1532)#1558

Merged
xenova merged 16 commits intomainfrom
v4-loadWasmFactory-fix
Mar 5, 2026
Merged

Fix WASM factory blob URL loading (#1527, #1532)#1558
xenova merged 16 commits intomainfrom
v4-loadWasmFactory-fix

Conversation

@nico-martin
Copy link
Copy Markdown
Collaborator

@nico-martin nico-martin commented Mar 5, 2026

Problem

Two related bugs in loadWasmFactory (introduced in v4.0.0-next.3):

Root cause

The factory .mjs file was cached and re-executed as a blob URL, but import.meta.url inside it became blob:.... The attempted fix (string-replacing import.meta.url) was fragile and broke edgecases.

Fix

Relies on onnxruntime PR #27411 (merged Feb 26 2026): when wasmBinary is pre-set in the ORT config, ORT sets locateFile = (f) => f, bypassing the new URL(fileName, import.meta.url) call entirely.

Changes:

  • Removesthe import.meta.url string replacements from loadWasmFactory
  • Pre-fetch the .wasm binary and set ONNX_ENV.wasm.wasmBinary before the factory executes; the .wasm and .mjs fetches run in parallel, with a revert guard: if wasmBinary fails, wasmPaths.mjs is reverted so ORT never sees a blob URL without a corresponding binary
  • Added canUseBlobURLs() exclude service workers and Chrome extensions from using the blob "hack"
  • useWasmCache now controls both .wasm binary caching and .mjs factory blob URL caching (they are coupled: a blob URL factory is only safe when wasmBinary is set)
  • Deno support: patch globalThis.process?.versions?.node → false in the factory blob to prevent Deno's web runtime from entering the Node.js branch; throw a clear error if useWasmCache=false is set in Deno

@nico-martin nico-martin requested a review from xenova March 5, 2026 10:32
Copy link
Copy Markdown
Collaborator

@xenova xenova left a comment

Choose a reason for hiding this comment

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

Very nice PR! 🙏

I just made some minor updates: moving env detection to env.js and simplifying loadWasmFactory. also updated some comments.

@xenova xenova merged commit 30773fb into main Mar 5, 2026
4 checks passed
@xenova xenova deleted the v4-loadWasmFactory-fix branch March 5, 2026 20:47
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.

[v4.0.0-next.3] Incorrect implementation of loadWasmFactory [4.0.0-next.3] CDN wasmPaths breaks local dev servers (CORS Worker construction error)

2 participants