aws-local-stepfunctions 0.3.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/main.cjs ADDED
@@ -0,0 +1,5 @@
1
+ "use strict";var K=Object.create;var E=Object.defineProperty;var X=Object.getOwnPropertyDescriptor;var Y=Object.getOwnPropertyNames,q=Object.getOwnPropertySymbols,Z=Object.getPrototypeOf,G=Object.prototype.hasOwnProperty,R=Object.prototype.propertyIsEnumerable;var M=(s,t,e)=>t in s?E(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e,A=(s,t)=>{for(var e in t||(t={}))G.call(t,e)&&M(s,e,t[e]);if(q)for(var e of q(t))R.call(t,e)&&M(s,e,t[e]);return s};var tt=(s,t)=>{for(var e in t)E(s,e,{get:t[e],enumerable:!0})},B=(s,t,e,a)=>{if(t&&typeof t=="object"||typeof t=="function")for(let r of Y(t))!G.call(s,r)&&r!==e&&E(s,r,{get:()=>t[r],enumerable:!(a=X(t,r))||a.enumerable});return s};var c=(s,t,e)=>(e=s!=null?K(Z(s)):{},B(t||!s||!s.__esModule?E(e,"default",{value:s,enumerable:!0}):e,s)),et=s=>B(E({},"__esModule",{value:!0}),s);var l=(s,t,e)=>new Promise((a,r)=>{var i=m=>{try{u(e.next(m))}catch(p){r(p)}},o=m=>{try{u(e.throw(m))}catch(p){r(p)}},u=m=>m.done?a(m.value):Promise.resolve(m.value).then(i,o);u((e=e.apply(s,t)).next())});var at={};tt(at,{ExecutionAbortedError:()=>y,StateMachine:()=>O});module.exports=et(at);var S=class{constructor(t){this.stateDefinition=t}buildExecutionResult(t){let e={stateResult:t,nextState:"",isEndState:!1};return"Next"in this.stateDefinition&&(e.nextState=this.stateDefinition.Next),"End"in this.stateDefinition&&(e.isEndState=this.stateDefinition.End),e}};var N=require("@aws-sdk/client-lambda");var V=class extends Error{constructor(t){super(t)}};var g=class extends V{constructor(e,a){super(`Execution of Lambda function "${a}" failed`);this.name="LambdaExecutionError",this.wrappedError=new Error(e.errorMessage),this.wrappedError.stack=e.trace.join(`
2
+ `)}toString(){return`${this.name}: ${this.message}. The error thrown by the Lambda was:
3
+ ${this.wrappedError.stack}`}};var b=class{constructor(t){this.client=new N.LambdaClient(t!=null?t:{})}invokeFunction(t,e){return l(this,null,function*(){let a=Buffer.from(JSON.stringify(e)),r=new N.InvokeCommand({FunctionName:t,Payload:a}),i=yield this.client.send(r),o=null;if(i.Payload&&(o=Buffer.from(i.Payload).toString(),o=JSON.parse(o)),i.FunctionError)throw new g(o,t);return o})}};var P=class extends S{constructor(t){super(t)}executeState(t,e,a){return l(this,null,function*(){let r=this.stateDefinition,i=new b;try{if(a!=null&&a.overrideFn){let u=yield a.overrideFn(t);return this.buildExecutionResult(u)}let o=yield i.invokeFunction(r.Resource,t);return this.buildExecutionResult(o)}catch(o){throw o instanceof g?console.error(o.toString()):console.error(o),o}})}};var H=require("jsonpath-plus");function n(s,t,e){return s.startsWith("$$")?(0,H.JSONPath)({path:s.slice(1),json:e!=null?e:null,wrap:!1}):(0,H.JSONPath)({path:s,json:t,wrap:!1})}function k(s){return!!s&&Object.getPrototypeOf(s)===Object.prototype}function x(s,t){return new Promise(e=>{let a=setTimeout(e,s);t==null||t.addEventListener("abort",()=>{a.unref()})})}var F=c(require("lodash/cloneDeep.js"),1),W=c(require("lodash/set.js"),1);function j(s,t,e){return s===void 0?t:s===null?{}:n(s,t,e)}function T(s,t,e){let a=Object.entries(s).map(([r,i])=>{let o=r,u=i;return k(i)&&(u=T(i,t)),r.endsWith(".$")&&typeof i=="string"&&(o=r.replace(".$",""),u=n(i,t,e)),[o,u]});return Object.fromEntries(a)}function $(s,t,e){if(s===void 0)return e;if(s===null)return t;let a=s.replace("$.","");if(k(t)){let r=(0,F.default)(t);return(0,W.default)(r,a,e)}else throw new Error("TODO: Change this error message for a more descriptive one")}function Q(s,t,e){return s===void 0?t:s===null?{}:n(s,t,e)}var z=c(require("p-limit"),1);var w=class extends S{constructor(t){super(t)}processItem(t,e,a,r,i){let o=this.stateDefinition,u;a.Map={Item:{Index:r,Value:t}},o.Parameters&&(u=T(o.Parameters,e,a));let m=new O(o.Iterator,i==null?void 0:i.validationOptions),{result:p}=m.run(u!=null?u:t,i==null?void 0:i.runOptions);return p}executeState(t,e,a){return l(this,null,function*(){let r=this.stateDefinition,i=t;if(r.ItemsPath&&(i=n(r.ItemsPath,t,e)),!Array.isArray(i))return this.buildExecutionResult([]);let o=40,u=(0,z.default)(r.MaxConcurrency||o),m=i.map((d,f)=>u(()=>this.processItem(d,t,e,f,a))),p=yield Promise.all(m);return delete e.Map,this.buildExecutionResult(p)})}};var v=class extends S{constructor(t){super(t)}executeState(t,e,a){return l(this,null,function*(){return this.stateDefinition.Result?this.buildExecutionResult(this.stateDefinition.Result):this.buildExecutionResult(t)})}};var h=class extends S{constructor(t){super(t)}executeState(t,e,a){return l(this,null,function*(){let r=this.stateDefinition;if((a==null?void 0:a.waitTimeOverrideOption)!==void 0)return yield x(a.waitTimeOverrideOption,a.abortSignal),this.buildExecutionResult(t);if(r.Seconds)yield x(r.Seconds*1e3,a==null?void 0:a.abortSignal);else if(r.Timestamp){let i=new Date(r.Timestamp),o=Date.now(),u=i.getTime()-o;yield x(u,a==null?void 0:a.abortSignal)}else if(r.SecondsPath){let i=n(r.SecondsPath,t,e);yield x(i*1e3,a==null?void 0:a.abortSignal)}else if(r.TimestampPath){let i=n(r.TimestampPath,t,e),o=new Date(i),u=Date.now(),m=o.getTime()-u;yield x(m,a==null?void 0:a.abortSignal)}return this.buildExecutionResult(t)})}};var U=c(require("wildcard-match"),1);var J=class extends S{constructor(t){super(t)}testChoiceRule(t,e){if("And"in t)return t.And.every(a=>this.testChoiceRule(a,e));if("Or"in t)return t.Or.some(a=>this.testChoiceRule(a,e));if("Not"in t)return!this.testChoiceRule(t.Not,e);if("StringEquals"in t)return n(t.Variable,e)===t.StringEquals;if("StringEqualsPath"in t){let a=n(t.Variable,e),r=n(t.StringEqualsPath,e);return a===r}if("StringLessThan"in t)return n(t.Variable,e)<t.StringLessThan;if("StringLessThanPath"in t){let a=n(t.Variable,e),r=n(t.StringLessThanPath,e);return a<r}if("StringGreaterThan"in t)return n(t.Variable,e)>t.StringGreaterThan;if("StringGreaterThanPath"in t){let a=n(t.Variable,e),r=n(t.StringGreaterThanPath,e);return a>r}if("StringLessThanEquals"in t)return n(t.Variable,e)<=t.StringLessThanEquals;if("StringLessThanEqualsPath"in t){let a=n(t.Variable,e),r=n(t.StringLessThanEqualsPath,e);return a<=r}if("StringGreaterThanEquals"in t)return n(t.Variable,e)>=t.StringGreaterThanEquals;if("StringGreaterThanEqualsPath"in t){let a=n(t.Variable,e),r=n(t.StringGreaterThanEqualsPath,e);return a>=r}if("StringMatches"in t){let a=n(t.Variable,e);return(0,U.default)(t.StringMatches,{separator:!1})(a)}if("NumericEquals"in t)return n(t.Variable,e)===t.NumericEquals;if("NumericEqualsPath"in t){let a=n(t.Variable,e),r=n(t.NumericEqualsPath,e);return a===r}if("NumericLessThan"in t)return n(t.Variable,e)<t.NumericLessThan;if("NumericLessThanPath"in t){let a=n(t.Variable,e),r=n(t.NumericLessThanPath,e);return a<r}if("NumericGreaterThan"in t)return n(t.Variable,e)>t.NumericGreaterThan;if("NumericGreaterThanPath"in t){let a=n(t.Variable,e),r=n(t.NumericGreaterThanPath,e);return a>r}if("NumericLessThanEquals"in t)return n(t.Variable,e)<=t.NumericLessThanEquals;if("NumericLessThanEqualsPath"in t){let a=n(t.Variable,e),r=n(t.NumericLessThanEqualsPath,e);return a<=r}if("NumericGreaterThanEquals"in t)return n(t.Variable,e)>=t.NumericGreaterThanEquals;if("NumericGreaterThanEqualsPath"in t){let a=n(t.Variable,e),r=n(t.NumericGreaterThanEqualsPath,e);return a>=r}if("BooleanEquals"in t)return n(t.Variable,e)===t.BooleanEquals;if("BooleanEqualsPath"in t){let a=n(t.Variable,e),r=n(t.BooleanEqualsPath,e);return a===r}if("TimestampEquals"in t){let a=new Date(n(t.Variable,e)),r=new Date(t.TimestampEquals);return a.getTime()===r.getTime()}if("TimestampEqualsPath"in t){let a=new Date(n(t.Variable,e)),r=new Date(n(t.TimestampEqualsPath,e));return a.getTime()===r.getTime()}if("TimestampLessThan"in t){let a=new Date(n(t.Variable,e)),r=new Date(t.TimestampLessThan);return a<r}if("TimestampLessThanPath"in t){let a=new Date(n(t.Variable,e)),r=new Date(n(t.TimestampLessThanPath,e));return a<r}if("TimestampGreaterThan"in t){let a=new Date(n(t.Variable,e)),r=new Date(t.TimestampGreaterThan);return a>r}if("TimestampGreaterThanPath"in t){let a=new Date(n(t.Variable,e)),r=new Date(n(t.TimestampGreaterThanPath,e));return a>r}if("TimestampLessThanEquals"in t){let a=new Date(n(t.Variable,e)),r=new Date(t.TimestampLessThanEquals);return a<=r}if("TimestampLessThanEqualsPath"in t){let a=new Date(n(t.Variable,e)),r=new Date(n(t.TimestampLessThanEqualsPath,e));return a<=r}if("TimestampGreaterThanEquals"in t){let a=new Date(n(t.Variable,e)),r=new Date(t.TimestampGreaterThanEquals);return a>=r}if("TimestampGreaterThanEqualsPath"in t){let a=new Date(n(t.Variable,e)),r=new Date(n(t.TimestampGreaterThanEqualsPath,e));return a>=r}if("IsNull"in t){let a=n(t.Variable,e);return t.IsNull&&a===null}if("IsPresent"in t){let a=n(t.Variable,e);return t.IsPresent&&a!==void 0}if("IsNumeric"in t){let a=n(t.Variable,e);return t.IsNumeric&&typeof a=="number"}if("IsString"in t){let a=n(t.Variable,e);return t.IsString&&typeof a=="string"}if("IsBoolean"in t){let a=n(t.Variable,e);return t.IsBoolean&&typeof a=="boolean"}if("IsTimestamp"in t){let a=n(t.Variable,e);return t.IsTimestamp&&/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|(\+|-)\d{2}:\d{2})/.test(a)}return!1}executeState(t,e,a){return l(this,null,function*(){let r=this.stateDefinition;for(let i of r.Choices)if(this.testChoiceRule(i,t))return{stateResult:t,nextState:i.Next,isEndState:!1};if(r.Default)return{stateResult:t,nextState:r.Default,isEndState:!1};throw new Error("States.NoChoiceMatched")})}};var D=class extends S{constructor(t){super(t)}executeState(t,e,a){return l(this,null,function*(){return{stateResult:t,nextState:"",isEndState:!0}})}};var I=class extends S{constructor(t){super(t)}executeState(t,e,a){return l(this,null,function*(){return{stateResult:t,nextState:"",isEndState:!0}})}};var y=class extends V{constructor(){super("Execution aborted"),this.name="ExecutionAbortedError"}toString(){return"Execution aborted"}};var _=c(require("asl-validator"),1),C=c(require("lodash/cloneDeep.js"),1);var O=class{constructor(t,e){let{isValid:a,errorsText:r}=(0,_.default)(t,A({checkArn:!0,checkPaths:!0},e));if(!a)throw new Error(`State machine definition is invalid, see error(s) below:
4
+ ${r(`
5
+ `)}`);this.definition=t,this.stateExecutors={Task:this.executeTaskState,Map:this.executeMapState,Pass:this.executePassState,Wait:this.executeWaitState,Choice:this.executeChoiceState,Succeed:this.executeSucceedState,Fail:this.executeFailState},this.validationOptions=e}run(t,e){let a=new AbortController,r=new Promise((u,m)=>{e!=null&&e.noThrowOnAbort?a.signal.addEventListener("abort",()=>u(null)):a.signal.addEventListener("abort",()=>m(new y))}),i=this.execute(t,{validationOptions:this.validationOptions,runOptions:e,abortSignal:a.signal}),o=Promise.race([i,r]);return{abort:()=>a.abort(),result:o}}execute(t,e){return l(this,null,function*(){let a=this.definition.States[this.definition.StartAt],r=this.definition.StartAt,i=(0,C.default)(t),o=(0,C.default)(t),u=null,m="",p=!1,d={};do o=this.processInput(a,o,d),{stateResult:u,nextState:m,isEndState:p}=yield this.stateExecutors[a.Type](a,o,d,r,e),u=this.processResult(a,u,i,d),i=u,o=u,a=this.definition.States[m],r=m;while(!p&&!e.abortSignal.aborted);return u})}processInput(t,e,a){let r=e;return"InputPath"in t&&(r=j(t.InputPath,r,a)),"Parameters"in t&&t.Type!=="Map"&&(r=T(t.Parameters,r,a)),r}processResult(t,e,a,r){let i=e;return"ResultSelector"in t&&(i=T(t.ResultSelector,i,r)),"ResultPath"in t&&(i=$(t.ResultPath,a,i)),"OutputPath"in t&&(i=Q(t.OutputPath,i,r)),i}executeTaskState(t,e,a,r,i){return l(this,null,function*(){var p,d,f;let o=(f=(d=(p=i.runOptions)==null?void 0:p.overrides)==null?void 0:d.taskResourceLocalHandlers)==null?void 0:f[r];return yield new P(t).executeState(e,a,{overrideFn:o})})}executeMapState(t,e,a,r,i){return l(this,null,function*(){return yield new w(t).executeState(e,a,{validationOptions:i.validationOptions,runOptions:i.runOptions})})}executePassState(t,e,a,r,i){return l(this,null,function*(){return yield new v(t).executeState(e,a)})}executeWaitState(t,e,a,r,i){return l(this,null,function*(){var d,f,L;let o=(L=(f=(d=i.runOptions)==null?void 0:d.overrides)==null?void 0:f.waitTimeOverrides)==null?void 0:L[r],u=i.abortSignal;return yield new h(t).executeState(e,a,{waitTimeOverrideOption:o,abortSignal:u})})}executeChoiceState(t,e,a,r,i){return l(this,null,function*(){return yield new J(t).executeState(e,a)})}executeSucceedState(t,e,a,r,i){return l(this,null,function*(){return yield new D(t).executeState(e,a)})}executeFailState(t,e,a,r,i){return l(this,null,function*(){return yield new I(t).executeState(e,a)})}};0&&(module.exports={ExecutionAbortedError,StateMachine});
@@ -0,0 +1,285 @@
1
+ declare type StateType = 'Task' | 'Map' | 'Pass' | 'Wait' | 'Choice' | 'Succeed' | 'Fail';
2
+
3
+ interface BaseState {
4
+ Type: StateType;
5
+ Comment?: string;
6
+ }
7
+
8
+ declare type PayloadTemplate = object;
9
+ interface CanHaveInputPath {
10
+ InputPath?: string | null;
11
+ }
12
+ interface CanHaveParameters {
13
+ Parameters?: PayloadTemplate;
14
+ }
15
+ interface CanHaveResultSelector {
16
+ ResultSelector?: PayloadTemplate;
17
+ }
18
+ interface CanHaveResultPath {
19
+ ResultPath?: string | null;
20
+ }
21
+ interface CanHaveOutputPath {
22
+ OutputPath?: string | null;
23
+ }
24
+
25
+ declare type StringOperatorNames = 'StringEquals' | 'StringLessThan' | 'StringGreaterThan' | 'StringLessThanEquals' | 'StringGreaterThanEquals' | 'StringMatches';
26
+ declare type StringPathOperatorNames = 'StringEqualsPath' | 'StringLessThanPath' | 'StringGreaterThanPath' | 'StringLessThanEqualsPath' | 'StringGreaterThanEqualsPath';
27
+ declare type NumericOperatorNames = 'NumericEquals' | 'NumericLessThan' | 'NumericGreaterThan' | 'NumericLessThanEquals' | 'NumericGreaterThanEquals';
28
+ declare type NumericPathOperatorNames = 'NumericEqualsPath' | 'NumericLessThanPath' | 'NumericGreaterThanPath' | 'NumericLessThanEqualsPath' | 'NumericGreaterThanEqualsPath';
29
+ declare type BooleanOperatorNames = 'BooleanEquals';
30
+ declare type BooleanPathOperatorNames = 'BooleanEqualsPath';
31
+ declare type TimestampOperatorNames = 'TimestampEquals' | 'TimestampLessThan' | 'TimestampGreaterThan' | 'TimestampLessThanEquals' | 'TimestampGreaterThanEquals';
32
+ declare type TimestampPathOperatorNames = 'TimestampEqualsPath' | 'TimestampLessThanPath' | 'TimestampGreaterThanPath' | 'TimestampLessThanEqualsPath' | 'TimestampGreaterThanEqualsPath';
33
+ declare type TypeTestOperatorNames = 'IsNull' | 'IsPresent' | 'IsNumeric' | 'IsString' | 'IsBoolean' | 'IsTimestamp';
34
+ declare type StringComparisonOperator = {
35
+ [P in StringOperatorNames]?: string;
36
+ };
37
+ declare type StringPathComparisonOperator = {
38
+ [P in StringPathOperatorNames]?: string;
39
+ };
40
+ declare type NumericComparisonOperator = {
41
+ [P in NumericOperatorNames]?: number;
42
+ };
43
+ declare type NumericPathComparisonOperator = {
44
+ [P in NumericPathOperatorNames]?: string;
45
+ };
46
+ declare type BooleanComparisonOperator = {
47
+ [P in BooleanOperatorNames]?: boolean;
48
+ };
49
+ declare type BooleanPathComparisonOperator = {
50
+ [P in BooleanPathOperatorNames]?: string;
51
+ };
52
+ declare type TimestampComparisonOperator = {
53
+ [P in TimestampOperatorNames]?: string;
54
+ };
55
+ declare type TimestampPathComparisonOperator = {
56
+ [P in TimestampPathOperatorNames]?: string;
57
+ };
58
+ declare type TypeTestComparisonOperator = {
59
+ [P in TypeTestOperatorNames]?: true;
60
+ };
61
+ declare type ComparisonOperator = StringComparisonOperator | StringPathComparisonOperator | NumericComparisonOperator | NumericPathComparisonOperator | BooleanComparisonOperator | BooleanPathComparisonOperator | TimestampComparisonOperator | TimestampPathComparisonOperator | TypeTestComparisonOperator;
62
+ declare type BaseDataTestExpression = {
63
+ Variable: string;
64
+ };
65
+ declare type DataTestExpression = BaseDataTestExpression & ComparisonOperator;
66
+ declare type BooleanExpression = {
67
+ And?: ChoiceRuleWithoutNext[];
68
+ Or?: ChoiceRuleWithoutNext[];
69
+ Not?: ChoiceRuleWithoutNext;
70
+ };
71
+ declare type ChoiceRuleWithoutNext = DataTestExpression | BooleanExpression;
72
+ declare type ChoiceRule = (DataTestExpression | BooleanExpression) & {
73
+ Next: string;
74
+ };
75
+ interface BaseChoiceState extends BaseState, CanHaveInputPath, CanHaveOutputPath {
76
+ Type: 'Choice';
77
+ Choices: ChoiceRule[];
78
+ Default?: string;
79
+ }
80
+ declare type ChoiceState = BaseChoiceState;
81
+
82
+ interface EndableState extends BaseState {
83
+ Type: Exclude<StateType, 'Choice' | 'Succeed' | 'Fail'>;
84
+ End: true;
85
+ }
86
+ interface SucceedOrFailState extends BaseState {
87
+ Type: Extract<StateType, 'Succeed' | 'Fail'>;
88
+ }
89
+ declare type TerminalState = EndableState | SucceedOrFailState;
90
+
91
+ interface BaseFailState extends BaseState {
92
+ Type: 'Fail';
93
+ Cause?: string;
94
+ Error?: string;
95
+ }
96
+ declare type FailState = TerminalState & BaseFailState;
97
+
98
+ interface NextableState extends BaseState {
99
+ Type: Exclude<StateType, 'Choice' | 'Succeed' | 'Fail'>;
100
+ Next: string;
101
+ }
102
+ declare type IntermediateState = NextableState;
103
+
104
+ interface BaseMapState extends BaseState, CanHaveInputPath, CanHaveParameters, CanHaveResultSelector, CanHaveResultPath, CanHaveOutputPath {
105
+ Type: 'Map';
106
+ Iterator: Omit<StateMachineDefinition, 'Version' | 'TimeoutSeconds'>;
107
+ ItemsPath?: string;
108
+ MaxConcurrency?: number;
109
+ }
110
+ declare type MapState = (IntermediateState | TerminalState) & BaseMapState;
111
+
112
+ declare type JSONValue = null | boolean | number | string | object | JSONValue[];
113
+
114
+ interface BasePassState extends BaseState, CanHaveInputPath, CanHaveParameters, CanHaveResultPath, CanHaveOutputPath {
115
+ Type: 'Pass';
116
+ Result?: JSONValue;
117
+ }
118
+ declare type PassState = (IntermediateState | TerminalState) & BasePassState;
119
+
120
+ interface BaseSucceedState extends BaseState, CanHaveInputPath, CanHaveOutputPath {
121
+ Type: 'Succeed';
122
+ }
123
+ declare type SucceedState = TerminalState & BaseSucceedState;
124
+
125
+ interface BaseTaskState extends BaseState, CanHaveInputPath, CanHaveParameters, CanHaveResultSelector, CanHaveResultPath, CanHaveOutputPath {
126
+ Type: 'Task';
127
+ Resource: string;
128
+ }
129
+ declare type TaskState = (IntermediateState | TerminalState) & BaseTaskState;
130
+
131
+ interface BaseWaitState extends BaseState, CanHaveInputPath, CanHaveOutputPath {
132
+ Type: 'Wait';
133
+ Seconds?: number;
134
+ Timestamp?: string;
135
+ SecondsPath?: string;
136
+ TimestampPath?: string;
137
+ }
138
+ declare type WaitState = (IntermediateState | TerminalState) & BaseWaitState;
139
+
140
+ declare type AllStates = TaskState | MapState | PassState | WaitState | ChoiceState | SucceedState | FailState;
141
+
142
+ interface StateMachineDefinition {
143
+ States: Record<string, AllStates>;
144
+ StartAt: string;
145
+ Comment?: string;
146
+ Version?: string;
147
+ TimeoutSeconds?: number;
148
+ }
149
+
150
+ declare type TaskStateResourceLocalHandler = {
151
+ [taskStateName: string]: (input: JSONValue) => Promise<JSONValue>;
152
+ };
153
+ declare type WaitStateTimeOverride = {
154
+ [waitStateName: string]: number;
155
+ };
156
+ interface Overrides {
157
+ taskResourceLocalHandlers?: TaskStateResourceLocalHandler;
158
+ waitTimeOverrides?: WaitStateTimeOverride;
159
+ }
160
+ interface ValidationOptions {
161
+ readonly checkPaths?: boolean;
162
+ readonly checkArn?: boolean;
163
+ }
164
+ interface RunOptions {
165
+ overrides?: Overrides;
166
+ noThrowOnAbort?: boolean;
167
+ }
168
+
169
+ declare class StateMachine {
170
+ /**
171
+ * The structure of the State Machine as represented by the Amazon States Language.
172
+ */
173
+ private readonly definition;
174
+ /**
175
+ * A map of functions to execute each type of state.
176
+ */
177
+ private readonly stateExecutors;
178
+ /**
179
+ * Options to control whether to apply certain validations to the state machine definition.
180
+ */
181
+ private readonly validationOptions;
182
+ /**
183
+ * Constructs a new state machine.
184
+ * @param definition The state machine definition defined using the Amazon States Language (https://states-language.net/spec.html).
185
+ * @param validationOptions Options to control whether to apply certain validations to the definition.
186
+ * These options also apply to state machines defined in the `Iterator` field of `Map` states.
187
+ */
188
+ constructor(definition: StateMachineDefinition, validationOptions?: ValidationOptions);
189
+ /**
190
+ * Executes the state machine, running through the states specified in the definition.
191
+ *
192
+ * By default, if the execution is aborted, the result will throw an `ExecutionAbortedError`. This behavior can be changed by setting
193
+ * the `noThrowOnAbort` option to `true`, in which case the result will be `null`.
194
+ *
195
+ * @param input The input to pass to this state machine execution.
196
+ * @param options Miscellaneous options to control certain behaviors of the execution.
197
+ */
198
+ run(input: JSONValue, options?: RunOptions): {
199
+ abort: () => void;
200
+ result: Promise<JSONValue>;
201
+ };
202
+ /**
203
+ * Helper method that handles the execution of the machine states and the transitions between them.
204
+ */
205
+ private execute;
206
+ /**
207
+ * Process the current input according to the `InputPath` and `Parameters` fields.
208
+ */
209
+ private processInput;
210
+ /**
211
+ * Process the current result according to the `ResultSelector`, `ResultPath` and `OutputPath` fields.
212
+ */
213
+ private processResult;
214
+ /**
215
+ * Handler for task states.
216
+ *
217
+ * Invokes the Lambda function specified in the `Resource` field
218
+ * and sets the current result of the state machine to the value returned by the Lambda.
219
+ */
220
+ private executeTaskState;
221
+ /**
222
+ * Handler for map states.
223
+ *
224
+ * Iterates over the current input items or the items of an array specified
225
+ * by the `ItemsPath` field, and then processes each item by passing it
226
+ * as the input to the state machine specified in the `Iterator` field.
227
+ */
228
+ private executeMapState;
229
+ /**
230
+ * Handler for pass states.
231
+ *
232
+ * If the `Result` field is specified, copies `Result` into the current result.
233
+ * Else, copies the current input into the current result.
234
+ */
235
+ private executePassState;
236
+ /**
237
+ * Handler for wait states.
238
+ *
239
+ * Pauses the state machine execution for a certain amount of time
240
+ * based on one of the `Seconds`, `Timestamp`, `SecondsPath` or `TimestampPath` fields.
241
+ */
242
+ private executeWaitState;
243
+ /**
244
+ * Handler for choice states.
245
+ *
246
+ * Evaluates each choice rule specified in the `Choices` field.
247
+ *
248
+ * If one of the rules matches, then the state machine transitions to the
249
+ * state specified in the `Next` field for that choice rule.
250
+ *
251
+ * If no rule matches but the `Default` field is specified,
252
+ * then the next state will be the state specified in said field.
253
+ *
254
+ * If no rule matches and the `Default` field is not specified, throws a
255
+ * States.NoChoiceMatched error.
256
+ */
257
+ private executeChoiceState;
258
+ /**
259
+ * Handler for succeed states.
260
+ *
261
+ * Ends the state machine execution successfully.
262
+ */
263
+ private executeSucceedState;
264
+ /**
265
+ * Handler for fail states.
266
+ *
267
+ * Ends the state machine execution and marks it as a failure.
268
+ */
269
+ private executeFailState;
270
+ }
271
+
272
+ declare abstract class CustomError extends Error {
273
+ constructor(message?: string);
274
+ abstract toString(): string;
275
+ }
276
+
277
+ /**
278
+ * Represents the abortion of a state machine execution.
279
+ */
280
+ declare class ExecutionAbortedError extends CustomError {
281
+ constructor();
282
+ toString(): string;
283
+ }
284
+
285
+ export { ExecutionAbortedError, StateMachine };
@@ -0,0 +1,5 @@
1
+ var A=Object.defineProperty;var I=Object.getOwnPropertySymbols;var B=Object.prototype.hasOwnProperty,F=Object.prototype.propertyIsEnumerable;var H=(i,t,a)=>t in i?A(i,t,{enumerable:!0,configurable:!0,writable:!0,value:a}):i[t]=a,k=(i,t)=>{for(var a in t||(t={}))B.call(t,a)&&H(i,a,t[a]);if(I)for(var a of I(t))F.call(t,a)&&H(i,a,t[a]);return i};var l=(i,t,a)=>new Promise((e,r)=>{var s=m=>{try{u(a.next(m))}catch(p){r(p)}},o=m=>{try{u(a.throw(m))}catch(p){r(p)}},u=m=>m.done?e(m.value):Promise.resolve(m.value).then(s,o);u((a=a.apply(i,t)).next())});var S=class{constructor(t){this.stateDefinition=t}buildExecutionResult(t){let a={stateResult:t,nextState:"",isEndState:!1};return"Next"in this.stateDefinition&&(a.nextState=this.stateDefinition.Next),"End"in this.stateDefinition&&(a.isEndState=this.stateDefinition.End),a}};import{LambdaClient as W,InvokeCommand as j}from"@aws-sdk/client-lambda";var c=class extends Error{constructor(t){super(t)}};var V=class extends c{constructor(a,e){super(`Execution of Lambda function "${e}" failed`);this.name="LambdaExecutionError",this.wrappedError=new Error(a.errorMessage),this.wrappedError.stack=a.trace.join(`
2
+ `)}toString(){return`${this.name}: ${this.message}. The error thrown by the Lambda was:
3
+ ${this.wrappedError.stack}`}};var y=class{constructor(t){this.client=new W(t!=null?t:{})}invokeFunction(t,a){return l(this,null,function*(){let e=Buffer.from(JSON.stringify(a)),r=new j({FunctionName:t,Payload:e}),s=yield this.client.send(r),o=null;if(s.Payload&&(o=Buffer.from(s.Payload).toString(),o=JSON.parse(o)),s.FunctionError)throw new V(o,t);return o})}};var E=class extends S{constructor(t){super(t)}executeState(t,a,e){return l(this,null,function*(){let r=this.stateDefinition,s=new y;try{if(e!=null&&e.overrideFn){let u=yield e.overrideFn(t);return this.buildExecutionResult(u)}let o=yield s.invokeFunction(r.Resource,t);return this.buildExecutionResult(o)}catch(o){throw o instanceof V?console.error(o.toString()):console.error(o),o}})}};import{JSONPath as C}from"jsonpath-plus";function n(i,t,a){return i.startsWith("$$")?C({path:i.slice(1),json:a!=null?a:null,wrap:!1}):C({path:i,json:t,wrap:!1})}function J(i){return!!i&&Object.getPrototypeOf(i)===Object.prototype}function g(i,t){return new Promise(a=>{let e=setTimeout(a,i);t==null||t.addEventListener("abort",()=>{e.unref()})})}import $ from"lodash/cloneDeep.js";import Q from"lodash/set.js";function L(i,t,a){return i===void 0?t:i===null?{}:n(i,t,a)}function x(i,t,a){let e=Object.entries(i).map(([r,s])=>{let o=r,u=s;return J(s)&&(u=x(s,t)),r.endsWith(".$")&&typeof s=="string"&&(o=r.replace(".$",""),u=n(s,t,a)),[o,u]});return Object.fromEntries(e)}function q(i,t,a){if(i===void 0)return a;if(i===null)return t;let e=i.replace("$.","");if(J(t)){let r=$(t);return Q(r,e,a)}else throw new Error("TODO: Change this error message for a more descriptive one")}function M(i,t,a){return i===void 0?t:i===null?{}:n(i,t,a)}import z from"p-limit";var b=class extends S{constructor(t){super(t)}processItem(t,a,e,r,s){let o=this.stateDefinition,u;e.Map={Item:{Index:r,Value:t}},o.Parameters&&(u=x(o.Parameters,a,e));let m=new T(o.Iterator,s==null?void 0:s.validationOptions),{result:p}=m.run(u!=null?u:t,s==null?void 0:s.runOptions);return p}executeState(t,a,e){return l(this,null,function*(){let r=this.stateDefinition,s=t;if(r.ItemsPath&&(s=n(r.ItemsPath,t,a)),!Array.isArray(s))return this.buildExecutionResult([]);let o=40,u=z(r.MaxConcurrency||o),m=s.map((d,f)=>u(()=>this.processItem(d,t,a,f,e))),p=yield Promise.all(m);return delete a.Map,this.buildExecutionResult(p)})}};var N=class extends S{constructor(t){super(t)}executeState(t,a,e){return l(this,null,function*(){return this.stateDefinition.Result?this.buildExecutionResult(this.stateDefinition.Result):this.buildExecutionResult(t)})}};var P=class extends S{constructor(t){super(t)}executeState(t,a,e){return l(this,null,function*(){let r=this.stateDefinition;if((e==null?void 0:e.waitTimeOverrideOption)!==void 0)return yield g(e.waitTimeOverrideOption,e.abortSignal),this.buildExecutionResult(t);if(r.Seconds)yield g(r.Seconds*1e3,e==null?void 0:e.abortSignal);else if(r.Timestamp){let s=new Date(r.Timestamp),o=Date.now(),u=s.getTime()-o;yield g(u,e==null?void 0:e.abortSignal)}else if(r.SecondsPath){let s=n(r.SecondsPath,t,a);yield g(s*1e3,e==null?void 0:e.abortSignal)}else if(r.TimestampPath){let s=n(r.TimestampPath,t,a),o=new Date(s),u=Date.now(),m=o.getTime()-u;yield g(m,e==null?void 0:e.abortSignal)}return this.buildExecutionResult(t)})}};import U from"wildcard-match";var w=class extends S{constructor(t){super(t)}testChoiceRule(t,a){if("And"in t)return t.And.every(e=>this.testChoiceRule(e,a));if("Or"in t)return t.Or.some(e=>this.testChoiceRule(e,a));if("Not"in t)return!this.testChoiceRule(t.Not,a);if("StringEquals"in t)return n(t.Variable,a)===t.StringEquals;if("StringEqualsPath"in t){let e=n(t.Variable,a),r=n(t.StringEqualsPath,a);return e===r}if("StringLessThan"in t)return n(t.Variable,a)<t.StringLessThan;if("StringLessThanPath"in t){let e=n(t.Variable,a),r=n(t.StringLessThanPath,a);return e<r}if("StringGreaterThan"in t)return n(t.Variable,a)>t.StringGreaterThan;if("StringGreaterThanPath"in t){let e=n(t.Variable,a),r=n(t.StringGreaterThanPath,a);return e>r}if("StringLessThanEquals"in t)return n(t.Variable,a)<=t.StringLessThanEquals;if("StringLessThanEqualsPath"in t){let e=n(t.Variable,a),r=n(t.StringLessThanEqualsPath,a);return e<=r}if("StringGreaterThanEquals"in t)return n(t.Variable,a)>=t.StringGreaterThanEquals;if("StringGreaterThanEqualsPath"in t){let e=n(t.Variable,a),r=n(t.StringGreaterThanEqualsPath,a);return e>=r}if("StringMatches"in t){let e=n(t.Variable,a);return U(t.StringMatches,{separator:!1})(e)}if("NumericEquals"in t)return n(t.Variable,a)===t.NumericEquals;if("NumericEqualsPath"in t){let e=n(t.Variable,a),r=n(t.NumericEqualsPath,a);return e===r}if("NumericLessThan"in t)return n(t.Variable,a)<t.NumericLessThan;if("NumericLessThanPath"in t){let e=n(t.Variable,a),r=n(t.NumericLessThanPath,a);return e<r}if("NumericGreaterThan"in t)return n(t.Variable,a)>t.NumericGreaterThan;if("NumericGreaterThanPath"in t){let e=n(t.Variable,a),r=n(t.NumericGreaterThanPath,a);return e>r}if("NumericLessThanEquals"in t)return n(t.Variable,a)<=t.NumericLessThanEquals;if("NumericLessThanEqualsPath"in t){let e=n(t.Variable,a),r=n(t.NumericLessThanEqualsPath,a);return e<=r}if("NumericGreaterThanEquals"in t)return n(t.Variable,a)>=t.NumericGreaterThanEquals;if("NumericGreaterThanEqualsPath"in t){let e=n(t.Variable,a),r=n(t.NumericGreaterThanEqualsPath,a);return e>=r}if("BooleanEquals"in t)return n(t.Variable,a)===t.BooleanEquals;if("BooleanEqualsPath"in t){let e=n(t.Variable,a),r=n(t.BooleanEqualsPath,a);return e===r}if("TimestampEquals"in t){let e=new Date(n(t.Variable,a)),r=new Date(t.TimestampEquals);return e.getTime()===r.getTime()}if("TimestampEqualsPath"in t){let e=new Date(n(t.Variable,a)),r=new Date(n(t.TimestampEqualsPath,a));return e.getTime()===r.getTime()}if("TimestampLessThan"in t){let e=new Date(n(t.Variable,a)),r=new Date(t.TimestampLessThan);return e<r}if("TimestampLessThanPath"in t){let e=new Date(n(t.Variable,a)),r=new Date(n(t.TimestampLessThanPath,a));return e<r}if("TimestampGreaterThan"in t){let e=new Date(n(t.Variable,a)),r=new Date(t.TimestampGreaterThan);return e>r}if("TimestampGreaterThanPath"in t){let e=new Date(n(t.Variable,a)),r=new Date(n(t.TimestampGreaterThanPath,a));return e>r}if("TimestampLessThanEquals"in t){let e=new Date(n(t.Variable,a)),r=new Date(t.TimestampLessThanEquals);return e<=r}if("TimestampLessThanEqualsPath"in t){let e=new Date(n(t.Variable,a)),r=new Date(n(t.TimestampLessThanEqualsPath,a));return e<=r}if("TimestampGreaterThanEquals"in t){let e=new Date(n(t.Variable,a)),r=new Date(t.TimestampGreaterThanEquals);return e>=r}if("TimestampGreaterThanEqualsPath"in t){let e=new Date(n(t.Variable,a)),r=new Date(n(t.TimestampGreaterThanEqualsPath,a));return e>=r}if("IsNull"in t){let e=n(t.Variable,a);return t.IsNull&&e===null}if("IsPresent"in t){let e=n(t.Variable,a);return t.IsPresent&&e!==void 0}if("IsNumeric"in t){let e=n(t.Variable,a);return t.IsNumeric&&typeof e=="number"}if("IsString"in t){let e=n(t.Variable,a);return t.IsString&&typeof e=="string"}if("IsBoolean"in t){let e=n(t.Variable,a);return t.IsBoolean&&typeof e=="boolean"}if("IsTimestamp"in t){let e=n(t.Variable,a);return t.IsTimestamp&&/\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(Z|(\+|-)\d{2}:\d{2})/.test(e)}return!1}executeState(t,a,e){return l(this,null,function*(){let r=this.stateDefinition;for(let s of r.Choices)if(this.testChoiceRule(s,t))return{stateResult:t,nextState:s.Next,isEndState:!1};if(r.Default)return{stateResult:t,nextState:r.Default,isEndState:!1};throw new Error("States.NoChoiceMatched")})}};var v=class extends S{constructor(t){super(t)}executeState(t,a,e){return l(this,null,function*(){return{stateResult:t,nextState:"",isEndState:!0}})}};var h=class extends S{constructor(t){super(t)}executeState(t,a,e){return l(this,null,function*(){return{stateResult:t,nextState:"",isEndState:!0}})}};var O=class extends c{constructor(){super("Execution aborted"),this.name="ExecutionAbortedError"}toString(){return"Execution aborted"}};import _ from"asl-validator";import G from"lodash/cloneDeep.js";var T=class{constructor(t,a){let{isValid:e,errorsText:r}=_(t,k({checkArn:!0,checkPaths:!0},a));if(!e)throw new Error(`State machine definition is invalid, see error(s) below:
4
+ ${r(`
5
+ `)}`);this.definition=t,this.stateExecutors={Task:this.executeTaskState,Map:this.executeMapState,Pass:this.executePassState,Wait:this.executeWaitState,Choice:this.executeChoiceState,Succeed:this.executeSucceedState,Fail:this.executeFailState},this.validationOptions=a}run(t,a){let e=new AbortController,r=new Promise((u,m)=>{a!=null&&a.noThrowOnAbort?e.signal.addEventListener("abort",()=>u(null)):e.signal.addEventListener("abort",()=>m(new O))}),s=this.execute(t,{validationOptions:this.validationOptions,runOptions:a,abortSignal:e.signal}),o=Promise.race([s,r]);return{abort:()=>e.abort(),result:o}}execute(t,a){return l(this,null,function*(){let e=this.definition.States[this.definition.StartAt],r=this.definition.StartAt,s=G(t),o=G(t),u=null,m="",p=!1,d={};do o=this.processInput(e,o,d),{stateResult:u,nextState:m,isEndState:p}=yield this.stateExecutors[e.Type](e,o,d,r,a),u=this.processResult(e,u,s,d),s=u,o=u,e=this.definition.States[m],r=m;while(!p&&!a.abortSignal.aborted);return u})}processInput(t,a,e){let r=a;return"InputPath"in t&&(r=L(t.InputPath,r,e)),"Parameters"in t&&t.Type!=="Map"&&(r=x(t.Parameters,r,e)),r}processResult(t,a,e,r){let s=a;return"ResultSelector"in t&&(s=x(t.ResultSelector,s,r)),"ResultPath"in t&&(s=q(t.ResultPath,e,s)),"OutputPath"in t&&(s=M(t.OutputPath,s,r)),s}executeTaskState(t,a,e,r,s){return l(this,null,function*(){var p,d,f;let o=(f=(d=(p=s.runOptions)==null?void 0:p.overrides)==null?void 0:d.taskResourceLocalHandlers)==null?void 0:f[r];return yield new E(t).executeState(a,e,{overrideFn:o})})}executeMapState(t,a,e,r,s){return l(this,null,function*(){return yield new b(t).executeState(a,e,{validationOptions:s.validationOptions,runOptions:s.runOptions})})}executePassState(t,a,e,r,s){return l(this,null,function*(){return yield new N(t).executeState(a,e)})}executeWaitState(t,a,e,r,s){return l(this,null,function*(){var d,f,D;let o=(D=(f=(d=s.runOptions)==null?void 0:d.overrides)==null?void 0:f.waitTimeOverrides)==null?void 0:D[r],u=s.abortSignal;return yield new P(t).executeState(a,e,{waitTimeOverrideOption:o,abortSignal:u})})}executeChoiceState(t,a,e,r,s){return l(this,null,function*(){return yield new w(t).executeState(a,e)})}executeSucceedState(t,a,e,r,s){return l(this,null,function*(){return yield new v(t).executeState(a,e)})}executeFailState(t,a,e,r,s){return l(this,null,function*(){return yield new h(t).executeState(a,e)})}};export{O as ExecutionAbortedError,T as StateMachine};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aws-local-stepfunctions",
3
- "version": "0.3.1",
3
+ "version": "0.3.2",
4
4
  "description": "Execute an AWS Step Function locally",
5
5
  "keywords": [
6
6
  "aws",