fix: unwrap ESM-style process polyfills in internal streams (fixes #539)#557
Open
smessie wants to merge 1 commit into
Open
fix: unwrap ESM-style process polyfills in internal streams (fixes #539)#557smessie wants to merge 1 commit into
smessie wants to merge 1 commit into
Conversation
Refs: nodejs#539 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: smessie <smessie@smessie.com>
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.
Summary
Some browser bundlers/polyfill setups expose the
process/polyfill as an ES module with adefaultexport (i.e.{ __esModule: true, default: <process> }). The internal streams code doesconst process = require('process/')and then usesprocess.nextTick(...), which throws in that scenario because the module namespace object has nonextTick.This routes the internal
process/imports through a small shim that unwraps such ESM-style polyfills (returning the first candidate exposingnextTick) while leaving normal CommonJS polyfills untouched.Fixes #539.
This PR follows a similar approach as #543.
Changes
lib/internal/shims/process.js(generated fromsrc/) that unwraps an ESM-defaultprocess/polyfill, validating candidates via'nextTick' in candidate, and falls back to the module as-is otherwise.destroy,duplexify,end-of-stream,from,pipeline,readable,writable) through the shim instead ofrequire('process/')directly.Tests
test/ours/test-process-shim.jsverifying the shim resolves the real process object (withnextTick) 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-packagepatch in Comunica to make it work in the browser; upstreaming it here removes the need for the patch. See comunica/comunica#1724.