@types/node 16.4.2 → 16.4.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- node/README.md +2 -2
- node/assert/strict.d.ts +0 -1
- node/assert.d.ts +1352 -40
- node/async_hooks.d.ts +359 -90
- node/buffer.d.ts +1503 -78
- node/child_process.d.ts +1054 -233
- node/cluster.d.ts +320 -100
- node/console.d.ts +305 -32
- node/crypto.d.ts +3115 -739
- node/dgram.d.ts +446 -55
- node/diagnostics_channel.d.ts +85 -12
- node/dns/promises.d.ts +292 -36
- node/dns.d.ts +410 -97
- node/domain.d.ts +154 -10
- node/events.d.ts +377 -31
- node/fs/promises.d.ts +697 -273
- node/fs.d.ts +2257 -858
- node/http.d.ts +888 -80
- node/http2.d.ts +1525 -459
- node/https.d.ts +261 -11
- node/index.d.ts +25 -0
- node/inspector.d.ts +354 -661
- node/module.d.ts +49 -11
- node/net.d.ts +548 -140
- node/os.d.ts +236 -26
- node/package.json +7 -2
- node/path.d.ts +9 -5
- node/perf_hooks.d.ts +288 -90
- node/process.d.ts +1092 -153
- node/punycode.d.ts +65 -26
- node/querystring.d.ts +107 -8
- node/readline.d.ts +425 -79
- node/repl.d.ts +135 -110
- node/stream/promises.d.ts +15 -44
- node/stream.d.ts +927 -225
- node/string_decoder.d.ts +57 -1
- node/timers/promises.d.ts +97 -9
- node/timers.d.ts +29 -10
- node/tls.d.ts +444 -221
- node/trace_events.d.ts +107 -11
- node/tty.d.ts +163 -23
- node/url.d.ts +739 -29
- node/util.d.ts +1361 -73
- node/v8.d.ts +254 -78
- node/vm.d.ts +381 -33
- node/wasi.d.ts +107 -24
- node/worker_threads.d.ts +494 -131
- node/zlib.d.ts +215 -63
node/domain.d.ts
CHANGED
|
@@ -1,25 +1,169 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/**
|
|
2
|
+
* **This module is pending deprecation**. Once a replacement API has been
|
|
3
|
+
* finalized, this module will be fully deprecated. Most developers should**not** have cause to use this module. Users who absolutely must have
|
|
4
|
+
* the functionality that domains provide may rely on it for the time being
|
|
5
|
+
* but should expect to have to migrate to a different solution
|
|
6
|
+
* in the future.
|
|
7
|
+
*
|
|
8
|
+
* Domains provide a way to handle multiple different IO operations as a
|
|
9
|
+
* single group. If any of the event emitters or callbacks registered to a
|
|
10
|
+
* domain emit an `'error'` event, or throw an error, then the domain object
|
|
11
|
+
* will be notified, rather than losing the context of the error in the`process.on('uncaughtException')` handler, or causing the program to
|
|
12
|
+
* exit immediately with an error code.
|
|
13
|
+
* @deprecated Since v1.4.2 - Deprecated
|
|
14
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/domain.js)
|
|
15
|
+
*/
|
|
4
16
|
declare module 'domain' {
|
|
5
17
|
import EventEmitter = require('node:events');
|
|
18
|
+
/**
|
|
19
|
+
* The `Domain` class encapsulates the functionality of routing errors and
|
|
20
|
+
* uncaught exceptions to the active `Domain` object.
|
|
21
|
+
*
|
|
22
|
+
* To handle the errors that it catches, listen to its `'error'` event.
|
|
23
|
+
*/
|
|
6
24
|
class Domain extends EventEmitter {
|
|
25
|
+
/**
|
|
26
|
+
* An array of timers and event emitters that have been explicitly added
|
|
27
|
+
* to the domain.
|
|
28
|
+
*/
|
|
7
29
|
members: Array<EventEmitter | NodeJS.Timer>;
|
|
30
|
+
/**
|
|
31
|
+
* The `enter()` method is plumbing used by the `run()`, `bind()`, and`intercept()` methods to set the active domain. It sets `domain.active` and`process.domain` to the domain, and implicitly
|
|
32
|
+
* pushes the domain onto the domain
|
|
33
|
+
* stack managed by the domain module (see {@link exit} for details on the
|
|
34
|
+
* domain stack). The call to `enter()` delimits the beginning of a chain of
|
|
35
|
+
* asynchronous calls and I/O operations bound to a domain.
|
|
36
|
+
*
|
|
37
|
+
* Calling `enter()` changes only the active domain, and does not alter the domain
|
|
38
|
+
* itself. `enter()` and `exit()` can be called an arbitrary number of times on a
|
|
39
|
+
* single domain.
|
|
40
|
+
*/
|
|
8
41
|
enter(): void;
|
|
42
|
+
/**
|
|
43
|
+
* The `exit()` method exits the current domain, popping it off the domain stack.
|
|
44
|
+
* Any time execution is going to switch to the context of a different chain of
|
|
45
|
+
* asynchronous calls, it's important to ensure that the current domain is exited.
|
|
46
|
+
* The call to `exit()` delimits either the end of or an interruption to the chain
|
|
47
|
+
* of asynchronous calls and I/O operations bound to a domain.
|
|
48
|
+
*
|
|
49
|
+
* If there are multiple, nested domains bound to the current execution context,`exit()` will exit any domains nested within this domain.
|
|
50
|
+
*
|
|
51
|
+
* Calling `exit()` changes only the active domain, and does not alter the domain
|
|
52
|
+
* itself. `enter()` and `exit()` can be called an arbitrary number of times on a
|
|
53
|
+
* single domain.
|
|
54
|
+
*/
|
|
9
55
|
exit(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Run the supplied function in the context of the domain, implicitly
|
|
58
|
+
* binding all event emitters, timers, and lowlevel requests that are
|
|
59
|
+
* created in that context. Optionally, arguments can be passed to
|
|
60
|
+
* the function.
|
|
61
|
+
*
|
|
62
|
+
* This is the most basic way to use a domain.
|
|
63
|
+
*
|
|
64
|
+
* ```js
|
|
65
|
+
* const domain = require('domain');
|
|
66
|
+
* const fs = require('fs');
|
|
67
|
+
* const d = domain.create();
|
|
68
|
+
* d.on('error', (er) => {
|
|
69
|
+
* console.error('Caught error!', er);
|
|
70
|
+
* });
|
|
71
|
+
* d.run(() => {
|
|
72
|
+
* process.nextTick(() => {
|
|
73
|
+
* setTimeout(() => { // Simulating some various async stuff
|
|
74
|
+
* fs.open('non-existent file', 'r', (er, fd) => {
|
|
75
|
+
* if (er) throw er;
|
|
76
|
+
* // proceed...
|
|
77
|
+
* });
|
|
78
|
+
* }, 100);
|
|
79
|
+
* });
|
|
80
|
+
* });
|
|
81
|
+
* ```
|
|
82
|
+
*
|
|
83
|
+
* In this example, the `d.on('error')` handler will be triggered, rather
|
|
84
|
+
* than crashing the program.
|
|
85
|
+
*/
|
|
10
86
|
run<T>(fn: (...args: any[]) => T, ...args: any[]): T;
|
|
87
|
+
/**
|
|
88
|
+
* Explicitly adds an emitter to the domain. If any event handlers called by
|
|
89
|
+
* the emitter throw an error, or if the emitter emits an `'error'` event, it
|
|
90
|
+
* will be routed to the domain's `'error'` event, just like with implicit
|
|
91
|
+
* binding.
|
|
92
|
+
*
|
|
93
|
+
* This also works with timers that are returned from `setInterval()` and `setTimeout()`. If their callback function throws, it will be caught by
|
|
94
|
+
* the domain `'error'` handler.
|
|
95
|
+
*
|
|
96
|
+
* If the Timer or `EventEmitter` was already bound to a domain, it is removed
|
|
97
|
+
* from that one, and bound to this one instead.
|
|
98
|
+
* @param emitter emitter or timer to be added to the domain
|
|
99
|
+
*/
|
|
11
100
|
add(emitter: EventEmitter | NodeJS.Timer): void;
|
|
101
|
+
/**
|
|
102
|
+
* The opposite of {@link add}. Removes domain handling from the
|
|
103
|
+
* specified emitter.
|
|
104
|
+
* @param emitter emitter or timer to be removed from the domain
|
|
105
|
+
*/
|
|
12
106
|
remove(emitter: EventEmitter | NodeJS.Timer): void;
|
|
13
|
-
|
|
14
|
-
|
|
107
|
+
/**
|
|
108
|
+
* The returned function will be a wrapper around the supplied callback
|
|
109
|
+
* function. When the returned function is called, any errors that are
|
|
110
|
+
* thrown will be routed to the domain's `'error'` event.
|
|
111
|
+
*
|
|
112
|
+
* ```js
|
|
113
|
+
* const d = domain.create();
|
|
114
|
+
*
|
|
115
|
+
* function readSomeFile(filename, cb) {
|
|
116
|
+
* fs.readFile(filename, 'utf8', d.bind((er, data) => {
|
|
117
|
+
* // If this throws, it will also be passed to the domain.
|
|
118
|
+
* return cb(er, data ? JSON.parse(data) : null);
|
|
119
|
+
* }));
|
|
120
|
+
* }
|
|
121
|
+
*
|
|
122
|
+
* d.on('error', (er) => {
|
|
123
|
+
* // An error occurred somewhere. If we throw it now, it will crash the program
|
|
124
|
+
* // with the normal line number and stack message.
|
|
125
|
+
* });
|
|
126
|
+
* ```
|
|
127
|
+
* @param callback The callback function
|
|
128
|
+
* @return The bound function
|
|
129
|
+
*/
|
|
130
|
+
bind<T extends Function>(callback: T): T;
|
|
131
|
+
/**
|
|
132
|
+
* This method is almost identical to {@link bind}. However, in
|
|
133
|
+
* addition to catching thrown errors, it will also intercept `Error` objects sent as the first argument to the function.
|
|
134
|
+
*
|
|
135
|
+
* In this way, the common `if (err) return callback(err);` pattern can be replaced
|
|
136
|
+
* with a single error handler in a single place.
|
|
137
|
+
*
|
|
138
|
+
* ```js
|
|
139
|
+
* const d = domain.create();
|
|
140
|
+
*
|
|
141
|
+
* function readSomeFile(filename, cb) {
|
|
142
|
+
* fs.readFile(filename, 'utf8', d.intercept((data) => {
|
|
143
|
+
* // Note, the first argument is never passed to the
|
|
144
|
+
* // callback since it is assumed to be the 'Error' argument
|
|
145
|
+
* // and thus intercepted by the domain.
|
|
146
|
+
*
|
|
147
|
+
* // If this throws, it will also be passed to the domain
|
|
148
|
+
* // so the error-handling logic can be moved to the 'error'
|
|
149
|
+
* // event on the domain instead of being repeated throughout
|
|
150
|
+
* // the program.
|
|
151
|
+
* return cb(null, JSON.parse(data));
|
|
152
|
+
* }));
|
|
153
|
+
* }
|
|
154
|
+
*
|
|
155
|
+
* d.on('error', (er) => {
|
|
156
|
+
* // An error occurred somewhere. If we throw it now, it will crash the program
|
|
157
|
+
* // with the normal line number and stack message.
|
|
158
|
+
* });
|
|
159
|
+
* ```
|
|
160
|
+
* @param callback The callback function
|
|
161
|
+
* @return The intercepted function
|
|
162
|
+
*/
|
|
163
|
+
intercept<T extends Function>(callback: T): T;
|
|
15
164
|
}
|
|
16
|
-
|
|
17
165
|
function create(): Domain;
|
|
18
166
|
}
|
|
19
|
-
|
|
20
|
-
/**
|
|
21
|
-
* @deprecated
|
|
22
|
-
*/
|
|
23
167
|
declare module 'node:domain' {
|
|
24
168
|
export * from 'domain';
|
|
25
169
|
}
|
node/events.d.ts
CHANGED
|
@@ -1,3 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Much of the Node.js core API is built around an idiomatic asynchronous
|
|
3
|
+
* event-driven architecture in which certain kinds of objects (called "emitters")
|
|
4
|
+
* emit named events that cause `Function` objects ("listeners") to be called.
|
|
5
|
+
*
|
|
6
|
+
* For instance: a `net.Server` object emits an event each time a peer
|
|
7
|
+
* connects to it; a `fs.ReadStream` emits an event when the file is opened;
|
|
8
|
+
* a `stream` emits an event whenever data is available to be read.
|
|
9
|
+
*
|
|
10
|
+
* All objects that emit events are instances of the `EventEmitter` class. These
|
|
11
|
+
* objects expose an `eventEmitter.on()` function that allows one or more
|
|
12
|
+
* functions to be attached to named events emitted by the object. Typically,
|
|
13
|
+
* event names are camel-cased strings but any valid JavaScript property key
|
|
14
|
+
* can be used.
|
|
15
|
+
*
|
|
16
|
+
* When the `EventEmitter` object emits an event, all of the functions attached
|
|
17
|
+
* to that specific event are called _synchronously_. Any values returned by the
|
|
18
|
+
* called listeners are _ignored_ and discarded.
|
|
19
|
+
*
|
|
20
|
+
* The following example shows a simple `EventEmitter` instance with a single
|
|
21
|
+
* listener. The `eventEmitter.on()` method is used to register listeners, while
|
|
22
|
+
* the `eventEmitter.emit()` method is used to trigger the event.
|
|
23
|
+
*
|
|
24
|
+
* ```js
|
|
25
|
+
* const EventEmitter = require('events');
|
|
26
|
+
*
|
|
27
|
+
* class MyEmitter extends EventEmitter {}
|
|
28
|
+
*
|
|
29
|
+
* const myEmitter = new MyEmitter();
|
|
30
|
+
* myEmitter.on('event', () => {
|
|
31
|
+
* console.log('an event occurred!');
|
|
32
|
+
* });
|
|
33
|
+
* myEmitter.emit('event');
|
|
34
|
+
* ```
|
|
35
|
+
* @see [source](https://github.com/nodejs/node/blob/v16.4.2/lib/events.js)
|
|
36
|
+
*/
|
|
1
37
|
declare module 'events' {
|
|
2
38
|
interface EventEmitterOptions {
|
|
3
39
|
/**
|
|
@@ -5,34 +41,46 @@ declare module 'events' {
|
|
|
5
41
|
*/
|
|
6
42
|
captureRejections?: boolean | undefined;
|
|
7
43
|
}
|
|
8
|
-
|
|
9
44
|
interface NodeEventTarget {
|
|
10
|
-
once(
|
|
45
|
+
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
11
46
|
}
|
|
12
|
-
|
|
13
47
|
interface DOMEventTarget {
|
|
14
|
-
addEventListener(
|
|
48
|
+
addEventListener(
|
|
49
|
+
eventName: string,
|
|
50
|
+
listener: (...args: any[]) => void,
|
|
51
|
+
opts?: {
|
|
52
|
+
once: boolean;
|
|
53
|
+
}
|
|
54
|
+
): any;
|
|
15
55
|
}
|
|
16
|
-
|
|
17
56
|
interface StaticEventEmitterOptions {
|
|
18
57
|
signal?: AbortSignal | undefined;
|
|
19
58
|
}
|
|
20
|
-
|
|
21
59
|
interface EventEmitter extends NodeJS.EventEmitter {}
|
|
60
|
+
/**
|
|
61
|
+
* The `EventEmitter` class is defined and exposed by the `events` module:
|
|
62
|
+
*
|
|
63
|
+
* ```js
|
|
64
|
+
* const EventEmitter = require('events');
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* All `EventEmitter`s emit the event `'newListener'` when new listeners are
|
|
68
|
+
* added and `'removeListener'` when existing listeners are removed.
|
|
69
|
+
*
|
|
70
|
+
* It supports the following option:
|
|
71
|
+
* @since v0.1.26
|
|
72
|
+
*/
|
|
22
73
|
class EventEmitter {
|
|
23
74
|
constructor(options?: EventEmitterOptions);
|
|
24
|
-
|
|
25
|
-
static once(emitter:
|
|
26
|
-
static
|
|
27
|
-
static on(emitter: NodeJS.EventEmitter, event: string, options?: StaticEventEmitterOptions): AsyncIterableIterator<any>;
|
|
28
|
-
|
|
75
|
+
static once(emitter: NodeEventTarget, eventName: string | symbol, options?: StaticEventEmitterOptions): Promise<any[]>;
|
|
76
|
+
static once(emitter: DOMEventTarget, eventName: string, options?: StaticEventEmitterOptions): Promise<any[]>;
|
|
77
|
+
static on(emitter: NodeJS.EventEmitter, eventName: string, options?: StaticEventEmitterOptions): AsyncIterableIterator<any>;
|
|
29
78
|
/** @deprecated since v4.0.0 */
|
|
30
|
-
static listenerCount(emitter: NodeJS.EventEmitter,
|
|
79
|
+
static listenerCount(emitter: NodeJS.EventEmitter, eventName: string | symbol): number;
|
|
31
80
|
/**
|
|
32
81
|
* Returns a list listener for a specific emitter event name.
|
|
33
82
|
*/
|
|
34
83
|
static getEventListener(emitter: DOMEventTarget | NodeJS.EventEmitter, name: string | symbol): Function[];
|
|
35
|
-
|
|
36
84
|
/**
|
|
37
85
|
* This symbol shall be used to install a listener for only monitoring `'error'`
|
|
38
86
|
* events. Listeners installed using this symbol are called before the regular
|
|
@@ -44,7 +92,6 @@ declare module 'events' {
|
|
|
44
92
|
*/
|
|
45
93
|
static readonly errorMonitor: unique symbol;
|
|
46
94
|
static readonly captureRejectionSymbol: unique symbol;
|
|
47
|
-
|
|
48
95
|
/**
|
|
49
96
|
* Sets or gets the default captureRejection value for all emitters.
|
|
50
97
|
*/
|
|
@@ -52,12 +99,10 @@ declare module 'events' {
|
|
|
52
99
|
static captureRejections: boolean;
|
|
53
100
|
static defaultMaxListeners: number;
|
|
54
101
|
}
|
|
55
|
-
|
|
56
102
|
import internal = require('node:events');
|
|
57
103
|
namespace EventEmitter {
|
|
58
104
|
// Should just be `export { EventEmitter }`, but that doesn't work in TypeScript 3.4
|
|
59
105
|
export { internal as EventEmitter };
|
|
60
|
-
|
|
61
106
|
export interface Abortable {
|
|
62
107
|
/**
|
|
63
108
|
* When provided the corresponding `AbortController` can be used to cancel an asynchronous action.
|
|
@@ -65,33 +110,334 @@ declare module 'events' {
|
|
|
65
110
|
signal?: AbortSignal | undefined;
|
|
66
111
|
}
|
|
67
112
|
}
|
|
68
|
-
|
|
69
113
|
global {
|
|
70
114
|
namespace NodeJS {
|
|
71
115
|
interface EventEmitter {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
116
|
+
/**
|
|
117
|
+
* Alias for `emitter.on(eventName, listener)`.
|
|
118
|
+
* @since v0.1.26
|
|
119
|
+
*/
|
|
120
|
+
addListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
121
|
+
/**
|
|
122
|
+
* Adds the `listener` function to the end of the listeners array for the
|
|
123
|
+
* event named `eventName`. No checks are made to see if the `listener` has
|
|
124
|
+
* already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
|
|
125
|
+
* times.
|
|
126
|
+
*
|
|
127
|
+
* ```js
|
|
128
|
+
* server.on('connection', (stream) => {
|
|
129
|
+
* console.log('someone connected!');
|
|
130
|
+
* });
|
|
131
|
+
* ```
|
|
132
|
+
*
|
|
133
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
134
|
+
*
|
|
135
|
+
* 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
|
|
136
|
+
* event listener to the beginning of the listeners array.
|
|
137
|
+
*
|
|
138
|
+
* ```js
|
|
139
|
+
* const myEE = new EventEmitter();
|
|
140
|
+
* myEE.on('foo', () => console.log('a'));
|
|
141
|
+
* myEE.prependListener('foo', () => console.log('b'));
|
|
142
|
+
* myEE.emit('foo');
|
|
143
|
+
* // Prints:
|
|
144
|
+
* // b
|
|
145
|
+
* // a
|
|
146
|
+
* ```
|
|
147
|
+
* @since v0.1.101
|
|
148
|
+
* @param eventName The name of the event.
|
|
149
|
+
* @param listener The callback function
|
|
150
|
+
*/
|
|
151
|
+
on(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
152
|
+
/**
|
|
153
|
+
* Adds a **one-time**`listener` function for the event named `eventName`. The
|
|
154
|
+
* next time `eventName` is triggered, this listener is removed and then invoked.
|
|
155
|
+
*
|
|
156
|
+
* ```js
|
|
157
|
+
* server.once('connection', (stream) => {
|
|
158
|
+
* console.log('Ah, we have our first user!');
|
|
159
|
+
* });
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
163
|
+
*
|
|
164
|
+
* 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
|
|
165
|
+
* event listener to the beginning of the listeners array.
|
|
166
|
+
*
|
|
167
|
+
* ```js
|
|
168
|
+
* const myEE = new EventEmitter();
|
|
169
|
+
* myEE.once('foo', () => console.log('a'));
|
|
170
|
+
* myEE.prependOnceListener('foo', () => console.log('b'));
|
|
171
|
+
* myEE.emit('foo');
|
|
172
|
+
* // Prints:
|
|
173
|
+
* // b
|
|
174
|
+
* // a
|
|
175
|
+
* ```
|
|
176
|
+
* @since v0.3.0
|
|
177
|
+
* @param eventName The name of the event.
|
|
178
|
+
* @param listener The callback function
|
|
179
|
+
*/
|
|
180
|
+
once(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
181
|
+
/**
|
|
182
|
+
* Removes the specified `listener` from the listener array for the event named`eventName`.
|
|
183
|
+
*
|
|
184
|
+
* ```js
|
|
185
|
+
* const callback = (stream) => {
|
|
186
|
+
* console.log('someone connected!');
|
|
187
|
+
* };
|
|
188
|
+
* server.on('connection', callback);
|
|
189
|
+
* // ...
|
|
190
|
+
* server.removeListener('connection', callback);
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* `removeListener()` will remove, at most, one instance of a listener from the
|
|
194
|
+
* listener array. If any single listener has been added multiple times to the
|
|
195
|
+
* listener array for the specified `eventName`, then `removeListener()` must be
|
|
196
|
+
* called multiple times to remove each instance.
|
|
197
|
+
*
|
|
198
|
+
* Once an event is emitted, all listeners attached to it at the
|
|
199
|
+
* time of emitting are called in order. This implies that any`removeListener()` or `removeAllListeners()` calls _after_ emitting and_before_ the last listener finishes execution will
|
|
200
|
+
* not remove them from`emit()` in progress. Subsequent events behave as expected.
|
|
201
|
+
*
|
|
202
|
+
* ```js
|
|
203
|
+
* const myEmitter = new MyEmitter();
|
|
204
|
+
*
|
|
205
|
+
* const callbackA = () => {
|
|
206
|
+
* console.log('A');
|
|
207
|
+
* myEmitter.removeListener('event', callbackB);
|
|
208
|
+
* };
|
|
209
|
+
*
|
|
210
|
+
* const callbackB = () => {
|
|
211
|
+
* console.log('B');
|
|
212
|
+
* };
|
|
213
|
+
*
|
|
214
|
+
* myEmitter.on('event', callbackA);
|
|
215
|
+
*
|
|
216
|
+
* myEmitter.on('event', callbackB);
|
|
217
|
+
*
|
|
218
|
+
* // callbackA removes listener callbackB but it will still be called.
|
|
219
|
+
* // Internal listener array at time of emit [callbackA, callbackB]
|
|
220
|
+
* myEmitter.emit('event');
|
|
221
|
+
* // Prints:
|
|
222
|
+
* // A
|
|
223
|
+
* // B
|
|
224
|
+
*
|
|
225
|
+
* // callbackB is now removed.
|
|
226
|
+
* // Internal listener array [callbackA]
|
|
227
|
+
* myEmitter.emit('event');
|
|
228
|
+
* // Prints:
|
|
229
|
+
* // A
|
|
230
|
+
* ```
|
|
231
|
+
*
|
|
232
|
+
* Because listeners are managed using an internal array, calling this will
|
|
233
|
+
* change the position indices of any listener registered _after_ the listener
|
|
234
|
+
* being removed. This will not impact the order in which listeners are called,
|
|
235
|
+
* but it means that any copies of the listener array as returned by
|
|
236
|
+
* the `emitter.listeners()` method will need to be recreated.
|
|
237
|
+
*
|
|
238
|
+
* When a single function has been added as a handler multiple times for a single
|
|
239
|
+
* event (as in the example below), `removeListener()` will remove the most
|
|
240
|
+
* recently added instance. In the example the `once('ping')`listener is removed:
|
|
241
|
+
*
|
|
242
|
+
* ```js
|
|
243
|
+
* const ee = new EventEmitter();
|
|
244
|
+
*
|
|
245
|
+
* function pong() {
|
|
246
|
+
* console.log('pong');
|
|
247
|
+
* }
|
|
248
|
+
*
|
|
249
|
+
* ee.on('ping', pong);
|
|
250
|
+
* ee.once('ping', pong);
|
|
251
|
+
* ee.removeListener('ping', pong);
|
|
252
|
+
*
|
|
253
|
+
* ee.emit('ping');
|
|
254
|
+
* ee.emit('ping');
|
|
255
|
+
* ```
|
|
256
|
+
*
|
|
257
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
258
|
+
* @since v0.1.26
|
|
259
|
+
*/
|
|
260
|
+
removeListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
261
|
+
/**
|
|
262
|
+
* Alias for `emitter.removeListener()`.
|
|
263
|
+
* @since v10.0.0
|
|
264
|
+
*/
|
|
265
|
+
off(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
266
|
+
/**
|
|
267
|
+
* Removes all listeners, or those of the specified `eventName`.
|
|
268
|
+
*
|
|
269
|
+
* It is bad practice to remove listeners added elsewhere in the code,
|
|
270
|
+
* particularly when the `EventEmitter` instance was created by some other
|
|
271
|
+
* component or module (e.g. sockets or file streams).
|
|
272
|
+
*
|
|
273
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
274
|
+
* @since v0.1.26
|
|
275
|
+
*/
|
|
77
276
|
removeAllListeners(event?: string | symbol): this;
|
|
277
|
+
/**
|
|
278
|
+
* By default `EventEmitter`s will print a warning if more than `10` listeners are
|
|
279
|
+
* added for a particular event. This is a useful default that helps finding
|
|
280
|
+
* memory leaks. The `emitter.setMaxListeners()` method allows the limit to be
|
|
281
|
+
* modified for this specific `EventEmitter` instance. The value can be set to`Infinity` (or `0`) to indicate an unlimited number of listeners.
|
|
282
|
+
*
|
|
283
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
284
|
+
* @since v0.3.5
|
|
285
|
+
*/
|
|
78
286
|
setMaxListeners(n: number): this;
|
|
287
|
+
/**
|
|
288
|
+
* Returns the current max listener value for the `EventEmitter` which is either
|
|
289
|
+
* set by `emitter.setMaxListeners(n)` or defaults to {@link defaultMaxListeners}.
|
|
290
|
+
* @since v1.0.0
|
|
291
|
+
*/
|
|
79
292
|
getMaxListeners(): number;
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
293
|
+
/**
|
|
294
|
+
* Returns a copy of the array of listeners for the event named `eventName`.
|
|
295
|
+
*
|
|
296
|
+
* ```js
|
|
297
|
+
* server.on('connection', (stream) => {
|
|
298
|
+
* console.log('someone connected!');
|
|
299
|
+
* });
|
|
300
|
+
* console.log(util.inspect(server.listeners('connection')));
|
|
301
|
+
* // Prints: [ [Function] ]
|
|
302
|
+
* ```
|
|
303
|
+
* @since v0.1.26
|
|
304
|
+
*/
|
|
305
|
+
listeners(eventName: string | symbol): Function[];
|
|
306
|
+
/**
|
|
307
|
+
* Returns a copy of the array of listeners for the event named `eventName`,
|
|
308
|
+
* including any wrappers (such as those created by `.once()`).
|
|
309
|
+
*
|
|
310
|
+
* ```js
|
|
311
|
+
* const emitter = new EventEmitter();
|
|
312
|
+
* emitter.once('log', () => console.log('log once'));
|
|
313
|
+
*
|
|
314
|
+
* // Returns a new Array with a function `onceWrapper` which has a property
|
|
315
|
+
* // `listener` which contains the original listener bound above
|
|
316
|
+
* const listeners = emitter.rawListeners('log');
|
|
317
|
+
* const logFnWrapper = listeners[0];
|
|
318
|
+
*
|
|
319
|
+
* // Logs "log once" to the console and does not unbind the `once` event
|
|
320
|
+
* logFnWrapper.listener();
|
|
321
|
+
*
|
|
322
|
+
* // Logs "log once" to the console and removes the listener
|
|
323
|
+
* logFnWrapper();
|
|
324
|
+
*
|
|
325
|
+
* emitter.on('log', () => console.log('log persistently'));
|
|
326
|
+
* // Will return a new Array with a single function bound by `.on()` above
|
|
327
|
+
* const newListeners = emitter.rawListeners('log');
|
|
328
|
+
*
|
|
329
|
+
* // Logs "log persistently" twice
|
|
330
|
+
* newListeners[0]();
|
|
331
|
+
* emitter.emit('log');
|
|
332
|
+
* ```
|
|
333
|
+
* @since v9.4.0
|
|
334
|
+
*/
|
|
335
|
+
rawListeners(eventName: string | symbol): Function[];
|
|
336
|
+
/**
|
|
337
|
+
* Synchronously calls each of the listeners registered for the event named`eventName`, in the order they were registered, passing the supplied arguments
|
|
338
|
+
* to each.
|
|
339
|
+
*
|
|
340
|
+
* Returns `true` if the event had listeners, `false` otherwise.
|
|
341
|
+
*
|
|
342
|
+
* ```js
|
|
343
|
+
* const EventEmitter = require('events');
|
|
344
|
+
* const myEmitter = new EventEmitter();
|
|
345
|
+
*
|
|
346
|
+
* // First listener
|
|
347
|
+
* myEmitter.on('event', function firstListener() {
|
|
348
|
+
* console.log('Helloooo! first listener');
|
|
349
|
+
* });
|
|
350
|
+
* // Second listener
|
|
351
|
+
* myEmitter.on('event', function secondListener(arg1, arg2) {
|
|
352
|
+
* console.log(`event with parameters ${arg1}, ${arg2} in second listener`);
|
|
353
|
+
* });
|
|
354
|
+
* // Third listener
|
|
355
|
+
* myEmitter.on('event', function thirdListener(...args) {
|
|
356
|
+
* const parameters = args.join(', ');
|
|
357
|
+
* console.log(`event with parameters ${parameters} in third listener`);
|
|
358
|
+
* });
|
|
359
|
+
*
|
|
360
|
+
* console.log(myEmitter.listeners('event'));
|
|
361
|
+
*
|
|
362
|
+
* myEmitter.emit('event', 1, 2, 3, 4, 5);
|
|
363
|
+
*
|
|
364
|
+
* // Prints:
|
|
365
|
+
* // [
|
|
366
|
+
* // [Function: firstListener],
|
|
367
|
+
* // [Function: secondListener],
|
|
368
|
+
* // [Function: thirdListener]
|
|
369
|
+
* // ]
|
|
370
|
+
* // Helloooo! first listener
|
|
371
|
+
* // event with parameters 1, 2 in second listener
|
|
372
|
+
* // event with parameters 1, 2, 3, 4, 5 in third listener
|
|
373
|
+
* ```
|
|
374
|
+
* @since v0.1.26
|
|
375
|
+
*/
|
|
376
|
+
emit(eventName: string | symbol, ...args: any[]): boolean;
|
|
377
|
+
/**
|
|
378
|
+
* Returns the number of listeners listening to the event named `eventName`.
|
|
379
|
+
* @since v3.2.0
|
|
380
|
+
* @param eventName The name of the event being listened for
|
|
381
|
+
*/
|
|
382
|
+
listenerCount(eventName: string | symbol): number;
|
|
383
|
+
/**
|
|
384
|
+
* Adds the `listener` function to the _beginning_ of the listeners array for the
|
|
385
|
+
* event named `eventName`. No checks are made to see if the `listener` has
|
|
386
|
+
* already been added. Multiple calls passing the same combination of `eventName`and `listener` will result in the `listener` being added, and called, multiple
|
|
387
|
+
* times.
|
|
388
|
+
*
|
|
389
|
+
* ```js
|
|
390
|
+
* server.prependListener('connection', (stream) => {
|
|
391
|
+
* console.log('someone connected!');
|
|
392
|
+
* });
|
|
393
|
+
* ```
|
|
394
|
+
*
|
|
395
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
396
|
+
* @since v6.0.0
|
|
397
|
+
* @param eventName The name of the event.
|
|
398
|
+
* @param listener The callback function
|
|
399
|
+
*/
|
|
400
|
+
prependListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
401
|
+
/**
|
|
402
|
+
* Adds a **one-time**`listener` function for the event named `eventName` to the_beginning_ of the listeners array. The next time `eventName` is triggered, this
|
|
403
|
+
* listener is removed, and then invoked.
|
|
404
|
+
*
|
|
405
|
+
* ```js
|
|
406
|
+
* server.prependOnceListener('connection', (stream) => {
|
|
407
|
+
* console.log('Ah, we have our first user!');
|
|
408
|
+
* });
|
|
409
|
+
* ```
|
|
410
|
+
*
|
|
411
|
+
* Returns a reference to the `EventEmitter`, so that calls can be chained.
|
|
412
|
+
* @since v6.0.0
|
|
413
|
+
* @param eventName The name of the event.
|
|
414
|
+
* @param listener The callback function
|
|
415
|
+
*/
|
|
416
|
+
prependOnceListener(eventName: string | symbol, listener: (...args: any[]) => void): this;
|
|
417
|
+
/**
|
|
418
|
+
* Returns an array listing the events for which the emitter has registered
|
|
419
|
+
* listeners. The values in the array are strings or `Symbol`s.
|
|
420
|
+
*
|
|
421
|
+
* ```js
|
|
422
|
+
* const EventEmitter = require('events');
|
|
423
|
+
* const myEE = new EventEmitter();
|
|
424
|
+
* myEE.on('foo', () => {});
|
|
425
|
+
* myEE.on('bar', () => {});
|
|
426
|
+
*
|
|
427
|
+
* const sym = Symbol('symbol');
|
|
428
|
+
* myEE.on(sym, () => {});
|
|
429
|
+
*
|
|
430
|
+
* console.log(myEE.eventNames());
|
|
431
|
+
* // Prints: [ 'foo', 'bar', Symbol(symbol) ]
|
|
432
|
+
* ```
|
|
433
|
+
* @since v6.0.0
|
|
434
|
+
*/
|
|
87
435
|
eventNames(): Array<string | symbol>;
|
|
88
436
|
}
|
|
89
437
|
}
|
|
90
438
|
}
|
|
91
|
-
|
|
92
439
|
export = EventEmitter;
|
|
93
440
|
}
|
|
94
|
-
|
|
95
441
|
declare module 'node:events' {
|
|
96
442
|
import events = require('events');
|
|
97
443
|
export = events;
|