/**
 *  Take an array of streams, pipe the output
 *  from each one into the next, listening for
 *  errors from any of the streams, and then resolve
 *  the promise once the final stream has finished
 *  writing/reading.
 *
 *  If the last stream is readable, it's final value
 *  will be provided as the promise value.
 *
 *  Errors emitted from any stream will cause
 *  the promise to be rejected with that error.
 */ import { pipeline , Writable } from 'stream' ; import { promisify } from 'util' ; const asyncPipeline = promisify ( pipeline ) ; export async function createPromiseFromStreams < T = any > ( streams ) { let finalChunk ; const last = streams [ streams . length - 1 ] ; if ( typeof last . read !== 'function' && streams . length === 1 ) { throw new Error ( 'A minimum of 2 streams is required when a non-readable stream is given' ) ; } if ( typeof last . read === 'function' ) { streams . push ( new Writable ( { objectMode : true , write ( chunk , _ , done ) { finalChunk = chunk ; done ( ) ; } , } ) ) ; } await asyncPipeline ( ... ( streams as [ any ] ) ) ; return finalChunk ; }