@eleven-am/pondsocket 0.1.135 → 0.1.136

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.
@@ -104,27 +104,29 @@ class ChannelEngine {
104
104
  /**
105
105
  * @desc Removes a user from the channel
106
106
  * @param userId - The id of the user to remove
107
- * @param graceful - Whether to remove the user gracefully or not
108
107
  */
109
- removeUser(userId, graceful = false) {
108
+ removeUser(userId) {
110
109
  var _a;
111
110
  const user = __classPrivateFieldGet(this, _ChannelEngine_users, "f").get(userId);
112
111
  if (user) {
113
112
  __classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
114
113
  __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId);
115
- (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, graceful);
114
+ const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
116
115
  if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
117
116
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
118
- id: userId,
119
- assigns: user,
120
117
  channel: new Channel(this),
118
+ user: {
119
+ id: userId,
120
+ presence: userPresence !== null && userPresence !== void 0 ? userPresence : {},
121
+ assigns: user,
122
+ },
121
123
  });
122
124
  }
123
125
  if (__classPrivateFieldGet(this, _ChannelEngine_users, "f").size === 0) {
124
126
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
125
127
  }
126
128
  }
127
- else if (!graceful) {
129
+ else {
128
130
  throw new pondError_1.ChannelError(`ChannelEngine: User with id ${userId} does not exist in channel ${this.name}`, 404, this.name);
129
131
  }
130
132
  }
@@ -153,7 +155,7 @@ class ChannelEngine {
153
155
  message: reason !== null && reason !== void 0 ? reason : 'Channel has been destroyed',
154
156
  });
155
157
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").destroyChannel();
156
- __classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").unsubscribe(userId));
158
+ __classPrivateFieldGet(this, _ChannelEngine_users, "f").forEach((_, userId) => this.removeUser(userId));
157
159
  }
158
160
  /**
159
161
  * @desc Updates a user's assigns
@@ -263,7 +265,7 @@ class ChannelEngine {
263
265
  }
264
266
  exports.ChannelEngine = ChannelEngine;
265
267
  _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new WeakMap(), _ChannelEngine_users = new WeakMap(), _ChannelEngine_parentEngine = new WeakMap(), _ChannelEngine_instances = new WeakSet(), _ChannelEngine_subscribe = function _ChannelEngine_subscribe(userId, onMessage) {
266
- __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribeWith(userId, (_a) => {
268
+ return __classPrivateFieldGet(this, _ChannelEngine_receiver, "f").subscribeWith(userId, (_a) => {
267
269
  var { recipients } = _a, event = __rest(_a, ["recipients"]);
268
270
  if (recipients.includes(userId)) {
269
271
  onMessage(event);
@@ -117,11 +117,11 @@ class Endpoint {
117
117
  });
118
118
  socket.addEventListener('close', () => {
119
119
  __classPrivateFieldGet(this, _Endpoint_channels, "f")
120
- .forEach((manager) => manager.removeUser(cache.clientId, true));
120
+ .forEach((manager) => manager.removeUser(cache.clientId));
121
121
  });
122
122
  socket.addEventListener('error', () => {
123
123
  __classPrivateFieldGet(this, _Endpoint_channels, "f")
124
- .forEach((manager) => manager.removeUser(cache.clientId, true));
124
+ .forEach((manager) => manager.removeUser(cache.clientId));
125
125
  });
126
126
  }
127
127
  /**
package/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
2
  // @ts-expect-error
3
- import { PondSocket } from './types';
3
+ import type { PondSocket } from './types';
4
4
 
5
5
  export default PondSocket;
package/lobby/lobby.js CHANGED
@@ -79,11 +79,10 @@ class LobbyEngine {
79
79
  /**
80
80
  * @desc Removes a user from all channels
81
81
  * @param clientId - The client id of the user to remove
82
- * @param graceful - Whether to gracefully remove the user or not
83
82
  */
84
- removeUser(clientId, graceful = false) {
83
+ removeUser(clientId) {
85
84
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").forEach((channel) => {
86
- channel.removeUser(clientId, graceful);
85
+ channel.removeUser(clientId);
87
86
  });
88
87
  }
89
88
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.135",
3
+ "version": "0.1.136",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -34,13 +34,13 @@
34
34
  "url": "git+https://github.com/Eleven-am/pondSocket.git"
35
35
  },
36
36
  "dependencies": {
37
- "@eleven-am/pondsocket-common": "^0.0.7",
37
+ "@eleven-am/pondsocket-common": "^0.0.10",
38
38
  "ws": "^8.16.0",
39
39
  "zod": "^3.22.4"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/jest": "^29.5.11",
43
- "@types/node": "^20.11.4",
43
+ "@types/node": "^20.11.5",
44
44
  "@types/websocket": "^1.0.10",
45
45
  "@types/ws": "^8.5.10",
46
46
  "@typescript-eslint/eslint-plugin": "^6.19.0",
@@ -62,9 +62,8 @@ class PresenceEngine {
62
62
  /**
63
63
  * @desc Removes a presence from the presence engine
64
64
  * @param presenceKey - The key of the presence
65
- * @param graceful - Whether to gracefully remove the presence
66
65
  */
67
- removePresence(presenceKey, graceful = false) {
66
+ removePresence(presenceKey) {
68
67
  const presence = __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").get(presenceKey);
69
68
  if (presence) {
70
69
  __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
@@ -73,11 +72,12 @@ class PresenceEngine {
73
72
  presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
74
73
  });
75
74
  }
76
- else if (!graceful) {
75
+ else {
77
76
  const code = 404;
78
77
  const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
79
78
  throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
80
79
  }
80
+ return presence;
81
81
  }
82
82
  /**
83
83
  * @desc Updates a presence
package/types.d.ts CHANGED
@@ -1,57 +1,23 @@
1
- import { Server as HTTPServer, IncomingHttpHeaders } from 'http';
2
-
1
+ import { Server as HTTPServer } from 'http';
2
+
3
+ import {
4
+ PondMessage,
5
+ PondPresence,
6
+ PondAssigns,
7
+ PondEvent,
8
+ PondPath,
9
+ JoinParams,
10
+ PondEventMap,
11
+ IncomingConnection, UserData,
12
+ } from '@eleven-am/pondsocket-common';
3
13
  import { WebSocketServer } from 'ws';
4
14
 
5
- export type default_t<T = any> = Record<string, T>;
6
- type IsParam<Path> = Path extends `:${infer Param}` ? Param : never;
7
-
8
- type FilteredParams<Path> = Path extends `${infer First}/${infer Second}`
9
- ? IsParam<First> | FilteredParams<Second>
10
- : IsParam<Path>
11
-
12
- type Params<Path> = {
13
- [Key in FilteredParams<Path>]: string
14
- }
15
-
16
- type PondPath<Path extends string> = Path | RegExp;
17
-
18
- type EventParams<Path> = {
19
- query: Record<string, string>;
20
- params: Params<Path>;
21
- }
22
-
23
- type PondObject = default_t;
24
- type PondPresence = PondObject;
25
- type PondMessage = PondObject;
26
- type PondAssigns = PondObject;
27
- type JoinParams = PondObject;
28
-
29
- type PondEvent<Path> = EventParams<Path> & {
30
- payload: PondMessage;
31
- event: string;
32
- }
33
-
34
- type IncomingConnection<Path> = EventParams<Path> & {
35
- id: string;
36
- headers: IncomingHttpHeaders;
37
- address: string;
38
- }
39
-
40
- interface LeaveEvent<EventTypes extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
41
- id: string;
42
- assigns: AssignType;
15
+ interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
16
+ user: UserData<PresenceType, AssignType>;
43
17
  channel: Channel<EventTypes, PresenceType, AssignType>;
44
18
  }
45
19
 
46
- type LeaveCallback<EventTypes extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
47
-
48
- type PondEvenType = { [key: string]: PondMessage };
49
-
50
- interface UserData<PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
51
- assigns: AssignType;
52
- presence: PresenceType;
53
- id: string;
54
- }
20
+ type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
55
21
 
56
22
  declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
57
23
  event: PondEvent<Path>;
@@ -69,7 +35,7 @@ declare class EventRequest<Path extends string, PresenceType extends PondPresenc
69
35
  channel: Channel;
70
36
  }
71
37
 
72
- declare class EventResponse<EventType extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
38
+ declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
73
39
  /**
74
40
  * @desc Whether the server has responded to the request
75
41
  */
@@ -176,7 +142,7 @@ declare class Endpoint {
176
142
  * response.reject('You are not an admin', 403);
177
143
  * });
178
144
  */
179
- createChannel<Path extends string, EventType extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> (path: PondPath<Path>, handler: (request: JoinRequest<Path, PresenceType, AssignType>, response: JoinResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): PondChannel<EventType, PresenceType, AssignType>;
145
+ createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> (path: PondPath<Path>, handler: (request: JoinRequest<Path, PresenceType, AssignType>, response: JoinResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): PondChannel<EventType, PresenceType, AssignType>;
180
146
 
181
147
  /**
182
148
  * @desc Broadcasts a message to all clients connected to this endpoint
@@ -192,7 +158,7 @@ declare class Endpoint {
192
158
  closeConnection (clientIds: string | string[]): void;
193
159
  }
194
160
 
195
- export declare class Channel<EventType extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
161
+ export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
196
162
  /**
197
163
  * The name of the channel.
198
164
  */
@@ -281,7 +247,7 @@ declare class JoinRequest<Path extends string, PresenceType extends PondPresence
281
247
  channel: Channel;
282
248
  }
283
249
 
284
- declare class JoinResponse<EventType extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
250
+ declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
285
251
  /**
286
252
  * @desc Whether the server has responded to the request
287
253
  */
@@ -365,7 +331,7 @@ declare class ConnectionResponse {
365
331
  send (event: string, payload: PondMessage, assigns?: PondAssigns): void;
366
332
  }
367
333
 
368
- export declare class PondChannel <EventType extends PondEvenType = PondEvenType, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
334
+ export declare class PondChannel <EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
369
335
  /**
370
336
  * @desc Handles an event request made by a user
371
337
  * @param event - The event to listen for
package/types.js DELETED
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });