1+ /**
2+ * --require'd by node before loading any other modules.
3+ * This file sets up a global agent for the http & https modules,
4+ * plus tweaks various other HTTP clients that need nudges, so they
5+ * all correctly pick up the proxy from the environment.
6+ *
7+ * Tested against Node 6, 8, 10 and 12.
8+ */
9+
10+ const wrapModule = require ( './wrap-require' ) ;
11+
12+ let httpAlreadyIntercepted = false ;
13+
14+ function interceptAllHttp ( ) {
15+ if ( httpAlreadyIntercepted ) return ;
16+ httpAlreadyIntercepted = true ;
17+
18+ const MAJOR_NODEJS_VERSION = parseInt ( process . version . slice ( 1 ) . split ( '.' ) [ 0 ] , 10 ) ;
19+
20+ if ( MAJOR_NODEJS_VERSION >= 10 ) {
21+ // `global-agent` works with Node.js v10 and above.
22+ const globalAgent = require ( 'global-agent' ) ;
23+ globalAgent . bootstrap ( ) ;
24+ } else {
25+ // `global-tunnel-ng` works only with Node.js v10 and below.
26+ const globalTunnel = require ( 'global-tunnel-ng' ) ;
27+ globalTunnel . initialize ( ) ;
28+ }
29+ }
30+
31+ wrapModule ( 'http' , interceptAllHttp , true ) ;
32+ wrapModule ( 'https' , interceptAllHttp , true ) ;
33+
34+ wrapModule ( 'axios' , function wrapAxios ( loadedModule ) {
35+ // Global agent handles this automatically, if used (i.e. Node >= 10)
36+ if ( global . GLOBAL_AGENT ) return ;
37+
38+ // Disable built-in proxy support, to let global-tunnel take precedence
39+ // Supported back to the very first release of Axios
40+ loadedModule . defaults . proxy = false ;
41+ } ) ;
42+
43+ wrapModule ( 'request' , function wrapRequest ( loadedModule ) {
44+ // Global agent handles this automatically, if used (i.e. Node >= 10)
45+ if ( global . GLOBAL_AGENT ) return ;
46+
47+ // Is this Request >= 2.17?
48+ // Before then proxy support isn't a problem anyway
49+ if ( ! loadedModule . defaults ) return ;
50+
51+ // Have we intercepted this already?
52+ if ( loadedModule . INTERCEPTED_BY_HTTPTOOLKIT ) return ;
53+
54+ const fixedModule = loadedModule . defaults ( { proxy : false } ) ;
55+ fixedModule . INTERCEPTED_BY_HTTPTOOLKIT = true ;
56+ return fixedModule ;
57+ } ) ;
58+
59+ wrapModule ( 'superagent' , function wrapSuperagent ( loadedModule ) {
60+ // Global agent handles this automatically, if used (i.e. Node >= 10)
61+ if ( global . GLOBAL_AGENT ) return ;
62+
63+ // Have we intercepted this already?
64+ if ( loadedModule . INTERCEPTED_BY_HTTPTOOLKIT ) return ;
65+ loadedModule . INTERCEPTED_BY_HTTPTOOLKIT = true ;
66+
67+ // Global tunnel doesn't successfully reconfigure superagent.
68+ // To fix it, we forcibly override the agent property on every request.
69+ const originalRequestMethod = loadedModule . Request . prototype . request ;
70+ loadedModule . Request . prototype . request = function ( ) {
71+ if ( this . url . indexOf ( 'https:' ) === 0 ) {
72+ this . _agent = require ( 'https' ) . globalAgent ;
73+ } else {
74+ this . _agent = require ( 'http' ) . globalAgent ;
75+ }
76+ return originalRequestMethod . apply ( this , arguments ) ;
77+ } ;
78+ } ) ;
79+
80+ wrapModule ( 'stripe' , function wrapStripe ( loadedModule ) {
81+ if ( loadedModule . INTERCEPTED_BY_HTTPTOOLKIT ) return ;
82+
83+ return Object . assign (
84+ function ( ) {
85+ const result = loadedModule . apply ( this , arguments ) ;
86+
87+ // Set by global-tunnel in Node < 10 (or global-agent in 11.7+)
88+ result . setHttpAgent ( require ( 'https' ) . globalAgent ) ;
89+ return result ;
90+ } ,
91+ loadedModule ,
92+ { INTERCEPTED_BY_HTTPTOOLKIT : true }
93+ ) ;
94+ } ) ;
0 commit comments