-
Notifications
You must be signed in to change notification settings - Fork 135
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
Update to use WHATWG URL API instead of url.parse() #793
Conversation
@@ -46,7 +43,7 @@ class HttpDependencyParser extends RequestParser { | |||
* Gets a dependency data contract object for a completed ClientRequest. | |||
*/ | |||
public getDependencyTelemetry(baseTelemetry?: Contracts.Telemetry, dependencyId?: string): Contracts.DependencyTelemetry { | |||
let urlObject = url.parse(this.url); | |||
let urlObject = new url.URL(this.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.
When this.url
is used, it is the full url (including protocol) so ignore base
.
const parsed = url.parse(options); | ||
if (parsed.host === "443") { | ||
options = url.parse("https://" + options); | ||
const parsed = new url.URL("http://" + options); |
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 protocol "http://" on this line is in order to make the new URL work - it requires protocol to parse url:
new URL(input[, base])
- input The absolute or relative input URL to parse. If input is relative, then base is required. If input is absolute, the base is ignored.
@@ -167,13 +165,15 @@ class HttpRequestParser extends RequestParser { | |||
} | |||
|
|||
var encrypted = (<any>request).connection ? ((<any>request).connection as any).encrypted : null; | |||
var requestUrl = url.parse(request.url); | |||
|
|||
var protocol = (encrypted || request.headers["x-forwarded-proto"] == "https") ? "https" : "http"; |
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.
Update the sequence to get the protocol first, then URL API can be used - it requires protocol in url.
telemetry.target = url.parse(telemetry.data).host; | ||
// url.parse() is deprecated, update to use WHATWG URL API instead | ||
try { | ||
telemetry.target = new url.URL(telemetry.data).host; |
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.
A TypeError
will be thrown if the input or base are not valid URLs.
To be compliant with previous behavior, when url is invalid, set telemetry.target
as null
.
assert.equal(dependencyTelemetry.data, "https://a.bing.com/search"); | ||
assert.equal(dependencyTelemetry.target, "a.bing.com"); |
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.
Remove port because :
When using URL API to create a new object, when port matches with the protocol, the port is dropped.
For example:
- new URL("https://a.bing.com:443/search").port returns "" - port dropped since https matches with 443
- new URL("http://a.bing.com:443/search").port returns "443" - port not dropped
- new URL("http://a.bing.com:80/search").port returns "" - port dropped since http matches with 80
- new URL("https://a.bing.com:80/search").port returns "80" - port not dropped
- new URL("https://a.bing.com:1234/search").port returns "1234" - port not dropped
- new URL("http://a.bing.com:1234/search").port returns "1234" - port not dropped
Confirmed with Nev that : the default port for a protocol is often never sent @hectorhdzg can you confirm this? Thanks.
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.
LGTM, thanks
This PR includes:
url.parse()
method.url.parse()
instead ofurl.URL()
#432Details:
Note:
new URL(input[, base])
: detailsinput
is relative, thenbase
is required. Ifinput
is absolute, thebase
is ignored.TypeError
will be thrown if theinput
orbase
are not valid URLs.