@mhubcloud/tms_core 3.3.0 → 3.4.0-beta.1

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/dist/index.js CHANGED
@@ -28,14 +28,117 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
28
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
29
29
 
30
30
  // src/index.ts
31
- var src_exports = {};
32
- __export(src_exports, {
31
+ var index_exports = {};
32
+ __export(index_exports, {
33
33
  DefaultPublisher: () => defaultPublisher,
34
+ broker: () => broker_exports,
34
35
  publisher: () => publisher,
35
36
  testUtils: () => testUtils_exports,
36
37
  utils: () => utils_exports
37
38
  });
38
- module.exports = __toCommonJS(src_exports);
39
+ module.exports = __toCommonJS(index_exports);
40
+
41
+ // src/broker.ts
42
+ var broker_exports = {};
43
+ __export(broker_exports, {
44
+ dispatchEvent: () => dispatchEvent,
45
+ logToCosmosDb: () => logToCosmosDb,
46
+ resolveEvent: () => resolveEvent
47
+ });
48
+ var import_cosmos = require("@azure/cosmos");
49
+ var import_service_bus = require("@azure/service-bus");
50
+ var serviceBusConnectionString = process.env.ServiceBusConnectionString;
51
+ var cosmosConnectionString = process.env.CosmosDbConnectionString;
52
+ var cosmosDatabaseName = process.env.CosmosDbDatabaseName;
53
+ var cosmosContainerName = process.env.CosmosEventsContainerName;
54
+ var serviceBusClient;
55
+ var cosmosClient;
56
+ var senders = {};
57
+ function resolveEvent(eventCode, publisherConfig) {
58
+ if (!publisherConfig) {
59
+ const envConfig = process.env.Broker_PublisherConfig;
60
+ publisherConfig = envConfig ? JSON.parse(envConfig) : {};
61
+ }
62
+ const publishers = [];
63
+ for (const publisher2 of Object.keys(publisherConfig)) {
64
+ const publisherEvents = publisherConfig[publisher2];
65
+ if (publisherEvents[0] === "*") {
66
+ let matchesExcluded = false;
67
+ if (Array.isArray(publisherEvents[1]) && publisherEvents[1].length > 0) {
68
+ for (const event of publisherEvents[1]) {
69
+ if (matchesExcluded) {
70
+ break;
71
+ }
72
+ if (typeof event === "string") {
73
+ const regex = new RegExp(event);
74
+ matchesExcluded = regex.test(eventCode);
75
+ }
76
+ }
77
+ }
78
+ if (!matchesExcluded) {
79
+ publishers.push(publisher2);
80
+ }
81
+ } else {
82
+ let match = false;
83
+ for (const event of publisherEvents) {
84
+ if (typeof event === "string") {
85
+ const regex = new RegExp(event);
86
+ match = regex.test(eventCode);
87
+ }
88
+ if (match) {
89
+ publishers.push(publisher2);
90
+ }
91
+ }
92
+ }
93
+ }
94
+ if (publishers.length === 0) {
95
+ return null;
96
+ }
97
+ return publishers;
98
+ }
99
+ async function dispatchEvent(message, queues) {
100
+ if (!serviceBusClient) {
101
+ if (!serviceBusConnectionString) {
102
+ throw new Error("Service Bus connection string is missing.");
103
+ }
104
+ serviceBusClient = new import_service_bus.ServiceBusClient(serviceBusConnectionString);
105
+ }
106
+ await Promise.all(
107
+ queues.map(async (queue) => {
108
+ try {
109
+ let sender;
110
+ if (senders[queue]) {
111
+ sender = senders[queue];
112
+ } else {
113
+ senders[queue] = serviceBusClient.createSender(queue);
114
+ sender = senders[queue];
115
+ }
116
+ await sender.sendMessages({ body: message });
117
+ } catch (e) {
118
+ console.log(`Sending to queue ${queue} failed`);
119
+ }
120
+ })
121
+ );
122
+ }
123
+ async function logToCosmosDb(message, logger) {
124
+ if (!cosmosClient) {
125
+ if (!cosmosConnectionString) {
126
+ throw new Error("Cosmos DB connection string is missing.");
127
+ }
128
+ cosmosClient = new import_cosmos.CosmosClient(cosmosConnectionString);
129
+ }
130
+ if (!cosmosDatabaseName) {
131
+ throw new Error("Cosmos DB database name is missing.");
132
+ }
133
+ if (!cosmosContainerName) {
134
+ throw new Error("Cosmos DB events container name is missing.");
135
+ }
136
+ try {
137
+ await cosmosClient.database(cosmosDatabaseName).container(cosmosContainerName).items.create(message);
138
+ } catch (e) {
139
+ utils_exports.logException(logger, e);
140
+ }
141
+ }
39
142
 
40
143
  // src/utils/utils.ts
41
144
  var utils_exports = {};
@@ -747,6 +850,7 @@ var publisher = {
747
850
  // Annotate the CommonJS export names for ESM import in node:
748
851
  0 && (module.exports = {
749
852
  DefaultPublisher,
853
+ broker,
750
854
  publisher,
751
855
  testUtils,
752
856
  utils
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mhubcloud/tms_core",
3
- "version": "3.3.0",
3
+ "version": "3.4.0-beta.1",
4
4
  "description": "Core function for mHub Cloud TMS publishers",
5
5
  "homepage": "https://mhubcloud.com/",
6
6
  "license": "Apache 2.0",
@@ -16,7 +16,9 @@
16
16
  "conversion API"
17
17
  ],
18
18
  "dependencies": {
19
+ "@azure/cosmos": "^4.4.1",
19
20
  "@azure/data-tables": "^13.2.2",
21
+ "@azure/service-bus": "^7.9.5",
20
22
  "ajv": "^8.12.0",
21
23
  "applicationinsights": "^2.6.0"
22
24
  },
@@ -34,7 +36,7 @@
34
36
  "mocha": "^10.2.0",
35
37
  "prettier": "^2.8.8",
36
38
  "ts-node": "^10.9.1",
37
- "tsup": "^6.7.0",
39
+ "tsup": "^8.5.0",
38
40
  "typescript": "^4.9.5"
39
41
  },
40
42
  "scripts": {
package/src/broker.ts ADDED
@@ -0,0 +1,137 @@
1
+ import { CosmosClient } from '@azure/cosmos';
2
+ import { ServiceBusClient, ServiceBusSender } from '@azure/service-bus';
3
+ import { utils } from '.';
4
+ import { ServiceBusMessage } from './types/ServiceBusMessage';
5
+ const serviceBusConnectionString = process.env.ServiceBusConnectionString;
6
+ const cosmosConnectionString = process.env.CosmosDbConnectionString;
7
+ const cosmosDatabaseName = process.env.CosmosDbDatabaseName;
8
+ const cosmosContainerName = process.env.CosmosEventsContainerName;
9
+ let serviceBusClient: ServiceBusClient | undefined;
10
+ let cosmosClient: CosmosClient | undefined;
11
+ const senders: Record<string, ServiceBusSender> = {};
12
+
13
+ export type PublisherConfig = Record<string, string[] | string[][]>;
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
+ }
29
+
30
+ const publishers = [];
31
+
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
+ }
42
+
43
+ if (typeof event === 'string') {
44
+ const regex = new RegExp(event);
45
+ matchesExcluded = regex.test(eventCode);
46
+ }
47
+ }
48
+ }
49
+ if (!matchesExcluded) {
50
+ publishers.push(publisher);
51
+ }
52
+ } else {
53
+ let match = false;
54
+
55
+ for (const event of publisherEvents) {
56
+ if (typeof event === 'string') {
57
+ const regex = new RegExp(event);
58
+ match = regex.test(eventCode);
59
+ }
60
+
61
+ if (match) {
62
+ publishers.push(publisher);
63
+ }
64
+ }
65
+ }
66
+ }
67
+
68
+ if (publishers.length === 0) {
69
+ return null;
70
+ }
71
+
72
+ return publishers;
73
+ }
74
+
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.');
87
+ }
88
+ serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
89
+ }
90
+
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];
100
+ }
101
+ await sender.sendMessages({ body: message });
102
+ } catch (e) {
103
+ console.log(`Sending to queue ${queue} failed`);
104
+ }
105
+ }),
106
+ );
107
+ }
108
+
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.');
117
+ }
118
+ cosmosClient = new CosmosClient(cosmosConnectionString);
119
+ }
120
+
121
+ if (!cosmosDatabaseName) {
122
+ throw new Error('Cosmos DB database name is missing.');
123
+ }
124
+
125
+ if (!cosmosContainerName) {
126
+ throw new Error('Cosmos DB events container name is missing.');
127
+ }
128
+
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
+ }
package/src/index.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as broker from './broker';
1
2
  import { defaultPublisher } from './defaultPublisher';
2
3
  import { affiliatePort } from './publishers/affiliatePort';
3
4
  import { cj } from './publishers/cj';
@@ -26,4 +27,4 @@ export const publisher = {
26
27
  AffiliatePort: affiliatePort,
27
28
  };
28
29
 
29
- export { utils, defaultPublisher as DefaultPublisher, testUtils };
30
+ export { broker, utils, defaultPublisher as DefaultPublisher, testUtils };
@@ -88,7 +88,7 @@ export function logEvent(
88
88
  /**
89
89
  * Logs exception to Azure Function context and to Application Insights (if configured)
90
90
  */
91
- export function logException(log: Logger, exception: Error) {
91
+ export function logException(exception: Error) {
92
92
  // log.warn(exception.message);
93
93
  aiClient?.trackException({ exception: exception });
94
94
  }