1
1
const url = require ( 'url' )
2
+ const { Agent : httpAgent } = require ( 'http' )
3
+ const { Agent : httpsAgent } = require ( 'https' )
2
4
const httpProxy = require ( 'http-proxy' )
3
5
const _ = require ( 'lodash' )
4
6
@@ -42,11 +44,14 @@ function parseProxyConfig (proxies, config) {
42
44
port = config . port
43
45
}
44
46
const changeOrigin = proxyConfiguration . changeOrigin || false
47
+ const Agent = https ? httpsAgent : httpAgent
48
+ const agent = new Agent ( { keepAlive : true } )
45
49
const proxy = httpProxy . createProxyServer ( {
46
50
target : { host : hostname , port, https, protocol } ,
47
51
xfwd : true ,
48
52
changeOrigin : changeOrigin ,
49
- secure : config . proxyValidateSSL
53
+ secure : config . proxyValidateSSL ,
54
+ agent
50
55
} )
51
56
52
57
; [ 'proxyReq' , 'proxyRes' ] . forEach ( function ( name ) {
@@ -66,7 +71,7 @@ function parseProxyConfig (proxies, config) {
66
71
res . destroy ( )
67
72
} )
68
73
69
- return { path : proxyPath , baseUrl : pathname , host : hostname , port, https, proxy }
74
+ return { path : proxyPath , baseUrl : pathname , host : hostname , port, https, proxy, agent }
70
75
} ) , 'path' ) . reverse ( )
71
76
}
72
77
@@ -112,6 +117,14 @@ function createProxyHandler (proxies, urlRoot) {
112
117
return createProxy
113
118
}
114
119
115
- exports . create = function ( /* config */ config , /* config.proxies */ proxies ) {
116
- return createProxyHandler ( parseProxyConfig ( proxies , config ) , config . urlRoot )
120
+ exports . create = function ( /* config */ config , /* config.proxies */ proxies , /* emitter */ emitter ) {
121
+ const proxyRecords = parseProxyConfig ( proxies , config )
122
+ emitter . on ( 'exit' , ( done ) => {
123
+ log . debug ( 'Destroying proxy agents' )
124
+ proxyRecords . forEach ( ( proxyRecord ) => {
125
+ proxyRecord . agent . destroy ( )
126
+ } )
127
+ done ( )
128
+ } )
129
+ return createProxyHandler ( proxyRecords , config . urlRoot )
117
130
}
0 commit comments