UNPKG

3.72 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 });
6const fs_1 = __importDefault(require("fs"));
7const path_1 = __importDefault(require("path"));
8const utils_1 = require("@terascope/utils");
9const execution_context_1 = require("./execution-context");
10/** Get the first opConfig from an operation name */
11function getOpConfig(job, name) {
12 return job.operations.find((op) => op._op === name);
13}
14exports.getOpConfig = getOpConfig;
15/* Get the asset path from a asset name or ID */
16async function getAssetPath(assetDir, assets, name) {
17 if (!assetDir) {
18 throw new Error('No asset_directroy has been configured, cannot get asset path');
19 }
20 const assetIds = assets || [];
21 if (!name) {
22 throw new Error('Invalid asset name');
23 }
24 if (name.length === 40) {
25 const assetPath = path_1.default.join(assetDir, name);
26 if (fs_1.default.existsSync(assetDir))
27 return assetPath;
28 }
29 const [assetName] = name.split(':').map(s => s.trim());
30 for (const id of assetIds) {
31 const rawAssetJSON = fs_1.default.readFileSync(path_1.default.join(assetDir, id, 'asset.json'));
32 const assetJSON = utils_1.parseJSON(rawAssetJSON);
33 if (assetJSON.name === assetName) {
34 return path_1.default.join(assetDir, id);
35 }
36 }
37 throw new Error(`Unable to find asset "${name}"`);
38}
39exports.getAssetPath = getAssetPath;
40/*
41 * This will request a connection based on the 'connection' attribute of
42 * an opConfig. Intended as a context API endpoint.
43 * If there is an error getting the connection, it will not throw an error
44 * it will log it and emit `client:initialization:error`
45 */
46function getClient(context, config, type) {
47 const clientConfig = {
48 type,
49 cached: true,
50 endpoint: 'default',
51 };
52 const events = context.apis.foundation.getSystemEvents();
53 if (config && config.connection) {
54 clientConfig.endpoint = config.connection || 'default';
55 const isCached = config.connection_cache != null;
56 clientConfig.cached = isCached ? config.connection_cache : true;
57 }
58 else {
59 clientConfig.endpoint = 'default';
60 clientConfig.cached = true;
61 }
62 try {
63 return context.foundation.getConnection(clientConfig).client;
64 }
65 catch (err) {
66 const message = `No configuration for endpoint ${clientConfig.endpoint} was found in the terafoundation connectors config`;
67 context.logger.error(err, message);
68 events.emit('client:initialization:error', {
69 error: message,
70 stack: err,
71 });
72 }
73}
74exports.getClient = getClient;
75function registerApis(context, job, assetIds) {
76 if (context.apis.executionContext == null) {
77 context.apis.registerAPI('executionContext', new execution_context_1.ExecutionContextAPI(context, job));
78 }
79 delete context.apis.op_runner;
80 context.apis.registerAPI('op_runner', {
81 getClient(config, type) {
82 return getClient(context, config, type);
83 },
84 });
85 delete context.apis.job_runner;
86 context.apis.registerAPI('job_runner', {
87 getOpConfig(name) {
88 return getOpConfig(job, name);
89 },
90 });
91 const assetDir = context.sysconfig.teraslice.assets_directory;
92 delete context.apis.assets;
93 context.apis.registerAPI('assets', {
94 getPath(name) {
95 return getAssetPath(assetDir || '', assetIds || job.assets, name);
96 },
97 });
98}
99exports.registerApis = registerApis;
100//# sourceMappingURL=register-apis.js.map
\No newline at end of file