stream-http
Advanced tools
Comparing version 2.7.2 to 2.8.0
var ClientRequest = require('./lib/request') | ||
var IncomingMessage = require('./lib/response') | ||
var extend = require('xtend') | ||
@@ -47,2 +48,5 @@ var statusCodes = require('builtin-status-codes') | ||
http.ClientRequest = ClientRequest | ||
http.IncomingMessage = IncomingMessage | ||
http.Agent = function () {} | ||
@@ -49,0 +53,0 @@ http.Agent.defaultMaxSockets = 4 |
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream) | ||
exports.writableStream = isFunction(global.WritableStream) | ||
exports.abortController = isFunction(global.AbortController) | ||
exports.blobConstructor = false | ||
@@ -4,0 +8,0 @@ try { |
@@ -41,5 +41,4 @@ var capability = require('./capability') | ||
var useFetch = true | ||
if (opts.mode === 'disable-fetch' || 'timeout' in opts) { | ||
// If the use of XHR should be preferred and includes preserving the 'content-type' header. | ||
// Force XHR to be used since the Fetch API does not yet support timeouts. | ||
if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) { | ||
// If the use of XHR should be preferred. Not typically needed. | ||
useFetch = false | ||
@@ -106,3 +105,5 @@ preferBinary = true | ||
if (opts.method !== 'GET' && opts.method !== 'HEAD') { | ||
if (capability.blobConstructor) { | ||
if (capability.arraybuffer) { | ||
body = toArrayBuffer(Buffer.concat(self._body)) | ||
} else if (capability.blobConstructor) { | ||
body = new global.Blob(self._body.map(function (buffer) { | ||
@@ -134,2 +135,17 @@ return toArrayBuffer(buffer) | ||
if (self._mode === 'fetch') { | ||
var signal = null | ||
if (capability.abortController) { | ||
var controller = new AbortController() | ||
signal = controller.signal | ||
self._fetchAbortController = controller | ||
if ('requestTimeout' in opts && opts.requestTimeout !== 0) { | ||
global.setTimeout(function () { | ||
self.emit('requestTimeout') | ||
if (self._fetchAbortController) | ||
self._fetchAbortController.abort() | ||
}, opts.requestTimeout) | ||
} | ||
} | ||
global.fetch(self._opts.url, { | ||
@@ -140,3 +156,4 @@ method: self._opts.method, | ||
mode: 'cors', | ||
credentials: opts.withCredentials ? 'include' : 'same-origin' | ||
credentials: opts.withCredentials ? 'include' : 'same-origin', | ||
signal: signal | ||
}).then(function (response) { | ||
@@ -169,6 +186,6 @@ self._fetchResponse = response | ||
if ('timeout' in opts) { | ||
xhr.timeout = opts.timeout | ||
if ('requestTimeout' in opts) { | ||
xhr.timeout = opts.requestTimeout | ||
xhr.ontimeout = function () { | ||
self.emit('timeout') | ||
self.emit('requestTimeout') | ||
} | ||
@@ -269,4 +286,4 @@ } | ||
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 | ||
else if (self._fetchAbortController) | ||
self._fetchAbortController.abort() | ||
} | ||
@@ -273,0 +290,0 @@ |
@@ -38,3 +38,3 @@ var capability = require('./capability') | ||
response.headers.forEach(function(header, key){ | ||
response.headers.forEach(function (header, key){ | ||
self.headers[key.toLowerCase()] = header | ||
@@ -44,4 +44,31 @@ self.rawHeaders.push(key, header) | ||
if (capability.writableStream) { | ||
var writable = new WritableStream({ | ||
write: function (chunk) { | ||
return new Promise(function (resolve, reject) { | ||
if (self._destroyed) { | ||
return | ||
} else if(self.push(new Buffer(chunk))) { | ||
resolve() | ||
} else { | ||
self._resumeFetch = resolve | ||
} | ||
}) | ||
}, | ||
close: function () { | ||
if (!self._destroyed) | ||
self.push(null) | ||
}, | ||
abort: function (err) { | ||
if (!self._destroyed) | ||
self.emit('error', err) | ||
} | ||
}) | ||
// TODO: this doesn't respect backpressure. Once WritableStream is available, this can be fixed | ||
try { | ||
response.body.pipeTo(writable) | ||
return | ||
} catch (e) {} // pipeTo method isn't defined. Can't find a better way to feature test this | ||
} | ||
// fallback for when writableStream or pipeTo aren't available | ||
var reader = response.body.getReader() | ||
@@ -59,7 +86,7 @@ function read () { | ||
}).catch(function(err) { | ||
self.emit('error', err) | ||
if (!self._destroyed) | ||
self.emit('error', err) | ||
}) | ||
} | ||
read() | ||
} else { | ||
@@ -108,4 +135,12 @@ self._xhr = xhr | ||
IncomingMessage.prototype._read = function () {} | ||
IncomingMessage.prototype._read = function () { | ||
var self = this | ||
var resolve = self._resumeFetch | ||
if (resolve) { | ||
self._resumeFetch = null | ||
resolve() | ||
} | ||
} | ||
IncomingMessage.prototype._onXHRProgress = function () { | ||
@@ -112,0 +147,0 @@ var self = this |
{ | ||
"name": "stream-http", | ||
"version": "2.7.2", | ||
"version": "2.8.0", | ||
"description": "Streaming http in the browser", | ||
@@ -32,3 +32,3 @@ "main": "index.js", | ||
"inherits": "^2.0.1", | ||
"readable-stream": "^2.2.6", | ||
"readable-stream": "^2.3.3", | ||
"to-arraybuffer": "^1.0.0", | ||
@@ -38,11 +38,11 @@ "xtend": "^4.0.0" | ||
"devDependencies": { | ||
"basic-auth": "^1.0.3", | ||
"basic-auth": "^2.0.0", | ||
"brfs": "^1.4.0", | ||
"cookie-parser": "^1.4.3", | ||
"express": "^4.15.2", | ||
"tape": "^4.0.0", | ||
"ua-parser-js": "^0.7.7", | ||
"webworkify": "^1.0.2", | ||
"express": "^4.16.2", | ||
"tape": "^4.8.0", | ||
"ua-parser-js": "^0.7.17", | ||
"webworkify": "^1.5.0", | ||
"zuul": "^3.10.3" | ||
} | ||
} |
@@ -16,2 +16,6 @@ # stream-http [![Build Status](https://travis-ci.org/jhiesey/stream-http.svg?branch=master)](https://travis-ci.org/jhiesey/stream-http) | ||
Backpressure, allowing the browser to only pull data from the server as fast as it is | ||
consumed, is supported in: | ||
* Chrome >= 58 (using `fetch` and `WritableStream`) | ||
The following browsers support true streaming, where only a small amount of the request | ||
@@ -84,2 +88,8 @@ has to be held in memory at once: | ||
* `options.requestTimeout` allows setting a timeout in millisecionds for XHR and fetch (if | ||
supported by the browser). This is a limit on how long the entire process takes from | ||
beginning to end. Note that this is not the same as the node `setTimeout` functions, | ||
which apply to pauses in data transfer over the underlying socket, or the node `timeout` | ||
option, which applies to opening the connection. | ||
### Features missing compared to Node | ||
@@ -99,4 +109,4 @@ | ||
redirect pages. | ||
* The `timeout` options in the `request` method is non-operational in Safari <= 5 and | ||
Opera <= 12. | ||
* The `timeout` event/option and `setTimeout` functions, which operate on the underlying | ||
socket, are not available. However, see `options.requestTimeout` above. | ||
@@ -103,0 +113,0 @@ ## Example |
@@ -11,4 +11,4 @@ var Buffer = require('buffer').Buffer | ||
var browserVersion = browser.major | ||
// Binary streaming doesn't work in IE10 or below or in Opera | ||
var skipStreamingCheck = (browserName === 'Opera' || (browserName === 'IE' && browserVersion <= 10)) | ||
// Binary streaming doesn't work in IE10 or below | ||
var skipStreamingCheck = (browserName === 'IE' && browserVersion <= 10) | ||
@@ -15,0 +15,0 @@ // Binary data gets corrupted in IE8 or below |
@@ -12,4 +12,3 @@ var Buffer = require('buffer').Buffer | ||
// Binary request bodies don't work in a bunch of browsers | ||
var skipVerification = ((browserName === 'Opera' && browserVersion <= 11) || | ||
(browserName === 'IE' && browserVersion <= 10) || | ||
var skipVerification = ((browserName === 'IE' && browserVersion <= 10) || | ||
(browserName === 'Safari' && browserVersion <= 5) || | ||
@@ -16,0 +15,0 @@ (browserName === 'WebKit' && browserVersion <= 534) || // Old mobile safari |
@@ -11,4 +11,4 @@ var Buffer = require('buffer').Buffer | ||
var browserVersion = browser.major | ||
// Streaming doesn't work in IE9 or below or in Opera | ||
var skipStreamingCheck = (browserName === 'Opera' || (browserName === 'IE' && browserVersion <= 9)) | ||
// Streaming doesn't work in IE9 or below | ||
var skipStreamingCheck = (browserName === 'IE' && browserVersion <= 9) | ||
@@ -15,0 +15,0 @@ var COPIES = 1000 |
@@ -13,4 +13,3 @@ var Buffer = require('buffer').Buffer | ||
// Response urls don't work on many browsers | ||
var skipResponseUrl = ((browserName === 'Opera') || | ||
(browserName === 'IE') || | ||
var skipResponseUrl = ((browserName === 'IE') || | ||
(browserName === 'Edge') || | ||
@@ -17,0 +16,0 @@ (browserName === 'Chrome' && browserVersion <= 36) || |
@@ -11,4 +11,3 @@ var fs = require('fs') | ||
// Skip browsers with poor or nonexistant WebWorker support | ||
var skip = ((browserName === 'Opera' && browserVersion <= 12) || | ||
(browserName === 'IE' && browserVersion <= 10) || | ||
var skip = ((browserName === 'IE' && browserVersion <= 10) || | ||
(browserName === 'Safari' && browserVersion <= 5) || | ||
@@ -15,0 +14,0 @@ (browserName === 'WebKit' && browserVersion <= 534) || // Old mobile safari |
@@ -1,2 +0,2 @@ | ||
// These tests are teken from http-browserify to ensure compatibility with | ||
// These tests are taken from http-browserify to ensure compatibility with | ||
// that module | ||
@@ -3,0 +3,0 @@ var test = require('tape') |
Sorry, the diff of this file is not supported yet
89548
1498
145
32
13
Updatedreadable-stream@^2.3.3