UNPKG

@deliverr/serverless-offline-step-functions

Version:

Serverless Offline Plugin to Support Step Functions for Local Development

149 lines (148 loc) 7.09 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TaskExecutor = void 0; const fs_1 = require("fs"); const path_1 = __importDefault(require("path")); const StateTypeExecutor_1 = require("../StateTypeExecutor"); const StateInfoHandler_1 = require("../../StateInfoHandler"); const StateProcessor_1 = require("../../StateProcessor"); const Retriers_1 = require("../../types/Retriers"); const Catchers_1 = require("../../types/Catchers"); const StatesErrors_1 = require("../../types/StatesErrors"); class TaskExecutor extends StateTypeExecutor_1.StateTypeExecutor { async execute(context, stateDefinition, inputJson) { const statesInfoHandler = StateInfoHandler_1.StateInfoHandler.getInstance(); const stateInfo = statesInfoHandler.getStateInfo(context.StateMachine.Name, context.State.Name); if (!stateInfo) { throw new Error('Handler does not exists'); } const input = this.processInput(inputJson, stateDefinition, context); const lambdaPath = await this.getWebpackOrCommonFuction(stateInfo.handlerPath); const functionLambda = await Promise.resolve().then(() => __importStar(require(`${lambdaPath}`))); let exec_output, exec_error; this.envVarResolver.injectEnvVarsLambdaSpecific(stateInfo.environment); const callback = (err, res) => { exec_output = res; exec_error = err; }; try { if (stateDefinition.Retry) { const retries = Retriers_1.Retriers.create(stateDefinition.Retry); await retries.retry(() => functionLambda[stateInfo.handlerName](input, context, callback), context); } else { await functionLambda[stateInfo.handlerName](input, context, callback); console.log(`the result is:${exec_output}`); } if (exec_error) { throw exec_error; } } catch (error) { this.envVarResolver.removeEnvVarsLambdaSpecific(stateInfo.environment); return this.dealWithError(stateDefinition, error, input); } if (exec_output === undefined) { exec_output = {}; } this.envVarResolver.removeEnvVarsLambdaSpecific(stateInfo.environment); const outputJson = this.processOutput(JSON.parse(inputJson || '{}'), exec_output, stateDefinition); return { Next: stateDefinition.Next, End: stateDefinition.End, json: outputJson, }; } isWaitForTaskToken(resource) { if (resource && typeof resource === 'string' && resource.endsWith('.waitForTaskToken')) { return true; } return false; } dealWithError(stateDefinition, error, input) { if (!stateDefinition.Catch) { throw error; } this.logger.error(`Caught an error in Catcher: ${error.stack}`); const catchers = Catchers_1.Catchers.create(stateDefinition.Catch); const catcher = catchers.getCatcherBasedOn([StatesErrors_1.StatesErrors.TaskFailed, StatesErrors_1.StatesErrors.All]); if (!catcher) { throw error; } const output = { message: error.message, stack: error.stack }; const outputJson = StateProcessor_1.StateProcessor.processResultPath(input, output, catcher.ResultPath); this.logger.log(`Using Next state of Catcher: ${catcher.Next}`); return { Next: catcher.Next, End: stateDefinition.End, json: outputJson, }; } processInput(json, stateDefinition, context) { this.logger.debug(`TaskExecutor - processInput1 - ${json}`); this.logger.debug(JSON.stringify(stateDefinition)); const proccessedInputJson = StateProcessor_1.StateProcessor.processInputPath(json, stateDefinition.InputPath); this.logger.debug(`TaskExecutor - processInput2 - ${proccessedInputJson}`); let output = proccessedInputJson; if (stateDefinition.Parameters && typeof stateDefinition.Resource === 'string' && stateDefinition.Resource.endsWith('.waitForTaskToken')) { output = StateProcessor_1.StateProcessor.processWaitForTokenParameters(proccessedInputJson, stateDefinition.Parameters, context); } else { output = StateProcessor_1.StateProcessor.processParameters(proccessedInputJson, stateDefinition.Parameters, context); } try { return JSON.parse(output); } catch (error) { this.logger.error(`TaskExecutor.processInput: Could not parse JSON for state ${context.State.Name}: "${output}"`); throw error; } } processOutput(input, output, stateDefinition) { this.logger.debug(`TaskExecutor - processOutput1 - ${JSON.stringify(output)}`); let outputJson = output ? JSON.stringify(output) : '{}'; outputJson = StateProcessor_1.StateProcessor.processResultSelector(outputJson, stateDefinition.ResultSelector); outputJson = StateProcessor_1.StateProcessor.processResultPath(input, output, stateDefinition.ResultPath); this.logger.debug(`TaskExecutor - processOutput2 - ${outputJson}`); outputJson = StateProcessor_1.StateProcessor.processOutputPath(outputJson, stateDefinition.OutputPath); this.logger.debug(`TaskExecutor - processOutput3 - ${outputJson}`); return outputJson; } async getWebpackOrCommonFuction(lambdaFilePath) { const webpackPath = path_1.default.resolve(process.cwd(), `./.webpack/service/${lambdaFilePath}.js`); let filePathResolved; try { await fs_1.promises.access(webpackPath, fs_1.constants.F_OK | fs_1.constants.R_OK); filePathResolved = webpackPath; } catch (error) { filePathResolved = path_1.default.resolve(process.cwd(), `./.build/${lambdaFilePath}.js`); } return filePathResolved; } } exports.TaskExecutor = TaskExecutor;