new stream.Readable([options])
-
options
<Object>-
highWaterMark
<number> 在停止从底层资源读取之前存储在内部缓冲区中的最大 字节数。默认值:65536
(64 KiB),或16
用于objectMode
流。¥
highWaterMark
<number> The maximum number of bytes to store in the internal buffer before ceasing to read from the underlying resource. Default:65536
(64 KiB), or16
forobjectMode
streams. -
encoding
<string> 如果指定,则缓冲区将使用指定的编码解码为字符串。默认值:null
。¥
encoding
<string> If specified, then buffers will be decoded to strings using the specified encoding. Default:null
. -
objectMode
<boolean> 此流是否应表现为对象流。这意味着stream.read(n)
返回单个值,而不是大小为n
的Buffer
。默认值:false
。¥
objectMode
<boolean> Whether this stream should behave as a stream of objects. Meaning thatstream.read(n)
returns a single value instead of aBuffer
of sizen
. Default:false
. -
emitClose
<boolean> 流被销毁后是否应该触发'close'
。默认值:true
。¥
emitClose
<boolean> Whether or not the stream should emit'close'
after it has been destroyed. Default:true
. -
read
<Function>stream._read()
方法的实现。¥
read
<Function> Implementation for thestream._read()
method. -
destroy
<Function>stream._destroy()
方法的实现。¥
destroy
<Function> Implementation for thestream._destroy()
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 { Readable } = require('node:stream');
class MyReadable extends Readable {
constructor(options) {
// Calls the stream.Readable(options) constructor.
super(options);
// ...
}
}
或者,当使用 ES6 之前的样式构造函数时:
¥Or, when using pre-ES6 style constructors:
const { Readable } = require('node:stream');
const util = require('node:util');
function MyReadable(options) {
if (!(this instanceof MyReadable))
return new MyReadable(options);
Readable.call(this, options);
}
util.inherits(MyReadable, Readable);
或者,使用简化的构造函数方法:
¥Or, using the simplified constructor approach:
const { Readable } = require('node:stream');
const myReadable = new Readable({
read(size) {
// ...
},
});
在对应于传递的 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 readable created.
const { Readable } = require('node:stream');
const controller = new AbortController();
const read = new Readable({
read(size) {
// ...
},
signal: controller.signal,
});
// Later, abort the operation closing the stream
controller.abort();