-
Notifications
You must be signed in to change notification settings - Fork 156
Expose a URLHost class to JavaScript #288
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
base: main
Are you sure you want to change the base?
Conversation
|
@tabatkins it seems a little weird that just because toJSON is the same as the stringification behavior, it needs to be annotated as a stringifier rather than a method, is that really how this should work? |
Just depends on how you want it to link. To get The "stringification behavior" thing is mostly to handle anonymous stringifiers. I've thought about also just making it an implicit |
|
@tabatkins I defined both, but toJSON() doesn't link and I end up with duplicate IDs somehow. |
|
@annevk - Is this something expected to land. I am working on updating documentation around stuff defined in this spec and it would be nice to be clear on what the state of things is. :) |
|
I don't expect this to land in this form. If something moves here I'll add the documentation team to make sure you all are informed. |
|
@tabatkins so the weird thing is that in |
|
Hmm, I catch stringifier specially, so likely I just didn't catch it in the attribute form. I'll look into it. |
|
@tabatkins wouldn't going that way break existing specifications? I guess that's another way to go though... |
|
@annevk ... at this point, what is the likelihood of this moving forward? |
|
This still seems like something the web platform should offer, but I'd rather wait until browsers have more aligned URL parsers and IDNA handling before making another push to expose this API. |
Tests: ...
cbf3e18 to
7f77861
Compare
|
@valenting @ricea is there interest from Gecko and Chromium in this API addition? Now that we're close with IDNA this seems like a nice improvement. Note that this intentionally does not expose ToUnicode. Doing that responsibly requires a separate effort. |
|
I don't feel like I could confidently write an explainer for this. |
|
I don't know if there's enough benefit to add it in its current form. If I'm reading it correctly then what it's bringing is an easy way to check whether a host is IPv4/v6/a domain. Is there much need for that? |
|
It also gives an easy way to parse a host. Which can be useful if your chosen scheme always gives you an opaque host (or IPv6). And more ergonomic than something like |
|
It seems you can get much the same functionality by abusing const c = new URL('https://example.com')
c.host = '😀'
'😀'
c.host
'xn--e28h'
c.host = 564
564
c.host
'0.0.2.52'Not that I'd call that a good API, but if the functionality is only needed by a small minority of developers, it might be good enough? |
|
Eww. Maybe? There definitely seems to be merit to this if we expose more of IDNA or the PSL: https://www.npmjs.com/search?q=domain%20parser and https://www.npmjs.com/search?q=idna. (And given the number of downloads of the packages there I'm not sure if it's a small minority that cares about hosts.) |
|
If it were to (safely) include ToUnicode or other new IDNA functionality it would be easy to say we should add it. But right now it seems to be more like syntactic sugar. I'm not strongly against it, but I don't think there's a strong case for it right now. |
|
To address an earlier question, there is demand for checking whether a string is an IP address: #696. And judging from https://www.npmjs.com/package/ipaddr.js this is very popular. Given how many strings can be turned into IP addresses offering an authoritative answer to that question would be good I think. |
|
We are definitely interested in implementing this in Ada & Node.js. |
|
FYI I have implemented this in my Swift library: documentation. For low-level networking applications, you'll find that they often pass the hostname through if case .ipv4Address(let address) = url.host,
case (10, 0, 0, _) = address.octets {
// URL has host "10.0.0.???"
}It's quite nice. I'm very happy with it. Possibly less useful on the web, but I could imagine NodeJS could make use of something like this. Another facet to this API that is quite useful is the ability to parse an opaque hostname in the context of a known URL scheme. In the documentation, I give the example of processing // 🚩 "http:" URLs use a special Unicode -> ASCII conversion
// (called "IDNA"), designed for compatibility with existing
// internet infrastructure.
let httpURL = WebURL("http://alice@أهلا.com/data")!
httpURL // "http://[email protected]/data"
// ^^^^^^^^^^^
httpURL.host // ✅ .domain(Domain { "xn--igbi0gl.com" })
// 🚩 "ssh:" URLs have opaque hostnames, so Unicode characters
// are just percent-encoded. The URL Standard doesn't even know
// this a network address, so we don't get any automatic processing.
let sshURL = WebURL("ssh://alice@أهلا.com/data")!
sshURL // "ssh://alice@%D8%A3%D9%87%D9%84%D8%A7.com/data"
// ^^^^^^^^^^^^^^^^^^^^^^^^
sshURL.host // 😐 .opaque("%D8%A3%D9%87%D9%84%D8%A7.com")
// 🚩 Using the WebURL.Host initializer, we can interpret our
// SSH hostname as if it were in an HTTP URL.
let sshAsHttp = WebURL.Host(sshURL.hostname!, scheme: "http")
// ✅ .domain(Domain { "xn--igbi0gl.com" })IPv4 support: let url = WebURL("ssh://[email protected]/data")!
url // "ssh://[email protected]/data"
url.host // 😐 .opaque("192.168.15.21")
// ^^^^^^
WebURL.Host(url.hostname!, scheme: "http")
// ✅ .ipv4Address(IPv4Address { 192.168.15.21 })Scenarios like that may be more broadly useful on the web. |
|
@annevk ... any updates on this an whether it might ever advance? |
|
@jasnell maybe. @mikewest is exploring some ideas around exposing origin or site, and some of the ideas around public suffix and registrable domains overlap. Not sure about IP addresses vs domains though. https://github.com/mikewest/origin-api Not quite sure what to make of that yet, especially with "partition" (for lack of a better word) becoming the security boundary for state in browsers. The other thing that concerns me a little bit is that I've learned that WebRTC wants something like this but for host + port. And so I've wondered if not addressing port is problematic. |
|
Port certainly is less important for workers but I can see use for it in node use cases. I'm certainly not opposed to supporting it. |
Regardless of where the
Perhaps we should try to piece together what that thing might look like? As I mentioned in WebKit/standards-positions#538 (comment), my feeling is that it would be something that held an
Do you have a pointer to that discussion? I'd like to understand the use case. |
|
See step 4 of https://w3c.github.io/webrtc-pc/#dfn-validate-an-ice-server-url. (Some discussion in https://bugs.webkit.org/show_bug.cgi?id=164508 and w3c/webrtc-pc#2660.) |
Thanks. I agree that something cleaner than step 4 of https://w3c.github.io/webrtc-pc/#dfn-validate-an-ice-server-url would be nice to have, though I wonder how much of a one-off it would end up being. For STUN and TURN specifically, the prevalence of those particular protocols might justify baking a scheme-specific parsing rule into URL more directly, such that |
|
I think we have to be very careful about playing favorites with schemes. The fact that we have special schemes to begin with is rather unfortunate. I'd rather figure out building blocks that work well for everyone. |
That's a reasonable desire. I feel like STUN and TURN scheme are defensible to add to the platform more centrally given how widely relied-upon they are, but I'm also in favor of providing the primitives that let them define behavior without inexplicable string concatenation. Do you think both a |
|
I don't have a great handle on it. It's not as easy as splitting the string on If you don't want to allow ports you could check for that to be null. So maybe |
That's also how I'd evaluate it, FWIW. |
(See WHATWG Working Mode: Changes for more details.)
Preview | Diff