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.
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:
The following browsers support true streaming, where only a small amount of the request has to be held in memory at once:
moz-chunked-arraybufferresponseType 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:
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.
As of version 2.0.0, IE8 support requires the user to supply polyfills for
implementations are provided in ie8-polyfill.js; alternately,
you may want to consider using es5-shim.
All browsers with full ES5 support shouldn't require any polyfills.
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.get, and most of
http.IncomingMessage in addition to
http.STATUS_CODES. See the
Node docs for how these work.
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.
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.
options.mode field passed into
http.get can take on one of the
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
which apply to pauses in data transfer over the underlying socket, or the node
option, which applies to opening the connection.
http.Agentis only a stub
request.setTimeout, that operate directly on the underlying socket.
message.rawHeadersis modified by the browser, and may not quite match what is sent by the server.
message.rawTrailerswill remain empty.
setTimeoutfunctions, which operate on the underlying socket, are not available. However, see
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
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
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
MIT. Copyright (C) John Hiesey and other contributors.