• options {Object} 同时传给 WritableReadable 的构造函数。
      • allowHalfOpen {boolean} 如果设为 false,则当可读端结束时,可写端也会自动结束。 默认为 true
      • readable {boolean} Sets whether the Duplex should be readable. Default: true.
      • writable {boolean} Sets whether the Duplex should be writable. Default: true.
      • readableObjectMode {boolean} 设置流的可读端为 objectMode。 如果 objectModetrue,则不起作用。 默认为 false
      • writableObjectMode {boolean} 设置流的可写端为 objectMode。 如果 objectModetrue,则不起作用。 默认为 false
      • readableHighWaterMark {number} 设置流的可读端的 highWaterMark。 如果已经设置了 highWaterMark,则不起作用。
      • writableHighWaterMark {number} 设置流的可写端的 highWaterMark。 如果已经设置了 highWaterMark,则不起作用。
    1. const { Duplex } = require('stream');
    2. class MyDuplex extends Duplex {
    3. constructor(options) {
    4. super(options);
    5. // ...
    6. }
    7. }

    使用 ES6 之前的语法:

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

    使用简化的构造函数:

    1. const { Duplex } = require('stream');
    2. const myDuplex = new Duplex({
    3. read(size) {
    4. // ...
    5. },
    6. write(chunk, encoding, callback) {
    7. // ...
    8. }
    9. });