@deliverr/serverless-offline-step-functions
Version:
Serverless Offline Plugin to Support Step Functions for Local Development
90 lines (89 loc) • 3.58 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WaitExecutor = void 0;
const delay_1 = __importDefault(require("delay"));
const jsonpath_plus_1 = require("jsonpath-plus");
const StateProcessor_1 = require("../../StateProcessor");
const validateTimestamp_1 = require("../../utils/validateTimestamp");
const StateTypeExecutor_1 = require("../StateTypeExecutor");
class WaitExecutor extends StateTypeExecutor_1.StateTypeExecutor {
async execute(context, definition, inputJson) {
const input = this.processInput(inputJson, definition);
let secondstoPrint;
let datetoPrint;
let millisecondsToWait;
if (definition.Seconds) {
secondstoPrint = definition.Seconds;
millisecondsToWait = this.convertSecondsToMillis(definition.Seconds);
}
else if (definition.Timestamp) {
validateTimestamp_1.validateTimestamp(definition.Timestamp);
datetoPrint = definition.Timestamp;
millisecondsToWait = this.convertTimestamptoMillis(definition.Timestamp);
}
else if (definition.SecondsPath) {
const seconds = this.getSecondsFromPath(input, definition.SecondsPath);
secondstoPrint = seconds;
millisecondsToWait = this.convertSecondsToMillis(seconds);
}
else if (definition.TimestampPath) {
const timestamp = this.getTimestampFromPath(input, definition.TimestampPath);
validateTimestamp_1.validateTimestamp(timestamp);
datetoPrint = timestamp;
millisecondsToWait = this.convertTimestamptoMillis(timestamp);
}
if (!millisecondsToWait || isNaN(millisecondsToWait)) {
throw new Error('');
}
if (datetoPrint) {
this.logger.log(`* * Will Wait Until ${datetoPrint} * *`);
}
else if (secondstoPrint) {
this.logger.log(`* * Will Wait ${secondstoPrint} Seconds * *`);
}
await delay_1.default(millisecondsToWait);
return {
Next: definition.Next,
End: definition.End,
json: this.processOutput(input, definition),
};
}
processInput(json, stateDefinition) {
const proccessedInputJson = StateProcessor_1.StateProcessor.processInputPath(json, stateDefinition.InputPath);
return proccessedInputJson;
}
processOutput(outputJson, stateDefinition) {
const proccessedOutputJson = StateProcessor_1.StateProcessor.processOutputPath(outputJson, stateDefinition.OutputPath);
return proccessedOutputJson;
}
convertSecondsToMillis(seconds) {
return seconds * 1000;
}
convertTimestamptoMillis(timestamp) {
return new Date(timestamp).getTime() - Date.now();
}
getSecondsFromPath(json, secondsPath) {
const result = jsonpath_plus_1.JSONPath({
path: secondsPath,
json: JSON.parse(json),
});
if (!result || result.length === 0) {
throw new Error();
}
return result[0];
}
getTimestampFromPath(json, timestampPath) {
const result = jsonpath_plus_1.JSONPath({
path: timestampPath,
json: JSON.parse(json),
});
if (!result || result.length === 0) {
throw new Error();
}
return result[0];
}
}
exports.WaitExecutor = WaitExecutor;