@eleven-am/pondsocket 0.1.57 → 0.1.58

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.
Files changed (71) hide show
  1. package/package.json +3 -3
  2. package/.eslintrc.json +0 -387
  3. package/dist/LICENSE +0 -674
  4. package/dist/README.md +0 -139
  5. package/dist/package.json +0 -51
  6. package/jest.config.js +0 -11
  7. package/src/abstracts/abstractRequest.test.ts +0 -49
  8. package/src/abstracts/abstractRequest.ts +0 -56
  9. package/src/abstracts/abstractResponse.ts +0 -26
  10. package/src/abstracts/middleware.test.ts +0 -75
  11. package/src/abstracts/middleware.ts +0 -50
  12. package/src/channel/channel.test.ts +0 -501
  13. package/src/channel/channel.ts +0 -305
  14. package/src/channel/eventRequest.test.ts +0 -37
  15. package/src/channel/eventRequest.ts +0 -27
  16. package/src/channel/eventResponse.test.ts +0 -249
  17. package/src/channel/eventResponse.ts +0 -172
  18. package/src/client/channel.test.ts +0 -799
  19. package/src/client/channel.ts +0 -342
  20. package/src/client.ts +0 -124
  21. package/src/endpoint/endpoint.test.ts +0 -825
  22. package/src/endpoint/endpoint.ts +0 -304
  23. package/src/endpoint/response.ts +0 -106
  24. package/src/enums.ts +0 -52
  25. package/src/errors/pondError.ts +0 -32
  26. package/src/express.ts +0 -58
  27. package/src/index.ts +0 -3
  28. package/src/lobby/JoinRequest.test.ts +0 -48
  29. package/src/lobby/JoinResponse.test.ts +0 -162
  30. package/src/lobby/joinRequest.ts +0 -32
  31. package/src/lobby/joinResponse.ts +0 -146
  32. package/src/lobby/lobby.ts +0 -182
  33. package/src/matcher/matcher.test.ts +0 -103
  34. package/src/matcher/matcher.ts +0 -105
  35. package/src/node.ts +0 -33
  36. package/src/presence/presence.ts +0 -127
  37. package/src/presence/presenceEngine.test.ts +0 -143
  38. package/src/server/pondSocket.ts +0 -153
  39. package/src/subjects/subject.test.ts +0 -163
  40. package/src/subjects/subject.ts +0 -137
  41. package/src/typedefs.d.ts +0 -451
  42. package/src/types.d.ts +0 -89
  43. package/tsconfig.build.json +0 -7
  44. package/tsconfig.json +0 -12
  45. /package/{dist/abstracts → abstracts}/abstractRequest.js +0 -0
  46. /package/{dist/abstracts → abstracts}/abstractResponse.js +0 -0
  47. /package/{dist/abstracts → abstracts}/middleware.js +0 -0
  48. /package/{dist/channel → channel}/channel.js +0 -0
  49. /package/{dist/channel → channel}/eventRequest.js +0 -0
  50. /package/{dist/channel → channel}/eventResponse.js +0 -0
  51. /package/{dist/client → client}/channel.js +0 -0
  52. /package/{dist/client.d.ts → client.d.ts} +0 -0
  53. /package/{dist/client.js → client.js} +0 -0
  54. /package/{dist/endpoint → endpoint}/endpoint.js +0 -0
  55. /package/{dist/endpoint → endpoint}/response.js +0 -0
  56. /package/{dist/enums.js → enums.js} +0 -0
  57. /package/{dist/errors → errors}/pondError.js +0 -0
  58. /package/{dist/express.d.ts → express.d.ts} +0 -0
  59. /package/{dist/express.js → express.js} +0 -0
  60. /package/{dist/index.d.ts → index.d.ts} +0 -0
  61. /package/{dist/index.js → index.js} +0 -0
  62. /package/{dist/lobby → lobby}/joinRequest.js +0 -0
  63. /package/{dist/lobby → lobby}/joinResponse.js +0 -0
  64. /package/{dist/lobby → lobby}/lobby.js +0 -0
  65. /package/{dist/matcher → matcher}/matcher.js +0 -0
  66. /package/{dist/node.d.ts → node.d.ts} +0 -0
  67. /package/{dist/node.js → node.js} +0 -0
  68. /package/{dist/presence → presence}/presence.js +0 -0
  69. /package/{dist/server → server}/pondSocket.js +0 -0
  70. /package/{dist/subjects → subjects}/subject.js +0 -0
  71. /package/{dist/types.d.ts → types.d.ts} +0 -0
@@ -1,342 +0,0 @@
1
- import {
2
- ChannelState,
3
- ClientActions,
4
- ServerActions,
5
- PresenceEventTypes,
6
- ChannelReceiver,
7
- Events,
8
- } from '../enums';
9
- import { SimpleSubject, SimpleBehaviorSubject } from '../subjects/subject';
10
- import {
11
- JoinParams,
12
- ChannelEvent,
13
- ClientMessage,
14
- Unsubscribe,
15
- PondPresence,
16
- PondMessage,
17
- PresencePayload,
18
- ChannelReceivers,
19
- // eslint-disable-next-line import/no-unresolved
20
- } from '../types';
21
-
22
-
23
- type Publisher = (data: ClientMessage) => void;
24
-
25
- export class Channel {
26
- readonly #name: string;
27
-
28
- readonly #joinParams: JoinParams;
29
-
30
- readonly #receiver: SimpleSubject<ChannelEvent>;
31
-
32
- readonly #clientState: SimpleBehaviorSubject<boolean>;
33
-
34
- readonly #joinState: SimpleBehaviorSubject<ChannelState>;
35
-
36
- readonly #publisher: Publisher;
37
-
38
- #queue: ClientMessage[];
39
-
40
- #presence: PondPresence[];
41
-
42
- readonly #presenceSub: Unsubscribe;
43
-
44
- constructor (publisher: Publisher, clientState: SimpleBehaviorSubject<boolean>, name: string, receiver: SimpleSubject<ChannelEvent>, params: JoinParams = {
45
- }) {
46
- this.#name = name;
47
- this.#queue = [];
48
- this.#presence = [];
49
- this.#joinParams = params;
50
- this.#publisher = publisher;
51
- this.#clientState = clientState;
52
- this.#receiver = new SimpleSubject<ChannelEvent>();
53
- this.#joinState = new SimpleBehaviorSubject<ChannelState>(ChannelState.IDLE);
54
- this.#presenceSub = this.#init(receiver);
55
- }
56
-
57
- /**
58
- * @desc Connects to the channel.
59
- */
60
- public join () {
61
- if (this.#joinState.value === ChannelState.CLOSED) {
62
- throw new Error('This channel has been closed');
63
- }
64
-
65
- const joinMessage: ClientMessage = {
66
- action: ClientActions.JOIN_CHANNEL,
67
- channelName: this.#name,
68
- event: ClientActions.JOIN_CHANNEL,
69
- payload: this.#joinParams,
70
- };
71
-
72
- this.#joinState.publish(ChannelState.JOINING);
73
- if (this.#clientState.value) {
74
- this.#publisher(joinMessage);
75
- } else {
76
- this.#joinState.publish(ChannelState.STALLED);
77
- }
78
- }
79
-
80
- /**
81
- * @desc Disconnects from the channel.
82
- */
83
- public leave () {
84
- const leaveMessage: ClientMessage = {
85
- action: ClientActions.LEAVE_CHANNEL,
86
- channelName: this.#name,
87
- event: ClientActions.LEAVE_CHANNEL,
88
- payload: {
89
- },
90
- };
91
-
92
- this.#publish(leaveMessage);
93
- this.#joinState.publish(ChannelState.CLOSED);
94
- this.#presenceSub();
95
- }
96
-
97
- /**
98
- * @desc Monitors the channel for messages.
99
- * @param callback - The callback to call when a message is received.
100
- */
101
- public onMessage (callback: (event: string, message: PondMessage) => void) {
102
- return this.#receiver.subscribe((data) => {
103
- if (data.action !== ServerActions.PRESENCE) {
104
- return callback(data.event, data.payload);
105
- }
106
- });
107
- }
108
-
109
- /**
110
- * @desc Monitors the channel for messages.
111
- * @param event - The event to monitor.
112
- * @param callback - The callback to call when a message is received.
113
- */
114
- public onMessageEvent (event: string, callback: (message: PondMessage) => void) {
115
- return this.onMessage((eventReceived, message) => {
116
- if (eventReceived === event) {
117
- return callback(message);
118
- }
119
- });
120
- }
121
-
122
- /**
123
- * @desc Monitors the channel state of the channel.
124
- * @param callback - The callback to call when the connection state changes.
125
- */
126
- public onChannelStateChange (callback: (connected: ChannelState) => void) {
127
- return this.#joinState.subscribe((data) => {
128
- callback(data);
129
- });
130
- }
131
-
132
- /**
133
- * @desc Detects when clients join the channel.
134
- * @param callback - The callback to call when a client joins the channel.
135
- */
136
- public onJoin (callback: (presence: PondPresence) => void) {
137
- return this.#subscribeToPresence((event, payload) => {
138
- if (event === PresenceEventTypes.JOIN) {
139
- return callback(payload.changed);
140
- }
141
- });
142
- }
143
-
144
- /**
145
- * @desc Detects when clients leave the channel.
146
- * @param callback - The callback to call when a client leaves the channel.
147
- */
148
- public onLeave (callback: (presence: PondPresence) => void) {
149
- return this.#subscribeToPresence((event, payload) => {
150
- if (event === PresenceEventTypes.LEAVE) {
151
- return callback(payload.changed);
152
- }
153
- });
154
- }
155
-
156
- /**
157
- * @desc Detects when clients change their presence in the channel.
158
- * @param callback - The callback to call when a client changes their presence in the channel.
159
- */
160
- public onPresenceChange (callback: (presence: PresencePayload) => void) {
161
- return this.#subscribeToPresence((event, payload) => {
162
- if (event === PresenceEventTypes.UPDATE) {
163
- return callback(payload);
164
- }
165
- });
166
- }
167
-
168
- /**
169
- * @desc Sends a message to specific clients in the channel.
170
- * @param event - The event to send.
171
- * @param payload - The message to send.
172
- * @param recipient - The clients to send the message to.
173
- */
174
- public sendMessage (event: string, payload: PondMessage, recipient: string[]) {
175
- this.#send(event, payload, recipient);
176
- }
177
-
178
- /**
179
- * @desc Broadcasts a message to every other client in the channel except yourself.
180
- * @param event - The event to send.
181
- * @param payload - The message to send.
182
- */
183
- public broadcastFrom (event: string, payload: PondMessage) {
184
- this.#send(event, payload, ChannelReceiver.ALL_EXCEPT_SENDER);
185
- }
186
-
187
- /**
188
- * @desc Broadcasts a message to the channel, including yourself.
189
- * @param event - The event to send.
190
- * @param payload - The message to send.
191
- */
192
- public broadcast (event: string, payload: PondMessage) {
193
- this.#send(event, payload);
194
- }
195
-
196
- /**
197
- * @desc Gets the current connection state of the channel.
198
- */
199
- public get channelState (): ChannelState {
200
- return this.#joinState.value as ChannelState;
201
- }
202
-
203
- /**
204
- * @desc Gets the current presence of the channel.
205
- */
206
- public getPresence (): PondPresence[] {
207
- return this.#presence;
208
- }
209
-
210
- /**
211
- * @desc Monitors the presence of the channel.
212
- * @param callback - The callback to call when the presence changes.
213
- */
214
- public onUsersChange (callback: (users: PondPresence[]) => void) {
215
- return this.#subscribeToPresence((_event, payload) => callback(payload.presence));
216
- }
217
-
218
- /**
219
- * @desc Gets the current connection state of the channel.
220
- */
221
- public isConnected () {
222
- return this.#joinState.value === ChannelState.JOINED || this.#joinState.value === ChannelState.STALLED;
223
- }
224
-
225
- /**
226
- * @desc Monitors the connection state of the channel.
227
- * @param callback - The callback to call when the connection state changes.
228
- */
229
- public onConnectionChange (callback: (connected: boolean) => void) {
230
- return this.onChannelStateChange((state) => {
231
- callback(state === ChannelState.JOINED || state === ChannelState.STALLED);
232
- });
233
- }
234
-
235
- /**
236
- * @desc Builds a message structure to and sends it to the server.
237
- * @param event - The event to send.
238
- * @param payload - The message to send.
239
- * @param receivers - The clients to send the message to.
240
- * @private
241
- */
242
- #send (event: string, payload: PondMessage, receivers: ChannelReceivers = ChannelReceiver.ALL_USERS) {
243
- const message: ClientMessage = {
244
- action: ClientActions.BROADCAST,
245
- channelName: this.#name,
246
- event,
247
- payload,
248
- addresses: receivers,
249
- };
250
-
251
- this.#publish(message);
252
- }
253
-
254
- /**
255
- * @desc Publishes a message received from the server.
256
- * @param data - The message to publish.
257
- * @private
258
- */
259
- #publish (data: ClientMessage) {
260
- if (this.#clientState.value) {
261
- if (this.#joinState.value === ChannelState.JOINED) {
262
- this.#publisher(data);
263
- }
264
-
265
- return;
266
- }
267
-
268
- this.#queue.push(data);
269
- }
270
-
271
- /**
272
- * @desc Publishes all presence events to the channel.
273
- * @param callback - The callback to call when a presence event is received.
274
- * @private
275
- */
276
- #subscribeToPresence (callback: (event: PresenceEventTypes, payload: PresencePayload) => void) {
277
- return this.#receiver.subscribe((data) => {
278
- if (data.action === ServerActions.PRESENCE) {
279
- return callback(data.event, data.payload);
280
- }
281
- });
282
- }
283
-
284
- /**
285
- * 2desc Initializes the channel.
286
- * @param receiver - The receiver to subscribe to.
287
- * @private
288
- */
289
- #init (receiver: SimpleSubject<ChannelEvent>): Unsubscribe {
290
- const unsubMessages = receiver.subscribe((data) => {
291
- if (data.channelName === this.#name) {
292
- if (data.event === Events.ACKNOWLEDGE) {
293
- this.#joinState.publish(ChannelState.JOINED);
294
- this.#emptyQueue();
295
- } else {
296
- this.#receiver.publish(data);
297
- }
298
- }
299
- });
300
-
301
- const unsubStateChange = this.#clientState.subscribe((state) => {
302
- if (state && this.#joinState.value === ChannelState.STALLED) {
303
- const joinMessage: ClientMessage = {
304
- action: ClientActions.JOIN_CHANNEL,
305
- channelName: this.#name,
306
- event: ClientActions.JOIN_CHANNEL,
307
- payload: this.#joinParams,
308
- };
309
-
310
- this.#publisher(joinMessage);
311
- } else if (!state && this.#joinState.value === ChannelState.JOINED) {
312
- this.#joinState.publish(ChannelState.STALLED);
313
- }
314
- });
315
-
316
- const unsubPresence = this.#subscribeToPresence((_, payload) => {
317
- this.#presence = payload.presence;
318
- });
319
-
320
- return () => {
321
- unsubMessages();
322
- unsubStateChange();
323
- unsubPresence();
324
- };
325
- }
326
-
327
- /**
328
- * @desc Publishes the queue of messages to the server.
329
- * @private
330
- */
331
- #emptyQueue () {
332
- this.#queue
333
- .filter((message) => message.action !== ClientActions.JOIN_CHANNEL)
334
- .forEach((message) => {
335
- this.#publisher(message);
336
- });
337
-
338
- this.#joinState.publish(ChannelState.JOINED);
339
-
340
- this.#queue = [];
341
- }
342
- }
package/src/client.ts DELETED
@@ -1,124 +0,0 @@
1
- import { Channel } from './client/channel';
2
- import { ChannelState } from './enums';
3
- import { SimpleSubject, SimpleBehaviorSubject } from './subjects/subject';
4
- // eslint-disable-next-line import/no-unresolved
5
- import { ChannelEvent, JoinParams, ClientMessage } from './types';
6
-
7
- export default class PondClient {
8
- protected readonly _address: URL;
9
-
10
- protected _socket: WebSocket | any | undefined;
11
-
12
- #channels: Record<string, Channel>;
13
-
14
- protected readonly _broadcaster: SimpleSubject<ChannelEvent>;
15
-
16
- protected readonly _connectionState: SimpleBehaviorSubject<boolean>;
17
-
18
- constructor (endpoint: string, params: Record<string, any> = {}) {
19
- let address: URL;
20
-
21
- try {
22
- address = new URL(endpoint);
23
- } catch (e) {
24
- address = new URL(window.location.toString());
25
- address.pathname = endpoint;
26
- }
27
-
28
- const query = new URLSearchParams(params);
29
-
30
- address.search = query.toString();
31
- const protocol = address.protocol === 'https:' ? 'wss:' : 'ws:';
32
-
33
- if (address.protocol !== 'wss:' && address.protocol !== 'ws:') {
34
- address.protocol = protocol;
35
- }
36
-
37
- this._address = address;
38
- this.#channels = {};
39
-
40
- this._broadcaster = new SimpleSubject<ChannelEvent>();
41
- this._connectionState = new SimpleBehaviorSubject<boolean>(false);
42
- }
43
-
44
- /**
45
- * @desc Connects to the server and returns the socket.
46
- */
47
- public connect (backoff = 1) {
48
- const socket = new WebSocket(this._address.toString());
49
-
50
- socket.onopen = () => {
51
- this._connectionState.publish(true);
52
- };
53
-
54
- socket.onmessage = (message) => {
55
- const data = JSON.parse(message.data) as ChannelEvent;
56
-
57
- this._broadcaster.publish(data);
58
- };
59
-
60
- socket.onerror = () => {
61
- this._connectionState.publish(false);
62
- setTimeout(() => {
63
- this.connect(backoff * 2);
64
- }, backoff * 1000);
65
- };
66
-
67
- this._socket = socket;
68
- }
69
-
70
- /**
71
- * @desc Returns the current state of the socket.
72
- */
73
- public getState () {
74
- return this._connectionState.value as boolean;
75
- }
76
-
77
- /**
78
- * @desc Disconnects the socket.
79
- */
80
- public disconnect () {
81
- Object.values(this.#channels).forEach((channel) => channel.leave());
82
- this._socket?.close();
83
- this.#channels = {};
84
- }
85
-
86
- /**
87
- * @desc Creates a channel with the given name and params.
88
- * @param name - The name of the channel.
89
- * @param params - The params to send to the server.
90
- */
91
- public createChannel (name: string, params?: JoinParams) {
92
- if (this.#channels[name] && this.#channels[name].channelState !== ChannelState.CLOSED) {
93
- return this.#channels[name];
94
- }
95
-
96
- const publisher = this.#createPublisher();
97
-
98
- const channel = new Channel(publisher, this._connectionState, name, this._broadcaster, params);
99
-
100
- this.#channels[name] = channel;
101
-
102
- return channel;
103
- }
104
-
105
- /**
106
- * @desc Subscribes to the connection state.
107
- * @param callback - The callback to call when the state changes.
108
- */
109
- public onConnectionChange (callback: (state: boolean) => void) {
110
- return this._connectionState.subscribe(callback);
111
- }
112
-
113
- /**
114
- * @desc Returns a function that publishes a message to the socket.
115
- * @private
116
- */
117
- #createPublisher () {
118
- return (message: ClientMessage) => {
119
- if (this._connectionState.value) {
120
- this._socket.send(JSON.stringify(message));
121
- }
122
- };
123
- }
124
- }