mirror of
https://github.com/d0zingcat/cors-anywhere.git
synced 2026-05-13 15:09:25 +00:00
Fix memory leak
This commit is contained in:
@@ -142,11 +142,27 @@ function onProxyResponse(req, res, response) {
|
||||
|
||||
req.method = 'GET';
|
||||
requestState.location = parseURL(locationHeader);
|
||||
|
||||
// ### Dispose the current proxied request
|
||||
// Verified assumption: When proxy.proxyRequest is called for the first time,
|
||||
// there are no event listeners on the "req" object.
|
||||
|
||||
// First remove the "end" event, to avoid the req.end() call by node-http-proxy/http-proxy
|
||||
// https://github.com/nodejitsu/node-http-proxy/blob/ebbba73e/lib/node-http-proxy/http-proxy.js#L310-319
|
||||
response.removeAllListeners('end');
|
||||
// Trigger disposal of the reverseProxy
|
||||
// https://github.com/nodejitsu/node-http-proxy/blob/ebbba73e/lib/node-http-proxy/http-proxy.js#L375-L378
|
||||
req.emit('aborted');
|
||||
// Remove all listeners (=events reset to initial state)
|
||||
req.removeAllListeners();
|
||||
|
||||
// Initiate a new proxy request.
|
||||
proxyRequest(req, res, proxy);
|
||||
// Trigger reverseProxy.end() to initiate the proxy
|
||||
// The event listener is added at the end of HttpProxy.prototype.proxyRequest, synchronously.
|
||||
// https://github.com/nodejitsu/node-http-proxy/blob/ebbba73e/lib/node-http-proxy/http-proxy.js#L407-L415
|
||||
req.emit('end');
|
||||
|
||||
response.end();
|
||||
// The proxyResponse event is wrapped in a try-catch, throwing an error
|
||||
// prevents the response from being passed to the client.
|
||||
throw new Error('Prevent current response from being passed through.');
|
||||
|
||||
Reference in New Issue
Block a user