@eleven-am/pondsocket 0.1.145 → 0.1.147

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.
@@ -56,6 +56,12 @@ class ChannelEngine {
56
56
  get size() {
57
57
  return __classPrivateFieldGet(this, _ChannelEngine_users, "f").size;
58
58
  }
59
+ /**
60
+ * @desc Gets the parent engine
61
+ */
62
+ get parent() {
63
+ return __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f");
64
+ }
59
65
  /**
60
66
  * @desc Adds a user to the channel
61
67
  * @param userId - The id of the user to add
@@ -13,7 +13,6 @@ const createParentEngine = () => {
13
13
  channelName: 'channel',
14
14
  requestId: 'requestId',
15
15
  subscriptions: new Map(),
16
- pendingSubscriptions: new Set(),
17
16
  socket: {
18
17
  send: jest.fn(),
19
18
  },
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _EndpointEngine_instances, _EndpointEngine_middleware, _EndpointEngine_channels, _EndpointEngine_sockets, _EndpointEngine_parentEngine, _EndpointEngine_sendMessage, _EndpointEngine_joinChannel, _EndpointEngine_execute, _EndpointEngine_retrieveChannel, _EndpointEngine_handleMessage, _EndpointEngine_readMessage, _EndpointEngine_buildError, _EndpointEngine_leaveChannel, _Endpoint_engine;
13
+ var _EndpointEngine_instances, _EndpointEngine_middleware, _EndpointEngine_channels, _EndpointEngine_sockets, _EndpointEngine_parentEngine, _EndpointEngine_joinChannel, _EndpointEngine_execute, _EndpointEngine_retrieveChannel, _EndpointEngine_handleMessage, _EndpointEngine_readMessage, _EndpointEngine_buildError, _EndpointEngine_leaveChannel, _Endpoint_engine;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.Endpoint = exports.EndpointEngine = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -30,7 +30,7 @@ class EndpointEngine {
30
30
  _EndpointEngine_parentEngine.set(this, void 0);
31
31
  __classPrivateFieldSet(this, _EndpointEngine_sockets, new Map(), "f");
32
32
  __classPrivateFieldSet(this, _EndpointEngine_middleware, new middleware_1.Middleware(), "f");
33
- __classPrivateFieldSet(this, _EndpointEngine_channels, new Set(), "f");
33
+ __classPrivateFieldSet(this, _EndpointEngine_channels, new Map(), "f");
34
34
  __classPrivateFieldSet(this, _EndpointEngine_parentEngine, parent, "f");
35
35
  }
36
36
  get parent() {
@@ -64,7 +64,7 @@ class EndpointEngine {
64
64
  }
65
65
  next();
66
66
  });
67
- __classPrivateFieldGet(this, _EndpointEngine_channels, "f").add(pondChannel);
67
+ __classPrivateFieldGet(this, _EndpointEngine_channels, "f").set(path, pondChannel);
68
68
  return new lobby_1.PondChannel(pondChannel);
69
69
  }
70
70
  /**
@@ -87,7 +87,7 @@ class EndpointEngine {
87
87
  action: pondsocket_common_1.ServerActions.BROADCAST,
88
88
  channelName: pondsocket_common_1.SystemSender.ENDPOINT,
89
89
  };
90
- __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_sendMessage).call(this, socket, message);
90
+ this.sendMessage(socket, message);
91
91
  });
92
92
  }
93
93
  /**
@@ -114,9 +114,11 @@ class EndpointEngine {
114
114
  __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_readMessage).call(this, cache, message.data);
115
115
  });
116
116
  socket.addEventListener('close', () => {
117
+ __classPrivateFieldGet(this, _EndpointEngine_sockets, "f").delete(cache.clientId);
117
118
  cache.subscriptions.forEach((unsubscribe) => unsubscribe());
118
119
  });
119
120
  socket.addEventListener('error', () => {
121
+ __classPrivateFieldGet(this, _EndpointEngine_sockets, "f").delete(cache.clientId);
120
122
  cache.subscriptions.forEach((unsubscribe) => unsubscribe());
121
123
  });
122
124
  }
@@ -140,14 +142,9 @@ class EndpointEngine {
140
142
  subscribeTo(userId, channel) {
141
143
  const user = this.getUser(userId);
142
144
  const channelEngine = __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_retrieveChannel).call(this, channel);
143
- if (!channelEngine) {
144
- user.pendingSubscriptions.add(channel);
145
- }
146
- else {
147
- user.subscriptions.set(channel, channelEngine.addUser(userId, user.assigns, (event) => {
148
- user.socket.send(JSON.stringify(event));
149
- }));
150
- }
145
+ const onMessage = this.sendMessage.bind(this, user.socket);
146
+ const subscription = channelEngine.addUser(userId, user.assigns, onMessage);
147
+ user.subscriptions.set(channel, subscription);
151
148
  }
152
149
  /**
153
150
  * @desc Unsubscribes a user from a channel
@@ -160,51 +157,45 @@ class EndpointEngine {
160
157
  if (unsubscribe) {
161
158
  unsubscribe();
162
159
  user.subscriptions.delete(channel);
160
+ return;
163
161
  }
164
- else if (user.pendingSubscriptions.has(channel)) {
165
- user.pendingSubscriptions.delete(channel);
166
- }
167
- else {
168
- throw new pondError_1.EndpointError(`GatewayEngine: User ${userId} is not subscribed to ${channel}`, 404);
169
- }
162
+ throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
170
163
  }
171
- subscribePendingUsers(channel) {
172
- const users = [...__classPrivateFieldGet(this, _EndpointEngine_sockets, "f").values()]
173
- .filter(({ pendingSubscriptions }) => pendingSubscriptions.has(channel.name));
174
- users.forEach(({ clientId, pendingSubscriptions, subscriptions, socket, assigns }) => {
175
- const unsubscribe = channel.addUser(clientId, assigns, (event) => {
176
- socket.send(JSON.stringify(event));
177
- });
178
- subscriptions.set(channel.name, unsubscribe);
179
- pendingSubscriptions.delete(channel.name);
180
- });
164
+ /**
165
+ * @desc Sends a message to a client
166
+ * @param socket - The socket to send the message to
167
+ * @param message - The message to send
168
+ */
169
+ sendMessage(socket, message) {
170
+ socket.send(JSON.stringify(message));
181
171
  }
182
172
  }
183
173
  exports.EndpointEngine = EndpointEngine;
184
- _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakMap(), _EndpointEngine_sockets = new WeakMap(), _EndpointEngine_parentEngine = new WeakMap(), _EndpointEngine_instances = new WeakSet(), _EndpointEngine_sendMessage = function _EndpointEngine_sendMessage(socket, message) {
185
- socket.send(JSON.stringify(message));
186
- }, _EndpointEngine_joinChannel = function _EndpointEngine_joinChannel(channel, socket, joinParams, requestId) {
174
+ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakMap(), _EndpointEngine_sockets = new WeakMap(), _EndpointEngine_parentEngine = new WeakMap(), _EndpointEngine_instances = new WeakSet(), _EndpointEngine_joinChannel = function _EndpointEngine_joinChannel(channel, socket, joinParams, requestId) {
187
175
  const cache = Object.assign(Object.assign({}, socket), { requestId, channelName: channel });
188
176
  __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").run(cache, joinParams, () => {
189
177
  throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
190
178
  });
191
179
  }, _EndpointEngine_execute = function _EndpointEngine_execute(channel, handler) {
192
- for (const manager of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
193
- const isPresent = manager.listChannels()
194
- .includes(channel);
195
- if (isPresent) {
180
+ for (const [path, manager] of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
181
+ const event = (0, matcher_1.parseAddress)(path, channel);
182
+ if (event) {
196
183
  return manager.execute(channel, handler);
197
184
  }
198
185
  }
199
- throw new Error(`GatewayEngine: Channel ${channel} does not exist`);
200
186
  }, _EndpointEngine_retrieveChannel = function _EndpointEngine_retrieveChannel(channel) {
201
- for (const manager of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
202
- const isPresent = manager.listChannels()
203
- .includes(channel);
204
- if (isPresent) {
205
- return manager.getChannel(channel);
187
+ let channelEngine;
188
+ for (const [path, manager] of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
189
+ const event = (0, matcher_1.parseAddress)(path, channel);
190
+ if (event) {
191
+ channelEngine = manager.getChannel(channel) || manager.createChannel(channel);
192
+ break;
206
193
  }
207
194
  }
195
+ if (!channelEngine) {
196
+ throw new Error(`GatewayEngine: Channel ${channel} does not exist`);
197
+ }
198
+ return channelEngine;
208
199
  }, _EndpointEngine_handleMessage = function _EndpointEngine_handleMessage(cache, message) {
209
200
  switch (message.action) {
210
201
  case pondsocket_common_1.ClientActions.JOIN_CHANNEL:
@@ -228,7 +219,7 @@ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakM
228
219
  __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_handleMessage).call(this, cache, result);
229
220
  }
230
221
  catch (e) {
231
- __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_sendMessage).call(this, cache.socket, __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_buildError).call(this, e));
222
+ this.sendMessage(cache.socket, __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_buildError).call(this, e));
232
223
  }
233
224
  }, _EndpointEngine_buildError = function _EndpointEngine_buildError(error) {
234
225
  const event = {
@@ -297,6 +288,7 @@ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakM
297
288
  throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
298
289
  }
299
290
  unsubscribe();
291
+ socket.subscriptions.delete(channel);
300
292
  };
301
293
  class Endpoint {
302
294
  constructor(engine) {
@@ -27,6 +27,7 @@ const createEndpointEngine = (socket) => ({
27
27
  getUser: () => socket,
28
28
  subscribeTo: jest.fn(),
29
29
  unsubscribeFrom: jest.fn(),
30
+ sendMessage: (socket, msg) => socket.send(JSON.stringify(msg)),
30
31
  });
31
32
  exports.createEndpointEngine = createEndpointEngine;
32
33
  describe('endpoint', () => {
@@ -61,7 +61,6 @@ class ConnectionResponse {
61
61
  socket: __classPrivateFieldGet(this, _ConnectionResponse_webSocket, "f"),
62
62
  assigns: __classPrivateFieldGet(this, _ConnectionResponse_assigns, "f"),
63
63
  subscriptions: new Map(),
64
- pendingSubscriptions: new Set(),
65
64
  };
66
65
  __classPrivateFieldGet(this, _ConnectionResponse_engine, "f").manageSocket(cache);
67
66
  return this;
@@ -116,7 +115,7 @@ _ConnectionResponse_webSocket = new WeakMap(), _ConnectionResponse_engine = new
116
115
  requestId: __classPrivateFieldGet(this, _ConnectionResponse_requestId, "f"),
117
116
  channelName: pondsocket_common_1.SystemSender.ENDPOINT,
118
117
  };
119
- __classPrivateFieldGet(this, _ConnectionResponse_webSocket, "f").send(JSON.stringify(message));
118
+ __classPrivateFieldGet(this, _ConnectionResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _ConnectionResponse_webSocket, "f"), message);
120
119
  }, _ConnectionResponse_performChecks = function _ConnectionResponse_performChecks() {
121
120
  if (__classPrivateFieldGet(this, _ConnectionResponse_executed, "f")) {
122
121
  const message = 'Cannot execute response more than once';
@@ -11,7 +11,6 @@ const createMockSocket = () => {
11
11
  channelName: 'channel',
12
12
  requestId: 'requestId',
13
13
  subscriptions: new Map(),
14
- pendingSubscriptions: new Set(),
15
14
  socket: {
16
15
  send: jest.fn(),
17
16
  },
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _JoinResponse_instances, _JoinResponse_user, _JoinResponse_engine, _JoinResponse_newAssigns, _JoinResponse_executed, _JoinResponse_accepted, _JoinResponse_performChecks, _JoinResponse_sendMessage;
13
+ var _JoinResponse_instances, _JoinResponse_user, _JoinResponse_engine, _JoinResponse_newAssigns, _JoinResponse_executed, _JoinResponse_accepted, _JoinResponse_performChecks, _JoinResponse_sendMessage, _JoinResponse_directMessage;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.JoinResponse = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
@@ -60,10 +60,9 @@ class JoinResponse {
60
60
  requestId: __classPrivateFieldGet(this, _JoinResponse_user, "f").requestId,
61
61
  payload: {},
62
62
  };
63
- __classPrivateFieldGet(this, _JoinResponse_user, "f").socket.send(JSON.stringify(acknowledgement));
64
- const unsubscribe = __classPrivateFieldGet(this, _JoinResponse_engine, "f").addUser(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, __classPrivateFieldGet(this, _JoinResponse_newAssigns, "f"), (event) => {
65
- __classPrivateFieldGet(this, _JoinResponse_user, "f").socket.send(JSON.stringify(event));
66
- });
63
+ __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_directMessage).call(this, acknowledgement);
64
+ const onMessage = __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_directMessage).bind(this);
65
+ const unsubscribe = __classPrivateFieldGet(this, _JoinResponse_engine, "f").addUser(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, __classPrivateFieldGet(this, _JoinResponse_newAssigns, "f"), onMessage);
67
66
  __classPrivateFieldGet(this, _JoinResponse_user, "f").subscriptions.set(__classPrivateFieldGet(this, _JoinResponse_engine, "f").name, unsubscribe);
68
67
  __classPrivateFieldSet(this, _JoinResponse_accepted, true, "f");
69
68
  return this;
@@ -85,7 +84,7 @@ class JoinResponse {
85
84
  action: pondsocket_common_1.ServerActions.ERROR,
86
85
  requestId: __classPrivateFieldGet(this, _JoinResponse_user, "f").requestId,
87
86
  };
88
- __classPrivateFieldGet(this, _JoinResponse_user, "f").socket.send(JSON.stringify(errorMessage));
87
+ __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_directMessage).call(this, errorMessage);
89
88
  return this;
90
89
  }
91
90
  /**
@@ -101,7 +100,7 @@ class JoinResponse {
101
100
  payload,
102
101
  event,
103
102
  };
104
- __classPrivateFieldGet(this, _JoinResponse_user, "f").socket.send(JSON.stringify(message));
103
+ __classPrivateFieldGet(this, _JoinResponse_instances, "m", _JoinResponse_directMessage).call(this, message);
105
104
  return this;
106
105
  }
107
106
  /**
@@ -168,4 +167,6 @@ _JoinResponse_user = new WeakMap(), _JoinResponse_engine = new WeakMap(), _JoinR
168
167
  __classPrivateFieldSet(this, _JoinResponse_executed, true, "f");
169
168
  }, _JoinResponse_sendMessage = function _JoinResponse_sendMessage(recipient, event, payload) {
170
169
  __classPrivateFieldGet(this, _JoinResponse_engine, "f").sendMessage(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, recipient, pondsocket_common_1.ServerActions.BROADCAST, event, payload, __classPrivateFieldGet(this, _JoinResponse_user, "f").requestId);
170
+ }, _JoinResponse_directMessage = function _JoinResponse_directMessage(event) {
171
+ __classPrivateFieldGet(this, _JoinResponse_engine, "f").parent.parent.sendMessage(__classPrivateFieldGet(this, _JoinResponse_user, "f").socket, event);
171
172
  };
package/lobby/lobby.js CHANGED
@@ -26,12 +26,21 @@ class LobbyEngine {
26
26
  __classPrivateFieldSet(this, _LobbyEngine_channels, new Set(), "f");
27
27
  __classPrivateFieldSet(this, _LobbyEngine_middleware, new middleware_1.Middleware(), "f");
28
28
  }
29
+ /**
30
+ * @desc The parent engine
31
+ */
29
32
  get parent() {
30
33
  return __classPrivateFieldGet(this, _LobbyEngine_parentEngine, "f");
31
34
  }
35
+ /**
36
+ * @desc The leave callback
37
+ */
32
38
  get leaveCallback() {
33
39
  return __classPrivateFieldGet(this, _LobbyEngine_leaveCallback, "f");
34
40
  }
41
+ /**
42
+ * @desc The middleware to use
43
+ */
35
44
  get middleware() {
36
45
  return __classPrivateFieldGet(this, _LobbyEngine_middleware, "f");
37
46
  }
@@ -120,14 +129,6 @@ class LobbyEngine {
120
129
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").delete(newChannel);
121
130
  }
122
131
  }
123
- /**
124
- * @desc Lists all channels
125
- * @private
126
- */
127
- listChannels() {
128
- return Array.from(__classPrivateFieldGet(this, _LobbyEngine_channels, "f"))
129
- .map((channel) => channel.name);
130
- }
131
132
  /**
132
133
  * @desc Creates a new channel
133
134
  * @param channelName - The name of the channel to create
@@ -135,7 +136,6 @@ class LobbyEngine {
135
136
  */
136
137
  createChannel(channelName) {
137
138
  const newChannel = new channel_1.ChannelEngine(channelName, this);
138
- __classPrivateFieldGet(this, _LobbyEngine_parentEngine, "f").subscribePendingUsers(newChannel);
139
139
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").add(newChannel);
140
140
  return newChannel;
141
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.145",
3
+ "version": "0.1.147",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -42,7 +42,7 @@
42
42
  "@types/node": "^20.11.20",
43
43
  "@types/websocket": "^1.0.10",
44
44
  "@types/ws": "^8.5.10",
45
- "@typescript-eslint/eslint-plugin": "^7.0.2",
45
+ "@typescript-eslint/eslint-plugin": "^7.1.0",
46
46
  "eslint": "^8.57.0",
47
47
  "eslint-plugin-file-progress": "^1.3.0",
48
48
  "eslint-plugin-import": "^2.29.1",