[6a3a178] | 1 | # minipass-collect
|
---|
| 2 |
|
---|
| 3 | A Minipass stream that collects all the data into a single chunk
|
---|
| 4 |
|
---|
| 5 | Note that this buffers ALL data written to it, so it's only good for
|
---|
| 6 | situations where you are sure the entire stream fits in memory.
|
---|
| 7 |
|
---|
| 8 | Note: this is primarily useful for the `Collect.PassThrough` class, since
|
---|
| 9 | Minipass streams already have a `.collect()` method which returns a promise
|
---|
| 10 | that resolves to the array of chunks, and a `.concat()` method that returns
|
---|
| 11 | the data concatenated into a single Buffer or String.
|
---|
| 12 |
|
---|
| 13 | ## USAGE
|
---|
| 14 |
|
---|
| 15 | ```js
|
---|
| 16 | const Collect = require('minipass-collect')
|
---|
| 17 |
|
---|
| 18 | const collector = new Collect()
|
---|
| 19 | collector.on('data', allTheData => {
|
---|
| 20 | console.log('all the data!', allTheData)
|
---|
| 21 | })
|
---|
| 22 |
|
---|
| 23 | someSourceOfData.pipe(collector)
|
---|
| 24 |
|
---|
| 25 | // note that you can also simply do:
|
---|
| 26 | someSourceOfData.pipe(new Minipass()).concat().then(data => ...)
|
---|
| 27 | // or even, if someSourceOfData is a Minipass:
|
---|
| 28 | someSourceOfData.concat().then(data => ...)
|
---|
| 29 | // but you might prefer to have it stream-shaped rather than
|
---|
| 30 | // Promise-shaped in some scenarios.
|
---|
| 31 | ```
|
---|
| 32 |
|
---|
| 33 | If you want to collect the data, but _also_ act as a passthrough stream,
|
---|
| 34 | then use `Collect.PassThrough` instead (for example to memoize streaming
|
---|
| 35 | responses), and listen on the `collect` event.
|
---|
| 36 |
|
---|
| 37 | ```js
|
---|
| 38 | const Collect = require('minipass-collect')
|
---|
| 39 |
|
---|
| 40 | const collector = new Collect.PassThrough()
|
---|
| 41 | collector.on('collect', allTheData => {
|
---|
| 42 | console.log('all the data!', allTheData)
|
---|
| 43 | })
|
---|
| 44 |
|
---|
| 45 | someSourceOfData.pipe(collector).pipe(someOtherStream)
|
---|
| 46 | ```
|
---|
| 47 |
|
---|
| 48 | All [minipass options](http://npm.im/minipass) are supported.
|
---|