Concatenate two (or n) streams

前端 未结 10 1287
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-24 02:34
  • 2 streams:

    Given readable streams stream1 and stream2, what\'s an idiomatic (concise) way to get a stream containing

10条回答
  •  误落风尘
    2020-12-24 02:52

    this can be done with vanilla nodejs

    import { PassThrough } from 'stream'
    const merge = (...streams) => {
        let pass = new PassThrough()
        let waiting = streams.length
        for (let stream of streams) {
            pass = stream.pipe(pass, {end: false})
            stream.once('end', () => --waiting === 0 && pass.emit('end'))
        }
        return pass
    }
    

提交回复
热议问题