You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(wasm): loadModule({ wasm }) works on Cloudflare Workers - #195
Fixes#194. On Cloudflare Workers (nodejs_compat), loadModule({ wasm }) rejects before it ever uses the bytes it was given. The Emscripten glue treats any host with WorkerGlobalScope as a worker and reads self.location.href, and any host with process.versions.node as Node and reads __dirname. workerd has neither of those values. Reproduced with libpg-query 18.2.2 + wrangler 4.157.0, and got both errors from the issue in order.
The fix is a --pre-js (templates/pre.js, copied to every version by copy:templates). Emscripten emits it right after the environment detection:
Real Node (CJS or ESM import) always has __dirname in the glue's scope, and real browsers and workers always have self.location, so their behavior doesn't change.
Only the JS glue changes. The rebuilt v18 libpg-query.js is exactly the published 18.2.2 glue plus that one inserted statement. No .wasm, wrapper API or @pgsql/types change.
On such a host, loadModule()withoutwasm still fails (now while fetching the file), which is expected: pass the bytes. LOADING_WASM.md gains the Workers import example.
Testing
New load-module.test.js case (identical in all versions): a child process with WorkerGlobalScope defined, no self.location, and the glue and wrapper evaluated without a module scope (__dirname and __filename undefined; the glue's require throws). It fails on main with Cannot read properties of undefined (reading 'href') and passes with the fix.
Full suites pass locally for v18 (150/150) and v17 (79 passed, 20 skipped, 0 failed, all on node 22).
I also ran the issue's exact wrangler dev repro against the locally built v18 wasm/, and it returns the select 1 parse tree.
This PR makes loadModule usable in worker-like hosts (Cloudflare Workers with nodejs_compat) where Node-only globals such as __dirname either leak fake signals from the compat shim or are absent entirely. Each build now injects a small preamble that runs before the emscripten environment probes and overrides ENVIRONMENT_IS_NODE/ENVIRONMENT_IS_WORKER when the host lacks self.location, forcing the glue down the shell-free shell path; tests spawn a worker-like child host that loads the module with and without the .wasm file on disk.
Files
Change
templates/pre.js, versions/{13..18}/src/pre.js
Add a pre-js preamble that forces ENVIRONMENT_IS_NODE/ENVIRONMENT_IS_WORKER to false in worker-like hosts lacking self.location, replicated byte-identically across all six versions.
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
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.
Summary
Fixes #194. On Cloudflare Workers (
nodejs_compat),loadModule({ wasm })rejects before it ever uses the bytes it was given. The Emscripten glue treats any host withWorkerGlobalScopeas a worker and readsself.location.href, and any host withprocess.versions.nodeas Node and reads__dirname. workerd has neither of those values. Reproduced with libpg-query 18.2.2 + wrangler 4.157.0, and got both errors from the issue in order.The fix is a
--pre-js(templates/pre.js, copied to every version bycopy:templates). Emscripten emits it right after the environment detection:__dirnamein the glue's scope, and real browsers and workers always haveself.location, so their behavior doesn't change.libpg-query.jsis exactly the published 18.2.2 glue plus that one inserted statement. No.wasm, wrapper API or@pgsql/typeschange.loadModule()withoutwasmstill fails (now while fetching the file), which is expected: pass the bytes.LOADING_WASM.mdgains the Workers import example.Testing
load-module.test.jscase (identical in all versions): a child process withWorkerGlobalScopedefined, noself.location, and the glue and wrapper evaluated without a module scope (__dirnameand__filenameundefined; the glue'srequirethrows). It fails onmainwithCannot read properties of undefined (reading 'href')and passes with the fix.wrangler devrepro against the locally built v18wasm/, and it returns theselect 1parse tree.Link to Devin session: https://app.devin.ai/sessions/8ffe0c4157eb424882da48eb003e5165
Open in Devin Desktop: https://app.devin.ai/desktop/session/8ffe0c4157eb424882da48eb003e5165?variant=devin
Requested by: @pyramation