Skip to content

Fix 'aborted' detection on Node v15.5.0+ #1559

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions lib/http-proxy/passes/web-incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var supportsAbortedEvent = (function () { var [major, minor] = process.versions.node.split('.').map(Number); return major < 15 || (major === 15 && minor < 5); }());

i think this will be more suitable

var supportsAbortedEvent = (function () {
var ver = process.versions.node.split('.').map(Number);
return ver[0] <= 14 || ver[0] === 15 && ver[1] <= 4;
}());

module.exports = {

Expand Down Expand Up @@ -143,9 +148,18 @@ module.exports = {
}

// Ensure we abort proxy if request is aborted
req.on('aborted', function () {
proxyReq.abort();
});
if (supportsAbortedEvent) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (supportsAbortedEvent) { req.on('aborted', () => proxyReq.abort()); } else { res.on('close', () => { if (!res.writableFinished) proxyReq.abort(); }); }
This would be suitable too i think

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);
Expand Down