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: add typechecking for postMessage transfer list #28033

Conversation

addaleax
Copy link
Member

@addaleax addaleax commented Jun 2, 2019

If the transfer list argument is present, it should be an array.
This commit adds typechecking to that effect. This aligns behaviour
with browsers.

I’m also more than happy to label this semver-major.

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • tests and/or benchmarks are included
  • commit message follows commit guidelines

If the transfer list argument is present, it should be an array.
This commit adds typechecking to that effect. This aligns behaviour
with browsers.
@addaleax addaleax added the worker Issues and PRs related to Worker support. label Jun 2, 2019
Copy link
Member

@Trott Trott left a comment

Choose a reason for hiding this comment

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

LGTM if CI is green

src/node_messaging.cc Outdated Show resolved Hide resolved
if (!args[1]->IsNullOrUndefined() && !args[1]->IsObject()) {
// Browsers also do not throw on `null` or objects, although it really
// should be an array or undefined, thus the mismatch between the checks
// above and the actual error message.
Copy link
Member

Choose a reason for hiding this comment

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

The reason for this is due to the fact that browsers have an overload that accepts an option bag in addition to just a transfer list. See https://html.spec.whatwg.org/multipage/web-messaging.html#messageport. How browsers resolve the overloading is roughly something like this

  1. If second arg is undefined or null then ignore.
  2. If second arg is not an object (i.e., also not a function), then throw.
  3. Try to get the Symbol.iterator property of the second arg.
    1. If that throws, then rethrow the exception.
    2. If that returns anything other than undefined or function, also throw an exception.
    3. If it returns a function, then treat this as an iterable and use the equivalent of Array.from to convert it to a list.
    4. Otherwise (it returns undefined), then treat the second arg as an option bag. Try to get the transferList property and then convert it to a list through something like Array.from if present.

Copy link
Member Author

Choose a reason for hiding this comment

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

@TimothyGu I’m adding a TODO comment in this PR and merge this with the current state, if that’s okay with you

Copy link
Member

@TimothyGu TimothyGu Jun 10, 2019

Choose a reason for hiding this comment

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

Yeah sure. You might encounter it as we try to pass more WPTs, so hope it'd be helpful.

@nodejs-github-bot
Copy link
Collaborator

@addaleax
Copy link
Member Author

Landed in 269c1d6

@addaleax addaleax closed this Jun 10, 2019
@addaleax addaleax deleted the worker-postmessage-transferlist-typecheck branch June 10, 2019 13:48
addaleax added a commit that referenced this pull request Jun 10, 2019
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 <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
BridgeAR pushed a commit that referenced this pull request Jun 17, 2019
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 <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: Ruben Bridgewater <[email protected]>
@BridgeAR BridgeAR mentioned this pull request Jun 17, 2019
addaleax added a commit to addaleax/node that referenced this pull request Aug 25, 2019
Allow generic iterables as transfer list arguments, as well
as an options object with a `transfer` option, for web compatibility.

Refs: nodejs#28033 (comment)
danbev pushed a commit that referenced this pull request Sep 6, 2019
Allow generic iterables as transfer list arguments, as well
as an options object with a `transfer` option, for web compatibility.

PR-URL: #29319
Refs: #28033 (comment)
Reviewed-By: James M Snell <[email protected]>
targos pushed a commit that referenced this pull request Sep 20, 2019
Allow generic iterables as transfer list arguments, as well
as an options object with a `transfer` option, for web compatibility.

PR-URL: #29319
Refs: #28033 (comment)
Reviewed-By: James M Snell <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
worker Issues and PRs related to Worker support.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants