@coherentglobal/spark-execute-sdk 0.3.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/.babelrc +12 -0
- package/.eslintrc.json +21 -0
- package/.github/workflows/build-publish.yml +46 -0
- package/.prettierrc +0 -0
- package/README.md +92 -0
- package/dist/browser.js +6925 -0
- package/examples/UsingBearerTokenFNAsync/bearerTokenExample.js +22 -0
- package/examples/UsingBearerTokenFNAsync/config.js +88 -0
- package/examples/UsingBearerTokenFNAsync/index.html +205 -0
- package/examples/UsingBearerTokenFNAsync/modelCInput.json +15 -0
- package/examples/UsingBearerTokenFNAsync/models/Model_C.zip +0 -0
- package/examples/UsingBearerTokenString/bearerTokenExample.js +22 -0
- package/examples/UsingBearerTokenString/config.js +182 -0
- package/examples/UsingBearerTokenString/index.html +191 -0
- package/examples/UsingBearerTokenString/modelCInput.json +15 -0
- package/examples/UsingBearerTokenString/models/Model_C.zip +0 -0
- package/examples/UsingSyntheticKeyFNAsync/bearerTokenExample.js +22 -0
- package/examples/UsingSyntheticKeyFNAsync/config.js +195 -0
- package/examples/UsingSyntheticKeyFNAsync/index.html +205 -0
- package/examples/UsingSyntheticKeyFNAsync/modelCInput.json +15 -0
- package/examples/UsingSyntheticKeyFNAsync/models/Model_C.zip +0 -0
- package/examples/UsingSyntheticKeyString/bearerTokenExample.js +22 -0
- package/examples/UsingSyntheticKeyString/config.js +181 -0
- package/examples/UsingSyntheticKeyString/index.html +192 -0
- package/examples/UsingSyntheticKeyString/modelCInput.json +15 -0
- package/examples/UsingSyntheticKeyString/models/Model_C.zip +0 -0
- package/examples/asyncFunctionModel/config.js +96 -0
- package/examples/asyncFunctionModel/modelCInput.json +15 -0
- package/examples/asyncFunctionModel/models/Model_C.zip +0 -0
- package/examples/asyncFunctionModel/node.js +125 -0
- package/examples/asyncFunctionModel/tool.js +17 -0
- package/examples/base64Model/config.js +96 -0
- package/examples/base64Model/modelCInput.json +15 -0
- package/examples/base64Model/models/Model_C.zip +0 -0
- package/examples/base64Model/node.js +125 -0
- package/examples/base64Model/tool.js +17 -0
- package/examples/base64modelBrowser/config.js +96 -0
- package/examples/base64modelBrowser/index.html +194 -0
- package/examples/base64modelBrowser/modelCInput.json +15 -0
- package/examples/base64modelBrowser/models/Model_C.zip +0 -0
- package/examples/base64modelBrowser/models/base64model.txt +1 -0
- package/examples/base64modelBrowser/node.js +126 -0
- package/examples/base64modelBrowser/tool.js +17 -0
- package/examples/binary/config.js +96 -0
- package/examples/binary/modelCInput.json +15 -0
- package/examples/binary/models/Model_C.zip +0 -0
- package/examples/binary/models/binary.txt +1 -0
- package/examples/binary/node.js +131 -0
- package/examples/binary/tool.js +18 -0
- package/examples/functionModel/config.js +96 -0
- package/examples/functionModel/modelCInput.json +15 -0
- package/examples/functionModel/models/Model_C.zip +0 -0
- package/examples/functionModel/node.js +138 -0
- package/examples/functionModel/tool.js +17 -0
- package/examples/nodejs/node.js +250 -0
- package/examples/nodejs/test_models/Archive.zip +0 -0
- package/examples/nodejs/test_models/BlackScholes.zip +0 -0
- package/examples/nodejs/test_models/Model_A.zip +0 -0
- package/examples/nodejs/test_models/Par7.zip +0 -0
- package/package.json +79 -0
- package/src/browser.js +198 -0
- package/src/error.js +87 -0
- package/src/helpers/utils.js +79 -0
- package/src/logger.js +3 -0
- package/src/models.js +26 -0
- package/src/node.js +254 -0
- package/src/resolver/externalResolver.js +118 -0
- package/src/services/entityStore.js +260 -0
- package/src/services/resolver.js +134 -0
- package/src/services/spark.js +80 -0
- package/src/tenant_resolver/externalResolver_coherent.js +118 -0
- package/src/tenant_resolver/externalResolver_test.js +118 -0
- package/src/validate.js +38 -0
- package/test/config.test.js +124 -0
- package/test/mock-data/dummy-config.json +102 -0
- package/test/mock-data/valid.json +21 -0
- package/test/spark-sdk.test.js +28 -0
- package/tsconfig.json +105 -0
- package/types/browser.d.ts +72 -0
- package/types/browser.d.ts.map +1 -0
- package/types/error.d.ts +42 -0
- package/types/error.d.ts.map +1 -0
- package/types/helpers/utils.d.ts +11 -0
- package/types/helpers/utils.d.ts.map +1 -0
- package/types/logger.d.ts +3 -0
- package/types/logger.d.ts.map +1 -0
- package/types/models.d.ts +8 -0
- package/types/models.d.ts.map +1 -0
- package/types/node.d.ts +78 -0
- package/types/node.d.ts.map +1 -0
- package/types/resolver/externalResolver.d.ts +2 -0
- package/types/resolver/externalResolver.d.ts.map +1 -0
- package/types/services/entityStore.d.ts +51 -0
- package/types/services/entityStore.d.ts.map +1 -0
- package/types/services/resolver.d.ts +20 -0
- package/types/services/resolver.d.ts.map +1 -0
- package/types/services/spark.d.ts +6 -0
- package/types/services/spark.d.ts.map +1 -0
- package/types/tenant_resolver/externalResolver_coherent.d.ts +2 -0
- package/types/tenant_resolver/externalResolver_coherent.d.ts.map +1 -0
- package/types/tenant_resolver/externalResolver_test.d.ts +2 -0
- package/types/tenant_resolver/externalResolver_test.d.ts.map +1 -0
- package/types/validate.d.ts +4 -0
- package/types/validate.d.ts.map +1 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
const got = require("got");
|
|
2
|
+
const crypto = require("crypto");
|
|
3
|
+
const config = require("../config");
|
|
4
|
+
const { ResolverError } = require("../error");
|
|
5
|
+
const path = require("path");
|
|
6
|
+
|
|
7
|
+
const getCacheKey = (obj) =>
|
|
8
|
+
crypto.createHash("sha256").update(JSON.stringify(obj), "utf8").digest("hex");
|
|
9
|
+
|
|
10
|
+
const resolverCache = new Map();
|
|
11
|
+
|
|
12
|
+
const DEFAULT_CACHE = 600; // 600 seconds
|
|
13
|
+
|
|
14
|
+
const resolve = async (requestMeta, tenant, headers) => {
|
|
15
|
+
const { sparkUrl } = config();
|
|
16
|
+
try {
|
|
17
|
+
const key = getCacheKey(requestMeta);
|
|
18
|
+
if (resolverCache.has(key)) {
|
|
19
|
+
const cache = resolverCache.get(key);
|
|
20
|
+
|
|
21
|
+
if (cache && cache.expireAt > Date.now()) {
|
|
22
|
+
return {
|
|
23
|
+
versionUuid: cache.versionUuid,
|
|
24
|
+
modelUrl: cache.modelUrl,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// If cache already expired
|
|
29
|
+
/* istanbul ignore next */
|
|
30
|
+
resolverCache.delete(key);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const options = {
|
|
34
|
+
headers,
|
|
35
|
+
json: {
|
|
36
|
+
request_data: {},
|
|
37
|
+
request_meta: requestMeta,
|
|
38
|
+
},
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const url = new URL(
|
|
42
|
+
`/${tenant}/api/v3/nodegen/resolver`,
|
|
43
|
+
sparkUrl
|
|
44
|
+
).toString();
|
|
45
|
+
|
|
46
|
+
const response = await got.post(url, options).json();
|
|
47
|
+
|
|
48
|
+
const { response_data: responseData } = response;
|
|
49
|
+
|
|
50
|
+
const {
|
|
51
|
+
cache_duration: cacheDuration,
|
|
52
|
+
version_id: versionUuid,
|
|
53
|
+
wasm_url: wasmUrl,
|
|
54
|
+
instance_no: instanceNo,
|
|
55
|
+
paramset_version_id: paramSetVersionId,
|
|
56
|
+
} = responseData;
|
|
57
|
+
|
|
58
|
+
const date = new Date();
|
|
59
|
+
|
|
60
|
+
const cache = {
|
|
61
|
+
versionUuid: versionUuid,
|
|
62
|
+
modelUrl: wasmUrl,
|
|
63
|
+
instanceNo,
|
|
64
|
+
paramSetVersionId,
|
|
65
|
+
expireAt: date.setSeconds(
|
|
66
|
+
date.getSeconds() + (Number(cacheDuration) || DEFAULT_CACHE)
|
|
67
|
+
),
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
resolverCache.set(key, cache);
|
|
71
|
+
return cache;
|
|
72
|
+
} catch (err) {
|
|
73
|
+
/* istanbul ignore next */
|
|
74
|
+
throw new ResolverError(err.message);
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
const resolveOffline = async (requestMeta, tenant, headers, servicemap) => {
|
|
79
|
+
const { modelLocation } = config();
|
|
80
|
+
try {
|
|
81
|
+
const key = getCacheKey(requestMeta);
|
|
82
|
+
if (resolverCache.has(key)) {
|
|
83
|
+
const cache = resolverCache.get(key);
|
|
84
|
+
|
|
85
|
+
if (cache && cache.expireAt > Date.now()) {
|
|
86
|
+
return {
|
|
87
|
+
versionUuid: cache.versionUuid,
|
|
88
|
+
modelUrl: cache.modelUrl,
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
// If cache already expired
|
|
93
|
+
/* istanbul ignore next */
|
|
94
|
+
resolverCache.delete(key);
|
|
95
|
+
}
|
|
96
|
+
// console.log("OFFLINE: ", servicemap)
|
|
97
|
+
const productName = servicemap.dataJson.EngineInformation.ProductName
|
|
98
|
+
const serviceName = servicemap.dataJson.EngineInformation.ServiceName
|
|
99
|
+
const revision = servicemap.dataJson.EngineInformation.Revision
|
|
100
|
+
const latestVersion = servicemap.latestVersionId
|
|
101
|
+
let wasmUrl = path.join(
|
|
102
|
+
`${modelLocation}/${tenant}/${productName}/${serviceName}/${revision}`,
|
|
103
|
+
`${latestVersion}.zip`
|
|
104
|
+
);
|
|
105
|
+
// console.log('wasmUrl: ', wasmUrl)
|
|
106
|
+
|
|
107
|
+
const responseData = {
|
|
108
|
+
cache_duration: 100,
|
|
109
|
+
version_id: latestVersion,
|
|
110
|
+
wasm_url: wasmUrl,
|
|
111
|
+
instance_no: 0,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const date = new Date();
|
|
115
|
+
|
|
116
|
+
const cache = {
|
|
117
|
+
versionUuid: responseData.version_id,
|
|
118
|
+
modelUrl: responseData.wasm_url,
|
|
119
|
+
instanceNo: responseData.instance_no,
|
|
120
|
+
expireAt: date.setSeconds(date.getSeconds() + DEFAULT_CACHE),
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
resolverCache.set(key, cache);
|
|
124
|
+
return cache;
|
|
125
|
+
} catch (err) {
|
|
126
|
+
/* istanbul ignore next */
|
|
127
|
+
throw new ResolverError(err.message);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
module.exports = {
|
|
132
|
+
resolve,
|
|
133
|
+
resolveOffline,
|
|
134
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
const got = require("got");
|
|
2
|
+
const fs = require("fs");
|
|
3
|
+
const path = require("path");
|
|
4
|
+
// const { isPathExist } = require("../utils");
|
|
5
|
+
const config = require("../config");
|
|
6
|
+
|
|
7
|
+
const DEFAULT_CACHE = 600; // 5 minutes
|
|
8
|
+
const tenantConfigCache = new Map();
|
|
9
|
+
|
|
10
|
+
const configDir = path.join(__dirname, "/tenant_config");
|
|
11
|
+
|
|
12
|
+
async function fetchTenantConfig(tenant, authHeaders) {
|
|
13
|
+
/* istanbul ignore next */
|
|
14
|
+
// if (!(await isPathExist(configDir))) {
|
|
15
|
+
// await fs.promises.mkdir(configDir, { mode: 0o750 });
|
|
16
|
+
// }
|
|
17
|
+
|
|
18
|
+
const { sparkUrl } = config();
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
const key = tenant;
|
|
22
|
+
if (tenantConfigCache.has(key)) {
|
|
23
|
+
const cache = tenantConfigCache.get(key);
|
|
24
|
+
|
|
25
|
+
if (cache.expire_at > Date.now()) {
|
|
26
|
+
return;
|
|
27
|
+
} else {
|
|
28
|
+
/* istanbul ignore next */
|
|
29
|
+
tenantConfigCache.delete(key);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
const options = {
|
|
34
|
+
headers: authHeaders,
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const url = new URL(
|
|
38
|
+
"/api/v1/config/GetAllSparkConfiguration",
|
|
39
|
+
sparkUrl
|
|
40
|
+
).toString();
|
|
41
|
+
|
|
42
|
+
const response = await got.get(url, options).json();
|
|
43
|
+
|
|
44
|
+
const { data } = response;
|
|
45
|
+
|
|
46
|
+
const { ApiDetails: apidetails } = data;
|
|
47
|
+
|
|
48
|
+
const cache = {
|
|
49
|
+
expire_at: Date.now() + DEFAULT_CACHE,
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
tenantConfigCache.set(key, cache);
|
|
53
|
+
await fs.promises.writeFile(
|
|
54
|
+
path.join(configDir, `${tenant}_config.json`),
|
|
55
|
+
JSON.stringify({
|
|
56
|
+
config: {
|
|
57
|
+
apidetails,
|
|
58
|
+
},
|
|
59
|
+
})
|
|
60
|
+
);
|
|
61
|
+
return cache;
|
|
62
|
+
} catch (err) {
|
|
63
|
+
throw new Error(err.message);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async function getConfig(tenant) {
|
|
68
|
+
const configPath = path.join(configDir, `${tenant}_config.json`);
|
|
69
|
+
if (fs.existsSync(configPath)) {
|
|
70
|
+
return JSON.parse(await fs.promises.readFile(configPath));
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
throw new Error(`Tenant: ${tenant} config is empty`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
module.exports = {
|
|
77
|
+
fetchTenantConfig,
|
|
78
|
+
getConfig,
|
|
79
|
+
configDir,
|
|
80
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const { StatusCodes } = require("http-status-codes");
|
|
2
|
+
const { logger } = require("@coherentglobal/node-sdk");
|
|
3
|
+
// const config = require("../config"); env vars
|
|
4
|
+
const {
|
|
5
|
+
handleDirectRequest,
|
|
6
|
+
modelServiceValidation,
|
|
7
|
+
getLatestVersionFromServiceMapTable,
|
|
8
|
+
} = require("../logic/runnerLogic");
|
|
9
|
+
const { getModelViaFolder } = require("../services/entityStore");
|
|
10
|
+
const tenant = "coherent";
|
|
11
|
+
|
|
12
|
+
const CALL_TYPE = {
|
|
13
|
+
0: "SparkService",
|
|
14
|
+
1: "ExternalApi",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const ERROR_CODES = {
|
|
18
|
+
A1: {
|
|
19
|
+
code: "a1",
|
|
20
|
+
message: "ERROR:[Main item] missing in Xcall [NAME] table.",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const XcallResBuilder = {
|
|
25
|
+
Json: function (callType, name, status, errorCode, responseTime, data) {
|
|
26
|
+
return JSON.stringify({
|
|
27
|
+
metadata: {
|
|
28
|
+
calltype: callType,
|
|
29
|
+
name: name,
|
|
30
|
+
status: status,
|
|
31
|
+
error_code: errorCode,
|
|
32
|
+
response_time: responseTime,
|
|
33
|
+
},
|
|
34
|
+
data: data || {},
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
XML: function (callType, name, status, errorCode, responseTime, data) {
|
|
38
|
+
const trimmedData = data.replace(/\<\?xml.+\?\>/gm, "");
|
|
39
|
+
|
|
40
|
+
const response = `
|
|
41
|
+
<data calltype="${callType}" name="${name}" status="${status}" error_code="${errorCode}" response_time = "${responseTime}">
|
|
42
|
+
${trimmedData}
|
|
43
|
+
</data>
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
sparkService: async (requestData, context) => {
|
|
52
|
+
const modelLocation = config().modelLocation;
|
|
53
|
+
const headers = context.headers;
|
|
54
|
+
const folderName = requestData.folder_name;
|
|
55
|
+
const serviceName = requestData.service_name;
|
|
56
|
+
|
|
57
|
+
logger.info({
|
|
58
|
+
eventType: "SparkService",
|
|
59
|
+
msg: `WASMSERVER:DISPATCH_TYPE.SPARK_SERVICE ${tenant}`,
|
|
60
|
+
});
|
|
61
|
+
logger.info({ eventType: "RequestData", msg: requestData });
|
|
62
|
+
const requestBodyFromEngine = JSON.parse(requestData.request_body);
|
|
63
|
+
const requestMetaFromEngine = requestBodyFromEngine.request_meta;
|
|
64
|
+
const findAvailableLatestModelFromLocal =
|
|
65
|
+
getLatestVersionFromServiceMapTable(
|
|
66
|
+
context.servicemap,
|
|
67
|
+
headers.tenant,
|
|
68
|
+
decodeURI(folderName),
|
|
69
|
+
decodeURI(serviceName)
|
|
70
|
+
);
|
|
71
|
+
requestMetaFromEngine.version_id = findAvailableLatestModelFromLocal;
|
|
72
|
+
const serviceMap = modelServiceValidation(
|
|
73
|
+
context.servicemap,
|
|
74
|
+
headers.tenant,
|
|
75
|
+
requestMetaFromEngine
|
|
76
|
+
);
|
|
77
|
+
// const modelVersion = modelLocation
|
|
78
|
+
// ? // ? await resolveOffline(requestMetaFromEngine, headers.tenant, headers, serviceMap)
|
|
79
|
+
// findAvailableLatestModelFromLocal
|
|
80
|
+
// : await getModelViaFolder(folderName, serviceName, headers);
|
|
81
|
+
|
|
82
|
+
const modelVersion = await getModelViaFolder(
|
|
83
|
+
folderName,
|
|
84
|
+
serviceName,
|
|
85
|
+
headers
|
|
86
|
+
);
|
|
87
|
+
const params = {
|
|
88
|
+
tenant: headers.tenant,
|
|
89
|
+
};
|
|
90
|
+
const body = {
|
|
91
|
+
...requestBodyFromEngine,
|
|
92
|
+
request_meta: {
|
|
93
|
+
...requestBodyFromEngine.request_meta,
|
|
94
|
+
version_id: modelVersion,
|
|
95
|
+
call_purpose: "Spark - API Tester",
|
|
96
|
+
source_system: "SPARK",
|
|
97
|
+
correlation_id: "",
|
|
98
|
+
requested_output: null,
|
|
99
|
+
service_category: "",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
const payload = {
|
|
103
|
+
body,
|
|
104
|
+
headers,
|
|
105
|
+
params,
|
|
106
|
+
};
|
|
107
|
+
const results = await handleDirectRequest(payload);
|
|
108
|
+
const response = XcallResBuilder.Json(
|
|
109
|
+
CALL_TYPE[0],
|
|
110
|
+
serviceName,
|
|
111
|
+
StatusCodes.ACCEPTED,
|
|
112
|
+
"",
|
|
113
|
+
0,
|
|
114
|
+
results
|
|
115
|
+
);
|
|
116
|
+
return response;
|
|
117
|
+
},
|
|
118
|
+
};
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
const { StatusCodes } = require("http-status-codes");
|
|
2
|
+
const { logger } = require("@coherentglobal/node-sdk");
|
|
3
|
+
// const config = require("../config"); env vars
|
|
4
|
+
const {
|
|
5
|
+
handleDirectRequest,
|
|
6
|
+
modelServiceValidation,
|
|
7
|
+
getLatestVersionFromServiceMapTable,
|
|
8
|
+
} = require("../logic/runnerLogic");
|
|
9
|
+
const { getModelViaFolder } = require("../services/entityStore");
|
|
10
|
+
const tenant = "test";
|
|
11
|
+
|
|
12
|
+
const CALL_TYPE = {
|
|
13
|
+
0: "SparkService",
|
|
14
|
+
1: "ExternalApi",
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const ERROR_CODES = {
|
|
18
|
+
A1: {
|
|
19
|
+
code: "a1",
|
|
20
|
+
message: "ERROR:[Main item] missing in Xcall [NAME] table.",
|
|
21
|
+
},
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const XcallResBuilder = {
|
|
25
|
+
Json: function (callType, name, status, errorCode, responseTime, data) {
|
|
26
|
+
return JSON.stringify({
|
|
27
|
+
metadata: {
|
|
28
|
+
calltype: callType,
|
|
29
|
+
name: name,
|
|
30
|
+
status: status,
|
|
31
|
+
error_code: errorCode,
|
|
32
|
+
response_time: responseTime,
|
|
33
|
+
},
|
|
34
|
+
data: data || {},
|
|
35
|
+
});
|
|
36
|
+
},
|
|
37
|
+
XML: function (callType, name, status, errorCode, responseTime, data) {
|
|
38
|
+
const trimmedData = data.replace(/\<\?xml.+\?\>/gm, "");
|
|
39
|
+
|
|
40
|
+
const response = `
|
|
41
|
+
<data calltype="${callType}" name="${name}" status="${status}" error_code="${errorCode}" response_time = "${responseTime}">
|
|
42
|
+
${trimmedData}
|
|
43
|
+
</data>
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
return response;
|
|
47
|
+
},
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
module.exports = {
|
|
51
|
+
sparkService: async (requestData, context) => {
|
|
52
|
+
const modelLocation = config().modelLocation;
|
|
53
|
+
const headers = context.headers;
|
|
54
|
+
const folderName = requestData.folder_name;
|
|
55
|
+
const serviceName = requestData.service_name;
|
|
56
|
+
|
|
57
|
+
logger.info({
|
|
58
|
+
eventType: "SparkService",
|
|
59
|
+
msg: `WASMSERVER:DISPATCH_TYPE.SPARK_SERVICE ${tenant}`,
|
|
60
|
+
});
|
|
61
|
+
logger.info({ eventType: "RequestData", msg: requestData });
|
|
62
|
+
const requestBodyFromEngine = JSON.parse(requestData.request_body);
|
|
63
|
+
const requestMetaFromEngine = requestBodyFromEngine.request_meta;
|
|
64
|
+
const findAvailableLatestModelFromLocal =
|
|
65
|
+
getLatestVersionFromServiceMapTable(
|
|
66
|
+
context.servicemap,
|
|
67
|
+
headers.tenant,
|
|
68
|
+
decodeURI(folderName),
|
|
69
|
+
decodeURI(serviceName)
|
|
70
|
+
);
|
|
71
|
+
requestMetaFromEngine.version_id = findAvailableLatestModelFromLocal;
|
|
72
|
+
const serviceMap = modelServiceValidation(
|
|
73
|
+
context.servicemap,
|
|
74
|
+
headers.tenant,
|
|
75
|
+
requestMetaFromEngine
|
|
76
|
+
);
|
|
77
|
+
// const modelVersion = modelLocation
|
|
78
|
+
// ? // ? await resolveOffline(requestMetaFromEngine, headers.tenant, headers, serviceMap)
|
|
79
|
+
// findAvailableLatestModelFromLocal
|
|
80
|
+
// : await getModelViaFolder(folderName, serviceName, headers);
|
|
81
|
+
|
|
82
|
+
const modelVersion = await getModelViaFolder(
|
|
83
|
+
folderName,
|
|
84
|
+
serviceName,
|
|
85
|
+
headers
|
|
86
|
+
);
|
|
87
|
+
const params = {
|
|
88
|
+
tenant: headers.tenant,
|
|
89
|
+
};
|
|
90
|
+
const body = {
|
|
91
|
+
...requestBodyFromEngine,
|
|
92
|
+
request_meta: {
|
|
93
|
+
...requestBodyFromEngine.request_meta,
|
|
94
|
+
version_id: modelVersion,
|
|
95
|
+
call_purpose: "Spark - API Tester",
|
|
96
|
+
source_system: "SPARK",
|
|
97
|
+
correlation_id: "",
|
|
98
|
+
requested_output: null,
|
|
99
|
+
service_category: "",
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
const payload = {
|
|
103
|
+
body,
|
|
104
|
+
headers,
|
|
105
|
+
params,
|
|
106
|
+
};
|
|
107
|
+
const results = await handleDirectRequest(payload);
|
|
108
|
+
const response = XcallResBuilder.Json(
|
|
109
|
+
CALL_TYPE[0],
|
|
110
|
+
serviceName,
|
|
111
|
+
StatusCodes.ACCEPTED,
|
|
112
|
+
"",
|
|
113
|
+
0,
|
|
114
|
+
results
|
|
115
|
+
);
|
|
116
|
+
return response;
|
|
117
|
+
},
|
|
118
|
+
};
|
package/src/validate.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
const Joi = require("joi");
|
|
2
|
+
|
|
3
|
+
const rules = Joi.object({
|
|
4
|
+
sparkEndpoint: Joi.object({
|
|
5
|
+
url: Joi.string()
|
|
6
|
+
.uri({
|
|
7
|
+
scheme: ["http", "https"],
|
|
8
|
+
})
|
|
9
|
+
.required(),
|
|
10
|
+
tenant: Joi.string().alphanum().required(),
|
|
11
|
+
authType: Joi.string()
|
|
12
|
+
.valid("public", "syntheticKey", "bearerToken")
|
|
13
|
+
.required(),
|
|
14
|
+
syntheticKey: [Joi.string().allow(""), Joi.function().allow("")], // allow fns
|
|
15
|
+
bearerToken: [Joi.string().allow(""), Joi.function().allow("")], // allow strings/fns
|
|
16
|
+
})
|
|
17
|
+
.and("url", "tenant", "authType")
|
|
18
|
+
.required(),
|
|
19
|
+
nodeGenModels: Joi.array()
|
|
20
|
+
.items(
|
|
21
|
+
Joi.object({
|
|
22
|
+
versionId: Joi.string(),
|
|
23
|
+
type: Joi.string(),
|
|
24
|
+
binary: Joi.string(),
|
|
25
|
+
metaData: Joi.object(),
|
|
26
|
+
})
|
|
27
|
+
)
|
|
28
|
+
.required(),
|
|
29
|
+
// xParameters: Joi.array().items(),
|
|
30
|
+
// xCallCallback: Joi.func()
|
|
31
|
+
}).required();
|
|
32
|
+
|
|
33
|
+
const validate = (config) => {
|
|
34
|
+
const isValidConfig = rules.validate(config);
|
|
35
|
+
return isValidConfig;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
module.exports = validate;
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const { expect } = require("chai");
|
|
2
|
+
const validate = require("../src/validate.js");
|
|
3
|
+
|
|
4
|
+
describe("config", () => {
|
|
5
|
+
it("invalidate empty config", () => {
|
|
6
|
+
const fixture = {};
|
|
7
|
+
const checkIfValid = validate(fixture);
|
|
8
|
+
const errorName = checkIfValid.error.name;
|
|
9
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
10
|
+
expect(errorName).to.equal("ValidationError");
|
|
11
|
+
expect(errorType).to.equal("any.required");
|
|
12
|
+
});
|
|
13
|
+
it('empty url not allowed', () => {
|
|
14
|
+
const fixture = {
|
|
15
|
+
sparkEndpoint: {
|
|
16
|
+
url: "",
|
|
17
|
+
tenant: "",
|
|
18
|
+
authType: "",
|
|
19
|
+
syntheticKey: "",
|
|
20
|
+
bearerToken: ""
|
|
21
|
+
},
|
|
22
|
+
nodeGenModels: []
|
|
23
|
+
}
|
|
24
|
+
const checkIfValid = validate(fixture)
|
|
25
|
+
const errorName = checkIfValid.error.name;
|
|
26
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
27
|
+
const errorLabel = checkIfValid.error.details[0].context.label;
|
|
28
|
+
// console.log('ASD: ', JSON.stringify(checkIfValid))
|
|
29
|
+
expect(errorName).to.equal("ValidationError");
|
|
30
|
+
expect(errorType).to.equal("string.empty");
|
|
31
|
+
expect(errorLabel).to.equal("sparkEndpoint.url");
|
|
32
|
+
})
|
|
33
|
+
it('invalid url', () => {
|
|
34
|
+
const fixture = {
|
|
35
|
+
sparkEndpoint: {
|
|
36
|
+
url: "http:",
|
|
37
|
+
tenant: "",
|
|
38
|
+
authType: "",
|
|
39
|
+
syntheticKey: "",
|
|
40
|
+
bearerToken: ""
|
|
41
|
+
},
|
|
42
|
+
nodeGenModels: []
|
|
43
|
+
}
|
|
44
|
+
const checkIfValid = validate(fixture)
|
|
45
|
+
const errorName = checkIfValid.error.name;
|
|
46
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
47
|
+
// console.log('ASD: ', JSON.stringify(checkIfValid))
|
|
48
|
+
expect(errorName).to.equal("ValidationError");
|
|
49
|
+
expect(errorType).to.equal("string.uriCustomScheme");
|
|
50
|
+
})
|
|
51
|
+
it('tenant not allowed to be emtpy', () => {
|
|
52
|
+
const fixture = {
|
|
53
|
+
sparkEndpoint: {
|
|
54
|
+
url: "https://spark.dev.coherent.global",
|
|
55
|
+
tenant: "",
|
|
56
|
+
authType: "",
|
|
57
|
+
syntheticKey: "",
|
|
58
|
+
bearerToken: ""
|
|
59
|
+
},
|
|
60
|
+
nodeGenModels: []
|
|
61
|
+
}
|
|
62
|
+
const checkIfValid = validate(fixture)
|
|
63
|
+
const errorName = checkIfValid.error.name;
|
|
64
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
65
|
+
const errorLabel = checkIfValid.error.details[0].context.label;
|
|
66
|
+
expect(errorName).to.equal("ValidationError");
|
|
67
|
+
expect(errorType).to.equal("string.empty");
|
|
68
|
+
expect(errorLabel).to.equal("sparkEndpoint.tenant");
|
|
69
|
+
})
|
|
70
|
+
it('authType not allowed to be emtpy', () => {
|
|
71
|
+
const fixture = {
|
|
72
|
+
sparkEndpoint: {
|
|
73
|
+
url: "https://spark.dev.coherent.global",
|
|
74
|
+
tenant: "coherent",
|
|
75
|
+
authType: "",
|
|
76
|
+
syntheticKey: "",
|
|
77
|
+
bearerToken: ""
|
|
78
|
+
},
|
|
79
|
+
nodeGenModels: []
|
|
80
|
+
}
|
|
81
|
+
const checkIfValid = validate(fixture)
|
|
82
|
+
const errorName = checkIfValid.error.name;
|
|
83
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
84
|
+
const errorLabel = checkIfValid.error.details[0].context.label;
|
|
85
|
+
expect(errorName).to.equal("ValidationError");
|
|
86
|
+
expect(errorType).to.equal("any.only");
|
|
87
|
+
expect(errorLabel).to.equal("sparkEndpoint.authType");
|
|
88
|
+
})
|
|
89
|
+
it('authType must use valid strings', () => {
|
|
90
|
+
const fixture = {
|
|
91
|
+
sparkEndpoint: {
|
|
92
|
+
url: "https://spark.dev.coherent.global",
|
|
93
|
+
tenant: "coherent",
|
|
94
|
+
authType: "publics",
|
|
95
|
+
syntheticKey: "",
|
|
96
|
+
bearerToken: ""
|
|
97
|
+
},
|
|
98
|
+
nodeGenModels: []
|
|
99
|
+
}
|
|
100
|
+
const checkIfValid = validate(fixture)
|
|
101
|
+
const errorName = checkIfValid.error.name;
|
|
102
|
+
const errorType = checkIfValid.error.details[0].type;
|
|
103
|
+
const errorLabel = checkIfValid.error.details[0].context.label;
|
|
104
|
+
expect(errorName).to.equal("ValidationError");
|
|
105
|
+
expect(errorType).to.equal("any.only");
|
|
106
|
+
expect(errorLabel).to.equal("sparkEndpoint.authType");
|
|
107
|
+
})
|
|
108
|
+
it('valid config', () => {
|
|
109
|
+
const fixture = {
|
|
110
|
+
sparkEndpoint: {
|
|
111
|
+
url: "https://spark.dev.coherent.global",
|
|
112
|
+
tenant: "coherent",
|
|
113
|
+
authType: "public",
|
|
114
|
+
syntheticKey: "",
|
|
115
|
+
bearerToken: ""
|
|
116
|
+
},
|
|
117
|
+
nodeGenModels: []
|
|
118
|
+
}
|
|
119
|
+
const checkIfValid = validate(fixture)
|
|
120
|
+
// console.log('ASD: ', JSON.stringify(checkIfValid))
|
|
121
|
+
const errorName = checkIfValid.error;
|
|
122
|
+
expect(errorName).to.equal(undefined);
|
|
123
|
+
})
|
|
124
|
+
});
|