@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
|
@@ -1,191 +1,191 @@
|
|
|
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 config = require( "#config" );
|
|
10
|
-
const tools = require( "#tools" );
|
|
11
|
-
const exceptions = require( "#exceptions" );
|
|
12
|
-
const redis = require( "#redis-integration" );
|
|
13
|
-
|
|
14
|
-
/**
|
|
15
|
-
* Used to create a Redis Cache client wrapped in a specialized message memory cache interface.
|
|
16
|
-
*
|
|
17
|
-
* @class MessageMemoryCache
|
|
18
|
-
* @public
|
|
19
|
-
*/
|
|
20
|
-
class MessageMemoryCache {
|
|
21
|
-
|
|
22
|
-
#redisClient = null;
|
|
23
|
-
#isShuttingDown = false;
|
|
24
|
-
|
|
25
|
-
/**
|
|
26
|
-
* @constructor
|
|
27
|
-
* @param {string} identifier The connection identifier for the Redis connection.
|
|
28
|
-
* @returns {MessageMemoryCache}
|
|
29
|
-
*/
|
|
30
|
-
constructor( identifier ) {
|
|
31
|
-
this.#redisClient = redis.createRedisClient( identifier );
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
/* Public interface */
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Used to initialize the cache service.
|
|
38
|
-
*
|
|
39
|
-
* @method
|
|
40
|
-
* @returns {Promise}
|
|
41
|
-
* @public
|
|
42
|
-
*/
|
|
43
|
-
initialize() {
|
|
44
|
-
let host = config.getSetting( config.setting.MEMORY_CACHE_REDIS_HOST );
|
|
45
|
-
let port = config.getSetting( config.setting.MEMORY_CACHE_REDIS_PORT );
|
|
46
|
-
let db = config.getSetting( config.setting.MEMORY_CACHE_REDIS_DB );
|
|
47
|
-
let authKey = config.getSetting( config.setting.MEMORY_CACHE_AUTH_KEY );
|
|
48
|
-
let user = config.getSetting( config.setting.MEMORY_CACHE_USER );
|
|
49
|
-
let retryMaxAttempts = config.getSetting( config.setting.MEMORY_CACHE_RETRY_MAX_ATTEMPTS );
|
|
50
|
-
let retryMaxInterval = config.getSetting( config.setting.MEMORY_CACHE_RETRY_MAX_INTERVAL );
|
|
51
|
-
|
|
52
|
-
return this.#redisClient.initialize( host, port, authKey, user, db, retryMaxInterval, retryMaxAttempts );
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Used to gracefully shut down the cache service.
|
|
57
|
-
*
|
|
58
|
-
* @method
|
|
59
|
-
* @param {number} [timeoutMs]
|
|
60
|
-
* @returns {Promise}
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
shutDown( timeoutMs ) {
|
|
64
|
-
this.#isShuttingDown = true;
|
|
65
|
-
return this.#redisClient.shutDown( timeoutMs );
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
/**
|
|
69
|
-
* Used to register a new {@link ConnectionObserver} for events related to the Redis connection state.
|
|
70
|
-
*
|
|
71
|
-
* @method
|
|
72
|
-
* @param {ConnectionObserver} connectionObserver The {@link ConnectionObserver} that will be notified of any changes.
|
|
73
|
-
* @public
|
|
74
|
-
*/
|
|
75
|
-
addConnectionObserver( connectionObserver ) {
|
|
76
|
-
this.#redisClient.addConnectionObserver( connectionObserver );
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/**
|
|
80
|
-
* Used to send a message to the specified route.
|
|
81
|
-
*
|
|
82
|
-
* @method
|
|
83
|
-
* @param {Message} message The message to send.
|
|
84
|
-
* @param {string} queue The destination queue for the message as recognized by the {@link MessageExchange} implementation.
|
|
85
|
-
* @returns {Promise<number>} Will return the destination queue length after adding the current message to it.
|
|
86
|
-
* @public
|
|
87
|
-
*/
|
|
88
|
-
sendMessage( message, queue ) {
|
|
89
|
-
return new Promise( ( resolve, reject ) => {
|
|
90
|
-
if ( this.#isShuttingDown === true ) {
|
|
91
|
-
reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_SENDER_UNAVAILABLE ) );
|
|
92
|
-
} else {
|
|
93
|
-
let command = [ redis.cacheCommands.LIST_PUSH, queue, tools.stringifyJSON( message ) ];
|
|
94
|
-
this.#redisClient.executeCommands( [ command ] ).then( ( results ) => {
|
|
95
|
-
results = results[ 0 ];
|
|
96
|
-
resolve( ( results && results.length > 1 ) ? results[ 1 ] : undefined );
|
|
97
|
-
} ).catch( ( error ) => {
|
|
98
|
-
reject( exceptions.raise( error ) );
|
|
99
|
-
} );
|
|
100
|
-
}
|
|
101
|
-
} );
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Used to store a message payload.
|
|
106
|
-
*
|
|
107
|
-
* @method
|
|
108
|
-
* @param {Object} payload
|
|
109
|
-
* @param {string} storeLocation
|
|
110
|
-
* @returns {Promise<string>} Will return a unique ID of the storage location for the payload.
|
|
111
|
-
* @public
|
|
112
|
-
*/
|
|
113
|
-
storeMessagePayload( payload, storeLocation ) {
|
|
114
|
-
return new Promise( ( resolve, reject ) => {
|
|
115
|
-
if ( payload ) {
|
|
116
|
-
let storeID = tools.getUUID();
|
|
117
|
-
let command = [ redis.cacheCommands.HASH_SET, storeLocation, storeID, tools.stringifyJSON( payload ) ];
|
|
118
|
-
this.#redisClient.executeCommands( [ command ] ).then( () => {
|
|
119
|
-
resolve( storeID );
|
|
120
|
-
} ).catch( ( error ) => {
|
|
121
|
-
reject( exceptions.raise( error ) );
|
|
122
|
-
} );
|
|
123
|
-
} else {
|
|
124
|
-
resolve();
|
|
125
|
-
}
|
|
126
|
-
} );
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Used to receive a message from the specified queue.
|
|
131
|
-
*
|
|
132
|
-
* @method
|
|
133
|
-
* @param {string} queue
|
|
134
|
-
* @returns {Promise<Message>}
|
|
135
|
-
* @public
|
|
136
|
-
*/
|
|
137
|
-
receiveMessage( queue ) {
|
|
138
|
-
return new Promise( ( resolve, reject ) => {
|
|
139
|
-
this.#redisClient.blockingCommand( redis.cacheCommands.LIST_POP_TAIL_BLOCKING, [ queue, 0 ] ).then( ( results ) => {
|
|
140
|
-
results = ( results && results.length > 1 ) ? results[ 1 ] : undefined;
|
|
141
|
-
resolve( tools.parseJSON( results ) );
|
|
142
|
-
} ).catch( ( error ) => {
|
|
143
|
-
if ( this.#isShuttingDown === true ) {
|
|
144
|
-
reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_RECEIVER_UNAVAILABLE ) );
|
|
145
|
-
} else {
|
|
146
|
-
reject( exceptions.raise( error ) );
|
|
147
|
-
}
|
|
148
|
-
} );
|
|
149
|
-
} );
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Used to retrieve a message payload by its store ID.
|
|
154
|
-
*
|
|
155
|
-
* @method
|
|
156
|
-
* @param {Message} message
|
|
157
|
-
* @param {string} storeLocation
|
|
158
|
-
* @returns {Promise<Message>} Will return the message with its payload populated if such is found.
|
|
159
|
-
* @public
|
|
160
|
-
*/
|
|
161
|
-
retrieveMessagePayload( message, storeLocation ) {
|
|
162
|
-
return new Promise( ( resolve, reject ) => {
|
|
163
|
-
if ( message.payload ) {
|
|
164
|
-
let command1 = [ redis.cacheCommands.HASH_GET, storeLocation, message.payload ];
|
|
165
|
-
let command2 = [ redis.cacheCommands.HASH_REMOVE, storeLocation, message.payload ];
|
|
166
|
-
this.#redisClient.executeCommands( [ command1, command2 ] ).then( ( results ) => {
|
|
167
|
-
results = results[ 0 ];
|
|
168
|
-
message.payload = ( results && results.length > 1 ) ? tools.parseJSON( results[ 1 ] ) : undefined;
|
|
169
|
-
resolve( message );
|
|
170
|
-
} ).catch( ( error ) => {
|
|
171
|
-
reject( exceptions.raise( error ) );
|
|
172
|
-
} );
|
|
173
|
-
} else {
|
|
174
|
-
resolve( message );
|
|
175
|
-
}
|
|
176
|
-
} );
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
/**
|
|
182
|
-
* Used to create a new message memory cache.
|
|
183
|
-
*
|
|
184
|
-
* @method
|
|
185
|
-
* @param {string} identifier The connection identifier for the Redis connection.
|
|
186
|
-
* @returns {MessageMemoryCache}
|
|
187
|
-
* @public
|
|
188
|
-
*/
|
|
189
|
-
module.exports.create = ( identifier ) => {
|
|
190
|
-
return Object.freeze( new MessageMemoryCache( identifier ) );
|
|
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 config = require( "#config" );
|
|
10
|
+
const tools = require( "#tools" );
|
|
11
|
+
const exceptions = require( "#exceptions" );
|
|
12
|
+
const redis = require( "#redis-integration" );
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Used to create a Redis Cache client wrapped in a specialized message memory cache interface.
|
|
16
|
+
*
|
|
17
|
+
* @class MessageMemoryCache
|
|
18
|
+
* @public
|
|
19
|
+
*/
|
|
20
|
+
class MessageMemoryCache {
|
|
21
|
+
|
|
22
|
+
#redisClient = null;
|
|
23
|
+
#isShuttingDown = false;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* @constructor
|
|
27
|
+
* @param {string} identifier The connection identifier for the Redis connection.
|
|
28
|
+
* @returns {MessageMemoryCache}
|
|
29
|
+
*/
|
|
30
|
+
constructor( identifier ) {
|
|
31
|
+
this.#redisClient = redis.createRedisClient( identifier );
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/* Public interface */
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Used to initialize the cache service.
|
|
38
|
+
*
|
|
39
|
+
* @method
|
|
40
|
+
* @returns {Promise}
|
|
41
|
+
* @public
|
|
42
|
+
*/
|
|
43
|
+
initialize() {
|
|
44
|
+
let host = config.getSetting( config.setting.MEMORY_CACHE_REDIS_HOST );
|
|
45
|
+
let port = config.getSetting( config.setting.MEMORY_CACHE_REDIS_PORT );
|
|
46
|
+
let db = config.getSetting( config.setting.MEMORY_CACHE_REDIS_DB );
|
|
47
|
+
let authKey = config.getSetting( config.setting.MEMORY_CACHE_AUTH_KEY );
|
|
48
|
+
let user = config.getSetting( config.setting.MEMORY_CACHE_USER );
|
|
49
|
+
let retryMaxAttempts = config.getSetting( config.setting.MEMORY_CACHE_RETRY_MAX_ATTEMPTS );
|
|
50
|
+
let retryMaxInterval = config.getSetting( config.setting.MEMORY_CACHE_RETRY_MAX_INTERVAL );
|
|
51
|
+
|
|
52
|
+
return this.#redisClient.initialize( host, port, authKey, user, db, retryMaxInterval, retryMaxAttempts );
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Used to gracefully shut down the cache service.
|
|
57
|
+
*
|
|
58
|
+
* @method
|
|
59
|
+
* @param {number} [timeoutMs]
|
|
60
|
+
* @returns {Promise}
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
shutDown( timeoutMs ) {
|
|
64
|
+
this.#isShuttingDown = true;
|
|
65
|
+
return this.#redisClient.shutDown( timeoutMs );
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Used to register a new {@link ConnectionObserver} for events related to the Redis connection state.
|
|
70
|
+
*
|
|
71
|
+
* @method
|
|
72
|
+
* @param {ConnectionObserver} connectionObserver The {@link ConnectionObserver} that will be notified of any changes.
|
|
73
|
+
* @public
|
|
74
|
+
*/
|
|
75
|
+
addConnectionObserver( connectionObserver ) {
|
|
76
|
+
this.#redisClient.addConnectionObserver( connectionObserver );
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Used to send a message to the specified route.
|
|
81
|
+
*
|
|
82
|
+
* @method
|
|
83
|
+
* @param {Message} message The message to send.
|
|
84
|
+
* @param {string} queue The destination queue for the message as recognized by the {@link MessageExchange} implementation.
|
|
85
|
+
* @returns {Promise<number>} Will return the destination queue length after adding the current message to it.
|
|
86
|
+
* @public
|
|
87
|
+
*/
|
|
88
|
+
sendMessage( message, queue ) {
|
|
89
|
+
return new Promise( ( resolve, reject ) => {
|
|
90
|
+
if ( this.#isShuttingDown === true ) {
|
|
91
|
+
reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_SENDER_UNAVAILABLE ) );
|
|
92
|
+
} else {
|
|
93
|
+
let command = [ redis.cacheCommands.LIST_PUSH, queue, tools.stringifyJSON( message ) ];
|
|
94
|
+
this.#redisClient.executeCommands( [ command ] ).then( ( results ) => {
|
|
95
|
+
results = results[ 0 ];
|
|
96
|
+
resolve( ( results && results.length > 1 ) ? results[ 1 ] : undefined );
|
|
97
|
+
} ).catch( ( error ) => {
|
|
98
|
+
reject( exceptions.raise( error ) );
|
|
99
|
+
} );
|
|
100
|
+
}
|
|
101
|
+
} );
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Used to store a message payload.
|
|
106
|
+
*
|
|
107
|
+
* @method
|
|
108
|
+
* @param {Object} payload
|
|
109
|
+
* @param {string} storeLocation
|
|
110
|
+
* @returns {Promise<string>} Will return a unique ID of the storage location for the payload.
|
|
111
|
+
* @public
|
|
112
|
+
*/
|
|
113
|
+
storeMessagePayload( payload, storeLocation ) {
|
|
114
|
+
return new Promise( ( resolve, reject ) => {
|
|
115
|
+
if ( payload ) {
|
|
116
|
+
let storeID = tools.getUUID();
|
|
117
|
+
let command = [ redis.cacheCommands.HASH_SET, storeLocation, storeID, tools.stringifyJSON( payload ) ];
|
|
118
|
+
this.#redisClient.executeCommands( [ command ] ).then( () => {
|
|
119
|
+
resolve( storeID );
|
|
120
|
+
} ).catch( ( error ) => {
|
|
121
|
+
reject( exceptions.raise( error ) );
|
|
122
|
+
} );
|
|
123
|
+
} else {
|
|
124
|
+
resolve();
|
|
125
|
+
}
|
|
126
|
+
} );
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Used to receive a message from the specified queue.
|
|
131
|
+
*
|
|
132
|
+
* @method
|
|
133
|
+
* @param {string} queue
|
|
134
|
+
* @returns {Promise<Message>}
|
|
135
|
+
* @public
|
|
136
|
+
*/
|
|
137
|
+
receiveMessage( queue ) {
|
|
138
|
+
return new Promise( ( resolve, reject ) => {
|
|
139
|
+
this.#redisClient.blockingCommand( redis.cacheCommands.LIST_POP_TAIL_BLOCKING, [ queue, 0 ] ).then( ( results ) => {
|
|
140
|
+
results = ( results && results.length > 1 ) ? results[ 1 ] : undefined;
|
|
141
|
+
resolve( tools.parseJSON( results ) );
|
|
142
|
+
} ).catch( ( error ) => {
|
|
143
|
+
if ( this.#isShuttingDown === true ) {
|
|
144
|
+
reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_RECEIVER_UNAVAILABLE ) );
|
|
145
|
+
} else {
|
|
146
|
+
reject( exceptions.raise( error ) );
|
|
147
|
+
}
|
|
148
|
+
} );
|
|
149
|
+
} );
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Used to retrieve a message payload by its store ID.
|
|
154
|
+
*
|
|
155
|
+
* @method
|
|
156
|
+
* @param {Message} message
|
|
157
|
+
* @param {string} storeLocation
|
|
158
|
+
* @returns {Promise<Message>} Will return the message with its payload populated if such is found.
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
retrieveMessagePayload( message, storeLocation ) {
|
|
162
|
+
return new Promise( ( resolve, reject ) => {
|
|
163
|
+
if ( message.payload ) {
|
|
164
|
+
let command1 = [ redis.cacheCommands.HASH_GET, storeLocation, message.payload ];
|
|
165
|
+
let command2 = [ redis.cacheCommands.HASH_REMOVE, storeLocation, message.payload ];
|
|
166
|
+
this.#redisClient.executeCommands( [ command1, command2 ] ).then( ( results ) => {
|
|
167
|
+
results = results[ 0 ];
|
|
168
|
+
message.payload = ( results && results.length > 1 ) ? tools.parseJSON( results[ 1 ] ) : undefined;
|
|
169
|
+
resolve( message );
|
|
170
|
+
} ).catch( ( error ) => {
|
|
171
|
+
reject( exceptions.raise( error ) );
|
|
172
|
+
} );
|
|
173
|
+
} else {
|
|
174
|
+
resolve( message );
|
|
175
|
+
}
|
|
176
|
+
} );
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Used to create a new message memory cache.
|
|
183
|
+
*
|
|
184
|
+
* @method
|
|
185
|
+
* @param {string} identifier The connection identifier for the Redis connection.
|
|
186
|
+
* @returns {MessageMemoryCache}
|
|
187
|
+
* @public
|
|
188
|
+
*/
|
|
189
|
+
module.exports.create = ( identifier ) => {
|
|
190
|
+
return Object.freeze( new MessageMemoryCache( identifier ) );
|
|
191
191
|
};
|
|
@@ -1,127 +1,127 @@
|
|
|
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 ConnectionObserver = require( "#connection-observer" );
|
|
10
|
-
const _ = require( "lodash" );
|
|
11
|
-
const exceptions = require( "#exceptions" );
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* An abstract class that allows the child class to observe and take action on message events.
|
|
15
|
-
* <br/>
|
|
16
|
-
* NOTE: This class inherits {@link ConnectionObserver} so it can also act in that capacity.
|
|
17
|
-
*
|
|
18
|
-
* @class MessageObserver
|
|
19
|
-
* @extends ConnectionObserver
|
|
20
|
-
* @abstract
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
|
-
class MessageObserver extends ConnectionObserver {
|
|
24
|
-
|
|
25
|
-
#priority = 0;
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* @constructor
|
|
29
|
-
* @param {number} [priority=0] The priority of this observer. Higher values indicate higher priority.
|
|
30
|
-
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
31
|
-
*/
|
|
32
|
-
constructor( priority = 0 ) {
|
|
33
|
-
super();
|
|
34
|
-
|
|
35
|
-
// make sure this abstract class cannot be instantiated:
|
|
36
|
-
if ( new.target === MessageObserver ) {
|
|
37
|
-
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
this.#priority = _.isNumber( priority ) ? priority : 0;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
/**
|
|
44
|
-
* Returns the priority of this observer.
|
|
45
|
-
* <br/>
|
|
46
|
-
* NOTE: Higher values indicate higher priority.
|
|
47
|
-
*
|
|
48
|
-
* @property
|
|
49
|
-
* @returns {number}
|
|
50
|
-
*/
|
|
51
|
-
get priority() {
|
|
52
|
-
return this.#priority;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Used to set the priority of this observer.
|
|
57
|
-
* <br/>
|
|
58
|
-
* NOTE: Higher values indicate higher priority.
|
|
59
|
-
*
|
|
60
|
-
* @property
|
|
61
|
-
* @param {number} value
|
|
62
|
-
*/
|
|
63
|
-
set priority( value ) {
|
|
64
|
-
this.#priority = _.isNumber( value ) ? value : 0;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Needs to be invoked by the message handler once a message enters its logic for processing.
|
|
69
|
-
* <br/>
|
|
70
|
-
* NOTE: Override this to add custom functionality.
|
|
71
|
-
*
|
|
72
|
-
* @method
|
|
73
|
-
* @param {string} identifier The identifier of the observed connection.
|
|
74
|
-
* @param {Message} message The message for processing.
|
|
75
|
-
* @returns {Message} The message that was received.
|
|
76
|
-
* @virtual
|
|
77
|
-
* @public
|
|
78
|
-
*/
|
|
79
|
-
onMessage( identifier, message ) {
|
|
80
|
-
return message;
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Needs to be invoked by the connection handler when the connection is disrupted.
|
|
85
|
-
* <br/>
|
|
86
|
-
* NOTE: Override this to add custom functionality.
|
|
87
|
-
*
|
|
88
|
-
* @method
|
|
89
|
-
* @param {string} identifier The identifier of the observed connection.
|
|
90
|
-
* @virtual
|
|
91
|
-
* @public
|
|
92
|
-
*/
|
|
93
|
-
onConnectionDisrupted( identifier ) {
|
|
94
|
-
super.onConnectionDisrupted( identifier );
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Needs to be invoked by the connection handler when the connection is recovered.
|
|
99
|
-
* <br/>
|
|
100
|
-
* NOTE: Override this to add custom functionality.
|
|
101
|
-
*
|
|
102
|
-
* @method
|
|
103
|
-
* @param {string} identifier The identifier of the observed connection.
|
|
104
|
-
* @virtual
|
|
105
|
-
* @public
|
|
106
|
-
*/
|
|
107
|
-
onConnectionRecovered( identifier ) {
|
|
108
|
-
super.onConnectionRecovered( identifier );
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Needs to be invoked by the connection handler when the connection is irrevocably lost.
|
|
113
|
-
* <br/>
|
|
114
|
-
* NOTE: Override this to add custom functionality.
|
|
115
|
-
*
|
|
116
|
-
* @method
|
|
117
|
-
* @param {string} identifier The identifier of the observed connection.
|
|
118
|
-
* @virtual
|
|
119
|
-
* @public
|
|
120
|
-
*/
|
|
121
|
-
onConnectionLost( identifier ) {
|
|
122
|
-
super.onConnectionLost( identifier );
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
}
|
|
126
|
-
|
|
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 ConnectionObserver = require( "#connection-observer" );
|
|
10
|
+
const _ = require( "lodash" );
|
|
11
|
+
const exceptions = require( "#exceptions" );
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* An abstract class that allows the child class to observe and take action on message events.
|
|
15
|
+
* <br/>
|
|
16
|
+
* NOTE: This class inherits {@link ConnectionObserver} so it can also act in that capacity.
|
|
17
|
+
*
|
|
18
|
+
* @class MessageObserver
|
|
19
|
+
* @extends ConnectionObserver
|
|
20
|
+
* @abstract
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
class MessageObserver extends ConnectionObserver {
|
|
24
|
+
|
|
25
|
+
#priority = 0;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* @constructor
|
|
29
|
+
* @param {number} [priority=0] The priority of this observer. Higher values indicate higher priority.
|
|
30
|
+
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
31
|
+
*/
|
|
32
|
+
constructor( priority = 0 ) {
|
|
33
|
+
super();
|
|
34
|
+
|
|
35
|
+
// make sure this abstract class cannot be instantiated:
|
|
36
|
+
if ( new.target === MessageObserver ) {
|
|
37
|
+
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
this.#priority = _.isNumber( priority ) ? priority : 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* Returns the priority of this observer.
|
|
45
|
+
* <br/>
|
|
46
|
+
* NOTE: Higher values indicate higher priority.
|
|
47
|
+
*
|
|
48
|
+
* @property
|
|
49
|
+
* @returns {number}
|
|
50
|
+
*/
|
|
51
|
+
get priority() {
|
|
52
|
+
return this.#priority;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Used to set the priority of this observer.
|
|
57
|
+
* <br/>
|
|
58
|
+
* NOTE: Higher values indicate higher priority.
|
|
59
|
+
*
|
|
60
|
+
* @property
|
|
61
|
+
* @param {number} value
|
|
62
|
+
*/
|
|
63
|
+
set priority( value ) {
|
|
64
|
+
this.#priority = _.isNumber( value ) ? value : 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Needs to be invoked by the message handler once a message enters its logic for processing.
|
|
69
|
+
* <br/>
|
|
70
|
+
* NOTE: Override this to add custom functionality.
|
|
71
|
+
*
|
|
72
|
+
* @method
|
|
73
|
+
* @param {string} identifier The identifier of the observed connection.
|
|
74
|
+
* @param {Message} message The message for processing.
|
|
75
|
+
* @returns {Message} The message that was received.
|
|
76
|
+
* @virtual
|
|
77
|
+
* @public
|
|
78
|
+
*/
|
|
79
|
+
onMessage( identifier, message ) {
|
|
80
|
+
return message;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Needs to be invoked by the connection handler when the connection is disrupted.
|
|
85
|
+
* <br/>
|
|
86
|
+
* NOTE: Override this to add custom functionality.
|
|
87
|
+
*
|
|
88
|
+
* @method
|
|
89
|
+
* @param {string} identifier The identifier of the observed connection.
|
|
90
|
+
* @virtual
|
|
91
|
+
* @public
|
|
92
|
+
*/
|
|
93
|
+
onConnectionDisrupted( identifier ) {
|
|
94
|
+
super.onConnectionDisrupted( identifier );
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Needs to be invoked by the connection handler when the connection is recovered.
|
|
99
|
+
* <br/>
|
|
100
|
+
* NOTE: Override this to add custom functionality.
|
|
101
|
+
*
|
|
102
|
+
* @method
|
|
103
|
+
* @param {string} identifier The identifier of the observed connection.
|
|
104
|
+
* @virtual
|
|
105
|
+
* @public
|
|
106
|
+
*/
|
|
107
|
+
onConnectionRecovered( identifier ) {
|
|
108
|
+
super.onConnectionRecovered( identifier );
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* Needs to be invoked by the connection handler when the connection is irrevocably lost.
|
|
113
|
+
* <br/>
|
|
114
|
+
* NOTE: Override this to add custom functionality.
|
|
115
|
+
*
|
|
116
|
+
* @method
|
|
117
|
+
* @param {string} identifier The identifier of the observed connection.
|
|
118
|
+
* @virtual
|
|
119
|
+
* @public
|
|
120
|
+
*/
|
|
121
|
+
onConnectionLost( identifier ) {
|
|
122
|
+
super.onConnectionLost( identifier );
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
}
|
|
126
|
+
|
|
127
127
|
module.exports = MessageObserver;
|