• options {Object}
      • highWaterMark {number} 当调用 [stream.write()][stream-write] 开始返回 false 时的缓冲大小。 默认为 16384 (16KB), 对象模式的流默认为 16
      • decodeStrings {boolean} 是否把传入 [stream._write()][stream-_write] 的 string 编码为 Buffer,使用的字符编码为调用 [stream.write()][stream-write] 时指定的。 不转换其他类型的数据(即不将 Buffer 解码为 string)。 设置为 false 将会阻止转换 string默认值: true
      • defaultEncoding {string} 当 [stream.write()][stream-write] 的参数没有指定字符编码时默认的字符编码。默认值: 'utf8'
      • objectMode {boolean} 是否可以调用 [stream.write(anyObj)][stream-write]。 一旦设为 true,则除了字符串、BufferUint8Array,还可以写入流实现支持的其他 JavaScript 值。默认值: false
      • emitClose {boolean} 流被销毁后是否触发 'close' 事件。默认值: true
      • write {Function} 对 [stream._write()][stream-_write] 方法的实现。
      • writev {Function} 对 [stream._writev()][stream-_writev] 方法的实现。
      • destroy {Function} 对 [stream._destroy()][writable-_destroy] 方法的实现。
      • final {Function} 对 [stream._final()][stream-_final] 方法的实现。
      • autoDestroy {boolean} 此流是否应在结束后自动调用 .destroy()默认值: true.
    1. const { Writable } = require('stream');
    2. class MyWritable extends Writable {
    3. constructor(options) {
    4. // 调用 stream.Writable() 构造函数。
    5. super(options);
    6. // ...
    7. }
    8. }

    使用 ES6 之前的语法:

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

    使用简化的构造函数:

    1. const { Writable } = require('stream');
    2. const myWritable = new Writable({
    3. write(chunk, encoding, callback) {
    4. // ...
    5. },
    6. writev(chunks, callback) {
    7. // ...
    8. }
    9. });