opticore-webapp 1.0.16 → 1.0.18

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.
@@ -0,0 +1,112 @@
1
+ import assert from "assert";
2
+ import { CErrorName } from "opticore-catch-exception-error";
3
+ import { HttpStatusCode } from "opticore-http-response";
4
+ import { TranslationLoader } from "opticore-translator";
5
+
6
+ export const SServerStartError = (err: any) => {
7
+ switch (err.name) {
8
+ case CErrorName.typeError:
9
+ this.javaScriptErrors.typeError(err);
10
+ break;
11
+ case CErrorName.error:
12
+ this.javaScriptErrors.allError(err);
13
+ break;
14
+ case CErrorName.evalError:
15
+ this.javaScriptErrors.evalError(err);
16
+ break;
17
+ case CErrorName.referenceError:
18
+ this.javaScriptErrors.referenceError(err);
19
+ break;
20
+ case CErrorName.rangeError:
21
+ this.javaScriptErrors.rangeError(err);
22
+ break;
23
+ case CErrorName.syntaxError:
24
+ this.javaScriptErrors.syntaxError(err);
25
+ break;
26
+ case CErrorName.uriError:
27
+ this.javaScriptErrors.uRIError(err);
28
+ break;
29
+ case CErrorName.eacces:
30
+ this.systemErrors.eAcces(err);
31
+ break;
32
+ case CErrorName.eaddrinuse:
33
+ this.systemErrors.eAddrInUse(err);
34
+ break;
35
+ case CErrorName.econnrefused:
36
+ this.systemErrors.eConnRefused(err);
37
+ break;
38
+ case CErrorName.econnreset:
39
+ this.systemErrors.eConnReset(err);
40
+ break;
41
+ case CErrorName.eexist:
42
+ this.systemErrors.eExist(err);
43
+ break;
44
+ case CErrorName.eisdir:
45
+ this.systemErrors.eIsDir(err);
46
+ break;
47
+ case CErrorName.emfile:
48
+ this.systemErrors.eMFile(err);
49
+ break;
50
+ case CErrorName.enoent:
51
+ this.systemErrors.eNoEnt(err);
52
+ break;
53
+ case CErrorName.enotdir:
54
+ this.systemErrors.eNotDir(err);
55
+ break;
56
+ case CErrorName.enotEmpty:
57
+ this.systemErrors.eNotEmpty(err);
58
+ break;
59
+ case CErrorName.eperm:
60
+ this.systemErrors.ePerm(err);
61
+ break;
62
+ case CErrorName.epipe:
63
+ this.systemErrors.ePipe(err);
64
+ break;
65
+ case CErrorName.etimedout:
66
+ this.systemErrors.eTimedOut(err);
67
+ break;
68
+ case CErrorName.assertionError:
69
+ this.stackTrace = this.traceError(err.message, err.name, HttpStatusCode.NOT_ACCEPTABLE);
70
+ err instanceof assert.AssertionError
71
+ ? this.logger.error(
72
+ TranslationLoader.t(this.stackTrace.name, this.localLanguage),
73
+ "AssertionError",
74
+ this.stackTrace.name,
75
+ this.stackTrace.stack,
76
+ HttpStatusCode.INTERNAL_SERVER_ERROR)
77
+ : this.logger.error(
78
+ TranslationLoader.t(this.stackTrace.name, this.localLanguage),
79
+ "Another Error",
80
+ this.stackTrace.name,
81
+ this.stackTrace.stack,
82
+ HttpStatusCode.INTERNAL_SERVER_ERROR);
83
+ break;
84
+ case CErrorName.errOsslEvpUnsupported:
85
+ this.openSSLErrors.errOsSLEvpUnsupported(err);
86
+ break;
87
+ case CErrorName.errOsslBadDecrypt:
88
+ this.openSSLErrors.errOsSLBadDecrypt(err);
89
+ break;
90
+ case CErrorName.errOsslWrongFinalBlockLength:
91
+ this.openSSLErrors.errOsSLWrongFinalBlockLength(err);
92
+ break;
93
+ case CErrorName.errInvalidArgType:
94
+ this.internalErrors.errInvalidArgType(err);
95
+ break;
96
+ case CErrorName.errInvalidCallback:
97
+ this.internalErrors.errInvalidCallback(err);
98
+ break;
99
+ case CErrorName.errHttpHeadersSent:
100
+ this.internalErrors.errHttpHeadersSent(err);
101
+ break;
102
+ case CErrorName.errStreamDestroyed:
103
+ this.internalErrors.errStreamDestroyed(err);
104
+ break;
105
+ case CErrorName.errTlsCertAltnameInvalid:
106
+ this.internalErrors.errTlsCertAltNameInvalid(err);
107
+ break;
108
+ case CErrorName.errUnsupportedEsmUrlScheme:
109
+ this.internalErrors.errUnsupportedEsmUrlScheme(err);
110
+ break;
111
+ }
112
+ }
@@ -12,15 +12,16 @@ import {
12
12
  /**
13
13
  *
14
14
  */
15
- export function eventProcessHandler(): void {
15
+ export function eventProcessHandler(localeLanguage: string): void {
16
16
  const errorEmitter = new EventEmitter();
17
17
  const app = express();
18
+ const serverListenEvent = new ServerListenEventError(localeLanguage);
18
19
 
19
20
  /**
20
21
  * Listener for error events
21
22
  */
22
23
  errorEmitter.on(eventName.error, (error: Error): void => {
23
- ServerListenEventError.__init().listenerError(error);
24
+ serverListenEvent.listenerError(error);
24
25
  });
25
26
 
26
27
  /**
@@ -29,7 +30,7 @@ export function eventProcessHandler(): void {
29
30
  */
30
31
  process.on(event.beforeExit, (code: number): void => {
31
32
  setTimeout((): void => {
32
- ServerListenEventError.__init().processBeforeExit(code);
33
+ serverListenEvent.processBeforeExit(code);
33
34
  }, 100);
34
35
  });
35
36
 
@@ -37,75 +38,75 @@ export function eventProcessHandler(): void {
37
38
  *
38
39
  */
39
40
  process.on(event.disconnect, (): void => {
40
- ServerListenEventError.__init().processDisconnected();
41
+ serverListenEvent.processDisconnected();
41
42
  });
42
43
 
43
44
  /**
44
45
  *
45
46
  */
46
47
  process.on(event.exit, (code: number): void => {
47
- ServerListenEventError.__init().exited(code);
48
+ serverListenEvent.exited(code);
48
49
  });
49
50
 
50
51
  /**
51
52
  *
52
53
  */
53
54
  process.on(event.rejectionHandled, (promise: Promise<any>): void => {
54
- ServerListenEventError.__init().promiseRejectionHandled(promise);
55
+ serverListenEvent.promiseRejectionHandled(promise);
55
56
  });
56
57
 
57
58
  /**
58
59
  *
59
60
  */
60
61
  process.on(event.uncaughtException, (error: any): void => {
61
- ServerListenEventError.__init().uncaughtException(error);
62
+ serverListenEvent.uncaughtException(error);
62
63
  });
63
64
 
64
65
  /**
65
66
  *
66
67
  */
67
68
  process.on(event.uncaughtExceptionMonitor, (error: any): void => {
68
- ServerListenEventError.__init().uncaughtExceptionMonitor(error);
69
+ serverListenEvent.uncaughtExceptionMonitor(error);
69
70
  });
70
71
 
71
72
  /**
72
73
  *
73
74
  */
74
75
  process.on(event.unhandledRejection, (reason: any, promise: Promise<any>): void => {
75
- ServerListenEventError.__init().unhandledRejection(reason, promise);
76
+ serverListenEvent.unhandledRejection(reason, promise);
76
77
  });
77
78
 
78
79
  /**
79
80
  *
80
81
  */
81
82
  process.on(event.warning, (warning: any): void => {
82
- ServerListenEventError.__init().warning(warning);
83
+ serverListenEvent.warning(warning);
83
84
  });
84
85
  process.on(event.message, (message: any): void => {
85
- ServerListenEventError.__init().message(message);
86
+ serverListenEvent.message(message);
86
87
  });
87
88
 
88
89
  /**
89
90
  *
90
91
  */
91
92
  process.on(event.multipleResolves, (type: string, promise: Promise<any>, reason: any): void => {
92
- ServerListenEventError.__init().multipleResolves(type, promise, reason);
93
+ serverListenEvent.multipleResolves(type, promise, reason);
93
94
  });
94
95
 
95
96
  /**
96
97
  * Handle specific signals
97
98
  */
98
99
  process.on(event.sigint, (): void => {
99
- ServerListenEventError.__init().processInterrupted();
100
+ serverListenEvent.processInterrupted();
100
101
  });
101
102
  process.on(event.sigterm, (signal: any): void => {
102
- ServerListenEventError.__init().sigtermSignalReceived(signal);
103
+ serverListenEvent.sigtermSignalReceived(signal);
103
104
  });
104
105
 
105
106
  /**
106
107
  * Express error-handling middleware
107
108
  */
108
109
  app.use((err: Error, req: Request, res: Response, next: NextFunction): void => {
109
- ServerListenEventError.__init().expressErrorHandlingMiddleware(errorEmitter, err, req, res, next);
110
+ serverListenEvent.expressErrorHandlingMiddleware(errorEmitter, err, req, res, next);
110
111
  });
111
112
  }
@@ -2,21 +2,22 @@ import colors from "ansi-colors";
2
2
  import { Router } from "opticore-express";
3
3
  import { LogMessageUtils } from "@webApp/core/helpers/logMessage.utils";
4
4
  import { dateTimeFormattedUtils } from "@webApp/core/helpers/dateTimeFormatted.utils";
5
- import { CLocal, TranslationLoader } from "opticore-translator";
5
+ import { TranslationLoader } from "opticore-translator";
6
+ import { LoggerCore } from "opticore-logger";
6
7
 
7
8
 
8
9
 
9
- export function modulesLoadedUtils(allAppRoutes: Router[], dbConChecker: () => void): void {
10
+ export function modulesLoadedUtils(allAppRoutes: Router[], dbConChecker: () => void, localeLanguage: string): void {
10
11
  LogMessageUtils.success(
11
- `${TranslationLoader.t("kernel", CLocal)}`,
12
- `${TranslationLoader.t("loadKernel", CLocal)}`,
13
- `${TranslationLoader.t("moduleAppLoaded", CLocal)}`
12
+ `${TranslationLoader.t("kernel", localeLanguage, loggerConfig)}`,
13
+ `${TranslationLoader.t("loadKernel", localeLanguage, loggerConfig)}`,
14
+ `${TranslationLoader.t("moduleAppLoaded", localeLanguage, loggerConfig)}`
14
15
  );
15
- console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", CLocal)}`)} ${colors.green(`${TranslationLoader.t("kernel", CLocal)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("serverSide", CLocal)}`)}`)} ${TranslationLoader.t("hasBeenLoadedSuccessfully", CLocal)} ${colors.green(`✔`)}`);
16
+ console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", localeLanguage, loggerConfig)}`)} ${colors.green(`${TranslationLoader.t("kernel", localeLanguage, loggerConfig)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("serverSide", localeLanguage, loggerConfig)}`)}`)} ${TranslationLoader.t("hasBeenLoadedSuccessfully", localeLanguage, loggerConfig)} ${colors.green(`✔`)}`);
16
17
  allAppRoutes
17
- ? console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", CLocal)}`)} ${colors.green(`${TranslationLoader.t("kernel", CLocal)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("routerService", CLocal)}`)} `)} ${colors.green(`✔`)}`)
18
- : console.log(`${colors.red(`✘`)} ${colors.bgRed(` ${colors.bold(`${colors.white(` ${TranslationLoader.t("registerRoutes", CLocal)} `)}`)} `)} | ${dateTimeFormattedUtils} | [ ${colors.red(`${colors.bold(` ${TranslationLoader.t("fail", CLocal)} `)}`)} ] | [ ${colors.bold(` ${TranslationLoader.t("loading", CLocal)} `)} ] - ${colors.red(` ${TranslationLoader.t("routers", CLocal)} `)} - ${TranslationLoader.t("registerLoadingFailed", CLocal)} `);
18
+ ? console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", localeLanguage, loggerConfig)}`)} ${colors.green(`${TranslationLoader.t("kernel", localeLanguage, loggerConfig)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("routerService", localeLanguage, loggerConfig)}`)} `)} ${colors.green(`✔`)}`)
19
+ : console.log(`${colors.red(`✘`)} ${colors.bgRed(` ${colors.bold(`${colors.white(` ${TranslationLoader.t("registerRoutes", localeLanguage, loggerConfig)} `)}`)} `)} | ${dateTimeFormattedUtils} | [ ${colors.red(`${colors.bold(` ${TranslationLoader.t("fail", localeLanguage, loggerConfig)} `)}`)} ] | [ ${colors.bold(` ${TranslationLoader.t("loading", localeLanguage, loggerConfig)} `)} ] - ${colors.red(` ${TranslationLoader.t("routers", localeLanguage, loggerConfig)} `)} - ${TranslationLoader.t("registerLoadingFailed", localeLanguage, loggerConfig)} `);
19
20
  typeof dbConChecker == "function"
20
- ? console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", CLocal)}`)} ${colors.green(`${TranslationLoader.t("kernel", CLocal)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("dbConnChecker", CLocal)}`)}`)} ${TranslationLoader.t("hasBeenLoadedSuccessfully", CLocal)} ${colors.green(`✔`)}`)
21
+ ? console.log(`${colors.whiteBright(` ${TranslationLoader.t("content", localeLanguage, loggerConfig)}`)} ${colors.green(`${TranslationLoader.t("kernel", localeLanguage, loggerConfig)} :`)} ${colors.cyan(`${colors.bold(`${TranslationLoader.t("dbConnChecker", localeLanguage, loggerConfig)}`)}`)} ${TranslationLoader.t("hasBeenLoadedSuccessfully", localeLanguage, loggerConfig)} ${colors.green(`✔`)}`)
21
22
  : "";
22
23
  }
@@ -1,38 +1,57 @@
1
1
  import * as path from "path";
2
+ import { createRequire } from "module";
2
3
  import corsOrigin, { CorsOptions } from "cors";
3
4
  import { Server as serverWebApp } from "http";
4
5
  import { IncomingMessage, ServerResponse } from "node:http";
5
- import { fileURLToPath } from 'url';
6
6
 
7
7
  import {
8
8
  CEventNameError as eventName,
9
+ ServerListenEventError,
10
+ CErrorName,
11
+ JavaScriptErrors,
12
+ InternalErrors,
13
+ AssertionErrors,
14
+ OpenSSLErrors,
15
+ SystemErrors,
9
16
  StackTraceError,
10
- ServerListenEventError as eventErrorOnListeningServer
11
17
  } from "opticore-catch-exception-error";
12
- import { Application, express } from "opticore-express";
18
+ import { express } from "opticore-express";
13
19
  import { getEnvVariable } from "opticore-env-access";
14
20
  import { requestCallsEvent } from "opticore-requests-called-events";
15
- import { HttpStatusCode } from "opticore-http-response";
21
+ import { TranslationLoader } from "opticore-translator";
22
+ import { LoggerCore } from "opticore-logger";
16
23
 
17
24
  import { IRouteDefinition } from "@webApp/domains/interfaces/routeDefinition.interface";
18
25
  import { eventProcessHandler } from "@webApp/core/handlers/eventProcess.handler";
19
26
  import { CoreService } from "@webApp/application/service/core.service";
20
27
  import { dateTimeFormattedUtils as currentDate } from "@webApp/core/helpers/dateTimeFormatted.utils";
21
- import { TranslationLoader } from "opticore-translator";
22
-
28
+ import { SServerStartError } from "@webApp/application/service/serverStartError.service";
23
29
 
24
30
 
25
31
  export class WebServerCore {
26
32
  private serverUtility: CoreService = new CoreService();
33
+
27
34
  private expressApp = express();
35
+
36
+ private readonly localLanguage: string;
37
+ private readonly loggerConfig: LoggerCore;
28
38
  private readonly routerExpressApp: express.Application;
29
39
  private readonly port: number;
30
40
  private readonly host: string;
31
41
 
32
- constructor(app: express.Application, corsOriginOptions?: Partial<CorsOptions>) {
42
+ private serverListenEvent: ServerListenEventError;
43
+ private javaScriptErrors: JavaScriptErrors;
44
+ private internalErrors: InternalErrors;
45
+ private assertionErrors: AssertionErrors;
46
+ private openSSLErrors: OpenSSLErrors;
47
+ private systemErrors: SystemErrors;
48
+
49
+ constructor(app: express.Application, loggerConfig: LoggerCore, localLanguage: string, corsOriginOptions?: Partial<CorsOptions>) {
33
50
  this.port = Number(getEnvVariable.appPort);
34
51
  this.host = getEnvVariable.appHost;
35
52
  this.routerExpressApp = app;
53
+ this.loggerConfig = loggerConfig;
54
+ this.localLanguage = localLanguage;
36
55
 
37
56
  this.expressApp.use(express.json());
38
57
  this.expressApp.use(express.raw());
@@ -40,10 +59,18 @@ export class WebServerCore {
40
59
  this.expressApp.use(express.urlencoded({ extended: true }));
41
60
  this.expressApp.use(corsOrigin(corsOriginOptions));
42
61
 
43
- this.stackTraceErrorHandling();
62
+ this.serverListenEvent = new ServerListenEventError(localLanguage);
63
+ this.javaScriptErrors = new JavaScriptErrors(localLanguage);
64
+ this.assertionErrors = new AssertionErrors(localLanguage);
65
+ this.internalErrors = new InternalErrors(localLanguage);
66
+ this.openSSLErrors = new OpenSSLErrors(localLanguage);
67
+ this.systemErrors = new SystemErrors(localLanguage);
68
+
69
+ this.stackTraceErrorHandling(localLanguage);
44
70
  this.translationWebAppLoader();
45
71
  }
46
72
 
73
+
47
74
  public onStartServer(routers: { featureRoute: IRouteDefinition[] }[]) {
48
75
  return this.expressApp.listen(
49
76
  this.port,
@@ -51,29 +78,29 @@ export class WebServerCore {
51
78
  (): void => {
52
79
  try {
53
80
  if (this.host === "" && this.port === 0) {
54
- eventErrorOnListeningServer.__init().hostPortUndefined();
81
+ this.serverListenEvent.hostPortUndefined(this.port);
55
82
  } else if (this.host === "") {
56
- eventErrorOnListeningServer.__init().hostUndefined();
83
+ this.serverListenEvent.hostUndefined(this.host);
57
84
  } else if (this.port === 0) {
58
- eventErrorOnListeningServer.__init().portUndefined();
85
+ this.serverListenEvent.portUndefined();
59
86
  } else {
60
87
  this.registerRoutes(routers);
61
88
  }
62
89
  } catch (err: any) {
63
- this.traceError(err.message, "Error", HttpStatusCode.NOT_ACCEPTABLE);
90
+ SServerStartError(err);
64
91
  }
65
92
  }
66
93
  );
67
94
  }
68
95
 
69
96
  //, kernelModule: KernelModuleType
70
- public onListeningOnServerEvent(serverWeb: serverWebApp): void {
97
+ public onListeningOnServerEvent(serverWeb: serverWebApp, localLanguage: string): void {
71
98
  serverWeb.on(eventName.error, (err: Error): void => {
72
- eventErrorOnListeningServer.__init().onEventError(err);
99
+ this.serverListenEvent.onEventError(err);
73
100
  }).on(eventName.close, (): void => {
74
- eventErrorOnListeningServer.__init().serverClosing();
101
+ this.serverListenEvent.serverClosing();
75
102
  }).on(eventName.drop, (): void => {
76
- eventErrorOnListeningServer.__init().dropNewConnection();
103
+ this.serverListenEvent.dropNewConnection();
77
104
  }).on(eventName.listening, (): void => {
78
105
  this.infoWebApp();
79
106
  //this.serverUtility.coreListenerEventLoaderModuleService(kernelModule);
@@ -87,10 +114,11 @@ export class WebServerCore {
87
114
  }
88
115
 
89
116
  private translationWebAppLoader(): void {
90
- const __filename: string = fileURLToPath(import.meta.url);
91
- const __dirname: string = path.dirname(__filename);
92
- const translateMsgJsonFilePath: string = path.join(__dirname, '../../../', 'utils', 'translations');
93
- TranslationLoader.loadTranslations(translateMsgJsonFilePath, getEnvVariable.defaultLocal);
117
+ const require: NodeJS.Require = createRequire(import.meta.url);
118
+ const packagePath: string = path.dirname(require.resolve("opticore-webapp"));
119
+
120
+ const translateMsgJsonFilePath: string = path.join(packagePath, "utils", "translations");
121
+ TranslationLoader.loadTranslations(translateMsgJsonFilePath);
94
122
  }
95
123
  private registerRoutes(allFeatureRoutes: { featureRoute: IRouteDefinition[] }[]): void {
96
124
  allFeatureRoutes.map((router: {featureRoute: IRouteDefinition[]} ): void => {
@@ -99,8 +127,8 @@ export class WebServerCore {
99
127
  });
100
128
  });
101
129
  }
102
- private stackTraceErrorHandling(): void {
103
- eventProcessHandler();
130
+ private stackTraceErrorHandling(localLanguage: string): void {
131
+ eventProcessHandler(localLanguage);
104
132
  }
105
133
  private infoWebApp(): void {
106
134
  this.serverUtility.infoServer(
@@ -114,7 +142,4 @@ export class WebServerCore {
114
142
  this.serverUtility.getUsageMemory().system
115
143
  );
116
144
  }
117
- private traceError(props: string, name: string, status: number): StackTraceError {
118
- return new StackTraceError(props, name, status, true);
119
- }
120
145
  }