mirror of
https://github.com/nodejs/node.git
synced 2025-05-17 22:02:33 +00:00

Currently, transferring the port on which postMessage is called causes a segmentation fault, and transferring the target port causes a subsequent port.onmessage setting to throw, or a deadlock if onmessage is set before the postMessage. Fix both of these behaviors and align the methods more closely with the normative definitions in the HTML Standard. Also, per spec postMessage must not throw just because the ports are disentangled. Implement that behavior. PR-URL: https://github.com/nodejs/node/pull/21540 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
34 lines
999 B
JavaScript
34 lines
999 B
JavaScript
// Flags: --experimental-worker
|
|
'use strict';
|
|
|
|
const common = require('../common');
|
|
const assert = require('assert');
|
|
const { MessageChannel } = require('worker_threads');
|
|
|
|
const { port1, port2 } = new MessageChannel();
|
|
|
|
assert.throws(common.mustCall(() => {
|
|
port1.postMessage(null, [port1]);
|
|
}), common.mustCall((err) => {
|
|
assert.strictEqual(err.name, 'DataCloneError');
|
|
assert.strictEqual(err.message, 'Transfer list contains source port');
|
|
assert.strictEqual(err.code, 25);
|
|
assert.ok(err instanceof Error);
|
|
|
|
const DOMException = err.constructor;
|
|
assert.ok(err instanceof DOMException);
|
|
assert.strictEqual(DOMException.name, 'DOMException');
|
|
|
|
return true;
|
|
}));
|
|
|
|
// The failed transfer should not affect the ports in anyway.
|
|
port2.onmessage = common.mustCall((message) => {
|
|
assert.strictEqual(message, 2);
|
|
port1.close();
|
|
|
|
setTimeout(common.mustNotCall('The communication channel is still open'),
|
|
common.platformTimeout(1000)).unref();
|
|
});
|
|
port1.postMessage(2);
|