@@ -26,6 +26,10 @@ function interceptAllHttp() {
2626 }
2727}
2828
29+ // Grab the built-in module loader that we're going to intercept
30+ const mod = require ( 'module' ) ;
31+ const realLoad = mod . _load ;
32+
2933// Intercept calls to require() certain modules, to monkey-patch/wrap them.
3034
3135// Primarily exists to intercept http/https, but could be used for any
@@ -44,25 +48,57 @@ const alreadyIntercepted = [];
4448// requires that can cause problems here.
4549let delayedInterception = false ;
4650
47- const mod = require ( 'module' ) ;
48- const realLoad = mod . _load ;
49-
5051// Given a just-loaded module's name, do whatever is required to set up interception
51- function fixModule ( name ) {
52+ // This must return the resulting module, but due to delayed interception, that isn't
53+ // used if it's hit in a circular require. Works for now, but needs careful thought.
54+ function fixModule ( name , loadedModule ) {
5255 if ( name === 'http' || name === 'https' ) {
5356 delayedInterception = [ ] ;
5457
5558 interceptAllHttp ( ) ;
5659
5760 delayedInterception . forEach ( function ( modDetails ) {
58- fixModule ( modDetails . name ) ;
61+ fixModule ( modDetails . name , modDetails . loadedModule ) ;
5962 } ) ;
63+ delayedInterception = false ;
64+
65+ return loadedModule ;
66+ } else if ( name === 'axios' ) {
67+ // Disable built-in proxy support, to let global-agent/tunnel take precedence
68+ // Supported back to the very first release of Axios
69+ loadedModule . defaults . proxy = false ;
70+ return loadedModule ;
71+ } else if ( name === 'request' ) {
72+ // Disable built-in proxy support, to let global-agent/tunnel take precedence
73+ if ( loadedModule . defaults ) {
74+ return loadedModule . defaults ( { proxy : false } ) ;
75+ } else {
76+ // Request < 2.17 (_very_ old). Predates the proxy support that makes
77+ // this necessary in the first place (added in 2.38).
78+ return loadedModule ;
79+ }
80+ } else if ( name === 'stripe' ) {
81+ stripeReplacement = Object . assign ( function ( ) {
82+ const result = loadedModule . apply ( this , arguments ) ;
83+
84+ if ( global . GLOBAL_AGENT ) {
85+ // Set by global-agent in Node 10+
86+ result . setHttpAgent ( global . GLOBAL_AGENT . HTTPS_PROXY_AGENT ) ;
87+ } else {
88+ // Set by global-tunnel in Node < 10 (or global-agent in 11.7+)
89+ result . setHttpAgent ( require ( 'https' ) . globalAgent ) ;
90+ }
91+
92+ return result ;
93+ } , loadedModule ) ;
94+ return stripeReplacement ;
6095 }
96+ else return loadedModule ;
6197}
6298
6399// Our hook into require():
64100mod . _load = function ( name ) {
65- const loadedModule = realLoad . apply ( this , arguments ) ;
101+ let loadedModule = realLoad . apply ( this , arguments ) ;
66102
67103 // Should always be set, but check just in case. This also allows users to disable
68104 // interception explicitly, if need be.
@@ -75,7 +111,7 @@ mod._load = function (name) {
75111 if ( delayedInterception !== false ) {
76112 delayedInterception . push ( { name : name , loadedModule : loadedModule } ) ;
77113 } else {
78- fixModule ( name ) ;
114+ loadedModule = fixModule ( name , loadedModule ) ;
79115 }
80116
81117 return loadedModule ;
0 commit comments