Skip to content

fix: unwrap ESM-style process polyfills in internal streams (fixes #539)#557

Open
smessie wants to merge 1 commit into
nodejs:mainfrom
smessie:fix/process-shim-esm-polyfill
Open

fix: unwrap ESM-style process polyfills in internal streams (fixes #539)#557
smessie wants to merge 1 commit into
nodejs:mainfrom
smessie:fix/process-shim-esm-polyfill

Conversation

@smessie

@smessie smessie commented Jul 3, 2026

Copy link
Copy Markdown

Summary

Some browser bundlers/polyfill setups expose the process/ polyfill as an ES module with a default export (i.e. { __esModule: true, default: <process> }). The internal streams code does const process = require('process/') and then uses process.nextTick(...), which throws in that scenario because the module namespace object has no nextTick.

This routes the internal process/ imports through a small shim that unwraps such ESM-style polyfills (returning the first candidate exposing nextTick) while leaving normal CommonJS polyfills untouched.

Fixes #539.
This PR follows a similar approach as #543.

Changes

  • Add lib/internal/shims/process.js (generated from src/) that unwraps an ESM-default process/ polyfill, validating candidates via 'nextTick' in candidate, and falls back to the module as-is otherwise.
  • Route the internal streams (destroy, duplexify, end-of-stream, from, pipeline, readable, writable) through the shim instead of require('process/') directly.
  • Wired through the repo's build/replacement generation so both the source and generated outputs are consistent.

Tests

  • Adds test/ours/test-process-shim.js verifying the shim resolves the real process object (with nextTick) when given an ESM-default-shaped polyfill, and returns a plain process-like object unchanged.
  • npm test, npm run lint, npm run test:format, and the browserify bundle test all pass.

Context

This fix is currently carried as a patch-package patch in Comunica to make it work in the browser; upstreaming it here removes the need for the patch. See comunica/comunica#1724.

Refs: nodejs#539

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Signed-off-by: smessie <smessie@smessie.com>
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.

Incompatibility with Rollup/Vite process polyfill

1 participant