@@ -19,55 +19,35 @@ function defaultProtocolPort (protocol) {
19
19
return protocol === 'https:' ? 443 : 80
20
20
}
21
21
22
- function buildProxyOptions ( opts ) {
23
- if ( typeof opts === 'string' ) {
24
- opts = { uri : opts }
25
- }
26
-
27
- if ( ! opts || ! opts . uri ) {
28
- throw new InvalidArgumentError ( 'Proxy opts.uri is mandatory' )
29
- }
30
-
31
- return {
32
- uri : opts . uri ,
33
- protocol : opts . protocol || 'https'
34
- }
35
- }
36
-
37
22
function defaultFactory ( origin , opts ) {
38
23
return new Pool ( origin , opts )
39
24
}
40
25
41
26
class ProxyAgent extends DispatcherBase {
42
27
constructor ( opts ) {
43
- super ( opts )
44
- this [ kProxy ] = buildProxyOptions ( opts )
45
- this [ kAgent ] = new Agent ( opts )
46
- this [ kInterceptors ] = opts . interceptors ?. ProxyAgent && Array . isArray ( opts . interceptors . ProxyAgent )
47
- ? opts . interceptors . ProxyAgent
48
- : [ ]
28
+ super ( )
49
29
50
- if ( typeof opts === 'string' ) {
51
- opts = { uri : opts }
52
- }
53
-
54
- if ( ! opts || ! opts . uri ) {
55
- throw new InvalidArgumentError ( 'Proxy opts.uri is mandatory' )
30
+ if ( ! opts || ( typeof opts === 'object' && ! ( opts instanceof URL ) && ! opts . uri ) ) {
31
+ throw new InvalidArgumentError ( 'Proxy uri is mandatory' )
56
32
}
57
33
58
34
const { clientFactory = defaultFactory } = opts
59
-
60
35
if ( typeof clientFactory !== 'function' ) {
61
36
throw new InvalidArgumentError ( 'Proxy opts.clientFactory must be a function.' )
62
37
}
63
38
39
+ const url = this . #getUrl( opts )
40
+ const { href, origin, port, protocol, username, password } = url
41
+
42
+ this [ kProxy ] = { uri : href , protocol }
43
+ this [ kAgent ] = new Agent ( opts )
44
+ this [ kInterceptors ] = opts . interceptors ?. ProxyAgent && Array . isArray ( opts . interceptors . ProxyAgent )
45
+ ? opts . interceptors . ProxyAgent
46
+ : [ ]
64
47
this [ kRequestTls ] = opts . requestTls
65
48
this [ kProxyTls ] = opts . proxyTls
66
49
this [ kProxyHeaders ] = opts . headers || { }
67
50
68
- const resolvedUrl = new URL ( opts . uri )
69
- const { origin, port, username, password } = resolvedUrl
70
-
71
51
if ( opts . auth && opts . token ) {
72
52
throw new InvalidArgumentError ( 'opts.auth cannot be used in combination with opts.token' )
73
53
} else if ( opts . auth ) {
@@ -81,7 +61,7 @@ class ProxyAgent extends DispatcherBase {
81
61
82
62
const connect = buildConnector ( { ...opts . proxyTls } )
83
63
this [ kConnectEndpoint ] = buildConnector ( { ...opts . requestTls } )
84
- this [ kClient ] = clientFactory ( resolvedUrl , { connect } )
64
+ this [ kClient ] = clientFactory ( url , { connect } )
85
65
this [ kAgent ] = new Agent ( {
86
66
...opts ,
87
67
connect : async ( opts , callback ) => {
@@ -138,6 +118,20 @@ class ProxyAgent extends DispatcherBase {
138
118
)
139
119
}
140
120
121
+ /**
122
+ * @param {import('../types/proxy-agent').ProxyAgent.Options | string | URL } opts
123
+ * @returns {URL }
124
+ */
125
+ #getUrl ( opts ) {
126
+ if ( typeof opts === 'string' ) {
127
+ return new URL ( opts )
128
+ } else if ( opts instanceof URL ) {
129
+ return opts
130
+ } else {
131
+ return new URL ( opts . uri )
132
+ }
133
+ }
134
+
141
135
async [ kClose ] ( ) {
142
136
await this [ kAgent ] . close ( )
143
137
await this [ kClient ] . close ( )
0 commit comments