Custom http agent support (#810)

This commit is contained in:
Tomas Della Vedova
2019-04-10 11:13:22 +02:00
committed by GitHub
parent 0d65274de3
commit 2919f93b73
5 changed files with 82 additions and 16 deletions

4
lib/Connection.d.ts vendored
View File

@ -24,12 +24,14 @@ import { inspect, InspectOptions } from 'util';
import * as http from 'http';
import { SecureContextOptions } from 'tls';
export declare type agentFn = () => any;
interface ConnectionOptions {
url: URL;
ssl?: SecureContextOptions;
id?: string;
headers?: any;
agent?: AgentOptions;
agent?: AgentOptions | agentFn;
status?: string;
roles?: any;
}

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