diff --git a/lib/http-proxy/passes/web-incoming.js b/lib/http-proxy/passes/web-incoming.js index 7ae735514..64da74135 100644 --- a/lib/http-proxy/passes/web-incoming.js +++ b/lib/http-proxy/passes/web-incoming.js @@ -18,6 +18,11 @@ var nativeAgents = { http: httpNative, https: httpsNative }; * flexible. */ +// 'aborted' event stopped working reliably on v15.5.0 and was later removed entirely +var supportsAbortedEvent = (function () { + var ver = process.versions.node.split('.').map(Number); + return ver[0] <= 14 || ver[0] === 15 && ver[1] <= 4; +}()); module.exports = { @@ -143,9 +148,18 @@ module.exports = { } // Ensure we abort proxy if request is aborted - req.on('aborted', function () { - proxyReq.abort(); - }); + if (supportsAbortedEvent) { + req.on('aborted', function () { + proxyReq.abort(); + }); + } else { + res.on('close', function () { + var aborted = !res.writableFinished; + if (aborted) { + proxyReq.abort(); + } + }); + } // handle errors in proxy and incoming request, just like for forward proxy var proxyError = createErrorHandler(proxyReq, options.target);