Package Exports
- stream-http
- stream-http/lib/capability
- stream-http/package
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (stream-http) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
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
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.
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)
The following browsers support pseudo-streaming, where the data is available before the request finishes, but the entire response must be held in memory:
- Chrome
- Safari >= 5, and maybe older
- IE >= 10
- Most other Webkit-based browsers, including the default Android browser
All browsers newer than IE8 support binary responses. All of the above browsers that support true streaming or pseudo-streaming support that for binary data as well except for IE10. Old (presto-based) Opera also does not support binary streaming either.
How do you use it?
The intent is to have the same api as the client part of the node HTTP module. 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
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 user.
- The
options.mode
field passed intohttp.request
orhttp.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-stream': 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').
- 'prefer-fast': Deprecated; now a synonym for 'default', which has the same performance characteristics as this mode did in versions before 1.5.
Features missing compared to node
http.Agent
is only a stub- The 'socket', 'connect', 'upgrade', and 'continue' events on
http.ClientRequest
. - 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.
message.httpVersion
message.rawHeaders
is modified by the browser, and may not quite match what is sent by the server.message.trailers
andmessage.rawTrailers
will remain empty.
Example
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__';
});
})
License
MIT. Copyright (C) John Hiesey and other contributors.