@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,192 +1,192 @@
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-2026 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
- const { EOL } = require( "node:os" )
10
- const _ = require( "lodash" );
11
- const tools = require( "#tools" );
12
- const logger = require( "#logger" );
13
- const config = require( "#config" );
14
- const ServiceInstance = require( "#service-instance" );
15
-
16
- /**
17
- * Used to create and/or return an Auditing System singleton instance.
18
- *
19
- * @class Auditing
20
- * @singleton
21
- * @public
22
- */
23
- class Auditing {
24
-
25
- static #instance = null;
26
-
27
- /**
28
- * @constructor
29
- * @returns {Auditing}
30
- */
31
- constructor() {
32
- if ( !Auditing.#instance ) {
33
- Auditing.#instance = this;
34
- }
35
- return Auditing.#instance;
36
- }
37
-
38
- /* Public interface */
39
-
40
- /**
41
- * Used to generate a log entry and dispatch it to all enabled logging destinations.
42
- *
43
- * @method
44
- * @param {string} message The primary log message.
45
- * @param {TiLogSeverity} [severity=DEFAULT] The log severity level. If the current log filtering setting is higher than this then the log entry will be ignored.
46
- * @param {string} [thread='main'] The logging thread to which the log entry belongs.
47
- * @param {Object} [data={}] Optional JSON data containing details of the log entry.
48
- * @public
49
- */
50
- log( message, severity = logger.logSeverity.DEFAULT, thread = "main", data = {} ) {
51
- try {
52
- // Log entries below the minimum allowed level will be directly ignored:
53
- if ( severity >= config.getSetting( config.setting.AUDITING_LOG_MIN_LEVEL ) ) {
54
- // Obscure any passwords that might have landed in the data object;
55
- // Also make sure to convert a potential Error object to a JSON:
56
- let copyOfData = ( config.getSetting( config.setting.AUDITING_LOG_DETAILS ) === true ) ? _.cloneDeep( data ) : undefined;
57
- let logEntry = Auditing.#createLogEntry( severity, thread, message, copyOfData );
58
-
59
- // Make sure there is a console available (especially important for Cloud or containerized environments):
60
- if ( config.getSetting( config.setting.AUDITING_LOG_CONSOLE_ENABLED ) === true && console ) {
61
- Auditing.#logToConsole( logEntry );
62
- }
63
- }
64
- } catch {
65
- // do nothing here for now...
66
- }
67
- }
68
-
69
- /* Private interface */
70
-
71
- /**
72
- * Used to generate a new Log Entry object.
73
- *
74
- * @method
75
- * @param {TiLogSeverity} severity
76
- * @param {string} thread
77
- * @param {string} message
78
- * @param {Object} data
79
- * @returns {TiLogEntry}
80
- * @private
81
- */
82
- static #createLogEntry( severity, thread, message, data ) {
83
- let currentDate = new Date();
84
- let logDate = tools.getUTCDateString( currentDate );
85
- let logTime = tools.getUTCTimeString( currentDate, true );
86
- let reporter = ServiceInstance.instanceID;
87
-
88
- return {
89
- _id: `${ logDate }-${ logTime }-${ thread }-${ reporter }-${ logger.getSeverityName( severity ) }-${ tools.getUUID() }`,
90
- severity: severity,
91
- thread: thread,
92
- reporter: reporter,
93
- message: message,
94
- timestamp: currentDate.getTime(),
95
- data: data
96
- };
97
- }
98
-
99
- /**
100
- * Used to write the log entries to the system console (i.e., STD OUT and STD ERR).
101
- * <br/>
102
- * NOTE: There was an issue in previous Node versions with console that can crash the application if the number of
103
- * outputs exceeds several thousands per second. To be monitored and adjusted as necessary!
104
- *
105
- * @method
106
- * @param {TiLogEntry} logEntry
107
- * @private
108
- */
109
- static #logToConsole( logEntry ) {
110
- if ( config.getSetting( config.setting.AUDITING_LOG_USES_JSON ) === true ) {
111
- if ( logEntry.severity >= logger.logSeverity.WARNING ) {
112
- console.error( tools.stringifyJSON( logEntry ) );
113
- } else {
114
- console.log( tools.stringifyJSON( logEntry ) );
115
- }
116
- } else {
117
- console.log( Auditing.#formatConsoleMessage( logEntry ) );
118
- if ( !_.isEmpty( logEntry.data ) ) {
119
- console.log( Auditing.#formatConsoleData( logEntry.data, " " ) );
120
- }
121
- }
122
- }
123
-
124
- /**
125
- * Used to format a log entry for the Node console.
126
- *
127
- * @method
128
- * @param {TiLogEntry} logEntry
129
- * @returns {string}
130
- * @private
131
- */
132
- static #formatConsoleMessage( logEntry ) {
133
- let logDate = new Date( logEntry.timestamp );
134
- return `${ tools.getUTCDateString( logDate ) }, ${ tools.getUTCTimeString( logDate, true ) } (UTC): ${ logEntry.reporter } - ${ logger.getSeverityName( logEntry.severity ) } - ${ logEntry.message }`;
135
- }
136
-
137
- /**
138
- * Used to format a log entry data payload for the Node console.
139
- *
140
- * @method
141
- * @param {*} data
142
- * @param {string} [prefix=""]
143
- * @param {number} [currentDepth=0]
144
- * @param {number} [maxDepth=5]
145
- * @param {Set} [visited=new Set()]
146
- * @returns {string}
147
- * @private
148
- */
149
- static #formatConsoleData( data, prefix = "", currentDepth = 0, maxDepth = 5, visited = new Set() ) {
150
- if ( data === null || data === undefined || !_.isObjectLike( data ) ) {
151
- // Handle null, undefined, and primitive values:
152
- return prefix + "» " + String( data );
153
- } else if ( currentDepth >= maxDepth ) {
154
- // Check max depth:
155
- return prefix + "! [max data depth reached]";
156
- } else if ( visited.has( data ) ) {
157
- // Check for circular references:
158
- return prefix + "! [circular reference detected]";
159
- } else {
160
- let formattedData = "";
161
- visited.add( data );
162
-
163
- _.forOwn( data, ( value, key ) => {
164
- let stackLines = ( _.isString( value ) ) ? value.split( "\n" ) : [ value ];
165
- if ( stackLines.length > 1 ) {
166
- _.forEach( stackLines, ( line, idx ) => {
167
- if ( idx === 0 ) {
168
- formattedData += prefix + `» ${ key }: ${ _.trim( line ) }` + EOL;
169
- } else {
170
- formattedData += prefix + `- ${ _.trim( line ) }` + EOL;
171
- }
172
- } );
173
- } else {
174
- if ( _.isObjectLike( value ) ) {
175
- formattedData += prefix + `» ${ key }:` + EOL + Auditing.#formatConsoleData( value, " " + prefix, currentDepth + 1, maxDepth, visited ) + EOL;
176
- } else {
177
- formattedData += prefix + `» ${ key }: ${ value }` + EOL;
178
- }
179
- }
180
- } );
181
-
182
- if ( formattedData.endsWith( EOL ) ) {
183
- formattedData = formattedData.slice( 0, -EOL.length );
184
- }
185
-
186
- return formattedData;
187
- }
188
- }
189
- }
190
-
191
- const instance = new Auditing();
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-2026 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
+ const { EOL } = require( "node:os" )
10
+ const _ = require( "lodash" );
11
+ const tools = require( "#tools" );
12
+ const logger = require( "#logger" );
13
+ const config = require( "#config" );
14
+ const ServiceInstance = require( "#service-instance" );
15
+
16
+ /**
17
+ * Used to create and/or return an Auditing System singleton instance.
18
+ *
19
+ * @class Auditing
20
+ * @singleton
21
+ * @public
22
+ */
23
+ class Auditing {
24
+
25
+ static #instance = null;
26
+
27
+ /**
28
+ * @constructor
29
+ * @returns {Auditing}
30
+ */
31
+ constructor() {
32
+ if ( !Auditing.#instance ) {
33
+ Auditing.#instance = this;
34
+ }
35
+ return Auditing.#instance;
36
+ }
37
+
38
+ /* Public interface */
39
+
40
+ /**
41
+ * Used to generate a log entry and dispatch it to all enabled logging destinations.
42
+ *
43
+ * @method
44
+ * @param {string} message The primary log message.
45
+ * @param {TiLogSeverity} [severity=DEFAULT] The log severity level. If the current log filtering setting is higher than this then the log entry will be ignored.
46
+ * @param {string} [thread='main'] The logging thread to which the log entry belongs.
47
+ * @param {Object} [data={}] Optional JSON data containing details of the log entry.
48
+ * @public
49
+ */
50
+ log( message, severity = logger.logSeverity.DEFAULT, thread = "main", data = {} ) {
51
+ try {
52
+ // Log entries below the minimum allowed level will be directly ignored:
53
+ if ( severity >= config.getSetting( config.setting.AUDITING_LOG_MIN_LEVEL ) ) {
54
+ // Obscure any passwords that might have landed in the data object;
55
+ // Also make sure to convert a potential Error object to a JSON:
56
+ let copyOfData = ( config.getSetting( config.setting.AUDITING_LOG_DETAILS ) === true ) ? _.cloneDeep( data ) : undefined;
57
+ let logEntry = Auditing.#createLogEntry( severity, thread, message, copyOfData );
58
+
59
+ // Make sure there is a console available (especially important for Cloud or containerized environments):
60
+ if ( config.getSetting( config.setting.AUDITING_LOG_CONSOLE_ENABLED ) === true && console ) {
61
+ Auditing.#logToConsole( logEntry );
62
+ }
63
+ }
64
+ } catch {
65
+ // do nothing here for now...
66
+ }
67
+ }
68
+
69
+ /* Private interface */
70
+
71
+ /**
72
+ * Used to generate a new Log Entry object.
73
+ *
74
+ * @method
75
+ * @param {TiLogSeverity} severity
76
+ * @param {string} thread
77
+ * @param {string} message
78
+ * @param {Object} data
79
+ * @returns {TiLogEntry}
80
+ * @private
81
+ */
82
+ static #createLogEntry( severity, thread, message, data ) {
83
+ let currentDate = new Date();
84
+ let logDate = tools.getUTCDateString( currentDate );
85
+ let logTime = tools.getUTCTimeString( currentDate, true );
86
+ let reporter = ServiceInstance.instanceID;
87
+
88
+ return {
89
+ _id: `${ logDate }-${ logTime }-${ thread }-${ reporter }-${ logger.getSeverityName( severity ) }-${ tools.getUUID() }`,
90
+ severity: severity,
91
+ thread: thread,
92
+ reporter: reporter,
93
+ message: message,
94
+ timestamp: currentDate.getTime(),
95
+ data: data
96
+ };
97
+ }
98
+
99
+ /**
100
+ * Used to write the log entries to the system console (i.e., STD OUT and STD ERR).
101
+ * <br/>
102
+ * NOTE: There was an issue in previous Node versions with console that can crash the application if the number of
103
+ * outputs exceeds several thousands per second. To be monitored and adjusted as necessary!
104
+ *
105
+ * @method
106
+ * @param {TiLogEntry} logEntry
107
+ * @private
108
+ */
109
+ static #logToConsole( logEntry ) {
110
+ if ( config.getSetting( config.setting.AUDITING_LOG_USES_JSON ) === true ) {
111
+ if ( logEntry.severity >= logger.logSeverity.WARNING ) {
112
+ console.error( tools.stringifyJSON( logEntry ) );
113
+ } else {
114
+ console.log( tools.stringifyJSON( logEntry ) );
115
+ }
116
+ } else {
117
+ console.log( Auditing.#formatConsoleMessage( logEntry ) );
118
+ if ( !_.isEmpty( logEntry.data ) ) {
119
+ console.log( Auditing.#formatConsoleData( logEntry.data, " " ) );
120
+ }
121
+ }
122
+ }
123
+
124
+ /**
125
+ * Used to format a log entry for the Node console.
126
+ *
127
+ * @method
128
+ * @param {TiLogEntry} logEntry
129
+ * @returns {string}
130
+ * @private
131
+ */
132
+ static #formatConsoleMessage( logEntry ) {
133
+ let logDate = new Date( logEntry.timestamp );
134
+ return `${ tools.getUTCDateString( logDate ) }, ${ tools.getUTCTimeString( logDate, true ) } (UTC): ${ logEntry.reporter } - ${ logger.getSeverityName( logEntry.severity ) } - ${ logEntry.message }`;
135
+ }
136
+
137
+ /**
138
+ * Used to format a log entry data payload for the Node console.
139
+ *
140
+ * @method
141
+ * @param {*} data
142
+ * @param {string} [prefix=""]
143
+ * @param {number} [currentDepth=0]
144
+ * @param {number} [maxDepth=5]
145
+ * @param {Set} [visited=new Set()]
146
+ * @returns {string}
147
+ * @private
148
+ */
149
+ static #formatConsoleData( data, prefix = "", currentDepth = 0, maxDepth = 5, visited = new Set() ) {
150
+ if ( data === null || data === undefined || !_.isObjectLike( data ) ) {
151
+ // Handle null, undefined, and primitive values:
152
+ return prefix + "» " + String( data );
153
+ } else if ( currentDepth >= maxDepth ) {
154
+ // Check max depth:
155
+ return prefix + "! [max data depth reached]";
156
+ } else if ( visited.has( data ) ) {
157
+ // Check for circular references:
158
+ return prefix + "! [circular reference detected]";
159
+ } else {
160
+ let formattedData = "";
161
+ visited.add( data );
162
+
163
+ _.forOwn( data, ( value, key ) => {
164
+ let stackLines = ( _.isString( value ) ) ? value.split( "\n" ) : [ value ];
165
+ if ( stackLines.length > 1 ) {
166
+ _.forEach( stackLines, ( line, idx ) => {
167
+ if ( idx === 0 ) {
168
+ formattedData += prefix + `» ${ key }: ${ _.trim( line ) }` + EOL;
169
+ } else {
170
+ formattedData += prefix + `- ${ _.trim( line ) }` + EOL;
171
+ }
172
+ } );
173
+ } else {
174
+ if ( _.isObjectLike( value ) ) {
175
+ formattedData += prefix + `» ${ key }:` + EOL + Auditing.#formatConsoleData( value, " " + prefix, currentDepth + 1, maxDepth, visited ) + EOL;
176
+ } else {
177
+ formattedData += prefix + `» ${ key }: ${ value }` + EOL;
178
+ }
179
+ }
180
+ } );
181
+
182
+ if ( formattedData.endsWith( EOL ) ) {
183
+ formattedData = formattedData.slice( 0, -EOL.length );
184
+ }
185
+
186
+ return formattedData;
187
+ }
188
+ }
189
+ }
190
+
191
+ const instance = new Auditing();
192
192
  module.exports.instance = Object.freeze( instance );
@@ -1,73 +1,73 @@
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-2026 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
- const exceptions = require( "#exceptions" );
10
-
11
- /**
12
- * An abstract class that allows the child class to observe and take action on various events related to external connections.
13
- *
14
- * @class ConnectionObserver
15
- * @abstract
16
- * @public
17
- */
18
- class ConnectionObserver {
19
-
20
- /**
21
- * @constructor
22
- * @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
23
- */
24
- constructor() {
25
- // make sure this abstract class cannot be instantiated:
26
- if ( new.target === ConnectionObserver ) {
27
- throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
28
- }
29
- }
30
-
31
- /**
32
- * Needs to be invoked by the connection handler when the connection is disrupted.
33
- * <br/>
34
- * NOTE: Override this to add custom functionality.
35
- *
36
- * @method
37
- * @param {string} identifier The identifier of the observed connection.
38
- * @virtual
39
- * @public
40
- *
41
- */
42
- onConnectionDisrupted( identifier ) {
43
- }
44
-
45
- /**
46
- * Needs to be invoked by the connection handler when the connection is recovered.
47
- * <br/>
48
- * NOTE: Override this to add custom functionality.
49
- *
50
- * @method
51
- * @param {string} identifier The identifier of the observed connection.
52
- * @virtual
53
- * @public
54
- */
55
- onConnectionRecovered( identifier ) {
56
- }
57
-
58
- /**
59
- * Needs to be invoked by the connection handler when the connection is irrevocably lost.
60
- * <br/>
61
- * NOTE: Override this to add custom functionality.
62
- *
63
- * @method
64
- * @param {string} identifier The identifier of the observed connection.
65
- * @virtual
66
- * @public
67
- */
68
- onConnectionLost( identifier ) {
69
- }
70
-
71
- }
72
-
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-2026 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
+ const exceptions = require( "#exceptions" );
10
+
11
+ /**
12
+ * An abstract class that allows the child class to observe and take action on various events related to external connections.
13
+ *
14
+ * @class ConnectionObserver
15
+ * @abstract
16
+ * @public
17
+ */
18
+ class ConnectionObserver {
19
+
20
+ /**
21
+ * @constructor
22
+ * @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
23
+ */
24
+ constructor() {
25
+ // make sure this abstract class cannot be instantiated:
26
+ if ( new.target === ConnectionObserver ) {
27
+ throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
28
+ }
29
+ }
30
+
31
+ /**
32
+ * Needs to be invoked by the connection handler when the connection is disrupted.
33
+ * <br/>
34
+ * NOTE: Override this to add custom functionality.
35
+ *
36
+ * @method
37
+ * @param {string} identifier The identifier of the observed connection.
38
+ * @virtual
39
+ * @public
40
+ *
41
+ */
42
+ onConnectionDisrupted( identifier ) {
43
+ }
44
+
45
+ /**
46
+ * Needs to be invoked by the connection handler when the connection is recovered.
47
+ * <br/>
48
+ * NOTE: Override this to add custom functionality.
49
+ *
50
+ * @method
51
+ * @param {string} identifier The identifier of the observed connection.
52
+ * @virtual
53
+ * @public
54
+ */
55
+ onConnectionRecovered( identifier ) {
56
+ }
57
+
58
+ /**
59
+ * Needs to be invoked by the connection handler when the connection is irrevocably lost.
60
+ * <br/>
61
+ * NOTE: Override this to add custom functionality.
62
+ *
63
+ * @method
64
+ * @param {string} identifier The identifier of the observed connection.
65
+ * @virtual
66
+ * @public
67
+ */
68
+ onConnectionLost( identifier ) {
69
+ }
70
+
71
+ }
72
+
73
73
  module.exports = ConnectionObserver;