@futdevpro/nts-dynamo 1.5.34 → 1.5.35

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/dynamo-nts-global.settings.d.ts.map +1 -1
  2. package/lib/_constants/dynamo-nts-global.settings.js +6 -1
  3. package/lib/_constants/dynamo-nts-global.settings.js.map +1 -1
  4. package/lib/_enums/dynamo-nts-socket-security.enum.d.ts +5 -0
  5. package/lib/_enums/dynamo-nts-socket-security.enum.d.ts.map +1 -0
  6. package/lib/_enums/dynamo-nts-socket-security.enum.js +9 -0
  7. package/lib/_enums/dynamo-nts-socket-security.enum.js.map +1 -0
  8. package/lib/_enums/http/socket-event.enum.d.ts +7 -0
  9. package/lib/_enums/http/socket-event.enum.d.ts.map +1 -0
  10. package/lib/_enums/http/socket-event.enum.js +11 -0
  11. package/lib/_enums/http/socket-event.enum.js.map +1 -0
  12. package/lib/_enums/index.d.ts +2 -0
  13. package/lib/_enums/index.d.ts.map +1 -1
  14. package/lib/_enums/index.js +2 -0
  15. package/lib/_enums/index.js.map +1 -1
  16. package/lib/_models/dynamo-nts-endpoint-params.js +1 -1
  17. package/lib/_models/dynamo-nts-endpoint-params.js.map +1 -1
  18. package/lib/_models/dynamo-nts-global-settings.d.ts +11 -1
  19. package/lib/_models/dynamo-nts-global-settings.d.ts.map +1 -1
  20. package/lib/_models/dynamo-nts-routing-module-settings.d.ts +11 -0
  21. package/lib/_models/dynamo-nts-routing-module-settings.d.ts.map +1 -0
  22. package/lib/_models/dynamo-nts-routing-module-settings.js +3 -0
  23. package/lib/_models/dynamo-nts-routing-module-settings.js.map +1 -0
  24. package/lib/_models/dynamo-nts-socket-event-params.d.ts +29 -0
  25. package/lib/_models/dynamo-nts-socket-event-params.d.ts.map +1 -0
  26. package/lib/_models/dynamo-nts-socket-event-params.js +79 -0
  27. package/lib/_models/dynamo-nts-socket-event-params.js.map +1 -0
  28. package/lib/_models/dynamo-nts-socket-module-settings.d.ts +14 -0
  29. package/lib/_models/dynamo-nts-socket-module-settings.d.ts.map +1 -0
  30. package/lib/_models/dynamo-nts-socket-module-settings.js +3 -0
  31. package/lib/_models/dynamo-nts-socket-module-settings.js.map +1 -0
  32. package/lib/_models/index.d.ts +3 -0
  33. package/lib/_models/index.d.ts.map +1 -1
  34. package/lib/_models/index.js +3 -0
  35. package/lib/_models/index.js.map +1 -1
  36. package/lib/_modules/app-extended.index.d.ts +12 -0
  37. package/lib/_modules/app-extended.index.d.ts.map +1 -0
  38. package/lib/_modules/app-extended.index.js +19 -0
  39. package/lib/_modules/app-extended.index.js.map +1 -0
  40. package/lib/_modules/app.index.d.ts +1 -1
  41. package/lib/_modules/app.index.d.ts.map +1 -1
  42. package/lib/_modules/app.index.js +1 -1
  43. package/lib/_modules/app.index.js.map +1 -1
  44. package/lib/_modules/test/get-test-routing-module.d.ts.map +1 -1
  45. package/lib/_modules/test/get-test-routing-module.js.map +1 -1
  46. package/lib/_modules/usage/get-usage-routing-module.d.ts.map +1 -1
  47. package/lib/_modules/usage/get-usage-routing-module.js.map +1 -1
  48. package/lib/_services/dynamo-nts-app-extended.d.ts +21 -8
  49. package/lib/_services/dynamo-nts-app-extended.d.ts.map +1 -1
  50. package/lib/_services/dynamo-nts-app-extended.js +92 -54
  51. package/lib/_services/dynamo-nts-app-extended.js.map +1 -1
  52. package/lib/_services/dynamo-nts-app.d.ts +34 -31
  53. package/lib/_services/dynamo-nts-app.d.ts.map +1 -1
  54. package/lib/_services/dynamo-nts-app.js +1 -0
  55. package/lib/_services/dynamo-nts-app.js.map +1 -1
  56. package/lib/_services/dynamo-nts-global.service.d.ts +15 -0
  57. package/lib/_services/dynamo-nts-global.service.d.ts.map +1 -1
  58. package/lib/_services/dynamo-nts-global.service.js +55 -3
  59. package/lib/_services/dynamo-nts-global.service.js.map +1 -1
  60. package/lib/_services/dynamo-nts-routing-module.service.d.ts +2 -7
  61. package/lib/_services/dynamo-nts-routing-module.service.d.ts.map +1 -1
  62. package/lib/_services/dynamo-nts-routing-module.service.js +1 -1
  63. package/lib/_services/dynamo-nts-routing-module.service.js.map +1 -1
  64. package/lib/_services/dynamo-nts-socket-service-collection.service.d.ts +10 -0
  65. package/lib/_services/dynamo-nts-socket-service-collection.service.d.ts.map +1 -0
  66. package/lib/_services/dynamo-nts-socket-service-collection.service.js +14 -0
  67. package/lib/_services/dynamo-nts-socket-service-collection.service.js.map +1 -0
  68. package/lib/_services/dynamo-nts-socket.service.d.ts +36 -0
  69. package/lib/_services/dynamo-nts-socket.service.d.ts.map +1 -0
  70. package/lib/_services/dynamo-nts-socket.service.js +151 -0
  71. package/lib/_services/dynamo-nts-socket.service.js.map +1 -0
  72. package/lib/_services/index.d.ts +2 -0
  73. package/lib/_services/index.d.ts.map +1 -1
  74. package/lib/_services/index.js +2 -0
  75. package/lib/_services/index.js.map +1 -1
  76. package/lib/tsconfig.tsbuildinfo +324 -134
  77. package/package.json +10 -1
  78. package/src/_constants/dynamo-nts-global.settings.ts +9 -1
  79. package/src/_enums/dynamo-nts-socket-security.enum.ts +5 -0
  80. package/src/_enums/http/socket-event.enum.ts +7 -0
  81. package/src/_enums/index.ts +2 -0
  82. package/src/_models/dynamo-nts-endpoint-params.ts +1 -1
  83. package/src/_models/dynamo-nts-global-settings.ts +14 -1
  84. package/src/_models/dynamo-nts-routing-module-settings.ts +11 -0
  85. package/src/_models/dynamo-nts-socket-event-params.ts +99 -0
  86. package/src/_models/dynamo-nts-socket-module-settings.ts +16 -0
  87. package/src/_models/index.ts +3 -0
  88. package/src/_modules/app-extended.index.ts +20 -0
  89. package/src/_modules/app.index.ts +1 -1
  90. package/src/_modules/test/get-test-routing-module.ts +2 -1
  91. package/src/_modules/usage/get-usage-routing-module.ts +2 -1
  92. package/src/_services/dynamo-nts-app-extended.ts +93 -78
  93. package/src/_services/dynamo-nts-app.ts +34 -31
  94. package/src/_services/dynamo-nts-global.service.ts +57 -3
  95. package/src/_services/dynamo-nts-routing-module.service.ts +2 -7
  96. package/src/_services/dynamo-nts-socket-service-collection.service.ts +16 -0
  97. package/src/_services/dynamo-nts-socket.service.ts +179 -0
  98. package/src/_services/index.ts +2 -0
  99. package/src/_models/dynamo-module-settings.ts +0 -10
@@ -8,6 +8,8 @@ import { DynamoNTS_DBService } from './dynamo-nts-db.service';
8
8
  import { DynamoNTS_EmailServiceCollection } from './dynamo-nts-email-service-collection.service';
9
9
  import { DynamoNTS_Shared } from './dynamo-nts-shared.service';
10
10
  import { DynamoNTS_SingletonService } from './dynamo-nts-singleton.service';
11
+ import { DynamoNTS_SocketServiceCollection } from './dynamo-nts-socket-service-collection.service';
12
+ import { DynamoNTS_SocketService } from './dynamo-nts-socket.service';
11
13
 
12
14
  /**
13
15
  * This is the main Global/Core Service Collection used by DynamoBE,
@@ -23,6 +25,8 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
23
25
  authService: DynamoNTS_AuthService;
24
26
  dbServiceCollection: DynamoNTS_DBServiceCollection;
25
27
  emailServiceCollection: DynamoNTS_EmailServiceCollection;
28
+
29
+ socketServiceCollection: DynamoNTS_SocketServiceCollection = DynamoNTS_SocketServiceCollection.getInstance();
26
30
 
27
31
  /**
28
32
  * You need to setup global Services through this function
@@ -72,7 +76,7 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
72
76
  if (!this.instance?.authService?.authenticateToken) {
73
77
  throw new Error(
74
78
  `\nUnique Authentication Service missing!` +
75
- `\nPlease create a Unique Authentication Service extending DynamoBEAuthService, ` +
79
+ `\nPlease create a Unique Authentication Service extending DynamoNTS_AuthService, ` +
76
80
  `and Setup with "DBE_Global_SC.setServices(...)"`);
77
81
  }
78
82
  return this.instance.authService;
@@ -86,7 +90,7 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
86
90
  if (!this.instance?.dbServiceCollection) {
87
91
  throw new Error(
88
92
  `\nUnique DataBase Service Collection missing!` +
89
- `\nPlease create a Unique DataBase Service Collectio extending DynamoBEDBServiceCollection, ` +
93
+ `\nPlease create a Unique DataBase Service Collection extending DynamoNTS_DBServiceCollection, ` +
90
94
  `and Setup with "DBE_Global_SC.setServices(...)"`);
91
95
  }
92
96
  return this.instance.dbServiceCollection;
@@ -114,7 +118,7 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
114
118
  let keys: string = `${Object.keys(collection)}`;
115
119
  keys = keys.replace(/,/g, '\n ');
116
120
  throw new Error(
117
- `\nDBE_Global_S.getDBService failed ERROR:
121
+ `\nDBE_Global_S.getDBService failed, ERROR:
118
122
  no dbService on collection with this key: ${key}!
119
123
  The available dbServices on collection:
120
124
  ${keys}\n`);
@@ -134,4 +138,54 @@ export class DynamoNTS_GlobalService extends DynamoNTS_SingletonService {
134
138
  }
135
139
  return this.instance.emailServiceCollection;
136
140
  }
141
+
142
+ /**
143
+ *
144
+ * @returns
145
+ */
146
+ static getSocketServiceCollection(): DynamoNTS_SocketServiceCollection {
147
+ if (!this.instance?.socketServiceCollection) {
148
+ throw new Error(
149
+ `\nUnique Socket Service Collection missing!` +
150
+ `\nSocket Service Collection will only be created if there are Socket Services properly setted up ` +
151
+ `in the setupModules in the App using DynamoNTS_AppExtended, ` +
152
+ `and Setup with "DBE_Global_SC.setServices(...)"`);
153
+ }
154
+ return this.instance.socketServiceCollection;
155
+ }
156
+
157
+ /**
158
+ *
159
+ * @param name
160
+ * @returns
161
+ */
162
+ static getSocketServiceByName(name: string): DynamoNTS_SocketService {
163
+ const collection = this.getSocketServiceCollection();
164
+ if (collection[name]) {
165
+ return collection[name];
166
+ } else {
167
+ let names: string = `${Object.keys(collection)}`;
168
+ names = names.replace(/,/g, '\n ');
169
+ throw new Error(
170
+ `\nDBE_Global_S.getSocketServiceByName failed, ERROR:
171
+ no socketService on collection with this name: ${name}!
172
+ The available socketServices on collection:
173
+ ${names}\n`);
174
+ }
175
+ }
176
+
177
+ static attachNewSocketService(service: DynamoNTS_SocketService): void {
178
+ const collection = this.getSocketServiceCollection();
179
+ if (!collection[service.name]) {
180
+ collection[service.name] = service;
181
+ } else {
182
+ let names: string = `${Object.keys(collection)}`;
183
+ names = names.replace(/,/g, '\n ');
184
+ throw new Error(
185
+ `\nDBE_Global_S.attachNewSocketService failed, ERROR:
186
+ socketService name is already taken on collection with this name: ${service.name}!
187
+ The available socketServices on collection:
188
+ ${names}\n`);
189
+ }
190
+ }
137
191
  }
@@ -8,12 +8,7 @@ import { HttpCallType } from '../_enums/http/http-call-type.enum';
8
8
  import { DynamoNTS_EndpointParams } from '../_models/dynamo-nts-endpoint-params';
9
9
  import { DynamoNTS_Controller } from './dynamo-nts-controller.service';
10
10
  import { DynamoNTS_Shared } from './dynamo-nts-shared.service';
11
-
12
- export interface DynamoNTS_RoutingModuleSettings {
13
- route: string;
14
- controllers: DynamoNTS_Controller[];
15
- securityOverride?: DynamoNTS_RouteSecurity;
16
- }
11
+ import { DynamoNTS_RoutingModuleSettings } from '../_models/dynamo-nts-routing-module-settings';
17
12
 
18
13
  /**
19
14
  *
@@ -47,7 +42,7 @@ export class DynamoNTS_RoutingModule {
47
42
  }
48
43
 
49
44
  /**
50
- * You must setup endpoints and required services in this function
45
+ * You must setup endpoints and required services for this function
51
46
  */
52
47
  private setupRoutes(): void {
53
48
  this.controllers.forEach((controller: DynamoNTS_Controller) => {
@@ -0,0 +1,16 @@
1
+
2
+ import { DynamoNTS_SingletonService } from './dynamo-nts-singleton.service';
3
+ import { DynamoNTS_SocketService } from './dynamo-nts-socket.service';
4
+
5
+ /**
6
+ *
7
+ */
8
+ export class DynamoNTS_SocketServiceCollection extends DynamoNTS_SingletonService {
9
+
10
+ static getInstance(): DynamoNTS_SocketServiceCollection {
11
+ return DynamoNTS_SocketServiceCollection.getSingletonInstance();
12
+ }
13
+
14
+ [service: string]: DynamoNTS_SocketService;
15
+
16
+ }
@@ -0,0 +1,179 @@
1
+
2
+ import * as SocketIO from 'socket.io';
3
+
4
+ import { dynamoNTS_GlobalSettings } from '../_constants/dynamo-nts-global.settings';
5
+ import { DynamoNTS_Shared } from './dynamo-nts-shared.service';
6
+ import { DynamoNTS_SocketServiceSettings } from '../_models';
7
+ import { DynamoNTS_SingletonService } from './dynamo-nts-singleton.service';
8
+ import { DynamoNTS_SocketEventParams } from '../_models/dynamo-nts-socket-event-params';
9
+ import { DynamoNTS_SocketEvent } from '../_enums/http/socket-event.enum';
10
+ import { DynamoNTS_GlobalService } from './dynamo-nts-global.service';
11
+ import { DynamoNTS_Error } from '@futdevpro/fsm-dynamo';
12
+ import { DynamoNTS_SocketSecurity } from '../_enums/dynamo-nts-socket-security.enum';
13
+
14
+ export interface DynamoNTS_SocketDictionary {
15
+ id?: string;
16
+ sockets: SocketIO.Socket[];
17
+ }
18
+
19
+ /**
20
+ *
21
+ */
22
+ export class DynamoNTS_SocketService extends DynamoNTS_SingletonService {
23
+ name: string;
24
+ security: DynamoNTS_SocketSecurity;
25
+ port: number;
26
+
27
+ socketServer: SocketIO.Server;
28
+
29
+ connectEvent: DynamoNTS_SocketEventParams<SocketIO.Socket>;
30
+ events: DynamoNTS_SocketEventParams<any>[];
31
+
32
+ subscriptionEvent: DynamoNTS_SocketEventParams<any>;
33
+ getIdFromSubscrioption?: <T>(content: T) => string;
34
+ activeSockets: DynamoNTS_SocketDictionary[] = [];
35
+
36
+ protected log: boolean = dynamoNTS_GlobalSettings.logSetup;
37
+
38
+ constructor(
39
+ set: DynamoNTS_SocketServiceSettings
40
+ ){
41
+ super();
42
+ try {
43
+ this.name = set.name;
44
+ this.port = set.port;
45
+ this.events = set.events;
46
+ this.security = set.security ? set.security : dynamoNTS_GlobalSettings.defaultSocketSecurity;
47
+
48
+ if (set.getIdFromSubscrioption) {
49
+ this.getIdFromSubscrioption = set.getIdFromSubscrioption;
50
+ if (set.groupActiveSocketsBy) {
51
+ DynamoNTS_Shared.logError(
52
+ `setting groupActiveSocketsBy on DynamoNTS_SocketService (${this.name}) won't take effect, ` +
53
+ `since getIdFromSubscrioption is also setted, so it will be used instead`)
54
+ }
55
+ } else if (set.groupActiveSocketsBy) {
56
+ this.getIdFromSubscrioption = content => content[set.groupActiveSocketsBy];
57
+ }
58
+
59
+ this.attachToGlobalCollection();
60
+ this.prepareEvents();
61
+
62
+ if (this.log) console.log(`Socket Controller setup done: ${this.name} (${this.port}) serurity: ${this.security}\n`);
63
+ } catch (error) {
64
+ DynamoNTS_Shared.logError(`Socket Controller setup failed: ${this.name} (${this.port})`, error);
65
+ }
66
+ }
67
+
68
+ private attachToGlobalCollection(): void {
69
+ DynamoNTS_GlobalService.attachNewSocketService(this);
70
+ }
71
+
72
+ /**
73
+ * You must setup events and required services for this function
74
+ */
75
+ private prepareEvents(): void {
76
+ const connectEventIndex = this.events.findIndex((event: DynamoNTS_SocketEventParams<any>) => event.event === DynamoNTS_SocketEvent.connection);
77
+ if (0 <= connectEventIndex) {
78
+ this.connectEvent = this.events.splice(connectEventIndex)[0];
79
+ } else {
80
+ this.connectEvent = new DynamoNTS_SocketEventParams({
81
+ event: DynamoNTS_SocketEvent.connection,
82
+ tasks: []
83
+ });
84
+ }
85
+
86
+ if (this.getIdFromSubscrioption) {
87
+ const subscriptionEventIndex = this.events.findIndex((event: DynamoNTS_SocketEventParams<any>) => event.event === DynamoNTS_SocketEvent.subscribe);
88
+ if (0 <= subscriptionEventIndex) {
89
+ this.subscriptionEvent = this.events.splice(subscriptionEventIndex)[0];
90
+ } else {
91
+ this.subscriptionEvent = new DynamoNTS_SocketEventParams<SocketIO.Socket>({
92
+ event: DynamoNTS_SocketEvent.subscribe,
93
+ tasks: []
94
+ });
95
+ }
96
+
97
+ const disconnectEventIndex = this.events.findIndex((event: DynamoNTS_SocketEventParams<any>) => event.event === DynamoNTS_SocketEvent.disconnect);
98
+ if (0 <= disconnectEventIndex) {
99
+ this.events[disconnectEventIndex].tasks.push(this.removeSubscriptionOnDisconnect);
100
+ } else {
101
+ this.events.push(new DynamoNTS_SocketEventParams<SocketIO.Socket>({
102
+ event: DynamoNTS_SocketEvent.disconnect,
103
+ tasks: [
104
+ this.removeSubscriptionOnDisconnect
105
+ ]
106
+ }));
107
+ }
108
+ }
109
+ }
110
+
111
+ setupSocketServer(newSocketServer: SocketIO.Server): void {
112
+ this.socketServer = newSocketServer;
113
+ this.socketServer.on(DynamoNTS_SocketEvent.connection, async (socket: SocketIO.Socket) => {
114
+ let issuer: string;
115
+
116
+ for(let i = 0; i < this.connectEvent.preProcessess.length; i++) {
117
+ await this.connectEvent.preProcessess[i](socket);
118
+ }
119
+
120
+ for(let i = 0; i < this.connectEvent.tasks.length; i++) {
121
+ await this.connectEvent.tasks[i](socket);
122
+ }
123
+
124
+ if (this.getIdFromSubscrioption) {
125
+ socket.on(DynamoNTS_SocketEvent.subscribe, (content: any) => {
126
+ issuer = this.getIdFromSubscrioption(content);
127
+ this.socketSubscription(socket, issuer);
128
+ this.subscriptionEvent.getFullExecution(issuer)(content);
129
+ })
130
+ }
131
+
132
+ this.events.forEach((event: DynamoNTS_SocketEventParams<any>) => {
133
+ socket.on(event.event, event.getFullExecution(issuer));
134
+ });
135
+ });
136
+
137
+ this.socketServer.listen(this.port);
138
+
139
+ if (this.log) console.log(
140
+ `\nsocket service setup finished: ${this.name}` +
141
+ `\nsocket server listening on port: ${this.port}`
142
+ );
143
+ }
144
+
145
+ private async socketSubscription(newSocket: SocketIO.Socket, id: string): Promise<void> {
146
+ const activeSocket: DynamoNTS_SocketDictionary = this.activeSockets.find((dic: DynamoNTS_SocketDictionary) => dic.id === id);
147
+ if (activeSocket) {
148
+ activeSocket.sockets.push(newSocket);
149
+ } else {
150
+ this.activeSockets.push({ id, sockets: [ newSocket ] });
151
+ }
152
+ }
153
+
154
+ private async removeSubscriptionOnDisconnect(socket: SocketIO.Socket): Promise<void> {
155
+ const activeSocketIndex: number = this.activeSockets.findIndex((dic: DynamoNTS_SocketDictionary) => dic.sockets.includes(socket));
156
+ if (0 <= activeSocketIndex) {
157
+ const socketIndex = this.activeSockets[activeSocketIndex].sockets.findIndex((s: SocketIO.Socket) => s === socket);
158
+ this.activeSockets[activeSocketIndex].sockets.splice(socketIndex);
159
+ if (this.activeSockets[activeSocketIndex].sockets.length === 0) {
160
+ this.activeSockets.splice(activeSocketIndex);
161
+ }
162
+ }
163
+ }
164
+
165
+ emitEvent(event: string, content: any): void {
166
+ this.socketServer.emit(event, content);
167
+ }
168
+
169
+ emitEventForId(id: string, event: string, content: any): void {
170
+ const dic: DynamoNTS_SocketDictionary = this.activeSockets.find((dic: DynamoNTS_SocketDictionary) => dic.id === id);
171
+ if (dic) {
172
+ dic.sockets.forEach((socket: SocketIO.Socket) => {
173
+ socket.emit(event, content);
174
+ });
175
+ } else {
176
+ throw new DynamoNTS_Error({ message: `No active socket whit this specific ID: ${id}` });
177
+ }
178
+ }
179
+ }
@@ -14,6 +14,8 @@ export * from './dynamo-nts-email-service-collection.service';
14
14
  export * from './dynamo-nts-email.service';
15
15
  export * from './dynamo-nts-global.service';
16
16
  export * from './dynamo-nts-routing-module.service';
17
+ export * from './dynamo-nts-socket.service';
18
+ export * from './dynamo-nts-socket-service-collection.service';
17
19
  export * from './dynamo-nts-shared.service';
18
20
  export * from './dynamo-nts-singleton.service';
19
21
 
@@ -1,10 +0,0 @@
1
-
2
- /**
3
- *
4
- */
5
- /* export interface DynamoModuleSettings {
6
- mainRoute: string;
7
- endPoints: {
8
- [endpointName: string]: string
9
- }
10
- } */