diff --git a/src/node_messaging.cc b/src/node_messaging.cc index fa583a2570315b..b705b6ad07108c 100644 --- a/src/node_messaging.cc +++ b/src/node_messaging.cc @@ -711,6 +711,15 @@ void MessagePort::PostMessage(const FunctionCallbackInfo& args) { return THROW_ERR_MISSING_ARGS(env, "Not enough arguments to " "MessagePort.postMessage"); } + if (!args[1]->IsNullOrUndefined() && !args[1]->IsObject()) { + // Browsers ignore null or undefined, and otherwise accept an array or an + // options object. + // TODO(addaleax): Add support for an options object and generic sequence + // support. + // Refs: https://github.com/nodejs/node/pull/28033#discussion_r289964991 + return THROW_ERR_INVALID_ARG_TYPE(env, + "Optional transferList argument must be an array"); + } MessagePort* port = Unwrap(args.This()); // Even if the backing MessagePort object has already been deleted, we still diff --git a/test/parallel/test-worker-message-port.js b/test/parallel/test-worker-message-port.js index dce9da0b3063f5..d1c58216fd9d00 100644 --- a/test/parallel/test-worker-message-port.js +++ b/test/parallel/test-worker-message-port.js @@ -70,6 +70,27 @@ const { MessageChannel, MessagePort } = require('worker_threads'); }); } +{ + const { port1, port2 } = new MessageChannel(); + port2.on('message', common.mustCall(4)); + port1.postMessage(1, null); + port1.postMessage(2, undefined); + port1.postMessage(3, []); + port1.postMessage(4, {}); + + const err = { + constructor: TypeError, + code: 'ERR_INVALID_ARG_TYPE', + message: 'Optional transferList argument must be an array' + }; + + assert.throws(() => port1.postMessage(5, 0), err); + assert.throws(() => port1.postMessage(5, false), err); + assert.throws(() => port1.postMessage(5, 'X'), err); + assert.throws(() => port1.postMessage(5, Symbol('X')), err); + port1.close(); +} + { assert.deepStrictEqual( Object.getOwnPropertyNames(MessagePort.prototype).sort(),