initial commit
This commit is contained in:
24
node_modules/stream-http/LICENSE
generated
vendored
Normal file
24
node_modules/stream-http/LICENSE
generated
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
The MIT License
|
||||
|
||||
Copyright (c) 2015 John Hiesey
|
||||
|
||||
Permission is hereby granted, free of charge,
|
||||
to any person obtaining a copy of this software and
|
||||
associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify,
|
||||
merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom
|
||||
the Software is furnished to do so,
|
||||
subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice
|
||||
shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
||||
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
|
||||
ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
||||
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
134
node_modules/stream-http/README.md
generated
vendored
Normal file
134
node_modules/stream-http/README.md
generated
vendored
Normal file
@@ -0,0 +1,134 @@
|
||||
# stream-http [](https://travis-ci.org/jhiesey/stream-http)
|
||||
|
||||
[](https://app.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
|
||||
aren't available, since browsers don't give nearly as much control over requests.
|
||||
|
||||
This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify).
|
||||
|
||||
## What does it do?
|
||||
|
||||
In accordance with its name, `stream-http` tries to provide data to its caller before
|
||||
the request has completed whenever possible.
|
||||
|
||||
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
|
||||
has to be held in memory at once:
|
||||
* Chrome >= 43 (using the `fetch` API)
|
||||
* Firefox >= 9 (using `moz-chunked-arraybuffer` responseType with xhr)
|
||||
|
||||
All other supported browsers support pseudo-streaming, where the data is available before
|
||||
the request finishes, but the entire response must be held in memory. This works for both
|
||||
text and binary data.
|
||||
|
||||
### IE note:
|
||||
As of version 3.0.0, IE10 and below are no longer supported. IE11 support will remain for
|
||||
now.
|
||||
|
||||
## How do you use it?
|
||||
|
||||
The intent is to have the same API as the client part of the
|
||||
[Node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
|
||||
practical, although limitations in browsers make an exact clone of the Node API impossible.
|
||||
|
||||
This module implements `http.request`, `http.get`, and most of `http.ClientRequest`
|
||||
and `http.IncomingMessage` in addition to `http.METHODS` and `http.STATUS_CODES`. See the
|
||||
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.
|
||||
|
||||
This module has to make some tradeoffs to support binary data and/or streaming. Generally,
|
||||
the module can make a fairly good decision about which underlying browser features to use,
|
||||
but sometimes it helps to get a little input from the developer.
|
||||
|
||||
* The `options.mode` field passed into `http.request` or `http.get` can take on one of the
|
||||
following values:
|
||||
* 'default' (or any falsy value, including `undefined`): Try to provide partial data before
|
||||
the request completes, but not at the cost of correctness for binary data or correctness of
|
||||
the 'content-type' response header. This mode will also avoid slower code paths whenever
|
||||
possible, which is particularly useful when making large requests in a browser like Safari
|
||||
that has a weaker JavaScript engine.
|
||||
* 'allow-wrong-content-type': Provides partial data in more cases than 'default', but
|
||||
at the expense of causing the 'content-type' response header to be incorrectly reported
|
||||
(as 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42
|
||||
and older. Preserves binary data whenever possible. In some cases the implementation may
|
||||
also be a bit slow. This was the default in versions of this module before 1.5.
|
||||
* 'prefer-streaming': Provide data before the request completes even if binary data (anything
|
||||
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.
|
||||
|
||||
* `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
|
||||
|
||||
* `http.Agent` is only a stub
|
||||
* The 'socket', 'connect', 'upgrade', and 'continue' events on `http.ClientRequest`.
|
||||
* Any operations, other than `request.setTimeout`, that operate directly on the underlying
|
||||
socket.
|
||||
* Any options that are disallowed for security reasons. This includes setting or getting
|
||||
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
|
||||
|
||||
``` js
|
||||
http.get('/bundle.js', function (res) {
|
||||
var div = document.getElementById('result');
|
||||
div.innerHTML += 'GET /beep<br>';
|
||||
|
||||
res.on('data', function (buf) {
|
||||
div.innerHTML += buf;
|
||||
});
|
||||
|
||||
res.on('end', function () {
|
||||
div.innerHTML += '<br>__END__';
|
||||
});
|
||||
})
|
||||
```
|
||||
|
||||
## Running tests
|
||||
|
||||
There are two sets of tests: the tests that run in Node (found in `test/node`) and the tests
|
||||
that run in the browser (found in `test/browser`). Normally the browser tests run on
|
||||
[Sauce Labs](http://saucelabs.com/).
|
||||
|
||||
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 [`.airtaprc` file](https://github.com/airtap/airtap/blob/master/doc/airtaprc.md).
|
||||
You will also need to run a [Sauce Connect Proxy](https://wiki.saucelabs.com/display/DOCS/Sauce+Connect+Proxy)
|
||||
with the same credentials.
|
||||
|
||||
To run just the Node tests, run `npm run test-node`.
|
||||
|
||||
To run the browser tests locally, run `npm run test-browser-local` and point your browser to
|
||||
the link shown in your terminal.
|
||||
|
||||
## License
|
||||
|
||||
MIT. Copyright (C) John Hiesey and other contributors.
|
||||
85
node_modules/stream-http/index.js
generated
vendored
Normal file
85
node_modules/stream-http/index.js
generated
vendored
Normal file
@@ -0,0 +1,85 @@
|
||||
var ClientRequest = require('./lib/request')
|
||||
var response = require('./lib/response')
|
||||
var extend = require('xtend')
|
||||
var statusCodes = require('builtin-status-codes')
|
||||
var url = require('url')
|
||||
|
||||
var http = exports
|
||||
|
||||
http.request = function (opts, cb) {
|
||||
if (typeof opts === 'string')
|
||||
opts = url.parse(opts)
|
||||
else
|
||||
opts = extend(opts)
|
||||
|
||||
// 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 || '/'
|
||||
|
||||
// Necessary for IPv6 addresses
|
||||
if (host && host.indexOf(':') !== -1)
|
||||
host = '[' + host + ']'
|
||||
|
||||
// This may be a relative url. The browser should always be able to interpret it correctly.
|
||||
opts.url = (host ? (protocol + '//' + host) : '') + (port ? ':' + port : '') + path
|
||||
opts.method = (opts.method || 'GET').toUpperCase()
|
||||
opts.headers = opts.headers || {}
|
||||
|
||||
// Also valid opts.auth, opts.mode
|
||||
|
||||
var req = new ClientRequest(opts)
|
||||
if (cb)
|
||||
req.on('response', cb)
|
||||
return req
|
||||
}
|
||||
|
||||
http.get = function get (opts, cb) {
|
||||
var req = http.request(opts, cb)
|
||||
req.end()
|
||||
return req
|
||||
}
|
||||
|
||||
http.ClientRequest = ClientRequest
|
||||
http.IncomingMessage = response.IncomingMessage
|
||||
|
||||
http.Agent = function () {}
|
||||
http.Agent.defaultMaxSockets = 4
|
||||
|
||||
http.globalAgent = new http.Agent()
|
||||
|
||||
http.STATUS_CODES = statusCodes
|
||||
|
||||
http.METHODS = [
|
||||
'CHECKOUT',
|
||||
'CONNECT',
|
||||
'COPY',
|
||||
'DELETE',
|
||||
'GET',
|
||||
'HEAD',
|
||||
'LOCK',
|
||||
'M-SEARCH',
|
||||
'MERGE',
|
||||
'MKACTIVITY',
|
||||
'MKCOL',
|
||||
'MOVE',
|
||||
'NOTIFY',
|
||||
'OPTIONS',
|
||||
'PATCH',
|
||||
'POST',
|
||||
'PROPFIND',
|
||||
'PROPPATCH',
|
||||
'PURGE',
|
||||
'PUT',
|
||||
'REPORT',
|
||||
'SEARCH',
|
||||
'SUBSCRIBE',
|
||||
'TRACE',
|
||||
'UNLOCK',
|
||||
'UNSUBSCRIBE'
|
||||
]
|
||||
59
node_modules/stream-http/lib/capability.js
generated
vendored
Normal file
59
node_modules/stream-http/lib/capability.js
generated
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
exports.fetch = isFunction(global.fetch) && isFunction(global.ReadableStream)
|
||||
|
||||
exports.writableStream = isFunction(global.WritableStream)
|
||||
|
||||
exports.abortController = isFunction(global.AbortController)
|
||||
|
||||
// The xhr request to example.com may violate some restrictive CSP configurations,
|
||||
// so if we're running in a browser that supports `fetch`, avoid calling getXHR()
|
||||
// and assume support for certain features below.
|
||||
var xhr
|
||||
function getXHR () {
|
||||
// Cache the xhr value
|
||||
if (xhr !== undefined) return xhr
|
||||
|
||||
if (global.XMLHttpRequest) {
|
||||
xhr = new global.XMLHttpRequest()
|
||||
// If XDomainRequest is available (ie only, where xhr might not work
|
||||
// cross domain), use the page location. Otherwise use example.com
|
||||
// Note: this doesn't actually make an http request.
|
||||
try {
|
||||
xhr.open('GET', global.XDomainRequest ? '/' : 'https://example.com')
|
||||
} catch(e) {
|
||||
xhr = null
|
||||
}
|
||||
} else {
|
||||
// Service workers don't have XHR
|
||||
xhr = null
|
||||
}
|
||||
return xhr
|
||||
}
|
||||
|
||||
function checkTypeSupport (type) {
|
||||
var xhr = getXHR()
|
||||
if (!xhr) return false
|
||||
try {
|
||||
xhr.responseType = type
|
||||
return xhr.responseType === type
|
||||
} catch (e) {}
|
||||
return false
|
||||
}
|
||||
|
||||
// If fetch is supported, then arraybuffer will be supported too. Skip calling
|
||||
// checkTypeSupport(), since that calls getXHR().
|
||||
exports.arraybuffer = exports.fetch || checkTypeSupport('arraybuffer')
|
||||
|
||||
// These next two tests unavoidably show warnings in Chrome. Since fetch will always
|
||||
// be used if it's available, just return false for these to avoid the warnings.
|
||||
exports.msstream = !exports.fetch && checkTypeSupport('ms-stream')
|
||||
exports.mozchunkedarraybuffer = !exports.fetch && checkTypeSupport('moz-chunked-arraybuffer')
|
||||
|
||||
// If fetch is supported, then overrideMimeType will be supported too. Skip calling
|
||||
// getXHR().
|
||||
exports.overrideMimeType = exports.fetch || (getXHR() ? isFunction(getXHR().overrideMimeType) : false)
|
||||
|
||||
function isFunction (value) {
|
||||
return typeof value === 'function'
|
||||
}
|
||||
|
||||
xhr = null // Help gc
|
||||
352
node_modules/stream-http/lib/request.js
generated
vendored
Normal file
352
node_modules/stream-http/lib/request.js
generated
vendored
Normal file
@@ -0,0 +1,352 @@
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var response = require('./response')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var IncomingMessage = response.IncomingMessage
|
||||
var rStates = response.readyStates
|
||||
|
||||
function decideMode (preferBinary, useFetch) {
|
||||
if (capability.fetch && useFetch) {
|
||||
return 'fetch'
|
||||
} else if (capability.mozchunkedarraybuffer) {
|
||||
return 'moz-chunked-arraybuffer'
|
||||
} else if (capability.msstream) {
|
||||
return 'ms-stream'
|
||||
} else if (capability.arraybuffer && preferBinary) {
|
||||
return 'arraybuffer'
|
||||
} else {
|
||||
return 'text'
|
||||
}
|
||||
}
|
||||
|
||||
var ClientRequest = module.exports = function (opts) {
|
||||
var self = this
|
||||
stream.Writable.call(self)
|
||||
|
||||
self._opts = opts
|
||||
self._body = []
|
||||
self._headers = {}
|
||||
if (opts.auth)
|
||||
self.setHeader('Authorization', 'Basic ' + Buffer.from(opts.auth).toString('base64'))
|
||||
Object.keys(opts.headers).forEach(function (name) {
|
||||
self.setHeader(name, opts.headers[name])
|
||||
})
|
||||
|
||||
var preferBinary
|
||||
var useFetch = true
|
||||
if (opts.mode === 'disable-fetch' || ('requestTimeout' in opts && !capability.abortController)) {
|
||||
// If the use of XHR should be preferred. Not typically needed.
|
||||
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
|
||||
} else if (opts.mode === 'allow-wrong-content-type') {
|
||||
// If streaming is more important than preserving the 'content-type' header
|
||||
preferBinary = !capability.overrideMimeType
|
||||
} else if (!opts.mode || opts.mode === 'default' || opts.mode === 'prefer-fast') {
|
||||
// Use binary if text streaming may corrupt data or the content-type header, or for speed
|
||||
preferBinary = true
|
||||
} else {
|
||||
throw new Error('Invalid value for opts.mode')
|
||||
}
|
||||
self._mode = decideMode(preferBinary, useFetch)
|
||||
self._fetchTimer = null
|
||||
self._socketTimeout = null
|
||||
self._socketTimer = null
|
||||
|
||||
self.on('finish', function () {
|
||||
self._onFinish()
|
||||
})
|
||||
}
|
||||
|
||||
inherits(ClientRequest, stream.Writable)
|
||||
|
||||
ClientRequest.prototype.setHeader = function (name, value) {
|
||||
var self = this
|
||||
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 (unsafeHeaders.indexOf(lowerName) !== -1)
|
||||
return
|
||||
|
||||
self._headers[lowerName] = {
|
||||
name: name,
|
||||
value: value
|
||||
}
|
||||
}
|
||||
|
||||
ClientRequest.prototype.getHeader = function (name) {
|
||||
var header = this._headers[name.toLowerCase()]
|
||||
if (header)
|
||||
return header.value
|
||||
return null
|
||||
}
|
||||
|
||||
ClientRequest.prototype.removeHeader = function (name) {
|
||||
var self = this
|
||||
delete self._headers[name.toLowerCase()]
|
||||
}
|
||||
|
||||
ClientRequest.prototype._onFinish = function () {
|
||||
var self = this
|
||||
|
||||
if (self._destroyed)
|
||||
return
|
||||
var opts = self._opts
|
||||
|
||||
if ('timeout' in opts && opts.timeout !== 0) {
|
||||
self.setTimeout(opts.timeout)
|
||||
}
|
||||
|
||||
var headersObj = self._headers
|
||||
var body = null
|
||||
if (opts.method !== 'GET' && opts.method !== 'HEAD') {
|
||||
body = new Blob(self._body, {
|
||||
type: (headersObj['content-type'] || {}).value || ''
|
||||
});
|
||||
}
|
||||
|
||||
// create flattened list of headers
|
||||
var headersList = []
|
||||
Object.keys(headersObj).forEach(function (keyName) {
|
||||
var name = headersObj[keyName].name
|
||||
var value = headersObj[keyName].value
|
||||
if (Array.isArray(value)) {
|
||||
value.forEach(function (v) {
|
||||
headersList.push([name, v])
|
||||
})
|
||||
} else {
|
||||
headersList.push([name, value])
|
||||
}
|
||||
})
|
||||
|
||||
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) {
|
||||
self._fetchTimer = global.setTimeout(function () {
|
||||
self.emit('requestTimeout')
|
||||
if (self._fetchAbortController)
|
||||
self._fetchAbortController.abort()
|
||||
}, opts.requestTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
global.fetch(self._opts.url, {
|
||||
method: self._opts.method,
|
||||
headers: headersList,
|
||||
body: body || undefined,
|
||||
mode: 'cors',
|
||||
credentials: opts.withCredentials ? 'include' : 'same-origin',
|
||||
signal: signal
|
||||
}).then(function (response) {
|
||||
self._fetchResponse = response
|
||||
self._resetTimers(false)
|
||||
self._connect()
|
||||
}, function (reason) {
|
||||
self._resetTimers(true)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', reason)
|
||||
})
|
||||
} else {
|
||||
var xhr = self._xhr = new global.XMLHttpRequest()
|
||||
try {
|
||||
xhr.open(self._opts.method, self._opts.url, true)
|
||||
} catch (err) {
|
||||
process.nextTick(function () {
|
||||
self.emit('error', err)
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
// Can't set responseType on really old browsers
|
||||
if ('responseType' in xhr)
|
||||
xhr.responseType = self._mode
|
||||
|
||||
if ('withCredentials' in xhr)
|
||||
xhr.withCredentials = !!opts.withCredentials
|
||||
|
||||
if (self._mode === 'text' && 'overrideMimeType' in xhr)
|
||||
xhr.overrideMimeType('text/plain; charset=x-user-defined')
|
||||
|
||||
if ('requestTimeout' in opts) {
|
||||
xhr.timeout = opts.requestTimeout
|
||||
xhr.ontimeout = function () {
|
||||
self.emit('requestTimeout')
|
||||
}
|
||||
}
|
||||
|
||||
headersList.forEach(function (header) {
|
||||
xhr.setRequestHeader(header[0], header[1])
|
||||
})
|
||||
|
||||
self._response = null
|
||||
xhr.onreadystatechange = function () {
|
||||
switch (xhr.readyState) {
|
||||
case rStates.LOADING:
|
||||
case rStates.DONE:
|
||||
self._onXHRProgress()
|
||||
break
|
||||
}
|
||||
}
|
||||
// Necessary for streaming in Firefox, since xhr.response is ONLY defined
|
||||
// in onprogress, not in onreadystatechange with xhr.readyState = 3
|
||||
if (self._mode === 'moz-chunked-arraybuffer') {
|
||||
xhr.onprogress = function () {
|
||||
self._onXHRProgress()
|
||||
}
|
||||
}
|
||||
|
||||
xhr.onerror = function () {
|
||||
if (self._destroyed)
|
||||
return
|
||||
self._resetTimers(true)
|
||||
self.emit('error', new Error('XHR error'))
|
||||
}
|
||||
|
||||
try {
|
||||
xhr.send(body)
|
||||
} catch (err) {
|
||||
process.nextTick(function () {
|
||||
self.emit('error', err)
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if xhr.status is readable and non-zero, indicating no error.
|
||||
* Even though the spec says it should be available in readyState 3,
|
||||
* accessing it throws an exception in IE8
|
||||
*/
|
||||
function statusValid (xhr) {
|
||||
try {
|
||||
var status = xhr.status
|
||||
return (status !== null && status !== 0)
|
||||
} catch (e) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
ClientRequest.prototype._onXHRProgress = function () {
|
||||
var self = this
|
||||
|
||||
self._resetTimers(false)
|
||||
|
||||
if (!statusValid(self._xhr) || self._destroyed)
|
||||
return
|
||||
|
||||
if (!self._response)
|
||||
self._connect()
|
||||
|
||||
self._response._onXHRProgress(self._resetTimers.bind(self))
|
||||
}
|
||||
|
||||
ClientRequest.prototype._connect = function () {
|
||||
var self = this
|
||||
|
||||
if (self._destroyed)
|
||||
return
|
||||
|
||||
self._response = new IncomingMessage(self._xhr, self._fetchResponse, self._mode, self._resetTimers.bind(self))
|
||||
self._response.on('error', function(err) {
|
||||
self.emit('error', err)
|
||||
})
|
||||
|
||||
self.emit('response', self._response)
|
||||
}
|
||||
|
||||
ClientRequest.prototype._write = function (chunk, encoding, cb) {
|
||||
var self = this
|
||||
|
||||
self._body.push(chunk)
|
||||
cb()
|
||||
}
|
||||
|
||||
ClientRequest.prototype._resetTimers = function (done) {
|
||||
var self = this
|
||||
|
||||
global.clearTimeout(self._socketTimer)
|
||||
self._socketTimer = null
|
||||
|
||||
if (done) {
|
||||
global.clearTimeout(self._fetchTimer)
|
||||
self._fetchTimer = null
|
||||
} else if (self._socketTimeout) {
|
||||
self._socketTimer = global.setTimeout(function () {
|
||||
self.emit('timeout')
|
||||
}, self._socketTimeout)
|
||||
}
|
||||
}
|
||||
|
||||
ClientRequest.prototype.abort = ClientRequest.prototype.destroy = function (err) {
|
||||
var self = this
|
||||
self._destroyed = true
|
||||
self._resetTimers(true)
|
||||
if (self._response)
|
||||
self._response._destroyed = true
|
||||
if (self._xhr)
|
||||
self._xhr.abort()
|
||||
else if (self._fetchAbortController)
|
||||
self._fetchAbortController.abort()
|
||||
|
||||
if (err)
|
||||
self.emit('error', err)
|
||||
}
|
||||
|
||||
ClientRequest.prototype.end = function (data, encoding, cb) {
|
||||
var self = this
|
||||
if (typeof data === 'function') {
|
||||
cb = data
|
||||
data = undefined
|
||||
}
|
||||
|
||||
stream.Writable.prototype.end.call(self, data, encoding, cb)
|
||||
}
|
||||
|
||||
ClientRequest.prototype.setTimeout = function (timeout, cb) {
|
||||
var self = this
|
||||
|
||||
if (cb)
|
||||
self.once('timeout', cb)
|
||||
|
||||
self._socketTimeout = timeout
|
||||
self._resetTimers(false)
|
||||
}
|
||||
|
||||
ClientRequest.prototype.flushHeaders = 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',
|
||||
'via'
|
||||
]
|
||||
211
node_modules/stream-http/lib/response.js
generated
vendored
Normal file
211
node_modules/stream-http/lib/response.js
generated
vendored
Normal file
@@ -0,0 +1,211 @@
|
||||
var capability = require('./capability')
|
||||
var inherits = require('inherits')
|
||||
var stream = require('readable-stream')
|
||||
|
||||
var rStates = exports.readyStates = {
|
||||
UNSENT: 0,
|
||||
OPENED: 1,
|
||||
HEADERS_RECEIVED: 2,
|
||||
LOADING: 3,
|
||||
DONE: 4
|
||||
}
|
||||
|
||||
var IncomingMessage = exports.IncomingMessage = function (xhr, response, mode, resetTimers) {
|
||||
var self = this
|
||||
stream.Readable.call(self)
|
||||
|
||||
self._mode = mode
|
||||
self.headers = {}
|
||||
self.rawHeaders = []
|
||||
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
|
||||
|
||||
self.url = response.url
|
||||
self.statusCode = response.status
|
||||
self.statusMessage = response.statusText
|
||||
|
||||
response.headers.forEach(function (header, key){
|
||||
self.headers[key.toLowerCase()] = header
|
||||
self.rawHeaders.push(key, header)
|
||||
})
|
||||
|
||||
if (capability.writableStream) {
|
||||
var writable = new WritableStream({
|
||||
write: function (chunk) {
|
||||
resetTimers(false)
|
||||
return new Promise(function (resolve, reject) {
|
||||
if (self._destroyed) {
|
||||
reject()
|
||||
} else if(self.push(Buffer.from(chunk))) {
|
||||
resolve()
|
||||
} else {
|
||||
self._resumeFetch = resolve
|
||||
}
|
||||
})
|
||||
},
|
||||
close: function () {
|
||||
resetTimers(true)
|
||||
if (!self._destroyed)
|
||||
self.push(null)
|
||||
},
|
||||
abort: function (err) {
|
||||
resetTimers(true)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
}
|
||||
})
|
||||
|
||||
try {
|
||||
response.body.pipeTo(writable).catch(function (err) {
|
||||
resetTimers(true)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
})
|
||||
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()
|
||||
function read () {
|
||||
reader.read().then(function (result) {
|
||||
if (self._destroyed)
|
||||
return
|
||||
resetTimers(result.done)
|
||||
if (result.done) {
|
||||
self.push(null)
|
||||
return
|
||||
}
|
||||
self.push(Buffer.from(result.value))
|
||||
read()
|
||||
}).catch(function (err) {
|
||||
resetTimers(true)
|
||||
if (!self._destroyed)
|
||||
self.emit('error', err)
|
||||
})
|
||||
}
|
||||
read()
|
||||
} else {
|
||||
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/)
|
||||
headers.forEach(function (header) {
|
||||
var matches = header.match(/^([^:]+):\s*(.*)/)
|
||||
if (matches) {
|
||||
var key = matches[1].toLowerCase()
|
||||
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 {
|
||||
self.headers[key] = matches[2]
|
||||
}
|
||||
self.rawHeaders.push(matches[1], matches[2])
|
||||
}
|
||||
})
|
||||
|
||||
self._charset = 'x-user-defined'
|
||||
if (!capability.overrideMimeType) {
|
||||
var mimeType = self.rawHeaders['mime-type']
|
||||
if (mimeType) {
|
||||
var charsetMatch = mimeType.match(/;\s*charset=([^;])(;|$)/)
|
||||
if (charsetMatch) {
|
||||
self._charset = charsetMatch[1].toLowerCase()
|
||||
}
|
||||
}
|
||||
if (!self._charset)
|
||||
self._charset = 'utf-8' // best guess
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
inherits(IncomingMessage, stream.Readable)
|
||||
|
||||
IncomingMessage.prototype._read = function () {
|
||||
var self = this
|
||||
|
||||
var resolve = self._resumeFetch
|
||||
if (resolve) {
|
||||
self._resumeFetch = null
|
||||
resolve()
|
||||
}
|
||||
}
|
||||
|
||||
IncomingMessage.prototype._onXHRProgress = function (resetTimers) {
|
||||
var self = this
|
||||
|
||||
var xhr = self._xhr
|
||||
|
||||
var response = null
|
||||
switch (self._mode) {
|
||||
case 'text':
|
||||
response = xhr.responseText
|
||||
if (response.length > self._pos) {
|
||||
var newData = response.substr(self._pos)
|
||||
if (self._charset === 'x-user-defined') {
|
||||
var buffer = Buffer.alloc(newData.length)
|
||||
for (var i = 0; i < newData.length; i++)
|
||||
buffer[i] = newData.charCodeAt(i) & 0xff
|
||||
|
||||
self.push(buffer)
|
||||
} else {
|
||||
self.push(newData, self._charset)
|
||||
}
|
||||
self._pos = response.length
|
||||
}
|
||||
break
|
||||
case 'arraybuffer':
|
||||
if (xhr.readyState !== rStates.DONE || !xhr.response)
|
||||
break
|
||||
response = xhr.response
|
||||
self.push(Buffer.from(new Uint8Array(response)))
|
||||
break
|
||||
case 'moz-chunked-arraybuffer': // take whole
|
||||
response = xhr.response
|
||||
if (xhr.readyState !== rStates.LOADING || !response)
|
||||
break
|
||||
self.push(Buffer.from(new Uint8Array(response)))
|
||||
break
|
||||
case 'ms-stream':
|
||||
response = xhr.response
|
||||
if (xhr.readyState !== rStates.LOADING)
|
||||
break
|
||||
var reader = new global.MSStreamReader()
|
||||
reader.onprogress = function () {
|
||||
if (reader.result.byteLength > self._pos) {
|
||||
self.push(Buffer.from(new Uint8Array(reader.result.slice(self._pos))))
|
||||
self._pos = reader.result.byteLength
|
||||
}
|
||||
}
|
||||
reader.onload = function () {
|
||||
resetTimers(true)
|
||||
self.push(null)
|
||||
}
|
||||
// reader.onerror = ??? // TODO: this
|
||||
reader.readAsArrayBuffer(response)
|
||||
break
|
||||
}
|
||||
|
||||
// The ms-stream case handles end separately in reader.onload()
|
||||
if (self._xhr.readyState === rStates.DONE && self._mode !== 'ms-stream') {
|
||||
resetTimers(true)
|
||||
self.push(null)
|
||||
}
|
||||
}
|
||||
47
node_modules/stream-http/package.json
generated
vendored
Normal file
47
node_modules/stream-http/package.json
generated
vendored
Normal file
@@ -0,0 +1,47 @@
|
||||
{
|
||||
"name": "stream-http",
|
||||
"version": "3.2.0",
|
||||
"description": "Streaming http in the browser",
|
||||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git://github.com/jhiesey/stream-http.git"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "npm run test-node && ([ -n \"${TRAVIS_PULL_REQUEST}\" -a \"${TRAVIS_PULL_REQUEST}\" != 'false' ] || npm run test-browser)",
|
||||
"test-node": "tape test/node/*.js",
|
||||
"test-browser": "airtap --concurrency 1 -- test/browser/*.js",
|
||||
"test-browser-local": "airtap --preset local -- test/browser/*.js"
|
||||
},
|
||||
"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",
|
||||
"streaming",
|
||||
"xhr",
|
||||
"http-browserify"
|
||||
],
|
||||
"dependencies": {
|
||||
"builtin-status-codes": "^3.0.0",
|
||||
"inherits": "^2.0.4",
|
||||
"readable-stream": "^3.6.0",
|
||||
"xtend": "^4.0.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"airtap": "^4.0.3",
|
||||
"airtap-manual": "^1.0.0",
|
||||
"airtap-sauce": "^1.1.0",
|
||||
"basic-auth": "^2.0.1",
|
||||
"brfs": "^2.0.2",
|
||||
"cookie-parser": "^1.4.5",
|
||||
"express": "^4.17.1",
|
||||
"tape": "^5.2.2",
|
||||
"ua-parser-js": "^0.7.28",
|
||||
"webworkify": "^1.5.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user