Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

forward esm syntax errors #1990

Merged
merged 1 commit into from
Jun 12, 2024
Merged
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
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'))
})
})