sindresorhus/normalize-url

Normalize encoding

Closed this issue · 6 comments

file:///foo/node_modules/@angular/http and file:///foo/node_modules/%40angular/http are equivalent, but encodeURI or decodeURI will do nothing to @ or %40, while encodeURIComponent/decodeURIComponent will do. When for example saving URLs in a Map it is very important to always save and get a normalized form, it would be awesome if this module supported this.

Why is @ encoded in the first place? @ is allowed in URLs.

I didn't write encodeURIComponent ¯_(ツ)_/¯
But isn't it used for separating username@host? So if the username contained an @, it would need to be encoded.
But often applications receive URLs from outside, and in theory you can encode every character as a % sequence. That's why it's necessary to normalise the encoding to a canonical form.

No, I mean, why are you using encodeURIComponent on a full URL? It's for URL components.

Where do we stop though? An URL could potentially be even double-encoded:

"file%253A%252F%252F%252Ffoo%252Fnode_modules%252F%2540angular%252Fhttp"

I am not using it for that. I just potentially receive URLs as file:///foo/node_modules/%40angular/http, and that is a perfectly valid URL.
The normalisation that can be done here is decoding all characters that are not reserved.

Alright 👍