@eleven-am/pondsocket 0.1.79 → 0.1.82

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 (4) hide show
  1. package/nest.d.ts +2 -2
  2. package/nest.js +96 -109
  3. package/package.json +7 -7
  4. package/types.d.ts +15 -9
package/nest.d.ts CHANGED
@@ -13,7 +13,7 @@ import {
13
13
  GetConnectionRequest, GetConnectionResponse,
14
14
  GetConnectionQuery, GetConnectionHeaders,
15
15
  GetConnectionRequestId, GetEventPayload,
16
- GetUserData, GetUserPresence, GetInternalChannel,
16
+ GetUserData, GetUserPresences, GetInternalChannel,
17
17
  } from './types';
18
18
 
19
19
  export {
@@ -29,5 +29,5 @@ export {
29
29
  GetConnectionRequestId, GetEventPayload,
30
30
  GetConnectionParams, GetConnectionRequest,
31
31
  GetConnectionResponse, GetConnectionHeaders,
32
- GetUserData, GetUserPresence, GetInternalChannel,
32
+ GetUserData, GetUserPresences, GetInternalChannel,
33
33
  }
package/nest.js CHANGED
@@ -20,7 +20,7 @@ var __rest = (this && this.__rest) || function (s, e) {
20
20
  return t;
21
21
  };
22
22
  Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.PondSocketModule = exports.Endpoints = exports.Endpoint = exports.Channels = exports.ChannelInstance = exports.Channel = exports.OnConnectionRequest = exports.OnEvent = exports.OnJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetUserPresence = exports.GetInternalChannel = exports.GetUserData = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = void 0;
23
+ exports.PondSocketModule = exports.Endpoints = exports.Endpoint = exports.Channel = exports.ChannelInstance = exports.OnConnectionRequest = exports.OnEvent = exports.OnJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetUserPresences = exports.GetInternalChannel = exports.GetUserData = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = void 0;
24
24
  const common_1 = require("@nestjs/common");
25
25
  require("reflect-metadata");
26
26
  const pondSocket_1 = require("./server/pondSocket");
@@ -44,9 +44,18 @@ const connectionHeadersKey = Symbol('connectionHeadersKey');
44
44
  const onJoinHandlerKey = Symbol('onJoinHandlerKey');
45
45
  const onEventHandlerKey = Symbol('onEventHandlerKey');
46
46
  const onConnectionHandlerKey = Symbol('onConnectionHandlerKey');
47
- const channelsKey = Symbol('channelsKey');
48
- const endpointsKey = Symbol('endpointsKey');
49
47
  const channelInstanceKey = Symbol('channelInstanceKey');
48
+ const channelClassKey = Symbol('channel');
49
+ const endpointClassKey = Symbol('endpoint');
50
+ const channelsClassKey = Symbol('channels');
51
+ const endpointsClassKey = Symbol('endpoints');
52
+ function createClassDecorator(key, value) {
53
+ return (0, common_1.applyDecorators)((0, common_1.Injectable)(), (0, common_1.SetMetadata)(key, value));
54
+ }
55
+ function getClassMetadata(key, target) {
56
+ var _a;
57
+ return (_a = Reflect.getMetadata(key, target)) !== null && _a !== void 0 ? _a : null;
58
+ }
50
59
  function createParamDecorator(key, error) {
51
60
  return (target, propertyKey, parameterIndex) => {
52
61
  const existingParams = Reflect.getMetadata(key, target, propertyKey);
@@ -63,16 +72,6 @@ function resolveParamDecorator(key, target, propertyKey) {
63
72
  }
64
73
  return index;
65
74
  }
66
- function createClassDecorator(key, target) {
67
- return {
68
- get() {
69
- return (Reflect.getMetadata(key, target) || []);
70
- },
71
- set(value) {
72
- Reflect.defineMetadata(key, value, target);
73
- },
74
- };
75
- }
76
75
  function manageClassData(key, target) {
77
76
  return {
78
77
  get() {
@@ -113,12 +112,6 @@ function manageEventHandlers(target) {
113
112
  function manageConnectionHandlers(target) {
114
113
  return manageHandlers(onConnectionHandlerKey, target);
115
114
  }
116
- function manageChannels(target) {
117
- return createClassDecorator(channelsKey, target);
118
- }
119
- function manageEndpoints(target) {
120
- return createClassDecorator(endpointsKey, target);
121
- }
122
115
  function GetJoinRequest() {
123
116
  return createParamDecorator(joinRequestKey, 'JoinRequest decorator already applied');
124
117
  }
@@ -139,10 +132,10 @@ function GetInternalChannel() {
139
132
  return createParamDecorator(internalChannelKey, 'InternalChannel decorator already applied');
140
133
  }
141
134
  exports.GetInternalChannel = GetInternalChannel;
142
- function GetUserPresence() {
135
+ function GetUserPresences() {
143
136
  return createParamDecorator(userPresenceKey, 'UserPresence decorator already applied');
144
137
  }
145
- exports.GetUserPresence = GetUserPresence;
138
+ exports.GetUserPresences = GetUserPresences;
146
139
  function GetEventPayload() {
147
140
  return createParamDecorator(eventPayloadKey, 'EventPayload decorator already applied');
148
141
  }
@@ -245,6 +238,7 @@ function resolveJoinParameters(request, response, target, propertyKey) {
245
238
  }
246
239
  function resolveEventParameters(request, response, target, propertyKey) {
247
240
  const userDataIndex = resolveParamDecorator(userDataKey, target, propertyKey);
241
+ const userPresenceIndex = resolveParamDecorator(userPresenceKey, target, propertyKey);
248
242
  const internalChannelIndex = resolveParamDecorator(internalChannelKey, target, propertyKey);
249
243
  const eventParamsIndex = resolveParamDecorator(eventParamsKey, target, propertyKey);
250
244
  const eventQueryIndex = resolveParamDecorator(eventQueryKey, target, propertyKey);
@@ -259,12 +253,12 @@ function resolveEventParameters(request, response, target, propertyKey) {
259
253
  eventPayloadIndex,
260
254
  eventResponseIndex,
261
255
  eventRequestIndex,
256
+ userPresenceIndex,
262
257
  ].filter((index) => typeof index === 'number');
263
258
  const rejectedKeys = [
264
259
  joinRequestKey,
265
260
  joinResponseKey,
266
261
  joinParamsKey,
267
- userPresenceKey,
268
262
  connectionRequestKey,
269
263
  connectionResponseKey,
270
264
  connectionRequestIdKey,
@@ -294,6 +288,8 @@ function resolveEventParameters(request, response, target, propertyKey) {
294
288
  return response;
295
289
  case eventRequestIndex:
296
290
  return request;
291
+ case userPresenceIndex:
292
+ return request.presence;
297
293
  default:
298
294
  throw new Error('Invalid parameter decorator');
299
295
  }
@@ -357,9 +353,9 @@ function OnJoinRequest() {
357
353
  return (target, propertyKey, descriptor) => {
358
354
  const originalMethod = descriptor.value;
359
355
  const { set } = manageJoinHandlers(target);
360
- set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
356
+ set('', (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
361
357
  try {
362
- const data = yield originalMethod.apply(target, resolveJoinParameters(request, response, target, propertyKey));
358
+ const data = yield originalMethod.apply(instance, resolveJoinParameters(request, response, target, propertyKey));
363
359
  if (!response.hasResponded) {
364
360
  if (data) {
365
361
  const { event } = data, rest = __rest(data, ["event"]);
@@ -381,9 +377,9 @@ function OnEvent(event = '*') {
381
377
  return (target, propertyKey, descriptor) => {
382
378
  const originalMethod = descriptor.value;
383
379
  const { set } = manageEventHandlers(target);
384
- set(event, (request, response) => __awaiter(this, void 0, void 0, function* () {
380
+ set(event, (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
385
381
  try {
386
- const data = yield originalMethod.apply(target, resolveEventParameters(request, response, target, propertyKey));
382
+ const data = yield originalMethod.apply(instance, resolveEventParameters(request, response, target, propertyKey));
387
383
  if (!response.hasResponded) {
388
384
  if (data) {
389
385
  const { event } = data, rest = __rest(data, ["event"]);
@@ -405,9 +401,9 @@ function OnConnectionRequest() {
405
401
  return (target, propertyKey, descriptor) => {
406
402
  const originalMethod = descriptor.value;
407
403
  const { set } = manageConnectionHandlers(target);
408
- set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
404
+ set('', (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
409
405
  try {
410
- const data = yield originalMethod.apply(target, resolveConnectionParameters(request, response, target, propertyKey));
406
+ const data = yield originalMethod.apply(instance, resolveConnectionParameters(request, response, target, propertyKey));
411
407
  if (!response.hasResponded) {
412
408
  if (data) {
413
409
  const { event } = data, rest = __rest(data, ["event"]);
@@ -425,34 +421,9 @@ function OnConnectionRequest() {
425
421
  };
426
422
  }
427
423
  exports.OnConnectionRequest = OnConnectionRequest;
428
- function Channel(path = '*') {
429
- return (constructor) => class extends constructor {
430
- _setEndpoint(endpoint) {
431
- const channel = endpoint.createChannel(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
432
- const { get } = manageJoinHandlers(this);
433
- const [handler] = get();
434
- if (handler) {
435
- yield handler.value(request, response);
436
- }
437
- else {
438
- response.accept();
439
- }
440
- }));
441
- const { set } = manageChannelInstance(constructor.prototype);
442
- const { get } = manageEventHandlers(this);
443
- set(channel);
444
- get().forEach((handler) => {
445
- channel.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
446
- yield handler.value(request, response);
447
- }));
448
- });
449
- }
450
- };
451
- }
452
- exports.Channel = Channel;
453
424
  function ChannelInstance() {
454
425
  return (target, propertyKey) => {
455
- const { get } = manageChannelInstance(target.constructor.prototype);
426
+ const { get } = manageChannelInstance(target);
456
427
  Object.defineProperty(target, propertyKey, {
457
428
  get() {
458
429
  return get();
@@ -464,54 +435,28 @@ function ChannelInstance() {
464
435
  };
465
436
  }
466
437
  exports.ChannelInstance = ChannelInstance;
467
- function Channels(channels) {
468
- return (constructor) => {
469
- const { set } = manageChannels(constructor.prototype);
470
- set(channels);
471
- return constructor;
472
- };
473
- }
474
- exports.Channels = Channels;
475
- function Endpoint(path = '*') {
476
- return (constructor) => class extends constructor {
477
- _setSocket(moduleRef, socket) {
478
- const { get } = manageConnectionHandlers(this);
479
- const { get: getChannels } = manageChannels(this);
480
- const [handler] = get();
481
- const endpoint = socket.createEndpoint(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
482
- if (handler) {
483
- yield handler.value(request, response);
484
- }
485
- else {
486
- response.accept();
487
- }
488
- }));
489
- getChannels().forEach((channel) => {
490
- const chan = moduleRef.get(channel, {
491
- strict: false,
492
- });
493
- chan._setEndpoint(endpoint);
494
- });
495
- }
496
- };
497
- }
438
+ const Channel = (path = '*') => createClassDecorator(channelClassKey, path);
439
+ exports.Channel = Channel;
440
+ const SetEndpoint = (path = '*') => createClassDecorator(endpointClassKey, path);
441
+ const SetChannels = (channels) => createClassDecorator(channelsClassKey, channels);
442
+ const getChannels = (target) => {
443
+ var _a;
444
+ return (_a = getClassMetadata(channelsClassKey, target)) !== null && _a !== void 0 ? _a : [];
445
+ };
446
+ const Endpoint = (metadata) => (0, common_1.applyDecorators)(SetChannels(metadata.channels), SetEndpoint(metadata.path));
498
447
  exports.Endpoint = Endpoint;
499
- function Endpoints(endpoints) {
500
- return (constructor) => {
501
- const channels = endpoints.reduce((acc, endpoint) => {
502
- const { get } = manageChannels(endpoint.prototype);
503
- const channels = get();
504
- return [...acc, ...channels];
505
- }, []);
506
- const { set } = manageEndpoints(constructor.prototype);
507
- set(endpoints);
508
- // eslint-disable-next-line new-cap
509
- return (0, common_1.Module)({
510
- providers: [...endpoints, ...channels],
511
- exports: [...endpoints, ...channels],
512
- })(constructor);
513
- };
514
- }
448
+ const Endpoints = ({ endpoints, providers = [], imports = [], exports = [], }) => (target) => {
449
+ const channels = endpoints.flatMap((endpoint) => getChannels(endpoint));
450
+ return (0, common_1.applyDecorators)(
451
+ // eslint-disable-next-line new-cap
452
+ (0, common_1.SetMetadata)(endpointsClassKey, endpoints),
453
+ // eslint-disable-next-line new-cap
454
+ (0, common_1.Module)({
455
+ imports,
456
+ providers: [...providers, ...endpoints, ...channels],
457
+ exports: [...exports, ...channels],
458
+ }))(target);
459
+ };
515
460
  exports.Endpoints = Endpoints;
516
461
  class PondSocketModule {
517
462
  constructor(moduleRef, adapterHost) {
@@ -519,16 +464,58 @@ class PondSocketModule {
519
464
  this.adapterHost = adapterHost;
520
465
  const expressInstance = this.adapterHost.httpAdapter;
521
466
  const server = expressInstance.getHttpServer();
522
- this.socket = new pondSocket_1.PondSocket(server);
467
+ const socket = new pondSocket_1.PondSocket(server);
523
468
  expressInstance.listen = (...args) => {
524
- const { get } = manageEndpoints(this);
525
- get().map((endpoint) => {
526
- const instance = this.moduleRef.get(endpoint, { strict: false });
527
- instance._setSocket(this.moduleRef, this.socket);
528
- return instance;
529
- });
530
- this.socket.listen(...args);
469
+ var _a;
470
+ const endpoints = (_a = getClassMetadata(endpointsClassKey, this.constructor)) !== null && _a !== void 0 ? _a : [];
471
+ endpoints.forEach((endpoint) => this.manageEndpoint(socket, endpoint));
472
+ return socket.listen(...args);
531
473
  };
532
474
  }
475
+ manageEndpoint(socket, endpoint) {
476
+ const endpointMetadata = getClassMetadata(endpointClassKey, endpoint);
477
+ if (!endpointMetadata) {
478
+ return;
479
+ }
480
+ const instance = this.moduleRef.get(endpoint, { strict: false });
481
+ const pondEndpoint = socket.createEndpoint(endpointMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
482
+ const { get } = manageConnectionHandlers(instance);
483
+ const [handler] = get();
484
+ if (handler) {
485
+ yield handler.value(instance, request, response);
486
+ }
487
+ else {
488
+ response.accept();
489
+ }
490
+ }));
491
+ getChannels(endpoint).forEach((channel) => {
492
+ this.manageChannel(channel, pondEndpoint);
493
+ });
494
+ }
495
+ manageChannel(channel, endpoint) {
496
+ const channelMetadata = getClassMetadata(channelClassKey, channel);
497
+ if (!channelMetadata) {
498
+ return;
499
+ }
500
+ const instance = this.moduleRef.get(channel, { strict: false });
501
+ const channelInstance = endpoint.createChannel(channelMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
502
+ const { get } = manageJoinHandlers(instance);
503
+ const [handler] = get();
504
+ if (handler) {
505
+ yield handler.value(instance, request, response);
506
+ }
507
+ else {
508
+ response.accept();
509
+ }
510
+ }));
511
+ const { get: getEventHandlers } = manageEventHandlers(instance);
512
+ const { set } = manageChannelInstance(instance);
513
+ getEventHandlers().forEach((handler) => {
514
+ channelInstance.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
515
+ yield handler.value(instance, request, response);
516
+ }));
517
+ });
518
+ set(channelInstance);
519
+ }
533
520
  }
534
521
  exports.PondSocketModule = PondSocketModule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.79",
3
+ "version": "0.1.82",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
@@ -29,8 +29,8 @@
29
29
  "url": "git+https://github.com/Eleven-am/pondSocket.git"
30
30
  },
31
31
  "dependencies": {
32
- "@nestjs/common": "^10.2.10",
33
- "@nestjs/core": "^10.2.10",
32
+ "@nestjs/common": "^10.3.0",
33
+ "@nestjs/core": "^10.3.0",
34
34
  "reflect-metadata": "^0.1.14",
35
35
  "websocket": "^1.0.34",
36
36
  "ws": "^8.15.1"
@@ -38,13 +38,13 @@
38
38
  "devDependencies": {
39
39
  "@types/express": "^4.17.21",
40
40
  "@types/jest": "^29.5.11",
41
- "@types/node": "^20.10.4",
41
+ "@types/node": "^20.10.5",
42
42
  "@types/websocket": "^1.0.10",
43
43
  "@types/ws": "^8.5.10",
44
- "@typescript-eslint/eslint-plugin": "^6.14.0",
45
- "eslint": "^8.55.0",
44
+ "@typescript-eslint/eslint-plugin": "^6.15.0",
45
+ "eslint": "^8.56.0",
46
46
  "eslint-plugin-file-progress": "^1.3.0",
47
- "eslint-plugin-import": "^2.29.0",
47
+ "eslint-plugin-import": "^2.29.1",
48
48
  "jest": "^29.7.0",
49
49
  "superwstest": "^2.0.3",
50
50
  "ts-jest": "^29.1.1",
package/types.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { ModuleMetadata } from '@nestjs/common/interfaces/modules/module-metadata.interface';
1
2
  import { Server as HTTPServer, IncomingHttpHeaders } from 'http';
2
3
 
3
4
  import type { ModuleRef, HttpAdapterHost } from '@nestjs/core';
@@ -7,6 +8,7 @@ import { WebSocketServer } from 'ws';
7
8
  type Unsubscribe = () => void;
8
9
 
9
10
  type Constructor<T> = new (...args: any[]) => T;
11
+ declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
10
12
  declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
11
13
  declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
12
14
  declare type ParameterDecorator = (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) => void;
@@ -48,6 +50,11 @@ interface UserAssigns {
48
50
  [userId: string]: PondAssigns;
49
51
  }
50
52
 
53
+ interface Metadata extends Omit<ModuleMetadata, 'controllers'> {
54
+ endpoints: Constructor<NonNullable<unknown>>[];
55
+ isGlobal?: boolean;
56
+ }
57
+
51
58
  type PondEvent<Path> = EventParams<Path> & {
52
59
  payload: PondMessage;
53
60
  event: string;
@@ -652,10 +659,10 @@ declare function GetUserData(): ParameterDecorator;
652
659
  declare function GetInternalChannel(): ParameterDecorator;
653
660
 
654
661
  /**
655
- * @desc The Decorator for retrieving the UserPresence object from the request in a handler
656
- * @returns {PondPresence}
662
+ * @desc The Decorator for retrieving the UserPresences object from the request in a handler
663
+ * @returns {UserPresences}
657
664
  */
658
- declare function GetUserPresence(): ParameterDecorator;
665
+ declare function GetUserPresences(): ParameterDecorator;
659
666
 
660
667
  /**
661
668
  * @desc The Decorator for retrieving the event payload from the request in a handler
@@ -724,19 +731,18 @@ declare function GetConnectionHeaders(): ParameterDecorator;
724
731
  declare function GetConnectionQuery(): ParameterDecorator;
725
732
 
726
733
  /**
727
- * @desc Marks a method as a handler for JoinRequest events.
728
- * @param - The path for the JoinRequest event.
734
+ * @desc Marks a method as a handler for JoinRequest events. Throwing an error will reject the request with the error message.
729
735
  */
730
736
  declare function OnJoinRequest(): MethodDecorator;
731
737
 
732
738
  /**
733
- * @desc Marks a method as a handler for events with the specified name.
739
+ * @desc Marks a method as a handler for events with the specified name. Throwing an error will reject the request with the error message
734
740
  * @param event - The name of the event to handle.
735
741
  */
736
742
  declare function OnEvent(event?: string): MethodDecorator;
737
743
 
738
744
  /**
739
- * @desc Marks a method as a handler for ConnectionRequest events.
745
+ * @desc Marks a method as a handler for ConnectionRequest events. Throwing an error will reject the request with the error message.
740
746
  */
741
747
  declare function OnConnectionRequest(): MethodDecorator;
742
748
 
@@ -765,9 +771,9 @@ declare function DEndpoint(path?: string): ClassDecorator;
765
771
 
766
772
  /**
767
773
  * Decorator to mark a class as having multiple endpoints.
768
- * @param endpoints - The array of endpoints.
774
+ * @param metadata - The metadata for the endpoints.
769
775
  */
770
- declare function Endpoints(endpoints: Constructor<NonNullable<unknown>>[]): ClassDecorator;
776
+ declare function Endpoints(metadata: Metadata): ClassDecorator;
771
777
 
772
778
  declare class PondSocketModule {
773
779
  /**