@eleven-am/pondsocket 0.1.57 → 0.1.58
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/package.json +3 -3
- package/.eslintrc.json +0 -387
- package/dist/LICENSE +0 -674
- package/dist/README.md +0 -139
- package/dist/package.json +0 -51
- package/jest.config.js +0 -11
- package/src/abstracts/abstractRequest.test.ts +0 -49
- package/src/abstracts/abstractRequest.ts +0 -56
- package/src/abstracts/abstractResponse.ts +0 -26
- package/src/abstracts/middleware.test.ts +0 -75
- package/src/abstracts/middleware.ts +0 -50
- package/src/channel/channel.test.ts +0 -501
- package/src/channel/channel.ts +0 -305
- package/src/channel/eventRequest.test.ts +0 -37
- package/src/channel/eventRequest.ts +0 -27
- package/src/channel/eventResponse.test.ts +0 -249
- package/src/channel/eventResponse.ts +0 -172
- package/src/client/channel.test.ts +0 -799
- package/src/client/channel.ts +0 -342
- package/src/client.ts +0 -124
- package/src/endpoint/endpoint.test.ts +0 -825
- package/src/endpoint/endpoint.ts +0 -304
- package/src/endpoint/response.ts +0 -106
- package/src/enums.ts +0 -52
- package/src/errors/pondError.ts +0 -32
- package/src/express.ts +0 -58
- package/src/index.ts +0 -3
- package/src/lobby/JoinRequest.test.ts +0 -48
- package/src/lobby/JoinResponse.test.ts +0 -162
- package/src/lobby/joinRequest.ts +0 -32
- package/src/lobby/joinResponse.ts +0 -146
- package/src/lobby/lobby.ts +0 -182
- package/src/matcher/matcher.test.ts +0 -103
- package/src/matcher/matcher.ts +0 -105
- package/src/node.ts +0 -33
- package/src/presence/presence.ts +0 -127
- package/src/presence/presenceEngine.test.ts +0 -143
- package/src/server/pondSocket.ts +0 -153
- package/src/subjects/subject.test.ts +0 -163
- package/src/subjects/subject.ts +0 -137
- package/src/typedefs.d.ts +0 -451
- package/src/types.d.ts +0 -89
- package/tsconfig.build.json +0 -7
- package/tsconfig.json +0 -12
- /package/{dist/abstracts → abstracts}/abstractRequest.js +0 -0
- /package/{dist/abstracts → abstracts}/abstractResponse.js +0 -0
- /package/{dist/abstracts → abstracts}/middleware.js +0 -0
- /package/{dist/channel → channel}/channel.js +0 -0
- /package/{dist/channel → channel}/eventRequest.js +0 -0
- /package/{dist/channel → channel}/eventResponse.js +0 -0
- /package/{dist/client → client}/channel.js +0 -0
- /package/{dist/client.d.ts → client.d.ts} +0 -0
- /package/{dist/client.js → client.js} +0 -0
- /package/{dist/endpoint → endpoint}/endpoint.js +0 -0
- /package/{dist/endpoint → endpoint}/response.js +0 -0
- /package/{dist/enums.js → enums.js} +0 -0
- /package/{dist/errors → errors}/pondError.js +0 -0
- /package/{dist/express.d.ts → express.d.ts} +0 -0
- /package/{dist/express.js → express.js} +0 -0
- /package/{dist/index.d.ts → index.d.ts} +0 -0
- /package/{dist/index.js → index.js} +0 -0
- /package/{dist/lobby → lobby}/joinRequest.js +0 -0
- /package/{dist/lobby → lobby}/joinResponse.js +0 -0
- /package/{dist/lobby → lobby}/lobby.js +0 -0
- /package/{dist/matcher → matcher}/matcher.js +0 -0
- /package/{dist/node.d.ts → node.d.ts} +0 -0
- /package/{dist/node.js → node.js} +0 -0
- /package/{dist/presence → presence}/presence.js +0 -0
- /package/{dist/server → server}/pondSocket.js +0 -0
- /package/{dist/subjects → subjects}/subject.js +0 -0
- /package/{dist/types.d.ts → types.d.ts} +0 -0
|
@@ -1,825 +0,0 @@
|
|
|
1
|
-
import request from 'superwstest';
|
|
2
|
-
|
|
3
|
-
import { SystemSender, ServerActions, ErrorTypes, ClientActions, Events, ChannelReceiver } from '../enums';
|
|
4
|
-
import { PondSocket } from '../server/pondSocket';
|
|
5
|
-
|
|
6
|
-
/* eslint-disable line-comment-position, no-inline-comments */
|
|
7
|
-
|
|
8
|
-
describe('endpoint', () => {
|
|
9
|
-
let socket: PondSocket;
|
|
10
|
-
let server: any;
|
|
11
|
-
|
|
12
|
-
beforeEach((done) => {
|
|
13
|
-
socket = new PondSocket();
|
|
14
|
-
server = socket.listen(3000, 'localhost', done);
|
|
15
|
-
});
|
|
16
|
-
|
|
17
|
-
afterEach((done) => {
|
|
18
|
-
server.close(done);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
/* it('should be able to close a socket', async () => {
|
|
22
|
-
const { socket, server } = createPondSocket();
|
|
23
|
-
|
|
24
|
-
expect(server).toBeDefined();
|
|
25
|
-
const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
|
|
26
|
-
expect(req.params.path).toBe('socket');
|
|
27
|
-
res.accept();
|
|
28
|
-
|
|
29
|
-
setTimeout(() => {
|
|
30
|
-
endpoint.closeConnection(req.id);
|
|
31
|
-
}, 100);
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
await request(server)
|
|
35
|
-
.ws('/api/socket')
|
|
36
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
37
|
-
.wait(200)
|
|
38
|
-
.expectClosed();
|
|
39
|
-
|
|
40
|
-
server.close();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should be able to list connections', async () => {
|
|
44
|
-
const { socket, server } = createPondSocket();
|
|
45
|
-
let connectionsCount = 0;
|
|
46
|
-
|
|
47
|
-
expect(server).toBeDefined();
|
|
48
|
-
const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
|
|
49
|
-
expect(req.params.path).toBe('socket');
|
|
50
|
-
connectionsCount = endpoint.listConnections().length;
|
|
51
|
-
res.accept();
|
|
52
|
-
});
|
|
53
|
-
|
|
54
|
-
await request(server)
|
|
55
|
-
.ws('/api/socket')
|
|
56
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101));
|
|
57
|
-
|
|
58
|
-
await request(server)
|
|
59
|
-
.ws('/api/socket')
|
|
60
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101));
|
|
61
|
-
|
|
62
|
-
server.close(); // Close the server to stop the connection from being kept alive
|
|
63
|
-
expect(connectionsCount).toBe(1);
|
|
64
|
-
expect(endpoint.listConnections().length).toBe(2); // The connections are still in the list
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
it('should be able to refuse connections to the endpoint', async () => {
|
|
68
|
-
const { socket, server } = createPondSocket();
|
|
69
|
-
|
|
70
|
-
expect(server).toBeDefined();
|
|
71
|
-
socket.createEndpoint('/api/:path', (req, res) => {
|
|
72
|
-
expect(req.params.path).toBe('socket');
|
|
73
|
-
res.reject();
|
|
74
|
-
});
|
|
75
|
-
|
|
76
|
-
await request(server)
|
|
77
|
-
.ws('/api/socket')
|
|
78
|
-
.expectConnectionError(); // The connection should be refused
|
|
79
|
-
|
|
80
|
-
server.close();
|
|
81
|
-
});
|
|
82
|
-
|
|
83
|
-
it('should be capable of sending messages to all clients', async () => {
|
|
84
|
-
const { socket, server } = createPondSocket();
|
|
85
|
-
|
|
86
|
-
expect(server).toBeDefined();
|
|
87
|
-
|
|
88
|
-
let users = 0;
|
|
89
|
-
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
90
|
-
users++;
|
|
91
|
-
res.send('Hello', { room: req.params.room });
|
|
92
|
-
if (users > 0) {
|
|
93
|
-
endpoint.broadcast('TEST', { message: 'Hello everyone' });
|
|
94
|
-
}
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
await request(server)
|
|
98
|
-
.ws('/api/socket')
|
|
99
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
100
|
-
.expectJson({
|
|
101
|
-
event: 'Hello',
|
|
102
|
-
channelName: SystemSender.ENDPOINT,
|
|
103
|
-
action: ServerActions.SYSTEM,
|
|
104
|
-
payload: {
|
|
105
|
-
room: 'socket',
|
|
106
|
-
},
|
|
107
|
-
})
|
|
108
|
-
.expectJson({
|
|
109
|
-
event: 'TEST',
|
|
110
|
-
channelName: SystemSender.ENDPOINT,
|
|
111
|
-
action: ServerActions.BROADCAST,
|
|
112
|
-
payload: {
|
|
113
|
-
message: 'Hello everyone',
|
|
114
|
-
},
|
|
115
|
-
})
|
|
116
|
-
.close()
|
|
117
|
-
.expectClosed();
|
|
118
|
-
|
|
119
|
-
await request(server)
|
|
120
|
-
.ws('/api/secondSocket')
|
|
121
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
122
|
-
.expectJson({
|
|
123
|
-
event: 'Hello',
|
|
124
|
-
channelName: SystemSender.ENDPOINT,
|
|
125
|
-
action: ServerActions.SYSTEM,
|
|
126
|
-
payload: {
|
|
127
|
-
room: 'secondSocket',
|
|
128
|
-
},
|
|
129
|
-
})
|
|
130
|
-
.expectJson({
|
|
131
|
-
event: 'TEST',
|
|
132
|
-
channelName: SystemSender.ENDPOINT,
|
|
133
|
-
action: ServerActions.BROADCAST,
|
|
134
|
-
payload: {
|
|
135
|
-
message: 'Hello everyone',
|
|
136
|
-
},
|
|
137
|
-
})
|
|
138
|
-
.close()
|
|
139
|
-
.expectClosed();
|
|
140
|
-
|
|
141
|
-
server.close();
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
it('should be able to accept connections on this handler', async () => {
|
|
145
|
-
const message: ClientMessage = {
|
|
146
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
147
|
-
channelName: '/test/socket',
|
|
148
|
-
event: 'TEST',
|
|
149
|
-
payload: {},
|
|
150
|
-
};
|
|
151
|
-
|
|
152
|
-
const { socket, server, createPondChannel } = createPondSocket();
|
|
153
|
-
|
|
154
|
-
expect(server).toBeDefined();
|
|
155
|
-
|
|
156
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
157
|
-
res.accept();
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
const testPond = createPondChannel();
|
|
161
|
-
const socketPond = createPondChannel();
|
|
162
|
-
|
|
163
|
-
testPond.onJoinRequest((req, res) => {
|
|
164
|
-
expect(req.event.params.room).toBeDefined();
|
|
165
|
-
res.accept({
|
|
166
|
-
assigns: {
|
|
167
|
-
status: 'online',
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
});
|
|
171
|
-
|
|
172
|
-
socketPond.onJoinRequest((req, res) => {
|
|
173
|
-
expect(req.event.params.room).toBeDefined();
|
|
174
|
-
res.accept({
|
|
175
|
-
assigns: {
|
|
176
|
-
status: 'online socket',
|
|
177
|
-
},
|
|
178
|
-
});
|
|
179
|
-
});
|
|
180
|
-
|
|
181
|
-
endpoint.addChannel('/test/:room', testPond);
|
|
182
|
-
endpoint.addChannel('/socket/:room', socketPond);
|
|
183
|
-
|
|
184
|
-
await request(server)
|
|
185
|
-
.ws('/api/socket')
|
|
186
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
187
|
-
.sendJson(message)
|
|
188
|
-
.close()
|
|
189
|
-
.expectClosed();
|
|
190
|
-
|
|
191
|
-
await request(server)
|
|
192
|
-
.ws('/api/socket')
|
|
193
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
194
|
-
.sendJson({
|
|
195
|
-
...message,
|
|
196
|
-
channelName: '/socket/socket',
|
|
197
|
-
})
|
|
198
|
-
.close()
|
|
199
|
-
.expectClosed();
|
|
200
|
-
|
|
201
|
-
expect(endpoint['_channels']).toHaveLength(2);
|
|
202
|
-
server.close();
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('should refuse connections if there are no pondChannel handlers', async () => {
|
|
206
|
-
const { socket, server, createPondChannel } = createPondSocket();
|
|
207
|
-
|
|
208
|
-
expect(server).toBeDefined();
|
|
209
|
-
|
|
210
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
211
|
-
res.accept();
|
|
212
|
-
});
|
|
213
|
-
|
|
214
|
-
const testPond = createPondChannel();
|
|
215
|
-
|
|
216
|
-
testPond.onJoinRequest((req, res) => {
|
|
217
|
-
expect(req.event.params.room).toBeDefined();
|
|
218
|
-
res.accept({
|
|
219
|
-
assigns: {
|
|
220
|
-
status: 'online',
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
endpoint.addChannel('/test/:room', testPond);
|
|
226
|
-
|
|
227
|
-
const message: ClientMessage = {
|
|
228
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
229
|
-
channelName: '/test/socket',
|
|
230
|
-
event: 'TEST',
|
|
231
|
-
payload: {},
|
|
232
|
-
};
|
|
233
|
-
|
|
234
|
-
await request(server)
|
|
235
|
-
.ws('/api/socket')
|
|
236
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
237
|
-
.sendJson(message)
|
|
238
|
-
.close()
|
|
239
|
-
.expectClosed();
|
|
240
|
-
|
|
241
|
-
await request(server)
|
|
242
|
-
.ws('/api/socket')
|
|
243
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
244
|
-
.sendJson({
|
|
245
|
-
...message,
|
|
246
|
-
channelName: '/socket/socket', // This channel handler does not exist
|
|
247
|
-
})
|
|
248
|
-
.expectJson({
|
|
249
|
-
event: ErrorTypes.INTERNAL_SERVER_ERROR,
|
|
250
|
-
channelName: SystemSender.ENDPOINT,
|
|
251
|
-
action: ServerActions.ERROR,
|
|
252
|
-
payload: {
|
|
253
|
-
message: 'GatewayEngine: Channel /socket/socket does not exist',
|
|
254
|
-
},
|
|
255
|
-
})
|
|
256
|
-
.close()
|
|
257
|
-
.expectClosed();
|
|
258
|
-
|
|
259
|
-
server.close();
|
|
260
|
-
expect(endpoint['_channels']).toHaveLength(1);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it('should send an error when the channel exists but other things happen', async () => {
|
|
264
|
-
const { socket, server, createPondChannel } = createPondSocket();
|
|
265
|
-
|
|
266
|
-
expect(server).toBeDefined();
|
|
267
|
-
|
|
268
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
269
|
-
res.accept();
|
|
270
|
-
});
|
|
271
|
-
|
|
272
|
-
const channel = createPondChannel();
|
|
273
|
-
|
|
274
|
-
channel.onEvent(':room', (req, res) => {
|
|
275
|
-
if (req.event.params.room === 'TEST') {
|
|
276
|
-
res.accept()
|
|
277
|
-
.broadcast(req.event.event, req.event.payload);
|
|
278
|
-
} else if (req.event.params.room === 'TEST2') {
|
|
279
|
-
res.reject();
|
|
280
|
-
} else {
|
|
281
|
-
res.reject('choke on my balls');
|
|
282
|
-
}
|
|
283
|
-
});
|
|
284
|
-
|
|
285
|
-
channel.onJoinRequest((_, res) => {
|
|
286
|
-
res.accept();
|
|
287
|
-
});
|
|
288
|
-
|
|
289
|
-
endpoint.addChannel('/test/:room', channel);
|
|
290
|
-
|
|
291
|
-
const message: ClientMessage = {
|
|
292
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
293
|
-
channelName: '/test/socket',
|
|
294
|
-
event: 'TEST',
|
|
295
|
-
payload: {},
|
|
296
|
-
};
|
|
297
|
-
|
|
298
|
-
await request(server)
|
|
299
|
-
.ws('/api/socket')
|
|
300
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
301
|
-
.sendJson(message) // Join the channel
|
|
302
|
-
.sendJson({
|
|
303
|
-
...message,
|
|
304
|
-
channelName: '/test/socket',
|
|
305
|
-
action: ClientActions.BROADCAST,
|
|
306
|
-
})
|
|
307
|
-
.expectJson({
|
|
308
|
-
payload: {},
|
|
309
|
-
event: 'TEST',
|
|
310
|
-
channelName: '/test/socket',
|
|
311
|
-
action: ServerActions.BROADCAST,
|
|
312
|
-
})
|
|
313
|
-
.expectJson({
|
|
314
|
-
payload: {},
|
|
315
|
-
event: 'TEST',
|
|
316
|
-
channelName: '/test/socket',
|
|
317
|
-
action: ServerActions.BROADCAST,
|
|
318
|
-
})
|
|
319
|
-
.sendJson({
|
|
320
|
-
...message,
|
|
321
|
-
event: 'TEST2',
|
|
322
|
-
channelName: '/test/socket',
|
|
323
|
-
action: ClientActions.BROADCAST,
|
|
324
|
-
})
|
|
325
|
-
.expectJson({
|
|
326
|
-
action: ServerActions.ERROR,
|
|
327
|
-
event: ErrorTypes.UNAUTHORIZED_BROADCAST,
|
|
328
|
-
payload: {
|
|
329
|
-
message: 'Unauthorized request',
|
|
330
|
-
code: 403,
|
|
331
|
-
},
|
|
332
|
-
channelName: '/test/socket',
|
|
333
|
-
})
|
|
334
|
-
.sendJson({
|
|
335
|
-
...message,
|
|
336
|
-
event: 'TEST3',
|
|
337
|
-
channelName: '/test/socket',
|
|
338
|
-
action: ClientActions.BROADCAST,
|
|
339
|
-
})
|
|
340
|
-
.expectJson({
|
|
341
|
-
action: ServerActions.ERROR,
|
|
342
|
-
event: ErrorTypes.UNAUTHORIZED_BROADCAST,
|
|
343
|
-
payload: {
|
|
344
|
-
message: 'choke on my balls',
|
|
345
|
-
code: 403,
|
|
346
|
-
},
|
|
347
|
-
channelName: '/test/socket',
|
|
348
|
-
})
|
|
349
|
-
.close()
|
|
350
|
-
.expectClosed();
|
|
351
|
-
|
|
352
|
-
server.close();
|
|
353
|
-
expect(endpoint['_channels']).toHaveLength(1);
|
|
354
|
-
});
|
|
355
|
-
|
|
356
|
-
it('should be able to track the presence of its users', async () => {
|
|
357
|
-
const { socket, server, createPondChannel } = createPondSocket();
|
|
358
|
-
|
|
359
|
-
expect(server).toBeDefined();
|
|
360
|
-
|
|
361
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
362
|
-
res.accept();
|
|
363
|
-
});
|
|
364
|
-
|
|
365
|
-
const channel = createPondChannel();
|
|
366
|
-
|
|
367
|
-
channel.onJoinRequest((_, res) => {
|
|
368
|
-
res.accept().trackPresence({
|
|
369
|
-
status: 'online',
|
|
370
|
-
});
|
|
371
|
-
});
|
|
372
|
-
|
|
373
|
-
endpoint.addChannel('/test/:room', channel);
|
|
374
|
-
|
|
375
|
-
const message: ClientMessage = {
|
|
376
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
377
|
-
channelName: '/test/socket',
|
|
378
|
-
event: 'TEST',
|
|
379
|
-
payload: {},
|
|
380
|
-
};
|
|
381
|
-
|
|
382
|
-
await request(server)
|
|
383
|
-
.ws('/api/socket')
|
|
384
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
385
|
-
.sendJson(message)
|
|
386
|
-
.expectJson({
|
|
387
|
-
event: PresenceEventTypes.JOIN,
|
|
388
|
-
channelName: '/test/socket',
|
|
389
|
-
action: ServerActions.PRESENCE,
|
|
390
|
-
payload: {
|
|
391
|
-
changed: {
|
|
392
|
-
status: 'online',
|
|
393
|
-
},
|
|
394
|
-
presence: [
|
|
395
|
-
{
|
|
396
|
-
status: 'online',
|
|
397
|
-
},
|
|
398
|
-
],
|
|
399
|
-
},
|
|
400
|
-
})
|
|
401
|
-
.close()
|
|
402
|
-
.expectClosed();
|
|
403
|
-
|
|
404
|
-
server.close();
|
|
405
|
-
expect(endpoint['_channels']).toHaveLength(1);
|
|
406
|
-
});
|
|
407
|
-
|
|
408
|
-
it('should throw an error when we try to leave a channel that does not exist', async () => {
|
|
409
|
-
const { socket, server, createPondChannel } = createPondSocket();
|
|
410
|
-
|
|
411
|
-
expect(server).toBeDefined();
|
|
412
|
-
|
|
413
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
414
|
-
res.accept();
|
|
415
|
-
});
|
|
416
|
-
|
|
417
|
-
const channel = createPondChannel();
|
|
418
|
-
|
|
419
|
-
channel.onJoinRequest((_, res) => {
|
|
420
|
-
res.send('TEST', { test: 'test' });
|
|
421
|
-
});
|
|
422
|
-
|
|
423
|
-
endpoint.addChannel('/test/:room', channel);
|
|
424
|
-
|
|
425
|
-
const message: ClientMessage = {
|
|
426
|
-
action: ClientActions.LEAVE_CHANNEL,
|
|
427
|
-
channelName: '/test/socket',
|
|
428
|
-
event: 'TEST',
|
|
429
|
-
payload: {},
|
|
430
|
-
};
|
|
431
|
-
|
|
432
|
-
await request(server)
|
|
433
|
-
.ws('/api/socket')
|
|
434
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
435
|
-
.sendJson(message)
|
|
436
|
-
.expectJson({
|
|
437
|
-
event: ErrorTypes.INTERNAL_SERVER_ERROR,
|
|
438
|
-
channelName: 'ENDPOINT',
|
|
439
|
-
action: ServerActions.ERROR,
|
|
440
|
-
payload: {
|
|
441
|
-
message: 'GatewayEngine: Channel /test/socket does not exist',
|
|
442
|
-
},
|
|
443
|
-
})
|
|
444
|
-
// now join the channel
|
|
445
|
-
.sendJson({
|
|
446
|
-
...message,
|
|
447
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
448
|
-
})
|
|
449
|
-
.expectJson({
|
|
450
|
-
event: 'TEST',
|
|
451
|
-
channelName: '/test/socket',
|
|
452
|
-
action: ServerActions.SYSTEM,
|
|
453
|
-
payload: {
|
|
454
|
-
test: 'test',
|
|
455
|
-
},
|
|
456
|
-
})
|
|
457
|
-
// now leave the channel
|
|
458
|
-
.sendJson(message)
|
|
459
|
-
.close()
|
|
460
|
-
.expectClosed();
|
|
461
|
-
|
|
462
|
-
server.close();
|
|
463
|
-
expect(endpoint['_channels']).toHaveLength(1);
|
|
464
|
-
|
|
465
|
-
// every pond channel can potentially hold a lot of channels
|
|
466
|
-
// remember that the path a pond channel is created with is a regex pattern
|
|
467
|
-
// and every channel that matches that pattern will be added to the pond channel
|
|
468
|
-
// e.g. /test/:room will match /test/1, /test/2, /test/3, /test/4, ... etc
|
|
469
|
-
// But also when a channel no longer has any users, it will be removed from the pond channel
|
|
470
|
-
expect(channel['_channels'].size).toEqual(0);
|
|
471
|
-
});
|
|
472
|
-
|
|
473
|
-
it('should be able to close multiple sockets', async () => {
|
|
474
|
-
const { socket, server } = createPondSocket();
|
|
475
|
-
|
|
476
|
-
expect(server).toBeDefined();
|
|
477
|
-
|
|
478
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
479
|
-
res.send('TEST', { test: 'test' });
|
|
480
|
-
|
|
481
|
-
const sockets = endpoint.listConnections();
|
|
482
|
-
|
|
483
|
-
endpoint.closeConnection(sockets);
|
|
484
|
-
});
|
|
485
|
-
|
|
486
|
-
await request(server)
|
|
487
|
-
.ws('/api/socket')
|
|
488
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
489
|
-
.expectJson({
|
|
490
|
-
event: 'TEST',
|
|
491
|
-
channelName: SystemSender.ENDPOINT,
|
|
492
|
-
action: ServerActions.SYSTEM,
|
|
493
|
-
payload: {
|
|
494
|
-
test: 'test',
|
|
495
|
-
},
|
|
496
|
-
})
|
|
497
|
-
.expectClosed();
|
|
498
|
-
|
|
499
|
-
await request(server)
|
|
500
|
-
.ws('/api/socket')
|
|
501
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
502
|
-
.expectJson({
|
|
503
|
-
event: 'TEST',
|
|
504
|
-
channelName: SystemSender.ENDPOINT,
|
|
505
|
-
action: ServerActions.SYSTEM,
|
|
506
|
-
payload: {
|
|
507
|
-
test: 'test',
|
|
508
|
-
},
|
|
509
|
-
})
|
|
510
|
-
.expectClosed();
|
|
511
|
-
|
|
512
|
-
server.close();
|
|
513
|
-
});*/
|
|
514
|
-
|
|
515
|
-
it('should be able to close a single socket', async () => {
|
|
516
|
-
let count = 0;
|
|
517
|
-
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
518
|
-
if (req.params.room === 'socket') {
|
|
519
|
-
res.accept();
|
|
520
|
-
count++;
|
|
521
|
-
|
|
522
|
-
setTimeout(() => {
|
|
523
|
-
endpoint.closeConnection(req.id);
|
|
524
|
-
}, 1000);
|
|
525
|
-
} else {
|
|
526
|
-
res.reject();
|
|
527
|
-
}
|
|
528
|
-
});
|
|
529
|
-
|
|
530
|
-
await request(server)
|
|
531
|
-
.ws('/api/socket')
|
|
532
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
533
|
-
.wait(200)
|
|
534
|
-
.expectClosed();
|
|
535
|
-
|
|
536
|
-
expect(count).toBe(1);
|
|
537
|
-
});
|
|
538
|
-
|
|
539
|
-
it('should be able to list connections', async () => {
|
|
540
|
-
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
541
|
-
if (req.params.room === 'socket') {
|
|
542
|
-
res.accept();
|
|
543
|
-
|
|
544
|
-
const connections = endpoint.listConnections();
|
|
545
|
-
|
|
546
|
-
expect(connections).toHaveLength(1);
|
|
547
|
-
} else {
|
|
548
|
-
res.reject();
|
|
549
|
-
}
|
|
550
|
-
});
|
|
551
|
-
|
|
552
|
-
await request(server)
|
|
553
|
-
.ws('/api/socket')
|
|
554
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101));
|
|
555
|
-
});
|
|
556
|
-
|
|
557
|
-
it('should be able to refuse connections to the endpoint', async () => {
|
|
558
|
-
let count = 0;
|
|
559
|
-
|
|
560
|
-
socket.createEndpoint('/api/:path', (req, res) => {
|
|
561
|
-
count++;
|
|
562
|
-
expect(req.params.path).toBe('socket');
|
|
563
|
-
res.reject();
|
|
564
|
-
});
|
|
565
|
-
|
|
566
|
-
await request(server)
|
|
567
|
-
.ws('/api/socket')
|
|
568
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101)) // the connection is still upgraded, so we can send error messages
|
|
569
|
-
.expectJson({
|
|
570
|
-
action: ServerActions.ERROR,
|
|
571
|
-
event: ErrorTypes.UNAUTHORIZED_CONNECTION,
|
|
572
|
-
channelName: SystemSender.ENDPOINT,
|
|
573
|
-
payload: {
|
|
574
|
-
message: 'Unauthorized connection',
|
|
575
|
-
code: 401,
|
|
576
|
-
},
|
|
577
|
-
})
|
|
578
|
-
.expectClosed();
|
|
579
|
-
|
|
580
|
-
expect(count).toBe(1);
|
|
581
|
-
});
|
|
582
|
-
|
|
583
|
-
it('should be able to send a message to all connection', async () => {
|
|
584
|
-
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
585
|
-
if (req.params.room === 'socket') {
|
|
586
|
-
res.accept();
|
|
587
|
-
|
|
588
|
-
const connections = endpoint.listConnections();
|
|
589
|
-
|
|
590
|
-
expect(connections).toHaveLength(1);
|
|
591
|
-
|
|
592
|
-
endpoint.broadcast('TEST', { test: 'test' });
|
|
593
|
-
} else {
|
|
594
|
-
res.reject();
|
|
595
|
-
}
|
|
596
|
-
});
|
|
597
|
-
|
|
598
|
-
await request(server)
|
|
599
|
-
.ws('/api/socket')
|
|
600
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
601
|
-
.expectJson({
|
|
602
|
-
event: 'TEST',
|
|
603
|
-
action: ServerActions.BROADCAST,
|
|
604
|
-
channelName: SystemSender.ENDPOINT,
|
|
605
|
-
payload: {
|
|
606
|
-
test: 'test',
|
|
607
|
-
},
|
|
608
|
-
});
|
|
609
|
-
});
|
|
610
|
-
|
|
611
|
-
it('should be able to accept connections on this handler', async () => {
|
|
612
|
-
const message = {
|
|
613
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
614
|
-
channelName: '/test/socket',
|
|
615
|
-
event: 'TEST',
|
|
616
|
-
payload: {},
|
|
617
|
-
};
|
|
618
|
-
|
|
619
|
-
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
620
|
-
if (req.params.room === 'socket') {
|
|
621
|
-
res.accept();
|
|
622
|
-
} else {
|
|
623
|
-
res.reject();
|
|
624
|
-
}
|
|
625
|
-
});
|
|
626
|
-
|
|
627
|
-
endpoint.createChannel('/test/:room', (req, res) => {
|
|
628
|
-
expect(req.event.params.room).toBeDefined();
|
|
629
|
-
res.accept({
|
|
630
|
-
assigns: {
|
|
631
|
-
status: 'online',
|
|
632
|
-
},
|
|
633
|
-
});
|
|
634
|
-
});
|
|
635
|
-
|
|
636
|
-
await request(server)
|
|
637
|
-
.ws('/api/socket')
|
|
638
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
639
|
-
.sendJson(message)
|
|
640
|
-
.close()
|
|
641
|
-
.expectClosed();
|
|
642
|
-
|
|
643
|
-
await request(server)
|
|
644
|
-
.ws('/api/socket')
|
|
645
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
646
|
-
.sendJson({
|
|
647
|
-
...message,
|
|
648
|
-
channelName: '/socket/socket',
|
|
649
|
-
})
|
|
650
|
-
.expectJson({
|
|
651
|
-
action: ServerActions.ERROR,
|
|
652
|
-
channelName: SystemSender.ENDPOINT,
|
|
653
|
-
event: ErrorTypes.ENDPOINT_ERROR,
|
|
654
|
-
payload: {
|
|
655
|
-
message: 'GatewayEngine: Channel /socket/socket does not exist',
|
|
656
|
-
code: 404,
|
|
657
|
-
},
|
|
658
|
-
})
|
|
659
|
-
.close()
|
|
660
|
-
.expectClosed();
|
|
661
|
-
});
|
|
662
|
-
|
|
663
|
-
it('should send an error when the channel exists but other things happen', async () => {
|
|
664
|
-
const message = {
|
|
665
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
666
|
-
channelName: '/test/socket',
|
|
667
|
-
event: 'TEST',
|
|
668
|
-
payload: {},
|
|
669
|
-
};
|
|
670
|
-
|
|
671
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
672
|
-
res.accept();
|
|
673
|
-
});
|
|
674
|
-
|
|
675
|
-
endpoint.createChannel('/test/:room', (req, res) => {
|
|
676
|
-
expect(req.event.params.room).toBeDefined();
|
|
677
|
-
res.reject('Something went wrong');
|
|
678
|
-
});
|
|
679
|
-
|
|
680
|
-
await request(server)
|
|
681
|
-
.ws('/api/socket')
|
|
682
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
683
|
-
.sendJson(message)
|
|
684
|
-
.expectJson({
|
|
685
|
-
action: ServerActions.ERROR,
|
|
686
|
-
channelName: '/test/socket',
|
|
687
|
-
event: ErrorTypes.UNAUTHORIZED_JOIN_REQUEST,
|
|
688
|
-
payload: {
|
|
689
|
-
message: 'Request to join channel /test/socket rejected: Something went wrong',
|
|
690
|
-
code: 403,
|
|
691
|
-
},
|
|
692
|
-
})
|
|
693
|
-
.close()
|
|
694
|
-
.expectClosed();
|
|
695
|
-
});
|
|
696
|
-
|
|
697
|
-
it('should be able to track the presence of its users', async () => {
|
|
698
|
-
const message = {
|
|
699
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
700
|
-
channelName: '/test/socket',
|
|
701
|
-
event: 'TEST',
|
|
702
|
-
payload: {},
|
|
703
|
-
};
|
|
704
|
-
|
|
705
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
706
|
-
res.accept();
|
|
707
|
-
});
|
|
708
|
-
|
|
709
|
-
endpoint.createChannel('/test/:room', (req, res) => {
|
|
710
|
-
expect(req.event.params.room).toBeDefined();
|
|
711
|
-
res
|
|
712
|
-
.accept()
|
|
713
|
-
.trackPresence({
|
|
714
|
-
status: 'online',
|
|
715
|
-
});
|
|
716
|
-
});
|
|
717
|
-
|
|
718
|
-
await request(server)
|
|
719
|
-
.ws('/api/socket')
|
|
720
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
721
|
-
.sendJson(message)
|
|
722
|
-
.expectJson({
|
|
723
|
-
action: ServerActions.SYSTEM,
|
|
724
|
-
channelName: '/test/socket',
|
|
725
|
-
event: Events.ACKNOWLEDGE,
|
|
726
|
-
payload: {},
|
|
727
|
-
});
|
|
728
|
-
});
|
|
729
|
-
|
|
730
|
-
it('should throw an error if accept, reject / send is called more than once', async () => {
|
|
731
|
-
socket.createEndpoint('/api/:room', (_, res) => {
|
|
732
|
-
res.send('hello', {
|
|
733
|
-
test: 'test',
|
|
734
|
-
});
|
|
735
|
-
|
|
736
|
-
expect(() => res.accept()).toThrowError('Cannot execute response more than once');
|
|
737
|
-
});
|
|
738
|
-
|
|
739
|
-
await request(server)
|
|
740
|
-
.ws('/api/socket')
|
|
741
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
742
|
-
.expectJson({
|
|
743
|
-
action: ServerActions.BROADCAST,
|
|
744
|
-
channelName: SystemSender.ENDPOINT,
|
|
745
|
-
event: 'hello',
|
|
746
|
-
payload: {
|
|
747
|
-
test: 'test',
|
|
748
|
-
},
|
|
749
|
-
});
|
|
750
|
-
});
|
|
751
|
-
|
|
752
|
-
it('should be able to connect, join a channel and send a message', async () => {
|
|
753
|
-
const message = {
|
|
754
|
-
action: ClientActions.JOIN_CHANNEL,
|
|
755
|
-
channelName: '/test/socket',
|
|
756
|
-
event: 'TEST',
|
|
757
|
-
payload: {},
|
|
758
|
-
};
|
|
759
|
-
|
|
760
|
-
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
761
|
-
res.accept();
|
|
762
|
-
});
|
|
763
|
-
|
|
764
|
-
const channel = endpoint.createChannel('/test/:room', (req, res) => {
|
|
765
|
-
expect(req.event.params.room).toBeDefined();
|
|
766
|
-
res.accept();
|
|
767
|
-
});
|
|
768
|
-
|
|
769
|
-
channel.onEvent('echo', (req, res) => {
|
|
770
|
-
res.send('echo', req.event.payload);
|
|
771
|
-
});
|
|
772
|
-
|
|
773
|
-
channel.onEvent('broadcast', (req) => {
|
|
774
|
-
channel.broadcast('broadcast', {
|
|
775
|
-
...req.event.payload,
|
|
776
|
-
broadcast: true,
|
|
777
|
-
});
|
|
778
|
-
});
|
|
779
|
-
|
|
780
|
-
await request(server)
|
|
781
|
-
.ws('/api/socket')
|
|
782
|
-
.expectUpgrade((res) => expect(res.statusCode).toBe(101))
|
|
783
|
-
.sendJson(message)
|
|
784
|
-
.expectJson({
|
|
785
|
-
action: ServerActions.SYSTEM,
|
|
786
|
-
channelName: '/test/socket',
|
|
787
|
-
event: Events.ACKNOWLEDGE,
|
|
788
|
-
payload: {},
|
|
789
|
-
})
|
|
790
|
-
.sendJson({
|
|
791
|
-
addresses: ChannelReceiver.ALL_EXCEPT_SENDER,
|
|
792
|
-
action: ClientActions.BROADCAST,
|
|
793
|
-
channelName: '/test/socket',
|
|
794
|
-
event: 'echo',
|
|
795
|
-
payload: {
|
|
796
|
-
test: 'test',
|
|
797
|
-
},
|
|
798
|
-
})
|
|
799
|
-
.expectJson({
|
|
800
|
-
action: ServerActions.SYSTEM,
|
|
801
|
-
channelName: '/test/socket',
|
|
802
|
-
event: 'echo',
|
|
803
|
-
payload: {
|
|
804
|
-
test: 'test',
|
|
805
|
-
},
|
|
806
|
-
})
|
|
807
|
-
.sendJson({
|
|
808
|
-
action: ClientActions.BROADCAST,
|
|
809
|
-
channelName: '/test/socket',
|
|
810
|
-
event: 'broadcast',
|
|
811
|
-
payload: {
|
|
812
|
-
test: 'test',
|
|
813
|
-
},
|
|
814
|
-
})
|
|
815
|
-
.expectJson({
|
|
816
|
-
action: ServerActions.SYSTEM,
|
|
817
|
-
channelName: '/test/socket',
|
|
818
|
-
event: 'broadcast',
|
|
819
|
-
payload: {
|
|
820
|
-
test: 'test',
|
|
821
|
-
broadcast: true,
|
|
822
|
-
},
|
|
823
|
-
});
|
|
824
|
-
});
|
|
825
|
-
});
|