[Pkg-javascript-commits] [node-stream-http] 60/208: Improve behavior of request.prototype.abort() and add destroy synonym

Bastien Roucariès rouca at moszumanska.debian.org
Sun Aug 13 13:39:29 UTC 2017


This is an automated email from the git hooks/post-receive script.

rouca pushed a commit to branch master
in repository node-stream-http.

commit 734974d279291149d444b184a7f8518b221b49fd
Author: John Hiesey <john at hiesey.com>
Date:   Mon Jul 13 16:37:04 2015 -0700

    Improve behavior of request.prototype.abort() and add destroy synonym
---
 lib/request.js  | 18 +++++++++++++++---
 lib/response.js |  2 ++
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/lib/request.js b/lib/request.js
index 8bb5463..efb86f2 100644
--- a/lib/request.js
+++ b/lib/request.js
@@ -90,6 +90,8 @@ ClientRequest.prototype.removeHeader = function (name) {
 ClientRequest.prototype._onFinish = function () {
 	var self = this
 
+	if (self._destroyed)
+		return
 	var opts = self._opts
 
 	var headersObj = self._headers
@@ -167,6 +169,8 @@ ClientRequest.prototype._onFinish = function () {
 		}
 
 		xhr.onerror = function () {
+			if (self._destroyed)
+				return
 			self.emit('error', new Error('XHR error'))
 		}
 
@@ -196,7 +200,7 @@ function statusValid (xhr) {
 ClientRequest.prototype._onXHRProgress = function () {
 	var self = this
 
-	if (!statusValid(self._xhr) || self._failed)
+	if (!statusValid(self._xhr) || self._destroyed)
 		return
 
 	if (!self._response)
@@ -208,6 +212,9 @@ ClientRequest.prototype._onXHRProgress = function () {
 ClientRequest.prototype._connect = function () {
 	var self = this
 
+	if (self._destroyed)
+		return
+
 	self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode)
 	self.emit('response', self._response)
 }
@@ -219,10 +226,15 @@ ClientRequest.prototype._write = function (chunk, encoding, cb) {
 	cb()
 }
 
-ClientRequest.prototype.abort = function () {
+ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function () {
 	var self = this
+	self._destoryed = true
+	if (self._response)
+		self._response._destroyed = true
 	if (self._xhr)
 		self._xhr.abort()
+	// Currently, there isn't a way to truly abort a fetch.
+	// If you like bikeshedding, see https://github.com/whatwg/fetch/issues/27
 }
 
 ClientRequest.prototype.end = function (data, encoding, cb) {
@@ -266,4 +278,4 @@ var unsafeHeaders = [
 	'upgrade',
 	'user-agent',
 	'via'
-];
+]
diff --git a/lib/response.js b/lib/response.js
index 6bec418..96f5bc9 100644
--- a/lib/response.js
+++ b/lib/response.js
@@ -42,6 +42,8 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
 		var reader = response.body.getReader()
 		function read () {
 			reader.read().then(function (result) {
+				if (self._destroyed)
+					return
 				if (result.done) {
 					self.push(null)
 					return

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-stream-http.git



More information about the Pkg-javascript-commits mailing list