@lafken/state-machine 0.10.1

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.
Files changed (33) hide show
  1. package/LICENCE +21 -0
  2. package/README.md +383 -0
  3. package/lib/index.d.ts +2 -0
  4. package/lib/index.js +18 -0
  5. package/lib/main/index.d.ts +2 -0
  6. package/lib/main/index.js +18 -0
  7. package/lib/main/param/index.d.ts +2 -0
  8. package/lib/main/param/index.js +18 -0
  9. package/lib/main/param/param.d.ts +105 -0
  10. package/lib/main/param/param.js +115 -0
  11. package/lib/main/param/param.types.d.ts +77 -0
  12. package/lib/main/param/param.types.js +2 -0
  13. package/lib/main/state-machine/index.d.ts +2 -0
  14. package/lib/main/state-machine/index.js +18 -0
  15. package/lib/main/state-machine/state-machine.d.ts +113 -0
  16. package/lib/main/state-machine/state-machine.js +133 -0
  17. package/lib/main/state-machine/state-machine.types.d.ts +977 -0
  18. package/lib/main/state-machine/state-machine.types.js +7 -0
  19. package/lib/resolver/index.d.ts +1 -0
  20. package/lib/resolver/index.js +17 -0
  21. package/lib/resolver/resolver.d.ts +6 -0
  22. package/lib/resolver/resolver.js +28 -0
  23. package/lib/resolver/state-machine/schema/schema.d.ts +34 -0
  24. package/lib/resolver/state-machine/schema/schema.js +431 -0
  25. package/lib/resolver/state-machine/schema/schema.types.d.ts +149 -0
  26. package/lib/resolver/state-machine/schema/schema.types.js +2 -0
  27. package/lib/resolver/state-machine/schema/schema.utils.d.ts +15 -0
  28. package/lib/resolver/state-machine/schema/schema.utils.js +53 -0
  29. package/lib/resolver/state-machine/state-machine.d.ts +19 -0
  30. package/lib/resolver/state-machine/state-machine.js +96 -0
  31. package/lib/resolver/state-machine/state-machine.types.d.ts +7 -0
  32. package/lib/resolver/state-machine/state-machine.types.js +2 -0
  33. package/package.json +87 -0
@@ -0,0 +1,15 @@
1
+ import { LambdaHandler } from '@lafken/resolver';
2
+ import type { ExecutionSource, IntegrationMode, StateMachineSource, StateSource, TaskSource } from '../../../main';
3
+ export declare const mapSourceExecution: Record<ExecutionSource, string>;
4
+ export declare const mapSourceStateMachine: Record<StateMachineSource, string>;
5
+ export declare const mapSourceState: Record<StateSource, string>;
6
+ export declare const mapSourceTask: Record<TaskSource, string>;
7
+ export declare const mapIntegrationMode: Record<IntegrationMode, string>;
8
+ export declare class StateNames {
9
+ private nameCount;
10
+ createName(name: string): string;
11
+ }
12
+ export declare class LambdaStates {
13
+ private lambdas;
14
+ createLambda([scope, id, props]: ConstructorParameters<typeof LambdaHandler>): LambdaHandler;
15
+ }
@@ -0,0 +1,53 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.LambdaStates = exports.StateNames = exports.mapIntegrationMode = exports.mapSourceTask = exports.mapSourceState = exports.mapSourceStateMachine = exports.mapSourceExecution = void 0;
4
+ const resolver_1 = require("@lafken/resolver");
5
+ exports.mapSourceExecution = {
6
+ id: 'Id',
7
+ name: 'Name',
8
+ role_arn: 'RoleArn',
9
+ redrive_count: 'RedriveCount',
10
+ redrive_time: 'RedriveTime',
11
+ start_time: 'StartTime',
12
+ };
13
+ exports.mapSourceStateMachine = {
14
+ id: 'Id',
15
+ name: 'Name',
16
+ };
17
+ exports.mapSourceState = {
18
+ entered_time: 'EnteredTime',
19
+ retry_count: 'Name',
20
+ name: 'Name',
21
+ };
22
+ exports.mapSourceTask = {
23
+ token: 'Token',
24
+ };
25
+ exports.mapIntegrationMode = {
26
+ token: 'waitForTaskToken',
27
+ sync: 'sync:2',
28
+ async: 'async',
29
+ };
30
+ class StateNames {
31
+ nameCount = {};
32
+ createName(name) {
33
+ this.nameCount[name] ??= 0;
34
+ this.nameCount[name]++;
35
+ if (this.nameCount[name] === 1) {
36
+ return name;
37
+ }
38
+ return `${name}-${this.nameCount[name]}`;
39
+ }
40
+ }
41
+ exports.StateNames = StateNames;
42
+ class LambdaStates {
43
+ lambdas = {};
44
+ createLambda([scope, id, props]) {
45
+ if (this.lambdas[id]) {
46
+ return this.lambdas[id];
47
+ }
48
+ const lambda = new resolver_1.LambdaHandler(scope, id, props);
49
+ this.lambdas[id] = lambda;
50
+ return lambda;
51
+ }
52
+ }
53
+ exports.LambdaStates = LambdaStates;
@@ -0,0 +1,19 @@
1
+ import { SfnStateMachine } from '@cdktn/provider-aws/lib/sfn-state-machine';
2
+ import { type AppModule } from '@lafken/resolver';
3
+ import type { StateMachineProps } from './state-machine.types';
4
+ declare const StateMachine_base: (new (...args: any[]) => {
5
+ isGlobal(module: import("@lafken/common").ModuleGlobalReferenceNames | (string & {}), id: string): void;
6
+ isDependent(resolveDependency: () => void): void;
7
+ readonly node: import("constructs").Node;
8
+ with(...mixins: import("constructs").IMixin[]): import("constructs").IConstruct;
9
+ toString(): string;
10
+ }) & typeof SfnStateMachine;
11
+ export declare class StateMachine extends StateMachine_base {
12
+ private props;
13
+ constructor(scope: AppModule, id: string, props: StateMachineProps);
14
+ attachDefinition(): Promise<void>;
15
+ private overrideDefinition;
16
+ private overrideRole;
17
+ private addLoggingConfiguration;
18
+ }
19
+ export {};
@@ -0,0 +1,96 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.StateMachine = void 0;
4
+ const cloudwatch_log_group_1 = require("@cdktn/provider-aws/lib/cloudwatch-log-group");
5
+ const sfn_state_machine_1 = require("@cdktn/provider-aws/lib/sfn-state-machine");
6
+ const resolver_1 = require("@lafken/resolver");
7
+ const schema_1 = require("./schema/schema");
8
+ class StateMachine extends resolver_1.lafkenResource.make(sfn_state_machine_1.SfnStateMachine) {
9
+ props;
10
+ constructor(scope, id, props) {
11
+ const { resourceMetadata } = props;
12
+ super(scope, `${id}-state-machine`, {
13
+ name: resourceMetadata.name,
14
+ roleArn: '',
15
+ definition: '',
16
+ tracingConfiguration: resourceMetadata.enableTrace
17
+ ? {
18
+ enabled: true,
19
+ }
20
+ : undefined,
21
+ });
22
+ this.props = props;
23
+ this.isGlobal(scope.id, `state-machine::${id}`);
24
+ this.addLoggingConfiguration(resourceMetadata);
25
+ new resolver_1.ResourceOutput(this, resourceMetadata.outputs);
26
+ }
27
+ async attachDefinition() {
28
+ const { classResource } = this.props;
29
+ const schema = new schema_1.Schema(this, classResource);
30
+ const definition = await schema.getDefinition();
31
+ this.overrideRole(schema);
32
+ if (!schema.hasUnresolvedDependency) {
33
+ this.overrideDefinition(definition);
34
+ }
35
+ if (schema.hasUnresolvedDependency) {
36
+ this.isDependent(async () => {
37
+ this.overrideDefinition(await schema.resolveArguments(definition));
38
+ });
39
+ }
40
+ }
41
+ overrideDefinition(definition) {
42
+ const { resourceMetadata } = this.props;
43
+ this.addOverride('definition', JSON.stringify({
44
+ ...definition,
45
+ QueryLanguage: 'JSONata',
46
+ ExecutionType: resourceMetadata.executionType
47
+ ? resourceMetadata.executionType.toUpperCase()
48
+ : undefined,
49
+ }));
50
+ }
51
+ overrideRole(schema) {
52
+ const { resourceMetadata, moduleName } = this.props;
53
+ const permissions = {
54
+ read: ['GetObject', 'ListBucket'],
55
+ write: ['GetObject', 'ListBucket', 'PutObject'],
56
+ };
57
+ const bucketServices = Object.entries(schema.buckets).map(([bucket, permission]) => {
58
+ return {
59
+ type: 's3',
60
+ permissions: permissions[permission],
61
+ resources: [`arn:aws:s3:::${bucket}`, `arn:aws:s3:::${bucket}/*`],
62
+ };
63
+ });
64
+ const roleName = `${resourceMetadata.name}-${moduleName}-role`;
65
+ const role = new resolver_1.Role(this, roleName, {
66
+ name: roleName,
67
+ services: ({ getResourceValue, getSSMValue }) => {
68
+ const basePermissions = ['cloudwatch', 'lambda', ...bucketServices];
69
+ if (typeof resourceMetadata.services === 'function') {
70
+ return [
71
+ ...basePermissions,
72
+ ...resourceMetadata.services({ getResourceValue, getSSMValue }),
73
+ ];
74
+ }
75
+ return [...basePermissions, ...(resourceMetadata.services || [])];
76
+ },
77
+ principal: 'states.amazonaws.com',
78
+ });
79
+ this.addOverride('role_arn', role.arn);
80
+ }
81
+ addLoggingConfiguration(resourceMetadata) {
82
+ if (!resourceMetadata.loggingConfiguration) {
83
+ return;
84
+ }
85
+ const logGroup = new cloudwatch_log_group_1.CloudwatchLogGroup(this, 'sfn-logs', {
86
+ name: resourceMetadata.loggingConfiguration.logGroupName,
87
+ retentionInDays: resourceMetadata.loggingConfiguration.retentionInDays,
88
+ });
89
+ this.putLoggingConfiguration({
90
+ includeExecutionData: resourceMetadata.loggingConfiguration.includeExecutionData,
91
+ level: resourceMetadata.loggingConfiguration.level?.toUpperCase(),
92
+ logDestination: `${logGroup.arn}:*`,
93
+ });
94
+ }
95
+ }
96
+ exports.StateMachine = StateMachine;
@@ -0,0 +1,7 @@
1
+ import type { ClassResource } from '@lafken/common';
2
+ import type { StateMachineResourceMetadata } from '../../main';
3
+ export interface StateMachineProps {
4
+ classResource: ClassResource;
5
+ resourceMetadata: StateMachineResourceMetadata;
6
+ moduleName: string;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json ADDED
@@ -0,0 +1,87 @@
1
+ {
2
+ "name": "@lafken/state-machine",
3
+ "version": "0.10.1",
4
+ "private": false,
5
+ "description": "Build Step Functions workflows with TypeScript decorators - declarative orchestration with Lafken",
6
+ "keywords": [
7
+ "aws",
8
+ "step functions",
9
+ "state machine",
10
+ "workflow",
11
+ "orchestration",
12
+ "serverless",
13
+ "typescript",
14
+ "decorators",
15
+ "lafken"
16
+ ],
17
+ "homepage": "https://github.com/Hero64/lafken#readme",
18
+ "bugs": "https://github.com/Hero64/lafken/issues",
19
+ "repository": {
20
+ "type": "git",
21
+ "url": "https://github.com/Hero64/lafken",
22
+ "directory": "packages/state-machine"
23
+ },
24
+ "license": "MIT",
25
+ "author": "Aníbal Jorquera",
26
+ "exports": {
27
+ "./main": {
28
+ "import": "./lib/main/index.js",
29
+ "types": "./lib/main/index.d.ts",
30
+ "require": "./lib/main/index.js"
31
+ },
32
+ "./resolver": {
33
+ "import": "./lib/resolver/index.js",
34
+ "types": "./lib/resolver/index.d.ts",
35
+ "require": "./lib/resolver/index.js"
36
+ }
37
+ },
38
+ "typesVersions": {
39
+ "*": {
40
+ "main": [
41
+ "./lib/main/index.d.ts"
42
+ ],
43
+ "resolver": [
44
+ "./lib/resolver/index.d.ts"
45
+ ]
46
+ }
47
+ },
48
+ "files": [
49
+ "lib"
50
+ ],
51
+ "dependencies": {
52
+ "reflect-metadata": "^0.2.2",
53
+ "@lafken/resolver": "0.10.1"
54
+ },
55
+ "devDependencies": {
56
+ "@cdktn/provider-aws": "^23.5.0",
57
+ "@swc/core": "^1.15.21",
58
+ "@swc/helpers": "^0.5.20",
59
+ "@vitest/runner": "^4.1.2",
60
+ "cdktn": "^0.22.1",
61
+ "cdktn-vitest": "^1.0.0",
62
+ "constructs": "^10.6.0",
63
+ "typescript": "6.0.2",
64
+ "unplugin-swc": "^1.5.9",
65
+ "vitest": "^4.1.2",
66
+ "@lafken/common": "0.10.1"
67
+ },
68
+ "peerDependencies": {
69
+ "@cdktn/provider-aws": ">=23.0.0",
70
+ "cdktn": ">=0.22.0",
71
+ "constructs": "^10.4.5",
72
+ "@lafken/common": "0.10.1"
73
+ },
74
+ "engines": {
75
+ "node": ">=20.19"
76
+ },
77
+ "publishConfig": {
78
+ "access": "public"
79
+ },
80
+ "scripts": {
81
+ "build": "pnpm clean && tsc -p ./tsconfig.build.json",
82
+ "check-types": "tsc --noEmit -p ./tsconfig.build.json",
83
+ "clean": "rm -rf ./lib",
84
+ "dev": "tsc -w",
85
+ "test": "vitest"
86
+ }
87
+ }