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.

123 lines
3.9 KiB

4 years ago
  1. # get-stream [![Build Status](https://travis-ci.org/sindresorhus/get-stream.svg?branch=master)](https://travis-ci.org/sindresorhus/get-stream)
  2. > Get a stream as a string, buffer, or array
  3. ## Install
  4. ```
  5. $ npm install get-stream
  6. ```
  7. ## Usage
  8. ```js
  9. const fs = require('fs');
  10. const getStream = require('get-stream');
  11. (async () => {
  12. const stream = fs.createReadStream('unicorn.txt');
  13. console.log(await getStream(stream));
  14. /*
  15. ,,))))))));,
  16. __)))))))))))))),
  17. \|/ -\(((((''''((((((((.
  18. -*-==//////(('' . `)))))),
  19. /|\ ))| o ;-. '((((( ,(,
  20. ( `| / ) ;))))' ,_))^;(~
  21. | | | ,))((((_ _____------~~~-. %,;(;(>';'~
  22. o_); ; )))(((` ~---~ `:: \ %%~~)(v;(`('~
  23. ; ''''```` `: `:::|\,__,%% );`'; ~
  24. | _ ) / `:|`----' `-'
  25. ______/\/~ | / /
  26. /~;;.____/;;' / ___--,-( `;;;/
  27. / // _;______;'------~~~~~ /;;/\ /
  28. // | | / ; \;;,\
  29. (<_ | ; /',/-----' _>
  30. \_| ||_ //~;~~~~~~~~~
  31. `\_| (,~~
  32. \~\
  33. ~~
  34. */
  35. })();
  36. ```
  37. ## API
  38. The methods returns a promise that resolves when the `end` event fires on the stream, indicating that there is no more data to be read. The stream is switched to flowing mode.
  39. ### getStream(stream, [options])
  40. Get the `stream` as a string.
  41. #### options
  42. Type: `Object`
  43. ##### encoding
  44. Type: `string`<br>
  45. Default: `utf8`
  46. [Encoding](https://nodejs.org/api/buffer.html#buffer_buffer) of the incoming stream.
  47. ##### maxBuffer
  48. Type: `number`<br>
  49. Default: `Infinity`
  50. Maximum length of the returned string. If it exceeds this value before the stream ends, the promise will be rejected with a `getStream.MaxBufferError` error.
  51. ### getStream.buffer(stream, [options])
  52. Get the `stream` as a buffer.
  53. It honors the `maxBuffer` option as above, but it refers to byte length rather than string length.
  54. ### getStream.array(stream, [options])
  55. Get the `stream` as an array of values.
  56. It honors both the `maxBuffer` and `encoding` options. The behavior changes slightly based on the encoding chosen:
  57. - When `encoding` is unset, it assumes an [object mode stream](https://nodesource.com/blog/understanding-object-streams/) and collects values emitted from `stream` unmodified. In this case `maxBuffer` refers to the number of items in the array (not the sum of their sizes).
  58. - When `encoding` is set to `buffer`, it collects an array of buffers. `maxBuffer` refers to the summed byte lengths of every buffer in the array.
  59. - When `encoding` is set to anything else, it collects an array of strings. `maxBuffer` refers to the summed character lengths of every string in the array.
  60. ## Errors
  61. If the input stream emits an `error` event, the promise will be rejected with the error. The buffered data will be attached to the `bufferedData` property of the error.
  62. ```js
  63. (async () => {
  64. try {
  65. await getStream(streamThatErrorsAtTheEnd('unicorn'));
  66. } catch (error) {
  67. console.log(error.bufferedData);
  68. //=> 'unicorn'
  69. }
  70. })()
  71. ```
  72. ## FAQ
  73. ### How is this different from [`concat-stream`](https://github.com/maxogden/concat-stream)?
  74. This module accepts a stream instead of being one and returns a promise instead of using a callback. The API is simpler and it only supports returning a string, buffer, or array. It doesn't have a fragile type inference. You explicitly choose what you want. And it doesn't depend on the huge `readable-stream` package.
  75. ## Related
  76. - [get-stdin](https://github.com/sindresorhus/get-stdin) - Get stdin as a string or buffer
  77. ## License
  78. MIT © [Sindre Sorhus](https://sindresorhus.com)