-
-
Notifications
You must be signed in to change notification settings - Fork 243
fix: unwrap ESM-style process polyfills in internal streams (fixes #539) #557
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
smessie
wants to merge
3
commits into
nodejs:main
Choose a base branch
from
smessie:fix/process-shim-esm-polyfill
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| 'use strict' | ||
|
|
||
| /* wraps the internal process module, circumventing issues with some polyfills (see #539) */ | ||
| const process = ((base, esmKey, keys, isValid) => { | ||
| if (esmKey in base && base[esmKey] === true) { | ||
| let candidate | ||
| for (const key of keys) { | ||
| if (!(key in base)) { | ||
| continue | ||
| } | ||
| candidate = base[key] | ||
| if (isValid(candidate)) { | ||
| return candidate | ||
| } | ||
| } | ||
| } | ||
| return base | ||
| })(require('process/'), '__esModule', ['default', 'process'], (candidate) => 'nextTick' in candidate) | ||
| module.exports = process | ||
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
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
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| 'use strict' | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Duplicate code is duplicate |
||
|
|
||
| /* wraps the internal process module, circumventing issues with some polyfills (see #539) */ | ||
|
|
||
| const process = ((base, esmKey, keys, isValid) => { | ||
| if (esmKey in base && base[esmKey] === true) { | ||
| let candidate | ||
| for (const key of keys) { | ||
| if (!(key in base)) { | ||
| continue | ||
| } | ||
| candidate = base[key] | ||
| if (isValid(candidate)) { | ||
| return candidate | ||
| } | ||
| } | ||
| } | ||
| return base | ||
| })(require('process/'), '__esModule', ['default', 'process'], (candidate) => 'nextTick' in candidate) | ||
|
|
||
| module.exports = process | ||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| 'use strict' | ||
|
|
||
| const t = require('tap') | ||
| const shimPath = '../../lib/internal/shims/process' | ||
|
|
||
| t.test('unwraps an ES module default export containing the process object', (t) => { | ||
| const processLike = { nextTick() {} } | ||
| const wrappedProcess = { __esModule: true, default: processLike } | ||
|
|
||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
|
|
||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
|
|
||
| t.test('unwraps an ES module process export when default is not process-like', (t) => { | ||
| const processLike = { nextTick() {} } | ||
| const wrappedProcess = { __esModule: true, default: {}, process: processLike } | ||
|
|
||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
|
|
||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
|
|
||
| t.test('returns a plain process-like object unchanged', (t) => { | ||
| const processLike = { nextTick() {} } | ||
|
|
||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': processLike | ||
| }) | ||
|
|
||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
|
|
||
| t.test('returns an ES module wrapper unchanged when no candidate is process-like', (t) => { | ||
| const wrappedProcess = { __esModule: true, default: {}, process: {} } | ||
|
|
||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
|
|
||
| t.equal(shimmedProcess, wrappedProcess) | ||
| t.notOk('nextTick' in shimmedProcess) | ||
| t.end() | ||
| }) |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| 'use strict' | ||
|
|
||
| const t = require('tap') | ||
| const shimPath = '../../lib/internal/shims/process' | ||
| t.test('unwraps an ES module default export containing the process object', (t) => { | ||
| const processLike = { | ||
| nextTick() {} | ||
| } | ||
| const wrappedProcess = { | ||
| __esModule: true, | ||
| default: processLike | ||
| } | ||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
| t.test('unwraps an ES module process export when default is not process-like', (t) => { | ||
| const processLike = { | ||
| nextTick() {} | ||
| } | ||
| const wrappedProcess = { | ||
| __esModule: true, | ||
| default: {}, | ||
| process: processLike | ||
| } | ||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
| t.test('returns a plain process-like object unchanged', (t) => { | ||
| const processLike = { | ||
| nextTick() {} | ||
| } | ||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': processLike | ||
| }) | ||
| t.equal(shimmedProcess, processLike) | ||
| t.equal(typeof shimmedProcess.nextTick, 'function') | ||
| t.end() | ||
| }) | ||
| t.test('returns an ES module wrapper unchanged when no candidate is process-like', (t) => { | ||
| const wrappedProcess = { | ||
| __esModule: true, | ||
| default: {}, | ||
| process: {} | ||
| } | ||
| const shimmedProcess = t.mock(shimPath, { | ||
| 'process/': wrappedProcess | ||
| }) | ||
| t.equal(shimmedProcess, wrappedProcess) | ||
| t.notOk('nextTick' in shimmedProcess) | ||
| t.end() | ||
| }) |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this version of the code not less readable than the original? Why choose to strip comments and increase line length in this way?