@@ -38,43 +38,55 @@ export default class HttpConnection implements IConnectionProvider {
38
38
39
39
public async getAgent ( ) : Promise < http . Agent > {
40
40
if ( ! this . agent ) {
41
- const { options } = this ;
42
-
43
- const httpAgentOptions : http . AgentOptions = {
44
- keepAlive : true ,
45
- maxSockets : 5 ,
46
- keepAliveMsecs : 10000 ,
47
- timeout : options . socketTimeout ?? globalConfig . socketTimeout ,
48
- } ;
49
-
50
- const httpsAgentOptions : https . AgentOptions = {
51
- ...httpAgentOptions ,
52
- minVersion : 'TLSv1.2' ,
53
- rejectUnauthorized : false ,
54
- ca : options . ca ,
55
- cert : options . cert ,
56
- key : options . key ,
57
- } ;
58
-
59
- if ( options . proxy !== undefined ) {
60
- const proxyUrl = buildProxyUrl ( options . proxy ) ;
61
- const proxyProtocol = `${ options . proxy . protocol } :` ;
62
-
63
- this . agent = new ProxyAgent ( {
64
- ...httpAgentOptions ,
65
- getProxyForUrl : ( ) => proxyUrl ,
66
- httpsAgent : new https . Agent ( httpsAgentOptions ) ,
67
- httpAgent : new http . Agent ( httpAgentOptions ) ,
68
- protocol : proxyProtocol ,
69
- } ) ;
41
+ if ( this . options . proxy !== undefined ) {
42
+ this . agent = this . createProxyAgent ( this . options . proxy ) ;
70
43
} else {
71
- this . agent = options . https ? new https . Agent ( httpsAgentOptions ) : new http . Agent ( httpAgentOptions ) ;
44
+ this . agent = this . options . https ? this . createHttpsAgent ( ) : this . createHttpAgent ( ) ;
72
45
}
73
46
}
74
47
75
48
return this . agent ;
76
49
}
77
50
51
+ private getAgentDefaultOptions ( ) : http . AgentOptions {
52
+ return {
53
+ keepAlive : true ,
54
+ maxSockets : 5 ,
55
+ keepAliveMsecs : 10000 ,
56
+ timeout : this . options . socketTimeout ?? globalConfig . socketTimeout ,
57
+ } ;
58
+ }
59
+
60
+ private createHttpAgent ( ) : http . Agent {
61
+ const httpAgentOptions = this . getAgentDefaultOptions ( ) ;
62
+ return new http . Agent ( httpAgentOptions ) ;
63
+ }
64
+
65
+ private createHttpsAgent ( ) : https . Agent {
66
+ const httpsAgentOptions : https . AgentOptions = {
67
+ ...this . getAgentDefaultOptions ( ) ,
68
+ minVersion : 'TLSv1.2' ,
69
+ rejectUnauthorized : false ,
70
+ ca : this . options . ca ,
71
+ cert : this . options . cert ,
72
+ key : this . options . key ,
73
+ } ;
74
+ return new https . Agent ( httpsAgentOptions ) ;
75
+ }
76
+
77
+ private createProxyAgent ( proxyOptions : ProxyOptions ) : ProxyAgent {
78
+ const proxyUrl = buildProxyUrl ( proxyOptions ) ;
79
+ const proxyProtocol = `${ proxyOptions . protocol } :` ;
80
+
81
+ return new ProxyAgent ( {
82
+ ...this . getAgentDefaultOptions ( ) ,
83
+ getProxyForUrl : ( ) => proxyUrl ,
84
+ httpsAgent : this . createHttpsAgent ( ) ,
85
+ httpAgent : this . createHttpAgent ( ) ,
86
+ protocol : proxyProtocol ,
87
+ } ) ;
88
+ }
89
+
78
90
public async getThriftConnection ( ) : Promise < any > {
79
91
if ( ! this . connection ) {
80
92
const { options } = this ;
0 commit comments