-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
Implement URL.from #28482
Implement URL.from #28482
Conversation
It should substitude legacy `url.format` with respect to the new URL API. Based on discussion: #25099
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you!
Just some possible doc nits.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should invent non-standard extensions on the URL class - if we want to provide a convenient method (even just as an experiment), we could put it directly on url
instead.
|
||
let flags = 0; | ||
|
||
if ( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if we instead wrote this more like we do in the existing codebase?:
if (protocol === 'file:' ||
protocol === 'https:' ||
protocol === 'wss:' ||
protocol === 'http:' ||
protocol === 'ftp:' ||
protocol === 'ws:' ||
protocol === 'gopher:') {
flags |= URL_FLAGS_SPECIAL;
} else if (!host) {
flags |= URL_FLAGS_CANNOT_BE_BASE;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you mean? Where would be such example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Example of what it would look like? I provided one above.
@joyeecheung you have the point and it is valid. But |
@viktor-ku It would make sense if we controlled the |
Just a quick update: Any thought on how to name such function? |
Could you link to that conversation? That seems silly. Web browsers also suffer from this. |
@Fishrock123 previous convos: whatwg/url#354 and #12682 right now URL is designed such that it can only ever represent an unambiguous absolute url. The current minimal possible output from this API ( |
I'm +1 on adding the URL.from = function(args) {
return Object.assign(new URL(`${args.protocol||'http:'}//${args.host}||'example.com'}`), args);
} Albeit with argument checking added. |
@jasnell needs an host too, just |
heh, yeah, I'd accidentally omitted that in the example above. Will fix! |
Should probably throw if |
I think throwing would be appropriate if those aren't available. A number of different scenarios may need to be accounted for depending on the protocol that is given (my example above is an oversimplification). For instance, if the protocol is Object.assign(new URL('mailto:bob@example.com'), { pathname: 'sally@example.org' }) We will essentially need to special case known special URL protocols and treat unknown ones generically, but doing so still simplifies the implementation relative to this PR |
Let me respectfully disagree on that one. I know that it will simplify implementation, but why would you need to parse the entire string when you already know parts of the URL like host, protocol etc? I want to make this function perform well! And yes, I'd like to add as minimal validation as possible so it would be users job to know what they want. So I am leaving I will restrict passing anything expect of |
PR has been updated! |
``` | ||
|
||
All properties are optional in case you want to build your `URL` object | ||
gradually (note that you have to pass an empty object in this case anyway) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit:
gradually (note that you have to pass an empty object in this case anyway) | |
gradually (note that you have to pass an empty object in this case anyway). |
@@ -1323,6 +1362,12 @@ console.log(myURL.origin); | |||
[`url.format()`]: #url_url_format_urlobject | |||
[`url.href`]: #url_url_href | |||
[`url.parse()`]: #url_url_parse_urlstring_parsequerystring_slashesdenotehost | |||
[`url.protocol`]: #url_url_protocol |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: bottom references are usually sorted in ASCII order.
What about url.format({protocol: 'http', hostname: '::1'})
// http://[::1]/ |
8ae28ff
to
2935f72
Compare
I will be honest, I was, and still is pretty confused about what to do with this. Opinions have greatly divided amongst reviewers and at this point I really don't know how (and if) to proceed. Closing for now, because of that. |
Thank you for the work on this @viktor-ku ... Yeah, can definitely understand the confusion, I think we're still working through exactly the right thing to do with this. |
It should substitute legacy
url.format
with respectto the new URL API.
Based on #25099
Checklist
make -j4 test
(UNIX), orvcbuild test
(Windows) passes