Skip to content

Commit

Permalink
Add ability to pass global agent options for HttpClient (#378)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
aleksandrlevochkin authored Sep 19, 2024
1 parent 7654878 commit 92f407b
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
14 changes: 13 additions & 1 deletion lib/HttpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ export class HttpClient implements ifm.IHttpClient {
private _ca: string;
private _cert: string;
private _key: string;
private _httpGlobalAgentOptions: ifm.IHttpGlobalAgentOptions = {
keepAlive: false,
timeout: 30_000
};

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

if (requestOptions.globalAgentOptions) {
this._httpGlobalAgentOptions = requestOptions.globalAgentOptions;
}

this._certConfig = requestOptions.cert;

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

// if not using private agent and tunnel agent isn't setup then use global agent
if (!agent) {
agent = usingSsl ? https.globalAgent : http.globalAgent;
const globalAgentOptions: http.AgentOptions = {
keepAlive: this._httpGlobalAgentOptions.keepAlive,
timeout: this._httpGlobalAgentOptions.timeout
};
agent = usingSsl ? new https.Agent(globalAgentOptions) : new http.Agent(globalAgentOptions);
}

if (usingSsl && this._ignoreSslError) {
Expand Down
6 changes: 6 additions & 0 deletions lib/Interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export interface IRequestOptions {
ignoreSslError?: boolean;
proxy?: IProxyConfiguration;
cert?: ICertConfiguration;
globalAgentOptions?: IHttpGlobalAgentOptions;
allowRedirects?: boolean;
allowRedirectDowngrade?: boolean;
maxRedirects?: number;
Expand All @@ -72,6 +73,11 @@ export interface ICertConfiguration {
passphrase?: string;
}

export interface IHttpGlobalAgentOptions {
keepAlive?: boolean;
timeout?: number;
}

export interface IRequestQueryParams {
options?: {
separator?: string,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typed-rest-client",
"version": "2.0.2",
"version": "2.1.0",
"description": "Node Rest and Http Clients for use with TypeScript",
"main": "./RestClient.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion samples/basic/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 92f407b

Please sign in to comment.