Skip to content

Commit 01400c5

Browse files
authored
fix: ensure onConnect is always called (#3327)
1 parent 951826c commit 01400c5

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/api/api-pipeline.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -145,15 +145,14 @@ class PipelineHandler extends AsyncResource {
145145
}
146146

147147
onConnect (abort, context) {
148-
const { ret, res } = this
148+
const { res } = this
149149

150150
if (this.reason) {
151151
abort(this.reason)
152152
return
153153
}
154154

155155
assert(!res, 'pipeline cannot be retried')
156-
assert(!ret.destroyed)
157156

158157
this.abort = abort
159158
this.context = context

lib/handler/decorator-handler.js

+32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
'use strict'
22

3+
const assert = require('node:assert')
4+
const noop = () => {}
5+
36
module.exports = class DecoratorHandler {
47
#handler
8+
#onConnectCalled = false
9+
#onCompleteCalled = false
10+
#onErrorCalled = false
511

612
constructor (handler) {
713
if (typeof handler !== 'object' || handler === null) {
@@ -11,34 +17,60 @@ module.exports = class DecoratorHandler {
1117
}
1218

1319
onConnect (...args) {
20+
this.#onConnectCalled = true
1421
return this.#handler.onConnect?.(...args)
1522
}
1623

1724
onError (...args) {
25+
if (!this.#onConnectCalled) {
26+
this.#onConnectCalled = true
27+
this.#handler.onConnect?.(noop)
28+
}
29+
30+
this.#onErrorCalled = true
1831
return this.#handler.onError?.(...args)
1932
}
2033

2134
onUpgrade (...args) {
35+
assert(!this.#onCompleteCalled)
36+
assert(!this.#onErrorCalled)
37+
2238
return this.#handler.onUpgrade?.(...args)
2339
}
2440

2541
onResponseStarted (...args) {
42+
assert(!this.#onCompleteCalled)
43+
assert(!this.#onErrorCalled)
44+
2645
return this.#handler.onResponseStarted?.(...args)
2746
}
2847

2948
onHeaders (...args) {
49+
assert(!this.#onCompleteCalled)
50+
assert(!this.#onErrorCalled)
51+
3052
return this.#handler.onHeaders?.(...args)
3153
}
3254

3355
onData (...args) {
56+
assert(!this.#onCompleteCalled)
57+
assert(!this.#onErrorCalled)
58+
3459
return this.#handler.onData?.(...args)
3560
}
3661

3762
onComplete (...args) {
63+
assert(!this.#onCompleteCalled)
64+
assert(!this.#onErrorCalled)
65+
66+
this.#onCompleteCalled = true
3867
return this.#handler.onComplete?.(...args)
3968
}
4069

4170
onBodySent (...args) {
71+
assert(!this.#onCompleteCalled)
72+
assert(!this.#onErrorCalled)
73+
4274
return this.#handler.onBodySent?.(...args)
4375
}
4476
}

0 commit comments

Comments
 (0)