Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc74bc83c8 | |||
| 5e81240a05 | |||
| 29a2ad2032 | |||
| 6b544c54a3 | |||
| 734974d279 | |||
| 57ce59c207 |
+15
-3
@@ -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._destroyed = 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'
|
||||
];
|
||||
]
|
||||
|
||||
@@ -21,6 +21,14 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
self.trailers = {}
|
||||
self.rawTrailers = []
|
||||
|
||||
// Fake the 'close' event, but only once 'end' fires
|
||||
self.on('end', function () {
|
||||
// The nextTick is necessary to prevent the 'request' module from causing an infinite loop
|
||||
process.nextTick(function () {
|
||||
self.emit('close')
|
||||
})
|
||||
})
|
||||
|
||||
if (mode === 'fetch') {
|
||||
self._fetchResponse = response
|
||||
|
||||
@@ -37,6 +45,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
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "1.1.1",
|
||||
"version": "1.2.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('abort before response', function (t) {
|
||||
var req = http.get('/basic.txt', function (res) {
|
||||
t.fail('unexpected response')
|
||||
})
|
||||
req.abort()
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('abort on response', function (t) {
|
||||
var req = http.get('/basic.txt', function (res) {
|
||||
req.abort()
|
||||
t.end()
|
||||
|
||||
res.on('end', function () {
|
||||
t.fail('unexpected end')
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
t.fail('unexpected data')
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('abort on data', function (t) {
|
||||
var req = http.get('/browserify.png?copies=5', function (res) {
|
||||
var firstData = true
|
||||
|
||||
res.on('end', function () {
|
||||
t.fail('unexpected end')
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
if (firstData) {
|
||||
firstData = false
|
||||
req.abort()
|
||||
t.end()
|
||||
} else {
|
||||
t.fail('unexpected data')
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user