new stream.Writable([options])
options
<Object>highWaterMark
<number>stream.write()
开始返回false
时的缓冲级别。 默认值:16384
(16KB) 或16
表示objectMode
流。decodeStrings
<boolean> 是否将传给stream.write()
的string
编码为Buffer
(使用stream.write()
调用中指定的编码),然后再将它们传给stream._write()
。 其他类型的数据不会被转换(即Buffer
不会被解码为string
)。 设置为 false 将阻止string
被转换。 默认值:true
。defaultEncoding
<string> 当没有将编码指定为stream.write()
的参数时使用的默认编码。 默认值:'utf8'
。objectMode
<boolean>stream.write(anyObj)
是否为有效操作。 当设置后,如果流实现支持,则可以写入字符串、Buffer
或Uint8Array
以外的 JavaScript 值。 默认值:false
。emitClose
<boolean> 流被销毁后是否应该触发'close'
。 默认值:true
。write
<Function>stream._write()
方法的实现。writev
<Function>stream._writev()
方法的实现。destroy
<Function>stream._destroy()
方法的实现。final
<Function>stream._final()
方法的实现。autoDestroy
<boolean> 此流是否应在结束后自动调用自身的.destroy()
。 默认值:false
。
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) {
// ...
}
});
options
<Object>highWaterMark
<number> Buffer level whenstream.write()
starts returningfalse
. Default:16384
(16KB), or16
forobjectMode
streams.decodeStrings
<boolean> Whether to encodestring
s passed tostream.write()
toBuffer
s (with the encoding specified in thestream.write()
call) before passing them tostream._write()
. Other types of data are not converted (i.e.Buffer
s are not decoded intostring
s). Setting to false will preventstring
s from being converted. Default:true
.defaultEncoding
<string> The default encoding that is used when no encoding is specified as an argument tostream.write()
. Default:'utf8'
.objectMode
<boolean> Whether or not thestream.write(anyObj)
is a valid operation. When set, it becomes possible to write JavaScript values other than string,Buffer
orUint8Array
if supported by the stream implementation. Default:false
.emitClose
<boolean> Whether or not the stream should emit'close'
after it has been destroyed. Default:true
.write
<Function> Implementation for thestream._write()
method.writev
<Function> Implementation for thestream._writev()
method.destroy
<Function> Implementation for thestream._destroy()
method.final
<Function> Implementation for thestream._final()
method.autoDestroy
<boolean> Whether this stream should automatically call.destroy()
on itself after ending. Default:false
.
const { Writable } = require('stream');
class MyWritable extends Writable {
constructor(options) {
// Calls the stream.Writable() constructor.
super(options);
// ...
}
}
Or, when using pre-ES6 style constructors:
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);
Or, using the simplified constructor approach:
const { Writable } = require('stream');
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
}
});