@eleven-am/pondsocket 0.1.81 → 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 (2) hide show
  1. package/nest.js +90 -119
  2. package/package.json +1 -1
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.GetUserPresences = 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
  }
@@ -360,9 +353,9 @@ function OnJoinRequest() {
360
353
  return (target, propertyKey, descriptor) => {
361
354
  const originalMethod = descriptor.value;
362
355
  const { set } = manageJoinHandlers(target);
363
- set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
356
+ set('', (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
364
357
  try {
365
- const data = yield originalMethod.apply(target, resolveJoinParameters(request, response, target, propertyKey));
358
+ const data = yield originalMethod.apply(instance, resolveJoinParameters(request, response, target, propertyKey));
366
359
  if (!response.hasResponded) {
367
360
  if (data) {
368
361
  const { event } = data, rest = __rest(data, ["event"]);
@@ -384,9 +377,9 @@ function OnEvent(event = '*') {
384
377
  return (target, propertyKey, descriptor) => {
385
378
  const originalMethod = descriptor.value;
386
379
  const { set } = manageEventHandlers(target);
387
- set(event, (request, response) => __awaiter(this, void 0, void 0, function* () {
380
+ set(event, (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
388
381
  try {
389
- const data = yield originalMethod.apply(target, resolveEventParameters(request, response, target, propertyKey));
382
+ const data = yield originalMethod.apply(instance, resolveEventParameters(request, response, target, propertyKey));
390
383
  if (!response.hasResponded) {
391
384
  if (data) {
392
385
  const { event } = data, rest = __rest(data, ["event"]);
@@ -408,9 +401,9 @@ function OnConnectionRequest() {
408
401
  return (target, propertyKey, descriptor) => {
409
402
  const originalMethod = descriptor.value;
410
403
  const { set } = manageConnectionHandlers(target);
411
- set('', (request, response) => __awaiter(this, void 0, void 0, function* () {
404
+ set('', (instance, request, response) => __awaiter(this, void 0, void 0, function* () {
412
405
  try {
413
- const data = yield originalMethod.apply(target, resolveConnectionParameters(request, response, target, propertyKey));
406
+ const data = yield originalMethod.apply(instance, resolveConnectionParameters(request, response, target, propertyKey));
414
407
  if (!response.hasResponded) {
415
408
  if (data) {
416
409
  const { event } = data, rest = __rest(data, ["event"]);
@@ -428,34 +421,9 @@ function OnConnectionRequest() {
428
421
  };
429
422
  }
430
423
  exports.OnConnectionRequest = OnConnectionRequest;
431
- function Channel(path = '*') {
432
- return (constructor) => class extends constructor {
433
- _setEndpoint(endpoint) {
434
- const channel = endpoint.createChannel(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
435
- const { get } = manageJoinHandlers(this);
436
- const [handler] = get();
437
- if (handler) {
438
- yield handler.value(request, response);
439
- }
440
- else {
441
- response.accept();
442
- }
443
- }));
444
- const { set } = manageChannelInstance(constructor.prototype);
445
- const { get } = manageEventHandlers(this);
446
- set(channel);
447
- get().forEach((handler) => {
448
- channel.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
449
- yield handler.value(request, response);
450
- }));
451
- });
452
- }
453
- };
454
- }
455
- exports.Channel = Channel;
456
424
  function ChannelInstance() {
457
425
  return (target, propertyKey) => {
458
- const { get } = manageChannelInstance(target.constructor.prototype);
426
+ const { get } = manageChannelInstance(target);
459
427
  Object.defineProperty(target, propertyKey, {
460
428
  get() {
461
429
  return get();
@@ -467,67 +435,28 @@ function ChannelInstance() {
467
435
  };
468
436
  }
469
437
  exports.ChannelInstance = ChannelInstance;
470
- function Channels(channels) {
471
- return (constructor) => {
472
- const { set } = manageChannels(constructor.prototype);
473
- set(channels);
474
- return constructor;
475
- };
476
- }
477
- exports.Channels = Channels;
478
- function Endpoint(path = '*') {
479
- return (constructor) => class extends constructor {
480
- _setSocket(moduleRef, socket) {
481
- const { get } = manageConnectionHandlers(this);
482
- const { get: getChannels } = manageChannels(this);
483
- const [handler] = get();
484
- const endpoint = socket.createEndpoint(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
485
- if (handler) {
486
- yield handler.value(request, response);
487
- }
488
- else {
489
- response.accept();
490
- }
491
- }));
492
- getChannels().forEach((channel) => {
493
- const chan = moduleRef.get(channel, {
494
- strict: false,
495
- });
496
- chan._setEndpoint(endpoint);
497
- });
498
- }
499
- };
500
- }
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));
501
447
  exports.Endpoint = Endpoint;
502
- function Endpoints(metadata) {
503
- const { endpoints, imports, exports = [], providers = [], isGlobal = false } = metadata;
504
- return (constructor) => {
505
- const channels = endpoints.reduce((acc, endpoint) => {
506
- const { get } = manageChannels(endpoint.prototype);
507
- const channels = get();
508
- return [...acc, ...channels];
509
- }, []);
510
- const { set } = manageEndpoints(constructor.prototype);
511
- set(endpoints);
512
- if (isGlobal) {
513
- return (0, common_1.applyDecorators)(
514
- // eslint-disable-next-line new-cap
515
- (0, common_1.Global)(),
516
- // eslint-disable-next-line new-cap
517
- (0, common_1.Module)({
518
- imports,
519
- exports: [...exports, ...channels],
520
- providers: [...providers, ...endpoints, ...channels],
521
- }))(constructor);
522
- }
523
- // eslint-disable-next-line new-cap
524
- return (0, common_1.Module)({
525
- imports,
526
- exports: [...exports, ...channels],
527
- providers: [...providers, ...endpoints, ...channels],
528
- })(constructor);
529
- };
530
- }
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
+ };
531
460
  exports.Endpoints = Endpoints;
532
461
  class PondSocketModule {
533
462
  constructor(moduleRef, adapterHost) {
@@ -535,16 +464,58 @@ class PondSocketModule {
535
464
  this.adapterHost = adapterHost;
536
465
  const expressInstance = this.adapterHost.httpAdapter;
537
466
  const server = expressInstance.getHttpServer();
538
- this.socket = new pondSocket_1.PondSocket(server);
467
+ const socket = new pondSocket_1.PondSocket(server);
539
468
  expressInstance.listen = (...args) => {
540
- const { get } = manageEndpoints(this);
541
- get().map((endpoint) => {
542
- const instance = this.moduleRef.get(endpoint, { strict: false });
543
- instance._setSocket(this.moduleRef, this.socket);
544
- return instance;
545
- });
546
- 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);
547
473
  };
548
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
+ }
549
520
  }
550
521
  exports.PondSocketModule = PondSocketModule;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.81",
3
+ "version": "0.1.82",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",