@eleven-am/pondsocket 0.1.142 → 0.1.144

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.
@@ -42,8 +42,7 @@ class AbstractRequest {
42
42
  return this._engine.getAssigns();
43
43
  }
44
44
  get presence() {
45
- var _a;
46
- return ((_a = this._engine.presenceEngine) === null || _a === void 0 ? void 0 : _a.getPresence()) || {};
45
+ return this._engine.presenceEngine.getPresence();
47
46
  }
48
47
  /**
49
48
  * @desc Parses the event and returns true if the event matches the path
@@ -1,11 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  const abstractRequest_1 = require("./abstractRequest");
4
- const createMockChannelEngine = () => ({
5
- name: 'test',
6
- getAssigns: () => ({}),
7
- getPresence: () => ({}),
8
- });
4
+ const eventResponse_test_1 = require("../channel/eventResponse.test");
5
+ const createMockChannelEngine = () => (0, eventResponse_test_1.createChannelEngine)().channelEngine;
9
6
  describe('AbstractRequest', () => {
10
7
  it('should be able to be instantiated', () => {
11
8
  const request = new abstractRequest_1.AbstractRequest('/test', createMockChannelEngine(), {});
@@ -21,62 +21,14 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  }
22
22
  return t;
23
23
  };
24
- var _Channel_engine, _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser;
24
+ var _ChannelEngine_instances, _ChannelEngine_receiver, _ChannelEngine_presenceEngine, _ChannelEngine_users, _ChannelEngine_parentEngine, _ChannelEngine_subscribe, _ChannelEngine_getUsersFromRecipients, _ChannelEngine_buildSubscription, _ChannelEngine_removeUser, _Channel_engine;
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.ChannelEngine = exports.Channel = void 0;
26
+ exports.Channel = exports.ChannelEngine = void 0;
27
27
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
28
28
  const eventRequest_1 = require("./eventRequest");
29
29
  const eventResponse_1 = require("./eventResponse");
30
30
  const pondError_1 = require("../errors/pondError");
31
31
  const presence_1 = require("../presence/presence");
32
- class Channel {
33
- constructor(engine) {
34
- _Channel_engine.set(this, void 0);
35
- __classPrivateFieldSet(this, _Channel_engine, engine, "f");
36
- }
37
- get name() {
38
- return __classPrivateFieldGet(this, _Channel_engine, "f").name;
39
- }
40
- getAssigns() {
41
- return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
42
- }
43
- getPresences() {
44
- return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
45
- }
46
- getUserData(userId) {
47
- return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
48
- }
49
- broadcast(event, payload) {
50
- __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
51
- }
52
- broadcastFrom(userId, event, payload) {
53
- __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
54
- }
55
- broadcastTo(userIds, event, payload) {
56
- const users = Array.isArray(userIds) ? userIds : [userIds];
57
- __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
58
- }
59
- evictUser(userId, reason) {
60
- __classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
61
- }
62
- trackPresence(userId, presence) {
63
- __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
64
- }
65
- removePresence(userId) {
66
- __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
67
- }
68
- updatePresence(userId, presence) {
69
- __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
70
- }
71
- subscribeTo(userId, channel) {
72
- __classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
73
- }
74
- unsubscribeFrom(userId, channel) {
75
- __classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
76
- }
77
- }
78
- exports.Channel = Channel;
79
- _Channel_engine = new WeakMap();
80
32
  class ChannelEngine {
81
33
  constructor(name, parent) {
82
34
  _ChannelEngine_instances.add(this);
@@ -297,6 +249,7 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
297
249
  const userPresence = (_a = __classPrivateFieldGet(this, _ChannelEngine_presenceEngine, "f")) === null || _a === void 0 ? void 0 : _a.removePresence(userId);
298
250
  if (__classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback) {
299
251
  __classPrivateFieldGet(this, _ChannelEngine_parentEngine, "f").leaveCallback({
252
+ // eslint-disable-next-line @typescript-eslint/no-use-before-define
300
253
  channel: new Channel(this),
301
254
  user: {
302
255
  id: userId,
@@ -317,3 +270,51 @@ _ChannelEngine_receiver = new WeakMap(), _ChannelEngine_presenceEngine = new Wea
317
270
  unsubscribe();
318
271
  cachedUser.subscriptions.delete(this.name);
319
272
  };
273
+ class Channel {
274
+ constructor(engine) {
275
+ _Channel_engine.set(this, void 0);
276
+ __classPrivateFieldSet(this, _Channel_engine, engine, "f");
277
+ }
278
+ get name() {
279
+ return __classPrivateFieldGet(this, _Channel_engine, "f").name;
280
+ }
281
+ getAssigns() {
282
+ return __classPrivateFieldGet(this, _Channel_engine, "f").getAssigns();
283
+ }
284
+ getPresences() {
285
+ return __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.getPresence();
286
+ }
287
+ getUserData(userId) {
288
+ return __classPrivateFieldGet(this, _Channel_engine, "f").getUserData(userId);
289
+ }
290
+ broadcast(event, payload) {
291
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, pondsocket_common_1.ChannelReceiver.ALL_USERS, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
292
+ }
293
+ broadcastFrom(userId, event, payload) {
294
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(userId, pondsocket_common_1.ChannelReceiver.ALL_EXCEPT_SENDER, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
295
+ }
296
+ broadcastTo(userIds, event, payload) {
297
+ const users = Array.isArray(userIds) ? userIds : [userIds];
298
+ __classPrivateFieldGet(this, _Channel_engine, "f").sendMessage(pondsocket_common_1.SystemSender.CHANNEL, users, pondsocket_common_1.ServerActions.BROADCAST, event, payload);
299
+ }
300
+ evictUser(userId, reason) {
301
+ __classPrivateFieldGet(this, _Channel_engine, "f").kickUser(userId, reason !== null && reason !== void 0 ? reason : 'You have been banned from the channel');
302
+ }
303
+ trackPresence(userId, presence) {
304
+ __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.trackPresence(userId, presence);
305
+ }
306
+ removePresence(userId) {
307
+ __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.removePresence(userId);
308
+ }
309
+ updatePresence(userId, presence) {
310
+ __classPrivateFieldGet(this, _Channel_engine, "f").presenceEngine.updatePresence(userId, presence);
311
+ }
312
+ subscribeTo(userId, channel) {
313
+ __classPrivateFieldGet(this, _Channel_engine, "f").subscribeTo(userId, channel);
314
+ }
315
+ unsubscribeFrom(userId, channel) {
316
+ __classPrivateFieldGet(this, _Channel_engine, "f").unsubscribeFrom(userId, channel);
317
+ }
318
+ }
319
+ exports.Channel = Channel;
320
+ _Channel_engine = new WeakMap();
@@ -119,6 +119,7 @@ class EventResponse {
119
119
  */
120
120
  evictUser(reason, userId = __classPrivateFieldGet(this, _EventResponse_event, "f").sender) {
121
121
  __classPrivateFieldGet(this, _EventResponse_engine, "f").kickUser(userId, reason);
122
+ return this;
122
123
  }
123
124
  /**
124
125
  * @desc Closes the channel from the server side for all clients
@@ -21,7 +21,6 @@ const joinRequest_1 = require("../lobby/joinRequest");
21
21
  const joinResponse_1 = require("../lobby/joinResponse");
22
22
  const lobby_1 = require("../lobby/lobby");
23
23
  const matcher_1 = require("../matcher/matcher");
24
- const schema_1 = require("../schema");
25
24
  class EndpointEngine {
26
25
  constructor(parent) {
27
26
  _EndpointEngine_instances.add(this);
@@ -225,7 +224,7 @@ _EndpointEngine_middleware = new WeakMap(), _EndpointEngine_channels = new WeakM
225
224
  }, _EndpointEngine_readMessage = function _EndpointEngine_readMessage(cache, message) {
226
225
  try {
227
226
  const data = JSON.parse(message);
228
- const result = schema_1.clientMessageSchema.parse(data);
227
+ const result = pondsocket_common_1.clientMessageSchema.parse(data);
229
228
  __classPrivateFieldGet(this, _EndpointEngine_instances, "m", _EndpointEngine_handleMessage).call(this, cache, result);
230
229
  }
231
230
  catch (e) {
@@ -37,6 +37,20 @@ class ConnectionResponse {
37
37
  get hasResponded() {
38
38
  return __classPrivateFieldGet(this, _ConnectionResponse_executed, "f");
39
39
  }
40
+ /**
41
+ * @desc Assigns data to the user
42
+ * @param assigns - the data to assign
43
+ */
44
+ assign(assigns) {
45
+ if (!__classPrivateFieldGet(this, _ConnectionResponse_executed, "f")) {
46
+ __classPrivateFieldSet(this, _ConnectionResponse_assigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _ConnectionResponse_assigns, "f")), assigns), "f");
47
+ }
48
+ else {
49
+ const user = __classPrivateFieldGet(this, _ConnectionResponse_engine, "f").getUser(__classPrivateFieldGet(this, _ConnectionResponse_clientId, "f"));
50
+ user.assigns = Object.assign(Object.assign({}, user.assigns), assigns);
51
+ }
52
+ return this;
53
+ }
40
54
  /**
41
55
  * @desc Accepts the join request
42
56
  */
@@ -52,20 +66,6 @@ class ConnectionResponse {
52
66
  __classPrivateFieldGet(this, _ConnectionResponse_engine, "f").manageSocket(cache);
53
67
  return this;
54
68
  }
55
- /**
56
- * @desc Assigns data to the user
57
- * @param assigns - the data to assign
58
- */
59
- assign(assigns) {
60
- if (!__classPrivateFieldGet(this, _ConnectionResponse_executed, "f")) {
61
- __classPrivateFieldSet(this, _ConnectionResponse_assigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _ConnectionResponse_assigns, "f")), assigns), "f");
62
- }
63
- else {
64
- const user = __classPrivateFieldGet(this, _ConnectionResponse_engine, "f").getUser(__classPrivateFieldGet(this, _ConnectionResponse_clientId, "f"));
65
- user.assigns = Object.assign(Object.assign({}, user.assigns), assigns);
66
- }
67
- return this;
68
- }
69
69
  /**
70
70
  * @desc Rejects the request with the given error message
71
71
  * @param message - the error message
@@ -35,6 +35,19 @@ class JoinResponse {
35
35
  get hasResponded() {
36
36
  return __classPrivateFieldGet(this, _JoinResponse_executed, "f");
37
37
  }
38
+ /**
39
+ * @desc Assigns data to the client
40
+ * @param assigns - the data to assign
41
+ */
42
+ assign(assigns) {
43
+ if (__classPrivateFieldGet(this, _JoinResponse_accepted, "f")) {
44
+ __classPrivateFieldGet(this, _JoinResponse_engine, "f").updateAssigns(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, assigns);
45
+ }
46
+ else {
47
+ __classPrivateFieldSet(this, _JoinResponse_newAssigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _JoinResponse_newAssigns, "f")), assigns), "f");
48
+ }
49
+ return this;
50
+ }
38
51
  /**
39
52
  * @desc Accepts the join request to the channel
40
53
  */
@@ -75,19 +88,6 @@ class JoinResponse {
75
88
  __classPrivateFieldGet(this, _JoinResponse_user, "f").socket.send(JSON.stringify(errorMessage));
76
89
  return this;
77
90
  }
78
- /**
79
- * @desc Assigns data to the client
80
- * @param assigns - the data to assign
81
- */
82
- assign(assigns) {
83
- if (__classPrivateFieldGet(this, _JoinResponse_accepted, "f")) {
84
- __classPrivateFieldGet(this, _JoinResponse_engine, "f").updateAssigns(__classPrivateFieldGet(this, _JoinResponse_user, "f").clientId, assigns);
85
- }
86
- else {
87
- __classPrivateFieldSet(this, _JoinResponse_newAssigns, Object.assign(Object.assign({}, __classPrivateFieldGet(this, _JoinResponse_newAssigns, "f")), assigns), "f");
88
- }
89
- return this;
90
- }
91
91
  /**
92
92
  * @desc Emits a direct message to the client
93
93
  * @param event - the event name
package/lobby/lobby.js CHANGED
@@ -147,46 +147,15 @@ class PondChannel {
147
147
  _PondChannel_lobby.set(this, void 0);
148
148
  __classPrivateFieldSet(this, _PondChannel_lobby, lobby, "f");
149
149
  }
150
- /**
151
- * @desc Handles an event request made by a user
152
- * @param event - The event to listen for
153
- * @param handler - The handler to execute when the event is received
154
- * @example
155
- * pond.onEvent('echo', (request, response) => {
156
- * response.send('echo', {
157
- * message: request.event.payload,
158
- * });
159
- * });
160
- */
161
150
  onEvent(event, handler) {
162
151
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").onEvent(event, handler);
163
152
  }
164
- /**
165
- * @desc Broadcasts a message to all users in a channel
166
- * @param event - The event to broadcast
167
- * @param payload - The payload to send
168
- * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
169
- * @example
170
- * pond.broadcast('echo', {
171
- * message: 'Hello World',
172
- * timestamp: Date.now(),
173
- * channel: 'my_channel',
174
- *});
175
- */
176
153
  broadcast(event, payload, channelName) {
177
154
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").broadcast(event, payload, channelName);
178
155
  }
179
- /**
180
- * @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
181
- * @param callback - The callback to execute when a user leaves
182
- */
183
156
  onLeave(callback) {
184
157
  __classPrivateFieldGet(this, _PondChannel_lobby, "f").onLeave(callback);
185
158
  }
186
- /**
187
- * @desc Gets a channel by name
188
- * @param channelName - The name of the channel to get
189
- */
190
159
  getChannel(channelName) {
191
160
  const channel = __classPrivateFieldGet(this, _PondChannel_lobby, "f").getChannel(channelName);
192
161
  if (channel) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.142",
3
+ "version": "0.1.144",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -34,9 +34,8 @@
34
34
  "url": "git+https://github.com/Eleven-am/pondSocket.git"
35
35
  },
36
36
  "dependencies": {
37
- "@eleven-am/pondsocket-common": "^0.0.11",
38
- "ws": "^8.16.0",
39
- "zod": "^3.22.4"
37
+ "@eleven-am/pondsocket-common": "^0.0.15",
38
+ "ws": "^8.16.0"
40
39
  },
41
40
  "devDependencies": {
42
41
  "@types/jest": "^29.5.12",
@@ -17,7 +17,7 @@ const http_1 = require("http");
17
17
  const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
18
18
  const ws_1 = require("ws");
19
19
  const middleware_1 = require("../abstracts/middleware");
20
- const endpointEngine_1 = require("../endpoint/endpointEngine");
20
+ const endpoint_1 = require("../endpoint/endpoint");
21
21
  const response_1 = require("../endpoint/response");
22
22
  const matcher_1 = require("../matcher/matcher");
23
23
  class PondSocket {
@@ -62,7 +62,7 @@ class PondSocket {
62
62
  * })
63
63
  */
64
64
  createEndpoint(path, handler) {
65
- const endpoint = new endpointEngine_1.EndpointEngine(this);
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 new endpointEngine_1.Endpoint(endpoint);
75
+ return new endpoint_1.Endpoint(endpoint);
76
76
  }
77
77
  }
78
78
  exports.PondSocket = PondSocket;
package/types.d.ts CHANGED
@@ -22,124 +22,47 @@ export interface LeaveEvent<EventTypes extends PondEventMap = PondEventMap, Pres
22
22
 
23
23
  export type LeaveCallback<EventTypes extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> = (event: LeaveEvent<EventTypes, PresenceType, AssignType>) => void;
24
24
 
25
- export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
26
- event: PondEvent<Path>;
27
-
28
- channelName: string;
29
-
30
- assigns: Record<string, AssignType>;
31
-
32
- presence: Record<string, PresenceType>;
33
- }
34
-
35
- export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
36
- user: UserData<PresenceType, AssignType>;
37
-
38
- channel: Channel;
39
- }
40
-
41
- export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
42
- /**
43
- * @desc Whether the server has responded to the request
44
- */
45
- hasResponded: boolean;
46
-
47
- /**
48
- * Assigns data to the client.
49
- * @param {PondAssigns} assigns - The data to assign to the client.
50
- * @returns {EventResponse} - The EventResponse instance for chaining.
51
- */
52
- assign(assigns: AssignType): EventResponse<EventType, PresenceType, AssignType>;
53
-
54
- /**
55
- * @desc Emits a direct message to the client.
56
- * @param {string} event - The event name.
57
- * @param {PondMessage} payload - The payload to send.
58
- * @returns {EventResponse} - The EventResponse instance for chaining.
59
- */
60
- reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
61
-
62
- /**
63
- * @desc Sends a message to all clients in the channel.
64
- * @param {string} event - The event to send.
65
- * @param {PondMessage} payload - The payload to send.
66
- * @returns {EventResponse} - The EventResponse instance for chaining.
67
- */
68
- broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
69
-
70
- /**
71
- * @desc Sends a message to all clients in the channel except the client making the request.
72
- * @param {string} event - The event to send.
73
- * @param {PondMessage} payload - The payload to send.
74
- * @returns {EventResponse} - The EventResponse instance for chaining.
75
- */
76
- broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventResponse<EventType, PresenceType, AssignType>;
77
-
78
- /**
79
- * @desc Sends a message to a set of clients in the channel.
80
- * @param {string} event - The event to send.
81
- * @param {PondMessage} payload - The payload to send.
82
- * @param {string | string[]} userIds - The ids of the clients to send the message to.
83
- * @returns {EventResponse} - The EventResponse instance for chaining.
84
- */
85
- broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventResponse<EventType, PresenceType, AssignType>;
86
-
87
- /**
88
- * @desc Tracks a user's presence in the channel.
89
- * @param {PondPresence} presence - The initial presence data.
90
- * @param {string} userId - The id of the user to track.
91
- * @returns {EventResponse} - The EventResponse instance for chaining.
92
- */
93
- trackPresence(presence: PresenceType, userId?: string): EventResponse<EventType, PresenceType, AssignType>;
25
+ export type RequestHandler<Request, Response> = (request: Request, response: Response) => void | Promise<void>;
94
26
 
95
- /**
96
- * @desc Updates a user's presence in the channel.
97
- * @param {PondPresence} presence - The updated presence data.
98
- * @param {string} userId - The id of the user to update.
99
- * @returns {EventResponse} - The EventResponse instance for chaining.
100
- */
101
- updatePresence(presence: PresenceType, userId?: string): EventResponse;
102
-
103
- /**
104
- * @desc Removes a user's presence from the channel.
105
- * @param {string} userId - The id of the user to remove.
106
- * @returns {EventResponse} - The EventResponse instance for chaining.
107
- */
108
- removePresence(userId?: string): EventResponse;
109
-
110
- /**
111
- * @desc Subscribes the client to a channel.
112
- * @param {string} channel - The channel to subscribe to.
113
- * @returns {EventResponse} - The EventResponse instance for chaining.
114
- */
115
- subscribeTo(channel: string): EventResponse;
27
+ export declare class PondSocket {
28
+ constructor(server?: HTTPServer, socketServer?: WebSocketServer);
116
29
 
117
30
  /**
118
- * @desc Unsubscribes the client from a channel.
119
- * @param {string} channel - The channel to unsubscribe from.
120
- * @returns {EventResponse} - The EventResponse instance for chaining.
31
+ * @desc Specifies the port to listen on
32
+ * @param args - the arguments to pass to the server
121
33
  */
122
- unsubscribeFrom(channel: string): EventResponse;
34
+ listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
123
35
 
124
36
  /**
125
- * @desc Evicts a user from the channel.
126
- * @param {string} reason - The reason for the eviction.
127
- * @param {string} userId - The id of the user to evict.
37
+ * @desc Closes the server
38
+ * @param callback - the callback to call when the server is closed
128
39
  */
129
- evictUser(reason: string, userId?: string): void;
40
+ close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
130
41
 
131
42
  /**
132
- * @desc Closes the channel from the server side for all clients.
133
- * @param {string} reason - The reason for closing the channel.
43
+ * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
44
+ * @param path - the pattern to accept || can also be a regex
45
+ * @param handler - the handler function to authenticate the socket
46
+ * @example
47
+ * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
48
+ * const token = req.query.token;
49
+ * if (!token)
50
+ * return res.reject('No token provided');
51
+ * res.accept({
52
+ * assign: {
53
+ * token
54
+ * }
55
+ * });
56
+ * })
134
57
  */
135
- closeChannel(reason: string): void;
58
+ createEndpoint<Path extends string>(path: PondPath<Path>, handler: RequestHandler<IncomingConnection<Path>, ConnectionResponse>): Endpoint;
136
59
  }
137
60
 
138
61
  export declare class Endpoint {
139
62
  /**
140
63
  * @desc Adds a new PondChannel to this path on this endpoint
141
64
  * @param {PondPath<string>} path - The path to add the channel to
142
- * @param {ChannelHandler} handler - The handler to use to authenticate the client
65
+ * @param {RequestHandler} handler - The handler to use to authenticate the client
143
66
  * @returns {PondChannel} - The PondChannel instance for the new channel
144
67
  *
145
68
  * @example
@@ -151,7 +74,7 @@ export declare class Endpoint {
151
74
  * response.reject('You are not an admin', 403);
152
75
  * });
153
76
  */
154
- 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>;
77
+ 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>;
155
78
 
156
79
  /**
157
80
  * @desc Broadcasts a message to all clients connected to this endpoint
@@ -173,6 +96,54 @@ export declare class Endpoint {
173
96
  getClients(): WebSocket[];
174
97
  }
175
98
 
99
+ export declare class PondChannel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
100
+ /**
101
+ * @desc Handles an event request made by a user
102
+ * @param {PondPath<string>} event - The event to handle
103
+ * @param {RequestHandler} handler - The handlers to execute when the event is received
104
+ * @example
105
+ * pond.onEvent('echo', (request, response) => {
106
+ * response.reply('echo', {
107
+ * message: request.event.payload,
108
+ * });
109
+ * });
110
+ */
111
+ onEvent<Event extends string>(event: PondPath<Event>, handler: RequestHandler<EventRequest<Event, PresenceType, AssignType>, EventResponse<EventType, PresenceType, AssignType>>): void;
112
+
113
+ /**
114
+ * @desc Broadcasts a message to all users in a channel
115
+ * @param event - The event to broadcast
116
+ * @param payload - The payload to send
117
+ * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
118
+ * @example
119
+ * pond.broadcast('echo', {
120
+ * message: 'Hello World',
121
+ * timestamp: Date.now(),
122
+ * channel: 'my_channel',
123
+ *});
124
+ */
125
+ broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
126
+
127
+ /**
128
+ * @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
129
+ * @param {LeaveCallback} callback - The callback to execute when a user leaves
130
+ * @example
131
+ * pond.onLeave((event) => {
132
+ * // Clean up resources
133
+ * });
134
+ */
135
+ onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
136
+
137
+ /**
138
+ * @desc Gets a channel by name
139
+ * @param {string} channelName - The name of the channel to get
140
+ * @returns {Channel} - The channel instance
141
+ * @example
142
+ * const channel = pond.getChannel('my_channel')!;
143
+ */
144
+ getChannel(channelName: string): Channel<EventType, PresenceType, AssignType> | null;
145
+ }
146
+
176
147
  export declare class Channel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
177
148
  /**
178
149
  * The name of the channel.
@@ -260,6 +231,16 @@ export declare class Channel<EventType extends PondEventMap = PondEventMap, Pres
260
231
  unsubscribeFrom(userId: string, channel: string): void;
261
232
  }
262
233
 
234
+ export declare class AbstractRequest<Path extends string, PresenceType extends PondPresence, AssignType extends PondAssigns> {
235
+ event: PondEvent<Path>;
236
+
237
+ channelName: string;
238
+
239
+ assigns: Record<string, AssignType>;
240
+
241
+ presence: Record<string, PresenceType>;
242
+ }
243
+
263
244
  export declare class JoinRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
264
245
  joinParams: JoinParams;
265
246
 
@@ -268,6 +249,59 @@ export declare class JoinRequest<Path extends string, PresenceType extends PondP
268
249
  channel: Channel;
269
250
  }
270
251
 
252
+ export declare class EventRequest<Path extends string, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> extends AbstractRequest<Path, PresenceType, AssignType> {
253
+ user: UserData<PresenceType, AssignType>;
254
+
255
+ channel: Channel;
256
+ }
257
+
258
+ export declare class ConnectionResponse {
259
+ /**
260
+ * @desc Whether the server has responded to the request
261
+ */
262
+ hasResponded: boolean;
263
+
264
+ /**
265
+ * @desc Assigns data to the user.
266
+ * @param {PondAssigns} assigns - The data to assign.
267
+ * @returns {ConnectionResponse} - The ConnectionResponse instance for chaining.
268
+ */
269
+ assign(assigns: PondAssigns): ConnectionResponse;
270
+
271
+ /**
272
+ * @desc Accepts the connection request to the endpoint.
273
+ */
274
+ accept(): void;
275
+
276
+ /**
277
+ * @desc Rejects the request with the given error message
278
+ * @param {string} message - The error message
279
+ * @param {number} errorCode - The error code
280
+ */
281
+ decline(message?: string, errorCode?: number): void;
282
+
283
+ /**
284
+ * @desc Emits a direct message to the client
285
+ * @param {string} event - The event name
286
+ * @param {PondMessage} payload - The payload to send
287
+ */
288
+ reply(event: string, payload: PondMessage): ConnectionResponse;
289
+
290
+ /**
291
+ * @desc Subscribes the client to a channel.
292
+ * @param {string} channel - The channel to subscribe to.
293
+ * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
294
+ */
295
+ subscribeTo(channel: string): ConnectionResponse;
296
+
297
+ /**
298
+ * @desc Unsubscribes the client from a channel.
299
+ * @param {string} channel - The channel to unsubscribe from.
300
+ * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
301
+ */
302
+ unsubscribeFrom(channel: string): ConnectionResponse;
303
+ }
304
+
271
305
  export declare class JoinResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
272
306
  /**
273
307
  * @desc Whether the server has responded to the request
@@ -344,128 +378,101 @@ export declare class JoinResponse<EventType extends PondEventMap = PondEventMap,
344
378
  unsubscribeFrom(channel: string): JoinResponse;
345
379
  }
346
380
 
347
- export declare class ConnectionResponse {
381
+ export declare class EventResponse<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
348
382
  /**
349
383
  * @desc Whether the server has responded to the request
350
384
  */
351
385
  hasResponded: boolean;
352
386
 
353
387
  /**
354
- * @desc Assigns data to the user.
355
- * @param {PondAssigns} assigns - The data to assign.
356
- * @returns {ConnectionResponse} - The ConnectionResponse instance for chaining.
357
- */
358
- assign(assigns: PondAssigns): ConnectionResponse;
359
-
360
- /**
361
- * @desc Accepts the connection request to the endpoint.
388
+ * @desc Assigns data to the client.
389
+ * @param {PondAssigns} assigns - The data to assign to the client.
390
+ * @returns {EventResponse} - The EventResponse instance for chaining.
362
391
  */
363
- accept(): void;
392
+ assign(assigns: AssignType): EventResponse<EventType, PresenceType, AssignType>;
364
393
 
365
394
  /**
366
- * @desc Rejects the request with the given error message
367
- * @param {string} message - The error message
368
- * @param {number} errorCode - The error code
395
+ * @desc Emits a direct message to the client.
396
+ * @param {string} event - The event name.
397
+ * @param {PondMessage} payload - The payload to send.
398
+ * @returns {EventResponse} - The EventResponse instance for chaining.
369
399
  */
370
- decline(message?: string, errorCode?: number): void;
400
+ reply<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
371
401
 
372
402
  /**
373
- * @desc Emits a direct message to the client
374
- * @param {string} event - The event name
375
- * @param {PondMessage} payload - The payload to send
403
+ * @desc Sends a message to all clients in the channel.
404
+ * @param {string} event - The event to send.
405
+ * @param {PondMessage} payload - The payload to send.
406
+ * @returns {EventResponse} - The EventResponse instance for chaining.
376
407
  */
377
- reply(event: string, payload: PondMessage): ConnectionResponse;
408
+ broadcast<Event extends keyof EventType>(event: Event, payload: EventType[Event]): EventResponse<EventType, PresenceType, AssignType>;
378
409
 
379
410
  /**
380
- * @desc Subscribes the client to a channel.
381
- * @param {string} channel - The channel to subscribe to.
382
- * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
411
+ * @desc Sends a message to all clients in the channel except the client making the request.
412
+ * @param {string} event - The event to send.
413
+ * @param {PondMessage} payload - The payload to send.
414
+ * @returns {EventResponse} - The EventResponse instance for chaining.
383
415
  */
384
- subscribeTo(channel: string): ConnectionResponse;
416
+ broadcastFrom<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent]): EventResponse<EventType, PresenceType, AssignType>;
385
417
 
386
418
  /**
387
- * @desc Unsubscribes the client from a channel.
388
- * @param {string} channel - The channel to unsubscribe from.
389
- * @returns {ConnectionResponse} - The JoinResponse instance for chaining.
419
+ * @desc Sends a message to a set of clients in the channel.
420
+ * @param {string} event - The event to send.
421
+ * @param {PondMessage} payload - The payload to send.
422
+ * @param {string | string[]} userIds - The ids of the clients to send the message to.
423
+ * @returns {EventResponse} - The EventResponse instance for chaining.
390
424
  */
391
- unsubscribeFrom(channel: string): ConnectionResponse;
392
- }
425
+ broadcastTo<UserEvent extends keyof EventType>(event: UserEvent, payload: EventType[UserEvent], userIds: string | string[]): EventResponse<EventType, PresenceType, AssignType>;
393
426
 
394
- export declare class PondChannel<EventType extends PondEventMap = PondEventMap, PresenceType extends PondPresence = PondPresence, AssignType extends PondAssigns = PondAssigns> {
395
427
  /**
396
- * @desc Handles an event request made by a user
397
- * @param event - The event to listen for
398
- * @param handler - The handler to execute when the event is received
399
- * @example
400
- * pond.onEvent('echo', (request, response) => {
401
- * response.send('echo', {
402
- * message: request.event.payload,
403
- * });
404
- * });
428
+ * @desc Tracks a user's presence in the channel.
429
+ * @param {PondPresence} presence - The initial presence data.
430
+ * @param {string} userId - The id of the user to track.
431
+ * @returns {EventResponse} - The EventResponse instance for chaining.
405
432
  */
406
- onEvent<Event extends string>(event: PondPath<Event>, handler: (request: EventRequest<Event, PresenceType, AssignType>, response: EventResponse<EventType, PresenceType, AssignType>) => void | Promise<void>): void;
433
+ trackPresence(presence: PresenceType, userId?: string): EventResponse<EventType, PresenceType, AssignType>;
407
434
 
408
435
  /**
409
- * @desc Broadcasts a message to all users in a channel
410
- * @param event - The event to broadcast
411
- * @param payload - The payload to send
412
- * @param channelName - The channel to broadcast to (if not specified, broadcast to all channels)
413
- * @example
414
- * pond.broadcast('echo', {
415
- * message: 'Hello World',
416
- * timestamp: Date.now(),
417
- * channel: 'my_channel',
418
- *});
436
+ * @desc Updates a user's presence in the channel.
437
+ * @param {PondPresence} presence - The updated presence data.
438
+ * @param {string} userId - The id of the user to update.
439
+ * @returns {EventResponse} - The EventResponse instance for chaining.
419
440
  */
420
- broadcast<Key extends keyof EventType>(event: Key, payload: EventType[Key], channelName?: string): void;
441
+ updatePresence(presence: PresenceType, userId?: string): EventResponse;
421
442
 
422
443
  /**
423
- * @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
424
- * @param {LeaveCallback} callback - The callback to execute when a user leaves
425
- * @example
426
- * pond.onLeave((event) => {
427
- * // Clean up resources
428
- * });
444
+ * @desc Removes a user's presence from the channel.
445
+ * @param {string} userId - The id of the user to remove.
446
+ * @returns {EventResponse} - The EventResponse instance for chaining.
429
447
  */
430
- onLeave(callback: LeaveCallback<EventType, PresenceType, AssignType>): void;
448
+ removePresence(userId?: string): EventResponse;
431
449
 
432
450
  /**
433
- * @desc Gets a channel by name
434
- * @param {string} channelName - The name of the channel to get
451
+ * @desc Subscribes the client to a channel.
452
+ * @param {string} channel - The channel to subscribe to.
453
+ * @returns {EventResponse} - The EventResponse instance for chaining.
435
454
  */
436
- getChannel(channelName: string): Channel<EventType, PresenceType, AssignType>;
437
- }
438
-
439
- export declare class PondSocket {
440
- constructor(server?: HTTPServer, socketServer?: WebSocketServer);
455
+ subscribeTo(channel: string): EventResponse;
441
456
 
442
457
  /**
443
- * @desc Specifies the port to listen on
444
- * @param args - the arguments to pass to the server
458
+ * @desc Unsubscribes the client from a channel.
459
+ * @param {string} channel - The channel to unsubscribe from.
460
+ * @returns {EventResponse} - The EventResponse instance for chaining.
445
461
  */
446
- listen(...args: any[]): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
462
+ unsubscribeFrom(channel: string): EventResponse;
447
463
 
448
464
  /**
449
- * @desc Closes the server
450
- * @param callback - the callback to call when the server is closed
465
+ * @desc Evicts a user from the channel.
466
+ * @param {string} reason - The reason for the eviction.
467
+ * @param {string} userId - The id of the user to evict.
468
+ * @returns {EventResponse} - The EventResponse instance for chaining.
451
469
  */
452
- close(callback?: () => void): HTTPServer<typeof import('http').IncomingMessage, typeof import('http').ServerResponse>;
470
+ evictUser(reason: string, userId?: string): EventResponse;
453
471
 
454
472
  /**
455
- * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
456
- * @param path - the pattern to accept || can also be a regex
457
- * @param handler - the handler function to authenticate the socket
458
- * @example
459
- * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
460
- * const token = req.query.token;
461
- * if (!token)
462
- * return res.reject('No token provided');
463
- * res.accept({
464
- * assign: {
465
- * token
466
- * }
467
- * });
468
- * })
473
+ * @desc Closes the channel from the server side for all clients.
474
+ * @param {string} reason - The reason for closing the channel.
469
475
  */
470
- createEndpoint<Path extends string>(path: PondPath<Path>, handler: (request: IncomingConnection<Path>, response: ConnectionResponse) => void | Promise<void>): Endpoint;
476
+ closeChannel(reason: string): void;
471
477
  }
478
+
package/schema.js DELETED
@@ -1,12 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.clientMessageSchema = void 0;
4
- const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
5
- const zod_1 = require("zod");
6
- exports.clientMessageSchema = zod_1.z.object({
7
- event: zod_1.z.string(),
8
- requestId: zod_1.z.string(),
9
- channelName: zod_1.z.string(),
10
- payload: zod_1.z.record(zod_1.z.any()),
11
- action: zod_1.z.nativeEnum(pondsocket_common_1.ClientActions),
12
- });