@eleven-am/pondsocket 0.1.155 → 0.1.158

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.
@@ -254,7 +254,7 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
254
254
  }
255
255
  unsubscribe();
256
256
  __classPrivateFieldGet(this, _ChannelEngine_users, "f").delete(userId);
257
- const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, true);
257
+ const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
258
258
  if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
259
259
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
260
260
  // eslint-disable-next-line @typescript-eslint/no-use-before-define
@@ -297,31 +297,44 @@ class Channel {
297
297
  }
298
298
  broadcast(event, payload) {
299
299
  __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
300
+ return this;
300
301
  }
301
302
  broadcastFrom(userId, event, payload) {
302
303
  __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
304
+ return this;
303
305
  }
304
306
  broadcastTo(userIds, event, payload) {
305
307
  const users = Array.isArray(userIds) ? userIds : [userIds];
306
308
  __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
309
+ return this;
307
310
  }
308
311
  evictUser(userId, reason) {
309
312
  __classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
313
+ return this;
310
314
  }
311
315
  trackPresence(userId, presence) {
312
316
  __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
317
+ return this;
313
318
  }
314
319
  removePresence(userId) {
315
320
  __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
321
+ return this;
316
322
  }
317
323
  updatePresence(userId, presence) {
318
324
  __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
325
+ return this;
326
+ }
327
+ updateAssigns(userId, assigns) {
328
+ __classPrivateFieldGet(this, _Channel_engine, "f").updateAssigns(userId, assigns);
329
+ return this;
319
330
  }
320
331
  subscribeTo(userId, channel) {
321
332
  __classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
333
+ return this;
322
334
  }
323
335
  unsubscribeFrom(userId, channel) {
324
336
  __classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
337
+ return this;
325
338
  }
326
339
  upsertPresence(userId, presence) {
327
340
  const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence()[userId];
@@ -331,6 +344,7 @@ class Channel {
331
344
  else {
332
345
  __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
333
346
  }
347
+ return this;
334
348
  }
335
349
  }
336
350
  exports.Channel = Channel;
@@ -121,14 +121,6 @@ class EndpointEngine {
121
121
  __classPrivateFieldGet(this, _EndpointEngine_sockets, "f").delete(cache.clientId);
122
122
  cache.subscriptions.forEach((unsubscribe) => unsubscribe());
123
123
  });
124
- const event = {
125
- event: pondsocket_common_1.Events.CONNECTION,
126
- action: pondsocket_common_1.ServerActions.CONNECT,
127
- channelName: pondsocket_common_1.SystemSender.ENDPOINT,
128
- requestId: (0, pondsocket_common_1.uuid)(),
129
- payload: {},
130
- };
131
- this.sendMessage(socket, event);
132
124
  }
133
125
  /**
134
126
  * @desc Retrieves a user from the endpoint
@@ -228,13 +228,12 @@ describe('endpoint', () => {
228
228
  }))
229
229
  .close();
230
230
  }));
231
- it('should throw an error if accept, reject is called more than once', () => __awaiter(void 0, void 0, void 0, function* () {
231
+ it('should throw an error if accept, reject / send is called more than once', () => __awaiter(void 0, void 0, void 0, function* () {
232
232
  socket.createEndpoint('/api/:room', (_, res) => {
233
233
  res.reply('hello', {
234
234
  test: 'test',
235
235
  });
236
- res.accept();
237
- expect(() => res.decline()).toThrowError('Cannot execute response more than once');
236
+ expect(() => res.accept()).toThrowError('Cannot execute response more than once');
238
237
  });
239
238
  yield (0, superwstest_1.default)(server)
240
239
  .ws('/api/socket')
@@ -14,7 +14,6 @@ var _ConnectionResponse_instances, _ConnectionResponse_webSocket, _ConnectionRes
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.ConnectionResponse = void 0;
16
16
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
- const ws_1 = require("ws");
18
17
  const pondError_1 = require("../errors/pondError");
19
18
  class ConnectionResponse {
20
19
  constructor(webSocket, engine, clientId) {
@@ -86,9 +85,7 @@ class ConnectionResponse {
86
85
  * @param payload - the payload to send
87
86
  */
88
87
  reply(event, payload) {
89
- if (__classPrivateFieldGet(this, _ConnectionResponse_webSocket, "f").readyState !== ws_1.WebSocket.OPEN) {
90
- throw new pondError_1.EndpointError('Socket is not open', 400);
91
- }
88
+ __classPrivateFieldGet(this, _ConnectionResponse_instances, "m", _ConnectionResponse_performChecks).call(this);
92
89
  __classPrivateFieldGet(this, _ConnectionResponse_instances, "m", _ConnectionResponse_sendMessage).call(this, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
93
90
  return this;
94
91
  }
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parseAddress = void 0;
3
+ exports.parseAddress = parseAddress;
4
4
  function pathToRegexp(path) {
5
5
  const parts = path.split('/');
6
6
  const regexpParts = parts.map((part) => {
@@ -72,4 +72,3 @@ function parseAddress(route, address) {
72
72
  query,
73
73
  };
74
74
  }
75
- exports.parseAddress = parseAddress;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.155",
3
+ "version": "0.1.158",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -35,22 +35,22 @@
35
35
  },
36
36
  "dependencies": {
37
37
  "@eleven-am/pondsocket-common": "^0.0.18",
38
- "ws": "^8.16.0"
38
+ "ws": "^8.17.1"
39
39
  },
40
40
  "devDependencies": {
41
41
  "@types/jest": "^29.5.12",
42
- "@types/node": "^20.12.5",
42
+ "@types/node": "^20.14.9",
43
43
  "@types/websocket": "^1.0.10",
44
44
  "@types/ws": "^8.5.10",
45
- "@typescript-eslint/eslint-plugin": "^7.5.0",
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",
49
49
  "jest": "^29.7.0",
50
50
  "superwstest": "^2.0.3",
51
- "ts-jest": "^29.1.2",
51
+ "ts-jest": "^29.1.5",
52
52
  "ts-node": "^10.9.2",
53
- "typescript": "^5.4.4"
53
+ "typescript": "^5.5.2"
54
54
  },
55
55
  "jest": {
56
56
  "moduleFileExtensions": [
@@ -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 safe - If true, it will not throw an error if the presence does not exist
66
65
  */
67
- removePresence(presenceKey, safe = 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,7 +72,7 @@ class PresenceEngine {
73
72
  presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
74
73
  });
75
74
  }
76
- else if (!safe) {
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);
@@ -114,9 +114,6 @@ describe('PresenceEngine', () => {
114
114
  it('should throw an error if the presence does not exist', () => {
115
115
  expect(() => presenceEngine.removePresence(presenceKey)).toThrowError(`PresenceEngine: Presence with key ${presenceKey} does not exist`);
116
116
  });
117
- it('should not throw an error if the safe flag is true', () => {
118
- expect(() => presenceEngine.removePresence(presenceKey, true)).not.toThrow();
119
- });
120
117
  });
121
118
  describe('getPresence', () => {
122
119
  it('should return the presence', () => {
package/types.d.ts CHANGED
@@ -171,7 +171,7 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
171
171
  * @param {string} event - The event to send.
172
172
  * @param {PondMessage} payload - The message to send.
173
173
  */
174
- broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): void;
174
+ broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key]): Channel<EventType, PresenceType, AssignType>;
175
175
 
176
176
  /**
177
177
  * @desc Broadcasts a message to every client in the channel except the sender,
@@ -179,7 +179,7 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
179
179
  * @param {string} event - The event to send.
180
180
  * @param {PondMessage} payload - The message to send.
181
181
  */
182
- broadcastFrom<Key extends keyof EventType>(userId: string, event: Key, payload: EventType[Key]): void;
182
+ broadcastFrom<Key extends keyof EventType>(userId: string, event: Key, payload: EventType[Key]): Channel<EventType, PresenceType, AssignType>;
183
183
 
184
184
  /**
185
185
  * @desc Sends a message to a specific client in the channel.
@@ -187,55 +187,62 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
187
187
  * @param {string} event - The event to send.
188
188
  * @param {PondMessage} payload - The message to send.
189
189
  */
190
- broadcastTo<Key extends keyof EventType>(clientIds: string | string[], event: Key, payload: EventType[Key]): void;
190
+ broadcastTo<Key extends keyof EventType>(clientIds: string | string[], event: Key, payload: EventType[Key]): Channel<EventType, PresenceType, AssignType>;
191
191
 
192
192
  /**
193
193
  * @desc Bans a user from the channel.
194
194
  * @param {string} userId - The id of the user to ban.
195
195
  * @param {string} reason - The reason for the ban.
196
196
  */
197
- evictUser(userId: string, reason?: string): void;
197
+ evictUser(userId: string, reason?: string): Channel<EventType, PresenceType, AssignType>;
198
198
 
199
199
  /**
200
200
  * @desc tracks a user's presence in the channel
201
201
  * @param {string} userId - the id of the user to track
202
202
  * @param {PondPresence} presence - the presence data to track
203
203
  */
204
- trackPresence(userId: string, presence: PresenceType): void;
204
+ trackPresence(userId: string, presence: PresenceType): Channel<EventType, PresenceType, AssignType>;
205
205
 
206
206
  /**
207
207
  * @desc removes a user's presence from the channel
208
208
  * @param {string} userId - the id of the user to remove
209
209
  */
210
- removePresence(userId: string): void;
210
+ removePresence(userId: string): Channel<EventType, PresenceType, AssignType>;
211
211
 
212
212
  /**
213
213
  * @desc updates a user's presence in the channel
214
214
  * @param {string} userId - the id of the user to update
215
215
  * @param {PondPresence} presence - the presence data to update
216
216
  */
217
- updatePresence(userId: string, presence: PresenceType): void;
217
+ updatePresence(userId: string, presence: Partial<PresenceType>): Channel<EventType, PresenceType, AssignType>;
218
+
219
+ /**
220
+ * @desc Updates a user's assigns in the channel
221
+ * @param userId - the id of the user to update
222
+ * @param assigns - the assigns data to update
223
+ */
224
+ updateAssigns(userId: string, assigns: Partial<AssignType>): Channel<EventType, PresenceType, AssignType>;
218
225
 
219
226
  /**
220
227
  * @desc Tracks or updates a user's presence in the channel
221
228
  * @param userId - the id of the user to upsert
222
229
  * @param presence - the presence data to upsert
223
230
  */
224
- upsertPresence (userId: string, presence: PresenceType): void;
231
+ upsertPresence (userId: string, presence: PresenceType): Channel<EventType, PresenceType, AssignType>;
225
232
 
226
233
  /**
227
234
  * @desc Subscribes a user to a channel.
228
235
  * @param {string} userId - The id of the user.
229
236
  * @param {string} channel - The channel to subscribe to.
230
237
  */
231
- subscribeTo(userId: string, channel: string): void;
238
+ subscribeTo(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
232
239
 
233
240
  /**
234
241
  * @desc Unsubscribes a user from a channel.
235
242
  * @param {string} userId - The id of the user.
236
243
  * @param {string} channel - The channel to unsubscribe from.
237
244
  */
238
- unsubscribeFrom(userId: string, channel: string): void;
245
+ unsubscribeFrom(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
239
246
  }
240
247
 
241
248
  export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {