Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| cc74bc83c8 | |||
| 5e81240a05 | |||
| 29a2ad2032 | |||
| 6b544c54a3 | |||
| 734974d279 | |||
| 57ce59c207 | |||
| 15181f5586 | |||
| 916ab55402 | |||
| 0f81836459 | |||
| 73f1e6f3f0 | |||
| d700e6d8d9 |
+50
-5
@@ -1,10 +1,11 @@
|
||||
// var Base64 = require('Base64')
|
||||
var capability = require('./capability')
|
||||
var foreach = require('foreach')
|
||||
var indexOf = require('indexof')
|
||||
var inherits = require('inherits')
|
||||
var keys = require('object-keys')
|
||||
var response = require('./response')
|
||||
var stream = require('stream')
|
||||
var inherits = require('inherits')
|
||||
|
||||
var IncomingMessage = response.IncomingMessage
|
||||
var rStates = response.readyStates
|
||||
@@ -63,7 +64,14 @@ inherits(ClientRequest, stream.Writable)
|
||||
|
||||
ClientRequest.prototype.setHeader = function (name, value) {
|
||||
var self = this
|
||||
self._headers[name.toLowerCase()] = {
|
||||
var lowerName = name.toLowerCase()
|
||||
// This check is not necessary, but it prevents warnings from browsers about setting unsafe
|
||||
// headers. To be honest I'm not entirely sure hiding these warnings is a good thing, but
|
||||
// http-browserify did it, so I will too.
|
||||
if (indexOf(unsafeHeaders, lowerName) !== -1)
|
||||
return
|
||||
|
||||
self._headers[lowerName] = {
|
||||
name: name,
|
||||
value: value
|
||||
}
|
||||
@@ -82,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
|
||||
@@ -109,7 +119,7 @@ ClientRequest.prototype._onFinish = function () {
|
||||
headers: headers,
|
||||
body: body,
|
||||
mode: 'cors',
|
||||
credentials: opts.credentials ? 'include' : 'omit'
|
||||
credentials: opts.withCredentials ? 'include' : 'omit'
|
||||
}).then(function (response) {
|
||||
self._fetchResponse = response
|
||||
self._connect()
|
||||
@@ -159,6 +169,8 @@ ClientRequest.prototype._onFinish = function () {
|
||||
}
|
||||
|
||||
xhr.onerror = function () {
|
||||
if (self._destroyed)
|
||||
return
|
||||
self.emit('error', new Error('XHR error'))
|
||||
}
|
||||
|
||||
@@ -188,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)
|
||||
@@ -200,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)
|
||||
}
|
||||
@@ -211,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) {
|
||||
@@ -234,3 +254,28 @@ ClientRequest.prototype.flushHeaders = function () {}
|
||||
ClientRequest.prototype.setTimeout = function () {}
|
||||
ClientRequest.prototype.setNoDelay = function () {}
|
||||
ClientRequest.prototype.setSocketKeepAlive = function () {}
|
||||
|
||||
// Taken from http://www.w3.org/TR/XMLHttpRequest/#the-setrequestheader%28%29-method
|
||||
var unsafeHeaders = [
|
||||
'accept-charset',
|
||||
'accept-encoding',
|
||||
'access-control-request-headers',
|
||||
'access-control-request-method',
|
||||
'connection',
|
||||
'content-length',
|
||||
'cookie',
|
||||
'cookie2',
|
||||
'date',
|
||||
'dnt',
|
||||
'expect',
|
||||
'host',
|
||||
'keep-alive',
|
||||
'origin',
|
||||
'referer',
|
||||
'te',
|
||||
'trailer',
|
||||
'transfer-encoding',
|
||||
'upgrade',
|
||||
'user-agent',
|
||||
'via'
|
||||
]
|
||||
|
||||
+11
-4
@@ -1,7 +1,7 @@
|
||||
var capability = require('./capability')
|
||||
var foreach = require('foreach')
|
||||
var stream = require('stream')
|
||||
var inherits = require('inherits')
|
||||
var stream = require('stream')
|
||||
|
||||
var rStates = exports.readyStates = {
|
||||
UNSENT: 0,
|
||||
@@ -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,9 +45,10 @@ 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)
|
||||
self.emit('close')
|
||||
return
|
||||
}
|
||||
self.push(new Buffer(result.value))
|
||||
@@ -151,7 +160,6 @@ IncomingMessage.prototype._onXHRProgress = function () {
|
||||
}
|
||||
reader.onload = function () {
|
||||
self.push(null)
|
||||
self.emit('close')
|
||||
}
|
||||
// reader.onerror = ??? // TODO: this
|
||||
reader.readAsArrayBuffer(response)
|
||||
@@ -161,6 +169,5 @@ IncomingMessage.prototype._onXHRProgress = function () {
|
||||
// The ms-stream case handles end separately in reader.onload()
|
||||
if (self._xhr.readyState === rStates.DONE && self._mode !== 'ms-stream') {
|
||||
self.push(null)
|
||||
self.emit('close')
|
||||
}
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "1.0.2",
|
||||
"version": "1.2.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
@@ -18,6 +18,7 @@
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "~1.0.0",
|
||||
"foreach": "^2.0.5",
|
||||
"indexof": "0.0.1",
|
||||
"inherits": "^2.0.1",
|
||||
"object-keys": "1.0.4",
|
||||
"xtend": "^4.0.0"
|
||||
|
||||
@@ -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