You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

144 lines
7.0 KiB

4 years ago
  1. # stream-http [![Build Status](https://travis-ci.org/jhiesey/stream-http.svg?branch=master)](https://travis-ci.org/jhiesey/stream-http)
  2. [![Sauce Test Status](https://saucelabs.com/browser-matrix/stream-http.svg)](https://saucelabs.com/u/stream-http)
  3. This module is an implementation of Node's native `http` module for the browser.
  4. It tries to match Node's API and behavior as closely as possible, but some features
  5. aren't available, since browsers don't give nearly as much control over requests.
  6. This is heavily inspired by, and intended to replace, [http-browserify](https://github.com/substack/http-browserify).
  7. ## What does it do?
  8. In accordance with its name, `stream-http` tries to provide data to its caller before
  9. the request has completed whenever possible.
  10. Backpressure, allowing the browser to only pull data from the server as fast as it is
  11. consumed, is supported in:
  12. * Chrome >= 58 (using `fetch` and `WritableStream`)
  13. The following browsers support true streaming, where only a small amount of the request
  14. has to be held in memory at once:
  15. * Chrome >= 43 (using the `fetch` API)
  16. * Firefox >= 9 (using `moz-chunked-arraybuffer` responseType with xhr)
  17. The following browsers support pseudo-streaming, where the data is available before the
  18. request finishes, but the entire response must be held in memory:
  19. * Chrome
  20. * Safari >= 5, and maybe older
  21. * IE >= 10
  22. * Most other Webkit-based browsers, including the default Android browser
  23. All browsers newer than IE8 support binary responses. All of the above browsers that
  24. support true streaming or pseudo-streaming support that for binary data as well
  25. except for IE10. Old (presto-based) Opera also does not support binary streaming either.
  26. ### IE8 note:
  27. As of version 2.0.0, IE8 support requires the user to supply polyfills for
  28. `Object.keys`, `Array.prototype.forEach`, and `Array.prototype.indexOf`. Example
  29. implementations are provided in [ie8-polyfill.js](ie8-polyfill.js); alternately,
  30. you may want to consider using [es5-shim](https://github.com/es-shims/es5-shim).
  31. All browsers with full ES5 support shouldn't require any polyfills.
  32. ## How do you use it?
  33. The intent is to have the same API as the client part of the
  34. [Node HTTP module](https://nodejs.org/api/http.html). The interfaces are the same wherever
  35. practical, although limitations in browsers make an exact clone of the Node API impossible.
  36. This module implements `http.request`, `http.get`, and most of `http.ClientRequest`
  37. and `http.IncomingMessage` in addition to `http.METHODS` and `http.STATUS_CODES`. See the
  38. Node docs for how these work.
  39. ### Extra features compared to Node
  40. * The `message.url` property provides access to the final URL after all redirects. This
  41. is useful since the browser follows all redirects silently, unlike Node. It is available
  42. in Chrome 37 and newer, Firefox 32 and newer, and Safari 9 and newer.
  43. * The `options.withCredentials` boolean flag, used to indicate if the browser should send
  44. cookies or authentication information with a CORS request. Default false.
  45. This module has to make some tradeoffs to support binary data and/or streaming. Generally,
  46. the module can make a fairly good decision about which underlying browser features to use,
  47. but sometimes it helps to get a little input from the developer.
  48. * The `options.mode` field passed into `http.request` or `http.get` can take on one of the
  49. following values:
  50. * 'default' (or any falsy value, including `undefined`): Try to provide partial data before
  51. the request completes, but not at the cost of correctness for binary data or correctness of
  52. the 'content-type' response header. This mode will also avoid slower code paths whenever
  53. possible, which is particularly useful when making large requests in a browser like Safari
  54. that has a weaker JavaScript engine.
  55. * 'allow-wrong-content-type': Provides partial data in more cases than 'default', but
  56. at the expense of causing the 'content-type' response header to be incorrectly reported
  57. (as 'text/plain; charset=x-user-defined') in some browsers, notably Safari and Chrome 42
  58. and older. Preserves binary data whenever possible. In some cases the implementation may
  59. also be a bit slow. This was the default in versions of this module before 1.5.
  60. * 'prefer-stream': Provide data before the request completes even if binary data (anything
  61. that isn't a single-byte ASCII or UTF8 character) will be corrupted. Of course, this option
  62. is only safe for text data. May also cause the 'content-type' response header to be
  63. incorrectly reported (as 'text/plain; charset=x-user-defined').
  64. * 'disable-fetch': Force the use of plain XHR regardless of the browser declaring a fetch
  65. capability. Preserves the correctness of binary data and the 'content-type' response header.
  66. * 'prefer-fast': Deprecated; now a synonym for 'default', which has the same performance
  67. characteristics as this mode did in versions before 1.5.
  68. * `options.requestTimeout` allows setting a timeout in millisecionds for XHR and fetch (if
  69. supported by the browser). This is a limit on how long the entire process takes from
  70. beginning to end. Note that this is not the same as the node `setTimeout` functions,
  71. which apply to pauses in data transfer over the underlying socket, or the node `timeout`
  72. option, which applies to opening the connection.
  73. ### Features missing compared to Node
  74. * `http.Agent` is only a stub
  75. * The 'socket', 'connect', 'upgrade', and 'continue' events on `http.ClientRequest`.
  76. * Any operations, including `request.setTimeout`, that operate directly on the underlying
  77. socket.
  78. * Any options that are disallowed for security reasons. This includes setting or getting
  79. certain headers.
  80. * `message.httpVersion`
  81. * `message.rawHeaders` is modified by the browser, and may not quite match what is sent by
  82. the server.
  83. * `message.trailers` and `message.rawTrailers` will remain empty.
  84. * Redirects are followed silently by the browser, so it isn't possible to access the 301/302
  85. redirect pages.
  86. * The `timeout` event/option and `setTimeout` functions, which operate on the underlying
  87. socket, are not available. However, see `options.requestTimeout` above.
  88. ## Example
  89. ``` js
  90. http.get('/bundle.js', function (res) {
  91. var div = document.getElementById('result');
  92. div.innerHTML += 'GET /beep<br>';
  93. res.on('data', function (buf) {
  94. div.innerHTML += buf;
  95. });
  96. res.on('end', function () {
  97. div.innerHTML += '<br>__END__';
  98. });
  99. })
  100. ```
  101. ## Running tests
  102. There are two sets of tests: the tests that run in Node (found in `test/node`) and the tests
  103. that run in the browser (found in `test/browser`). Normally the browser tests run on
  104. [Sauce Labs](http://saucelabs.com/).
  105. Running `npm test` will run both sets of tests, but in order for the Sauce Labs tests to run
  106. you will need to sign up for an account (free for open source projects) and put the
  107. credentials in a [`.zuulrc` file](https://github.com/defunctzombie/zuul/wiki/zuulrc).
  108. To run just the Node tests, run `npm run test-node`.
  109. To run the browser tests locally, run `npm run test-browser-local` and point your browser to
  110. `http://localhost:8080/__zuul`
  111. ## License
  112. MIT. Copyright (C) John Hiesey and other contributors.