@eleven-am/pondsocket 0.1.9 → 0.1.10
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 +1 -1
- package/pondBase/baseClass.js +32 -51
- package/pondBase/baseClass.test.js +34 -34
- package/pondBase/pondBase.js +34 -95
- package/pondBase/pondBase.test.js +39 -67
- package/pondBase/pubSub.js +60 -124
- package/pondBase/pubSub.test.js +110 -110
- package/pondBase/simpleBase.js +92 -166
- package/pondBase/simpleBase.test.js +66 -124
- package/pondClient/channel.js +37 -40
- package/pondClient/socket.js +35 -38
- package/pondSocket/channel.js +121 -174
- package/pondSocket/channel.test.js +78 -89
- package/pondSocket/channelMiddleWare.js +16 -43
- package/pondSocket/endpoint.js +93 -188
- package/pondSocket/endpoint.test.js +447 -632
- package/pondSocket/pondChannel.js +81 -142
- package/pondSocket/pondChannel.test.js +40 -40
- package/pondSocket/pondResponse.js +52 -87
- package/pondSocket/pondSocket.js +35 -55
- package/pondSocket/server.test.js +88 -142
- package/pondSocket/socketMiddleWare.js +14 -16
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __assign = (this && this.__assign) || function () {
|
|
3
|
-
__assign = Object.assign || function(t) {
|
|
4
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
-
s = arguments[i];
|
|
6
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
-
t[p] = s[p];
|
|
8
|
-
}
|
|
9
|
-
return t;
|
|
10
|
-
};
|
|
11
|
-
return __assign.apply(this, arguments);
|
|
12
|
-
};
|
|
13
2
|
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
14
3
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
15
4
|
return new (P || (P = Promise))(function (resolve, reject) {
|
|
@@ -19,657 +8,483 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
19
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
20
9
|
});
|
|
21
10
|
};
|
|
22
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
23
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
24
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
25
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
26
|
-
function step(op) {
|
|
27
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
28
|
-
while (_) try {
|
|
29
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
30
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
31
|
-
switch (op[0]) {
|
|
32
|
-
case 0: case 1: t = op; break;
|
|
33
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
34
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
35
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
36
|
-
default:
|
|
37
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
38
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
39
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
40
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
41
|
-
if (t[2]) _.ops.pop();
|
|
42
|
-
_.trys.pop(); continue;
|
|
43
|
-
}
|
|
44
|
-
op = body.call(thisArg, _);
|
|
45
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
46
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
50
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
51
|
-
if (!m) return o;
|
|
52
|
-
var i = m.call(o), r, ar = [], e;
|
|
53
|
-
try {
|
|
54
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
55
|
-
}
|
|
56
|
-
catch (error) { e = { error: error }; }
|
|
57
|
-
finally {
|
|
58
|
-
try {
|
|
59
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
60
|
-
}
|
|
61
|
-
finally { if (e) throw e.error; }
|
|
62
|
-
}
|
|
63
|
-
return ar;
|
|
64
|
-
};
|
|
65
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
66
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
67
|
-
if (ar || !(i in from)) {
|
|
68
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
69
|
-
ar[i] = from[i];
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
73
|
-
};
|
|
74
11
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
75
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
76
13
|
};
|
|
77
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
return { socket
|
|
15
|
+
const endpoint_1 = require("./endpoint");
|
|
16
|
+
const pondSocket_1 = require("./pondSocket");
|
|
17
|
+
const superwstest_1 = __importDefault(require("superwstest"));
|
|
18
|
+
const enums_1 = require("./enums");
|
|
19
|
+
const createPondSocket = () => {
|
|
20
|
+
const mock = jest.fn();
|
|
21
|
+
const socket = new pondSocket_1.PondSocket();
|
|
22
|
+
const server = socket.listen(3001, mock);
|
|
23
|
+
return { socket, server, mock };
|
|
87
24
|
};
|
|
88
|
-
describe('endpoint',
|
|
89
|
-
it('should be defined',
|
|
25
|
+
describe('endpoint', () => {
|
|
26
|
+
it('should be defined', () => {
|
|
90
27
|
expect(endpoint_1.Endpoint).toBeDefined();
|
|
91
28
|
});
|
|
92
|
-
it('should be instantiable',
|
|
93
|
-
|
|
94
|
-
|
|
29
|
+
it('should be instantiable', () => {
|
|
30
|
+
const socketServer = {};
|
|
31
|
+
const handler = jest.fn();
|
|
95
32
|
expect(new endpoint_1.Endpoint(socketServer, handler)).toBeInstanceOf(endpoint_1.Endpoint);
|
|
96
33
|
});
|
|
97
34
|
// Functionality tests
|
|
98
|
-
it('should be able to close a socket',
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
35
|
+
it('should be able to close a socket', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
36
|
+
const { socket, server } = createPondSocket();
|
|
37
|
+
expect(server).toBeDefined();
|
|
38
|
+
const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
|
|
39
|
+
expect(req.params.path).toBe('socket');
|
|
40
|
+
res.accept();
|
|
41
|
+
setTimeout(() => {
|
|
42
|
+
endpoint.closeConnection(req.clientId);
|
|
43
|
+
}, 100);
|
|
44
|
+
});
|
|
45
|
+
yield (0, superwstest_1.default)(server)
|
|
46
|
+
.ws('/api/socket')
|
|
47
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
48
|
+
.wait(200)
|
|
49
|
+
.expectClosed();
|
|
50
|
+
server.close();
|
|
51
|
+
}));
|
|
52
|
+
it('should be able to list connections', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
53
|
+
const { socket, server } = createPondSocket();
|
|
54
|
+
let connectionsCount = 0;
|
|
55
|
+
expect(server).toBeDefined();
|
|
56
|
+
const endpoint = socket.createEndpoint('/api/:path', (req, res) => {
|
|
57
|
+
expect(req.params.path).toBe('socket');
|
|
58
|
+
connectionsCount = endpoint.listConnections().length;
|
|
59
|
+
res.accept();
|
|
60
|
+
});
|
|
61
|
+
yield (0, superwstest_1.default)(server)
|
|
62
|
+
.ws('/api/socket')
|
|
63
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101));
|
|
64
|
+
yield (0, superwstest_1.default)(server)
|
|
65
|
+
.ws('/api/socket')
|
|
66
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101));
|
|
67
|
+
server.close(); // Close the server to stop the connection from being kept alive
|
|
68
|
+
expect(connectionsCount).toBe(1);
|
|
69
|
+
expect(endpoint.listConnections().length).toBe(2); // The connections are still in the list
|
|
70
|
+
}));
|
|
71
|
+
it('should be capable of sending messages to all clients', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
72
|
+
const { socket, server } = createPondSocket();
|
|
73
|
+
expect(server).toBeDefined();
|
|
74
|
+
let users = 0;
|
|
75
|
+
const endpoint = socket.createEndpoint('/api/:room', (req, res) => {
|
|
76
|
+
users++;
|
|
77
|
+
res.send('Hello', { room: req.params.room });
|
|
78
|
+
if (users > 0)
|
|
79
|
+
endpoint.broadcast('TEST', { message: 'Hello everyone' });
|
|
80
|
+
});
|
|
81
|
+
yield (0, superwstest_1.default)(server)
|
|
82
|
+
.ws('/api/socket')
|
|
83
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
84
|
+
.expectJson({
|
|
85
|
+
action: 'MESSAGE', event: 'Hello', channelName: 'SERVER', payload: {
|
|
86
|
+
room: 'socket'
|
|
87
|
+
}
|
|
88
|
+
})
|
|
89
|
+
.expectJson({
|
|
90
|
+
action: 'MESSAGE', event: 'TEST', channelName: `ENDPOINT`, payload: {
|
|
91
|
+
message: 'Hello everyone'
|
|
121
92
|
}
|
|
122
93
|
});
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
connectionsCount = endpoint.listConnections().length;
|
|
135
|
-
res.accept();
|
|
136
|
-
});
|
|
137
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
138
|
-
.ws('/api/socket')
|
|
139
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })];
|
|
140
|
-
case 1:
|
|
141
|
-
_b.sent();
|
|
142
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
143
|
-
.ws('/api/socket')
|
|
144
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })];
|
|
145
|
-
case 2:
|
|
146
|
-
_b.sent();
|
|
147
|
-
server.close(); // Close the server to stop the connection from being kept alive
|
|
148
|
-
expect(connectionsCount).toBe(1);
|
|
149
|
-
expect(endpoint.listConnections().length).toBe(2); // The connections are still in the list
|
|
150
|
-
return [2 /*return*/];
|
|
94
|
+
yield (0, superwstest_1.default)(server)
|
|
95
|
+
.ws('/api/secondSocket')
|
|
96
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
97
|
+
.expectJson({
|
|
98
|
+
action: 'MESSAGE', event: 'Hello', channelName: 'SERVER', payload: {
|
|
99
|
+
room: 'secondSocket'
|
|
100
|
+
}
|
|
101
|
+
})
|
|
102
|
+
.expectJson({
|
|
103
|
+
action: 'MESSAGE', event: 'TEST', channelName: 'ENDPOINT', payload: {
|
|
104
|
+
message: 'Hello everyone'
|
|
151
105
|
}
|
|
152
106
|
});
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
107
|
+
server.close();
|
|
108
|
+
}));
|
|
109
|
+
it('should be able to accept connections on this handler', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
110
|
+
const { socket, server } = createPondSocket();
|
|
111
|
+
expect(server).toBeDefined();
|
|
112
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
113
|
+
res.accept();
|
|
114
|
+
});
|
|
115
|
+
endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
116
|
+
expect(req.params.room).toBeDefined();
|
|
117
|
+
res.accept({
|
|
118
|
+
presence: {
|
|
119
|
+
status: 'online',
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
endpoint.createChannel('/socket/:room', (req, res, _) => {
|
|
124
|
+
expect(req.params.room).toBeDefined();
|
|
125
|
+
res.accept({
|
|
126
|
+
presence: {
|
|
127
|
+
status: 'online socket',
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
const message = {
|
|
132
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
133
|
+
};
|
|
134
|
+
yield (0, superwstest_1.default)(server)
|
|
135
|
+
.ws('/api/socket')
|
|
136
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
137
|
+
.sendJson(message)
|
|
138
|
+
.expectJson(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
139
|
+
yield (0, superwstest_1.default)(server)
|
|
140
|
+
.ws('/api/socket')
|
|
141
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
142
|
+
.sendJson(Object.assign(Object.assign({}, message), { channelName: '/socket/socket' }))
|
|
143
|
+
.expectJson(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
144
|
+
expect([...endpoint['_channels'].generator()]).toHaveLength(2);
|
|
145
|
+
server.close();
|
|
146
|
+
}));
|
|
147
|
+
it('should refuse connections if there are no handlers', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
148
|
+
const { socket, server } = createPondSocket();
|
|
149
|
+
expect(server).toBeDefined();
|
|
150
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
151
|
+
res.accept();
|
|
152
|
+
});
|
|
153
|
+
endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
154
|
+
expect(req.params.room).toBeDefined();
|
|
155
|
+
res.accept({
|
|
156
|
+
presence: {
|
|
157
|
+
status: 'online',
|
|
158
|
+
}
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
const message = {
|
|
162
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
163
|
+
};
|
|
164
|
+
yield (0, superwstest_1.default)(server)
|
|
165
|
+
.ws('/api/socket')
|
|
166
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
167
|
+
.sendJson(message)
|
|
168
|
+
.expectJson(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
169
|
+
yield (0, superwstest_1.default)(server)
|
|
170
|
+
.ws('/api/socket')
|
|
171
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
172
|
+
.sendJson(Object.assign(Object.assign({}, message), { channelName: '/socket/socket' // This channel handler does not exist
|
|
173
|
+
}))
|
|
174
|
+
.expectJson(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
175
|
+
expect([...endpoint['_channels'].generator()]).toHaveLength(1);
|
|
176
|
+
server.close();
|
|
177
|
+
}));
|
|
178
|
+
it('should send an error when we send an incomplete message', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
179
|
+
const { socket, server } = createPondSocket();
|
|
180
|
+
expect(server).toBeDefined();
|
|
181
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
182
|
+
res.accept();
|
|
183
|
+
});
|
|
184
|
+
endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
185
|
+
expect(req.params.room).toBeDefined();
|
|
186
|
+
res.accept({
|
|
187
|
+
presence: {
|
|
188
|
+
status: 'online',
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
const message = {
|
|
193
|
+
action: enums_1.ClientActions.LEAVE_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
194
|
+
};
|
|
195
|
+
yield (0, superwstest_1.default)(server)
|
|
196
|
+
.ws('/api/socket')
|
|
197
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
198
|
+
.sendJson(message)
|
|
199
|
+
.expectJson({
|
|
200
|
+
action: enums_1.ServerActions.ERROR,
|
|
201
|
+
event: "error",
|
|
202
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
203
|
+
payload: {
|
|
204
|
+
message: "Channel /test/socket does not exist"
|
|
200
205
|
}
|
|
201
206
|
});
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
});
|
|
213
|
-
endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
214
|
-
expect(req.params.room).toBeDefined();
|
|
215
|
-
res.accept({
|
|
216
|
-
presence: {
|
|
217
|
-
status: 'online',
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
});
|
|
221
|
-
endpoint.createChannel('/socket/:room', function (req, res, _) {
|
|
222
|
-
expect(req.params.room).toBeDefined();
|
|
223
|
-
res.accept({
|
|
224
|
-
presence: {
|
|
225
|
-
status: 'online socket',
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
});
|
|
229
|
-
message = {
|
|
230
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
231
|
-
};
|
|
232
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
233
|
-
.ws('/api/socket')
|
|
234
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
235
|
-
.sendJson(message)
|
|
236
|
-
.expectJson()]; // receives a presence message, this can not be matched because the payload is dynamic
|
|
237
|
-
case 1:
|
|
238
|
-
_b.sent(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
239
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
240
|
-
.ws('/api/socket')
|
|
241
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
242
|
-
.sendJson(__assign(__assign({}, message), { channelName: '/socket/socket' }))
|
|
243
|
-
.expectJson()]; // receives a presence message, this can not be matched because the payload is dynamic
|
|
244
|
-
case 2:
|
|
245
|
-
_b.sent(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
246
|
-
expect(__spreadArray([], __read(endpoint['_channels'].generator()), false)).toHaveLength(2);
|
|
247
|
-
server.close();
|
|
248
|
-
return [2 /*return*/];
|
|
207
|
+
yield (0, superwstest_1.default)(server)
|
|
208
|
+
.ws('/api/socket')
|
|
209
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
210
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: null }))
|
|
211
|
+
.expectJson({
|
|
212
|
+
action: enums_1.ServerActions.ERROR,
|
|
213
|
+
event: "error",
|
|
214
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
215
|
+
payload: {
|
|
216
|
+
message: "No action provided",
|
|
249
217
|
}
|
|
250
218
|
});
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
});
|
|
262
|
-
endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
263
|
-
expect(req.params.room).toBeDefined();
|
|
264
|
-
res.accept({
|
|
265
|
-
presence: {
|
|
266
|
-
status: 'online',
|
|
267
|
-
}
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
message = {
|
|
271
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
272
|
-
};
|
|
273
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
274
|
-
.ws('/api/socket')
|
|
275
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
276
|
-
.sendJson(message)
|
|
277
|
-
.expectJson()]; // receives a presence message, this can not be matched because the payload is dynamic
|
|
278
|
-
case 1:
|
|
279
|
-
_b.sent(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
280
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
281
|
-
.ws('/api/socket')
|
|
282
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
283
|
-
.sendJson(__assign(__assign({}, message), { channelName: '/socket/socket' // This channel handler does not exist
|
|
284
|
-
}))
|
|
285
|
-
.expectJson()]; // receives a presence message, this can not be matched because the payload is dynamic
|
|
286
|
-
case 2:
|
|
287
|
-
_b.sent(); // receives a presence message, this can not be matched because the payload is dynamic
|
|
288
|
-
expect(__spreadArray([], __read(endpoint['_channels'].generator()), false)).toHaveLength(1);
|
|
289
|
-
server.close();
|
|
290
|
-
return [2 /*return*/];
|
|
219
|
+
yield (0, superwstest_1.default)(server)
|
|
220
|
+
.ws('/api/socket')
|
|
221
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
222
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.BROADCAST, channelName: null }))
|
|
223
|
+
.expectJson({
|
|
224
|
+
action: enums_1.ServerActions.ERROR,
|
|
225
|
+
event: "error",
|
|
226
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
227
|
+
payload: {
|
|
228
|
+
message: "No channel name provided",
|
|
291
229
|
}
|
|
292
230
|
});
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
});
|
|
304
|
-
endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
305
|
-
expect(req.params.room).toBeDefined();
|
|
306
|
-
res.accept({
|
|
307
|
-
presence: {
|
|
308
|
-
status: 'online',
|
|
309
|
-
}
|
|
310
|
-
});
|
|
311
|
-
});
|
|
312
|
-
message = {
|
|
313
|
-
action: enums_1.ClientActions.LEAVE_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
314
|
-
};
|
|
315
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
316
|
-
.ws('/api/socket')
|
|
317
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
318
|
-
.sendJson(message)
|
|
319
|
-
.expectJson({
|
|
320
|
-
action: enums_1.ServerActions.ERROR,
|
|
321
|
-
event: "error",
|
|
322
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
323
|
-
payload: {
|
|
324
|
-
message: "Channel /test/socket does not exist"
|
|
325
|
-
}
|
|
326
|
-
})];
|
|
327
|
-
case 1:
|
|
328
|
-
_b.sent();
|
|
329
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
330
|
-
.ws('/api/socket')
|
|
331
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
332
|
-
.sendJson(__assign(__assign({}, message), { action: null }))
|
|
333
|
-
.expectJson({
|
|
334
|
-
action: enums_1.ServerActions.ERROR,
|
|
335
|
-
event: "error",
|
|
336
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
337
|
-
payload: {
|
|
338
|
-
message: "No action provided",
|
|
339
|
-
}
|
|
340
|
-
})];
|
|
341
|
-
case 2:
|
|
342
|
-
_b.sent();
|
|
343
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
344
|
-
.ws('/api/socket')
|
|
345
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
346
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.BROADCAST, channelName: null }))
|
|
347
|
-
.expectJson({
|
|
348
|
-
action: enums_1.ServerActions.ERROR,
|
|
349
|
-
event: "error",
|
|
350
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
351
|
-
payload: {
|
|
352
|
-
message: "No channel name provided",
|
|
353
|
-
}
|
|
354
|
-
})];
|
|
355
|
-
case 3:
|
|
356
|
-
_b.sent();
|
|
357
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
358
|
-
.ws('/api/socket')
|
|
359
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
360
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.BROADCAST_FROM, payload: null }))
|
|
361
|
-
.expectJson({
|
|
362
|
-
action: enums_1.ServerActions.ERROR,
|
|
363
|
-
event: "error",
|
|
364
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
365
|
-
payload: {
|
|
366
|
-
message: "No payload provided",
|
|
367
|
-
}
|
|
368
|
-
})];
|
|
369
|
-
case 4:
|
|
370
|
-
_b.sent();
|
|
371
|
-
// send incorrect Json message
|
|
372
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
373
|
-
.ws('/api/socket')
|
|
374
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
375
|
-
.send('"action": "JOIN_CHANNEL", "channelName": "/test/socket", "event": "TEST", "payload": {}}')
|
|
376
|
-
.expectJson({
|
|
377
|
-
action: enums_1.ServerActions.ERROR,
|
|
378
|
-
event: "error",
|
|
379
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
380
|
-
payload: {
|
|
381
|
-
message: "Invalid JSON",
|
|
382
|
-
}
|
|
383
|
-
})];
|
|
384
|
-
case 5:
|
|
385
|
-
// send incorrect Json message
|
|
386
|
-
_b.sent();
|
|
387
|
-
expect(__spreadArray([], __read(endpoint['_channels'].generator()), false)).toHaveLength(1);
|
|
388
|
-
server.close();
|
|
389
|
-
return [2 /*return*/];
|
|
231
|
+
yield (0, superwstest_1.default)(server)
|
|
232
|
+
.ws('/api/socket')
|
|
233
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
234
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.BROADCAST_FROM, payload: null }))
|
|
235
|
+
.expectJson({
|
|
236
|
+
action: enums_1.ServerActions.ERROR,
|
|
237
|
+
event: "error",
|
|
238
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
239
|
+
payload: {
|
|
240
|
+
message: "No payload provided",
|
|
390
241
|
}
|
|
391
242
|
});
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
channel = endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
404
|
-
expect(req.params.room).toBeDefined();
|
|
405
|
-
res.accept();
|
|
406
|
-
});
|
|
407
|
-
channel.on('/test/:room', function (req, res, _) {
|
|
408
|
-
if (req.params.room === 'TEST') {
|
|
409
|
-
res.accept();
|
|
410
|
-
}
|
|
411
|
-
else if (req.params.room === 'TEST2') {
|
|
412
|
-
res.reject();
|
|
413
|
-
}
|
|
414
|
-
else if (req.params.room === 'TEST3') {
|
|
415
|
-
res.reject('choke on my balls');
|
|
416
|
-
}
|
|
417
|
-
else
|
|
418
|
-
res.reject('TEST');
|
|
419
|
-
});
|
|
420
|
-
message = {
|
|
421
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
422
|
-
};
|
|
423
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
424
|
-
.ws('/api/socket')
|
|
425
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
426
|
-
.sendJson(message)
|
|
427
|
-
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
428
|
-
.sendJson(__assign(__assign({}, message), { event: '/test/TEST2', action: enums_1.ClientActions.BROADCAST }))
|
|
429
|
-
.expectJson({
|
|
430
|
-
action: "ERROR", event: "error", channelName: "/test/socket", payload: {
|
|
431
|
-
message: "Message rejected", code: 403
|
|
432
|
-
}
|
|
433
|
-
})
|
|
434
|
-
.sendJson(__assign(__assign({}, message), { channelName: "/test/socket", action: enums_1.ClientActions.BROADCAST }))
|
|
435
|
-
.expectJson({
|
|
436
|
-
action: enums_1.ServerActions.MESSAGE, payload: {}, event: "TEST", channelName: "/test/socket"
|
|
437
|
-
})
|
|
438
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER }))
|
|
439
|
-
.expectJson({
|
|
440
|
-
action: enums_1.ServerActions.ERROR,
|
|
441
|
-
event: "error",
|
|
442
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
443
|
-
payload: {
|
|
444
|
-
message: "Error while executing event 'TEST' on channel '/test/socket': No addresses provided"
|
|
445
|
-
}
|
|
446
|
-
})];
|
|
447
|
-
case 1:
|
|
448
|
-
_b.sent();
|
|
449
|
-
expect(endpoint.listChannels()).toHaveLength(1);
|
|
450
|
-
server.close();
|
|
451
|
-
return [2 /*return*/];
|
|
243
|
+
// send incorrect Json message
|
|
244
|
+
yield (0, superwstest_1.default)(server)
|
|
245
|
+
.ws('/api/socket')
|
|
246
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
247
|
+
.send('"action": "JOIN_CHANNEL", "channelName": "/test/socket", "event": "TEST", "payload": {}}')
|
|
248
|
+
.expectJson({
|
|
249
|
+
action: enums_1.ServerActions.ERROR,
|
|
250
|
+
event: "error",
|
|
251
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
252
|
+
payload: {
|
|
253
|
+
message: "Invalid JSON",
|
|
452
254
|
}
|
|
453
255
|
});
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
else if (req.params.room === 'TEST2') {
|
|
474
|
-
res.reject();
|
|
475
|
-
}
|
|
476
|
-
else if (req.params.room === 'TEST3') {
|
|
477
|
-
res.reject('choke on my balls');
|
|
478
|
-
}
|
|
479
|
-
else
|
|
480
|
-
res.reject('TEST');
|
|
481
|
-
});
|
|
482
|
-
message = {
|
|
483
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
484
|
-
};
|
|
485
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
486
|
-
.ws('/api/socket')
|
|
487
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
488
|
-
.sendJson(message)
|
|
489
|
-
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
490
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.BROADCAST_FROM, payload: {
|
|
491
|
-
message: {
|
|
492
|
-
action: enums_1.ServerActions.MESSAGE, payload: {}, event: "TEST", channelName: "/test/socket"
|
|
493
|
-
}
|
|
494
|
-
} }))];
|
|
495
|
-
case 1:
|
|
496
|
-
_b.sent();
|
|
497
|
-
expect(endpoint.listChannels()).toHaveLength(1);
|
|
498
|
-
server.close();
|
|
499
|
-
return [2 /*return*/];
|
|
256
|
+
expect([...endpoint['_channels'].generator()]).toHaveLength(1);
|
|
257
|
+
server.close();
|
|
258
|
+
}));
|
|
259
|
+
it('should send an error when the channel exists but other things happen', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
260
|
+
const { socket, server } = createPondSocket();
|
|
261
|
+
expect(server).toBeDefined();
|
|
262
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
263
|
+
res.accept();
|
|
264
|
+
});
|
|
265
|
+
const channel = endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
266
|
+
expect(req.params.room).toBeDefined();
|
|
267
|
+
res.accept();
|
|
268
|
+
});
|
|
269
|
+
channel.on('/test/:room', (req, res, _) => {
|
|
270
|
+
if (req.params.room === 'TEST') {
|
|
271
|
+
res.accept();
|
|
272
|
+
}
|
|
273
|
+
else if (req.params.room === 'TEST2') {
|
|
274
|
+
res.reject();
|
|
500
275
|
}
|
|
276
|
+
else if (req.params.room === 'TEST3') {
|
|
277
|
+
res.reject('choke on my balls');
|
|
278
|
+
}
|
|
279
|
+
else
|
|
280
|
+
res.reject('TEST');
|
|
281
|
+
});
|
|
282
|
+
const message = {
|
|
283
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
284
|
+
};
|
|
285
|
+
yield (0, superwstest_1.default)(server)
|
|
286
|
+
.ws('/api/socket')
|
|
287
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
288
|
+
.sendJson(message)
|
|
289
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
290
|
+
.sendJson(Object.assign(Object.assign({}, message), { event: '/test/TEST2', action: enums_1.ClientActions.BROADCAST }))
|
|
291
|
+
.expectJson({
|
|
292
|
+
action: "ERROR", event: "error", channelName: "/test/socket", payload: {
|
|
293
|
+
message: "Message rejected", code: 403
|
|
294
|
+
}
|
|
295
|
+
})
|
|
296
|
+
.sendJson(Object.assign(Object.assign({}, message), { channelName: "/test/socket", action: enums_1.ClientActions.BROADCAST }))
|
|
297
|
+
.expectJson({
|
|
298
|
+
action: enums_1.ServerActions.MESSAGE, payload: {}, event: "TEST", channelName: "/test/socket"
|
|
299
|
+
})
|
|
300
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER }))
|
|
301
|
+
.expectJson({
|
|
302
|
+
action: enums_1.ServerActions.ERROR,
|
|
303
|
+
event: "error",
|
|
304
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
305
|
+
payload: {
|
|
306
|
+
message: "Error while executing event 'TEST' on channel '/test/socket': No addresses provided"
|
|
307
|
+
}
|
|
308
|
+
});
|
|
309
|
+
expect(endpoint.listChannels()).toHaveLength(1);
|
|
310
|
+
server.close();
|
|
311
|
+
}));
|
|
312
|
+
it('should be capable of sending messages to a specific user', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
313
|
+
const { socket, server } = createPondSocket();
|
|
314
|
+
expect(server).toBeDefined();
|
|
315
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
316
|
+
res.accept();
|
|
317
|
+
});
|
|
318
|
+
const channel = endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
319
|
+
expect(req.params.room).toBeDefined();
|
|
320
|
+
res.accept();
|
|
501
321
|
});
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
return __generator(this, function (_b) {
|
|
506
|
-
switch (_b.label) {
|
|
507
|
-
case 0:
|
|
508
|
-
_a = createPondSocket(), socket = _a.socket, server = _a.server;
|
|
509
|
-
expect(server).toBeDefined();
|
|
510
|
-
endpoint = socket.createEndpoint('/api/:room', function (_, res) {
|
|
511
|
-
res.accept();
|
|
512
|
-
});
|
|
513
|
-
endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
514
|
-
expect(req.params.room).toBeDefined();
|
|
515
|
-
res.accept();
|
|
516
|
-
});
|
|
517
|
-
message = {
|
|
518
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
519
|
-
};
|
|
520
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
521
|
-
.ws('/api/socket')
|
|
522
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
523
|
-
.sendJson(message)
|
|
524
|
-
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
525
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.UPDATE_PRESENCE, payload: {
|
|
526
|
-
presence: {
|
|
527
|
-
status: 'online'
|
|
528
|
-
}
|
|
529
|
-
} }))
|
|
530
|
-
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
531
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER, addresses: [], payload: {} }))
|
|
532
|
-
.expectJson({
|
|
533
|
-
action: enums_1.ServerActions.ERROR,
|
|
534
|
-
event: "error",
|
|
535
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
536
|
-
payload: {
|
|
537
|
-
message: "Error while executing event 'TEST' on channel '/test/socket': No addresses provided"
|
|
538
|
-
}
|
|
539
|
-
})
|
|
540
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER, addresses: ['hello'], payload: {} }))
|
|
541
|
-
.expectJson({
|
|
542
|
-
action: enums_1.ServerActions.ERROR,
|
|
543
|
-
event: "error",
|
|
544
|
-
channelName: enums_1.PondSenders.ENDPOINT,
|
|
545
|
-
payload: {
|
|
546
|
-
message: "Error while executing event 'TEST' on channel '/test/socket': Client(s) with clientId(s) hello were not found in channel /test/socket"
|
|
547
|
-
}
|
|
548
|
-
})
|
|
549
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.UPDATE_PRESENCE, payload: {
|
|
550
|
-
assigns: {
|
|
551
|
-
status: 'online'
|
|
552
|
-
}
|
|
553
|
-
} }))
|
|
554
|
-
.close()
|
|
555
|
-
.expectClosed()];
|
|
556
|
-
case 1:
|
|
557
|
-
_b.sent();
|
|
558
|
-
expect(endpoint.listChannels()).toHaveLength(1); // the channel has not been removed yet
|
|
559
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
560
|
-
.ws('/api/newSocket')
|
|
561
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
562
|
-
.sendJson(__assign(__assign({}, message), { channelName: '/test/socket2' }))
|
|
563
|
-
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
564
|
-
.sendJson(__assign(__assign({}, message), { action: enums_1.ClientActions.LEAVE_CHANNEL, channelName: '/test/socket2' }))
|
|
565
|
-
.expectJson()];
|
|
566
|
-
case 2:
|
|
567
|
-
_b.sent();
|
|
568
|
-
expect(endpoint.listChannels()).toHaveLength(0); // by now the first channel should have been removed; and since we gracefully closed the connection, the second channel should have been removed as well
|
|
569
|
-
server.close();
|
|
570
|
-
return [2 /*return*/];
|
|
322
|
+
channel.on('/test/:room', (req, res, _) => {
|
|
323
|
+
if (req.params.room === 'TEST') {
|
|
324
|
+
res.accept();
|
|
571
325
|
}
|
|
326
|
+
else if (req.params.room === 'TEST2') {
|
|
327
|
+
res.reject();
|
|
328
|
+
}
|
|
329
|
+
else if (req.params.room === 'TEST3') {
|
|
330
|
+
res.reject('choke on my balls');
|
|
331
|
+
}
|
|
332
|
+
else
|
|
333
|
+
res.reject('TEST');
|
|
334
|
+
});
|
|
335
|
+
const message = {
|
|
336
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
337
|
+
};
|
|
338
|
+
yield (0, superwstest_1.default)(server)
|
|
339
|
+
.ws('/api/socket')
|
|
340
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
341
|
+
.sendJson(message)
|
|
342
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
343
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.BROADCAST_FROM, payload: {
|
|
344
|
+
message: {
|
|
345
|
+
action: enums_1.ServerActions.MESSAGE, payload: {}, event: "TEST", channelName: "/test/socket"
|
|
346
|
+
}
|
|
347
|
+
} }));
|
|
348
|
+
expect(endpoint.listChannels()).toHaveLength(1);
|
|
349
|
+
server.close();
|
|
350
|
+
}));
|
|
351
|
+
it('should be able to update user presence on user demand', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
352
|
+
const { socket, server } = createPondSocket();
|
|
353
|
+
expect(server).toBeDefined();
|
|
354
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
355
|
+
res.accept();
|
|
572
356
|
});
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
action: enums_1.ServerActions.MESSAGE,
|
|
608
|
-
event: 'Test', channelName: enums_1.PondSenders.ENDPOINT,
|
|
609
|
-
payload: {
|
|
610
|
-
message: 'hello'
|
|
611
|
-
}
|
|
612
|
-
})];
|
|
613
|
-
case 1:
|
|
614
|
-
_b.sent();
|
|
615
|
-
expect(endpoint.listChannels()).toHaveLength(1);
|
|
616
|
-
server.close();
|
|
617
|
-
return [2 /*return*/];
|
|
357
|
+
endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
358
|
+
expect(req.params.room).toBeDefined();
|
|
359
|
+
res.accept();
|
|
360
|
+
});
|
|
361
|
+
const message = {
|
|
362
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
363
|
+
};
|
|
364
|
+
yield (0, superwstest_1.default)(server)
|
|
365
|
+
.ws('/api/socket')
|
|
366
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
367
|
+
.sendJson(message)
|
|
368
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
369
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.UPDATE_PRESENCE, payload: {
|
|
370
|
+
presence: {
|
|
371
|
+
status: 'online'
|
|
372
|
+
}
|
|
373
|
+
} }))
|
|
374
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
375
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER, addresses: [], payload: {} }))
|
|
376
|
+
.expectJson({
|
|
377
|
+
action: enums_1.ServerActions.ERROR,
|
|
378
|
+
event: "error",
|
|
379
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
380
|
+
payload: {
|
|
381
|
+
message: "Error while executing event 'TEST' on channel '/test/socket': No addresses provided"
|
|
382
|
+
}
|
|
383
|
+
})
|
|
384
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.SEND_MESSAGE_TO_USER, addresses: ['hello'], payload: {} }))
|
|
385
|
+
.expectJson({
|
|
386
|
+
action: enums_1.ServerActions.ERROR,
|
|
387
|
+
event: "error",
|
|
388
|
+
channelName: enums_1.PondSenders.ENDPOINT,
|
|
389
|
+
payload: {
|
|
390
|
+
message: "Error while executing event 'TEST' on channel '/test/socket': Client(s) with clientId(s) hello were not found in channel /test/socket"
|
|
618
391
|
}
|
|
392
|
+
})
|
|
393
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.UPDATE_PRESENCE, payload: {
|
|
394
|
+
assigns: {
|
|
395
|
+
status: 'online'
|
|
396
|
+
}
|
|
397
|
+
} }))
|
|
398
|
+
.close()
|
|
399
|
+
.expectClosed();
|
|
400
|
+
expect(endpoint.listChannels()).toHaveLength(1); // the channel has not been removed yet
|
|
401
|
+
yield (0, superwstest_1.default)(server)
|
|
402
|
+
.ws('/api/newSocket')
|
|
403
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
404
|
+
.sendJson(Object.assign(Object.assign({}, message), { channelName: '/test/socket2' }))
|
|
405
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
406
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.LEAVE_CHANNEL, channelName: '/test/socket2' }))
|
|
407
|
+
.expectJson();
|
|
408
|
+
expect(endpoint.listChannels()).toHaveLength(0); // by now the first channel should have been removed; and since we gracefully closed the connection, the second channel should have been removed as well
|
|
409
|
+
server.close();
|
|
410
|
+
}));
|
|
411
|
+
it('should ba able to send messages to a specific user', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
412
|
+
const { socket, server } = createPondSocket();
|
|
413
|
+
expect(server).toBeDefined();
|
|
414
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
415
|
+
res.accept();
|
|
416
|
+
});
|
|
417
|
+
const channel = endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
418
|
+
expect(req.params.room).toBeDefined();
|
|
419
|
+
res.accept();
|
|
619
420
|
});
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
switch (_b.label) {
|
|
625
|
-
case 0:
|
|
626
|
-
_a = createPondSocket(), socket = _a.socket, server = _a.server;
|
|
627
|
-
expect(server).toBeDefined();
|
|
628
|
-
endpoint = socket.createEndpoint('/api/:room', function (_, res) {
|
|
629
|
-
res.accept();
|
|
630
|
-
});
|
|
631
|
-
channel = endpoint.createChannel('/test/:room', function (req, res, _) {
|
|
632
|
-
expect(req.params.room).toBeDefined();
|
|
633
|
-
res.accept();
|
|
634
|
-
});
|
|
635
|
-
channel.on(':room', function (req, res) {
|
|
636
|
-
if (req.params.room === 'TEST') {
|
|
637
|
-
res.reject('TEST');
|
|
638
|
-
}
|
|
639
|
-
});
|
|
640
|
-
message = {
|
|
641
|
-
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
642
|
-
};
|
|
643
|
-
functionToTest = function (ws) {
|
|
644
|
-
try {
|
|
645
|
-
ws.emit('error', {});
|
|
646
|
-
}
|
|
647
|
-
catch (e) {
|
|
648
|
-
console.log(e);
|
|
649
|
-
}
|
|
650
|
-
};
|
|
651
|
-
_b.label = 1;
|
|
652
|
-
case 1:
|
|
653
|
-
_b.trys.push([1, 3, , 4]);
|
|
654
|
-
return [4 /*yield*/, (0, superwstest_1.default)(server)
|
|
655
|
-
.ws('/api/socket')
|
|
656
|
-
.expectUpgrade(function (res) { return expect(res.statusCode).toBe(101); })
|
|
657
|
-
.sendJson(message)
|
|
658
|
-
.exec(functionToTest)
|
|
659
|
-
.expectClosed()];
|
|
660
|
-
case 2:
|
|
661
|
-
_b.sent();
|
|
662
|
-
return [3 /*break*/, 4];
|
|
663
|
-
case 3:
|
|
664
|
-
e_1 = _b.sent();
|
|
665
|
-
console.log(e_1);
|
|
666
|
-
return [3 /*break*/, 4];
|
|
667
|
-
case 4:
|
|
668
|
-
expect(endpoint.listChannels()).toHaveLength(0); // the socket should have been removed
|
|
669
|
-
expect(endpoint['_findChannel']('/test/socket')).toBeUndefined();
|
|
670
|
-
server.close();
|
|
671
|
-
return [2 /*return*/];
|
|
421
|
+
channel.on(':room', (req, res, _) => {
|
|
422
|
+
if (req.params.room === 'TEST') {
|
|
423
|
+
endpoint.send(req.client.clientId, 'Test', { message: 'hello' });
|
|
424
|
+
res.accept();
|
|
672
425
|
}
|
|
673
426
|
});
|
|
674
|
-
|
|
427
|
+
const message = {
|
|
428
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
429
|
+
};
|
|
430
|
+
yield (0, superwstest_1.default)(server)
|
|
431
|
+
.ws('/api/socket')
|
|
432
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
433
|
+
.sendJson(message)
|
|
434
|
+
.expectJson() // receives a presence message, this can not be matched because the payload is dynamic
|
|
435
|
+
.sendJson(Object.assign(Object.assign({}, message), { action: enums_1.ClientActions.BROADCAST_FROM, payload: {
|
|
436
|
+
message: {
|
|
437
|
+
action: enums_1.ServerActions.MESSAGE, payload: {}, event: "TEST", channelName: "/test/socket"
|
|
438
|
+
}
|
|
439
|
+
} })).expectJson({
|
|
440
|
+
action: enums_1.ServerActions.MESSAGE,
|
|
441
|
+
event: 'Test', channelName: enums_1.PondSenders.ENDPOINT,
|
|
442
|
+
payload: {
|
|
443
|
+
message: 'hello'
|
|
444
|
+
}
|
|
445
|
+
});
|
|
446
|
+
expect(endpoint.listChannels()).toHaveLength(1);
|
|
447
|
+
server.close();
|
|
448
|
+
}));
|
|
449
|
+
it('should be able ot manage error from the client side', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
450
|
+
const { socket, server } = createPondSocket();
|
|
451
|
+
expect(server).toBeDefined();
|
|
452
|
+
const endpoint = socket.createEndpoint('/api/:room', (_, res) => {
|
|
453
|
+
res.accept();
|
|
454
|
+
});
|
|
455
|
+
const channel = endpoint.createChannel('/test/:room', (req, res, _) => {
|
|
456
|
+
expect(req.params.room).toBeDefined();
|
|
457
|
+
res.accept();
|
|
458
|
+
});
|
|
459
|
+
channel.on(':room', (req, res) => {
|
|
460
|
+
if (req.params.room === 'TEST') {
|
|
461
|
+
res.reject('TEST');
|
|
462
|
+
}
|
|
463
|
+
});
|
|
464
|
+
const message = {
|
|
465
|
+
action: enums_1.ClientActions.JOIN_CHANNEL, channelName: '/test/socket', event: 'TEST', payload: {}
|
|
466
|
+
};
|
|
467
|
+
const functionToTest = (ws) => {
|
|
468
|
+
try {
|
|
469
|
+
ws.emit('error', {});
|
|
470
|
+
}
|
|
471
|
+
catch (e) {
|
|
472
|
+
console.log(e);
|
|
473
|
+
}
|
|
474
|
+
};
|
|
475
|
+
try {
|
|
476
|
+
yield (0, superwstest_1.default)(server)
|
|
477
|
+
.ws('/api/socket')
|
|
478
|
+
.expectUpgrade(res => expect(res.statusCode).toBe(101))
|
|
479
|
+
.sendJson(message)
|
|
480
|
+
.exec(functionToTest)
|
|
481
|
+
.expectClosed();
|
|
482
|
+
}
|
|
483
|
+
catch (e) {
|
|
484
|
+
console.log(e);
|
|
485
|
+
}
|
|
486
|
+
expect(endpoint.listChannels()).toHaveLength(0); // the socket should have been removed
|
|
487
|
+
expect(endpoint['_findChannel']('/test/socket')).toBeUndefined();
|
|
488
|
+
server.close();
|
|
489
|
+
}));
|
|
675
490
|
});
|