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