Concatenate two (or n) streams

前端 未结 10 1265
佛祖请我去吃肉
佛祖请我去吃肉 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

    The combined-stream package concatenates streams. Example from the README:

    var CombinedStream = require('combined-stream');
    var fs = require('fs');
    
    var combinedStream = CombinedStream.create();
    combinedStream.append(fs.createReadStream('file1.txt'));
    combinedStream.append(fs.createReadStream('file2.txt'));
    
    combinedStream.pipe(fs.createWriteStream('combined.txt'));
    

    I believe you have to append all streams at once. If the queue runs empty, the combinedStream automatically ends. See issue #5.

    The stream-stream library is an alternative that has an explicit .end, but it's much less popular and presumably not as well-tested. It uses the streams2 API of Node 0.10 (see this discussion).

提交回复
热议问题