-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(transport-stream): Fix import error when using pkg with node v20
- Loading branch information
Showing
5 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |