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