Skip to content

Commit

Permalink
fix: no early return in loadTransportStreamBuilder (#2014)
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice authored Jul 25, 2024
1 parent 8aafa88 commit 7646914
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
3 changes: 1 addition & 2 deletions lib/transport-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = loadTransportStreamBuilder
/**
* Loads & returns a function to build transport streams
* @param {string} target
* @returns {function(object): Promise<import('node:stream').Writable>}
* @returns {Promise<function(object): Promise<import('node:stream').Writable>>}
* @throws {Error} In case the target module does not export a function
*/
async function loadTransportStreamBuilder (target) {
Expand All @@ -31,7 +31,6 @@ 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
Expand Down
26 changes: 26 additions & 0 deletions test/transport-stream.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict'

const { test } = require('tap')

test('should import', async (t) => {
t.plan(2)
const mockRealRequire = (target) => {
return {
default: {
default: () => {
t.equal(target, 'pino-pretty')
return Promise.resolve()
}
}
}
}
const mockRealImport = async () => { await Promise.resolve(); throw Object.assign(new Error(), { code: 'ERR_MODULE_NOT_FOUND' }) }

/** @type {typeof import('../lib/transport-stream.js')} */
const loadTransportStreamBuilder = t.mock('../lib/transport-stream.js', { 'real-require': { realRequire: mockRealRequire, realImport: mockRealImport } })

const fn = await loadTransportStreamBuilder('pino-pretty')

t.resolves(fn())
t.end()
})

0 comments on commit 7646914

Please sign in to comment.