-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Use WHATWG URL parsing, if available, instead of url.parse #2564
base: master
Are you sure you want to change the base?
Conversation
Make sure to add a new test case for what you are trying to fix with this. Also curious on the change to the existing test: would this change now break anyone who has a colon in thier password? |
That's an interesting question. I did this in kind of a hurry but then figured I could just pass an object and avoid the problem I was having. I'm not in a hurry now, so I'll sure add a test for that. About people using and it breaking, I think I need to change the parser to decode the password when using WHATWG URL as it seems it is always encoded when parsed. |
> new URL("foo://user:pa:ss@host")
URL {
href: 'foo://user:pa%3Ass@host',
origin: 'null',
protocol: 'foo:',
username: 'user',
password: 'pa%3Ass',
host: 'host',
hostname: 'host',
port: '',
pathname: '',
search: '',
searchParams: URLSearchParams {},
hash: ''
}
> new URL("foo://user:pa%3Ass@host")
URL {
href: 'foo://user:pa%3Ass@host',
origin: 'null',
protocol: 'foo:',
username: 'user',
password: 'pa%3Ass',
host: 'host',
hostname: 'host',
port: '',
pathname: '',
search: '',
searchParams: URLSearchParams {},
hash: ''
} It always encodes, whether it's already encoded or not. |
What was failing for me was that I had a In the following example, assume password is > url.parse("foo://user:pass%@host")
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>)
at Url.parse (node:url:368:19)
at Object.urlParse [as parse] (node:url:156:13)
> new URL("foo://user:pass%@host")
URL {
href: 'foo://user:pass%@host',
origin: 'null',
protocol: 'foo:',
username: 'user',
password: 'pass%',
host: 'host',
hostname: 'host',
port: '',
pathname: '',
search: '',
searchParams: URLSearchParams {},
hash: ''
} What's failing: > decodeURIComponent("pass%")
Uncaught URIError: URI malformed
at decodeURIComponent (<anonymous>) |
But shouldn't you always need to url encodure your password? What if your password is |
In that case you should, but according to the spec you don't need to unless it causes ambiguity (like your case). Password must be encoded if it has |
Gotcha. What are we expecting the outcome of |
I don't know the implementation. Do you have any hints? As a funny note, your example URL is parsed in a different way on Chrome. I thought the implementation was the same. |
@dougwilson you meant something like this? function decodeUriComponent(str) {
return str.replace(/\%([a-f0-9]{2})/ig, (_, hex) => (String.fromCharCode(parseInt(hex, 16))));
} (but converted to run on older node versions) |
The strategy:
URL
is a functionURL.prototype
is an objectIf the checks are true, use
new URL
instead ofurl.parse
.The only 2 changes I know of, are:
URL
hasusername
andpassword
already parsed, no need to check forauth
URL
hassearchParams
instead ofquery
Closes #2563