@mhubcloud/tms_core 3.4.0-beta.4 → 3.4.0

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 (2) hide show
  1. package/package.json +1 -1
  2. package/src/broker.ts +52 -14
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mhubcloud/tms_core",
3
- "version": "3.4.0-beta.4",
3
+ "version": "3.4.0",
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
@@ -10,7 +10,16 @@ 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<
14
+ string,
15
+ string | [string, PublisherEvent[]] | PublisherEvent[]
16
+ >;
17
+
18
+ type PublisherEvent = Record<string, string>;
19
+ interface EventInfo {
20
+ code: string;
21
+ [key: string]: string;
22
+ }
14
23
 
15
24
  /**
16
25
  * Evaluates an event against publisher configurations
@@ -19,18 +28,21 @@ export type PublisherConfig = Record<string, string[] | string[][]>;
19
28
  * @returns list of publishers for which an event is relevant.
20
29
  */
21
30
  export function resolveEvent(
22
- eventCode: string,
31
+ event: EventInfo,
23
32
  publisherConfig?: PublisherConfig,
24
33
  ) {
25
34
  if (!publisherConfig) {
26
35
  const envConfig = process.env.Broker_PublisherConfig;
36
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
27
37
  publisherConfig = envConfig ? JSON.parse(envConfig) : {};
28
38
  }
29
39
 
30
40
  const publishers = [];
31
41
 
32
- for (const publisher of Object.keys(publisherConfig)) {
33
- const publisherEvents = publisherConfig[publisher];
42
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
43
+ for (const publisher of Object.keys(publisherConfig!)) {
44
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
45
+ const publisherEvents = publisherConfig![publisher];
34
46
  if (publisherEvents) {
35
47
  if (publisherEvents[0] === '*') {
36
48
  let matchesExcluded = false;
@@ -39,15 +51,12 @@ export function resolveEvent(
39
51
  Array.isArray(publisherEvents[1]) &&
40
52
  publisherEvents[1].length > 0
41
53
  ) {
42
- for (const event of publisherEvents[1]) {
54
+ for (const publisherEvent of publisherEvents[1]) {
43
55
  if (matchesExcluded) {
44
56
  break;
45
57
  }
46
58
 
47
- if (typeof event === 'string') {
48
- const regex = new RegExp(event);
49
- matchesExcluded = regex.test(eventCode);
50
- }
59
+ matchesExcluded = matchEvent(event, publisherEvent);
51
60
  }
52
61
  }
53
62
  if (!matchesExcluded) {
@@ -56,12 +65,11 @@ export function resolveEvent(
56
65
  } else {
57
66
  let match = false;
58
67
 
59
- for (const event of publisherEvents) {
60
- if (typeof event === 'string') {
61
- const regex = new RegExp(event);
62
- match = regex.test(eventCode);
68
+ for (const publisherEvent of publisherEvents) {
69
+ if (Array.isArray(publisherEvent)) {
70
+ continue;
63
71
  }
64
-
72
+ match = matchEvent(event, publisherEvent);
65
73
  if (match) {
66
74
  publishers.push(publisher);
67
75
  }
@@ -77,6 +85,35 @@ export function resolveEvent(
77
85
  return publishers;
78
86
  }
79
87
 
88
+ function matchEvent(event: EventInfo, publisherEvent: string | PublisherEvent) {
89
+ if (typeof publisherEvent === 'string') {
90
+ const regex = new RegExp(publisherEvent);
91
+ const match = regex.test(event.code);
92
+ return match;
93
+ } else if (typeof publisherEvent === 'object') {
94
+ let paramMatch = true;
95
+ for (const key of Object.keys(publisherEvent)) {
96
+ const publisherEventParam = publisherEvent[key];
97
+ const eventParam = event[key];
98
+ if (
99
+ typeof publisherEventParam === 'string' &&
100
+ typeof eventParam === 'string'
101
+ ) {
102
+ const regex = new RegExp(publisherEventParam);
103
+ paramMatch = regex.test(eventParam);
104
+ if (!paramMatch) {
105
+ return false;
106
+ }
107
+ } else {
108
+ throw new Error('Unexpected formatting in publisher configuration');
109
+ }
110
+ }
111
+ return true;
112
+ } else {
113
+ throw new Error('Unexpected formatting in publisher configuration');
114
+ }
115
+ }
116
+
80
117
  /**
81
118
  * Sends an event to Service Bus
82
119
  * @param message event to be sent
@@ -100,6 +137,7 @@ export async function dispatchEvent(
100
137
  if (senders[queue]) {
101
138
  sender = senders[queue];
102
139
  } else {
140
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
103
141
  senders[queue] = serviceBusClient!.createSender(queue);
104
142
  sender = senders[queue];
105
143
  }