-
Notifications
You must be signed in to change notification settings - Fork 121
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
fix: Don't mutate passed URL object #470
Conversation
Before this change, a passed URL object would be mutated if query parameters were appended via the `data` option. Now we clone the object instead, so the original is not affected.
Codecov Report
@@ Coverage Diff @@
## master #470 +/- ##
=======================================
Coverage 99.64% 99.64%
=======================================
Files 8 9 +1
Lines 1411 1423 +12
Branches 253 253
=======================================
+ Hits 1406 1418 +12
Misses 5 5
... and 3 files with indirect coverage changes 📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
src/HttpClient.ts
Outdated
@@ -201,7 +201,8 @@ export class HttpClient extends EventEmitter { | |||
// url maybe url.parse(url) object in urllib2 | |||
requestUrl = new URL(urlFormat(url)); | |||
} else { | |||
requestUrl = url; | |||
// Don't mutate the URL object the user passed in | |||
requestUrl = new URL(url); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should change url to a string before pass to URL
requestUrl = new URL(url.toString());
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The URL constructor does that automatically; it's perfectly happy being passed a URL object or anything else with a toString
method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, weird, the node:url URL type wants a string, but the whatwg URL type (in browsers and with types provided by the dom module of typescript) is happy to take another URL. I'll add the .toString(), and a test.
src/HttpClient.ts
Outdated
} | ||
// url maybe url.parse(url) object in urllib2 | ||
// or even if not, we clone to avoid mutating it | ||
requestUrl = new URL(urlFormat(url)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://nodejs.org/dist/latest-v18.x/docs/api/url.html#urlformaturlobject
urlFormat is only for the url.parse()
return object.
If the url is a URL instance, I think we should use new URL(url.toString())
is better.
And the urlformat()
is Legacy
, it should not be used for non-compatibility issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see
@tremby Thanks for your patient contribution! |
[skip ci] ## [3.19.3](v3.19.2...v3.19.3) (2023-09-21) ### Bug Fixes * Don't mutate passed URL object ([#470](#470)) ([5b8867f](5b8867f))
Before this change, a passed URL object would be mutated if query parameters were appended via the
data
option.Now we clone the object instead, so the original is not affected.
Background:
I am passing
URL
objects to therequest
method, and then adding query parameters via thedata
option. Example:I found that if I then make another request to the same URL but with different parameters:
...a request is actually made to
http://example.com?param1=value1¶m2=value2
.My URL object is being mutated by the urllib library. I see this as a bug.