UNPKG

zcatalyst-cli

Version:

Command Line Tool for CATALYST

161 lines (160 loc) 8.94 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.Caller = void 0; const js_1 = require("../util_modules/js"); const constants_1 = require("../util_modules/constants"); const error_1 = __importDefault(require("../error")); const path_1 = require("path"); const runtime_store_1 = __importDefault(require("../runtime-store")); const index_1 = require("../util_modules/logger/index"); const credential_1 = __importDefault(require("../authentication/credential")); const fs_1 = require("../util_modules/fs"); const shell_1 = require("../util_modules/shell"); const port_resolver_1 = __importDefault(require("../port-resolver")); const project_1 = require("../util_modules/project"); const container_1 = require("../util_modules/container"); const fn_execution_handler_1 = require("../shell/dependencies/non-http-function/fn-execution-handler"); class Caller extends fn_execution_handler_1.FnExecutionHandler { constructor(serverDetails, localFnEvents) { var _a, _b; super(serverDetails, localFnEvents); this.isContainer = (0, container_1.isContainer)(); this.responseFile = (0, path_1.join)(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_res_body'); this.metaFile = (0, path_1.join)(runtime_store_1.default.get('cwd'), '.build', '.catalyst', 'user_meta.json'); const projectRoot = (0, project_1.getProjectRoot)(); this.slaveFnTarget = { index: ((_b = (_a = this.fn.target) === null || _a === void 0 ? void 0 : _a.index) === null || _b === void 0 ? void 0 : _b.replace((0, path_1.join)(projectRoot, constants_1.FOLDERNAME.functions, this.fn.target.name) + path_1.sep, '')) || '', name: this.fn.target.name }; } call(data) { var _a, _b, _c, _d, _e, _f, _g, _h; return __awaiter(this, void 0, void 0, function* () { if (typeof data !== 'object') { throw new error_1.default('Data is not a valid JSON object', { exit: 1 }); } if (this.fn.target.type === constants_1.FN_TYPE.event) { if (!('event_bus_details' in data)) { if (!('events' in data)) { (0, index_1.info)('For event function kindly use signals:generate command to generate the input json.'); (0, index_1.info)('You can also provide event details as per documentation.'); throw new error_1.default('Event details missing', { exit: 1, skipHelp: true }); } } else { (0, index_1.warning)('Since the event:generate command is being deprecated and being sunsetted in future releases. Please use signals:generate command here after to get the event details.'); } } if (this.fn.target.type === constants_1.FN_TYPE.cron) { data = { data }; } const accessToken = yield credential_1.default.getAccessToken(); yield fs_1.ASYNC.ensureFile(this.responseFile, true); yield fs_1.ASYNC.ensureFile(this.metaFile, true); if (this.fn.target.type === 'job') { const templateFile = (yield fs_1.ASYNC.readJSONFile(constants_1.TEMPLATE.job_data)) || {}; const inputKeys = Object.keys(data); if (inputKeys.length === 1 && inputKeys.at(0) === 'params') { data = { job_details: { job_meta_details: Object.assign({}, data) } }; } const _input = js_1.JS.merge(templateFile, data); const jobMetaDetails = { headers: Object.assign(Object.assign({}, (_b = (_a = _input.job_details) === null || _a === void 0 ? void 0 : _a.job_meta_details) === null || _b === void 0 ? void 0 : _b.headers), { 'x-zc-projectid': (0, project_1.getProjectId)(), 'x-zc-project-domain': (0, project_1.getDomainPrefix)() + '.' + constants_1.ORIGIN.app.replace('https://', ''), 'x-zc-project-key': (0, project_1.getDomainKey)(), 'x-zc-environment': (0, project_1.getEnvName)(), 'x-zc-user-cred-type': 'token', 'x-zc-user-cred-token': accessToken, 'x-zc-admin-cred-type': 'token', 'x-zc-admin-cred-token': accessToken, 'x-zc-user-type': 'admin' }), params: ((_d = (_c = _input === null || _input === void 0 ? void 0 : _input.job_details) === null || _c === void 0 ? void 0 : _c.job_meta_details) === null || _d === void 0 ? void 0 : _d.params) || {}, jobpool_details: Object.assign({ type: 'Function', project_details: { project_name: (0, project_1.getProjectName)(), id: (0, project_1.getProjectId)() } }, (_f = (_e = _input === null || _input === void 0 ? void 0 : _input.job_details) === null || _e === void 0 ? void 0 : _e.job_meta_details) === null || _f === void 0 ? void 0 : _f.jobpool_details) }; _input.job_details = Object.assign(Object.assign({}, _input.job_details), { capacity: _input.capacity || { memory: '256' }, job_meta_details: jobMetaDetails }); _input.capacity = _input.capacity || { memory: '256' }; data = _input; } if ((0, container_1.isContainer)()) { yield this.executeWithContainer(data, accessToken); } else { yield this.execute(data, accessToken); } if (!this.slave) { throw new error_1.default('Slave listening started before initializing', { exit: 2 }); } const fnSigintHandler = () => { this.kill(); }; process.once('SIGINT', fnSigintHandler); (_g = this.slave.stdout) === null || _g === void 0 ? void 0 : _g.on('data', (message) => { (0, shell_1.clearLine)(process.stdout); (0, index_1.info)(message.toString()); }); (_h = this.slave.stderr) === null || _h === void 0 ? void 0 : _h.on('data', (message) => { (0, shell_1.clearLine)(process.stdout); (0, index_1.info)(message.toString()); }); this.slave.on('error', (err) => { this.localFnEvents.listenerCount('error') > 0 && this.localFnEvents.emit('error', err); }); return new Promise((resolve, reject) => { if (!this.slave) { return reject(new error_1.default('Invalid slave')); } this.slave.on('exit', (code, sig) => __awaiter(this, void 0, void 0, function* () { (0, index_1.debug)(`caller fn slave exit: ${code}, ${sig}`); if (this.isContainer && code === 143) { code = 0; } yield this.fnResponseHandler(code, sig); if (this.fn.debugPort !== -1) { port_resolver_1.default.freePort(this.fn.debugPort); } code ? reject(code) : resolve(); process.removeListener('SIGINT', fnSigintHandler); })); }); }); } kill() { const _super = Object.create(null, { kill: { get: () => super.kill } }); return __awaiter(this, void 0, void 0, function* () { (0, index_1.info)(); (0, index_1.info)('Terminating Function'); _super.kill.call(this); }); } } exports.Caller = Caller;