@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/nest.js DELETED
@@ -1,769 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __rest = (this && this.__rest) || function (s, e) {
12
- var t = {};
13
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
14
- t[p] = s[p];
15
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
16
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
17
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
18
- t[p[i]] = s[p[i]];
19
- }
20
- return t;
21
- };
22
- Object.defineProperty(exports, "__esModule", { value: true });
23
- exports.PondSocketModule = exports.Endpoint = exports.Channel = exports.EndpointInstance = exports.ChannelInstance = exports.OnLeaveEvent = exports.OnEvent = exports.OnJoinRequest = exports.OnConnectionRequest = exports.GetChannel = exports.GetLeaveEvent = exports.GetEventRequest = exports.GetEventResponse = exports.GetEventQuery = exports.GetEventParams = exports.GetEventPayload = exports.GetJoinParams = exports.GetJoinResponse = exports.GetJoinRequest = exports.GetConnectionQuery = exports.GetConnectionHeaders = exports.GetConnectionParams = exports.GetConnectionRequestId = exports.GetConnectionResponse = exports.GetConnectionRequest = exports.GetUserPresences = exports.GetInternalChannel = exports.GetUserData = exports.createParamDecorator = exports.PondGuards = void 0;
24
- const http_1 = require("http");
25
- const common_1 = require("@nestjs/common");
26
- // eslint-disable-next-line import/no-unresolved
27
- const core_1 = require("@nestjs/core");
28
- require("reflect-metadata");
29
- const eventRequest_1 = require("./channel/eventRequest");
30
- const eventResponse_1 = require("./channel/eventResponse");
31
- const response_1 = require("./endpoint/response");
32
- const joinRequest_1 = require("./lobby/joinRequest");
33
- const joinResponse_1 = require("./lobby/joinResponse");
34
- const pondSocket_1 = require("./server/pondSocket");
35
- const onJoinHandlerKey = Symbol('onJoinHandlerKey');
36
- const onEventHandlerKey = Symbol('onEventHandlerKey');
37
- const onConnectionHandlerKey = Symbol('onConnectionHandlerKey');
38
- const channelInstanceKey = Symbol('channelInstanceKey');
39
- const endpointInstanceKey = Symbol('endpointInstanceKey');
40
- const channelClassKey = Symbol('channel');
41
- const endpointClassKey = Symbol('endpoint');
42
- const channelsClassKey = Symbol('channels');
43
- const parametersKey = Symbol('generalParametersKey');
44
- const pondGuardsKey = Symbol('pondGuardsKey');
45
- function isNotEmpty(value) {
46
- return value !== null &&
47
- value !== undefined &&
48
- value !== '' &&
49
- Object.keys(value).length !== 0;
50
- }
51
- function manageClassData(key, target) {
52
- return {
53
- get() {
54
- var _a;
55
- return ((_a = Reflect.getMetadata(key, target)) !== null && _a !== void 0 ? _a : null);
56
- },
57
- set(value) {
58
- Reflect.defineMetadata(key, value, target);
59
- },
60
- };
61
- }
62
- function createClassDecorator(key, path) {
63
- return (target) => {
64
- const { set } = manageClassData(key, target);
65
- set(path);
66
- };
67
- }
68
- function manageMethodData(key, target, propertyKey) {
69
- function getter() {
70
- var _a;
71
- return ((_a = Reflect.getMetadata(key, target, propertyKey)) !== null && _a !== void 0 ? _a : null);
72
- }
73
- return {
74
- get: getter,
75
- set(value) {
76
- Reflect.defineMetadata(key, value, target, propertyKey);
77
- },
78
- };
79
- }
80
- function manageHandlers(key, target) {
81
- const { get, set } = manageClassData(key, target);
82
- return {
83
- get() {
84
- return get() || [];
85
- },
86
- set(path, value) {
87
- const handlers = get() || [];
88
- set([
89
- ...handlers,
90
- {
91
- path,
92
- value,
93
- },
94
- ]);
95
- },
96
- };
97
- }
98
- function manageParameters(target, propertyKey) {
99
- const { get, set } = manageMethodData(parametersKey, target, propertyKey);
100
- return {
101
- get() {
102
- return get() || [];
103
- },
104
- set(index, callback) {
105
- const handlers = get() || [];
106
- set([
107
- ...handlers,
108
- {
109
- index,
110
- callback,
111
- },
112
- ]);
113
- },
114
- };
115
- }
116
- function managePropertyData(key, target) {
117
- function build(propertyKey, callback) {
118
- Object.defineProperty(target, propertyKey, {
119
- get() {
120
- const value = Reflect.getMetadata(key, this);
121
- if (callback) {
122
- return callback(value);
123
- }
124
- return value;
125
- },
126
- set() {
127
- throw new Error(`${propertyKey} is readonly`);
128
- },
129
- enumerable: true,
130
- configurable: true,
131
- });
132
- }
133
- function set(value) {
134
- Reflect.defineMetadata(key, value, target);
135
- }
136
- return {
137
- build,
138
- set,
139
- };
140
- }
141
- function PondGuards(...guards) {
142
- return (target, propertyKey) => {
143
- var _a, _b;
144
- if (propertyKey) {
145
- const { get, set } = manageMethodData(pondGuardsKey, target, propertyKey);
146
- set([...((_a = get()) !== null && _a !== void 0 ? _a : []), ...guards]);
147
- }
148
- else {
149
- const { get, set } = manageClassData(pondGuardsKey, target);
150
- set([...((_b = get()) !== null && _b !== void 0 ? _b : []), ...guards]);
151
- }
152
- };
153
- }
154
- exports.PondGuards = PondGuards;
155
- function resolveGuards(moduleRef, context) {
156
- var _a, _b;
157
- return __awaiter(this, void 0, void 0, function* () {
158
- const retrieveGuard = (Guard) => {
159
- try {
160
- return moduleRef.get(Guard, { strict: false });
161
- }
162
- catch (e) {
163
- console.warn(`Unable to resolve guard: ${Guard.name}, creating new instance, WARNING: this will not inject dependencies. To fix this, add the guard to the providers array of the PondSocketModule.`);
164
- return new Guard();
165
- }
166
- };
167
- const classGuards = (_a = context.retrieveClassData(pondGuardsKey)) !== null && _a !== void 0 ? _a : [];
168
- const methodGuards = (_b = context.retrieveMethodData(pondGuardsKey)) !== null && _b !== void 0 ? _b : [];
169
- const instances = [...classGuards, ...methodGuards].map((guard) => retrieveGuard(guard));
170
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
171
- // @ts-expect-error
172
- const promises = instances.map((instance) => instance.canActivate(context));
173
- const results = yield Promise.all(promises);
174
- return results.every((result) => result);
175
- });
176
- }
177
- class Context {
178
- // eslint-disable-next-line no-useless-constructor
179
- constructor(request, response, instance, propertyKey) {
180
- this.request = request;
181
- this.response = response;
182
- this.instance = instance;
183
- this.propertyKey = propertyKey;
184
- this.data = {};
185
- }
186
- get joinRequest() {
187
- var _a;
188
- return (_a = this.request.joinRequest) !== null && _a !== void 0 ? _a : null;
189
- }
190
- get eventRequest() {
191
- var _a;
192
- return (_a = this.request.eventRequest) !== null && _a !== void 0 ? _a : null;
193
- }
194
- get connection() {
195
- var _a;
196
- return (_a = this.request.connection) !== null && _a !== void 0 ? _a : null;
197
- }
198
- get leveeEvent() {
199
- var _a;
200
- return (_a = this.request.leveeEvent) !== null && _a !== void 0 ? _a : null;
201
- }
202
- get joinResponse() {
203
- var _a;
204
- return (_a = this.response.joinResponse) !== null && _a !== void 0 ? _a : null;
205
- }
206
- get eventResponse() {
207
- var _a;
208
- return (_a = this.response.eventResponse) !== null && _a !== void 0 ? _a : null;
209
- }
210
- get connectionResponse() {
211
- var _a;
212
- return (_a = this.response.connection) !== null && _a !== void 0 ? _a : null;
213
- }
214
- get user() {
215
- var _a, _b, _c, _d;
216
- 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;
217
- }
218
- get channel() {
219
- var _a, _b, _c, _d;
220
- 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;
221
- }
222
- get presence() {
223
- var _a, _b, _c, _d;
224
- 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;
225
- }
226
- get event() {
227
- var _a, _b, _c, _d;
228
- if (this.connection) {
229
- const event = {
230
- params: this.connection.params,
231
- query: this.connection.query,
232
- payload: {},
233
- event: 'CONNECTION',
234
- };
235
- return event;
236
- }
237
- else if (this.joinRequest || this.eventRequest) {
238
- 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;
239
- }
240
- return null;
241
- }
242
- get assigns() {
243
- var _a, _b, _c, _d, _e, _f;
244
- return (_f = (_d = (_b = (_a = this.joinRequest) === null || _a === void 0 ? void 0 : _a.user.assigns) !== null && _b !== void 0 ? _b : (_c = this.eventRequest) === null || _c === void 0 ? void 0 : _c.user.assigns) !== null && _d !== void 0 ? _d : (_e = this.leveeEvent) === null || _e === void 0 ? void 0 : _e.assigns) !== null && _f !== void 0 ? _f : null;
245
- }
246
- retrieveClassData(key) {
247
- return manageClassData(key, this.instance.constructor).get();
248
- }
249
- retrieveMethodData(key) {
250
- return manageMethodData(key, this.instance, this.propertyKey).get();
251
- }
252
- getClass() {
253
- return this.instance.constructor;
254
- }
255
- getHandler() {
256
- return this.instance[this.propertyKey];
257
- }
258
- addData(key, value) {
259
- this.data[key] = value;
260
- }
261
- getData(key) {
262
- var _a;
263
- return (_a = this.data[key]) !== null && _a !== void 0 ? _a : null;
264
- }
265
- }
266
- function manageResponse(data, channel, response) {
267
- if (response && response.hasResponded || !isNotEmpty(data)) {
268
- return;
269
- }
270
- const { event, presence, updatePresence, assigns, broadcast } = data, rest = __rest(data, ["event", "presence", "updatePresence", "assigns", "broadcast"]);
271
- if (response) {
272
- if (event && typeof event === 'string' && isNotEmpty(rest)) {
273
- if (response instanceof eventResponse_1.EventResponse) {
274
- response.sendOnly(event, rest, assigns);
275
- }
276
- else {
277
- response.send(event, rest, assigns);
278
- }
279
- }
280
- else if (isNotEmpty(assigns)) {
281
- response.accept(assigns);
282
- }
283
- if (broadcast && typeof broadcast === 'string' && isNotEmpty(rest) && 'broadcast' in response) {
284
- response.broadcast(broadcast, rest);
285
- }
286
- if ('trackPresence' in response && presence) {
287
- response.trackPresence(presence);
288
- }
289
- else if ('updatePresence' in response && updatePresence) {
290
- response.updatePresence(updatePresence);
291
- }
292
- }
293
- else if (channel && broadcast && typeof broadcast === 'string' && isNotEmpty(rest)) {
294
- channel.broadcastMessage(broadcast, rest);
295
- }
296
- }
297
- function manageError(error, response) {
298
- if (response.hasResponded) {
299
- return response;
300
- }
301
- if (error instanceof common_1.HttpException) {
302
- return response.reject(error.message, error.getStatus());
303
- }
304
- if (error instanceof Error) {
305
- return response.reject(error.message, 500);
306
- }
307
- }
308
- function manageConnectionHandlers(target) {
309
- return manageHandlers(onConnectionHandlerKey, target);
310
- }
311
- function manageJoinHandlers(target) {
312
- return manageHandlers(onJoinHandlerKey, target);
313
- }
314
- function manageEventHandlers(target) {
315
- return manageHandlers(onEventHandlerKey, target);
316
- }
317
- function manageOnLeaveHandlers(target) {
318
- return manageHandlers(onConnectionHandlerKey, target);
319
- }
320
- function manageChannelInstance(target) {
321
- return managePropertyData(channelInstanceKey, target);
322
- }
323
- function manageEndpointInstance(target) {
324
- return managePropertyData(endpointInstanceKey, target);
325
- }
326
- function createParamDecorator(callback) {
327
- return (data) => (target, propertyKey, index) => {
328
- const { set } = manageParameters(target, propertyKey);
329
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
330
- // @ts-expect-error
331
- set(index, (context) => callback(data, context));
332
- };
333
- }
334
- exports.createParamDecorator = createParamDecorator;
335
- function resolveParameters(context) {
336
- var _a;
337
- const gottenValues = (_a = context.retrieveMethodData(parametersKey)) !== null && _a !== void 0 ? _a : [];
338
- const values = gottenValues
339
- .map(({ callback, index }) => ({
340
- value: callback(context),
341
- index,
342
- }));
343
- return values
344
- .sort((a, b) => a.index - b.index)
345
- .map(({ value }) => value);
346
- }
347
- function GetUserData() {
348
- return createParamDecorator((_, context) => {
349
- const userData = context.user;
350
- if (!userData) {
351
- throw new Error('Invalid decorator usage: GetUserData');
352
- }
353
- return userData;
354
- })(null);
355
- }
356
- exports.GetUserData = GetUserData;
357
- function GetInternalChannel() {
358
- return createParamDecorator((_, context) => {
359
- const channel = context.channel;
360
- if (!channel) {
361
- throw new Error('Invalid decorator usage: GetInternalChannel');
362
- }
363
- return channel;
364
- })(null);
365
- }
366
- exports.GetInternalChannel = GetInternalChannel;
367
- function GetUserPresences() {
368
- return createParamDecorator((_, context) => {
369
- const presences = context.presence;
370
- if (!presences) {
371
- throw new Error('Invalid decorator usage: GetUserPresences');
372
- }
373
- return presences;
374
- })(null);
375
- }
376
- exports.GetUserPresences = GetUserPresences;
377
- function GetConnectionRequest() {
378
- return createParamDecorator((_, context) => {
379
- const connection = context.connection;
380
- if (!connection) {
381
- throw new Error('Invalid decorator usage: GetConnectionRequest');
382
- }
383
- return connection;
384
- })(null);
385
- }
386
- exports.GetConnectionRequest = GetConnectionRequest;
387
- function GetConnectionResponse() {
388
- return createParamDecorator((_, context) => {
389
- const response = context.connectionResponse;
390
- if (!response) {
391
- throw new Error('Invalid decorator usage: GetConnectionResponse');
392
- }
393
- return response;
394
- })(null);
395
- }
396
- exports.GetConnectionResponse = GetConnectionResponse;
397
- function GetConnectionRequestId() {
398
- return createParamDecorator((_, context) => {
399
- const connection = context.connection;
400
- if (!connection) {
401
- throw new Error('Invalid decorator usage: GetConnectionRequestId');
402
- }
403
- return connection.id;
404
- })(null);
405
- }
406
- exports.GetConnectionRequestId = GetConnectionRequestId;
407
- function GetConnectionParams() {
408
- return createParamDecorator((_, context) => {
409
- const connection = context.connection;
410
- if (!connection) {
411
- throw new Error('Invalid decorator usage: GetConnectionParams');
412
- }
413
- return connection.params;
414
- })(null);
415
- }
416
- exports.GetConnectionParams = GetConnectionParams;
417
- function GetConnectionHeaders() {
418
- return createParamDecorator((_, context) => {
419
- const connection = context.connection;
420
- if (!connection) {
421
- throw new Error('Invalid decorator usage: GetConnectionHeaders');
422
- }
423
- return connection.headers;
424
- })(null);
425
- }
426
- exports.GetConnectionHeaders = GetConnectionHeaders;
427
- function GetConnectionQuery() {
428
- return createParamDecorator((_, context) => {
429
- const connection = context.connection;
430
- if (!connection) {
431
- throw new Error('Invalid decorator usage: GetConnectionQuery');
432
- }
433
- return connection.query;
434
- })(null);
435
- }
436
- exports.GetConnectionQuery = GetConnectionQuery;
437
- function GetJoinRequest() {
438
- return createParamDecorator((_, context) => {
439
- const joinRequest = context.joinRequest;
440
- if (!joinRequest) {
441
- throw new Error('Invalid decorator usage: GetJoinRequest');
442
- }
443
- return joinRequest;
444
- })(null);
445
- }
446
- exports.GetJoinRequest = GetJoinRequest;
447
- function GetJoinResponse() {
448
- return createParamDecorator((_, context) => {
449
- const joinResponse = context.joinResponse;
450
- if (!joinResponse) {
451
- throw new Error('Invalid decorator usage: GetJoinResponse');
452
- }
453
- return joinResponse;
454
- })(null);
455
- }
456
- exports.GetJoinResponse = GetJoinResponse;
457
- function GetJoinParams() {
458
- return createParamDecorator((_, context) => {
459
- const joinRequest = context.joinRequest;
460
- if (!joinRequest) {
461
- throw new Error('Invalid decorator usage: GetJoinParams');
462
- }
463
- return joinRequest.joinParams;
464
- })(null);
465
- }
466
- exports.GetJoinParams = GetJoinParams;
467
- function GetEventPayload() {
468
- return createParamDecorator((_, context) => {
469
- var _a;
470
- const payload = (_a = context.event) === null || _a === void 0 ? void 0 : _a.payload;
471
- if (!payload) {
472
- throw new Error('Invalid decorator usage: GetEventPayload');
473
- }
474
- return payload;
475
- })(null);
476
- }
477
- exports.GetEventPayload = GetEventPayload;
478
- function GetEventParams() {
479
- return createParamDecorator((_, context) => {
480
- var _a;
481
- const params = (_a = context.event) === null || _a === void 0 ? void 0 : _a.params;
482
- if (!params) {
483
- throw new Error('Invalid decorator usage: GetEventParams');
484
- }
485
- return params;
486
- })(null);
487
- }
488
- exports.GetEventParams = GetEventParams;
489
- function GetEventQuery() {
490
- return createParamDecorator((_, request) => {
491
- var _a;
492
- const query = (_a = request.event) === null || _a === void 0 ? void 0 : _a.query;
493
- if (!query) {
494
- throw new Error('Invalid decorator usage: GetEventQuery');
495
- }
496
- return query;
497
- })(null);
498
- }
499
- exports.GetEventQuery = GetEventQuery;
500
- function GetEventResponse() {
501
- return createParamDecorator((_, context) => {
502
- const response = context.eventResponse;
503
- if (!response) {
504
- throw new Error('Invalid decorator usage: GetEventResponse');
505
- }
506
- return response;
507
- })(null);
508
- }
509
- exports.GetEventResponse = GetEventResponse;
510
- function GetEventRequest() {
511
- return createParamDecorator((_, context) => {
512
- const request = context.eventRequest;
513
- if (!request) {
514
- throw new Error('Invalid decorator usage: GetEventRequest');
515
- }
516
- return request;
517
- })(null);
518
- }
519
- exports.GetEventRequest = GetEventRequest;
520
- function GetLeaveEvent() {
521
- return createParamDecorator((_, context) => {
522
- const event = context.leveeEvent;
523
- if (!event) {
524
- throw new Error('Invalid decorator usage: GetLeaveEvent');
525
- }
526
- return event;
527
- })(null);
528
- }
529
- exports.GetLeaveEvent = GetLeaveEvent;
530
- function GetChannel() {
531
- return createParamDecorator((_, context) => {
532
- const channel = context.channel;
533
- if (!channel) {
534
- throw new Error('Invalid decorator usage: GetChannel');
535
- }
536
- return channel;
537
- })(null);
538
- }
539
- exports.GetChannel = GetChannel;
540
- function manageAction(instance, moduleRef, originalMethod, propertyKey, leaveEvent, request, response) {
541
- return __awaiter(this, void 0, void 0, function* () {
542
- const req = {};
543
- const res = {};
544
- let channel = null;
545
- if (request && response) {
546
- if (request instanceof joinRequest_1.JoinRequest && response instanceof joinResponse_1.JoinResponse) {
547
- channel = request.channel;
548
- req.joinRequest = request;
549
- res.joinResponse = response;
550
- }
551
- else if (request instanceof eventRequest_1.EventRequest && response instanceof eventResponse_1.EventResponse) {
552
- channel = request.channel;
553
- req.eventRequest = request;
554
- res.eventResponse = response;
555
- }
556
- else if ('headers' in request && response instanceof response_1.ConnectionResponse) {
557
- req.connection = request;
558
- res.connection = response;
559
- }
560
- }
561
- else if (leaveEvent) {
562
- channel = leaveEvent.channel;
563
- req.leveeEvent = leaveEvent;
564
- }
565
- const context = new Context(req, res, instance, propertyKey);
566
- const canProceed = yield resolveGuards(moduleRef, context);
567
- if (canProceed) {
568
- const data = yield originalMethod.apply(instance, resolveParameters(context));
569
- manageResponse(data, channel, response);
570
- }
571
- else if (response) {
572
- response.reject('Unauthorized', 401);
573
- }
574
- });
575
- }
576
- function OnConnectionRequest() {
577
- return (target, propertyKey, descriptor) => {
578
- const originalMethod = descriptor.value;
579
- const { set } = manageConnectionHandlers(target);
580
- set('', (instance, moduleRef, request, response) => __awaiter(this, void 0, void 0, function* () {
581
- try {
582
- yield manageAction(instance, moduleRef, originalMethod, propertyKey, null, request, response);
583
- }
584
- catch (error) {
585
- manageError(error, response);
586
- }
587
- }));
588
- };
589
- }
590
- exports.OnConnectionRequest = OnConnectionRequest;
591
- function OnJoinRequest() {
592
- return (target, propertyKey, descriptor) => {
593
- const originalMethod = descriptor.value;
594
- const { set } = manageJoinHandlers(target);
595
- set('', (instance, moduleRef, request, response) => __awaiter(this, void 0, void 0, function* () {
596
- try {
597
- yield manageAction(instance, moduleRef, originalMethod, propertyKey, null, request, response);
598
- }
599
- catch (error) {
600
- manageError(error, response);
601
- }
602
- }));
603
- };
604
- }
605
- exports.OnJoinRequest = OnJoinRequest;
606
- function OnEvent(event = '*') {
607
- return (target, propertyKey, descriptor) => {
608
- const originalMethod = descriptor.value;
609
- const { set } = manageEventHandlers(target);
610
- set(event, (instance, moduleRef, request, response) => __awaiter(this, void 0, void 0, function* () {
611
- try {
612
- yield manageAction(instance, moduleRef, originalMethod, propertyKey, null, request, response);
613
- }
614
- catch (error) {
615
- manageError(error, response);
616
- }
617
- }));
618
- };
619
- }
620
- exports.OnEvent = OnEvent;
621
- function OnLeaveEvent() {
622
- return (target, propertyKey, descriptor) => {
623
- const originalMethod = descriptor.value;
624
- const { set } = manageOnLeaveHandlers(target);
625
- set('', (instance, moduleRef, event) => __awaiter(this, void 0, void 0, function* () {
626
- yield manageAction(instance, moduleRef, originalMethod, propertyKey, event);
627
- }));
628
- };
629
- }
630
- exports.OnLeaveEvent = OnLeaveEvent;
631
- function ChannelInstance(name) {
632
- return (target, propertyKey) => {
633
- const { build } = manageChannelInstance(target);
634
- build(propertyKey, (value) => {
635
- if (name) {
636
- return value.getChannel(name);
637
- }
638
- return value;
639
- });
640
- };
641
- }
642
- exports.ChannelInstance = ChannelInstance;
643
- function EndpointInstance() {
644
- return (target, propertyKey) => {
645
- const { build } = manageEndpointInstance(target);
646
- build(propertyKey);
647
- };
648
- }
649
- exports.EndpointInstance = EndpointInstance;
650
- const Channel = (path = '*') => createClassDecorator(channelClassKey, path);
651
- exports.Channel = Channel;
652
- const setEndpoint = (path = '*') => createClassDecorator(endpointClassKey, path);
653
- const setChannels = (channels) => createClassDecorator(channelsClassKey, channels);
654
- const getChannels = (target) => {
655
- var _a;
656
- return (_a = manageClassData(channelsClassKey, target).get()) !== null && _a !== void 0 ? _a : [];
657
- };
658
- const getGuards = (target) => {
659
- var _a;
660
- const classGuards = (_a = manageClassData(pondGuardsKey, target).get()) !== null && _a !== void 0 ? _a : [];
661
- const methodGuards = Object.getOwnPropertyNames(target.prototype)
662
- .map((propertyKey) => {
663
- var _a;
664
- return (_a = manageMethodData(pondGuardsKey, target.prototype, propertyKey).get()) !== null && _a !== void 0 ? _a : [];
665
- });
666
- return [...classGuards, ...methodGuards.flat()];
667
- };
668
- const Endpoint = (metadata) => (0, common_1.applyDecorators)(setChannels(metadata.channels), setEndpoint(metadata.path));
669
- exports.Endpoint = Endpoint;
670
- class PondSocketService {
671
- constructor(moduleRef, adapterHost, externalGuards, endpoints) {
672
- this.moduleRef = moduleRef;
673
- this.adapterHost = adapterHost;
674
- this.externalGuards = externalGuards;
675
- this.endpoints = endpoints;
676
- const httpAdapter = this.adapterHost.httpAdapter;
677
- httpAdapter.listen = (...args) => {
678
- const app = httpAdapter.getInstance();
679
- const server = (0, http_1.createServer)(app);
680
- const socket = new pondSocket_1.PondSocket(server);
681
- this.endpoints.forEach((endpoint) => this.manageEndpoint(socket, endpoint));
682
- return socket.listen(...args);
683
- };
684
- }
685
- manageEndpoint(socket, endpoint) {
686
- var _a;
687
- const endpointMetadata = manageClassData(endpointClassKey, endpoint).get();
688
- if (!endpointMetadata) {
689
- return;
690
- }
691
- const { get, set: setGuards } = manageClassData(pondGuardsKey, endpoint);
692
- setGuards([...this.externalGuards, ...((_a = get()) !== null && _a !== void 0 ? _a : [])]);
693
- const instance = this.moduleRef.get(endpoint, { strict: false });
694
- const { set } = manageEndpointInstance(instance);
695
- const pondEndpoint = socket.createEndpoint(endpointMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
696
- const { get } = manageConnectionHandlers(instance);
697
- const [handler] = get();
698
- if (handler) {
699
- yield handler.value(instance, this.moduleRef, request, response);
700
- }
701
- else {
702
- response.accept();
703
- }
704
- }));
705
- set(pondEndpoint);
706
- getChannels(endpoint).forEach((channel) => {
707
- this.manageChannel(channel, pondEndpoint);
708
- });
709
- }
710
- manageChannel(channel, endpoint) {
711
- var _a;
712
- const channelMetadata = manageClassData(channelClassKey, channel).get();
713
- if (!channelMetadata) {
714
- return;
715
- }
716
- const { get, set: setGuards } = manageClassData(pondGuardsKey, channel);
717
- setGuards([...this.externalGuards, ...((_a = get()) !== null && _a !== void 0 ? _a : [])]);
718
- const instance = this.moduleRef.get(channel, { strict: false });
719
- const channelInstance = endpoint.createChannel(channelMetadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
720
- const { get } = manageJoinHandlers(instance);
721
- const [handler] = get();
722
- if (handler) {
723
- yield handler.value(instance, this.moduleRef, request, response);
724
- }
725
- else {
726
- response.accept();
727
- }
728
- }));
729
- const { get: getEventHandlers } = manageEventHandlers(instance);
730
- const { get: getLeaveHandlers } = manageOnLeaveHandlers(instance);
731
- const { set } = manageChannelInstance(instance);
732
- getEventHandlers().forEach((handler) => {
733
- channelInstance.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
734
- yield handler.value(instance, this.moduleRef, request, response);
735
- }));
736
- });
737
- const [leaveHandler] = getLeaveHandlers();
738
- if (leaveHandler) {
739
- channelInstance.onLeave((event) => __awaiter(this, void 0, void 0, function* () {
740
- yield leaveHandler.value(instance, this.moduleRef, event);
741
- }));
742
- }
743
- set(channelInstance);
744
- }
745
- }
746
- class PondSocketModule {
747
- static forRoot({ endpoints, guards = [], providers = [], imports = [], exports = [], isGlobal = false, }) {
748
- const endpointsSet = new Set(endpoints);
749
- const channels = Array.from(endpointsSet).flatMap((endpoint) => getChannels(endpoint));
750
- const channelsSet = new Set(channels);
751
- const uniqueGuards = new Set(guards);
752
- const allGuards = Array.from(new Set([...endpointsSet, ...channelsSet])).flatMap(((target) => getGuards(target)));
753
- const guardsSet = new Set([...uniqueGuards, ...allGuards]);
754
- const pondSocketProvider = {
755
- provide: PondSocketService,
756
- useFactory: (moduleRef, adapterHost) => new PondSocketService(moduleRef, adapterHost, Array.from(uniqueGuards), endpoints),
757
- inject: [core_1.ModuleRef, core_1.HttpAdapterHost],
758
- };
759
- const providersSet = new Set([...providers, ...guardsSet, ...channelsSet, ...endpointsSet, pondSocketProvider]);
760
- return {
761
- imports,
762
- module: PondSocketModule,
763
- providers: [...providersSet],
764
- exports: [...exports, ...channelsSet],
765
- global: isGlobal,
766
- };
767
- }
768
- }
769
- exports.PondSocketModule = PondSocketModule;