sindresorhus / normalize-url

Normalize a URL
MIT License
837 stars 123 forks source link

sortQueryParameters encodes the query string #149

Closed freearhey closed 2 years ago

freearhey commented 3 years ago

Is it supposed to be like this?

sortQueryParameters: true

normalizeUrl('http://sindresorhus.com/?url=http://example.com', {
    sortQueryParameters: true
});
//=> 'http://sindresorhus.com/?url=http%3A%2F%2Fexample.com'

sortQueryParameters: false

normalizeUrl('http://sindresorhus.com/?url=http://example.com', {
    sortQueryParameters: false
});
//=> 'http://sindresorhus.com/?url=http://example.com'
ludofischer commented 2 years ago

That's unforunately expected since normalize-url uses URL.searchParams() under the hood which follows different rules from the IETF URI spec since it is based on the WHATWG URL spec https://nodejs.org/api/url.html#urlsearchparams.

freearhey commented 2 years ago

Got it.

freearhey commented 2 years ago

why closed.

because my question was answered

how to resolve

pass the output string through the decodeURIComponent():

decodeURIComponent('http://sindresorhus.com/?url=http%3A%2F%2Fexample.com') 
// => 'http://sindresorhus.com/?url=http://example.com'
ludofischer commented 2 years ago

pass the output string through the decodeURIComponent():

decodeURIComponent('http://sindresorhus.com/?url=http%3A%2F%2Fexample.com') 
// => 'http://sindresorhus.com/?url=http://example.com'

Wow! Good find. But what if the original URL parameters were encoded?

ludofischer commented 2 years ago

I've made a PR to always decode the parameters, since after all the package name starts with normalize- :-)