@eleven-am/pondsocket 0.1.11 → 0.1.13

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.
Files changed (90) hide show
  1. package/dist/client/channel.js +200 -0
  2. package/dist/client/index.d.ts +122 -0
  3. package/{pondClient/socket.js → dist/client/index.js} +30 -44
  4. package/dist/express/index.d.ts +36 -0
  5. package/dist/express/index.js +16 -0
  6. package/dist/index.d.ts +340 -0
  7. package/dist/index.js +4 -0
  8. package/dist/server/abstracts/abstractRequest.js +40 -0
  9. package/dist/server/abstracts/abstractRequest.test.js +41 -0
  10. package/dist/server/abstracts/abstractResponse.js +6 -0
  11. package/dist/server/abstracts/middleware.js +38 -0
  12. package/dist/server/abstracts/middleware.test.js +70 -0
  13. package/dist/server/channel/channelEngine.js +279 -0
  14. package/dist/server/channel/channelEngine.test.js +377 -0
  15. package/dist/server/channel/channelRequest.test.js +29 -0
  16. package/dist/server/channel/channelResponse.test.js +134 -0
  17. package/dist/server/channel/eventRequest.js +18 -0
  18. package/dist/server/channel/eventResponse.js +141 -0
  19. package/dist/server/endpoint/connectionResponse.js +50 -0
  20. package/dist/server/endpoint/endpoint.js +269 -0
  21. package/dist/server/endpoint/endpoint.test.js +406 -0
  22. package/dist/server/endpoint/endpointResponse.test.js +43 -0
  23. package/dist/server/pondChannel/joinRequest.js +29 -0
  24. package/dist/server/pondChannel/joinResponse.js +96 -0
  25. package/dist/server/pondChannel/pondChannel.js +161 -0
  26. package/dist/server/pondChannel/pondChannelResponse.test.js +108 -0
  27. package/dist/server/presence/presenceEngine.js +112 -0
  28. package/dist/server/presence/presenceEngine.test.js +104 -0
  29. package/dist/server/server/pondSocket.js +122 -0
  30. package/{pondSocket → dist/server/server}/server.test.js +7 -21
  31. package/{pondBase/baseClass.js → dist/server/utils/matchPattern.js} +33 -43
  32. package/{pondBase/baseClass.test.js → dist/server/utils/matchPattern.test.js} +16 -25
  33. package/dist/server/utils/subjectUtils.js +68 -0
  34. package/package.json +31 -12
  35. package/.eslintrc.js +0 -28
  36. package/base.d.ts +0 -1
  37. package/base.js +0 -17
  38. package/client.d.ts +0 -1
  39. package/client.js +0 -17
  40. package/index.d.ts +0 -1
  41. package/index.js +0 -17
  42. package/jest.config.js +0 -11
  43. package/pondBase/baseClass.d.ts +0 -55
  44. package/pondBase/enums.d.ts +0 -9
  45. package/pondBase/enums.js +0 -14
  46. package/pondBase/index.d.ts +0 -6
  47. package/pondBase/index.js +0 -22
  48. package/pondBase/pondBase.d.ts +0 -41
  49. package/pondBase/pondBase.js +0 -60
  50. package/pondBase/pondBase.test.js +0 -101
  51. package/pondBase/pubSub.d.ts +0 -82
  52. package/pondBase/pubSub.js +0 -158
  53. package/pondBase/pubSub.test.js +0 -332
  54. package/pondBase/simpleBase.d.ts +0 -126
  55. package/pondBase/simpleBase.js +0 -211
  56. package/pondBase/simpleBase.test.js +0 -153
  57. package/pondBase/types.d.ts +0 -2
  58. package/pondBase/types.js +0 -2
  59. package/pondClient/channel.d.ts +0 -77
  60. package/pondClient/channel.js +0 -167
  61. package/pondClient/index.d.ts +0 -2
  62. package/pondClient/index.js +0 -18
  63. package/pondClient/socket.d.ts +0 -41
  64. package/pondSocket/channel.d.ts +0 -129
  65. package/pondSocket/channel.js +0 -287
  66. package/pondSocket/channel.test.js +0 -377
  67. package/pondSocket/channelMiddleWare.d.ts +0 -28
  68. package/pondSocket/channelMiddleWare.js +0 -36
  69. package/pondSocket/endpoint.d.ts +0 -90
  70. package/pondSocket/endpoint.js +0 -320
  71. package/pondSocket/endpoint.test.js +0 -490
  72. package/pondSocket/enums.d.ts +0 -19
  73. package/pondSocket/enums.js +0 -25
  74. package/pondSocket/index.d.ts +0 -7
  75. package/pondSocket/index.js +0 -23
  76. package/pondSocket/pondChannel.d.ts +0 -79
  77. package/pondSocket/pondChannel.js +0 -211
  78. package/pondSocket/pondChannel.test.js +0 -430
  79. package/pondSocket/pondResponse.d.ts +0 -25
  80. package/pondSocket/pondResponse.js +0 -120
  81. package/pondSocket/pondSocket.d.ts +0 -47
  82. package/pondSocket/pondSocket.js +0 -94
  83. package/pondSocket/socketMiddleWare.d.ts +0 -6
  84. package/pondSocket/socketMiddleWare.js +0 -32
  85. package/pondSocket/types.d.ts +0 -74
  86. package/pondSocket/types.js +0 -2
  87. package/socket.d.ts +0 -1
  88. package/socket.js +0 -17
  89. package/tsconfig.eslint.json +0 -5
  90. package/tsconfig.json +0 -90
@@ -1,320 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- 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;
15
- }
16
- /**
17
- * @desc Sends a message to a client
18
- * @param socket - The socket to send the message to
19
- * @param message - The message to send
20
- */
21
- static _sendMessage(socket, message) {
22
- socket.send(JSON.stringify(message));
23
- }
24
- /**
25
- * @desc Accepts a new socket join request to the room provided using the handler function to authorise the socket
26
- * @param path - the pattern to accept || can also be a regex
27
- * @param handler - the handler function to authenticate the socket
28
- *
29
- * @example
30
- * const channel = endpoint.createChannel('channel:*', (req, res) => {
31
- * const isAdmin = req.clientAssigns.admin;
32
- * if (!isAdmin)
33
- * return res.reject("You are not an admin");
34
- *
35
- * res.accept({
36
- * assign: {
37
- * admin: true,
38
- * joinedDate: new Date()
39
- * },
40
- * presence: {state: 'online'},
41
- * channelData: {private: true}
42
- * });
43
- * });
44
- *
45
- * channel.on('ping', (req, res, channel) => {
46
- * const users = channel.getPresence();
47
- * res.assign({
48
- * assign: {
49
- * pingDate: new Date(),
50
- * users: users.length
51
- * }
52
- * });
53
- * })
54
- */
55
- createChannel(path, handler) {
56
- const pondChannel = new pondChannel_1.PondChannel(path, handler);
57
- this._channels.set(path.toString(), pondChannel);
58
- return pondChannel;
59
- }
60
- /**
61
- * @desc Authenticates the client to the endpoint
62
- * @param request - Incoming request
63
- * @param socket - Incoming socket
64
- * @param head - Incoming head
65
- * @param data - Incoming the data resolved from the handler
66
- */
67
- authoriseConnection(request, socket, head, data) {
68
- const doc = this._sockets.createGenericDocument();
69
- const req = Object.assign(Object.assign({ headers: request.headers }, data), { clientId: doc.id });
70
- const resolver = (assigns, data) => {
71
- if (data.error) {
72
- socket.write(`HTTP/1.1 ${data.error.code} ${data.error.message}\r\n\r\n`);
73
- return socket.destroy();
74
- }
75
- this._server.handleUpgrade(request, socket, head, (ws) => {
76
- this._server.emit("connection", ws);
77
- const socketCache = {
78
- socket: ws,
79
- assigns: assigns
80
- };
81
- doc.updateDoc(socketCache);
82
- this._manageSocket(doc);
83
- if (data.message) {
84
- const newMessage = {
85
- action: enums_1.ServerActions.MESSAGE,
86
- event: data.message.event,
87
- channelName: "SERVER",
88
- payload: data.message.payload
89
- };
90
- Endpoint._sendMessage(ws, newMessage);
91
- }
92
- });
93
- };
94
- const res = new pondResponse_1.EndpointResponse(resolver);
95
- this._handler(req, res, this);
96
- }
97
- /**
98
- * @desc Closes a client connection to the endpoint.
99
- * @param clientId - The id of the client to close the connection to.
100
- */
101
- closeConnection(clientId) {
102
- const message = {
103
- action: enums_1.ServerActions.CLOSE,
104
- channelName: "SERVER",
105
- event: "CLOSED_FROM_SERVER", payload: {}
106
- };
107
- const stringifiedMessage = JSON.stringify(message);
108
- const socketDoc = this._sockets.get(clientId);
109
- if (socketDoc) {
110
- socketDoc.doc.socket.send(stringifiedMessage);
111
- socketDoc.doc.socket.close();
112
- socketDoc.removeDoc();
113
- }
114
- }
115
- /**
116
- * @desc Sends a message to a client on the endpoint.
117
- * @param clientId - The id of the client to send the message to.
118
- * @param event - The event to send the message with.
119
- * @param message - The message to send.
120
- */
121
- send(clientId, event, message) {
122
- const newMessage = {
123
- action: enums_1.ServerActions.MESSAGE,
124
- channelName: enums_1.PondSenders.ENDPOINT,
125
- event, payload: message
126
- };
127
- const stringifiedMessage = JSON.stringify(newMessage);
128
- const addresses = Array.isArray(clientId) ? clientId : [clientId];
129
- addresses.forEach((address) => {
130
- const socketDoc = this._sockets.get(address);
131
- if (socketDoc)
132
- socketDoc.doc.socket.send(stringifiedMessage);
133
- });
134
- }
135
- /**
136
- * @desc lists all the channels in the endpoint
137
- */
138
- listChannels() {
139
- return this._channels.map(channel => channel.info).flat();
140
- }
141
- /**
142
- * @desc lists all the clients in the endpoint
143
- */
144
- listConnections() {
145
- return this._sockets.map(socket => socket.socket);
146
- }
147
- /**
148
- * @desc Broadcasts a message to all clients in the endpoint.
149
- * @param event - The event to broadcast.
150
- * @param message - The message to broadcast.
151
- */
152
- broadcast(event, message) {
153
- const sockets = [...this._sockets.generator()];
154
- const newMessage = {
155
- action: enums_1.ServerActions.MESSAGE,
156
- channelName: enums_1.PondSenders.ENDPOINT,
157
- event, payload: message
158
- };
159
- const stringifiedMessage = JSON.stringify(newMessage);
160
- sockets.forEach(doc => doc.doc.socket.send(stringifiedMessage));
161
- }
162
- /**
163
- * @desc Searches for a channel in the endpoint.
164
- * @param name - The name of the channel to search for.
165
- */
166
- _findChannel(name) {
167
- const pond = this._findPondChannel(name);
168
- if (pond) {
169
- const channel = pond.doc.getChannel(name);
170
- if (channel)
171
- return channel;
172
- }
173
- return undefined;
174
- }
175
- /**
176
- * @desc Manages a new socket connection
177
- * @param cache - The socket cache
178
- * @private
179
- */
180
- _manageSocket(cache) {
181
- const socket = cache.doc.socket;
182
- socket.addEventListener("message", (message) => {
183
- this._readMessage(cache, message.data);
184
- });
185
- socket.addEventListener("close", () => {
186
- for (const channel of this._channels.generator())
187
- channel.doc.removeUser(cache.id);
188
- cache.removeDoc();
189
- });
190
- socket.addEventListener("error", () => {
191
- for (const channel of this._channels.generator())
192
- channel.doc.removeUser(cache.id);
193
- cache.removeDoc();
194
- });
195
- }
196
- /**
197
- * @desc Finds a pond channel in the endpoint.
198
- * @param channelName - The name of the channel to find.
199
- * @private
200
- */
201
- _findPondChannel(channelName) {
202
- return this._channels.find(channel => this.generateEventRequest(channel.path, channelName) !== null);
203
- }
204
- /**
205
- * @desc Handles a message sent from a client
206
- * @param cache - The socket cache of the client
207
- * @param message - The message to handle
208
- * @private
209
- */
210
- _readMessage(cache, message) {
211
- const errorMessage = {
212
- action: enums_1.ServerActions.ERROR,
213
- event: "error",
214
- channelName: enums_1.PondSenders.ENDPOINT,
215
- payload: {}
216
- };
217
- try {
218
- const data = JSON.parse(message);
219
- if (!data.action)
220
- errorMessage.payload = {
221
- message: "No action provided"
222
- };
223
- else if (!data.channelName)
224
- errorMessage.payload = {
225
- message: "No channel name provided"
226
- };
227
- else if (!data.payload)
228
- errorMessage.payload = {
229
- message: "No payload provided"
230
- };
231
- else
232
- this._handleMessage(cache, data);
233
- if (!this.isObjectEmpty(errorMessage.payload))
234
- Endpoint._sendMessage(cache.doc.socket, errorMessage);
235
- }
236
- catch (e) {
237
- if (e instanceof SyntaxError) {
238
- errorMessage.payload = {
239
- message: "Invalid JSON"
240
- };
241
- Endpoint._sendMessage(cache.doc.socket, errorMessage);
242
- }
243
- else if (e instanceof Error) {
244
- errorMessage.payload = {
245
- message: e.message,
246
- };
247
- Endpoint._sendMessage(cache.doc.socket, errorMessage);
248
- }
249
- }
250
- }
251
- /**
252
- * @desc Deals with a message sent from a client
253
- * @param cache - The socket cache of the client
254
- * @param message - The message to handle
255
- */
256
- _handleMessage(cache, message) {
257
- switch (message.action) {
258
- case "JOIN_CHANNEL":
259
- const pond = this._findPondChannel(message.channelName);
260
- if (pond) {
261
- const user = {
262
- clientId: cache.id,
263
- socket: cache.doc.socket,
264
- assigns: cache.doc.assigns
265
- };
266
- pond.doc.addUser(user, message.channelName, message.payload);
267
- }
268
- else
269
- throw new Error(`Channel ${message.channelName} does not exist`);
270
- break;
271
- case "LEAVE_CHANNEL":
272
- this._channelAction(message.channelName, "LEAVE_CHANNEL", channel => {
273
- channel.removeUser(cache.id);
274
- });
275
- break;
276
- case "BROADCAST_FROM":
277
- this._channelAction(message.channelName, message.event, (channel) => {
278
- channel.broadcastFrom(message.event, message.payload, cache.id);
279
- });
280
- break;
281
- case "BROADCAST":
282
- this._channelAction(message.channelName, message.event, (channel) => {
283
- channel.broadcast(message.event, message.payload, cache.id);
284
- });
285
- break;
286
- case "SEND_MESSAGE_TO_USER":
287
- this._channelAction(message.channelName, message.event, (channel) => {
288
- if (!message.addresses || message.addresses.length === 0)
289
- throw new Error(`No addresses provided`);
290
- channel.sendTo(message.event, message.payload, cache.id, message.addresses);
291
- });
292
- break;
293
- case "UPDATE_PRESENCE":
294
- this._channelAction(message.channelName, "UPDATE_PRESENCE", (channel) => {
295
- var _a, _b;
296
- 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) || {});
297
- });
298
- break;
299
- }
300
- }
301
- /**
302
- * @desc Handles a channel action by finding the channel and executing the callback.
303
- * @param channelName - The name of the channel to find.
304
- * @param event - The event to execute.
305
- * @param action - The action to execute.
306
- * @private
307
- */
308
- _channelAction(channelName, event, action) {
309
- const channel = this._findChannel(channelName);
310
- if (!channel)
311
- throw new Error(`Channel ${channelName} does not exist`);
312
- try {
313
- return action(channel);
314
- }
315
- catch (e) {
316
- throw new Error(`Error while executing event '${event}' on channel '${channelName}': ${e.message}`);
317
- }
318
- }
319
- }
320
- exports.Endpoint = Endpoint;