Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 79a8cd0

Browse filesBrowse files
addaleaxBridgeAR
authored andcommitted
worker: add typechecking for postMessage transfer list
If the transfer list argument is present, it should be an array. This commit adds typechecking to that effect. This aligns behaviour with browsers. PR-URL: #28033 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent d7641d8 commit 79a8cd0
Copy full SHA for 79a8cd0

File tree

Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

2 files changed

+30
-0
lines changed
Open diff view settings
Collapse file

‎src/node_messaging.cc‎

Copy file name to clipboardExpand all lines: src/node_messaging.cc
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,15 @@ void MessagePort::PostMessage(const FunctionCallbackInfo<Value>& args) {
714714
return THROW_ERR_MISSING_ARGS(env, "Not enough arguments to "
715715
"MessagePort.postMessage");
716716
}
717+
if (!args[1]->IsNullOrUndefined() && !args[1]->IsObject()) {
718+
// Browsers ignore null or undefined, and otherwise accept an array or an
719+
// options object.
720+
// TODO(addaleax): Add support for an options object and generic sequence
721+
// support.
722+
// Refs: https://github.com/nodejs/node/pull/28033#discussion_r289964991
723+
return THROW_ERR_INVALID_ARG_TYPE(env,
724+
"Optional transferList argument must be an array");
725+
}
717726

718727
MessagePort* port = Unwrap<MessagePort>(args.This());
719728
// Even if the backing MessagePort object has already been deleted, we still
Collapse file

‎test/parallel/test-worker-message-port.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-worker-message-port.js
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,27 @@ const { MessageChannel, MessagePort } = require('worker_threads');
7070
});
7171
}
7272

73+
{
74+
const { port1, port2 } = new MessageChannel();
75+
port2.on('message', common.mustCall(4));
76+
port1.postMessage(1, null);
77+
port1.postMessage(2, undefined);
78+
port1.postMessage(3, []);
79+
port1.postMessage(4, {});
80+
81+
const err = {
82+
constructor: TypeError,
83+
code: 'ERR_INVALID_ARG_TYPE',
84+
message: 'Optional transferList argument must be an array'
85+
};
86+
87+
assert.throws(() => port1.postMessage(5, 0), err);
88+
assert.throws(() => port1.postMessage(5, false), err);
89+
assert.throws(() => port1.postMessage(5, 'X'), err);
90+
assert.throws(() => port1.postMessage(5, Symbol('X')), err);
91+
port1.close();
92+
}
93+
7394
{
7495
assert.deepStrictEqual(
7596
Object.getOwnPropertyNames(MessagePort.prototype).sort(),

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.