@eleven-am/pondsocket 0.1.157 → 0.1.160

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);
257
+ const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId, true);
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,34 +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;
319
326
  }
320
327
  updateAssigns(userId, assigns) {
321
328
  __classPrivateFieldGet(this, _Channel_engine, "f").updateAssigns(userId, assigns);
329
+ return this;
322
330
  }
323
331
  subscribeTo(userId, channel) {
324
332
  __classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
333
+ return this;
325
334
  }
326
335
  unsubscribeFrom(userId, channel) {
327
336
  __classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
337
+ return this;
328
338
  }
329
339
  upsertPresence(userId, presence) {
330
340
  const oldPresence = __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence()[userId];
@@ -334,6 +344,7 @@ class Channel {
334
344
  else {
335
345
  __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
336
346
  }
347
+ return this;
337
348
  }
338
349
  }
339
350
  exports.Channel = Channel;
@@ -121,6 +121,14 @@ 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);
124
132
  }
125
133
  /**
126
134
  * @desc Retrieves a user from the endpoint
@@ -228,12 +228,13 @@ describe('endpoint', () => {
228
228
  }))
229
229
  .close();
230
230
  }));
231
- it('should throw an error if accept, reject / send is called more than once', () => __awaiter(void 0, void 0, void 0, function* () {
231
+ it('should throw an error if accept, reject 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
- expect(() => res.accept()).toThrowError('Cannot execute response more than once');
236
+ res.accept();
237
+ expect(() => res.decline()).toThrowError('Cannot execute response more than once');
237
238
  });
238
239
  yield (0, superwstest_1.default)(server)
239
240
  .ws('/api/socket')
@@ -14,6 +14,7 @@ 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");
17
18
  const pondError_1 = require("../errors/pondError");
18
19
  class ConnectionResponse {
19
20
  constructor(webSocket, engine, clientId) {
@@ -85,7 +86,9 @@ class ConnectionResponse {
85
86
  * @param payload - the payload to send
86
87
  */
87
88
  reply(event, payload) {
88
- __classPrivateFieldGet(this, _ConnectionResponse_instances, "m", _ConnectionResponse_performChecks).call(this);
89
+ if (__classPrivateFieldGet(this, _ConnectionResponse_webSocket, "f").readyState !== ws_1.WebSocket.OPEN) {
90
+ throw new pondError_1.EndpointError('Socket is not open', 400);
91
+ }
89
92
  __classPrivateFieldGet(this, _ConnectionResponse_instances, "m", _ConnectionResponse_sendMessage).call(this, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
90
93
  return this;
91
94
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.157",
3
+ "version": "0.1.160",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -62,8 +62,9 @@ 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
65
66
  */
66
- removePresence(presenceKey) {
67
+ removePresence(presenceKey, safe = false) {
67
68
  const presence = __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").get(presenceKey);
68
69
  if (presence) {
69
70
  __classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").delete(presenceKey);
@@ -72,7 +73,7 @@ class PresenceEngine {
72
73
  presence: Array.from(__classPrivateFieldGet(this, _PresenceEngine_presenceMap, "f").values()),
73
74
  });
74
75
  }
75
- else {
76
+ else if (!safe) {
76
77
  const code = 404;
77
78
  const message = `PresenceEngine: Presence with key ${presenceKey} does not exist`;
78
79
  throw new pondError_1.PresenceError(message, code, __classPrivateFieldGet(this, _PresenceEngine_channel, "f").name, pondsocket_common_1.PresenceEventTypes.LEAVE);
@@ -114,6 +114,9 @@ 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
+ });
117
120
  });
118
121
  describe('getPresence', () => {
119
122
  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,62 +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
218
 
219
219
  /**
220
220
  * @desc Updates a user's assigns in the channel
221
221
  * @param userId - the id of the user to update
222
222
  * @param assigns - the assigns data to update
223
223
  */
224
- updateAssigns(userId: string, assigns: AssignType): void;
224
+ updateAssigns(userId: string, assigns: Partial<AssignType>): Channel<EventType, PresenceType, AssignType>;
225
225
 
226
226
  /**
227
227
  * @desc Tracks or updates a user's presence in the channel
228
228
  * @param userId - the id of the user to upsert
229
229
  * @param presence - the presence data to upsert
230
230
  */
231
- upsertPresence (userId: string, presence: PresenceType): void;
231
+ upsertPresence (userId: string, presence: PresenceType): Channel<EventType, PresenceType, AssignType>;
232
232
 
233
233
  /**
234
234
  * @desc Subscribes a user to a channel.
235
235
  * @param {string} userId - The id of the user.
236
236
  * @param {string} channel - The channel to subscribe to.
237
237
  */
238
- subscribeTo(userId: string, channel: string): void;
238
+ subscribeTo(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
239
239
 
240
240
  /**
241
241
  * @desc Unsubscribes a user from a channel.
242
242
  * @param {string} userId - The id of the user.
243
243
  * @param {string} channel - The channel to unsubscribe from.
244
244
  */
245
- unsubscribeFrom(userId: string, channel: string): void;
245
+ unsubscribeFrom(userId: string, channel: string): Channel<EventType, PresenceType, AssignType>;
246
246
  }
247
247
 
248
248
  export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {