Home

Awesome

stream-http Build Status

Sauce Test Status

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.

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:

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. 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

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.

Features missing compared to Node

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__';
	});
})

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.

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. You will also need to run a 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.