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