@ti-engine/core 1.7.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 -377
- package/LICENSE.md +321 -321
- package/README.md +597 -597
- package/bin/localization/labels.json +122 -122
- package/bin/settings.json +41 -41
- package/bin/start-instance.js +164 -164
- 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 -235
- 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 -89
- 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,182 +1,182 @@
|
|
|
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 MessageHandler = require( "#message-handler" );
|
|
10
|
-
const logger = require( "#logger" );
|
|
11
|
-
const exceptions = require( "#exceptions" );
|
|
12
|
-
const config = require( "#config" );
|
|
13
|
-
const tools = require( "#tools" );
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* An abstract class that defines a basic message receiver behavior.
|
|
17
|
-
*
|
|
18
|
-
* @class MessageReceiver
|
|
19
|
-
* @extends MessageHandler
|
|
20
|
-
* @abstract
|
|
21
|
-
* @public
|
|
22
|
-
*/
|
|
23
|
-
class MessageReceiver extends MessageHandler {
|
|
24
|
-
|
|
25
|
-
#receiveQueue;
|
|
26
|
-
#isReceiving = false;
|
|
27
|
-
|
|
28
|
-
/**
|
|
29
|
-
* @constructor
|
|
30
|
-
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
31
|
-
* @param {string} receiveQueue The queue from which the messages will be received.
|
|
32
|
-
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
33
|
-
*/
|
|
34
|
-
constructor( identifier, receiveQueue ) {
|
|
35
|
-
super( identifier );
|
|
36
|
-
|
|
37
|
-
// make sure this abstract class cannot be instantiated:
|
|
38
|
-
if ( new.target === MessageReceiver ) {
|
|
39
|
-
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
this.#receiveQueue = receiveQueue;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
/* Public interface */
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* Property returning the configured receiving queue.
|
|
49
|
-
*
|
|
50
|
-
* @property
|
|
51
|
-
* @returns {string}
|
|
52
|
-
* @public
|
|
53
|
-
*/
|
|
54
|
-
get receiveQueue() {
|
|
55
|
-
return this.#receiveQueue;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
/**
|
|
59
|
-
* Indicates whether the receiver is currently receiving messages.
|
|
60
|
-
*
|
|
61
|
-
* @property
|
|
62
|
-
* @returns {boolean}
|
|
63
|
-
* @public
|
|
64
|
-
*/
|
|
65
|
-
get isReceiving() {
|
|
66
|
-
return this.#isReceiving;
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
/**
|
|
70
|
-
* Used to set the value of the {@link MessageReceiver#isReceiving} property.
|
|
71
|
-
*
|
|
72
|
-
* @property
|
|
73
|
-
* @param {boolean} value
|
|
74
|
-
* @public
|
|
75
|
-
*/
|
|
76
|
-
set isReceiving( value ) {
|
|
77
|
-
this.#isReceiving = value;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
* Used to initialize and enable the communication capabilities of the handler.
|
|
82
|
-
* <br/>
|
|
83
|
-
* NOTE: Override this to add functionality.
|
|
84
|
-
*
|
|
85
|
-
* @method
|
|
86
|
-
* @returns {Promise}
|
|
87
|
-
* @abstract
|
|
88
|
-
* @public
|
|
89
|
-
*/
|
|
90
|
-
enable() {
|
|
91
|
-
return super.enable();
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Used to shut down and disable the communication behavior of the handler.
|
|
96
|
-
* <br/>
|
|
97
|
-
* NOTE: Override this to add functionality.
|
|
98
|
-
*
|
|
99
|
-
* @method
|
|
100
|
-
* @returns {Promise}
|
|
101
|
-
* @abstract
|
|
102
|
-
* @public
|
|
103
|
-
*/
|
|
104
|
-
disable() {
|
|
105
|
-
return super.disable();
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Used to receive messages.
|
|
110
|
-
* <br/>
|
|
111
|
-
* NOTE: This method will start a recursion of subsequent receive calls that will continue even if an individual message fetch fails.
|
|
112
|
-
*
|
|
113
|
-
* @method
|
|
114
|
-
* @recursion
|
|
115
|
-
* @public
|
|
116
|
-
*/
|
|
117
|
-
receive() {
|
|
118
|
-
if ( this.isReceiving === true ) {
|
|
119
|
-
this.onReceive().then( ( message ) => {
|
|
120
|
-
return this.#postReceive( message );
|
|
121
|
-
} ).then( ( message ) => {
|
|
122
|
-
this.notifyMessageObservers( message );
|
|
123
|
-
} ).catch( ( error ) => {
|
|
124
|
-
if ( error.code !== exceptions.exceptionCode.E_COM_MESSAGE_RECEIVER_UNAVAILABLE ) {
|
|
125
|
-
logger.log( `Error while trying to receive the next pending message from memory cache in receiver '${ this.connectionIdentifier }'! Resuming operation...`, logger.logSeverity.ERROR, error );
|
|
126
|
-
}
|
|
127
|
-
} ).finally( () => {
|
|
128
|
-
this.receive();
|
|
129
|
-
} );
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
/**
|
|
134
|
-
* Used to receive messages.
|
|
135
|
-
* <br/>
|
|
136
|
-
* NOTE: This method will be called automatically even if overridden.
|
|
137
|
-
* <br/>
|
|
138
|
-
* NOTE: Override this to add functionality.
|
|
139
|
-
*
|
|
140
|
-
* @method
|
|
141
|
-
* @returns {Promise<Message>}
|
|
142
|
-
* @abstract
|
|
143
|
-
* @public
|
|
144
|
-
*/
|
|
145
|
-
onReceive() {
|
|
146
|
-
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_METHOD_CALL, { name: this.constructor.name + "." + this.onReceive.name } ) );
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
/* Private interface */
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Used to process the received message before providing it to any {@link MessageObserver}.
|
|
153
|
-
*
|
|
154
|
-
* @method
|
|
155
|
-
* @param {Message} message
|
|
156
|
-
* @returns {Promise<Message>}
|
|
157
|
-
* @private
|
|
158
|
-
*/
|
|
159
|
-
#postReceive( message ) {
|
|
160
|
-
return new Promise( ( resolve, reject ) => {
|
|
161
|
-
if ( config.getSetting( config.setting.MESSAGE_EXCHANGE_SECURITY_HASH_ENABLED ) === true ) {
|
|
162
|
-
let receivedHash = message.hash;
|
|
163
|
-
delete message.hash;
|
|
164
|
-
let currentHash = this.createMessageHash( message );
|
|
165
|
-
if ( tools.constantTimeEquals( receivedHash, currentHash ) ) {
|
|
166
|
-
resolve( message );
|
|
167
|
-
} else {
|
|
168
|
-
reject( exceptions.raise( exceptions.exceptionCode.E_SEC_MESSAGE_TAMPERING_DETECTED, {
|
|
169
|
-
messageID: message.messageID,
|
|
170
|
-
receivedHash: receivedHash,
|
|
171
|
-
currentHash: currentHash
|
|
172
|
-
} ) );
|
|
173
|
-
}
|
|
174
|
-
} else {
|
|
175
|
-
resolve( message );
|
|
176
|
-
}
|
|
177
|
-
} );
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
|
|
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 MessageHandler = require( "#message-handler" );
|
|
10
|
+
const logger = require( "#logger" );
|
|
11
|
+
const exceptions = require( "#exceptions" );
|
|
12
|
+
const config = require( "#config" );
|
|
13
|
+
const tools = require( "#tools" );
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* An abstract class that defines a basic message receiver behavior.
|
|
17
|
+
*
|
|
18
|
+
* @class MessageReceiver
|
|
19
|
+
* @extends MessageHandler
|
|
20
|
+
* @abstract
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
23
|
+
class MessageReceiver extends MessageHandler {
|
|
24
|
+
|
|
25
|
+
#receiveQueue;
|
|
26
|
+
#isReceiving = false;
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* @constructor
|
|
30
|
+
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
31
|
+
* @param {string} receiveQueue The queue from which the messages will be received.
|
|
32
|
+
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
33
|
+
*/
|
|
34
|
+
constructor( identifier, receiveQueue ) {
|
|
35
|
+
super( identifier );
|
|
36
|
+
|
|
37
|
+
// make sure this abstract class cannot be instantiated:
|
|
38
|
+
if ( new.target === MessageReceiver ) {
|
|
39
|
+
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
this.#receiveQueue = receiveQueue;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/* Public interface */
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Property returning the configured receiving queue.
|
|
49
|
+
*
|
|
50
|
+
* @property
|
|
51
|
+
* @returns {string}
|
|
52
|
+
* @public
|
|
53
|
+
*/
|
|
54
|
+
get receiveQueue() {
|
|
55
|
+
return this.#receiveQueue;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Indicates whether the receiver is currently receiving messages.
|
|
60
|
+
*
|
|
61
|
+
* @property
|
|
62
|
+
* @returns {boolean}
|
|
63
|
+
* @public
|
|
64
|
+
*/
|
|
65
|
+
get isReceiving() {
|
|
66
|
+
return this.#isReceiving;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Used to set the value of the {@link MessageReceiver#isReceiving} property.
|
|
71
|
+
*
|
|
72
|
+
* @property
|
|
73
|
+
* @param {boolean} value
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
set isReceiving( value ) {
|
|
77
|
+
this.#isReceiving = value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Used to initialize and enable the communication capabilities of the handler.
|
|
82
|
+
* <br/>
|
|
83
|
+
* NOTE: Override this to add functionality.
|
|
84
|
+
*
|
|
85
|
+
* @method
|
|
86
|
+
* @returns {Promise}
|
|
87
|
+
* @abstract
|
|
88
|
+
* @public
|
|
89
|
+
*/
|
|
90
|
+
enable() {
|
|
91
|
+
return super.enable();
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Used to shut down and disable the communication behavior of the handler.
|
|
96
|
+
* <br/>
|
|
97
|
+
* NOTE: Override this to add functionality.
|
|
98
|
+
*
|
|
99
|
+
* @method
|
|
100
|
+
* @returns {Promise}
|
|
101
|
+
* @abstract
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
disable() {
|
|
105
|
+
return super.disable();
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Used to receive messages.
|
|
110
|
+
* <br/>
|
|
111
|
+
* NOTE: This method will start a recursion of subsequent receive calls that will continue even if an individual message fetch fails.
|
|
112
|
+
*
|
|
113
|
+
* @method
|
|
114
|
+
* @recursion
|
|
115
|
+
* @public
|
|
116
|
+
*/
|
|
117
|
+
receive() {
|
|
118
|
+
if ( this.isReceiving === true ) {
|
|
119
|
+
this.onReceive().then( ( message ) => {
|
|
120
|
+
return this.#postReceive( message );
|
|
121
|
+
} ).then( ( message ) => {
|
|
122
|
+
this.notifyMessageObservers( message );
|
|
123
|
+
} ).catch( ( error ) => {
|
|
124
|
+
if ( error.code !== exceptions.exceptionCode.E_COM_MESSAGE_RECEIVER_UNAVAILABLE ) {
|
|
125
|
+
logger.log( `Error while trying to receive the next pending message from memory cache in receiver '${ this.connectionIdentifier }'! Resuming operation...`, logger.logSeverity.ERROR, error );
|
|
126
|
+
}
|
|
127
|
+
} ).finally( () => {
|
|
128
|
+
this.receive();
|
|
129
|
+
} );
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Used to receive messages.
|
|
135
|
+
* <br/>
|
|
136
|
+
* NOTE: This method will be called automatically even if overridden.
|
|
137
|
+
* <br/>
|
|
138
|
+
* NOTE: Override this to add functionality.
|
|
139
|
+
*
|
|
140
|
+
* @method
|
|
141
|
+
* @returns {Promise<Message>}
|
|
142
|
+
* @abstract
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
145
|
+
onReceive() {
|
|
146
|
+
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_METHOD_CALL, { name: this.constructor.name + "." + this.onReceive.name } ) );
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/* Private interface */
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Used to process the received message before providing it to any {@link MessageObserver}.
|
|
153
|
+
*
|
|
154
|
+
* @method
|
|
155
|
+
* @param {Message} message
|
|
156
|
+
* @returns {Promise<Message>}
|
|
157
|
+
* @private
|
|
158
|
+
*/
|
|
159
|
+
#postReceive( message ) {
|
|
160
|
+
return new Promise( ( resolve, reject ) => {
|
|
161
|
+
if ( config.getSetting( config.setting.MESSAGE_EXCHANGE_SECURITY_HASH_ENABLED ) === true ) {
|
|
162
|
+
let receivedHash = message.hash;
|
|
163
|
+
delete message.hash;
|
|
164
|
+
let currentHash = this.createMessageHash( message );
|
|
165
|
+
if ( tools.constantTimeEquals( receivedHash, currentHash ) ) {
|
|
166
|
+
resolve( message );
|
|
167
|
+
} else {
|
|
168
|
+
reject( exceptions.raise( exceptions.exceptionCode.E_SEC_MESSAGE_TAMPERING_DETECTED, {
|
|
169
|
+
messageID: message.messageID,
|
|
170
|
+
receivedHash: receivedHash,
|
|
171
|
+
currentHash: currentHash
|
|
172
|
+
} ) );
|
|
173
|
+
}
|
|
174
|
+
} else {
|
|
175
|
+
resolve( message );
|
|
176
|
+
}
|
|
177
|
+
} );
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
}
|
|
181
|
+
|
|
182
182
|
module.exports = MessageReceiver;
|
|
@@ -1,143 +1,143 @@
|
|
|
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 MessageHandler = require( "#message-handler" );
|
|
10
|
-
const exceptions = require( "#exceptions" );
|
|
11
|
-
const config = require( "#config" );
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* An abstract class that defines a basic message sender behavior.
|
|
15
|
-
*
|
|
16
|
-
* @class MessageSender
|
|
17
|
-
* @extends MessageHandler
|
|
18
|
-
* @abstract
|
|
19
|
-
* @public
|
|
20
|
-
*/
|
|
21
|
-
class MessageSender extends MessageHandler {
|
|
22
|
-
|
|
23
|
-
/**
|
|
24
|
-
* @constructor
|
|
25
|
-
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
26
|
-
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
27
|
-
*/
|
|
28
|
-
constructor( identifier ) {
|
|
29
|
-
super( identifier );
|
|
30
|
-
|
|
31
|
-
// make sure this abstract class cannot be instantiated:
|
|
32
|
-
if ( new.target === MessageSender ) {
|
|
33
|
-
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/* Public interface */
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Used to initialize and enable the communication capabilities of the handler.
|
|
41
|
-
* <br/>
|
|
42
|
-
* NOTE: Override this to add functionality.
|
|
43
|
-
*
|
|
44
|
-
* @method
|
|
45
|
-
* @returns {Promise}
|
|
46
|
-
* @abstract
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
enable() {
|
|
50
|
-
return super.enable();
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
/**
|
|
54
|
-
* Used to shut down and disable the communication behavior of the handler.
|
|
55
|
-
* <br/>
|
|
56
|
-
* NOTE: Override this to add functionality.
|
|
57
|
-
*
|
|
58
|
-
* @method
|
|
59
|
-
* @returns {Promise}
|
|
60
|
-
* @abstract
|
|
61
|
-
* @public
|
|
62
|
-
*/
|
|
63
|
-
disable() {
|
|
64
|
-
return super.disable();
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
/**
|
|
68
|
-
* Used to send a {@link Message} via this message handler.
|
|
69
|
-
*
|
|
70
|
-
* @method
|
|
71
|
-
* @param {Message} message The message to send.
|
|
72
|
-
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
73
|
-
* @returns {Promise}
|
|
74
|
-
* @public
|
|
75
|
-
*/
|
|
76
|
-
send( message, queue ) {
|
|
77
|
-
return new Promise( ( resolve, reject ) => {
|
|
78
|
-
this.#preSend( message ).then( ( message ) => {
|
|
79
|
-
return this.onSend( message, queue );
|
|
80
|
-
} ).then( () => {
|
|
81
|
-
return this.#postSend();
|
|
82
|
-
} ).then( () => {
|
|
83
|
-
resolve();
|
|
84
|
-
} ).catch( ( error ) => {
|
|
85
|
-
reject( exceptions.raise( error ) );
|
|
86
|
-
} );
|
|
87
|
-
} );
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Used to perform the actual sending of a message.
|
|
92
|
-
* <br/>
|
|
93
|
-
* NOTE: This method will be called automatically even if overridden.
|
|
94
|
-
* <br/>
|
|
95
|
-
* NOTE: Override this to add functionality.
|
|
96
|
-
*
|
|
97
|
-
* @method
|
|
98
|
-
* @param {Message} message The message to send.
|
|
99
|
-
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
100
|
-
* @returns {Promise<*>}
|
|
101
|
-
* @abstract
|
|
102
|
-
* @public
|
|
103
|
-
*/
|
|
104
|
-
onSend( message, queue ) {
|
|
105
|
-
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_METHOD_CALL, { name: this.constructor.name + "." + this.onSend.name } ) );
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
/* Private interface */
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Used to do pre-send verifications and checks.
|
|
112
|
-
*
|
|
113
|
-
* @method
|
|
114
|
-
* @param {Message} message
|
|
115
|
-
* @returns {Promise<Message>}
|
|
116
|
-
* @private
|
|
117
|
-
*/
|
|
118
|
-
#preSend( message ) {
|
|
119
|
-
if ( this.isAvailable === true ) {
|
|
120
|
-
if ( config.getSetting( config.setting.MESSAGE_EXCHANGE_SECURITY_HASH_ENABLED ) === true ) {
|
|
121
|
-
message.hash = this.createMessageHash( message );
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
return Promise.resolve( message );
|
|
125
|
-
} else {
|
|
126
|
-
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_SENDER_UNAVAILABLE ) );
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
/**
|
|
131
|
-
* Used to execute post- successful send logic.
|
|
132
|
-
*
|
|
133
|
-
* @method
|
|
134
|
-
* @returns {Promise}
|
|
135
|
-
* @private
|
|
136
|
-
*/
|
|
137
|
-
#postSend() {
|
|
138
|
-
return Promise.resolve();
|
|
139
|
-
}
|
|
140
|
-
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
module.exports = MessageSender;
|
|
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 MessageHandler = require( "#message-handler" );
|
|
10
|
+
const exceptions = require( "#exceptions" );
|
|
11
|
+
const config = require( "#config" );
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* An abstract class that defines a basic message sender behavior.
|
|
15
|
+
*
|
|
16
|
+
* @class MessageSender
|
|
17
|
+
* @extends MessageHandler
|
|
18
|
+
* @abstract
|
|
19
|
+
* @public
|
|
20
|
+
*/
|
|
21
|
+
class MessageSender extends MessageHandler {
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* @constructor
|
|
25
|
+
* @param {string} identifier An identifier for this message handler. Should be unique in the context of the message exchange.
|
|
26
|
+
* @throws {TiException.E_GEN_ABSTRACT_CLASS_INIT} If this class is instantiated directly.
|
|
27
|
+
*/
|
|
28
|
+
constructor( identifier ) {
|
|
29
|
+
super( identifier );
|
|
30
|
+
|
|
31
|
+
// make sure this abstract class cannot be instantiated:
|
|
32
|
+
if ( new.target === MessageSender ) {
|
|
33
|
+
throw exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_CLASS_INIT, { name: this.constructor.name } );
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/* Public interface */
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Used to initialize and enable the communication capabilities of the handler.
|
|
41
|
+
* <br/>
|
|
42
|
+
* NOTE: Override this to add functionality.
|
|
43
|
+
*
|
|
44
|
+
* @method
|
|
45
|
+
* @returns {Promise}
|
|
46
|
+
* @abstract
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
enable() {
|
|
50
|
+
return super.enable();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Used to shut down and disable the communication behavior of the handler.
|
|
55
|
+
* <br/>
|
|
56
|
+
* NOTE: Override this to add functionality.
|
|
57
|
+
*
|
|
58
|
+
* @method
|
|
59
|
+
* @returns {Promise}
|
|
60
|
+
* @abstract
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
disable() {
|
|
64
|
+
return super.disable();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Used to send a {@link Message} via this message handler.
|
|
69
|
+
*
|
|
70
|
+
* @method
|
|
71
|
+
* @param {Message} message The message to send.
|
|
72
|
+
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
73
|
+
* @returns {Promise}
|
|
74
|
+
* @public
|
|
75
|
+
*/
|
|
76
|
+
send( message, queue ) {
|
|
77
|
+
return new Promise( ( resolve, reject ) => {
|
|
78
|
+
this.#preSend( message ).then( ( message ) => {
|
|
79
|
+
return this.onSend( message, queue );
|
|
80
|
+
} ).then( () => {
|
|
81
|
+
return this.#postSend();
|
|
82
|
+
} ).then( () => {
|
|
83
|
+
resolve();
|
|
84
|
+
} ).catch( ( error ) => {
|
|
85
|
+
reject( exceptions.raise( error ) );
|
|
86
|
+
} );
|
|
87
|
+
} );
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Used to perform the actual sending of a message.
|
|
92
|
+
* <br/>
|
|
93
|
+
* NOTE: This method will be called automatically even if overridden.
|
|
94
|
+
* <br/>
|
|
95
|
+
* NOTE: Override this to add functionality.
|
|
96
|
+
*
|
|
97
|
+
* @method
|
|
98
|
+
* @param {Message} message The message to send.
|
|
99
|
+
* @param {string} queue The route to destination (queue) for the message as recognized by the {@link MessageExchange} implementation.
|
|
100
|
+
* @returns {Promise<*>}
|
|
101
|
+
* @abstract
|
|
102
|
+
* @public
|
|
103
|
+
*/
|
|
104
|
+
onSend( message, queue ) {
|
|
105
|
+
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_GEN_ABSTRACT_METHOD_CALL, { name: this.constructor.name + "." + this.onSend.name } ) );
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/* Private interface */
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Used to do pre-send verifications and checks.
|
|
112
|
+
*
|
|
113
|
+
* @method
|
|
114
|
+
* @param {Message} message
|
|
115
|
+
* @returns {Promise<Message>}
|
|
116
|
+
* @private
|
|
117
|
+
*/
|
|
118
|
+
#preSend( message ) {
|
|
119
|
+
if ( this.isAvailable === true ) {
|
|
120
|
+
if ( config.getSetting( config.setting.MESSAGE_EXCHANGE_SECURITY_HASH_ENABLED ) === true ) {
|
|
121
|
+
message.hash = this.createMessageHash( message );
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return Promise.resolve( message );
|
|
125
|
+
} else {
|
|
126
|
+
return Promise.reject( exceptions.raise( exceptions.exceptionCode.E_COM_MESSAGE_SENDER_UNAVAILABLE ) );
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Used to execute post- successful send logic.
|
|
132
|
+
*
|
|
133
|
+
* @method
|
|
134
|
+
* @returns {Promise}
|
|
135
|
+
* @private
|
|
136
|
+
*/
|
|
137
|
+
#postSend() {
|
|
138
|
+
return Promise.resolve();
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
module.exports = MessageSender;
|