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.

102 lines
3.4 KiB

4 years ago
  1. # concat-stream
  2. Writable stream that concatenates all the data from a stream and calls a callback with the result. Use this when you want to collect all the data from a stream into a single buffer.
  3. [![Build Status](https://travis-ci.org/maxogden/concat-stream.svg?branch=master)](https://travis-ci.org/maxogden/concat-stream)
  4. [![NPM](https://nodei.co/npm/concat-stream.png)](https://nodei.co/npm/concat-stream/)
  5. ### description
  6. Streams emit many buffers. If you want to collect all of the buffers, and when the stream ends concatenate all of the buffers together and receive a single buffer then this is the module for you.
  7. Only use this if you know you can fit all of the output of your stream into a single Buffer (e.g. in RAM).
  8. There are also `objectMode` streams that emit things other than Buffers, and you can concatenate these too. See below for details.
  9. ## Related
  10. `concat-stream` is part of the [mississippi stream utility collection](https://github.com/maxogden/mississippi) which includes more useful stream modules similar to this one.
  11. ### examples
  12. #### Buffers
  13. ```js
  14. var fs = require('fs')
  15. var concat = require('concat-stream')
  16. var readStream = fs.createReadStream('cat.png')
  17. var concatStream = concat(gotPicture)
  18. readStream.on('error', handleError)
  19. readStream.pipe(concatStream)
  20. function gotPicture(imageBuffer) {
  21. // imageBuffer is all of `cat.png` as a node.js Buffer
  22. }
  23. function handleError(err) {
  24. // handle your error appropriately here, e.g.:
  25. console.error(err) // print the error to STDERR
  26. process.exit(1) // exit program with non-zero exit code
  27. }
  28. ```
  29. #### Arrays
  30. ```js
  31. var write = concat(function(data) {})
  32. write.write([1,2,3])
  33. write.write([4,5,6])
  34. write.end()
  35. // data will be [1,2,3,4,5,6] in the above callback
  36. ```
  37. #### Uint8Arrays
  38. ```js
  39. var write = concat(function(data) {})
  40. var a = new Uint8Array(3)
  41. a[0] = 97; a[1] = 98; a[2] = 99
  42. write.write(a)
  43. write.write('!')
  44. write.end(Buffer.from('!!1'))
  45. ```
  46. See `test/` for more examples
  47. # methods
  48. ```js
  49. var concat = require('concat-stream')
  50. ```
  51. ## var writable = concat(opts={}, cb)
  52. Return a `writable` stream that will fire `cb(data)` with all of the data that
  53. was written to the stream. Data can be written to `writable` as strings,
  54. Buffers, arrays of byte integers, and Uint8Arrays.
  55. By default `concat-stream` will give you back the same data type as the type of the first buffer written to the stream. Use `opts.encoding` to set what format `data` should be returned as, e.g. if you if you don't want to rely on the built-in type checking or for some other reason.
  56. * `string` - get a string
  57. * `buffer` - get back a Buffer
  58. * `array` - get an array of byte integers
  59. * `uint8array`, `u8`, `uint8` - get back a Uint8Array
  60. * `object`, get back an array of Objects
  61. If you don't specify an encoding, and the types can't be inferred (e.g. you write things that aren't in the list above), it will try to convert concat them into a `Buffer`.
  62. If nothing is written to `writable` then `data` will be an empty array `[]`.
  63. # error handling
  64. `concat-stream` does not handle errors for you, so you must handle errors on whatever streams you pipe into `concat-stream`. This is a general rule when programming with node.js streams: always handle errors on each and every stream. Since `concat-stream` is not itself a stream it does not emit errors.
  65. We recommend using [`end-of-stream`](https://npmjs.org/end-of-stream) or [`pump`](https://npmjs.org/pump) for writing error tolerant stream code.
  66. # license
  67. MIT LICENSE