UNPKG

3.34 kBJavaScriptView Raw
1"use strict";
2var __importDefault = (this && this.__importDefault) || function (mod) {
3 return (mod && mod.__esModule) ? mod : { "default": mod };
4};
5Object.defineProperty(exports, "__esModule", { value: true });
6exports.registerApis = exports.getClient = exports.getAssetPath = exports.getOpConfig = void 0;
7const fs_1 = __importDefault(require("fs"));
8const path_1 = __importDefault(require("path"));
9const utils_1 = require("@terascope/utils");
10const execution_context_1 = require("./execution-context");
11/** Get the first opConfig from an operation name */
12function getOpConfig(job, name) {
13 return job.operations.find((op) => op._op === name);
14}
15exports.getOpConfig = getOpConfig;
16/* Get the asset path from a asset name or ID */
17async 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}
40exports.getAssetPath = getAssetPath;
41/*
42 * This will request a connection based on the 'connection' attribute of
43 * an opConfig. Intended as a context API endpoint.
44 */
45function 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}
62exports.getClient = getClient;
63function 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}
88exports.registerApis = registerApis;
89//# sourceMappingURL=register-apis.js.map
\No newline at end of file