Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build/build.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ async function main() {
contents.push(['lib/ours/primordials.js', await readFile('src/primordials.js', 'utf-8')])
contents.push(['lib/ours/util.js', await readFile('src/util.js', 'utf-8')])
contents.push(['lib/ours/util/inspect.js', await readFile('src/util/inspect.js', 'utf-8')])
contents.push(['lib/internal/shims/process.js', await readFile('src/internal/shims/process.js', 'utf-8')])

for (const file of await readdir('src/test/ours')) {
contents.push([`test/ours/${file}`, await readFile(`src/test/ours/${file}`, 'utf-8')])
Expand Down
2 changes: 1 addition & 1 deletion build/headers.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const bufferRequire = `
const processRequire = `
/* replacement start */

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

/* replacement end */
`
Expand Down
19 changes: 19 additions & 0 deletions lib/internal/shims/process.js
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
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('../shims/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('../shims/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
Expand Up @@ -5,7 +5,7 @@

/* replacement start */

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

/* replacement end */

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('../shims/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('../shims/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
Expand Up @@ -23,7 +23,7 @@

/* replacement start */

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

/* replacement end */

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

/* replacement start */

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

/* replacement end */

Expand Down
21 changes: 21 additions & 0 deletions src/internal/shims/process.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'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
54 changes: 54 additions & 0 deletions src/test/ours/test-process-shim.js
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()
})
59 changes: 59 additions & 0 deletions test/ours/test-process-shim.js
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()
})