@futdevpro/nts-dynamo 1.6.19 → 1.6.20

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 (63) hide show
  1. package/lib/_constants/mocks/app-extended-server.mock.d.ts +29 -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 +99 -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 +9 -5
  8. package/lib/_constants/mocks/app-server.mock.js.map +1 -1
  9. package/lib/_constants/mocks/socket-server.mock.d.ts +1 -0
  10. package/lib/_constants/mocks/socket-server.mock.d.ts.map +1 -1
  11. package/lib/_constants/mocks/socket-server.mock.js +4 -1
  12. package/lib/_constants/mocks/socket-server.mock.js.map +1 -1
  13. package/lib/_models/control-models/app-system-controls.control-model copy.d.ts +7 -0
  14. package/lib/_models/control-models/app-system-controls.control-model copy.d.ts.map +1 -0
  15. package/lib/_models/control-models/app-system-controls.control-model copy.js +13 -0
  16. package/lib/_models/control-models/app-system-controls.control-model copy.js.map +1 -0
  17. package/lib/_models/control-models/app-system-controls.control-model.d.ts +8 -0
  18. package/lib/_models/control-models/app-system-controls.control-model.d.ts.map +1 -0
  19. package/lib/_models/control-models/app-system-controls.control-model.js +14 -0
  20. package/lib/_models/control-models/app-system-controls.control-model.js.map +1 -0
  21. package/lib/_models/control-models/system-control.control-model.d.ts +6 -0
  22. package/lib/_models/control-models/system-control.control-model.d.ts.map +1 -0
  23. package/lib/_models/control-models/system-control.control-model.js +15 -0
  24. package/lib/_models/control-models/system-control.control-model.js.map +1 -0
  25. package/lib/_services/base/db.service.d.ts +1 -2
  26. package/lib/_services/base/db.service.d.ts.map +1 -1
  27. package/lib/_services/base/db.service.js +23 -15
  28. package/lib/_services/base/db.service.js.map +1 -1
  29. package/lib/_services/server/app-extended.server.d.ts +9 -0
  30. package/lib/_services/server/app-extended.server.d.ts.map +1 -1
  31. package/lib/_services/server/app-extended.server.js +230 -81
  32. package/lib/_services/server/app-extended.server.js.map +1 -1
  33. package/lib/_services/server/app-extended.server.spec.d.ts +2 -0
  34. package/lib/_services/server/app-extended.server.spec.d.ts.map +1 -0
  35. package/lib/_services/server/app-extended.server.spec.js +39 -0
  36. package/lib/_services/server/app-extended.server.spec.js.map +1 -0
  37. package/lib/_services/server/app.server.d.ts +17 -17
  38. package/lib/_services/server/app.server.d.ts.map +1 -1
  39. package/lib/_services/server/app.server.js +401 -275
  40. package/lib/_services/server/app.server.js.map +1 -1
  41. package/lib/_services/server/app.server.spec.js +17 -5
  42. package/lib/_services/server/app.server.spec.js.map +1 -1
  43. package/lib/_services/shared.service.spec.js +7 -1
  44. package/lib/_services/shared.service.spec.js.map +1 -1
  45. package/lib/_services/socket/socket-server.service.d.ts +5 -3
  46. package/lib/_services/socket/socket-server.service.d.ts.map +1 -1
  47. package/lib/_services/socket/socket-server.service.js +32 -7
  48. package/lib/_services/socket/socket-server.service.js.map +1 -1
  49. package/lib/tsconfig.tsbuildinfo +1 -1
  50. package/package.json +2 -2
  51. package/src/_constants/mocks/app-extended-server.mock.ts +113 -0
  52. package/src/_constants/mocks/app-server.mock.ts +7 -5
  53. package/src/_constants/mocks/socket-server.mock.ts +5 -1
  54. package/src/_models/control-models/app-system-controls.control-model copy.ts +9 -0
  55. package/src/_models/control-models/app-system-controls.control-model.ts +9 -0
  56. package/src/_models/control-models/system-control.control-model.ts +13 -0
  57. package/src/_services/base/db.service.ts +25 -15
  58. package/src/_services/server/app-extended.server.spec.ts +45 -0
  59. package/src/_services/server/app-extended.server.ts +208 -41
  60. package/src/_services/server/app.server.spec.ts +17 -7
  61. package/src/_services/server/app.server.ts +277 -167
  62. package/src/_services/shared.service.spec.ts +10 -1
  63. package/src/_services/socket/socket-server.service.ts +36 -11
@@ -12,6 +12,7 @@ const singleton_service_1 = require("../base/singleton.service");
12
12
  const route_security_enum_1 = require("../../_enums/route-security.enum");
13
13
  const global_service_1 = require("../core/global.service");
14
14
  const global_settings_const_1 = require("../../_constants/global-settings.const");
15
+ const app_system_controls_control_model_1 = require("../../_models/control-models/app-system-controls.control-model");
15
16
  /**
16
17
  * This will be the MAIN service of our server project,
17
18
  * follow the types and type instructions while setting up your project
@@ -168,160 +169,223 @@ const global_settings_const_1 = require("../../_constants/global-settings.const"
168
169
  *
169
170
  */
170
171
  class DynamoNTS_App extends singleton_service_1.DynamoNTS_SingletonService {
172
+ get started() { return this.systemControls.app.started; }
173
+ get superStarted() { return this.systemControls.app.started; }
171
174
  get serverName() { return this.params.name; }
172
175
  get params() { return this._params; }
173
176
  get security() { return this._security; }
174
177
  get ports() { return this._ports; }
175
178
  get cert() { return this._cert; }
176
- constructor() {
177
- var _a;
179
+ constructor(dontLog) {
178
180
  super();
179
- this.started = false;
180
- this.initDone = false;
181
- this.haveMongoose = false;
182
- this.mongooseStarted = false;
183
- this.haveHttpServer = false;
184
- this.httpServerStarted = false;
185
- this.haveHttpsServer = false;
186
- this.httpsServerStarted = false;
181
+ this.systemControls = new app_system_controls_control_model_1.DynamoNTS_AppSystemControls();
187
182
  this.constructErrors = [];
188
183
  this.mongoose = Mongoose;
189
184
  this._routingModules = [];
190
185
  /* protected get routingModules(): DynamoNTS_RoutingModule[] { return this._routingModules; } */
191
186
  this._rootServices = [];
192
- try {
193
- this._params = this.getAppParams();
194
- if (this.overrideDynamoNTSGlobalSettings) {
195
- this.overrideDynamoNTSGlobalSettings();
196
- }
197
- this.globalService = global_service_1.DynamoNTS_GlobalService.getInstance();
198
- global_service_1.DynamoNTS_GlobalService.setServices(this.getGlobalServiceCollection());
199
- global_service_1.DynamoNTS_GlobalService.setParams(this.params);
200
- if (this.getPortSettings) {
201
- this._ports = this.getPortSettings();
202
- }
203
- if (this.getCertificationSettings) {
204
- this._cert = this.getCertificationSettings();
205
- }
206
- if (this.getRoutingModules) {
207
- this._routingModules = this.getRoutingModules();
208
- }
209
- if (this.createEntries) {
210
- this.createEntries();
211
- }
212
- console.log(`\n\n\nStarting ${this._params.name}... `);
213
- if (this._params.title) {
214
- console.log(this._params.title);
215
- }
216
- console.log(`Version: ${this._params.version}\n`);
217
- this.startDB();
218
- this.getGlobalServiceCollection();
219
- this.debugLog = global_settings_const_1.dynamoNTS_globalSettings.logSetup;
220
- this.setSecurity();
221
- if ((_a = this._routingModules) === null || _a === void 0 ? void 0 : _a.length) {
222
- this.initExpresses();
223
- this.startExpresses();
224
- if (this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
225
- this.mountOpenRoutes();
187
+ this.asyncConstruct(dontLog);
188
+ }
189
+ asyncConstruct(dontLog) {
190
+ var _a;
191
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
192
+ if (this.logFn && this.deepLog)
193
+ console.log('\nfn:. asyncConstruct');
194
+ try {
195
+ this.systemControls.app.init = true;
196
+ this.logSetup = global_settings_const_1.dynamoNTS_globalSettings.logSetup;
197
+ this._params = this.getAppParams();
198
+ if (this.overrideDynamoNTSGlobalSettings) {
199
+ this.overrideDynamoNTSGlobalSettings();
226
200
  }
227
- if (this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.open && this._cert) {
228
- this.mountSecureRoutes();
201
+ this.globalService = global_service_1.DynamoNTS_GlobalService.getInstance();
202
+ global_service_1.DynamoNTS_GlobalService.setServices(this.getGlobalServiceCollection());
203
+ global_service_1.DynamoNTS_GlobalService.setParams(this.params);
204
+ if (this.getPortSettings) {
205
+ this._ports = this.getPortSettings();
229
206
  }
230
- if (this.debugLog) {
231
- console.log(`\nRoutes mounted.... server using security: ${this._security}`);
207
+ if (this.getCertificationSettings) {
208
+ this._cert = this.getCertificationSettings();
209
+ }
210
+ if (this.getRoutingModules) {
211
+ this._routingModules = this.getRoutingModules();
212
+ }
213
+ if (this.createEntries) {
214
+ yield this.createEntries();
215
+ }
216
+ console.log(`\n\n\nStarting ${this._params.name}... `);
217
+ if (this._params.title) {
218
+ console.log(this._params.title);
219
+ }
220
+ console.log(`Version: ${this._params.version}\n`);
221
+ yield this.startDB();
222
+ yield this.getGlobalServiceCollection();
223
+ yield this.setSecurity();
224
+ if ((_a = this._routingModules) === null || _a === void 0 ? void 0 : _a.length) {
225
+ yield this.initExpresses();
226
+ yield this.startExpresses();
227
+ if (this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
228
+ yield this.mountOpenRoutes();
229
+ }
230
+ if (this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.open && this._cert) {
231
+ yield this.mountSecureRoutes();
232
+ }
233
+ if (this.logSetup) {
234
+ console.log(`\nRoutes mounted.... server using security: ${this._security}`);
235
+ }
236
+ }
237
+ if (this.getRootServices) {
238
+ this._rootServices = yield this.getRootServices();
239
+ }
240
+ if (this.postProcess) {
241
+ yield this.postProcess();
242
+ }
243
+ yield this.ready();
244
+ if (!dontLog) {
245
+ fsm_dynamo_1.Dynamo_Log.test(`${this._params.name} started successfully.`);
232
246
  }
233
247
  }
234
- if (this.getRootServices) {
235
- this._rootServices = this.getRootServices();
236
- }
237
- if (this.postProcess) {
238
- this.postProcess();
248
+ catch (error) {
249
+ this.constructErrors.push(error);
250
+ fsm_dynamo_1.Dynamo_Log.error(`\nApplication: ${this._params.name} start failed.\n`, error);
239
251
  }
240
- this.initDone = true;
241
- this.ready();
242
- }
243
- catch (error) {
244
- this.constructErrors.push(error);
245
- fsm_dynamo_1.Dynamo_Log.error('\nApplication start failed.\n', error);
246
- }
252
+ });
247
253
  }
248
254
  ready(timeout = 4 * fsm_dynamo_1.second) {
255
+ var _a, _b;
249
256
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
250
- return new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
251
- let ready = false;
252
- const start = +new Date();
253
- if (this.constructErrors.length) {
254
- reject({
255
- message: 'Application start failed. TIMEOUT',
256
- errors: this.constructErrors
257
- });
258
- }
259
- while (!ready && +new Date() - start < timeout) {
260
- yield (0, fsm_dynamo_1.delay)(100);
261
- if (this.initDone) {
262
- ready = (this.haveMongoose ? this.mongooseStarted : true &&
263
- this.haveHttpServer ? this.httpServerStarted : true &&
264
- this.haveHttpsServer ? this.httpsServerStarted : true);
265
- }
257
+ if (this.logFn)
258
+ console.log('\nfn:. ready');
259
+ /* return new Promise<void>(async (resolve, reject) => { */
260
+ let ready = false;
261
+ const start = +new Date();
262
+ if (this.constructErrors.length) {
263
+ if (this.logFn && this.deepLog)
264
+ console.log('\nfn:. ready: throw ERROR-0', this.constructErrors);
265
+ throw new fsm_dynamo_1.Dynamo_Error({
266
+ message: `${this._params.name} start failed. ERRORS`,
267
+ additionalContent: this.constructErrors.length === 1 ? this.constructErrors[0] : { errors: this.constructErrors },
268
+ });
269
+ }
270
+ if (this.logFn && this.deepLog)
271
+ console.log('\nfn:. ready: bef while', this.systemControls);
272
+ while (!ready && +new Date() - start < timeout) {
273
+ // if (this.logFn && this.deepLog) console.log('\nfn:. ready: while', this.systemControls);
274
+ /* if (this.logFn && this.deepLog) console.log('\nfn:. ready: while', this.systemControls,
275
+ {
276
+ mongoose: this.systemControls.mongoose.getReady(),
277
+ httpServer: this.systemControls.httpServer.getReady(),
278
+ httpsServer: this.systemControls.httpsServer.getReady(),
279
+ }); */
280
+ if (this.systemControls.app.init) {
281
+ ready = (this.systemControls.mongoose.getReady() &&
282
+ this.systemControls.httpServer.getReady() &&
283
+ this.systemControls.httpsServer.getReady());
266
284
  }
267
- if (this.constructErrors.length) {
268
- reject({
269
- message: 'Application start failed. TIMEOUT',
270
- errors: this.constructErrors
285
+ yield (0, fsm_dynamo_1.wait)(100);
286
+ }
287
+ if (timeout < +new Date() - start) {
288
+ //if (this.logFn && this.deepLog) console.log('\nfn:. ready: throw TIMEOUT', this.constructErrors, this.systemControls);
289
+ if (this.logFn && this.deepLog)
290
+ console.log('\nfn:. ready: throw TIMEOUT', this.constructErrors, this.systemControls, {
291
+ mongoose: this.systemControls.mongoose.getReady(),
292
+ httpServer: this.systemControls.httpServer.getReady(),
293
+ httpsServer: this.systemControls.httpsServer.getReady(),
271
294
  });
295
+ throw new fsm_dynamo_1.Dynamo_Error({
296
+ message: `${this._params.name} start failed. TIMEOUT`,
297
+ additionalContent: {
298
+ constructErrors: this.constructErrors,
299
+ systemControls: this.systemControls,
300
+ systemReadies: {
301
+ mongoose: this.systemControls.mongoose.getReady(),
302
+ httpServer: this.systemControls.httpServer.getReady(),
303
+ httpsServer: this.systemControls.httpsServer.getReady(),
304
+ },
305
+ },
306
+ });
307
+ }
308
+ if (this.constructErrors.length) {
309
+ if (this.logFn && this.deepLog)
310
+ console.log('\nfn:. ready: throw ERROR');
311
+ throw new fsm_dynamo_1.Dynamo_Error({
312
+ message: `${this._params.name} start failed. ERROR`,
313
+ additionalContent: this.constructErrors,
314
+ });
315
+ }
316
+ if (ready) {
317
+ this.systemControls.app.started = true;
318
+ if (this.logFn && this.deepLog)
319
+ console.log('\nfn:. ready: return');
320
+ return;
321
+ }
322
+ else {
323
+ this.systemControls.app.started = false;
324
+ let msg = `${this._params.name} start failed. UNKNOWN`;
325
+ if (this.systemControls.mongoose.init && !this.systemControls.mongoose.started) {
326
+ msg += `\nMongoose start failed.`;
272
327
  }
273
- if (ready) {
274
- this.started = true;
275
- resolve();
328
+ if (this.systemControls.httpServer.init && !this.systemControls.httpServer.started) {
329
+ msg += `\nHTTP Server start failed.`;
276
330
  }
277
- else {
278
- this.started = false;
279
- let msg = 'Application start failed. TIMEOUT';
280
- if (this.haveMongoose && !this.mongooseStarted) {
281
- msg += '\nMongoose start failed.';
282
- }
283
- if (this.haveHttpServer && !this.httpServerStarted) {
284
- msg += '\nHTTP Server start failed.';
285
- }
286
- if (this.haveHttpsServer && !this.httpsServerStarted) {
287
- msg += '\nHTTPS Server start failed.';
288
- }
289
- reject({
290
- message: 'Application start failed. TIMEOUT',
291
- errors: this.constructErrors
292
- });
331
+ if (this.systemControls.httpsServer.init && !this.systemControls.httpsServer.started) {
332
+ msg += `\nHTTPS Server start failed.`;
293
333
  }
294
- }));
334
+ if (this.logFn && this.deepLog)
335
+ console.log('\nfn:. ready: throw UNKNOWN');
336
+ throw new fsm_dynamo_1.Dynamo_Error({
337
+ message: msg,
338
+ additionalContent: this.constructErrors,
339
+ error: (_b = (_a = this.constructErrors) === null || _a === void 0 ? void 0 : _a[0]) !== null && _b !== void 0 ? _b : new Error(),
340
+ });
341
+ }
295
342
  });
296
343
  }
297
- stop() {
344
+ stop(dontLog) {
298
345
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
346
+ if (this.logFn)
347
+ console.log('\nfn:. stop');
299
348
  if (this.started) {
300
- if (this.haveHttpServer) {
301
- yield new Promise((resolve) => {
302
- this.httpServer.close(resolve);
303
- });
349
+ if (this.systemControls.mongoose.init) {
350
+ this.systemControls.mongoose.started = false;
351
+ if (this.mongoose) {
352
+ yield fsm_dynamo_1.Dynamo_Array.asyncForEach(Object.keys(this.mongoose.models), (modelName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
353
+ yield this.mongoose.deleteModel(modelName);
354
+ }));
355
+ yield this.mongoose.disconnect();
356
+ }
357
+ else {
358
+ fsm_dynamo_1.Dynamo_Log.error(`\nMongoose not found.`);
359
+ }
360
+ this.systemControls.mongoose.init = false;
304
361
  }
305
- if (this.haveHttpsServer) {
306
- yield new Promise((resolve) => {
307
- this.httpsServer.close(resolve);
308
- });
362
+ if (this.systemControls.httpServer.init) {
363
+ this.systemControls.httpServer.started = false;
364
+ if (this.httpServer) {
365
+ yield new Promise((resolve) => {
366
+ this.httpServer.close(resolve);
367
+ });
368
+ }
369
+ else {
370
+ fsm_dynamo_1.Dynamo_Log.error(`\nHTTP Server not found.`);
371
+ }
372
+ this.systemControls.httpServer.init = false;
373
+ }
374
+ if (this.systemControls.httpsServer.init) {
375
+ this.systemControls.httpsServer.started = false;
376
+ if (this.httpsServer) {
377
+ yield new Promise((resolve) => {
378
+ this.httpsServer.close(resolve);
379
+ });
380
+ }
381
+ else {
382
+ fsm_dynamo_1.Dynamo_Log.error(`\nHTTPS Server not found.`);
383
+ }
384
+ this.systemControls.httpsServer.init = false;
309
385
  }
310
- if (this.haveMongoose) {
311
- yield fsm_dynamo_1.Dynamo_Array.asyncForEach(Object.keys(this.mongoose.models), (modelName) => tslib_1.__awaiter(this, void 0, void 0, function* () {
312
- yield this.mongoose.deleteModel(modelName);
313
- }));
314
- yield this.mongoose.disconnect();
386
+ if (!dontLog) {
387
+ fsm_dynamo_1.Dynamo_Log.test(`${this._params.name} stopped successfully.`);
315
388
  }
316
- this.initDone = false;
317
- this.haveHttpServer = false;
318
- this.haveHttpsServer = false;
319
- this.haveMongoose = false;
320
- this.mongooseStarted = false;
321
- this.httpServerStarted = false;
322
- this.httpsServerStarted = false;
323
- this.started = false;
324
- fsm_dynamo_1.Dynamo_Log.success('\nServer stopped.\n');
325
389
  }
326
390
  });
327
391
  }
@@ -329,140 +393,184 @@ class DynamoNTS_App extends singleton_service_1.DynamoNTS_SingletonService {
329
393
  *
330
394
  */
331
395
  startDB() {
332
- this.haveMongoose = true;
333
- this.mongoose.connection
334
- .on('error', (error) => {
335
- this.mongooseStarted = false;
336
- this.constructErrors.push(error);
337
- fsm_dynamo_1.Dynamo_Log.error('\nUnable to connect to MongoDB server, ERROR: ', error);
338
- })
339
- .once('open', () => {
340
- this.mongooseStarted = true;
341
- fsm_dynamo_1.Dynamo_Log.success('\nConnected to MongoDB\n');
342
- });
343
- this.mongoose.connect(this._params.dbUri, {
344
- useNewUrlParser: true,
345
- useUnifiedTopology: true
396
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
397
+ if (this.logFn && this.deepLog)
398
+ console.log('\nfn:. startDB');
399
+ yield new Promise((resolve, reject) => tslib_1.__awaiter(this, void 0, void 0, function* () {
400
+ this.systemControls.mongoose.init = true;
401
+ this.mongoose.connection
402
+ .on('error', (error) => {
403
+ this.systemControls.mongoose.started = false;
404
+ this.constructErrors.push(error);
405
+ fsm_dynamo_1.Dynamo_Log.error('\nUnable to connect to MongoDB server, ERROR: ', error);
406
+ reject(error);
407
+ })
408
+ .once('open', () => {
409
+ this.systemControls.mongoose.started = true;
410
+ fsm_dynamo_1.Dynamo_Log.success('\nConnected to MongoDB\n');
411
+ resolve();
412
+ });
413
+ this.mongoose.connect(this._params.dbUri, {
414
+ useNewUrlParser: true,
415
+ useUnifiedTopology: true
416
+ });
417
+ }));
346
418
  });
347
419
  }
348
420
  /**
349
421
  *
350
422
  */
351
423
  initExpresses() {
352
- if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
353
- if (!this._ports.httpPort) {
354
- let errorMsg = `\nYou have open routes, but httpPort is not set!` +
355
- `\nsecurity: ${this._security}` +
356
- `\nset httpPort in DynamoBEServer - setupRoutingModules() to enable secure routes.`;
357
- errorMsg += '\n\nThe routes setted to use open server:';
358
- this._routingModules.forEach((module) => {
359
- if (module.security != route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
360
- errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using open sever:`;
361
- module.endpoints.forEach((endpoint) => {
362
- if (endpoint.security != route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
363
- errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
364
- }
365
- });
366
- }
367
- });
368
- fsm_dynamo_1.Dynamo_Log.error(errorMsg);
369
- let error = new Error('Open routes cannot be established!');
370
- let errorStack = error.stack.split('\n');
371
- errorStack.splice(1, 2);
372
- error.stack = errorStack.join('\n');
373
- throw error;
424
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
425
+ if (this.logFn && this.deepLog)
426
+ console.log('\nfn:. initExpresses');
427
+ if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
428
+ if (!this._ports.httpPort) {
429
+ let errorMsg = `\nYou have open routes, but httpPort is not set!` +
430
+ `\nsecurity: ${this._security}` +
431
+ `\nset httpPort in DynamoBEServer - setupRoutingModules() to enable secure routes.`;
432
+ errorMsg += '\n\nThe routes setted to use open server:';
433
+ this._routingModules.forEach((module) => {
434
+ if (module.security != route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
435
+ errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using open sever:`;
436
+ module.endpoints.forEach((endpoint) => {
437
+ if (endpoint.security != route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
438
+ errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
439
+ }
440
+ });
441
+ }
442
+ });
443
+ fsm_dynamo_1.Dynamo_Log.error(errorMsg);
444
+ let error = new Error('Open routes cannot be established!');
445
+ let errorStack = error.stack.split('\n');
446
+ errorStack.splice(1, 2);
447
+ error.stack = errorStack.join('\n');
448
+ throw error;
449
+ }
450
+ yield this.initOpenExpress();
374
451
  }
375
- this.initOpenExpress();
376
- }
377
- if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
378
- if (!this._cert || !this._ports.httpsPort) {
379
- let errorMsg = `\nYou have secure routes, but the certification paths or httpsPort are not set!` +
380
- `\nsecurity: ${this._security}` +
381
- `\nset...` +
382
- `\n httpsPort and` +
383
- `\n cert: {` +
384
- `\n keyPath: FileSystem.PathLike,` +
385
- `\n certPath: FileSystem.PathLike,` +
386
- `\n }` +
387
- `\nin DynamoBEServer - getRoutingModules() to enable secure routes.`;
388
- errorMsg += '\n\nThe routes setted to use secure server:';
389
- this._routingModules.forEach((module) => {
390
- if (module.security && module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
391
- errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using secure sever:`;
392
- module.endpoints.forEach((endpoint) => {
393
- if (endpoint.security && endpoint.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
394
- errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
395
- }
396
- });
397
- }
398
- });
399
- fsm_dynamo_1.Dynamo_Log.error(errorMsg);
400
- let error = new Error('Secure routes cannot be established!');
401
- let errorStack = error.stack.split('\n');
402
- errorStack.splice(1, 2);
403
- error.stack = errorStack.join('\n');
404
- throw error;
452
+ if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
453
+ if (!this._cert || !this._ports.httpsPort) {
454
+ let errorMsg = `\nYou have secure routes, but the certification paths or httpsPort are not set!` +
455
+ `\nsecurity: ${this._security}` +
456
+ `\nset...` +
457
+ `\n httpsPort and` +
458
+ `\n cert: {` +
459
+ `\n keyPath: FileSystem.PathLike,` +
460
+ `\n certPath: FileSystem.PathLike,` +
461
+ `\n }` +
462
+ `\nin DynamoBEServer - getRoutingModules() to enable secure routes.`;
463
+ errorMsg += '\n\nThe routes setted to use secure server:';
464
+ this._routingModules.forEach((module) => {
465
+ if (module.security && module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
466
+ errorMsg += `\n ${module.route} (security: ${module.security}) \n subroutes using secure sever:`;
467
+ module.endpoints.forEach((endpoint) => {
468
+ if (endpoint.security && endpoint.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
469
+ errorMsg += `\n ${endpoint.endpoint} (security: ${endpoint.security})`;
470
+ }
471
+ });
472
+ }
473
+ });
474
+ fsm_dynamo_1.Dynamo_Log.error(errorMsg);
475
+ let error = new Error('Secure routes cannot be established!');
476
+ let errorStack = error.stack.split('\n');
477
+ errorStack.splice(1, 2);
478
+ error.stack = errorStack.join('\n');
479
+ throw error;
480
+ }
481
+ yield this.initSecureExpress();
405
482
  }
406
- this.initSecureExpress();
407
- }
483
+ });
408
484
  }
409
485
  /**
410
486
  *
411
487
  */
412
488
  initOpenExpress() {
413
- this.openExpress = Express();
414
- this.openExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
415
- this.openExpress.use(BodyParser.json({ limit: '50mb' }));
489
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
490
+ if (this.logFn)
491
+ console.log('\nfn:. initOpenExpress');
492
+ this.openExpress = Express();
493
+ this.openExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
494
+ this.openExpress.use(BodyParser.json({ limit: '50mb' }));
495
+ });
416
496
  }
417
497
  /**
418
498
  *
419
499
  */
420
500
  initSecureExpress() {
421
- this.secureExpress = Express();
422
- this.secureExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
423
- this.secureExpress.use(BodyParser.json({ limit: '50mb' }));
424
- const options = {
425
- key: FileSystem.readFileSync(this._cert.keyPath),
426
- cert: FileSystem.readFileSync(this._cert.certPath),
427
- };
428
- this.httpsServer = Https.createServer(options, this.secureExpress);
501
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
502
+ if (this.logFn)
503
+ console.log('\nfn:. initSecureExpress');
504
+ this.secureExpress = Express();
505
+ this.secureExpress.use(BodyParser.urlencoded({ limit: '50mb', extended: true }));
506
+ this.secureExpress.use(BodyParser.json({ limit: '50mb' }));
507
+ const options = {
508
+ key: FileSystem.readFileSync(this._cert.keyPath),
509
+ cert: FileSystem.readFileSync(this._cert.certPath),
510
+ };
511
+ this.httpsServer = Https.createServer(options, this.secureExpress);
512
+ });
429
513
  }
430
514
  /**
431
515
  *
432
516
  */
433
517
  startExpresses() {
434
518
  return tslib_1.__awaiter(this, void 0, void 0, function* () {
519
+ if (this.logFn && this.deepLog)
520
+ console.log('\nfn:. startExpresses');
435
521
  try {
436
522
  if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
437
- this.haveHttpsServer = true;
438
- this.httpsServer
439
- .listen(this._ports.httpsPort, 'localhost', 0, () => {
440
- this.httpsServerStarted = true;
441
- fsm_dynamo_1.Dynamo_Log.success(`\nHTTPS (secure) server is listening on port ${this._ports.httpsPort}`);
442
- })
443
- .on('error', (error) => {
444
- this.httpsServerStarted = false;
445
- this.constructErrors.push(error);
446
- fsm_dynamo_1.Dynamo_Log.error(`\nHTTPS (secure) server ERROR`, error);
447
- })
448
- .on('uncaughtException', (ex) => {
449
- fsm_dynamo_1.Dynamo_Log.warn(`\nHTTPS (secure) server uncaughtException`, ex);
523
+ yield new Promise((resolve, reject) => {
524
+ this.systemControls.httpsServer.init = true;
525
+ this.httpsServer
526
+ .listen(this._ports.httpsPort, 'localhost', 0, () => {
527
+ this.systemControls.httpsServer.started = true;
528
+ fsm_dynamo_1.Dynamo_Log.success(`\nHTTPS (secure) server is listening on port ${this._ports.httpsPort}`);
529
+ resolve();
530
+ })
531
+ .on('error', (error) => {
532
+ this.systemControls.httpsServer.started = false;
533
+ this.constructErrors.push(error);
534
+ /* Dynamo_Log.error(`\nHTTPS (secure) server ERROR`, error); */
535
+ reject(new fsm_dynamo_1.Dynamo_Error({
536
+ message: `HTTPS (secure) server ERROR`,
537
+ error: error,
538
+ }));
539
+ })
540
+ .on('uncaughtException', (ex) => {
541
+ fsm_dynamo_1.Dynamo_Log.warn(`\nHTTPS (secure) server uncaughtException`, ex);
542
+ reject(new fsm_dynamo_1.Dynamo_Error({
543
+ message: `HTTPS (secure) server uncaughtException`,
544
+ error: ex,
545
+ }));
546
+ });
450
547
  });
451
548
  }
452
549
  if (this._security && this._security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
453
- this.haveHttpServer = true;
454
- this.httpServer = this.openExpress
455
- .listen(this._ports.httpPort, () => {
456
- this.httpServerStarted = true;
457
- fsm_dynamo_1.Dynamo_Log.success(`\nHTTP (-open-) server is listening on port ${this._ports.httpPort}`);
458
- })
459
- .on('error', (error) => {
460
- this.httpServerStarted = false;
461
- this.constructErrors.push(error);
462
- fsm_dynamo_1.Dynamo_Log.error(`\nHTTP (-open-) server ERROR`, error);
463
- })
464
- .on('uncaughtException', (ex) => {
465
- fsm_dynamo_1.Dynamo_Log.warn(`\nHTTP (-open-) server uncaughtException`, ex);
550
+ this.systemControls.httpServer.init = true;
551
+ yield new Promise((resolve, reject) => {
552
+ this.httpServer = this.openExpress
553
+ .listen(this._ports.httpPort, () => {
554
+ this.systemControls.httpServer.started = true;
555
+ fsm_dynamo_1.Dynamo_Log.success(`\nHTTP (open) server is listening on port ${this._ports.httpPort}`);
556
+ resolve();
557
+ })
558
+ .on('error', (error) => {
559
+ this.systemControls.httpServer.started = false;
560
+ this.constructErrors.push(error);
561
+ fsm_dynamo_1.Dynamo_Log.error(`\nHTTP (open) server ERROR`, error);
562
+ reject(new fsm_dynamo_1.Dynamo_Error({
563
+ message: `HTTP (open) server ERROR`,
564
+ error: error,
565
+ }));
566
+ })
567
+ .on('uncaughtException', (ex) => {
568
+ fsm_dynamo_1.Dynamo_Log.warn(`\nHTTP (open) server uncaughtException`, ex);
569
+ reject(new fsm_dynamo_1.Dynamo_Error({
570
+ message: `HTTP (open) server uncaughtException`,
571
+ error: ex
572
+ }));
573
+ });
466
574
  });
467
575
  }
468
576
  }
@@ -475,70 +583,88 @@ class DynamoNTS_App extends singleton_service_1.DynamoNTS_SingletonService {
475
583
  *
476
584
  */
477
585
  mountSecureRoutes() {
478
- this._routingModules.forEach((module) => {
479
- if (module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
480
- if (this.debugLog) {
481
- console.log(`route mount (secure): ${module.route}`);
482
- }
483
- const existingRoutes = this._routingModules.filter((mod) => mod.route === module.route);
484
- if (1 < existingRoutes.length) {
485
- let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
486
- let errorStack = error.stack.split('\n');
487
- errorStack.splice(1, 4);
488
- error.stack = errorStack.join('\n');
489
- throw error;
586
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
587
+ if (this.logFn && this.deepLog)
588
+ console.log('\nfn:. mountSecureRoutes');
589
+ yield fsm_dynamo_1.Dynamo_Array.asyncForEach(this._routingModules, (module) => tslib_1.__awaiter(this, void 0, void 0, function* () {
590
+ if (module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.open) {
591
+ if (this.logSetup) {
592
+ console.log(`route mount (secure): ${module.route}`);
593
+ }
594
+ const existingRoutes = this._routingModules.filter((mod) => mod.route === module.route);
595
+ if (1 < existingRoutes.length) {
596
+ let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
597
+ let errorStack = error.stack.split('\n');
598
+ errorStack.splice(1, 4);
599
+ error.stack = errorStack.join('\n');
600
+ throw new fsm_dynamo_1.Dynamo_Error({
601
+ message: `ROUTE DUPLICATION: ${module.route}`,
602
+ error: error,
603
+ });
604
+ }
605
+ this.secureExpress.use(module.route, module.secureRouter);
490
606
  }
491
- this.secureExpress.use(module.route, module.secureRouter);
492
- }
607
+ }));
493
608
  });
494
609
  }
495
610
  /**
496
611
  *
497
612
  */
498
613
  mountOpenRoutes() {
499
- this._routingModules.forEach((module) => {
500
- if (module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
501
- if (this.debugLog) {
502
- console.log(`route mount (open): ${module.route}`);
503
- }
504
- const existingRoutes = this._routingModules.filter((mod) => mod.route === module.route);
505
- if (1 < existingRoutes.length) {
506
- let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
507
- let errorStack = error.stack.split('\n');
508
- errorStack.splice(1, 4);
509
- error.stack = errorStack.join('\n');
510
- throw error;
614
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
615
+ if (this.logFn && this.deepLog)
616
+ console.log('\nfn:. mountOpenRoutes');
617
+ yield fsm_dynamo_1.Dynamo_Array.asyncForEach(this._routingModules, (module) => tslib_1.__awaiter(this, void 0, void 0, function* () {
618
+ if (module.security !== route_security_enum_1.DynamoNTS_RouteSecurity.secure) {
619
+ if (this.logSetup) {
620
+ console.log(`route mount (open): ${module.route}`);
621
+ }
622
+ const existingRoutes = this._routingModules.filter((mod) => mod.route === module.route);
623
+ if (1 < existingRoutes.length) {
624
+ let error = new Error(`ROUTE DUPLICATION: ${module.route}`);
625
+ let errorStack = error.stack.split('\n');
626
+ errorStack.splice(1, 4);
627
+ error.stack = errorStack.join('\n');
628
+ throw new fsm_dynamo_1.Dynamo_Error({
629
+ message: `ROUTE DUPLICATION: ${module.route}`,
630
+ error: error,
631
+ });
632
+ }
633
+ this.openExpress.use(module.route, module.openRouter);
511
634
  }
512
- this.openExpress.use(module.route, module.openRouter);
513
- }
635
+ }));
514
636
  });
515
637
  }
516
638
  /**
517
639
  *
518
640
  */
519
641
  setSecurity() {
520
- this._routingModules.forEach((module) => {
521
- if (!module.security) {
522
- fsm_dynamo_1.Dynamo_Log.error(`RoutingModule security is not set for ${module.route}\n`);
523
- }
524
- else if (!this._security) {
525
- this._security = module.security;
526
- }
527
- else if (this._security !== module.security) {
528
- this._security = route_security_enum_1.DynamoNTS_RouteSecurity.both;
529
- }
530
- });
531
- if (!this._security) {
532
- let msg = `Could not set security for the server! (${this.security})`;
533
- msg += '\n RoutingModules:';
642
+ return tslib_1.__awaiter(this, void 0, void 0, function* () {
643
+ if (this.logFn && this.deepLog)
644
+ console.log('\nfn:. setSecurity');
534
645
  this._routingModules.forEach((module) => {
535
- msg += `\n ${module.route} (security: ${module.security})`;
646
+ if (!module.security) {
647
+ fsm_dynamo_1.Dynamo_Log.error(`RoutingModule security is not set for ${module.route}\n`);
648
+ }
649
+ else if (!this._security) {
650
+ this._security = module.security;
651
+ }
652
+ else if (this._security !== module.security) {
653
+ this._security = route_security_enum_1.DynamoNTS_RouteSecurity.both;
654
+ }
536
655
  });
537
- if (this._routingModules.length === 0) {
538
- msg += '\n No RoutingModules found!';
656
+ if (!this._security) {
657
+ let msg = `Could not set security for the server! (${this.security})`;
658
+ msg += '\n RoutingModules:';
659
+ this._routingModules.forEach((module) => {
660
+ msg += `\n ${module.route} (security: ${module.security})`;
661
+ });
662
+ if (this._routingModules.length === 0) {
663
+ msg += '\n - no RoutingModule found -\n';
664
+ }
665
+ fsm_dynamo_1.Dynamo_Log.warn(msg);
539
666
  }
540
- fsm_dynamo_1.Dynamo_Log.warn(msg);
541
- }
667
+ });
542
668
  }
543
669
  }
544
670
  exports.DynamoNTS_App = DynamoNTS_App;