@mhubcloud/tms_core 2.0.0-beta.4 → 2.0.1-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 +13 -3
- package/index.js +1 -0
- package/package.json +5 -6
- package/publishers/TableStorage.js +52 -0
- package/src/utils/utils.ts +16 -3
package/dist/index.js
CHANGED
|
@@ -53,7 +53,6 @@ __export(utils_exports, {
|
|
|
53
53
|
});
|
|
54
54
|
var crypto = __toESM(require("crypto"));
|
|
55
55
|
var appInsights = __toESM(require("applicationinsights"));
|
|
56
|
-
var import_node_fetch = __toESM(require("node-fetch"));
|
|
57
56
|
var aiClient = null;
|
|
58
57
|
if (process.env.APPINSIGHTS_INSTRUMENTATIONKEY) {
|
|
59
58
|
appInsights.setup().setAutoCollectDependencies(false).setAutoCollectConsole(true, true).setSendLiveMetrics(false).setAutoCollectRequests(false).setAutoCollectPerformance(false).setUseDiskRetryCaching(false).start();
|
|
@@ -199,7 +198,10 @@ async function handlePlatformRequest({
|
|
|
199
198
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
200
199
|
headers: { ...requestOptions.headers }
|
|
201
200
|
};
|
|
202
|
-
const rawPlatformResponse = await (
|
|
201
|
+
const rawPlatformResponse = await fetchWithTimeout(
|
|
202
|
+
fullUrl,
|
|
203
|
+
fetchRequestOptions
|
|
204
|
+
);
|
|
203
205
|
if (process.env.PUBLISHER_TESTURL) {
|
|
204
206
|
await handleTestRequest({
|
|
205
207
|
fetchRequestOptions,
|
|
@@ -228,13 +230,21 @@ async function handleTestRequest(params) {
|
|
|
228
230
|
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
229
231
|
"x-mhub-orig-response-status-message": params.rawPlatformResponse.statusText
|
|
230
232
|
};
|
|
231
|
-
await (
|
|
233
|
+
await fetchWithTimeout(
|
|
232
234
|
`${process.env.PUBLISHER_TESTURL ?? ""} + ${params.query ?? ""}`,
|
|
233
235
|
params.fetchRequestOptions
|
|
234
236
|
);
|
|
235
237
|
} catch {
|
|
236
238
|
}
|
|
237
239
|
}
|
|
240
|
+
async function fetchWithTimeout(resource, options = {}) {
|
|
241
|
+
const { timeoutMS = 3e3 } = options;
|
|
242
|
+
const response = await fetch(resource, {
|
|
243
|
+
...options,
|
|
244
|
+
signal: AbortSignal.timeout(timeoutMS)
|
|
245
|
+
});
|
|
246
|
+
return response;
|
|
247
|
+
}
|
|
238
248
|
|
|
239
249
|
// src/defaultPublisher.ts
|
|
240
250
|
async function defaultPublisher(azureFunctionContext, eventHubMessages, source, platformPublisher, trigger) {
|
package/index.js
CHANGED
|
@@ -10,6 +10,7 @@ module.exports = {
|
|
|
10
10
|
GoogleAnalytics4 : require('./publishers/GoogleAnalytics4'),
|
|
11
11
|
CJ : require('./publishers/CJ'),
|
|
12
12
|
UniversalAnalytics : require('./publishers/UniversalAnalytics'),
|
|
13
|
+
TableStorage : require('./publishers/TableStorage'),
|
|
13
14
|
HttpPublisher : require('./publishers/HttpPublisher'),
|
|
14
15
|
AffiliatePort : require('./publishers/AffiliatePort')
|
|
15
16
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mhubcloud/tms_core",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.1-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,15 +16,14 @@
|
|
|
16
16
|
"conversion API"
|
|
17
17
|
],
|
|
18
18
|
"dependencies": {
|
|
19
|
+
"@azure/data-tables": "^13.2.2",
|
|
19
20
|
"ajv": "^8.12.0",
|
|
20
|
-
"applicationinsights": "^2.6.0"
|
|
21
|
-
"node-fetch": "^2.6.11"
|
|
21
|
+
"applicationinsights": "^2.6.0"
|
|
22
22
|
},
|
|
23
23
|
"devDependencies": {
|
|
24
24
|
"@azure/functions": "^3.5.1",
|
|
25
25
|
"@tsconfig/recommended": "^1.0.2",
|
|
26
26
|
"@types/node": "^18.16.14",
|
|
27
|
-
"@types/node-fetch": "^2.6.4",
|
|
28
27
|
"@typescript-eslint/eslint-plugin": "^5.59.7",
|
|
29
28
|
"@typescript-eslint/parser": "^5.59.7",
|
|
30
29
|
"eslint": "^8.41.0",
|
|
@@ -39,8 +38,8 @@
|
|
|
39
38
|
"typescript": "^4.9.5"
|
|
40
39
|
},
|
|
41
40
|
"scripts": {
|
|
41
|
+
"test": "mocha ./tests/*.js --local-settings=./local.settings.json --timeout 100000",
|
|
42
42
|
"dev": "ts-node index.ts",
|
|
43
|
-
"build": "tsup ./src/index.ts --format cjs --target node18"
|
|
44
|
-
"test": "mocha ./tests/*.js --timeout 10000 "
|
|
43
|
+
"build": "tsup ./src/index.ts --format cjs --target node18"
|
|
45
44
|
}
|
|
46
45
|
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
const { TableClient } = require('@azure/data-tables');
|
|
2
|
+
const utils = require('../src/utils/utils');
|
|
3
|
+
const SOURCE = 'f_TableStorage';
|
|
4
|
+
|
|
5
|
+
module.exports = {
|
|
6
|
+
/**
|
|
7
|
+
* Processed data comming from EventHub to data for further processing
|
|
8
|
+
* @param {Object} data The data comming from EventHub
|
|
9
|
+
* @returns {Object} Data for further processing
|
|
10
|
+
*/
|
|
11
|
+
parse: function parse(data, context) {
|
|
12
|
+
// ConsumerBase.cs:ConsumeOneEventAsync
|
|
13
|
+
data.enqueuedAt = context.test ? 1600000000000 : Date.now();
|
|
14
|
+
return data;
|
|
15
|
+
},
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Publishes data to TableStorage
|
|
19
|
+
* https://www.npmjs.com/package/@azure/data-tables
|
|
20
|
+
* @param {{id: string, access_token: string}} config Publisher configuration
|
|
21
|
+
* @param {Object} data Data in publisher-friendly format to be published
|
|
22
|
+
* @param {string} event for which is it triggered
|
|
23
|
+
*/
|
|
24
|
+
publish: async function publish(config, data, event) {
|
|
25
|
+
data = utils.removeUndefined(data);
|
|
26
|
+
|
|
27
|
+
const client = new TableClient(config.connectionString, config.tableName);
|
|
28
|
+
|
|
29
|
+
await client.createEntity(data);
|
|
30
|
+
|
|
31
|
+
console.log(`TableStorage - ${event} - sent`);
|
|
32
|
+
},
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Validates an object against given schema
|
|
36
|
+
* @param {Object} data An object to validate.
|
|
37
|
+
* @param {string} schema Name of the schema. That's name of a file with `.schema.json` extension in `customizable/schemas` subfolder of current publisher folder
|
|
38
|
+
* @returns {boolean} Validation result, true if successful, false if unsuccessfull (in which case more info is written to console)
|
|
39
|
+
*/
|
|
40
|
+
validate: function validate(data, schema) {
|
|
41
|
+
if (!schema) {
|
|
42
|
+
throw new Error('Schema parameters not provided');
|
|
43
|
+
}
|
|
44
|
+
if (schema.includes('/')) {
|
|
45
|
+
throw new Error(
|
|
46
|
+
'Schema must be provided as schema name, not schema path',
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
// More validation done by utils.validate
|
|
50
|
+
return utils.validate(data, SOURCE, schema);
|
|
51
|
+
},
|
|
52
|
+
};
|
package/src/utils/utils.ts
CHANGED
|
@@ -5,7 +5,6 @@ import {
|
|
|
5
5
|
SeverityLevel,
|
|
6
6
|
Telemetry,
|
|
7
7
|
} from 'applicationinsights/out/Declarations/Contracts';
|
|
8
|
-
import fetch, { RequestInit, Response } from 'node-fetch';
|
|
9
8
|
import { EventPublisherBaseClass } from '../types/EventPublisher';
|
|
10
9
|
import { PlatformRequestOptions } from '../types/PlatformPublisher';
|
|
11
10
|
import { TypeSafeObject } from '../types/UtilTypes';
|
|
@@ -307,7 +306,10 @@ export async function handlePlatformRequest({
|
|
|
307
306
|
headers: { ...requestOptions.headers },
|
|
308
307
|
};
|
|
309
308
|
|
|
310
|
-
const rawPlatformResponse = await
|
|
309
|
+
const rawPlatformResponse = await fetchWithTimeout(
|
|
310
|
+
fullUrl,
|
|
311
|
+
fetchRequestOptions,
|
|
312
|
+
);
|
|
311
313
|
|
|
312
314
|
if (process.env.PUBLISHER_TESTURL) {
|
|
313
315
|
await handleTestRequest({
|
|
@@ -348,7 +350,7 @@ async function handleTestRequest(params: HandleTestRequestParams) {
|
|
|
348
350
|
'x-mhub-orig-response-status-message':
|
|
349
351
|
params.rawPlatformResponse.statusText,
|
|
350
352
|
};
|
|
351
|
-
await
|
|
353
|
+
await fetchWithTimeout(
|
|
352
354
|
`${process.env.PUBLISHER_TESTURL ?? ''} + ${params.query ?? ''}`,
|
|
353
355
|
params.fetchRequestOptions,
|
|
354
356
|
);
|
|
@@ -356,3 +358,14 @@ async function handleTestRequest(params: HandleTestRequestParams) {
|
|
|
356
358
|
/* empty */
|
|
357
359
|
}
|
|
358
360
|
}
|
|
361
|
+
|
|
362
|
+
async function fetchWithTimeout(resource, options = {}) {
|
|
363
|
+
const { timeoutMS = 3000 } = options;
|
|
364
|
+
|
|
365
|
+
const response = await fetch(resource, {
|
|
366
|
+
...options,
|
|
367
|
+
signal: AbortSignal.timeout(timeoutMS),
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
return response;
|
|
371
|
+
}
|