有条件地在Node.js中管道流

朱利安

我想有条件地以一种不错的方式管道流。我要实现的行为如下:

if (someBoolean) {

  stream = fs
    .createReadStream(filepath)
    .pipe(decodeStream(someOptions))
    .pipe(writeStream);

} else {

  stream = fs
    .createReadStream(filepath)
    .pipe(writeStream);

}

因此,我准备了所有流,如果someBoolean为true,则想向管道中添加其他流。

然后我以为我找到了一个绕行流的解决方案,但是不幸的是没有成功进行设置。我使用了类似于gulp-if的表示法,因为这是作为启发而提到的:

var detour = require('detour-stream');

stream = fs
  .createReadStream(filepath)
  .detour(someBoolean, decodeStream(someOptions))
  .pipe(writeStream);

但是不幸的是,这只会导致错误:

  .detour(someBoolean, decodeStream(someOptions))
 ^
TypeError: undefined is not a function

有任何想法吗?

阿尔贝托·里尔

detour是创建可写流的函数:https : //nodejs.org/api/stream.html#stream_visible_pipe_destination_options

因此,从您的示例来看,这应该可行:

var detour = require('detour-stream');

stream = fs
  .createReadStream(filepath)
  .pipe(detour(someBoolean, decodeStream(someOptions))) // just pipe it
  .pipe(writeStream);

x-发布于https://github.com/dashed/detour-stream/issues/2#issuecomment-231423878

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章