Skip to content

Commit

Permalink
Circumvent issues with ESM process polyfills
Browse files Browse the repository at this point in the history
  • Loading branch information
WasabiThumb committed Sep 30, 2024
1 parent a2e9aed commit f1c640c
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 7 deletions.
27 changes: 27 additions & 0 deletions lib/internal/patches/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* wraps the internal process module, circumventing issues with some polyfills (see #539) */

/** @type {import('node:process')} */
const process = ((base, esmKey, keys, isValid) => {
// check if top-level es module, in which case it may have a default export
if (esmKey in base && base[esmKey] === true) {
let candidate
for (const key of keys) {
if (!(key in base)) {
continue
}
candidate = base[key]
// sanity check
if (isValid(candidate)) {
return candidate
}
}
}
return base
})(
require('process/'),
'__esModule',
['default', 'process'],
(candidate) => 'nextTick' in candidate
)

module.exports = process
2 changes: 1 addition & 1 deletion lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/duplexify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */
// Ported from https://github.com/mafintosh/end-of-stream with
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/from.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/pipeline.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */
// Ported from https://github.com/mafintosh/pump with
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */
// Copyright Joyent, Inc. and other Node contributors.
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* replacement start */

const process = require('process/')
const process = require('../patches/process')

/* replacement end */
// Copyright Joyent, Inc. and other Node contributors.
Expand Down

0 comments on commit f1c640c

Please sign in to comment.