readable.wrap(stream)
在 Node.js 0.10 之前,流并没有实现当前定义的整个 node:stream 模块 API。(有关更多信息,请参见 兼容性。)
【Prior to Node.js 0.10, streams did not implement the entire node:stream
module API as it is currently defined. (See Compatibility for more
information.)】
在使用一个发出 'data' 事件并且具有仅供参考的 stream.pause() 方法的旧 Node.js 库时,可以使用 readable.wrap() 方法创建一个 Readable 流,该流以旧流作为其数据源。
【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.】
很少需要使用 readable.wrap(),但提供该方法是为了方便与旧版 Node.js 应用和库的交互。
【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('node:stream');
const oreader = new OldReader();
const myReader = new Readable().wrap(oreader);
myReader.on('readable', () => {
myReader.read(); // etc.
});