Skip to content

Commit 2eeae6d

Browse files
committed
ARTESCA-6419: implement base http(s) agents and export
1 parent 1f939d7 commit 2eeae6d

File tree

4 files changed

+94
-0
lines changed

4 files changed

+94
-0
lines changed

index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import httpAgent from "./lib/http-agent";
2+
import httpsAgent from "./lib/https-agent";
3+
4+
export const http = {
5+
Agent: httpAgent,
6+
};
7+
8+
export const https = {
9+
Agent: httpsAgent,
10+
};

lib/config/default.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export default interface clientConfigurationDefault {
2+
/**
3+
* Maximum Socket Number: true if TCP session reuse must be enabled
4+
*/
5+
maxSockets?: boolean;
6+
}

lib/http-agent.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as http from 'http';
2+
import clientConfigurationDefault from './config/default';
3+
4+
/**
5+
* @class ClientHttp
6+
* Abstracts the native http.Agent class to enforce common
7+
* networking configuration across components.
8+
*/
9+
export default class ClientHttp extends http.Agent {
10+
/**
11+
* The maximum socket configuration defaults to 50.
12+
*/
13+
private static maxSocketsConfiguration = Number(process.env.MAX_SOCKETS) || 50;
14+
15+
/**
16+
* Constructor for the ClientHttp class
17+
*
18+
* @param opts - Custom HTTP Agent options
19+
* @param config - user-defined default configuration du apply
20+
*/
21+
constructor(
22+
opts?: http.AgentOptions,
23+
config: clientConfigurationDefault = {
24+
maxSockets: true,
25+
},
26+
) {
27+
// Enforce TCP session reuse configuration, unless explicitely specified.
28+
const defaultConfigurations: http.AgentOptions = {};
29+
if (config.maxSockets) {
30+
defaultConfigurations.keepAlive = true;
31+
defaultConfigurations.maxSockets = ClientHttp.maxSocketsConfiguration;
32+
defaultConfigurations.maxFreeSockets = ClientHttp.maxSocketsConfiguration;
33+
}
34+
super({
35+
...opts,
36+
...defaultConfigurations,
37+
});
38+
}
39+
}

lib/https-agent.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import * as https from 'https';
2+
import clientConfigurationDefault from './config/default';
3+
4+
/**
5+
* @class ClientHttps
6+
* Abstracts the native https.Agent class to enforce common
7+
* networking configuration across components.
8+
*/
9+
export default class ClientHttps extends https.Agent {
10+
/**
11+
* The maximum socket configuration defaults to 50.
12+
*/
13+
private static maxSocketsConfiguration = Number(process.env.MAX_SOCKETS) || 50;
14+
15+
/**
16+
* Constructor for the ClientHttps class
17+
*
18+
* @param opts - Custom HTTPs Agent options
19+
* @param config - user-defined default configuration du apply
20+
*/
21+
constructor(
22+
opts?: https.AgentOptions,
23+
config: clientConfigurationDefault = {
24+
maxSockets: true,
25+
},
26+
) {
27+
// Enforce TCP session reuse configuration, unless explicitely specified.
28+
const defaultConfigurations: https.AgentOptions = {};
29+
if (config.maxSockets) {
30+
defaultConfigurations.keepAlive = true;
31+
defaultConfigurations.maxSockets = ClientHttps.maxSocketsConfiguration;
32+
defaultConfigurations.maxFreeSockets = ClientHttps.maxSocketsConfiguration;
33+
}
34+
super({
35+
...opts,
36+
...defaultConfigurations,
37+
});
38+
}
39+
}

0 commit comments

Comments
 (0)