@eleven-am/pondsocket 0.1.97 → 0.1.99

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 (3) hide show
  1. package/nest.js +96 -71
  2. package/package.json +1 -1
  3. package/types.d.ts +1 -1
package/nest.js CHANGED
@@ -37,72 +37,7 @@ const endpointClassKey = Symbol('endpoint');
37
37
  const channelsClassKey = Symbol('channels');
38
38
  const parametersKey = Symbol('generalParametersKey');
39
39
  const pondGuardsKey = Symbol('pondGuardsKey');
40
- class Context {
41
- constructor(request, response, instance, propertyKey) {
42
- this.request = request;
43
- this.response = response;
44
- this.instance = instance;
45
- this.propertyKey = propertyKey;
46
- this.data = {};
47
- }
48
- get joinRequest() {
49
- var _a;
50
- return (_a = this.request.joinRequest) !== null && _a !== void 0 ? _a : null;
51
- }
52
- get eventRequest() {
53
- var _a;
54
- return (_a = this.request.eventRequest) !== null && _a !== void 0 ? _a : null;
55
- }
56
- get connection() {
57
- var _a;
58
- return (_a = this.request.connection) !== null && _a !== void 0 ? _a : null;
59
- }
60
- get leveeEvent() {
61
- var _a;
62
- return (_a = this.request.leveeEvent) !== null && _a !== void 0 ? _a : null;
63
- }
64
- get joinResponse() {
65
- var _a;
66
- return (_a = this.response.joinResponse) !== null && _a !== void 0 ? _a : null;
67
- }
68
- get eventResponse() {
69
- var _a;
70
- return (_a = this.response.eventResponse) !== null && _a !== void 0 ? _a : null;
71
- }
72
- get connectionResponse() {
73
- var _a;
74
- return (_a = this.response.connection) !== null && _a !== void 0 ? _a : null;
75
- }
76
- get user() {
77
- var _a, _b, _c, _d;
78
- return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.user) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.user) !== null && _d !== void 0 ? _d : null;
79
- }
80
- get channel() {
81
- var _a, _b, _c, _d;
82
- return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.channel) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.channel) !== null && _d !== void 0 ? _d : null;
83
- }
84
- get presence() {
85
- var _a, _b, _c, _d;
86
- return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.presence) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.presence) !== null && _d !== void 0 ? _d : null;
87
- }
88
- get event() {
89
- var _a, _b, _c;
90
- return (_c = (_b = ((_a = this.joinRequest) !== null && _a !== void 0 ? _a : this.eventRequest)) === null || _b === void 0 ? void 0 : _b.event) !== null && _c !== void 0 ? _c : null;
91
- }
92
- retrieveClassData(key) {
93
- return manageClassData(key, this.instance.constructor).get();
94
- }
95
- retrieveMethodData(key) {
96
- return manageMethodData(key, this.instance, this.propertyKey).get();
97
- }
98
- addData(key, value) {
99
- this.data[key] = value;
100
- }
101
- getData(key) {
102
- var _a;
103
- return (_a = this.data[key]) !== null && _a !== void 0 ? _a : null;
104
- }
105
- }
40
+ const endpointGuardsKey = Symbol('endpointGuardsKey');
106
41
  function isNotEmpty(value) {
107
42
  return value !== null &&
108
43
  value !== undefined &&
@@ -226,12 +161,92 @@ function resolveGuards(moduleRef, context) {
226
161
  const classGuards = (_a = context.retrieveClassData(pondGuardsKey)) !== null && _a !== void 0 ? _a : [];
227
162
  const methodGuards = (_b = context.retrieveMethodData(pondGuardsKey)) !== null && _b !== void 0 ? _b : [];
228
163
  const instances = [...classGuards, ...methodGuards].map((guard) => retrieveGuard(guard));
229
- // @ts-ignore
164
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
165
+ // @ts-expect-error
230
166
  const promises = instances.map((instance) => instance.canActivate(context));
231
167
  const results = yield Promise.all(promises);
232
168
  return results.every((result) => result);
233
169
  });
234
170
  }
171
+ class Context {
172
+ // eslint-disable-next-line no-useless-constructor
173
+ constructor(request, response, instance, propertyKey) {
174
+ this.request = request;
175
+ this.response = response;
176
+ this.instance = instance;
177
+ this.propertyKey = propertyKey;
178
+ this.data = {};
179
+ }
180
+ get joinRequest() {
181
+ var _a;
182
+ return (_a = this.request.joinRequest) !== null && _a !== void 0 ? _a : null;
183
+ }
184
+ get eventRequest() {
185
+ var _a;
186
+ return (_a = this.request.eventRequest) !== null && _a !== void 0 ? _a : null;
187
+ }
188
+ get connection() {
189
+ var _a;
190
+ return (_a = this.request.connection) !== null && _a !== void 0 ? _a : null;
191
+ }
192
+ get leveeEvent() {
193
+ var _a;
194
+ return (_a = this.request.leveeEvent) !== null && _a !== void 0 ? _a : null;
195
+ }
196
+ get joinResponse() {
197
+ var _a;
198
+ return (_a = this.response.joinResponse) !== null && _a !== void 0 ? _a : null;
199
+ }
200
+ get eventResponse() {
201
+ var _a;
202
+ return (_a = this.response.eventResponse) !== null && _a !== void 0 ? _a : null;
203
+ }
204
+ get connectionResponse() {
205
+ var _a;
206
+ return (_a = this.response.connection) !== null && _a !== void 0 ? _a : null;
207
+ }
208
+ get user() {
209
+ var _a, _b, _c, _d;
210
+ return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.user) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.user) !== null && _d !== void 0 ? _d : null;
211
+ }
212
+ get channel() {
213
+ var _a, _b, _c, _d;
214
+ return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.channel) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.channel) !== null && _d !== void 0 ? _d : null;
215
+ }
216
+ get presence() {
217
+ var _a, _b, _c, _d;
218
+ return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.presence) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.presence) !== null && _d !== void 0 ? _d : null;
219
+ }
220
+ get event() {
221
+ var _a, _b, _c, _d;
222
+ if (this.connection) {
223
+ const event = {
224
+ params: this.connection.params,
225
+ query: this.connection.query,
226
+ payload: {},
227
+ event: 'CONNECTION',
228
+ };
229
+ return event;
230
+ }
231
+ else if (this.joinRequest || this.eventRequest) {
232
+ return (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.event) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.event) !== null && _d !== void 0 ? _d : null;
233
+ }
234
+ return null;
235
+ }
236
+ retrieveClassData(key) {
237
+ return manageClassData(key, this.instance.constructor).get();
238
+ }
239
+ retrieveMethodData(key) {
240
+ return manageMethodData(key, this.instance, this.propertyKey).get();
241
+ }
242
+ addData(key, value) {
243
+ this.data[key] = value;
244
+ }
245
+ getData(key) {
246
+ var _a;
247
+ return (_a = this.data[key]) !== null && _a !== void 0 ? _a : null;
248
+ }
249
+ }
235
250
  function manageConnectionHandlers(target) {
236
251
  return manageHandlers(onConnectionHandlerKey, target);
237
252
  }
@@ -253,7 +268,8 @@ function manageEndpointInstance(target) {
253
268
  function createParamDecorator(callback) {
254
269
  return (data) => (target, propertyKey, index) => {
255
270
  const { set } = manageParameters(target, propertyKey);
256
- // @ts-ignore
271
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
272
+ // @ts-expect-error
257
273
  set(index, (context) => callback(data, context));
258
274
  };
259
275
  }
@@ -635,12 +651,16 @@ exports.EndpointInstance = EndpointInstance;
635
651
  const Channel = (path = '*') => createClassDecorator(channelClassKey, path);
636
652
  exports.Channel = Channel;
637
653
  const setEndpoint = (path = '*') => createClassDecorator(endpointClassKey, path);
654
+ const setGuards = (guards) => createClassDecorator(endpointGuardsKey, guards);
638
655
  const setChannels = (channels) => createClassDecorator(channelsClassKey, channels);
639
656
  const getChannels = (target) => {
640
657
  var _a;
641
658
  return (_a = manageClassData(channelsClassKey, target).get()) !== null && _a !== void 0 ? _a : [];
642
659
  };
643
- const Endpoint = (metadata) => (0, common_1.applyDecorators)(setChannels(metadata.channels), setEndpoint(metadata.path));
660
+ const Endpoint = (metadata) => {
661
+ var _a;
662
+ return (0, common_1.applyDecorators)(setChannels(metadata.channels), setEndpoint(metadata.path), setGuards((_a = metadata.guards) !== null && _a !== void 0 ? _a : []));
663
+ };
644
664
  exports.Endpoint = Endpoint;
645
665
  class PondSocketService {
646
666
  constructor(moduleRef, adapterHost, endpoints) {
@@ -657,10 +677,12 @@ class PondSocketService {
657
677
  };
658
678
  }
659
679
  manageEndpoint(socket, endpoint) {
680
+ var _a;
660
681
  const endpointMetadata = manageClassData(endpointClassKey, endpoint).get();
661
682
  if (!endpointMetadata) {
662
683
  return;
663
684
  }
685
+ const endpointGuards = (_a = manageClassData(endpointGuardsKey, endpoint).get()) !== null && _a !== void 0 ? _a : [];
664
686
  const instance = this.moduleRef.get(endpoint, { strict: false });
665
687
  const { set } = manageEndpointInstance(instance);
666
688
  const pondEndpoint = socket.createEndpoint(endpointMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
@@ -675,14 +697,17 @@ class PondSocketService {
675
697
  }));
676
698
  set(pondEndpoint);
677
699
  getChannels(endpoint).forEach((channel) => {
678
- this.manageChannel(channel, pondEndpoint);
700
+ this.manageChannel(channel, endpointGuards, pondEndpoint);
679
701
  });
680
702
  }
681
- manageChannel(channel, endpoint) {
703
+ manageChannel(channel, guards, endpoint) {
704
+ var _a;
682
705
  const channelMetadata = manageClassData(channelClassKey, channel).get();
683
706
  if (!channelMetadata) {
684
707
  return;
685
708
  }
709
+ const { get, set: setGuards } = manageClassData(pondGuardsKey, channel);
710
+ setGuards([...guards, ...((_a = get()) !== null && _a !== void 0 ? _a : [])]);
686
711
  const instance = this.moduleRef.get(channel, { strict: false });
687
712
  const channelInstance = endpoint.createChannel(channelMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
688
713
  const { get } = manageJoinHandlers(instance);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@eleven-am/pondsocket",
3
- "version": "0.1.97",
3
+ "version": "0.1.99",
4
4
  "description": "PondSocket is a fast simple socket server",
5
5
  "keywords": [
6
6
  "socket",
package/types.d.ts CHANGED
@@ -167,7 +167,7 @@ declare class Context<Path extends string = string> {
167
167
  /**
168
168
  * @desc The assigns, available in onJoin, onEvent handlers
169
169
  */
170
- event: EventParams<string> | null;
170
+ event: PondEvent<string> | null;
171
171
 
172
172
  /**
173
173
  * @desc Retrieves metadata associated with the class