A stream that emits multiple other streams one after another.
A stream that emits multiple other streams one after another.
NB Currently
combined-streamworks with streams version 1 only. There is ongoing effort to switch this library to streams version 2. Any help is welcome. :) Meanwhile you can explore other libraries that provide streams2 support with more or less compatibility with
combined-stream.
combined-stream2: A drop-in streams2-compatible replacement for the combined-stream module.
multistream: A stream that emits multiple other streams one after another.
npm install combined-stream
Here is a simple example that shows how you can use combined-stream to combine two files into one:
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'));
While the example above works great, it will pause all source streams until they are needed. If you don't want that to happen, you can set
pauseStreamsto
false:
var CombinedStream = require('combined-stream'); var fs = require('fs');var combinedStream = CombinedStream.create({pauseStreams: false}); combinedStream.append(fs.createReadStream('file1.txt')); combinedStream.append(fs.createReadStream('file2.txt'));
combinedStream.pipe(fs.createWriteStream('combined.txt'));
However, what if you don't have all the source streams yet, or you don't want to allocate the resources (file descriptors, memory, etc.) for them right away? Well, in that case you can simply provide a callback that supplies the stream by calling a
next()function:
var CombinedStream = require('combined-stream'); var fs = require('fs');var combinedStream = CombinedStream.create(); combinedStream.append(function(next) { next(fs.createReadStream('file1.txt')); }); combinedStream.append(function(next) { next(fs.createReadStream('file2.txt')); });
combinedStream.pipe(fs.createWriteStream('combined.txt'));
Returns a new combined stream object. Available options are:
maxDataSize
pauseStreams
The effect of those options is described below.
true
Whether to apply back pressure to the underlaying streams. If set to
false, the underlaying streams will never be paused. If set to
true, the underlaying streams will be paused right after being appended, as well as when
delayedStream.pipe()wants to throttle.
2 * 1024 * 1024
The maximum amount of bytes (or characters) to buffer for all source streams. If this value is exceeded,
combinedStreamemits an
'error'event.
0
The amount of bytes (or characters) currently buffered by
combinedStream.
Appends the given
streamto the combinedStream object. If
pauseStreamsis set to `true, this stream will also be paused right away.
streamscan also be a function that takes one parameter called
next.
nextis a function that must be invoked in order to provide the
nextstream, see example above.
Regardless of how the
streamis appended, combined-stream always attaches an
'error'listener to it, so you don't have to do that manually.
Special case:
streamcan also be a String or Buffer.
You should not call this,
combinedStreamtakes care of piping the appended streams into itself for you.
Causes
combinedStreamto start drain the streams it manages. The function is idempotent, and also emits a
'resume'event each time which usually goes to the stream that is currently being drained.
If
combinedStream.pauseStreamsis set to
false, this does nothing. Otherwise a
'pause'event is emitted, this goes to the stream that is currently being drained, so you can use it to apply back pressure.
Sets
combinedStream.writableto false, emits an
'end'event, and removes all streams from the queue.
Same as
combinedStream.end(), except it emits a
'close'event instead of
'end'.
combined-stream is licensed under the MIT license.