Skip to content

Commit

Permalink
Merge pull request #510 from perrin4869/chore/nodejs/test-worker-threads
Browse files Browse the repository at this point in the history
Node.JS bindings Worker threads segfault reproduction
  • Loading branch information
tdewolff committed Jul 10, 2022
2 parents 40b82ac + a01c7d6 commit 2eed2f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion bindings/js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
],
"scripts": {
"install": "node-gyp-build",
"test": "node test.js",
"test": "node test/index.js && node test/worker.js",
"prebuildify": "prebuildify --napi --strip"
},
"homepage": "https://github.com/tdewolff/minify#readme",
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions bindings/js/test/worker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Worker, isMainThread, workerData, parentPort } from 'node:worker_threads';

if (isMainThread) {
let input = "<html><span style=\"color:#ff0000;\">A phrase</span>";
const worker = new Worker(new URL(import.meta.url), {
workerData: input,
});

let expected = "<html><span style=color:red>A phrase</span>";
let output = await new Promise((resolve, reject) => {
worker.on('message', resolve);
worker.on('error', reject);
worker.on('exit', (code) => {
if (code !== 0)
reject(new Error(`Worker stopped with exit code ${code}`));
});
})
if (output != expected) {
throw "unexpected output using worker threads: '"+output+"' instead of '"+expected+"'";
}
await worker.terminate();
} else {
const { config, string } = await import('@tdewolff/minify');
config({'html-keep-document-tags': true})
parentPort.postMessage(string("text/html", workerData));
}

0 comments on commit 2eed2f6

Please sign in to comment.