@eleven-am/pondsocket 0.1.141 → 0.1.143

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/lobby/lobby.js CHANGED
@@ -10,20 +10,31 @@ 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 _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _PondChannel_lobby;
13
+ var _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _LobbyEngine_parentEngine, _PondChannel_lobby;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondChannel = exports.LobbyEngine = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
17
  const middleware_1 = require("../abstracts/middleware");
18
18
  const channel_1 = require("../channel/channel");
19
19
  class LobbyEngine {
20
- constructor() {
20
+ constructor(endpointEngine) {
21
21
  _LobbyEngine_channels.set(this, void 0);
22
22
  _LobbyEngine_middleware.set(this, void 0);
23
23
  _LobbyEngine_leaveCallback.set(this, void 0);
24
+ _LobbyEngine_parentEngine.set(this, void 0);
25
+ __classPrivateFieldSet(this, _LobbyEngine_parentEngine, endpointEngine, "f");
24
26
  __classPrivateFieldSet(this, _LobbyEngine_channels, new Set(), "f");
25
27
  __classPrivateFieldSet(this, _LobbyEngine_middleware, new middleware_1.Middleware(), "f");
26
28
  }
29
+ get parent() {
30
+ return __classPrivateFieldGet(this, _LobbyEngine_parentEngine, "f");
31
+ }
32
+ get leaveCallback() {
33
+ return __classPrivateFieldGet(this, _LobbyEngine_leaveCallback, "f");
34
+ }
35
+ get middleware() {
36
+ return __classPrivateFieldGet(this, _LobbyEngine_middleware, "f");
37
+ }
27
38
  /**
28
39
  * @desc Handles an event request made by a user
29
40
  * @param event - The event to listen for
@@ -68,23 +79,14 @@ class LobbyEngine {
68
79
  if (!channel) {
69
80
  throw new Error(`GatewayEngine: Channel ${channelName} does not exist`);
70
81
  }
71
- channel.sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, event, payload);
82
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, event, payload);
72
83
  }
73
84
  else {
74
85
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").forEach((channel) => {
75
- channel.sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, event, payload);
86
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.SYSTEM, event, payload);
76
87
  });
77
88
  }
78
89
  }
79
- /**
80
- * @desc Removes a user from all channels
81
- * @param clientId - The client id of the user to remove
82
- */
83
- removeUser(clientId) {
84
- __classPrivateFieldGet(this, _LobbyEngine_channels, "f").forEach((channel) => {
85
- channel.removeUser(clientId, true);
86
- });
87
- }
88
90
  /**
89
91
  * @desc Executes a function on a channel
90
92
  * @param channelName - The name of the channel to execute the function on
@@ -132,65 +134,28 @@ class LobbyEngine {
132
134
  * @private
133
135
  */
134
136
  createChannel(channelName) {
135
- const destroyChannel = this.destroyChannel.bind(this, channelName);
136
- const execute = __classPrivateFieldGet(this, _LobbyEngine_middleware, "f").run.bind(__classPrivateFieldGet(this, _LobbyEngine_middleware, "f"));
137
- const parentEngine = {
138
- execute,
139
- destroyChannel,
140
- leaveCallback: __classPrivateFieldGet(this, _LobbyEngine_leaveCallback, "f"),
141
- };
142
- const newChannel = new channel_1.ChannelEngine(channelName, parentEngine);
137
+ const newChannel = new channel_1.ChannelEngine(channelName, this);
138
+ __classPrivateFieldGet(this, _LobbyEngine_parentEngine, "f").subscribePendingUsers(newChannel);
143
139
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").add(newChannel);
144
140
  return newChannel;
145
141
  }
146
142
  }
147
143
  exports.LobbyEngine = LobbyEngine;
148
- _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap();
144
+ _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap(), _LobbyEngine_parentEngine = new WeakMap();
149
145
  class PondChannel {
150
146
  constructor(lobby) {
151
147
  _PondChannel_lobby.set(this, void 0);
152
148
  __classPrivateFieldSet(this, _PondChannel_lobby, lobby, "f");
153
149
  }
154
- /**
155
- * @desc Handles an event request made by a user
156
- * @param event - The event to listen for
157
- * @param handler - The handler to execute when the event is received
158
- * @example
159
- * pond.onEvent('echo', (request, response) => {
160
- * response.send('echo', {
161
- * message: request.event.payload,
162
- * });
163
- * });
164
- */
165
150
  onEvent(event, handler) {
166
151
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").onEvent(event, handler);
167
152
  }
168
- /**
169
- * @desc Broadcasts a message to all users in a channel
170
- * @param event - The event to broadcast
171
- * @param payload - The payload to send
172
- * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
173
- * @example
174
- * pond.broadcast('echo', {
175
- * message: 'Hello World',
176
- * timestamp: Date.now(),
177
- * channel: 'my_channel',
178
- *});
179
- */
180
153
  broadcast(event, payload, channelName) {
181
154
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcast(event, payload, channelName);
182
155
  }
183
- /**
184
- * @desc Handles the leave event for a user, can occur when a user disconnects or leaves a channel, use this to clean up any resources
185
- * @param callback - The callback to execute when a user leaves
186
- */
187
156
  onLeave(callback) {
188
157
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").onLeave(callback);
189
158
  }
190
- /**
191
- * @desc Gets a channel by name
192
- * @param channelName - The name of the channel to get
193
- */
194
159
  getChannel(channelName) {
195
160
  const channel = __classPrivateFieldGet(this, _PondChannel_lobby, "f").getChannel(channelName);
196
161
  if (channel) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.141",
3
+ "version": "0.1.143",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -16,7 +16,7 @@
16
16
  "access": "public"
17
17
  },
18
18
  "scripts": {
19
- "test": "jest --coverage --verbose",
19
+ "test": "jest --coverage --verbose --forceExit --detectOpenHandles --runInBand --bail",
20
20
  "build": "rimraf dist && tsc && npm run remove",
21
21
  "lint": "eslint --ext .ts src",
22
22
  "lint:fix": "eslint --fix --ext .ts src",
@@ -34,22 +34,22 @@
34
34
  "url": "git+https://github.com/Eleven-am/pondSocket.git"
35
35
  },
36
36
  "dependencies": {
37
- "@eleven-am/pondsocket-common": "^0.0.10",
37
+ "@eleven-am/pondsocket-common": "^0.0.11",
38
38
  "ws": "^8.16.0",
39
39
  "zod": "^3.22.4"
40
40
  },
41
41
  "devDependencies": {
42
- "@types/jest": "^29.5.11",
43
- "@types/node": "^20.11.5",
42
+ "@types/jest": "^29.5.12",
43
+ "@types/node": "^20.11.20",
44
44
  "@types/websocket": "^1.0.10",
45
45
  "@types/ws": "^8.5.10",
46
- "@typescript-eslint/eslint-plugin": "^6.19.0",
47
- "eslint": "^8.56.0",
46
+ "@typescript-eslint/eslint-plugin": "^7.0.2",
47
+ "eslint": "^8.57.0",
48
48
  "eslint-plugin-file-progress": "^1.3.0",
49
49
  "eslint-plugin-import": "^2.29.1",
50
50
  "jest": "^29.7.0",
51
51
  "superwstest": "^2.0.3",
52
- "ts-jest": "^29.1.1",
52
+ "ts-jest": "^29.1.2",
53
53
  "ts-node": "^10.9.2",
54
54
  "typescript": "^5.3.3"
55
55
  },
@@ -107,5 +107,5 @@ _PresenceEngine_presenceMap = new WeakMap(), _PresenceEngine_channel = new WeakM
107
107
  if (recipients.length === 0) {
108
108
  return;
109
109
  }
110
- __classPrivateFieldGet(this, _PresenceEngine_channel, "f").sendMessage((0, pondsocket_common_1.uuid)(), pondsocket_common_1.SystemSender.CHANNEL, recipients, pondsocket_common_1.ServerActions.PRESENCE, event, payload);
110
+ __classPrivateFieldGet(this, _PresenceEngine_channel, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, recipients, pondsocket_common_1.ServerActions.PRESENCE, event, payload);
111
111
  };
@@ -9,7 +9,7 @@ describe('PresenceEngine', () => {
9
9
  let presenceKey;
10
10
  let channel;
11
11
  beforeEach(() => {
12
- channel = (0, eventResponse_test_1.createChannelEngine)();
12
+ channel = (0, eventResponse_test_1.createChannelEngine)().channelEngine;
13
13
  channel.addUser('presenceKey', { assign: 'assign' }, () => {
14
14
  // do nothing
15
15
  });
@@ -33,7 +33,6 @@ describe('PresenceEngine', () => {
33
33
  // remove the first element as it contains the request id which is random
34
34
  params.shift();
35
35
  expect(params).toEqual([
36
- pondsocket_common_1.SystemSender.CHANNEL,
37
36
  ['presenceKey'],
38
37
  pondsocket_common_1.ServerActions.PRESENCE,
39
38
  pondsocket_common_1.PresenceEventTypes.JOIN,
@@ -65,7 +64,6 @@ describe('PresenceEngine', () => {
65
64
  // remove the first element as it contains the request id which is random
66
65
  params.shift();
67
66
  expect(params).toEqual([
68
- pondsocket_common_1.SystemSender.CHANNEL,
69
67
  ['presenceKey'],
70
68
  pondsocket_common_1.ServerActions.PRESENCE,
71
69
  pondsocket_common_1.PresenceEventTypes.UPDATE,
@@ -98,7 +96,6 @@ describe('PresenceEngine', () => {
98
96
  // remove the first element as it contains the request id which is random
99
97
  params.shift();
100
98
  expect(params).toEqual([
101
- pondsocket_common_1.SystemSender.CHANNEL,
102
99
  ['presenceKey2'],
103
100
  pondsocket_common_1.ServerActions.PRESENCE,
104
101
  pondsocket_common_1.PresenceEventTypes.LEAVE,
package/schema.js CHANGED
@@ -9,5 +9,4 @@ exports.clientMessageSchema = zod_1.z.object({
9
9
  channelName: zod_1.z.string(),
10
10
  payload: zod_1.z.record(zod_1.z.any()),
11
11
  action: zod_1.z.nativeEnum(pondsocket_common_1.ClientActions),
12
- addresses: zod_1.z.union([zod_1.z.nativeEnum(pondsocket_common_1.ChannelReceiver), zod_1.z.array(zod_1.z.string())]).optional(),
13
12
  });
@@ -62,7 +62,7 @@ class PondSocket {
62
62
  * })
63
63
  */
64
64
  createEndpoint(path, handler) {
65
- const endpoint = new endpoint_1.Endpoint();
65
+ const endpoint = new endpoint_1.EndpointEngine(this);
66
66
  __classPrivateFieldGet(this, _PondSocket_middleware, "f").use((req, socket, next) => {
67
67
  const event = (0, matcher_1.parseAddress)(path, req.address);
68
68
  if (event) {
@@ -72,7 +72,7 @@ class PondSocket {
72
72
  }
73
73
  return next();
74
74
  });
75
- return endpoint;
75
+ return new endpoint_1.Endpoint(endpoint);
76
76
  }
77
77
  }
78
78
  exports.PondSocket = PondSocket;
@@ -82,7 +82,7 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
82
82
  socket.isAlive = true;
83
83
  });
84
84
  });
85
- const interval = setInterval(() => {
85
+ return setInterval(() => {
86
86
  __classPrivateFieldGet(this, _PondSocket_socketServer, "f").clients.forEach((socket) => {
87
87
  if (socket.isAlive === false) {
88
88
  return socket.terminate();
@@ -91,12 +91,15 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
91
91
  socket.ping();
92
92
  });
93
93
  }, 30000);
94
- __classPrivateFieldGet(this, _PondSocket_socketServer, "f").on('close', () => clearInterval(interval));
95
94
  }, _PondSocket_init = function _PondSocket_init() {
96
- __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
95
+ const timeout = __classPrivateFieldGet(this, _PondSocket_instances, "m", _PondSocket_manageHeartbeat).call(this);
97
96
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('error', (error) => {
97
+ clearInterval(timeout);
98
98
  throw new Error(error.message);
99
99
  });
100
+ __classPrivateFieldGet(this, _PondSocket_server, "f").on('close', () => {
101
+ clearInterval(timeout);
102
+ });
100
103
  __classPrivateFieldGet(this, _PondSocket_server, "f").on('upgrade', (req, socket, head) => {
101
104
  const clientId = req.headers['sec-websocket-key'];
102
105
  const request = {