Skip to content

Commit

Permalink
fix: moved setMaxListeners to a utility function
Browse files Browse the repository at this point in the history
[ci skip]
  • Loading branch information
tegefaulkes committed Jun 27, 2024
1 parent 773855d commit 0d7bc46
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
61 changes: 61 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions src/QUICServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import type {
StreamReasonToCode,
} from './types';
import type { Header } from './native/types';
import nodesEvents from 'events';
import Logger from '@matrixai/logger';
import { AbstractEvent, EventAll } from '@matrixai/events';
import {
Expand Down Expand Up @@ -376,7 +375,7 @@ class QUICServer {
this.stopAbortController = new AbortController();
// Since we have a one-to-many relationship with clients and connections,
// we want to up the warning limit on the stopAbortController
nodesEvents.setMaxListeners(100000, this.stopAbortController.signal);
utils.setMaxListeners(this.stopAbortController.signal);
let address: string;
if (!this.isSocketShared) {
address = utils.buildAddress(host, port);
Expand Down
3 changes: 1 addition & 2 deletions src/QUICSocket.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import type QUICServer from './QUICServer';
import type { Host, Hostname, Port, ResolveHostname } from './types';
import type { Header } from './native/types';
import nodesEvents from 'events';
import dgram from 'dgram';
import Logger from '@matrixai/logger';
import { StartStop, ready, running } from '@matrixai/async-init/dist/StartStop';
Expand Down Expand Up @@ -251,7 +250,7 @@ class QUICSocket {
} = {}): Promise<void> {
// Since we have a one-to-many relationship with clients and connections,
// we want to up the warning limit on the EventTarget
nodesEvents.setMaxListeners(100000, this[_eventTarget]);
utils.setMaxListeners(this[_eventTarget]);
let address = utils.buildAddress(host, port);
this.logger.info(`Start ${this.constructor.name} on ${address}`);
// Resolves the host which could be a hostname and acquire the type.
Expand Down
16 changes: 16 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type {
StreamId,
} from './types';
import dns from 'dns';
import events from 'events';
import { IPv4, IPv6, Validator } from 'ip-num';
import QUICConnectionId from './QUICConnectionId';
import * as errors from './errors';
Expand Down Expand Up @@ -554,6 +555,20 @@ function isStreamReset(e: Error): number | false {
}
}

/**
* Increases the total number of registered event handlers before a node warning is emitted.
* In most cases this is not needed but in the case where you have one event emitter for multiple handlers you'll need
* to increase the limit.
* @param target - The specific `EventTarget` or `EventEmitter` to increase the warning for.
* @param limit - The limit before the warning is emitted, defaults to 100000.
*/
function setMaxListeners(
target: EventTarget | NodeJS.EventEmitter,
limit: number = 100000,
) {
events.setMaxListeners(limit, target);
}

export {
textEncoder,
textDecoder,
Expand Down Expand Up @@ -592,4 +607,5 @@ export {
isStreamUnidirectional,
isStreamStopped,
isStreamReset,
setMaxListeners,
};

0 comments on commit 0d7bc46

Please sign in to comment.