From cb35ce3980937d595313fbcc1abc096a16bc45a9 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Thu, 24 Jul 2025 21:02:00 +0300 Subject: [PATCH 1/3] Fix the module import workaround for Windows Suprisingly we had pkg code actually hit the ERR_MODULE_NOT_FOUND on Node.js 22, but the workaround for Windows was missing there... BTW it might be better to use `require('url').fileURLToPath()` for this? --- lib/worker.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/worker.js b/lib/worker.js index d050b4b..2b25fad 100644 --- a/lib/worker.js +++ b/lib/worker.js @@ -34,7 +34,7 @@ async function start () { // The error codes may change based on the node.js version (ENOTDIR > 12, ERR_MODULE_NOT_FOUND <= 12 ) if ((error.code === 'ENOTDIR' || error.code === 'ERR_MODULE_NOT_FOUND') && filename.startsWith('file://')) { - worker = realRequire(decodeURIComponent(filename.replace('file://', ''))) + worker = realRequire(decodeURIComponent(filename.replace(process.platform === 'win32' ? 'file:///' : 'file://', ''))) } 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 From d1adeba5edada2d4b13ab7d1bac764d266e0d9fd Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Fri, 25 Jul 2025 14:45:18 +0300 Subject: [PATCH 2/3] Fix pkg tests due to latest pkg not working properly with Node <=16 --- test/pkg/.gitignore | 1 + test/pkg/pkg.config.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) create mode 100644 test/pkg/.gitignore diff --git a/test/pkg/.gitignore b/test/pkg/.gitignore new file mode 100644 index 0000000..2b70cf7 --- /dev/null +++ b/test/pkg/.gitignore @@ -0,0 +1 @@ +/index-* diff --git a/test/pkg/pkg.config.json b/test/pkg/pkg.config.json index 90d73b6..fb93798 100644 --- a/test/pkg/pkg.config.json +++ b/test/pkg/pkg.config.json @@ -11,4 +11,4 @@ ], "outputPath": "test/pkg" } -} \ No newline at end of file +} From a0ce8b0f4a4d861fd0e5d07644aff1dc992d5e77 Mon Sep 17 00:00:00 2001 From: Segev Finer Date: Tue, 2 Dec 2025 14:57:41 +0200 Subject: [PATCH 3/3] Try to add a test --- test/pkg/index.js | 40 +++++++++++++++++++++++++++++++--------- test/pkg/pkg.config.json | 3 ++- test/to-file-worker.js | 14 ++++++++++++++ 3 files changed, 47 insertions(+), 10 deletions(-) create mode 100644 test/to-file-worker.js diff --git a/test/pkg/index.js b/test/pkg/index.js index 8d7fd47..1cfcddb 100644 --- a/test/pkg/index.js +++ b/test/pkg/index.js @@ -21,17 +21,39 @@ process.on('uncaughtException', (error) => { process.exit(1) }) -const stream = new ThreadStream({ - filename: join(__dirname, '..', 'to-file.js'), - workerData: { dest }, - sync: true +const stream1Promise = new Promise((resolve) => { + const stream = new ThreadStream({ + filename: join(__dirname, '..', 'to-file.js'), + workerData: { dest }, + sync: true + }) + + stream.worker.removeAllListeners('message') + stream.worker.once('message', (message) => { + assert.strictEqual(message.code, 'CUSTOM-WORKER-CALLED') + resolve() + }) + + stream.end() }) -stream.worker.removeAllListeners('message') -stream.worker.once('message', (message) => { - assert.strictEqual(message.code, 'CUSTOM-WORKER-CALLED') +const stream2Promise = new Promise((resolve) => { + const stream = new ThreadStream({ + filename: join(__dirname, '..', 'to-file-worker.js'), + workerData: { dest }, + sync: true + }) + + stream.worker.removeAllListeners('message') + stream.worker.once('message', (message) => { + assert.strictEqual(message.code, 'CUSTOM-WORKER-CALLED') + resolve() + }) + + stream.end() +}) + +Promise.all([stream1Promise, stream2Promise]).then(() => { console.log('pkg test passed') process.exit(0) }) - -stream.end() diff --git a/test/pkg/pkg.config.json b/test/pkg/pkg.config.json index fb93798..065140b 100644 --- a/test/pkg/pkg.config.json +++ b/test/pkg/pkg.config.json @@ -2,7 +2,8 @@ "pkg": { "assets": [ "../custom-worker.js", - "../to-file.js" + "../to-file.js", + "../to-file-worker.js" ], "targets": [ "node20", diff --git a/test/to-file-worker.js b/test/to-file-worker.js new file mode 100644 index 0000000..c0efd6b --- /dev/null +++ b/test/to-file-worker.js @@ -0,0 +1,14 @@ +'use strict' + +const { join } = require('path') +const ThreadStream = require('../..') + +async function run (opts) { + return new ThreadStream({ + filename: join(__dirname, '..', 'to-file.js'), + workerData: opts, + sync: true + }) +} + +module.exports = run