@mhubcloud/tms_core 3.4.1 → 3.4.2
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 +181 -21
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -31,12 +31,23 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var index_exports = {};
|
|
32
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
39
|
module.exports = __toCommonJS(index_exports);
|
|
39
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
|
+
|
|
40
51
|
// src/utils/utils.ts
|
|
41
52
|
var utils_exports = {};
|
|
42
53
|
__export(utils_exports, {
|
|
@@ -44,19 +55,19 @@ __export(utils_exports, {
|
|
|
44
55
|
encodeEmail: () => encodeEmail,
|
|
45
56
|
extractAdCode: () => extractAdCode,
|
|
46
57
|
flatten: () => flatten,
|
|
58
|
+
formatError: () => formatError,
|
|
47
59
|
handlePlatformRequest: () => handlePlatformRequest,
|
|
48
60
|
handleResponse: () => handleResponse,
|
|
49
61
|
logEvent: () => logEvent,
|
|
50
62
|
logException: () => logException,
|
|
51
63
|
logMessage: () => logMessage,
|
|
52
|
-
parseErrorMessage: () => parseErrorMessage,
|
|
53
64
|
prepareTestRequestHeaders: () => prepareTestRequestHeaders,
|
|
54
65
|
removeUndefined: () => removeUndefined
|
|
55
66
|
});
|
|
56
67
|
var crypto = __toESM(require("crypto"));
|
|
57
68
|
var appInsights = __toESM(require("applicationinsights"));
|
|
58
69
|
var aiClient = null;
|
|
59
|
-
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING
|
|
70
|
+
if (process.env.APPLICATIONINSIGHTS_CONNECTION_STRING) {
|
|
60
71
|
appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, false).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false).setUseDiskRetryCaching(false).start();
|
|
61
72
|
aiClient = appInsights.defaultClient;
|
|
62
73
|
}
|
|
@@ -89,15 +100,17 @@ function logMessage(level, message, properties) {
|
|
|
89
100
|
properties
|
|
90
101
|
});
|
|
91
102
|
}
|
|
92
|
-
function logEvent(publisher2, event, region) {
|
|
103
|
+
function logEvent(publisher2, event, region, status, payload) {
|
|
93
104
|
const properties = {
|
|
94
105
|
publisher: publisher2,
|
|
95
106
|
event,
|
|
96
|
-
region
|
|
107
|
+
region,
|
|
108
|
+
status,
|
|
109
|
+
payload
|
|
97
110
|
};
|
|
98
111
|
logMessage(1, "Event dispatched to platform", properties);
|
|
99
112
|
}
|
|
100
|
-
function logException(
|
|
113
|
+
function logException(exception) {
|
|
101
114
|
aiClient?.trackException({ exception });
|
|
102
115
|
}
|
|
103
116
|
function removeUndefined(originalValue) {
|
|
@@ -211,7 +224,10 @@ async function handlePlatformRequest({
|
|
|
211
224
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
212
225
|
headers: { ...requestOptions.headers }
|
|
213
226
|
};
|
|
214
|
-
const rawPlatformResponse = await fetch(
|
|
227
|
+
const rawPlatformResponse = await fetch(
|
|
228
|
+
fullUrl,
|
|
229
|
+
fetchRequestOptions
|
|
230
|
+
);
|
|
215
231
|
if (process.env.PUBLISHER_TESTURL) {
|
|
216
232
|
await handleTestRequest({
|
|
217
233
|
fetchRequestOptions,
|
|
@@ -225,10 +241,22 @@ async function handlePlatformRequest({
|
|
|
225
241
|
rawPlatformResponse,
|
|
226
242
|
event.event
|
|
227
243
|
);
|
|
228
|
-
logEvent(
|
|
244
|
+
logEvent(
|
|
245
|
+
source,
|
|
246
|
+
event.event,
|
|
247
|
+
event.region,
|
|
248
|
+
rawPlatformResponse.status,
|
|
249
|
+
requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
|
|
250
|
+
);
|
|
229
251
|
return processedPlatformResponse;
|
|
230
252
|
} else {
|
|
231
|
-
logEvent(
|
|
253
|
+
logEvent(
|
|
254
|
+
source,
|
|
255
|
+
event.event,
|
|
256
|
+
event.region,
|
|
257
|
+
rawPlatformResponse.status,
|
|
258
|
+
requestOptions.body ? JSON.stringify(requestOptions.body) : void 0
|
|
259
|
+
);
|
|
232
260
|
}
|
|
233
261
|
}
|
|
234
262
|
async function handleTestRequest(params) {
|
|
@@ -251,18 +279,137 @@ async function handleTestRequest(params) {
|
|
|
251
279
|
} catch {
|
|
252
280
|
}
|
|
253
281
|
}
|
|
254
|
-
function
|
|
255
|
-
let
|
|
282
|
+
function formatError(error) {
|
|
283
|
+
let formattedError;
|
|
256
284
|
if (error instanceof Error) {
|
|
285
|
+
formattedError = error;
|
|
257
286
|
if (error.cause) {
|
|
258
|
-
|
|
287
|
+
formattedError.message = `${error.message} Cause: ${error.cause.toString()}`;
|
|
259
288
|
} else {
|
|
260
|
-
|
|
289
|
+
formattedError.message = error.message;
|
|
261
290
|
}
|
|
262
291
|
} else {
|
|
263
|
-
|
|
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(event, 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 publisherEvent of publisherEvents[1]) {
|
|
318
|
+
if (matchesExcluded) {
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
matchesExcluded = matchEvent(event, publisherEvent);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
if (!matchesExcluded) {
|
|
325
|
+
publishers.push(publisher2);
|
|
326
|
+
}
|
|
327
|
+
} else {
|
|
328
|
+
let match = false;
|
|
329
|
+
for (const publisherEvent of publisherEvents) {
|
|
330
|
+
if (Array.isArray(publisherEvent)) {
|
|
331
|
+
continue;
|
|
332
|
+
}
|
|
333
|
+
match = matchEvent(event, publisherEvent);
|
|
334
|
+
if (match) {
|
|
335
|
+
publishers.push(publisher2);
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
if (publishers.length === 0) {
|
|
342
|
+
return null;
|
|
343
|
+
}
|
|
344
|
+
return publishers;
|
|
345
|
+
}
|
|
346
|
+
function matchEvent(event, publisherEvent) {
|
|
347
|
+
if (typeof publisherEvent === "string") {
|
|
348
|
+
const regex = new RegExp(publisherEvent);
|
|
349
|
+
const match = regex.test(event.code);
|
|
350
|
+
return match;
|
|
351
|
+
} else if (typeof publisherEvent === "object") {
|
|
352
|
+
let paramMatch = true;
|
|
353
|
+
for (const key of Object.keys(publisherEvent)) {
|
|
354
|
+
const publisherEventParam = publisherEvent[key];
|
|
355
|
+
const eventParam = event[key];
|
|
356
|
+
if (typeof publisherEventParam === "string" && typeof eventParam === "string") {
|
|
357
|
+
const regex = new RegExp(publisherEventParam);
|
|
358
|
+
paramMatch = regex.test(eventParam);
|
|
359
|
+
if (!paramMatch) {
|
|
360
|
+
return false;
|
|
361
|
+
}
|
|
362
|
+
} else {
|
|
363
|
+
throw new Error("Unexpected formatting in publisher configuration");
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return true;
|
|
367
|
+
} else {
|
|
368
|
+
throw new Error("Unexpected formatting in publisher configuration");
|
|
369
|
+
}
|
|
370
|
+
}
|
|
371
|
+
async function dispatchEvent(message, queues) {
|
|
372
|
+
if (!serviceBusClient) {
|
|
373
|
+
if (!serviceBusConnectionString) {
|
|
374
|
+
throw new Error("Service Bus connection string is missing.");
|
|
375
|
+
}
|
|
376
|
+
serviceBusClient = new import_service_bus.ServiceBusClient(serviceBusConnectionString);
|
|
377
|
+
}
|
|
378
|
+
await Promise.all(
|
|
379
|
+
queues.map(async (queue) => {
|
|
380
|
+
try {
|
|
381
|
+
let sender;
|
|
382
|
+
if (senders[queue]) {
|
|
383
|
+
sender = senders[queue];
|
|
384
|
+
} else {
|
|
385
|
+
senders[queue] = serviceBusClient.createSender(queue);
|
|
386
|
+
sender = senders[queue];
|
|
387
|
+
}
|
|
388
|
+
await sender.sendMessages({ body: message });
|
|
389
|
+
} catch (e) {
|
|
390
|
+
console.log(`Sending to queue ${queue} failed`);
|
|
391
|
+
}
|
|
392
|
+
})
|
|
393
|
+
);
|
|
394
|
+
}
|
|
395
|
+
async function logToCosmosDb(message) {
|
|
396
|
+
if (!cosmosClient) {
|
|
397
|
+
if (!cosmosConnectionString) {
|
|
398
|
+
throw new Error("Cosmos DB connection string is missing.");
|
|
399
|
+
}
|
|
400
|
+
cosmosClient = new import_cosmos.CosmosClient(cosmosConnectionString);
|
|
401
|
+
}
|
|
402
|
+
if (!cosmosDatabaseName) {
|
|
403
|
+
throw new Error("Cosmos DB database name is missing.");
|
|
404
|
+
}
|
|
405
|
+
if (!cosmosContainerName) {
|
|
406
|
+
throw new Error("Cosmos DB events container name is missing.");
|
|
407
|
+
}
|
|
408
|
+
try {
|
|
409
|
+
await cosmosClient.database(cosmosDatabaseName).container(cosmosContainerName).items.create(message);
|
|
410
|
+
} catch (e) {
|
|
411
|
+
logException(e);
|
|
264
412
|
}
|
|
265
|
-
return errorMessage;
|
|
266
413
|
}
|
|
267
414
|
|
|
268
415
|
// src/defaultPublisher.ts
|
|
@@ -283,15 +430,21 @@ async function defaultPublisher(azureFunctionContext, serviceBusMessage, source,
|
|
|
283
430
|
for (const result of results) {
|
|
284
431
|
if (result.status === "rejected") {
|
|
285
432
|
if (result.reason instanceof Error) {
|
|
286
|
-
const
|
|
287
|
-
logException(
|
|
288
|
-
|
|
433
|
+
const formattedError = formatError(result.reason);
|
|
434
|
+
logException(
|
|
435
|
+
azureFunctionContext.log,
|
|
436
|
+
new Error(formattedError.message)
|
|
437
|
+
);
|
|
438
|
+
throw formattedError;
|
|
289
439
|
} else {
|
|
290
|
-
const
|
|
440
|
+
const formattedError = formatError(
|
|
291
441
|
typeof result.reason === "object" ? JSON.stringify(result.reason) : result.reason
|
|
292
442
|
);
|
|
293
|
-
logException(
|
|
294
|
-
|
|
443
|
+
logException(
|
|
444
|
+
azureFunctionContext.log,
|
|
445
|
+
new Error(formattedError.message)
|
|
446
|
+
);
|
|
447
|
+
throw formattedError;
|
|
295
448
|
}
|
|
296
449
|
}
|
|
297
450
|
}
|
|
@@ -532,7 +685,13 @@ var googleAds = {
|
|
|
532
685
|
} else if (responseJson.error) {
|
|
533
686
|
throw new Error(responseJson.error.message);
|
|
534
687
|
} else {
|
|
535
|
-
logEvent(
|
|
688
|
+
logEvent(
|
|
689
|
+
this.source,
|
|
690
|
+
event.event,
|
|
691
|
+
event.region,
|
|
692
|
+
response.status,
|
|
693
|
+
requestOptions.body
|
|
694
|
+
);
|
|
536
695
|
}
|
|
537
696
|
},
|
|
538
697
|
async getToken(config) {
|
|
@@ -754,6 +913,7 @@ var publisher = {
|
|
|
754
913
|
// Annotate the CommonJS export names for ESM import in node:
|
|
755
914
|
0 && (module.exports = {
|
|
756
915
|
DefaultPublisher,
|
|
916
|
+
broker,
|
|
757
917
|
publisher,
|
|
758
918
|
testUtils,
|
|
759
919
|
utils
|