-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathReactiveSocketTypes.js
More file actions
352 lines (327 loc) · 8.84 KB
/
ReactiveSocketTypes.js
File metadata and controls
352 lines (327 loc) · 8.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/** Copyright (c) Facebook, Inc. and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @flow
*/
import type {Flowable, Single} from 'rsocket-flowable';
export interface Responder<D, M> {
/**
* Fire and Forget interaction model of `ReactiveSocket`. The returned
* Publisher resolves when the passed `payload` is successfully handled.
*/
fireAndForget(payload: Payload<D, M>): void,
/**
* Request-Response interaction model of `ReactiveSocket`. The returned
* Publisher resolves with the response.
*/
requestResponse(payload: Payload<D, M>): Single<Payload<D, M>>,
/**
* Request-Stream interaction model of `ReactiveSocket`. The returned
* Publisher returns values representing the response(s).
*/
requestStream(payload: Payload<D, M>): Flowable<Payload<D, M>>,
/**
* Request-Channel interaction model of `ReactiveSocket`. The returned
* Publisher returns values representing the response(s).
*/
requestChannel(payloads: Flowable<Payload<D, M>>): Flowable<Payload<D, M>>,
/**
* Metadata-Push interaction model of `ReactiveSocket`. The returned Publisher
* resolves when the passed `payload` is successfully handled.
*/
metadataPush(payload: Payload<D, M>): Single<void>,
}
export interface PartialResponder<D, M> {
+fireAndForget?: (payload: Payload<D, M>) => void,
+requestResponse?: (payload: Payload<D, M>) => Single<Payload<D, M>>,
+requestStream?: (payload: Payload<D, M>) => Flowable<Payload<D, M>>,
+requestChannel?: (
payloads: Flowable<Payload<D, M>>,
) => Flowable<Payload<D, M>>,
+metadataPush?: (payload: Payload<D, M>) => Single<void>,
}
/**
* A contract providing different interaction models per the [ReactiveSocket protocol]
(https://github.com/ReactiveSocket/reactivesocket/blob/master/Protocol.md).
*/
export interface ReactiveSocket<D, M> extends Responder<D, M> {
/**
* Close this `ReactiveSocket` and the underlying transport connection.
*/
close(): void,
/**
* Returns a Flowable that immediately publishes the current connection
* status and thereafter updates as it changes. Once a connection is in
* the CLOSED or ERROR state, it may not be connected again.
* Implementations must publish values per the comments on ConnectionStatus.
*/
connectionStatus(): Flowable<ConnectionStatus>,
/**
* Returns positive number representing the availability of RSocket requester. Higher is better, 0.0
* means not available.
*/
availability(): number,
}
/**
* Represents a network connection with input/output used by a ReactiveSocket to
* send/receive data.
*/
export interface DuplexConnection {
/**
* Send a single frame on the connection.
*/
sendOne(frame: Frame): void,
/**
* Send all the `input` frames on this connection.
*
* Notes:
* - Implementations must not cancel the subscription.
* - Implementations must signal any errors by calling `onError` on the
* `receive()` Publisher.
*/
send(input: Flowable<Frame>): void,
/**
* Returns a stream of all `Frame`s received on this connection.
*
* Notes:
* - Implementations must call `onComplete` if the underlying connection is
* closed by the peer or by calling `close()`.
* - Implementations must call `onError` if there are any errors
* sending/receiving frames.
* - Implemenations may optionally support multi-cast receivers. Those that do
* not should throw if `receive` is called more than once.
*/
receive(): Flowable<Frame>,
/**
* Close the underlying connection, emitting `onComplete` on the receive()
* Publisher.
*/
close(): void,
/**
* Open the underlying connection. Throws if the connection is already in
* the CLOSED or ERROR state.
*/
connect(): void,
/**
* Returns a Flowable that immediately publishes the current connection
* status and thereafter updates as it changes. Once a connection is in
* the CLOSED or ERROR state, it may not be connected again.
* Implementations must publish values per the comments on ConnectionStatus.
*/
connectionStatus(): Flowable<ConnectionStatus>,
}
/**
* Describes the connection status of a ReactiveSocket/DuplexConnection.
* - NOT_CONNECTED: no connection established or pending.
* - CONNECTING: when `connect()` has been called but a connection is not yet
* established.
* - CONNECTED: when a connection is established.
* - CLOSED: when the connection has been explicitly closed via `close()`.
* - ERROR: when the connection has been closed for any other reason.
*/
export type ConnectionStatus =
| {kind: 'NOT_CONNECTED'}
| {kind: 'CONNECTING'}
| {kind: 'CONNECTED'}
| {kind: 'CLOSED'}
| {kind: 'ERROR', error: Error};
export const CONNECTION_STATUS = {
CLOSED: Object.freeze({kind: 'CLOSED'}),
CONNECTED: Object.freeze({kind: 'CONNECTED'}),
CONNECTING: Object.freeze({kind: 'CONNECTING'}),
NOT_CONNECTED: Object.freeze({kind: 'NOT_CONNECTED'}),
};
/**
* A type that can be written to a buffer.
*/
export type Encodable = string | Buffer | Uint8Array;
/**
* A single unit of data exchanged between the peers of a `ReactiveSocket`.
*/
export type Payload<D, M> = {|
data: ?D,
metadata: ?M,
|};
/**
* A single unit of data for connection from the peers of a `ReactiveSocket`.
*/
export type SetupPayload<D, M> = {|
data: ?D,
metadata: ?M,
keepAlive: number,
lifetime: number,
metadataMimeType: ?string,
dataMimeType: ?string
|};
export type Frame =
| CancelFrame
| ErrorFrame
| KeepAliveFrame
| LeaseFrame
| PayloadFrame
| MetadataPushFrame
| RequestChannelFrame
| RequestFnfFrame
| RequestNFrame
| RequestResponseFrame
| RequestStreamFrame
| ResumeFrame
| ResumeOkFrame
| SetupFrame
| UnsupportedFrame;
export type FrameWithData = {
data: ?Encodable,
metadata: ?Encodable,
};
// prettier-ignore
export type CancelFrame = {|
type: 0x09,
flags: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type ErrorFrame = {|
type: 0x0B,
flags: number,
code: number,
message: string,
streamId: number,
length?: number,
|};
// prettier-ignore
export type KeepAliveFrame = {|
type: 0x03,
flags: number,
data: ?Encodable,
lastReceivedPosition: number,
streamId: 0,
length?: number,
|};
// prettier-ignore
export type LeaseFrame = {|
type: 0x02,
flags: number,
ttl: number,
requestCount: number,
metadata: ?Encodable,
streamId: 0,
length?: number,
|};
// prettier-ignore
export type PayloadFrame = {|
type: 0x0A,
flags: number,
data: ?Encodable,
metadata: ?Encodable,
streamId: number,
length?: number,
|};
// prettier-ignore
export type MetadataPushFrame = {|
type: 0x0C,
metadata: ?Encodable,
flags: number,
streamId: 0,
length?: number,
|};
// prettier-ignore
export type RequestChannelFrame = {|
type: 0x07,
data: ?Encodable,
metadata: ?Encodable,
flags: number,
requestN: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type RequestFnfFrame = {|
type: 0x05,
data: ?Encodable,
metadata: ?Encodable,
flags: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type RequestNFrame = {|
type: 0x08,
flags: number,
requestN: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type RequestResponseFrame = {|
type: 0x04,
data: ?Encodable,
metadata: ?Encodable,
flags: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type RequestStreamFrame = {|
type: 0x06,
data: ?Encodable,
metadata: ?Encodable,
flags: number,
requestN: number,
streamId: number,
length?: number,
|};
// prettier-ignore
export type ResumeFrame = {|
type: 0x0d,
clientPosition: number,
flags: number,
majorVersion: number,
minorVersion: number,
resumeToken: Encodable,
serverPosition: number,
streamId: 0,
length?: number,
|};
// prettier-ignore
export type ResumeOkFrame = {|
type: 0x0e,
clientPosition: number,
flags: number,
streamId: 0,
length?: number,
|};
// prettier-ignore
export type SetupFrame = {|
type: 0x01,
data: ?Encodable,
dataMimeType: string,
flags: number,
keepAlive: number,
lifetime: number,
metadata: ?Encodable,
metadataMimeType: string,
resumeToken: ?Encodable,
streamId: 0,
majorVersion: number,
minorVersion: number,
length?: number,
|};
// prettier-ignore
export type UnsupportedFrame = {|
type: 0x3f | 0x00,
streamId: 0,
flags: number,
length?: number,
|};