• options {Object}
      • highWaterMark {number} 从底层资源读取数据并存储在内部缓冲区中的最大[字节数][hwm-gotcha]。 默认值: 16384 (16KB), 对象模式的流默认为 16
      • encoding {string} 如果指定了,则使用指定的字符编码将 buffer 解码成字符串。 默认值: null
      • objectMode {boolean} 流是否可以是一个对象流。 也就是说 [stream.read(n)][stream-read] 会返回对象而不是 Buffer默认值: false
      • emitClose {boolean} 流被销毁后是否应该触发 'close'默认值: true
      • read {Function} 对 [stream._read()][stream-_read] 方法的实现。
      • destroy {Function} 对 [stream._destroy()][readable-_destroy] 方法的实现。
      • autoDestroy {boolean} 流是否应在结束后自动调用 .destroy()默认值: true
    1. const { Readable } = require('stream');
    2. class MyReadable extends Readable {
    3. constructor(options) {
    4. // 调用 stream.Readable(options) 构造函数。
    5. super(options);
    6. // ...
    7. }
    8. }

    使用 ES6 之前的语法:

    1. const { Readable } = require('stream');
    2. const util = require('util');
    3. function MyReadable(options) {
    4. if (!(this instanceof MyReadable))
    5. return new MyReadable(options);
    6. Readable.call(this, options);
    7. }
    8. util.inherits(MyReadable, Readable);

    使用简化的构造函数:

    1. const { Readable } = require('stream');
    2. const myReadable = new Readable({
    3. read(size) {
    4. // ...
    5. }
    6. });