Skip to content

Commit

Permalink
forward esm syntax errors (#1990)
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Jun 12, 2024
1 parent 52bf0aa commit 8564376
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/transport-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,16 @@ async function loadTransportStreamBuilder (target) {
// See this PR for details: https://github.com/pinojs/thread-stream/pull/34
if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND')) {
fn = realRequire(target)
return
} else if (error.code === undefined || error.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') {
// 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
fn = realRequire(decodeURIComponent(target))
try {
fn = realRequire(decodeURIComponent(target))
} catch {
throw error
}
} else {
throw error
}
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/syntax-error-esm.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This is a syntax error
import
16 changes: 16 additions & 0 deletions test/transport/targets.test.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
'use strict'

const { test } = require('tap')
const { join } = require('path')
const proxyquire = require('proxyquire')
const Writable = require('stream').Writable
const pino = require('../../pino')

test('file-target mocked', async function ({ equal, same, plan, pass }) {
plan(1)
Expand All @@ -26,3 +28,17 @@ test('file-target mocked', async function ({ equal, same, plan, pass }) {

await fileTarget()
})

test('pino.transport with syntax error', ({ same, teardown, plan }) => {
plan(1)
const transport = pino.transport({
targets: [{
target: join(__dirname, '..', 'fixtures', 'syntax-error-esm.mjs')
}]
})
teardown(transport.end.bind(transport))

transport.on('error', (err) => {
same(err, new SyntaxError('Unexpected end of input'))
})
})

0 comments on commit 8564376

Please sign in to comment.