1
1
import * as _ from 'lodash' ;
2
- import { observable , action } from 'mobx' ;
2
+ import { observable , action , computed } from 'mobx' ;
3
3
4
4
import {
5
5
InputRequest ,
6
6
InputResponse ,
7
7
InputWebSocketMessage ,
8
8
InputWebSocketClose ,
9
- InputFailedRequest
9
+ InputFailedRequest ,
10
+ InputRuleEventDataMap
10
11
} from '../../types' ;
11
12
12
13
import { ApiStore } from '../api/api-store' ;
13
14
import { StreamMessage } from '../events/stream-message' ;
14
15
import { HttpExchange } from '../http/http-exchange' ;
15
16
17
+ import { WebSocketOriginalView , WebSocketTransformedView , WebSocketView } from './websocket-views' ;
18
+ import { UpstreamWebSocket } from './upstream-websocket' ;
19
+
16
20
// A websocket stream is an HTTP exchange (the initial setup, or even rejection), but
17
21
// may include a series of many ongoing messages and a final websocket close event,
18
22
// if the initial websocket connection is successful.
19
- export class WebSocketStream extends HttpExchange {
23
+ export class WebSocketStream extends HttpExchange implements WebSocketView {
24
+
20
25
constructor ( request : InputRequest , apiStore : ApiStore ) {
21
26
super ( request , apiStore ) ;
22
27
this . searchIndex += '\nwebsocket' ;
@@ -26,11 +31,43 @@ export class WebSocketStream extends HttpExchange {
26
31
return true ;
27
32
}
28
33
34
+ declare public upstream : UpstreamWebSocket | undefined ;
35
+
36
+ // These are the same as HttpExchangeViewBase, but need to be copied here (because we're not a _view_,
37
+ // we're original, and TS has no proper mixin support).
38
+ @computed
39
+ get original ( ) : WebSocketView {
40
+ if ( ! this . upstream ) return this ;
41
+
42
+ // If the request is original, then upstream data matches original data
43
+ // I.e. only possible transform was after all upstream data
44
+ if ( ! this . upstream . wasRequestTransformed ) {
45
+ return this . upstream ;
46
+ } else {
47
+ return new WebSocketOriginalView ( this . downstream , this . apiStore ) ;
48
+ }
49
+ }
50
+
51
+ @computed
52
+ get transformed ( ) : WebSocketView {
53
+ if ( ! this . upstream ) return this ;
54
+
55
+ // If the response is original, then upstream data matches transformed data
56
+ // I.e. all transforms happened before any upstream data
57
+ if ( ! this . upstream ?. wasResponseTransformed ) {
58
+ return this . upstream ;
59
+ } else {
60
+ return new WebSocketTransformedView ( this . downstream , this . apiStore ) ;
61
+ }
62
+ }
63
+
29
64
@observable
30
65
private accepted = false ;
66
+ get wasAccepted ( ) { return this . accepted ; }
31
67
32
68
@observable
33
69
private subprotocol : string | undefined ;
70
+ get selectedSubprotocol ( ) { return this . subprotocol ; }
34
71
35
72
@action
36
73
setAccepted ( response : InputResponse ) {
@@ -41,14 +78,6 @@ export class WebSocketStream extends HttpExchange {
41
78
Object . assign ( this . timingEvents , response . timingEvents ) ;
42
79
}
43
80
44
- wasAccepted ( ) {
45
- return this . accepted ;
46
- }
47
-
48
- get selectedSubprotocol ( ) {
49
- return this . subprotocol ;
50
- }
51
-
52
81
@observable
53
82
readonly messages : Array < StreamMessage > = [ ] ;
54
83
@@ -73,7 +102,7 @@ export class WebSocketStream extends HttpExchange {
73
102
}
74
103
75
104
markAborted ( request : InputFailedRequest ) {
76
- if ( ! this . wasAccepted ( ) ) {
105
+ if ( ! this . wasAccepted ) {
77
106
// An abort before accept acts exactly as in normal HTTP
78
107
return super . markAborted ( request ) ;
79
108
} else {
@@ -88,6 +117,16 @@ export class WebSocketStream extends HttpExchange {
88
117
}
89
118
}
90
119
120
+ // The assorted normal upstreamFromUpstream... methods will never be called, as the server side
121
+ // is completely different, so UpstreamHttpExchange will never be populated by the base class.
122
+
123
+ updateWithUpstreamConnect ( params : InputRuleEventDataMap [ 'passthrough-websocket-connect' ] ) {
124
+ if ( ! this . upstream ) {
125
+ this . upstream = new UpstreamWebSocket ( this , this . apiStore ) ;
126
+ }
127
+ this . upstream . updateWithRequestHead ( params ) ;
128
+ }
129
+
91
130
cleanup ( ) {
92
131
super . cleanup ( ) ;
93
132
0 commit comments