-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathtypes.ts
376 lines (330 loc) · 11.7 KB
/
types.ts
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/** EventMap */
export type EventMap = {
[key: string]: (...args: any[]) => void
}
/** Typed event emitter. */
export interface TypedEventEmitter<Events extends EventMap> {
addListener<E extends keyof Events>(event: E, listener: Events[E]): this
on<E extends keyof Events>(event: E, listener: Events[E]): this
once<E extends keyof Events>(event: E, listener: Events[E]): this
prependListener<E extends keyof Events>(event: E, listener: Events[E]): this
prependOnceListener<E extends keyof Events>(event: E, listener: Events[E]): this
off<E extends keyof Events>(event: E, listener: Events[E]): this
removeAllListeners<E extends keyof Events>(event?: E): this
removeListener<E extends keyof Events>(event: E, listener: Events[E]): this
emit<E extends keyof Events>(event: E, ...args: Parameters<Events[E]>): boolean
rawListeners<E extends keyof Events>(event: E): Events[E][]
listeners<E extends keyof Events>(event: E): Events[E][]
}
/** Client events which can be emitted. */
export type ClientEvents = {
/** called when client state changes */
state: (ctx: StateContext) => void;
/** called when client goes to connecting state */
connecting: (ctx: ConnectingContext) => void;
/** called when client goes to connected state */
connected: (ctx: ConnectedContext) => void;
/** called when client goes to disconnected state */
disconnected: (ctx: DisconnectedContext) => void;
// Async message coming from a server.
message: (ctx: MessageContext) => void;
// Listen to errors happening internally.
error: (ctx: ErrorContext) => void;
// Listen for server-side subscription events.
subscribed: (ctx: ServerSubscribedContext) => void;
subscribing: (ctx: ServerSubscribingContext) => void;
unsubscribed: (ctx: ServerUnsubscribedContext) => void;
publication: (ctx: ServerPublicationContext) => void;
join: (ctx: ServerJoinContext) => void;
leave: (ctx: ServerLeaveContext) => void;
}
/** State of client. */
export enum State {
Disconnected = "disconnected",
Connecting = "connecting",
Connected = "connected"
}
/** Events of Subscription. */
export type SubscriptionEvents = {
/** called when subscription state changes */
state: (ctx: SubscriptionStateContext) => void;
/** called when subscription state goes to subscribing */
subscribing: (ctx: SubscribingContext) => void;
/** called when subscription state goes to subscribed */
subscribed: (ctx: SubscribedContext) => void;
/** called when subscription state goes to unsubscribed */
unsubscribed: (ctx: UnsubscribedContext) => void;
/** called when publication from channel received */
publication: (ctx: PublicationContext) => void;
/** called when join event from channel received */
join: (ctx: JoinContext) => void;
/** called when leave event from channel received */
leave: (ctx: LeaveContext) => void;
/** listen to subscription errors happening internally */
error: (ctx: SubscriptionErrorContext) => void;
}
/** State of Subscription */
export enum SubscriptionState {
Unsubscribed = "unsubscribed",
Subscribing = "subscribing",
Subscribed = "subscribed"
}
export type TransportName = 'websocket' | 'http_stream' | 'sse' | 'sockjs' | 'webtransport';
/** TransportEndpoint allows configuring transport when using fallback mode */
export interface TransportEndpoint {
/** transport to use */
transport: TransportName;
/** endpoint for a selected transport type */
endpoint: string;
}
/** Options for Centrifuge client. */
export interface Options {
// provide header emulation, these headers are sent with first protocol message
// the backend can process those in a customized manner. In case of Centrifugo
// these headers are then used like real HTTP headers sent from the client.
// Requires Centrifugo v6.
headers: {[key: string]: string};
/** allows enabling debug mode */
debug: boolean;
/** allows setting initial connection token (JWT) */
token: string;
/** allows setting function to get/refresh connection token */
getToken: null | ((ctx: ConnectionTokenContext) => Promise<string>);
/** data to send to a server with connect command */
data: any | null;
/** allows setting function to get/renew connection data */
getData: null | (() => Promise<any>);
/** name of client - it's not a unique name of each connection, it's sth to identify
* from where client connected */
name: string;
/** version of client */
version: string;
/** minimum delay between reconnect attempts in milliseconds */
minReconnectDelay: number;
/** maximum delay between reconnect attempts in milliseconds */
maxReconnectDelay: number;
/** timeout for operations in milliseconds */
timeout: number;
/** maximum delay of server pings to detect broken connection in milliseconds */
maxServerPingDelay: number;
/** provide custom WebSocket constructor, useful for NodeJS env where WebSocket is not
* available globally */
websocket: any | null;
/** provide shim for fetch implementation */
fetch: any | null;
/** provide shim for ReadableStream */
readableStream: any | null;
/** provide shim for EventSource object */
eventsource: any | null;
/** provide shim for SockJS object */
sockjs: any | null;
/** allows modifying options passed to SockJS constructor */
sockjsOptions: SockjsOptions;
/** which emulation endpoint to use */
emulationEndpoint: string;
/** EventTarget for network online/offline events, in browser environment
* Centrifuge uses global window online/offline events automatically
* by default. */
networkEventTarget: EventTarget | null;
}
export interface SockjsOptions {
transports?: string[];
timeout?: number;
}
export interface StateContext {
newState: State;
oldState: State;
}
export interface ConnectedContext {
client: string;
transport: string;
data?: any;
}
export interface ErrorContext {
type: string;
error: Error;
transport?: string;
}
export interface Error {
code: number;
message: string;
}
export interface ConnectingContext {
code: number;
reason: string;
}
export interface DisconnectedContext {
code: number;
reason: string;
}
export interface MessageContext {
data: any;
}
export interface PublicationContext {
// channel from which publication was received.
channel: string;
// data contains publication payload.
data: any;
// info is an optional ClientInfo object. It's appended to publication only if publication was
// sent using client SDK's publish method. If publication was sent over server publish API
// this info object is missing as we don't have publisher client context in that case.
info?: ClientInfo;
// offset may be set for channels where history Centrifugo feature is on. In this case it's an
// incremental number assigned to publication by server broker (upon adding to history stream).
offset?: number;
// tags is an extra key-value attached to publication, tags may be set when calling server publish API.
tags?: Record<string, string>;
}
export interface ClientInfo {
// client is a globally unique identifier which server allocates for every connection.
client: string;
// user contains ID of authenticated user. Empty user means anonymous user. One user can have
// many client connections.
user: string;
// connInfo is optional information attached to connection (during connection authentication).
connInfo?: any;
// chanInfo is optional information attached to subscription (during subscription authorization).
chanInfo?: any;
}
export interface JoinContext {
channel: string;
info: ClientInfo;
}
export interface LeaveContext {
channel: string;
info: ClientInfo;
}
export interface SubscriptionStateContext {
channel: string;
newState: SubscriptionState;
oldState: SubscriptionState;
}
export interface ServerSubscribedContext {
/** channel of Subscription. */
channel: string;
/** Subscription is recoverable – i.e. can automatically recover missed messages */
recoverable: boolean;
/** Subscription is positioned – i.e. server tracks message loss on the way from PUB/SUB broker */
positioned: boolean;
/** streamPosition set when Subscription is recoverable or positioned. */
streamPosition?: StreamPosition;
/** wasRecovering is true when recovery was used in subscribe request. */
wasRecovering: boolean;
/** whether or not missed publications may be successfully recovered. */
recovered: boolean;
/** custom data for Subscription returned from server. */
data?: any;
}
export interface SubscribedContext {
channel: string;
recoverable: boolean;
positioned: boolean;
streamPosition?: StreamPosition;
wasRecovering: boolean;
recovered: boolean;
data?: any;
}
export interface SubscriptionErrorContext {
channel: string;
type: string;
error: Error;
}
export interface UnsubscribedContext {
channel: string;
code: number;
reason: string;
}
export interface ServerPublicationContext {
// channel from which publication was received.
channel: string;
// data contains publication payload.
data: any;
// info is an optional ClientInfo object. It's appended to publication only if publication was
// sent using client SDK's publish method. If publication was sent over server publish API
// this info object is missing as we don't have publisher client context in that case.
info?: ClientInfo;
// offset may be set for channels where history Centrifugo feature is on. In this case it's an
// incremental number assigned to publication by server broker (upon adding to history stream).
offset?: number;
// tags is an extra key-value attached to publication, tags may be set when calling server publish API.
tags?: Record<string, string>;
}
export interface ServerJoinContext {
channel: string;
info: ClientInfo;
}
export interface ServerLeaveContext {
channel: string;
info: ClientInfo;
}
export interface ServerUnsubscribedContext {
channel: string;
}
export interface SubscribingContext {
channel: string;
code: number;
reason: string;
}
export interface ServerSubscribingContext {
channel: string;
}
export interface ConnectionTokenContext {
}
export interface SubscriptionTokenContext {
channel: string;
}
export interface SubscriptionDataContext {
channel: string;
}
export interface PublishResult {
}
export interface RpcResult {
data: any;
}
export interface PresenceResult {
clients: Record<string, ClientInfo>;
}
export interface PresenceStatsResult {
numClients: number;
numUsers: number;
}
export interface HistoryResult {
publications: PublicationContext[];
offset: number;
epoch: string;
}
export interface HistoryOptions {
limit?: number;
since?: StreamPosition;
reverse?: boolean;
}
/** SubscriptionOptions can customize Subscription. */
export interface SubscriptionOptions {
/** allows setting initial subscription token (JWT) */
token: string;
/** allows setting function to get/refresh subscription token */
getToken: null | ((ctx: SubscriptionTokenContext) => Promise<string>);
/** data to send to a server with subscribe command */
data: any | null;
/** allows setting function to get/renew subscription data */
getData: null | ((ctx: SubscriptionDataContext) => Promise<any>);
/** force recovery on first subscribe from a provided StreamPosition. */
since: StreamPosition | null;
/** min delay between resubscribe attempts. */
minResubscribeDelay: number;
/** max delay between resubscribe attempts. */
maxResubscribeDelay: number;
/** ask server to make subscription positioned. */
positioned: boolean;
/** ask server to make subscription recoverable. */
recoverable: boolean;
/** ask server to send join/leave messages. */
joinLeave: boolean;
/** delta format to be used */
delta: 'fossil';
}
/** Stream postion describes position of publication inside a stream. */
export interface StreamPosition {
offset: number;
epoch: string;
}