@eleven-am/pondsocket-nest 0.0.2

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 (56) hide show
  1. package/LICENSE +674 -0
  2. package/README.md +523 -0
  3. package/constants.js +13 -0
  4. package/context/context.js +96 -0
  5. package/context.d.ts +263 -0
  6. package/decorators/channel.js +11 -0
  7. package/decorators/channelInstance.js +16 -0
  8. package/decorators/endpoint.js +11 -0
  9. package/decorators/endpointInstance.js +11 -0
  10. package/decorators/getChannel.js +11 -0
  11. package/decorators/getConnectionHeaders.js +11 -0
  12. package/decorators/getConnectionRequest.js +11 -0
  13. package/decorators/getConnectionResponse.js +11 -0
  14. package/decorators/getContext.js +5 -0
  15. package/decorators/getEventParams.js +11 -0
  16. package/decorators/getEventPayload.js +11 -0
  17. package/decorators/getEventQuery.js +11 -0
  18. package/decorators/getEventRequest.js +11 -0
  19. package/decorators/getEventResponse.js +11 -0
  20. package/decorators/getJoinParams.js +11 -0
  21. package/decorators/getJoinRequest.js +11 -0
  22. package/decorators/getJoinResponse.js +11 -0
  23. package/decorators/getLeaveEvent.js +11 -0
  24. package/decorators/getUserData.js +11 -0
  25. package/decorators/getUserPresences.js +11 -0
  26. package/decorators/index.js +41 -0
  27. package/decorators/onConnectionRequest.js +30 -0
  28. package/decorators/onEvent.js +30 -0
  29. package/decorators/onJoinRequest.js +30 -0
  30. package/decorators/onLeave.js +24 -0
  31. package/decorators/pondGuards.js +11 -0
  32. package/helpers/createParamDecorator.js +11 -0
  33. package/index.js +18 -0
  34. package/managers/channel.js +9 -0
  35. package/managers/channelInstance.js +9 -0
  36. package/managers/class.js +16 -0
  37. package/managers/connection.js +9 -0
  38. package/managers/endpoint.js +9 -0
  39. package/managers/endpointInstance.js +9 -0
  40. package/managers/event.js +9 -0
  41. package/managers/guards.js +29 -0
  42. package/managers/handlers.js +23 -0
  43. package/managers/join.js +9 -0
  44. package/managers/leave.js +9 -0
  45. package/managers/method.js +16 -0
  46. package/managers/parametres.js +25 -0
  47. package/managers/property.js +29 -0
  48. package/modules/pondSocket.js +36 -0
  49. package/package.json +64 -0
  50. package/performers/action.js +62 -0
  51. package/performers/errors.js +17 -0
  52. package/performers/guards.js +35 -0
  53. package/performers/response.js +52 -0
  54. package/services/discovery.js +127 -0
  55. package/services/pondSocket.js +138 -0
  56. package/types.js +2 -0
@@ -0,0 +1,127 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.DiscoveryService = void 0;
13
+ // eslint-disable-next-line import/no-unresolved
14
+ const common_1 = require("@nestjs/common");
15
+ // eslint-disable-next-line import/no-unresolved
16
+ const constants_1 = require("@nestjs/core/injector/constants");
17
+ const constants_2 = require("../constants");
18
+ class DiscoveryService {
19
+ constructor(modulesContainer, appModuleName) {
20
+ this.modulesContainer = modulesContainer;
21
+ this.appModuleName = appModuleName;
22
+ this.providers = [];
23
+ }
24
+ groupToModules() {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ const endpoints = yield this.findEndpoints();
27
+ const channels = yield this.findChannels();
28
+ const modules = new Map();
29
+ endpoints.forEach((endpoint) => {
30
+ const module = endpoint.parentModule.injectType;
31
+ if (!modules.has(module)) {
32
+ modules.set(module, {
33
+ endpoints: [],
34
+ channels: [],
35
+ });
36
+ }
37
+ modules.get(module).endpoints.push(endpoint);
38
+ });
39
+ channels.forEach((channel) => {
40
+ const module = channel.parentModule.injectType;
41
+ if (!modules.has(module)) {
42
+ modules.set(module, {
43
+ endpoints: [],
44
+ channels: [],
45
+ });
46
+ }
47
+ modules.get(module).channels.push(channel);
48
+ });
49
+ return modules;
50
+ });
51
+ }
52
+ getAppModule() {
53
+ return __awaiter(this, void 0, void 0, function* () {
54
+ const providers = yield this.getProviders();
55
+ const appModule = providers
56
+ .find((provider) => provider.name === this.appModuleName);
57
+ if (!appModule) {
58
+ throw new Error('AppModule not found');
59
+ }
60
+ return appModule;
61
+ });
62
+ }
63
+ getProviders() {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ if (this.providers.length) {
66
+ return this.providers;
67
+ }
68
+ const promises = [...this.modulesContainer.values()]
69
+ .flatMap((nestModule) => {
70
+ const providers = [...nestModule.providers.values()];
71
+ return providers
72
+ .filter((wrapper) => wrapper.isDependencyTreeStatic())
73
+ .filter((component) => component.scope !== common_1.Scope.REQUEST)
74
+ .map((wrapper) => this.toDiscoveredClass(nestModule, wrapper));
75
+ });
76
+ const resolvedProviders = yield Promise.all(promises);
77
+ this.providers.push(...resolvedProviders.filter((provider) => provider !== undefined));
78
+ return this.providers;
79
+ });
80
+ }
81
+ toDiscoveredClass(nestModule, wrapper) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ const instanceHost = wrapper.getInstanceByContextId(constants_1.STATIC_CONTEXT, wrapper && wrapper.id ? wrapper.id : undefined);
84
+ if (instanceHost.isPending && !instanceHost.isResolved) {
85
+ yield instanceHost.donePromise;
86
+ }
87
+ if (!instanceHost.instance) {
88
+ return;
89
+ }
90
+ return {
91
+ name: wrapper.name,
92
+ instance: instanceHost.instance,
93
+ injectType: wrapper.metatype,
94
+ dependencyType: instanceHost.instance.constructor,
95
+ parentModule: {
96
+ name: nestModule.metatype.name,
97
+ instance: nestModule.instance,
98
+ injectType: nestModule.metatype,
99
+ dependencyType: nestModule.instance.constructor,
100
+ },
101
+ };
102
+ });
103
+ }
104
+ findEndpoints() {
105
+ return __awaiter(this, void 0, void 0, function* () {
106
+ const providers = yield this.getProviders();
107
+ return providers
108
+ .filter((provider) => this.withMetadata(constants_2.endpointKey, provider));
109
+ });
110
+ }
111
+ findChannels() {
112
+ return __awaiter(this, void 0, void 0, function* () {
113
+ const providers = yield this.getProviders();
114
+ return providers
115
+ .filter((provider) => this.withMetadata(constants_2.channelKey, provider));
116
+ });
117
+ }
118
+ withMetadata(metaKey, provider) {
119
+ return [
120
+ provider.instance.constructor,
121
+ provider.dependencyType,
122
+ provider.injectType,
123
+ ].filter((target) => target !== null && target !== undefined)
124
+ .some((target) => Reflect.getMetadata(metaKey, target));
125
+ }
126
+ }
127
+ exports.DiscoveryService = DiscoveryService;
@@ -0,0 +1,138 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.PondSocketService = void 0;
16
+ const http_1 = require("http");
17
+ const pondsocket_1 = __importDefault(require("@eleven-am/pondsocket"));
18
+ const channel_1 = require("../managers/channel");
19
+ const channelInstance_1 = require("../managers/channelInstance");
20
+ const connection_1 = require("../managers/connection");
21
+ const endpoint_1 = require("../managers/endpoint");
22
+ const endpointInstance_1 = require("../managers/endpointInstance");
23
+ const event_1 = require("../managers/event");
24
+ const guards_1 = require("../managers/guards");
25
+ const join_1 = require("../managers/join");
26
+ const leave_1 = require("../managers/leave");
27
+ class PondSocketService {
28
+ constructor(moduleRef, discovery, adapterHost, externalGuards) {
29
+ this.moduleRef = moduleRef;
30
+ this.discovery = discovery;
31
+ this.adapterHost = adapterHost;
32
+ this.externalGuards = externalGuards;
33
+ const httpAdapter = this.adapterHost.httpAdapter;
34
+ httpAdapter.listen = (...args) => __awaiter(this, void 0, void 0, function* () {
35
+ const socket = yield this.init(httpAdapter);
36
+ return socket.listen(...args);
37
+ });
38
+ }
39
+ init(httpAdapter) {
40
+ return __awaiter(this, void 0, void 0, function* () {
41
+ const groupedInstances = yield this.buildInstances();
42
+ const app = httpAdapter.getInstance();
43
+ const server = (0, http_1.createServer)(app);
44
+ const socket = new pondsocket_1.default(server);
45
+ groupedInstances.forEach((groupedInstance) => {
46
+ this.manageEndpoint(socket, groupedInstance);
47
+ });
48
+ return socket;
49
+ });
50
+ }
51
+ buildInstances() {
52
+ return __awaiter(this, void 0, void 0, function* () {
53
+ const modules = yield this.discovery.groupToModules();
54
+ const values = [...modules.values()];
55
+ const channelsWithNoEndpoint = values.filter((value) => value.endpoints.length === 0 && value.channels.length > 0);
56
+ const channelsWithEndpoint = values.filter((value) => value.endpoints.length > 0 && value.channels.length > 0);
57
+ const groupedInstances = channelsWithEndpoint.map((group) => group.endpoints.map((endpoint) => ({
58
+ endpoint,
59
+ channels: group.channels,
60
+ }))).flat();
61
+ if (channelsWithNoEndpoint.length > 0) {
62
+ const appModule = yield this.discovery.getAppModule();
63
+ const value = modules.get(appModule.dependencyType);
64
+ if (value && value.endpoints.length > 0) {
65
+ value.endpoints.forEach((endpoint) => {
66
+ groupedInstances.push({
67
+ endpoint,
68
+ channels: channelsWithNoEndpoint.map((group) => group.channels).flat(),
69
+ });
70
+ });
71
+ }
72
+ }
73
+ return groupedInstances;
74
+ });
75
+ }
76
+ manageEndpoint(socket, groupedInstance) {
77
+ const instance = groupedInstance.endpoint.instance;
78
+ const constructor = instance.constructor;
79
+ const metadata = (0, endpoint_1.manageEndpoint)(constructor).get();
80
+ const { set: setGuards } = (0, guards_1.manageGuards)(constructor);
81
+ const { set: setEndpoint } = (0, endpointInstance_1.manageEndpointInstance)(instance);
82
+ if (!metadata) {
83
+ return;
84
+ }
85
+ setGuards(this.externalGuards);
86
+ const endpoint = socket.createEndpoint(metadata, (request, response) => __awaiter(this, void 0, void 0, function* () {
87
+ const { get } = (0, connection_1.manageConnection)(instance);
88
+ const [handler] = get();
89
+ if (handler) {
90
+ yield handler.value(instance, this.moduleRef, request, response);
91
+ }
92
+ else {
93
+ response.accept();
94
+ }
95
+ }));
96
+ setEndpoint(endpoint);
97
+ const channels = [...new Set([...groupedInstance.channels.map((channel) => channel)])];
98
+ channels.forEach((channel) => {
99
+ this.manageChannel(channel, endpoint);
100
+ });
101
+ }
102
+ manageChannel(channel, endpoint) {
103
+ const instance = channel.instance;
104
+ const constructor = instance.constructor;
105
+ const path = (0, channel_1.manageChannel)(constructor).get();
106
+ if (!path) {
107
+ return;
108
+ }
109
+ const { set: setGuards } = (0, guards_1.manageGuards)(constructor);
110
+ const { set: setChannel } = (0, channelInstance_1.manageChannelInstance)(instance);
111
+ setGuards(this.externalGuards);
112
+ const channelInstance = endpoint.createChannel(path, (request, response) => __awaiter(this, void 0, void 0, function* () {
113
+ const { get } = (0, join_1.manageJoin)(instance);
114
+ const [handler] = get();
115
+ if (handler) {
116
+ yield handler.value(instance, this.moduleRef, request, response);
117
+ }
118
+ else {
119
+ response.accept();
120
+ }
121
+ }));
122
+ setChannel(channelInstance);
123
+ const { get: getEventHandlers } = (0, event_1.manageEvent)(instance);
124
+ const { get: getLeaveHandlers } = (0, leave_1.manageLeave)(instance);
125
+ getEventHandlers().forEach((handler) => {
126
+ channelInstance.onEvent(handler.path, (request, response) => __awaiter(this, void 0, void 0, function* () {
127
+ yield handler.value(instance, this.moduleRef, request, response);
128
+ }));
129
+ });
130
+ const [leaveHandler] = getLeaveHandlers();
131
+ if (leaveHandler) {
132
+ channelInstance.onLeave((event) => __awaiter(this, void 0, void 0, function* () {
133
+ yield leaveHandler.value(instance, this.moduleRef, event);
134
+ }));
135
+ }
136
+ }
137
+ }
138
+ exports.PondSocketService = PondSocketService;
package/types.js ADDED
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });