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