@@ -2,9 +2,10 @@ import thrift from 'thrift';
22import https from 'https' ;
33import http from 'http' ;
44import { HeadersInit } from 'node-fetch' ;
5+ import { ProxyAgent } from 'proxy-agent' ;
56
67import IConnectionProvider from '../contracts/IConnectionProvider' ;
7- import IConnectionOptions from '../contracts/IConnectionOptions' ;
8+ import IConnectionOptions , { ProxyOptions } from '../contracts/IConnectionOptions' ;
89import globalConfig from '../../globalConfig' ;
910
1011import ThriftHttpConnection from './ThriftHttpConnection' ;
@@ -16,6 +17,8 @@ export default class HttpConnection implements IConnectionProvider {
1617
1718 private connection ?: ThriftHttpConnection ;
1819
20+ private agent ?: http . Agent ;
21+
1922 constructor ( options : IConnectionOptions ) {
2023 this . options = options ;
2124 }
@@ -28,26 +31,59 @@ export default class HttpConnection implements IConnectionProvider {
2831 } ) ;
2932 }
3033
31- private async getAgent ( ) : Promise < http . Agent > {
32- const { options } = this ;
34+ public async getAgent ( ) : Promise < http . Agent > {
35+ if ( ! this . agent ) {
36+ if ( this . options . proxy !== undefined ) {
37+ this . agent = this . createProxyAgent ( this . options . proxy ) ;
38+ } else {
39+ this . agent = this . options . https ? this . createHttpsAgent ( ) : this . createHttpAgent ( ) ;
40+ }
41+ }
42+
43+ return this . agent ;
44+ }
3345
34- const httpAgentOptions : http . AgentOptions = {
46+ private getAgentDefaultOptions ( ) : http . AgentOptions {
47+ return {
3548 keepAlive : true ,
3649 maxSockets : 5 ,
3750 keepAliveMsecs : 10000 ,
38- timeout : options . socketTimeout ?? globalConfig . socketTimeout ,
51+ timeout : this . options . socketTimeout ?? globalConfig . socketTimeout ,
3952 } ;
53+ }
4054
55+ private createHttpAgent ( ) : http . Agent {
56+ const httpAgentOptions = this . getAgentDefaultOptions ( ) ;
57+ return new http . Agent ( httpAgentOptions ) ;
58+ }
59+
60+ private createHttpsAgent ( ) : https . Agent {
4161 const httpsAgentOptions : https . AgentOptions = {
42- ...httpAgentOptions ,
62+ ...this . getAgentDefaultOptions ( ) ,
4363 minVersion : 'TLSv1.2' ,
4464 rejectUnauthorized : false ,
45- ca : options . ca ,
46- cert : options . cert ,
47- key : options . key ,
65+ ca : this . options . ca ,
66+ cert : this . options . cert ,
67+ key : this . options . key ,
4868 } ;
69+ return new https . Agent ( httpsAgentOptions ) ;
70+ }
71+
72+ private createProxyAgent ( proxyOptions : ProxyOptions ) : ProxyAgent {
73+ const proxyAuth = proxyOptions . auth ?. username
74+ ? `${ proxyOptions . auth . username } :${ proxyOptions . auth ?. password ?? '' } @`
75+ : '' ;
76+ const proxyUrl = `${ proxyOptions . protocol } ://${ proxyAuth } ${ proxyOptions . host } :${ proxyOptions . port } ` ;
4977
50- return options . https ? new https . Agent ( httpsAgentOptions ) : new http . Agent ( httpAgentOptions ) ;
78+ const proxyProtocol = `${ proxyOptions . protocol } :` ;
79+
80+ return new ProxyAgent ( {
81+ ...this . getAgentDefaultOptions ( ) ,
82+ getProxyForUrl : ( ) => proxyUrl ,
83+ httpsAgent : this . createHttpsAgent ( ) ,
84+ httpAgent : this . createHttpAgent ( ) ,
85+ protocol : proxyProtocol ,
86+ } ) ;
5187 }
5288
5389 public async getThriftConnection ( ) : Promise < any > {
0 commit comments