@mhubcloud/tms_core 3.4.1 → 3.4.3
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 +176 -24
- package/package.json +1 -1
- package/src/defaultPublisher.ts +2 -10
- package/src/index.ts +3 -3
- package/src/publishers/googleAds.ts +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,15 @@ 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
|
-
throw
|
|
433
|
+
const formattedError = formatError(result.reason);
|
|
434
|
+
logException(new Error(formattedError.message));
|
|
435
|
+
throw formattedError;
|
|
289
436
|
} else {
|
|
290
|
-
const
|
|
437
|
+
const formattedError = formatError(
|
|
291
438
|
typeof result.reason === "object" ? JSON.stringify(result.reason) : result.reason
|
|
292
439
|
);
|
|
293
|
-
logException(
|
|
294
|
-
throw
|
|
440
|
+
logException(new Error(formattedError.message));
|
|
441
|
+
throw formattedError;
|
|
295
442
|
}
|
|
296
443
|
}
|
|
297
444
|
}
|
|
@@ -328,7 +475,6 @@ async function processMessage(message, log2, platformPublisher, trigger) {
|
|
|
328
475
|
publishedAny = true;
|
|
329
476
|
} else {
|
|
330
477
|
logException(
|
|
331
|
-
log2,
|
|
332
478
|
new Error(
|
|
333
479
|
`The message has not been published. Source: ${platformPublisher.source}`
|
|
334
480
|
)
|
|
@@ -337,7 +483,6 @@ async function processMessage(message, log2, platformPublisher, trigger) {
|
|
|
337
483
|
}
|
|
338
484
|
if (!publishedAny) {
|
|
339
485
|
logException(
|
|
340
|
-
log2,
|
|
341
486
|
new Error(
|
|
342
487
|
"The message has not been published to any publisher configuration"
|
|
343
488
|
)
|
|
@@ -532,7 +677,13 @@ var googleAds = {
|
|
|
532
677
|
} else if (responseJson.error) {
|
|
533
678
|
throw new Error(responseJson.error.message);
|
|
534
679
|
} else {
|
|
535
|
-
logEvent(
|
|
680
|
+
logEvent(
|
|
681
|
+
this.source,
|
|
682
|
+
event.event,
|
|
683
|
+
event.region,
|
|
684
|
+
response.status,
|
|
685
|
+
requestOptions.body
|
|
686
|
+
);
|
|
536
687
|
}
|
|
537
688
|
},
|
|
538
689
|
async getToken(config) {
|
|
@@ -740,7 +891,7 @@ var publisher = {
|
|
|
740
891
|
Facebook: facebook,
|
|
741
892
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
742
893
|
CJ: cj,
|
|
743
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
894
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unnecessary-type-assertion
|
|
744
895
|
GoogleAds: googleAds,
|
|
745
896
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
746
897
|
GoogleAnalytics4: googleAnalytics4,
|
|
@@ -754,6 +905,7 @@ var publisher = {
|
|
|
754
905
|
// Annotate the CommonJS export names for ESM import in node:
|
|
755
906
|
0 && (module.exports = {
|
|
756
907
|
DefaultPublisher,
|
|
908
|
+
broker,
|
|
757
909
|
publisher,
|
|
758
910
|
testUtils,
|
|
759
911
|
utils
|
package/package.json
CHANGED
package/src/defaultPublisher.ts
CHANGED
|
@@ -41,10 +41,7 @@ export async function defaultPublisher(
|
|
|
41
41
|
if (result.status === 'rejected') {
|
|
42
42
|
if (result.reason instanceof Error) {
|
|
43
43
|
const formattedError = utils.formatError(result.reason);
|
|
44
|
-
utils.logException(
|
|
45
|
-
azureFunctionContext.log,
|
|
46
|
-
new Error(formattedError.message),
|
|
47
|
-
);
|
|
44
|
+
utils.logException(new Error(formattedError.message));
|
|
48
45
|
throw formattedError;
|
|
49
46
|
} else {
|
|
50
47
|
const formattedError = utils.formatError(
|
|
@@ -52,10 +49,7 @@ export async function defaultPublisher(
|
|
|
52
49
|
? JSON.stringify(result.reason)
|
|
53
50
|
: (result.reason as Error['message']),
|
|
54
51
|
);
|
|
55
|
-
utils.logException(
|
|
56
|
-
azureFunctionContext.log,
|
|
57
|
-
new Error(formattedError.message),
|
|
58
|
-
);
|
|
52
|
+
utils.logException(new Error(formattedError.message));
|
|
59
53
|
throw formattedError;
|
|
60
54
|
}
|
|
61
55
|
}
|
|
@@ -112,7 +106,6 @@ async function processMessage(
|
|
|
112
106
|
publishedAny = true;
|
|
113
107
|
} else {
|
|
114
108
|
utils.logException(
|
|
115
|
-
log,
|
|
116
109
|
new Error(
|
|
117
110
|
`The message has not been published. Source: ${platformPublisher.source}`,
|
|
118
111
|
),
|
|
@@ -121,7 +114,6 @@ async function processMessage(
|
|
|
121
114
|
}
|
|
122
115
|
if (!publishedAny) {
|
|
123
116
|
utils.logException(
|
|
124
|
-
log,
|
|
125
117
|
new Error(
|
|
126
118
|
'The message has not been published to any publisher configuration',
|
|
127
119
|
),
|
package/src/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { defaultPublisher } from './defaultPublisher';
|
|
|
3
3
|
import { affiliatePort } from './publishers/affiliatePort';
|
|
4
4
|
import { cj } from './publishers/cj';
|
|
5
5
|
import { facebook } from './publishers/facebook';
|
|
6
|
-
import { googleAds } from './publishers/googleAds';
|
|
6
|
+
import { googleAds, GoogleAdsPlatformPublisher } from './publishers/googleAds';
|
|
7
7
|
import { googleAnalytics4 } from './publishers/googleAnalytics4';
|
|
8
8
|
import { piano } from './publishers/piano';
|
|
9
9
|
import { universalAnalytics } from './publishers/universalAnalytics';
|
|
@@ -15,8 +15,8 @@ export const publisher = {
|
|
|
15
15
|
Facebook: facebook,
|
|
16
16
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
17
17
|
CJ: cj,
|
|
18
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
19
|
-
GoogleAds: googleAds,
|
|
18
|
+
// eslint-disable-next-line @typescript-eslint/naming-convention, @typescript-eslint/no-unnecessary-type-assertion
|
|
19
|
+
GoogleAds: googleAds as GoogleAdsPlatformPublisher,
|
|
20
20
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
21
21
|
GoogleAnalytics4: googleAnalytics4,
|
|
22
22
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
@@ -3,7 +3,7 @@ import * as utils from './../utils/utils';
|
|
|
3
3
|
import { EventToLog } from '../types/EventToLog';
|
|
4
4
|
import { TypeSafeObject } from '../types/UtilTypes';
|
|
5
5
|
|
|
6
|
-
interface GoogleAdsPlatformPublisher {
|
|
6
|
+
export interface GoogleAdsPlatformPublisher {
|
|
7
7
|
source: string;
|
|
8
8
|
publish: (
|
|
9
9
|
log: Logger,
|