@mhubcloud/tms_core 3.4.0-beta.3 → 3.4.0-beta.5
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 +197 -156
- package/package.json +1 -1
- package/src/broker.ts +150 -114
package/dist/index.js
CHANGED
|
@@ -47,98 +47,6 @@ __export(broker_exports, {
|
|
|
47
47
|
});
|
|
48
48
|
var import_cosmos = require("@azure/cosmos");
|
|
49
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
|
-
}
|
|
142
50
|
|
|
143
51
|
// src/utils/utils.ts
|
|
144
52
|
var utils_exports = {};
|
|
@@ -147,19 +55,19 @@ __export(utils_exports, {
|
|
|
147
55
|
encodeEmail: () => encodeEmail,
|
|
148
56
|
extractAdCode: () => extractAdCode,
|
|
149
57
|
flatten: () => flatten,
|
|
58
|
+
formatError: () => formatError,
|
|
150
59
|
handlePlatformRequest: () => handlePlatformRequest,
|
|
151
60
|
handleResponse: () => handleResponse,
|
|
152
61
|
logEvent: () => logEvent,
|
|
153
62
|
logException: () => logException,
|
|
154
63
|
logMessage: () => logMessage,
|
|
155
|
-
parseErrorMessage: () => parseErrorMessage,
|
|
156
64
|
prepareTestRequestHeaders: () => prepareTestRequestHeaders,
|
|
157
65
|
removeUndefined: () => removeUndefined
|
|
158
66
|
});
|
|
159
67
|
var crypto = __toESM(require("crypto"));
|
|
160
68
|
var appInsights = __toESM(require("applicationinsights"));
|
|
161
69
|
var aiClient = null;
|
|
162
|
-
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
70
|
+
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
|
|
163
71
|
appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, false).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false).setUseDiskRetryCaching(false).start();
|
|
164
72
|
aiClient = appInsights.defaultClient;
|
|
165
73
|
}
|
|
@@ -192,15 +100,17 @@ function logMessage(level, message, properties) {
|
|
|
192
100
|
properties
|
|
193
101
|
});
|
|
194
102
|
}
|
|
195
|
-
function logEvent(publisher2, event, region) {
|
|
103
|
+
function logEvent(publisher2, event, region, status, payload) {
|
|
196
104
|
const properties = {
|
|
197
105
|
publisher: publisher2,
|
|
198
106
|
event,
|
|
199
|
-
region
|
|
107
|
+
region,
|
|
108
|
+
status,
|
|
109
|
+
payload
|
|
200
110
|
};
|
|
201
111
|
logMessage(1, "Event dispatched to platform", properties);
|
|
202
112
|
}
|
|
203
|
-
function logException(
|
|
113
|
+
function logException(exception) {
|
|
204
114
|
aiClient?.trackException({ exception });
|
|
205
115
|
}
|
|
206
116
|
function removeUndefined(originalValue) {
|
|
@@ -314,7 +224,10 @@ async function handlePlatformRequest({
|
|
|
314
224
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
315
225
|
headers: { ...requestOptions.headers }
|
|
316
226
|
};
|
|
317
|
-
const rawPlatformResponse = await fetch(
|
|
227
|
+
const rawPlatformResponse = await fetch(
|
|
228
|
+
fullUrl,
|
|
229
|
+
fetchRequestOptions
|
|
230
|
+
);
|
|
318
231
|
if (process.env.PUBLISHER_TESTURL) {
|
|
319
232
|
await handleTestRequest({
|
|
320
233
|
fetchRequestOptions,
|
|
@@ -328,10 +241,22 @@ async function handlePlatformRequest({
|
|
|
328
241
|
rawPlatformResponse,
|
|
329
242
|
event.event
|
|
330
243
|
);
|
|
331
|
-
logEvent(
|
|
244
|
+
logEvent(
|
|
245
|
+
source,
|
|
246
|
+
event.event,
|
|
247
|
+
event.region,
|
|
248
|
+
rawPlatformResponse.status,
|
|
249
|
+
requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
|
|
250
|
+
);
|
|
332
251
|
return processedPlatformResponse;
|
|
333
252
|
} else {
|
|
334
|
-
logEvent(
|
|
253
|
+
logEvent(
|
|
254
|
+
source,
|
|
255
|
+
event.event,
|
|
256
|
+
event.region,
|
|
257
|
+
rawPlatformResponse.status,
|
|
258
|
+
requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
|
|
259
|
+
);
|
|
335
260
|
}
|
|
336
261
|
}
|
|
337
262
|
async function handleTestRequest(params) {
|
|
@@ -354,18 +279,115 @@ async function handleTestRequest(params) {
|
|
|
354
279
|
} catch {
|
|
355
280
|
}
|
|
356
281
|
}
|
|
357
|
-
function
|
|
358
|
-
let
|
|
282
|
+
function formatError(error) {
|
|
283
|
+
let formattedError;
|
|
359
284
|
if (error instanceof Error) {
|
|
285
|
+
formattedError = error;
|
|
360
286
|
if (error.cause) {
|
|
361
|
-
|
|
287
|
+
formattedError.message = `${error.message} Cause: ${error.cause.toString()}`;
|
|
362
288
|
} else {
|
|
363
|
-
|
|
289
|
+
formattedError.message = error.message;
|
|
364
290
|
}
|
|
365
291
|
} else {
|
|
366
|
-
|
|
292
|
+
formattedError = new Error(error);
|
|
293
|
+
}
|
|
294
|
+
return formattedError;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
// src/broker.ts
|
|
298
|
+
var serviceBusConnectionString = process.env.ServiceBusConnectionString;
|
|
299
|
+
var cosmosConnectionString = process.env.CosmosDbConnectionString;
|
|
300
|
+
var cosmosDatabaseName = process.env.CosmosDbDatabaseName;
|
|
301
|
+
var cosmosContainerName = process.env.CosmosDbEventsContainerName;
|
|
302
|
+
var serviceBusClient;
|
|
303
|
+
var cosmosClient;
|
|
304
|
+
var senders = {};
|
|
305
|
+
function resolveEvent(eventCode, publisherConfig) {
|
|
306
|
+
if (!publisherConfig) {
|
|
307
|
+
const envConfig = process.env.Broker_PublisherConfig;
|
|
308
|
+
publisherConfig = envConfig ? JSON.parse(envConfig) : {};
|
|
309
|
+
}
|
|
310
|
+
const publishers = [];
|
|
311
|
+
for (const publisher2 of Object.keys(publisherConfig)) {
|
|
312
|
+
const publisherEvents = publisherConfig[publisher2];
|
|
313
|
+
if (publisherEvents) {
|
|
314
|
+
if (publisherEvents[0] === "*") {
|
|
315
|
+
let matchesExcluded = false;
|
|
316
|
+
if (Array.isArray(publisherEvents[1]) && publisherEvents[1].length > 0) {
|
|
317
|
+
for (const event of publisherEvents[1]) {
|
|
318
|
+
if (matchesExcluded) {
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
if (typeof event === "string") {
|
|
322
|
+
const regex = new RegExp(event);
|
|
323
|
+
matchesExcluded = regex.test(eventCode);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
if (!matchesExcluded) {
|
|
328
|
+
publishers.push(publisher2);
|
|
329
|
+
}
|
|
330
|
+
} else {
|
|
331
|
+
let match = false;
|
|
332
|
+
for (const event of publisherEvents) {
|
|
333
|
+
if (typeof event === "string") {
|
|
334
|
+
const regex = new RegExp(event);
|
|
335
|
+
match = regex.test(eventCode);
|
|
336
|
+
}
|
|
337
|
+
if (match) {
|
|
338
|
+
publishers.push(publisher2);
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
if (publishers.length === 0) {
|
|
345
|
+
return null;
|
|
346
|
+
}
|
|
347
|
+
return publishers;
|
|
348
|
+
}
|
|
349
|
+
async function dispatchEvent(message, queues) {
|
|
350
|
+
if (!serviceBusClient) {
|
|
351
|
+
if (!serviceBusConnectionString) {
|
|
352
|
+
throw new Error("Service Bus connection string is missing.");
|
|
353
|
+
}
|
|
354
|
+
serviceBusClient = new import_service_bus.ServiceBusClient(serviceBusConnectionString);
|
|
355
|
+
}
|
|
356
|
+
await Promise.all(
|
|
357
|
+
queues.map(async (queue) => {
|
|
358
|
+
try {
|
|
359
|
+
let sender;
|
|
360
|
+
if (senders[queue]) {
|
|
361
|
+
sender = senders[queue];
|
|
362
|
+
} else {
|
|
363
|
+
senders[queue] = serviceBusClient.createSender(queue);
|
|
364
|
+
sender = senders[queue];
|
|
365
|
+
}
|
|
366
|
+
await sender.sendMessages({ body: message });
|
|
367
|
+
} catch (e) {
|
|
368
|
+
console.log(`Sending to queue ${queue} failed`);
|
|
369
|
+
}
|
|
370
|
+
})
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
async function logToCosmosDb(message) {
|
|
374
|
+
if (!cosmosClient) {
|
|
375
|
+
if (!cosmosConnectionString) {
|
|
376
|
+
throw new Error("Cosmos DB connection string is missing.");
|
|
377
|
+
}
|
|
378
|
+
cosmosClient = new import_cosmos.CosmosClient(cosmosConnectionString);
|
|
379
|
+
}
|
|
380
|
+
if (!cosmosDatabaseName) {
|
|
381
|
+
throw new Error("Cosmos DB database name is missing.");
|
|
382
|
+
}
|
|
383
|
+
if (!cosmosContainerName) {
|
|
384
|
+
throw new Error("Cosmos DB events container name is missing.");
|
|
385
|
+
}
|
|
386
|
+
try {
|
|
387
|
+
await cosmosClient.database(cosmosDatabaseName).container(cosmosContainerName).items.create(message);
|
|
388
|
+
} catch (e) {
|
|
389
|
+
logException(e);
|
|
367
390
|
}
|
|
368
|
-
return errorMessage;
|
|
369
391
|
}
|
|
370
392
|
|
|
371
393
|
// src/defaultPublisher.ts
|
|
@@ -386,15 +408,21 @@ async function defaultPublisher(azureFunctionContext, serviceBusMessage, source,
|
|
|
386
408
|
for (const result of results) {
|
|
387
409
|
if (result.status === "rejected") {
|
|
388
410
|
if (result.reason instanceof Error) {
|
|
389
|
-
const
|
|
390
|
-
logException(
|
|
391
|
-
|
|
411
|
+
const formattedError = formatError(result.reason);
|
|
412
|
+
logException(
|
|
413
|
+
azureFunctionContext.log,
|
|
414
|
+
new Error(formattedError.message)
|
|
415
|
+
);
|
|
416
|
+
throw formattedError;
|
|
392
417
|
} else {
|
|
393
|
-
const
|
|
418
|
+
const formattedError = formatError(
|
|
394
419
|
typeof result.reason === "object" ? JSON.stringify(result.reason) : result.reason
|
|
395
420
|
);
|
|
396
|
-
logException(
|
|
397
|
-
|
|
421
|
+
logException(
|
|
422
|
+
azureFunctionContext.log,
|
|
423
|
+
new Error(formattedError.message)
|
|
424
|
+
);
|
|
425
|
+
throw formattedError;
|
|
398
426
|
}
|
|
399
427
|
}
|
|
400
428
|
}
|
|
@@ -580,42 +608,9 @@ var facebook = {
|
|
|
580
608
|
}
|
|
581
609
|
};
|
|
582
610
|
|
|
583
|
-
// src/utils/tokens/googleAdsToken.ts
|
|
584
|
-
var tokenCache = null;
|
|
585
|
-
var tokenPromise = null;
|
|
586
|
-
async function fetchGoogleAdsToken(config) {
|
|
587
|
-
if (tokenCache && tokenCache.createdAt + 3500 * 1e3 > Date.now()) {
|
|
588
|
-
return tokenCache;
|
|
589
|
-
}
|
|
590
|
-
if (!tokenPromise) {
|
|
591
|
-
tokenPromise = (async () => {
|
|
592
|
-
const tokenResponse = await fetch(`https://oauth2.googleapis.com/token`, {
|
|
593
|
-
method: "POST",
|
|
594
|
-
headers: {
|
|
595
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
596
|
-
"Content-Type": "application/x-www-form-urlencoded"
|
|
597
|
-
},
|
|
598
|
-
body: new URLSearchParams({
|
|
599
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
600
|
-
client_id: config.client.id,
|
|
601
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
602
|
-
client_secret: config.client.secret,
|
|
603
|
-
refreshToken: config.refreshToken,
|
|
604
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
605
|
-
grant_type: "refresh_token"
|
|
606
|
-
})
|
|
607
|
-
});
|
|
608
|
-
const parsedTokenResponse = await tokenResponse.json();
|
|
609
|
-
const createdAt = Date.now();
|
|
610
|
-
tokenCache = { token: parsedTokenResponse.access_token, createdAt };
|
|
611
|
-
tokenPromise = null;
|
|
612
|
-
return tokenCache;
|
|
613
|
-
})();
|
|
614
|
-
}
|
|
615
|
-
return tokenPromise;
|
|
616
|
-
}
|
|
617
|
-
|
|
618
611
|
// src/publishers/googleAds.ts
|
|
612
|
+
var tokenCache;
|
|
613
|
+
var tokenPromise;
|
|
619
614
|
var googleAds = {
|
|
620
615
|
source: "GoogleAds",
|
|
621
616
|
/**
|
|
@@ -630,7 +625,7 @@ var googleAds = {
|
|
|
630
625
|
logMessage(1, `Excluded ${event.event} due to lack of identifiers`);
|
|
631
626
|
return;
|
|
632
627
|
}
|
|
633
|
-
const tokenData = await
|
|
628
|
+
const tokenData = await this.getToken(config);
|
|
634
629
|
data.conversionAction = `customers/${config.customerId}/conversionActions/${config.conversionId}`;
|
|
635
630
|
const body = {
|
|
636
631
|
conversions: [data],
|
|
@@ -643,6 +638,7 @@ var googleAds = {
|
|
|
643
638
|
headers: {
|
|
644
639
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
645
640
|
Authorization: `Bearer ${tokenData.token}`,
|
|
641
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
646
642
|
"developer-token": config.client.developerToken
|
|
647
643
|
},
|
|
648
644
|
body: JSON.stringify(body)
|
|
@@ -651,23 +647,68 @@ var googleAds = {
|
|
|
651
647
|
const response = await fetch(baseUrl, requestOptions);
|
|
652
648
|
const responseJson = await response.json();
|
|
653
649
|
const knownErrors = [
|
|
654
|
-
"The imported event has a conversion_date_time that precedes the click. Make sure your conversion_date_time is correct and try again
|
|
655
|
-
"The click ID or call is associated with an Ads account you don't have access to. Make sure you import conversions for accounts managed by your manager account
|
|
656
|
-
"The imported event can't be recorded because its click occurred before this conversion's click-through window. Make sure you import the most recent data
|
|
657
|
-
"The imported event includes an order ID that was previously recorded, so the event was not processed
|
|
658
|
-
"The imported event could not be attributed to a click. This may be because the event did not come from a Google Ads campaign
|
|
659
|
-
"The imported event can't be recorded because its click occurred before this conversion's click-through window. Make sure you import the most recent data
|
|
650
|
+
"The imported event has a conversion_date_time that precedes the click. Make sure your conversion_date_time is correct and try again.",
|
|
651
|
+
"The click ID or call is associated with an Ads account you don't have access to. Make sure you import conversions for accounts managed by your manager account.",
|
|
652
|
+
"The imported event can't be recorded because its click occurred before this conversion's click-through window. Make sure you import the most recent data.",
|
|
653
|
+
"The imported event includes an order ID that was previously recorded, so the event was not processed.",
|
|
654
|
+
"The imported event could not be attributed to a click. This may be because the event did not come from a Google Ads campaign.",
|
|
655
|
+
"The imported event can't be recorded because its click occurred before this conversion's click-through window. Make sure you import the most recent data."
|
|
660
656
|
];
|
|
661
657
|
if (responseJson.partialFailureError) {
|
|
662
658
|
logMessage(1, responseJson.partialFailureError.message);
|
|
663
|
-
|
|
659
|
+
const trimmedMessage = responseJson.partialFailureError.message.split("., at")[0] ?? "";
|
|
660
|
+
if (!knownErrors.includes(trimmedMessage)) {
|
|
664
661
|
throw new Error(responseJson.partialFailureError.message);
|
|
665
662
|
}
|
|
666
663
|
} else if (responseJson.error) {
|
|
667
664
|
throw new Error(responseJson.error.message);
|
|
668
665
|
} else {
|
|
669
|
-
logEvent(
|
|
666
|
+
logEvent(
|
|
667
|
+
this.source,
|
|
668
|
+
event.event,
|
|
669
|
+
event.region,
|
|
670
|
+
response.status,
|
|
671
|
+
requestOptions.body
|
|
672
|
+
);
|
|
673
|
+
}
|
|
674
|
+
},
|
|
675
|
+
async getToken(config) {
|
|
676
|
+
if (tokenCache && tokenCache.createdAt + 3500 * 1e3 > Date.now()) {
|
|
677
|
+
return tokenCache;
|
|
678
|
+
}
|
|
679
|
+
if (!tokenPromise) {
|
|
680
|
+
tokenPromise = (async () => {
|
|
681
|
+
const tokenResponse = await fetch(
|
|
682
|
+
`https://oauth2.googleapis.com/token`,
|
|
683
|
+
{
|
|
684
|
+
method: "POST",
|
|
685
|
+
headers: {
|
|
686
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
687
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
688
|
+
},
|
|
689
|
+
body: new URLSearchParams({
|
|
690
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
691
|
+
client_id: config.client.id,
|
|
692
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
693
|
+
client_secret: config.client.secret,
|
|
694
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
695
|
+
refreshToken: config.refreshToken,
|
|
696
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
697
|
+
grant_type: "refresh_token"
|
|
698
|
+
})
|
|
699
|
+
}
|
|
700
|
+
);
|
|
701
|
+
if (!tokenResponse.ok) {
|
|
702
|
+
throw new Error("Access token fetch failed");
|
|
703
|
+
}
|
|
704
|
+
const parsedTokenResponse = await tokenResponse.json();
|
|
705
|
+
const createdAt = Date.now();
|
|
706
|
+
tokenCache = { token: parsedTokenResponse.access_token, createdAt };
|
|
707
|
+
tokenPromise = null;
|
|
708
|
+
return tokenCache;
|
|
709
|
+
})();
|
|
670
710
|
}
|
|
711
|
+
return tokenPromise;
|
|
671
712
|
}
|
|
672
713
|
};
|
|
673
714
|
|
package/package.json
CHANGED
package/src/broker.ts
CHANGED
|
@@ -5,140 +5,176 @@ 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;
|
|
8
|
-
const cosmosContainerName = process.env.
|
|
8
|
+
const cosmosContainerName = process.env.CosmosDbEventsContainerName;
|
|
9
9
|
let serviceBusClient: ServiceBusClient | undefined;
|
|
10
10
|
let cosmosClient: CosmosClient | undefined;
|
|
11
11
|
const senders: Record<string, ServiceBusSender> = {};
|
|
12
12
|
|
|
13
|
-
type PublisherConfig = Record<
|
|
14
|
-
|
|
15
|
-
|
|
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
|
-
}
|
|
13
|
+
type PublisherConfig = Record<
|
|
14
|
+
string,
|
|
15
|
+
string | [string, PublisherEvent[]] | PublisherEvent[]
|
|
16
|
+
>;
|
|
30
17
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
}
|
|
47
|
-
|
|
48
|
-
if (typeof event === 'string') {
|
|
49
|
-
const regex = new RegExp(event);
|
|
50
|
-
matchesExcluded = regex.test(eventCode);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
if (!matchesExcluded) {
|
|
55
|
-
publishers.push(publisher);
|
|
56
|
-
}
|
|
57
|
-
} else {
|
|
58
|
-
let match = false;
|
|
18
|
+
type PublisherEvent = Record<string, string>;
|
|
19
|
+
interface EventInfo {
|
|
20
|
+
code: string;
|
|
21
|
+
[key: string]: string;
|
|
22
|
+
}
|
|
59
23
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
24
|
+
/**
|
|
25
|
+
* Evaluates an event against publisher configurations
|
|
26
|
+
* @param eventCode event.code parameter of the event
|
|
27
|
+
* @param publisherConfig object containing publisher event consumption configurations
|
|
28
|
+
* @returns list of publishers for which an event is relevant.
|
|
29
|
+
*/
|
|
30
|
+
export function resolveEvent(
|
|
31
|
+
event: EventInfo,
|
|
32
|
+
publisherConfig?: PublisherConfig,
|
|
33
|
+
) {
|
|
34
|
+
if (!publisherConfig) {
|
|
35
|
+
const envConfig = process.env.Broker_PublisherConfig;
|
|
36
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
37
|
+
publisherConfig = envConfig ? JSON.parse(envConfig) : {};
|
|
38
|
+
}
|
|
65
39
|
|
|
66
|
-
|
|
67
|
-
|
|
40
|
+
const publishers = [];
|
|
41
|
+
|
|
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];
|
|
46
|
+
if (publisherEvents) {
|
|
47
|
+
if (publisherEvents[0] === '*') {
|
|
48
|
+
let matchesExcluded = false;
|
|
49
|
+
// Checking whether event matches any of the excluded events
|
|
50
|
+
if (
|
|
51
|
+
Array.isArray(publisherEvents[1]) &&
|
|
52
|
+
publisherEvents[1].length > 0
|
|
53
|
+
) {
|
|
54
|
+
for (const publisherEvent of publisherEvents[1]) {
|
|
55
|
+
if (matchesExcluded) {
|
|
56
|
+
break;
|
|
68
57
|
}
|
|
58
|
+
|
|
59
|
+
matchesExcluded = matchEvent(event, publisherEvent);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if (!matchesExcluded) {
|
|
63
|
+
publishers.push(publisher);
|
|
64
|
+
}
|
|
65
|
+
} else {
|
|
66
|
+
let match = false;
|
|
67
|
+
|
|
68
|
+
for (const publisherEvent of publisherEvents) {
|
|
69
|
+
if (Array.isArray(publisherEvent)) {
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
match = matchEvent(event, publisherEvent);
|
|
73
|
+
if (match) {
|
|
74
|
+
publishers.push(publisher);
|
|
69
75
|
}
|
|
70
76
|
}
|
|
71
77
|
}
|
|
72
78
|
}
|
|
79
|
+
}
|
|
73
80
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
if (publishers.length === 0) {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
77
84
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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);
|
|
95
|
-
}
|
|
85
|
+
return publishers;
|
|
86
|
+
}
|
|
96
87
|
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
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;
|
|
110
106
|
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
},
|
|
114
|
-
|
|
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.');
|
|
107
|
+
} else {
|
|
108
|
+
throw new Error('Unexpected formatting in publisher configuration');
|
|
123
109
|
}
|
|
124
|
-
cosmosClient = new CosmosClient(cosmosConnectionString);
|
|
125
110
|
}
|
|
111
|
+
return true;
|
|
112
|
+
} else {
|
|
113
|
+
throw new Error('Unexpected formatting in publisher configuration');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
126
116
|
|
|
127
|
-
|
|
128
|
-
|
|
117
|
+
/**
|
|
118
|
+
* Sends an event to Service Bus
|
|
119
|
+
* @param message event to be sent
|
|
120
|
+
* @param queues list of Service Bus queues to send the event to
|
|
121
|
+
*/
|
|
122
|
+
export async function dispatchEvent(
|
|
123
|
+
message: Record<string, string>,
|
|
124
|
+
queues: string[],
|
|
125
|
+
) {
|
|
126
|
+
if (!serviceBusClient) {
|
|
127
|
+
if (!serviceBusConnectionString) {
|
|
128
|
+
throw new Error('Service Bus connection string is missing.');
|
|
129
129
|
}
|
|
130
|
+
serviceBusClient = new ServiceBusClient(serviceBusConnectionString);
|
|
131
|
+
}
|
|
130
132
|
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
133
|
+
await Promise.all(
|
|
134
|
+
queues.map(async (queue) => {
|
|
135
|
+
try {
|
|
136
|
+
let sender;
|
|
137
|
+
if (senders[queue]) {
|
|
138
|
+
sender = senders[queue];
|
|
139
|
+
} else {
|
|
140
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
141
|
+
senders[queue] = serviceBusClient!.createSender(queue);
|
|
142
|
+
sender = senders[queue];
|
|
143
|
+
}
|
|
144
|
+
await sender.sendMessages({ body: message });
|
|
145
|
+
} catch (e) {
|
|
146
|
+
console.log(`Sending to queue ${queue} failed`);
|
|
147
|
+
}
|
|
148
|
+
}),
|
|
149
|
+
);
|
|
150
|
+
}
|
|
134
151
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
152
|
+
/**
|
|
153
|
+
* Logs an event to Cosmos DB
|
|
154
|
+
* @param message event to be logged
|
|
155
|
+
*/
|
|
156
|
+
export async function logToCosmosDb(message: ServiceBusMessage) {
|
|
157
|
+
if (!cosmosClient) {
|
|
158
|
+
if (!cosmosConnectionString) {
|
|
159
|
+
throw new Error('Cosmos DB connection string is missing.');
|
|
142
160
|
}
|
|
143
|
-
|
|
144
|
-
}
|
|
161
|
+
cosmosClient = new CosmosClient(cosmosConnectionString);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (!cosmosDatabaseName) {
|
|
165
|
+
throw new Error('Cosmos DB database name is missing.');
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
if (!cosmosContainerName) {
|
|
169
|
+
throw new Error('Cosmos DB events container name is missing.');
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
try {
|
|
173
|
+
await cosmosClient
|
|
174
|
+
.database(cosmosDatabaseName)
|
|
175
|
+
.container(cosmosContainerName)
|
|
176
|
+
.items.create(message);
|
|
177
|
+
} catch (e) {
|
|
178
|
+
utils.logException(e as Error);
|
|
179
|
+
}
|
|
180
|
+
}
|