I want to conditionally pipe streams in a nice way. The behaviour I want to achieve is the following:
if (someBoolean) {
stream = fs
.createReadStream(filepath)
.pipe(decodeStream(someOptions))
.pipe(writeStream);
} else {
stream = fs
.createReadStream(filepath)
.pipe(writeStream);
}
So I prepared all my streams, and if someBoolean
is true, I want to add an additional stream to the pipe.
Then I thought I found a solution with detour-stream, but unfortunately didn't manage to set this up. I used notation similar to gulp-if, because this was mentioned as inspiration:
var detour = require('detour-stream');
stream = fs
.createReadStream(filepath)
.detour(someBoolean, decodeStream(someOptions))
.pipe(writeStream);
But this unfortunately only results in an error:
.detour(someBoolean, decodeStream(someOptions))
^
TypeError: undefined is not a function
Any ideas?
pipe() is file streams. In Node. js, fs. createReadStream() and fs. createWriteStream() are used to create a stream to an open file descriptor.
The Node. js Stream API has been around for a long time and is used as a uniform API for reading and writing asynchronous data.
A stream pipe is a UNIX interprocess communication (IPC) facility that allows processes on the same computer to communicate with each other.
detour
is a function that creates a writable stream: https://nodejs.org/api/stream.html#stream_readable_pipe_destination_options
Thus, from your example, this should work:
var detour = require('detour-stream');
stream = fs
.createReadStream(filepath)
.pipe(detour(someBoolean, decodeStream(someOptions))) // just pipe it
.pipe(writeStream);
x-posted from https://github.com/dashed/detour-stream/issues/2#issuecomment-231423878
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With