Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

worker: fix type check in receiveMessageOnPort #32745

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/node_messaging.cc
Expand Up @@ -865,7 +865,12 @@ void MessagePort::Drain(const FunctionCallbackInfo<Value>& args) {
}

void MessagePort::ReceiveMessage(const FunctionCallbackInfo<Value>& args) {
CHECK(args[0]->IsObject());
Environment* env = Environment::GetCurrent(args);
if (!args[0]->IsObject() ||
!env->message_port_constructor_template()->HasInstance(args[0])) {
return THROW_ERR_INVALID_ARG_TYPE(env,
"First argument needs to be a MessagePort instance");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the doc, maybe change to:

Suggested change
"First argument needs to be a MessagePort instance");
'The "port" argument must be a MessagePort instance');

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ZYSzys Do you maybe want to open a PR after this lands that makes this consistent across both instances?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aha, I missed that there is still somewhere else need to be consistent.

Make sense to do it entirely after this lands.

}
MessagePort* port = Unwrap<MessagePort>(args[0].As<Object>());
if (port == nullptr) {
// Return 'no messages' for a closed port.
Expand Down
8 changes: 8 additions & 0 deletions test/parallel/test-worker-message-port-receive-message.js
Expand Up @@ -23,3 +23,11 @@ port2.on('message', common.mustNotCall());
port1.postMessage(message1);
assert.deepStrictEqual(receiveMessageOnPort(port2), { message: message1 });
port1.close();

for (const value of [null, 0, -1, {}, []]) {
assert.throws(() => receiveMessageOnPort(value), {
name: 'TypeError',
code: 'ERR_INVALID_ARG_TYPE',
message: 'First argument needs to be a MessagePort instance'
});
}