@ti-engine/core 1.3.13 → 1.6.0
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 +70 -1
- package/README.md +3 -3
- package/bin/localization/labels.json +9 -0
- package/bin/settings.json +1 -1
- package/components/auditing.js +1 -18
- package/components/connection-observer.js +2 -2
- package/components/definitions.types.js +248 -0
- package/components/exchange/message-dispatcher.js +1 -1
- package/components/exchange/message-exchange.js +6 -30
- package/components/exchange/message-handler.js +16 -6
- package/components/exchange/message-memory-cache.js +1 -1
- package/components/exchange/message-observer.js +2 -2
- package/components/exchange/message-receiver.js +4 -3
- package/components/exchange/message-sender.js +2 -2
- package/components/exchange/message-tracer.js +4 -27
- package/components/service-caller.js +1 -40
- package/components/service-consumer.js +1 -1
- package/components/service-executor.js +1 -16
- package/components/service-instance.js +4 -9
- package/components/service-provider.js +9 -8
- package/integrations/redis-integration.js +8 -13
- package/package.json +10 -11
- package/utils/cache.js +64 -18
- package/utils/config.js +4 -101
- package/utils/exceptions.js +37 -20
- package/utils/localization.js +2 -17
- package/utils/logger.js +5 -8
- package/utils/tools.js +25 -20
- package/integrations/gcloud-integration.js +0 -65
package/utils/config.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
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-
|
|
3
|
+
* Copyright © 2021-2026 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
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
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
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/>.
|
|
@@ -9,104 +9,6 @@
|
|
|
9
9
|
const _ = require( "lodash" );
|
|
10
10
|
const tools = require( "#tools" );
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {string} EnvironmentVariable
|
|
14
|
-
*/
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* @typedef {NodeJS.Process} Environment
|
|
18
|
-
* @property {ProcessEnv} env
|
|
19
|
-
* @property {EnvironmentVariable} env.TI_GCLOUD_API_KEY
|
|
20
|
-
* @property {EnvironmentVariable} env.TI_GCLOUD_ENABLED
|
|
21
|
-
* @property {EnvironmentVariable} env.TI_GCLOUD_PROJECT_ID
|
|
22
|
-
* @property {EnvironmentVariable} env.TI_INSTANCE_CLASS
|
|
23
|
-
* @property {EnvironmentVariable} env.TI_INSTANCE_CONFIG
|
|
24
|
-
* @property {EnvironmentVariable} env.TI_INSTANCE_ID
|
|
25
|
-
* @property {EnvironmentVariable} env.TI_INSTANCE_NAME
|
|
26
|
-
* @property {EnvironmentVariable} env.TI_AUDITING_LOG_CONSOLE_ENABLED
|
|
27
|
-
* @property {EnvironmentVariable} env.TI_AUDITING_LOG_DETAILS
|
|
28
|
-
* @property {EnvironmentVariable} env.TI_AUDITING_LOG_MIN_LEVEL
|
|
29
|
-
* @property {EnvironmentVariable} env.TI_AUDITING_LOG_USES_JSON
|
|
30
|
-
* @property {EnvironmentVariable} env.TI_LOCALIZATION_LABELS_PATH
|
|
31
|
-
* @property {EnvironmentVariable} env.TI_LOCALIZATION_LANGUAGE
|
|
32
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_AUTH_KEY
|
|
33
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_REDIS_DB
|
|
34
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_REDIS_HOST
|
|
35
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_REDIS_PORT
|
|
36
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_RETRY_MAX_ATTEMPTS
|
|
37
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_RETRY_MAX_INTERVAL
|
|
38
|
-
* @property {EnvironmentVariable} env.TI_MEMORY_CACHE_USER
|
|
39
|
-
* @property {EnvironmentVariable} env.TI_MESSAGE_EXCHANGE_SECURITY_HASH_ENABLED
|
|
40
|
-
* @property {EnvironmentVariable} env.TI_MESSAGE_EXCHANGE_SECURITY_HASH_KEY
|
|
41
|
-
* @property {EnvironmentVariable} env.TI_MESSAGE_EXCHANGE_TRACE_LOG_ENABLED
|
|
42
|
-
*/
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* @typedef {string} CronString
|
|
46
|
-
*/
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* @typedef {Object} SettingsMain
|
|
50
|
-
* @property {SettingsAuditing} auditing
|
|
51
|
-
* @property {SettingsGcloudIntegration} gcloudIntegration
|
|
52
|
-
* @property {SettingsLocalization} localization
|
|
53
|
-
* @property {SettingsMemoryCache} memoryCache
|
|
54
|
-
* @property {SettingsMessageExchange} messageExchange
|
|
55
|
-
* @property {SettingsServiceConfig} serviceConfig
|
|
56
|
-
* @property {string} operationMode
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* @typedef {Object} SettingsAuditing
|
|
61
|
-
* @property {boolean} logConsoleEnabled
|
|
62
|
-
* @property {boolean} logDetails
|
|
63
|
-
* @property {TiLogSeverity} logMinLevel
|
|
64
|
-
* @property {boolean} logUsesJSON
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* @typedef {Object} SettingsGcloudIntegration
|
|
69
|
-
* @property {string} apiKey
|
|
70
|
-
* @property {string} projectID
|
|
71
|
-
*/
|
|
72
|
-
|
|
73
|
-
/**
|
|
74
|
-
* @typedef {Object} SettingsLocalization
|
|
75
|
-
* @property {Array<string>} labelsPath
|
|
76
|
-
* @property {TiLocalizationLanguage} language
|
|
77
|
-
*/
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* @typedef {Object} SettingsMemoryCache
|
|
81
|
-
* @property {string} authKey
|
|
82
|
-
* @property {number} redisDB
|
|
83
|
-
* @property {string} redisHost
|
|
84
|
-
* @property {number} redisPort
|
|
85
|
-
* @property {number} retryMaxAttempts
|
|
86
|
-
* @property {number} retryMaxInterval
|
|
87
|
-
* @property {string} user
|
|
88
|
-
*/
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* @typedef {Object} SettingsMessageExchange
|
|
92
|
-
* @property {string} messageQueuePrefix
|
|
93
|
-
* @property {string} messageStore
|
|
94
|
-
* @property {boolean} securityHashEnabled
|
|
95
|
-
* @property {string} securityHashKey
|
|
96
|
-
* @property {number} traceExpirationTime
|
|
97
|
-
* @property {boolean} traceLogEnabled
|
|
98
|
-
* @property {string} traceRepository
|
|
99
|
-
*/
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* @typedef {Object} SettingsServiceConfig
|
|
103
|
-
* @property {number} executionTimeout
|
|
104
|
-
* @property {string} healthCheckAddress
|
|
105
|
-
* @property {CronString} healthCheckInterval
|
|
106
|
-
* @property {number} healthCheckTimeout
|
|
107
|
-
* @property {string} serviceRegistryAddress
|
|
108
|
-
*/
|
|
109
|
-
|
|
110
12
|
/**
|
|
111
13
|
* Enum for listing all system settings.
|
|
112
14
|
*
|
|
@@ -144,7 +46,6 @@ const settingsEnum = tools.enum( {
|
|
|
144
46
|
SERVICE_REGISTRY_ADDRESS: [ "serviceConfig.serviceRegistryAddress", "serviceRegistryAddress", "" ],
|
|
145
47
|
OPERATION_MODE: [ "operationMode", "operationMode", "" ]
|
|
146
48
|
} );
|
|
147
|
-
|
|
148
49
|
module.exports.setting = settingsEnum;
|
|
149
50
|
|
|
150
51
|
/** @type {SettingsMain} */
|
|
@@ -158,7 +59,9 @@ if ( settings.auditing ) {
|
|
|
158
59
|
settings.auditing.logUsesJSON = ( process.env.TI_AUDITING_LOG_USES_JSON !== undefined ) ? tools.toBool( process.env.TI_AUDITING_LOG_USES_JSON ) : settings.auditing.logUsesJSON;
|
|
159
60
|
}
|
|
160
61
|
if ( settings.localization ) {
|
|
161
|
-
settings.localization.labelsPath = ( process.env.TI_LOCALIZATION_LABELS_PATH !== undefined )
|
|
62
|
+
settings.localization.labelsPath = ( process.env.TI_LOCALIZATION_LABELS_PATH !== undefined )
|
|
63
|
+
? process.env.TI_LOCALIZATION_LABELS_PATH.split( "," ).map( ( p ) => p.trim() ).filter( Boolean )
|
|
64
|
+
: settings.localization.labelsPath;
|
|
162
65
|
settings.localization.language = ( process.env.TI_LOCALIZATION_LANGUAGE !== undefined ) ? process.env.TI_LOCALIZATION_LANGUAGE : settings.localization.language;
|
|
163
66
|
}
|
|
164
67
|
if ( settings.memoryCache ) {
|
package/utils/exceptions.js
CHANGED
|
@@ -29,6 +29,7 @@ const exceptionCodeEnum = tools.enum( {
|
|
|
29
29
|
E_GEN_INVALID_ARGUMENT_TYPE: [ 1007, "invalid argument type", "The provided argument is not of the expected type." ],
|
|
30
30
|
E_GEN_NOT_INITIALIZED: [ 1008, "not initialized", "The invoked framework component is not initialized." ],
|
|
31
31
|
E_GEN_UNALLOWED_OVERRIDE: [ 1009, "unallowed override", "Attempt to override a protected or private method or property detected." ],
|
|
32
|
+
E_GEN_NOT_IMPLEMENTED: [ 1010, "not implemented", "The requested functionality is not yet implemented." ],
|
|
32
33
|
/** Security & Administration exceptions - codes under 2xxx */
|
|
33
34
|
E_SEC_INVALID_AUTH_TOKEN: [ 2000, "invalid auth token", "Invalid authorization token provided." ],
|
|
34
35
|
E_SEC_INVALID_EXPIRED_SESSION: [ 2001, "invalid or expired session", "Invalid or expired session encountered." ],
|
|
@@ -56,9 +57,12 @@ const exceptionCodeEnum = tools.enum( {
|
|
|
56
57
|
E_WEB_INVALID_REQUEST_FORMAT: [ 4006, "invalid request format", "The request format is not recognized or not supported." ],
|
|
57
58
|
E_WEB_INVALID_REQUEST_CONTENT_TYPE: [ 4007, "invalid request content type", "The request content type is not recognized or not supported." ],
|
|
58
59
|
E_WEB_INVALID_REQUEST_CONTENT_LENGTH: [ 4008, "invalid request content length", "The request content length is not recognized or not supported." ],
|
|
59
|
-
E_WEB_INVALID_REQUEST_CONTENT_ENCODING: [ 4009, "invalid request content encoding", "The request content encoding is not recognized or not supported." ]
|
|
60
|
+
E_WEB_INVALID_REQUEST_CONTENT_ENCODING: [ 4009, "invalid request content encoding", "The request content encoding is not recognized or not supported." ],
|
|
61
|
+
/** Application exceptions - codes under 5xxx */
|
|
62
|
+
E_APP_RESOURCE_NOT_FOUND: [ 5004, "resource not found", "The requested resource cannot be found. See details for more information." ],
|
|
63
|
+
E_APP_SERVICE_ERROR: [ 5005, "app service error", "The application service encountered an error. See details for more information." ],
|
|
64
|
+
E_APP_RESOURCE_ALREADY_EXISTS: [ 5006, "resource already exists", "The resource cannot be created because it already exists. See details for more information." ]
|
|
60
65
|
} );
|
|
61
|
-
|
|
62
66
|
module.exports.exceptionCode = exceptionCodeEnum;
|
|
63
67
|
|
|
64
68
|
/**
|
|
@@ -134,18 +138,17 @@ const httpCodeEnum = tools.enum( {
|
|
|
134
138
|
C_510: [ 510, "Not Extended", "Further extensions to the request are required for the server to fulfil it." ],
|
|
135
139
|
C_511: [ 511, "Network Authentication Required", "The client needs to authenticate to gain network access. Intended for use by intercepting proxies used to control access to the network." ]
|
|
136
140
|
} );
|
|
137
|
-
|
|
138
141
|
module.exports.httpCode = httpCodeEnum;
|
|
139
142
|
|
|
140
143
|
const labelPath = "system.exceptions.";
|
|
141
144
|
|
|
142
145
|
/**
|
|
143
|
-
* Represents an exception.
|
|
146
|
+
* Represents an any-purpose exception.
|
|
144
147
|
*
|
|
145
|
-
* @class
|
|
148
|
+
* @class TiException
|
|
146
149
|
* @public
|
|
147
150
|
*/
|
|
148
|
-
class
|
|
151
|
+
class TiException {
|
|
149
152
|
|
|
150
153
|
#id;
|
|
151
154
|
#code;
|
|
@@ -266,7 +269,7 @@ class Exception {
|
|
|
266
269
|
}
|
|
267
270
|
|
|
268
271
|
/**
|
|
269
|
-
* Extracts the essential information about the
|
|
272
|
+
* Extracts the essential information about the {@link TiException} and returns it as JSON.
|
|
270
273
|
*
|
|
271
274
|
* @method
|
|
272
275
|
* @param {boolean} [includeData=true] Whether to include the data property in the output.
|
|
@@ -292,38 +295,48 @@ class Exception {
|
|
|
292
295
|
|
|
293
296
|
/**
|
|
294
297
|
* Used to raise an exception from the provided source.
|
|
298
|
+
* <br/>
|
|
299
|
+
* NOTE: Custom numeric exception IDs are not supported when 'httpCode' is omitted!
|
|
295
300
|
*
|
|
296
301
|
* @method
|
|
297
|
-
* @param {Error|TiExceptionCode|
|
|
302
|
+
* @param {Error|TiExceptionCode|TiException} source Could be a standard JS Error, an ExceptionCode, or another TiException (in which case it will be raised further).
|
|
298
303
|
* @param {Object} [data] Additional JSON data that can go with the exception. If more data is added on later Raise calls, it will be merged with the existing one.
|
|
299
|
-
* @param {string} [exceptionID] Should be used only in cases when we have a recognizable exception ID beforehand. Should not be entered otherwise!
|
|
300
|
-
* @
|
|
304
|
+
* @param {string} [exceptionID=undefined] Should be used only in cases when we have a recognizable exception ID beforehand. Should not be entered otherwise!
|
|
305
|
+
* @param {TiHttpCode} [httpCode=undefined] An optional HTTP code in case this exception needs to be propagated to a web-application frontend. If provided, this will
|
|
306
|
+
* override any preexisting HTTP code in 'source'!
|
|
307
|
+
* @returns {TiException}
|
|
301
308
|
* @public
|
|
302
309
|
*/
|
|
303
|
-
module.exports.raise = ( source, data, exceptionID ) => {
|
|
304
|
-
/** @type
|
|
310
|
+
module.exports.raise = ( source, data, exceptionID = undefined, httpCode = undefined ) => {
|
|
311
|
+
/** @type TiException */
|
|
305
312
|
let exception;
|
|
306
313
|
|
|
314
|
+
// Support flexible argument passing: if exceptionID is a number (likely an HTTP code) and httpCode is missing, swap them:
|
|
315
|
+
if ( typeof exceptionID === "number" && httpCode === undefined ) {
|
|
316
|
+
httpCode = exceptionID;
|
|
317
|
+
exceptionID = undefined;
|
|
318
|
+
}
|
|
319
|
+
|
|
307
320
|
if ( source instanceof Error ) {
|
|
308
|
-
exception = new
|
|
309
|
-
} else if ( source instanceof
|
|
321
|
+
exception = new TiException( exceptionID || tools.getUUID(), exceptionCodeEnum.E_GEN_JS_INTERNAL_ERROR, tools.errorToJSON( source ) );
|
|
322
|
+
} else if ( source instanceof TiException ) {
|
|
310
323
|
exception = source;
|
|
311
324
|
} else if ( _.isString( source ) ) {
|
|
312
|
-
exception = new
|
|
325
|
+
exception = new TiException( exceptionID || tools.getUUID(), exceptionCodeEnum.E_GEN_JS_INTERNAL_ERROR, {
|
|
313
326
|
message: source
|
|
314
327
|
} );
|
|
315
328
|
} else if ( _.isObjectLike( source ) ) {
|
|
316
|
-
exception = new
|
|
329
|
+
exception = new TiException(
|
|
317
330
|
exceptionID || ( source.id || tools.getUUID() ),
|
|
318
331
|
source.code || exceptionCodeEnum.E_GEN_JS_INTERNAL_ERROR,
|
|
319
332
|
source.data,
|
|
320
333
|
source.description
|
|
321
334
|
);
|
|
322
|
-
if ( httpCodeEnum.contains( source.httpCode ) ) {
|
|
335
|
+
if ( source.httpCode && httpCodeEnum.contains( source.httpCode ) ) {
|
|
323
336
|
exception.httpCode = source.httpCode;
|
|
324
337
|
}
|
|
325
338
|
} else {
|
|
326
|
-
exception = new
|
|
339
|
+
exception = new TiException( exceptionID || tools.getUUID(), ( exceptionCodeEnum.contains( source ) ) ? source : exceptionCodeEnum.E_UNKNOWN_ERROR );
|
|
327
340
|
}
|
|
328
341
|
|
|
329
342
|
// Merge the default exception data with the additional one if it's provided:
|
|
@@ -336,11 +349,15 @@ module.exports.raise = ( source, data, exceptionID ) => {
|
|
|
336
349
|
exception.data = tools.decycle( exception.data );
|
|
337
350
|
}
|
|
338
351
|
|
|
352
|
+
if ( httpCode && httpCodeEnum.contains( httpCode ) ) {
|
|
353
|
+
exception.httpCode = httpCode;
|
|
354
|
+
}
|
|
355
|
+
|
|
339
356
|
return exception;
|
|
340
357
|
};
|
|
341
358
|
|
|
342
359
|
/**
|
|
343
|
-
* Verifies if the passed object is
|
|
360
|
+
* Verifies if the passed object is a {@link TiException}.
|
|
344
361
|
*
|
|
345
362
|
* @method
|
|
346
363
|
* @param {*} object
|
|
@@ -348,5 +365,5 @@ module.exports.raise = ( source, data, exceptionID ) => {
|
|
|
348
365
|
* @public
|
|
349
366
|
*/
|
|
350
367
|
module.exports.isException = ( object ) => {
|
|
351
|
-
return ( object instanceof
|
|
368
|
+
return ( object instanceof TiException );
|
|
352
369
|
};
|
package/utils/localization.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
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-
|
|
3
|
+
* Copyright © 2021-2026 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
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
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
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/>.
|
|
@@ -18,6 +18,7 @@ const defaultEmptyLabel = "!!! label not found !!!";
|
|
|
18
18
|
*
|
|
19
19
|
* @readonly
|
|
20
20
|
* @enum {string}
|
|
21
|
+
* @typedef {string} TiLocalizationLanguage
|
|
21
22
|
*/
|
|
22
23
|
const localizationLanguageEnum = tools.enum( {
|
|
23
24
|
// A:
|
|
@@ -227,24 +228,8 @@ const localizationLanguageEnum = tools.enum( {
|
|
|
227
228
|
ZHUANG: [ "za", "Zhuang", "Zhuang" ],
|
|
228
229
|
ZULU: [ "zu", "Zulu", "Zulu" ]
|
|
229
230
|
} );
|
|
230
|
-
|
|
231
|
-
/**
|
|
232
|
-
* @typedef {string} TiLocalizationLanguage
|
|
233
|
-
*/
|
|
234
231
|
module.exports.localizationLanguage = localizationLanguageEnum;
|
|
235
232
|
|
|
236
|
-
/**
|
|
237
|
-
* The key of this object is the language code, and the value is the textual representation of the label.
|
|
238
|
-
*
|
|
239
|
-
* @typedef {Object<TiLocalizationLanguage, string>} TiLocalizedLabel
|
|
240
|
-
*/
|
|
241
|
-
|
|
242
|
-
/**
|
|
243
|
-
* A nested labels tree where intermediate nodes are objects and leaf nodes are language-to-text maps.
|
|
244
|
-
*
|
|
245
|
-
* @typedef {Object<string, TiLocalizedLabel | TiLabelsTree>} TiLabelsTree
|
|
246
|
-
*/
|
|
247
|
-
|
|
248
233
|
/** @type {TiLabelsTree} */
|
|
249
234
|
const labels = require( "#labels" );
|
|
250
235
|
|
package/utils/logger.js
CHANGED
|
@@ -16,6 +16,7 @@ const localization = require( "#localization" );
|
|
|
16
16
|
*
|
|
17
17
|
* @readonly
|
|
18
18
|
* @enum {number}
|
|
19
|
+
* @typedef {number} TiLogSeverity
|
|
19
20
|
*/
|
|
20
21
|
const logSeverityEnum = tools.enum( {
|
|
21
22
|
DEFAULT: [ 0, "default", "The log entry has no assigned severity level." ],
|
|
@@ -28,10 +29,6 @@ const logSeverityEnum = tools.enum( {
|
|
|
28
29
|
ALERT: [ 700, "alert", "A person must take an action immediately." ],
|
|
29
30
|
EMERGENCY: [ 800, "emergency", "One or more systems are unusable." ]
|
|
30
31
|
} );
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* @typedef {number} TiLogSeverity
|
|
34
|
-
*/
|
|
35
32
|
module.exports.logSeverity = logSeverityEnum;
|
|
36
33
|
|
|
37
34
|
/**
|
|
@@ -46,10 +43,10 @@ module.exports.getSeverityName = ( severity ) => {
|
|
|
46
43
|
};
|
|
47
44
|
|
|
48
45
|
/**
|
|
49
|
-
* Used to extract information from
|
|
46
|
+
* Used to extract information from a {@link TiException} and convert it to a loggable data object.
|
|
50
47
|
*
|
|
51
48
|
* @method
|
|
52
|
-
* @param {
|
|
49
|
+
* @param {TiException} exception
|
|
53
50
|
* @returns {{description, details: (*|undefined), exceptionID}}
|
|
54
51
|
* @private
|
|
55
52
|
*/
|
|
@@ -66,8 +63,8 @@ const exceptionToLog = ( exception ) => {
|
|
|
66
63
|
*
|
|
67
64
|
* @method
|
|
68
65
|
* @param {string} message The primary log message.
|
|
69
|
-
* @param {TiLogSeverity} [level=DEFAULT] The log severity level. If the current log filtering setting is higher than this then the log entry will be ignored.
|
|
70
|
-
* @param {Object|Error|
|
|
66
|
+
* @param {TiLogSeverity} [level=DEFAULT] The log severity level. If the current log filtering setting is higher than this, then the log entry will be ignored.
|
|
67
|
+
* @param {Object|Error|TiException} [data={}] Optional JSON data containing details of the log entry.
|
|
71
68
|
* @param {string} [thread='main'] The logging thread to which the log entry belongs.
|
|
72
69
|
* @public
|
|
73
70
|
*/
|
package/utils/tools.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/*
|
|
2
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-
|
|
3
|
+
* Copyright © 2021-2026 Boris Kostadinov <kostadinov.boris@gmail.com>
|
|
4
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
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
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/>.
|
|
@@ -9,21 +9,6 @@
|
|
|
9
9
|
const _ = require( "lodash" );
|
|
10
10
|
const crypto = require( "node:crypto" );
|
|
11
11
|
|
|
12
|
-
/**
|
|
13
|
-
* @typedef {Object} TiEnumValue
|
|
14
|
-
* @property {number|string} value
|
|
15
|
-
* @property {string} name
|
|
16
|
-
* @property {string} [description]
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
/**
|
|
20
|
-
* @typedef {Object} TiEnum
|
|
21
|
-
* @property {Object.<number|string,TiEnumValue>} properties
|
|
22
|
-
* @property {function( (number|string), [string] ): (string|undefined)} name
|
|
23
|
-
* @property {function( (number|string), [string] ): (string|undefined)} description
|
|
24
|
-
* @property {function( (number|string) ): boolean} contains
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
12
|
/**
|
|
28
13
|
* Used to generate and return new UUID.
|
|
29
14
|
*
|
|
@@ -41,7 +26,7 @@ module.exports.getUUID = () => {
|
|
|
41
26
|
* @method
|
|
42
27
|
* @param {Object} object
|
|
43
28
|
* @param {WeakSet} [seen]
|
|
44
|
-
* @
|
|
29
|
+
* @returns {Object}
|
|
45
30
|
* @public
|
|
46
31
|
*/
|
|
47
32
|
module.exports.deepFreeze = ( object, seen = new WeakSet() ) => {
|
|
@@ -367,7 +352,7 @@ module.exports.decycle = ( object, replacer ) => {
|
|
|
367
352
|
* replaced with references to the value found by the PATH. This will restore cycles. The object will be mutated.
|
|
368
353
|
*
|
|
369
354
|
* The eval function is used to locate the values described by a PATH. The root object is kept in a $ variable. A
|
|
370
|
-
* regular expression is used to
|
|
355
|
+
* regular expression is used to ensure that the PATH is extremely well-formed. The regexp contains nested quantifiers.
|
|
371
356
|
* That has been known to have extremely bad performance problems on some browsers for very long strings. A PATH is
|
|
372
357
|
* expected to be reasonably short. A PATH is allowed to belong to a very restricted subset of Goessner's JSONPath.
|
|
373
358
|
*
|
|
@@ -455,7 +440,7 @@ module.exports.isJsonString = ( string ) => {
|
|
|
455
440
|
};
|
|
456
441
|
|
|
457
442
|
/**
|
|
458
|
-
* Use this to parse any JSON string into JSON object for internal system purposes as it ensures to restore any
|
|
443
|
+
* Use this to parse any JSON string into a JSON object for internal system purposes as it ensures to restore any
|
|
459
444
|
* circular dependencies obscured with 'stringifyJSON'.
|
|
460
445
|
*
|
|
461
446
|
* @method
|
|
@@ -474,7 +459,7 @@ module.exports.parseJSON = ( value ) => {
|
|
|
474
459
|
|
|
475
460
|
/**
|
|
476
461
|
* Use this to decompose a JSON object into a sorted string. The values will be ordered alphabetically and combined with
|
|
477
|
-
* their keys, where applicable, starting from the bottom and moving up. Null or undefined values will be ignored and
|
|
462
|
+
* their keys, where applicable, starting from the bottom and moving up. Null or undefined values will be ignored, and
|
|
478
463
|
* their keys will not be included in the final string.
|
|
479
464
|
*
|
|
480
465
|
* @param {Object} input
|
|
@@ -514,6 +499,26 @@ module.exports.decomposeJSON = ( input ) => {
|
|
|
514
499
|
return decomposed;
|
|
515
500
|
};
|
|
516
501
|
|
|
502
|
+
/**
|
|
503
|
+
* Constant-time string comparison. Mirrors the web-framework safe-compare idiom: coerces inputs to
|
|
504
|
+
* utf8 buffers, short-circuits on length mismatch, and never throws on hostile/non-string input.
|
|
505
|
+
*
|
|
506
|
+
* @method
|
|
507
|
+
* @param {*} a
|
|
508
|
+
* @param {*} b
|
|
509
|
+
* @returns {boolean} True only when both inputs coerce to equal-length, byte-identical strings.
|
|
510
|
+
* @public
|
|
511
|
+
*/
|
|
512
|
+
module.exports.constantTimeEquals = ( a, b ) => {
|
|
513
|
+
try {
|
|
514
|
+
const ba = Buffer.from( String( a || "" ), "utf8" );
|
|
515
|
+
const bb = Buffer.from( String( b || "" ), "utf8" );
|
|
516
|
+
return ( ba.length !== bb.length ) ? false : crypto.timingSafeEqual( ba, bb );
|
|
517
|
+
} catch {
|
|
518
|
+
return false;
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
|
|
517
522
|
/**
|
|
518
523
|
* Used to create retry policy for the execution of an operation.
|
|
519
524
|
*
|
|
@@ -1,65 +0,0 @@
|
|
|
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
|
-
const config = require( "#config" );
|
|
10
|
-
const tools = require( "#tools" );
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Used to verify if the GCloud integration is enabled.
|
|
14
|
-
*
|
|
15
|
-
* @method
|
|
16
|
-
* @returns {boolean}
|
|
17
|
-
* @public
|
|
18
|
-
*/
|
|
19
|
-
module.exports.isEnabled = () => {
|
|
20
|
-
return tools.toBool( process.env.TI_GCLOUD_ENABLED );
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* Will be used to gracefully shut down the instance.
|
|
25
|
-
* <br/>
|
|
26
|
-
* NOTE: Will be overridden below after the instance creation.
|
|
27
|
-
*
|
|
28
|
-
* @method
|
|
29
|
-
* @param {Error} error
|
|
30
|
-
* @abstract
|
|
31
|
-
* @public
|
|
32
|
-
*/
|
|
33
|
-
module.exports.reportError = ( error ) => {
|
|
34
|
-
console.error( "Attempting to report error to GCloud while integration to it is disabled!" );
|
|
35
|
-
};
|
|
36
|
-
|
|
37
|
-
if ( process.env.TI_GCLOUD_ENABLED === true ) {
|
|
38
|
-
const { ErrorReporting } = require( "@google-cloud/error-reporting" );
|
|
39
|
-
|
|
40
|
-
const errorReporting = new ErrorReporting( {
|
|
41
|
-
projectId: config.getSetting( config.setting.GCLOUD_PROJECT_ID ),
|
|
42
|
-
key: config.getSetting( config.setting.GCLOUD_API_KEY ),
|
|
43
|
-
reportMode: "production",
|
|
44
|
-
logLevel: 2,
|
|
45
|
-
reportUnhandledRejections: true
|
|
46
|
-
} );
|
|
47
|
-
|
|
48
|
-
/**
|
|
49
|
-
* Reports an error to the GCloud error reporting system.
|
|
50
|
-
*
|
|
51
|
-
* @method
|
|
52
|
-
* @param {Error} error
|
|
53
|
-
* @override
|
|
54
|
-
* @public
|
|
55
|
-
*/
|
|
56
|
-
module.exports.reportError = ( error ) => {
|
|
57
|
-
errorReporting.report( {
|
|
58
|
-
eventTime: ( new Date() ).toISOString(),
|
|
59
|
-
message: error.stack,
|
|
60
|
-
serviceContext: {
|
|
61
|
-
service: process.env.TI_INSTANCE_ID
|
|
62
|
-
}
|
|
63
|
-
} );
|
|
64
|
-
};
|
|
65
|
-
}
|