@futdevpro/nts-dynamo 1.6.19 → 1.6.21

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 (88) hide show
  1. package/lib/_constants/mocks/app-extended-server.mock.d.ts +41 -0
  2. package/lib/_constants/mocks/app-extended-server.mock.d.ts.map +1 -0
  3. package/lib/_constants/mocks/app-extended-server.mock.js +168 -0
  4. package/lib/_constants/mocks/app-extended-server.mock.js.map +1 -0
  5. package/lib/_constants/mocks/app-server.mock.d.ts +1 -1
  6. package/lib/_constants/mocks/app-server.mock.d.ts.map +1 -1
  7. package/lib/_constants/mocks/app-server.mock.js +12 -5
  8. package/lib/_constants/mocks/app-server.mock.js.map +1 -1
  9. package/lib/_constants/mocks/data-model.mock.d.ts +27 -0
  10. package/lib/_constants/mocks/data-model.mock.d.ts.map +1 -0
  11. package/lib/_constants/mocks/data-model.mock.js +57 -0
  12. package/lib/_constants/mocks/data-model.mock.js.map +1 -0
  13. package/lib/_constants/mocks/email-service-collection.mock.d.ts +7 -0
  14. package/lib/_constants/mocks/email-service-collection.mock.d.ts.map +1 -0
  15. package/lib/_constants/mocks/email-service-collection.mock.js +16 -0
  16. package/lib/_constants/mocks/email-service-collection.mock.js.map +1 -0
  17. package/lib/_constants/mocks/email-service.mock.d.ts +5 -0
  18. package/lib/_constants/mocks/email-service.mock.d.ts.map +1 -0
  19. package/lib/_constants/mocks/email-service.mock.js +21 -0
  20. package/lib/_constants/mocks/email-service.mock.js.map +1 -0
  21. package/lib/_constants/mocks/socket-client.mock.d.ts +9 -0
  22. package/lib/_constants/mocks/socket-client.mock.d.ts.map +1 -0
  23. package/lib/_constants/mocks/socket-client.mock.js +39 -0
  24. package/lib/_constants/mocks/socket-client.mock.js.map +1 -0
  25. package/lib/_constants/mocks/socket-server.mock.d.ts +1 -0
  26. package/lib/_constants/mocks/socket-server.mock.d.ts.map +1 -1
  27. package/lib/_constants/mocks/socket-server.mock.js +4 -1
  28. package/lib/_constants/mocks/socket-server.mock.js.map +1 -1
  29. package/lib/_models/control-models/app-system-controls.control-model copy.d.ts +7 -0
  30. package/lib/_models/control-models/app-system-controls.control-model copy.d.ts.map +1 -0
  31. package/lib/_models/control-models/app-system-controls.control-model copy.js +13 -0
  32. package/lib/_models/control-models/app-system-controls.control-model copy.js.map +1 -0
  33. package/lib/_models/control-models/app-system-controls.control-model.d.ts +8 -0
  34. package/lib/_models/control-models/app-system-controls.control-model.d.ts.map +1 -0
  35. package/lib/_models/control-models/app-system-controls.control-model.js +14 -0
  36. package/lib/_models/control-models/app-system-controls.control-model.js.map +1 -0
  37. package/lib/_models/control-models/system-control.control-model.d.ts +6 -0
  38. package/lib/_models/control-models/system-control.control-model.d.ts.map +1 -0
  39. package/lib/_models/control-models/system-control.control-model.js +15 -0
  40. package/lib/_models/control-models/system-control.control-model.js.map +1 -0
  41. package/lib/_services/base/db.service.d.ts +1 -2
  42. package/lib/_services/base/db.service.d.ts.map +1 -1
  43. package/lib/_services/base/db.service.js +23 -15
  44. package/lib/_services/base/db.service.js.map +1 -1
  45. package/lib/_services/core/email.service.d.ts.map +1 -1
  46. package/lib/_services/core/email.service.js +0 -1
  47. package/lib/_services/core/email.service.js.map +1 -1
  48. package/lib/_services/server/app-extended.server.d.ts +9 -0
  49. package/lib/_services/server/app-extended.server.d.ts.map +1 -1
  50. package/lib/_services/server/app-extended.server.js +228 -81
  51. package/lib/_services/server/app-extended.server.js.map +1 -1
  52. package/lib/_services/server/app-extended.server.spec.d.ts +2 -0
  53. package/lib/_services/server/app-extended.server.spec.d.ts.map +1 -0
  54. package/lib/_services/server/app-extended.server.spec.js +55 -0
  55. package/lib/_services/server/app-extended.server.spec.js.map +1 -0
  56. package/lib/_services/server/app.server.d.ts +17 -17
  57. package/lib/_services/server/app.server.d.ts.map +1 -1
  58. package/lib/_services/server/app.server.js +382 -258
  59. package/lib/_services/server/app.server.js.map +1 -1
  60. package/lib/_services/server/app.server.spec.js +17 -5
  61. package/lib/_services/server/app.server.spec.js.map +1 -1
  62. package/lib/_services/shared.service.spec.js +7 -1
  63. package/lib/_services/shared.service.spec.js.map +1 -1
  64. package/lib/_services/socket/socket-server.service.d.ts +5 -3
  65. package/lib/_services/socket/socket-server.service.d.ts.map +1 -1
  66. package/lib/_services/socket/socket-server.service.js +32 -7
  67. package/lib/_services/socket/socket-server.service.js.map +1 -1
  68. package/lib/tsconfig.tsbuildinfo +1 -1
  69. package/package.json +2 -2
  70. package/src/_constants/mocks/app-extended-server.mock.ts +191 -0
  71. package/src/_constants/mocks/app-server.mock.ts +10 -5
  72. package/src/_constants/mocks/data-model.mock.ts +83 -0
  73. package/src/_constants/mocks/email-service-collection.mock.ts +12 -0
  74. package/src/_constants/mocks/email-service.mock.ts +19 -0
  75. package/src/_constants/mocks/email-template.mock.html +15 -0
  76. package/src/_constants/mocks/socket-client.mock.ts +42 -0
  77. package/src/_constants/mocks/socket-server.mock.ts +5 -1
  78. package/src/_models/control-models/app-system-controls.control-model copy.ts +9 -0
  79. package/src/_models/control-models/app-system-controls.control-model.ts +9 -0
  80. package/src/_models/control-models/system-control.control-model.ts +13 -0
  81. package/src/_services/base/db.service.ts +25 -15
  82. package/src/_services/core/email.service.ts +0 -1
  83. package/src/_services/server/app-extended.server.spec.ts +66 -0
  84. package/src/_services/server/app-extended.server.ts +205 -41
  85. package/src/_services/server/app.server.spec.ts +17 -7
  86. package/src/_services/server/app.server.ts +284 -165
  87. package/src/_services/shared.service.spec.ts +10 -1
  88. package/src/_services/socket/socket-server.service.ts +36 -12
@@ -2,12 +2,13 @@
2
2
  import * as SocketIO from 'socket.io';
3
3
  import * as Http from 'http';
4
4
 
5
- import { Dynamo_Log } from '@futdevpro/fsm-dynamo';
5
+ import { delay, Dynamo_Array, Dynamo_Error, Dynamo_Log, second, wait } from '@futdevpro/fsm-dynamo';
6
6
 
7
7
  import { DynamoNTS_App } from './app.server';
8
8
  import { DynamoNTS_RouteSecurity } from '../../_enums/route-security.enum';
9
9
  import { DynamoNTS_SocketServerService } from '../socket/socket-server.service';
10
10
  import { DynamoNTS_SocketSecurity } from '../../_enums/socket-security.enum';
11
+ import { DynamoNTS_AppExtSysControls } from '../../_models/control-models/app-system-controls.control-model copy';
11
12
 
12
13
  /**
13
14
  * This will be the MAIN service of our server project,
@@ -110,8 +111,15 @@ import { DynamoNTS_SocketSecurity } from '../../_enums/socket-security.enum';
110
111
  * }
111
112
  */
112
113
  export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
114
+
115
+ private systemControlsExt: DynamoNTS_AppExtSysControls = new DynamoNTS_AppExtSysControls();
116
+ override get started(): boolean { return this.systemControlsExt.appExtended.started; }
117
+ private httpsSocketCount: number = 0;
118
+ private httpSocketCount: number = 0;
113
119
 
114
120
  private socketSecurity: DynamoNTS_RouteSecurity;
121
+
122
+ private httpSocketServer: Http.Server;
115
123
 
116
124
  /**
117
125
  * @example
@@ -123,27 +131,147 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
123
131
  * ];
124
132
  */
125
133
  protected socketServices: DynamoNTS_SocketServerService<any>[];
134
+ private allSocketServers: SocketIO.Server[] = [];
126
135
 
127
136
  constructor(){
128
- super();
137
+ super(true);
138
+
139
+ this.asyncConstructExt();
140
+ }
129
141
 
142
+ private async asyncConstructExt(): Promise<void> {
143
+ if (this.logFn && this.deepLog) console.log('\nfn:. asyncConstruct-extended');
130
144
  try {
145
+ this.systemControlsExt.appExtended.init = true;
146
+
131
147
  this.socketServices = this.getSocketServices();
132
148
 
133
149
  if (this.socketServices?.length) {
134
150
  this.setSocketSecurity();
135
151
 
136
- this.setupSocketServerServices();
137
-
138
- if (this.debugLog) console.log(`\nAll sockets setted up.... sockets using security: ${this.socketSecurity}`);
152
+ await this.setupSocketServerServices();
153
+ if (this.logSetup) console.log(`\nAll sockets setted up.... sockets using security: ${this.socketSecurity}`);
139
154
  } else {
140
- Dynamo_Log.warn(
155
+ Dynamo_Log.testWarn(
141
156
  '\nNo socketServices setted up while using Extended Application.',
142
157
  '\nYou should use DynamoNTS_App if you don`t need socket services.\n'
143
158
  );
144
159
  }
160
+
161
+ await this.ready();
162
+
163
+ Dynamo_Log.test(`${this.params.name} started successfully.`);
145
164
  } catch (error) {
146
- Dynamo_Log.error('\nExtended Application start failed.\n', error, '\n');
165
+ Dynamo_Log.error(`\nExtended Application: ${this.params?.name} start failed.\n`, error, '\n');
166
+ }
167
+ }
168
+
169
+ override async ready(timeout: number = 4 * second): Promise<void> {
170
+ if (this.logFn) console.log('\nfn:. ready-extended');
171
+ await super.ready(timeout);
172
+
173
+ let ready: boolean = false;
174
+ const start: number = +new Date();
175
+
176
+ if (this.constructErrors.length) {
177
+ Dynamo_Log.error(`Extended Application: ${this.params?.name} start failed. ERRORS`, this.constructErrors, '\n');
178
+ throw new Dynamo_Error({
179
+ message: `${this.params?.name} start failed. ERRORS`,
180
+ additionalContent: this.constructErrors,
181
+ error: this.constructErrors?.[0] ?? new Error()
182
+ });
183
+ }
184
+
185
+ while (!ready && +new Date() - start < timeout) {
186
+ if (this.systemControlsExt.appExtended.init) {
187
+ ready = (
188
+ this.superStarted &&
189
+ this.systemControlsExt.httpSocketServer.getReady() &&
190
+ this.systemControlsExt.httpsSocketServer.getReady()
191
+ );
192
+ }
193
+
194
+ await wait(100);
195
+ }
196
+
197
+ if (this.constructErrors.length) {
198
+ Dynamo_Log.error(`Extended Application: ${this.params?.name} start failed. TIMEOUT`, this.constructErrors, '\n');
199
+ throw new Dynamo_Error({
200
+ message: `${this.params?.name} start failed. TIMEOUT`,
201
+ additionalContent: this.constructErrors,
202
+ error: this.constructErrors?.[0] ?? new Error()
203
+ });
204
+ }
205
+
206
+ if (ready) {
207
+ this.systemControlsExt.appExtended.started = true;
208
+
209
+ if (this.logFn && this.deepLog) console.log('\nfn:. ready-extended: return');
210
+ return;
211
+ } else {
212
+ this.systemControlsExt.appExtended.started = false;
213
+
214
+ let msg: string = `${this.params?.name} start failed. UNKNOWN`;
215
+ if (this.systemControlsExt.httpSocketServer.init && !this.systemControlsExt.httpSocketServer.started) {
216
+ msg += '\nhttpSocketServer start failed.';
217
+ }
218
+ if (this.systemControlsExt.httpsSocketServer.init && !this.systemControlsExt.httpsSocketServer.started) {
219
+ msg += '\nhttpsSocketServer start failed.';
220
+ }
221
+
222
+ Dynamo_Log.error(msg, this.constructErrors, '\n');
223
+ throw new Dynamo_Error({
224
+ errorCode: 'NTS-AES-110',
225
+ message: msg,
226
+ additionalContent: {
227
+ constructErrors: this.constructErrors,
228
+ systemControls: this.systemControls,
229
+ systemControlsExt: this.systemControlsExt,
230
+ systemReadies: {
231
+ app: this.systemControls.app.getReady(),
232
+ appExtended: this.systemControlsExt.appExtended.getReady(),
233
+ mongoose: this.systemControls.mongoose.getReady(),
234
+ httpServer: this.systemControls.httpServer.getReady(),
235
+ httpsServer: this.systemControls.httpsServer.getReady(),
236
+ httpSocketServer: this.systemControlsExt.httpSocketServer.getReady(),
237
+ httpsSocketServer: this.systemControlsExt.httpsSocketServer.getReady(),
238
+ },
239
+ },
240
+ error: this.constructErrors?.[0] ?? new Error()
241
+ });
242
+ }
243
+ }
244
+
245
+ override async stop(): Promise<void> {
246
+ if (this.logFn) console.log('\nfn:. stop-extended');
247
+ if (this.started) {
248
+ await super.stop(true);
249
+
250
+ this.systemControlsExt.httpSocketServer.started = false;
251
+ this.systemControlsExt.httpsSocketServer.started = false;
252
+ await Dynamo_Array.asyncForEach(this.allSocketServers, async (socketServer: SocketIO.Server) => {
253
+ await new Promise<void>((resolve, reject) => {
254
+ socketServer.disconnectSockets(true);
255
+ socketServer.close((err) => {
256
+ if (err) {
257
+ Dynamo_Log.error(`\nHTTP socket server close error`, err);
258
+ reject(
259
+ new Dynamo_Error({
260
+ errorCode: 'NTS-AES-120',
261
+ message: `SocketServer close failed.`,
262
+ error: err
263
+ })
264
+ );
265
+ } else {
266
+ resolve();
267
+ }
268
+ });
269
+ });
270
+ });
271
+ this.systemControlsExt.httpSocketServer.init = false;
272
+ this.systemControlsExt.httpsSocketServer.init = false;
273
+
274
+ Dynamo_Log.test(`${this.params.name} stopped successfully.`);
147
275
  }
148
276
  }
149
277
 
@@ -151,6 +279,7 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
151
279
  *
152
280
  */
153
281
  private setSocketSecurity(): void {
282
+ if (this.logFn) console.log('\nfn:. setSocketSecurity');
154
283
  this.socketServices.forEach((service: DynamoNTS_SocketServerService<any>) => {
155
284
  if (!this.socketSecurity) {
156
285
  this.socketSecurity = DynamoNTS_RouteSecurity[service.security];
@@ -163,20 +292,23 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
163
292
  /**
164
293
  *
165
294
  */
166
- private setupSocketServerServices(): void {
295
+ private async setupSocketServerServices(): Promise<void> {
167
296
  try {
168
- let httpSocketServer: Http.Server;
297
+ if (this.logFn) console.log('\nfn:. setupSocketServerServices');
298
+ /* let httpServer: Http.Server; */
169
299
  if (this.socketSecurity !== DynamoNTS_RouteSecurity.secure) {
170
300
  if (!this.openExpress) {
171
- this.initOpenExpress();
301
+ await this.initOpenExpress();
302
+ }
303
+ if (!this.httpSocketServer) {
304
+ this.httpSocketServer = Http.createServer(this.openExpress);
172
305
  }
173
- httpSocketServer = Http.createServer(this.openExpress);
174
306
  }
175
307
 
176
308
  if (this.socketSecurity !== DynamoNTS_RouteSecurity.open) {
177
309
  if (!this.httpsServer) {
178
310
  if (this.cert) {
179
- this.initSecureExpress();
311
+ await this.initSecureExpress();
180
312
  } else {
181
313
  let errorMsg: string =
182
314
  `\nYou have secure socket service, but the certification paths are not set!` +
@@ -190,7 +322,7 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
190
322
  errorMsg += '\n\nThe socket services setted to use secure server:';
191
323
  this.socketServices.forEach((service: DynamoNTS_SocketServerService<any>) => {
192
324
  if (service.security != DynamoNTS_SocketSecurity.open) {
193
- errorMsg += `\n ${service.name} (port: ${service.port})`;
325
+ errorMsg += `\n ${service?.name} (port: ${service.port})`;
194
326
  }
195
327
  });
196
328
  Dynamo_Log.error(errorMsg);
@@ -205,42 +337,74 @@ export abstract class DynamoNTS_AppExtended extends DynamoNTS_App {
205
337
  }
206
338
  }
207
339
 
208
- this.socketServices.forEach((service: DynamoNTS_SocketServerService<any>) => {
209
- try {
210
- const existingPorts: DynamoNTS_SocketServerService<any>[] = this.socketServices.filter(
211
- (mod: DynamoNTS_SocketServerService<any>) => mod.port === service.port
212
- );
340
+ await Dynamo_Array.asyncForEach(
341
+ this.socketServices,
342
+ async (service: DynamoNTS_SocketServerService<any>) => {
343
+ try {
344
+ this.systemControlsExt.httpSocketServer.init = true;
345
+ const existingPorts: DynamoNTS_SocketServerService<any>[] = this.socketServices.filter(
346
+ (mod: DynamoNTS_SocketServerService<any>) => mod.port === service.port
347
+ );
213
348
 
214
- if (1 < existingPorts.length || this.ports.httpPort === service.port || this.ports.httpsPort === service.port) {
215
- let error = new Error(`PORT DUPLICATION: ${service.port}`);
216
- let errorStack: string[] = error.stack.split('\n');
217
- errorStack.splice(1, 4);
218
- error.stack = errorStack.join('\n');
349
+ if (1 < existingPorts.length || this.ports.httpPort === service.port || this.ports.httpsPort === service.port) {
350
+ let error = new Error(`PORT DUPLICATION: ${service.port}`);
351
+ let errorStack: string[] = error.stack.split('\n');
352
+ errorStack.splice(1, 4);
353
+ error.stack = errorStack.join('\n');
219
354
 
220
- throw error;
221
- }
355
+ throw error;
356
+ }
222
357
 
223
- if (service.security === DynamoNTS_SocketSecurity.open) {
224
- if (this.debugLog) console.log(`\nsocket setup (open): ${service.name}:${service.port}`);
225
- service.setupSocketServer(new SocketIO.Server(httpSocketServer));
358
+ if (service.security === DynamoNTS_SocketSecurity.open) {
359
+ if (this.logSetup) console.log(`\nsocket setup (open): ${service?.name}:${service.port}`);
360
+ this.httpSocketCount++;
361
+ this.systemControlsExt.httpSocketServer.started = false;
226
362
 
227
- } else if (service.security === DynamoNTS_SocketSecurity.secure) {
228
- if (this.debugLog) console.log(`\nsocket setup (secure): ${service.name}:${service.port}`);
229
- service.setupSocketServer(new SocketIO.Server(this.httpsServer));
230
-
231
- } else {
232
- let error = new Error(`INVALID Socket Service security: ${service.security} on ${service.name}`);
233
- let errorStack: string[] = error.stack.split('\n');
234
- errorStack.splice(1, 4);
235
- error.stack = errorStack.join('\n');
363
+ this.allSocketServers.push(
364
+ await service.setupSocketServer(
365
+ new SocketIO.Server(this.httpSocketServer),
366
+ DynamoNTS_SocketSecurity.open,
367
+ () => {
368
+ this.httpSocketCount--;
369
+ if (this.httpSocketCount === 0) {
370
+ this.systemControlsExt.httpSocketServer.started = true;
371
+ }
372
+ }
373
+ )
374
+ );
375
+
376
+ } else if (service.security === DynamoNTS_SocketSecurity.secure) {
377
+ if (this.logSetup) console.log(`\nsocket setup (secure): ${service?.name}:${service.port}`);
378
+ this.httpsSocketCount++;
379
+ this.systemControlsExt.httpsSocketServer.started = false;
236
380
 
381
+ this.allSocketServers.push(
382
+ await service.setupSocketServer(
383
+ new SocketIO.Server(this.httpsServer),
384
+ DynamoNTS_SocketSecurity.secure,
385
+ () => {
386
+ this.httpsSocketCount--;
387
+ if (this.httpsSocketCount === 0) {
388
+ this.systemControlsExt.httpsSocketServer.started = true;
389
+ }
390
+ }
391
+ )
392
+ );
393
+
394
+ } else {
395
+ let error = new Error(`INVALID Socket Service security: ${service.security} on ${service?.name}`);
396
+ let errorStack: string[] = error.stack.split('\n');
397
+ errorStack.splice(1, 4);
398
+ error.stack = errorStack.join('\n');
399
+
400
+ throw error;
401
+ }
402
+ } catch (error) {
403
+ Dynamo_Log.error(`\nSocket Server service setup failed. (${service?.name})\n`, error, '\n');
237
404
  throw error;
238
405
  }
239
- } catch (error) {
240
- Dynamo_Log.error(`\nSocket Server service setup failed. (${service.name})\n`, error, '\n');
241
- throw error;
242
406
  }
243
- });
407
+ );
244
408
  } catch (error) {
245
409
  Dynamo_Log.error(`\nSocket Server services setup failed. (${this.socketServices?.length} services)\n`, error, '\n');
246
410
  throw error;
@@ -1,14 +1,21 @@
1
1
 
2
- import { delay } from '@futdevpro/fsm-dynamo';
2
+ import { delay, Dynamo_Log, second } from '@futdevpro/fsm-dynamo';
3
+ import { DynamoNTS_AppExtendedBase_Mock, DynamoNTS_AppExtendedFull_Mock } from '../../_constants/mocks/app-extended-server.mock';
3
4
  import { DynamoNTS_AppBase_Mock, DynamoNTS_AppFull_Mock } from '../../_constants/mocks/app-server.mock';
4
5
 
5
- describe('DynamoNTS_App;', () => {
6
+ describe('DynamoNTS_App AND DynamoNTS_AppExtended?;', () => {
7
+ // jasmine.DEFAULT_TIMEOUT_INTERVAL = 15 * second;
8
+
6
9
  describe('a base instance;', () => {
7
10
  let app: DynamoNTS_AppBase_Mock;
8
11
 
9
12
  beforeEach(async () => {
10
- app = new DynamoNTS_AppBase_Mock();
11
- await app.ready();
13
+ try {
14
+ app = new DynamoNTS_AppBase_Mock();
15
+ await app.ready();
16
+ } catch (error) {
17
+ Dynamo_Log.test('error', error);
18
+ }
12
19
  });
13
20
 
14
21
  afterEach(async () => {
@@ -28,8 +35,12 @@ describe('DynamoNTS_App;', () => {
28
35
  let app: DynamoNTS_AppFull_Mock;
29
36
 
30
37
  beforeEach(async () => {
31
- app = new DynamoNTS_AppFull_Mock();
32
- await app.ready();
38
+ try {
39
+ app = new DynamoNTS_AppFull_Mock();
40
+ await app.ready();
41
+ } catch (error) {
42
+ Dynamo_Log.test('error', error);
43
+ }
33
44
  });
34
45
 
35
46
  afterEach(async () => {
@@ -43,7 +54,6 @@ describe('DynamoNTS_App;', () => {
43
54
  it('should be started', () => {
44
55
  expect(app.started).toBeTrue();
45
56
  });
46
-
47
57
  });
48
58
  });
49
59