@eleven-am/pondsocket 0.1.214 → 0.1.216
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/abstracts/distributor.d.ts +13 -0
- package/abstracts/distributor.js +148 -56
- package/abstracts/middleware.d.ts +22 -0
- package/abstracts/middleware.js +8 -1
- package/abstracts/types.d.ts +65 -0
- package/abstracts/types.js +0 -14
- package/contexts/baseContext.d.ts +20 -0
- package/contexts/baseContext.js +17 -25
- package/contexts/connectionContext.d.ts +66 -0
- package/contexts/connectionContext.js +1 -0
- package/contexts/eventContext.d.ts +15 -0
- package/contexts/eventContext.js +5 -49
- package/contexts/joinContext.d.ts +17 -0
- package/contexts/joinContext.js +14 -55
- package/contexts/outgoingContext.d.ts +28 -0
- package/contexts/outgoingContext.js +3 -0
- package/engines/channelEngine.d.ts +26 -0
- package/engines/channelEngine.js +243 -192
- package/engines/endpointEngine.d.ts +34 -0
- package/engines/endpointEngine.js +23 -7
- package/engines/lobbyEngine.d.ts +54 -0
- package/engines/lobbyEngine.js +1 -1
- package/engines/presenceEngine.d.ts +15 -0
- package/engines/presenceEngine.js +6 -59
- package/errors/httpError.d.ts +6 -0
- package/errors/httpError.js +2 -0
- package/index.d.ts +14 -5
- package/index.js +37 -3
- package/matcher/matcher.d.ts +2 -0
- package/matcher/matcher.js +5 -5
- package/package.json +44 -21
- package/server/server.d.ts +21 -0
- package/server/server.js +55 -12
- package/types.d.ts +59 -622
- package/types.js +19 -0
- package/wrappers/channel.d.ts +54 -0
- package/wrappers/endpoint.d.ts +10 -0
- package/wrappers/pondChannel.d.ts +15 -0
package/types.d.ts
CHANGED
|
@@ -1,34 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
EventParams,
|
|
5
|
-
IncomingConnection,
|
|
6
|
-
JoinParams,
|
|
7
|
-
Params,
|
|
8
|
-
PondAssigns,
|
|
9
|
-
PondEvent,
|
|
10
|
-
PondEventMap,
|
|
11
|
-
PondMessage,
|
|
12
|
-
PondPath,
|
|
13
|
-
PondPresence,
|
|
14
|
-
Unsubscribe,
|
|
15
|
-
UserAssigns,
|
|
16
|
-
UserData,
|
|
17
|
-
UserPresences,
|
|
18
|
-
} from '@eleven-am/pondsocket-common';
|
|
19
|
-
import { WebSocket, WebSocketServer } from 'ws';
|
|
20
|
-
|
|
21
|
-
export * from '@eleven-am/pondsocket-common';
|
|
22
|
-
|
|
23
|
-
export interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
24
|
-
user: UserData<PresenceType, AssignType>;
|
|
25
|
-
channel: Channel<EventTypes, PresenceType, AssignType>;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
|
|
29
|
-
|
|
30
|
-
export type RequestHandler<Request> = (request: Request) => void | Promise<void>;
|
|
31
|
-
|
|
1
|
+
import { Server as HTTPServer } from 'http';
|
|
2
|
+
import { ChannelReceivers, PondAssigns, PondMessage, PondPresence, Unsubscribe } from '@eleven-am/pondsocket-common';
|
|
3
|
+
import { WebSocketServer } from 'ws';
|
|
32
4
|
export interface RedisDistributedBackendOptions {
|
|
33
5
|
host?: string;
|
|
34
6
|
port?: number;
|
|
@@ -36,40 +8,48 @@ export interface RedisDistributedBackendOptions {
|
|
|
36
8
|
database?: number;
|
|
37
9
|
url?: string;
|
|
38
10
|
keyPrefix?: string;
|
|
11
|
+
namespace?: string;
|
|
12
|
+
heartbeatIntervalMs?: number;
|
|
13
|
+
heartbeatTimeoutMs?: number;
|
|
14
|
+
onError?: (error: Error) => void;
|
|
39
15
|
}
|
|
40
|
-
|
|
41
16
|
export interface PondSocketOptions {
|
|
42
17
|
server?: HTTPServer;
|
|
43
18
|
socketServer?: WebSocketServer;
|
|
44
19
|
exclusiveServer?: boolean;
|
|
45
20
|
distributedBackend?: IDistributedBackend;
|
|
21
|
+
maxMessageSize?: number;
|
|
22
|
+
heartbeatInterval?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare enum DistributedMessageType {
|
|
25
|
+
STATE_REQUEST = "STATE_REQUEST",
|
|
26
|
+
STATE_RESPONSE = "STATE_RESPONSE",
|
|
27
|
+
USER_JOINED = "USER_JOINED",
|
|
28
|
+
USER_LEFT = "USER_LEFT",
|
|
29
|
+
USER_MESSAGE = "USER_MESSAGE",
|
|
30
|
+
PRESENCE_UPDATE = "PRESENCE_UPDATE",
|
|
31
|
+
PRESENCE_REMOVED = "PRESENCE_REMOVED",
|
|
32
|
+
ASSIGNS_UPDATE = "ASSIGNS_UPDATE",
|
|
33
|
+
EVICT_USER = "EVICT_USER",
|
|
34
|
+
USER_REMOVE = "USER_REMOVE",
|
|
35
|
+
USER_GET_REQUEST = "USER_GET_REQUEST",
|
|
36
|
+
USER_GET_RESPONSE = "USER_GET_RESPONSE",
|
|
37
|
+
NODE_HEARTBEAT = "NODE_HEARTBEAT"
|
|
46
38
|
}
|
|
47
|
-
|
|
48
|
-
export enum DistributedMessageType {
|
|
49
|
-
STATE_REQUEST = 'STATE_REQUEST',
|
|
50
|
-
STATE_RESPONSE = 'STATE_RESPONSE',
|
|
51
|
-
USER_JOINED = 'USER_JOINED',
|
|
52
|
-
USER_LEFT = 'USER_LEFT',
|
|
53
|
-
USER_MESSAGE = 'USER_MESSAGE',
|
|
54
|
-
PRESENCE_UPDATE = 'PRESENCE_UPDATE',
|
|
55
|
-
PRESENCE_REMOVED = 'PRESENCE_REMOVED',
|
|
56
|
-
ASSIGNS_UPDATE = 'ASSIGNS_UPDATE',
|
|
57
|
-
ASSIGNS_REMOVED = 'ASSIGNS_REMOVED',
|
|
58
|
-
EVICT_USER = 'EVICT_USER'
|
|
59
|
-
}
|
|
60
|
-
|
|
61
39
|
export interface DistributedMessage {
|
|
40
|
+
protocol?: 'pondsocket.distributed';
|
|
41
|
+
version?: 1;
|
|
62
42
|
type: DistributedMessageType;
|
|
43
|
+
messageId?: string;
|
|
63
44
|
endpointName: string;
|
|
64
45
|
channelName: string;
|
|
65
46
|
timestamp?: number;
|
|
47
|
+
sourceNodeId?: string;
|
|
66
48
|
}
|
|
67
|
-
|
|
68
49
|
export interface StateRequest extends DistributedMessage {
|
|
69
50
|
type: DistributedMessageType.STATE_REQUEST;
|
|
70
51
|
fromNode: string;
|
|
71
52
|
}
|
|
72
|
-
|
|
73
53
|
export interface StateResponse extends DistributedMessage {
|
|
74
54
|
type: DistributedMessageType.STATE_RESPONSE;
|
|
75
55
|
users: Array<{
|
|
@@ -78,617 +58,74 @@ export interface StateResponse extends DistributedMessage {
|
|
|
78
58
|
assigns: PondAssigns;
|
|
79
59
|
}>;
|
|
80
60
|
}
|
|
81
|
-
|
|
82
61
|
export interface UserJoined extends DistributedMessage {
|
|
83
62
|
type: DistributedMessageType.USER_JOINED;
|
|
84
63
|
userId: string;
|
|
85
64
|
presence: PondPresence;
|
|
86
65
|
assigns: PondAssigns;
|
|
87
66
|
}
|
|
88
|
-
|
|
89
67
|
export interface UserLeft extends DistributedMessage {
|
|
90
68
|
type: DistributedMessageType.USER_LEFT;
|
|
91
69
|
userId: string;
|
|
92
70
|
}
|
|
93
|
-
|
|
94
71
|
export interface UserMessage extends DistributedMessage {
|
|
95
72
|
type: DistributedMessageType.USER_MESSAGE;
|
|
96
73
|
fromUserId: string;
|
|
97
74
|
event: string;
|
|
98
75
|
payload: PondMessage;
|
|
99
76
|
requestId: string;
|
|
100
|
-
|
|
77
|
+
recipientDescriptor: ChannelReceivers;
|
|
101
78
|
}
|
|
102
|
-
|
|
103
79
|
export interface PresenceUpdate extends DistributedMessage {
|
|
104
80
|
type: DistributedMessageType.PRESENCE_UPDATE;
|
|
105
81
|
userId: string;
|
|
106
82
|
presence: PondPresence;
|
|
107
83
|
}
|
|
108
|
-
|
|
109
84
|
export interface PresenceRemoved extends DistributedMessage {
|
|
110
85
|
type: DistributedMessageType.PRESENCE_REMOVED;
|
|
111
86
|
userId: string;
|
|
112
87
|
}
|
|
113
|
-
|
|
114
88
|
export interface AssignsUpdate extends DistributedMessage {
|
|
115
89
|
type: DistributedMessageType.ASSIGNS_UPDATE;
|
|
116
90
|
userId: string;
|
|
117
|
-
assigns
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
export interface AssignsRemoved extends DistributedMessage {
|
|
121
|
-
type: DistributedMessageType.ASSIGNS_REMOVED;
|
|
122
|
-
userId: string;
|
|
91
|
+
assigns?: PondAssigns;
|
|
92
|
+
key?: string;
|
|
93
|
+
value?: unknown;
|
|
123
94
|
}
|
|
124
|
-
|
|
125
95
|
export interface EvictUser extends DistributedMessage {
|
|
126
96
|
type: DistributedMessageType.EVICT_USER;
|
|
127
97
|
userId: string;
|
|
128
98
|
reason: string;
|
|
129
|
-
fromNode?: string;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export type DistributedChannelMessage =
|
|
133
|
-
| StateRequest
|
|
134
|
-
| StateResponse
|
|
135
|
-
| UserJoined
|
|
136
|
-
| UserLeft
|
|
137
|
-
| UserMessage
|
|
138
|
-
| PresenceUpdate
|
|
139
|
-
| PresenceRemoved
|
|
140
|
-
| AssignsUpdate
|
|
141
|
-
| AssignsRemoved
|
|
142
|
-
| EvictUser;
|
|
143
|
-
|
|
144
|
-
export interface IDistributedBackend {
|
|
145
|
-
broadcast(endpointName: string, channelName: string, message: DistributedChannelMessage): Promise<void>;
|
|
146
|
-
subscribe(endpointName: string, channelName: string, handler: (message: DistributedChannelMessage) => void): Unsubscribe;
|
|
147
|
-
cleanup(): Promise<void>;
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
export declare class PondSocket {
|
|
151
|
-
constructor(options?: PondSocketOptions);
|
|
152
|
-
|
|
153
|
-
/**
|
|
154
|
-
* @desc Specifies the port to listen on
|
|
155
|
-
* @param args - the arguments to pass to the server
|
|
156
|
-
*/
|
|
157
|
-
listen(...args: any[]): Server<typeof IncomingMessage, typeof ServerResponse>
|
|
158
|
-
|
|
159
|
-
/**
|
|
160
|
-
* @desc Closes the server
|
|
161
|
-
* @param callback - the callback to call when the server is closed
|
|
162
|
-
*/
|
|
163
|
-
close(callback?: () => void): Server<typeof IncomingMessage, typeof ServerResponse>
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
|
|
167
|
-
* @param path - the pattern to accept || can also be a regex
|
|
168
|
-
* @param handler - the handler function to authenticate the socket
|
|
169
|
-
* @example
|
|
170
|
-
* const endpoint = pond.createEndpoint('/api/socket', (context) => {
|
|
171
|
-
* const token = context.query.token;
|
|
172
|
-
* if (!token)
|
|
173
|
-
* return context.decline('No token provided');
|
|
174
|
-
* context.assign({
|
|
175
|
-
* token
|
|
176
|
-
* }).accept();
|
|
177
|
-
* })
|
|
178
|
-
*/
|
|
179
|
-
createEndpoint<Path extends string>(path: PondPath<Path>, handler: RequestHandler<ConnectionContext<Path>>): Endpoint;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
export declare class Endpoint {
|
|
183
|
-
/**
|
|
184
|
-
* @desc Adds a new PondChannel to this path on this endpoint
|
|
185
|
-
* @param {PondPath<string>} path - The path to add the channel to
|
|
186
|
-
* @param {RequestHandler} handler - The handler to use to authenticate the client
|
|
187
|
-
* @returns {PondChannel} - The PondChannel instance for the new channel
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* const channel = endpoint.createChannel('/chat', (context) => {
|
|
191
|
-
* if (context.user.assigns.admin)
|
|
192
|
-
* context.accept();
|
|
193
|
-
* else
|
|
194
|
-
* context.decline('You are not an admin', 403);
|
|
195
|
-
* });
|
|
196
|
-
*/
|
|
197
|
-
createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: RequestHandler<JoinContext<Path, EventType, PresenceType, AssignType>>): PondChannel<EventType, PresenceType, AssignType>;
|
|
198
|
-
|
|
199
|
-
/**
|
|
200
|
-
* @desc Closes specific clients connected to this endpoint
|
|
201
|
-
* @param {string | string[]} clientIds - The ids of the clients to close.
|
|
202
|
-
*/
|
|
203
|
-
closeConnection(clientIds: string | string[]): void;
|
|
204
|
-
|
|
205
|
-
/**
|
|
206
|
-
* @desc Returns all the clients connected to this endpoint
|
|
207
|
-
*/
|
|
208
|
-
getClients(): WebSocket[];
|
|
209
99
|
}
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
* @param {PondPath<string>} event - The event to handle
|
|
215
|
-
* @param {RequestHandler} handler - The handlers to execute when the event is received
|
|
216
|
-
* @example
|
|
217
|
-
* pond.onEvent('echo', (context) => {
|
|
218
|
-
* context.reply('echo', {
|
|
219
|
-
* message: context.event.payload,
|
|
220
|
-
* });
|
|
221
|
-
* });
|
|
222
|
-
*/
|
|
223
|
-
onEvent<Event extends string>(event: PondPath<Event>, handler: RequestHandler<EventContext<Event, EventType, PresenceType, AssignType>>): void;
|
|
224
|
-
|
|
225
|
-
/**
|
|
226
|
-
* @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
|
|
227
|
-
* @param {LeaveCallback} callback - The callback to execute when a user leaves
|
|
228
|
-
* @example
|
|
229
|
-
* pond.onLeave((event) => {
|
|
230
|
-
* // Clean up resources
|
|
231
|
-
* });
|
|
232
|
-
*/
|
|
233
|
-
onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
|
|
234
|
-
|
|
235
|
-
/**
|
|
236
|
-
* @desc Handles an outgoing event, this is useful for modifying the event before it is sent to the client
|
|
237
|
-
* @param {PondPath<string>} event - The event to handle
|
|
238
|
-
* @param {RequestHandler} handler - The handler to execute when the event is sent
|
|
239
|
-
* @example
|
|
240
|
-
* pond.handleOutgoingEvent('echo', (event) => {
|
|
241
|
-
* return {
|
|
242
|
-
* message: 'Hello, world!'
|
|
243
|
-
* };
|
|
244
|
-
* });
|
|
245
|
-
*/
|
|
246
|
-
handleOutgoingEvent<Event extends string> (event: PondPath<Event>, handler: RequestHandler<OutgoingContext<Event, PresenceType, AssignType>>): void;
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* @desc Gets a channel by name
|
|
250
|
-
* @param {string} channelName - The name of the channel to get
|
|
251
|
-
* @returns {Channel} - The channel instance
|
|
252
|
-
* @example
|
|
253
|
-
* const channel = pond.getChannel('my_channel')!;
|
|
254
|
-
*
|
|
255
|
-
* if (channel === null) {
|
|
256
|
-
* console.log('Channel not found');
|
|
257
|
-
* return;
|
|
258
|
-
* }
|
|
259
|
-
*/
|
|
260
|
-
getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
|
|
261
|
-
|
|
262
|
-
/**
|
|
263
|
-
* Broadcasts a message to all clients in the channel
|
|
264
|
-
* @param channelName - The name of the channel to broadcast to
|
|
265
|
-
* @param event - The event to send
|
|
266
|
-
* @param payload - The payload to send
|
|
267
|
-
* @example
|
|
268
|
-
*
|
|
269
|
-
* pond.broadcast('my_channel', 'message', {
|
|
270
|
-
* text: 'Hello, world!'
|
|
271
|
-
* });
|
|
272
|
-
*/
|
|
273
|
-
broadcast <Key extends keyof EventType>(channelName: string, event: Key, payload: EventType[Key]): void;
|
|
274
|
-
|
|
275
|
-
/**
|
|
276
|
-
* Broadcasts a message to all clients in the channel except the sender
|
|
277
|
-
* @param channelName - The name of the channel to broadcast to
|
|
278
|
-
* @param event - The event to send
|
|
279
|
-
* @param payload - The payload to send
|
|
280
|
-
* @example
|
|
281
|
-
*
|
|
282
|
-
* pond.broadcastFrom('my_channel', 'message', {
|
|
283
|
-
* text: 'Hello, everyone but me!'
|
|
284
|
-
* });
|
|
285
|
-
*/
|
|
286
|
-
broadcastFrom <Key extends keyof EventType> (channelName: string, event: Key, payload: EventType[Key]): void
|
|
287
|
-
|
|
288
|
-
/**
|
|
289
|
-
* Broadcasts a message to a specific set of clients
|
|
290
|
-
* @param channelName - The name of the channel to broadcast to
|
|
291
|
-
* @param event - The event to send
|
|
292
|
-
* @param payload - The payload to send
|
|
293
|
-
* @param userIds - The ids of the clients to send the message to
|
|
294
|
-
* @example
|
|
295
|
-
*
|
|
296
|
-
* pond.broadcastTo('my_channel', 'message', {
|
|
297
|
-
* text: 'Hello, specific people!'
|
|
298
|
-
* }, ['user1', 'user2']);
|
|
299
|
-
*/
|
|
300
|
-
broadcastTo <Key extends keyof EventType> (channelName: string, event: Key, payload: EventType[Key], userIds: string | string[]): void;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
export declare class Channel<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
304
|
-
/**
|
|
305
|
-
* The name of the channel.
|
|
306
|
-
*/
|
|
307
|
-
name: string;
|
|
308
|
-
|
|
309
|
-
/**
|
|
310
|
-
* @desc Gets the current assign data for the channel.
|
|
311
|
-
*/
|
|
312
|
-
getAssigns(): Record<string, AssignType>;
|
|
313
|
-
|
|
314
|
-
/**
|
|
315
|
-
* @desc Gets the current presence data for the channel.
|
|
316
|
-
*/
|
|
317
|
-
getPresences(): Record<string, PresenceType>;
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* @desc Gets the assign date for a specific user.
|
|
321
|
-
* @param {string} userId - The id of the user to get the data for.
|
|
322
|
-
*/
|
|
323
|
-
getUserData(userId: string): UserData<PresenceType, AssignType> | null;
|
|
324
|
-
|
|
325
|
-
/**
|
|
326
|
-
* @desc Broadcasts a message to every client in the channel,
|
|
327
|
-
* @param {string} event - The event to send.
|
|
328
|
-
* @param {PondMessage} payload - The message to send.
|
|
329
|
-
*/
|
|
330
|
-
broadcast<Key extends keyof EventTypes>(event: Key, payload: EventTypes[Key]): Channel<EventTypes, PresenceType, AssignType>;
|
|
331
|
-
|
|
332
|
-
/**
|
|
333
|
-
* @desc Broadcasts a message to every client in the channel except the sender,
|
|
334
|
-
* @param {string} userId - The id of the user to exclude from the broadcast.
|
|
335
|
-
* @param {string} event - The event to send.
|
|
336
|
-
* @param {PondMessage} payload - The message to send.
|
|
337
|
-
*/
|
|
338
|
-
broadcastFrom<Key extends keyof EventTypes>(userId: string, event: Key, payload: EventTypes[Key]): Channel<EventTypes, PresenceType, AssignType>;
|
|
339
|
-
|
|
340
|
-
/**
|
|
341
|
-
* @desc Sends a message to a specific client in the channel.
|
|
342
|
-
* @param {string | string[]} clientIds - The id of the client to send the message to.
|
|
343
|
-
* @param {string} event - The event to send.
|
|
344
|
-
* @param {PondMessage} payload - The message to send.
|
|
345
|
-
*/
|
|
346
|
-
broadcastTo<Key extends keyof EventTypes>(clientIds: string | string[], event: Key, payload: EventTypes[Key]): Channel<EventTypes, PresenceType, AssignType>;
|
|
347
|
-
|
|
348
|
-
/**
|
|
349
|
-
* @desc Bans a user from the channel.
|
|
350
|
-
* @param {string} userId - The id of the user to ban.
|
|
351
|
-
* @param {string} reason - The reason for the ban.
|
|
352
|
-
*/
|
|
353
|
-
evictUser(userId: string, reason?: string): Channel<EventTypes, PresenceType, AssignType>;
|
|
354
|
-
|
|
355
|
-
/**
|
|
356
|
-
* @desc tracks a user's presence in the channel
|
|
357
|
-
* @param {string} userId - the id of the user to track
|
|
358
|
-
* @param {PondPresence} presence - the presence data to track
|
|
359
|
-
*/
|
|
360
|
-
trackPresence(userId: string, presence: PresenceType): Channel<EventTypes, PresenceType, AssignType>;
|
|
361
|
-
|
|
362
|
-
/**
|
|
363
|
-
* @desc removes a user's presence from the channel
|
|
364
|
-
* @param {string} userId - the id of the user to remove
|
|
365
|
-
*/
|
|
366
|
-
removePresence(userId: string): Channel<EventTypes, PresenceType, AssignType>;
|
|
367
|
-
|
|
368
|
-
/**
|
|
369
|
-
* @desc updates a user's presence in the channel
|
|
370
|
-
* @param {string} userId - the id of the user to update
|
|
371
|
-
* @param {PondPresence} presence - the presence data to update
|
|
372
|
-
*/
|
|
373
|
-
updatePresence(userId: string, presence: Partial<PresenceType>): Channel<EventTypes, PresenceType, AssignType>;
|
|
374
|
-
|
|
375
|
-
/**
|
|
376
|
-
* @desc Updates a user's assigns in the channel
|
|
377
|
-
* @param userId - the id of the user to update
|
|
378
|
-
* @param assigns - the assigns data to update
|
|
379
|
-
*/
|
|
380
|
-
updateAssigns(userId: string, assigns: Partial<AssignType>): Channel<EventTypes, PresenceType, AssignType>;
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* @desc Tracks or updates a user's presence in the channel
|
|
384
|
-
* @param userId - the id of the user to upsert
|
|
385
|
-
* @param presence - the presence data to upsert
|
|
386
|
-
*/
|
|
387
|
-
upsertPresence(userId: string, presence: PresenceType): Channel<EventTypes, PresenceType, AssignType>;
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
export declare class ConnectionContext<Path extends string> {
|
|
391
|
-
/**
|
|
392
|
-
* @desc Checks if the server has responded to the connection request.
|
|
393
|
-
*/
|
|
394
|
-
get hasResponded(): boolean;
|
|
395
|
-
|
|
396
|
-
/**
|
|
397
|
-
* @desc Get the connection details.
|
|
398
|
-
*/
|
|
399
|
-
get connection(): IncomingConnection<Path>;
|
|
400
|
-
|
|
401
|
-
/**
|
|
402
|
-
* @desc Get the client ID.
|
|
403
|
-
*/
|
|
404
|
-
get clientId(): string;
|
|
405
|
-
|
|
406
|
-
/**
|
|
407
|
-
* @desc Get the request ID.
|
|
408
|
-
*/
|
|
409
|
-
get requestId(): string;
|
|
410
|
-
|
|
411
|
-
/**
|
|
412
|
-
* @desc Get the request headers.
|
|
413
|
-
*/
|
|
414
|
-
get headers(): IncomingHttpHeaders;
|
|
415
|
-
|
|
416
|
-
/**
|
|
417
|
-
* @desc Get the request cookies.
|
|
418
|
-
*/
|
|
419
|
-
get cookies(): Record<string, string>;
|
|
420
|
-
|
|
421
|
-
/**
|
|
422
|
-
* @desc Get the client address.
|
|
423
|
-
*/
|
|
424
|
-
get address(): string;
|
|
425
|
-
|
|
426
|
-
/**
|
|
427
|
-
* @desc Get the event parameters.
|
|
428
|
-
*/
|
|
429
|
-
get event(): EventParams<Path>;
|
|
430
|
-
|
|
431
|
-
/**
|
|
432
|
-
* @desc Get the query parameters.
|
|
433
|
-
*/
|
|
434
|
-
get query(): Record<string, string>;
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* @desc Get the path parameters.
|
|
438
|
-
*/
|
|
439
|
-
get params(): Params<Path>;
|
|
440
|
-
|
|
441
|
-
/**
|
|
442
|
-
* @desc Assigns data to the user.
|
|
443
|
-
* @param {PondAssigns} assigns - The data to assign.
|
|
444
|
-
* @returns {ConnectionContext} - The ConnectionContext instance for chaining.
|
|
445
|
-
*/
|
|
446
|
-
assign(assigns: PondAssigns): ConnectionContext<Path>;
|
|
447
|
-
|
|
448
|
-
/**
|
|
449
|
-
* @desc Accepts the connection request to the endpoint.
|
|
450
|
-
*/
|
|
451
|
-
accept(): ConnectionContext<Path>;
|
|
452
|
-
|
|
453
|
-
/**
|
|
454
|
-
* @desc Rejects the request with the given error message
|
|
455
|
-
* @param {string} message - The error message
|
|
456
|
-
* @param {number} errorCode - The error code
|
|
457
|
-
*/
|
|
458
|
-
decline(message?: string, errorCode?: number): ConnectionContext<Path>;
|
|
459
|
-
|
|
460
|
-
/**
|
|
461
|
-
* @desc Emits a direct message to the client
|
|
462
|
-
* @param {string} event - The event name
|
|
463
|
-
* @param {PondMessage} payload - The payload to send
|
|
464
|
-
*/
|
|
465
|
-
reply(event: string, payload: PondMessage): ConnectionContext<Path>;
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
export declare class BaseContext<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
|
|
469
|
-
/**
|
|
470
|
-
* @desc Get the event information.
|
|
471
|
-
*/
|
|
472
|
-
get event(): PondEvent<Path>;
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* @desc Get the channel name.
|
|
476
|
-
*/
|
|
477
|
-
get channelName(): string;
|
|
478
|
-
|
|
479
|
-
/**
|
|
480
|
-
* @desc Get the channel instance.
|
|
481
|
-
*/
|
|
482
|
-
get channel(): Channel;
|
|
483
|
-
|
|
484
|
-
/**
|
|
485
|
-
* @desc Get all current presences in the channel.
|
|
486
|
-
*/
|
|
487
|
-
get presences(): UserPresences;
|
|
488
|
-
|
|
489
|
-
/**
|
|
490
|
-
* @desc Get all current assigns in the channel.
|
|
491
|
-
*/
|
|
492
|
-
get assigns(): UserAssigns;
|
|
493
|
-
|
|
494
|
-
/**
|
|
495
|
-
* @desc Get the user who sent the request.
|
|
496
|
-
*/
|
|
497
|
-
get user(): UserData<PresenceType, AssignType>;
|
|
100
|
+
export interface UserRemove extends DistributedMessage {
|
|
101
|
+
type: DistributedMessageType.USER_REMOVE;
|
|
102
|
+
userId: string;
|
|
103
|
+
reason: string;
|
|
498
104
|
}
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
PresenceType extends PondPresence = PondPresence,
|
|
504
|
-
AssignType extends PondAssigns = PondAssigns
|
|
505
|
-
> extends BaseContext<Path, PresenceType, AssignType> {
|
|
506
|
-
/**
|
|
507
|
-
* @desc Get the join parameters.
|
|
508
|
-
*/
|
|
509
|
-
get joinParams(): JoinParams;
|
|
510
|
-
|
|
511
|
-
/**
|
|
512
|
-
* @desc Checks if the server has responded to the join request.
|
|
513
|
-
*/
|
|
514
|
-
get hasResponded(): boolean;
|
|
515
|
-
|
|
516
|
-
/**
|
|
517
|
-
* @desc Assigns data to the client
|
|
518
|
-
* @param {PondAssigns} assigns - The data to assign
|
|
519
|
-
*/
|
|
520
|
-
assign(assigns: AssignType): JoinContext<Path, PresenceType, AssignType>;
|
|
521
|
-
|
|
522
|
-
/**
|
|
523
|
-
* @desc Accepts the join request to the channel.
|
|
524
|
-
* @returns {JoinContext} - The JoinContext instance for chaining.
|
|
525
|
-
*/
|
|
526
|
-
accept(): JoinContext<Path, PresenceType, AssignType>;
|
|
527
|
-
|
|
528
|
-
/**
|
|
529
|
-
* @desc Rejects the request and optionally assigns data to the client
|
|
530
|
-
* @param {string} message - The error message
|
|
531
|
-
* @param {number} errorCode - The error code
|
|
532
|
-
*/
|
|
533
|
-
decline(message?: string, errorCode?: number): JoinContext<Path, PresenceType, AssignType>;
|
|
534
|
-
|
|
535
|
-
/**
|
|
536
|
-
* @desc Emits a direct message to the client
|
|
537
|
-
* @param {string} event - The event name
|
|
538
|
-
* @param {PondMessage} payload - The payload to send
|
|
539
|
-
*/
|
|
540
|
-
reply<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinContext<Path, PresenceType, AssignType>;
|
|
541
|
-
|
|
542
|
-
/**
|
|
543
|
-
* @desc Emits a message to all clients in the channel
|
|
544
|
-
* @param {string} event - The event name
|
|
545
|
-
* @param {PondMessage} payload - The payload to send
|
|
546
|
-
*/
|
|
547
|
-
broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinContext<Path, PresenceType, AssignType>;
|
|
548
|
-
|
|
549
|
-
/**
|
|
550
|
-
* @desc Emits a message to all clients in the channel except the sender
|
|
551
|
-
* @param event - the event name
|
|
552
|
-
* @param payload - the payload to send
|
|
553
|
-
*/
|
|
554
|
-
broadcastFrom<Key extends keyof EventType>(event: Key, payload: EventType[Key]): JoinContext<Path, PresenceType, AssignType>;
|
|
555
|
-
|
|
556
|
-
/**
|
|
557
|
-
* @desc Emits a message to a specific set of clients
|
|
558
|
-
* @param {string} event - The event name
|
|
559
|
-
* @param {PondMessage} payload - The payload to send
|
|
560
|
-
* @param {string | string[]} userIds - The ids of the clients to send the message to
|
|
561
|
-
*/
|
|
562
|
-
broadcastTo<Key extends keyof EventType>(event: Key, payload: EventType[Key], userIds: string | string[]): JoinContext<Path, PresenceType, AssignType>;
|
|
563
|
-
|
|
564
|
-
/**
|
|
565
|
-
* @desc tracks the presence of a client
|
|
566
|
-
* @param {PondPresence} presence - the presence data to track
|
|
567
|
-
*/
|
|
568
|
-
trackPresence(presence: PresenceType): JoinContext<Path, PresenceType, AssignType>;
|
|
105
|
+
export interface UserGetRequest extends DistributedMessage {
|
|
106
|
+
type: DistributedMessageType.USER_GET_REQUEST;
|
|
107
|
+
userId: string;
|
|
108
|
+
lookupRequestId: string;
|
|
569
109
|
}
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
/**
|
|
578
|
-
* @desc Assigns data to the client.
|
|
579
|
-
* @param {PondAssigns} assigns - The data to assign to the client.
|
|
580
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
581
|
-
*/
|
|
582
|
-
assign(assigns: AssignType): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
583
|
-
|
|
584
|
-
/**
|
|
585
|
-
* @desc Emits a direct message to the client.
|
|
586
|
-
* @param {string} event - The event name.
|
|
587
|
-
* @param {PondMessage} payload - The payload to send.
|
|
588
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
589
|
-
*/
|
|
590
|
-
reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
591
|
-
|
|
592
|
-
/**
|
|
593
|
-
* @desc Sends a message to all clients in the channel.
|
|
594
|
-
* @param {string} event - The event to send.
|
|
595
|
-
* @param {PondMessage} payload - The payload to send.
|
|
596
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
597
|
-
*/
|
|
598
|
-
broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
599
|
-
|
|
600
|
-
/**
|
|
601
|
-
* @desc Sends a message to all clients in the channel except the client making the request.
|
|
602
|
-
* @param {string} event - The event to send.
|
|
603
|
-
* @param {PondMessage} payload - The payload to send.
|
|
604
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
605
|
-
*/
|
|
606
|
-
broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
607
|
-
|
|
608
|
-
/**
|
|
609
|
-
* @desc Sends a message to a set of clients in the channel.
|
|
610
|
-
* @param {string} event - The event to send.
|
|
611
|
-
* @param {PondMessage} payload - The payload to send.
|
|
612
|
-
* @param {string | string[]} userIds - The ids of the clients to send the message to.
|
|
613
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
614
|
-
*/
|
|
615
|
-
broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
616
|
-
|
|
617
|
-
/**
|
|
618
|
-
* @desc Tracks a user's presence in the channel.
|
|
619
|
-
* @param {PondPresence} presence - The initial presence data.
|
|
620
|
-
* @param {string} userId - The id of the user to track.
|
|
621
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
622
|
-
*/
|
|
623
|
-
trackPresence(presence: PresenceType, userId?: string): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
624
|
-
|
|
625
|
-
/**
|
|
626
|
-
* @desc Updates a user's presence in the channel.
|
|
627
|
-
* @param {PondPresence} presence - The updated presence data.
|
|
628
|
-
* @param {string} userId - The id of the user to update.
|
|
629
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
630
|
-
*/
|
|
631
|
-
updatePresence(presence: PresenceType, userId?: string): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
632
|
-
|
|
633
|
-
/**
|
|
634
|
-
* @desc Removes a user's presence from the channel.
|
|
635
|
-
* @param {string} userId - The id of the user to remove.
|
|
636
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
637
|
-
*/
|
|
638
|
-
removePresence(userId?: string): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
639
|
-
|
|
640
|
-
/**
|
|
641
|
-
* @desc Evicts a user from the channel.
|
|
642
|
-
* @param {string} reason - The reason for the eviction.
|
|
643
|
-
* @param {string} userId - The id of the user to evict.
|
|
644
|
-
* @returns {EventContext} - The EventContext instance for chaining.
|
|
645
|
-
*/
|
|
646
|
-
evictUser(reason: string, userId?: string): EventContext<Path, EventType, PresenceType, AssignType>;
|
|
110
|
+
export interface UserGetResponse extends DistributedMessage {
|
|
111
|
+
type: DistributedMessageType.USER_GET_RESPONSE;
|
|
112
|
+
lookupRequestId: string;
|
|
113
|
+
userId: string;
|
|
114
|
+
found: boolean;
|
|
115
|
+
assigns?: PondAssigns;
|
|
116
|
+
presence?: PondPresence;
|
|
647
117
|
}
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
PresenceType extends PondPresence = PondPresence,
|
|
652
|
-
AssignType extends PondAssigns = PondAssigns
|
|
653
|
-
> extends BaseContext<Path, PresenceType, AssignType> {
|
|
654
|
-
/**
|
|
655
|
-
* @desc Get the join parameters.
|
|
656
|
-
*/
|
|
657
|
-
get payload(): PondMessage;
|
|
658
|
-
|
|
659
|
-
/**
|
|
660
|
-
* @desc Blocks the outgoing context, preventing further processing of the event.
|
|
661
|
-
*/
|
|
662
|
-
block(): OutgoingContext<Path, PresenceType, AssignType>;
|
|
663
|
-
|
|
664
|
-
/**
|
|
665
|
-
* desc Transforms the outgoing context with a new payload.
|
|
666
|
-
* @param payload - The new payload to set for the context.
|
|
667
|
-
*/
|
|
668
|
-
transform(payload: PondMessage): OutgoingContext<Path, PresenceType, AssignType>;
|
|
118
|
+
export interface NodeHeartbeat extends DistributedMessage {
|
|
119
|
+
type: DistributedMessageType.NODE_HEARTBEAT;
|
|
120
|
+
nodeId: string;
|
|
669
121
|
}
|
|
670
|
-
|
|
671
|
-
export
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
*/
|
|
680
|
-
broadcast (endpointName: string, channelName: string, message: DistributedChannelMessage): Promise<void>;
|
|
681
|
-
|
|
682
|
-
/**
|
|
683
|
-
* @desc Cleans up the distributed backend, closing any connections and cleaning up resources
|
|
684
|
-
*/
|
|
685
|
-
cleanup (): Promise<void>;
|
|
686
|
-
|
|
687
|
-
/**
|
|
688
|
-
* @desc Subscribe to messages for a specific endpoint and channel
|
|
689
|
-
* @param endpointName - The name of the endpoint to subscribe to
|
|
690
|
-
* @param channelName - The name of the channel to subscribe to
|
|
691
|
-
* @param handler - The handler function to call when a message is received
|
|
692
|
-
*/
|
|
693
|
-
subscribe (endpointName: string, channelName: string, handler: (message: DistributedChannelMessage) => void): Unsubscribe;
|
|
122
|
+
export type DistributedChannelMessage = StateRequest | StateResponse | UserJoined | UserLeft | UserMessage | PresenceUpdate | PresenceRemoved | AssignsUpdate | EvictUser | UserRemove | UserGetRequest | UserGetResponse | NodeHeartbeat;
|
|
123
|
+
export interface IDistributedBackend {
|
|
124
|
+
readonly nodeId: string;
|
|
125
|
+
readonly heartbeatTimeoutMs: number;
|
|
126
|
+
initialize(): Promise<void>;
|
|
127
|
+
broadcast(endpointName: string, channelName: string, message: DistributedChannelMessage): Promise<void>;
|
|
128
|
+
subscribeToChannel(endpointName: string, channelName: string, handler: (message: DistributedChannelMessage) => void): Promise<Unsubscribe>;
|
|
129
|
+
subscribeToHeartbeats(handler: (nodeId: string) => void): Unsubscribe;
|
|
130
|
+
cleanup(): Promise<void>;
|
|
694
131
|
}
|