简单的实现


对于许多简单的情况,可以在不依赖继承的情况下构造流。 这可以通过直接创建 stream.Writablestream.Readablestream.Duplexstream.Transform 对象的实例并传入适当的方法作为构造函数选项来实现。

const { Writable } = require('stream');

const myWritable = new Writable({
  write(chunk, encoding, callback) {
    // ...
  }
});

For many simple cases, it is possible to construct a stream without relying on inheritance. This can be accomplished by directly creating instances of the stream.Writable, stream.Readable, stream.Duplex or stream.Transform objects and passing appropriate methods as constructor options.

const { Writable } = require('stream');

const myWritable = new Writable({
  write(chunk, encoding, callback) {
    // ...
  }
});