@mhubcloud/tms_core 3.4.0-beta.1 → 3.4.0-beta.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/package.json +1 -1
- package/src/broker.ts +112 -105
package/package.json
CHANGED
package/src/broker.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { CosmosClient } from '@azure/cosmos';
|
|
2
2
|
import { ServiceBusClient, ServiceBusSender } from '@azure/service-bus';
|
|
3
|
-
import { utils } from '.';
|
|
4
3
|
import { ServiceBusMessage } from './types/ServiceBusMessage';
|
|
4
|
+
import * as utils from './utils/utils';
|
|
5
5
|
const serviceBusConnectionString = process.env.ServiceBusConnectionString;
|
|
6
6
|
const cosmosConnectionString = process.env.CosmosDbConnectionString;
|
|
7
7
|
const cosmosDatabaseName = process.env.CosmosDbDatabaseName;
|
|
@@ -10,128 +10,135 @@ let serviceBusClient: ServiceBusClient | undefined;
|
|
|
10
10
|
let cosmosClient: CosmosClient | undefined;
|
|
11
11
|
const senders: Record<string, ServiceBusSender> = {};
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
type PublisherConfig = Record<string, string[] | string[][]>;
|
|
14
14
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
15
|
+
export const broker = {
|
|
16
|
+
/**
|
|
17
|
+
* Evaluates an event against publisher configurations
|
|
18
|
+
* @param eventCode event.code parameter of the event
|
|
19
|
+
* @param publisherConfig object containing publisher event consumption configurations
|
|
20
|
+
* @returns list of publishers for which an event is relevant.
|
|
21
|
+
*/
|
|
22
|
+
resolveEvent: function (
|
|
23
|
+
eventCode: string,
|
|
24
|
+
publisherConfig?: PublisherConfig,
|
|
25
|
+
) {
|
|
26
|
+
if (!publisherConfig) {
|
|
27
|
+
const envConfig = process.env.Broker_PublisherConfig;
|
|
28
|
+
publisherConfig = envConfig ? JSON.parse(envConfig) : {};
|
|
29
|
+
}
|
|
29
30
|
|
|
30
|
-
|
|
31
|
+
const publishers = [];
|
|
31
32
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
33
|
+
for (const publisher of Object.keys(publisherConfig)) {
|
|
34
|
+
const publisherEvents = publisherConfig[publisher];
|
|
35
|
+
if (publisherEvents) {
|
|
36
|
+
if (publisherEvents[0] === '*') {
|
|
37
|
+
let matchesExcluded = false;
|
|
38
|
+
// Checking whether event matches any of the excluded events
|
|
39
|
+
if (
|
|
40
|
+
Array.isArray(publisherEvents[1]) &&
|
|
41
|
+
publisherEvents[1].length > 0
|
|
42
|
+
) {
|
|
43
|
+
for (const event of publisherEvents[1]) {
|
|
44
|
+
if (matchesExcluded) {
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
42
47
|
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
48
|
+
if (typeof event === 'string') {
|
|
49
|
+
const regex = new RegExp(event);
|
|
50
|
+
matchesExcluded = regex.test(eventCode);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
46
53
|
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
} else {
|
|
53
|
-
let match = false;
|
|
54
|
+
if (!matchesExcluded) {
|
|
55
|
+
publishers.push(publisher);
|
|
56
|
+
}
|
|
57
|
+
} else {
|
|
58
|
+
let match = false;
|
|
54
59
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
+
for (const event of publisherEvents) {
|
|
61
|
+
if (typeof event === 'string') {
|
|
62
|
+
const regex = new RegExp(event);
|
|
63
|
+
match = regex.test(eventCode);
|
|
64
|
+
}
|
|
60
65
|
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
if (match) {
|
|
67
|
+
publishers.push(publisher);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
63
70
|
}
|
|
64
71
|
}
|
|
65
72
|
}
|
|
66
|
-
}
|
|
67
73
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
74
|
+
if (publishers.length === 0) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
71
77
|
|
|
72
|
-
|
|
73
|
-
}
|
|
78
|
+
return publishers;
|
|
79
|
+
},
|
|
74
80
|
|
|
75
|
-
/**
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
) {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Sends an event to Service Bus
|
|
83
|
+
* @param message event to be sent
|
|
84
|
+
* @param queues list of Service Bus queues to send the event to
|
|
85
|
+
*/
|
|
86
|
+
dispatchEvent: async function (
|
|
87
|
+
message: Record<string, string>,
|
|
88
|
+
queues: string[],
|
|
89
|
+
) {
|
|
90
|
+
if (!serviceBusClient) {
|
|
91
|
+
if (!serviceBusConnectionString) {
|
|
92
|
+
throw new Error('Service Bus connection string is missing.');
|
|
93
|
+
}
|
|
94
|
+
serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
|
|
87
95
|
}
|
|
88
|
-
serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
|
|
89
|
-
}
|
|
90
96
|
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
97
|
+
await Promise.all(
|
|
98
|
+
queues.map(async (queue) => {
|
|
99
|
+
try {
|
|
100
|
+
let sender;
|
|
101
|
+
if (senders[queue]) {
|
|
102
|
+
sender = senders[queue];
|
|
103
|
+
} else {
|
|
104
|
+
senders[queue] = serviceBusClient!.createSender(queue);
|
|
105
|
+
sender = senders[queue];
|
|
106
|
+
}
|
|
107
|
+
await sender.sendMessages({ body: message });
|
|
108
|
+
} catch (e) {
|
|
109
|
+
console.log(`Sending to queue ${queue} failed`);
|
|
100
110
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
}
|
|
105
|
-
}),
|
|
106
|
-
);
|
|
107
|
-
}
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
113
|
+
},
|
|
108
114
|
|
|
109
|
-
/**
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
115
|
+
/**
|
|
116
|
+
* Logs an event to Cosmos DB
|
|
117
|
+
* @param message event to be logged
|
|
118
|
+
*/
|
|
119
|
+
logToCosmosDb: async function (message: ServiceBusMessage) {
|
|
120
|
+
if (!cosmosClient) {
|
|
121
|
+
if (!cosmosConnectionString) {
|
|
122
|
+
throw new Error('Cosmos DB connection string is missing.');
|
|
123
|
+
}
|
|
124
|
+
cosmosClient = new CosmosClient(cosmosConnectionString);
|
|
117
125
|
}
|
|
118
|
-
cosmosClient = new CosmosClient(cosmosConnectionString);
|
|
119
|
-
}
|
|
120
126
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
127
|
+
if (!cosmosDatabaseName) {
|
|
128
|
+
throw new Error('Cosmos DB database name is missing.');
|
|
129
|
+
}
|
|
124
130
|
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
131
|
+
if (!cosmosContainerName) {
|
|
132
|
+
throw new Error('Cosmos DB events container name is missing.');
|
|
133
|
+
}
|
|
128
134
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
}
|
|
135
|
+
try {
|
|
136
|
+
await cosmosClient
|
|
137
|
+
.database(cosmosDatabaseName)
|
|
138
|
+
.container(cosmosContainerName)
|
|
139
|
+
.items.create(message);
|
|
140
|
+
} catch (e) {
|
|
141
|
+
utils.logException(e as Error);
|
|
142
|
+
}
|
|
143
|
+
},
|
|
144
|
+
};
|