Compare commits
37 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 92500c71be | |||
| 05c6f4394c | |||
| 9ed7614aed | |||
| bad84ecb3a | |||
| 70b928bbb9 | |||
| 53bf1d38e2 | |||
| ac250a9b04 | |||
| 83f7b7ef32 | |||
| f3df5d7ace | |||
| 345149d708 | |||
| 9c18855feb | |||
| ff601a9eff | |||
| e046049394 | |||
| 333b0437a9 | |||
| 0552e1e4f5 | |||
| 1fbfc4264a | |||
| a0c6d49ba4 | |||
| 1d3fca03fa | |||
| 5e25ed2f65 | |||
| b339f4afa9 | |||
| 4b1419cff7 | |||
| c9183cd2ad | |||
| 0f1f0c6365 | |||
| 581a4d12da | |||
| 945083bd1d | |||
| 2054c18374 | |||
| 218fd54ef0 | |||
| 9d24d5510a | |||
| ff68130d89 | |||
| 6bab67d17a | |||
| 6433ea8cad | |||
| 9ebf801ebb | |||
| 154f6b9e56 | |||
| 6dea87e704 | |||
| 8b9f5a43c1 | |||
| 3c8b44379c | |||
| c36da3bade |
+1
-1
@@ -1,3 +1,3 @@
|
||||
language: node_js
|
||||
node_js:
|
||||
- "4.1"
|
||||
- "6.4"
|
||||
@@ -6,15 +6,17 @@ browsers:
|
||||
version: 34..latest
|
||||
- name: safari
|
||||
version: 5..latest
|
||||
- name: microsoftedge
|
||||
version: 13..latest
|
||||
- name: ie
|
||||
version: 9..latest
|
||||
- name: opera
|
||||
version: 11..latest
|
||||
- name: iphone
|
||||
version: 5.1..latest
|
||||
version: '8.1..latest'
|
||||
- name: android
|
||||
version: 4.0..latest
|
||||
version: '4.0..latest'
|
||||
server: ./test/server/index.js
|
||||
scripts:
|
||||
- "/ie8-polyfill.js"
|
||||
- "/test-polyfill.js"
|
||||
- "/test-polyfill.js"
|
||||
|
||||
@@ -48,6 +48,10 @@ node docs for how these work.
|
||||
|
||||
### Extra features compared to node
|
||||
|
||||
* The `message.url` property provides access to the final url after all redirects. This
|
||||
is useful since the browser follows all redirects silently, unlike node. It is available
|
||||
in Chrome 37 and newer, Firefox 32 and newer, and Safari 9 and newer.
|
||||
|
||||
* The `options.withCredentials` boolean flag, used to indicate if the browser should send
|
||||
cookies or authentication information with a CORS request. Default false.
|
||||
|
||||
@@ -71,6 +75,8 @@ also be a bit slow. This was the default in versions of this module before 1.5.
|
||||
that isn't a single-byte ASCII or utf8 character) will be corrupted. Of course, this option
|
||||
is only safe for text data. May also cause the 'content-type' response header to be
|
||||
incorrectly reported (as 'text/plain; charset=x-user-defined').
|
||||
* 'disable-fetch': Force the use of plain XHR regardless of the browser declaring a fetch
|
||||
capability. Preserves the correctness of binary data and the 'content-type' response header.
|
||||
* 'prefer-fast': Deprecated; now a synonym for 'default', which has the same performance
|
||||
characteristics as this mode did in versions before 1.5.
|
||||
|
||||
@@ -81,11 +87,13 @@ characteristics as this mode did in versions before 1.5.
|
||||
* Any operations, including `request.setTimeout`, that operate directly on the underlying
|
||||
socket.
|
||||
* Any options that are disallowed for security reasons. This includes setting or getting
|
||||
certian headers.
|
||||
certain headers.
|
||||
* `message.httpVersion`
|
||||
* `message.rawHeaders` is modified by the browser, and may not quite match what is sent by
|
||||
the server.
|
||||
* `message.trailers` and `message.rawTrailers` will remain empty.
|
||||
* Redirects are followed silently by the browser, so it isn't possible to access the 301/302
|
||||
redirect pages.
|
||||
|
||||
## Example
|
||||
|
||||
@@ -112,7 +120,7 @@ that run in the browser (found in `test/browser`). Normally the browser tests ru
|
||||
|
||||
Running `npm test` will run both sets of tests, but in order for the Sauce Labs tests to run
|
||||
you will need to sign up for an account (free for open source projects) and put the
|
||||
credentials in [a `.zuulrc` file](https://github.com/defunctzombie/zuul/wiki/zuulrc).
|
||||
credentials in a [`.zuulrc` file](https://github.com/defunctzombie/zuul/wiki/zuulrc).
|
||||
|
||||
To run just the node tests, run `npm run test-node`.
|
||||
|
||||
|
||||
@@ -11,7 +11,12 @@ http.request = function (opts, cb) {
|
||||
else
|
||||
opts = extend(opts)
|
||||
|
||||
var protocol = opts.protocol || ''
|
||||
// Normally, the page is loaded from http or https, so not specifying a protocol
|
||||
// will result in a (valid) protocol-relative url. However, this won't work if
|
||||
// the protocol is something else, like 'file:'
|
||||
var defaultProtocol = global.location.protocol.search(/^https?:$/) === -1 ? 'http:' : ''
|
||||
|
||||
var protocol = opts.protocol || defaultProtocol
|
||||
var host = opts.hostname || opts.host
|
||||
var port = opts.port
|
||||
var path = opts.path || '/'
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableByteStream)
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
|
||||
|
||||
exports.blobConstructor = false
|
||||
try {
|
||||
|
||||
+12
-7
@@ -1,14 +1,14 @@
|
||||
// var Base64 = require('Base64')
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var response = require('./response')
|
||||
var stream = require('stream')
|
||||
var stream = require('readable-stream')
|
||||
var toArrayBuffer = require('to-arraybuffer')
|
||||
|
||||
var IncomingMessage = response.IncomingMessage
|
||||
var rStates = response.readyStates
|
||||
|
||||
function decideMode (preferBinary) {
|
||||
if (capability.fetch) {
|
||||
function decideMode (preferBinary, useFetch) {
|
||||
if (capability.fetch && useFetch) {
|
||||
return 'fetch'
|
||||
} else if (capability.mozchunkedarraybuffer) {
|
||||
return 'moz-chunked-arraybuffer'
|
||||
@@ -37,7 +37,12 @@ var ClientRequest = module.exports = function (opts) {
|
||||
})
|
||||
|
||||
var preferBinary
|
||||
if (opts.mode === 'prefer-streaming') {
|
||||
var useFetch = true
|
||||
if (opts.mode === 'disable-fetch') {
|
||||
// If the use of XHR should be preferred and includes preserving the 'content-type' header
|
||||
useFetch = false
|
||||
preferBinary = true
|
||||
} else if (opts.mode === 'prefer-streaming') {
|
||||
// If streaming is a high priority but binary compatibility and
|
||||
// the accuracy of the 'content-type' header aren't
|
||||
preferBinary = false
|
||||
@@ -50,7 +55,7 @@ var ClientRequest = module.exports = function (opts) {
|
||||
} else {
|
||||
throw new Error('Invalid value for opts.mode')
|
||||
}
|
||||
self._mode = decideMode(preferBinary)
|
||||
self._mode = decideMode(preferBinary, useFetch)
|
||||
|
||||
self.on('finish', function () {
|
||||
self._onFinish()
|
||||
@@ -96,7 +101,7 @@ ClientRequest.prototype._onFinish = function () {
|
||||
if (opts.method === 'POST' || opts.method === 'PUT' || opts.method === 'PATCH') {
|
||||
if (capability.blobConstructor) {
|
||||
body = new global.Blob(self._body.map(function (buffer) {
|
||||
return buffer.toArrayBuffer()
|
||||
return toArrayBuffer(buffer)
|
||||
}), {
|
||||
type: (headersObj['content-type'] || {}).value || ''
|
||||
})
|
||||
|
||||
+12
-4
@@ -1,6 +1,6 @@
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var stream = require('stream')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var rStates = exports.readyStates = {
|
||||
UNSENT: 0,
|
||||
@@ -31,6 +31,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
if (mode === 'fetch') {
|
||||
self._fetchResponse = response
|
||||
|
||||
self.url = response.url
|
||||
self.statusCode = response.status
|
||||
self.statusMessage = response.statusText
|
||||
// backwards compatible version of for (<item> of <iterable>):
|
||||
@@ -60,6 +61,7 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
self._xhr = xhr
|
||||
self._pos = 0
|
||||
|
||||
self.url = xhr.responseURL
|
||||
self.statusCode = xhr.status
|
||||
self.statusMessage = xhr.statusText
|
||||
var headers = xhr.getAllResponseHeaders().split(/\r?\n/)
|
||||
@@ -67,10 +69,16 @@ var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode) {
|
||||
var matches = header.match(/^([^:]+):\s*(.*)/)
|
||||
if (matches) {
|
||||
var key = matches[1].toLowerCase()
|
||||
if (self.headers[key] !== undefined)
|
||||
if (key === 'set-cookie') {
|
||||
if (self.headers[key] === undefined) {
|
||||
self.headers[key] = []
|
||||
}
|
||||
self.headers[key].push(matches[2])
|
||||
} else if (self.headers[key] !== undefined) {
|
||||
self.headers[key] += ', ' + matches[2]
|
||||
else
|
||||
} else {
|
||||
self.headers[key] = matches[2]
|
||||
}
|
||||
self.rawHeaders.push(matches[1], matches[2])
|
||||
}
|
||||
})
|
||||
@@ -135,7 +143,7 @@ IncomingMessage.prototype._onXHRProgress = function () {
|
||||
}
|
||||
break
|
||||
case 'arraybuffer':
|
||||
if (xhr.readyState !== rStates.DONE)
|
||||
if (xhr.readyState !== rStates.DONE || !xhr.response)
|
||||
break
|
||||
response = xhr.response
|
||||
self.push(new Buffer(new Uint8Array(response)))
|
||||
|
||||
+9
-3
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "2.0.4",
|
||||
"version": "2.4.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
@@ -15,6 +15,10 @@
|
||||
},
|
||||
"author": "John Hiesey",
|
||||
"license": "MIT",
|
||||
"bugs": {
|
||||
"url": "https://github.com/jhiesey/stream-http/issues"
|
||||
},
|
||||
"homepage": "https://github.com/jhiesey/stream-http#readme",
|
||||
"keywords": [
|
||||
"http",
|
||||
"stream",
|
||||
@@ -23,8 +27,10 @@
|
||||
"http-browserify"
|
||||
],
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "^1.0.0",
|
||||
"builtin-status-codes": "^2.0.0",
|
||||
"inherits": "^2.0.1",
|
||||
"readable-stream": "^2.1.0",
|
||||
"to-arraybuffer": "^1.0.0",
|
||||
"xtend": "^4.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
@@ -35,6 +41,6 @@
|
||||
"tape": "^4.0.0",
|
||||
"ua-parser-js": "^0.7.7",
|
||||
"webworkify": "^1.0.2",
|
||||
"zuul": "^3.1.0"
|
||||
"zuul": "^3.10.3"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var test = require('tape')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
test('disable fetch', function (t) {
|
||||
var originalFetch
|
||||
if (typeof fetch === 'function') {
|
||||
originalFetch = fetch
|
||||
}
|
||||
|
||||
var fetchCalled = false
|
||||
fetch = function (input, options) {
|
||||
fetchCalled = true
|
||||
if (originalFetch) {
|
||||
return originalFetch(input, options)
|
||||
}
|
||||
}
|
||||
|
||||
http.get({
|
||||
path: '/browserify.png',
|
||||
mode: 'disable-fetch'
|
||||
}, function (res) {
|
||||
t.ok(!fetchCalled, 'fetch was not called')
|
||||
|
||||
if (originalFetch) {
|
||||
fetch = originalFetch
|
||||
}
|
||||
|
||||
res.on('end', function () {
|
||||
t.ok(res.headers['content-type'] === 'image/png', 'content-type was set correctly')
|
||||
t.end()
|
||||
})
|
||||
|
||||
res.on('data', function () {})
|
||||
})
|
||||
})
|
||||
@@ -9,7 +9,7 @@ module.exports = function (self) {
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
self.postMessage(Buffer.concat(buffers).toArrayBuffer())
|
||||
self.postMessage(Buffer.concat(buffers).buffer)
|
||||
})
|
||||
|
||||
res.on('data', function (data) {
|
||||
|
||||
@@ -1,13 +1,35 @@
|
||||
var Buffer = require('buffer').Buffer
|
||||
var fs = require('fs')
|
||||
var test = require('tape')
|
||||
var UAParser = require('ua-parser-js')
|
||||
var url = require('url')
|
||||
|
||||
var http = require('../..')
|
||||
|
||||
var browser = (new UAParser()).setUA(navigator.userAgent).getBrowser()
|
||||
var browserName = browser.name
|
||||
var browserVersion = browser.major
|
||||
// Response urls don't work on many browsers
|
||||
var skipResponseUrl = ((browserName === 'Opera') ||
|
||||
(browserName === 'IE') ||
|
||||
(browserName === 'Edge') ||
|
||||
(browserName === 'Chrome' && browserVersion <= 36) ||
|
||||
(browserName === 'Firefox' && browserVersion <= 31) ||
|
||||
((browserName === 'Safari' || browserName === 'Mobile Safari') && browserVersion <= 8) ||
|
||||
(browserName === 'WebKit') || // Old mobile safari
|
||||
(browserName === 'Android Browser' && browserVersion <= 4))
|
||||
|
||||
var reference = fs.readFileSync(__dirname + '/../server/static/basic.txt')
|
||||
|
||||
test('basic functionality', function (t) {
|
||||
http.get('/basic.txt', function (res) {
|
||||
if (!skipResponseUrl) {
|
||||
var testUrl = url.resolve(global.location.href, '/basic.txt')
|
||||
// Redirects aren't tested, but presumably only browser bugs
|
||||
// would cause this to fail only after redirects.
|
||||
t.equals(res.url, testUrl, 'response url correct')
|
||||
}
|
||||
|
||||
var buffers = []
|
||||
|
||||
res.on('end', function () {
|
||||
|
||||
@@ -64,6 +64,25 @@ test('Test alt protocol', function(t) {
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test page with \'file:\' protocol', function (t) {
|
||||
var params = {
|
||||
hostname: 'localhost',
|
||||
port: 3000,
|
||||
path: '/bar'
|
||||
}
|
||||
|
||||
var fileLocation = 'file:///home/me/stuff/index.html'
|
||||
|
||||
var normalLocation = global.location
|
||||
global.location = url.parse(fileLocation) // Temporarily change the location
|
||||
var request = http.get(params, noop)
|
||||
global.location = normalLocation // Reset the location
|
||||
|
||||
var resolved = url.resolve(fileLocation, request._opts.url)
|
||||
t.equal(resolved, 'http://localhost:3000/bar', 'Url should be correct')
|
||||
t.end()
|
||||
})
|
||||
|
||||
test('Test string as parameters', function(t) {
|
||||
var testUrl = '/api/foo'
|
||||
var request = http.get(testUrl, noop)
|
||||
|
||||
Reference in New Issue
Block a user