new stream.Writable([options])
历史
| 版本 | 变化 |
|---|---|
| v15.5.0 | 支持传入 AbortSignal。 |
| v14.0.0 | 将 |
| v11.2.0、v10.16.0 | 添加 |
| v10.0.0 | 添加 |
参数
options<Object>highWaterMark<number>开始返回stream.write()false时的缓冲区级别。 默认:16384(16 KiB),或16用于objectMode流。decodeStrings<boolean> 是否将传递给的stream.write()string编码为Buffer(使用在调用中指定的编码),然后再将它们传递给stream.write()。其他类型的数据不会被转换(即stream._write()Buffers 不会被解码为strings)。设置为 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()construct<Function>方法的实现。stream._construct()autoDestroy<boolean> 此流是否应在结束后自动调用自身的.destroy()。 默认:true。signal<AbortSignal> 表示可能取消的信号。
const { Writable } = require('node:stream');
class MyWritable extends Writable {
constructor(options) {
// Calls the stream.Writable() constructor.
super(options);
// ...
}
}
或者,当使用 pre-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
});
// Later, abort the operation closing the stream
controller.abort();
相关用法
- Node.js new stream.Duplex(options)用法及代码示例
- Node.js new stream.Readable([options])用法及代码示例
- Node.js new stream.Transform([options])用法及代码示例
- Node.js new assert.AssertionError(options)用法及代码示例
- Node.js new AsyncResource(type[, options])用法及代码示例
- Node.js new Console(options)用法及代码示例
- Node.js new URLSearchParams(obj)用法及代码示例
- Node.js new crypto.Certificate()用法及代码示例
- Node.js new URLSearchParams(iterable)用法及代码示例
- Node.js new Agent([options])用法及代码示例
- Node.js new vm.SourceTextModule(code[, options])用法及代码示例
- Node.js new PerformanceObserver(callback)用法及代码示例
- Node.js new URL(input[, base])用法及代码示例
- Node.js new URLSearchParams(string)用法及代码示例
- Node.js new assert.CallTracker()用法及代码示例
- Node.js net.isIP(input)用法及代码示例
- Node.js net.createConnection(options[, connectListener])用法及代码示例
- Node.js net.isIPv6(input)用法及代码示例
- Node.js net.Server.address()用法及代码示例
- Node.js net.createServer([options][, connectionListener])用法及代码示例
- Node.js net.Server.listen()用法及代码示例
- Node.js net.Socket.setTimeout(timeout[, callback])用法及代码示例
- Node.js net.BlockList.check(address[, type])用法及代码示例
- Node.js net.isIPv4(input)用法及代码示例
- Node.js ServerHttp2Stream http2stream.pushStream(headers[, options], callback)用法及代码示例
注:本文由纯净天空筛选整理自nodejs.org大神的英文原创作品 new stream.Writable([options])。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。
