Skip to content

Commit e17e5fb

Browse files
author
Jonathan Ginsburg
authored
Merge 2e4f1da into 94cf15e
2 parents 94cf15e + 2e4f1da commit e17e5fb

File tree

2 files changed

+42
-70
lines changed

2 files changed

+42
-70
lines changed

lib/middleware/proxy.js

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
const url = require('url')
2-
const { Agent: httpAgent } = require('http')
3-
const { Agent: httpsAgent } = require('https')
42
const httpProxy = require('http-proxy')
53
const _ = require('lodash')
64

@@ -34,24 +32,17 @@ function parseProxyConfig (proxies, config) {
3432

3533
const hostname = proxyDetails.hostname || config.hostname
3634
const protocol = proxyDetails.protocol || config.protocol
37-
const https = proxyDetails.protocol === 'https:'
38-
let port
39-
if (proxyDetails.port) {
40-
port = proxyDetails.port
41-
} else if (proxyDetails.protocol) {
42-
port = https ? '443' : '80'
43-
} else {
44-
port = config.port
35+
const defaultPorts = {
36+
'http:': '80',
37+
'https:': '443'
4538
}
39+
const port = proxyDetails.port || defaultPorts[proxyDetails.protocol] || config.port
4640
const changeOrigin = proxyConfiguration.changeOrigin || false
47-
const Agent = https ? httpsAgent : httpAgent
48-
const agent = new Agent({ keepAlive: true })
4941
const proxy = httpProxy.createProxyServer({
50-
target: { host: hostname, port, https, protocol },
42+
target: { host: hostname, port, protocol },
5143
xfwd: true,
5244
changeOrigin: changeOrigin,
53-
secure: config.proxyValidateSSL,
54-
agent
45+
secure: config.proxyValidateSSL
5546
})
5647

5748
;['proxyReq', 'proxyRes'].forEach(function (name) {
@@ -71,7 +62,7 @@ function parseProxyConfig (proxies, config) {
7162
res.destroy()
7263
})
7364

74-
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy, agent }
65+
return { path: proxyPath, baseUrl: pathname, host: hostname, port, proxy }
7566
}), 'path').reverse()
7667
}
7768

@@ -117,14 +108,7 @@ function createProxyHandler (proxies, urlRoot) {
117108
return createProxy
118109
}
119110

120-
exports.create = function (/* config */config, /* config.proxies */proxies, /* emitter */emitter) {
111+
exports.create = function (/* config */config, /* config.proxies */proxies) {
121112
const proxyRecords = parseProxyConfig(proxies, config)
122-
emitter.on('exit', (done) => {
123-
log.debug('Destroying proxy agents')
124-
proxyRecords.forEach((proxyRecord) => {
125-
proxyRecord.agent.destroy()
126-
})
127-
done()
128-
})
129113
return createProxyHandler(proxyRecords, config.urlRoot)
130114
}

test/unit/middleware/proxy.spec.js

Lines changed: 34 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -146,10 +146,16 @@ describe('middleware.proxy', () => {
146146
host: 'localhost',
147147
port: '8000',
148148
baseUrl: '/',
149-
path: '/base/',
150-
https: false
149+
path: '/base/'
151150
})
152151
expect(parsedProxyConfig[0].proxy).to.exist
152+
expect(parsedProxyConfig[0].proxy).to.containSubset({
153+
options: {
154+
target: {
155+
protocol: 'http:'
156+
}
157+
}
158+
})
153159
})
154160

155161
it('should set default http port', () => {
@@ -160,10 +166,16 @@ describe('middleware.proxy', () => {
160166
host: 'localhost',
161167
port: '80',
162168
baseUrl: '/',
163-
path: '/base/',
164-
https: false
169+
path: '/base/'
165170
})
166171
expect(parsedProxyConfig[0].proxy).to.exist
172+
expect(parsedProxyConfig[0].proxy).to.containSubset({
173+
options: {
174+
target: {
175+
protocol: 'http:'
176+
}
177+
}
178+
})
167179
})
168180

169181
it('should set default https port', () => {
@@ -174,8 +186,7 @@ describe('middleware.proxy', () => {
174186
host: 'localhost',
175187
port: '443',
176188
baseUrl: '/',
177-
path: '/base/',
178-
https: true
189+
path: '/base/'
179190
})
180191
expect(parsedProxyConfig[0].proxy).to.exist
181192
expect(parsedProxyConfig[0].proxy).to.containSubset({
@@ -195,10 +206,16 @@ describe('middleware.proxy', () => {
195206
host: 'localhost',
196207
port: '8000',
197208
baseUrl: '/proxy',
198-
path: '/base',
199-
https: false
209+
path: '/base'
200210
})
201211
expect(parsedProxyConfig[0].proxy).to.exist
212+
expect(parsedProxyConfig[0].proxy).to.containSubset({
213+
options: {
214+
target: {
215+
protocol: 'http:'
216+
}
217+
}
218+
})
202219
})
203220

204221
it('should determine protocol', () => {
@@ -209,8 +226,7 @@ describe('middleware.proxy', () => {
209226
host: 'localhost',
210227
port: '8000',
211228
baseUrl: '',
212-
path: '/base',
213-
https: true
229+
path: '/base'
214230
})
215231
expect(parsedProxyConfig[0].proxy).to.exist
216232
expect(parsedProxyConfig[0].proxy).to.containSubset({
@@ -231,8 +247,7 @@ describe('middleware.proxy', () => {
231247
host: 'localhost',
232248
port: 9877,
233249
baseUrl: '/proxy/test',
234-
path: '/base',
235-
https: false
250+
path: '/base'
236251
})
237252
expect(parsedProxyConfig[0].proxy).to.exist
238253
})
@@ -246,8 +261,7 @@ describe('middleware.proxy', () => {
246261
host: 'localhost',
247262
port: 9877,
248263
baseUrl: '/proxy/test/',
249-
path: '/base/',
250-
https: false
264+
path: '/base/'
251265
})
252266
expect(parsedProxyConfig[0].proxy).to.exist
253267
})
@@ -261,8 +275,7 @@ describe('middleware.proxy', () => {
261275
host: 'krinkle.dev',
262276
port: '80',
263277
baseUrl: '/w',
264-
path: '/w',
265-
https: false
278+
path: '/w'
266279
})
267280
expect(parsedProxyConfig[0].proxy).to.exist
268281
})
@@ -276,8 +289,7 @@ describe('middleware.proxy', () => {
276289
host: 'krinkle.dev',
277290
port: '443',
278291
baseUrl: '/w',
279-
path: '/w',
280-
https: true
292+
path: '/w'
281293
})
282294
expect(parsedProxyConfig[0].proxy).to.exist
283295
})
@@ -290,8 +302,7 @@ describe('middleware.proxy', () => {
290302
host: 'localhost',
291303
port: '8000',
292304
baseUrl: '/proxy/test/',
293-
path: '/base/',
294-
https: false
305+
path: '/base/'
295306
})
296307
expect(parsedProxyConfig[0].proxy).to.exist
297308
})
@@ -307,16 +318,14 @@ describe('middleware.proxy', () => {
307318
host: 'gstatic.com',
308319
port: '80',
309320
baseUrl: '/something',
310-
path: '/sub/some',
311-
https: false
321+
path: '/sub/some'
312322
})
313323
expect(parsedProxyConfig[0].proxy).to.exist
314324
expect(parsedProxyConfig[1]).to.containSubset({
315325
host: 'localhost',
316326
port: '9000',
317327
baseUrl: '',
318-
path: '/sub',
319-
https: false
328+
path: '/sub'
320329
})
321330
expect(parsedProxyConfig[1].proxy).to.exist
322331
})
@@ -331,8 +340,7 @@ describe('middleware.proxy', () => {
331340
host: 'localhost',
332341
port: '8000',
333342
baseUrl: '/',
334-
path: '/base/',
335-
https: false
343+
path: '/base/'
336344
})
337345
expect(parsedProxyConfig[0].proxy).to.exist
338346
})
@@ -352,24 +360,4 @@ describe('middleware.proxy', () => {
352360
it('should handle empty proxy config', () => {
353361
expect(m.parseProxyConfig({})).to.deep.equal([])
354362
})
355-
356-
it('should use http agent with keepAlive=true', () => {
357-
const proxy = { '/base': 'http://localhost:8000/proxy' }
358-
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
359-
expect(parsedProxyConfig).to.have.length(1)
360-
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
361-
keepAlive: true,
362-
protocol: 'http:'
363-
})
364-
})
365-
366-
it('should use https agent with keepAlive=true', () => {
367-
const proxy = { '/base': 'https://localhost:8000/proxy' }
368-
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
369-
expect(parsedProxyConfig).to.have.length(1)
370-
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
371-
keepAlive: true,
372-
protocol: 'https:'
373-
})
374-
})
375363
})

0 commit comments

Comments
 (0)