调用了end()
was called and the write stream successfully wrote the file metadata and all the chunks to MongoDB.end()
,写入流成功地将文件元数据和所有块写入MongoDB。
Is true after 发出'close'
has been emitted.'close'
后为true
。
Is 在调用true
after writable.destroy()
has been called.writeable.destroy()
后为true
。
Returns error if the stream has been destroyed with an error.如果流因错误而被销毁,则返回错误。
Is 如果调用true
if it is safe to call writable.write()
, which means the stream has not been destroyed, errored or ended.writable.write()
是安全的,则为true
,这意味着流未被破坏、出错或结束。
Number of times 需要调用writable.uncork()
needs to be called in order to fully uncork the stream.writeable.uncork()
才能完全取消标记流的次数。
Is 在调用true
after writable.end()
has been called. writeable.end()
后为true
。This property does not indicate whether the data has been flushed, for this use 此属性不指示数据是否已刷新,因此改用writable.writableFinished
instead.writeable.writeableFinished
。
Is set to 在发出true
immediately before the 'finish'
event is emitted.'finish'
事件之前立即设置为true
。
Return the value of 返回创建此highWaterMark
passed when creating this Writable
.Writable
时传递的highWaterMark
值。
This property contains the number of bytes (or objects) in the queue ready to be written. 此属性包含队列中准备写入的字节数(或对象数)。The value provides introspection data regarding the status of the 该值提供有关highWaterMark
.highWaterMark
状态的内省数据。
Is 如果流的缓冲区已满且流将发出“drain”,则为true
if the stream's buffer has been full and stream will emit 'drain'.true
。
Getter for the property 给定objectMode
of a given Writable
stream.Writable
流的属性objectMode
的Getter。
Sets or gets the default captureRejection value for all emitters.设置或获取所有发射器的默认captureRejection值。
This symbol shall be used to install a listener for only monitoring 此符号应用于安装仅用于监视'error'
events. 'error'
事件的侦听器。Listeners installed using this symbol are called before the regular 使用此符号安装的侦听器在调用常规'error'
listeners are called.'error'
侦听器之前被调用。
Installing a listener using this symbol does not change the behavior once an 使用此符号安装侦听器不会在发出'error'
event is emitted, therefore the process will still crash if no regular 'error'
listener is installed.'error'
事件后改变行为,因此如果没有安装常规的'error'
侦听器,进程仍将崩溃。
Places this write stream into an aborted state (all future writes fail) and deletes all chunks that have already been written.将此写入流置于中止状态(所有将来的写入都失败),并删除所有已写入的块。
Event emitter The defined events on documents including:事件发射器文档上定义的事件包括:
The writable.cork()
method forces all written data to be buffered in memory. writeable.cork()
方法强制将所有写入的数据缓冲在内存中。The buffered data will be flushed when either the uncork or end methods are called.当调用uncork
方法或end
方法时,缓冲数据将被刷新。
The primary intent of writable.cork()
is to accommodate a situation in which several small chunks are written to the stream in rapid succession. writable.cork()
的主要目的是适应这样一种情况,即几个小的块被快速连续地写入流中。Instead of immediately forwarding them to the underlying destination, writable.cork()
buffers all the chunks until writable.uncork()
is called, which will pass them all to writable._writev()
, if present. writeable.cork()
不是立即将它们转发到底层目的地,而是缓冲所有的块,直到调用writable.uncork()
,这将把它们全部传递给writeable._writev()
,如果存在。This prevents a head-of-line blocking situation where data is being buffered while waiting for the first small chunk to be processed. 这防止了在等待处理第一个小数据块时缓冲数据的行头阻塞情况。However, use of 但是,在不实现writable.cork()
without implementingwritable._writev()
may have an adverse effect on throughput.writeable._writev()
的情况下使用writeable.cork()
可能会对吞吐量产生不利影响。
See also: 另请参见:writable.uncork()
, writable._writev()
.writable.uncork()
、writable._writev()
。
Destroy the stream. 破坏溪流。Optionally emit an 可选地,发出'error'
event, and emit a 'close'
event (unless emitClose
is set to false
). 'error'
事件,并发出'close'
事件(除非emitClose
设置为false
)。After this call, the writable stream has ended and subsequent calls to 在此调用之后,可写流已结束,随后对write()
or end()
will result in an ERR_STREAM_DESTROYED
error. write()
或end()
的调用将导致ERR_stream_DESTROYED
错误。This is a destructive and immediate way to destroy a stream. 这是一种破坏性的、立即摧毁河流的方法。Previous calls to以前对write()
may not have drained, and may trigger an ERR_STREAM_DESTROYED
error. rite()
的调用可能没有耗尽,可能会触发ERR_STREAM_DESTROYED
错误。Use 如果数据在关闭之前应该刷新,请使用end()
instead of destroy if data should flush before close, or wait for the 'drain'
event before destroying the stream.end()
而不是destroy,或者在销毁流之前等待'drain'
事件。
Once 一旦调用destroy()
has been called any further calls will be a no-op and no further errors except from _destroy()
may be emitted as 'error'
.destroy()
,任何进一步的调用都将是no-op,并且除了_destroy()
之外,其他错误都不会作为'error'
发出。
Implementors should not override this method, but instead implement 实现者不应重写此方法,而应实现writable._destroy()
.writeable._destroy()
。
Optional, an error to emit with 可选,使用'error'
event.'error'
事件发出错误。
Tells the stream that no more data will be coming in. 告诉流不再有数据进入。The stream will persist the remaining data to MongoDB, write the files document, and then emit a 'finish' event.该流将剩余的数据保存到MongoDB,写入文件文档,然后发出'finish'
事件。
Returns an array listing the events for which the emitter has registered listeners. 返回一个数组,列出发射器已注册侦听器的事件。The values in the array are strings or 数组中的值是字符串或Symbol
s.Symbol
。
const EventEmitter = require('events');
const myEE = new EventEmitter();
myEE.on('foo', () => {});
myEE.on('bar', () => {});
const sym = Symbol('symbol');
myEE.on(sym, () => {});
console.log(myEE.eventNames());
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
Returns the current max listener value for the 返回EventEmitter
which is either set by emitter.setMaxListeners(n)
or defaults to defaultMaxListeners.EventEmitter
的当前最大侦听器值,该值由emitter.setMaxListeners(n)
设置或默认为defaultMaxListeners
。
Returns the number of listeners listening to the event named 返回侦听名为eventName
.eventName
的事件的侦听器数。
The name of the event being listened for正在侦听的事件的名称
Returns a copy of the array of listeners for the event named 返回名为eventName
.eventName
的事件的侦听器数组的副本。
server.on('connection', (stream) => {
console.log('someone connected!');
});
console.log(util.inspect(server.listeners('connection')));
// Prints: [ [Function] ]
Alias for emitter.removeListener()
.emitter.removeListener()
的别名。
Returns a copy of the array of listeners for the event named 返回名为eventName
, including any wrappers (such as those created by .once()
).eventName
的事件的侦听器数组的副本,包括任何包装器(例如由once()
创建的包装器)。
const emitter = new EventEmitter();
emitter.once('log', () => console.log('log once'));
// Returns a new Array with a function onceWrapper
which has a property
// listener
which contains the original listener bound above
const listeners = emitter.rawListeners('log');
const logFnWrapper = listeners[0];
// Logs "log once" to the console and does not unbind the once
event
logFnWrapper.listener();
// Logs "log once" to the console and removes the listener
logFnWrapper();
emitter.on('log', () => console.log('log persistently'));
// Will return a new Array with a single function bound by .on()
above
const newListeners = emitter.rawListeners('log');
// Logs "log persistently" twice
newListeners[0]();
emitter.emit('log');
Removes all listeners, or those of the specified 删除所有侦听器,或指定eventName
.eventName
的侦听器。
It is bad practice to remove listeners added elsewhere in the code, particularly when the 删除代码中其他地方添加的侦听器是一种糟糕的做法,尤其是当EventEmitter
instance was created by some other component or module (e.g. sockets or file streams).EventEmitter
实例由其他组件或模块(例如套接字或文件流)创建时。
Returns a reference to the 返回对EventEmitter
, so that calls can be chained.EventEmitter
的引用,以便可以连缀调用。
The writable.setDefaultEncoding()
method sets the default encoding
for a Writable
stream.writable.setDefaultEncoding()
方法设置Writable
流的默认encoding
。
The new default encoding新的默认编码
By default 默认情况下,如果为特定事件添加了EventEmitter
s will print a warning if more than 10
listeners are added for a particular event. 10
个以上的侦听器,EventEmitter
将打印警告。This is a useful default that helps finding memory leaks. 这是一个有用的默认值,有助于查找内存泄漏。The emitter.setMaxListeners()
method allows the limit to be modified for this specific EventEmitter
instance. emitter.setMaxListeners()
方法允许修改此特定EventEmitter
实例的限制。The value can be set to该值可以设置为Infinity
(or 0
) to indicate an unlimited number of listeners.Infinity
(或0
),以表示侦听器的数量不受限制。
Returns a reference to the 返回对EventEmitter
, so that calls can be chained.EventEmitter
的引用,以便可以连缀调用。
The writable.uncork()
method flushes all data buffered since cork was called.writeable.uncork()
方法刷新自调用cork
以来缓冲的所有数据。
When using 当使用writable.cork()
and writable.uncork()
to manage the buffering of writes to a stream, defer calls to writable.uncork()
usingprocess.nextTick()
. writable.cock()
和writable.uncork()
管理对流的写入缓冲时,使用process.nextTick()
推迟对writable.uncork()
的调用。Doing so allows batching of all这样做允许对给定Node.js事件循环阶段内发生的全部writable.write()
calls that occur within a given Node.js event loop phase.writable.write()
调用进行批处理。
stream.cork();
stream.write('some ');
stream.write('data ');
process.nextTick(() => stream.uncork());
If the 如果在流上多次调用writable.cork()
method is called multiple times on a stream, the same number of calls to writable.uncork()
must be called to flush the buffered data.writable.cork()
方法,则必须调用相同数量的writable.uncork()
调用来刷新缓冲数据。
stream.cork();
stream.write('some ');
stream.cork();
stream.write('data ');
process.nextTick(() => {
stream.uncork();
// The data will not be flushed until uncork() is called a second time.
stream.uncork();
});
See also: 另请参见:writable.cork()
.writable.cork()
。
Write a buffer to the stream.将缓冲区写入流。
Buffer to write要写入的缓冲区
False if this write required flushing a chunk to MongoDB. 如果此写入需要将块刷新到MongoDB,则为false
。True otherwise.否则为true
。
A utility method for creating a 从web Writable
from a web WritableStream
.WritableStream
创建Writable
的实用方法。
Returns a copy of the array of listeners for the event named 返回名为eventName
.eventName
的事件的侦听器数组的副本。
For 对于EventEmitter
s this behaves exactly the same as calling .listeners
on the emitter
.EventEmitter
,这与调用emitter
上的.listeners
完全相同。
For 对于EventTarget
s this is the only way to get the event listeners for the event target. EventTarget
,这是获取事件目标的事件侦听器的唯一方法。This is useful for debugging and diagnostic purposes.这对于调试和诊断非常有用。
const { getEventListeners, EventEmitter } = require('events');
{
const ee = new EventEmitter();
const listener = () => console.log('Events are fun');
ee.on('foo', listener);
getEventListeners(ee, 'foo'); // [listener]
}
{
const et = new EventTarget();
const listener = () => console.log('Events are fun');
et.addEventListener('foo', listener);
getEventListeners(et, 'foo'); // [listener]
}
A class method that returns the number of listeners for the given 返回在给定eventName
registered on the given emitter
.emitter
上注册的给定eventName
的侦听器数量的类方法。
const { EventEmitter, listenerCount } = require('events');
const myEmitter = new EventEmitter();
myEmitter.on('event', () => {});
myEmitter.on('event', () => {});
console.log(listenerCount(myEmitter, 'event'));
// Prints: 2
The emitter to query要查询的发射器
The event name事件名称
const { on, EventEmitter } = require('events');
(async () => {
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar'); ee.emit('foo', 42); });
for await (const event of on(ee, 'foo')) {
// The execution of this inner block is synchronous and it processes one event at a time (even with await). 这个内部块的执行是同步的,它一次处理一个事件(即使有等待)。
// Do not use if concurrent execution is required. 如果需要并发执行,请不要使用。
console.log(event); // prints ['bar'] [42]
}
// Unreachable here此处无法访问 })();
Returns an 返回迭代AsyncIterator
that iterates eventName
events. eventName
事件的AsyncIterator
。It will throw if the 如果EventEmitter
emits 'error'
. EventEmitter
发出'error'
,它将抛出。It removes all listeners when exiting the loop. 退出循环时,它会删除所有侦听器。The 每次迭代返回的value
returned by each iteration is an array composed of the emitted event arguments.value
是由发出的事件参数组成的数组。
An AbortSignal
can be used to cancel waiting on events:AbortSignal
可用于取消等待事件:
const { on, EventEmitter } = require('events');
const ac = new AbortController();
(async () => {
const ee = new EventEmitter();
// Emit later on
process.nextTick(() => {
ee.emit('foo', 'bar');
ee.emit('foo', 42);
});
for await (const event of on(ee, 'foo', { signal: ac.signal })) {
// The execution of this inner block is synchronous and it processes one event at a time (even with await). 这个内部块的执行是同步的,它一次处理一个事件(即使有等待)。
// Do not use if concurrent execution is required.如果需要并发执行,请不要使用。
console.log(event); // prints ['bar'] [42]
}
// Unreachable here此处无法访问
})();
process.nextTick(() => ac.abort());
The name of the event being listened for正在侦听的事件的名称
that iterates 迭代eventName
events emitted by the emitter
emitter
发出的eventName
事件
Creates a 创建一个Promise
that is fulfilled when the EventEmitter
emits the given event or that is rejected if the EventEmitter
emits 'error'
while waiting. Promise
,当EventEmitter
发出给定事件时,该Promise
将被满足,或者当EventEmitter
在等待时发出'error'
时,该Promise
被拒绝。The Promise
will resolve with an array of all the arguments emitted to the given event.Promise
将使用向给定事件发出的所有参数的数组进行解析。
This method is intentionally generic and works with the web platform EventTarget interface, which has no special此方法是有意泛型的,可与web平台EventTarget接口配合使用,该接口没有特殊的'error'
event semantics and does not listen to the 'error'
event.'error'
事件语义,也不会侦听'error'
。
const { once, EventEmitter } = require('events');
async function run() {
const ee = new EventEmitter();
process.nextTick(() => {
ee.emit('myevent', 42);
});
const [value] = await once(ee, 'myevent');
console.log(value);
const err = new Error('kaboom');
process.nextTick(() => {
ee.emit('error', err);
});
try {
await once(ee, 'myevent');
} catch (err) {
console.log('error happened', err);
}
}
run();
The special handling of the 'error'
event is only used when events.once()
is used to wait for another event. 'error'
事件的特殊处理仅在events.once()
用于等待另一个事件时使用。If 如果events.once()
is used to wait for the 'error'
event itself, then it is treated as any other kind of event without special handling:events.once()
用于等待'error'
事件本身,则它将被视为任何其他类型的事件,无需特殊处理:
const { EventEmitter, once } = require('events');
const ee = new EventEmitter();
once(ee, 'error')
.then(([err]) => console.log('ok', err.message))
.catch((err) => console.log('error', err.message));
ee.emit('error', new Error('boom'));
// Prints: ok boom
An AbortSignal
can be used to cancel waiting for the event:AbortSignal
可用于取消等待事件:
const { EventEmitter, once } = require('events');
const ee = new EventEmitter();
const ac = new AbortController();
async function foo(emitter, event, signal) {
try {
await once(emitter, event, { signal });
console.log('event emitted!');
} catch (error) {
if (error.name === 'AbortError') {
console.error('Waiting for the event was canceled!');
} else {
console.error('There was an error', error.message);
}
}
}
foo(ee, 'foo', ac.signal);
ac.abort(); // Abort waiting for the event
ee.emit('foo'); // Prints: Waiting for the event was canceled!
const { setMaxListeners, EventEmitter } = require('events');
const target = new EventTarget();
const emitter = new EventEmitter();
setMaxListeners(5, target, emitter);
A non-negative number. 非负数。The maximum number of listeners per 每个EventTarget
event.EventTarget
事件的最大侦听器数。
A utility method for creating a web 一种用于从WritableStream
from a Writable
.Writable
创建web WritableStream
的实用程序方法。
Generated using TypeDoc
A writable stream that enables you to write buffers to GridFS.一个可写流,使您能够将缓冲区写入GridFS。Do not instantiate this class directly.不要直接实例化这个类。Use请改用openUploadStream()
instead.openUploadStream()
。