@eleven-am/pondsocket 0.1.7 → 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.
@@ -1,26 +1,90 @@
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 __read = (this && this.__read) || function (o, n) {
29
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
30
+ if (!m) return o;
31
+ var i = m.call(o), r, ar = [], e;
32
+ try {
33
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
34
+ }
35
+ catch (error) { e = { error: error }; }
36
+ finally {
37
+ try {
38
+ if (r && !r.done && (m = i["return"])) m.call(i);
39
+ }
40
+ finally { if (e) throw e.error; }
41
+ }
42
+ return ar;
43
+ };
44
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
45
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
46
+ if (ar || !(i in from)) {
47
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
48
+ ar[i] = from[i];
49
+ }
50
+ }
51
+ return to.concat(ar || Array.prototype.slice.call(from));
52
+ };
53
+ var __values = (this && this.__values) || function(o) {
54
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
55
+ if (m) return m.call(o);
56
+ if (o && typeof o.length === "number") return {
57
+ next: function () {
58
+ if (o && i >= o.length) o = void 0;
59
+ return { value: o && o[i++], done: !o };
60
+ }
61
+ };
62
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
63
+ };
2
64
  Object.defineProperty(exports, "__esModule", { value: true });
3
65
  exports.Endpoint = void 0;
4
- const enums_1 = require("./enums");
5
- const pondBase_1 = require("../pondBase");
6
- const pondChannel_1 = require("./pondChannel");
7
- const pondResponse_1 = require("./pondResponse");
8
- class Endpoint extends pondBase_1.BaseClass {
9
- constructor(server, handler) {
10
- super();
11
- this._channels = new pondBase_1.SimpleBase();
12
- this._sockets = new pondBase_1.SimpleBase();
13
- this._handler = handler;
14
- this._server = server;
66
+ var enums_1 = require("./enums");
67
+ var pondBase_1 = require("../pondBase");
68
+ var pondChannel_1 = require("./pondChannel");
69
+ var pondResponse_1 = require("./pondResponse");
70
+ var Endpoint = /** @class */ (function (_super) {
71
+ __extends(Endpoint, _super);
72
+ function Endpoint(server, handler) {
73
+ var _this = _super.call(this) || this;
74
+ _this._channels = new pondBase_1.SimpleBase();
75
+ _this._sockets = new pondBase_1.SimpleBase();
76
+ _this._handler = handler;
77
+ _this._server = server;
78
+ return _this;
15
79
  }
16
80
  /**
17
81
  * @desc Sends a message to a client
18
82
  * @param socket - The socket to send the message to
19
83
  * @param message - The message to send
20
84
  */
21
- static _sendMessage(socket, message) {
85
+ Endpoint._sendMessage = function (socket, message) {
22
86
  socket.send(JSON.stringify(message));
23
- }
87
+ };
24
88
  /**
25
89
  * @desc Accepts a new socket join request to the room provided using the handler function to authorise the socket
26
90
  * @param path - the pattern to accept || can also be a regex
@@ -52,11 +116,11 @@ class Endpoint extends pondBase_1.BaseClass {
52
116
  * });
53
117
  * })
54
118
  */
55
- createChannel(path, handler) {
56
- const pondChannel = new pondChannel_1.PondChannel(path, handler);
119
+ Endpoint.prototype.createChannel = function (path, handler) {
120
+ var pondChannel = new pondChannel_1.PondChannel(path, handler);
57
121
  this._channels.set(path.toString(), pondChannel);
58
122
  return pondChannel;
59
- }
123
+ };
60
124
  /**
61
125
  * @desc Authenticates the client to the endpoint
62
126
  * @param request - Incoming request
@@ -64,27 +128,25 @@ class Endpoint extends pondBase_1.BaseClass {
64
128
  * @param head - Incoming head
65
129
  * @param data - Incoming the data resolved from the handler
66
130
  */
67
- authoriseConnection(request, socket, head, data) {
68
- const doc = this._sockets.createGenericDocument();
69
- const req = {
70
- headers: request.headers,
71
- ...data, clientId: doc.id
72
- };
73
- const resolver = (assigns, data) => {
131
+ Endpoint.prototype.authoriseConnection = function (request, socket, head, data) {
132
+ var _this = this;
133
+ var doc = this._sockets.createGenericDocument();
134
+ var req = __assign(__assign({ headers: request.headers }, data), { clientId: doc.id });
135
+ var resolver = function (assigns, data) {
74
136
  if (data.error) {
75
- socket.write(`HTTP/1.1 ${data.error.code} ${data.error.message}\r\n\r\n`);
137
+ socket.write("HTTP/1.1 ".concat(data.error.code, " ").concat(data.error.message, "\r\n\r\n"));
76
138
  return socket.destroy();
77
139
  }
78
- this._server.handleUpgrade(request, socket, head, (ws) => {
79
- this._server.emit("connection", ws);
80
- const socketCache = {
140
+ _this._server.handleUpgrade(request, socket, head, function (ws) {
141
+ _this._server.emit("connection", ws);
142
+ var socketCache = {
81
143
  socket: ws,
82
144
  assigns: assigns
83
145
  };
84
146
  doc.updateDoc(socketCache);
85
- this._manageSocket(doc);
147
+ _this._manageSocket(doc);
86
148
  if (data.message) {
87
- const newMessage = {
149
+ var newMessage = {
88
150
  action: enums_1.ServerActions.MESSAGE,
89
151
  event: data.message.event,
90
152
  channelName: "SERVER",
@@ -94,131 +156,160 @@ class Endpoint extends pondBase_1.BaseClass {
94
156
  }
95
157
  });
96
158
  };
97
- const res = new pondResponse_1.EndpointResponse(resolver);
159
+ var res = new pondResponse_1.EndpointResponse(resolver);
98
160
  this._handler(req, res, this);
99
- }
161
+ };
100
162
  /**
101
163
  * @desc Closes a client connection to the endpoint.
102
164
  * @param clientId - The id of the client to close the connection to.
103
165
  */
104
- closeConnection(clientId) {
105
- const message = {
166
+ Endpoint.prototype.closeConnection = function (clientId) {
167
+ var message = {
106
168
  action: enums_1.ServerActions.CLOSE,
107
169
  channelName: "SERVER",
108
170
  event: "CLOSED_FROM_SERVER", payload: {}
109
171
  };
110
- const stringifiedMessage = JSON.stringify(message);
111
- const socketDoc = this._sockets.get(clientId);
172
+ var stringifiedMessage = JSON.stringify(message);
173
+ var socketDoc = this._sockets.get(clientId);
112
174
  if (socketDoc) {
113
175
  socketDoc.doc.socket.send(stringifiedMessage);
114
176
  socketDoc.doc.socket.close();
115
177
  socketDoc.removeDoc();
116
178
  }
117
- }
179
+ };
118
180
  /**
119
181
  * @desc Sends a message to a client on the endpoint.
120
182
  * @param clientId - The id of the client to send the message to.
121
183
  * @param event - The event to send the message with.
122
184
  * @param message - The message to send.
123
185
  */
124
- send(clientId, event, message) {
125
- const newMessage = {
186
+ Endpoint.prototype.send = function (clientId, event, message) {
187
+ var _this = this;
188
+ var newMessage = {
126
189
  action: enums_1.ServerActions.MESSAGE,
127
190
  channelName: enums_1.PondSenders.ENDPOINT,
128
- event, payload: message
191
+ event: event,
192
+ payload: message
129
193
  };
130
- const stringifiedMessage = JSON.stringify(newMessage);
131
- const addresses = Array.isArray(clientId) ? clientId : [clientId];
132
- addresses.forEach((address) => {
133
- const socketDoc = this._sockets.get(address);
194
+ var stringifiedMessage = JSON.stringify(newMessage);
195
+ var addresses = Array.isArray(clientId) ? clientId : [clientId];
196
+ addresses.forEach(function (address) {
197
+ var socketDoc = _this._sockets.get(address);
134
198
  if (socketDoc)
135
199
  socketDoc.doc.socket.send(stringifiedMessage);
136
200
  });
137
- }
201
+ };
138
202
  /**
139
203
  * @desc lists all the channels in the endpoint
140
204
  */
141
- listChannels() {
142
- return this._channels.map(channel => channel.info).flat();
143
- }
205
+ Endpoint.prototype.listChannels = function () {
206
+ return this._channels.map(function (channel) { return channel.info; }).flat();
207
+ };
144
208
  /**
145
209
  * @desc lists all the clients in the endpoint
146
210
  */
147
- listConnections() {
148
- return this._sockets.map(socket => socket.socket);
149
- }
211
+ Endpoint.prototype.listConnections = function () {
212
+ return this._sockets.map(function (socket) { return socket.socket; });
213
+ };
150
214
  /**
151
215
  * @desc Broadcasts a message to all clients in the endpoint.
152
216
  * @param event - The event to broadcast.
153
217
  * @param message - The message to broadcast.
154
218
  */
155
- broadcast(event, message) {
156
- const sockets = [...this._sockets.generator()];
157
- const newMessage = {
219
+ Endpoint.prototype.broadcast = function (event, message) {
220
+ var sockets = __spreadArray([], __read(this._sockets.generator()), false);
221
+ var newMessage = {
158
222
  action: enums_1.ServerActions.MESSAGE,
159
223
  channelName: enums_1.PondSenders.ENDPOINT,
160
- event, payload: message
224
+ event: event,
225
+ payload: message
161
226
  };
162
- const stringifiedMessage = JSON.stringify(newMessage);
163
- sockets.forEach(doc => doc.doc.socket.send(stringifiedMessage));
164
- }
227
+ var stringifiedMessage = JSON.stringify(newMessage);
228
+ sockets.forEach(function (doc) { return doc.doc.socket.send(stringifiedMessage); });
229
+ };
165
230
  /**
166
231
  * @desc Searches for a channel in the endpoint.
167
232
  * @param name - The name of the channel to search for.
168
233
  */
169
- _findChannel(name) {
170
- const pond = this._findPondChannel(name);
234
+ Endpoint.prototype._findChannel = function (name) {
235
+ var pond = this._findPondChannel(name);
171
236
  if (pond) {
172
- const channel = pond.doc.getChannel(name);
237
+ var channel = pond.doc.getChannel(name);
173
238
  if (channel)
174
239
  return channel;
175
240
  }
176
241
  return undefined;
177
- }
242
+ };
178
243
  /**
179
244
  * @desc Manages a new socket connection
180
245
  * @param cache - The socket cache
181
246
  * @private
182
247
  */
183
- _manageSocket(cache) {
184
- const socket = cache.doc.socket;
185
- socket.addEventListener("message", (message) => {
186
- this._readMessage(cache, message.data);
248
+ Endpoint.prototype._manageSocket = function (cache) {
249
+ var _this = this;
250
+ var socket = cache.doc.socket;
251
+ socket.addEventListener("message", function (message) {
252
+ _this._readMessage(cache, message.data);
187
253
  });
188
- socket.addEventListener("close", () => {
189
- for (const channel of this._channels.generator())
190
- channel.doc.removeUser(cache.id);
254
+ socket.addEventListener("close", function () {
255
+ var e_1, _a;
256
+ try {
257
+ for (var _b = __values(_this._channels.generator()), _c = _b.next(); !_c.done; _c = _b.next()) {
258
+ var channel = _c.value;
259
+ channel.doc.removeUser(cache.id);
260
+ }
261
+ }
262
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
263
+ finally {
264
+ try {
265
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
266
+ }
267
+ finally { if (e_1) throw e_1.error; }
268
+ }
191
269
  cache.removeDoc();
192
270
  });
193
- socket.addEventListener("error", () => {
194
- for (const channel of this._channels.generator())
195
- channel.doc.removeUser(cache.id);
271
+ socket.addEventListener("error", function () {
272
+ var e_2, _a;
273
+ try {
274
+ for (var _b = __values(_this._channels.generator()), _c = _b.next(); !_c.done; _c = _b.next()) {
275
+ var channel = _c.value;
276
+ channel.doc.removeUser(cache.id);
277
+ }
278
+ }
279
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
280
+ finally {
281
+ try {
282
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
283
+ }
284
+ finally { if (e_2) throw e_2.error; }
285
+ }
196
286
  cache.removeDoc();
197
287
  });
198
- }
288
+ };
199
289
  /**
200
290
  * @desc Finds a pond channel in the endpoint.
201
291
  * @param channelName - The name of the channel to find.
202
292
  * @private
203
293
  */
204
- _findPondChannel(channelName) {
205
- return this._channels.find(channel => this.generateEventRequest(channel.path, channelName) !== null);
206
- }
294
+ Endpoint.prototype._findPondChannel = function (channelName) {
295
+ var _this = this;
296
+ return this._channels.find(function (channel) { return _this.generateEventRequest(channel.path, channelName) !== null; });
297
+ };
207
298
  /**
208
299
  * @desc Handles a message sent from a client
209
300
  * @param cache - The socket cache of the client
210
301
  * @param message - The message to handle
211
302
  * @private
212
303
  */
213
- _readMessage(cache, message) {
214
- const errorMessage = {
304
+ Endpoint.prototype._readMessage = function (cache, message) {
305
+ var errorMessage = {
215
306
  action: enums_1.ServerActions.ERROR,
216
307
  event: "error",
217
308
  channelName: enums_1.PondSenders.ENDPOINT,
218
309
  payload: {}
219
310
  };
220
311
  try {
221
- const data = JSON.parse(message);
312
+ var data = JSON.parse(message);
222
313
  if (!data.action)
223
314
  errorMessage.payload = {
224
315
  message: "No action provided"
@@ -250,18 +341,18 @@ class Endpoint extends pondBase_1.BaseClass {
250
341
  Endpoint._sendMessage(cache.doc.socket, errorMessage);
251
342
  }
252
343
  }
253
- }
344
+ };
254
345
  /**
255
346
  * @desc Deals with a message sent from a client
256
347
  * @param cache - The socket cache of the client
257
348
  * @param message - The message to handle
258
349
  */
259
- _handleMessage(cache, message) {
350
+ Endpoint.prototype._handleMessage = function (cache, message) {
260
351
  switch (message.action) {
261
352
  case "JOIN_CHANNEL":
262
- const pond = this._findPondChannel(message.channelName);
353
+ var pond = this._findPondChannel(message.channelName);
263
354
  if (pond) {
264
- const user = {
355
+ var user = {
265
356
  clientId: cache.id,
266
357
  socket: cache.doc.socket,
267
358
  assigns: cache.doc.assigns
@@ -269,38 +360,38 @@ class Endpoint extends pondBase_1.BaseClass {
269
360
  pond.doc.addUser(user, message.channelName, message.payload);
270
361
  }
271
362
  else
272
- throw new Error(`Channel ${message.channelName} does not exist`);
363
+ throw new Error("Channel ".concat(message.channelName, " does not exist"));
273
364
  break;
274
365
  case "LEAVE_CHANNEL":
275
- this._channelAction(message.channelName, "LEAVE_CHANNEL", channel => {
366
+ this._channelAction(message.channelName, "LEAVE_CHANNEL", function (channel) {
276
367
  channel.removeUser(cache.id);
277
368
  });
278
369
  break;
279
370
  case "BROADCAST_FROM":
280
- this._channelAction(message.channelName, message.event, (channel) => {
371
+ this._channelAction(message.channelName, message.event, function (channel) {
281
372
  channel.broadcastFrom(message.event, message.payload, cache.id);
282
373
  });
283
374
  break;
284
375
  case "BROADCAST":
285
- this._channelAction(message.channelName, message.event, (channel) => {
376
+ this._channelAction(message.channelName, message.event, function (channel) {
286
377
  channel.broadcast(message.event, message.payload, cache.id);
287
378
  });
288
379
  break;
289
380
  case "SEND_MESSAGE_TO_USER":
290
- this._channelAction(message.channelName, message.event, (channel) => {
381
+ this._channelAction(message.channelName, message.event, function (channel) {
291
382
  if (!message.addresses || message.addresses.length === 0)
292
- throw new Error(`No addresses provided`);
383
+ throw new Error("No addresses provided");
293
384
  channel.sendTo(message.event, message.payload, cache.id, message.addresses);
294
385
  });
295
386
  break;
296
387
  case "UPDATE_PRESENCE":
297
- this._channelAction(message.channelName, "UPDATE_PRESENCE", (channel) => {
388
+ this._channelAction(message.channelName, "UPDATE_PRESENCE", function (channel) {
298
389
  var _a, _b;
299
390
  channel.updateUser(cache.id, ((_a = message.payload) === null || _a === void 0 ? void 0 : _a.presence) || {}, ((_b = message.payload) === null || _b === void 0 ? void 0 : _b.assigns) || {});
300
391
  });
301
392
  break;
302
393
  }
303
- }
394
+ };
304
395
  /**
305
396
  * @desc Handles a channel action by finding the channel and executing the callback.
306
397
  * @param channelName - The name of the channel to find.
@@ -308,16 +399,17 @@ class Endpoint extends pondBase_1.BaseClass {
308
399
  * @param action - The action to execute.
309
400
  * @private
310
401
  */
311
- _channelAction(channelName, event, action) {
312
- const channel = this._findChannel(channelName);
402
+ Endpoint.prototype._channelAction = function (channelName, event, action) {
403
+ var channel = this._findChannel(channelName);
313
404
  if (!channel)
314
- throw new Error(`Channel ${channelName} does not exist`);
405
+ throw new Error("Channel ".concat(channelName, " does not exist"));
315
406
  try {
316
407
  return action(channel);
317
408
  }
318
409
  catch (e) {
319
- throw new Error(`Error while executing event '${event}' on channel '${channelName}': ${e.message}`);
410
+ throw new Error("Error while executing event '".concat(event, "' on channel '").concat(channelName, "': ").concat(e.message));
320
411
  }
321
- }
322
- }
412
+ };
413
+ return Endpoint;
414
+ }(pondBase_1.BaseClass));
323
415
  exports.Endpoint = Endpoint;