Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: increase default value of max event listeners #81

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/connection/BaseConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface ConnectionOptions {
agent?: HttpAgentOptions | UndiciAgentOptions | agentFn | boolean
proxy?: string | URL
caFingerprint?: string
maxEventListeners?: number
}

export interface ConnectionRequestParams {
Expand Down Expand Up @@ -92,6 +93,7 @@ export default class BaseConnection {
resurrectTimeout: number
_openRequests: number
weight: number
maxEventListeners: number
[kStatus]: string
[kCaFingerprint]: string | null
[kDiagnostic]: Diagnostic
Expand All @@ -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}'`)
}
Expand Down
1 change: 1 addition & 0 deletions src/connection/UndiciConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 20 additions & 0 deletions test/unit/undici-connection.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down