combined-stream

中文文档
1.0.8发布于 2019/05/13MITCommonJS近 30 天下载 5.6亿

A stream that emits multiple other streams one after another.

原始使用文档
文档目录14 节
  1. Installation
  2. Usage
  3. API
  4. CombinedStream.create([options])
  5. combinedStream.pauseStreams = true
  6. combinedStream.maxDataSize = 2 * 1024 * 1024
  7. combinedStream.dataSize = 0
  8. combinedStream.append(stream)
  9. combinedStream.write(data)
  10. combinedStream.resume()
  11. combinedStream.pause();
  12. combinedStream.end();
  13. combinedStream.destroy();
  14. License

combined-stream

A stream that emits multiple other streams one after another.

NB Currently combined-stream works with streams version 1 only. There is ongoing effort to switch this library to streams version 2. Any help is welcome. :) Meanwhile you can explore other libraries that provide streams2 support with more or less compatibility with combined-stream.

  • combined-stream2: A drop-in streams2-compatible replacement for the combined-stream module.

  • multistream: A stream that emits multiple other streams one after another.

Installation

npm install combined-stream

Usage

Here is a simple example that shows how you can use combined-stream to combine two files into one:

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

While the example above works great, it will pause all source streams until they are needed. If you don't want that to happen, you can set pauseStreams to false:

var CombinedStream = require('combined-stream');
var fs = require('fs');

var combinedStream = CombinedStream.create({pauseStreams: false});
combinedStream.append(fs.createReadStream('file1.txt'));
combinedStream.append(fs.createReadStream('file2.txt'));

combinedStream.pipe(fs.createWriteStream('combined.txt'));

However, what if you don't have all the source streams yet, or you don't want to allocate the resources (file descriptors, memory, etc.) for them right away? Well, in that case you can simply provide a callback that supplies the stream by calling a next() function:

var CombinedStream = require('combined-stream');
var fs = require('fs');

var combinedStream = CombinedStream.create();
combinedStream.append(function(next) {
  next(fs.createReadStream('file1.txt'));
});
combinedStream.append(function(next) {
  next(fs.createReadStream('file2.txt'));
});

combinedStream.pipe(fs.createWriteStream('combined.txt'));

API

CombinedStream.create([options])

Returns a new combined stream object. Available options are:

  • maxDataSize
  • pauseStreams

The effect of those options is described below.

combinedStream.pauseStreams = true

Whether to apply back pressure to the underlaying streams. If set to false, the underlaying streams will never be paused. If set to true, the underlaying streams will be paused right after being appended, as well as when delayedStream.pipe() wants to throttle.

combinedStream.maxDataSize = 2 * 1024 * 1024

The maximum amount of bytes (or characters) to buffer for all source streams. If this value is exceeded, combinedStream emits an 'error' event.

combinedStream.dataSize = 0

The amount of bytes (or characters) currently buffered by combinedStream.

combinedStream.append(stream)

Appends the given stream to the combinedStream object. If pauseStreams is set to `true, this stream will also be paused right away.

streams can also be a function that takes one parameter called next. next is a function that must be invoked in order to provide the next stream, see example above.

Regardless of how the stream is appended, combined-stream always attaches an 'error' listener to it, so you don't have to do that manually.

Special case: stream can also be a String or Buffer.

combinedStream.write(data)

You should not call this, combinedStream takes care of piping the appended streams into itself for you.

combinedStream.resume()

Causes combinedStream to start drain the streams it manages. The function is idempotent, and also emits a 'resume' event each time which usually goes to the stream that is currently being drained.

combinedStream.pause();

If combinedStream.pauseStreams is set to false, this does nothing. Otherwise a 'pause' event is emitted, this goes to the stream that is currently being drained, so you can use it to apply back pressure.

combinedStream.end();

Sets combinedStream.writable to false, emits an 'end' event, and removes all streams from the queue.

combinedStream.destroy();

Same as combinedStream.end(), except it emits a 'close' event instead of 'end'.

License

combined-stream is licensed under the MIT license.

版本与发布记录

combined-stream 共发布 18 个版本,首次发布于 2011/05/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.8latest2019/05/137 年前
1.0.72018/09/198 年前
1.0.62018/02/148 年前
1.0.6-rc1next2018/02/138 年前
1.0.52015/06/1511 年前
1.0.42015/06/1411 年前

关于 combined-stream 的常见问题

combined-stream 是什么?

A stream that emits multiple other streams one after another.

如何安装 combined-stream?

在项目目录运行 npm install combined-stream,或使用 pnpm add / yarn add / bun add。

combined-stream 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/combined-stream 是否存在。

combined-stream 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

combined-stream 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 0.8。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

combined-stream 可以商用吗?

combined-stream 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

combined-stream 的下载量有多少?

根据 npm 官方下载统计,combined-stream 在 2026/10/07 – 2026/11/05 这 30 天内被下载 556,343,644 次,日均约 18,544,788 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

combined-stream 还在维护吗?

最新版本 1.0.8 发布于 2019/05/13(7 年前);至今共发布 18 个版本,其中过去 12 个月发布了 0 个;首次发布于 2011/05/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。