Skip to content

Commit

Permalink
fix(transport-stream): Fix import error when using pkg with node v20
Browse files Browse the repository at this point in the history
  • Loading branch information
nagyszabi committed Apr 24, 2024
1 parent 4f8ea32 commit c2f12ac
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/transport-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ 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)
} else if (error.code === undefined) {
} 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))
} else {
throw error
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
"@types/flush-write-stream": "^1.0.0",
"@types/node": "^20.2.3",
"@types/tap": "^15.0.6",
"@yao-pkg/pkg": "^5.11.5",
"airtap": "4.0.4",
"benchmark": "^2.1.4",
"bole": "^5.0.5",
Expand Down
46 changes: 46 additions & 0 deletions test/pkg/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'use strict'

const os = require('os')
const { join } = require('path')
const { readFile } = require('fs').promises
const { watchFileCreated, file } = require('../helper')
const { test } = require('tap')
const pino = require('../../pino')

const { pid } = process
const hostname = os.hostname()

/**
* This file is packaged using pkg in order to test if transport-stream.js works in that context
*/

test('pino.transport with worker destination overridden by bundler and mjs transport', async ({ same, teardown }) => {
globalThis.__bundlerPathsOverrides = {
'pino-worker': join(__dirname, '..', '..', 'lib/worker.js')
}

const destination = file()
const transport = pino.transport({
targets: [
{
target: join(__dirname, '..', 'fixtures', 'ts', 'to-file-transport.es2017.cjs'),
options: { destination }
}
]
})

teardown(transport.end.bind(transport))
const instance = pino(transport)
instance.info('hello')
await watchFileCreated(destination)
const result = JSON.parse(await readFile(destination))
delete result.time
same(result, {
pid,
hostname,
level: 30,
msg: 'hello'
})

globalThis.__bundlerPathsOverrides = undefined
})
17 changes: 17 additions & 0 deletions test/pkg/pkg.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"pkg": {
"assets": [
"../../lib/worker.js",
"../../lib/transport-stream.js",
"../../test/fixtures/ts/to-file-transport.es2017.cjs",
"../../node_modules/pino-abstract-transport/index.js"
],
"targets": [
"node14",
"node16",
"node18",
"node20"
],
"outputPath": "test/pkg"
}
}
44 changes: 44 additions & 0 deletions test/pkg/pkg.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const { test } = require('tap')
const config = require('./pkg.config.json')
const { promisify } = require('util')
const { unlink } = require('fs/promises')
const { join } = require('path')
const { platform } = require('process')
const exec = promisify(require('child_process').exec)

test('worker test when packaged into executable using pkg', async (t) => {
const packageName = 'index'

// package the app into several node versions, check config for more info
const filePath = `${join(__dirname, packageName)}.js`
const configPath = join(__dirname, 'pkg.config.json')
const { stderr } = await exec(`npx pkg ${filePath} --config ${configPath}`)

// there should be no error when packaging
t.equal(stderr, '')

// pkg outputs files in the following format by default: {filename}-{node version}
for (const target of config.pkg.targets) {
// execute the packaged test
let executablePath = `${join(config.pkg.outputPath, packageName)}-${target}`

// when on windows, we need the .exe extension
if (platform === 'win32') {
executablePath = `${executablePath}.exe`
} else {
executablePath = `./${executablePath}`
}

const { stderr } = await exec(executablePath)

// check if there were no errors
t.equal(stderr, '')

// clean up afterwards
await unlink(executablePath)
}

t.end()
})

0 comments on commit c2f12ac

Please sign in to comment.