Skip to content

Commit

Permalink
fix: ensure onConnect is always called (#3327)
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag authored Jun 25, 2024
1 parent 951826c commit 01400c5
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
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)
}
}

0 comments on commit 01400c5

Please # to comment.