18
18
*/
19
19
20
20
import { ClientRequest , IncomingHttpHeaders , IncomingMessage , request as httpRequest } from "http" ;
21
- import { Agent , AgentOptions , request as httpsRequest } from "https" ;
21
+ import { Agent , AgentOptions , globalAgent , request as httpsRequest } from "https" ;
22
22
import { HttpsProxyAgent } from "https-proxy-agent" ;
23
23
24
24
import * as fs from "fs" ;
@@ -37,7 +37,7 @@ import checkServerIdentity from "../helpers/checkServerIdentity";
37
37
class HttpURLConnectionClient implements ClientInterface {
38
38
private static CHARSET = "utf-8" ;
39
39
public proxy ?: AgentOptions ;
40
- private agentOptions ! : AgentOptions ;
40
+ private agents : Record < string , Agent > = { } ;
41
41
42
42
/**
43
43
* Sends an HTTP request to the specified endpoint with the provided JSON payload and configuration.
@@ -64,9 +64,7 @@ class HttpURLConnectionClient implements ClientInterface {
64
64
requestOptions . headers ??= { } ;
65
65
requestOptions . timeout = config . connectionTimeoutMillis ;
66
66
67
- if ( config . certificatePath ) {
68
- this . installCertificateVerifier ( config . certificatePath ) ;
69
- }
67
+ const agent = this . selectAgent ( config . certificatePath ) ;
70
68
71
69
const apiKey = config . apiKey ;
72
70
@@ -85,12 +83,12 @@ class HttpURLConnectionClient implements ClientInterface {
85
83
86
84
requestOptions . headers [ ApiConstants . CONTENT_TYPE ] = ApiConstants . APPLICATION_JSON_TYPE ;
87
85
88
- const httpConnection : ClientRequest = this . createRequest ( endpoint , requestOptions , config . applicationName ) ;
86
+ const httpConnection : ClientRequest = this . createRequest ( endpoint , requestOptions , agent , config . applicationName ) ;
89
87
return this . doRequest ( httpConnection , json ) ;
90
88
}
91
89
92
90
// create Request object
93
- private createRequest ( endpoint : string , requestOptions : IRequest . Options , applicationName ?: string ) : ClientRequest {
91
+ private createRequest ( endpoint : string , requestOptions : IRequest . Options , agent : Agent , applicationName ?: string ) : ClientRequest {
94
92
if ( ! requestOptions . headers ) {
95
93
requestOptions . headers = { } ;
96
94
}
@@ -114,7 +112,7 @@ class HttpURLConnectionClient implements ClientInterface {
114
112
const { host, port, ...options } = this . proxy ;
115
113
requestOptions . agent = new HttpsProxyAgent ( { host, port : port || 443 , ...options } ) ;
116
114
} else {
117
- requestOptions . agent = new Agent ( this . agentOptions ) ;
115
+ requestOptions . agent = agent ;
118
116
}
119
117
120
118
requestOptions . headers [ "Cache-Control" ] = "no-cache" ;
@@ -255,27 +253,6 @@ class HttpURLConnectionClient implements ClientInterface {
255
253
} ) ;
256
254
}
257
255
258
- private installCertificateVerifier ( terminalCertificatePath : string ) : void | Promise < HttpClientException > {
259
- try {
260
- if ( terminalCertificatePath == "unencrypted" ) {
261
- this . agentOptions = {
262
- rejectUnauthorized : false
263
- } ;
264
- } else {
265
- const certificateInput = fs . readFileSync ( terminalCertificatePath ) ;
266
- this . agentOptions = {
267
- ca : certificateInput ,
268
- checkServerIdentity,
269
- } ;
270
- }
271
-
272
- } catch ( e ) {
273
- const message = e instanceof Error ? e . message : "undefined" ;
274
- return Promise . reject ( new HttpClientException ( { message : `Error loading certificate from path: ${ message } ` } ) ) ;
275
- }
276
-
277
- }
278
-
279
256
private verifyLocation ( location : string ) : boolean {
280
257
try {
281
258
const url = new URL ( location ) ;
@@ -286,6 +263,40 @@ class HttpURLConnectionClient implements ClientInterface {
286
263
return false ;
287
264
}
288
265
}
266
+
267
+ private selectAgent ( terminalCertificatePath ?: string ) : Agent {
268
+ if ( ! terminalCertificatePath ) return globalAgent ;
269
+
270
+ if ( this . agents [ terminalCertificatePath ] )
271
+ return this . agents [ terminalCertificatePath ] ;
272
+
273
+ if ( terminalCertificatePath == "unencrypted" ) {
274
+ this . agents [ "unencrypted" ] = new Agent ( {
275
+ keepAlive : true ,
276
+ scheduling : "lifo" ,
277
+ timeout : 5000 ,
278
+ rejectUnauthorized : false ,
279
+ } ) ;
280
+ return this . agents [ "unencrypted" ] ;
281
+ } else {
282
+ try {
283
+ const certificateInput = fs . readFileSync ( terminalCertificatePath ) ;
284
+ this . agents [ terminalCertificatePath ] = new Agent ( {
285
+ keepAlive : true ,
286
+ scheduling : "lifo" ,
287
+ timeout : 5000 ,
288
+ ca : certificateInput ,
289
+ checkServerIdentity,
290
+ } ) ;
291
+ return this . agents [ terminalCertificatePath ] ;
292
+ } catch ( e ) {
293
+ const message = e instanceof Error ? e . message : "undefined" ;
294
+ throw new HttpClientException ( {
295
+ message : `Error loading certificate from path: ${ message } ` ,
296
+ } ) ;
297
+ }
298
+ }
299
+ }
289
300
}
290
301
291
302
0 commit comments