From 8b1923d4a0f22bec49ec6eebead499ce0ac7f694 Mon Sep 17 00:00:00 2001 From: niftylettuce Date: Thu, 6 Jan 2022 19:19:56 -0500 Subject: [PATCH] fix: added abort fix logic for all Node versions --- package.json | 6 +++++- src/request-base.js | 26 +++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index ea3598402..69a66c6ba 100644 --- a/package.json +++ b/package.json @@ -16,6 +16,9 @@ "@commitlint/config-conventional" ] }, + "engines": { + "node": ">=6.4.0 <13|>=14" + }, "contributors": [ "Kornel LesiƄski ", "Peter Lyons ", @@ -57,10 +60,11 @@ "express": "^4.17.1", "express-session": "^1.17.2", "fixpack": "^4.0.0", + "get-port": "4.2.0", "husky": "^7.0.4", "lint-staged": "^12.1.2", "marked": "^2.0.0", - "mocha": "3.5.3", + "mocha": "6.2.2", "multer": "^1.4.3", "nyc": "^15.1.0", "remark-cli": "^10.0.1", diff --git a/src/request-base.js b/src/request-base.js index ff355ae73..31bd42b5e 100644 --- a/src/request-base.js +++ b/src/request-base.js @@ -1,3 +1,5 @@ +const semver = require('semver'); + /** * Module of mixed-in functions shared between node and client code */ @@ -496,7 +498,29 @@ RequestBase.prototype.abort = function () { this._aborted = true; if (this.xhr) this.xhr.abort(); // browser - if (this.req) this.req.abort(); // node + if (this.req) { + // Node v13 has major differences in `abort()` + // https://github.com/nodejs/node/blob/v12.x/lib/internal/streams/end-of-stream.js + // https://github.com/nodejs/node/blob/v13.x/lib/internal/streams/end-of-stream.js + // https://github.com/nodejs/node/blob/v14.x/lib/internal/streams/end-of-stream.js + // (if you run a diff across these you will see the differences) + // + // References: + // + // + // + // Thanks to @shadowgate15 and @niftylettuce + if (semver.gte(process.version, 'v13.0.0') && semver.lt(process.version, 'v14.0.0')) { + // Note that the reason this doesn't work is because in v13 as compared to v14 + // there is no `callback = nop` set in end-of-stream.js above + throw new Error('Superagent does not work in v13 properly with abort() due to Node.js core changes'); + } else if (semver.gte(process.version, 'v14.0.0')) { + // We have to manually set `destroyed` to `true` in order for this to work + // (see core internals of end-of-stream.js above in v14 branch as compared to v12) + this.req.destroyed = true; + } + this.req.abort(); // node + } this.clearTimeout(); this.emit('abort'); return this;