@eleven-am/pondsocket 0.1.10 → 0.1.12

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 (90) hide show
  1. package/dist/client/channel.js +200 -0
  2. package/dist/client/index.d.ts +122 -0
  3. package/{pondClient/socket.js → dist/client/index.js} +30 -44
  4. package/dist/express/index.d.ts +36 -0
  5. package/dist/express/index.js +16 -0
  6. package/dist/index.d.ts +340 -0
  7. package/dist/index.js +4 -0
  8. package/dist/server/abstracts/abstractRequest.js +40 -0
  9. package/dist/server/abstracts/abstractRequest.test.js +41 -0
  10. package/dist/server/abstracts/abstractResponse.js +6 -0
  11. package/dist/server/abstracts/middleware.js +38 -0
  12. package/dist/server/abstracts/middleware.test.js +70 -0
  13. package/dist/server/channel/channelEngine.js +279 -0
  14. package/dist/server/channel/channelEngine.test.js +377 -0
  15. package/dist/server/channel/channelRequest.test.js +29 -0
  16. package/dist/server/channel/channelResponse.test.js +134 -0
  17. package/dist/server/channel/eventRequest.js +18 -0
  18. package/dist/server/channel/eventResponse.js +141 -0
  19. package/dist/server/endpoint/connectionResponse.js +50 -0
  20. package/dist/server/endpoint/endpoint.js +269 -0
  21. package/dist/server/endpoint/endpoint.test.js +406 -0
  22. package/dist/server/endpoint/endpointResponse.test.js +43 -0
  23. package/dist/server/pondChannel/joinRequest.js +29 -0
  24. package/dist/server/pondChannel/joinResponse.js +96 -0
  25. package/dist/server/pondChannel/pondChannel.js +161 -0
  26. package/dist/server/pondChannel/pondChannelResponse.test.js +108 -0
  27. package/dist/server/presence/presenceEngine.js +112 -0
  28. package/dist/server/presence/presenceEngine.test.js +104 -0
  29. package/dist/server/server/pondSocket.js +122 -0
  30. package/{pondSocket → dist/server/server}/server.test.js +7 -21
  31. package/{pondBase/baseClass.js → dist/server/utils/matchPattern.js} +33 -43
  32. package/{pondBase/baseClass.test.js → dist/server/utils/matchPattern.test.js} +16 -25
  33. package/dist/server/utils/subjectUtils.js +68 -0
  34. package/package.json +28 -12
  35. package/.eslintrc.js +0 -28
  36. package/base.d.ts +0 -1
  37. package/base.js +0 -17
  38. package/client.d.ts +0 -1
  39. package/client.js +0 -17
  40. package/index.d.ts +0 -1
  41. package/index.js +0 -17
  42. package/jest.config.js +0 -11
  43. package/pondBase/baseClass.d.ts +0 -55
  44. package/pondBase/enums.d.ts +0 -9
  45. package/pondBase/enums.js +0 -14
  46. package/pondBase/index.d.ts +0 -6
  47. package/pondBase/index.js +0 -22
  48. package/pondBase/pondBase.d.ts +0 -41
  49. package/pondBase/pondBase.js +0 -60
  50. package/pondBase/pondBase.test.js +0 -101
  51. package/pondBase/pubSub.d.ts +0 -83
  52. package/pondBase/pubSub.js +0 -150
  53. package/pondBase/pubSub.test.js +0 -323
  54. package/pondBase/simpleBase.d.ts +0 -126
  55. package/pondBase/simpleBase.js +0 -211
  56. package/pondBase/simpleBase.test.js +0 -153
  57. package/pondBase/types.d.ts +0 -2
  58. package/pondBase/types.js +0 -2
  59. package/pondClient/channel.d.ts +0 -66
  60. package/pondClient/channel.js +0 -139
  61. package/pondClient/index.d.ts +0 -2
  62. package/pondClient/index.js +0 -18
  63. package/pondClient/socket.d.ts +0 -41
  64. package/pondSocket/channel.d.ts +0 -129
  65. package/pondSocket/channel.js +0 -287
  66. package/pondSocket/channel.test.js +0 -377
  67. package/pondSocket/channelMiddleWare.d.ts +0 -28
  68. package/pondSocket/channelMiddleWare.js +0 -36
  69. package/pondSocket/endpoint.d.ts +0 -90
  70. package/pondSocket/endpoint.js +0 -320
  71. package/pondSocket/endpoint.test.js +0 -490
  72. package/pondSocket/enums.d.ts +0 -19
  73. package/pondSocket/enums.js +0 -25
  74. package/pondSocket/index.d.ts +0 -7
  75. package/pondSocket/index.js +0 -23
  76. package/pondSocket/pondChannel.d.ts +0 -79
  77. package/pondSocket/pondChannel.js +0 -211
  78. package/pondSocket/pondChannel.test.js +0 -430
  79. package/pondSocket/pondResponse.d.ts +0 -25
  80. package/pondSocket/pondResponse.js +0 -120
  81. package/pondSocket/pondSocket.d.ts +0 -47
  82. package/pondSocket/pondSocket.js +0 -94
  83. package/pondSocket/socketMiddleWare.d.ts +0 -6
  84. package/pondSocket/socketMiddleWare.js +0 -32
  85. package/pondSocket/types.d.ts +0 -74
  86. package/pondSocket/types.js +0 -2
  87. package/socket.d.ts +0 -1
  88. package/socket.js +0 -17
  89. package/tsconfig.eslint.json +0 -5
  90. package/tsconfig.json +0 -90
@@ -0,0 +1,406 @@
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
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ const superwstest_1 = __importDefault(require("superwstest"));
16
+ const endpoint_1 = require("./endpoint");
17
+ const pondChannel_1 = require("../pondChannel/pondChannel");
18
+ const pondSocket_1 = require("../server/pondSocket");
19
+ const createPondSocket = () => {
20
+ const mock = jest.fn();
21
+ const socket = new pondSocket_1.PondSocket();
22
+ const server = socket.listen(3001, mock);
23
+ const createPondChannel = () => new pondChannel_1.PondChannel();
24
+ return { socket,
25
+ server,
26
+ mock,
27
+ createPondChannel };
28
+ };
29
+ /* eslint-disable line-comment-position, no-inline-comments */
30
+ describe('endpoint', () => {
31
+ it('should be able to close a socket', () => __awaiter(void 0, void 0, void 0, function* () {
32
+ const { socket, server } = createPondSocket();
33
+ expect(server).toBeDefined();
34
+ const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
35
+ expect(req.params.path).toBe('socket');
36
+ res.accept();
37
+ setTimeout(() => {
38
+ endpoint.closeConnection(req.id);
39
+ }, 100);
40
+ });
41
+ yield (0, superwstest_1.default)(server)
42
+ .ws('/api/socket')
43
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
44
+ .wait(200)
45
+ .expectClosed();
46
+ server.close();
47
+ }));
48
+ it('should be able to list connections', () => __awaiter(void 0, void 0, void 0, function* () {
49
+ const { socket, server } = createPondSocket();
50
+ let connectionsCount = 0;
51
+ expect(server).toBeDefined();
52
+ const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
53
+ expect(req.params.path).toBe('socket');
54
+ connectionsCount = endpoint.listConnections().length;
55
+ res.accept();
56
+ });
57
+ yield (0, superwstest_1.default)(server)
58
+ .ws('/api/socket')
59
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101));
60
+ yield (0, superwstest_1.default)(server)
61
+ .ws('/api/socket')
62
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101));
63
+ server.close(); // Close the server to stop the connection from being kept alive
64
+ expect(connectionsCount).toBe(1);
65
+ expect(endpoint.listConnections().length).toBe(2); // The connections are still in the list
66
+ }));
67
+ it('should be able to refuse connections to the endpoint', () => __awaiter(void 0, void 0, void 0, function* () {
68
+ const { socket, server } = createPondSocket();
69
+ expect(server).toBeDefined();
70
+ socket.createEndpoint('/api/:path', (req, res) => {
71
+ expect(req.params.path).toBe('socket');
72
+ res.reject();
73
+ });
74
+ yield (0, superwstest_1.default)(server)
75
+ .ws('/api/socket')
76
+ .expectConnectionError(); // The connection should be refused
77
+ server.close();
78
+ }));
79
+ it('should be capable of sending messages to all clients', () => __awaiter(void 0, void 0, void 0, function* () {
80
+ const { socket, server } = createPondSocket();
81
+ expect(server).toBeDefined();
82
+ let users = 0;
83
+ const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
84
+ users++;
85
+ res.send('Hello', { room: req.params.room });
86
+ if (users > 0) {
87
+ endpoint.broadcast('TEST', { message: 'Hello everyone' });
88
+ }
89
+ });
90
+ yield (0, superwstest_1.default)(server)
91
+ .ws('/api/socket')
92
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
93
+ .expectJson({
94
+ event: 'Hello',
95
+ channelName: 'SERVER',
96
+ payload: {
97
+ room: 'socket',
98
+ },
99
+ })
100
+ .expectJson({
101
+ event: 'TEST',
102
+ channelName: 'SERVER',
103
+ payload: {
104
+ message: 'Hello everyone',
105
+ },
106
+ })
107
+ .close()
108
+ .expectClosed();
109
+ yield (0, superwstest_1.default)(server)
110
+ .ws('/api/secondSocket')
111
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
112
+ .expectJson({
113
+ event: 'Hello',
114
+ channelName: 'SERVER',
115
+ payload: {
116
+ room: 'secondSocket',
117
+ },
118
+ })
119
+ .expectJson({
120
+ event: 'TEST',
121
+ channelName: 'SERVER',
122
+ payload: {
123
+ message: 'Hello everyone',
124
+ },
125
+ })
126
+ .close()
127
+ .expectClosed();
128
+ server.close();
129
+ }));
130
+ it('should be able to accept connections on this handler', () => __awaiter(void 0, void 0, void 0, function* () {
131
+ const message = {
132
+ action: endpoint_1.ClientActions.JOIN_CHANNEL,
133
+ channelName: '/test/socket',
134
+ event: 'TEST',
135
+ payload: {},
136
+ };
137
+ const { socket, server, createPondChannel } = createPondSocket();
138
+ expect(server).toBeDefined();
139
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
140
+ res.accept();
141
+ });
142
+ const testPond = createPondChannel();
143
+ const socketPond = createPondChannel();
144
+ testPond.onJoinRequest((req, res) => {
145
+ expect(req.event.params.room).toBeDefined();
146
+ res.accept({
147
+ assigns: {
148
+ status: 'online',
149
+ },
150
+ });
151
+ });
152
+ socketPond.onJoinRequest((req, res) => {
153
+ expect(req.event.params.room).toBeDefined();
154
+ res.accept({
155
+ assigns: {
156
+ status: 'online socket',
157
+ },
158
+ });
159
+ });
160
+ endpoint.useChannel('/test/:room', testPond);
161
+ endpoint.useChannel('/socket/:room', socketPond);
162
+ yield (0, superwstest_1.default)(server)
163
+ .ws('/api/socket')
164
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
165
+ .sendJson(message)
166
+ .close()
167
+ .expectClosed();
168
+ yield (0, superwstest_1.default)(server)
169
+ .ws('/api/socket')
170
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
171
+ .sendJson(Object.assign(Object.assign({}, message), { channelName: '/socket/socket' }))
172
+ .close()
173
+ .expectClosed();
174
+ expect(endpoint['_channels']).toHaveLength(2);
175
+ server.close();
176
+ }));
177
+ it('should refuse connections if there are no pondChannel handlers', () => __awaiter(void 0, void 0, void 0, function* () {
178
+ const { socket, server, createPondChannel } = createPondSocket();
179
+ expect(server).toBeDefined();
180
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
181
+ res.accept();
182
+ });
183
+ const testPond = createPondChannel();
184
+ testPond.onJoinRequest((req, res) => {
185
+ expect(req.event.params.room).toBeDefined();
186
+ res.accept({
187
+ assigns: {
188
+ status: 'online',
189
+ },
190
+ });
191
+ });
192
+ endpoint.useChannel('/test/:room', testPond);
193
+ const message = {
194
+ action: endpoint_1.ClientActions.JOIN_CHANNEL,
195
+ channelName: '/test/socket',
196
+ event: 'TEST',
197
+ payload: {},
198
+ };
199
+ yield (0, superwstest_1.default)(server)
200
+ .ws('/api/socket')
201
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
202
+ .sendJson(message)
203
+ .close()
204
+ .expectClosed();
205
+ yield (0, superwstest_1.default)(server)
206
+ .ws('/api/socket')
207
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
208
+ .sendJson(Object.assign(Object.assign({}, message), { channelName: '/socket/socket' }))
209
+ .expectJson({
210
+ event: 'error',
211
+ channelName: 'ENDPOINT',
212
+ payload: {
213
+ message: 'GatewayEngine: Channel /socket/socket does not exist',
214
+ },
215
+ })
216
+ .close()
217
+ .expectClosed();
218
+ server.close();
219
+ expect(endpoint['_channels']).toHaveLength(1);
220
+ }));
221
+ it('should send an error when the channel exists but other things happen', () => __awaiter(void 0, void 0, void 0, function* () {
222
+ const { socket, server, createPondChannel } = createPondSocket();
223
+ expect(server).toBeDefined();
224
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
225
+ res.accept();
226
+ });
227
+ const channel = createPondChannel();
228
+ channel.onEvent(':room', (req, res) => {
229
+ if (req.event.params.room === 'TEST') {
230
+ res.accept().broadcast(req.event.event, req.event.payload);
231
+ }
232
+ else if (req.event.params.room === 'TEST2') {
233
+ res.reject();
234
+ }
235
+ else {
236
+ res.reject('choke on my balls');
237
+ }
238
+ });
239
+ channel.onJoinRequest((_, res) => {
240
+ res.accept();
241
+ });
242
+ endpoint.useChannel('/test/:room', channel);
243
+ const message = {
244
+ action: endpoint_1.ClientActions.JOIN_CHANNEL,
245
+ channelName: '/test/socket',
246
+ event: 'TEST',
247
+ payload: {},
248
+ };
249
+ yield (0, superwstest_1.default)(server)
250
+ .ws('/api/socket')
251
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
252
+ .sendJson(message)
253
+ .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST2', action: endpoint_1.ClientActions.BROADCAST }))
254
+ .expectJson({
255
+ event: 'error_channel',
256
+ channelName: '/test/socket',
257
+ payload: {
258
+ message: 'Unauthorized request',
259
+ code: 403,
260
+ },
261
+ })
262
+ .sendJson(Object.assign(Object.assign({}, message), { channelName: '/test/socket', action: endpoint_1.ClientActions.BROADCAST }))
263
+ .expectJson({
264
+ payload: {},
265
+ event: 'TEST',
266
+ channelName: '/test/socket',
267
+ })
268
+ .sendJson(Object.assign(Object.assign({}, message), { event: 'TEST3', action: endpoint_1.ClientActions.BROADCAST }))
269
+ .expectJson({
270
+ event: 'error_channel',
271
+ channelName: '/test/socket',
272
+ payload: {
273
+ message: 'choke on my balls',
274
+ code: 403,
275
+ },
276
+ })
277
+ .close()
278
+ .expectClosed();
279
+ server.close();
280
+ expect(endpoint['_channels']).toHaveLength(1);
281
+ }));
282
+ it('should be able to track the presence of its users', () => __awaiter(void 0, void 0, void 0, function* () {
283
+ const { socket, server, createPondChannel } = createPondSocket();
284
+ expect(server).toBeDefined();
285
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
286
+ res.accept();
287
+ });
288
+ const channel = createPondChannel();
289
+ channel.onJoinRequest((_, res) => {
290
+ res.accept().trackPresence({
291
+ status: 'online',
292
+ });
293
+ });
294
+ endpoint.useChannel('/test/:room', channel);
295
+ const message = {
296
+ action: endpoint_1.ClientActions.JOIN_CHANNEL,
297
+ channelName: '/test/socket',
298
+ event: 'TEST',
299
+ payload: {},
300
+ };
301
+ yield (0, superwstest_1.default)(server)
302
+ .ws('/api/socket')
303
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
304
+ .sendJson(message)
305
+ .expectJson({
306
+ event: 'presence_change',
307
+ channelName: '/test/socket',
308
+ payload: {
309
+ type: 'join',
310
+ changed: {
311
+ status: 'online',
312
+ },
313
+ presence: [
314
+ {
315
+ status: 'online',
316
+ },
317
+ ],
318
+ },
319
+ })
320
+ .close()
321
+ .expectClosed();
322
+ server.close();
323
+ expect(endpoint['_channels']).toHaveLength(1);
324
+ }));
325
+ it('should throw an error when we try to leave a channel that does not exist', () => __awaiter(void 0, void 0, void 0, function* () {
326
+ const { socket, server, createPondChannel } = createPondSocket();
327
+ expect(server).toBeDefined();
328
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
329
+ res.accept();
330
+ });
331
+ const channel = createPondChannel();
332
+ channel.onJoinRequest((_, res) => {
333
+ res.send('TEST', { test: 'test' });
334
+ });
335
+ endpoint.useChannel('/test/:room', channel);
336
+ const message = {
337
+ action: endpoint_1.ClientActions.LEAVE_CHANNEL,
338
+ channelName: '/test/socket',
339
+ event: 'TEST',
340
+ payload: {},
341
+ };
342
+ yield (0, superwstest_1.default)(server)
343
+ .ws('/api/socket')
344
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
345
+ .sendJson(message)
346
+ .expectJson({
347
+ event: 'error',
348
+ channelName: 'ENDPOINT',
349
+ payload: {
350
+ message: 'GatewayEngine: Channel /test/socket does not exist',
351
+ },
352
+ })
353
+ // now join the channel
354
+ .sendJson(Object.assign(Object.assign({}, message), { action: endpoint_1.ClientActions.JOIN_CHANNEL }))
355
+ .expectJson({
356
+ event: 'TEST',
357
+ channelName: '/test/socket',
358
+ payload: {
359
+ test: 'test',
360
+ },
361
+ })
362
+ // now leave the channel
363
+ .sendJson(message)
364
+ .close()
365
+ .expectClosed();
366
+ server.close();
367
+ expect(endpoint['_channels']).toHaveLength(1);
368
+ // every pond channel can potentially hold a lot of channels
369
+ // remember that the path a pond channel is created with is a regex pattern
370
+ // and every channel that matches that pattern will be added to the pond channel
371
+ // e.g. /test/:room will match /test/1, /test/2, /test/3, /test/4, ... etc
372
+ // But also when a channel no longer has any users, it will be removed from the pond channel
373
+ expect(channel['_channels'].size).toEqual(0);
374
+ }));
375
+ it('should be able to close multiple sockets', () => __awaiter(void 0, void 0, void 0, function* () {
376
+ const { socket, server } = createPondSocket();
377
+ expect(server).toBeDefined();
378
+ const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
379
+ res.send('TEST', { test: 'test' });
380
+ const sockets = endpoint.listConnections();
381
+ endpoint.closeConnection(sockets);
382
+ });
383
+ yield (0, superwstest_1.default)(server)
384
+ .ws('/api/socket')
385
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
386
+ .expectJson({
387
+ event: 'TEST',
388
+ channelName: 'SERVER',
389
+ payload: {
390
+ test: 'test',
391
+ },
392
+ })
393
+ .expectClosed();
394
+ yield (0, superwstest_1.default)(server)
395
+ .ws('/api/socket')
396
+ .expectUpgrade((res) => expect(res.statusCode).toBe(101))
397
+ .expectJson({
398
+ event: 'TEST',
399
+ channelName: 'SERVER',
400
+ payload: {
401
+ test: 'test',
402
+ },
403
+ })
404
+ .expectClosed();
405
+ }));
406
+ });
@@ -0,0 +1,43 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const connectionResponse_1 = require("./connectionResponse");
4
+ describe('EndpointResponse', () => {
5
+ it('should be able to accept', () => {
6
+ const handler = jest.fn();
7
+ const response = new connectionResponse_1.ConnectionResponse(handler);
8
+ response.accept();
9
+ expect(handler).toBeCalledWith({}, {});
10
+ });
11
+ it('should be able to reject', () => {
12
+ const handler = jest.fn();
13
+ const response = new connectionResponse_1.ConnectionResponse(handler);
14
+ response.reject();
15
+ expect(handler).toBeCalledWith({}, { error: { message: 'Message rejected',
16
+ code: 403 } });
17
+ });
18
+ it('should be able to send', () => {
19
+ const handler = jest.fn();
20
+ const response = new connectionResponse_1.ConnectionResponse(handler);
21
+ response.send('test', { test: 'test' });
22
+ expect(handler).toBeCalledWith({}, { message: { event: 'test',
23
+ payload: { test: 'test' } } });
24
+ });
25
+ it('should throw if accept is called twice', () => {
26
+ const handler = jest.fn();
27
+ const response = new connectionResponse_1.ConnectionResponse(handler);
28
+ response.accept();
29
+ expect(() => response.accept()).toThrow();
30
+ });
31
+ it('should throw if reject is called twice', () => {
32
+ const handler = jest.fn();
33
+ const response = new connectionResponse_1.ConnectionResponse(handler);
34
+ response.reject();
35
+ expect(() => response.reject()).toThrow();
36
+ });
37
+ it('should throw if send is called twice', () => {
38
+ const handler = jest.fn();
39
+ const response = new connectionResponse_1.ConnectionResponse(handler);
40
+ response.send('test', { test: 'test' });
41
+ expect(() => response.send('test', { test: 'test' })).toThrow();
42
+ });
43
+ });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JoinRequest = void 0;
4
+ const abstractRequest_1 = require("../abstracts/abstractRequest");
5
+ class JoinRequest extends abstractRequest_1.AbstractRequest {
6
+ constructor(event, engine) {
7
+ super(event.channelName, engine, event.joinParams);
8
+ this._requestCache = event;
9
+ }
10
+ get joinParams() {
11
+ return this._requestCache.joinParams;
12
+ }
13
+ get user() {
14
+ return {
15
+ id: this._requestCache.clientId,
16
+ assigns: this._requestCache.assigns,
17
+ presence: {},
18
+ };
19
+ }
20
+ get event() {
21
+ return {
22
+ event: this._requestCache.channelName,
23
+ params: this._requestCache.params,
24
+ query: this._requestCache.query,
25
+ payload: this._requestCache.joinParams,
26
+ };
27
+ }
28
+ }
29
+ exports.JoinRequest = JoinRequest;
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JoinResponse = void 0;
4
+ const abstractResponse_1 = require("../abstracts/abstractResponse");
5
+ const channelEngine_1 = require("../channel/channelEngine");
6
+ class JoinResponse extends abstractResponse_1.PondResponse {
7
+ constructor(user, engine) {
8
+ super();
9
+ this._user = user;
10
+ this._engine = engine;
11
+ this._hasExecuted = false;
12
+ }
13
+ get responseSent() {
14
+ return this._hasExecuted;
15
+ }
16
+ /**
17
+ * @desc Accepts the request and optionally assigns data to the client
18
+ * @param assigns - the data to assign to the client
19
+ */
20
+ accept(assigns) {
21
+ this._hasExecuted = true;
22
+ assigns = Object.assign(Object.assign({}, assigns), this._user.assigns);
23
+ this._engine.addUser(this._user.clientId, assigns, (event) => {
24
+ this._user.socket.send(JSON.stringify(event));
25
+ });
26
+ return this;
27
+ }
28
+ /**
29
+ * @desc Rejects the request and optionally assigns data to the client
30
+ * @param message - the error message
31
+ * @param errorCode - the error code
32
+ */
33
+ reject(message, errorCode) {
34
+ this._hasExecuted = true;
35
+ const text = `Request to join channel ${this._engine.name} rejected: ${message || 'Unauthorized request'}`;
36
+ const errorMessage = {
37
+ event: 'POND_ERROR',
38
+ payload: {
39
+ message: text,
40
+ code: errorCode || 403,
41
+ },
42
+ channelName: this._engine.name,
43
+ action: channelEngine_1.ServerActions.ERROR,
44
+ };
45
+ this._user.socket.send(JSON.stringify(errorMessage));
46
+ return this;
47
+ }
48
+ /**
49
+ * @desc Emits a direct message to the client
50
+ * @param event - the event name
51
+ * @param payload - the payload to send
52
+ * @param assigns - the data to assign to the client
53
+ */
54
+ send(event, payload, assigns) {
55
+ this.accept(assigns);
56
+ this._engine.sendMessage('channel', [this._user.clientId], channelEngine_1.ServerActions.SYSTEM, event, payload);
57
+ return this;
58
+ }
59
+ /**
60
+ * @desc Emits a message to all clients in the channel
61
+ * @param event - the event name
62
+ * @param payload - the payload to send
63
+ */
64
+ broadcast(event, payload) {
65
+ this._engine.sendMessage(this._user.clientId, 'all_users', channelEngine_1.ServerActions.BROADCAST, event, payload);
66
+ return this;
67
+ }
68
+ /**
69
+ * @desc Emits a message to all clients in the channel except the sender
70
+ * @param event - the event name
71
+ * @param payload - the payload to send
72
+ */
73
+ broadcastFromUser(event, payload) {
74
+ this._engine.sendMessage(this._user.clientId, 'all_except_sender', channelEngine_1.ServerActions.BROADCAST, event, payload);
75
+ return this;
76
+ }
77
+ /**
78
+ * @desc Emits a message to a specific set of clients
79
+ * @param event - the event name
80
+ * @param payload - the payload to send
81
+ * @param userIds - the ids of the clients to send the message to
82
+ */
83
+ sendToUsers(event, payload, userIds) {
84
+ this._engine.sendMessage(this._user.clientId, userIds, channelEngine_1.ServerActions.BROADCAST, event, payload);
85
+ return this;
86
+ }
87
+ /**
88
+ * @desc tracks the presence of a client
89
+ * @param presence - the presence data to track
90
+ */
91
+ trackPresence(presence) {
92
+ this._engine.trackPresence(this._user.clientId, presence);
93
+ return this;
94
+ }
95
+ }
96
+ exports.JoinResponse = JoinResponse;