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

module: use isURLInstance instead of instanceof #34951

Closed
wants to merge 1 commit into from

Conversation

aduh95
Copy link
Contributor

@aduh95 aduh95 commented Aug 27, 2020

Related to #34622, in case there are several WHATWG URL implementations available, isURLInstance would work better than instanceof URL. Might also be more efficient, but I haven't run any benchmarks :)

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

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/modules

@ljharb
Copy link
Member

ljharb commented Aug 27, 2020

this is a good change - instanceof should be avoided - but perhaps this should have a test using the vm module?

@aduh95
Copy link
Contributor Author

aduh95 commented Aug 28, 2020

perhaps this should have a test using the vm module?

@ljharb can you elaborate on what you have in mind? URL is not a primordial object, it's not available to VM Script instances:

$ node -p "require('vm').runInContext('URL', require('vm').createContext({})) === URL"
evalmachine.<anonymous>:1
URL
^

ReferenceError: URL is not defined
    at evalmachine.<anonymous>:1:1
    at Script.runInContext (vm.js:141:18)
    at Object.runInContext (vm.js:279:6)
    at [eval]:1:15
    at Script.runInThisContext (vm.js:131:18)
    at Object.runInThisContext (vm.js:295:38)
    at Object.<anonymous> ([eval]-wrapper:10:26)
    at Module._compile (internal/modules/cjs/loader.js:1200:30)
    at evalScript (internal/process/execution.js:98:25)
    at internal/main/eval_string.js:23:3
$ node -p "require('vm').runInContext('URL', require('vm').createContext({ URL })) === URL"
true

We'd need a whole new implementation of URL (in case of Electron, Blink provides one, Node.js another one) to test this behaviour.

@ljharb
Copy link
Member

ljharb commented Aug 28, 2020

ahhh, hm. what's the implementation of isURLInstance doing that would be robust to that scenario?

@aduh95
Copy link
Contributor Author

aduh95 commented Aug 28, 2020

ahhh, hm. what's the implementation of isURLInstance doing that would be robust to that scenario?

More info on that on #34622, I have actually very little knowledge myself on that topic 🙈

@ljharb
Copy link
Member

ljharb commented Aug 28, 2020

Looking at that implementation, that's not "is URL instance", that's "has truthy href and origin properties", which is more like "is URL-like".

Is the intention to allow { href: 1, origin: 2 } as a valid URL-like thing?

@aduh95
Copy link
Contributor Author

aduh95 commented Aug 28, 2020

@ljharb I'd say it's probably OK, using such an object would fail anyway when converted to string. Hopefully the isURLInstance will be updated to get a finer test if the current implementation creates issues.

@aduh95
Copy link
Contributor Author

aduh95 commented Sep 7, 2020

I think this should be labeled Author ready if no one objects.

@DerekNonGeneric DerekNonGeneric added author ready PRs that have at least one approval, no pending requests for changes, and a CI started. request-ci Add this label to start a Jenkins CI on a PR. labels Sep 11, 2020
@DerekNonGeneric
Copy link
Contributor

In case anyone is curious as to what isURLInstance does…

node/lib/internal/url.js

Lines 1420 to 1422 in 22c52aa

function isURLInstance(fileURLOrPath) {
return fileURLOrPath != null && fileURLOrPath.href && fileURLOrPath.origin;
}

@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Sep 11, 2020
@nodejs-github-bot
Copy link
Collaborator

@DerekNonGeneric DerekNonGeneric added module Issues and PRs related to the module subsystem. commit-queue Add this label to land a pull request using GitHub Actions. labels Sep 11, 2020
@github-actions github-actions bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Sep 15, 2020
@github-actions
Copy link
Contributor

Landed in d24eecd

@github-actions github-actions bot closed this Sep 15, 2020
nodejs-github-bot pushed a commit that referenced this pull request Sep 15, 2020
PR-URL: #34951
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Derek Lewis <[email protected]>
ruyadorno pushed a commit that referenced this pull request Sep 21, 2020
PR-URL: #34951
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Derek Lewis <[email protected]>
@ruyadorno ruyadorno mentioned this pull request Sep 21, 2020
4 tasks
guybedford pushed a commit to guybedford/node that referenced this pull request Sep 28, 2020
PR-URL: nodejs#34951
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Derek Lewis <[email protected]>
codebytere pushed a commit that referenced this pull request Oct 1, 2020
PR-URL: #34951
Backport-PR-URL: #35385
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Derek Lewis <[email protected]>
@codebytere codebytere mentioned this pull request Oct 4, 2020
joesepi pushed a commit to joesepi/node that referenced this pull request Jan 8, 2021
PR-URL: nodejs#34951
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Derek Lewis <[email protected]>
@aduh95 aduh95 deleted the use-isurlinstance-createrequire branch July 16, 2022 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
author ready PRs that have at least one approval, no pending requests for changes, and a CI started. module Issues and PRs related to the module subsystem.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants