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: ensure onConnect is always called #3327

Merged
merged 5 commits into from
Jun 25, 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
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
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