Skip to content

Commit

Permalink
Merge 7e4df8a into 41fa9c0
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Jun 10, 2020
2 parents 41fa9c0 + 7e4df8a commit ac23b43
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 4 deletions.
21 changes: 17 additions & 4 deletions lib/middleware/proxy.js
@@ -1,4 +1,6 @@
const url = require('url')
const { Agent: httpAgent } = require('http')
const { Agent: httpsAgent } = require('https')
const httpProxy = require('http-proxy')
const _ = require('lodash')

Expand Down Expand Up @@ -42,11 +44,14 @@ function parseProxyConfig (proxies, config) {
port = config.port
}
const changeOrigin = proxyConfiguration.changeOrigin || false
const Agent = https ? httpsAgent : httpAgent
const agent = new Agent({ keepAlive: true })
const proxy = httpProxy.createProxyServer({
target: { host: hostname, port, https, protocol },
xfwd: true,
changeOrigin: changeOrigin,
secure: config.proxyValidateSSL
secure: config.proxyValidateSSL,
agent
})

;['proxyReq', 'proxyRes'].forEach(function (name) {
Expand All @@ -66,7 +71,7 @@ function parseProxyConfig (proxies, config) {
res.destroy()
})

return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy }
return { path: proxyPath, baseUrl: pathname, host: hostname, port, https, proxy, agent }
}), 'path').reverse()
}

Expand Down Expand Up @@ -112,6 +117,14 @@ function createProxyHandler (proxies, urlRoot) {
return createProxy
}

exports.create = function (/* config */config, /* config.proxies */proxies) {
return createProxyHandler(parseProxyConfig(proxies, config), config.urlRoot)
exports.create = function (/* config */config, /* config.proxies */proxies, /* emitter */emitter) {
const proxyRecords = parseProxyConfig(proxies, config)
emitter.on('exit', (done) => {
log.debug('Destroying proxy agents')
proxyRecords.forEach((proxyRecord) => {
proxyRecord.agent.destroy()
})
done()
})
return createProxyHandler(proxyRecords, config.urlRoot)
}
20 changes: 20 additions & 0 deletions test/unit/middleware/proxy.spec.js
Expand Up @@ -352,4 +352,24 @@ describe('middleware.proxy', () => {
it('should handle empty proxy config', () => {
expect(m.parseProxyConfig({})).to.deep.equal([])
})

it('should use http agent with keepAlive=true', () => {
const proxy = { '/base': 'http://localhost:8000/proxy' }
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
keepAlive: true,
protocol: 'http:'
})
})

it('should use https agent with keepAlive=true', () => {
const proxy = { '/base': 'https://localhost:8000/proxy' }
const parsedProxyConfig = m.parseProxyConfig(proxy, {})
expect(parsedProxyConfig).to.have.length(1)
expect(parsedProxyConfig[0].proxy.options.agent).to.containSubset({
keepAlive: true,
protocol: 'https:'
})
})
})

0 comments on commit ac23b43

Please sign in to comment.