Skip to content
New issue

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

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

Already on GitHub? # to your account

fix: ensure onConnect is always called #3327

Merged
merged 5 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 1 addition & 2 deletions lib/api/api-pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,14 @@ class PipelineHandler extends AsyncResource {
}

onConnect (abort, context) {
const { ret, res } = this
const { res } = this

if (this.reason) {
abort(this.reason)
return
}

assert(!res, 'pipeline cannot be retried')
assert(!ret.destroyed)

this.abort = abort
this.context = context
Expand Down
8 changes: 8 additions & 0 deletions lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const { headerNameLowerCasedRecord } = require('./constants')
const invalidPathRegex = /[^\u0021-\u00ff]/

const kHandler = Symbol('handler')
const noop = () => {}
ronag marked this conversation as resolved.
Show resolved Hide resolved

class Request {
constructor (origin, {
Expand Down Expand Up @@ -296,6 +297,13 @@ 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[kHandler].onConnect(noop)
}
ronag marked this conversation as resolved.
Show resolved Hide resolved
ronag marked this conversation as resolved.
Show resolved Hide resolved

ronag marked this conversation as resolved.
Show resolved Hide resolved
return this[kHandler].onError(error)
}

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

const assert = require('node:assert')
const noop = () => {}

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

constructor (handler) {
if (typeof handler !== 'object' || handler === null) {
Expand All @@ -11,34 +17,60 @@ 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)
}

this.#onErrorCalled = true
return this.#handler.onError?.(...args)
}

onUpgrade (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onUpgrade?.(...args)
}

onResponseStarted (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onResponseStarted?.(...args)
}

onHeaders (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onHeaders?.(...args)
}

onData (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onData?.(...args)
}

onComplete (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

this.#onCompleteCalled = true
return this.#handler.onComplete?.(...args)
}

onBodySent (...args) {
assert(!this.#onCompleteCalled)
assert(!this.#onErrorCalled)

return this.#handler.onBodySent?.(...args)
}
}
Loading