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

lib: use non-symbols in isURLInstance check #34622

Closed

Conversation

codebytere
Copy link
Member

This slightly changes the conditional used to determine whether or not something is a URL instance. Since Node.js adds symbols to the URL not specified by the WHATWG, those symbols are not present in other implementations (like Blink's) and therefore can cause false negatives. In Electron's renderer process, running e.g. this code:

const fs = require('fs');
const path = require('path');

const url = new URL('file://' + path.resolve('index.html'));

const stream = fs.createReadStream(url);

stream.pipe(process.stdout);

would cause the following error:

"Uncaught TypeError: The "path" argument must be of type string or an instance of Buffer or URL. Received an instance of URL", source: internal/fs/utils.js (541)

To remedy this, I think we should instead switch on properties guaranteed to be present in all URL instances as specified by the WHATWG.

cc @jasnell

Checklist
  • make -j4 test (UNIX), or vcbuild test (Windows) passes
  • commit message follows commit guidelines

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@jasnell jasnell left a comment

Choose a reason for hiding this comment

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

Let's use this as part of the argument back to TC-39 that URL really ought to be added to the language ;-) ... LGTM!

@devsnek
Copy link
Member

devsnek commented Aug 4, 2020

Does this mean that in electron there are two different implementations of URL floating around?

@jasnell
Copy link
Member

jasnell commented Aug 4, 2020

Sadly, yes, but I believe the blink one is the one that's actually global. The Node.js one would still be accessible via require('url'). As I alude, what I'd really like to see is URL added to JavaScript directly as an intrinsic -- yes with a normative reference to the WHATWG spec -- and provided to environments by v8.

@codebytere
Copy link
Member Author

codebytere commented Aug 4, 2020

@devsnek heh well "floating around" is a bit nebulous, but two can exist depending on where/how you're invoking new URL. In the main process, it will always be the Node.js implementation, and in the renderer process without nodeIntegration: true enabled in webPrefs it will be Blink by default. When nodeIntegration: true is set webPrefs, it will still be Blink's by default, unless you invoke const { URL } = require('url') in which case it will then be the impl provided by Node.js.

Renderer example:

const fs = require('fs');
const path = require('path');

const url1 = new URL('file://' + path.resolve('index.html'));

const url = require('url')
const url2 = new url.URL('file://' + path.resolve('index.html'));

console.log(url1) // Blink impl
console.log(url2) // Node.js impl

will show:

nxmbFXRD

@nodejs-github-bot
Copy link
Collaborator

@nodejs-github-bot
Copy link
Collaborator

nodejs-github-bot commented Aug 5, 2020

Trott pushed a commit that referenced this pull request Aug 9, 2020
PR-URL: #34622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@Trott
Copy link
Member

Trott commented Aug 9, 2020

Landed in 8825eb4.

@Trott Trott closed this Aug 9, 2020
@codebytere codebytere deleted the better-url-instance-check branch August 9, 2020 00:20
codebytere added a commit that referenced this pull request Aug 10, 2020
PR-URL: #34622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@codebytere codebytere mentioned this pull request Aug 10, 2020
codebytere added a commit that referenced this pull request Aug 11, 2020
PR-URL: #34622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
codebytere added a commit to electron/electron that referenced this pull request Sep 1, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 1, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 2, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 4, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 14, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 14, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 14, 2020
codebytere added a commit to electron/electron that referenced this pull request Sep 16, 2020
addaleax pushed a commit that referenced this pull request Sep 22, 2020
PR-URL: #34622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
addaleax pushed a commit that referenced this pull request Sep 22, 2020
PR-URL: #34622
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
@codebytere codebytere mentioned this pull request Sep 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants