What is ordered-read-streams?
The ordered-read-streams npm package allows you to combine multiple readable streams into a single readable stream, ensuring that the data from each stream is read in the order the streams were provided. This can be particularly useful when you need to process multiple streams sequentially.
What are ordered-read-streams's main functionalities?
Combining Multiple Streams
This feature allows you to combine multiple readable streams into a single stream. The data from each stream is read in the order the streams were provided. In this example, the contents of 'file1.txt', 'file2.txt', and 'file3.txt' are read sequentially and output to the console.
const OrderedReadStreams = require('ordered-read-streams');
const fs = require('fs');
const stream1 = fs.createReadStream('file1.txt');
const stream2 = fs.createReadStream('file2.txt');
const stream3 = fs.createReadStream('file3.txt');
const combinedStream = new OrderedReadStreams([stream1, stream2, stream3]);
combinedStream.on('data', (chunk) => {
console.log(chunk.toString());
});
combinedStream.on('end', () => {
console.log('All streams have been read in order.');
});
Other packages similar to ordered-read-streams
multistream
The multistream package also allows you to combine multiple streams into a single stream. However, unlike ordered-read-streams, multistream provides more flexibility by allowing you to dynamically add streams during the reading process. This can be useful if the streams to be combined are not known upfront.
merge-stream
The merge-stream package allows you to merge multiple streams into one. Unlike ordered-read-streams, merge-stream does not guarantee the order of the streams. It is useful when you need to process multiple streams concurrently and do not require the data to be in a specific order.
stream-combiner2
The stream-combiner2 package allows you to combine multiple streams into a pipeline. While it does not specifically focus on maintaining the order of streams, it is useful for creating complex stream processing pipelines where the output of one stream is the input to another.
ordered-read-streams
Combines array of streams into one Readable stream in strict order.
Usage
var { Readable } = require('streamx');
var ordered = require('ordered-read-streams');
var s1 = new Readable({
read: function (cb) {
var self = this;
if (self.called) {
self.push(null);
return cb(null);
}
setTimeout(function () {
self.called = true;
self.push('stream 1');
cb(null);
}, 200);
},
});
var s2 = new Readable({
read: function (cb) {
var self = this;
if (self.called) {
self.push(null);
return cb(null);
}
setTimeout(function () {
self.called = true;
self.push('stream 2');
cb(null);
}, 30);
},
});
var s3 = new Readable({
read: function (cb) {
var self = this;
if (self.called) {
self.push(null);
return cb(null);
}
setTimeout(function () {
self.called = true;
self.push('stream 3');
cb(null);
}, 100);
},
});
var readable = ordered([s1, s2, s3]);
readable.on('data', function (data) {
console.log(data);
});
API
ordered(streams, [options])
Takes an array of Readable
streams and produces a single OrderedReadable
stream that will consume the provided streams in strict order. The produced Readable
stream respects backpressure on itself and any provided streams.
orderedReadable.addSource(stream)
The returned Readable
stream has an addSource
instance function that takes appends a Readable
stream to the list of source streams that the OrderedReadable
is reading from.
License
MIT