Reputation: 4609
I am doing exercises from the "stream-adventure", and I'm very uncertain about the stream-combiner
module.
Specifically it says that:
The
stream-combiner
module creates a pipeline from a list of streams, returning a single stream that exposes the first stream as the writable side and the last stream as the readable side like theduplexer
module, but with an arbitrary number of streams in between. Unlike theduplexer
module, each stream is piped to the next. For example:var combine = require('stream-combiner'); var stream = combine(a, b, c, d);
will internally do
a.pipe(b).pipe(c).pipe(d)
but thestream
returned bycombine()
has its writable side hooked intoa
and its readable side hooked intod
.
Now as it says, "its writable side hooked into a
and its readable side hooked into d
", one can use the above stream
like follows:
someReadableStream.pipe(stream).pipe(someWritableStream)
But won't the above just become:
someReadableStream.pipe(a).pipe(b).pipe(c).pipe(d).pipe(someWritableStream)
My question is how can a readable stream pipe to another readable stream. And at the end, how can the result of piping to a writable stream again pipe to a writable stream.
Upvotes: 2
Views: 1258
Reputation: 571
you are correct that it is the same as readable.pipe(a).pipe(b).pipe(c).pipe(d).pipe(writable)
,
the advantage of combiner is that you cannot return a.pipe(b).pipe(c).pipe(d)
from a function, so you must always manually connect them. with combiner you can create a stream out of other streams, and then tidy that away into a function or module.
for this to work, though, a,b,c,d all must be transform streams, that are both readable and writable.
Upvotes: 0
Reputation: 146134
Here's a textual example that hopefully clarifies the rules about the streams in order for stream-combiner to do something useful:
var rw = combine(r_or_rw, rw1, rw2, rw3, ...rwN, w_or_rw)
So the first stream needs to be at least readable, the interior streams all need to be read/write, and the final stream needs to at least be writeable. The returned new stream is read/write. When you write to the returned stream, that sends new data down through the pipeline, and when you read that gives you data from the end of the pipeline.
Upvotes: 1