@eleven-am/pondsocket 0.1.162 → 0.1.164

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.
@@ -55,7 +55,7 @@ class EndpointEngine {
55
55
  * });
56
56
  */
57
57
  createChannel(path, handler) {
58
- const pondChannel = new lobby_1.LobbyEngine(this);
58
+ const pondChannel = new lobby_1.LobbyEngine(this, path);
59
59
  __classPrivateFieldGet(this, _EndpointEngine_middleware, "f").use((user, joinParams, next) => {
60
60
  const event = (0, matcher_1.parseAddress)(path, user.channelName);
61
61
  if (event) {
package/lobby/lobby.js CHANGED
@@ -10,20 +10,26 @@ 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, _LobbyEngine_parentEngine, _PondChannel_lobby;
13
+ var _LobbyEngine_instances, _LobbyEngine_channels, _LobbyEngine_middleware, _LobbyEngine_leaveCallback, _LobbyEngine_parentEngine, _LobbyEngine_path, _LobbyEngine_performAction, _PondChannel_lobby;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondChannel = exports.LobbyEngine = void 0;
16
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
16
17
  const middleware_1 = require("../abstracts/middleware");
17
18
  const channel_1 = require("../channel/channel");
19
+ const pondError_1 = require("../errors/pondError");
20
+ const matcher_1 = require("../matcher/matcher");
18
21
  class LobbyEngine {
19
- constructor(endpointEngine) {
22
+ constructor(endpointEngine, path) {
23
+ _LobbyEngine_instances.add(this);
20
24
  _LobbyEngine_channels.set(this, void 0);
21
25
  _LobbyEngine_middleware.set(this, void 0);
22
26
  _LobbyEngine_leaveCallback.set(this, void 0);
23
27
  _LobbyEngine_parentEngine.set(this, void 0);
28
+ _LobbyEngine_path.set(this, void 0);
24
29
  __classPrivateFieldSet(this, _LobbyEngine_parentEngine, endpointEngine, "f");
25
30
  __classPrivateFieldSet(this, _LobbyEngine_channels, new Set(), "f");
26
31
  __classPrivateFieldSet(this, _LobbyEngine_middleware, new middleware_1.Middleware(), "f");
32
+ __classPrivateFieldSet(this, _LobbyEngine_path, path, "f");
27
33
  }
28
34
  /**
29
35
  * @desc The parent engine
@@ -99,9 +105,55 @@ class LobbyEngine {
99
105
  __classPrivateFieldGet(this, _LobbyEngine_channels, "f").add(newChannel);
100
106
  return newChannel;
101
107
  }
108
+ /**
109
+ * @desc Sends a message to all clients in the channel
110
+ * @param channelName - the name of the channel to send the message to
111
+ * @param event - the event to send
112
+ * @param payload - the payload to send
113
+ */
114
+ broadcast(channelName, event, payload) {
115
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
116
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
117
+ });
118
+ }
119
+ /**
120
+ * @desc Sends a message to all clients in the channel except the client making the request
121
+ * @param channelName - the name of the channel to send the message to
122
+ * @param event - the event to send
123
+ * @param payload - the payload to send
124
+ */
125
+ broadcastFrom(channelName, event, payload) {
126
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
127
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
128
+ });
129
+ }
130
+ /**
131
+ * @desc Sends a message to a set of clients in the channel
132
+ * @param channelName - the name of the channel to send the message to
133
+ * @param event - the event to send
134
+ * @param payload - the payload to send
135
+ * @param userIds - the ids of the clients to send the message to
136
+ */
137
+ broadcastTo(channelName, event, payload, userIds) {
138
+ const ids = Array.isArray(userIds) ? userIds : [userIds];
139
+ __classPrivateFieldGet(this, _LobbyEngine_instances, "m", _LobbyEngine_performAction).call(this, channelName, (channel) => {
140
+ channel.sendMessage(pondsocket_common_1.SystemSender.CHANNEL, ids, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
141
+ });
142
+ }
102
143
  }
103
144
  exports.LobbyEngine = LobbyEngine;
104
- _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap(), _LobbyEngine_parentEngine = new WeakMap();
145
+ _LobbyEngine_channels = new WeakMap(), _LobbyEngine_middleware = new WeakMap(), _LobbyEngine_leaveCallback = new WeakMap(), _LobbyEngine_parentEngine = new WeakMap(), _LobbyEngine_path = new WeakMap(), _LobbyEngine_instances = new WeakSet(), _LobbyEngine_performAction = function _LobbyEngine_performAction(channelName, handler) {
146
+ const matches = (0, matcher_1.parseAddress)(__classPrivateFieldGet(this, _LobbyEngine_path, "f"), channelName);
147
+ if (matches === null) {
148
+ throw new pondError_1.EndpointError('Invalid channel name', 402);
149
+ }
150
+ const channel = this.getChannel(channelName) || this.createChannel(channelName);
151
+ const assigns = channel.getAssigns();
152
+ handler(channel);
153
+ if (Object.keys(assigns).length === 0) {
154
+ this.destroyChannel(channelName);
155
+ }
156
+ };
105
157
  class PondChannel {
106
158
  constructor(lobby) {
107
159
  _PondChannel_lobby.set(this, void 0);
@@ -120,6 +172,15 @@ class PondChannel {
120
172
  }
121
173
  return null;
122
174
  }
175
+ broadcast(channelName, event, payload) {
176
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcast(channelName, event, payload);
177
+ }
178
+ broadcastFrom(channelName, event, payload) {
179
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcastFrom(channelName, event, payload);
180
+ }
181
+ broadcastTo(channelName, event, payload, userIds) {
182
+ __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcastTo(channelName, event, payload, userIds);
183
+ }
123
184
  }
124
185
  exports.PondChannel = PondChannel;
125
186
  _PondChannel_lobby = new WeakMap();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.162",
3
+ "version": "0.1.164",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -54,7 +54,7 @@ export declare class PondSocket {
54
54
  * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
55
55
  * const token = req.query.token;
56
56
  * if (!token)
57
- * return res.reject('No token provided');
57
+ * return res.decline('No token provided');
58
58
  * res.accept({
59
59
  * assign: {
60
60
  * token
@@ -78,7 +78,7 @@ export declare class Endpoint {
78
78
  * response.accept();
79
79
  *
80
80
  * else
81
- * response.reject('You are not an admin', 403);
81
+ * response.decline('You are not an admin', 403);
82
82
  * });
83
83
  */
84
84
  createChannel<Path extends string, EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns>(path: PondPath<Path>, handler: RequestHandler<JoinRequest<Path, PresenceType, AssignType>, JoinResponse<EventType, PresenceType, AssignType>>): PondChannel<EventType, PresenceType, AssignType>;
@@ -117,20 +117,6 @@ export declare class PondChannel<EventType extends PondEventMap = PondEventMap,
117
117
  */
118
118
  onEvent<Event extends string>(event: PondPath<Event>, handler: RequestHandler<EventRequest<Event, PresenceType, AssignType>, EventResponse<EventType, PresenceType, AssignType>>): void;
119
119
 
120
- /**
121
- * @desc Broadcasts a message to all users in a channel
122
- * @param event - The event to broadcast
123
- * @param payload - The payload to send
124
- * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
125
- * @example
126
- * pond.broadcast('echo', {
127
- * message: 'Hello World',
128
- * timestamp: Date.now(),
129
- * channel: 'my_channel',
130
- *});
131
- */
132
- broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
133
-
134
120
  /**
135
121
  * @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
136
122
  * @param {LeaveCallback} callback - The callback to execute when a user leaves
@@ -147,8 +133,53 @@ export declare class PondChannel<EventType extends PondEventMap = PondEventMap,
147
133
  * @returns {Channel} - The channel instance
148
134
  * @example
149
135
  * const channel = pond.getChannel('my_channel')!;
136
+ *
137
+ * if (channel === null) {
138
+ * console.log('Channel not found');
139
+ * return;
140
+ * }
150
141
  */
151
142
  getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
143
+
144
+ /**
145
+ * Broadcasts a message to all clients in the channel
146
+ * @param channelName - The name of the channel to broadcast to
147
+ * @param event - The event to send
148
+ * @param payload - The payload to send
149
+ * @example
150
+ *
151
+ * pond.broadcast('my_channel', 'message', {
152
+ * text: 'Hello, world!'
153
+ * });
154
+ */
155
+ broadcast <Key extends keyof EventType>(channelName: string, event: Key, payload: EventType[Key]): void;
156
+
157
+ /**
158
+ * Broadcasts a message to all clients in the channel except the sender
159
+ * @param channelName - The name of the channel to broadcast to
160
+ * @param event - The event to send
161
+ * @param payload - The payload to send
162
+ * @example
163
+ *
164
+ * pond.broadcastFrom('my_channel', 'message', {
165
+ * text: 'Hello, everyone but me!'
166
+ * });
167
+ */
168
+ broadcastFrom <Key extends keyof EventType> (channelName: string, event: Key, payload: EventType[Key]): void
169
+
170
+ /**
171
+ * Broadcasts a message to a specific set of clients
172
+ * @param channelName - The name of the channel to broadcast to
173
+ * @param event - The event to send
174
+ * @param payload - The payload to send
175
+ * @param userIds - The ids of the clients to send the message to
176
+ * @example
177
+ *
178
+ * pond.broadcastTo('my_channel', 'message', {
179
+ * text: 'Hello, specific people!'
180
+ * }, ['user1', 'user2']);
181
+ */
182
+ broadcastTo <Key extends keyof EventType> (channelName: string, event: Key, payload: EventType[Key], userIds: string | string[]): void;
152
183
  }
153
184
 
154
185
  export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {