连接两个(或 n 个)流 [英] Concatenate two (or n) streams

查看:30
本文介绍了连接两个(或 n 个)流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 2 个流:

  • 2 streams:

给定可读的 streams stream1stream2,什么是获取包含 stream1stream2 连接的流的惯用(简洁)方法?

Given readable streams stream1 and stream2, what's an idiomatic (concise) way to get a stream containing stream1 and stream2 concatenated?

我不能做 stream1.pipe(outStream);stream2.pipe(outStream),因为这样流的内容就混杂在一起了.

I cannot do stream1.pipe(outStream); stream2.pipe(outStream), because then the stream contents are jumbled together.

n 个流:

给定一个 EventEmitter 发出不确定数量的流,例如

Given an EventEmitter that emits an indeterminate number of streams, e.g.

eventEmitter.emit('stream', stream1)
eventEmitter.emit('stream', stream2)
eventEmitter.emit('stream', stream3)
...
eventEmitter.emit('end')

获得所有流连接在一起的流的惯用(简洁)方法是什么?

what's an idiomatic (concise) way to get a stream with all streams concatenated together?

推荐答案

combined-stream 包连接流.README 中的示例:

The combined-stream package concatenates streams. Example from the README:

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'));

我相信您必须一次附加所有流.如果队列为空,combinedStream 会自动结束.请参阅问题 #5.

I believe you have to append all streams at once. If the queue runs empty, the combinedStream automatically ends. See issue #5.

stream-stream 库是一种具有显式 的替代方案.结束,但它不太受欢迎,并且可能没有经过充分测试.它使用 Node 0.10 的streams2 API(请参阅此讨论).

The stream-stream library is an alternative that has an explicit .end, but it's much less popular and presumably not as well-tested. It uses the streams2 API of Node 0.10 (see this discussion).

这篇关于连接两个(或 n 个)流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆