Fix WASM factory blob URL loading (#1527, #1532)#1558
Merged
Conversation
- Add canUseBlobURLs() to detect restricted environments (Service Workers, extensions, multi-threading)
- Add env.useWasmBlobURL setting ('auto' | true | false) to control blob URL usage
- Skip Cache API operations when blob URLs are disabled (HTTP cache fallback)
- Fix import.meta.url references in blob URL code for correct path resolution
… and the factory)
This will be useful in other places too, I'm sure
xenova
approved these changes
Mar 5, 2026
Collaborator
xenova
left a comment
There was a problem hiding this comment.
Very nice PR! 🙏
I just made some minor updates: moving env detection to env.js and simplifying loadWasmFactory. also updated some comments.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Two related bugs in
loadWasmFactory(introduced in v4.0.0-next.3):loadWasmFactory#1532: Theimport.meta.urlstring replacement was broken —new URL(fileName, import.meta.url)inside the blob URL factory resolved againstblob:...instead of the original CDN URL, producing malformed Worker URLs.wasmPathscaused a CORS error when constructing Workers from a different origin (e.g.localhostdev server).Root cause
The factory
.mjsfile was cached and re-executed as a blob URL, butimport.meta.urlinside it becameblob:.... The attempted fix (string-replacingimport.meta.url) was fragile and broke edgecases.Fix
Relies on onnxruntime PR #27411 (merged Feb 26 2026): when
wasmBinaryis pre-set in the ORT config, ORT setslocateFile = (f) => f, bypassing thenew URL(fileName, import.meta.url)call entirely.Changes:
import.meta.urlstring replacements fromloadWasmFactory.wasmbinary and setONNX_ENV.wasm.wasmBinarybefore the factory executes; the.wasmand.mjsfetches run in parallel, with a revert guard: ifwasmBinaryfails,wasmPaths.mjsis reverted so ORT never sees a blob URL without a corresponding binarycanUseBlobURLs()exclude service workers and Chrome extensions from using the blob "hack"useWasmCachenow controls both.wasmbinary caching and.mjsfactory blob URL caching (they are coupled: a blob URL factory is only safe whenwasmBinaryis set)globalThis.process?.versions?.node → falsein the factory blob to prevent Deno's web runtime from entering the Node.js branch; throw a clear error ifuseWasmCache=falseis set in Deno