Skip to content

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

Open
smessie wants to merge 2 commits 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 2 commits 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>
@smessie

smessie commented Jul 13, 2026

Copy link
Copy Markdown
Author

Any chance we can have a look at this @mcollina ?

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina

Copy link
Copy Markdown
Member

ci looks red

@smessie

smessie commented Jul 14, 2026

Copy link
Copy Markdown
Author

@mcollina This is because the CI still uses unsupported Node.js versions.

Run ./node_modules/.bin/playwright install firefox
You are running Node.js 16.20.2.
Playwright requires Node.js 18 or higher. 
Please update your version of Node.js.
Error: Process completed with exit code 1.

I've pushed a commit that updates the CI to the latest LTS versions.

@smessie

smessie commented Jul 16, 2026

Copy link
Copy Markdown
Author

Does this look good to you @mcollina or do you prefer another way to solve this CI problem?

@smessie

smessie commented Jul 27, 2026

Copy link
Copy Markdown
Author

Any chance we can get this merged @mcollina ?

Comment thread .github/workflows/node.yml Outdated
node-version: 12.x
- os: macos-latest
node-version: 14.x
node-version: [22.x, 24.x, 26.x]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Unfortunately, we still needs to test 12, 14, 16, 18, 20 and 21.

@smessie
smessie force-pushed the fix/process-shim-esm-polyfill branch from 83eb7e9 to 047741a Compare July 27, 2026 08:57
playwright is declared as "^1.21.1", so a fresh CI install resolves to
1.62.0, which requires Node.js 20 or higher. The browsers workflow was
still on Node.js 16, so "playwright install" aborted before any test ran.

Only this workflow is bumped; node.yml and bundlers.yml keep testing the
older Node.js versions.
@smessie
smessie force-pushed the fix/process-shim-esm-polyfill branch from 047741a to 9379ad0 Compare July 27, 2026 09:01
@smessie

smessie commented Jul 27, 2026

Copy link
Copy Markdown
Author

The failing CI is not related to this PR, yet I pushed a commit that solves it. I dropped my previous commit such that it is testing against the old Node.js versions again.
The new commit updates the browser tests to Node.js 22 as playwright, used for these tests, no longer supports Node.js 16, so any other PR would face the same issue.

Does this suffice, @mcollina? If not, please be specific about the path you want to take. I’ll then determine if I still have the time to investigate this, or if I’ll have to abandon this effort.

@smessie

smessie commented Jul 31, 2026

Copy link
Copy Markdown
Author

Any word on this @mcollina ?

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

2 participants