diff --git a/src/connection/BaseConnection.ts b/src/connection/BaseConnection.ts index 9dc197d..dcdeefb 100644 --- a/src/connection/BaseConnection.ts +++ b/src/connection/BaseConnection.ts @@ -46,6 +46,7 @@ export interface ConnectionOptions { agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | boolean proxy?: string | URL caFingerprint?: string + maxEventListeners?: number } export interface ConnectionRequestParams { @@ -92,6 +93,7 @@ export default class BaseConnection { resurrectTimeout: number _openRequests: number weight: number + maxEventListeners: number [kStatus]: string [kCaFingerprint]: string | null [kDiagnostic]: Diagnostic @@ -111,10 +113,10 @@ export default class BaseConnection { this.resurrectTimeout = 0 this.weight = 0 this._openRequests = 0 + this.maxEventListeners = opts.maxEventListeners ?? 100 this[kStatus] = opts.status ?? BaseConnection.statuses.ALIVE this[kDiagnostic] = opts.diagnostic ?? new Diagnostic() this[kCaFingerprint] = opts.caFingerprint ?? null - if (!['http:', 'https:'].includes(this.url.protocol)) { throw new ConfigurationError(`Invalid protocol: '${this.url.protocol}'`) } diff --git a/src/connection/UndiciConnection.ts b/src/connection/UndiciConnection.ts index d085f0d..af1a825 100644 --- a/src/connection/UndiciConnection.ts +++ b/src/connection/UndiciConnection.ts @@ -68,6 +68,7 @@ export default class Connection extends BaseConnection { } this[kEmitter] = new EventEmitter() + this[kEmitter].setMaxListeners(this.maxEventListeners) const undiciOptions: Pool.Options = { keepAliveTimeout: 600e3, keepAliveMaxTimeout: 600e3, diff --git a/test/unit/undici-connection.test.ts b/test/unit/undici-connection.test.ts index bc5b4bb..e4e1c1e 100644 --- a/test/unit/undici-connection.test.ts +++ b/test/unit/undici-connection.test.ts @@ -1059,6 +1059,26 @@ test('Path without intial slash', async t => { server.stop() }) +test('Should increase number of max event listeners', async t => { + t.plan(1) + + function handler (req: http.IncomingMessage, res: http.ServerResponse) { + res.end('ok') + } + + const [{ port }, server] = await buildServer(handler, { secure: true }) + const connection = new UndiciConnection({ + url: new URL(`https://localhost:${port}`), + maxEventListeners: 100, + }) + const res = await connection.request({ + path: '/hello', + method: 'GET' + }, options) + t.equal(res.body, 'ok') + server.stop() +}) + test('as stream', async t => { t.plan(2)