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

url: improve isURLThis detection #46866

Merged
merged 1 commit into from
Mar 1, 2023

Conversation

anonrig
Copy link
Member

@anonrig anonrig commented Feb 27, 2023

Resolve the TODO and updates the implementation to avoid prototype look.

cc @TimothyGu

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/url

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation. labels Feb 27, 2023
@anonrig anonrig requested a review from TimothyGu February 27, 2023 13:37
@lemire
Copy link
Member

lemire commented Feb 27, 2023

@anonrig Is that faster?

@anonrig
Copy link
Member Author

anonrig commented Feb 27, 2023

@anonrig Is that faster?

I don't think so. This was a recommendation by @TimothyGu on the original Ada migration pull request. I'm following up with the TODOs

@anonrig anonrig added the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Feb 27, 2023
@nodejs-github-bot
Copy link
Collaborator

@anonrig anonrig added the commit-queue Add this label to land a pull request using GitHub Actions. label Feb 27, 2023
@joyeecheung
Copy link
Member

joyeecheung commented Feb 28, 2023

I wonder if we are just going to do a brand check, why can't we just make context a private field and let V8 perform the checks which is likely to be more performant...(that might be a breaking change, though I doubt if there's anyone relying on the error code being ERR_INVALID_THIS, the spec only says it wants a TypeError in those cases)

@anonrig
Copy link
Member Author

anonrig commented Feb 28, 2023

I wonder if we are just going to do a brand check, why can't we just make context a private field and let V8 perform the checks which is likely to be more performant...(that might be a breaking change, though I doubt if there's anyone relying on the error code being ERR_INVALID_THIS, the spec only says it wants a TypeError in those cases)

I didn't follow. Can you share an example?

@aduh95
Copy link
Contributor

aduh95 commented Feb 28, 2023

@anonrig

class URL {
  #context;

  get href() {
    this.#context;
  }
}

Object.getOwnPropertyDescriptor(URL.prototype, 'href').get.call('not a URL object'); // TypeError

@anonrig
Copy link
Member Author

anonrig commented Mar 1, 2023

I'll follow up on a different pull request since it might be a breaking change.

@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 1, 2023
@nodejs-github-bot nodejs-github-bot merged commit 37c736f into nodejs:main Mar 1, 2023
@nodejs-github-bot
Copy link
Collaborator

Landed in 37c736f

@anonrig anonrig deleted the url-is-url-this branch March 1, 2023 13:47
targos pushed a commit that referenced this pull request Mar 13, 2023
PR-URL: #46866
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
targos pushed a commit that referenced this pull request Mar 14, 2023
PR-URL: #46866
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
danielleadams pushed a commit that referenced this pull request Apr 11, 2023
PR-URL: #46866
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Tierney Cyren <hello@bnb.im>
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
@victordidenko
Copy link

Oh.

In the original comment from @TimothyGu there was

e.g., Object.create(new URL(…)) should not be considered a URL

Can you please elaborate, why that so?
I think this is quite breaking change, that I cannot no more put URL as prototype... Actually this become unusable at all.

> Object.create(new URL('http://test.com'))
Uncaught TypeError [ERR_INVALID_THIS]: Value of "this" must be of type URL
    at __node_internal_captureLargerStackTrace (node:internal/errors:490:5)
    at new NodeError (node:internal/errors:399:5)
    at get href [as href] (node:internal/url:624:13)
    at [nodejs.util.inspect.custom] (node:internal/url:576:21)
    at formatValue (node:internal/util/inspect:806:19)
    at inspect (node:internal/util/inspect:365:10)
    at REPLServer.writer (node:repl:224:25)
    at finish (node:repl:945:32)
    at finishExecution (node:repl:550:7)
    at REPLServer.defaultEval (node:repl:639:7)
    at bound (node:domain:433:15)
    at REPLServer.runBound [as eval] (node:domain:444:12)
    at REPLServer.onLine (node:repl:902:10)
    at REPLServer.emit (node:events:525:35)
    at REPLServer.emit (node:domain:489:12)
    at [_onLine] [as _onLine] (node:internal/readline/interface:422:12)
    at [_line] [as _line] (node:internal/readline/interface:893:18)
    at [_ttyWrite] [as _ttyWrite] (node:internal/readline/interface:1271:22)
    at REPLServer.self._ttyWrite (node:repl:997:9)
    at ReadStream.onkeypress (node:internal/readline/interface:270:20)
    at ReadStream.emit (node:events:513:28)
    at ReadStream.emit (node:domain:489:12)
    at emitKeys (node:internal/readline/utils:357:14)
    at emitKeys.next (<anonymous>)
    at ReadStream.onData (node:internal/readline/emitKeypressEvents:64:36)
    at ReadStream.emit (node:events:513:28)
    at ReadStream.emit (node:domain:489:12)
    at addChunk (node:internal/streams/readable:324:12)
    at readableAddChunk (node:internal/streams/readable:297:9)
    at Readable.push (node:internal/streams/readable:234:10)
    at TTY.onStreamRead (node:internal/stream_base_commons:190:23)
    at TTY.callbackTrampoline (node:internal/async_hooks:130:17) {
  code: 'ERR_INVALID_THIS'

yumauri added a commit to yumauri/effector-storage that referenced this pull request Apr 22, 2023
In nodejs 18.16.0 become impossible to do a trick `Object.create(new URL(...))`,
so rewrote all mocks implementations to classes.

Nodejs behaviour was changed in this PR - nodejs/node#46866
@TimothyGu
Copy link
Member

@victordidenko If you try running Object.create(new URL('http://test.com')).href in the browser, you'll find that it doesn't work either. In short, a new object, whose prototype is a URL, should not be a URL in itself.

@victordidenko
Copy link

@TimothyGu Thank you! Yes, I know that already :)
I was hoping to see reference to exact place in specification though, because I cannot find it :( And I cannot understand why this object should break language fundamentals...
And I also wanted to emphasis that this is rather breaking change, and should not have been done in minor update, in my opinion. But also I'm not sure if node follows semver or not.

@lemire
Copy link
Member

lemire commented Jul 5, 2023

In the safari console (macOS):

Capture d’écran, le 2023-07-05 à 15 51 46

@lemire
Copy link
Member

lemire commented Jul 5, 2023

In deno...

Capture d’écran, le 2023-07-05 à 15 55 35

In bun...

Capture d’écran, le 2023-07-05 à 15 56 34

@anonrig
Copy link
Member Author

anonrig commented Jul 5, 2023

I think I see what the issue is... Object.create(new URL('https://yagiz.co')) worked in Node 16...

Welcome to Node.js v16.20.1.
Type ".help" for more information.
> Object.create(new URL('https://www.google.com'))
URL {
  href: 'https://www.google.com/',
  origin: 'https://www.google.com',
  protocol: 'https:',
  username: '',
  password: '',
  host: 'www.google.com',
  hostname: 'www.google.com',
  port: '',
  pathname: '/',
  search: '',
  searchParams: URLSearchParams {},
  hash: ''
}

@victordidenko
Copy link

It worked in 18.15
image
but in 18.16 this fix has landed

@victordidenko
Copy link

I totally agree with that URL should behave the same in all environments. I was just using this feature in my tests, and was really surprised, that node update from 18.15 to 18.16 broke my tests. But I've already rewrote them, and for now I'm just curious, why this behaviour in the first place?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-ci PRs that need a full CI run. whatwg-url Issues and PRs related to the WHATWG URL implementation.
Projects
None yet
Development

Successfully merging this pull request may close these issues.