@eleven-am/pondsocket 0.1.169 → 0.1.170
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/middleware.js +40 -17
- package/abstracts/redisClient.js +278 -0
- package/engines/channelEngine.js +163 -0
- package/engines/endpointEngine.js +163 -0
- package/engines/lobbyEngine.js +79 -0
- package/errors/httpError.js +11 -0
- package/index.js +2 -2
- package/managers/distributedManager.js +115 -0
- package/managers/localManager.js +41 -0
- package/managers/manager.js +219 -0
- package/managers/managerFactory.js +31 -0
- package/package.json +24 -14
- package/requests/eventRequest.js +51 -0
- package/requests/joinRequest.js +56 -0
- package/responses/connectionResponse.js +89 -0
- package/{channel → responses}/eventResponse.js +8 -79
- package/{lobby → responses}/joinResponse.js +25 -84
- package/server/{pondSocket.js → server.js} +55 -55
- package/types.d.ts +33 -104
- package/wrappers/channel.js +70 -0
- package/wrappers/endpoint.js +32 -0
- package/wrappers/pondChannel.js +48 -0
- package/abstracts/abstractRequest.js +0 -57
- package/abstracts/abstractRequest.test.js +0 -48
- package/abstracts/middleware.test.js +0 -70
- package/channel/channel.js +0 -351
- package/channel/channel.test.js +0 -435
- package/channel/eventRequest.js +0 -25
- package/channel/eventRequest.test.js +0 -30
- package/channel/eventResponse.test.js +0 -175
- package/endpoint/endpoint.js +0 -299
- package/endpoint/endpoint.test.js +0 -299
- package/endpoint/response.js +0 -129
- package/errors/pondError.js +0 -28
- package/lobby/JoinRequest.test.js +0 -50
- package/lobby/JoinResponse.test.js +0 -142
- package/lobby/joinRequest.js +0 -43
- package/lobby/lobby.js +0 -186
- package/matcher/matcher.test.js +0 -90
- package/presence/presence.js +0 -112
- package/presence/presenceEngine.test.js +0 -129
package/channel/channel.test.js
DELETED
|
@@ -1,435 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createParentEngine = void 0;
|
|
4
|
-
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
5
|
-
const channel_1 = require("./channel");
|
|
6
|
-
const eventRequest_1 = require("./eventRequest");
|
|
7
|
-
const eventResponse_1 = require("./eventResponse");
|
|
8
|
-
const endpoint_test_1 = require("../endpoint/endpoint.test");
|
|
9
|
-
const createParentEngine = () => {
|
|
10
|
-
const socket = {
|
|
11
|
-
clientId: 'test2',
|
|
12
|
-
assigns: { assign: 'assign' },
|
|
13
|
-
channelName: 'channel',
|
|
14
|
-
requestId: 'requestId',
|
|
15
|
-
subscriptions: new Map(),
|
|
16
|
-
socket: {
|
|
17
|
-
send: jest.fn(),
|
|
18
|
-
},
|
|
19
|
-
};
|
|
20
|
-
const lobbyEngineMock = {
|
|
21
|
-
onEvent: jest.fn(),
|
|
22
|
-
onLeave: jest.fn(),
|
|
23
|
-
broadcast: jest.fn(),
|
|
24
|
-
execute: jest.fn(),
|
|
25
|
-
getChannel: jest.fn(),
|
|
26
|
-
destroyChannel: jest.fn(),
|
|
27
|
-
listChannels: jest.fn(),
|
|
28
|
-
createChannel: jest.fn(),
|
|
29
|
-
parent: (0, endpoint_test_1.createEndpointEngine)(socket),
|
|
30
|
-
middleware: {
|
|
31
|
-
run: jest.fn(),
|
|
32
|
-
},
|
|
33
|
-
};
|
|
34
|
-
return {
|
|
35
|
-
parentEngine: lobbyEngineMock,
|
|
36
|
-
socket,
|
|
37
|
-
};
|
|
38
|
-
};
|
|
39
|
-
exports.createParentEngine = createParentEngine;
|
|
40
|
-
describe('ChannelEngine', () => {
|
|
41
|
-
it('should add user to channel', () => {
|
|
42
|
-
const onMessage = jest.fn();
|
|
43
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
44
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
45
|
-
expect(channelEngine.size).toEqual(0);
|
|
46
|
-
expect(channelEngine.getUserData('test')).not.toBeDefined();
|
|
47
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
48
|
-
expect(channelEngine.size).toEqual(1);
|
|
49
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
50
|
-
assigns: { test: 1 },
|
|
51
|
-
presence: {},
|
|
52
|
-
id: 'test',
|
|
53
|
-
});
|
|
54
|
-
});
|
|
55
|
-
it('should throw error if user is already in channel', () => {
|
|
56
|
-
const onMessage = jest.fn();
|
|
57
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
58
|
-
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
59
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
60
|
-
expect(() => {
|
|
61
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
62
|
-
}).toThrow('ChannelEngine: User with id test already exists in channel testChannel');
|
|
63
|
-
});
|
|
64
|
-
it('should throw error if user is not in channel: updateAssigns', () => {
|
|
65
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
66
|
-
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
67
|
-
expect(() => {
|
|
68
|
-
channelEngine.updateAssigns('test', { test: 2 });
|
|
69
|
-
}).toThrow('ChannelEngine: User with id test does not exist in channel testChannel');
|
|
70
|
-
});
|
|
71
|
-
it('should throw error if user is not in channel: removeUser', () => {
|
|
72
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
73
|
-
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
74
|
-
expect(() => {
|
|
75
|
-
channelEngine.kickUser('test', 'test reason');
|
|
76
|
-
}).toThrow('ChannelEngine: Invalid recipients test some users do not exist in channel testChannel');
|
|
77
|
-
});
|
|
78
|
-
it('should update a users assigns', () => {
|
|
79
|
-
const onMessage = jest.fn();
|
|
80
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
81
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
82
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
83
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
84
|
-
assigns: { test: 1 },
|
|
85
|
-
presence: {},
|
|
86
|
-
id: 'test',
|
|
87
|
-
});
|
|
88
|
-
channelEngine.updateAssigns('test', { test: 2 });
|
|
89
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
90
|
-
assigns: { test: 2 },
|
|
91
|
-
presence: {},
|
|
92
|
-
id: 'test',
|
|
93
|
-
});
|
|
94
|
-
});
|
|
95
|
-
it('should be able to get users assigns in the channel', () => {
|
|
96
|
-
const onMessage = jest.fn();
|
|
97
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
98
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
99
|
-
expect(channelEngine.getAssigns()).toEqual({});
|
|
100
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
101
|
-
expect(channelEngine.getAssigns()).toEqual({
|
|
102
|
-
test: { test: 1 },
|
|
103
|
-
});
|
|
104
|
-
channelEngine.addUser('test2', { test: 2 }, onMessage);
|
|
105
|
-
expect(channelEngine.getAssigns()).toEqual({
|
|
106
|
-
test: { test: 1 },
|
|
107
|
-
test2: { test: 2 },
|
|
108
|
-
});
|
|
109
|
-
});
|
|
110
|
-
it('should be able to track presence', () => {
|
|
111
|
-
const onMessage = jest.fn();
|
|
112
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
113
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
114
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
115
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
116
|
-
assigns: { test: 1 },
|
|
117
|
-
presence: {},
|
|
118
|
-
id: 'test',
|
|
119
|
-
});
|
|
120
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
121
|
-
expect(channelEngine.presenceEngine).toBeDefined();
|
|
122
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
123
|
-
assigns: { test: 1 },
|
|
124
|
-
presence: { test: 2 },
|
|
125
|
-
id: 'test',
|
|
126
|
-
});
|
|
127
|
-
});
|
|
128
|
-
it('should throw error if channel is already tracking the users presence', () => {
|
|
129
|
-
const onMessage = jest.fn();
|
|
130
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
131
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
132
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
133
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
134
|
-
expect(() => {
|
|
135
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
136
|
-
}).toThrow('PresenceEngine: Presence with key test already exists');
|
|
137
|
-
});
|
|
138
|
-
it('should be able to list presence', () => {
|
|
139
|
-
var _a;
|
|
140
|
-
const onMessage = jest.fn();
|
|
141
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
142
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
143
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
144
|
-
channelEngine.addUser('test2', { test: 2 }, onMessage);
|
|
145
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
146
|
-
expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
|
|
147
|
-
channelName: 'test',
|
|
148
|
-
action: pondsocket_common_1.ServerActions.PRESENCE,
|
|
149
|
-
event: pondsocket_common_1.PresenceEventTypes.JOIN,
|
|
150
|
-
payload: {
|
|
151
|
-
presence: [{ test: 2 }],
|
|
152
|
-
changed: {
|
|
153
|
-
test: 2,
|
|
154
|
-
},
|
|
155
|
-
},
|
|
156
|
-
}));
|
|
157
|
-
channelEngine.presenceEngine.trackPresence('test2', { test: 3 });
|
|
158
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
159
|
-
test: { test: 2 },
|
|
160
|
-
test2: { test: 3 },
|
|
161
|
-
});
|
|
162
|
-
expect(onMessage).toHaveBeenCalledTimes(5);
|
|
163
|
-
});
|
|
164
|
-
it('should update a users presence', () => {
|
|
165
|
-
var _a;
|
|
166
|
-
const onMessage = jest.fn();
|
|
167
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
168
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
169
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
170
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
171
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
172
|
-
assigns: { test: 1 },
|
|
173
|
-
presence: { test: 2 },
|
|
174
|
-
id: 'test',
|
|
175
|
-
});
|
|
176
|
-
(_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.updatePresence('test', { test: 3 });
|
|
177
|
-
expect(channelEngine.getUserData('test')).toEqual({
|
|
178
|
-
assigns: { test: 1 },
|
|
179
|
-
presence: { test: 3 },
|
|
180
|
-
id: 'test',
|
|
181
|
-
});
|
|
182
|
-
});
|
|
183
|
-
it('should remove user from channel', () => {
|
|
184
|
-
const onMessage = jest.fn();
|
|
185
|
-
const { parentEngine, socket } = (0, exports.createParentEngine)();
|
|
186
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
187
|
-
const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
|
|
188
|
-
socket.subscriptions.set('test', unsub);
|
|
189
|
-
expect(channelEngine.size).toEqual(1);
|
|
190
|
-
expect(channelEngine.getUserData('test1')).toBeDefined();
|
|
191
|
-
channelEngine.kickUser('test1', 'test reason');
|
|
192
|
-
expect(channelEngine.size).toEqual(0);
|
|
193
|
-
expect(channelEngine.getUserData('test')).not.toBeDefined();
|
|
194
|
-
});
|
|
195
|
-
it('should untrack presence when user is removed', () => {
|
|
196
|
-
var _a;
|
|
197
|
-
const onMessage = jest.fn();
|
|
198
|
-
const { parentEngine, socket } = (0, exports.createParentEngine)();
|
|
199
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
200
|
-
const unsub = channelEngine.addUser('test1', { test: 1 }, onMessage);
|
|
201
|
-
socket.subscriptions.set('test', unsub);
|
|
202
|
-
channelEngine.presenceEngine.trackPresence('test1', { test: 2 });
|
|
203
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
204
|
-
channelEngine.presenceEngine.trackPresence('test2', { test: 2 });
|
|
205
|
-
expect(channelEngine.presenceEngine.getPresence()).toEqual({
|
|
206
|
-
test1: { test: 2 },
|
|
207
|
-
test2: { test: 2 },
|
|
208
|
-
});
|
|
209
|
-
onMessage.mockClear();
|
|
210
|
-
channelEngine.kickUser('test1', 'test reason');
|
|
211
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
212
|
-
test2: { test: 2 },
|
|
213
|
-
});
|
|
214
|
-
expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
|
|
215
|
-
channelName: 'test',
|
|
216
|
-
event: pondsocket_common_1.PresenceEventTypes.LEAVE,
|
|
217
|
-
action: pondsocket_common_1.ServerActions.PRESENCE,
|
|
218
|
-
payload: {
|
|
219
|
-
presence: [{ test: 2 }],
|
|
220
|
-
changed: {
|
|
221
|
-
test: 2,
|
|
222
|
-
},
|
|
223
|
-
},
|
|
224
|
-
}));
|
|
225
|
-
});
|
|
226
|
-
it('should throw error if user is not in channel and isPond is false', () => {
|
|
227
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
228
|
-
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
229
|
-
// add a user to the channel
|
|
230
|
-
channelEngine.addUser('test', { test: 1 }, jest.fn());
|
|
231
|
-
// track the added user's presence
|
|
232
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
233
|
-
// we do this because we want to initialize the presence engine
|
|
234
|
-
// the engine is not initialized by default when a channel is created
|
|
235
|
-
expect(() => {
|
|
236
|
-
var _a;
|
|
237
|
-
// now we try to untrack the presence of a user that is not in the channel
|
|
238
|
-
(_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test1');
|
|
239
|
-
}).toThrow('PresenceEngine: Presence with key test1 does not exist');
|
|
240
|
-
});
|
|
241
|
-
it('should not throw error if user is not in channel and isPond is true', () => {
|
|
242
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
243
|
-
const channelEngine = new channel_1.ChannelEngine('testChannel', parentEngine);
|
|
244
|
-
// add a user to the channel
|
|
245
|
-
channelEngine.addUser('test', { test: 1 }, jest.fn());
|
|
246
|
-
// track the added user's presence
|
|
247
|
-
channelEngine.presenceEngine.trackPresence('test', { test: 2 });
|
|
248
|
-
// we do this because we want to initialize the presence engine
|
|
249
|
-
// the engine is not initialized by default when a xhannel is created
|
|
250
|
-
expect(() => {
|
|
251
|
-
var _a;
|
|
252
|
-
// now we try to untrack the presence of a user that is not in the channel
|
|
253
|
-
(_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.removePresence('test');
|
|
254
|
-
}).not.toThrow();
|
|
255
|
-
});
|
|
256
|
-
it('should be able to kick a user from the channel', () => {
|
|
257
|
-
const onMessage = jest.fn();
|
|
258
|
-
const { parentEngine, socket } = (0, exports.createParentEngine)();
|
|
259
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
260
|
-
channelEngine.addUser('test1', { test: 1 }, onMessage);
|
|
261
|
-
const unsub = channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
262
|
-
onMessage.mockClear();
|
|
263
|
-
socket.subscriptions.set('test', unsub);
|
|
264
|
-
channelEngine.kickUser('test1', 'test reason');
|
|
265
|
-
expect(channelEngine.size).toEqual(1);
|
|
266
|
-
expect(channelEngine.getUserData('test2')).not.toBeDefined();
|
|
267
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
|
|
268
|
-
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
269
|
-
channelName: 'test',
|
|
270
|
-
event: 'kicked_out',
|
|
271
|
-
payload: {
|
|
272
|
-
code: 403,
|
|
273
|
-
message: 'test reason',
|
|
274
|
-
},
|
|
275
|
-
}));
|
|
276
|
-
expect(onMessage.mock.calls[1][0]).toStrictEqual(expect.objectContaining({
|
|
277
|
-
action: pondsocket_common_1.ServerActions.SYSTEM,
|
|
278
|
-
channelName: 'test',
|
|
279
|
-
event: 'kicked',
|
|
280
|
-
payload: {
|
|
281
|
-
reason: 'test reason',
|
|
282
|
-
userId: 'test1',
|
|
283
|
-
},
|
|
284
|
-
}));
|
|
285
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
286
|
-
});
|
|
287
|
-
it('should call destroy on parent engine', () => {
|
|
288
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
289
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
290
|
-
channelEngine.destroy('test');
|
|
291
|
-
expect(parentEngine.destroyChannel).toHaveBeenCalled();
|
|
292
|
-
});
|
|
293
|
-
it('should broadcast a message to all users', () => {
|
|
294
|
-
const onMessage = jest.fn();
|
|
295
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
296
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
297
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
298
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
299
|
-
onMessage.mockClear();
|
|
300
|
-
channelEngine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
|
|
301
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
|
|
302
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
303
|
-
channelName: 'test',
|
|
304
|
-
event: 'test',
|
|
305
|
-
payload: {
|
|
306
|
-
test: 2,
|
|
307
|
-
},
|
|
308
|
-
}));
|
|
309
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
310
|
-
onMessage.mockClear();
|
|
311
|
-
channelEngine.sendMessage('test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
312
|
-
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
313
|
-
onMessage.mockClear();
|
|
314
|
-
// when user is not in channel it throws an error
|
|
315
|
-
expect(() => {
|
|
316
|
-
channelEngine.sendMessage('test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
317
|
-
}).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
|
|
318
|
-
});
|
|
319
|
-
it('should broadcast a message to all users except sender', () => {
|
|
320
|
-
const onMessage = jest.fn();
|
|
321
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
322
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
323
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
324
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
325
|
-
onMessage.mockClear();
|
|
326
|
-
channelEngine.sendMessage('test2', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
|
|
327
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
|
|
328
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
329
|
-
channelName: 'test',
|
|
330
|
-
event: 'test',
|
|
331
|
-
payload: {
|
|
332
|
-
test: 2,
|
|
333
|
-
},
|
|
334
|
-
}));
|
|
335
|
-
expect(onMessage).toHaveBeenCalledTimes(1);
|
|
336
|
-
// when user is not in channel it throws an error
|
|
337
|
-
expect(() => {
|
|
338
|
-
channelEngine.sendMessage('test3', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
339
|
-
}).toThrow('ChannelEngine: User with id test3 does not exist in channel test');
|
|
340
|
-
// when sender is channel itself it throws an error
|
|
341
|
-
expect(() => {
|
|
342
|
-
channelEngine.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
343
|
-
}).toThrow(`ChannelEngine: Cannot use ${pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER} with ${pondsocket_common_1.SystemSender.CHANNEL}`);
|
|
344
|
-
});
|
|
345
|
-
it('should broadcast a message to specific users', () => {
|
|
346
|
-
const onMessage = jest.fn();
|
|
347
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
348
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
349
|
-
channelEngine.addUser('test', { test: 1 }, onMessage);
|
|
350
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
351
|
-
channelEngine.addUser('test3', { test: 1 }, onMessage);
|
|
352
|
-
onMessage.mockClear();
|
|
353
|
-
channelEngine.sendMessage('test2', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 2 });
|
|
354
|
-
expect(onMessage.mock.calls[0][0]).toStrictEqual(expect.objectContaining({
|
|
355
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
356
|
-
channelName: 'test',
|
|
357
|
-
event: 'test',
|
|
358
|
-
payload: {
|
|
359
|
-
test: 2,
|
|
360
|
-
},
|
|
361
|
-
}));
|
|
362
|
-
expect(onMessage.mock.calls[1][0]).toStrictEqual(expect.objectContaining({
|
|
363
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
364
|
-
channelName: 'test',
|
|
365
|
-
event: 'test',
|
|
366
|
-
payload: {
|
|
367
|
-
test: 2,
|
|
368
|
-
},
|
|
369
|
-
}));
|
|
370
|
-
expect(onMessage).toHaveBeenCalledTimes(2);
|
|
371
|
-
// when recipient is not in channel it throws an error
|
|
372
|
-
expect(() => {
|
|
373
|
-
channelEngine.sendMessage('test3', ['test', 'test3', 'test4'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
374
|
-
}).toThrow('ChannelEngine: Invalid recipients test,test3,test4 some users do not exist in channel test');
|
|
375
|
-
// when sender is not in channel it throws an error
|
|
376
|
-
expect(() => {
|
|
377
|
-
channelEngine.sendMessage('test4', ['test', 'test3'], pondsocket_common_1.ServerActions.BROADCAST, 'test', { test: 3 });
|
|
378
|
-
}).toThrow('ChannelEngine: User with id test4 does not exist in channel test');
|
|
379
|
-
});
|
|
380
|
-
it('should broadcast messages while also triggering the onMessage callback', () => {
|
|
381
|
-
const onMessage = jest.fn();
|
|
382
|
-
const { parentEngine } = (0, exports.createParentEngine)();
|
|
383
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
384
|
-
parentEngine.middleware.run = (req, res) => {
|
|
385
|
-
expect(req).toBeInstanceOf(eventRequest_1.EventRequest);
|
|
386
|
-
expect(res).toBeInstanceOf(eventResponse_1.EventResponse);
|
|
387
|
-
};
|
|
388
|
-
expect(() => channelEngine.broadcastMessage('test2', {
|
|
389
|
-
action: pondsocket_common_1.ClientActions.BROADCAST,
|
|
390
|
-
channelName: 'test',
|
|
391
|
-
event: 'test',
|
|
392
|
-
payload: { test: 1 },
|
|
393
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
394
|
-
})).toThrow('ChannelEngine: User with id test2 does not exist in channel test');
|
|
395
|
-
channelEngine.addUser('test2', { test: 1 }, onMessage);
|
|
396
|
-
onMessage.mockClear();
|
|
397
|
-
channelEngine.broadcastMessage('test2', {
|
|
398
|
-
action: pondsocket_common_1.ClientActions.BROADCAST,
|
|
399
|
-
channelName: 'test',
|
|
400
|
-
event: 'test',
|
|
401
|
-
payload: { test: 1 },
|
|
402
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
403
|
-
});
|
|
404
|
-
// This is because the message didn't perform any sort of broadcast
|
|
405
|
-
expect(onMessage).toHaveBeenCalledTimes(0);
|
|
406
|
-
let count = 0;
|
|
407
|
-
onMessage.mockClear();
|
|
408
|
-
parentEngine.middleware.run = (req, res, next) => {
|
|
409
|
-
expect(req).toBeInstanceOf(eventRequest_1.EventRequest);
|
|
410
|
-
expect(res).toBeInstanceOf(eventResponse_1.EventResponse);
|
|
411
|
-
count++;
|
|
412
|
-
next();
|
|
413
|
-
};
|
|
414
|
-
channelEngine.broadcastMessage('test2', {
|
|
415
|
-
action: pondsocket_common_1.ClientActions.BROADCAST,
|
|
416
|
-
channelName: 'test',
|
|
417
|
-
event: 'test',
|
|
418
|
-
payload: { test: 1 },
|
|
419
|
-
requestId: (0, pondsocket_common_1.uuid)(),
|
|
420
|
-
});
|
|
421
|
-
// proves that the message was received by the parent engine
|
|
422
|
-
expect(count).toBe(1);
|
|
423
|
-
// however the onMessage callback was not called
|
|
424
|
-
// because the message was never handled by the parent engine
|
|
425
|
-
expect(onMessage).toHaveBeenCalledWith(expect.objectContaining({
|
|
426
|
-
action: pondsocket_common_1.ServerActions.ERROR,
|
|
427
|
-
channelName: 'test',
|
|
428
|
-
event: pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND,
|
|
429
|
-
payload: {
|
|
430
|
-
code: 404,
|
|
431
|
-
message: 'A handler did not respond to the event',
|
|
432
|
-
},
|
|
433
|
-
}));
|
|
434
|
-
});
|
|
435
|
-
});
|
package/channel/eventRequest.js
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.EventRequest = void 0;
|
|
4
|
-
const channel_1 = require("./channel");
|
|
5
|
-
const abstractRequest_1 = require("../abstracts/abstractRequest");
|
|
6
|
-
const pondError_1 = require("../errors/pondError");
|
|
7
|
-
class EventRequest extends abstractRequest_1.AbstractRequest {
|
|
8
|
-
constructor(event, engine) {
|
|
9
|
-
super(event.event, engine, event.payload);
|
|
10
|
-
this._internalEvent = event;
|
|
11
|
-
}
|
|
12
|
-
get user() {
|
|
13
|
-
const assigns = this._engine.getUserData(this._internalEvent.sender);
|
|
14
|
-
if (!assigns) {
|
|
15
|
-
const message = `ChannelRequest: User with id ${this._internalEvent.sender} does not exist in channel ${this._engine.name}`;
|
|
16
|
-
const code = 404;
|
|
17
|
-
throw new pondError_1.ChannelError(message, code, this._engine.name);
|
|
18
|
-
}
|
|
19
|
-
return assigns;
|
|
20
|
-
}
|
|
21
|
-
get channel() {
|
|
22
|
-
return new channel_1.Channel(this._engine);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
exports.EventRequest = EventRequest;
|
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const eventRequest_1 = require("./eventRequest");
|
|
4
|
-
const eventResponse_test_1 = require("./eventResponse.test");
|
|
5
|
-
describe('ChannelRequest', () => {
|
|
6
|
-
it('should create a new ChannelRequest', () => {
|
|
7
|
-
const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
|
|
8
|
-
const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
|
|
9
|
-
const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
|
|
10
|
-
expect(channelRequest).toBeDefined();
|
|
11
|
-
});
|
|
12
|
-
it('should return the payload', () => {
|
|
13
|
-
const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
|
|
14
|
-
const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
|
|
15
|
-
const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
|
|
16
|
-
channelRequest._parseQueries('event');
|
|
17
|
-
expect(channelRequest.event.payload).toEqual(event.payload);
|
|
18
|
-
});
|
|
19
|
-
it('should return the user', () => {
|
|
20
|
-
const { channelEngine } = (0, eventResponse_test_1.createChannelEngine)();
|
|
21
|
-
const event = (0, eventResponse_test_1.createChannelEvent)(channelEngine.name);
|
|
22
|
-
const channelRequest = new eventRequest_1.EventRequest(event, channelEngine);
|
|
23
|
-
// because the user in the event does not exist in the channel, this should throw an error
|
|
24
|
-
expect(() => channelRequest.user).toThrow();
|
|
25
|
-
// add the user to the channel
|
|
26
|
-
channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
|
|
27
|
-
// now the user should be returned
|
|
28
|
-
expect(channelRequest.user).toEqual(channelEngine.getUserData(event.sender));
|
|
29
|
-
});
|
|
30
|
-
});
|
|
@@ -1,175 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createChannelEvent = exports.createChannelEngine = void 0;
|
|
4
|
-
const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
|
|
5
|
-
const channel_1 = require("./channel");
|
|
6
|
-
const channel_test_1 = require("./channel.test");
|
|
7
|
-
const eventResponse_1 = require("./eventResponse");
|
|
8
|
-
const createChannelEngine = () => {
|
|
9
|
-
const { parentEngine, socket } = (0, channel_test_1.createParentEngine)();
|
|
10
|
-
const channelEngine = new channel_1.ChannelEngine('test', parentEngine);
|
|
11
|
-
return {
|
|
12
|
-
channelEngine,
|
|
13
|
-
parentEngine,
|
|
14
|
-
socket,
|
|
15
|
-
};
|
|
16
|
-
};
|
|
17
|
-
exports.createChannelEngine = createChannelEngine;
|
|
18
|
-
const createChannelEvent = (name) => {
|
|
19
|
-
const responseEvent = {
|
|
20
|
-
event: 'event',
|
|
21
|
-
payload: {
|
|
22
|
-
payload: 'payload',
|
|
23
|
-
},
|
|
24
|
-
sender: 'sender',
|
|
25
|
-
action: pondsocket_common_1.ServerActions.BROADCAST,
|
|
26
|
-
channelName: name,
|
|
27
|
-
requestId: 'requestId',
|
|
28
|
-
};
|
|
29
|
-
return responseEvent;
|
|
30
|
-
};
|
|
31
|
-
exports.createChannelEvent = createChannelEvent;
|
|
32
|
-
const createChannelResponse = () => {
|
|
33
|
-
const { channelEngine, socket } = (0, exports.createChannelEngine)();
|
|
34
|
-
const event = (0, exports.createChannelEvent)(channelEngine.name);
|
|
35
|
-
const unsub = channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
|
|
36
|
-
socket.subscriptions.set(channelEngine.name, unsub);
|
|
37
|
-
channelEngine.addUser('tester', { assign: 'assign' }, () => { });
|
|
38
|
-
const response = new eventResponse_1.EventResponse(event, channelEngine);
|
|
39
|
-
return {
|
|
40
|
-
channelEngine,
|
|
41
|
-
event,
|
|
42
|
-
response,
|
|
43
|
-
};
|
|
44
|
-
};
|
|
45
|
-
describe('ChannelResponse', () => {
|
|
46
|
-
it('should create a new ChannelResponse', () => {
|
|
47
|
-
const { response } = createChannelResponse();
|
|
48
|
-
expect(response).toBeDefined();
|
|
49
|
-
});
|
|
50
|
-
it('should send a direct message', () => {
|
|
51
|
-
const { response, channelEngine, event } = createChannelResponse();
|
|
52
|
-
jest.spyOn(channelEngine, 'sendMessage');
|
|
53
|
-
response.reply('event', { payload: 'payload' });
|
|
54
|
-
expect(channelEngine.sendMessage).toHaveBeenCalledWith(pondsocket_common_1.SystemSender.CHANNEL, [event.sender], pondsocket_common_1.ServerActions.SYSTEM, 'event', { payload: 'payload' }, 'requestId');
|
|
55
|
-
});
|
|
56
|
-
it('should broadcast a message', () => {
|
|
57
|
-
const { response, channelEngine } = createChannelResponse();
|
|
58
|
-
jest.spyOn(channelEngine, 'sendMessage');
|
|
59
|
-
response.broadcast('event', { payload: 'payload' });
|
|
60
|
-
expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, 'event', { payload: 'payload' }, 'requestId');
|
|
61
|
-
});
|
|
62
|
-
it('should broadcastFromUser a message', () => {
|
|
63
|
-
const { response, channelEngine } = createChannelResponse();
|
|
64
|
-
jest.spyOn(channelEngine, 'sendMessage');
|
|
65
|
-
response.broadcastFrom('event', { payload: 'payload' });
|
|
66
|
-
expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, 'event', { payload: 'payload' }, 'requestId');
|
|
67
|
-
});
|
|
68
|
-
it('should sendToUsers a message', () => {
|
|
69
|
-
const { response, channelEngine } = createChannelResponse();
|
|
70
|
-
jest.spyOn(channelEngine, 'sendMessage');
|
|
71
|
-
response.broadcastTo('event', { payload: 'payload' }, 'tester');
|
|
72
|
-
expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', ['tester'], pondsocket_common_1.ServerActions.BROADCAST, 'event', { payload: 'payload' }, 'requestId');
|
|
73
|
-
channelEngine.sendMessage.mockClear();
|
|
74
|
-
response.broadcastTo('event', { payload: 'payload' }, ['tester', 'sender']);
|
|
75
|
-
expect(channelEngine.sendMessage).toHaveBeenCalledWith('sender', ['tester', 'sender'], pondsocket_common_1.ServerActions.BROADCAST, 'event', { payload: 'payload' }, 'requestId');
|
|
76
|
-
});
|
|
77
|
-
it('should fail to send to non existing users', () => {
|
|
78
|
-
const { response } = createChannelResponse();
|
|
79
|
-
expect(() => response.broadcastTo('event', { payload: 'payload' }, ['non_existing_user']))
|
|
80
|
-
.toThrow('ChannelEngine: Invalid recipients non_existing_user some users do not exist in channel test');
|
|
81
|
-
});
|
|
82
|
-
it('should track a trackPresence', () => {
|
|
83
|
-
const { response, channelEngine } = createChannelResponse();
|
|
84
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
85
|
-
response.trackPresence({ status: 'online' });
|
|
86
|
-
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
87
|
-
});
|
|
88
|
-
it('should update a users assign data', () => {
|
|
89
|
-
const { response, channelEngine } = createChannelResponse();
|
|
90
|
-
jest.spyOn(channelEngine, 'updateAssigns');
|
|
91
|
-
response.assign({ assign: 'updated' });
|
|
92
|
-
expect(channelEngine.updateAssigns).toHaveBeenCalledWith('sender', { assign: 'updated' });
|
|
93
|
-
});
|
|
94
|
-
it('should evict a user', () => {
|
|
95
|
-
const { response, channelEngine } = createChannelResponse();
|
|
96
|
-
jest.spyOn(channelEngine, 'kickUser');
|
|
97
|
-
response.evictUser('recipient');
|
|
98
|
-
expect(channelEngine.kickUser).toHaveBeenCalledWith('sender', 'recipient');
|
|
99
|
-
});
|
|
100
|
-
it('should destroy the channel', () => {
|
|
101
|
-
const { channelEngine, socket } = (0, exports.createChannelEngine)();
|
|
102
|
-
const event = (0, exports.createChannelEvent)(channelEngine.name);
|
|
103
|
-
const unsub = channelEngine.addUser(event.sender, { assign: 'assign' }, () => { });
|
|
104
|
-
socket.subscriptions.set(channelEngine.name, unsub);
|
|
105
|
-
const response = new eventResponse_1.EventResponse(event, channelEngine);
|
|
106
|
-
jest.spyOn(channelEngine, 'destroy');
|
|
107
|
-
response.closeChannel('recipient');
|
|
108
|
-
expect(channelEngine.destroy).toHaveBeenCalledWith('recipient');
|
|
109
|
-
});
|
|
110
|
-
it('should call the presence engine when the trackPresence is called', () => {
|
|
111
|
-
const { response, channelEngine } = createChannelResponse();
|
|
112
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
113
|
-
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
114
|
-
response.trackPresence({ status: 'online' });
|
|
115
|
-
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
116
|
-
});
|
|
117
|
-
it('should throw an error if trackPresence is called twice', () => {
|
|
118
|
-
const { response, channelEngine } = createChannelResponse();
|
|
119
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
120
|
-
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
121
|
-
response.trackPresence({ status: 'online' });
|
|
122
|
-
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
123
|
-
expect(() => response.trackPresence({ status: 'online' })).toThrow('PresenceEngine: Presence with key sender already exists');
|
|
124
|
-
});
|
|
125
|
-
it('should throw an error if trackPresence is called for a non existing user', () => {
|
|
126
|
-
const { response, channelEngine } = createChannelResponse();
|
|
127
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
128
|
-
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
129
|
-
expect(() => response.trackPresence({ status: 'online' }, 'non_existent_user'))
|
|
130
|
-
.toThrow('ChannelEngine: Invalid recipients non_existent_user some users do not exist in channel test');
|
|
131
|
-
});
|
|
132
|
-
it('should update the presence of a user that is already tracked', () => {
|
|
133
|
-
var _a, _b;
|
|
134
|
-
const { response, channelEngine } = createChannelResponse();
|
|
135
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
136
|
-
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
137
|
-
response.trackPresence({ status: 'online' });
|
|
138
|
-
expect(channelEngine.presenceEngine.trackPresence).toHaveBeenCalledWith('sender', { status: 'online' });
|
|
139
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
140
|
-
sender: { status: 'online' },
|
|
141
|
-
});
|
|
142
|
-
response.updatePresence({ status: 'offline' });
|
|
143
|
-
expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({
|
|
144
|
-
sender: { status: 'offline' },
|
|
145
|
-
});
|
|
146
|
-
});
|
|
147
|
-
it('should throw an error if updatePresence is called for a non existing user', () => {
|
|
148
|
-
const { response, channelEngine } = createChannelResponse();
|
|
149
|
-
jest.spyOn(channelEngine.presenceEngine, 'trackPresence');
|
|
150
|
-
expect(channelEngine.presenceEngine.trackPresence).not.toHaveBeenCalled();
|
|
151
|
-
response.trackPresence({ status: 'online' });
|
|
152
|
-
expect(() => response.updatePresence({ status: 'online' }, 'non_existent_user'))
|
|
153
|
-
.toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
|
|
154
|
-
});
|
|
155
|
-
it('should unTrack a trackPresence', () => {
|
|
156
|
-
var _a, _b;
|
|
157
|
-
const { response, channelEngine } = createChannelResponse();
|
|
158
|
-
response.trackPresence({ status: 'online' });
|
|
159
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
160
|
-
sender: { status: 'online' },
|
|
161
|
-
});
|
|
162
|
-
response.removePresence();
|
|
163
|
-
expect((_b = channelEngine.presenceEngine) === null || _b === void 0 ? void 0 : _b.getPresence()).toEqual({});
|
|
164
|
-
});
|
|
165
|
-
it('should throw an error if unTrackPresence is called for a non existing user', () => {
|
|
166
|
-
var _a;
|
|
167
|
-
const { response, channelEngine } = createChannelResponse();
|
|
168
|
-
response.trackPresence({ status: 'online' });
|
|
169
|
-
expect((_a = channelEngine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()).toEqual({
|
|
170
|
-
sender: { status: 'online' },
|
|
171
|
-
});
|
|
172
|
-
expect(() => response.removePresence('non_existent_user'))
|
|
173
|
-
.toThrow('PresenceEngine: Presence with key non_existent_user does not exist');
|
|
174
|
-
});
|
|
175
|
-
});
|