1
1
'use strict'
2
2
3
+ const assert = require ( 'node:assert' )
4
+ const noop = ( ) => { }
5
+
3
6
module . exports = class DecoratorHandler {
4
7
#handler
8
+ #onConnectCalled = false
9
+ #onCompleteCalled = false
10
+ #onErrorCalled = false
5
11
6
12
constructor ( handler ) {
7
13
if ( typeof handler !== 'object' || handler === null ) {
@@ -11,34 +17,60 @@ module.exports = class DecoratorHandler {
11
17
}
12
18
13
19
onConnect ( ...args ) {
20
+ this . #onConnectCalled = true
14
21
return this . #handler. onConnect ?. ( ...args )
15
22
}
16
23
17
24
onError ( ...args ) {
25
+ if ( ! this . #onConnectCalled) {
26
+ this . #onConnectCalled = true
27
+ this . #handler. onConnect ?. ( noop )
28
+ }
29
+
30
+ this . #onErrorCalled = true
18
31
return this . #handler. onError ?. ( ...args )
19
32
}
20
33
21
34
onUpgrade ( ...args ) {
35
+ assert ( ! this . #onCompleteCalled)
36
+ assert ( ! this . #onErrorCalled)
37
+
22
38
return this . #handler. onUpgrade ?. ( ...args )
23
39
}
24
40
25
41
onResponseStarted ( ...args ) {
42
+ assert ( ! this . #onCompleteCalled)
43
+ assert ( ! this . #onErrorCalled)
44
+
26
45
return this . #handler. onResponseStarted ?. ( ...args )
27
46
}
28
47
29
48
onHeaders ( ...args ) {
49
+ assert ( ! this . #onCompleteCalled)
50
+ assert ( ! this . #onErrorCalled)
51
+
30
52
return this . #handler. onHeaders ?. ( ...args )
31
53
}
32
54
33
55
onData ( ...args ) {
56
+ assert ( ! this . #onCompleteCalled)
57
+ assert ( ! this . #onErrorCalled)
58
+
34
59
return this . #handler. onData ?. ( ...args )
35
60
}
36
61
37
62
onComplete ( ...args ) {
63
+ assert ( ! this . #onCompleteCalled)
64
+ assert ( ! this . #onErrorCalled)
65
+
66
+ this . #onCompleteCalled = true
38
67
return this . #handler. onComplete ?. ( ...args )
39
68
}
40
69
41
70
onBodySent ( ...args ) {
71
+ assert ( ! this . #onCompleteCalled)
72
+ assert ( ! this . #onErrorCalled)
73
+
42
74
return this . #handler. onBodySent ?. ( ...args )
43
75
}
44
76
}
0 commit comments