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