Skip to content

Commit 0d7bc46

Browse files
committed
fix: moved setMaxListeners to a utility function
[ci skip]
1 parent 773855d commit 0d7bc46

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

package-lock.json

Lines changed: 61 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/QUICServer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type {
1010
StreamReasonToCode,
1111
} from './types';
1212
import type { Header } from './native/types';
13-
import nodesEvents from 'events';
1413
import Logger from '@matrixai/logger';
1514
import { AbstractEvent, EventAll } from '@matrixai/events';
1615
import {
@@ -376,7 +375,7 @@ class QUICServer {
376375
this.stopAbortController = new AbortController();
377376
// Since we have a one-to-many relationship with clients and connections,
378377
// we want to up the warning limit on the stopAbortController
379-
nodesEvents.setMaxListeners(100000, this.stopAbortController.signal);
378+
utils.setMaxListeners(this.stopAbortController.signal);
380379
let address: string;
381380
if (!this.isSocketShared) {
382381
address = utils.buildAddress(host, port);

src/QUICSocket.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type QUICServer from './QUICServer';
22
import type { Host, Hostname, Port, ResolveHostname } from './types';
33
import type { Header } from './native/types';
4-
import nodesEvents from 'events';
54
import dgram from 'dgram';
65
import Logger from '@matrixai/logger';
76
import { StartStop, ready, running } from '@matrixai/async-init/dist/StartStop';
@@ -251,7 +250,7 @@ class QUICSocket {
251250
} = {}): Promise<void> {
252251
// Since we have a one-to-many relationship with clients and connections,
253252
// we want to up the warning limit on the EventTarget
254-
nodesEvents.setMaxListeners(100000, this[_eventTarget]);
253+
utils.setMaxListeners(this[_eventTarget]);
255254
let address = utils.buildAddress(host, port);
256255
this.logger.info(`Start ${this.constructor.name} on ${address}`);
257256
// Resolves the host which could be a hostname and acquire the type.

src/utils.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import type {
1010
StreamId,
1111
} from './types';
1212
import dns from 'dns';
13+
import events from 'events';
1314
import { IPv4, IPv6, Validator } from 'ip-num';
1415
import QUICConnectionId from './QUICConnectionId';
1516
import * as errors from './errors';
@@ -554,6 +555,20 @@ function isStreamReset(e: Error): number | false {
554555
}
555556
}
556557

558+
/**
559+
* Increases the total number of registered event handlers before a node warning is emitted.
560+
* In most cases this is not needed but in the case where you have one event emitter for multiple handlers you'll need
561+
* to increase the limit.
562+
* @param target - The specific `EventTarget` or `EventEmitter` to increase the warning for.
563+
* @param limit - The limit before the warning is emitted, defaults to 100000.
564+
*/
565+
function setMaxListeners(
566+
target: EventTarget | NodeJS.EventEmitter,
567+
limit: number = 100000,
568+
) {
569+
events.setMaxListeners(limit, target);
570+
}
571+
557572
export {
558573
textEncoder,
559574
textDecoder,
@@ -592,4 +607,5 @@ export {
592607
isStreamUnidirectional,
593608
isStreamStopped,
594609
isStreamReset,
610+
setMaxListeners,
595611
};

0 commit comments

Comments
 (0)