Skip to content

Commit 5efa907

Browse files
committed
feat: update http/https/socks agents
1 parent 57f73b9 commit 5efa907

File tree

5 files changed

+365
-539
lines changed

5 files changed

+365
-539
lines changed

network/agent/agent.spec.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ jest.mock('agentkeepalive', () => {
55
MockHttp['HttpsAgent'] = mockHttpAgent('https')
66
return MockHttp
77
})
8-
jest.mock('https-proxy-agent', () => mockHttpAgent('https-proxy'))
98

109
function mockHttpAgent (type: string) {
1110
return function Agent (opts: any) { // eslint-disable-line @typescript-eslint/no-explicit-any
@@ -55,8 +54,9 @@ test('all expected options passed down to proxy agent', () => {
5554
noProxy: 'qar.com, bar.com',
5655
...OPTS,
5756
}
58-
expect((getAgent('https://foo.com/bar', opts) as any).proxy).toEqual({
59-
ALPNProtocols: ['http 1.1'],
57+
const agent = (getAgent('https://foo.com/bar', opts) as any)
58+
expect(agent.connectOpts).toEqual({
59+
ALPNProtocols: ['http/1.1'],
6060
auth: 'user:pass',
6161
ca: 'ca',
6262
cert: 'cert',
@@ -65,10 +65,10 @@ test('all expected options passed down to proxy agent', () => {
6565
localAddress: 'localAddress',
6666
maxSockets: 5,
6767
port: 1234,
68-
protocol: 'https:',
6968
rejectUnauthorized: true,
7069
timeout: 6,
7170
})
71+
expect(agent.proxy.protocol).toEqual('https:')
7272
})
7373

7474
test("don't use a proxy when the URL is in noProxy", () => {
@@ -225,4 +225,4 @@ test('select correct client certificates when host has a path and the cert conta
225225
timeout: 0,
226226
__type: 'https',
227227
})
228-
})
228+
})

network/proxy-agent/proxy-agent.spec.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { getProxyAgent } from './proxy-agent';
2-
import SocksProxyAgent from 'socks-proxy-agent'
2+
import { SocksProxyAgent } from 'socks-proxy-agent'
33

44
jest.mock('agentkeepalive', () => {
55
const MockHttp = mockHttpAgent('http')
@@ -33,8 +33,9 @@ test('all expected options passed down to proxy agent', () => {
3333
noProxy: 'qar.com, bar.com',
3434
...OPTS,
3535
}
36-
expect((getProxyAgent('https://foo.com/bar', opts) as any).proxy).toEqual({
37-
ALPNProtocols: ['http 1.1'],
36+
const agent = (getProxyAgent('https://foo.com/bar', opts) as any)
37+
expect(agent.connectOpts).toEqual({
38+
ALPNProtocols: ['http/1.1'],
3839
auth: 'user:pass',
3940
ca: 'ca',
4041
cert: 'cert',
@@ -43,10 +44,10 @@ test('all expected options passed down to proxy agent', () => {
4344
localAddress: 'localAddress',
4445
maxSockets: 5,
4546
port: 1234,
46-
protocol: 'https:',
4747
rejectUnauthorized: true,
4848
timeout: 6,
4949
})
50+
expect(agent.proxy.protocol).toEqual('https:')
5051
})
5152

5253
test('a socks proxy', () => {
@@ -68,8 +69,9 @@ test('proxy credentials are decoded', () => {
6869
httpsProxy: `https://${encodeURIComponent('use@!r')}:${encodeURIComponent('p#as*s')}@my.proxy:1234/foo`,
6970
...OPTS,
7071
}
71-
expect((getProxyAgent('https://foo.com/bar', opts) as any).proxy).toEqual({
72-
ALPNProtocols: ['http 1.1'],
72+
const agent = (getProxyAgent('https://foo.com/bar', opts) as any)
73+
expect(agent.connectOpts).toEqual({
74+
ALPNProtocols: ['http/1.1'],
7375
auth: 'use@!r:p#as*s',
7476
ca: 'ca',
7577
cert: 'cert',
@@ -78,10 +80,11 @@ test('proxy credentials are decoded', () => {
7880
localAddress: 'localAddress',
7981
maxSockets: 5,
8082
port: 1234,
81-
protocol: 'https:',
83+
// protocol: 'https:',
8284
rejectUnauthorized: true,
8385
timeout: 6,
8486
})
87+
expect(agent.proxy.protocol).toEqual('https:')
8588
})
8689

8790
test('proxy credentials are decoded', () => {

network/proxy-agent/proxy-agent.ts

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PnpmError } from '@pnpm/error'
2-
import HttpsProxyAgent from 'https-proxy-agent/dist/agent'
3-
import createHttpProxyAgent from 'http-proxy-agent'
4-
import createSocksProxyAgent from 'socks-proxy-agent'
2+
import { HttpsProxyAgent } from 'https-proxy-agent'
3+
import { HttpProxyAgent } from 'http-proxy-agent'
4+
import { SocksProxyAgent } from 'socks-proxy-agent'
55
import LRU from 'lru-cache'
66

77
const DEFAULT_MAX_SOCKETS = 50
@@ -115,13 +115,9 @@ function getProxy (
115115
auth: getAuth(proxyUrl),
116116
ca: opts.ca,
117117
cert: opts.cert,
118-
host: proxyUrl.hostname,
119118
key: opts.key,
120119
localAddress: opts.localAddress,
121120
maxSockets: opts.maxSockets ?? DEFAULT_MAX_SOCKETS,
122-
path: proxyUrl.pathname,
123-
port: proxyUrl.port,
124-
protocol: proxyUrl.protocol,
125121
rejectUnauthorized: opts.strictSsl,
126122
timeout:
127123
typeof opts.timeout !== 'number' || opts.timeout === 0
@@ -131,13 +127,13 @@ function getProxy (
131127

132128
if (proxyUrl.protocol === 'http:' || proxyUrl.protocol === 'https:') {
133129
if (!isHttps) {
134-
return createHttpProxyAgent(popts)
130+
return new HttpProxyAgent(proxyUrl, popts)
135131
} else {
136-
return new PatchedHttpsProxyAgent(popts)
132+
return new PatchedHttpsProxyAgent(proxyUrl, popts)
137133
}
138134
}
139135
if (proxyUrl.protocol?.startsWith('socks')) {
140-
return createSocksProxyAgent(popts)
136+
return new SocksProxyAgent(proxyUrl, popts)
141137
}
142138
return undefined
143139
}
@@ -156,14 +152,14 @@ function getAuth (user: { username?: string, password?: string }) {
156152
const extraOpts = Symbol('extra agent opts')
157153

158154
// This is a workaround for this issue: https://github.com/TooTallNate/node-https-proxy-agent/issues/89
159-
class PatchedHttpsProxyAgent extends HttpsProxyAgent {
160-
constructor (opts: any) {
161-
super(opts)
155+
class PatchedHttpsProxyAgent<Uri extends string> extends HttpsProxyAgent<Uri> {
156+
constructor (proxyUrl: Uri | URL, opts: any) {
157+
super(proxyUrl, opts)
162158

163159
this[extraOpts] = opts
164160
}
165161

166-
callback (req: any, opts: any) {
167-
return super.callback(req, { ...this[extraOpts], ...opts })
162+
connect (req: any, opts: any) {
163+
return super.connect(req, { ...this[extraOpts], ...opts })
168164
}
169165
}

0 commit comments

Comments
 (0)