mirror of
https://github.com/http-party/node-http-proxy.git
synced 2025-12-08 20:59:18 +00:00
[refactor] Improved event handler cleanup
This commit is contained in:
parent
9e630daf81
commit
9f923325d0
@ -236,24 +236,46 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
|
|||||||
|
|
||||||
res.on('drain', ondrain);
|
res.on('drain', ondrain);
|
||||||
|
|
||||||
//
|
function onend() {
|
||||||
// When the `reverseProxy` `response` ends, end the
|
cleanup();
|
||||||
// corresponding outgoing `res` unless we have entered
|
res.end();
|
||||||
// an error state. In which case, assume `res.end()` has
|
|
||||||
// already been called and the 'error' event listener
|
|
||||||
// removed.
|
|
||||||
//
|
|
||||||
response.on('end', function () {
|
|
||||||
if (!errState) {
|
|
||||||
reverseProxy.removeListener('error', proxyError);
|
|
||||||
|
|
||||||
try { res.end() }
|
|
||||||
catch (ex) { console.error("res.end error: %s", ex.message) }
|
|
||||||
|
|
||||||
// Emit the `end` event now that we have completed proxying
|
|
||||||
self.emit('end', req, res);
|
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
function onclose() {
|
||||||
|
cleanup();
|
||||||
|
res.destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
response.on('close', onclose);
|
||||||
|
response.on('end', onend);
|
||||||
|
|
||||||
|
response.on('error', proxyError);
|
||||||
|
res.on('error', proxyError);
|
||||||
|
|
||||||
|
|
||||||
|
function cleanup() {
|
||||||
|
response.removeListener('data', ondata);
|
||||||
|
res.removeListener('drain', ondrain);
|
||||||
|
|
||||||
|
response.removeListener('end', onend);
|
||||||
|
response.removeListener('close', onclose);
|
||||||
|
|
||||||
|
response.removeListener('error', proxyError);
|
||||||
|
res.removeListener('error', proxyError);
|
||||||
|
|
||||||
|
response.removeListener('end', cleanup);
|
||||||
|
response.removeListener('close', cleanup);
|
||||||
|
|
||||||
|
res.removeListener('end', cleanup);
|
||||||
|
res.removeListener('close', cleanup);
|
||||||
|
}
|
||||||
|
|
||||||
|
response.on('end', cleanup);
|
||||||
|
response.on('close', cleanup);
|
||||||
|
|
||||||
|
res.on('end', cleanup);
|
||||||
|
res.on('close', cleanup);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
//
|
//
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user