UNPKG

1.62 kBPlain TextView Raw
1import { Writable } from 'stream'
2import { readableCreate, ReadableTyped, _pipeline } from '../..'
3import { TransformOptions, WritableTyped } from '../stream.model'
4
5/**
6 * Allows "forking" a stream inside pipeline into a number of pipeline chains (2 or more).
7 * Currently does NOT (!) maintain backpressure.
8 * Error in the forked pipeline will propagate up to the main pipeline (and log error, to be sure).
9 * Will wait until all forked pipelines are completed before completing the stream.
10 */
11export function writableFork<T>(
12 chains: NodeJS.WritableStream[][],
13 opt?: TransformOptions,
14): WritableTyped<T> {
15 const readables: ReadableTyped<T>[] = []
16
17 const allChainsDone = Promise.all(
18 chains.map(async chain => {
19 const readable = readableCreate<T>()
20 readables.push(readable)
21
22 return await _pipeline([readable, ...chain])
23 }),
24 ).catch(err => {
25 console.error(err) // ensure the error is logged
26 throw err
27 })
28
29 return new Writable({
30 objectMode: true,
31 ...opt,
32 write(chunk: T, _encoding, cb) {
33 // Push/fork to all sub-streams
34 // No backpressure is ensured here, it'll push regardless of the
35 readables.forEach(readable => readable.push(chunk))
36
37 cb()
38 },
39 async final(cb) {
40 try {
41 // Push null (complete) to all sub-streams
42 readables.forEach(readable => readable.push(null))
43
44 console.log(`writableFork.final is waiting for all chains to be done`)
45 await allChainsDone
46 console.log(`writableFork.final all chains done`)
47 cb()
48 } catch (err) {
49 cb(err)
50 }
51 },
52 })
53}