@eleven-am/pondsocket 0.1.169 → 0.1.171

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.
Files changed (41) hide show
  1. package/abstracts/middleware.js +40 -17
  2. package/abstracts/redisClient.js +278 -0
  3. package/engines/channelEngine.js +163 -0
  4. package/engines/endpointEngine.js +163 -0
  5. package/engines/lobbyEngine.js +79 -0
  6. package/errors/httpError.js +11 -0
  7. package/index.js +2 -2
  8. package/managers/distributedManager.js +115 -0
  9. package/managers/localManager.js +41 -0
  10. package/managers/manager.js +219 -0
  11. package/managers/managerFactory.js +31 -0
  12. package/package.json +24 -14
  13. package/requests/eventRequest.js +51 -0
  14. package/requests/joinRequest.js +56 -0
  15. package/responses/connectionResponse.js +89 -0
  16. package/{channel → responses}/eventResponse.js +8 -79
  17. package/{lobby → responses}/joinResponse.js +25 -84
  18. package/server/{pondSocket.js → server.js} +55 -55
  19. package/types.d.ts +34 -103
  20. package/wrappers/channel.js +70 -0
  21. package/wrappers/endpoint.js +32 -0
  22. package/wrappers/pondChannel.js +48 -0
  23. package/abstracts/abstractRequest.js +0 -57
  24. package/abstracts/abstractRequest.test.js +0 -48
  25. package/abstracts/middleware.test.js +0 -70
  26. package/channel/channel.js +0 -351
  27. package/channel/channel.test.js +0 -435
  28. package/channel/eventRequest.js +0 -25
  29. package/channel/eventRequest.test.js +0 -30
  30. package/channel/eventResponse.test.js +0 -175
  31. package/endpoint/endpoint.js +0 -299
  32. package/endpoint/endpoint.test.js +0 -299
  33. package/endpoint/response.js +0 -129
  34. package/errors/pondError.js +0 -28
  35. package/lobby/JoinRequest.test.js +0 -50
  36. package/lobby/JoinResponse.test.js +0 -142
  37. package/lobby/joinRequest.js +0 -43
  38. package/lobby/lobby.js +0 -186
  39. package/matcher/matcher.test.js +0 -90
  40. package/presence/presence.js +0 -112
  41. package/presence/presenceEngine.test.js +0 -129
@@ -1,4 +1,13 @@
1
1
  "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
2
11
  var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
12
  if (kind === "m") throw new TypeError("Private method is not writable");
4
13
  if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
@@ -10,65 +19,57 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
19
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
20
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
21
  };
13
- var _PondSocket_instances, _PondSocket_server, _PondSocket_socketServer, _PondSocket_middleware, _PondSocket_manageHeartbeat, _PondSocket_init;
22
+ var _PondSocket_instances, _PondSocket_server, _PondSocket_socketServer, _PondSocket_redisClient, _PondSocket_middleware, _PondSocket_manageHeartbeat, _PondSocket_init;
14
23
  Object.defineProperty(exports, "__esModule", { value: true });
15
24
  exports.PondSocket = void 0;
16
25
  const http_1 = require("http");
17
26
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
18
27
  const ws_1 = require("ws");
19
28
  const middleware_1 = require("../abstracts/middleware");
20
- const endpoint_1 = require("../endpoint/endpoint");
21
- const response_1 = require("../endpoint/response");
29
+ const redisClient_1 = require("../abstracts/redisClient");
30
+ const endpointEngine_1 = require("../engines/endpointEngine");
22
31
  const matcher_1 = require("../matcher/matcher");
32
+ const connectionResponse_1 = require("../responses/connectionResponse");
33
+ const endpoint_1 = require("../wrappers/endpoint");
23
34
  class PondSocket {
24
- constructor({ server, socketServer } = {}) {
35
+ constructor({ server, socketServer, redisOptions } = {}) {
25
36
  _PondSocket_instances.add(this);
26
37
  _PondSocket_server.set(this, void 0);
27
38
  _PondSocket_socketServer.set(this, void 0);
39
+ _PondSocket_redisClient.set(this, void 0);
28
40
  _PondSocket_middleware.set(this, void 0);
41
+ __classPrivateFieldSet(this, _PondSocket_middleware, new middleware_1.Middleware(), "f");
29
42
  __classPrivateFieldSet(this, _PondSocket_server, server !== null && server !== void 0 ? server : new http_1.Server(), "f");
43
+ __classPrivateFieldSet(this, _PondSocket_redisClient, redisOptions ? new redisClient_1.RedisClient(redisOptions) : null, "f");
30
44
  __classPrivateFieldSet(this, _PondSocket_socketServer, socketServer !== null && socketServer !== void 0 ? socketServer : new ws_1.WebSocketServer({ noServer: true }), "f");
31
- __classPrivateFieldSet(this, _PondSocket_middleware, new middleware_1.Middleware(), "f");
32
45
  __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_init).call(this);
33
46
  }
34
- /**
35
- * @desc Specifies the port to listen on
36
- * @param args - the arguments to pass to the server
37
- */
38
47
  listen(...args) {
39
- return __classPrivateFieldGet(this, _PondSocket_server, "f").listen(...args);
48
+ if (__classPrivateFieldGet(this, _PondSocket_redisClient, "f")) {
49
+ __classPrivateFieldGet(this, _PondSocket_redisClient, "f").initialize()
50
+ .then(() => __classPrivateFieldGet(this, _PondSocket_server, "f").listen(...args));
51
+ }
52
+ else {
53
+ return __classPrivateFieldGet(this, _PondSocket_server, "f").listen(...args);
54
+ }
40
55
  }
41
- /**
42
- * @desc Closes the server
43
- * @param callback - the callback to call when the server is closed
44
- */
45
56
  close(callback) {
46
57
  return __classPrivateFieldGet(this, _PondSocket_server, "f").close(callback);
47
58
  }
48
- /**
49
- * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
50
- * @param path - the pattern to accept || can also be a regex
51
- * @param handler - the handler function to authenticate the socket
52
- * @example
53
- * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
54
- * const token = req.query.token;
55
- * if (!token)
56
- * return res.reject('No token provided');
57
- * res.accept({
58
- * assign: {
59
- * token
60
- * }
61
- * });
62
- * })
63
- */
64
59
  createEndpoint(path, handler) {
65
- const endpoint = new endpoint_1.EndpointEngine(this);
66
- __classPrivateFieldGet(this, _PondSocket_middleware, "f").use((req, socket, next) => {
60
+ var _a, _b;
61
+ const endpoint = new endpointEngine_1.EndpointEngine((_b = (_a = __classPrivateFieldGet(this, _PondSocket_redisClient, "f")) === null || _a === void 0 ? void 0 : _a.buildClient(String(path))) !== null && _b !== void 0 ? _b : null);
62
+ __classPrivateFieldGet(this, _PondSocket_middleware, "f").use((req, params, next) => {
67
63
  const event = (0, matcher_1.parseAddress)(path, req.address);
68
64
  if (event) {
69
65
  const request = Object.assign(Object.assign({}, event), { headers: req.headers, address: req.address, id: req.id });
70
- const response = new response_1.ConnectionResponse(socket, endpoint, request.id);
71
- return handler(request, response);
66
+ const newParams = {
67
+ params,
68
+ engine: endpoint,
69
+ webSocketServer: __classPrivateFieldGet(this, _PondSocket_socketServer, "f"),
70
+ };
71
+ const response = new connectionResponse_1.ConnectionResponse(request.id, newParams);
72
+ return handler(request, response, next);
72
73
  }
73
74
  return next();
74
75
  });
@@ -76,7 +77,7 @@ class PondSocket {
76
77
  }
77
78
  }
78
79
  exports.PondSocket = PondSocket;
79
- _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _PondSocket_middleware = new WeakMap(), _PondSocket_instances = new WeakSet(), _PondSocket_manageHeartbeat = function _PondSocket_manageHeartbeat() {
80
+ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _PondSocket_redisClient = new WeakMap(), _PondSocket_middleware = new WeakMap(), _PondSocket_instances = new WeakSet(), _PondSocket_manageHeartbeat = function _PondSocket_manageHeartbeat() {
80
81
  __classPrivateFieldGet(this, _PondSocket_socketServer, "f").on('connection', (socket) => {
81
82
  socket.on('pong', () => {
82
83
  socket.isAlive = true;
@@ -93,13 +94,17 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
93
94
  }, 30000);
94
95
  }, _PondSocket_init = function _PondSocket_init() {
95
96
  const timeout = __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
96
- __classPrivateFieldGet(this, _PondSocket_server, "f").on('error', (error) => {
97
+ __classPrivateFieldGet(this, _PondSocket_server, "f").on('error', (error) => __awaiter(this, void 0, void 0, function* () {
98
+ var _a;
97
99
  clearInterval(timeout);
100
+ yield ((_a = __classPrivateFieldGet(this, _PondSocket_redisClient, "f")) === null || _a === void 0 ? void 0 : _a.shutdown());
98
101
  throw new Error(error.message);
99
- });
100
- __classPrivateFieldGet(this, _PondSocket_server, "f").on('close', () => {
102
+ }));
103
+ __classPrivateFieldGet(this, _PondSocket_server, "f").on('close', () => __awaiter(this, void 0, void 0, function* () {
104
+ var _a;
101
105
  clearInterval(timeout);
102
- });
106
+ yield ((_a = __classPrivateFieldGet(this, _PondSocket_redisClient, "f")) === null || _a === void 0 ? void 0 : _a.shutdown());
107
+ }));
103
108
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('upgrade', (req, socket, head) => {
104
109
  const clientId = req.headers['sec-websocket-key'];
105
110
  const request = {
@@ -107,22 +112,17 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
107
112
  headers: req.headers,
108
113
  address: req.url || '',
109
114
  };
110
- __classPrivateFieldGet(this, _PondSocket_socketServer, "f").handleUpgrade(req, socket, head, (socket) => {
111
- __classPrivateFieldGet(this, _PondSocket_socketServer, "f").emit('connection', socket);
112
- __classPrivateFieldGet(this, _PondSocket_middleware, "f").run(request, socket, () => {
113
- const message = {
114
- action: pondsocket_common_1.ServerActions.ERROR,
115
- event: pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND,
116
- channelName: pondsocket_common_1.SystemSender.ENDPOINT,
117
- requestId: (0, pondsocket_common_1.uuid)(),
118
- payload: {
119
- message: 'No endpoint found',
120
- code: 404,
121
- },
122
- };
123
- socket.send(JSON.stringify(message));
124
- socket.close();
125
- });
115
+ const params = {
116
+ head,
117
+ socket,
118
+ request: req,
119
+ requestId: (0, pondsocket_common_1.uuid)(),
120
+ };
121
+ __classPrivateFieldGet(this, _PondSocket_middleware, "f").run(request, params, (error) => {
122
+ const code = (error === null || error === void 0 ? void 0 : error.statusCode) || 400;
123
+ const message = (error === null || error === void 0 ? void 0 : error.message) || 'Unauthorized connection';
124
+ socket.write(`HTTP/1.1 ${code} ${message}\r\n\r\n`);
125
+ socket.destroy();
126
126
  });
127
127
  });
128
128
  };
package/types.d.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { Server as HTTPServer } from 'http';
1
+ import { Server, ServerResponse, IncomingMessage } from 'http';
2
2
 
3
3
  import {
4
4
  IncomingConnection,
5
- JoinParams,
6
5
  PondAssigns,
7
6
  PondEvent,
8
7
  PondEventMap,
@@ -10,11 +9,22 @@ import {
10
9
  PondPath,
11
10
  PondPresence,
12
11
  UserData,
12
+ UserPresences,
13
+ UserAssigns,
14
+ JoinParams,
13
15
  } from '@eleven-am/pondsocket-common';
14
16
  import { WebSocket, WebSocketServer } from 'ws';
15
17
 
16
18
  export * from '@eleven-am/pondsocket-common';
17
19
 
20
+ export interface RedisOptions {
21
+ host: string;
22
+ port: number;
23
+ db?: number;
24
+ username?: string;
25
+ password?: string;
26
+ }
27
+
18
28
  export interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
19
29
  user: UserData<PresenceType, AssignType>;
20
30
  channel: Channel<EventTypes, PresenceType, AssignType>;
@@ -25,7 +35,8 @@ export type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, Presen
25
35
  export type RequestHandler<Request, Response> = (request: Request, response: Response) => void | Promise<void>;
26
36
 
27
37
  export interface PondSocketOptions {
28
- server?: HTTPServer;
38
+ server?: Server;
39
+ redisOptions?: RedisOptions;
29
40
  socketServer?: WebSocketServer;
30
41
  }
31
42
 
@@ -36,13 +47,13 @@ export declare class PondSocket {
36
47
  * @desc Specifies the port to listen on
37
48
  * @param args - the arguments to pass to the server
38
49
  */
39
- listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
50
+ listen(...args: any[]): Server<typeof IncomingMessage, typeof ServerResponse>
40
51
 
41
52
  /**
42
53
  * @desc Closes the server
43
54
  * @param callback - the callback to call when the server is closed
44
55
  */
45
- close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
56
+ close(callback?: () => void): Server<typeof IncomingMessage, typeof ServerResponse>
46
57
 
47
58
  /**
48
59
  * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
@@ -81,14 +92,6 @@ export declare class Endpoint {
81
92
  */
82
93
  createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: RequestHandler<JoinRequest<Path, PresenceType, AssignType>, JoinResponse<EventType, PresenceType, AssignType>>): PondChannel<EventType, PresenceType, AssignType>;
83
94
 
84
- /**
85
- * @desc Broadcasts a message to all clients connected to this endpoint
86
- * @param {string} event - The event to broadcast.
87
- * @param {PondMessage} payload - The payload to broadcast.
88
- * @returns {void}
89
- */
90
- broadcast(event: string, payload: PondMessage): void;
91
-
92
95
  /**
93
96
  * @desc Closes specific clients connected to this endpoint
94
97
  * @param {string | string[]} clientIds - The ids of the clients to close.
@@ -265,52 +268,39 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
265
268
  * @param presence - the presence data to upsert
266
269
  */
267
270
  upsertPresence (userId: string, presence: PresenceType): Channel<EventType, PresenceType, AssignType>;
271
+ }
268
272
 
269
- /**
270
- * @desc Subscribes a user to a channel.
271
- * @param {string} userId - The id of the user.
272
- * @param {string} channel - The channel to subscribe to.
273
- */
274
- subscribeTo(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
273
+ export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
274
+ get event(): PondEvent<Path>;
275
275
 
276
- /**
277
- * @desc Unsubscribes a user from a channel.
278
- * @param {string} userId - The id of the user.
279
- * @param {string} channel - The channel to unsubscribe from.
280
- */
281
- unsubscribeFrom(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
282
- }
276
+ get channelName(): string;
277
+
278
+ get channel(): Channel
283
279
 
284
- export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
285
- event: PondEvent<Path>;
280
+ get joinParams(): JoinParams;
286
281
 
287
- channelName: string;
282
+ get presences(): UserPresences;
288
283
 
289
- assigns: Record<string, AssignType>;
284
+ get assigns(): UserAssigns;
290
285
 
291
- getPresence(): Record<string, PresenceType>;
286
+ get user(): UserData<PresenceType, AssignType>;
292
287
  }
293
288
 
294
- export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
295
- joinParams: JoinParams;
289
+ export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
290
+ get event(): PondEvent<Path>;
296
291
 
297
- user: UserData<PresenceType, AssignType>;
292
+ get channelName(): string;
298
293
 
299
- channel: Channel;
300
- }
294
+ get channel(): Channel;
301
295
 
302
- export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
303
- user: UserData<PresenceType, AssignType>;
296
+ get presences(): UserPresences;
297
+
298
+ get assigns(): UserAssigns;
304
299
 
305
- channel: Channel;
300
+ get user(): UserData<PresenceType, AssignType>;
306
301
  }
307
302
 
308
303
  export declare class ConnectionResponse {
309
- /**
310
- * @desc Whether the server has responded to the request
311
- */
312
- hasResponded: boolean;
313
-
314
304
  /**
315
305
  * @desc Assigns data to the user.
316
306
  * @param {PondAssigns} assigns - The data to assign.
@@ -336,28 +326,9 @@ export declare class ConnectionResponse {
336
326
  * @param {PondMessage} payload - The payload to send
337
327
  */
338
328
  reply(event: string, payload: PondMessage): ConnectionResponse;
339
-
340
- /**
341
- * @desc Subscribes the client to a channel.
342
- * @param {string} channel - The channel to subscribe to.
343
- * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
344
- */
345
- subscribeTo(channel: string): ConnectionResponse;
346
-
347
- /**
348
- * @desc Unsubscribes the client from a channel.
349
- * @param {string} channel - The channel to unsubscribe from.
350
- * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
351
- */
352
- unsubscribeFrom(channel: string): ConnectionResponse;
353
329
  }
354
330
 
355
331
  export declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
356
- /**
357
- * @desc Whether the server has responded to the request
358
- */
359
- hasResponded: boolean;
360
-
361
332
  /**
362
333
  * @desc Assigns data to the client
363
334
  * @param {PondAssigns} assigns - The data to assign
@@ -412,28 +383,9 @@ export declare class JoinResponse<EventType extends PondEventMap = PondEventMap,
412
383
  * @param {PondPresence} presence - the presence data to track
413
384
  */
414
385
  trackPresence(presence: PresenceType): JoinResponse;
415
-
416
- /**
417
- * @desc Subscribes the client to a channel.
418
- * @param {string} channel - The channel to subscribe to.
419
- * @returns {JoinResponse} - The JoinResponse instance for chaining.
420
- */
421
- subscribeTo(channel: string): JoinResponse;
422
-
423
- /**
424
- * @desc Unsubscribes the client from a channel.
425
- * @param {string} channel - The channel to unsubscribe from.
426
- * @returns {JoinResponse} - The JoinResponse instance for chaining.
427
- */
428
- unsubscribeFrom(channel: string): JoinResponse;
429
386
  }
430
387
 
431
388
  export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
432
- /**
433
- * @desc Whether the server has responded to the request
434
- */
435
- hasResponded: boolean;
436
-
437
389
  /**
438
390
  * @desc Assigns data to the client.
439
391
  * @param {PondAssigns} assigns - The data to assign to the client.
@@ -497,20 +449,6 @@ export declare class EventResponse<EventType extends PondEventMap = PondEventMap
497
449
  */
498
450
  removePresence(userId?: string): EventResponse;
499
451
 
500
- /**
501
- * @desc Subscribes the client to a channel.
502
- * @param {string} channel - The channel to subscribe to.
503
- * @returns {EventResponse} - The EventResponse instance for chaining.
504
- */
505
- subscribeTo(channel: string): EventResponse;
506
-
507
- /**
508
- * @desc Unsubscribes the client from a channel.
509
- * @param {string} channel - The channel to unsubscribe from.
510
- * @returns {EventResponse} - The EventResponse instance for chaining.
511
- */
512
- unsubscribeFrom(channel: string): EventResponse;
513
-
514
452
  /**
515
453
  * @desc Evicts a user from the channel.
516
454
  * @param {string} reason - The reason for the eviction.
@@ -518,11 +456,4 @@ export declare class EventResponse<EventType extends PondEventMap = PondEventMap
518
456
  * @returns {EventResponse} - The EventResponse instance for chaining.
519
457
  */
520
458
  evictUser(reason: string, userId?: string): EventResponse;
521
-
522
- /**
523
- * @desc Closes the channel from the server side for all clients.
524
- * @param {string} reason - The reason for closing the channel.
525
- */
526
- closeChannel(reason: string): void;
527
459
  }
528
-
@@ -0,0 +1,70 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _Channel_engine;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Channel = void 0;
16
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
+ class Channel {
18
+ constructor(engine) {
19
+ _Channel_engine.set(this, void 0);
20
+ __classPrivateFieldSet(this, _Channel_engine, engine, "f");
21
+ }
22
+ getUserData(userId) {
23
+ return __classPrivateFieldGet(this, _Channel_engine, "f").getUser(userId);
24
+ }
25
+ getPresence() {
26
+ return __classPrivateFieldGet(this, _Channel_engine, "f").getPresence();
27
+ }
28
+ getAssigns() {
29
+ return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
30
+ }
31
+ broadcast(event, payload) {
32
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
33
+ return this;
34
+ }
35
+ broadcastFrom(userId, event, payload) {
36
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
37
+ return this;
38
+ }
39
+ broadcastTo(userIds, event, payload) {
40
+ const users = Array.isArray(userIds) ? userIds : [userIds];
41
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
42
+ return this;
43
+ }
44
+ evictUser(userId, reason) {
45
+ __classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
46
+ return this;
47
+ }
48
+ trackPresence(userId, presence) {
49
+ __classPrivateFieldGet(this, _Channel_engine, "f").trackPresence(userId, presence);
50
+ return this;
51
+ }
52
+ updatePresence(userId, presence) {
53
+ __classPrivateFieldGet(this, _Channel_engine, "f").updatePresence(userId, presence);
54
+ return this;
55
+ }
56
+ removePresence(userId) {
57
+ __classPrivateFieldGet(this, _Channel_engine, "f").removePresence(userId);
58
+ return this;
59
+ }
60
+ upsertPresence(userId, presence) {
61
+ __classPrivateFieldGet(this, _Channel_engine, "f").upsertPresence(userId, presence);
62
+ return this;
63
+ }
64
+ updateAssigns(userId, assigns) {
65
+ __classPrivateFieldGet(this, _Channel_engine, "f").updateAssigns(userId, assigns);
66
+ return this;
67
+ }
68
+ }
69
+ exports.Channel = Channel;
70
+ _Channel_engine = new WeakMap();
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _Endpoint_engine;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.Endpoint = void 0;
16
+ class Endpoint {
17
+ constructor(engine) {
18
+ _Endpoint_engine.set(this, void 0);
19
+ __classPrivateFieldSet(this, _Endpoint_engine, engine, "f");
20
+ }
21
+ createChannel(path, handler) {
22
+ return __classPrivateFieldGet(this, _Endpoint_engine, "f").createChannel(path, handler);
23
+ }
24
+ closeConnection(clientIds) {
25
+ __classPrivateFieldGet(this, _Endpoint_engine, "f").closeConnection(clientIds);
26
+ }
27
+ getClients() {
28
+ return __classPrivateFieldGet(this, _Endpoint_engine, "f").getClients().map(({ socket }) => socket);
29
+ }
30
+ }
31
+ exports.Endpoint = Endpoint;
32
+ _Endpoint_engine = new WeakMap();
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _PondChannel_lobby;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PondChannel = void 0;
16
+ const channel_1 = require("./channel");
17
+ class PondChannel {
18
+ constructor(lobby) {
19
+ _PondChannel_lobby.set(this, void 0);
20
+ __classPrivateFieldSet(this, _PondChannel_lobby, lobby, "f");
21
+ }
22
+ onEvent(event, handler) {
23
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").onEvent(event, handler);
24
+ return this;
25
+ }
26
+ onLeave(callback) {
27
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").onLeave(callback);
28
+ return this;
29
+ }
30
+ getChannel(channelName) {
31
+ const channel = __classPrivateFieldGet(this, _PondChannel_lobby, "f").getChannel(channelName);
32
+ return new channel_1.Channel(channel);
33
+ }
34
+ broadcast(channelName, event, payload) {
35
+ this.getChannel(channelName).broadcast(event, payload);
36
+ return this;
37
+ }
38
+ broadcastFrom(channelName, userId, event, payload) {
39
+ this.getChannel(channelName).broadcastFrom(userId, event, payload);
40
+ return this;
41
+ }
42
+ broadcastTo(channelName, userIds, event, payload) {
43
+ this.getChannel(channelName).broadcastTo(userIds, event, payload);
44
+ return this;
45
+ }
46
+ }
47
+ exports.PondChannel = PondChannel;
48
+ _PondChannel_lobby = new WeakMap();
@@ -1,57 +0,0 @@
1
- "use strict";
2
- var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
- if (kind === "m") throw new TypeError("Private method is not writable");
4
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
- return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
- };
8
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
10
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
- };
13
- var _AbstractRequest_eventObject, _AbstractRequest_event, _AbstractRequest_payload;
14
- Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.AbstractRequest = void 0;
16
- const matcher_1 = require("../matcher/matcher");
17
- class AbstractRequest {
18
- constructor(event, engine, payload) {
19
- _AbstractRequest_eventObject.set(this, void 0);
20
- _AbstractRequest_event.set(this, void 0);
21
- _AbstractRequest_payload.set(this, void 0);
22
- this._engine = engine;
23
- __classPrivateFieldSet(this, _AbstractRequest_event, event, "f");
24
- __classPrivateFieldSet(this, _AbstractRequest_eventObject, null, "f");
25
- __classPrivateFieldSet(this, _AbstractRequest_payload, payload, "f");
26
- }
27
- get event() {
28
- if (__classPrivateFieldGet(this, _AbstractRequest_eventObject, "f") === null) {
29
- throw new Error('Event was not parsed');
30
- }
31
- return {
32
- event: __classPrivateFieldGet(this, _AbstractRequest_event, "f"),
33
- params: __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f").params || {},
34
- query: __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f").query || {},
35
- payload: __classPrivateFieldGet(this, _AbstractRequest_payload, "f"),
36
- };
37
- }
38
- get channelName() {
39
- return this._engine.name;
40
- }
41
- get assigns() {
42
- return this._engine.getAssigns();
43
- }
44
- getPresence() {
45
- return this._engine.presenceEngine.getPresence();
46
- }
47
- /**
48
- * @desc Parses the event and returns true if the event matches the path
49
- * @param path - the path to match
50
- */
51
- _parseQueries(path) {
52
- __classPrivateFieldSet(this, _AbstractRequest_eventObject, (0, matcher_1.parseAddress)(path, __classPrivateFieldGet(this, _AbstractRequest_event, "f")), "f");
53
- return __classPrivateFieldGet(this, _AbstractRequest_eventObject, "f") !== null;
54
- }
55
- }
56
- exports.AbstractRequest = AbstractRequest;
57
- _AbstractRequest_eventObject = new WeakMap(), _AbstractRequest_event = new WeakMap(), _AbstractRequest_payload = new WeakMap();