Skip to content
This repository was archived by the owner on May 3, 2022. It is now read-only.

Commit 5f61f72

Browse files
Merge pull request #25 from JamieMagee/deprecated-url
Remove deprecated URL API usage
2 parents 3e37a9d + 6543260 commit 5f61f72

File tree

4 files changed

+31
-36
lines changed

4 files changed

+31
-36
lines changed

__tests__/proxy.test.ts

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as http from 'http'
22
import * as httpm from '../_out'
33
import * as pm from '../_out/proxy'
44
import * as proxy from 'proxy'
5-
import * as url from 'url'
65

76
let _proxyConnects: string[]
87
let _proxyServer: http.Server
@@ -39,107 +38,107 @@ describe('proxy', () => {
3938
})
4039

4140
it('getProxyUrl does not return proxyUrl if variables not set', () => {
42-
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'))
41+
let proxyUrl = pm.getProxyUrl(new URL('https://github.com'))
4342
expect(proxyUrl).toBeUndefined()
4443
})
4544

4645
it('getProxyUrl returns proxyUrl if https_proxy set for https url', () => {
4746
process.env['https_proxy'] = 'https://myproxysvr'
48-
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'))
47+
let proxyUrl = pm.getProxyUrl(new URL('https://github.com'))
4948
expect(proxyUrl).toBeDefined()
5049
})
5150

5251
it('getProxyUrl does not return proxyUrl if http_proxy set for https url', () => {
5352
process.env['http_proxy'] = 'https://myproxysvr'
54-
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'))
53+
let proxyUrl = pm.getProxyUrl(new URL('https://github.com'))
5554
expect(proxyUrl).toBeUndefined()
5655
})
5756

5857
it('getProxyUrl returns proxyUrl if http_proxy set for http url', () => {
5958
process.env['http_proxy'] = 'http://myproxysvr'
60-
let proxyUrl = pm.getProxyUrl(url.parse('http://github.com'))
59+
let proxyUrl = pm.getProxyUrl(new URL('http://github.com'))
6160
expect(proxyUrl).toBeDefined()
6261
})
6362

6463
it('getProxyUrl does not return proxyUrl if https_proxy set and in no_proxy list', () => {
6564
process.env['https_proxy'] = 'https://myproxysvr'
6665
process.env['no_proxy'] = 'otherserver,myserver,anotherserver:8080'
67-
let proxyUrl = pm.getProxyUrl(url.parse('https://myserver'))
66+
let proxyUrl = pm.getProxyUrl(new URL('https://myserver'))
6867
expect(proxyUrl).toBeUndefined()
6968
})
7069

7170
it('getProxyUrl returns proxyUrl if https_proxy set and not in no_proxy list', () => {
7271
process.env['https_proxy'] = 'https://myproxysvr'
7372
process.env['no_proxy'] = 'otherserver,myserver,anotherserver:8080'
74-
let proxyUrl = pm.getProxyUrl(url.parse('https://github.com'))
73+
let proxyUrl = pm.getProxyUrl(new URL('https://github.com'))
7574
expect(proxyUrl).toBeDefined()
7675
})
7776

7877
it('getProxyUrl does not return proxyUrl if http_proxy set and in no_proxy list', () => {
7978
process.env['http_proxy'] = 'http://myproxysvr'
8079
process.env['no_proxy'] = 'otherserver,myserver,anotherserver:8080'
81-
let proxyUrl = pm.getProxyUrl(url.parse('http://myserver'))
80+
let proxyUrl = pm.getProxyUrl(new URL('http://myserver'))
8281
expect(proxyUrl).toBeUndefined()
8382
})
8483

8584
it('getProxyUrl returns proxyUrl if http_proxy set and not in no_proxy list', () => {
8685
process.env['http_proxy'] = 'http://myproxysvr'
8786
process.env['no_proxy'] = 'otherserver,myserver,anotherserver:8080'
88-
let proxyUrl = pm.getProxyUrl(url.parse('http://github.com'))
87+
let proxyUrl = pm.getProxyUrl(new URL('http://github.com'))
8988
expect(proxyUrl).toBeDefined()
9089
})
9190

9291
it('checkBypass returns true if host as no_proxy list', () => {
9392
process.env['no_proxy'] = 'myserver'
94-
let bypass = pm.checkBypass(url.parse('https://myserver'))
93+
let bypass = pm.checkBypass(new URL('https://myserver'))
9594
expect(bypass).toBeTruthy()
9695
})
9796

9897
it('checkBypass returns true if host in no_proxy list', () => {
9998
process.env['no_proxy'] = 'otherserver,myserver,anotherserver:8080'
100-
let bypass = pm.checkBypass(url.parse('https://myserver'))
99+
let bypass = pm.checkBypass(new URL('https://myserver'))
101100
expect(bypass).toBeTruthy()
102101
})
103102

104103
it('checkBypass returns true if host in no_proxy list with spaces', () => {
105104
process.env['no_proxy'] = 'otherserver, myserver ,anotherserver:8080'
106-
let bypass = pm.checkBypass(url.parse('https://myserver'))
105+
let bypass = pm.checkBypass(new URL('https://myserver'))
107106
expect(bypass).toBeTruthy()
108107
})
109108

110109
it('checkBypass returns true if host in no_proxy list with port', () => {
111110
process.env['no_proxy'] = 'otherserver, myserver:8080 ,anotherserver'
112-
let bypass = pm.checkBypass(url.parse('https://myserver:8080'))
111+
let bypass = pm.checkBypass(new URL('https://myserver:8080'))
113112
expect(bypass).toBeTruthy()
114113
})
115114

116115
it('checkBypass returns true if host with port in no_proxy list without port', () => {
117116
process.env['no_proxy'] = 'otherserver, myserver ,anotherserver'
118-
let bypass = pm.checkBypass(url.parse('https://myserver:8080'))
117+
let bypass = pm.checkBypass(new URL('https://myserver:8080'))
119118
expect(bypass).toBeTruthy()
120119
})
121120

122121
it('checkBypass returns true if host in no_proxy list with default https port', () => {
123122
process.env['no_proxy'] = 'otherserver, myserver:443 ,anotherserver'
124-
let bypass = pm.checkBypass(url.parse('https://myserver'))
123+
let bypass = pm.checkBypass(new URL('https://myserver'))
125124
expect(bypass).toBeTruthy()
126125
})
127126

128127
it('checkBypass returns true if host in no_proxy list with default http port', () => {
129128
process.env['no_proxy'] = 'otherserver, myserver:80 ,anotherserver'
130-
let bypass = pm.checkBypass(url.parse('http://myserver'))
129+
let bypass = pm.checkBypass(new URL('http://myserver'))
131130
expect(bypass).toBeTruthy()
132131
})
133132

134133
it('checkBypass returns false if host not in no_proxy list', () => {
135134
process.env['no_proxy'] = 'otherserver, myserver ,anotherserver:8080'
136-
let bypass = pm.checkBypass(url.parse('https://github.com'))
135+
let bypass = pm.checkBypass(new URL('https://github.com'))
137136
expect(bypass).toBeFalsy()
138137
})
139138

140139
it('checkBypass returns false if empty no_proxy', () => {
141140
process.env['no_proxy'] = ''
142-
let bypass = pm.checkBypass(url.parse('https://github.com'))
141+
let bypass = pm.checkBypass(new URL('https://github.com'))
143142
expect(bypass).toBeFalsy()
144143
})
145144

index.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import url = require('url')
21
import http = require('http')
32
import https = require('https')
43
import ifm = require('./interfaces')
@@ -50,7 +49,7 @@ export enum MediaTypes {
5049
* @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
5150
*/
5251
export function getProxyUrl(serverUrl: string): string {
53-
let proxyUrl = pm.getProxyUrl(url.parse(serverUrl))
52+
let proxyUrl = pm.getProxyUrl(new URL(serverUrl))
5453
return proxyUrl ? proxyUrl.href : ''
5554
}
5655

@@ -92,7 +91,7 @@ export class HttpClientResponse implements ifm.IHttpClientResponse {
9291
}
9392

9493
export function isHttps(requestUrl: string) {
95-
let parsedUrl: url.Url = url.parse(requestUrl)
94+
let parsedUrl: URL = new URL(requestUrl)
9695
return parsedUrl.protocol === 'https:'
9796
}
9897

@@ -322,7 +321,7 @@ export class HttpClient {
322321
throw new Error('Client has already been disposed.')
323322
}
324323

325-
let parsedUrl = url.parse(requestUrl)
324+
let parsedUrl = new URL(requestUrl)
326325
let info: ifm.IRequestInfo = this._prepareRequest(verb, parsedUrl, headers)
327326

328327
// Only perform retries on reads since writes may not be idempotent.
@@ -371,7 +370,7 @@ export class HttpClient {
371370
// if there's no location to redirect to, we won't
372371
break
373372
}
374-
let parsedRedirectUrl = url.parse(redirectUrl)
373+
let parsedRedirectUrl = new URL(redirectUrl)
375374
if (
376375
parsedUrl.protocol == 'https:' &&
377376
parsedUrl.protocol != parsedRedirectUrl.protocol &&
@@ -526,13 +525,13 @@ export class HttpClient {
526525
* @param serverUrl The server URL where the request will be sent. For example, https://api.github.com
527526
*/
528527
public getAgent(serverUrl: string): http.Agent {
529-
let parsedUrl = url.parse(serverUrl)
528+
let parsedUrl = new URL(serverUrl)
530529
return this._getAgent(parsedUrl)
531530
}
532531

533532
private _prepareRequest(
534533
method: string,
535-
requestUrl: url.Url,
534+
requestUrl: URL,
536535
headers: ifm.IHeaders
537536
): ifm.IRequestInfo {
538537
const info: ifm.IRequestInfo = <ifm.IRequestInfo>{}
@@ -597,9 +596,9 @@ export class HttpClient {
597596
return additionalHeaders[header] || clientHeader || _default
598597
}
599598

600-
private _getAgent(parsedUrl: url.Url): http.Agent {
599+
private _getAgent(parsedUrl: URL): http.Agent {
601600
let agent
602-
let proxyUrl: url.Url = pm.getProxyUrl(parsedUrl)
601+
let proxyUrl: URL = pm.getProxyUrl(parsedUrl)
603602
let useProxy = proxyUrl && proxyUrl.hostname
604603

605604
if (this._keepAlive && useProxy) {
@@ -631,7 +630,7 @@ export class HttpClient {
631630
maxSockets: maxSockets,
632631
keepAlive: this._keepAlive,
633632
proxy: {
634-
proxyAuth: proxyUrl.auth,
633+
proxyAuth: `${proxyUrl.username}:${proxyUrl.password}`,
635634
host: proxyUrl.hostname,
636635
port: proxyUrl.port
637636
}

interfaces.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import http = require('http')
2-
import url = require('url')
32

43
export interface IHeaders {
54
[key: string]: any
@@ -73,7 +72,7 @@ export interface IHttpClientResponse {
7372

7473
export interface IRequestInfo {
7574
options: http.RequestOptions
76-
parsedUrl: url.Url
75+
parsedUrl: URL
7776
httpModule: any
7877
}
7978

proxy.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
import * as url from 'url'
2-
3-
export function getProxyUrl(reqUrl: url.Url): url.Url | undefined {
1+
export function getProxyUrl(reqUrl: URL): URL | undefined {
42
let usingSsl = reqUrl.protocol === 'https:'
53

6-
let proxyUrl: url.Url
4+
let proxyUrl: URL
75
if (checkBypass(reqUrl)) {
86
return proxyUrl
97
}
@@ -16,13 +14,13 @@ export function getProxyUrl(reqUrl: url.Url): url.Url | undefined {
1614
}
1715

1816
if (proxyVar) {
19-
proxyUrl = url.parse(proxyVar)
17+
proxyUrl = new URL(proxyVar)
2018
}
2119

2220
return proxyUrl
2321
}
2422

25-
export function checkBypass(reqUrl: url.Url): boolean {
23+
export function checkBypass(reqUrl: URL): boolean {
2624
if (!reqUrl.hostname) {
2725
return false
2826
}

0 commit comments

Comments
 (0)