@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,58 +1,102 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
var __assign = (this && this.__assign) || function () {
|
|
18
|
+
__assign = Object.assign || function(t) {
|
|
19
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
20
|
+
s = arguments[i];
|
|
21
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
22
|
+
t[p] = s[p];
|
|
23
|
+
}
|
|
24
|
+
return t;
|
|
25
|
+
};
|
|
26
|
+
return __assign.apply(this, arguments);
|
|
27
|
+
};
|
|
28
|
+
var __values = (this && this.__values) || function(o) {
|
|
29
|
+
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
30
|
+
if (m) return m.call(o);
|
|
31
|
+
if (o && typeof o.length === "number") return {
|
|
32
|
+
next: function () {
|
|
33
|
+
if (o && i >= o.length) o = void 0;
|
|
34
|
+
return { value: o && o[i++], done: !o };
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
38
|
+
};
|
|
2
39
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
40
|
exports.PondChannel = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
* @desc Gets a list of all the channels in the endpoint.
|
|
20
|
-
*/
|
|
21
|
-
get info() {
|
|
22
|
-
return this._channels.map(channel => channel.info);
|
|
41
|
+
var pondBase_1 = require("../pondBase");
|
|
42
|
+
var channel_1 = require("./channel");
|
|
43
|
+
var enums_1 = require("./enums");
|
|
44
|
+
var pondResponse_1 = require("./pondResponse");
|
|
45
|
+
var channelMiddleWare_1 = require("./channelMiddleWare");
|
|
46
|
+
var PondChannel = /** @class */ (function (_super) {
|
|
47
|
+
__extends(PondChannel, _super);
|
|
48
|
+
function PondChannel(path, handler) {
|
|
49
|
+
var _this = _super.call(this) || this;
|
|
50
|
+
_this._channels = new pondBase_1.PondBase();
|
|
51
|
+
_this._handler = handler;
|
|
52
|
+
_this.path = path;
|
|
53
|
+
_this._subscriptions = {};
|
|
54
|
+
_this._middleware = new channelMiddleWare_1.ChannelMiddleware();
|
|
55
|
+
return _this;
|
|
23
56
|
}
|
|
57
|
+
Object.defineProperty(PondChannel.prototype, "info", {
|
|
58
|
+
/**
|
|
59
|
+
* @desc Gets a list of all the channels in the endpoint.
|
|
60
|
+
*/
|
|
61
|
+
get: function () {
|
|
62
|
+
return this._channels.map(function (channel) { return channel.info; });
|
|
63
|
+
},
|
|
64
|
+
enumerable: false,
|
|
65
|
+
configurable: true
|
|
66
|
+
});
|
|
24
67
|
/**
|
|
25
68
|
* @desc Sends a message to a client
|
|
26
69
|
* @param socket - The socket to send the message to
|
|
27
70
|
* @param message - The message to send
|
|
28
71
|
*/
|
|
29
|
-
|
|
72
|
+
PondChannel._sendMessage = function (socket, message) {
|
|
30
73
|
socket.send(JSON.stringify(message));
|
|
31
|
-
}
|
|
74
|
+
};
|
|
32
75
|
/**
|
|
33
76
|
* @desc A listener for a channel event
|
|
34
77
|
* @param event - The event to listen for, can be a regex
|
|
35
78
|
* @param callback - The callback to call when the event is received
|
|
36
79
|
*/
|
|
37
|
-
on(event, callback) {
|
|
80
|
+
PondChannel.prototype.on = function (event, callback) {
|
|
38
81
|
this._buildHandler(event, callback);
|
|
39
|
-
}
|
|
82
|
+
};
|
|
40
83
|
/**
|
|
41
84
|
* @desc Add new user to channel
|
|
42
85
|
* @param user - The user to add to the channel
|
|
43
86
|
* @param channelName - The name of the channel
|
|
44
87
|
* @param joinParams - The params to join the channel with
|
|
45
88
|
*/
|
|
46
|
-
addUser(user, channelName, joinParams) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
89
|
+
PondChannel.prototype.addUser = function (user, channelName, joinParams) {
|
|
90
|
+
var _this = this;
|
|
91
|
+
var document = this._getChannel(channelName);
|
|
92
|
+
var channel = document.doc;
|
|
93
|
+
var resolver = function (newAssigns, message) {
|
|
94
|
+
var assigns = newAssigns.assigns, presence = newAssigns.presence, channelData = newAssigns.channelData;
|
|
95
|
+
_this._subscriptions[user.clientId] = _this._subscriptions[user.clientId] || [];
|
|
96
|
+
var sub = channel.subscribeToMessages(user.clientId, function (event) {
|
|
53
97
|
PondChannel._sendMessage(user.socket, event);
|
|
54
98
|
});
|
|
55
|
-
|
|
99
|
+
_this._subscriptions[user.clientId].push({ name: channelName, sub: sub });
|
|
56
100
|
channel.addUser({
|
|
57
101
|
presence: presence,
|
|
58
102
|
assigns: assigns,
|
|
@@ -62,65 +106,62 @@ class PondChannel extends pondBase_1.BaseClass {
|
|
|
62
106
|
if (message)
|
|
63
107
|
channel.sendTo(message.event, message.payload, enums_1.PondSenders.POND_CHANNEL, [user.clientId]);
|
|
64
108
|
};
|
|
65
|
-
|
|
66
|
-
|
|
109
|
+
var response = new pondResponse_1.PondChannelResponse(user, resolver);
|
|
110
|
+
var resolved = this.generateEventRequest(this.path, channelName);
|
|
67
111
|
if (resolved === null) {
|
|
68
112
|
document.removeDoc();
|
|
69
|
-
return response.reject(
|
|
113
|
+
return response.reject("Invalid channel name: ".concat(channelName));
|
|
70
114
|
}
|
|
71
|
-
|
|
72
|
-
joinParams, ...resolved,
|
|
73
|
-
clientId: user.clientId, channelName,
|
|
74
|
-
clientAssigns: user.assigns
|
|
75
|
-
};
|
|
115
|
+
var request = __assign(__assign({ joinParams: joinParams }, resolved), { clientId: user.clientId, channelName: channelName, clientAssigns: user.assigns });
|
|
76
116
|
this._handler(request, response, channel);
|
|
77
117
|
if (channel.presence.length === 0)
|
|
78
118
|
document.removeDoc();
|
|
79
119
|
if (!response.isResolved)
|
|
80
120
|
throw new Error("PondChannel: Response was not resolved");
|
|
81
|
-
}
|
|
121
|
+
};
|
|
82
122
|
/**
|
|
83
123
|
* @desc Sends a message to a channel in the endpoint.
|
|
84
124
|
* @param channelName - The name of the channel to send the message to.
|
|
85
125
|
* @param event - The event to send the message with.
|
|
86
126
|
* @param message - The message to send.
|
|
87
127
|
*/
|
|
88
|
-
broadcastToChannel(channelName, event, message) {
|
|
89
|
-
this._execute(channelName, channel
|
|
128
|
+
PondChannel.prototype.broadcastToChannel = function (channelName, event, message) {
|
|
129
|
+
this._execute(channelName, function (channel) {
|
|
90
130
|
channel.broadcast(event, message, enums_1.PondSenders.POND_CHANNEL);
|
|
91
131
|
});
|
|
92
|
-
}
|
|
132
|
+
};
|
|
93
133
|
/**
|
|
94
134
|
* @desc Closes a client connection to a channel in the endpoint.
|
|
95
135
|
* @param channelName - The name of the channel to close the connection to.
|
|
96
136
|
* @param clientId - The id of the client to close the connection to.
|
|
97
137
|
*/
|
|
98
|
-
closeFromChannel(channelName, clientId) {
|
|
99
|
-
|
|
100
|
-
|
|
138
|
+
PondChannel.prototype.closeFromChannel = function (channelName, clientId) {
|
|
139
|
+
var _this = this;
|
|
140
|
+
this._execute(channelName, function (channel) {
|
|
141
|
+
_this._removeSubscriptions(clientId, channelName);
|
|
101
142
|
channel.removeUser(clientId);
|
|
102
143
|
});
|
|
103
|
-
}
|
|
144
|
+
};
|
|
104
145
|
/**
|
|
105
146
|
* @desc Modify the presence of a client in a channel on the endpoint.
|
|
106
147
|
* @param channelName - The name of the channel to modify the presence of.
|
|
107
148
|
* @param clientId - The id of the client to modify the presence of.
|
|
108
149
|
* @param assigns - The assigns to modify the presence with.
|
|
109
150
|
*/
|
|
110
|
-
modifyPresence(channelName, clientId, assigns) {
|
|
111
|
-
this._execute(channelName, channel
|
|
151
|
+
PondChannel.prototype.modifyPresence = function (channelName, clientId, assigns) {
|
|
152
|
+
this._execute(channelName, function (channel) {
|
|
112
153
|
channel.updateUser(clientId, assigns.presence || {}, assigns.assigns || {});
|
|
113
154
|
});
|
|
114
|
-
}
|
|
155
|
+
};
|
|
115
156
|
/**
|
|
116
157
|
* @desc Gets the information of the channel
|
|
117
158
|
* @param channelName - The name of the channel to get the information of.
|
|
118
159
|
*/
|
|
119
|
-
getChannelInfo(channelName) {
|
|
120
|
-
return this._execute(channelName, channel
|
|
160
|
+
PondChannel.prototype.getChannelInfo = function (channelName) {
|
|
161
|
+
return this._execute(channelName, function (channel) {
|
|
121
162
|
return channel.info;
|
|
122
163
|
});
|
|
123
|
-
}
|
|
164
|
+
};
|
|
124
165
|
/**
|
|
125
166
|
* @desc Sends a message to the channel
|
|
126
167
|
* @param channelName - The name of the channel to send the message to.
|
|
@@ -128,92 +169,104 @@ class PondChannel extends pondBase_1.BaseClass {
|
|
|
128
169
|
* @param event - The event to send the message to
|
|
129
170
|
* @param message - The message to send
|
|
130
171
|
*/
|
|
131
|
-
send(channelName, clientId, event, message) {
|
|
132
|
-
|
|
133
|
-
this._execute(channelName, channel
|
|
172
|
+
PondChannel.prototype.send = function (channelName, clientId, event, message) {
|
|
173
|
+
var clients = Array.isArray(clientId) ? clientId : [clientId];
|
|
174
|
+
this._execute(channelName, function (channel) {
|
|
134
175
|
channel.sendTo(event, message, enums_1.PondSenders.POND_CHANNEL, clients);
|
|
135
176
|
});
|
|
136
|
-
}
|
|
177
|
+
};
|
|
137
178
|
/**
|
|
138
179
|
* @desc Searches for a channel in the endpoint.
|
|
139
180
|
* @param channelName - The name of the channel to search for.
|
|
140
181
|
*/
|
|
141
|
-
getChannel(channelName) {
|
|
182
|
+
PondChannel.prototype.getChannel = function (channelName) {
|
|
142
183
|
var _a;
|
|
143
184
|
return ((_a = this._channels.get(channelName)) === null || _a === void 0 ? void 0 : _a.doc) || null;
|
|
144
|
-
}
|
|
185
|
+
};
|
|
145
186
|
/**
|
|
146
187
|
* @desc removes a user from all channels
|
|
147
188
|
* @param clientId - The id of the client to remove
|
|
148
189
|
*/
|
|
149
|
-
removeUser(clientId) {
|
|
190
|
+
PondChannel.prototype.removeUser = function (clientId) {
|
|
191
|
+
var e_1, _a;
|
|
150
192
|
if (this._subscriptions[clientId]) {
|
|
151
|
-
this._subscriptions[clientId].forEach(doc
|
|
193
|
+
this._subscriptions[clientId].forEach(function (doc) { return doc.sub.unsubscribe(); });
|
|
152
194
|
delete this._subscriptions[clientId];
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
channel.
|
|
195
|
+
try {
|
|
196
|
+
for (var _b = __values(this._channels), _c = _b.next(); !_c.done; _c = _b.next()) {
|
|
197
|
+
var channel = _c.value;
|
|
198
|
+
if (channel.doc.hasUser(clientId))
|
|
199
|
+
channel.doc.removeUser(clientId);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
203
|
+
finally {
|
|
204
|
+
try {
|
|
205
|
+
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
206
|
+
}
|
|
207
|
+
finally { if (e_1) throw e_1.error; }
|
|
208
|
+
}
|
|
156
209
|
}
|
|
157
|
-
}
|
|
210
|
+
};
|
|
158
211
|
/**
|
|
159
212
|
* @desc Executes a function on a channel in the endpoint.
|
|
160
213
|
* @param channelName - The name of the channel to execute the function on.
|
|
161
214
|
* @param handler - The function to execute on the channel.
|
|
162
215
|
* @private
|
|
163
216
|
*/
|
|
164
|
-
_execute(channelName, handler) {
|
|
165
|
-
|
|
217
|
+
PondChannel.prototype._execute = function (channelName, handler) {
|
|
218
|
+
var newChannel = this.getChannel(channelName);
|
|
166
219
|
if (newChannel)
|
|
167
220
|
return handler(newChannel);
|
|
168
|
-
throw new Error(
|
|
169
|
-
}
|
|
221
|
+
throw new Error("Channel ".concat(channelName, " does not exist"));
|
|
222
|
+
};
|
|
170
223
|
/**
|
|
171
224
|
* @desc Creates a new channel in the endpoint.
|
|
172
225
|
* @param channelName - The name of the channel to create.
|
|
173
226
|
* @private
|
|
174
227
|
*/
|
|
175
|
-
_getChannel(channelName) {
|
|
176
|
-
|
|
177
|
-
|
|
228
|
+
PondChannel.prototype._getChannel = function (channelName) {
|
|
229
|
+
var _this = this;
|
|
230
|
+
return this._channels.getOrCreate(channelName, function (doc) {
|
|
231
|
+
return new channel_1.Channel(channelName, _this._middleware, doc.removeDoc.bind(doc));
|
|
178
232
|
});
|
|
179
|
-
}
|
|
233
|
+
};
|
|
180
234
|
/**
|
|
181
235
|
* @desc Removes a subscription from a user
|
|
182
236
|
* @param clientId - The id of the client to remove the subscription from
|
|
183
237
|
* @param channelName - The name of the channel to remove the subscription from
|
|
184
238
|
* @private
|
|
185
239
|
*/
|
|
186
|
-
_removeSubscriptions(clientId, channelName) {
|
|
187
|
-
|
|
188
|
-
clients.
|
|
189
|
-
|
|
240
|
+
PondChannel.prototype._removeSubscriptions = function (clientId, channelName) {
|
|
241
|
+
var _this = this;
|
|
242
|
+
var clients = Array.isArray(clientId) ? clientId : [clientId];
|
|
243
|
+
clients.forEach(function (client) {
|
|
244
|
+
var subs = _this._subscriptions[client];
|
|
190
245
|
if (subs) {
|
|
191
|
-
|
|
246
|
+
var sub = subs.find(function (s) { return s.name === channelName; });
|
|
192
247
|
if (sub) {
|
|
193
248
|
sub.sub.unsubscribe();
|
|
194
249
|
subs.splice(subs.indexOf(sub), 1);
|
|
195
250
|
}
|
|
196
251
|
}
|
|
197
252
|
});
|
|
198
|
-
}
|
|
253
|
+
};
|
|
199
254
|
/**
|
|
200
255
|
* @desc Builds an event handler for a channel
|
|
201
256
|
* @param event - The event to build the handler for
|
|
202
257
|
* @param callback - The callback to build the handler for
|
|
203
258
|
* @private
|
|
204
259
|
*/
|
|
205
|
-
_buildHandler(event, callback) {
|
|
206
|
-
|
|
207
|
-
|
|
260
|
+
PondChannel.prototype._buildHandler = function (event, callback) {
|
|
261
|
+
var _this = this;
|
|
262
|
+
this._middleware.use(function (data, res, channel) {
|
|
263
|
+
var info = _this.generateEventRequest(event, data.event);
|
|
208
264
|
if (!info)
|
|
209
265
|
return;
|
|
210
|
-
|
|
211
|
-
...data,
|
|
212
|
-
params: info.params,
|
|
213
|
-
query: info.query,
|
|
214
|
-
};
|
|
266
|
+
var req = __assign(__assign({}, data), { params: info.params, query: info.query });
|
|
215
267
|
callback(req, res, channel);
|
|
216
268
|
});
|
|
217
|
-
}
|
|
218
|
-
|
|
269
|
+
};
|
|
270
|
+
return PondChannel;
|
|
271
|
+
}(pondBase_1.BaseClass));
|
|
219
272
|
exports.PondChannel = PondChannel;
|
|
@@ -1,34 +1,34 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
3
|
+
var pondChannel_1 = require("./pondChannel");
|
|
4
|
+
var channel_test_1 = require("./channel.test");
|
|
5
|
+
var enums_1 = require("./enums");
|
|
6
|
+
var channel_1 = require("./channel");
|
|
7
|
+
var pondBase_1 = require("../pondBase");
|
|
8
|
+
var createPondChannel = function (path, handler) {
|
|
9
9
|
path = path || "/pond";
|
|
10
10
|
handler = handler || jest.fn();
|
|
11
11
|
return new pondChannel_1.PondChannel(path, handler);
|
|
12
12
|
};
|
|
13
|
-
describe('PondChannel', ()
|
|
14
|
-
it('should exists', ()
|
|
13
|
+
describe('PondChannel', function () {
|
|
14
|
+
it('should exists', function () {
|
|
15
15
|
expect(pondChannel_1.PondChannel).toBeDefined();
|
|
16
16
|
});
|
|
17
|
-
it('should be a class', ()
|
|
17
|
+
it('should be a class', function () {
|
|
18
18
|
expect(pondChannel_1.PondChannel).toBeInstanceOf(Function);
|
|
19
19
|
});
|
|
20
|
-
it('should be able to get the info of a channel', ()
|
|
21
|
-
|
|
22
|
-
|
|
20
|
+
it('should be able to get the info of a channel', function () {
|
|
21
|
+
var pondChannel = createPondChannel();
|
|
22
|
+
var channel = (0, channel_test_1.createChannel)('/channel').channel;
|
|
23
23
|
// Because we want to get the info of the channel, we need to add it to the pondChannel
|
|
24
24
|
// for testing purposes
|
|
25
25
|
pondChannel['_channels'].set(channel.name, channel);
|
|
26
26
|
expect(pondChannel.info).toEqual([channel.info]);
|
|
27
27
|
});
|
|
28
|
-
it('should be able ot add a user', ()
|
|
29
|
-
|
|
30
|
-
expect(()
|
|
31
|
-
|
|
28
|
+
it('should be able ot add a user', function () {
|
|
29
|
+
var pondChannel = createPondChannel('/test');
|
|
30
|
+
expect(function () { return pondChannel.getChannelInfo('/test'); }).toThrow(); // This throws an error because the channel does not exist anymore
|
|
31
|
+
var testUser = {
|
|
32
32
|
clientId: 'test', assigns: {}, socket: {
|
|
33
33
|
send: jest.fn(), on: jest.fn(),
|
|
34
34
|
}
|
|
@@ -37,25 +37,25 @@ describe('PondChannel', () => {
|
|
|
37
37
|
// /balls !== /test so we expect an error message to be sent to the client from the pond channel
|
|
38
38
|
expect(testUser.socket.send).toBeCalledWith(JSON.stringify({
|
|
39
39
|
action: enums_1.ServerActions.ERROR, event: "JOIN_REQUEST_ERROR", channelName: enums_1.PondSenders.POND_CHANNEL, payload: {
|
|
40
|
-
message:
|
|
40
|
+
message: "Invalid channel name: /balls", code: 403,
|
|
41
41
|
}
|
|
42
42
|
}));
|
|
43
43
|
expect(pondChannel.getChannel('/test')).toBeNull(); // The channel does not exist yet
|
|
44
|
-
expect(()
|
|
44
|
+
expect(function () { return pondChannel.addUser(testUser, '/test', {}); })
|
|
45
45
|
.toThrow(); // handler function does not act on the incoming connection, it is just a jest mock function
|
|
46
46
|
// /test === /test so we expect the user to be added to the channel
|
|
47
47
|
// if the channel does not exist, it will be created
|
|
48
48
|
expect(pondChannel.getChannel('/test')).toBeNull(); // The channel does not exist yet
|
|
49
|
-
|
|
49
|
+
var newPondChannel = new pondChannel_1.PondChannel('/test', function (_, res) {
|
|
50
50
|
res.accept(); // here we accept the connection
|
|
51
51
|
});
|
|
52
52
|
newPondChannel.addUser(testUser, '/test', {});
|
|
53
53
|
expect(newPondChannel.getChannel('/test')).not.toBeNull(); // The channel does exist now
|
|
54
|
-
|
|
54
|
+
var newChannel = newPondChannel.getChannel('/test');
|
|
55
55
|
expect(newChannel).toBeDefined();
|
|
56
56
|
expect(newChannel).toBeInstanceOf(channel_1.Channel);
|
|
57
57
|
expect(newChannel === null || newChannel === void 0 ? void 0 : newChannel.name).toBe('/test');
|
|
58
|
-
|
|
58
|
+
var rejectPondChannel = new pondChannel_1.PondChannel('/:test', function (req, res) {
|
|
59
59
|
if (req.params.test === 'balls')
|
|
60
60
|
res.reject(); // here we reject the connection
|
|
61
61
|
else if (req.params.test === 'rejectWithMessage')
|
|
@@ -66,24 +66,24 @@ describe('PondChannel', () => {
|
|
|
66
66
|
expect(rejectPondChannel.getChannel('/rejectWithMessage')).toBeNull(); // The channel does not exist as it rejects the connection and deletes itself
|
|
67
67
|
expect(testUser.socket.send).toBeCalledWith(JSON.stringify({
|
|
68
68
|
action: enums_1.ServerActions.ERROR, event: "JOIN_REQUEST_ERROR", channelName: enums_1.PondSenders.POND_CHANNEL, payload: {
|
|
69
|
-
message:
|
|
69
|
+
message: "test", code: 69420,
|
|
70
70
|
}
|
|
71
71
|
}));
|
|
72
72
|
rejectPondChannel.addUser(testUser, '/balls', {});
|
|
73
73
|
expect(rejectPondChannel.getChannel('/balls')).toBeNull(); // The channel does not exist as it rejects the connection and deletes itself
|
|
74
74
|
expect(testUser.socket.send).toBeCalledWith(JSON.stringify({
|
|
75
75
|
action: enums_1.ServerActions.ERROR, event: "JOIN_REQUEST_ERROR", channelName: enums_1.PondSenders.POND_CHANNEL, payload: {
|
|
76
|
-
message:
|
|
76
|
+
message: "Unauthorized join request", code: 403,
|
|
77
77
|
}
|
|
78
78
|
}));
|
|
79
|
-
|
|
79
|
+
var acceptPondChannel = new pondChannel_1.PondChannel('/:test', function (_, res) {
|
|
80
80
|
res.send('test', {
|
|
81
81
|
test: 'test'
|
|
82
82
|
}); // here we send a message after accepting the connection
|
|
83
83
|
});
|
|
84
84
|
testUser.socket.send.mockClear(); // Clear the mock function
|
|
85
|
-
|
|
86
|
-
testUser.socket.send = (message)
|
|
85
|
+
var messages = [];
|
|
86
|
+
testUser.socket.send = function (message) {
|
|
87
87
|
messages.push(JSON.parse(message));
|
|
88
88
|
};
|
|
89
89
|
acceptPondChannel.addUser(testUser, '/test', {});
|
|
@@ -97,18 +97,18 @@ describe('PondChannel', () => {
|
|
|
97
97
|
}
|
|
98
98
|
}]);
|
|
99
99
|
});
|
|
100
|
-
it('should be able to receive subscriptions', ()
|
|
101
|
-
|
|
100
|
+
it('should be able to receive subscriptions', function () {
|
|
101
|
+
var pond = new pondChannel_1.PondChannel('/:test', function (_, res) {
|
|
102
102
|
res.accept();
|
|
103
103
|
});
|
|
104
104
|
//when events are added, they are added sequentially
|
|
105
105
|
// this means the first handler is called first, and the second handler is called second...
|
|
106
106
|
// so if a regex handler should be added last so everything else can be tried first
|
|
107
|
-
|
|
108
|
-
pond.on('event:test', (req, res)
|
|
107
|
+
var narrowedMessageCount = 0;
|
|
108
|
+
pond.on('event:test', function (req, res) {
|
|
109
109
|
narrowedMessageCount++;
|
|
110
110
|
if (req.params.test === 'balls')
|
|
111
|
-
expect(()
|
|
111
|
+
expect(function () { return res.reject(); }).toThrow(); // this would throw an error because the sender is the pond channel itself
|
|
112
112
|
// we know this because we wrote the test that way, to confirm who the sender is you can check the req.client property
|
|
113
113
|
else if (req.params.test === 'rejectWithMessage')
|
|
114
114
|
res.reject('test', 69420); // here we reject the connection with a message and a status code
|
|
@@ -149,8 +149,8 @@ describe('PondChannel', () => {
|
|
|
149
149
|
}
|
|
150
150
|
});
|
|
151
151
|
});
|
|
152
|
-
|
|
153
|
-
pond.on(/(.*?)/, (req, res)
|
|
152
|
+
var encompassingMessageCount = 0;
|
|
153
|
+
pond.on(/(.*?)/, function (req, res) {
|
|
154
154
|
console.log(req.event);
|
|
155
155
|
expect(req.params).toEqual({});
|
|
156
156
|
// with all encompassing regex, the params should be empty
|
|
@@ -161,8 +161,8 @@ describe('PondChannel', () => {
|
|
|
161
161
|
message: 'This is a fallback route'
|
|
162
162
|
});
|
|
163
163
|
});
|
|
164
|
-
|
|
165
|
-
|
|
164
|
+
var userMessageCount = 0;
|
|
165
|
+
var sender = function () {
|
|
166
166
|
userMessageCount++;
|
|
167
167
|
};
|
|
168
168
|
// we add two users to the pond on ethe channel /pond
|
|
@@ -207,7 +207,7 @@ describe('PondChannel', () => {
|
|
|
207
207
|
encompassingMessageCount = 0;
|
|
208
208
|
userMessageCount = 0;
|
|
209
209
|
narrowedMessageCount = 0;
|
|
210
|
-
|
|
210
|
+
var channel = pond.getChannel('/pond');
|
|
211
211
|
expect(channel).toBeInstanceOf(channel_1.Channel);
|
|
212
212
|
expect(channel).not.toBeNull();
|
|
213
213
|
// since the PondChannel has no way to send messages on behalf of a client we get the channel from the pond and send the message directly to the channel
|
|
@@ -339,12 +339,12 @@ describe('PondChannel', () => {
|
|
|
339
339
|
assigns: [{}],
|
|
340
340
|
});
|
|
341
341
|
});
|
|
342
|
-
it('should be capable of removing a user from a channel', ()
|
|
343
|
-
|
|
342
|
+
it('should be capable of removing a user from a channel', function () {
|
|
343
|
+
var pond = new pondChannel_1.PondChannel('/test', function (_, res) {
|
|
344
344
|
res.accept(); // here we accept the connection
|
|
345
345
|
});
|
|
346
|
-
|
|
347
|
-
|
|
346
|
+
var userMessageCount = 0;
|
|
347
|
+
var sender = function () {
|
|
348
348
|
userMessageCount++;
|
|
349
349
|
};
|
|
350
350
|
pond.addUser({
|