1 | import { Transform } from 'stream'
|
2 | import { TransformOptions, TransformTyped } from '../stream.model'
|
3 |
|
4 | /**
|
5 | * Will collect all stream results in the array (keeping it in memory) and emit in the end as one result.
|
6 | */
|
7 | export function transformToArray<IN>(opt: TransformOptions = {}): TransformTyped<IN, IN[]> {
|
8 | const res: IN[] = []
|
9 |
|
10 | return new Transform({
|
11 | objectMode: true,
|
12 | ...opt,
|
13 | transform(chunk: IN, _encoding, cb) {
|
14 | res.push(chunk)
|
15 | // callback to signal that we processed input, but not emitting any output
|
16 | cb()
|
17 | },
|
18 | final(this: Transform, cb) {
|
19 | this.push(res)
|
20 | cb()
|
21 | },
|
22 | })
|
23 | }
|