@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.
- package/CHANGELOG.md +383 -364
- package/LICENSE.md +321 -321
- package/README.md +597 -548
- package/bin/localization/labels.json +122 -122
- package/bin/settings.json +41 -41
- package/bin/start-instance.js +164 -156
- package/components/auditing.js +191 -191
- package/components/connection-observer.js +72 -72
- package/components/definitions.types.js +248 -248
- package/components/exchange/default/default-message-exchange.js +136 -136
- package/components/exchange/default/default-message-receiver.js +101 -101
- package/components/exchange/default/default-message-sender.js +100 -100
- package/components/exchange/message-dispatcher.js +168 -168
- package/components/exchange/message-exchange.js +449 -449
- package/components/exchange/message-handler.js +235 -234
- package/components/exchange/message-memory-cache.js +190 -190
- package/components/exchange/message-observer.js +126 -126
- package/components/exchange/message-receiver.js +181 -181
- package/components/exchange/message-sender.js +143 -143
- package/components/exchange/message-tracer.js +212 -212
- package/components/service-caller.js +370 -370
- package/components/service-consumer.js +131 -131
- package/components/service-executor.js +278 -278
- package/components/service-instance.js +316 -316
- package/components/service-provider.js +251 -251
- package/integrations/redis-integration.js +591 -591
- package/package.json +89 -90
- package/utils/cache.js +772 -772
- package/utils/config.js +103 -103
- package/utils/exceptions.js +368 -368
- package/utils/localization.js +298 -298
- package/utils/logger.js +82 -82
- package/utils/tools.js +632 -632
package/bin/start-instance.js
CHANGED
|
@@ -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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
const
|
|
133
|
-
|
|
134
|
-
/** @
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
logger.log( `Error
|
|
148
|
-
setImmediate( () => process.exit( 1 ) );
|
|
149
|
-
} );
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
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
|
}
|