Compare commits

...

14 Commits

Author SHA1 Message Date
John Hiesey c8c0966c63 1.3.0 2015-07-20 16:10:29 -07:00
John Hiesey efaad2a3c7 Add keywords 2015-07-20 16:07:30 -07:00
John Hiesey 9059849673 Make withCredentials default to false
This may break compatibility with code that was written for
`http-browserify`. Fixes #7
2015-07-20 16:01:24 -07:00
John Hiesey aeabb2cc70 Merge pull request #6 from cesarandreu/consistent-fetch
Fix inconsistency between XMLHttpRequest and fetch
2015-07-20 00:32:46 -07:00
Cesar Andreu 588e5874f3 Add test for cookie behavior 2015-07-19 17:50:30 -07:00
Cesar Andreu 5fc4858d6d Use same-origin instead of omit with fetch credentials
In order to have parity with XMLHttpRequest fetch credentials should use same-origin instead of omit
Setting XMLHttpRequest.withCredentials to false has no effect on same-site request
2015-07-19 17:06:28 -07:00
John Hiesey 22b3cc7acb v1.2.1 2015-07-17 00:50:31 -07:00
John Hiesey 5ffc869490 Fix readme to point to new saucelabs account 2015-07-17 00:35:03 -07:00
John Hiesey cc74bc83c8 v1.2.0 2015-07-13 18:18:13 -07:00
John Hiesey 5e81240a05 add process.nextTick for 'close' 2015-07-13 18:14:30 -07:00
John Hiesey 29a2ad2032 Add abort() test 2015-07-13 16:57:31 -07:00
John Hiesey 6b544c54a3 Fix spelling of 'destroyed' 2015-07-13 16:45:46 -07:00
John Hiesey 734974d279 Improve behavior of request.prototype.abort() and add destroy synonym 2015-07-13 16:37:04 -07:00
John Hiesey 57ce59c207 Re-add close event on response 2015-07-13 16:07:46 -07:00
9 changed files with 117 additions and 10 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
# stream-http [![Build Status](https://travis-ci.org/jhiesey/stream-http.svg?branch=master)](https://travis-ci.org/jhiesey/stream-http)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/jhiesey.svg)](https://saucelabs.com/u/jhiesey)
[![Sauce Test Status](https://saucelabs.com/browser-matrix/stream-http.svg)](https://saucelabs.com/u/stream-http)
This module is an implementation of node's native `http` module for the browser.
It tries to match node's api and behavior as closely as possible, but some features
-3
View File
@@ -25,9 +25,6 @@ http.request = function (opts, cb) {
opts.hostname = opts.hostname || hostHostname || window.location.hostname
opts.port = opts.port || hostPort || defaultPort
if (opts.withCredentials === undefined)
opts.withCredentials = true
// Also valid opts.auth, opts.mode
var req = new ClientRequest(opts)
+16 -4
View File
@@ -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
@@ -117,7 +119,7 @@ ClientRequest.prototype._onFinish = function () {
headers: headers,
body: body,
mode: 'cors',
credentials: opts.withCredentials ? 'include' : 'omit'
credentials: opts.withCredentials ? 'include' : 'same-origin'
}).then(function (response) {
self._fetchResponse = response
self._connect()
@@ -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'
];
]
+10
View File
@@ -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
+9 -1
View File
@@ -1,6 +1,6 @@
{
"name": "stream-http",
"version": "1.1.1",
"version": "1.3.0",
"description": "Streaming http in the browser",
"main": "index.js",
"repository": {
@@ -15,6 +15,13 @@
},
"author": "John Hiesey",
"license": "MIT",
"keywords": [
"http",
"stream",
"streaming",
"xhr",
"http-browserify"
],
"dependencies": {
"builtin-status-codes": "~1.0.0",
"foreach": "^2.0.5",
@@ -26,6 +33,7 @@
"devDependencies": {
"basic-auth": "^1.0.3",
"brfs": "^1.4.0",
"cookie-parser": "^1.3.5",
"express": "^4.13.0",
"tape": "^4.0.0",
"ua-parser-js": "^0.7.7",
+48
View File
@@ -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')
}
})
})
})
+25
View File
@@ -0,0 +1,25 @@
var Buffer = require('buffer').Buffer
var test = require('tape')
var http = require('../..')
test('cookie', function (t) {
var cookie = 'hello=world'
window.document.cookie = cookie
http.get({
path: '/cookie',
withCredentials: false
}, function (res) {
var buffers = []
res.on('end', function () {
t.ok(new Buffer(cookie).equals(Buffer.concat(buffers)), 'hello cookie echoed')
t.end()
})
res.on('data', function (data) {
buffers.push(data)
})
})
})
+1 -1
View File
@@ -85,7 +85,7 @@ test('Test withCredentials param', function(t) {
t.equal( request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
var request = http.get({ url: url }, noop)
t.equal( request._xhr.withCredentials, true, 'xhr.withCredentials should be true')
t.equal( request._xhr.withCredentials, false, 'xhr.withCredentials should be false')
t.end()
})
+7
View File
@@ -1,3 +1,4 @@
var cookieParser = require('cookie-parser')
var basicAuth = require('basic-auth')
var express = require('express')
var fs = require('fs')
@@ -46,6 +47,12 @@ app.get('/testHeaders', function (req, res) {
res.end()
})
app.get('/cookie', cookieParser(), function (req, res) {
res.setHeader('Content-Type', 'text/plain')
res.write('hello=' + req.cookies.hello)
res.end()
})
app.get('/auth', function (req, res) {
var user = basicAuth(req)