Skip to content

Commit

Permalink
fix: ensure onConnect is always called
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Jun 14, 2024
1 parent 077531a commit 85a6354
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const { headerNameLowerCasedRecord } = require('./constants')
const invalidPathRegex = /[^\u0021-\u00ff]/

const kHandler = Symbol('handler')
const noop = () => {}

class Request {
constructor (origin, {
Expand Down Expand Up @@ -295,6 +296,14 @@ class Request {
}
this.aborted = true

if (!this.abort) {
// Make sure to always call onConnect even on errors to avoid
// a common user mistake of assuming that onConnect is always
// called before any other hook.
this.abort = noop
this[kHandler].onConnect(noop)
}

return this[kHandler].onError(error)
}

Expand Down
8 changes: 8 additions & 0 deletions lib/handler/decorator-handler.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
'use strict'

const noop = () => {}

module.exports = class DecoratorHandler {
#handler
#onConnectCalled = false

constructor (handler) {
if (typeof handler !== 'object' || handler === null) {
Expand All @@ -11,10 +14,15 @@ module.exports = class DecoratorHandler {
}

onConnect (...args) {
this.#onConnectCalled = true
return this.#handler.onConnect?.(...args)
}

onError (...args) {
if (!this.#onConnectCalled) {
this.#onConnectCalled = true
this.#handler.onConnect?.(noop)
}
return this.#handler.onError?.(...args)
}

Expand Down

0 comments on commit 85a6354

Please sign in to comment.