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,则除了字符串、Buffer或Uint8Array,还可以写入流实现支持的其他 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.
const { Writable } = require('stream');class MyWritable extends Writable {constructor(options) {// 调用 stream.Writable() 构造函数。super(options);// ...}}
使用 ES6 之前的语法:
const { Writable } = require('stream');const util = require('util');function MyWritable(options) {if (!(this instanceof MyWritable))return new MyWritable(options);Writable.call(this, options);}util.inherits(MyWritable, Writable);
使用简化的构造函数:
const { Writable } = require('stream');const myWritable = new Writable({write(chunk, encoding, callback) {// ...},writev(chunks, callback) {// ...}});
