@@ -35,24 +35,6 @@ const defaultRequestOptions = {
35
35
throwHttpErrors : false ,
36
36
} ;
37
37
38
- //Simple proxy tunnel for easier debug
39
- if ( socksProxy ) {
40
- const socksProtocol = 'socks:' ;
41
- defaultRequestOptions . agent = new SocksProxyAgent (
42
- String ( socksProxy ) . startsWith ( socksProtocol )
43
- ? socksProxy
44
- : `${ socksProtocol } //${ socksProxy } `
45
- ) ;
46
- } else if ( httpProxy ) {
47
- const parsedUrl = new URL ( httpProxy ) ;
48
- defaultRequestOptions . agent = tunnel . httpOverHttp ( {
49
- proxy : {
50
- host : parsedUrl . hostname ,
51
- port : parsedUrl . port ,
52
- } ,
53
- } ) ;
54
- }
55
-
56
38
/**
57
39
* Proxy for apis or other http requests
58
40
*/
@@ -70,23 +52,56 @@ class HttpClient {
70
52
constructor ( options = { } , requestOptions = { } ) {
71
53
this . endPoint = options . endPoint || defaultEndpoint ;
72
54
this . endPointHost = '' ;
55
+ this . endPointParsedUrl = { } ;
73
56
if ( this . endPoint ) {
74
- const parsedUrl = new URL ( this . endPoint ) ;
75
- this . endPointHost = parsedUrl . host ;
57
+ this . endPointParsedUrl = new URL ( this . endPoint ) ;
58
+ this . endPointHost = this . endPointParsedUrl . host ;
59
+ }
60
+ const clientBaseOptions = {
61
+ baseUrl : this . endPoint ,
62
+ } ;
63
+ const agent = this . _getAgent ( ) ;
64
+ if ( agent ) {
65
+ clientBaseOptions . agent = agent ;
76
66
}
77
67
this . options = options ;
78
68
this . got = got . extend (
79
- Object . assign (
80
- {
81
- baseUrl : this . endPoint ,
82
- } ,
83
- defaultRequestOptions ,
84
- requestOptions
85
- )
69
+ Object . assign ( clientBaseOptions , defaultRequestOptions , requestOptions )
86
70
) ;
87
71
this . debugLevel = options . debugLevel || debugLevel ;
88
72
}
89
73
74
+ /**
75
+ * Get the http agent for proxying or requesting
76
+ * @return {* }
77
+ * @private
78
+ */
79
+ _getAgent ( ) {
80
+ let agent ;
81
+ //Simple proxy tunnel
82
+ if ( socksProxy ) {
83
+ const socksProtocol = 'socks:' ;
84
+ agent = new SocksProxyAgent (
85
+ String ( socksProxy ) . startsWith ( socksProtocol )
86
+ ? socksProxy
87
+ : `${ socksProtocol } //${ socksProxy } `
88
+ ) ;
89
+ } else if ( httpProxy ) {
90
+ const parsedUrl = new URL ( httpProxy ) ;
91
+ const tunnelOptions = {
92
+ proxy : {
93
+ host : parsedUrl . hostname ,
94
+ port : parsedUrl . port ,
95
+ } ,
96
+ } ;
97
+ agent =
98
+ this . endPointParsedUrl . protocol === 'https:'
99
+ ? tunnel . httpsOverHttp ( tunnelOptions )
100
+ : tunnel . httpOverHttp ( tunnelOptions ) ;
101
+ }
102
+ return agent ;
103
+ }
104
+
90
105
/**
91
106
* Customize real options for destination
92
107
* @param ctx
@@ -159,9 +174,7 @@ class HttpClient {
159
174
) ;
160
175
} else {
161
176
this . _log (
162
- `[${ gotOptions . method } ][${
163
- gotOptions . href
164
- } ] response body[${ type } ] length: ${ ret . length } `
177
+ `[${ gotOptions . method } ][${ gotOptions . href } ] response body[${ type } ] length: ${ ret . length } `
165
178
) ;
166
179
}
167
180
} ) ;
0 commit comments