Custom http agent support (#810)

This commit is contained in:
Tomas Della Vedova
2019-04-10 11:13:22 +02:00
committed by delvedor
parent 11c7475566
commit 48233503f7
5 changed files with 82 additions and 16 deletions

View File

@ -46,18 +46,20 @@ class Connection {
throw new ConfigurationError(`Invalid protocol: '${this.url.protocol}'`)
}
// Probably there is a bug in Node Core
// see https://github.com/nodejs/node/issues/26357
const keepAliveFalse = opts.agent && opts.agent.keepAlive === false
const agentOptions = Object.assign({}, {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: keepAliveFalse ? Infinity : 256,
maxFreeSockets: 256
}, opts.agent)
this.agent = this.url.protocol === 'http:'
? new http.Agent(agentOptions)
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
if (typeof opts.agent === 'function') {
this.agent = opts.agent()
} else {
const keepAliveFalse = opts.agent && opts.agent.keepAlive === false
const agentOptions = Object.assign({}, {
keepAlive: true,
keepAliveMsecs: 1000,
maxSockets: keepAliveFalse ? Infinity : 256,
maxFreeSockets: 256
}, opts.agent)
this.agent = this.url.protocol === 'http:'
? new http.Agent(agentOptions)
: new https.Agent(Object.assign({}, agentOptions, this.ssl))
}
this.makeRequest = this.url.protocol === 'http:'
? http.request