node/test/parallel/test-worker-message-port-constructor.js
Anna Henningsen 0640526303 worker: make MessagePort constructor non-callable
Refactor the C++ code for creating `MessagePort`s to skip calling the
constructor and instead directly instantiating the `InstanceTemplate`,
and always throw an error from the `MessagePort` constructor.

This aligns behaviour with the web, and creating single `MessagePort`s
does not make sense anyway.

PR-URL: https://github.com/nodejs/node/pull/28032
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
2019-06-12 20:56:44 -07:00

28 lines
692 B
JavaScript

'use strict';
require('../common');
const assert = require('assert');
const { MessageChannel, MessagePort } = require('worker_threads');
// Make sure that `MessagePort` is the constructor for MessagePort instances,
// but not callable.
const { port1 } = new MessageChannel();
assert(port1 instanceof MessagePort);
assert.strictEqual(port1.constructor, MessagePort);
assert.throws(() => MessagePort(), {
constructor: TypeError,
code: 'ERR_CONSTRUCT_CALL_INVALID'
});
assert.throws(() => new MessagePort(), {
constructor: TypeError,
code: 'ERR_CONSTRUCT_CALL_INVALID'
});
assert.throws(() => MessageChannel(), {
constructor: TypeError,
code: 'ERR_CONSTRUCT_CALL_REQUIRED'
});