Skip to content

Commit

Permalink
Forward the correct error if there is an esm syntax error (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Jun 12, 2024
1 parent 5c39680 commit a93e517
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ 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:/pinojs/thread-stream/issues/143
worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
try {
worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', '')))
} catch {
throw error
}
} else {
throw error
}
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
"standard": {
"ignore": [
"test/ts/**/*"
"test/ts/**/*",
"test/syntax-error.mjs"
]
},
"repository": {
Expand Down
12 changes: 12 additions & 0 deletions test/base.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,15 @@ test('destroy does not error', function (t) {
t.doesNotThrow(() => stream.end())
})
})

test('syntax error', function (t) {
t.plan(1)

const stream = new ThreadStream({
filename: join(__dirname, 'syntax-error.mjs')
})

stream.on('error', (err) => {
t.equal(err.message, 'Unexpected end of input')
})
})
2 changes: 2 additions & 0 deletions test/syntax-error.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this is a syntax error
import

0 comments on commit a93e517

Please sign in to comment.