diff options
author | Juan José Arboleda <soyjuanarbol@gmail.com> | 2020-03-15 02:11:03 +0300 |
---|---|---|
committer | Beth Griggs <Bethany.Griggs@uk.ibm.com> | 2020-04-07 18:28:19 +0300 |
commit | 855e9bba1771619cb0948fe9fb4465792843ee21 (patch) | |
tree | 08a1ee4eab63c685fb11691186ad184813a4f592 | |
parent | b2339c881c5093f29afbe9670fc62a3492797f28 (diff) |
worker: support MessagePort to workers data
PR-URL: https://github.com/nodejs/node/pull/32278
Fixes: https://github.com/nodejs/node/issues/32250
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r-- | doc/api/worker_threads.md | 3 | ||||
-rw-r--r-- | lib/internal/worker.js | 7 | ||||
-rw-r--r-- | test/parallel/test-worker-workerdata-messageport.js | 60 |
3 files changed, 69 insertions, 1 deletions
diff --git a/doc/api/worker_threads.md b/doc/api/worker_threads.md index 9632050b581..89caad84437 100644 --- a/doc/api/worker_threads.md +++ b/doc/api/worker_threads.md @@ -513,6 +513,9 @@ if (isMainThread) { <!-- YAML added: v10.5.0 changes: + - version: REPLACEME + pr-url: https://github.com/nodejs/node/pull/32278 + description: The `transferList` option was introduced. - version: v13.12.0 pr-url: https://github.com/nodejs/node/pull/31664 description: The `filename` parameter can be a WHATWG `URL` object using diff --git a/lib/internal/worker.js b/lib/internal/worker.js index 6a14d2dd56e..5f92a7898e9 100644 --- a/lib/internal/worker.js +++ b/lib/internal/worker.js @@ -183,6 +183,11 @@ class Worker extends EventEmitter { this[kParentSideStdio] = { stdin, stdout, stderr }; const { port1, port2 } = new MessageChannel(); + const transferList = [port2]; + // If transferList is provided. + if (options.transferList) + transferList.push(...options.transferList); + this[kPublicPort] = port1; this[kPublicPort].on('message', (message) => this.emit('message', message)); setupPortReferencing(this[kPublicPort], this, 'message'); @@ -198,7 +203,7 @@ class Worker extends EventEmitter { require('internal/process/policy').src : null, hasStdin: !!options.stdin - }, [port2]); + }, transferList); // Actually start the new thread now that everything is in place. this[kHandle].startThread(); } diff --git a/test/parallel/test-worker-workerdata-messageport.js b/test/parallel/test-worker-workerdata-messageport.js new file mode 100644 index 00000000000..352d0729412 --- /dev/null +++ b/test/parallel/test-worker-workerdata-messageport.js @@ -0,0 +1,60 @@ +'use strict'; + +require('../common'); +const assert = require('assert'); + +const { + Worker, MessageChannel +} = require('worker_threads'); + +const channel = new MessageChannel(); +const workerData = { mesage: channel.port1 }; +const transferList = [channel.port1]; +const meowScript = () => 'meow'; + +{ + // Should receive the transferList param. + new Worker(`${meowScript}`, { eval: true, workerData, transferList }); +} + +{ + // Should work with more than one MessagePort. + const channel1 = new MessageChannel(); + const channel2 = new MessageChannel(); + const workerData = { message: channel1.port1, message2: channel2.port1 }; + const transferList = [channel1.port1, channel2.port1]; + new Worker(`${meowScript}`, { eval: true, workerData, transferList }); +} + +{ + const uint8Array = new Uint8Array([ 1, 2, 3, 4 ]); + assert.deepStrictEqual(uint8Array.length, 4); + new Worker(` + const { parentPort, workerData } = require('worker_threads'); + parentPort.postMessage(workerData); + `, { + eval: true, + workerData: uint8Array, + transferList: [uint8Array.buffer] + }).on( + 'message', + (message) => + assert.deepStrictEqual(message, Uint8Array.of(1, 2, 3, 4)) + ); + assert.deepStrictEqual(uint8Array.length, 0); +} + +{ + // Should throw on non valid transferList input. + const channel1 = new MessageChannel(); + const channel2 = new MessageChannel(); + const workerData = { message: channel1.port1, message2: channel2.port1 }; + assert.throws(() => new Worker(`${meowScript}`, { + eval: true, + workerData, + transferList: [] + }), { + code: 'ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST', + message: 'MessagePort was found in message but not listed in transferList' + }); +} |