forked from vkkis93/serverless-step-functions-offline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.js
300 lines (280 loc) · 11.8 KB
/
build.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
'use strict';
const path = require('path');
const moment = require('moment');
const _ = require('lodash');
const Promise = require('bluebird');
const enumList = require('./enum');
module.exports = {
// findFunctionsPathAndHandler() {
// for (const functionName in this.variables) {
// const functionHandler = this.variables[functionName];
// const {handler, filePath} = this._findFunctionPathAndHandler(functionHandler);
//
// this.variables[functionName] = {handler, filePath};
// }
// console.log('this.va', this.variables)
// },
//
_findFunctionPathAndHandler(functionHandler) {
const dir = path.dirname(functionHandler);
const handler = path.basename(functionHandler);
const splitHandler = handler.split('.');
const filePath = `${dir}/${splitHandler[0]}.js`;
const handlerName = `${splitHandler[1]}`;
return {handler: handlerName, filePath};
},
buildStepWorkFlow() {
this.cliLog('Building StepWorkFlow');
this.contextObject = this.createContextObject();
this.states = this.stateDefinition.States;
return Promise.resolve()
.then(() => this.process(this.states[this.stateDefinition.StartAt], this.stateDefinition.StartAt, this.eventFile))
.catch(err => {
// console.log('OOPS', err.stack);
// this.cliLog(err);
throw err;
});
},
process(state, stateName, event) {
if (state && state.Type === 'Parallel') {
this.eventForParallelExecution = event;
}
const data = this._findStep(state, stateName);
// if (data instanceof Promise) return Promise.resolve();
if (!data || data instanceof Promise) {
if (!state || state.Type !== 'Parallel') {
this.cliLog('Serverless step function offline: Finished');
}
return Promise.resolve();
}
if (data.choice) {
return this._runChoice(data, event);
} else {
return this._run(data.f(event), event);
}
},
_findStep(currentState, currentStateName) {
// it means end of states
if (!currentState) {
this.currentState = null;
this.currentStateName = null;
return;
}
this.currentState = currentState;
this.currentStateName = currentStateName;
return this._states(currentState, currentStateName);
},
_run(f, event) {
if (!f) {
return;
}// end of states
this.executionLog(`~~~~~~~~~~~~~~~~~~~~~~~~~~~ ${this.currentStateName} started ~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
f(event, this.contextObject, this.contextObject.done);
},
_states(currentState, currentStateName) {
switch (currentState.Type) {
case 'Task': // just push task to general array
//before each task restore global default env variables
process.env = Object.assign({}, this.environmentVariables);
let f = this.variables[currentStateName];
f = this.functions[f];
if (!f) {
this.cliLog(`Function "${currentStateName}" does not presented in serverless manifest`);
process.exit(1);
}
const {handler, filePath} = this._findFunctionPathAndHandler(f.handler);
// if function has additional variables - attach it to function
if (f.environment) {
process.env = _.extend(process.env, f.environment);
}
return {
name: currentStateName,
f: () => require(path.join(this.location, filePath))[handler]
};
case 'Parallel': // look through branches and push all of them
this.eventParallelResult = [];
_.forEach(currentState.Branches, (branch) => {
this.parallelBranch = branch;
return this.process(branch.States[branch.StartAt], branch.StartAt, this.eventForParallelExecution);
});
this.process(this.states[currentState.Next], currentState.Next, this.eventParallelResult);
delete this.parallelBranch;
delete this.eventParallelResult;
return;
case 'Choice':
//push all choices. but need to store information like
// 1) on which variable need to look: ${variable}
// 2) find operator: ${condition}
// 3) find function which will check data: ${checkFunction}
// 4) value which we will use in order to compare data: ${compareWithValue}
// 5) find target function - will be used if condition true: ${f}
const choiceConditional = {choice: []};
_.forEach(currentState.Choices, (choice) => {
const variable = choice.Variable.split('$.')[1];
const condition = _.pick(choice, enumList.supportedComparisonOperator);
if (!condition) {
this.cliLog(`Sorry! At this moment we don't support operator '${operator}'`);
process.exit(1);
}
const operator = Object.keys(condition)[0];
const checkFunction = enumList.convertOperator[operator];
const compareWithValue = condition[operator];
const choiceObj = {
variable,
condition,
checkFunction,
compareWithValue
};
choiceObj.choiceFunction = choice.Next;
choiceConditional.choice.push(choiceObj);
});
// if exists default function - store it
if (currentState.Default) {
choiceConditional.defaultFunction = currentState.Default;
}
return choiceConditional;
case 'Wait':
// Wait State
// works with parameter: seconds, timestamp, timestampPath, secondsPath;
return {
waitState: true,
f: (event) => {
const waitTimer = this._waitState(event, currentState, currentStateName);
this.cliLog(`Wait function ${currentStateName} - please wait ${waitTimer} seconds`);
return (arg1, arg2, cb) => {
setTimeout(() => {
cb(null, event);
}, waitTimer * 1000);
};
}
};
case 'Pass':
return {
f: (event) => {
return (arg1, arg2, cb) => {
this.cliLog('!!! Pass State !!!');
const eventResult = this._passStateFields(currentState, event);
cb(null, eventResult);
};
}
};
case 'Succeed':
this.cliLog('Succeed');
return Promise.resolve('Succeed');
case 'Fail':
const obj = {};
if (currentState.Cause) obj.Cause = currentState.Cause;
if (currentState.Error) obj.Error = currentState.Error;
this.cliLog('Fail');
if (!_.isEmpty(obj)) {
this.cliLog(JSON.stringify(obj));
}
return Promise.resolve('Fail');
}
return;
},
_passStateFields(currentState, event) {
if (!currentState.ResultPath) {
return currentState.Result || event;
} else {
const variableName = currentState.ResultPath.split('$.')[1];
if (!currentState.Result) {
event[variableName] = event;
return event;
}
event[variableName] = currentState.Result;
return event;
}
},
_runChoice(data, result) {
let existsAnyMatches = false;
//look through choice and find appropriate
_.forEach(data.choice, choice => {
//check if result from previous function has of value which described in Choice
const functionResultValue = _.get(result, choice.variable);
if (!_.isNil(functionResultValue)) {
//check condition
const isConditionTrue = choice.checkFunction(functionResultValue, choice.compareWithValue);
if (isConditionTrue) {
existsAnyMatches = true;
return this.process(this.states[choice.choiceFunction], choice.choiceFunction, result);
}
}
});
if (!existsAnyMatches && data.defaultFunction) {
const fName = data.defaultFunction;
return this.process(this.states[fName], fName, result);
}
},
_waitState(event, currentState, currentStateName) {
let waitTimer = 0, targetTime, timeDiff;
const currentTime = moment();
const waitListKeys = ['Seconds', 'Timestamp', 'TimestampPath', 'SecondsPath'];
const waitField = _.omit(currentState, 'Type', 'Next', 'Result');
const waitKey = Object.keys(waitField)[0];
if (!waitListKeys.includes(waitKey)) {
const error = `Plugin does not support wait operator "${waitKey}"`;
throw new this.serverless.classes.Error(error);
}
switch (Object.keys(waitField)[0]) {
case 'Seconds':
waitTimer = waitField['Seconds'];
break;
case 'Timestamp':
targetTime = moment(waitField['Timestamp']);
timeDiff = targetTime.diff(currentTime, 'seconds');
if (timeDiff > 0) waitTimer = timeDiff;
break;
case 'TimestampPath':
const timestampPath = waitField['TimestampPath'].split('$.')[1];
if (!event[timestampPath]) {
const error =
`An error occurred while executing the state ${currentStateName}.
The TimestampPath parameter does not reference an input value: ${waitField['TimestampPath']}`;
throw new this.serverless.classes.Error(error);
}
targetTime = moment(event[timestampPath]);
timeDiff = targetTime.diff(currentTime, 'seconds');
if (timeDiff > 0) waitTimer = timeDiff;
break;
case 'SecondsPath':
const secondsPath = waitField['SecondsPath'].split('$.')[1];
const waitSeconds = event[secondsPath];
if (!waitSeconds) {
const error = `
An error occurred while executing the state ${currentStateName}.
The TimestampPath parameter does not reference an input value: ${waitField['SecondsPath']}`;
throw new this.serverless.classes.Error(error);
}
waitTimer = waitSeconds;
break;
}
return waitTimer;
},
createContextObject() {
const cb = (err, result) => {
// return new Promise((resolve, reject) => {
if (err) {
throw `Error in function "${this.currentStateName}": ${JSON.stringify(err)}`;
}
this.executionLog(`~~~~~~~~~~~~~~~~~~~~~~~~~~~ ${this.currentStateName} finished ~~~~~~~~~~~~~~~~~~~~~~~~~~~`);
let state = this.states;
if (this.parallelBranch && this.parallelBranch.States) {
state = this.parallelBranch.States;
if (!this.currentState.Next) this.eventParallelResult.push(result); //it means the end of execution of branch
}
this.process(state[this.currentState.Next], this.currentState.Next, result);
// return resolve();
// });
};
return {
cb: cb,
done: cb,
succeed: (result) => cb(null, result),
fail: (err) => cb(err)
};
},
executionLog(log) {
if (this.detailedLog) this.cliLog(log);
}
};