@ti-engine/core 1.6.1 → 1.7.2

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 (33) hide show
  1. package/CHANGELOG.md +383 -364
  2. package/LICENSE.md +321 -321
  3. package/README.md +597 -548
  4. package/bin/localization/labels.json +122 -122
  5. package/bin/settings.json +41 -41
  6. package/bin/start-instance.js +164 -156
  7. package/components/auditing.js +191 -191
  8. package/components/connection-observer.js +72 -72
  9. package/components/definitions.types.js +248 -248
  10. package/components/exchange/default/default-message-exchange.js +136 -136
  11. package/components/exchange/default/default-message-receiver.js +101 -101
  12. package/components/exchange/default/default-message-sender.js +100 -100
  13. package/components/exchange/message-dispatcher.js +168 -168
  14. package/components/exchange/message-exchange.js +449 -449
  15. package/components/exchange/message-handler.js +235 -234
  16. package/components/exchange/message-memory-cache.js +190 -190
  17. package/components/exchange/message-observer.js +126 -126
  18. package/components/exchange/message-receiver.js +181 -181
  19. package/components/exchange/message-sender.js +143 -143
  20. package/components/exchange/message-tracer.js +212 -212
  21. package/components/service-caller.js +370 -370
  22. package/components/service-consumer.js +131 -131
  23. package/components/service-executor.js +278 -278
  24. package/components/service-instance.js +316 -316
  25. package/components/service-provider.js +251 -251
  26. package/integrations/redis-integration.js +591 -591
  27. package/package.json +89 -90
  28. package/utils/cache.js +772 -772
  29. package/utils/config.js +103 -103
  30. package/utils/exceptions.js +368 -368
  31. package/utils/localization.js +298 -298
  32. package/utils/logger.js +82 -82
  33. package/utils/tools.js +632 -632
@@ -1,157 +1,165 @@
1
- /*
2
- * The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
3
- * Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
4
- * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5
- * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6
- * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
7
- */
8
-
9
- "use strict";
10
-
11
- const path = require( "path" );
12
-
13
- // Load any ENV variables defined in a .env file - before including any framework files:
14
- // - If an env file path is provided via process arguments, use it; otherwise, default to CWD/.env
15
- const envFilePath = ( () => {
16
- let envPath = path.join( process.cwd(), ".env" );
17
- const argv = Array.isArray( process.argv ) ? process.argv.slice( 2 ) : [];
18
- const aliases = [ "--env", "--env-file", "--dotenv", "--dotenv-path", "-e" ];
19
- for ( const key of aliases ) {
20
- const idx = argv.indexOf( key );
21
- if ( idx !== -1 && idx + 1 < argv.length ) {
22
- const value = argv[ idx + 1 ];
23
- if ( typeof value === "string" && !value.startsWith( "-" ) ) {
24
- envPath = path.join( process.cwd(), value.trim() );
25
- break;
26
- }
27
- }
28
- }
29
- return envPath;
30
- } )();
31
-
32
- require( "@dotenvx/dotenvx" ).config( { path: envFilePath, quiet: true } );
33
-
34
- const tools = require( "#tools" );
35
- const logger = require( "#logger" );
36
-
37
- // Configure the current instance variables before requiring any platform modules and store the necessary ones in memory cache:
38
- process.env.TI_INSTANCE_ID = "ti-" + tools.getUUID();
39
- process.env.TI_INSTANCE_CLASS = process.env.TI_INSTANCE_CLASS || "";
40
-
41
- // Derive a safe default service domain name if not explicitly provided:
42
- // - Normalize path separators
43
- // - Strip directory and file extension
44
- const defaultNameFromClass = ( () => {
45
- try {
46
- const normalized = ( process.env.TI_INSTANCE_CLASS || "" ).replace( /\\/g, "/" );
47
- const base = path.posix.basename( normalized );
48
- const name = path.parse( base ).name;
49
- return name || "";
50
- } catch {
51
- return "";
52
- }
53
- } )();
54
-
55
- process.env.TI_INSTANCE_NAME = process.env.TI_INSTANCE_NAME || defaultNameFromClass;
56
-
57
- // Configure the process termination handlers to ensure a graceful shutdown:
58
-
59
- /**
60
- * Will be used to gracefully shut down the instance.
61
- * <br/>
62
- * NOTE: Will be overridden below after the instance creation.
63
- *
64
- * @method
65
- * @param {number} exitCode
66
- * @abstract
67
- * @private
68
- */
69
- let shutDownInstance = ( exitCode ) => {
70
- process.exit( exitCode );
71
- };
72
-
73
- // This event will handle the process termination (Ctrl + C):
74
- process.on( "SIGINT", () => {
75
- logger.log( `SIGINT event detected in main instance process.`, logger.logSeverity.NOTICE );
76
- shutDownInstance( 0 );
77
- } );
78
-
79
- // This event will handle the process termination via terminal hangup (CMD close) - not delivered on all platforms:
80
- process.on( "SIGHUP", () => {
81
- logger.log( `SIGHUP event detected in main instance process.`, logger.logSeverity.NOTICE );
82
- shutDownInstance( 0 );
83
- } );
84
-
85
- // This event will handle the process termination:
86
- process.on( "SIGTERM", () => {
87
- logger.log( `SIGTERM event detected in main instance process.`, logger.logSeverity.NOTICE );
88
- shutDownInstance( 0 );
89
- } );
90
-
91
- // Handle Windows console break (Ctrl + Break):
92
- process.on( "SIGBREAK", () => {
93
- logger.log( `SIGBREAK event detected in main instance process.`, logger.logSeverity.NOTICE );
94
- shutDownInstance( 0 );
95
- } );
96
-
97
- // Configure the process general error handlers:
98
-
99
- process.on( "unhandledRejection", ( reason ) => {
100
- // Check if the fail-fast behavior has been forcefully disabled:
101
- const failFastDisabled = tools.toBool( process.env.TI_FAIL_FAST_ON_UNHANDLED_OFF || "" );
102
- logger.log( `Unhandled promise rejection identified! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
103
- reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
104
- } );
105
- if ( failFastDisabled !== true ) {
106
- setImmediate( () => process.exit( 1 ) );
107
- }
108
- } );
109
-
110
- process.on( "multipleResolves", ( type, promise, reason ) => {
111
- logger.log( `Multiple promise resolves detected! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
112
- type,
113
- reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
114
- } );
115
- } );
116
-
117
- process.on( "uncaughtException", ( error ) => {
118
- logger.log( `Some nasty and uncaught error just occurred in the application!`, logger.logSeverity.ALERT, error );
119
- setImmediate( () => process.exit( 1 ) );
120
- } );
121
-
122
- // Start the instance:
123
- try {
124
- const serviceConstructor = require( path.join( process.cwd(), process.env.TI_INSTANCE_CLASS ) );
125
- const serviceConfigPath = process.env.TI_INSTANCE_CONFIG;
126
- /** @type ServiceConfiguration */
127
- let serviceConfig;
128
- if ( serviceConfigPath ) {
129
- serviceConfig = require( path.join( process.cwd(), process.env.TI_INSTANCE_CONFIG ) );
130
- }
131
- /** @type ServiceInstance */
132
- const mainInstance = new serviceConstructor( process.env.TI_INSTANCE_NAME, serviceConfig );
133
-
134
- /** @override */
135
- shutDownInstance = ( exitCode ) => {
136
- mainInstance.stop().then( () => {
137
- setImmediate( () => process.exit( exitCode ) );
138
- } ).catch( ( error ) => {
139
- logger.log( `Error occurred during shut down of instance '${ process.env.TI_INSTANCE_ID }'! Exit code changed from '${ exitCode }' to '1'.`, logger.logSeverity.ERROR, error );
140
- setImmediate( () => process.exit( 1 ) );
141
- } );
142
- };
143
-
144
- if ( mainInstance.isServiceInstance ) {
145
- logger.log( `Starting new instance of type '${ process.env.TI_INSTANCE_NAME }' with instance ID '${ process.env.TI_INSTANCE_ID }'.`, logger.logSeverity.NOTICE );
146
- mainInstance.start().catch( ( error ) => {
147
- logger.log( `Error detected during instance '${ process.env.TI_INSTANCE_ID }' startup!`, logger.logSeverity.ALERT, error );
148
- setImmediate( () => process.exit( 1 ) );
149
- } );
150
- } else {
151
- logger.log( `Attempting to start a module that does not implement the ServiceInstance abstract class!`, logger.logSeverity.CRITICAL );
152
- setImmediate( () => process.exit( 1 ) );
153
- }
154
- } catch ( error ) {
155
- logger.log( `Error detected in the instance startup script!`, logger.logSeverity.ALERT, error );
156
- setImmediate( () => process.exit( 1 ) );
1
+ /*
2
+ * The ti-engine is an open source, free to use—both for personal and commercial projects—framework for the creation of microservice-based solutions using node.js.
3
+ * Copyright © 2021-2025 Boris Kostadinov <kostadinov.boris@gmail.com>
4
+ * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
5
+ * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
6
+ * You should have received a copy of the GNU General Public License along with this program. If not, see <https://www.gnu.org/licenses/>.
7
+ */
8
+
9
+ "use strict";
10
+
11
+ const path = require( "path" );
12
+
13
+ // Load any ENV variables defined in a .env file - before including any framework files:
14
+ // - If an env file path is provided via process arguments, use it; otherwise, default to CWD/.env
15
+ const envFilePath = ( () => {
16
+ let envPath = path.join( process.cwd(), ".env" );
17
+ const argv = Array.isArray( process.argv ) ? process.argv.slice( 2 ) : [];
18
+ const aliases = [ "--env", "--env-file", "--dotenv", "--dotenv-path", "-e" ];
19
+ for ( const key of aliases ) {
20
+ const idx = argv.indexOf( key );
21
+ if ( idx !== -1 && idx + 1 < argv.length ) {
22
+ const value = argv[ idx + 1 ];
23
+ if ( typeof value === "string" && !value.startsWith( "-" ) ) {
24
+ envPath = path.join( process.cwd(), value.trim() );
25
+ break;
26
+ }
27
+ }
28
+ }
29
+ return envPath;
30
+ } )();
31
+
32
+ // Load the resolved .env file using the native Node loader (Node >= 20.12). A missing file is not fatal
33
+ // environment variables may be supplied entirely by the OS/container. Existing process.env values are NOT overridden.
34
+ try {
35
+ process.loadEnvFile( envFilePath );
36
+ } catch ( error ) {
37
+ if ( error.code !== "ENOENT" ) {
38
+ throw error;
39
+ }
40
+ }
41
+
42
+ const tools = require( "#tools" );
43
+ const logger = require( "#logger" );
44
+
45
+ // Configure the current instance variables before requiring any platform modules and store the necessary ones in memory cache:
46
+ process.env.TI_INSTANCE_ID = "ti-" + tools.getUUID();
47
+ process.env.TI_INSTANCE_CLASS = process.env.TI_INSTANCE_CLASS || "";
48
+
49
+ // Derive a safe default service domain name if not explicitly provided:
50
+ // - Normalize path separators
51
+ // - Strip directory and file extension
52
+ const defaultNameFromClass = ( () => {
53
+ try {
54
+ const normalized = ( process.env.TI_INSTANCE_CLASS || "" ).replace( /\\/g, "/" );
55
+ const base = path.posix.basename( normalized );
56
+ const name = path.parse( base ).name;
57
+ return name || "";
58
+ } catch {
59
+ return "";
60
+ }
61
+ } )();
62
+
63
+ process.env.TI_INSTANCE_NAME = process.env.TI_INSTANCE_NAME || defaultNameFromClass;
64
+
65
+ // Configure the process termination handlers to ensure a graceful shutdown:
66
+
67
+ /**
68
+ * Will be used to gracefully shut down the instance.
69
+ * <br/>
70
+ * NOTE: Will be overridden below after the instance creation.
71
+ *
72
+ * @method
73
+ * @param {number} exitCode
74
+ * @abstract
75
+ * @private
76
+ */
77
+ let shutDownInstance = ( exitCode ) => {
78
+ process.exit( exitCode );
79
+ };
80
+
81
+ // This event will handle the process termination (Ctrl + C):
82
+ process.on( "SIGINT", () => {
83
+ logger.log( `SIGINT event detected in main instance process.`, logger.logSeverity.NOTICE );
84
+ shutDownInstance( 0 );
85
+ } );
86
+
87
+ // This event will handle the process termination via terminal hangup (CMD close) - not delivered on all platforms:
88
+ process.on( "SIGHUP", () => {
89
+ logger.log( `SIGHUP event detected in main instance process.`, logger.logSeverity.NOTICE );
90
+ shutDownInstance( 0 );
91
+ } );
92
+
93
+ // This event will handle the process termination:
94
+ process.on( "SIGTERM", () => {
95
+ logger.log( `SIGTERM event detected in main instance process.`, logger.logSeverity.NOTICE );
96
+ shutDownInstance( 0 );
97
+ } );
98
+
99
+ // Handle Windows console break (Ctrl + Break):
100
+ process.on( "SIGBREAK", () => {
101
+ logger.log( `SIGBREAK event detected in main instance process.`, logger.logSeverity.NOTICE );
102
+ shutDownInstance( 0 );
103
+ } );
104
+
105
+ // Configure the process general error handlers:
106
+
107
+ process.on( "unhandledRejection", ( reason ) => {
108
+ // Check if the fail-fast behavior has been forcefully disabled:
109
+ const failFastDisabled = tools.toBool( process.env.TI_FAIL_FAST_ON_UNHANDLED_OFF || "" );
110
+ logger.log( `Unhandled promise rejection identified! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
111
+ reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
112
+ } );
113
+ if ( failFastDisabled !== true ) {
114
+ setImmediate( () => process.exit( 1 ) );
115
+ }
116
+ } );
117
+
118
+ process.on( "multipleResolves", ( type, promise, reason ) => {
119
+ logger.log( `Multiple promise resolves detected! Make sure this isn't a software bug.`, logger.logSeverity.WARNING, {
120
+ type,
121
+ reason: tools.errorToJSON && reason instanceof Error ? tools.errorToJSON( reason ) : reason
122
+ } );
123
+ } );
124
+
125
+ process.on( "uncaughtException", ( error ) => {
126
+ logger.log( `Some nasty and uncaught error just occurred in the application!`, logger.logSeverity.ALERT, error );
127
+ setImmediate( () => process.exit( 1 ) );
128
+ } );
129
+
130
+ // Start the instance:
131
+ try {
132
+ const serviceConstructor = require( path.join( process.cwd(), process.env.TI_INSTANCE_CLASS ) );
133
+ const serviceConfigPath = process.env.TI_INSTANCE_CONFIG;
134
+ /** @type ServiceConfiguration */
135
+ let serviceConfig;
136
+ if ( serviceConfigPath ) {
137
+ serviceConfig = require( path.join( process.cwd(), process.env.TI_INSTANCE_CONFIG ) );
138
+ }
139
+ /** @type ServiceInstance */
140
+ const mainInstance = new serviceConstructor( process.env.TI_INSTANCE_NAME, serviceConfig );
141
+
142
+ /** @override */
143
+ shutDownInstance = ( exitCode ) => {
144
+ mainInstance.stop().then( () => {
145
+ setImmediate( () => process.exit( exitCode ) );
146
+ } ).catch( ( error ) => {
147
+ logger.log( `Error occurred during shut down of instance '${ process.env.TI_INSTANCE_ID }'! Exit code changed from '${ exitCode }' to '1'.`, logger.logSeverity.ERROR, error );
148
+ setImmediate( () => process.exit( 1 ) );
149
+ } );
150
+ };
151
+
152
+ if ( mainInstance.isServiceInstance ) {
153
+ logger.log( `Starting new instance of type '${ process.env.TI_INSTANCE_NAME }' with instance ID '${ process.env.TI_INSTANCE_ID }'.`, logger.logSeverity.NOTICE );
154
+ mainInstance.start().catch( ( error ) => {
155
+ logger.log( `Error detected during instance '${ process.env.TI_INSTANCE_ID }' startup!`, logger.logSeverity.ALERT, error );
156
+ setImmediate( () => process.exit( 1 ) );
157
+ } );
158
+ } else {
159
+ logger.log( `Attempting to start a module that does not implement the ServiceInstance abstract class!`, logger.logSeverity.CRITICAL );
160
+ setImmediate( () => process.exit( 1 ) );
161
+ }
162
+ } catch ( error ) {
163
+ logger.log( `Error detected in the instance startup script!`, logger.logSeverity.ALERT, error );
164
+ setImmediate( () => process.exit( 1 ) );
157
165
  }