@eleven-am/pondsocket 0.1.126 → 0.1.128

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.
package/schema.js CHANGED
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.clientMessageSchema = void 0;
4
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
4
5
  const zod_1 = require("zod");
5
- const enums_1 = require("./enums");
6
6
  exports.clientMessageSchema = zod_1.z.object({
7
7
  event: zod_1.z.string(),
8
8
  requestId: zod_1.z.string(),
9
9
  channelName: zod_1.z.string(),
10
10
  payload: zod_1.z.record(zod_1.z.any()),
11
- action: zod_1.z.nativeEnum(enums_1.ClientActions),
12
- addresses: zod_1.z.union([zod_1.z.nativeEnum(enums_1.ChannelReceiver), zod_1.z.array(zod_1.z.string())]).optional(),
11
+ action: zod_1.z.nativeEnum(pondsocket_common_1.ClientActions),
12
+ addresses: zod_1.z.union([zod_1.z.nativeEnum(pondsocket_common_1.ChannelReceiver), zod_1.z.array(zod_1.z.string())]).optional(),
13
13
  });
@@ -14,11 +14,11 @@ var _PondSocket_instances, _PondSocket_server, _PondSocket_socketServer, _PondSo
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  exports.PondSocket = void 0;
16
16
  const http_1 = require("http");
17
+ const pondsocket_common_1 = require("@eleven-am/pondsocket-common");
17
18
  const ws_1 = require("ws");
18
19
  const middleware_1 = require("../abstracts/middleware");
19
20
  const endpoint_1 = require("../endpoint/endpoint");
20
21
  const response_1 = require("../endpoint/response");
21
- const enums_1 = require("../enums");
22
22
  const matcher_1 = require("../matcher/matcher");
23
23
  class PondSocket {
24
24
  constructor(server, socketServer) {
@@ -108,9 +108,10 @@ _PondSocket_server = new WeakMap(), _PondSocket_socketServer = new WeakMap(), _P
108
108
  __classPrivateFieldGet(this, _PondSocket_socketServer, "f").emit('connection', socket);
109
109
  __classPrivateFieldGet(this, _PondSocket_middleware, "f").run(request, socket, () => {
110
110
  const message = {
111
- action: enums_1.ServerActions.ERROR,
112
- event: enums_1.ErrorTypes.HANDLER_NOT_FOUND,
113
- channelName: enums_1.SystemSender.ENDPOINT,
111
+ action: pondsocket_common_1.ServerActions.ERROR,
112
+ event: pondsocket_common_1.ErrorTypes.HANDLER_NOT_FOUND,
113
+ channelName: pondsocket_common_1.SystemSender.ENDPOINT,
114
+ requestId: (0, pondsocket_common_1.uuid)(),
114
115
  payload: {
115
116
  message: 'No endpoint found',
116
117
  code: 404,
package/types.d.ts CHANGED
@@ -1,17 +1,9 @@
1
1
  import { Server as HTTPServer, IncomingHttpHeaders } from 'http';
2
2
 
3
- import type { ModuleMetadata, DynamicModule, Type } from '@nestjs/common';
4
- import type { Express } from 'express';
5
3
  import { WebSocketServer } from 'ws';
6
4
 
7
5
  type Unsubscribe = () => void;
8
6
 
9
- type Constructor<T> = new (...args: any[]) => T;
10
- declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
11
- declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
12
- declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
13
- declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
14
-
15
7
  export type default_t<T = any> = Record<string, T>;
16
8
  type IsParam<Path> = Path extends `:${infer Param}` ? Param : never;
17
9
 
@@ -33,11 +25,6 @@ type Params<Path> = {
33
25
  [Key in FilteredParams<Path>]: string
34
26
  }
35
27
 
36
- interface EndpointMetadata {
37
- path?: string;
38
- channels: Constructor<NonNullable<unknown>>[];
39
- }
40
-
41
28
  type PondPath<Path extends string> = Path | RegExp;
42
29
 
43
30
  type EventParams<Path> = {
@@ -51,11 +38,6 @@ type PondMessage = PondObject;
51
38
  type PondAssigns = PondObject;
52
39
  type JoinParams = PondObject;
53
40
 
54
- interface PresencePayload {
55
- changed: PondPresence;
56
- presence: PondPresence[];
57
- }
58
-
59
41
  interface UserPresences {
60
42
  [userId: string]: PondPresence;
61
43
  }
@@ -64,12 +46,6 @@ interface UserAssigns {
64
46
  [userId: string]: PondAssigns;
65
47
  }
66
48
 
67
- interface Metadata extends Omit<ModuleMetadata, 'controllers'> {
68
- endpoints: Constructor<NonNullable<unknown>>[];
69
- guards?: Constructor<CanActivate>[];
70
- isGlobal?: boolean;
71
- }
72
-
73
49
  type PondEvent<Path> = EventParams<Path> & {
74
50
  payload: PondMessage;
75
51
  event: string;
@@ -89,131 +65,15 @@ interface LeaveEvent {
89
65
 
90
66
  type LeaveCallback = (event: LeaveEvent) => void;
91
67
 
92
- type ParamDecoratorCallback<Input> = (data: Input, context: Context) => unknown | Promise<unknown>;
93
68
 
94
69
  type PondEvenType = { [key: string]: PondMessage };
95
70
 
96
- type NestFuncType<Event extends string, Payload extends PondMessage> = {
97
- event?: Event;
98
- broadcast?: Event;
99
- assigns?: PondAssigns;
100
- presence?: PondPresence;
101
- updatePresence?: PondPresence;
102
- } & Payload;
103
-
104
- type NestReturnType<EventType extends PondEvenType, Event extends keyof EventType> = Event extends string ?
105
- NestFuncType<Event, EventType[Event]> :
106
- never;
107
-
108
71
  interface UserData {
109
72
  assigns: PondAssigns;
110
73
  presence: PondPresence;
111
74
  id: string;
112
75
  }
113
76
 
114
- export interface CanActivate {
115
-
116
- /**
117
- * @desc Whether the client can continue with the request
118
- * @param context - The context of the request
119
- */
120
- canActivate(context: Context): boolean | Promise<boolean>;
121
- }
122
-
123
- export enum ChannelState {
124
- IDLE = 'IDLE',
125
- JOINING = 'JOINING',
126
- JOINED = 'JOINED',
127
- STALLED = 'STALLED',
128
- CLOSED = 'CLOSED',
129
- }
130
-
131
- declare class Context<Path extends string = string> {
132
- /**
133
- * @desc The request object, available in onJoin handlers
134
- */
135
- joinRequest: JoinRequest<Path> | null;
136
-
137
- /**
138
- * @desc The request object, available in onEvent handlers
139
- */
140
- eventRequest: EventRequest<Path> | null;
141
-
142
- /**
143
- * @desc The request object, available in onConnection handlers
144
- */
145
- connection: IncomingConnection<Path> | null;
146
-
147
- /**
148
- * @desc The leave event, available in onLeave handlers
149
- */
150
- leveeEvent: LeaveEvent | null;
151
-
152
- /**
153
- * @desc The response object, available in onJoin handlers
154
- */
155
- joinResponse: JoinResponse | null;
156
-
157
- /**
158
- * @desc The response object, available in onEvent handlers
159
- */
160
- eventResponse: EventResponse | null;
161
-
162
- /**
163
- * @desc The response object, available in onConnection handlers
164
- */
165
- connectionResponse: ConnectionResponse | null;
166
-
167
- /**
168
- * @desc The user data, available in onJoin, onEvent handlers
169
- */
170
- user: UserData | null;
171
-
172
- /**
173
- * @desc The channel, available in onJoin, onEvent handlers
174
- */
175
- channel: Channel | null;
176
-
177
- /**
178
- * @desc The assigns, available in onJoin, onEvent handlers
179
- */
180
- presence: UserPresences | null;
181
-
182
- /**
183
- * @desc The assigns, available in onJoin, onEvent handlers
184
- */
185
- event: PondEvent<Path> | null;
186
-
187
- /**
188
- * @desc The assigns, available in onJoin, onEvent handlers
189
- */
190
- assigns: PondAssigns | null;
191
-
192
- /**
193
- * @desc Returns the *type* of the controller class which the current handler belongs to.
194
- */
195
- getClass<T = any>(): Type<T>;
196
-
197
- /**
198
- * @desc Returns a reference to the handler (method) that will be invoked next in the
199
- * request pipeline.
200
- */
201
- getHandler(): Function;
202
-
203
- /**
204
- * @desc Adds request data to the context
205
- * @param key - the key to add
206
- * @param value - the value to add
207
- */
208
- public addData(key: string, value: unknown): void;
209
-
210
- /**
211
- * @desc Retrieves request data from the context
212
- * @param key - the key to retrieve
213
- */
214
- public getData<A = unknown>(key: string): A | null;
215
- }
216
-
217
77
  declare class AbstractRequest<Path extends string> {
218
78
  event: PondEvent<Path>;
219
79
 
@@ -322,125 +182,6 @@ declare class EventResponse<EventType extends PondEvenType = PondEvenType> {
322
182
  closeChannel (reason: string): void;
323
183
  }
324
184
 
325
- export declare class ClientChannel<EventType extends PondEvenType = PondEvenType> {
326
- channelState: ChannelState;
327
-
328
- /**
329
- * @desc Connects to the channel.
330
- */
331
- join (): void;
332
-
333
- /**
334
- * @desc Disconnects from the channel.
335
- */
336
- leave (): void;
337
-
338
- /**
339
- * @desc Monitors the channel for messages.
340
- * @param callback - The callback to call when a message is received.
341
- */
342
- onMessage (callback: (event: keyof EventType, message: EventType[keyof EventType]) => void): Unsubscribe;
343
-
344
- /**
345
- * @desc Monitors the channel for messages.
346
- * @param event - The event to monitor.
347
- * @param callback - The callback to call when a message is received.
348
- */
349
- onMessageEvent <Key extends keyof EventType> (event: Key, callback: (message: EventType[Key]) => void): Unsubscribe;
350
-
351
- /**
352
- * @desc Monitors the channel state of the channel.
353
- * @param callback - The callback to call when the connection state changes.
354
- */
355
- onChannelStateChange (callback: (connected: ChannelState) => void): Unsubscribe;
356
-
357
- /**
358
- * @desc Detects when clients join the channel.
359
- * @param callback - The callback to call when a client joins the channel.
360
- */
361
- onJoin (callback: (presence: PondPresence) => void): Unsubscribe;
362
-
363
- /**
364
- * @desc Detects when clients leave the channel.
365
- * @param callback - The callback to call when a client leaves the channel.
366
- */
367
- onLeave (callback: (presence: PondPresence) => void): Unsubscribe;
368
-
369
- /**
370
- * @desc Detects when clients change their presence in the channel.
371
- * @param callback - The callback to call when a client changes their presence in the channel.
372
- */
373
- onPresenceChange (callback: (presence: PresencePayload) => void): Unsubscribe;
374
-
375
- /**
376
- * @desc Sends a message to specific clients in the channel.
377
- * @param event - The event to send.
378
- * @param payload - The message to send.
379
- * @param recipient - The clients to send the message to.
380
- */
381
- sendMessage <Key extends keyof EventType> (event: Key, payload: EventType[Key], recipient: string[]): void;
382
-
383
- /**
384
- * @desc Sends a message to the server and waits for a response.
385
- * @param sentEvent - The event to send.
386
- * @param payload - The message to send.
387
- * @param responseEvent - The event to wait for.
388
- */
389
- sendForResponse <ResponseKey extends keyof EventType, SentKey extends keyof EventType> (sentEvent: SentKey, payload: EventType[SentKey], responseEvent: ResponseKey): Promise<EventType[ResponseKey]>;
390
-
391
- /**
392
- * @desc Broadcasts a message to every other client in the channel except yourself.
393
- * @param event - The event to send.
394
- * @param payload - The message to send.
395
- */
396
- broadcastFrom <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
397
-
398
- /**
399
- * @desc Broadcasts a message to the channel, including yourself.
400
- * @param event - The event to send.
401
- * @param payload - The message to send.
402
- */
403
- broadcast <Key extends keyof EventType> (event: Key, payload: EventType[Key]): void;
404
-
405
- /**
406
- * @desc Gets the current presence of the channel.
407
- */
408
- getPresence (): PondPresence[];
409
-
410
- /**
411
- * @desc Monitors the presence of the channel.
412
- * @param callback - The callback to call when the presence changes.
413
- */
414
- onUsersChange (callback: (users: PondPresence[]) => void): Unsubscribe;
415
-
416
- /**
417
- * @desc Checks if the channel is connected.
418
- */
419
- isConnected (): boolean;
420
-
421
- /**
422
- * @desc Checks if the channel is stalled.
423
- */
424
- isStalled (): boolean;
425
-
426
- /**
427
- * @desc Checks if the channel is closed.
428
- */
429
- isClosed (): boolean;
430
-
431
- /**
432
- * @desc Monitors the connection state of the channel.
433
- * @param callback - The callback to call when the connection state changes.
434
- */
435
- onConnectionChange (callback: (connected: boolean) => void): Unsubscribe;
436
-
437
- /**
438
- * @desc Gets the first response from the channel.
439
- * @param event - The event to monitor.
440
- */
441
- getFirstResponse <Key extends keyof EventType> (event: Key): Promise<EventType[Key]>;
442
- }
443
-
444
185
  declare class Endpoint {
445
186
  /**
446
187
  * @desc Adds a new PondChannel to this path on this endpoint
@@ -678,7 +419,7 @@ export declare class PondChannel <EventType extends PondEvenType = PondEvenType>
678
419
  * @desc Gets a channel by name
679
420
  * @param channelName - The name of the channel to get
680
421
  */
681
- public getChannel <EventType extends PondEvenType = PondEvenType> (channelName: string): Channel<EventType> | null;
422
+ public getChannel (channelName: string): Channel<EventType> | null;
682
423
  }
683
424
 
684
425
  declare class PondSocket {
@@ -714,240 +455,3 @@ declare class PondSocket {
714
455
  */
715
456
  createEndpoint<Path extends string> (path: PondPath<Path>, handler: (request: IncomingConnection<Path>, response: ConnectionResponse) => void | Promise<void>): Endpoint;
716
457
  }
717
-
718
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
719
- // @ts-ignore
720
- interface PondSocketExpressApp extends Express {
721
-
722
- /**
723
- * @desc Accepts a new socket upgrade request on the provided endpoint using the handler function to authenticate the socket
724
- * @param path - the pattern to accept || can also be a regex
725
- * @param handler - the handler function to authenticate the socket
726
- * @example
727
- * const endpoint = pond.createEndpoint('/api/socket', (req, res) => {
728
- * const token = req.query.token;
729
- * if (!token)
730
- * return res.reject('No token provided');
731
- * res.accept({
732
- * assign: {
733
- * token
734
- * }
735
- * });
736
- * })
737
- */
738
- upgrade<Path extends string> (path: PondPath<Path>, handler: (request: IncomingConnection<Path>, response: ConnectionResponse) => void | Promise<void>): Endpoint;
739
- }
740
-
741
- declare class PondClient {
742
- constructor (endpoint: string, params?: Record<string, any>);
743
-
744
- /**
745
- * @desc Connects to the server and returns the socket.
746
- */
747
- connect (backoff?: number): void;
748
-
749
- /**
750
- * @desc Returns the current state of the socket.
751
- */
752
- getState (): boolean;
753
-
754
- /**
755
- * @desc Disconnects the socket.
756
- */
757
- disconnect (): void;
758
-
759
- /**
760
- * @desc Creates a channel with the given name and params.
761
- * @param name - The name of the channel.
762
- * @param params - The params to send to the server.
763
- */
764
- createChannel <EventType extends PondEvenType = PondEvenType> (name: string, params?: JoinParams): ClientChannel<EventType>;
765
-
766
- /**
767
- * @desc Subscribes to the connection state.
768
- * @param callback - The callback to call when the state changes.
769
- */
770
- onConnectionChange (callback: (state: boolean) => void): Unsubscribe;
771
- }
772
-
773
- declare const pondSocket: (app: Express) => PondSocketExpressApp;
774
-
775
- /**
776
- * @desc The Decorator for retrieving the JoinRequest object from the request in a handler
777
- * @returns {JoinRequest}
778
- */
779
- declare function GetJoinRequest(): ParameterDecorator;
780
-
781
- /**
782
- * @desc The Decorator for retrieving the JoinResponse object from the response in a handler
783
- * @returns {JoinResponse}
784
- */
785
- declare function GetJoinResponse(): ParameterDecorator;
786
-
787
- /**
788
- * @desc The Decorator for retrieving the JoinParams object from the request in a handler
789
- * @returns {JoinParams}
790
- */
791
- declare function GetJoinParams(): ParameterDecorator;
792
-
793
- /**
794
- * @desc The Decorator for retrieving the UserData object from the request in a handler
795
- * @returns {UserData}
796
- */
797
- declare function GetUserData(): ParameterDecorator;
798
-
799
- /**
800
- * @desc The Decorator for retrieving the Channel object from the request in a handler
801
- * @returns {Channel}
802
- */
803
- declare function GetInternalChannel(): ParameterDecorator;
804
-
805
- /**
806
- * @desc The Decorator for retrieving the UserPresences object from the request in a handler
807
- * @returns {UserPresences}
808
- */
809
- declare function GetUserPresences(): ParameterDecorator;
810
-
811
- /**
812
- * @desc The Decorator for retrieving the event payload from the request in a handler
813
- * @returns {PondMessage}
814
- */
815
- declare function GetEventPayload(): ParameterDecorator;
816
-
817
- /**
818
- * @desc The Decorator for retrieving the EventRequest Params object from the request in a handler
819
- * @returns {Params}
820
- */
821
- declare function GetEventParams(): ParameterDecorator;
822
-
823
- /**
824
- * @desc The Decorator for retrieving the EventRequest Query object from the request in a handler
825
- * @returns {Record<string, string>}
826
- */
827
- declare function GetEventQuery(): ParameterDecorator;
828
-
829
- /**
830
- * @desc The Decorator for retrieving the EventResponse in a handler
831
- * @returns {EventResponse}
832
- */
833
- declare function GetEventResponse(): ParameterDecorator;
834
-
835
- /**
836
- * @desc The Decorator for retrieving the EventRequest in a handler
837
- * @returns {EventRequest}
838
- */
839
- declare function GetEventRequest(): ParameterDecorator;
840
-
841
- /**
842
- * @desc The Decorator for retrieving the ConnectionReQuest in a handler
843
- * @returns {IncomingConnection}
844
- */
845
- declare function GetConnectionRequest(): ParameterDecorator;
846
-
847
- /**
848
- * @desc The Decorator for retrieving the ConnectionResponse in a handler
849
- * @returns {ConnectionResponse}
850
- */
851
- declare function GetConnectionResponse(): ParameterDecorator;
852
-
853
- /**
854
- * @desc The Decorator for retrieving the ConnectionRequestId in a handler
855
- * @returns {string}
856
- */
857
- declare function GetConnectionRequestId(): ParameterDecorator;
858
-
859
- /**
860
- * @desc The Decorator for retrieving the ConnectionParams in a handler
861
- * @returns {Params}
862
- */
863
- declare function GetConnectionParams(): ParameterDecorator;
864
-
865
- /**
866
- * @desc The Decorator for retrieving the ConnectionHeaders from the request in a handler
867
- * @returns {IncomingHttpHeaders}
868
- */
869
- declare function GetConnectionHeaders(): ParameterDecorator;
870
-
871
- /**
872
- * @desc The Decorator for retrieving the ConnectionQuery in a handler
873
- * @returns {Record<string, string>}
874
- */
875
- declare function GetConnectionQuery(): ParameterDecorator;
876
-
877
- /**
878
- * @desc The Decorator for retrieving the LeaveEvent in a handler
879
- * @returns {LeaveEvent}
880
- */
881
- declare function GetLeaveEvent(): ParameterDecorator;
882
-
883
- /**
884
- * @desc The Decorator for retrieving the Channel object from the request in a handler
885
- * @returns {Channel}
886
- */
887
- declare function GetChannel (): ParameterDecorator;
888
-
889
- /**
890
- * @desc Function to create a param decorator
891
- * @param callback - The callback to call when the decorator is used
892
- * @returns {ReturnType<callback>}
893
- */
894
- declare function createParamDecorator<Input> (callback: ParamDecoratorCallback<Input>): (input: Input) => ParameterDecorator;
895
-
896
- /**
897
- * @desc Marks a method as a handler for JoinRequest events. Throwing an error will reject the request with the error message.
898
- */
899
- declare function OnJoinRequest(): MethodDecorator;
900
-
901
- /**
902
- * @desc Marks a method as a handler for events with the specified name. Throwing an error will reject the request with the error message
903
- * @param event - The name of the event to handle.
904
- */
905
- declare function OnEvent(event?: string): MethodDecorator;
906
-
907
- /**
908
- * @desc Marks a method as a handler for Leave events. Useful for cleaning up resources when a user leaves.
909
- */
910
- declare function OnLeaveEvent (): MethodDecorator;
911
-
912
- /**
913
- * @desc Marks a method as a handler for ConnectionRequest events. Throwing an error will reject the request with the error message.
914
- */
915
- declare function OnConnectionRequest(): MethodDecorator;
916
-
917
- /**
918
- * Decorator to mark a class as a channel.
919
- * @param path - The path for the channel (default is '*').
920
- */
921
- declare function DChannel(path?: string): ClassDecorator;
922
-
923
- /**
924
- * Decorator to retrieve the channel instance as a read-only property in a class.
925
- * It may either return an instance of the PondChannel if no name is provided otherwise an instance of Channel or null (depending on if a client is connected)
926
- * @param name - The name of the channel to return
927
- */
928
- declare function ChannelInstance(name?: string): PropertyDecorator;
929
-
930
- /**
931
- * Decorator to retrieve the endpoint instance as a read-only property in a class.
932
- */
933
- declare function EndpointInstance (): PropertyDecorator;
934
-
935
- /**
936
- * Decorator to mark a class as an endpoint.
937
- * @param metadata - The metadata for the endpoint.
938
- */
939
- declare function DEndpoint(metadata: EndpointMetadata): ClassDecorator;
940
-
941
- /**
942
- * Decorator to add a guard to a class or method.
943
- * @param guards - The guards to add.
944
- */
945
- declare function PondGuards (...guards: Constructor<CanActivate>[]): MethodDecorator & ClassDecorator;
946
-
947
- declare class PondSocketModule {
948
- /**
949
- * @desc Creates a new PondSocketModule
950
- * @param metadata - The metadata for the module
951
- */
952
- static forRoot (metadata: Metadata): DynamicModule;
953
- }
package/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });