2012-02-27 11:09:33 -08:00
|
|
|
# Events
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2017-01-22 19:16:21 -08:00
|
|
|
<!--introduced_in=v0.10.0-->
|
|
|
|
|
2016-07-16 00:35:38 +02:00
|
|
|
> Stability: 2 - Stable
|
2012-03-02 15:14:03 -08:00
|
|
|
|
2012-02-27 11:37:26 -08:00
|
|
|
<!--type=module-->
|
|
|
|
|
2020-06-22 13:56:08 -04:00
|
|
|
<!-- source_link=lib/events.js -->
|
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
Much of the Node.js core API is built around an idiomatic asynchronous
|
|
|
|
event-driven architecture in which certain kinds of objects (called "emitters")
|
2018-05-07 15:17:09 -04:00
|
|
|
emit named events that cause `Function` objects ("listeners") to be called.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
For instance: a [`net.Server`][] object emits an event each time a peer
|
|
|
|
connects to it; a [`fs.ReadStream`][] emits an event when the file is opened;
|
|
|
|
a [stream][] emits an event whenever data is available to be read.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
All objects that emit events are instances of the `EventEmitter` class. These
|
|
|
|
objects expose an `eventEmitter.on()` function that allows one or more
|
2016-06-29 23:20:23 +05:30
|
|
|
functions to be attached to named events emitted by the object. Typically,
|
2015-12-29 10:04:13 -08:00
|
|
|
event names are camel-cased strings but any valid JavaScript property key
|
|
|
|
can be used.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
When the `EventEmitter` object emits an event, all of the functions attached
|
2015-12-29 10:04:13 -08:00
|
|
|
to that specific event are called _synchronously_. Any values returned by the
|
2020-09-05 06:36:59 -07:00
|
|
|
called listeners are _ignored_ and discarded.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
The following example shows a simple `EventEmitter` instance with a single
|
|
|
|
listener. The `eventEmitter.on()` method is used to register listeners, while
|
|
|
|
the `eventEmitter.emit()` method is used to trigger the event.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log('an event occurred!');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const EventEmitter = require('node:events');
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
class MyEmitter extends EventEmitter {}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
2016-01-24 11:15:51 +02:00
|
|
|
myEmitter.on('event', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('an event occurred!');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
|
|
|
## Passing arguments and `this` to listeners
|
|
|
|
|
|
|
|
The `eventEmitter.emit()` method allows an arbitrary set of arguments to be
|
2019-10-24 15:19:07 -07:00
|
|
|
passed to the listener functions. Keep in mind that when
|
2018-05-05 10:53:43 +03:00
|
|
|
an ordinary listener function is called, the standard `this` keyword
|
|
|
|
is intentionally set to reference the `EventEmitter` instance to which the
|
2015-12-29 10:04:13 -08:00
|
|
|
listener is attached.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', function(a, b) {
|
|
|
|
console.log(a, b, this, this === myEmitter);
|
|
|
|
// Prints:
|
|
|
|
// a b MyEmitter {
|
|
|
|
// _events: [Object: null prototype] { event: [Function (anonymous)] },
|
|
|
|
// _eventsCount: 1,
|
|
|
|
// _maxListeners: undefined,
|
2024-03-03 00:35:37 +09:00
|
|
|
// [Symbol(shapeMode)]: false,
|
2023-01-19 04:25:11 +09:00
|
|
|
// [Symbol(kCapture)]: false
|
|
|
|
// } true
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', function(a, b) {
|
2018-05-05 10:53:43 +03:00
|
|
|
console.log(a, b, this, this === myEmitter);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// a b MyEmitter {
|
2022-12-06 14:05:58 +09:00
|
|
|
// _events: [Object: null prototype] { event: [Function (anonymous)] },
|
2016-11-08 21:04:57 +01:00
|
|
|
// _eventsCount: 1,
|
2022-12-06 14:05:58 +09:00
|
|
|
// _maxListeners: undefined,
|
2024-03-03 00:35:37 +09:00
|
|
|
// [Symbol(shapeMode)]: false,
|
2022-12-06 14:05:58 +09:00
|
|
|
// [Symbol(kCapture)]: false
|
|
|
|
// } true
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
It is possible to use ES6 Arrow Functions as listeners, however, when doing so,
|
|
|
|
the `this` keyword will no longer reference the `EventEmitter` instance:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
console.log(a, b, this);
|
2023-09-20 02:31:09 +09:00
|
|
|
// Prints: a b undefined
|
2023-01-19 04:25:11 +09:00
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
console.log(a, b, this);
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: a b {}
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2020-06-14 14:49:34 -07:00
|
|
|
## Asynchronous vs. synchronous
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2017-11-06 02:37:50 -07:00
|
|
|
The `EventEmitter` calls all listeners synchronously in the order in which
|
2019-10-24 15:19:07 -07:00
|
|
|
they were registered. This ensures the proper sequencing of
|
|
|
|
events and helps avoid race conditions and logic errors. When appropriate,
|
2015-12-29 10:04:13 -08:00
|
|
|
listener functions can switch to an asynchronous mode of operation using
|
|
|
|
the `setImmediate()` or `process.nextTick()` methods:
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
console.log('this happens asynchronously');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('event', (a, b) => {
|
|
|
|
setImmediate(() => {
|
|
|
|
console.log('this happens asynchronously');
|
|
|
|
});
|
|
|
|
});
|
|
|
|
myEmitter.emit('event', 'a', 'b');
|
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
|
|
|
## Handling events only once
|
|
|
|
|
|
|
|
When a listener is registered using the `eventEmitter.on()` method, that
|
2020-09-05 06:36:59 -07:00
|
|
|
listener is invoked _every time_ the named event is emitted.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
let m = 0;
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 1
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 2
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
2017-03-12 13:11:13 +02:00
|
|
|
let m = 0;
|
2016-01-17 18:39:07 +01:00
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: 1
|
2016-01-17 18:39:07 +01:00
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: 2
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
|
|
|
Using the `eventEmitter.once()` method, it is possible to register a listener
|
2016-06-29 23:20:23 +05:30
|
|
|
that is called at most once for a particular event. Once the event is emitted,
|
2021-10-10 21:55:04 -07:00
|
|
|
the listener is unregistered and _then_ called.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
let m = 0;
|
|
|
|
myEmitter.once('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints: 1
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Ignored
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
2017-03-12 13:11:13 +02:00
|
|
|
let m = 0;
|
2016-01-17 18:39:07 +01:00
|
|
|
myEmitter.once('event', () => {
|
|
|
|
console.log(++m);
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: 1
|
2016-01-17 18:39:07 +01:00
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Ignored
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
|
|
|
## Error events
|
|
|
|
|
|
|
|
When an error occurs within an `EventEmitter` instance, the typical action is
|
2016-06-29 23:20:23 +05:30
|
|
|
for an `'error'` event to be emitted. These are treated as special cases
|
2015-12-29 10:04:13 -08:00
|
|
|
within Node.js.
|
|
|
|
|
|
|
|
If an `EventEmitter` does _not_ have at least one listener registered for the
|
|
|
|
`'error'` event, and an `'error'` event is emitted, the error is thrown, a
|
|
|
|
stack trace is printed, and the Node.js process exits.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Throws and crashes Node.js
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Throws and crashes Node.js
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2017-11-09 17:58:42 +01:00
|
|
|
To guard against crashing the Node.js process the [`domain`][] module can be
|
2022-04-20 10:23:41 +02:00
|
|
|
used. (Note, however, that the `node:domain` module is deprecated.)
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
As a best practice, listeners should always be added for the `'error'` events.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('error', (err) => {
|
|
|
|
console.error('whoops! there was an error');
|
|
|
|
});
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Prints: whoops! there was an error
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
myEmitter.on('error', (err) => {
|
2017-03-12 13:14:00 +02:00
|
|
|
console.error('whoops! there was an error');
|
2016-01-17 18:39:07 +01:00
|
|
|
});
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: whoops! there was an error
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2013-04-30 15:24:48 -07:00
|
|
|
|
2019-12-12 21:08:42 +01:00
|
|
|
It is possible to monitor `'error'` events without consuming the emitted error
|
2020-11-12 10:02:12 -08:00
|
|
|
by installing a listener using the symbol `events.errorMonitor`.
|
2019-12-12 21:08:42 +01:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, errorMonitor } from 'node:events';
|
|
|
|
|
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
myEmitter.on(errorMonitor, (err) => {
|
|
|
|
MyMonitoringTool.log(err);
|
|
|
|
});
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Still throws and crashes Node.js
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, errorMonitor } = require('node:events');
|
2020-11-12 10:02:12 -08:00
|
|
|
|
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
myEmitter.on(errorMonitor, (err) => {
|
2019-12-12 21:08:42 +01:00
|
|
|
MyMonitoringTool.log(err);
|
|
|
|
});
|
|
|
|
myEmitter.emit('error', new Error('whoops!'));
|
|
|
|
// Still throws and crashes Node.js
|
|
|
|
```
|
|
|
|
|
2020-06-14 14:49:34 -07:00
|
|
|
## Capture rejections of promises
|
2019-05-19 13:55:18 +02:00
|
|
|
|
|
|
|
Using `async` functions with event handlers is problematic, because it
|
|
|
|
can lead to an unhandled rejection in case of a thrown exception:
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
ee.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2019-05-19 13:55:18 +02:00
|
|
|
const ee = new EventEmitter();
|
|
|
|
ee.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
The `captureRejections` option in the `EventEmitter` constructor or the global
|
|
|
|
setting change this behavior, installing a `.then(undefined, handler)`
|
|
|
|
handler on the `Promise`. This handler routes the exception
|
|
|
|
asynchronously to the [`Symbol.for('nodejs.rejection')`][rejection] method
|
|
|
|
if there is one, or to [`'error'`][error] event handler if there is none.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const ee1 = new EventEmitter({ captureRejections: true });
|
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
|
|
|
|
const ee2 = new EventEmitter({ captureRejections: true });
|
|
|
|
ee2.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee2[Symbol.for('nodejs.rejection')] = console.log;
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2019-05-19 13:55:18 +02:00
|
|
|
const ee1 = new EventEmitter({ captureRejections: true });
|
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
|
|
|
|
const ee2 = new EventEmitter({ captureRejections: true });
|
|
|
|
ee2.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee2[Symbol.for('nodejs.rejection')] = console.log;
|
|
|
|
```
|
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
Setting `events.captureRejections = true` will change the default for all
|
2019-05-19 13:55:18 +02:00
|
|
|
new instances of `EventEmitter`.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
|
|
|
|
EventEmitter.captureRejections = true;
|
|
|
|
const ee1 = new EventEmitter();
|
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const events = require('node:events');
|
2020-11-12 10:02:12 -08:00
|
|
|
events.captureRejections = true;
|
|
|
|
const ee1 = new events.EventEmitter();
|
2019-05-19 13:55:18 +02:00
|
|
|
ee1.on('something', async (value) => {
|
|
|
|
throw new Error('kaboom');
|
|
|
|
});
|
|
|
|
|
|
|
|
ee1.on('error', console.log);
|
|
|
|
```
|
|
|
|
|
|
|
|
The `'error'` events that are generated by the `captureRejections` behavior
|
|
|
|
do not have a catch handler to avoid infinite error loops: the
|
|
|
|
recommendation is to **not use `async` functions as `'error'` event handlers**.
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
## Class: `EventEmitter`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
2019-05-19 13:55:18 +02:00
|
|
|
changes:
|
2020-04-24 18:43:06 +02:00
|
|
|
- version:
|
|
|
|
- v13.4.0
|
|
|
|
- v12.16.0
|
2019-05-19 13:55:18 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/27867
|
|
|
|
description: Added captureRejections option.
|
2016-07-21 15:58:56 +02:00
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2022-04-20 10:23:41 +02:00
|
|
|
The `EventEmitter` class is defined and exposed by the `node:events` module:
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const EventEmitter = require('node:events');
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2018-04-29 20:46:41 +03:00
|
|
|
All `EventEmitter`s emit the event `'newListener'` when new listeners are
|
2016-06-29 23:20:23 +05:30
|
|
|
added and `'removeListener'` when existing listeners are removed.
|
2013-04-30 15:24:48 -07:00
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
It supports the following option:
|
|
|
|
|
|
|
|
* `captureRejections` {boolean} It enables
|
|
|
|
[automatic capturing of promise rejection][capturerejections].
|
2020-05-31 16:30:41 -04:00
|
|
|
**Default:** `false`.
|
2019-05-19 13:55:18 +02:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### Event: `'newListener'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
|
|
|
-->
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event being listened for
|
2015-11-04 11:45:27 -05:00
|
|
|
* `listener` {Function} The event handler function
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
The `EventEmitter` instance will emit its own `'newListener'` event _before_
|
2016-06-29 23:20:23 +05:30
|
|
|
a listener is added to its internal array of listeners.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
Listeners registered for the `'newListener'` event are passed the event
|
2015-12-29 10:04:13 -08:00
|
|
|
name and a reference to the listener being added.
|
|
|
|
|
|
|
|
The fact that the event is triggered before adding the listener has a subtle
|
2021-10-10 21:55:04 -07:00
|
|
|
but important side effect: any _additional_ listeners registered to the same
|
|
|
|
`name` _within_ the `'newListener'` callback are inserted _before_ the
|
2015-12-29 10:04:13 -08:00
|
|
|
listener that is in the process of being added.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
// Only do this once so we don't loop forever
|
|
|
|
myEmitter.once('newListener', (event, listener) => {
|
|
|
|
if (event === 'event') {
|
|
|
|
// Insert a new listener in front
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log('B');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log('A');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// B
|
|
|
|
// A
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2020-05-22 18:10:14 +05:30
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
// Only do this once so we don't loop forever
|
|
|
|
myEmitter.once('newListener', (event, listener) => {
|
|
|
|
if (event === 'event') {
|
|
|
|
// Insert a new listener in front
|
2015-12-29 10:04:13 -08:00
|
|
|
myEmitter.on('event', () => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('B');
|
2015-12-29 10:04:13 -08:00
|
|
|
});
|
2016-01-17 18:39:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
myEmitter.on('event', () => {
|
|
|
|
console.log('A');
|
|
|
|
});
|
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// B
|
|
|
|
// A
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### Event: `'removeListener'`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.3
|
2017-02-21 23:38:45 +01:00
|
|
|
changes:
|
2020-10-01 20:23:33 +02:00
|
|
|
- version:
|
|
|
|
- v6.1.0
|
|
|
|
- v4.7.0
|
2017-02-21 23:38:45 +01:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/6394
|
|
|
|
description: For listeners attached using `.once()`, the `listener` argument
|
|
|
|
now yields the original listener function.
|
2016-07-21 15:58:56 +02:00
|
|
|
-->
|
2014-12-19 09:53:20 -08:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The event name
|
2015-11-04 11:45:27 -05:00
|
|
|
* `listener` {Function} The event handler function
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
The `'removeListener'` event is emitted _after_ the `listener` is removed.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.addListener(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
|
|
|
* `listener` {Function}
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Alias for `emitter.on(eventName, listener)`.
|
2015-11-04 11:45:27 -05:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.emit(eventName[, ...args])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
2019-09-06 01:42:22 -04:00
|
|
|
* `...args` {any}
|
2018-07-12 13:48:11 -04:00
|
|
|
* Returns: {boolean}
|
2015-11-04 11:45:27 -05:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Synchronously calls each of the listeners registered for the event named
|
|
|
|
`eventName`, in the order they were registered, passing the supplied arguments
|
|
|
|
to each.
|
2015-11-04 11:45:27 -05:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Returns `true` if the event had listeners, `false` otherwise.
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
// First listener
|
|
|
|
myEmitter.on('event', function firstListener() {
|
|
|
|
console.log('Helloooo! first listener');
|
|
|
|
});
|
|
|
|
// Second listener
|
|
|
|
myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
|
|
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
|
|
});
|
|
|
|
// Third listener
|
|
|
|
myEmitter.on('event', function thirdListener(...args) {
|
|
|
|
const parameters = args.join(', ');
|
|
|
|
console.log(`event with parameters ${parameters} in third listener`);
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(myEmitter.listeners('event'));
|
|
|
|
|
|
|
|
myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
|
|
|
|
|
|
// Prints:
|
|
|
|
// [
|
|
|
|
// [Function: firstListener],
|
|
|
|
// [Function: secondListener],
|
|
|
|
// [Function: thirdListener]
|
|
|
|
// ]
|
|
|
|
// Helloooo! first listener
|
|
|
|
// event with parameters 1, 2 in second listener
|
|
|
|
// event with parameters 1, 2, 3, 4, 5 in third listener
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const EventEmitter = require('node:events');
|
2019-06-21 17:37:37 -05:00
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
|
|
|
|
// First listener
|
|
|
|
myEmitter.on('event', function firstListener() {
|
|
|
|
console.log('Helloooo! first listener');
|
|
|
|
});
|
|
|
|
// Second listener
|
|
|
|
myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
|
|
console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
|
|
});
|
|
|
|
// Third listener
|
|
|
|
myEmitter.on('event', function thirdListener(...args) {
|
|
|
|
const parameters = args.join(', ');
|
|
|
|
console.log(`event with parameters ${parameters} in third listener`);
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(myEmitter.listeners('event'));
|
|
|
|
|
|
|
|
myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
|
|
|
|
|
|
// Prints:
|
|
|
|
// [
|
|
|
|
// [Function: firstListener],
|
|
|
|
// [Function: secondListener],
|
|
|
|
// [Function: thirdListener]
|
|
|
|
// ]
|
|
|
|
// Helloooo! first listener
|
|
|
|
// event with parameters 1, 2 in second listener
|
|
|
|
// event with parameters 1, 2, 3, 4, 5 in third listener
|
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.eventNames()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.0.0
|
|
|
|
-->
|
2016-03-08 21:29:38 -08:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* Returns: {Array}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
2016-03-08 21:29:38 -08:00
|
|
|
Returns an array listing the events for which the emitter has registered
|
2020-09-05 06:36:59 -07:00
|
|
|
listeners. The values in the array are strings or `Symbol`s.
|
2016-03-08 21:29:38 -08:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node: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) ]
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const EventEmitter = require('node:events');
|
2022-05-28 11:50:20 +08:00
|
|
|
|
2016-03-08 21:29:38 -08:00
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.on('foo', () => {});
|
|
|
|
myEE.on('bar', () => {});
|
|
|
|
|
|
|
|
const sym = Symbol('symbol');
|
|
|
|
myEE.on(sym, () => {});
|
|
|
|
|
2016-04-27 12:55:34 +03:00
|
|
|
console.log(myEE.eventNames());
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: [ 'foo', 'bar', Symbol(symbol) ]
|
2016-03-08 21:29:38 -08:00
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.getMaxListeners()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v1.0.0
|
|
|
|
-->
|
2014-12-05 14:50:05 +01:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* Returns: {integer}
|
2018-04-11 21:07:14 +03:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
Returns the current max listener value for the `EventEmitter` which is either
|
|
|
|
set by [`emitter.setMaxListeners(n)`][] or defaults to
|
2020-11-12 10:02:12 -08:00
|
|
|
[`events.defaultMaxListeners`][].
|
2014-12-05 14:50:05 +01:00
|
|
|
|
2023-02-21 02:38:51 -08:00
|
|
|
### `emitter.listenerCount(eventName[, listener])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v3.2.0
|
2023-02-21 02:38:51 -08:00
|
|
|
changes:
|
2023-04-10 23:02:28 -04:00
|
|
|
- version:
|
|
|
|
- v19.8.0
|
|
|
|
- v18.16.0
|
2023-02-21 02:38:51 -08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/46523
|
|
|
|
description: Added the `listener` argument.
|
2016-07-21 15:58:56 +02:00
|
|
|
-->
|
2012-05-04 17:45:27 +02:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event being listened for
|
2023-02-21 02:38:51 -08:00
|
|
|
* `listener` {Function} The event handler function
|
2018-04-11 21:07:14 +03:00
|
|
|
* Returns: {integer}
|
2010-12-31 18:32:52 -08:00
|
|
|
|
2023-02-21 02:38:51 -08:00
|
|
|
Returns the number of listeners listening for the event named `eventName`.
|
|
|
|
If `listener` is provided, it will return how many times the listener is found
|
|
|
|
in the list of the listeners of the event.
|
2010-12-31 18:32:52 -08:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.listeners(eventName)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
2017-02-21 23:38:45 +01:00
|
|
|
changes:
|
|
|
|
- version: v7.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/6881
|
|
|
|
description: For listeners attached using `.once()` this returns the
|
|
|
|
original listeners instead of wrapper functions now.
|
2016-07-21 15:58:56 +02:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
2021-10-10 21:55:04 -07:00
|
|
|
* Returns: {Function\[]}
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Returns a copy of the array of listeners for the event named `eventName`.
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.on('connection', (stream) => {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
|
|
|
console.log(util.inspect(server.listeners('connection')));
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints: [ [Function] ]
|
2016-01-17 18:39:07 +01:00
|
|
|
```
|
2010-10-28 23:18:16 +11:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.off(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2018-04-25 23:46:54 +09:00
|
|
|
<!-- YAML
|
|
|
|
added: v10.0.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
* `listener` {Function}
|
|
|
|
* Returns: {EventEmitter}
|
|
|
|
|
|
|
|
Alias for [`emitter.removeListener()`][].
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.on(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.101
|
|
|
|
-->
|
2012-06-14 17:24:40 -07:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-03 19:55:22 -07:00
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 21:07:14 +03:00
|
|
|
* Returns: {EventEmitter}
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
Adds the `listener` function to the end of the listeners array for the
|
2016-03-22 23:21:46 +02:00
|
|
|
event named `eventName`. No checks are made to see if the `listener` has
|
|
|
|
already been added. Multiple calls passing the same combination of `eventName`
|
|
|
|
and `listener` will result in the `listener` being added, and called, multiple
|
2015-12-29 10:04:13 -08:00
|
|
|
times.
|
2015-08-12 00:01:50 +05:30
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.on('connection', (stream) => {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
|
|
|
```
|
2015-08-12 00:01:50 +05:30
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2013-02-14 00:48:11 -08:00
|
|
|
|
2016-04-03 19:55:22 -07:00
|
|
|
By default, event listeners are invoked in the order they are added. The
|
|
|
|
`emitter.prependListener()` method can be used as an alternative to add the
|
|
|
|
event listener to the beginning of the listeners array.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.on('foo', () => console.log('a'));
|
|
|
|
myEE.prependListener('foo', () => console.log('b'));
|
|
|
|
myEE.emit('foo');
|
|
|
|
// Prints:
|
|
|
|
// b
|
|
|
|
// a
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2016-04-03 19:55:22 -07:00
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.on('foo', () => console.log('a'));
|
|
|
|
myEE.prependListener('foo', () => console.log('b'));
|
|
|
|
myEE.emit('foo');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// b
|
|
|
|
// a
|
2016-04-03 19:55:22 -07:00
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.once(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.0
|
|
|
|
-->
|
2013-02-14 00:48:11 -08:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-03 19:55:22 -07:00
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 21:07:14 +03:00
|
|
|
* Returns: {EventEmitter}
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2017-12-06 10:15:58 -08:00
|
|
|
Adds a **one-time** `listener` function for the event named `eventName`. The
|
2016-04-25 09:35:30 -04:00
|
|
|
next time `eventName` is triggered, this listener is removed and then invoked.
|
2013-02-14 00:48:11 -08:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
|
|
|
server.once('connection', (stream) => {
|
|
|
|
console.log('Ah, we have our first user!');
|
|
|
|
});
|
|
|
|
```
|
2010-11-14 23:51:57 +11:00
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2010-11-14 23:51:57 +11:00
|
|
|
|
2016-04-03 19:55:22 -07:00
|
|
|
By default, event listeners are invoked in the order they are added. The
|
|
|
|
`emitter.prependOnceListener()` method can be used as an alternative to add the
|
|
|
|
event listener to the beginning of the listeners array.
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.once('foo', () => console.log('a'));
|
|
|
|
myEE.prependOnceListener('foo', () => console.log('b'));
|
|
|
|
myEE.emit('foo');
|
|
|
|
// Prints:
|
|
|
|
// b
|
|
|
|
// a
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2016-04-03 19:55:22 -07:00
|
|
|
const myEE = new EventEmitter();
|
|
|
|
myEE.once('foo', () => console.log('a'));
|
|
|
|
myEE.prependOnceListener('foo', () => console.log('b'));
|
|
|
|
myEE.emit('foo');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// b
|
|
|
|
// a
|
2016-04-03 19:55:22 -07:00
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.prependListener(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.0.0
|
|
|
|
-->
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-03 19:55:22 -07:00
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 21:07:14 +03:00
|
|
|
* Returns: {EventEmitter}
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
Adds the `listener` function to the _beginning_ of the listeners array for the
|
2016-04-03 19:55:22 -07:00
|
|
|
event named `eventName`. No checks are made to see if the `listener` has
|
|
|
|
already been added. Multiple calls passing the same combination of `eventName`
|
|
|
|
and `listener` will result in the `listener` being added, and called, multiple
|
|
|
|
times.
|
|
|
|
|
|
|
|
```js
|
|
|
|
server.prependListener('connection', (stream) => {
|
|
|
|
console.log('someone connected!');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.prependOnceListener(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v6.0.0
|
|
|
|
-->
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2017-12-13 14:40:12 -08:00
|
|
|
* `eventName` {string|symbol} The name of the event.
|
2016-04-03 19:55:22 -07:00
|
|
|
* `listener` {Function} The callback function
|
2018-04-11 21:07:14 +03:00
|
|
|
* Returns: {EventEmitter}
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2017-12-06 10:15:58 -08:00
|
|
|
Adds a **one-time** `listener` function for the event named `eventName` to the
|
2021-10-10 21:55:04 -07:00
|
|
|
_beginning_ of the listeners array. The next time `eventName` is triggered, this
|
2016-04-25 09:35:30 -04:00
|
|
|
listener is removed, and then invoked.
|
2016-04-03 19:55:22 -07:00
|
|
|
|
|
|
|
```js
|
|
|
|
server.prependOnceListener('connection', (stream) => {
|
|
|
|
console.log('Ah, we have our first user!');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2016-04-03 19:55:22 -07:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.removeAllListeners([eventName])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
|
|
|
* Returns: {EventEmitter}
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Removes all listeners, or those of the specified `eventName`.
|
2015-12-29 10:04:13 -08:00
|
|
|
|
2019-06-20 13:54:55 -06:00
|
|
|
It is bad practice to remove listeners added elsewhere in the code,
|
2015-12-29 10:04:13 -08:00
|
|
|
particularly when the `EventEmitter` instance was created by some other
|
|
|
|
component or module (e.g. sockets or file streams).
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.removeListener(eventName, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.1.26
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
|
|
|
* `listener` {Function}
|
|
|
|
* Returns: {EventEmitter}
|
2013-03-12 00:03:56 +01:00
|
|
|
|
2016-03-22 23:21:46 +02:00
|
|
|
Removes the specified `listener` from the listener array for the event named
|
|
|
|
`eventName`.
|
2015-07-12 16:10:57 +00:00
|
|
|
|
2016-01-17 18:39:07 +01:00
|
|
|
```js
|
2017-03-12 13:11:13 +02:00
|
|
|
const callback = (stream) => {
|
2016-01-17 18:39:07 +01:00
|
|
|
console.log('someone connected!');
|
|
|
|
};
|
|
|
|
server.on('connection', callback);
|
|
|
|
// ...
|
|
|
|
server.removeListener('connection', callback);
|
|
|
|
```
|
2015-07-12 16:10:57 +00:00
|
|
|
|
2018-04-09 19:30:22 +03:00
|
|
|
`removeListener()` will remove, at most, one instance of a listener from the
|
2015-11-04 11:45:27 -05:00
|
|
|
listener array. If any single listener has been added multiple times to the
|
2018-04-09 19:30:22 +03:00
|
|
|
listener array for the specified `eventName`, then `removeListener()` must be
|
2016-03-22 23:21:46 +02:00
|
|
|
called multiple times to remove each instance.
|
2015-07-12 16:10:57 +00:00
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
Once an event is emitted, all listeners attached to it at the
|
|
|
|
time of emitting are called in order. This implies that any
|
2021-10-10 21:55:04 -07:00
|
|
|
`removeListener()` or `removeAllListeners()` calls _after_ emitting and
|
|
|
|
_before_ the last listener finishes execution will not remove them from
|
2020-09-05 06:36:59 -07:00
|
|
|
`emit()` in progress. Subsequent events behave as expected.
|
2016-02-12 15:18:21 +05:30
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
class MyEmitter extends EventEmitter {}
|
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
|
|
|
const callbackA = () => {
|
|
|
|
console.log('A');
|
|
|
|
myEmitter.removeListener('event', callbackB);
|
|
|
|
};
|
|
|
|
|
|
|
|
const callbackB = () => {
|
|
|
|
console.log('B');
|
|
|
|
};
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackA);
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackB);
|
|
|
|
|
|
|
|
// callbackA removes listener callbackB but it will still be called.
|
|
|
|
// Internal listener array at time of emit [callbackA, callbackB]
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// A
|
|
|
|
// B
|
|
|
|
|
|
|
|
// callbackB is now removed.
|
|
|
|
// Internal listener array [callbackA]
|
|
|
|
myEmitter.emit('event');
|
|
|
|
// Prints:
|
|
|
|
// A
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
class MyEmitter extends EventEmitter {}
|
2016-02-12 15:18:21 +05:30
|
|
|
const myEmitter = new MyEmitter();
|
|
|
|
|
2017-03-12 13:11:13 +02:00
|
|
|
const callbackA = () => {
|
2016-02-12 15:18:21 +05:30
|
|
|
console.log('A');
|
|
|
|
myEmitter.removeListener('event', callbackB);
|
|
|
|
};
|
|
|
|
|
2017-03-12 13:11:13 +02:00
|
|
|
const callbackB = () => {
|
2016-02-12 15:18:21 +05:30
|
|
|
console.log('B');
|
|
|
|
};
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackA);
|
|
|
|
|
|
|
|
myEmitter.on('event', callbackB);
|
|
|
|
|
|
|
|
// callbackA removes listener callbackB but it will still be called.
|
2016-03-22 17:18:02 -04:00
|
|
|
// Internal listener array at time of emit [callbackA, callbackB]
|
2016-02-12 15:18:21 +05:30
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// A
|
|
|
|
// B
|
2016-02-12 15:18:21 +05:30
|
|
|
|
|
|
|
// callbackB is now removed.
|
2016-03-22 17:18:02 -04:00
|
|
|
// Internal listener array [callbackA]
|
2016-02-12 15:18:21 +05:30
|
|
|
myEmitter.emit('event');
|
2016-11-08 21:04:57 +01:00
|
|
|
// Prints:
|
|
|
|
// A
|
2016-02-12 15:18:21 +05:30
|
|
|
```
|
|
|
|
|
2015-12-29 10:04:13 -08:00
|
|
|
Because listeners are managed using an internal array, calling this will
|
2024-04-06 23:46:29 -07:00
|
|
|
change the position indexes of any listener registered _after_ the listener
|
2015-12-29 10:04:13 -08:00
|
|
|
being removed. This will not impact the order in which listeners are called,
|
2016-06-29 23:20:23 +05:30
|
|
|
but it means that any copies of the listener array as returned by
|
2015-12-29 10:04:13 -08:00
|
|
|
the `emitter.listeners()` method will need to be recreated.
|
|
|
|
|
2018-10-19 11:57:19 -07:00
|
|
|
When a single function has been added as a handler multiple times for a single
|
|
|
|
event (as in the example below), `removeListener()` will remove the most
|
|
|
|
recently added instance. In the example the `once('ping')`
|
|
|
|
listener is removed:
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
function pong() {
|
|
|
|
console.log('pong');
|
|
|
|
}
|
|
|
|
|
|
|
|
ee.on('ping', pong);
|
|
|
|
ee.once('ping', pong);
|
|
|
|
ee.removeListener('ping', pong);
|
|
|
|
|
|
|
|
ee.emit('ping');
|
|
|
|
ee.emit('ping');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2018-10-19 11:57:19 -07:00
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
function pong() {
|
|
|
|
console.log('pong');
|
|
|
|
}
|
|
|
|
|
|
|
|
ee.on('ping', pong);
|
|
|
|
ee.once('ping', pong);
|
|
|
|
ee.removeListener('ping', pong);
|
|
|
|
|
|
|
|
ee.emit('ping');
|
|
|
|
ee.emit('ping');
|
|
|
|
```
|
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2015-07-12 16:10:57 +00:00
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.setMaxListeners(n)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2016-07-21 15:58:56 +02:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.3.5
|
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `n` {integer}
|
|
|
|
* Returns: {EventEmitter}
|
2015-07-12 16:10:57 +00:00
|
|
|
|
2018-04-29 20:46:41 +03:00
|
|
|
By default `EventEmitter`s will print a warning if more than `10` listeners are
|
2015-12-29 10:04:13 -08:00
|
|
|
added for a particular event. This is a useful default that helps finding
|
2020-03-24 06:26:10 -07:00
|
|
|
memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
|
|
|
|
modified for this specific `EventEmitter` instance. The value can be set to
|
|
|
|
`Infinity` (or `0`) to indicate an unlimited number of listeners.
|
2015-08-20 04:07:52 +05:30
|
|
|
|
2016-06-29 23:20:23 +05:30
|
|
|
Returns a reference to the `EventEmitter`, so that calls can be chained.
|
2015-08-20 04:07:52 +05:30
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter.rawListeners(eventName)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2017-11-25 13:26:28 -05:00
|
|
|
<!-- YAML
|
2018-01-09 19:23:55 -05:00
|
|
|
added: v9.4.0
|
2017-11-25 13:26:28 -05:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2018-07-12 13:48:11 -04:00
|
|
|
* `eventName` {string|symbol}
|
2021-10-10 21:55:04 -07:00
|
|
|
* Returns: {Function\[]}
|
2017-11-25 13:26:28 -05:00
|
|
|
|
|
|
|
Returns a copy of the array of listeners for the event named `eventName`,
|
2018-04-09 19:30:22 +03:00
|
|
|
including any wrappers (such as those created by `.once()`).
|
2017-11-25 13:26:28 -05:00
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
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');
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
2017-11-25 13:26:28 -05:00
|
|
|
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];
|
|
|
|
|
2018-12-03 17:15:45 +01:00
|
|
|
// Logs "log once" to the console and does not unbind the `once` event
|
2017-11-25 13:26:28 -05:00
|
|
|
logFnWrapper.listener();
|
|
|
|
|
2018-12-10 13:27:32 +01:00
|
|
|
// Logs "log once" to the console and removes the listener
|
2017-11-25 13:26:28 -05:00
|
|
|
logFnWrapper();
|
|
|
|
|
|
|
|
emitter.on('log', () => console.log('log persistently'));
|
2018-12-03 17:15:45 +01:00
|
|
|
// Will return a new Array with a single function bound by `.on()` above
|
2017-11-25 13:26:28 -05:00
|
|
|
const newListeners = emitter.rawListeners('log');
|
|
|
|
|
2019-03-22 03:44:26 +01:00
|
|
|
// Logs "log persistently" twice
|
2017-11-25 13:26:28 -05:00
|
|
|
newListeners[0]();
|
|
|
|
emitter.emit('log');
|
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
### `emitter[Symbol.for('nodejs.rejection')](err, eventName[, ...args])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
|
|
|
- v13.4.0
|
|
|
|
- v12.16.0
|
2021-12-27 06:14:35 -08:00
|
|
|
changes:
|
2022-02-01 00:34:51 -05:00
|
|
|
- version:
|
|
|
|
- v17.4.0
|
|
|
|
- v16.14.0
|
2021-12-27 06:14:35 -08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/41267
|
|
|
|
description: No longer experimental.
|
2019-05-19 13:55:18 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `err` Error
|
|
|
|
* `eventName` {string|symbol}
|
|
|
|
* `...args` {any}
|
|
|
|
|
|
|
|
The `Symbol.for('nodejs.rejection')` method is called in case a
|
|
|
|
promise rejection happens when emitting an event and
|
|
|
|
[`captureRejections`][capturerejections] is enabled on the emitter.
|
|
|
|
It is possible to use [`events.captureRejectionSymbol`][rejectionsymbol] in
|
|
|
|
place of `Symbol.for('nodejs.rejection')`.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, captureRejectionSymbol } from 'node:events';
|
|
|
|
|
|
|
|
class MyClass extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super({ captureRejections: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
[captureRejectionSymbol](err, event, ...args) {
|
|
|
|
console.log('rejection happened for', event, 'with', err, ...args);
|
|
|
|
this.destroy(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(err) {
|
|
|
|
// Tear the resource down here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, captureRejectionSymbol } = require('node:events');
|
2019-05-19 13:55:18 +02:00
|
|
|
|
|
|
|
class MyClass extends EventEmitter {
|
|
|
|
constructor() {
|
|
|
|
super({ captureRejections: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
[captureRejectionSymbol](err, event, ...args) {
|
|
|
|
console.log('rejection happened for', event, 'with', err, ...args);
|
|
|
|
this.destroy(err);
|
|
|
|
}
|
|
|
|
|
|
|
|
destroy(err) {
|
|
|
|
// Tear the resource down here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
## `events.defaultMaxListeners`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.11.2
|
|
|
|
-->
|
|
|
|
|
|
|
|
By default, a maximum of `10` listeners can be registered for any single
|
|
|
|
event. This limit can be changed for individual `EventEmitter` instances
|
|
|
|
using the [`emitter.setMaxListeners(n)`][] method. To change the default
|
2021-10-10 21:55:04 -07:00
|
|
|
for _all_ `EventEmitter` instances, the `events.defaultMaxListeners`
|
2020-11-12 10:02:12 -08:00
|
|
|
property can be used. If this value is not a positive number, a `RangeError`
|
|
|
|
is thrown.
|
|
|
|
|
|
|
|
Take caution when setting the `events.defaultMaxListeners` because the
|
2021-10-10 21:55:04 -07:00
|
|
|
change affects _all_ `EventEmitter` instances, including those created before
|
2020-11-12 10:02:12 -08:00
|
|
|
the change is made. However, calling [`emitter.setMaxListeners(n)`][] still has
|
|
|
|
precedence over `events.defaultMaxListeners`.
|
|
|
|
|
|
|
|
This is not a hard limit. The `EventEmitter` instance will allow
|
|
|
|
more listeners to be added but will output a trace warning to stderr indicating
|
|
|
|
that a "possible EventEmitter memory leak" has been detected. For any single
|
|
|
|
`EventEmitter`, the `emitter.getMaxListeners()` and `emitter.setMaxListeners()`
|
|
|
|
methods can be used to temporarily avoid this warning:
|
|
|
|
|
2023-01-19 04:25:11 +09:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter } from 'node:events';
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
|
|
emitter.once('event', () => {
|
|
|
|
// do stuff
|
|
|
|
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const EventEmitter = require('node:events');
|
|
|
|
const emitter = new EventEmitter();
|
2020-11-12 10:02:12 -08:00
|
|
|
emitter.setMaxListeners(emitter.getMaxListeners() + 1);
|
|
|
|
emitter.once('event', () => {
|
|
|
|
// do stuff
|
|
|
|
emitter.setMaxListeners(Math.max(emitter.getMaxListeners() - 1, 0));
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
The [`--trace-warnings`][] command-line flag can be used to display the
|
|
|
|
stack trace for such warnings.
|
|
|
|
|
|
|
|
The emitted warning can be inspected with [`process.on('warning')`][] and will
|
2022-05-15 20:23:31 +02:00
|
|
|
have the additional `emitter`, `type`, and `count` properties, referring to
|
2022-05-17 21:04:51 +02:00
|
|
|
the event emitter instance, the event's name and the number of attached
|
2020-11-12 10:02:12 -08:00
|
|
|
listeners, respectively.
|
|
|
|
Its `name` property is set to `'MaxListenersExceededWarning'`.
|
|
|
|
|
|
|
|
## `events.errorMonitor`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
<!-- YAML
|
|
|
|
added:
|
|
|
|
- v13.6.0
|
|
|
|
- v12.17.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
This symbol shall be used to install a listener for only monitoring `'error'`
|
|
|
|
events. Listeners installed using this symbol are called before the regular
|
|
|
|
`'error'` listeners are called.
|
|
|
|
|
|
|
|
Installing a listener using this symbol does not change the behavior once an
|
2022-03-27 15:33:06 -07:00
|
|
|
`'error'` event is emitted. Therefore, the process will still crash if no
|
2020-11-12 10:02:12 -08:00
|
|
|
regular `'error'` listener is installed.
|
|
|
|
|
2020-11-06 12:05:08 +02:00
|
|
|
## `events.getEventListeners(emitterOrTarget, eventName)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-06 12:05:08 +02:00
|
|
|
<!-- YAML
|
|
|
|
added:
|
2020-11-09 14:47:44 -05:00
|
|
|
- v15.2.0
|
2021-05-11, Version 14.17.0 'Fermium' (LTS)
Notable Changes:
Diagnostics channel (experimental module):
`diagnostics_channel` is a new experimental module that provides an API
to create named channels to report arbitrary message data for
diagnostics purposes.
The module was initially introduced in Node.js v15.1.0 and is
backported to v14.17.0 to enable testing it at a larger scale.
With `diagnostics_channel`, Node.js core and module authors can publish
contextual data about what they are doing at a given time. This could
be the hostname and query string of a mysql query, for example. Just
create a named channel with `dc.channel(name)` and call
`channel.publish(data)` to send the data to any listeners to that
channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
MySQL.prototype.query = function query(queryString, values, callback) {
// Broadcast query information whenever a query is made
channel.publish({
query: queryString,
host: this.hostname,
});
this.doQuery(queryString, values, callback);
};
```
Channels are like one big global event emitter but are split into
separate objects to ensure they get the best performance. If nothing is
listening to the channel, the publishing overhead should be as close to
zero as possible. Consuming channel data is as easy as using
`channel.subscribe(listener)` to run a function whenever a message is
published to that channel.
```js
const dc = require('diagnostics_channel');
const channel = dc.channel('mysql.query');
channel.subscribe(({ query, host }) => {
console.log(`mysql query to ${host}: ${query}`);
});
```
The data captured can be used to provide context for what an app is
doing at a given time. This can be used for things like augmenting
tracing data, tracking network and filesystem activity, logging
queries, and many other things. It's also a very useful data source
for diagnostics tools to provide a clearer picture of exactly what the
application is doing at a given point in the data they are presenting.
Contributed by Stephen Belanger (https://github.com/nodejs/node/pull/34895).
UUID support in the crypto module:
The new `crypto.randomUUID()` method now allows to generate random
[RFC 4122](https://www.rfc-editor.org/rfc/rfc4122.txt) Version 4
UUID strings:
```js
const { randomUUID } = require('crypto');
console.log(randomUUID());
// 'aa7c91a1-f8fc-4339-b9db-f93fc7233429'
```
Contributed by James M Snell (https://github.com/nodejs/node/pull/36729).
Experimental support for `AbortController` and `AbortSignal`:
Node.js 14.17.0 adds experimental partial support for `AbortController`
and `AbortSignal`.
Both constructors can be enabled globally using the
`--experimental-abortcontroller` flag.
Additionally, several Node.js APIs have been updated to support
`AbortSignal` for cancellation.
It is not mandatory to use the built-in constructors with them. Any
spec-compliant third-party alternatives should be compatible.
`AbortSignal` support was added to the following methods:
* `child_process.exec`
* `child_process.execFile`
* `child_process.fork`
* `child_process.spawn`
* `dgram.createSocket`
* `events.on`
* `events.once`
* `fs.readFile`
* `fs.watch`
* `fs.writeFile`
* `http.request`
* `https.request`
* `http2Session.request`
* The promisified variants of `setImmediate` and `setTimeout`
Other notable changes:
* doc:
* revoke deprecation of legacy url, change status to legacy (James M Snell) (https://github.com/nodejs/node/pull/37784)
* add legacy status to stability index (James M Snell) (https://github.com/nodejs/node/pull/37784)
* upgrade stability status of report API (Gireesh Punathil) (https://github.com/nodejs/node/pull/35654)
* deps:
* V8: Backport various patches for Apple Silicon support (BoHong Li) (https://github.com/nodejs/node/pull/38051)
* update ICU to 68.1 (Michaël Zasso) (https://github.com/nodejs/node/pull/36187)
* upgrade to libuv 1.41.0 (Colin Ihrig) (https://github.com/nodejs/node/pull/37360)
* http:
* add http.ClientRequest.getRawHeaderNames() (simov) (https://github.com/nodejs/node/pull/37660)
* report request start and end with diagnostics\_channel (Stephen Belanger) (https://github.com/nodejs/node/pull/34895)
* util:
* add getSystemErrorMap() impl (eladkeyshawn) (https://github.com/nodejs/node/pull/38101)
PR-URL: https://github.com/nodejs/node/pull/38507
2021-05-02 23:12:18 -04:00
|
|
|
- v14.17.0
|
2020-11-06 12:05:08 +02:00
|
|
|
-->
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-06 12:05:08 +02:00
|
|
|
* `emitterOrTarget` {EventEmitter|EventTarget}
|
|
|
|
* `eventName` {string|symbol}
|
2021-10-10 21:55:04 -07:00
|
|
|
* Returns: {Function\[]}
|
2020-11-06 12:05:08 +02:00
|
|
|
|
|
|
|
Returns a copy of the array of listeners for the event named `eventName`.
|
|
|
|
|
|
|
|
For `EventEmitter`s this behaves exactly the same as calling `.listeners` on
|
|
|
|
the emitter.
|
|
|
|
|
|
|
|
For `EventTarget`s this is the only way to get the event listeners for the
|
|
|
|
event target. This is useful for debugging and diagnostic purposes.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { getEventListeners, EventEmitter } from 'node:events';
|
|
|
|
|
|
|
|
{
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
const listener = () => console.log('Events are fun');
|
|
|
|
ee.on('foo', listener);
|
2023-01-21 19:39:54 +09:00
|
|
|
console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
|
2022-05-28 11:50:20 +08:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const et = new EventTarget();
|
|
|
|
const listener = () => console.log('Events are fun');
|
|
|
|
et.addEventListener('foo', listener);
|
2023-01-21 19:39:54 +09:00
|
|
|
console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
|
2022-05-28 11:50:20 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { getEventListeners, EventEmitter } = require('node:events');
|
2020-11-06 12:05:08 +02:00
|
|
|
|
|
|
|
{
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
const listener = () => console.log('Events are fun');
|
|
|
|
ee.on('foo', listener);
|
2023-01-21 19:39:54 +09:00
|
|
|
console.log(getEventListeners(ee, 'foo')); // [ [Function: listener] ]
|
2020-11-06 12:05:08 +02:00
|
|
|
}
|
|
|
|
{
|
|
|
|
const et = new EventTarget();
|
|
|
|
const listener = () => console.log('Events are fun');
|
2020-11-11 18:52:57 +03:00
|
|
|
et.addEventListener('foo', listener);
|
2023-01-21 19:39:54 +09:00
|
|
|
console.log(getEventListeners(et, 'foo')); // [ [Function: listener] ]
|
2020-11-06 12:05:08 +02:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2023-03-19 08:58:19 -04:00
|
|
|
## `events.getMaxListeners(emitterOrTarget)`
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-07-10 08:12:52 -04:00
|
|
|
added:
|
|
|
|
- v19.9.0
|
|
|
|
- v18.17.0
|
2023-03-19 08:58:19 -04:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `emitterOrTarget` {EventEmitter|EventTarget}
|
|
|
|
* Returns: {number}
|
|
|
|
|
|
|
|
Returns the currently set max amount of listeners.
|
|
|
|
|
|
|
|
For `EventEmitter`s this behaves exactly the same as calling `.getMaxListeners` on
|
|
|
|
the emitter.
|
|
|
|
|
|
|
|
For `EventTarget`s this is the only way to get the max event listeners for the
|
|
|
|
event target. If the number of event handlers on a single EventTarget exceeds
|
|
|
|
the max set, the EventTarget will print a warning.
|
|
|
|
|
|
|
|
```mjs
|
|
|
|
import { getMaxListeners, setMaxListeners, EventEmitter } from 'node:events';
|
|
|
|
|
|
|
|
{
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
console.log(getMaxListeners(ee)); // 10
|
|
|
|
setMaxListeners(11, ee);
|
|
|
|
console.log(getMaxListeners(ee)); // 11
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const et = new EventTarget();
|
|
|
|
console.log(getMaxListeners(et)); // 10
|
|
|
|
setMaxListeners(11, et);
|
|
|
|
console.log(getMaxListeners(et)); // 11
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const { getMaxListeners, setMaxListeners, EventEmitter } = require('node:events');
|
|
|
|
|
|
|
|
{
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
console.log(getMaxListeners(ee)); // 10
|
|
|
|
setMaxListeners(11, ee);
|
|
|
|
console.log(getMaxListeners(ee)); // 11
|
|
|
|
}
|
|
|
|
{
|
|
|
|
const et = new EventTarget();
|
|
|
|
console.log(getMaxListeners(et)); // 10
|
|
|
|
setMaxListeners(11, et);
|
|
|
|
console.log(getMaxListeners(et)); // 11
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-08-24 13:11:23 -07:00
|
|
|
## `events.once(emitter, name[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-02-13 13:30:28 +01:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
|
|
|
- v11.13.0
|
|
|
|
- v10.16.0
|
2021-03-29 13:57:47 +02:00
|
|
|
changes:
|
|
|
|
- version: v15.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/34912
|
|
|
|
description: The `signal` option is supported now.
|
2019-02-13 13:30:28 +01:00
|
|
|
-->
|
2019-09-06 01:42:22 -04:00
|
|
|
|
2019-02-13 13:30:28 +01:00
|
|
|
* `emitter` {EventEmitter}
|
2024-06-27 22:56:25 +01:00
|
|
|
* `name` {string|symbol}
|
2020-08-24 13:11:23 -07:00
|
|
|
* `options` {Object}
|
2020-08-31 19:36:20 -07:00
|
|
|
* `signal` {AbortSignal} Can be used to cancel waiting for the event.
|
2019-02-13 13:30:28 +01:00
|
|
|
* Returns: {Promise}
|
|
|
|
|
2019-08-19 01:13:26 +03:00
|
|
|
Creates a `Promise` that is fulfilled when the `EventEmitter` emits the given
|
2020-07-06 12:51:46 -07:00
|
|
|
event or that is rejected if the `EventEmitter` emits `'error'` while waiting.
|
2019-02-13 13:30:28 +01:00
|
|
|
The `Promise` will resolve with an array of all the arguments emitted to the
|
|
|
|
given event.
|
|
|
|
|
2019-08-19 01:13:26 +03:00
|
|
|
This method is intentionally generic and works with the web platform
|
2019-09-23 16:57:03 +03:00
|
|
|
[EventTarget][WHATWG-EventTarget] interface, which has no special
|
2019-08-19 01:13:26 +03:00
|
|
|
`'error'` event semantics and does not listen to the `'error'` event.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { once, EventEmitter } from 'node:events';
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
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) {
|
2022-12-07 03:47:15 +03:00
|
|
|
console.error('error happened', err);
|
2022-05-28 11:50:20 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { once, EventEmitter } = require('node:events');
|
2019-02-13 13:30:28 +01:00
|
|
|
|
|
|
|
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) {
|
2022-12-07 03:47:15 +03:00
|
|
|
console.error('error happened', err);
|
2019-02-13 13:30:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
run();
|
|
|
|
```
|
2019-09-23 16:57:03 +03:00
|
|
|
|
2020-07-06 12:51:46 -07:00
|
|
|
The special handling of the `'error'` event is only used when `events.once()`
|
|
|
|
is used to wait for another event. 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:
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, once } from 'node:events';
|
|
|
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
once(ee, 'error')
|
|
|
|
.then(([err]) => console.log('ok', err.message))
|
2022-12-07 03:47:15 +03:00
|
|
|
.catch((err) => console.error('error', err.message));
|
2022-05-28 11:50:20 +08:00
|
|
|
|
|
|
|
ee.emit('error', new Error('boom'));
|
|
|
|
|
|
|
|
// Prints: ok boom
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, once } = require('node:events');
|
2020-07-06 12:51:46 -07:00
|
|
|
|
|
|
|
const ee = new EventEmitter();
|
|
|
|
|
|
|
|
once(ee, 'error')
|
|
|
|
.then(([err]) => console.log('ok', err.message))
|
2022-12-07 03:47:15 +03:00
|
|
|
.catch((err) => console.error('error', err.message));
|
2020-07-06 12:51:46 -07:00
|
|
|
|
|
|
|
ee.emit('error', new Error('boom'));
|
|
|
|
|
|
|
|
// Prints: ok boom
|
|
|
|
```
|
|
|
|
|
2020-08-31 19:36:20 -07:00
|
|
|
An {AbortSignal} can be used to cancel waiting for the event:
|
2020-08-24 13:11:23 -07:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, once } from 'node: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!
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, once } = require('node:events');
|
2020-08-24 13:11:23 -07:00
|
|
|
|
|
|
|
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
|
2020-08-31 19:36:20 -07:00
|
|
|
ee.emit('foo'); // Prints: Waiting for the event was canceled!
|
2020-08-24 13:11:23 -07:00
|
|
|
```
|
|
|
|
|
2020-07-06 08:00:51 -07:00
|
|
|
### Awaiting multiple events emitted on `process.nextTick()`
|
|
|
|
|
|
|
|
There is an edge case worth noting when using the `events.once()` function
|
|
|
|
to await multiple events emitted on in the same batch of `process.nextTick()`
|
|
|
|
operations, or whenever multiple events are emitted synchronously. Specifically,
|
|
|
|
because the `process.nextTick()` queue is drained before the `Promise` microtask
|
|
|
|
queue, and because `EventEmitter` emits all events synchronously, it is possible
|
|
|
|
for `events.once()` to miss an event.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, once } from 'node:events';
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
|
|
|
async function foo() {
|
|
|
|
await once(myEE, 'bar');
|
|
|
|
console.log('bar');
|
|
|
|
|
|
|
|
// This Promise will never resolve because the 'foo' event will
|
|
|
|
// have already been emitted before the Promise is created.
|
|
|
|
await once(myEE, 'foo');
|
|
|
|
console.log('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
myEE.emit('bar');
|
|
|
|
myEE.emit('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
foo().then(() => console.log('done'));
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, once } = require('node:events');
|
2020-07-06 08:00:51 -07:00
|
|
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
|
|
|
async function foo() {
|
|
|
|
await once(myEE, 'bar');
|
|
|
|
console.log('bar');
|
|
|
|
|
|
|
|
// This Promise will never resolve because the 'foo' event will
|
|
|
|
// have already been emitted before the Promise is created.
|
|
|
|
await once(myEE, 'foo');
|
|
|
|
console.log('foo');
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
myEE.emit('bar');
|
|
|
|
myEE.emit('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
foo().then(() => console.log('done'));
|
|
|
|
```
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
To catch both events, create each of the Promises _before_ awaiting either
|
2020-07-06 08:00:51 -07:00
|
|
|
of them, then it becomes possible to use `Promise.all()`, `Promise.race()`,
|
|
|
|
or `Promise.allSettled()`:
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, once } from 'node:events';
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
|
|
|
async function foo() {
|
|
|
|
await Promise.all([once(myEE, 'bar'), once(myEE, 'foo')]);
|
|
|
|
console.log('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
myEE.emit('bar');
|
|
|
|
myEE.emit('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
foo().then(() => console.log('done'));
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, once } = require('node:events');
|
2020-07-06 08:00:51 -07:00
|
|
|
|
|
|
|
const myEE = new EventEmitter();
|
|
|
|
|
|
|
|
async function foo() {
|
|
|
|
await Promise.all([once(myEE, 'bar'), once(myEE, 'foo')]);
|
|
|
|
console.log('foo', 'bar');
|
|
|
|
}
|
|
|
|
|
|
|
|
process.nextTick(() => {
|
|
|
|
myEE.emit('bar');
|
|
|
|
myEE.emit('foo');
|
|
|
|
});
|
|
|
|
|
|
|
|
foo().then(() => console.log('done'));
|
|
|
|
```
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
## `events.captureRejections`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
|
|
|
- v13.4.0
|
|
|
|
- v12.16.0
|
2021-12-27 06:14:35 -08:00
|
|
|
changes:
|
2022-02-01 00:34:51 -05:00
|
|
|
- version:
|
|
|
|
- v17.4.0
|
|
|
|
- v16.14.0
|
2021-12-27 06:14:35 -08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/41267
|
|
|
|
description: No longer experimental.
|
2019-05-19 13:55:18 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
Value: {boolean}
|
|
|
|
|
|
|
|
Change the default `captureRejections` option on all new `EventEmitter` objects.
|
|
|
|
|
2019-12-23 21:35:16 -08:00
|
|
|
## `events.captureRejectionSymbol`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-05-19 13:55:18 +02:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
2021-12-27 06:14:35 -08:00
|
|
|
- v13.4.0
|
|
|
|
- v12.16.0
|
|
|
|
changes:
|
2022-02-01 00:34:51 -05:00
|
|
|
- version:
|
|
|
|
- v17.4.0
|
|
|
|
- v16.14.0
|
2021-12-27 06:14:35 -08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/41267
|
|
|
|
description: No longer experimental.
|
2019-05-19 13:55:18 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
Value: `Symbol.for('nodejs.rejection')`
|
|
|
|
|
|
|
|
See how to write a custom [rejection handler][rejection].
|
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
## `events.listenerCount(emitter, eventName)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
<!-- YAML
|
|
|
|
added: v0.9.12
|
|
|
|
deprecated: v3.2.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
> Stability: 0 - Deprecated: Use [`emitter.listenerCount()`][] instead.
|
|
|
|
|
|
|
|
* `emitter` {EventEmitter} The emitter to query
|
|
|
|
* `eventName` {string|symbol} The event name
|
|
|
|
|
|
|
|
A class method that returns the number of listeners for the given `eventName`
|
|
|
|
registered on the given `emitter`.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { EventEmitter, listenerCount } from 'node:events';
|
|
|
|
|
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
console.log(listenerCount(myEmitter, 'event'));
|
|
|
|
// Prints: 2
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { EventEmitter, listenerCount } = require('node:events');
|
2022-05-28 11:50:20 +08:00
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
const myEmitter = new EventEmitter();
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
myEmitter.on('event', () => {});
|
|
|
|
console.log(listenerCount(myEmitter, 'event'));
|
|
|
|
// Prints: 2
|
|
|
|
```
|
|
|
|
|
2020-08-24 13:48:15 -07:00
|
|
|
## `events.on(emitter, eventName[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2019-05-30 17:58:55 +02:00
|
|
|
<!-- YAML
|
2020-04-24 18:43:06 +02:00
|
|
|
added:
|
|
|
|
- v13.6.0
|
|
|
|
- v12.16.0
|
2024-03-14 10:02:46 +02:00
|
|
|
changes:
|
2024-05-02 11:31:36 +02:00
|
|
|
- version:
|
|
|
|
- v22.0.0
|
|
|
|
- v20.13.0
|
2024-03-14 10:02:46 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/52080
|
|
|
|
description: Support `highWaterMark` and `lowWaterMark` options,
|
|
|
|
For consistency. Old options are still supported.
|
|
|
|
- version:
|
|
|
|
- v20.0.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/41276
|
|
|
|
description: The `close`, `highWatermark`, and `lowWatermark`
|
|
|
|
options are supported now.
|
2019-05-30 17:58:55 +02:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `emitter` {EventEmitter}
|
|
|
|
* `eventName` {string|symbol} The name of the event being listened for
|
2020-08-24 13:48:15 -07:00
|
|
|
* `options` {Object}
|
2020-09-02 20:55:43 -07:00
|
|
|
* `signal` {AbortSignal} Can be used to cancel awaiting events.
|
2024-03-14 10:02:46 +02:00
|
|
|
* `close` - {string\[]} Names of events that will end the iteration.
|
|
|
|
* `highWaterMark` - {integer} **Default:** `Number.MAX_SAFE_INTEGER`
|
|
|
|
The high watermark. The emitter is paused every time the size of events
|
|
|
|
being buffered is higher than it. Supported only on emitters implementing
|
|
|
|
`pause()` and `resume()` methods.
|
|
|
|
* `lowWaterMark` - {integer} **Default:** `1`
|
|
|
|
The low watermark. The emitter is resumed every time the size of events
|
|
|
|
being buffered is lower than it. Supported only on emitters implementing
|
|
|
|
`pause()` and `resume()` methods.
|
2019-05-30 17:58:55 +02:00
|
|
|
* Returns: {AsyncIterator} that iterates `eventName` events emitted by the `emitter`
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { on, EventEmitter } from 'node:events';
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
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
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { on, EventEmitter } = require('node:events');
|
2019-05-30 17:58:55 +02:00
|
|
|
|
|
|
|
(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]
|
|
|
|
}
|
2020-03-19 22:19:46 +08:00
|
|
|
// Unreachable here
|
2019-05-30 17:58:55 +02:00
|
|
|
})();
|
|
|
|
```
|
|
|
|
|
|
|
|
Returns an `AsyncIterator` that iterates `eventName` events. It will throw
|
|
|
|
if the `EventEmitter` emits `'error'`. It removes all listeners when
|
|
|
|
exiting the loop. The `value` returned by each iteration is an array
|
|
|
|
composed of the emitted event arguments.
|
|
|
|
|
2020-09-02 20:55:43 -07:00
|
|
|
An {AbortSignal} can be used to cancel waiting on events:
|
2020-08-24 13:48:15 -07:00
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { on, EventEmitter } from 'node:events';
|
|
|
|
import process from 'node:process';
|
|
|
|
|
|
|
|
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());
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-04-20 10:23:41 +02:00
|
|
|
const { on, EventEmitter } = require('node:events');
|
2022-05-28 11:50:20 +08:00
|
|
|
|
2020-08-24 13:48:15 -07:00
|
|
|
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());
|
|
|
|
```
|
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
## `events.setMaxListeners(n[, ...eventTargets])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-11-12 10:02:12 -08:00
|
|
|
<!-- YAML
|
2020-12-07 15:40:58 -05:00
|
|
|
added: v15.4.0
|
2020-11-12 10:02:12 -08:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `n` {number} A non-negative number. The maximum number of listeners per
|
|
|
|
`EventTarget` event.
|
2021-10-10 21:55:04 -07:00
|
|
|
* `...eventsTargets` {EventTarget\[]|EventEmitter\[]} Zero or more {EventTarget}
|
2020-11-12 10:02:12 -08:00
|
|
|
or {EventEmitter} instances. If none are specified, `n` is set as the default
|
|
|
|
max for all newly created {EventTarget} and {EventEmitter} objects.
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
|
|
|
import { setMaxListeners, EventEmitter } from 'node:events';
|
|
|
|
|
|
|
|
const target = new EventTarget();
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
|
|
|
|
setMaxListeners(5, target, emitter);
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2020-11-12 10:02:12 -08:00
|
|
|
const {
|
|
|
|
setMaxListeners,
|
2022-11-17 08:19:12 -05:00
|
|
|
EventEmitter,
|
2022-04-20 10:23:41 +02:00
|
|
|
} = require('node:events');
|
2020-11-12 10:02:12 -08:00
|
|
|
|
|
|
|
const target = new EventTarget();
|
|
|
|
const emitter = new EventEmitter();
|
|
|
|
|
|
|
|
setMaxListeners(5, target, emitter);
|
|
|
|
```
|
|
|
|
|
2023-07-28 00:10:48 +09:00
|
|
|
## `events.addAbortListener(signal, listener)`
|
2023-07-06 01:04:23 +03:00
|
|
|
|
|
|
|
<!-- YAML
|
2023-09-16 22:51:24 -04:00
|
|
|
added:
|
|
|
|
- v20.5.0
|
|
|
|
- v18.18.0
|
2023-07-06 01:04:23 +03:00
|
|
|
-->
|
|
|
|
|
|
|
|
> Stability: 1 - Experimental
|
|
|
|
|
|
|
|
* `signal` {AbortSignal}
|
|
|
|
* `listener` {Function|EventListener}
|
2024-02-14 00:37:42 +03:00
|
|
|
* Returns: {Disposable} A Disposable that removes the `abort` listener.
|
2023-07-06 01:04:23 +03:00
|
|
|
|
|
|
|
Listens once to the `abort` event on the provided `signal`.
|
|
|
|
|
|
|
|
Listening to the `abort` event on abort signals is unsafe and may
|
|
|
|
lead to resource leaks since another third party with the signal can
|
|
|
|
call [`e.stopImmediatePropagation()`][]. Unfortunately Node.js cannot change
|
|
|
|
this since it would violate the web standard. Additionally, the original
|
|
|
|
API makes it easy to forget to remove listeners.
|
|
|
|
|
|
|
|
This API allows safely using `AbortSignal`s in Node.js APIs by solving these
|
|
|
|
two issues by listening to the event such that `stopImmediatePropagation` does
|
|
|
|
not prevent the listener from running.
|
|
|
|
|
|
|
|
Returns a disposable so that it may be unsubscribed from more easily.
|
|
|
|
|
|
|
|
```cjs
|
|
|
|
const { addAbortListener } = require('node:events');
|
|
|
|
|
|
|
|
function example(signal) {
|
|
|
|
let disposable;
|
|
|
|
try {
|
|
|
|
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
|
|
|
|
disposable = addAbortListener(signal, (e) => {
|
|
|
|
// Do something when signal is aborted.
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
disposable?.[Symbol.dispose]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```mjs
|
|
|
|
import { addAbortListener } from 'node:events';
|
|
|
|
|
|
|
|
function example(signal) {
|
|
|
|
let disposable;
|
|
|
|
try {
|
|
|
|
signal.addEventListener('abort', (e) => e.stopImmediatePropagation());
|
|
|
|
disposable = addAbortListener(signal, (e) => {
|
|
|
|
// Do something when signal is aborted.
|
|
|
|
});
|
|
|
|
} finally {
|
|
|
|
disposable?.[Symbol.dispose]();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2021-12-22 16:56:57 -08:00
|
|
|
## Class: `events.EventEmitterAsyncResource extends EventEmitter`
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-02-01 00:34:51 -05:00
|
|
|
added:
|
|
|
|
- v17.4.0
|
|
|
|
- v16.14.0
|
2021-12-22 16:56:57 -08:00
|
|
|
-->
|
|
|
|
|
|
|
|
Integrates `EventEmitter` with {AsyncResource} for `EventEmitter`s that
|
|
|
|
require manual async tracking. Specifically, all events emitted by instances
|
|
|
|
of `events.EventEmitterAsyncResource` will run within its [async context][].
|
|
|
|
|
2022-05-28 11:50:20 +08:00
|
|
|
```mjs
|
2022-09-01 00:59:15 +01:00
|
|
|
import { EventEmitterAsyncResource, EventEmitter } from 'node:events';
|
2022-05-28 11:50:20 +08:00
|
|
|
import { notStrictEqual, strictEqual } from 'node:assert';
|
2022-09-01 00:59:15 +01:00
|
|
|
import { executionAsyncId, triggerAsyncId } from 'node:async_hooks';
|
2022-05-28 11:50:20 +08:00
|
|
|
|
|
|
|
// Async tracking tooling will identify this as 'Q'.
|
|
|
|
const ee1 = new EventEmitterAsyncResource({ name: 'Q' });
|
|
|
|
|
|
|
|
// 'foo' listeners will run in the EventEmitters async context.
|
|
|
|
ee1.on('foo', () => {
|
|
|
|
strictEqual(executionAsyncId(), ee1.asyncId);
|
|
|
|
strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ee2 = new EventEmitter();
|
|
|
|
|
|
|
|
// 'foo' listeners on ordinary EventEmitters that do not track async
|
|
|
|
// context, however, run in the same async context as the emit().
|
|
|
|
ee2.on('foo', () => {
|
|
|
|
notStrictEqual(executionAsyncId(), ee2.asyncId);
|
|
|
|
notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
ee1.emit('foo');
|
|
|
|
ee2.emit('foo');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
```cjs
|
2022-09-01 00:59:15 +01:00
|
|
|
const { EventEmitterAsyncResource, EventEmitter } = require('node:events');
|
2022-04-20 10:23:41 +02:00
|
|
|
const { notStrictEqual, strictEqual } = require('node:assert');
|
2022-09-01 00:59:15 +01:00
|
|
|
const { executionAsyncId, triggerAsyncId } = require('node:async_hooks');
|
2021-12-22 16:56:57 -08:00
|
|
|
|
|
|
|
// Async tracking tooling will identify this as 'Q'.
|
|
|
|
const ee1 = new EventEmitterAsyncResource({ name: 'Q' });
|
|
|
|
|
|
|
|
// 'foo' listeners will run in the EventEmitters async context.
|
|
|
|
ee1.on('foo', () => {
|
|
|
|
strictEqual(executionAsyncId(), ee1.asyncId);
|
|
|
|
strictEqual(triggerAsyncId(), ee1.triggerAsyncId);
|
|
|
|
});
|
|
|
|
|
|
|
|
const ee2 = new EventEmitter();
|
|
|
|
|
|
|
|
// 'foo' listeners on ordinary EventEmitters that do not track async
|
|
|
|
// context, however, run in the same async context as the emit().
|
|
|
|
ee2.on('foo', () => {
|
|
|
|
notStrictEqual(executionAsyncId(), ee2.asyncId);
|
|
|
|
notStrictEqual(triggerAsyncId(), ee2.triggerAsyncId);
|
|
|
|
});
|
|
|
|
|
|
|
|
Promise.resolve().then(() => {
|
|
|
|
ee1.emit('foo');
|
|
|
|
ee2.emit('foo');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
The `EventEmitterAsyncResource` class has the same methods and takes the
|
|
|
|
same options as `EventEmitter` and `AsyncResource` themselves.
|
|
|
|
|
2022-12-27 22:00:30 +09:00
|
|
|
### `new events.EventEmitterAsyncResource([options])`
|
2021-12-22 16:56:57 -08:00
|
|
|
|
|
|
|
* `options` {Object}
|
|
|
|
* `captureRejections` {boolean} It enables
|
|
|
|
[automatic capturing of promise rejection][capturerejections].
|
|
|
|
**Default:** `false`.
|
2023-06-12 21:45:04 +05:30
|
|
|
* `name` {string} The type of async event. **Default:** [`new.target.name`][].
|
2021-12-22 16:56:57 -08:00
|
|
|
* `triggerAsyncId` {number} The ID of the execution context that created this
|
|
|
|
async event. **Default:** `executionAsyncId()`.
|
|
|
|
* `requireManualDestroy` {boolean} If set to `true`, disables `emitDestroy`
|
|
|
|
when the object is garbage collected. This usually does not need to be set
|
|
|
|
(even if `emitDestroy` is called manually), unless the resource's `asyncId`
|
|
|
|
is retrieved and the sensitive API's `emitDestroy` is called with it.
|
|
|
|
When set to `false`, the `emitDestroy` call on garbage collection
|
|
|
|
will only take place if there is at least one active `destroy` hook.
|
|
|
|
**Default:** `false`.
|
|
|
|
|
|
|
|
### `eventemitterasyncresource.asyncId`
|
|
|
|
|
|
|
|
* Type: {number} The unique `asyncId` assigned to the resource.
|
|
|
|
|
|
|
|
### `eventemitterasyncresource.asyncResource`
|
|
|
|
|
|
|
|
* Type: The underlying {AsyncResource}.
|
|
|
|
|
|
|
|
The returned `AsyncResource` object has an additional `eventEmitter` property
|
|
|
|
that provides a reference to this `EventEmitterAsyncResource`.
|
|
|
|
|
|
|
|
### `eventemitterasyncresource.emitDestroy()`
|
|
|
|
|
|
|
|
Call all `destroy` hooks. This should only ever be called once. An error will
|
|
|
|
be thrown if it is called more than once. This **must** be manually called. If
|
|
|
|
the resource is left to be collected by the GC then the `destroy` hooks will
|
|
|
|
never be called.
|
|
|
|
|
|
|
|
### `eventemitterasyncresource.triggerAsyncId`
|
|
|
|
|
|
|
|
* Type: {number} The same `triggerAsyncId` that is passed to the
|
|
|
|
`AsyncResource` constructor.
|
|
|
|
|
2020-10-04 13:11:36 +02:00
|
|
|
<a id="event-target-and-event-api"></a>
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
## `EventTarget` and `Event` API
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-10-04 13:11:36 +02:00
|
|
|
changes:
|
2021-03-03 15:36:13 +00:00
|
|
|
- version: v16.0.0
|
2021-02-04 15:31:47 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/37237
|
|
|
|
description: changed EventTarget error handling.
|
2020-12-07 15:40:58 -05:00
|
|
|
- version: v15.4.0
|
2020-11-03 12:26:19 -08:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/35949
|
|
|
|
description: No longer experimental.
|
2020-10-20, Version 15.0.0 (Current)
Notable changes:
Deprecations and Removals:
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
npm 7 (https://github.com/nodejs/node/pull/35631):
Node.js 15 comes with a new major release of npm, npm 7. npm 7 comes
with many new features - including npm workspaces and a new
package-lock.json format. npm 7 also includes yarn.lock file support.
One of the big changes in npm 7 is that peer dependencies are now
installed by default.
Throw On Unhandled Rejections
(https://github.com/nodejs/node/pull/33021):
As of Node.js 15, the default mode for `unhandledRejection` is changed
to `throw` (from `warn`). In `throw` mode, if an `unhandledRejection`
hook is not set, the `unhandledRejection` is raised as an uncaught
exception. Users that have an `unhandledRejection` hook should see no
change in behavior, and it’s still possible to switch modes using the
`--unhandled-rejections=mode` process flag.
QUIC (https://github.com/nodejs/node/pull/32379):
Node.js 15 comes with experimental support QUIC, which can be enabled
by compiling Node.js with the `--experimental-quic` configuration flag.
The Node.js QUIC implementation is exposed by the core `net` module.
V8 8.6 (https://github.com/nodejs/node/pull/35415):
The V8 JavaScript engine has been updated to V8 8.6 (V8 8.4 is the
latest available in Node.js 14). Along with performance tweaks and
improvements the V8 update also brings the following language features:
* `Promise.any()` (from V8 8.5)
* `AggregateError` (from V8 8.5)
* `String.prototype.replaceAll()` (from V8 8.5)
* Logical assignment operators `&&=`, `||=`, and `??=` (from V8 8.5)
Other Notable Changes:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
Semver-Major Commits:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **build**: reset embedder string to "-node.0" (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **crypto**: refactoring internals, add WebCrypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **crypto**: move node\_crypto files to src/crypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **deps**: V8: cherry-pick d76abfed3512 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 717543bbf0ef (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 6be2f6e26e8d (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix V8 build issue with inline methods (Jiawen Geng)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix platform-embedded-file-writer-win for ARM64 (Michaël
Zasso) (https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 postmortem metadata script (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 to 8.6.395 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: upgrade npm to 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **deps**: update npm to 7.0.0-rc.3 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **deps**: V8: cherry-pick 0d6debcc5f08 (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **doc**: update support macos version for 15.x (Ash Cripps)
(https://github.com/nodejs/node/pull/35022)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http**: fixed socket.setEncoding fatal error (iskore)
(https://github.com/nodejs/node/pull/33405)
- **http**: emit 'error' on aborted server request (Robert Nagy)
(https://github.com/nodejs/node/pull/33172)
- **http**: cleanup end argument handling (Robert Nagy)
(https://github.com/nodejs/node/pull/31818)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **http2**: add `invalidheaders` test (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33161)
- **http2**: refactor state code validation for the http2Stream class
(rickyes) (https://github.com/nodejs/node/pull/33535)
- **http2**: header field valid checks (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33193)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: remove ERR\_INVALID\_OPT\_VALUE and
ERR\_INVALID\_OPT\_VALUE\_ENCODING (Denys Otrishko)
(https://github.com/nodejs/node/pull/34682)
- **lib**: handle one of args case in ERR\_MISSING\_ARGS (Denys
Otrishko) (https://github.com/nodejs/node/pull/34022)
- **lib**: remove NodeError from the prototype of errors with code
(Michaël Zasso) (https://github.com/nodejs/node/pull/33857)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: check args in net.connect() and socket.connect() calls
(Denys Otrishko) (https://github.com/nodejs/node/pull/34022)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **process**: update v8 fast api calls usage (Maya Lekova)
(https://github.com/nodejs/node/pull/35415)
- **process**: change default --unhandled-rejections=throw (Dan
Fabulich) (https://github.com/nodejs/node/pull/33021)
- **process**: use v8 fast api calls for hrtime (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **process**: delay throwing an error using `throwDeprecation` (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/32312)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: implement NodePlatform::PostJob (Clemens Backes)
(https://github.com/nodejs/node/pull/35415)
- **src**: update NODE\_MODULE\_VERSION to 88 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **src**: error reporting on CPUUsage (Yash Ladha)
(https://github.com/nodejs/node/pull/34762)
- **src**: use node:moduleName as builtin module filename (Michaël
Zasso) (https://github.com/nodejs/node/pull/35498)
- **src**: enable wasm trap handler on windows (Gus Caplan)
(https://github.com/nodejs/node/pull/35033)
- **src**: update NODE\_MODULE\_VERSION to 86 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33579)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **src**: remove \_third\_party\_main support (Anna Henningsen)
(https://github.com/nodejs/node/pull/33971)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **src**: remove unused CancelPendingDelayedTasks (Anna Henningsen)
(https://github.com/nodejs/node/pull/32859)
- **stream**: try to wait for flush to complete before 'finish' (Robert
Nagy) (https://github.com/nodejs/node/pull/34314)
- **stream**: cleanup and fix Readable.wrap (Robert Nagy)
(https://github.com/nodejs/node/pull/34204)
- **stream**: add promises version to utility functions (rickyes)
(https://github.com/nodejs/node/pull/33991)
- **stream**: fix writable.end callback behavior (Robert Nagy)
(https://github.com/nodejs/node/pull/34101)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **stream**: write should throw on unknown encoding (Robert Nagy)
(https://github.com/nodejs/node/pull/33075)
- **stream**: fix \_final and 'prefinish' timing (Robert Nagy)
(https://github.com/nodejs/node/pull/32780)
- **stream**: simplify Transform stream implementation (Robert Nagy)
(https://github.com/nodejs/node/pull/32763)
- **stream**: use callback to properly propagate error (Robert Nagy)
(https://github.com/nodejs/node/pull/29179)
- **test**: update tests after increasing typed array size to 4GB
(Kim-Anh Tran) (https://github.com/nodejs/node/pull/35415)
- **test**: fix tests for npm 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **test**: fix test suite to work with npm 7 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **test**: update WPT harness and tests (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **tools**: disable x86 safe exception handlers in V8 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.6 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.5 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **url**: file URL path normalization (Daijiro Wachi)
(https://github.com/nodejs/node/pull/35477)
- **url**: verify domain is not empty after "ToASCII" (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove U+0000 case in the fragment state (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove gopher from special schemes (Michaël Zasso)
(https://github.com/nodejs/node/pull/33325)
- **url**: forbid lt and gt in url host code point (Yash Ladha)
(https://github.com/nodejs/node/pull/33328)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **win, child_process**: sanitize env variables (Bartosz Sosnowski)
(https://github.com/nodejs/node/pull/35210)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
- **worker**: set trackUnmanagedFds to true by default (Anna
Henningsen) (https://github.com/nodejs/node/pull/34394)
- **worker**: rename error code to be more accurate (Anna Henningsen)
(https://github.com/nodejs/node/pull/33872)
PR-URL: https://github.com/nodejs/node/pull/35014
2020-09-01 21:16:46 +01:00
|
|
|
- version: v15.0.0
|
2020-10-04 13:11:36 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/35496
|
|
|
|
description:
|
|
|
|
The `EventTarget` and `Event` classes are now available as globals.
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
The `EventTarget` and `Event` objects are a Node.js-specific implementation
|
|
|
|
of the [`EventTarget` Web API][] that are exposed by some Node.js core APIs.
|
|
|
|
|
|
|
|
```js
|
2020-10-04 13:11:36 +02:00
|
|
|
const target = new EventTarget();
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
target.addEventListener('foo', (event) => {
|
|
|
|
console.log('foo event happened!');
|
|
|
|
});
|
|
|
|
```
|
|
|
|
|
|
|
|
### Node.js `EventTarget` vs. DOM `EventTarget`
|
|
|
|
|
|
|
|
There are two key differences between the Node.js `EventTarget` and the
|
|
|
|
[`EventTarget` Web API][]:
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
1. Whereas DOM `EventTarget` instances _may_ be hierarchical, there is no
|
2020-05-23 15:43:58 -07:00
|
|
|
concept of hierarchy and event propagation in Node.js. That is, an event
|
|
|
|
dispatched to an `EventTarget` does not propagate through a hierarchy of
|
|
|
|
nested target objects that may each have their own set of handlers for the
|
|
|
|
event.
|
|
|
|
2. In the Node.js `EventTarget`, if an event listener is an async function
|
|
|
|
or returns a `Promise`, and the returned `Promise` rejects, the rejection
|
2020-09-05 06:36:59 -07:00
|
|
|
is automatically captured and handled the same way as a listener that
|
2020-06-14 14:49:34 -07:00
|
|
|
throws synchronously (see [`EventTarget` error handling][] for details).
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
### `NodeEventTarget` vs. `EventEmitter`
|
|
|
|
|
|
|
|
The `NodeEventTarget` object implements a modified subset of the
|
2021-10-10 21:55:04 -07:00
|
|
|
`EventEmitter` API that allows it to closely _emulate_ an `EventEmitter` in
|
|
|
|
certain situations. A `NodeEventTarget` is _not_ an instance of `EventEmitter`
|
2020-05-31 16:20:20 -04:00
|
|
|
and cannot be used in place of an `EventEmitter` in most cases.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
1. Unlike `EventEmitter`, any given `listener` can be registered at most once
|
2020-09-05 06:36:59 -07:00
|
|
|
per event `type`. Attempts to register a `listener` multiple times are
|
2020-05-23 15:43:58 -07:00
|
|
|
ignored.
|
|
|
|
2. The `NodeEventTarget` does not emulate the full `EventEmitter` API.
|
|
|
|
Specifically the `prependListener()`, `prependOnceListener()`,
|
2023-01-19 18:41:09 +09:00
|
|
|
`rawListeners()`, and `errorMonitor` APIs are not emulated.
|
|
|
|
The `'newListener'` and `'removeListener'` events will also not be emitted.
|
2020-05-23 15:43:58 -07:00
|
|
|
3. The `NodeEventTarget` does not implement any special default behavior
|
|
|
|
for events with type `'error'`.
|
2021-10-10 21:55:04 -07:00
|
|
|
4. The `NodeEventTarget` supports `EventListener` objects as well as
|
2020-05-23 15:43:58 -07:00
|
|
|
functions as handlers for all event types.
|
|
|
|
|
2020-06-14 14:49:34 -07:00
|
|
|
### Event listener
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
Event listeners registered for an event `type` may either be JavaScript
|
|
|
|
functions or objects with a `handleEvent` property whose value is a function.
|
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
In either case, the handler function is invoked with the `event` argument
|
2020-05-23 15:43:58 -07:00
|
|
|
passed to the `eventTarget.dispatchEvent()` function.
|
|
|
|
|
|
|
|
Async functions may be used as event listeners. If an async handler function
|
2020-09-05 06:36:59 -07:00
|
|
|
rejects, the rejection is captured and handled as described in
|
2020-06-14 14:49:34 -07:00
|
|
|
[`EventTarget` error handling][].
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
An error thrown by one handler function does not prevent the other handlers
|
2020-05-23 15:43:58 -07:00
|
|
|
from being invoked.
|
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
The return value of a handler function is ignored.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
Handlers are always invoked in the order they were added.
|
|
|
|
|
|
|
|
Handler functions may mutate the `event` object.
|
|
|
|
|
|
|
|
```js
|
|
|
|
function handler1(event) {
|
|
|
|
console.log(event.type); // Prints 'foo'
|
|
|
|
event.a = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function handler2(event) {
|
|
|
|
console.log(event.type); // Prints 'foo'
|
|
|
|
console.log(event.a); // Prints 1
|
|
|
|
}
|
|
|
|
|
|
|
|
const handler3 = {
|
|
|
|
handleEvent(event) {
|
|
|
|
console.log(event.type); // Prints 'foo'
|
2022-11-17 08:19:12 -05:00
|
|
|
},
|
2020-05-23 15:43:58 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
const handler4 = {
|
|
|
|
async handleEvent(event) {
|
|
|
|
console.log(event.type); // Prints 'foo'
|
2022-11-17 08:19:12 -05:00
|
|
|
},
|
2020-05-23 15:43:58 -07:00
|
|
|
};
|
|
|
|
|
2020-10-04 13:11:36 +02:00
|
|
|
const target = new EventTarget();
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
target.addEventListener('foo', handler1);
|
|
|
|
target.addEventListener('foo', handler2);
|
|
|
|
target.addEventListener('foo', handler3);
|
|
|
|
target.addEventListener('foo', handler4, { once: true });
|
|
|
|
```
|
|
|
|
|
2020-06-14 14:49:34 -07:00
|
|
|
### `EventTarget` error handling
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
When a registered event listener throws (or returns a Promise that rejects),
|
2021-02-07 14:25:35 +02:00
|
|
|
by default the error is treated as an uncaught exception on
|
|
|
|
`process.nextTick()`. This means uncaught exceptions in `EventTarget`s will
|
|
|
|
terminate the Node.js process by default.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
Throwing within an event listener will _not_ stop the other registered handlers
|
2021-02-07 14:25:35 +02:00
|
|
|
from being invoked.
|
|
|
|
|
|
|
|
The `EventTarget` does not implement any special default handling for `'error'`
|
|
|
|
type events like `EventEmitter`.
|
|
|
|
|
|
|
|
Currently errors are first forwarded to the `process.on('error')` event
|
|
|
|
before reaching `process.on('uncaughtException')`. This behavior is
|
|
|
|
deprecated and will change in a future release to align `EventTarget` with
|
|
|
|
other Node.js APIs. Any code relying on the `process.on('error')` event should
|
|
|
|
be aligned with the new behavior.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
### Class: `Event`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-10-04 13:11:36 +02:00
|
|
|
changes:
|
2020-10-20, Version 15.0.0 (Current)
Notable changes:
Deprecations and Removals:
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
npm 7 (https://github.com/nodejs/node/pull/35631):
Node.js 15 comes with a new major release of npm, npm 7. npm 7 comes
with many new features - including npm workspaces and a new
package-lock.json format. npm 7 also includes yarn.lock file support.
One of the big changes in npm 7 is that peer dependencies are now
installed by default.
Throw On Unhandled Rejections
(https://github.com/nodejs/node/pull/33021):
As of Node.js 15, the default mode for `unhandledRejection` is changed
to `throw` (from `warn`). In `throw` mode, if an `unhandledRejection`
hook is not set, the `unhandledRejection` is raised as an uncaught
exception. Users that have an `unhandledRejection` hook should see no
change in behavior, and it’s still possible to switch modes using the
`--unhandled-rejections=mode` process flag.
QUIC (https://github.com/nodejs/node/pull/32379):
Node.js 15 comes with experimental support QUIC, which can be enabled
by compiling Node.js with the `--experimental-quic` configuration flag.
The Node.js QUIC implementation is exposed by the core `net` module.
V8 8.6 (https://github.com/nodejs/node/pull/35415):
The V8 JavaScript engine has been updated to V8 8.6 (V8 8.4 is the
latest available in Node.js 14). Along with performance tweaks and
improvements the V8 update also brings the following language features:
* `Promise.any()` (from V8 8.5)
* `AggregateError` (from V8 8.5)
* `String.prototype.replaceAll()` (from V8 8.5)
* Logical assignment operators `&&=`, `||=`, and `??=` (from V8 8.5)
Other Notable Changes:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
Semver-Major Commits:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **build**: reset embedder string to "-node.0" (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **crypto**: refactoring internals, add WebCrypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **crypto**: move node\_crypto files to src/crypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **deps**: V8: cherry-pick d76abfed3512 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 717543bbf0ef (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 6be2f6e26e8d (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix V8 build issue with inline methods (Jiawen Geng)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix platform-embedded-file-writer-win for ARM64 (Michaël
Zasso) (https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 postmortem metadata script (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 to 8.6.395 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: upgrade npm to 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **deps**: update npm to 7.0.0-rc.3 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **deps**: V8: cherry-pick 0d6debcc5f08 (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **doc**: update support macos version for 15.x (Ash Cripps)
(https://github.com/nodejs/node/pull/35022)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http**: fixed socket.setEncoding fatal error (iskore)
(https://github.com/nodejs/node/pull/33405)
- **http**: emit 'error' on aborted server request (Robert Nagy)
(https://github.com/nodejs/node/pull/33172)
- **http**: cleanup end argument handling (Robert Nagy)
(https://github.com/nodejs/node/pull/31818)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **http2**: add `invalidheaders` test (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33161)
- **http2**: refactor state code validation for the http2Stream class
(rickyes) (https://github.com/nodejs/node/pull/33535)
- **http2**: header field valid checks (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33193)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: remove ERR\_INVALID\_OPT\_VALUE and
ERR\_INVALID\_OPT\_VALUE\_ENCODING (Denys Otrishko)
(https://github.com/nodejs/node/pull/34682)
- **lib**: handle one of args case in ERR\_MISSING\_ARGS (Denys
Otrishko) (https://github.com/nodejs/node/pull/34022)
- **lib**: remove NodeError from the prototype of errors with code
(Michaël Zasso) (https://github.com/nodejs/node/pull/33857)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: check args in net.connect() and socket.connect() calls
(Denys Otrishko) (https://github.com/nodejs/node/pull/34022)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **process**: update v8 fast api calls usage (Maya Lekova)
(https://github.com/nodejs/node/pull/35415)
- **process**: change default --unhandled-rejections=throw (Dan
Fabulich) (https://github.com/nodejs/node/pull/33021)
- **process**: use v8 fast api calls for hrtime (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **process**: delay throwing an error using `throwDeprecation` (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/32312)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: implement NodePlatform::PostJob (Clemens Backes)
(https://github.com/nodejs/node/pull/35415)
- **src**: update NODE\_MODULE\_VERSION to 88 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **src**: error reporting on CPUUsage (Yash Ladha)
(https://github.com/nodejs/node/pull/34762)
- **src**: use node:moduleName as builtin module filename (Michaël
Zasso) (https://github.com/nodejs/node/pull/35498)
- **src**: enable wasm trap handler on windows (Gus Caplan)
(https://github.com/nodejs/node/pull/35033)
- **src**: update NODE\_MODULE\_VERSION to 86 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33579)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **src**: remove \_third\_party\_main support (Anna Henningsen)
(https://github.com/nodejs/node/pull/33971)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **src**: remove unused CancelPendingDelayedTasks (Anna Henningsen)
(https://github.com/nodejs/node/pull/32859)
- **stream**: try to wait for flush to complete before 'finish' (Robert
Nagy) (https://github.com/nodejs/node/pull/34314)
- **stream**: cleanup and fix Readable.wrap (Robert Nagy)
(https://github.com/nodejs/node/pull/34204)
- **stream**: add promises version to utility functions (rickyes)
(https://github.com/nodejs/node/pull/33991)
- **stream**: fix writable.end callback behavior (Robert Nagy)
(https://github.com/nodejs/node/pull/34101)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **stream**: write should throw on unknown encoding (Robert Nagy)
(https://github.com/nodejs/node/pull/33075)
- **stream**: fix \_final and 'prefinish' timing (Robert Nagy)
(https://github.com/nodejs/node/pull/32780)
- **stream**: simplify Transform stream implementation (Robert Nagy)
(https://github.com/nodejs/node/pull/32763)
- **stream**: use callback to properly propagate error (Robert Nagy)
(https://github.com/nodejs/node/pull/29179)
- **test**: update tests after increasing typed array size to 4GB
(Kim-Anh Tran) (https://github.com/nodejs/node/pull/35415)
- **test**: fix tests for npm 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **test**: fix test suite to work with npm 7 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **test**: update WPT harness and tests (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **tools**: disable x86 safe exception handlers in V8 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.6 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.5 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **url**: file URL path normalization (Daijiro Wachi)
(https://github.com/nodejs/node/pull/35477)
- **url**: verify domain is not empty after "ToASCII" (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove U+0000 case in the fragment state (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove gopher from special schemes (Michaël Zasso)
(https://github.com/nodejs/node/pull/33325)
- **url**: forbid lt and gt in url host code point (Yash Ladha)
(https://github.com/nodejs/node/pull/33328)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **win, child_process**: sanitize env variables (Bartosz Sosnowski)
(https://github.com/nodejs/node/pull/35210)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
- **worker**: set trackUnmanagedFds to true by default (Anna
Henningsen) (https://github.com/nodejs/node/pull/34394)
- **worker**: rename error code to be more accurate (Anna Henningsen)
(https://github.com/nodejs/node/pull/33872)
PR-URL: https://github.com/nodejs/node/pull/35014
2020-09-01 21:16:46 +01:00
|
|
|
- version: v15.0.0
|
2020-10-04 13:11:36 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/35496
|
|
|
|
description: The `Event` class is now available through the global object.
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
The `Event` object is an adaptation of the [`Event` Web API][]. Instances
|
|
|
|
are created internally by Node.js.
|
|
|
|
|
|
|
|
#### `event.bubbles`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {boolean} Always returns `false`.
|
|
|
|
|
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
2023-01-07 16:56:34 +09:00
|
|
|
#### `event.cancelBubble`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2023-01-12 01:29:53 +09:00
|
|
|
> Stability: 3 - Legacy: Use [`event.stopPropagation()`][] instead.
|
|
|
|
|
2023-01-07 16:56:34 +09:00
|
|
|
* Type: {boolean}
|
|
|
|
|
|
|
|
Alias for `event.stopPropagation()` if set to `true`. This is not used
|
|
|
|
in Node.js and is provided purely for completeness.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
#### `event.cancelable`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {boolean} True if the event was created with the `cancelable` option.
|
|
|
|
|
|
|
|
#### `event.composed`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {boolean} Always returns `false`.
|
|
|
|
|
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
|
|
|
#### `event.composedPath()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
Returns an array containing the current `EventTarget` as the only entry or
|
2020-06-05 10:48:00 -07:00
|
|
|
empty if the event is not being dispatched. This is not used in
|
2020-05-23 15:43:58 -07:00
|
|
|
Node.js and is provided purely for completeness.
|
|
|
|
|
|
|
|
#### `event.currentTarget`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2020-06-05 10:48:00 -07:00
|
|
|
* Type: {EventTarget} The `EventTarget` dispatching the event.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
Alias for `event.target`.
|
|
|
|
|
|
|
|
#### `event.defaultPrevented`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {boolean}
|
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
Is `true` if `cancelable` is `true` and `event.preventDefault()` has been
|
2020-05-23 15:43:58 -07:00
|
|
|
called.
|
|
|
|
|
|
|
|
#### `event.eventPhase`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {number} Returns `0` while an event is not being dispatched, `2` while
|
|
|
|
it is being dispatched.
|
|
|
|
|
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
2023-01-19 17:59:00 +09:00
|
|
|
#### `event.initEvent(type[, bubbles[, cancelable]])`
|
|
|
|
|
|
|
|
<!-- YAML
|
2023-01-19 23:57:34 -03:00
|
|
|
added: v19.5.0
|
2023-01-19 17:59:00 +09:00
|
|
|
-->
|
|
|
|
|
|
|
|
> Stability: 3 - Legacy: The WHATWG spec considers it deprecated and users
|
|
|
|
> shouldn't use it at all.
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
* `bubbles` {boolean}
|
|
|
|
* `cancelable` {boolean}
|
|
|
|
|
|
|
|
Redundant with event constructors and incapable of setting `composed`.
|
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
#### `event.isTrusted`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2021-01-07 06:54:28 -08:00
|
|
|
* Type: {boolean}
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2021-01-07 06:54:28 -08:00
|
|
|
The {AbortSignal} `"abort"` event is emitted with `isTrusted` set to `true`. The
|
|
|
|
value is `false` in all other cases.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
#### `event.preventDefault()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
Sets the `defaultPrevented` property to `true` if `cancelable` is `true`.
|
|
|
|
|
|
|
|
#### `event.returnValue`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2023-01-19 16:10:25 +09:00
|
|
|
> Stability: 3 - Legacy: Use [`event.defaultPrevented`][] instead.
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Type: {boolean} True if the event has not been canceled.
|
|
|
|
|
2023-01-19 16:10:25 +09:00
|
|
|
The value of `event.returnValue` is always the opposite of `event.defaultPrevented`.
|
2020-05-23 15:43:58 -07:00
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
|
|
|
#### `event.srcElement`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2023-01-07 19:54:00 +09:00
|
|
|
> Stability: 3 - Legacy: Use [`event.target`][] instead.
|
|
|
|
|
2020-06-05 10:48:00 -07:00
|
|
|
* Type: {EventTarget} The `EventTarget` dispatching the event.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
Alias for `event.target`.
|
|
|
|
|
|
|
|
#### `event.stopImmediatePropagation()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
Stops the invocation of event listeners after the current one completes.
|
|
|
|
|
|
|
|
#### `event.stopPropagation()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
This is not used in Node.js and is provided purely for completeness.
|
|
|
|
|
|
|
|
#### `event.target`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2020-06-05 10:48:00 -07:00
|
|
|
* Type: {EventTarget} The `EventTarget` dispatching the event.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
#### `event.timeStamp`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {number}
|
|
|
|
|
|
|
|
The millisecond timestamp when the `Event` object was created.
|
|
|
|
|
|
|
|
#### `event.type`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* Type: {string}
|
|
|
|
|
|
|
|
The event type identifier.
|
|
|
|
|
|
|
|
### Class: `EventTarget`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-10-04 13:11:36 +02:00
|
|
|
changes:
|
2020-10-20, Version 15.0.0 (Current)
Notable changes:
Deprecations and Removals:
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
npm 7 (https://github.com/nodejs/node/pull/35631):
Node.js 15 comes with a new major release of npm, npm 7. npm 7 comes
with many new features - including npm workspaces and a new
package-lock.json format. npm 7 also includes yarn.lock file support.
One of the big changes in npm 7 is that peer dependencies are now
installed by default.
Throw On Unhandled Rejections
(https://github.com/nodejs/node/pull/33021):
As of Node.js 15, the default mode for `unhandledRejection` is changed
to `throw` (from `warn`). In `throw` mode, if an `unhandledRejection`
hook is not set, the `unhandledRejection` is raised as an uncaught
exception. Users that have an `unhandledRejection` hook should see no
change in behavior, and it’s still possible to switch modes using the
`--unhandled-rejections=mode` process flag.
QUIC (https://github.com/nodejs/node/pull/32379):
Node.js 15 comes with experimental support QUIC, which can be enabled
by compiling Node.js with the `--experimental-quic` configuration flag.
The Node.js QUIC implementation is exposed by the core `net` module.
V8 8.6 (https://github.com/nodejs/node/pull/35415):
The V8 JavaScript engine has been updated to V8 8.6 (V8 8.4 is the
latest available in Node.js 14). Along with performance tweaks and
improvements the V8 update also brings the following language features:
* `Promise.any()` (from V8 8.5)
* `AggregateError` (from V8 8.5)
* `String.prototype.replaceAll()` (from V8 8.5)
* Logical assignment operators `&&=`, `||=`, and `??=` (from V8 8.5)
Other Notable Changes:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
Semver-Major Commits:
- **assert**: add `assert/strict` alias module (ExE Boss)
(https://github.com/nodejs/node/pull/34001)
- **build**: reset embedder string to "-node.0" (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **build**: remove --build-v8-with-gn configure option (Yang Guo)
(https://github.com/nodejs/node/pull/27576)
- **build**: drop support for VS2017 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33694)
- **crypto**: refactoring internals, add WebCrypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **crypto**: move node\_crypto files to src/crypto (James M Snell)
(https://github.com/nodejs/node/pull/35093)
- **deps**: V8: cherry-pick d76abfed3512 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 717543bbf0ef (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: V8: cherry-pick 6be2f6e26e8d (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix V8 build issue with inline methods (Jiawen Geng)
(https://github.com/nodejs/node/pull/35415)
- **deps**: fix platform-embedded-file-writer-win for ARM64 (Michaël
Zasso) (https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 postmortem metadata script (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **deps**: update V8 to 8.6.395 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **deps**: upgrade npm to 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **deps**: update npm to 7.0.0-rc.3 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **deps**: V8: cherry-pick 0d6debcc5f08 (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **dns**: add dns/promises alias (shisama)
(https://github.com/nodejs/node/pull/32953)
- **doc**: move DEP0018 to End-of-Life (Rich Trott)
(https://github.com/nodejs/node/pull/35316)
- **doc**: update support macos version for 15.x (Ash Cripps)
(https://github.com/nodejs/node/pull/35022)
- **fs**: deprecation warning on recursive rmdir (Ian Sutherland)
(https://github.com/nodejs/node/pull/35562)
- **fs**: reimplement read and write streams using stream.construct
(Robert Nagy) (https://github.com/nodejs/node/pull/29656)
- **http**: fixed socket.setEncoding fatal error (iskore)
(https://github.com/nodejs/node/pull/33405)
- **http**: emit 'error' on aborted server request (Robert Nagy)
(https://github.com/nodejs/node/pull/33172)
- **http**: cleanup end argument handling (Robert Nagy)
(https://github.com/nodejs/node/pull/31818)
- **http2**: allow Host in HTTP/2 requests (Alba Mendez)
(https://github.com/nodejs/node/pull/34664)
- **http2**: add `invalidheaders` test (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33161)
- **http2**: refactor state code validation for the http2Stream class
(rickyes) (https://github.com/nodejs/node/pull/33535)
- **http2**: header field valid checks (Pranshu Srivastava)
(https://github.com/nodejs/node/pull/33193)
- **lib**: add EventTarget-related browser globals (Anna Henningsen)
(https://github.com/nodejs/node/pull/35496)
- **lib**: remove ERR\_INVALID\_OPT\_VALUE and
ERR\_INVALID\_OPT\_VALUE\_ENCODING (Denys Otrishko)
(https://github.com/nodejs/node/pull/34682)
- **lib**: handle one of args case in ERR\_MISSING\_ARGS (Denys
Otrishko) (https://github.com/nodejs/node/pull/34022)
- **lib**: remove NodeError from the prototype of errors with code
(Michaël Zasso) (https://github.com/nodejs/node/pull/33857)
- **lib**: unflag AbortController (James M Snell)
(https://github.com/nodejs/node/pull/33527)
- **lib**: initial experimental AbortController implementation (James M
Snell) (https://github.com/nodejs/node/pull/33527)
- **net**: check args in net.connect() and socket.connect() calls
(Denys Otrishko) (https://github.com/nodejs/node/pull/34022)
- **net**: remove long deprecated server.connections property (James M
Snell) (https://github.com/nodejs/node/pull/33647)
- **net**: autoDestroy Socket (Robert Nagy)
(https://github.com/nodejs/node/pull/31806)
- **process**: update v8 fast api calls usage (Maya Lekova)
(https://github.com/nodejs/node/pull/35415)
- **process**: change default --unhandled-rejections=throw (Dan
Fabulich) (https://github.com/nodejs/node/pull/33021)
- **process**: use v8 fast api calls for hrtime (Gus Caplan)
(https://github.com/nodejs/node/pull/33600)
- **process**: delay throwing an error using `throwDeprecation` (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/32312)
- **repl**: remove deprecated repl.memory function (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.turnOffEditorMode() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated repl.parseREPLKeyword() function (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated bufferedCommand property (Ruben
Bridgewater) (https://github.com/nodejs/node/pull/33286)
- **repl**: remove deprecated .rli (Ruben Bridgewater)
(https://github.com/nodejs/node/pull/33286)
- **src**: implement NodePlatform::PostJob (Clemens Backes)
(https://github.com/nodejs/node/pull/35415)
- **src**: update NODE\_MODULE\_VERSION to 88 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **src**: error reporting on CPUUsage (Yash Ladha)
(https://github.com/nodejs/node/pull/34762)
- **src**: use node:moduleName as builtin module filename (Michaël
Zasso) (https://github.com/nodejs/node/pull/35498)
- **src**: enable wasm trap handler on windows (Gus Caplan)
(https://github.com/nodejs/node/pull/35033)
- **src**: update NODE\_MODULE\_VERSION to 86 (Michaël Zasso)
(https://github.com/nodejs/node/pull/33579)
- **src**: disallow JS execution inside FreeEnvironment (Anna
Henningsen) (https://github.com/nodejs/node/pull/33874)
- **src**: remove \_third\_party\_main support (Anna Henningsen)
(https://github.com/nodejs/node/pull/33971)
- **src**: remove deprecated node debug command (James M Snell)
(https://github.com/nodejs/node/pull/33648)
- **src**: remove unused CancelPendingDelayedTasks (Anna Henningsen)
(https://github.com/nodejs/node/pull/32859)
- **stream**: try to wait for flush to complete before 'finish' (Robert
Nagy) (https://github.com/nodejs/node/pull/34314)
- **stream**: cleanup and fix Readable.wrap (Robert Nagy)
(https://github.com/nodejs/node/pull/34204)
- **stream**: add promises version to utility functions (rickyes)
(https://github.com/nodejs/node/pull/33991)
- **stream**: fix writable.end callback behavior (Robert Nagy)
(https://github.com/nodejs/node/pull/34101)
- **stream**: construct (Robert Nagy)
(https://github.com/nodejs/node/pull/29656)
- **stream**: write should throw on unknown encoding (Robert Nagy)
(https://github.com/nodejs/node/pull/33075)
- **stream**: fix \_final and 'prefinish' timing (Robert Nagy)
(https://github.com/nodejs/node/pull/32780)
- **stream**: simplify Transform stream implementation (Robert Nagy)
(https://github.com/nodejs/node/pull/32763)
- **stream**: use callback to properly propagate error (Robert Nagy)
(https://github.com/nodejs/node/pull/29179)
- **test**: update tests after increasing typed array size to 4GB
(Kim-Anh Tran) (https://github.com/nodejs/node/pull/35415)
- **test**: fix tests for npm 7.0.0 (Myles Borins)
(https://github.com/nodejs/node/pull/35631)
- **test**: fix test suite to work with npm 7 (Myles Borins)
(https://github.com/nodejs/node/pull/35474)
- **test**: update WPT harness and tests (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **timers**: introduce timers/promises (James M Snell)
(https://github.com/nodejs/node/pull/33950)
- **tools**: disable x86 safe exception handlers in V8 (Michaël Zasso)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.6 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **tools**: update V8 gypfiles for 8.5 (Ujjwal Sharma)
(https://github.com/nodejs/node/pull/35415)
- **url**: file URL path normalization (Daijiro Wachi)
(https://github.com/nodejs/node/pull/35477)
- **url**: verify domain is not empty after "ToASCII" (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove U+0000 case in the fragment state (Michaël Zasso)
(https://github.com/nodejs/node/pull/33770)
- **url**: remove gopher from special schemes (Michaël Zasso)
(https://github.com/nodejs/node/pull/33325)
- **url**: forbid lt and gt in url host code point (Yash Ladha)
(https://github.com/nodejs/node/pull/33328)
- **util**: change default value of `maxStringLength` to 10000
(unknown) (https://github.com/nodejs/node/pull/32744)
- **wasi**: drop --experimental-wasm-bigint requirement (Colin Ihrig)
(https://github.com/nodejs/node/pull/35415)
- **win, child_process**: sanitize env variables (Bartosz Sosnowski)
(https://github.com/nodejs/node/pull/35210)
- **worker**: make MessageEvent class more Web-compatible (Anna
Henningsen) (https://github.com/nodejs/node/pull/35496)
- **worker**: set trackUnmanagedFds to true by default (Anna
Henningsen) (https://github.com/nodejs/node/pull/34394)
- **worker**: rename error code to be more accurate (Anna Henningsen)
(https://github.com/nodejs/node/pull/33872)
PR-URL: https://github.com/nodejs/node/pull/35014
2020-09-01 21:16:46 +01:00
|
|
|
- version: v15.0.0
|
2020-10-04 13:11:36 +02:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/35496
|
|
|
|
description:
|
|
|
|
The `EventTarget` class is now available through the global object.
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
#### `eventTarget.addEventListener(type, listener[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2022-05-25 17:24:03 +02:00
|
|
|
changes:
|
|
|
|
- version: v15.4.0
|
|
|
|
pr-url: https://github.com/nodejs/node/pull/36258
|
|
|
|
description: add support for `signal` option.
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
* `listener` {Function|EventListener}
|
|
|
|
* `options` {Object}
|
2020-09-05 06:36:59 -07:00
|
|
|
* `once` {boolean} When `true`, the listener is automatically removed
|
2020-05-31 16:30:41 -04:00
|
|
|
when it is first invoked. **Default:** `false`.
|
2020-05-23 15:43:58 -07:00
|
|
|
* `passive` {boolean} When `true`, serves as a hint that the listener will
|
2020-05-31 16:30:41 -04:00
|
|
|
not call the `Event` object's `preventDefault()` method.
|
|
|
|
**Default:** `false`.
|
2020-05-23 15:43:58 -07:00
|
|
|
* `capture` {boolean} Not directly used by Node.js. Added for API
|
2020-05-31 16:30:41 -04:00
|
|
|
completeness. **Default:** `false`.
|
2022-05-25 17:24:03 +02:00
|
|
|
* `signal` {AbortSignal} The listener will be removed when the given
|
|
|
|
AbortSignal object's `abort()` method is called.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
Adds a new handler for the `type` event. Any given `listener` is added
|
2020-05-23 15:43:58 -07:00
|
|
|
only once per `type` and per `capture` option value.
|
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
If the `once` option is `true`, the `listener` is removed after the
|
2020-05-23 15:43:58 -07:00
|
|
|
next time a `type` event is dispatched.
|
|
|
|
|
|
|
|
The `capture` option is not used by Node.js in any functional way other than
|
|
|
|
tracking registered event listeners per the `EventTarget` specification.
|
|
|
|
Specifically, the `capture` option is used as part of the key when registering
|
|
|
|
a `listener`. Any individual `listener` may be added once with
|
|
|
|
`capture = false`, and once with `capture = true`.
|
|
|
|
|
|
|
|
```js
|
|
|
|
function handler(event) {}
|
|
|
|
|
2020-10-04 13:11:36 +02:00
|
|
|
const target = new EventTarget();
|
2020-05-23 15:43:58 -07:00
|
|
|
target.addEventListener('foo', handler, { capture: true }); // first
|
|
|
|
target.addEventListener('foo', handler, { capture: false }); // second
|
|
|
|
|
|
|
|
// Removes the second instance of handler
|
|
|
|
target.removeEventListener('foo', handler);
|
|
|
|
|
|
|
|
// Removes the first instance of handler
|
|
|
|
target.removeEventListener('foo', handler, { capture: true });
|
|
|
|
```
|
|
|
|
|
|
|
|
#### `eventTarget.dispatchEvent(event)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2021-06-23 20:14:56 +05:30
|
|
|
* `event` {Event}
|
2022-05-17 21:04:51 +02:00
|
|
|
* Returns: {boolean} `true` if either event's `cancelable` attribute value is
|
2021-06-23 20:14:56 +05:30
|
|
|
false or its `preventDefault()` method was not invoked, otherwise `false`.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2021-06-23 20:14:56 +05:30
|
|
|
Dispatches the `event` to the list of handlers for `event.type`.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2020-09-05 06:36:59 -07:00
|
|
|
The registered event listeners is synchronously invoked in the order they
|
2020-05-23 15:43:58 -07:00
|
|
|
were registered.
|
|
|
|
|
2022-11-29 18:56:43 +09:00
|
|
|
#### `eventTarget.removeEventListener(type, listener[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
* `listener` {Function|EventListener}
|
|
|
|
* `options` {Object}
|
|
|
|
* `capture` {boolean}
|
|
|
|
|
|
|
|
Removes the `listener` from the list of handlers for event `type`.
|
|
|
|
|
2022-07-17 20:27:49 +09:00
|
|
|
### Class: `CustomEvent`
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
added:
|
|
|
|
- v18.7.0
|
|
|
|
- v16.17.0
|
2024-04-23 16:09:23 +09:00
|
|
|
changes:
|
2024-05-02 11:31:36 +02:00
|
|
|
- version:
|
|
|
|
- v22.1.0
|
|
|
|
- v20.13.0
|
2024-04-23 16:09:23 +09:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/52618
|
|
|
|
description: CustomEvent is now stable.
|
2022-07-17 20:27:49 +09:00
|
|
|
-->
|
|
|
|
|
2024-04-23 16:09:23 +09:00
|
|
|
> Stability: 2 - Stable
|
2022-07-17 20:27:49 +09:00
|
|
|
|
|
|
|
* Extends: {Event}
|
|
|
|
|
|
|
|
The `CustomEvent` object is an adaptation of the [`CustomEvent` Web API][].
|
|
|
|
Instances are created internally by Node.js.
|
|
|
|
|
|
|
|
#### `event.detail`
|
|
|
|
|
|
|
|
<!-- YAML
|
2022-08-16, Version 16.17.0 'Gallium' (LTS)
Notable changes:
Adds `util.parseArgs` helper for higher level command-line argument
parsing.
Contributed by Benjamin Coe, John Gee, Darcy Clarke, Joe Sepi,
Kevin Gibbons, Aaron Casanova, Jessica Nahulan, and Jordan Harband.
https://github.com/nodejs/node/pull/42675
Node.js ESM Loader hooks now support multiple custom loaders, and
composition is achieved via "chaining": `foo-loader` calls `bar-loader`
calls `qux-loader` (a custom loader _must_ now signal a short circuit
when intentionally not calling the next). See the ESM docs
(https://nodejs.org/dist/latest-v16.x/docs/api/esm.html) for details.
Contributed by Jacob Smith, Geoffrey Booth, and Bradley Farias.
https://github.com/nodejs/node/pull/42623
The `node:test` module, which was initially introduced in Node.js
v18.0.0, is now available with all the changes done to it up to Node.js
v18.7.0.
To better align Node.js' experimental implementation of the Web Crypto
API with other runtimes, several changes were made:
* Support for CFRG curves was added, with the `'Ed25519'`, `'Ed448'`,
`'X25519'`, and `'X448'` algorithms.
* The proprietary `'NODE-DSA'`, `'NODE-DH'`, `'NODE-SCRYPT'`,
`'NODE-ED25519'`, `'NODE-ED448'`, `'NODE-X25519'`, and `'NODE-X448'`
algorithms were removed.
* The proprietary `'node.keyObject'` import/export format was removed.
Contributed by Filip Skokan.
https://github.com/nodejs/node/pull/42507
https://github.com/nodejs/node/pull/43310
Updated Corepack to 0.12.1 - https://github.com/nodejs/node/pull/43965
Updated ICU to 71.1 - https://github.com/nodejs/node/pull/42655
Updated npm to 8.15.0 - https://github.com/nodejs/node/pull/43917
Updated Undici to 5.8.0 - https://github.com/nodejs/node/pull/43886
(SEMVER-MINOR) crypto: make authTagLength optional for CC20P1305 (Tobias Nießen) https://github.com/nodejs/node/pull/42427
(SEMVER-MINOR) crypto: align webcrypto RSA key import/export with other implementations (Filip Skokan) https://github.com/nodejs/node/pull/42816
(SEMVER-MINOR) dns: export error code constants from `dns/promises` (Feng Yu) https://github.com/nodejs/node/pull/43176
doc: deprecate coercion to integer in process.exit (Daeyeon Jeong) https://github.com/nodejs/node/pull/43738
(SEMVER-MINOR) doc: deprecate diagnostics_channel object subscribe method (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) errors: add support for cause in aborterror (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) events: expose CustomEvent on global with CLI flag (Daeyeon Jeong) https://github.com/nodejs/node/pull/43885
(SEMVER-MINOR) events: add `CustomEvent` (Daeyeon Jeong) https://github.com/nodejs/node/pull/43514
(SEMVER-MINOR) events: propagate abortsignal reason in new AbortError ctor in events (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: propagate abortsignal reason in new AbortSignal constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) fs: make params in writing methods optional (LiviaMedeiros) https://github.com/nodejs/node/pull/42601
(SEMVER-MINOR) fs: add `read(buffer[, options])` versions (LiviaMedeiros) https://github.com/nodejs/node/pull/42768
(SEMVER-MINOR) http: add drop request event for http server (theanarkh) https://github.com/nodejs/node/pull/43806
(SEMVER-MINOR) http: add diagnostics channel for http client (theanarkh) https://github.com/nodejs/node/pull/43580
(SEMVER-MINOR) http: add perf_hooks detail for http request and client (theanarkh) https://github.com/nodejs/node/pull/43361
(SEMVER-MINOR) http: add uniqueHeaders option to request and createServer (Paolo Insogna) https://github.com/nodejs/node/pull/41397
(SEMVER-MINOR) http2: propagate abortsignal reason in new AbortError constructor (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) http2: compat support for array headers (OneNail) https://github.com/nodejs/node/pull/42901
(SEMVER-MINOR) lib: propagate abortsignal reason in new AbortError constructor in blob (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) lib: add abortSignal.throwIfAborted() (James M Snell) https://github.com/nodejs/node/pull/40951
(SEMVER-MINOR) lib: improved diagnostics_channel subscribe/unsubscribe (Stephen Belanger) https://github.com/nodejs/node/pull/42714
(SEMVER-MINOR) module: add isBuiltIn method (hemanth.hm) https://github.com/nodejs/node/pull/43396
(SEMVER-MINOR) module,repl: support 'node:'-only core modules (Colin Ihrig) https://github.com/nodejs/node/pull/42325
(SEMVER-MINOR) net: add drop event for net server (theanarkh) https://github.com/nodejs/node/pull/43582
(SEMVER-MINOR) net: add ability to reset a tcp socket (pupilTong) https://github.com/nodejs/node/pull/43112
(SEMVER-MINOR) node-api: emit uncaught-exception on unhandled tsfn callbacks (Chengzhong Wu) https://github.com/nodejs/node/pull/36510
(SEMVER-MINOR) perf_hooks: add PerformanceResourceTiming (RafaelGSS) https://github.com/nodejs/node/pull/42725
(SEMVER-MINOR) report: add more heap infos in process report (theanarkh) https://github.com/nodejs/node/pull/43116
(SEMVER-MINOR) src: add --openssl-legacy-provider option (Daniel Bevenius) https://github.com/nodejs/node/pull/40478
(SEMVER-MINOR) src: define fs.constants.S_IWUSR & S_IRUSR for Win (Liviu Ionescu) https://github.com/nodejs/node/pull/42757
(SEMVER-MINOR) src,doc,test: add --openssl-shared-config option (Daniel Bevenius) https://github.com/nodejs/node/pull/43124
(SEMVER-MINOR) stream: use cause options in AbortError constructors (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) stream: add iterator helper find (Nitzan Uziely) https://github.com/nodejs/node/pull/41849
(SEMVER-MINOR) stream: add writableAborted (Robert Nagy) https://github.com/nodejs/node/pull/40802
(SEMVER-MINOR) timers: propagate signal.reason in awaitable timers (James M Snell) https://github.com/nodejs/node/pull/41008
(SEMVER-MINOR) v8: add v8.startupSnapshot utils (Joyee Cheung) https://github.com/nodejs/node/pull/43329
(SEMVER-MINOR) v8: export more fields in getHeapStatistics (theanarkh) https://github.com/nodejs/node/pull/42784
(SEMVER-MINOR) worker: add hasRef() to MessagePort (Darshan Sen) https://github.com/nodejs/node/pull/42849
PR-URL: https://github.com/nodejs/node/pull/44098
2022-08-02 14:34:18 +02:00
|
|
|
added:
|
|
|
|
- v18.7.0
|
|
|
|
- v16.17.0
|
2024-04-23 16:09:23 +09:00
|
|
|
changes:
|
2024-05-02 11:31:36 +02:00
|
|
|
- version:
|
|
|
|
- v22.1.0
|
|
|
|
- v20.13.0
|
2024-04-23 16:09:23 +09:00
|
|
|
pr-url: https://github.com/nodejs/node/pull/52618
|
|
|
|
description: CustomEvent is now stable.
|
2022-07-17 20:27:49 +09:00
|
|
|
-->
|
|
|
|
|
2024-04-23 16:09:23 +09:00
|
|
|
> Stability: 2 - Stable
|
2022-07-17 20:27:49 +09:00
|
|
|
|
|
|
|
* Type: {any} Returns custom data passed when initializing.
|
|
|
|
|
|
|
|
Read-only.
|
|
|
|
|
2020-06-06 16:42:35 -04:00
|
|
|
### Class: `NodeEventTarget`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2020-06-06 16:42:35 -04:00
|
|
|
* Extends: {EventTarget}
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
The `NodeEventTarget` is a Node.js-specific extension to `EventTarget`
|
|
|
|
that emulates a subset of the `EventEmitter` API.
|
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
#### `nodeEventTarget.addListener(type, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* `listener` {Function|EventListener}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that emulates the
|
|
|
|
equivalent `EventEmitter` API. The only difference between `addListener()` and
|
|
|
|
`addEventListener()` is that `addListener()` will return a reference to the
|
|
|
|
`EventTarget`.
|
|
|
|
|
2023-01-26 12:13:54 +09:00
|
|
|
#### `nodeEventTarget.emit(type, arg)`
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added: v15.2.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
* `arg` {any}
|
|
|
|
* Returns: {boolean} `true` if event listeners registered for the `type` exist,
|
|
|
|
otherwise `false`.
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that dispatches the
|
|
|
|
`arg` to the list of handlers for `type`.
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
#### `nodeEventTarget.eventNames()`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
2021-10-10 21:55:04 -07:00
|
|
|
* Returns: {string\[]}
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that returns an array
|
2020-06-05 10:48:00 -07:00
|
|
|
of event `type` names for which event listeners are registered.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
|
|
|
#### `nodeEventTarget.listenerCount(type)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
|
|
|
|
* Returns: {number}
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that returns the number
|
|
|
|
of event listeners registered for the `type`.
|
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
#### `nodeEventTarget.setMaxListeners(n)`
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added: v14.5.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
* `n` {number}
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that sets the number
|
|
|
|
of max event listeners as `n`.
|
|
|
|
|
|
|
|
#### `nodeEventTarget.getMaxListeners()`
|
|
|
|
|
|
|
|
<!-- YAML
|
|
|
|
added: v14.5.0
|
|
|
|
-->
|
|
|
|
|
|
|
|
* Returns: {number}
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that returns the number
|
|
|
|
of max event listeners.
|
|
|
|
|
|
|
|
#### `nodeEventTarget.off(type, listener[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* `listener` {Function|EventListener}
|
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
* `options` {Object}
|
|
|
|
* `capture` {boolean}
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
2023-01-26 12:13:54 +09:00
|
|
|
Node.js-specific alias for `eventTarget.removeEventListener()`.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
#### `nodeEventTarget.on(type, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* `listener` {Function|EventListener}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
2023-01-26 12:13:54 +09:00
|
|
|
Node.js-specific alias for `eventTarget.addEventListener()`.
|
2020-05-23 15:43:58 -07:00
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
#### `nodeEventTarget.once(type, listener)`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* `listener` {Function|EventListener}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that adds a `once`
|
|
|
|
listener for the given event `type`. This is equivalent to calling `on`
|
|
|
|
with the `once` option set to `true`.
|
|
|
|
|
|
|
|
#### `nodeEventTarget.removeAllListeners([type])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
|
|
|
|
2020-10-26 09:03:16 +01:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
Node.js-specific extension to the `EventTarget` class. If `type` is specified,
|
|
|
|
removes all registered listeners for `type`, otherwise removes all registered
|
|
|
|
listeners.
|
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
#### `nodeEventTarget.removeListener(type, listener[, options])`
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
<!-- YAML
|
2020-06-27 20:26:32 -07:00
|
|
|
added: v14.5.0
|
2020-05-23 15:43:58 -07:00
|
|
|
-->
|
|
|
|
|
|
|
|
* `type` {string}
|
2021-10-10 21:55:04 -07:00
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* `listener` {Function|EventListener}
|
|
|
|
|
2023-01-19 18:41:09 +09:00
|
|
|
* `options` {Object}
|
|
|
|
* `capture` {boolean}
|
|
|
|
|
2020-05-23 15:43:58 -07:00
|
|
|
* Returns: {EventTarget} this
|
|
|
|
|
|
|
|
Node.js-specific extension to the `EventTarget` class that removes the
|
|
|
|
`listener` for the given `type`. The only difference between `removeListener()`
|
|
|
|
and `removeEventListener()` is that `removeListener()` will return a reference
|
|
|
|
to the `EventTarget`.
|
|
|
|
|
2019-09-23 16:57:03 +03:00
|
|
|
[WHATWG-EventTarget]: https://dom.spec.whatwg.org/#interface-eventtarget
|
2021-07-04 20:39:17 -07:00
|
|
|
[`--trace-warnings`]: cli.md#--trace-warnings
|
2022-07-17 20:27:49 +09:00
|
|
|
[`CustomEvent` Web API]: https://dom.spec.whatwg.org/#customevent
|
2020-09-17 18:53:37 +02:00
|
|
|
[`EventTarget` Web API]: https://dom.spec.whatwg.org/#eventtarget
|
2021-07-04 20:39:17 -07:00
|
|
|
[`EventTarget` error handling]: #eventtarget-error-handling
|
2020-09-17 18:53:37 +02:00
|
|
|
[`Event` Web API]: https://dom.spec.whatwg.org/#event
|
2020-09-14 17:09:13 +02:00
|
|
|
[`domain`]: domain.md
|
2023-07-06 01:04:23 +03:00
|
|
|
[`e.stopImmediatePropagation()`]: #eventstopimmediatepropagation
|
2023-02-21 02:38:51 -08:00
|
|
|
[`emitter.listenerCount()`]: #emitterlistenercounteventname-listener
|
2021-07-04 20:39:17 -07:00
|
|
|
[`emitter.removeListener()`]: #emitterremovelistenereventname-listener
|
|
|
|
[`emitter.setMaxListeners(n)`]: #emittersetmaxlistenersn
|
2023-01-19 16:10:25 +09:00
|
|
|
[`event.defaultPrevented`]: #eventdefaultprevented
|
2023-01-12 01:29:53 +09:00
|
|
|
[`event.stopPropagation()`]: #eventstoppropagation
|
2023-01-07 19:54:00 +09:00
|
|
|
[`event.target`]: #eventtarget
|
2021-07-04 20:39:17 -07:00
|
|
|
[`events.defaultMaxListeners`]: #eventsdefaultmaxlisteners
|
|
|
|
[`fs.ReadStream`]: fs.md#class-fsreadstream
|
|
|
|
[`net.Server`]: net.md#class-netserver
|
2021-12-22 16:56:57 -08:00
|
|
|
[`new.target.name`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/new.target
|
2021-07-04 20:39:17 -07:00
|
|
|
[`process.on('warning')`]: process.md#event-warning
|
2021-12-22 16:56:57 -08:00
|
|
|
[async context]: async_context.md
|
2021-07-04 20:39:17 -07:00
|
|
|
[capturerejections]: #capture-rejections-of-promises
|
|
|
|
[error]: #error-events
|
|
|
|
[rejection]: #emittersymbolfornodejsrejectionerr-eventname-args
|
|
|
|
[rejectionsymbol]: #eventscapturerejectionsymbol
|
2021-06-27 14:25:47 +02:00
|
|
|
[stream]: stream.md
|