UNPKG

@deliverr/serverless-offline-step-functions

Version:

Serverless Offline Plugin to Support Step Functions for Local Development

67 lines (66 loc) 3.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StateMachineExecutor = exports.StateMachineExecutorError = void 0; const StateTypeExecutorFactory_1 = require("./stateTasks/StateTypeExecutorFactory"); const Logger_1 = require("./utils/Logger"); const StateContext_1 = require("./Context/StateContext"); const ContextToJson_1 = require("./Context/ContextToJson"); const isJsonByteLengthValid_1 = require("./utils/isJsonByteLengthValid"); class StateMachineExecutorError { constructor(error) { this.error = error; } } exports.StateMachineExecutorError = StateMachineExecutorError; class StateMachineExecutor { constructor(stateMachine, context) { this.context = context; this.stateMachine = stateMachine; this.logger = Logger_1.Logger.getInstance(); } async execute(stateDefinition, inputJson) { this.logger.log(`* * * * * ${this.context.State.Name} * * * * *`); this.logger.log(`input: \n${inputJson ? JSON.stringify(JSON.parse(inputJson), null, 2) : 'undefined'}\n`); this.logger.log(`context: \n${JSON.stringify(ContextToJson_1.ContextToJson(this.context), null, 2)}\n`); const typeExecutor = StateTypeExecutorFactory_1.StateTypeExecutorFactory.getExecutor(stateDefinition.Type); let stateExecutorOutput; try { stateExecutorOutput = await typeExecutor.execute(this.context, stateDefinition, inputJson); this.logger.debug(`StateMachineExecutor - execute1 - ${JSON.stringify(stateExecutorOutput)}`); if (!isJsonByteLengthValid_1.isJsonByteLengthValid(stateExecutorOutput.json)) { this.logger.error(`The state/task '${this.context.State.Name}' returned a result with a size exceeding the maximum number of bytes service limit.`); throw new Error('Return result must have size less than or equal to 262144 bytes in UTF-8 encoding'); } if (stateExecutorOutput.End) { this.logger.log(`[${this.context.State.Name}] State Machine Ended`); return stateExecutorOutput.json; } if (!stateExecutorOutput.Next) { this.logger.error(`[${this.context.State.Name}] Should Have ended`); throw new Error('Should have ended'); } const nextState = StateContext_1.StateContext.create(stateExecutorOutput.Next); const executeNextState = (waitForTaskTokenOutput) => { this.context.transitionTo(nextState); let output = stateExecutorOutput.json; if (waitForTaskTokenOutput) { output = typeExecutor.processOutput(JSON.parse(inputJson || '{}'), waitForTaskTokenOutput, stateDefinition); } return this.execute(this.stateMachine.definition.States[nextState.Name], output); }; this.logger.log(`Output: \n${JSON.stringify(JSON.parse(stateExecutorOutput.json), null, 2)}\n`); if (typeExecutor.isWaitForTaskToken(stateDefinition.Resource)) { this.logger.log(`Step function execution paused. \n Waiting for success or failure with task token "${this.context.Task.Token}"\n`); return executeNextState.bind(this); } else { return executeNextState(); } } catch (error) { this.logger.error(error.stack); return new StateMachineExecutorError(error); } } } exports.StateMachineExecutor = StateMachineExecutor;