Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to pass global agent options for HttpClient #378

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.