pwnall / node-xhr2

XMLHttpRequest emulator for node.js
MIT License
104 stars 41 forks source link

Fix 'TypeError [ERR_UNESCAPED_CHARACTERS]: Request path contains unescaped characters' #42

Open supermario opened 4 years ago

supermario commented 4 years ago

It appears most browsers (tested in Firefox, Safari and Chromium[Chrome/Brave]) will url-encode paths automatically if they are not already encoded.

xhr2 doesn't match that behaviour when run in node: using an unencoded URL will result in TypeError [ERR_UNESCAPED_CHARACTERS].

Just adding the encoding line however causes a new issue; already encoded URLs end up being double-encoded, changing the URL incorrectly.

Using native-url instead of url fixes this, as well as removing the dependency on the deprecated Legacy URL API.

It would have of course been better to refactor xhr2 to not use the deprecated API, however this does not appear to be trivial looking at the native-url code – at least, I am not able to do that work presently.

I'm not necessarily expecting this to get merged (I think the fact that xhr2 has no dependencies is pretty nice!), but I figured it might be useful for anyone else that runs into the ERR_UNESCAPED_CHARACTERS issue, or for the project maintainers as they're considering how to deal with the API deprecation.

This probably resolves the issue referenced in #35 .