From 8919169bb718a53a0c4496dcf2cd9674f8a79b0b Mon Sep 17 00:00:00 2001 From: Anna Henningsen Date: Mon, 28 Jan 2019 15:29:48 +0100 Subject: [PATCH] worker: export workerData to ESM workers Fixes: https://github.com/nodejs/node/issues/25765 PR-URL: https://github.com/nodejs/node/pull/25768 Reviewed-By: Gus Caplan Reviewed-By: Colin Ihrig Reviewed-By: Tiancheng "Timothy" Gu Reviewed-By: Jeremiah Senkpiel Reviewed-By: Luigi Pinca Reviewed-By: James M Snell --- lib/worker_threads.js | 3 ++- test/fixtures/worker-data.mjs | 3 +++ test/parallel/test-worker-mjs-workerdata.js | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 test/fixtures/worker-data.mjs create mode 100644 test/parallel/test-worker-mjs-workerdata.js diff --git a/lib/worker_threads.js b/lib/worker_threads.js index 828edb6bffc..2fe1a872464 100644 --- a/lib/worker_threads.js +++ b/lib/worker_threads.js @@ -17,5 +17,6 @@ module.exports = { MessageChannel, threadId, Worker, - parentPort: null + parentPort: null, + workerData: null, }; diff --git a/test/fixtures/worker-data.mjs b/test/fixtures/worker-data.mjs new file mode 100644 index 00000000000..1ec884aadb1 --- /dev/null +++ b/test/fixtures/worker-data.mjs @@ -0,0 +1,3 @@ +import { workerData, parentPort } from 'worker_threads'; + +parentPort.postMessage(workerData); diff --git a/test/parallel/test-worker-mjs-workerdata.js b/test/parallel/test-worker-mjs-workerdata.js new file mode 100644 index 00000000000..c107730ea4b --- /dev/null +++ b/test/parallel/test-worker-mjs-workerdata.js @@ -0,0 +1,16 @@ +'use strict'; +const common = require('../common'); +const fixtures = require('../common/fixtures'); +const assert = require('assert'); +const { Worker } = require('worker_threads'); + +const workerData = 'Hello from main thread'; + +const worker = new Worker(fixtures.path('worker-data.mjs'), { + workerData, + execArgv: ['--experimental-modules'] +}); + +worker.on('message', common.mustCall((message) => { + assert.strictEqual(message, workerData); +}));