Custom http agent support (#810)
This commit is contained in:
committed by
GitHub
parent
0d65274de3
commit
2919f93b73
4
lib/Connection.d.ts
vendored
4
lib/Connection.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
||||
Reference in New Issue
Block a user