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