new stream.Writable([options])
版本历史
| 版本 | 变更 |
|---|---|
| v22.0.0 | 凹凸默认高水位标记。 |
| v15.5.0 | 支持传入中止信号。 |
| v14.0.0 | 将 |
| v11.2.0, v10.16.0 | 添加 |
| v10.0.0 | 添加 |
-
options<Object>-
highWaterMark<number>stream.write()开始返回false时的缓冲级别。默认值:65536(64 KiB),或16用于objectMode流。\
highWaterMark<number> Buffer level whenstream.write()starts returningfalse. Default:65536(64 KiB), or16forobjectModestreams. -
decodeStrings<boolean> 是否将传递给stream.write()的string编码为Buffer(使用stream.write()调用中指定的编码),然后再将它们传递给stream._write()。不转换其他类型的数据(即Buffer不解码为string)。设置为 false 将阻止string被转换。默认值:true。\
decodeStrings<boolean> Whether to encodestrings passed tostream.write()toBuffers (with the encoding specified in thestream.write()call) before passing them tostream._write(). Other types of data are not converted (i.e.Buffers are not decoded intostrings). Setting to false will preventstrings from being converted. Default:true. -
defaultEncoding<string> 当没有将编码指定为stream.write()的参数时使用的默认编码。默认值:'utf8'。\
defaultEncoding<string> The default encoding that is used when no encoding is specified as an argument tostream.write(). Default:'utf8'. -
objectMode<boolean>stream.write(anyObj)是否为有效操作。设置后,如果流实现支持,则可以写入除字符串、<Buffer>、<TypedArray> 或 <DataView> 之外的 JavaScript 值。默认值:false。\
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>, <TypedArray> or <DataView> if supported by the stream implementation. Default:false. -
emitClose<boolean> 流被销毁后是否应该触发'close'。默认值:true。\
emitClose<boolean> Whether or not the stream should emit'close'after it has been destroyed. Default:true. -
write<Function>stream._write()方法的实现。\
write<Function> Implementation for thestream._write()method. -
writev<Function>stream._writev()方法的实现。\
writev<Function> Implementation for thestream._writev()method. -
destroy<Function>stream._destroy()方法的实现。\
destroy<Function> Implementation for thestream._destroy()method. -
final<Function>stream._final()方法的实现。\
final<Function> Implementation for thestream._final()method. -
construct<Function>stream._construct()方法的实现。\
construct<Function> Implementation for thestream._construct()method. -
autoDestroy<boolean> 此流是否应在结束后自动调用自身的.destroy()。默认值:true。\
autoDestroy<boolean> Whether this stream should automatically call.destroy()on itself after ending. Default:true. -
signal<AbortSignal> 表示可能取消的信号。\
signal<AbortSignal> A signal representing possible cancellation.
-
const { Writable } = require('node:stream');
class MyWritable extends Writable {
constructor(options) {
// Calls the stream.Writable() constructor.
super(options);
// ...
}
} 或者,当使用 ES6 之前的样式构造函数时:
\Or, when using pre-ES6 style constructors:
const { Writable } = require('node:stream');
const util = require('node: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('node:stream');
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
},
}); 在对应于传递的 AbortSignal 的 AbortController 上调用 abort 的行为方式与在可写流上调用 .destroy(new AbortError()) 的方式相同。
\Calling abort on the AbortController corresponding to the passed
AbortSignal will behave the same way as calling .destroy(new AbortError())
on the writeable stream.
const { Writable } = require('node:stream');
const controller = new AbortController();
const myWritable = new Writable({
write(chunk, encoding, callback) {
// ...
},
writev(chunks, callback) {
// ...
},
signal: controller.signal,
});
// Later, abort the operation closing the stream
controller.abort();