Skip to content

fix(wasm): loadModule({ wasm }) works on Cloudflare Workers - #195

Merged
pyramation merged 1 commit into
mainfrom
fix/workers-glue-env
Nov 12, 2026
Merged

pyramation merged 1 commit into
mainfrom
fix/workers-glue-env

Conversation

@pyramation

Copy link
Copy Markdown
Collaborator

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 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:

if (ENVIRONMENT_IS_NODE && typeof __dirname == 'undefined') ENVIRONMENT_IS_NODE = false;
if (ENVIRONMENT_IS_WORKER && !globalThis.self?.location) ENVIRONMENT_IS_WORKER = false;
  • 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() without wasm 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.

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

@devin-ai-integration

Copy link
Copy Markdown

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@luxor-code-review

luxor-code-review Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Review complete. No issues found — approved ✅.


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.
templates/Makefile.template, versions/{13..18}/Makefile Wire src/pre.js into the emscripten build (dependency + --pre-js flag) so rebuilds pick up preamble changes.
scripts/copy-templates.js Copy the new pre.js preamble into each version tree during template replication.
versions/{13..18}/test/load-module.test.js Add a spawned worker-like-host test that exercises loadModule with and without the .wasm file present.
LOADING_WASM.md Document the Cloudflare Workers load path (import wasm from ..., loadModule({ wasm })) and the nodejs_compat requirement.

No runtime library code (index.cjs/index.ts) is modified; the behavior change is confined to the emscripten glue preamble, tests, and docs.

Reviewed commit: 8f35ea8

@pyramation
pyramation merged commit 2cd86da into main Nov 12, 2026
59 checks passed
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.

loadModule({ wasm }) fails on Cloudflare Workers: the glue reads self.location.href and __dirname

1 participant