-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(worker): Fix import error when using pkg with node v20
- Loading branch information
Showing
5 changed files
with
102 additions
and
3 deletions.
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,37 @@ | ||
'use strict' | ||
|
||
/** | ||
* This file is packaged using pkg in order to test if worker.js works in that context | ||
*/ | ||
|
||
const { test } = require('tap') | ||
const { join } = require('path') | ||
const { file } = require('../helper') | ||
const ThreadStream = require('../..') | ||
|
||
test('bundlers support with .js file', function (t) { | ||
t.plan(1) | ||
|
||
globalThis.__bundlerPathsOverrides = { | ||
'thread-stream-worker': join(__dirname, '..', 'custom-worker.js') | ||
} | ||
|
||
const dest = file() | ||
|
||
process.on('uncaughtException', (error) => { | ||
console.log(error) | ||
}) | ||
|
||
const stream = new ThreadStream({ | ||
filename: join(__dirname, '..', 'to-file.js'), | ||
workerData: { dest }, | ||
sync: true | ||
}) | ||
|
||
stream.worker.removeAllListeners('message') | ||
stream.worker.once('message', (message) => { | ||
t.equal(message.code, 'CUSTOM-WORKER-CALLED') | ||
}) | ||
|
||
stream.end() | ||
}) |
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,15 @@ | ||
{ | ||
"pkg": { | ||
"assets": [ | ||
"../custom-worker.js", | ||
"../to-file.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() | ||
}) |