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])。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
