@futdevpro/nts-dynamo 1.6.58 → 1.6.71

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 (99) hide show
  1. package/lib/_constants/mocks/app-extended-server.mock.js +8 -13
  2. package/lib/_constants/mocks/app-extended-server.mock.js.map +1 -1
  3. package/lib/_constants/mocks/app-server.mock.js +4 -9
  4. package/lib/_constants/mocks/app-server.mock.js.map +1 -1
  5. package/lib/_constants/mocks/auth-service.mock.js +3 -10
  6. package/lib/_constants/mocks/auth-service.mock.js.map +1 -1
  7. package/lib/_constants/mocks/data-model.mock.js +17 -0
  8. package/lib/_constants/mocks/data-model.mock.js.map +1 -1
  9. package/lib/_constants/mocks/email-service-collection.mock.js +1 -4
  10. package/lib/_constants/mocks/email-service-collection.mock.js.map +1 -1
  11. package/lib/_constants/mocks/endpoint.mock.js +12 -13
  12. package/lib/_constants/mocks/endpoint.mock.js.map +1 -1
  13. package/lib/_constants/mocks/socket-client.mock.js +2 -3
  14. package/lib/_constants/mocks/socket-client.mock.js.map +1 -1
  15. package/lib/_constants/mocks/socket-server.mock.js +2 -5
  16. package/lib/_constants/mocks/socket-server.mock.js.map +1 -1
  17. package/lib/_enums/http/socket-event-type.enum.d.ts +1 -0
  18. package/lib/_enums/http/socket-event-type.enum.d.ts.map +1 -1
  19. package/lib/_enums/http/socket-event-type.enum.js +1 -0
  20. package/lib/_enums/http/socket-event-type.enum.js.map +1 -1
  21. package/lib/_models/control-models/api-call-params.control-model.js +32 -0
  22. package/lib/_models/control-models/api-call-params.control-model.js.map +1 -1
  23. package/lib/_models/control-models/app-params.control-model.js +31 -4
  24. package/lib/_models/control-models/app-params.control-model.js.map +1 -1
  25. package/lib/_models/control-models/app-system-controls.control-model copy.js +3 -5
  26. package/lib/_models/control-models/app-system-controls.control-model copy.js.map +1 -1
  27. package/lib/_models/control-models/app-system-controls.control-model.js +4 -6
  28. package/lib/_models/control-models/app-system-controls.control-model.js.map +1 -1
  29. package/lib/_models/control-models/endpoint-params.control-model.js +91 -88
  30. package/lib/_models/control-models/endpoint-params.control-model.js.map +1 -1
  31. package/lib/_models/control-models/socket-client-service-params.control-model.js +8 -5
  32. package/lib/_models/control-models/socket-client-service-params.control-model.js.map +1 -1
  33. package/lib/_models/control-models/socket-event.control-model.js +35 -34
  34. package/lib/_models/control-models/socket-event.control-model.js.map +1 -1
  35. package/lib/_models/control-models/socket-presence.control-model.d.ts.map +1 -1
  36. package/lib/_models/control-models/socket-presence.control-model.js +14 -11
  37. package/lib/_models/control-models/socket-presence.control-model.js.map +1 -1
  38. package/lib/_models/control-models/socket-server-service-params.control-model.js +3 -0
  39. package/lib/_models/control-models/socket-server-service-params.control-model.js.map +1 -1
  40. package/lib/_models/control-models/system-control.control-model.js +3 -5
  41. package/lib/_models/control-models/system-control.control-model.js.map +1 -1
  42. package/lib/_modules/custom-data/custom-data.controller.js +6 -7
  43. package/lib/_modules/custom-data/custom-data.controller.js.map +1 -1
  44. package/lib/_modules/test/test.controller.js +12 -13
  45. package/lib/_modules/test/test.controller.js.map +1 -1
  46. package/lib/_modules/usage/usage.controller.js +15 -14
  47. package/lib/_modules/usage/usage.controller.js.map +1 -1
  48. package/lib/_modules/usage/usage.data-service.js +88 -96
  49. package/lib/_modules/usage/usage.data-service.js.map +1 -1
  50. package/lib/_services/base/data.service.d.ts +2 -0
  51. package/lib/_services/base/data.service.d.ts.map +1 -1
  52. package/lib/_services/base/data.service.js +376 -253
  53. package/lib/_services/base/data.service.js.map +1 -1
  54. package/lib/_services/base/db.service.d.ts +1 -0
  55. package/lib/_services/base/db.service.d.ts.map +1 -1
  56. package/lib/_services/base/db.service.js +321 -247
  57. package/lib/_services/base/db.service.js.map +1 -1
  58. package/lib/_services/base/singleton.service.js +2 -0
  59. package/lib/_services/base/singleton.service.js.map +1 -1
  60. package/lib/_services/core/api.service.js +185 -188
  61. package/lib/_services/core/api.service.js.map +1 -1
  62. package/lib/_services/core/auth.service.js +3 -3
  63. package/lib/_services/core/auth.service.js.map +1 -1
  64. package/lib/_services/core/email.service.js +103 -88
  65. package/lib/_services/core/email.service.js.map +1 -1
  66. package/lib/_services/core/global.service.d.ts +1 -1
  67. package/lib/_services/core/global.service.d.ts.map +1 -1
  68. package/lib/_services/core/global.service.js +41 -41
  69. package/lib/_services/core/global.service.js.map +1 -1
  70. package/lib/_services/route/controller.service.js +28 -0
  71. package/lib/_services/route/controller.service.js.map +1 -1
  72. package/lib/_services/route/routing-module.service.js +11 -8
  73. package/lib/_services/route/routing-module.service.js.map +1 -1
  74. package/lib/_services/server/app-extended.server.js +241 -247
  75. package/lib/_services/server/app-extended.server.js.map +1 -1
  76. package/lib/_services/server/app-extended.server.spec.js +18 -19
  77. package/lib/_services/server/app-extended.server.spec.js.map +1 -1
  78. package/lib/_services/server/app.server.d.ts +2 -1
  79. package/lib/_services/server/app.server.d.ts.map +1 -1
  80. package/lib/_services/server/app.server.js +413 -410
  81. package/lib/_services/server/app.server.js.map +1 -1
  82. package/lib/_services/server/app.server.spec.js +12 -13
  83. package/lib/_services/server/app.server.spec.js.map +1 -1
  84. package/lib/_services/shared.static-service.js +15 -17
  85. package/lib/_services/shared.static-service.js.map +1 -1
  86. package/lib/_services/socket/socket-client.service.js +134 -145
  87. package/lib/_services/socket/socket-client.service.js.map +1 -1
  88. package/lib/_services/socket/socket-server.service.js +294 -263
  89. package/lib/_services/socket/socket-server.service.js.map +1 -1
  90. package/lib/tsconfig.tsbuildinfo +1 -1
  91. package/package.json +19 -21
  92. package/src/_enums/http/socket-event-type.enum.ts +2 -0
  93. package/src/_models/control-models/socket-presence.control-model.ts +8 -2
  94. package/src/_services/base/data.service.ts +79 -0
  95. package/src/_services/base/db.service.ts +47 -28
  96. package/src/_services/core/global.service.ts +4 -2
  97. package/src/_services/server/app-extended.server.ts +2 -2
  98. package/src/_services/server/app.server.ts +18 -4
  99. package/tsconfig.json +2 -2
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.DynamoNTS_SocketServerService = void 0;
4
- const tslib_1 = require("tslib");
5
4
  const fsm_dynamo_1 = require("@futdevpro/fsm-dynamo");
6
5
  const singleton_service_1 = require("../base/singleton.service");
7
6
  const global_settings_const_1 = require("../../_constants/global-settings.const");
@@ -22,262 +21,268 @@ const socket_event_control_model_1 = require("../../_models/control-models/socke
22
21
  * ```
23
22
  */
24
23
  class DynamoNTS_SocketServerService extends singleton_service_1.DynamoNTS_SingletonService {
24
+ params;
25
25
  get name() { return this.params.name; }
26
26
  get port() { return this.params.port; }
27
27
  get security() { return this.params.security; }
28
+ openSocketServer;
29
+ secureSocketServer;
30
+ connectEvent;
31
+ incomingEvents;
32
+ subscriptionEvent;
33
+ presences = [];
34
+ logSetup;
35
+ logFn;
36
+ defaultErrorUserMsg = `We encountered an unhandled Socket Server Error, ` +
37
+ `\nplease contact the responsible development team.`;
28
38
  constructor() {
29
39
  super();
30
- this.presences = [];
31
- this.defaultErrorUserMsg = `We encountered an unhandled Socket Server Error, ` +
32
- `\nplease contact the responsible development team.`;
33
40
  this.asyncConstructor().catch((error) => {
34
- var _a, _b;
35
- fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service setup failed: ${(_a = this.params) === null || _a === void 0 ? void 0 : _a.name} (${(_b = this.params) === null || _b === void 0 ? void 0 : _b.port})`, error);
41
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service setup failed: ${this.params?.name} (${this.params?.port})`, error);
36
42
  });
37
43
  }
38
- asyncConstructor() {
39
- var _a, _b, _c;
40
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
41
- try {
42
- this.logSetup = global_settings_const_1.dynamoNTS_globalSettings.logSetup;
43
- this.params = this.getServiceParams();
44
- this.incomingEvents = (_a = this.getIncomingEvents()) !== null && _a !== void 0 ? _a : [];
45
- yield this.prepareEvents();
46
- if (this.logSetup)
47
- console.log(`Socket Server Service setup done: ${this.params.name} (${this.params.port}) serurity: ${this.params.security}\n`);
48
- }
49
- catch (error) {
50
- fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service setup failed: ${(_b = this.params) === null || _b === void 0 ? void 0 : _b.name} (${(_c = this.params) === null || _c === void 0 ? void 0 : _c.port})`, error);
51
- throw new fsm_dynamo_1.Dynamo_Error({
52
- errorCode: 'NTS-SSS-001',
53
- error: error,
54
- });
55
- }
56
- });
44
+ async asyncConstructor() {
45
+ try {
46
+ this.logSetup = global_settings_const_1.dynamoNTS_globalSettings.logSetup;
47
+ this.params = this.getServiceParams();
48
+ this.incomingEvents = this.getIncomingEvents() ?? [];
49
+ await this.prepareEvents();
50
+ if (this.logSetup)
51
+ console.log(`Socket Server Service setup done: ${this.params.name} (${this.params.port}) serurity: ${this.params.security}\n`);
52
+ }
53
+ catch (error) {
54
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service setup failed: ${this.params?.name} (${this.params?.port})`, error);
55
+ throw new fsm_dynamo_1.Dynamo_Error({
56
+ errorCode: 'NTS-SSS-001',
57
+ error: error,
58
+ });
59
+ }
57
60
  }
58
61
  /**
59
62
  * You must setup events and required services for this function
60
63
  */
61
- prepareEvents() {
62
- var _a, _b;
63
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
64
- try {
65
- /** Setup connect event */
66
- const connectEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection);
67
- if (0 <= connectEventIndex) {
68
- this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
64
+ async prepareEvents() {
65
+ try {
66
+ /** Setup connect event */
67
+ const connectEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection);
68
+ if (0 <= connectEventIndex) {
69
+ this.connectEvent = this.incomingEvents.splice(connectEventIndex)[0];
70
+ }
71
+ else {
72
+ this.connectEvent = new socket_event_control_model_1.DynamoNTS_SocketEvent({
73
+ eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection,
74
+ });
75
+ }
76
+ this.connectEvent.serviceName = this.params.name;
77
+ /** Setup subscription event */
78
+ if (this.getPresenceFromSubscrioptionEventContent) {
79
+ const subscriptionEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe);
80
+ if (0 <= subscriptionEventIndex) {
81
+ this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
82
+ fsm_dynamo_1.Dynamo_Log.error(`You should not set the subscription event, but the subscriptions tasks, ` +
83
+ `in case you need additional steps for your subscriptions.`, `${this.params.name} (${this.params.port})`);
69
84
  }
70
85
  else {
71
- this.connectEvent = new socket_event_control_model_1.DynamoNTS_SocketEvent({
72
- eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection,
86
+ this.subscriptionEvent = new socket_event_control_model_1.DynamoNTS_SocketEvent({
87
+ eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe
73
88
  });
74
89
  }
75
- this.connectEvent.serviceName = this.params.name;
76
- /** Setup subscription event */
77
- if (this.getPresenceFromSubscrioptionEventContent) {
78
- const subscriptionEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe);
79
- if (0 <= subscriptionEventIndex) {
80
- this.subscriptionEvent = this.incomingEvents.splice(subscriptionEventIndex)[0];
81
- fsm_dynamo_1.Dynamo_Log.error(`You should not set the subscription event, but the subscriptions tasks, ` +
82
- `in case you need additional steps for your subscriptions.`, `${this.params.name} (${this.params.port})`);
83
- }
84
- else {
85
- this.subscriptionEvent = new socket_event_control_model_1.DynamoNTS_SocketEvent({
86
- eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe
87
- });
88
- }
89
- if (this.getSubscriptionPreProcessess) {
90
- this.subscriptionEvent.preProcessess.push(...this.getSubscriptionPreProcessess());
91
- }
92
- if (this.getSubscriptionTasks) {
93
- this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
94
- }
95
- this.subscriptionEvent.serviceName = this.params.name;
96
- /** Setup disconnect event */
97
- const disconnectEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.disconnect);
98
- if (0 <= disconnectEventIndex) {
99
- this.incomingEvents[disconnectEventIndex].serviceName = this.params.name;
100
- this.incomingEvents[disconnectEventIndex].tasks.push(this.disconnectBaseTask);
101
- }
102
- else {
103
- this.incomingEvents.push(new socket_event_control_model_1.DynamoNTS_SocketEvent({
104
- serviceName: this.params.name,
105
- eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.disconnect,
106
- tasks: [
107
- this.disconnectBaseTask
108
- ]
109
- }));
110
- }
111
- /** Setup error event */
112
- const errorEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.error);
113
- if (errorEventIndex == -1 && this.errorHandling) {
114
- this.incomingEvents.push(new socket_event_control_model_1.DynamoNTS_SocketEvent({
115
- serviceName: this.params.name,
116
- eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.error,
117
- tasks: [
118
- this.errorHandling
119
- ]
120
- }));
121
- }
122
- this.subscriptionEvent.socketName = this.params.name;
90
+ if (this.getSubscriptionPreProcessess) {
91
+ this.subscriptionEvent.preProcessess.push(...this.getSubscriptionPreProcessess());
92
+ }
93
+ if (this.getSubscriptionTasks) {
94
+ this.subscriptionEvent.tasks.push(...this.getSubscriptionTasks());
95
+ }
96
+ this.subscriptionEvent.serviceName = this.params.name;
97
+ /** Setup disconnect event */
98
+ const disconnectEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.disconnect);
99
+ if (0 <= disconnectEventIndex) {
100
+ this.incomingEvents[disconnectEventIndex].serviceName = this.params.name;
101
+ this.incomingEvents[disconnectEventIndex].tasks.push(this.disconnectBaseTask);
123
102
  }
124
103
  else {
125
- fsm_dynamo_1.Dynamo_Log.error('getPresenceFromSubscrioptionEventContent is not set', `${this.params.name} (${this.params.port})`);
104
+ this.incomingEvents.push(new socket_event_control_model_1.DynamoNTS_SocketEvent({
105
+ serviceName: this.params.name,
106
+ eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.disconnect,
107
+ tasks: [
108
+ this.disconnectBaseTask
109
+ ]
110
+ }));
126
111
  }
127
- /** Set incoming events serviceName */
128
- this.incomingEvents.forEach((event) => {
129
- event.serviceName = this.params.name;
130
- });
112
+ /** Setup error event */
113
+ const errorEventIndex = this.incomingEvents.findIndex((event) => event.eventKey === socket_event_type_enum_1.DynamoNTS_SocketEventKey.error);
114
+ if (errorEventIndex == -1 && this.errorHandling) {
115
+ this.incomingEvents.push(new socket_event_control_model_1.DynamoNTS_SocketEvent({
116
+ serviceName: this.params.name,
117
+ eventKey: socket_event_type_enum_1.DynamoNTS_SocketEventKey.error,
118
+ tasks: [
119
+ this.errorHandling
120
+ ]
121
+ }));
122
+ }
123
+ this.subscriptionEvent.socketName = this.params.name;
131
124
  }
132
- catch (error) {
133
- fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service - Event Preparation setup failed: ${(_a = this.params) === null || _a === void 0 ? void 0 : _a.name} (${(_b = this.params) === null || _b === void 0 ? void 0 : _b.port})`, error);
134
- throw error;
125
+ else {
126
+ fsm_dynamo_1.Dynamo_Log.error('getPresenceFromSubscrioptionEventContent is not set', `${this.params.name} (${this.params.port})`);
135
127
  }
136
- });
128
+ /** Set incoming events serviceName */
129
+ this.incomingEvents.forEach((event) => {
130
+ event.serviceName = this.params.name;
131
+ });
132
+ }
133
+ catch (error) {
134
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service - Event Preparation setup failed: ${this.params?.name} (${this.params?.port})`, error);
135
+ throw error;
136
+ }
137
137
  }
138
- setupSocketServer(newSocketServer, security, successCallback) {
139
- var _a, _b;
140
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
141
- try {
142
- if (this.logFn)
143
- console.log('\nfn:. setupSocketServer');
144
- newSocketServer.on(socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection, (socket) => tslib_1.__awaiter(this, void 0, void 0, function* () {
145
- try {
146
- let issuer;
147
- yield this.connectEvent.executeEventTasks(socket);
148
- if (this.getPresenceFromSubscrioptionEventContent) {
149
- socket.on(socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe, (content) => tslib_1.__awaiter(this, void 0, void 0, function* () {
150
- try {
151
- /**
152
- * usually socket logs are in event.executeEventTasks(),
153
- * but subscribe event is an exception from this,
154
- * to be able to check content before getPresenceFromSubscrioptionEventContent
155
- */
156
- if (global_settings_const_1.dynamoNTS_globalSettings.logSocketEventContent) {
157
- fsm_dynamo_1.Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe};` +
158
- `\ncontent:`, content);
159
- }
160
- else {
161
- fsm_dynamo_1.Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe}`);
162
- }
163
- const presence = yield this.getPresenceFromSubscrioptionEventContent(content, socket);
164
- presence.serviceName = this.params.name;
165
- issuer = presence.issuerId;
166
- this.addSocketToPresence(presence);
167
- yield this.subscriptionEvent.executeEventTasks(content, issuer);
168
- socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscriptionSuccessful, 'subscribe was successful', error => {
169
- fsm_dynamo_1.Dynamo_Log.error(`Emitting subscriptionSuccessful event failed!\nerror:`, error);
170
- });
171
- fsm_dynamo_1.Dynamo_Log.success(`<===> socket(${this.params.name}) subscription successfull (${issuer})`);
172
- }
173
- catch (error) {
174
- fsm_dynamo_1.Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
175
- socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
176
- socket.disconnect();
177
- }
178
- }));
179
- }
180
- this.incomingEvents.forEach((event) => {
181
- socket.on(event.eventKey, (content) => tslib_1.__awaiter(this, void 0, void 0, function* () {
182
- try {
183
- yield event.executeEventTasks(content, issuer);
138
+ async setupSocketServer(newSocketServer, security, successCallback) {
139
+ try {
140
+ if (this.logFn)
141
+ console.log('\nfn:. setupSocketServer');
142
+ newSocketServer.on(socket_event_type_enum_1.DynamoNTS_SocketEventKey.connection, async (socket) => {
143
+ try {
144
+ let issuer;
145
+ await this.connectEvent.executeEventTasks(socket);
146
+ if (this.getPresenceFromSubscrioptionEventContent) {
147
+ socket.on(socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe, async (content) => {
148
+ try {
149
+ /**
150
+ * usually socket logs are in event.executeEventTasks(),
151
+ * but subscribe event is an exception from this,
152
+ * to be able to check content before getPresenceFromSubscrioptionEventContent
153
+ */
154
+ if (global_settings_const_1.dynamoNTS_globalSettings.logSocketEventContent) {
155
+ fsm_dynamo_1.Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe};` +
156
+ `\ncontent:`, content);
184
157
  }
185
- catch (error) {
186
- fsm_dynamo_1.Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
187
- socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
158
+ else {
159
+ fsm_dynamo_1.Dynamo_Log.log(`---> incoming socket(${this.params.name}) event: ${socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscribe}`);
188
160
  }
189
- }));
161
+ const presence = await this.getPresenceFromSubscrioptionEventContent(content, socket);
162
+ presence.serviceName = this.params.name;
163
+ issuer = presence.issuerId;
164
+ this.addSocketToPresence(presence);
165
+ await this.subscriptionEvent.executeEventTasks(content, issuer);
166
+ socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.subscriptionSuccessful, 'subscribe was successful', error => {
167
+ fsm_dynamo_1.Dynamo_Log.error(`Emitting subscriptionSuccessful event failed!\nerror:`, error);
168
+ });
169
+ fsm_dynamo_1.Dynamo_Log.success(`<===> socket(${this.params.name}) subscription successfull (${issuer})`);
170
+ }
171
+ catch (error) {
172
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Subscription failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
173
+ socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
174
+ socket.disconnect();
175
+ }
190
176
  });
191
- if (global_settings_const_1.dynamoNTS_globalSettings.logMainSocketEvents) {
192
- fsm_dynamo_1.Dynamo_Log.success(`< > socket(${this.params.name}): new CONNECTION established`);
193
- }
194
177
  }
195
- catch (error) {
196
- fsm_dynamo_1.Dynamo_Log.error(`Socket Connection failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
197
- socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
198
- socket.disconnect();
178
+ this.incomingEvents.forEach((event) => {
179
+ socket.on(event.eventKey, async (content) => {
180
+ try {
181
+ await event.executeEventTasks(content, issuer);
182
+ }
183
+ catch (error) {
184
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Event failed: ${this.params.name} (${this.params.port})`, error);
185
+ socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
186
+ }
187
+ });
188
+ });
189
+ if (global_settings_const_1.dynamoNTS_globalSettings.logMainSocketEvents) {
190
+ fsm_dynamo_1.Dynamo_Log.success(`< > socket(${this.params.name}): new CONNECTION established`);
199
191
  }
200
- }));
201
- newSocketServer.listen(this.params.port);
202
- fsm_dynamo_1.Dynamo_Log.success(`\nsocket server setup finished: ${this.params.name}` +
203
- `\nsocket server listening on port: ${this.params.port}`);
204
- successCallback();
205
- if (security === socket_security_enum_1.DynamoNTS_SocketSecurity.open) {
206
- this.openSocketServer = newSocketServer;
207
- return this.openSocketServer;
208
192
  }
209
- else {
210
- this.secureSocketServer = newSocketServer;
211
- return this.secureSocketServer;
193
+ catch (error) {
194
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Connection failed: ${this.params.name} (${this.params.port}) will disconnect now...`, error);
195
+ socket.emit(socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
196
+ socket.disconnect();
212
197
  }
198
+ });
199
+ newSocketServer.listen(this.params.port);
200
+ fsm_dynamo_1.Dynamo_Log.success(`\nsocket server setup finished: ${this.params.name}` +
201
+ `\nsocket server listening on port: ${this.params.port}`);
202
+ successCallback();
203
+ if (security === socket_security_enum_1.DynamoNTS_SocketSecurity.open) {
204
+ this.openSocketServer = newSocketServer;
205
+ return this.openSocketServer;
213
206
  }
214
- catch (error) {
215
- fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service - Deep Setup failed: ${(_a = this.params) === null || _a === void 0 ? void 0 : _a.name} (${(_b = this.params) === null || _b === void 0 ? void 0 : _b.port})`, error);
216
- throw error;
207
+ else {
208
+ this.secureSocketServer = newSocketServer;
209
+ return this.secureSocketServer;
217
210
  }
218
- });
211
+ }
212
+ catch (error) {
213
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Server Service - Deep Setup failed: ${this.params?.name} (${this.params?.port})`, error);
214
+ throw error;
215
+ }
219
216
  }
220
- addSocketToPresence(newPresence) {
221
- var _a, _b;
222
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
223
- try {
224
- const activePresence = this.presences.find((pres) => pres.issuerId === newPresence.issuerId);
225
- if (activePresence) {
226
- activePresence.sockets.push(...newPresence.sockets);
227
- }
228
- else {
229
- this.presences.push(newPresence);
230
- }
217
+ async addSocketToPresence(newPresence) {
218
+ try {
219
+ const activePresence = this.presences.find((pres) => pres.issuerId === newPresence.issuerId);
220
+ if (activePresence) {
221
+ activePresence.sockets.push(...newPresence.sockets);
231
222
  }
232
- catch (error) {
233
- fsm_dynamo_1.Dynamo_Log.error(`Socket Subscription failed: ${(_a = this.params) === null || _a === void 0 ? void 0 : _a.name} (${(_b = this.params) === null || _b === void 0 ? void 0 : _b.port})`, error);
234
- throw error;
223
+ else {
224
+ this.presences.push(newPresence);
235
225
  }
236
- });
226
+ }
227
+ catch (error) {
228
+ fsm_dynamo_1.Dynamo_Log.error(`Socket Subscription failed: ${this.params?.name} (${this.params?.port})`, error);
229
+ throw error;
230
+ }
237
231
  }
238
- disconnectBaseTask(socket, issuer) {
239
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
240
- try {
241
- yield this.removeSubscription(socket, issuer);
242
- fsm_dynamo_1.Dynamo_Log.info(`< x > socket(${this.params.name}) disconnected (${issuer})`);
232
+ async disconnectBaseTask(socket, issuer) {
233
+ try {
234
+ await this.removeSubscription(socket, issuer);
235
+ fsm_dynamo_1.Dynamo_Log.info(`< x > socket(${this.params.name}) disconnected (${issuer})`);
236
+ }
237
+ catch (error) {
238
+ throw new fsm_dynamo_1.Dynamo_Error({
239
+ ...this._getDefaultErrorSettings('disconnectBaseTask', error, issuer),
240
+ errorCode: 'NTS-SSS-109'
241
+ });
242
+ }
243
+ }
244
+ async removeSubscription(socket, issuer) {
245
+ try {
246
+ const activePresenceIndex = this.presences.findIndex((pres) => pres.sockets.includes(socket));
247
+ if (activePresenceIndex === -1) {
248
+ throw new fsm_dynamo_1.Dynamo_Error({
249
+ ...this._getDefaultErrorSettings('sendEventForId', new Error(`closing socket(${this.params.name}) does not match any in the activePresences`), issuer),
250
+ errorCode: 'NTS-SSS-203',
251
+ });
243
252
  }
244
- catch (error) {
245
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('disconnectBaseTask', error, issuer)), { errorCode: 'NTS-SSS-109' }));
253
+ const activePresence = this.presences[activePresenceIndex];
254
+ if (activePresence.issuerId !== issuer) {
255
+ throw new fsm_dynamo_1.Dynamo_Error({
256
+ ...this._getDefaultErrorSettings('sendEventForId', new Error(`socket subscription for ${issuer} and ${activePresence.issuerId} does not match.`), issuer),
257
+ errorCode: 'NTS-SSS-201',
258
+ });
246
259
  }
247
- });
248
- }
249
- removeSubscription(socket, issuer) {
250
- var _a;
251
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
252
- try {
253
- const activePresenceIndex = this.presences.findIndex((pres) => pres.sockets.includes(socket));
254
- if (activePresenceIndex === -1) {
255
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', new Error(`closing socket(${this.params.name}) does not match any in the activePresences`), issuer)), { errorCode: 'NTS-SSS-203' }));
256
- }
257
- const activePresence = this.presences[activePresenceIndex];
258
- if (activePresence.issuerId !== issuer) {
259
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', new Error(`socket subscription for ${issuer} and ${activePresence.issuerId} does not match.`), issuer)), { errorCode: 'NTS-SSS-201' }));
260
- }
261
- const socketIndex = activePresence.sockets.findIndex((s) => s === socket);
262
- if (socketIndex === -1) {
263
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', new Error(`closing socket(${this.params.name}) does not match any in the activePresences`), issuer)), { errorCode: 'NTS-SSS-202' }));
264
- }
265
- activePresence.sockets.splice(socketIndex);
266
- if (this.getSubscriptionCloseTasks) {
267
- yield fsm_dynamo_1.Dynamo_Array.asyncForEach(this.getSubscriptionCloseTasks(), (task) => tslib_1.__awaiter(this, void 0, void 0, function* () {
268
- yield task(null, issuer);
269
- }));
270
- }
271
- if (activePresence.sockets.length === 0) {
272
- this.presences.splice(activePresenceIndex);
273
- }
274
- socket.disconnect();
275
- fsm_dynamo_1.Dynamo_Log.info(`<=x=> socket(${this.params.name}) unsubscription successfull (${issuer})`);
260
+ const socketIndex = activePresence.sockets.findIndex((s) => s === socket);
261
+ if (socketIndex === -1) {
262
+ throw new fsm_dynamo_1.Dynamo_Error({
263
+ ...this._getDefaultErrorSettings('sendEventForId', new Error(`closing socket(${this.params.name}) does not match any in the activePresences`), issuer),
264
+ errorCode: 'NTS-SSS-202',
265
+ });
276
266
  }
277
- catch (error) {
278
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', error, issuer)), { errorCode: 'NTS-SSS-200', message: `socket(${this.params.name}) Subscription Removal failed: ${(_a = this.params) === null || _a === void 0 ? void 0 : _a.name}` }));
267
+ activePresence.sockets.splice(socketIndex);
268
+ if (this.getSubscriptionCloseTasks) {
269
+ await fsm_dynamo_1.Dynamo_Array.asyncForEach(this.getSubscriptionCloseTasks(), async (task) => {
270
+ await task(null, issuer);
271
+ });
279
272
  }
280
- });
273
+ if (activePresence.sockets.length === 0) {
274
+ this.presences.splice(activePresenceIndex);
275
+ }
276
+ socket.disconnect();
277
+ fsm_dynamo_1.Dynamo_Log.info(`<=x=> socket(${this.params.name}) unsubscription successfull (${issuer})`);
278
+ }
279
+ catch (error) {
280
+ throw new fsm_dynamo_1.Dynamo_Error({
281
+ ...this._getDefaultErrorSettings('sendEventForId', error, issuer),
282
+ errorCode: 'NTS-SSS-200',
283
+ message: `socket(${this.params.name}) Subscription Removal failed: ${this.params?.name}`,
284
+ });
285
+ }
281
286
  }
282
287
  /* emitServerEvent(event: string, content: any): void {
283
288
  try {
@@ -303,55 +308,74 @@ class DynamoNTS_SocketServerService extends singleton_service_1.DynamoNTS_Single
303
308
  return !!this.presences.find((presence) => presence.issuerId === id);
304
309
  }
305
310
  catch (error) {
306
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('idIsSubscribed', error)), { errorCode: 'NTS-SSS-600', message: `socket(${this.params.name}) ID Subscription Check (${id}) failed` }));
311
+ throw new fsm_dynamo_1.Dynamo_Error({
312
+ ...this._getDefaultErrorSettings('idIsSubscribed', error),
313
+ errorCode: 'NTS-SSS-600',
314
+ message: `socket(${this.params.name}) ID Subscription Check (${id}) failed`,
315
+ });
307
316
  }
308
317
  }
309
- emitError(presenceIssuerId, error) {
310
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
311
- try {
312
- yield this.sendEventForId(presenceIssuerId, socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
318
+ async emitError(presenceIssuerId, error) {
319
+ try {
320
+ await this.sendEventForId(presenceIssuerId, socket_event_type_enum_1.DynamoNTS_SocketEventKey.error, error);
321
+ }
322
+ catch (error) {
323
+ throw new fsm_dynamo_1.Dynamo_Error({
324
+ ...this._getDefaultErrorSettings('emitError', error, presenceIssuerId),
325
+ errorCode: 'NTS-SSS-700',
326
+ message: `socket(${this.params.name}) Error Emit (id: ${presenceIssuerId}) failed`,
327
+ });
328
+ }
329
+ }
330
+ async sendEventForId(id, event, content /* , error?: (err: any) => void */) {
331
+ try {
332
+ const presence = this.presences.find((pres) => pres.issuerId === id);
333
+ if (!presence) {
334
+ throw new fsm_dynamo_1.Dynamo_Error({
335
+ ...this._getDefaultErrorSettings('sendEventForId', new Error(`No active socket with this specific ID: ${id}`), content?.source),
336
+ status: 404,
337
+ errorCode: 'NTS-SSS-301',
338
+ });
313
339
  }
314
- catch (error) {
315
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('emitError', error, presenceIssuerId)), { errorCode: 'NTS-SSS-700', message: `socket(${this.params.name}) Error Emit (id: ${presenceIssuerId}) failed` }));
340
+ presence.emitEvent(event, content);
341
+ if (global_settings_const_1.dynamoNTS_globalSettings.logSocketEventContent) {
342
+ fsm_dynamo_1.Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id}) \ncontent:`, content);
316
343
  }
317
- });
318
- }
319
- sendEventForId(id, event, content /* , error?: (err: any) => void */) {
320
- var _a;
321
- return tslib_1.__awaiter(this, void 0, void 0, function* () {
344
+ else {
345
+ fsm_dynamo_1.Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id})`);
346
+ }
347
+ }
348
+ catch (error) {
322
349
  try {
323
- const presence = this.presences.find((pres) => pres.issuerId === id);
324
- if (!presence) {
325
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', new Error(`No active socket with this specific ID: ${id}`), content === null || content === void 0 ? void 0 : content.source)), { status: 404, errorCode: 'NTS-SSS-301' }));
326
- }
327
- presence.emitEvent(event, content);
328
- if (global_settings_const_1.dynamoNTS_globalSettings.logSocketEventContent) {
329
- fsm_dynamo_1.Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id}) \ncontent:`, content);
330
- }
331
- else {
332
- fsm_dynamo_1.Dynamo_Log.success(` <--- emitted socket(${this.params.name}) event for presence: ${event}, presenceId: ${id})`);
350
+ if (error.flag.includes('DYNAMO') && error?.accitionalInfo?.inactiveSockets) {
351
+ const sockets = error.accitionalInfo.inactiveSockets;
352
+ await fsm_dynamo_1.Dynamo_Array.asyncForEach(sockets, async (socket) => {
353
+ await this.removeSubscription(socket);
354
+ });
355
+ const presence = this.presences.find((pres) => pres.issuerId === id);
356
+ if (!presence) {
357
+ throw new fsm_dynamo_1.Dynamo_Error({
358
+ ...this._getDefaultErrorSettings('sendEventForId', new Error(`No active socket(${this.params.name}) with this specific ID: ${id}`), content?.source),
359
+ status: 404,
360
+ errorCode: 'NTS-SSS-302',
361
+ });
362
+ }
333
363
  }
334
364
  }
335
365
  catch (error) {
336
- try {
337
- if (error.flag.includes('DYNAMO') && ((_a = error === null || error === void 0 ? void 0 : error.accitionalInfo) === null || _a === void 0 ? void 0 : _a.inactiveSockets)) {
338
- const sockets = error.accitionalInfo.inactiveSockets;
339
- yield fsm_dynamo_1.Dynamo_Array.asyncForEach(sockets, (socket) => tslib_1.__awaiter(this, void 0, void 0, function* () {
340
- yield this.removeSubscription(socket);
341
- }));
342
- const presence = this.presences.find((pres) => pres.issuerId === id);
343
- if (!presence) {
344
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', new Error(`No active socket(${this.params.name}) with this specific ID: ${id}`), content === null || content === void 0 ? void 0 : content.source)), { status: 404, errorCode: 'NTS-SSS-302' }));
345
- }
346
- }
347
- }
348
- catch (error) {
349
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', error, content === null || content === void 0 ? void 0 : content.source)), { errorCode: 'NTS-SSS-310', message: `Error handling of inactive sockets(${this.params.name}) failed!` +
350
- `\n(Socket Event Emit for id (${id}, ${event}) failed)` }));
351
- }
352
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('sendEventForId', error, content === null || content === void 0 ? void 0 : content.source)), { errorCode: 'NTS-SSS-310', message: `socket(${this.params.name}) Event Emit for id (${id}, ${event}) failed` }));
366
+ throw new fsm_dynamo_1.Dynamo_Error({
367
+ ...this._getDefaultErrorSettings('sendEventForId', error, content?.source),
368
+ errorCode: 'NTS-SSS-310',
369
+ message: `Error handling of inactive sockets(${this.params.name}) failed!` +
370
+ `\n(Socket Event Emit for id (${id}, ${event}) failed)`,
371
+ });
353
372
  }
354
- });
373
+ throw new fsm_dynamo_1.Dynamo_Error({
374
+ ...this._getDefaultErrorSettings('sendEventForId', error, content?.source),
375
+ errorCode: 'NTS-SSS-310',
376
+ message: `socket(${this.params.name}) Event Emit for id (${id}, ${event}) failed`,
377
+ });
378
+ }
355
379
  }
356
380
  broadcastEvent(event, content) {
357
381
  try {
@@ -362,21 +386,28 @@ class DynamoNTS_SocketServerService extends singleton_service_1.DynamoNTS_Single
362
386
  }
363
387
  catch (error) {
364
388
  fsm_dynamo_1.Dynamo_Log.error(`socket(${this.params.name}) Event Broadcast (${event}) failed`, error);
365
- throw new fsm_dynamo_1.Dynamo_Error(Object.assign(Object.assign({}, this._getDefaultErrorSettings('broadcastEvent', error)), { errorCode: 'NTS-SSS-400', message: `socket(${this.params.name}) Event Broadcast (${event}) failed` }));
389
+ throw new fsm_dynamo_1.Dynamo_Error({
390
+ ...this._getDefaultErrorSettings('broadcastEvent', error),
391
+ errorCode: 'NTS-SSS-400',
392
+ message: `socket(${this.params.name}) Event Broadcast (${event}) failed`,
393
+ });
366
394
  }
367
395
  }
368
396
  _getDefaultErrorSettings(fnName, error, issuer) {
369
- var _a, _b, _c;
370
397
  return {
371
- status: (_a = error === null || error === void 0 ? void 0 : error.___status) !== null && _a !== void 0 ? _a : 500,
372
- message: (_b = error === null || error === void 0 ? void 0 : error.message) !== null && _b !== void 0 ? _b : `${fnName} was UNSUCCESFUL (NTS)`,
398
+ status: error?.___status ?? 500,
399
+ message: error?.message ?? `${fnName} was UNSUCCESFUL (NTS)`,
373
400
  addECToUserMsg: true,
374
401
  userMessage: this.defaultErrorUserMsg,
375
402
  issuer: issuer,
376
- issuerService: (_c = this.constructor) === null || _c === void 0 ? void 0 : _c.name,
403
+ issuerService: this.constructor?.name,
377
404
  error: error,
378
405
  };
379
406
  }
407
+ /**
408
+ * You can setup tasks for the error event in this function
409
+ */
410
+ errorHandling;
380
411
  }
381
412
  exports.DynamoNTS_SocketServerService = DynamoNTS_SocketServerService;
382
413
  //# sourceMappingURL=socket-server.service.js.map