new stream.Writable([options])


版本历史
版本变更
v22.0.0

凹凸默认高水位标记。

v15.5.0

支持传入中止信号。

v14.0.0

autoDestroy 选项默认更改为 true

v11.2.0, v10.16.0

添加 autoDestroy 选项以在流触发 'finish' 或错误时自动对流进行 destroy()

v10.0.0

添加 emitClose 选项以指定是否在销毁时触发 'close'

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) {
 // ...
 },
}); 

在对应于传递的 AbortSignalAbortController 上调用 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(); 

AltStyle によって変換されたページ (->オリジナル) /