@eleven-am/pondsocket 0.1.146 → 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
  /**
@@ -142,14 +142,9 @@ class EndpointEngine {
142
142
  subscribeTo(userId, channel) {
143
143
  const user = this.getUser(userId);
144
144
  const channelEngine = __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_retrieveChannel).call(this, channel);
145
- if (!channelEngine) {
146
- user.pendingSubscriptions.add(channel);
147
- }
148
- else {
149
- user.subscriptions.set(channel, channelEngine.addUser(userId, user.assigns, (event) => {
150
- user.socket.send(JSON.stringify(event));
151
- }));
152
- }
145
+ const onMessage = this.sendMessage.bind(this, user.socket);
146
+ const subscription = channelEngine.addUser(userId, user.assigns, onMessage);
147
+ user.subscriptions.set(channel, subscription);
153
148
  }
154
149
  /**
155
150
  * @desc Unsubscribes a user from a channel
@@ -162,55 +157,45 @@ class EndpointEngine {
162
157
  if (unsubscribe) {
163
158
  unsubscribe();
164
159
  user.subscriptions.delete(channel);
160
+ return;
165
161
  }
166
- else if (user.pendingSubscriptions.has(channel)) {
167
- user.pendingSubscriptions.delete(channel);
168
- }
169
- else {
170
- throw new pondError_1.EndpointError(`GatewayEngine: User ${userId} is not subscribed to ${channel}`, 404);
171
- }
162
+ throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
172
163
  }
173
164
  /**
174
- * @desc Subscribes all pending users to a channel
175
- * @param channel - The channel to subscribe the users to
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
176
168
  */
177
- subscribePendingUsers(channel) {
178
- const users = [...__classPrivateFieldGet(this, _EndpointEngine_sockets, "f").values()]
179
- .filter(({ pendingSubscriptions }) => pendingSubscriptions.has(channel.name));
180
- users.forEach(({ clientId, pendingSubscriptions, subscriptions, socket, assigns }) => {
181
- const unsubscribe = channel.addUser(clientId, assigns, (event) => {
182
- socket.send(JSON.stringify(event));
183
- });
184
- subscriptions.set(channel.name, unsubscribe);
185
- pendingSubscriptions.delete(channel.name);
186
- });
169
+ sendMessage(socket, message) {
170
+ socket.send(JSON.stringify(message));
187
171
  }
188
172
  }
189
173
  exports.EndpointEngine = EndpointEngine;
190
- _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) {
191
- socket.send(JSON.stringify(message));
192
- }, _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) {
193
175
  const cache = Object.assign(Object.assign({}, socket), { requestId, channelName: channel });
194
176
  __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").run(cache, joinParams, () => {
195
177
  throw new pondError_1.EndpointError(`GatewayEngine: Channel ${channel} does not exist`, 404);
196
178
  });
197
179
  }, _EndpointEngine_execute = function _EndpointEngine_execute(channel, handler) {
198
- for (const manager of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
199
- const isPresent = manager.listChannels()
200
- .includes(channel);
201
- 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) {
202
183
  return manager.execute(channel, handler);
203
184
  }
204
185
  }
205
- throw new Error(`GatewayEngine: Channel ${channel} does not exist`);
206
186
  }, _EndpointEngine_retrieveChannel = function _EndpointEngine_retrieveChannel(channel) {
207
- for (const manager of __classPrivateFieldGet(this, _EndpointEngine_channels, "f")) {
208
- const isPresent = manager.listChannels()
209
- .includes(channel);
210
- if (isPresent) {
211
- 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;
212
193
  }
213
194
  }
195
+ if (!channelEngine) {
196
+ throw new Error(`GatewayEngine: Channel ${channel} does not exist`);
197
+ }
198
+ return channelEngine;
214
199
  }, _EndpointEngine_handleMessage = function _EndpointEngine_handleMessage(cache, message) {
215
200
  switch (message.action) {
216
201
  case pondsocket_common_1.ClientActions.JOIN_CHANNEL:
@@ -234,7 +219,7 @@ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakM
234
219
  __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_handleMessage).call(this, cache, result);
235
220
  }
236
221
  catch (e) {
237
- __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));
238
223
  }
239
224
  }, _EndpointEngine_buildError = function _EndpointEngine_buildError(error) {
240
225
  const event = {
@@ -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.146",
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",