Skip to content

Commit 92f407b

Browse files
Add ability to pass global agent options for HttpClient (#378)
* Increase timeout for global agent * Add ability to pass global agent options to http client constructor * Update version in test projects --------- Co-authored-by: v-levockina <undefined>
1 parent 7654878 commit 92f407b

File tree

6 files changed

+24
-6
lines changed

6 files changed

+24
-6
lines changed

lib/HttpClient.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,10 @@ export class HttpClient implements ifm.IHttpClient {
121121
private _ca: string;
122122
private _cert: string;
123123
private _key: string;
124+
private _httpGlobalAgentOptions: ifm.IHttpGlobalAgentOptions = {
125+
keepAlive: false,
126+
timeout: 30_000
127+
};
124128

125129
constructor(userAgent: string | null | undefined, handlers?: ifm.IRequestHandler[], requestOptions?: ifm.IRequestOptions) {
126130
this.userAgent = userAgent;
@@ -148,6 +152,10 @@ export class HttpClient implements ifm.IHttpClient {
148152
});
149153
}
150154

155+
if (requestOptions.globalAgentOptions) {
156+
this._httpGlobalAgentOptions = requestOptions.globalAgentOptions;
157+
}
158+
151159
this._certConfig = requestOptions.cert;
152160

153161
if (this._certConfig) {
@@ -529,7 +537,11 @@ export class HttpClient implements ifm.IHttpClient {
529537

530538
// if not using private agent and tunnel agent isn't setup then use global agent
531539
if (!agent) {
532-
agent = usingSsl ? https.globalAgent : http.globalAgent;
540+
const globalAgentOptions: http.AgentOptions = {
541+
keepAlive: this._httpGlobalAgentOptions.keepAlive,
542+
timeout: this._httpGlobalAgentOptions.timeout
543+
};
544+
agent = usingSsl ? new https.Agent(globalAgentOptions) : new http.Agent(globalAgentOptions);
533545
}
534546

535547
if (usingSsl && this._ignoreSslError) {

lib/Interfaces.ts

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export interface IRequestOptions {
4747
ignoreSslError?: boolean;
4848
proxy?: IProxyConfiguration;
4949
cert?: ICertConfiguration;
50+
globalAgentOptions?: IHttpGlobalAgentOptions;
5051
allowRedirects?: boolean;
5152
allowRedirectDowngrade?: boolean;
5253
maxRedirects?: number;
@@ -72,6 +73,11 @@ export interface ICertConfiguration {
7273
passphrase?: string;
7374
}
7475

76+
export interface IHttpGlobalAgentOptions {
77+
keepAlive?: boolean;
78+
timeout?: number;
79+
}
80+
7581
export interface IRequestQueryParams {
7682
options?: {
7783
separator?: string,

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "typed-rest-client",
3-
"version": "2.0.2",
3+
"version": "2.1.0",
44
"description": "Node Rest and Http Clients for use with TypeScript",
55
"main": "./RestClient.js",
66
"scripts": {

samples/basic/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test/package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)