@@ -16,10 +16,39 @@ const { codes } = require('./SDKErrors')
16
16
const HttpProxyAgent = require ( 'http-proxy-agent' )
17
17
const HttpsProxyAgent = require ( 'https-proxy-agent' )
18
18
const { urlToHttpOptions } = require ( './utils' )
19
- const http = require ( 'http' )
20
19
21
20
/* global Response, Request */
22
21
22
+ /**
23
+ * @private
24
+ *
25
+ * @param {string } resourceUrl an endpoint url for proxyAgent selection
26
+ * @param {object } authOptions an object which contains auth information
27
+ * @returns {http.Agent } a http.Agent for basic auth proxy
28
+ */
29
+ function proxyAgent ( resourceUrl , authOptions ) {
30
+ const { proxyUrl, username, password, rejectUnauthorized = true } = authOptions
31
+ const proxyOpts = urlToHttpOptions ( proxyUrl )
32
+
33
+ if ( ! proxyOpts . auth && username && password ) {
34
+ logger . debug ( 'username and password not set in proxy url, using credentials passed in the constructor.' )
35
+ proxyOpts . auth = `${ username } :${ password } `
36
+ }
37
+
38
+ // the passing on of this property to the underlying implementation only works on https-proxy-agent@2.2.4
39
+ // this is only used for unit-tests and passed in the constructor
40
+ proxyOpts . rejectUnauthorized = rejectUnauthorized
41
+ if ( rejectUnauthorized === false ) {
42
+ logger . warn ( `proxyAgent - rejectUnauthorized is set to ${ rejectUnauthorized } ` )
43
+ }
44
+
45
+ if ( resourceUrl . startsWith ( 'https' ) ) {
46
+ return new HttpsProxyAgent ( proxyOpts )
47
+ } else {
48
+ return new HttpProxyAgent ( proxyOpts )
49
+ }
50
+ }
51
+
23
52
/**
24
53
* Proxy Auth Options
25
54
*
@@ -55,34 +84,6 @@ class ProxyFetch {
55
84
return this
56
85
}
57
86
58
- /**
59
- * Returns the http.Agent used for this proxy
60
- *
61
- * @returns {http.Agent } a http.Agent for basic auth proxy
62
- */
63
- proxyAgent ( ) {
64
- const { proxyUrl, username, password, rejectUnauthorized = true } = this . authOptions
65
- const proxyOpts = urlToHttpOptions ( proxyUrl )
66
-
67
- if ( ! proxyOpts . auth && username && password ) {
68
- logger . debug ( 'username and password not set in proxy url, using credentials passed in the constructor.' )
69
- proxyOpts . auth = `${ username } :${ password } `
70
- }
71
-
72
- // the passing on of this property to the underlying implementation only works on https-proxy-agent@2.2.4
73
- // this is only used for unit-tests and passed in the constructor
74
- proxyOpts . rejectUnauthorized = rejectUnauthorized
75
- if ( rejectUnauthorized === false ) {
76
- logger . warn ( `proxyAgent - rejectUnauthorized is set to ${ rejectUnauthorized } ` )
77
- }
78
-
79
- if ( proxyOpts . protocol . startsWith ( 'https' ) ) {
80
- return new HttpsProxyAgent ( proxyOpts )
81
- } else {
82
- return new HttpProxyAgent ( proxyOpts )
83
- }
84
- }
85
-
86
87
/**
87
88
* Fetch function, using the configured NTLM Auth options.
88
89
*
@@ -93,7 +94,7 @@ class ProxyFetch {
93
94
async fetch ( resource , options = { } ) {
94
95
return originalFetch ( resource , {
95
96
...options ,
96
- agent : this . proxyAgent ( )
97
+ agent : proxyAgent ( resource , this . authOptions )
97
98
} )
98
99
}
99
100
}
0 commit comments