Skip to content
Closed
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
13 changes: 11 additions & 2 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,19 @@ async function start () {
// When bundled with pkg, an undefined error is thrown when called with realImport
// When bundled with pkg and using node v20, an ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING error is thrown when called with realImport
// More info at: https://github.com/pinojs/thread-stream/issues/143

try {
worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
} catch {
throw error
} catch (requireError) {
// MODULE_NOT_FOUND and ERR_REQUIRE_ESM mean the file couldn't be loaded
// via require — fall back to the original import error.
// All other errors (SyntaxError, ReferenceError, TypeError, etc.) are real
// runtime errors from the loaded module and should bubble up to the user.
// More info at: https://github.com/pinojs/thread-stream/issues/156
if (requireError.code === 'MODULE_NOT_FOUND' || requireError.code === 'ERR_REQUIRE_ESM') {
throw error
}
throw requireError
}
} else if (filename.endsWith('.ts') || filename.endsWith('.cts')) {
// Native TypeScript import failed (type stripping not enabled).
Expand Down
11 changes: 11 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@
stream.on('finish', () => {
readFile(dest, 'utf8', (err, data) => {
assert.ifError(err)
assert.strictEqual(data, 'hello world\nsomething else\n')

Check failure on line 136 in test/base.test.js

View workflow job for this annotation

GitHub Actions / Test (20, macos-latest)

over the bufferSize at startup

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'hello worlhello worlg else\n' - 'hello world\nsomething else\n' ^ at /Users/runner/work/thread-stream/thread-stream/test/base.test.js:136:14 at FSReqCallback.readFileAfterClose [as oncomplete] (node:internal/fs/read/context:68:3) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'hello worlhello worlg else\n', expected: 'hello world\nsomething else\n', operator: 'strictEqual' }
})
})

Expand Down Expand Up @@ -253,7 +253,18 @@
})

stream.on('error', (err) => {
assert.strictEqual(err.message, 'Unexpected end of input')

Check failure on line 256 in test/base.test.js

View workflow job for this annotation

GitHub Actions / Test (20, ubuntu-latest)

syntax error

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'Cannot require() ES Module /home/runner/work/thread-stream/thread-stream/test/syntax-error.mjs because it is not yet fully loaded. This may be caused by a race condition if the module is simultaneously dynamically import()-ed via Promise.all(). Try await-ing the import() sequentially in a loop instead. (from /home/runner/work/thread-stream/thread-stream/node_modules/real-require/src/index.js)\n' + + 'This is caused by either a bug in Node.js or incorrect usage of Node.js internals.\n' + + 'Please open an issue with this stack trace at https://github.com/nodejs/node/issues\n' - 'Unexpected end of input' at ThreadStream.<anonymous> (/home/runner/work/thread-stream/thread-stream/test/base.test.js:256:12) at ThreadStream.emit (node:events:524:28) at Immediate._onImmediate (/home/runner/work/thread-stream/thread-stream/index.js:463:12) at process.processImmediate (node:internal/timers:483:21) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'Cannot require() ES Module /home/runner/work/thread-stream/thread-stream/test/syntax-error.mjs because it is not yet fully loaded. This may be caused by a race condition if the module is simultaneously dynamically import()-ed via Promise.all(). Try await-ing the import() sequentially in a loop instead. (from /home/runner/work/thread-stream/thread-stream/node_modules/real-require/src/index.js)\nThis is caused by either a bug in Node.js or incorrect usage of Node.js internals.\nPlease open an issue with this stack trace at https://github.com/nodejs/node/issues\n', expected: 'Unexpected end of input', operator: 'strictEqual' }

Check failure on line 256 in test/base.test.js

View workflow job for this annotation

GitHub Actions / Test (20, macos-latest)

syntax error

AssertionError [ERR_ASSERTION]: Expected values to be strictly equal: + actual - expected + 'Cannot require() ES Module /Users/runner/work/thread-stream/thread-stream/test/syntax-error.mjs because it is not yet fully loaded. This may be caused by a race condition if the module is simultaneously dynamically import()-ed via Promise.all(). Try await-ing the import() sequentially in a loop instead. (from /Users/runner/work/thread-stream/thread-stream/node_modules/real-require/src/index.js)\n' + + 'This is caused by either a bug in Node.js or incorrect usage of Node.js internals.\n' + + 'Please open an issue with this stack trace at https://github.com/nodejs/node/issues\n' - 'Unexpected end of input' at ThreadStream.<anonymous> (/Users/runner/work/thread-stream/thread-stream/test/base.test.js:256:12) at ThreadStream.emit (node:events:524:28) at Immediate._onImmediate (/Users/runner/work/thread-stream/thread-stream/index.js:463:12) at process.processImmediate (node:internal/timers:483:21) { generatedMessage: true, code: 'ERR_ASSERTION', actual: 'Cannot require() ES Module /Users/runner/work/thread-stream/thread-stream/test/syntax-error.mjs because it is not yet fully loaded. This may be caused by a race condition if the module is simultaneously dynamically import()-ed via Promise.all(). Try await-ing the import() sequentially in a loop instead. (from /Users/runner/work/thread-stream/thread-stream/node_modules/real-require/src/index.js)\nThis is caused by either a bug in Node.js or incorrect usage of Node.js internals.\nPlease open an issue with this stack trace at https://github.com/nodejs/node/issues\n', expected: 'Unexpected end of input', operator: 'strictEqual' }
done()
})
})

test('runtime error in worker module is not hidden by require retry', function (t, done) {
const stream = new ThreadStream({
filename: join(__dirname, 'reference-error.js')
})

stream.on('error', (err) => {
assert.ok(err.message.includes('undeclaredVariable'))
done()
})
})
19 changes: 19 additions & 0 deletions test/reference-error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
'use strict'

// This module intentionally has a ReferenceError to test
// that runtime errors are not masked by the require() fallback.
// See https://github.com/pinojs/thread-stream/issues/156

undeclaredVariable.foo() // eslint-disable-line no-undef

const { Writable } = require('stream')

async function run (opts) {
return new Writable({
write (chunk, enc, cb) {
cb()
}
})
}

module.exports = run
Loading