readable.wrap(stream)


在 Node.js v0.10 之前,流没有实现当前定义的所有的流模块 API。(详见兼容性

当使用老版本的 Node.js 时,只能触发 'data' 事件或调用 stream.pause() 方法,可以使用 readable.wrap() 创建老版本的流作为数据源。

现在几乎无需使用 readable.wrap(),该方法主要用于老版本的 Node.js 应用和库。

例子:

const { OldReader } = require('./old-api-module.js');
const { Readable } = require('stream');
const oreader = new OldReader();
const myReader = new Readable().wrap(oreader);

myReader.on('readable', () => {
  myReader.read(); // 各种操作。
});

Prior to Node.js 0.10, streams did not implement the entire stream module API as it is currently defined. (See Compatibility for more information.)

When using an older Node.js library that emits 'data' events and has a stream.pause() method that is advisory only, the readable.wrap() method can be used to create a Readable stream that uses the old stream as its data source.

It will rarely be necessary to use readable.wrap() but the method has been provided as a convenience for interacting with older Node.js applications and libraries.

const { OldReader } = require('./old-api-module.js');
const { Readable } = require('stream');
const oreader = new OldReader();
const myReader = new Readable().wrap(oreader);

myReader.on('readable', () => {
  myReader.read(); // etc.
});