From 85643764b1d6ef5c6178380d5304ad26a711ad28 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Wed, 12 Jun 2024 15:39:42 +0200 Subject: [PATCH] forward esm syntax errors (#1990) Signed-off-by: Matteo Collina --- lib/transport-stream.js | 7 ++++++- test/fixtures/syntax-error-esm.mjs | 2 ++ test/transport/targets.test.js | 16 ++++++++++++++++ 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/syntax-error-esm.mjs diff --git a/lib/transport-stream.js b/lib/transport-stream.js index da864a9b7..91671cc8b 100644 --- a/lib/transport-stream.js +++ b/lib/transport-stream.js @@ -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 } diff --git a/test/fixtures/syntax-error-esm.mjs b/test/fixtures/syntax-error-esm.mjs new file mode 100644 index 000000000..021d53bac --- /dev/null +++ b/test/fixtures/syntax-error-esm.mjs @@ -0,0 +1,2 @@ +// This is a syntax error +import diff --git a/test/transport/targets.test.js b/test/transport/targets.test.js index f368fc71b..7570f1594 100644 --- a/test/transport/targets.test.js +++ b/test/transport/targets.test.js @@ -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) @@ -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')) + }) +})