@@ -3,7 +3,7 @@ import * as https from 'https';
3
3
import LRUCache from 'lru-cache' ;
4
4
import { Agent , AgentConnectOpts } from 'agent-base' ;
5
5
import createDebug from 'debug' ;
6
- import { getProxyForUrl } from 'proxy-from-env' ;
6
+ import { getProxyForUrl as envGetProxyForUrl } from 'proxy-from-env' ;
7
7
import { PacProxyAgent , PacProxyAgentOptions } from 'pac-proxy-agent' ;
8
8
import { HttpProxyAgent , HttpProxyAgentOptions } from 'http-proxy-agent' ;
9
9
import { HttpsProxyAgent , HttpsProxyAgentOptions } from 'https-proxy-agent' ;
@@ -21,6 +21,8 @@ type ValidProtocol = (typeof PROTOCOLS)[number];
21
21
22
22
type AgentConstructor = new ( ...args : never [ ] ) => Agent ;
23
23
24
+ type GetProxyForUrlCallback = ( url : string ) => string ;
25
+
24
26
/**
25
27
* Supported proxy types.
26
28
*/
@@ -61,6 +63,12 @@ export type ProxyAgentOptions = HttpProxyAgentOptions<''> &
61
63
* instance with the proxy agent options passed in.
62
64
*/
63
65
httpsAgent ?: http . Agent ;
66
+ /**
67
+ * A callback for dynamic provision of proxy for url.
68
+ * Defaults to standard proxy environment variables,
69
+ * see https://www.npmjs.com/package/proxy-from-env for details
70
+ */
71
+ getProxyForUrl ?: GetProxyForUrlCallback ;
64
72
} ;
65
73
66
74
/**
@@ -79,6 +87,7 @@ export class ProxyAgent extends Agent {
79
87
connectOpts ?: ProxyAgentOptions ;
80
88
httpAgent : http . Agent ;
81
89
httpsAgent : http . Agent ;
90
+ getProxyForUrl : GetProxyForUrlCallback ;
82
91
83
92
constructor ( opts ?: ProxyAgentOptions ) {
84
93
super ( opts ) ;
@@ -87,6 +96,7 @@ export class ProxyAgent extends Agent {
87
96
this . httpAgent = opts ?. httpAgent || new http . Agent ( opts ) ;
88
97
this . httpsAgent =
89
98
opts ?. httpsAgent || new https . Agent ( opts as https . AgentOptions ) ;
99
+ this . getProxyForUrl = opts ?. getProxyForUrl || envGetProxyForUrl ;
90
100
}
91
101
92
102
async connect (
@@ -97,7 +107,7 @@ export class ProxyAgent extends Agent {
97
107
const protocol = secureEndpoint ? 'https:' : 'http:' ;
98
108
const host = req . getHeader ( 'host' ) ;
99
109
const url = new URL ( req . path , `${ protocol } //${ host } ` ) . href ;
100
- const proxy = getProxyForUrl ( url ) ;
110
+ const proxy = this . getProxyForUrl ( url ) ;
101
111
102
112
if ( ! proxy ) {
103
113
debug ( 'Proxy not enabled for URL: %o' , url ) ;
0 commit comments