new stream.Writable([options])


const { Writable } = require('node:stream');

class MyWritable extends Writable {
  constructor(options) {
    // 调用 stream.Writable() 构造函数。
    super(options);
    // ...
  }
}

或者,当使用 ES6 之前的风格构造函数时:

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);

或者,使用简化的构造函数方法:

const { Writable } = require('node:stream');

const myWritable = new Writable({
  write(chunk, encoding, callback) {
    // ...
  },
  writev(chunks, callback) {
    // ...
  }
});

在与传入的 AbortSignal 对应的 AbortController 上调用 abort 的行为与在可写流上调用 .destroy(new AbortError()) 的行为相同。

const { Writable } = require('node:stream');

const controller = new AbortController();
const myWritable = new Writable({
  write(chunk, encoding, callback) {
    // ...
  },
  writev(chunks, callback) {
    // ...
  },
  signal: controller.signal
});
// 稍后,中止关闭流的操作
controller.abort();
  • options <Object>
    • highWaterMark <number> Buffer level when stream.write() starts returning false. Default: 16384 (16 KiB), or 16 for objectMode streams.
    • decodeStrings <boolean> Whether to encode strings passed to stream.write() to Buffers (with the encoding specified in the stream.write() call) before passing them to stream._write(). Other types of data are not converted (i.e. Buffers are not decoded into strings). Setting to false will prevent strings from being converted. Default: true.
    • defaultEncoding <string> The default encoding that is used when no encoding is specified as an argument to stream.write(). Default: 'utf8'.
    • objectMode <boolean> Whether or not the stream.write(anyObj) is a valid operation. When set, it becomes possible to write JavaScript values other than string, Buffer or Uint8Array 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 the stream._write() method.
    • writev <Function> Implementation for the stream._writev() method.
    • destroy <Function> Implementation for the stream._destroy() method.
    • final <Function> Implementation for the stream._final() method.
    • construct <Function> Implementation for the stream._construct() method.
    • autoDestroy <boolean> Whether this stream should automatically call .destroy() on itself after ending. Default: true.
    • 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);
    // ...
  }
}

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

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();