1 | "use strict";
|
2 | var __importDefault = (this && this.__importDefault) || function (mod) {
|
3 | return (mod && mod.__esModule) ? mod : { "default": mod };
|
4 | };
|
5 | Object.defineProperty(exports, "__esModule", { value: true });
|
6 | exports.registerApis = exports.getClient = exports.getAssetPath = exports.getOpConfig = void 0;
|
7 | const fs_1 = __importDefault(require("fs"));
|
8 | const path_1 = __importDefault(require("path"));
|
9 | const utils_1 = require("@terascope/utils");
|
10 | const execution_context_1 = require("./execution-context");
|
11 |
|
12 | function getOpConfig(job, name) {
|
13 | return job.operations.find((op) => op._op === name);
|
14 | }
|
15 | exports.getOpConfig = getOpConfig;
|
16 |
|
17 | async function getAssetPath(assetDir, assets, name) {
|
18 | if (!assetDir) {
|
19 | throw new Error('No asset_directroy has been configured, cannot get asset path');
|
20 | }
|
21 | const assetIds = assets || [];
|
22 | if (!name) {
|
23 | throw new Error('Invalid asset name');
|
24 | }
|
25 | if (name.length === 40) {
|
26 | const assetPath = path_1.default.join(assetDir, name);
|
27 | if (fs_1.default.existsSync(assetDir))
|
28 | return assetPath;
|
29 | }
|
30 | const [assetName] = name.split(':').map((s) => s.trim());
|
31 | for (const id of assetIds) {
|
32 | const rawAssetJSON = fs_1.default.readFileSync(path_1.default.join(assetDir, id, 'asset.json'));
|
33 | const assetJSON = utils_1.parseJSON(rawAssetJSON);
|
34 | if (assetJSON.name === assetName) {
|
35 | return path_1.default.join(assetDir, id);
|
36 | }
|
37 | }
|
38 | throw new Error(`Unable to find asset "${name}"`);
|
39 | }
|
40 | exports.getAssetPath = getAssetPath;
|
41 |
|
42 |
|
43 |
|
44 |
|
45 | function getClient(context, config, type) {
|
46 | const clientConfig = {
|
47 | type,
|
48 | cached: true,
|
49 | endpoint: 'default',
|
50 | };
|
51 | if (config && config.connection) {
|
52 | clientConfig.endpoint = config.connection || 'default';
|
53 | const isCached = config.connection_cache != null;
|
54 | clientConfig.cached = isCached ? config.connection_cache : true;
|
55 | }
|
56 | else {
|
57 | clientConfig.endpoint = 'default';
|
58 | clientConfig.cached = true;
|
59 | }
|
60 | return context.foundation.getConnection(clientConfig).client;
|
61 | }
|
62 | exports.getClient = getClient;
|
63 | function registerApis(context, job, assetIds) {
|
64 | const cleanupApis = ['op_runner', 'executionContext', 'job_runner', 'assets'];
|
65 | for (const api of cleanupApis) {
|
66 | if (context.apis[api] != null) {
|
67 | delete context.apis[api];
|
68 | }
|
69 | }
|
70 | context.apis.registerAPI('executionContext', new execution_context_1.ExecutionContextAPI(context, job));
|
71 | context.apis.registerAPI('op_runner', {
|
72 | getClient(config, type) {
|
73 | return getClient(context, config, type);
|
74 | },
|
75 | });
|
76 | context.apis.registerAPI('job_runner', {
|
77 | getOpConfig(name) {
|
78 | return getOpConfig(job, name);
|
79 | },
|
80 | });
|
81 | const assetDir = context.sysconfig.teraslice.assets_directory;
|
82 | context.apis.registerAPI('assets', {
|
83 | getPath(name) {
|
84 | return getAssetPath(assetDir || '', assetIds || job.assets, name);
|
85 | },
|
86 | });
|
87 | }
|
88 | exports.registerApis = registerApis;
|
89 |
|
\ | No newline at end of file |