@ti-engine/core 1.0.2 → 1.0.3
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/README.md
CHANGED
|
@@ -8,6 +8,19 @@ Being a messenger system, the **@ti-engine/core** relies on a message broker for
|
|
|
8
8
|
|
|
9
9
|
Please be aware, that this framework is under active development and will expand in the near future. Also, this documentation is still in the process of being created and refined. Make sure to keep an eye on the changes in case you want to use it in the meantime.
|
|
10
10
|
|
|
11
|
+
## why ti-engine?
|
|
12
|
+
The framework is created based on a decade of professional experience with the utilized technologies and architectural approach. It's primary goal is to provide you with a lightweight and flexible solution that can help you build quickly a microservice ecosystem with any degree of size and complexity.
|
|
13
|
+
|
|
14
|
+
This is what you gain by using ti-engine:
|
|
15
|
+
* Simplicity - begin productive work within minutes and get to codding you business logic
|
|
16
|
+
* Flexibility - go as complex as you need to in your implementation
|
|
17
|
+
* Reliability - message exchange between the services is constantly tracked across the entire ecosystem
|
|
18
|
+
* Security - messages are encrypted in transit and cannot be modified by external agents
|
|
19
|
+
* Scalability - serve mullions of requests by multiplying stateless service instances (hardware limitations still apply)
|
|
20
|
+
* Containerization - go with containers from the very start as the framework is designed to work in such an environment
|
|
21
|
+
|
|
22
|
+
These are just some benefits **ti-engine** offers. Get to know it to find out more way in which it can help you improve productivity.
|
|
23
|
+
|
|
11
24
|
## prerequisites & installation
|
|
12
25
|
In order to run the basic ti-engine framework you will need a couple of things:
|
|
13
26
|
1. A local [node.js installation](https://nodejs.org/en/download/) with a minimum version of **14.17.0**
|
|
@@ -20,14 +20,14 @@ const messageDispatcher = require( "#message-dispatcher" );
|
|
|
20
20
|
*/
|
|
21
21
|
|
|
22
22
|
/**
|
|
23
|
-
* @callback
|
|
23
|
+
* @callback VerifyAccessMethod
|
|
24
24
|
* @param {string} authToken
|
|
25
25
|
* @param {ServiceAddress} serviceAddress
|
|
26
26
|
* @returns {Promise}
|
|
27
27
|
*/
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* @callback
|
|
30
|
+
* @callback ServiceHandlerMethod
|
|
31
31
|
* @param {ServiceDefinition} serviceDefinition The service definition as provided during the service registration.
|
|
32
32
|
* @param {Object} serviceParams Set of named parameters provided to the called service.
|
|
33
33
|
* @param {ServiceExecContext} serviceExecContext The context in which the service call is being executed.
|
|
@@ -44,7 +44,7 @@ const messageDispatcher = require( "#message-dispatcher" );
|
|
|
44
44
|
class ServiceExecutor extends MessageObserver {
|
|
45
45
|
|
|
46
46
|
#serviceInterface = {};
|
|
47
|
-
/** @type
|
|
47
|
+
/** @type VerifyAccessMethod */
|
|
48
48
|
#verifyAccess;
|
|
49
49
|
|
|
50
50
|
/**
|
|
@@ -114,7 +114,7 @@ class ServiceExecutor extends MessageObserver {
|
|
|
114
114
|
* Used to setup the method for service access verification.
|
|
115
115
|
*
|
|
116
116
|
* @method
|
|
117
|
-
* @param {
|
|
117
|
+
* @param {VerifyAccessMethod} verifyAccess
|
|
118
118
|
* @public
|
|
119
119
|
*/
|
|
120
120
|
configureVerifyAccess( verifyAccess ) {
|
|
@@ -131,18 +131,19 @@ class ServiceExecutor extends MessageObserver {
|
|
|
131
131
|
* NOTE: If the same version of the service handler already exists, it will be overridden!
|
|
132
132
|
*
|
|
133
133
|
* @method
|
|
134
|
-
* @param {
|
|
134
|
+
* @param {ServiceHandlerMethod} serviceHandler
|
|
135
135
|
* @param {ServiceDefinition} serviceDefinition
|
|
136
|
+
* @param {ServiceInstance} serviceInstance This will be used as context to bind all business services.
|
|
136
137
|
* @public
|
|
137
138
|
*/
|
|
138
|
-
addServiceHandler( serviceHandler, serviceDefinition ) {
|
|
139
|
+
addServiceHandler( serviceHandler, serviceDefinition, serviceInstance ) {
|
|
139
140
|
if ( !this.#serviceInterface[ serviceDefinition.serviceAlias ] ) {
|
|
140
141
|
this.#serviceInterface[ serviceDefinition.serviceAlias ] = {};
|
|
141
142
|
}
|
|
142
143
|
if ( this.#serviceInterface[ serviceDefinition.serviceAlias ][ serviceDefinition.serviceVersion ] ) {
|
|
143
144
|
logger.log( `Service handler for '${ serviceDefinition.serviceAlias }' version '${ serviceDefinition.serviceVersion }' already existed and will be overridden.`, logger.logSeverity.WARNING );
|
|
144
145
|
}
|
|
145
|
-
this.#serviceInterface[ serviceDefinition.serviceAlias ][ serviceDefinition.serviceVersion ] = serviceHandler.bind(
|
|
146
|
+
this.#serviceInterface[ serviceDefinition.serviceAlias ][ serviceDefinition.serviceVersion ] = serviceHandler.bind( serviceInstance, _.cloneDeep( serviceDefinition ) );
|
|
146
147
|
}
|
|
147
148
|
|
|
148
149
|
/* Private interface */
|
|
@@ -203,7 +204,7 @@ class ServiceExecutor extends MessageObserver {
|
|
|
203
204
|
*
|
|
204
205
|
* @method
|
|
205
206
|
* @param {ServiceAddress} serviceAddress
|
|
206
|
-
* @returns {Promise<
|
|
207
|
+
* @returns {Promise<ServiceHandlerMethod>}
|
|
207
208
|
* @private
|
|
208
209
|
*/
|
|
209
210
|
#identifyService( serviceAddress ) {
|
|
@@ -128,13 +128,13 @@ class ServiceProvider extends ServiceConsumer {
|
|
|
128
128
|
*
|
|
129
129
|
* @method
|
|
130
130
|
* @param {ServiceDefinition} serviceDefinition Full service definition object.
|
|
131
|
-
* @param {
|
|
131
|
+
* @param {ServiceHandlerMethod} [defaultServiceHandler=undefined] A default service handler in case there is one.
|
|
132
132
|
* @return {Promise}
|
|
133
133
|
* @public
|
|
134
134
|
*/
|
|
135
135
|
registerService( serviceDefinition, defaultServiceHandler = undefined ) {
|
|
136
136
|
return new Promise( ( resolve, reject ) => {
|
|
137
|
-
/** @type {
|
|
137
|
+
/** @type {ServiceHandlerMethod} */
|
|
138
138
|
let serviceHandler = null;
|
|
139
139
|
if ( serviceDefinition.serviceFile ) {
|
|
140
140
|
let filePath = process.cwd() + serviceDefinition.serviceFile + ( ( _.endsWith( serviceDefinition.serviceFile, ".js" ) ) ? "" : ".js" );
|
|
@@ -160,7 +160,7 @@ class ServiceProvider extends ServiceConsumer {
|
|
|
160
160
|
if ( typeof ( serviceHandler ) === "function" ) {
|
|
161
161
|
// make sure we have a version and parent service provider specified:
|
|
162
162
|
serviceDefinition.serviceVersion = serviceDefinition.serviceVersion || 1;
|
|
163
|
-
this.#serviceExecutor.addServiceHandler( serviceHandler, serviceDefinition );
|
|
163
|
+
this.#serviceExecutor.addServiceHandler( serviceHandler, serviceDefinition, this );
|
|
164
164
|
resolve();
|
|
165
165
|
} else {
|
|
166
166
|
reject( exceptions.raise( exceptions.exceptionCode.E_GEN_BAD_SERVICE_HANDLER ) );
|
|
@@ -173,7 +173,7 @@ class ServiceProvider extends ServiceConsumer {
|
|
|
173
173
|
*
|
|
174
174
|
* @method
|
|
175
175
|
* @param {ServiceDefinition[]} serviceDefinitions
|
|
176
|
-
* @param {
|
|
176
|
+
* @param {ServiceHandlerMethod} [defaultServiceHandler=undefined]
|
|
177
177
|
* @return {Promise}
|
|
178
178
|
* @public
|
|
179
179
|
*/
|
package/package.json
CHANGED
|
@@ -1,14 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ti-engine/core",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "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.",
|
|
5
5
|
"author": "Boris Kostadinov <kostadinov.boris@gmail.com>",
|
|
6
6
|
"license": "ISC",
|
|
7
|
+
"main": "./start-instance.js",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"start": "node ./start-instance.js"
|
|
10
|
+
},
|
|
7
11
|
"exports": {
|
|
8
12
|
"./service-consumer": "./components/service-consumer.js",
|
|
9
13
|
"./service-instance": "./components/service-instance.js",
|
|
10
14
|
"./service-provider": "./components/service-provider.js",
|
|
11
15
|
"./exceptions": "./utils/exceptions.js",
|
|
16
|
+
"./logger": "./utils/logger.js",
|
|
12
17
|
"./tools": "./utils/tools.js"
|
|
13
18
|
},
|
|
14
19
|
"imports": {
|
|
@@ -49,6 +54,10 @@
|
|
|
49
54
|
"optionalDependencies": {
|
|
50
55
|
"@google-cloud/error-reporting": "^2.0.4"
|
|
51
56
|
},
|
|
57
|
+
"repository": {
|
|
58
|
+
"type": "git",
|
|
59
|
+
"url": "https://github.com/Belleal/ti-engine.git"
|
|
60
|
+
},
|
|
52
61
|
"engines": {
|
|
53
62
|
"node": ">=14.17.0"
|
|
54
63
|
}
|
package/start-instance.js
CHANGED
package/utils/exceptions.js
CHANGED
|
@@ -69,8 +69,9 @@ class Exception {
|
|
|
69
69
|
* @param {string} id The unique ID to be assigned to this exception.
|
|
70
70
|
* @param {TiExceptionCode} exceptionCode An unique exception identifier. If this is not recognized, the default error code will be used instead.
|
|
71
71
|
* @param {Object} [data] Any additional data to insert into the exception.
|
|
72
|
+
* @param {string} [description] Description of the exception.
|
|
72
73
|
*/
|
|
73
|
-
constructor( id, exceptionCode, data ) {
|
|
74
|
+
constructor( id, exceptionCode, data, description ) {
|
|
74
75
|
exceptionCode = ( exceptionCodeEnum.properties[ exceptionCode ] ) ? exceptionCode : module.exports.exceptionCode.E_UNKNOWN_ERROR;
|
|
75
76
|
|
|
76
77
|
this.#id = id;
|
|
@@ -184,7 +185,8 @@ class Exception {
|
|
|
184
185
|
code: this.code,
|
|
185
186
|
httpCode: this.httpCode,
|
|
186
187
|
label: this.label,
|
|
187
|
-
description: this.description
|
|
188
|
+
description: this.description,
|
|
189
|
+
data: this.data
|
|
188
190
|
};
|
|
189
191
|
}
|
|
190
192
|
}
|
|
@@ -211,6 +213,8 @@ module.exports.raise = ( source, data, exceptionID ) => {
|
|
|
211
213
|
exception = new Exception( exceptionID || tools.getUUID(), module.exports.exceptionCode.E_GEN_JS_INTERNAL_ERROR, {
|
|
212
214
|
message: source
|
|
213
215
|
} );
|
|
216
|
+
} else if ( _.isObjectLike( source ) ) {
|
|
217
|
+
exception = new Exception( exceptionID || ( source.id || tools.getUUID() ), source.code || module.exports.exceptionCode.E_GEN_JS_INTERNAL_ERROR, source.data, source.description );
|
|
214
218
|
} else {
|
|
215
219
|
exception = new Exception( exceptionID || tools.getUUID(), ( exceptionCodeEnum.properties[ source ] ) ? source : module.exports.exceptionCode.E_UNKNOWN_ERROR );
|
|
216
220
|
}
|