1'use strict'; 2const common = require('../common'); 3const assert = require('assert'); 4const { MessageChannel, MessagePort, Worker } = require('worker_threads'); 5 6{ 7 const channel = new MessageChannel(); 8 9 channel.port1.on('message', common.mustCall(({ typedArray }) => { 10 assert.deepStrictEqual(typedArray, new Uint8Array([0, 1, 2, 3, 4])); 11 })); 12 13 const typedArray = new Uint8Array([0, 1, 2, 3, 4]); 14 channel.port2.postMessage({ typedArray }, [ typedArray.buffer ]); 15 assert.strictEqual(typedArray.buffer.byteLength, 0); 16 channel.port2.close(); 17} 18 19{ 20 const channel = new MessageChannel(); 21 22 channel.port1.on('close', common.mustCall()); 23 channel.port2.on('close', common.mustCall()); 24 channel.port2.close(); 25} 26 27{ 28 const channel = new MessageChannel(); 29 30 const w = new Worker(` 31 const { MessagePort } = require('worker_threads'); 32 const assert = require('assert'); 33 require('worker_threads').parentPort.on('message', ({ port }) => { 34 assert(port instanceof MessagePort); 35 port.postMessage('works'); 36 }); 37 `, { eval: true }); 38 w.postMessage({ port: channel.port2 }, [ channel.port2 ]); 39 assert(channel.port1 instanceof MessagePort); 40 assert(channel.port2 instanceof MessagePort); 41 channel.port1.on('message', common.mustCall((message) => { 42 assert.strictEqual(message, 'works'); 43 w.terminate(); 44 })); 45} 46