@flow-as-code/cdk 0.1.0

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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 The flow-as-code Authors
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md ADDED
@@ -0,0 +1,96 @@
1
+ # @flow-as-code/cdk
2
+
3
+ ```
4
+ npm i @flow-as-code/cdk
5
+ ```
6
+
7
+ Apache-2.0, Node 22.12 or newer. Peers on `aws-cdk-lib` and `constructs`, both
8
+ optional so a CLI-only install does not fetch them.
9
+
10
+ CDK binding for `@flow-as-code/core`. Two exports, plus a scaffold generator on a subpath:
11
+
12
+ - `TokenBinder`: an interface you implement, mapping reference names to construct attributes (`queue.attrQueueArn`, `fn.functionArn`, a Lex alias ARN). `@flow-as-code/core`'s `materializeWithBinder` inserts the returned strings byte-for-byte, so CDK tokens pass through and CloudFormation resolves them per account at deploy time. No resource map, no literal ARNs in this path.
13
+ - `FlowSet`: a construct consuming a directory of `*.flowdoc.json` files (or `FlowDoc[]`) plus a `TokenBinder`. Flows become `AWS::Connect::ContactFlow` (type from `connectType`), modules become `AWS::Connect::ContactFlowModule`, created in dependency order.
14
+
15
+ ## Usage
16
+
17
+ ```ts
18
+ import { FlowSet } from "@flow-as-code/cdk";
19
+
20
+ new FlowSet(stack, "Flows", {
21
+ instanceArn: instance.attrArn,
22
+ source: "flows/", // or FlowDoc[]
23
+ binder: {
24
+ queue: (name) => queues[name].attrQueueArn,
25
+ hours: (name) => hours[name].attrHoursOfOperationArn,
26
+ lambda: (name) => fns[name].functionArn,
27
+ lex: (name) => lexAliases[name].attrBotAliasArn,
28
+ prompt: (name) => prompts[name].attrPromptArn,
29
+ // flow?: only needed when a doc uses ${cdref:flow:...}
30
+ },
31
+ });
32
+ ```
33
+
34
+ Module references (`${cdref:module:name@alias}`) are not the binder's job: `FlowSet` resolves them itself, to the alias ARN of the module it manages.
35
+
36
+ ## The scaffold (`@flow-as-code/cdk/scaffold`)
37
+
38
+ Writing that stack by hand starts with the same file every time, so `cdkScaffold({ docs, source })` generates it: a `FlowStack` constructing a `FlowSet` over the FlowDoc directory, and a `TokenBinder` carrying one `TODO` per reference type the documents actually use, with the referenced names listed. Types the set does not use still get a method, because the interface requires them, but theirs throws instead of carrying a TODO.
39
+
40
+ The subpath exists because two tools emit this file and must emit it identically: `flow-cli emit --target cdk` and the studio's CDK export button. It is pure and imports no node builtin, so the studio can bundle it for a browser; `source` is the FLOW_DOCS expression (`.`, `..`), which is the one thing that needs a path calculation, and `sourceForDepth` computes it where node's `path.relative` is unavailable. The emitted file resolves that expression against itself with `new URL(source, import.meta.url)`, so the stack reads the same directory whatever the CDK app's working directory is. Output is byte-stable and independent of the order the documents were read.
41
+
42
+ ## Peer dependencies
43
+
44
+ `aws-cdk-lib ^2.267.0` and `constructs ^10.8.1`, both optional. A project that
45
+ has them pinned below that floor gets an ERESOLVE from npm 7 and later rather
46
+ than a silent mismatch; a project that has neither installs neither. The floor
47
+ is deliberate: `FlowSet` builds `CfnContactFlowModuleVersion` and
48
+ `CfnContactFlowModuleAlias` (see the versioning model below), which are recent
49
+ additions to `aws-cdk-lib/aws-connect`, and 2.267 is the version this package is
50
+ developed and tested against. Lowering it means finding the release that
51
+ introduced both L1s and running the `aws-cdk-lib/assertions` tests against it,
52
+ not just widening the range.
53
+
54
+ They are optional because the CDK entry is not the only entry. `@flow-as-code/cli` and
55
+ the studio reach this package for `@flow-as-code/cdk/scaffold` alone,
56
+ which imports nothing but `@flow-as-code/core`: `aws-cdk-lib` appears in that module as a
57
+ line of the stack source it generates, not as an import. Non-optional peers are
58
+ auto-installed, so with these declared as ordinary peers every
59
+ `npm install @flow-as-code/cli` would fetch 166 MB of CDK that nothing in that
60
+ install loads. Importing `FlowSet` or the
61
+ binder still needs both packages present, and `tests/packaging.test.ts` holds
62
+ the scaffold subpath to its one bare import.
63
+
64
+ The cost of optional is that npm says nothing at install time, so the package
65
+ says it itself. Importing `@flow-as-code/cdk` with `aws-cdk-lib`
66
+ absent throws:
67
+
68
+ ```
69
+ @flow-as-code/cdk needs the CDK at runtime, but aws-cdk-lib is not
70
+ installed. It and constructs are optional peers so that a CLI-only install
71
+ stays small, which means npm does not warn about them at install time. Install
72
+ them with: npm install aws-cdk-lib@^2.267.0 constructs@^10.8.1
73
+ ```
74
+
75
+ The entry checks for the peer before it loads `FlowSet`, so this replaces the
76
+ bare `ERR_MODULE_NOT_FOUND` that used to come out of `dist/flow-set.js`. The
77
+ `./scaffold` subpath is unaffected and imports with no CDK installed.
78
+ `src/index.test.ts` runs the entry in a directory with no `node_modules` and
79
+ asserts the message, including the version floor read from `package.json`.
80
+
81
+ ## Versioning model
82
+
83
+ - Modules are versioned. Each module gets an `AWS::Connect::ContactFlowModuleVersion` whose logical ID embeds a hash of the module's canonical content, so a content change publishes a new immutable version, plus an `AWS::Connect::ContactFlowModuleAlias` per alias name the doc set pins (default `live`) with a stable logical ID that repoints to the new version. Flows reference the alias ARN, so repointing changes no flow content.
84
+ - Full flows are not versioned; content updates in place. Connect's `CreateContactFlowVersion` states "This API only supports creating versions for flows of type Campaign." (https://docs.aws.amazon.com/connect/latest/APIReference/API_CreateContactFlowVersion.html, verified 2026-08-31)
85
+
86
+ ## Validation
87
+
88
+ `FlowSet` refuses, at synth time: duplicate document names, `kind`/`connectType` mismatches, references to modules not in the set, module reference cycles, and any document failing a hard `@flow-as-code/core` lint rule (`no-literal-arn`, `no-unresolved-token`). Binder gaps fail with an error naming the token, the document, and the missing method.
89
+
90
+ No instance/queue provisioning constructs here; users bring their own or use L1s.
91
+
92
+ ## Testing
93
+
94
+ Offline tests assert on synthesized templates with `aws-cdk-lib/assertions`. The live-deploy test in `src/integration.test.ts` runs only when both `FLOW_TEST_INSTANCE_ARN` and `FLOW_TEST_DEPLOY=1` are set, deploys the demo flow plus a versioned module to that instance through the AWS CLI, and tears the stack down. It writes to the account, so naming an instance is not enough on its own: use an empty sandbox.
95
+
96
+ That test has run green against a live Connect instance (2026-09-02). The test asserts only that the deploy exited zero, and it deletes the stack in its `finally`, so the content check was a separate step: the same synthesized template was deployed again and left up, and its flows were read back with `DescribeContactFlow` and `DescribeContactFlowModule` and compared against what the FlowDocs materialize to. That comparison is structural rather than byte-for-byte, because deploying resolves each `${cdref:...}` reference to a real ARN: every other position matched exactly, and the reference positions were checked against the stack's own resources instead, each resolving to a resource of the right kind and the module reference resolving to the alias rather than the version. The stack needs no CDK bootstrap: it is asset-free and uses `CliCredentialsStackSynthesizer`. `tasks/A07-flow-cdk.md` records both.
@@ -0,0 +1,38 @@
1
+ import type { RefEntry } from "@flow-as-code/core";
2
+ /**
3
+ * Maps reference names to CloudFormation-token strings. Implemented by the
4
+ * user, typically as thin closures over constructs in the same stack.
5
+ *
6
+ * `flow` is optional because cross-flow references are rare; a doc set that
7
+ * uses `${cdref:flow:...}` against a binder without `flow()` fails synth with
8
+ * an error naming the token.
9
+ */
10
+ /**
11
+ * Boundary worth knowing: a binder may return any opaque string and it passes
12
+ * through byte-exact, EXCEPT that CDK itself parses `${Token[...]}` markers.
13
+ * Returning a string containing an unregistered marker such as
14
+ * `${Token[TOKEN.999]}` fails deep inside aws-cdk-lib with "Unrecognized token
15
+ * key". Real tokens (Fn.importValue, attr getters) are registered and fine.
16
+ * This is CDK's behavior, not something this package can intercept.
17
+ */
18
+ export interface TokenBinder {
19
+ /** ARN of the queue, e.g. `queue.attrQueueArn`. */
20
+ queue(name: string): string;
21
+ /** ARN of the hours of operation, e.g. `hours.attrHoursOfOperationArn`. */
22
+ hours(name: string): string;
23
+ /** ARN of the Lambda function, e.g. `fn.functionArn`. */
24
+ lambda(name: string): string;
25
+ /** ARN of the Lex bot alias. */
26
+ lex(name: string): string;
27
+ /** ARN of the prompt. */
28
+ prompt(name: string): string;
29
+ /** ARN of a contact flow not managed by this FlowSet. */
30
+ flow?(name: string): string;
31
+ }
32
+ /**
33
+ * Resolves one non-module reference through the binder. Throws a diagnostic
34
+ * naming the token, the document, and the missing method when the binder
35
+ * cannot answer, so the failure is actionable without a stack-trace dig.
36
+ */
37
+ export declare function bindRef(binder: TokenBinder, ref: RefEntry, docName: string): string;
38
+ //# sourceMappingURL=binder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binder.d.ts","sourceRoot":"","sources":["../src/binder.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,oBAAoB,CAAC;AAEnD;;;;;;;GAOG;AACH;;;;;;;GAOG;AACH,MAAM,WAAW,WAAW;IAC1B,mDAAmD;IACnD,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,2EAA2E;IAC3E,KAAK,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC5B,yDAAyD;IACzD,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC7B,gCAAgC;IAChC,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC1B,yBAAyB;IACzB,MAAM,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;IAC7B,yDAAyD;IACzD,IAAI,CAAC,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAAC;CAC7B;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,MAAM,EAAE,WAAW,EAAE,GAAG,EAAE,QAAQ,EAAE,OAAO,EAAE,MAAM,GAAG,MAAM,CAqBnF"}
package/dist/binder.js ADDED
@@ -0,0 +1,40 @@
1
+ /*
2
+ * Copyright 2026 The flow-as-code Authors
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ // TokenBinder: the user's side of CDK materialization.
6
+ //
7
+ // A binder maps each non-module reference in a FlowDoc to a string, normally a
8
+ // CloudFormation token from an L1/L2 construct attribute (queue.attrQueueArn,
9
+ // fn.functionArn, a Lex alias ARN). @flow-as-code/core's materializeWithBinder inserts
10
+ // the returned string byte-for-byte, so CDK tokens survive untouched and
11
+ // CloudFormation resolves them per account at deploy time. No resource map, no
12
+ // literal ARNs in this path.
13
+ //
14
+ // Module references (`${cdref:module:name@alias}`) are deliberately absent:
15
+ // FlowSet manages the module resources itself and resolves those tokens to the
16
+ // alias ARN of the CfnContactFlowModuleAlias it created (see flow-set.ts).
17
+ /**
18
+ * Resolves one non-module reference through the binder. Throws a diagnostic
19
+ * naming the token, the document, and the missing method when the binder
20
+ * cannot answer, so the failure is actionable without a stack-trace dig.
21
+ */
22
+ export function bindRef(binder, ref, docName) {
23
+ if (ref.type === "module") {
24
+ // FlowSet resolves module refs itself; reaching here is a programming
25
+ // error in the caller, not a binder gap.
26
+ throw new Error(`Internal: module ref ${ref.token} must be resolved by FlowSet, not a binder.`);
27
+ }
28
+ const method = binder[ref.type];
29
+ if (typeof method !== "function") {
30
+ throw new Error(`TokenBinder has no ${ref.type}() method, but "${docName}" contains ${ref.token}. ` +
31
+ `Implement ${ref.type}(name) on the binder.`);
32
+ }
33
+ const value = method.call(binder, ref.name);
34
+ if (typeof value !== "string") {
35
+ throw new Error(`TokenBinder.${ref.type}("${ref.name}") returned ${String(value)} for ${ref.token} ` +
36
+ `in "${docName}". Binder methods must return a string.`);
37
+ }
38
+ return value;
39
+ }
40
+ //# sourceMappingURL=binder.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"binder.js","sourceRoot":"","sources":["../src/binder.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,uDAAuD;AACvD,EAAE;AACF,+EAA+E;AAC/E,8EAA8E;AAC9E,uFAAuF;AACvF,yEAAyE;AACzE,+EAA+E;AAC/E,6BAA6B;AAC7B,EAAE;AACF,4EAA4E;AAC5E,+EAA+E;AAC/E,2EAA2E;AAmC3E;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,MAAmB,EAAE,GAAa,EAAE,OAAe;IACzE,IAAI,GAAG,CAAC,IAAI,KAAK,QAAQ,EAAE,CAAC;QAC1B,sEAAsE;QACtE,yCAAyC;QACzC,MAAM,IAAI,KAAK,CAAC,wBAAwB,GAAG,CAAC,KAAK,6CAA6C,CAAC,CAAC;IAClG,CAAC;IACD,MAAM,MAAM,GAAG,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IAChC,IAAI,OAAO,MAAM,KAAK,UAAU,EAAE,CAAC;QACjC,MAAM,IAAI,KAAK,CACb,sBAAsB,GAAG,CAAC,IAAI,mBAAmB,OAAO,cAAc,GAAG,CAAC,KAAK,IAAI;YACjF,aAAa,GAAG,CAAC,IAAI,uBAAuB,CAC/C,CAAC;IACJ,CAAC;IACD,MAAM,KAAK,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC;IAC5C,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,MAAM,IAAI,KAAK,CACb,eAAe,GAAG,CAAC,IAAI,KAAK,GAAG,CAAC,IAAI,eAAe,MAAM,CAAC,KAAK,CAAC,QAAQ,GAAG,CAAC,KAAK,GAAG;YAClF,OAAO,OAAO,yCAAyC,CAC1D,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC"}
@@ -0,0 +1,35 @@
1
+ import { type FlowDoc } from "@flow-as-code/core";
2
+ import { CfnContactFlow, CfnContactFlowModule, CfnContactFlowModuleAlias } from "aws-cdk-lib/aws-connect";
3
+ import { Construct } from "constructs";
4
+ import { type TokenBinder } from "./binder.js";
5
+ /** Alias created for a module no flow pins to a named alias. */
6
+ export declare const DEFAULT_MODULE_ALIAS = "live";
7
+ export interface FlowSetProps {
8
+ /** ARN of the Connect instance the flows deploy into. */
9
+ instanceArn: string;
10
+ /**
11
+ * Either a directory containing `*.flowdoc.json` files (read at synth time,
12
+ * sorted by file name) or the documents themselves.
13
+ */
14
+ source: string | FlowDoc[];
15
+ /** Resolves non-module references to CloudFormation-token strings. */
16
+ binder: TokenBinder;
17
+ }
18
+ /**
19
+ * Creates every flow and module in a FlowDoc set on a Connect instance, with
20
+ * references resolved to CloudFormation tokens: non-module refs through the
21
+ * user's TokenBinder, module refs to the alias ARN of the module this
22
+ * construct manages.
23
+ */
24
+ export declare class FlowSet extends Construct {
25
+ /** Created contact flows, keyed by document name. */
26
+ readonly flows: ReadonlyMap<string, CfnContactFlow>;
27
+ /** Created modules, keyed by document name. */
28
+ readonly modules: ReadonlyMap<string, CfnContactFlowModule>;
29
+ /** Created module aliases, keyed by `name@alias`. */
30
+ readonly moduleAliases: ReadonlyMap<string, CfnContactFlowModuleAlias>;
31
+ constructor(scope: Construct, id: string, props: FlowSetProps);
32
+ /** Refuses duplicate names, kind mismatches, and hard lint findings. */
33
+ private validateDocs;
34
+ }
35
+ //# sourceMappingURL=flow-set.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"flow-set.d.ts","sourceRoot":"","sources":["../src/flow-set.ts"],"names":[],"mappings":"AAkCA,OAAO,EAOL,KAAK,OAAO,EAEb,MAAM,oBAAoB,CAAC;AAC5B,OAAO,EACL,cAAc,EACd,oBAAoB,EACpB,yBAAyB,EAE1B,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,SAAS,EAAE,MAAM,YAAY,CAAC;AAEvC,OAAO,EAAW,KAAK,WAAW,EAAE,MAAM,aAAa,CAAC;AAExD,gEAAgE;AAChE,eAAO,MAAM,oBAAoB,SAAS,CAAC;AAE3C,MAAM,WAAW,YAAY;IAC3B,yDAAyD;IACzD,WAAW,EAAE,MAAM,CAAC;IACpB;;;OAGG;IACH,MAAM,EAAE,MAAM,GAAG,OAAO,EAAE,CAAC;IAC3B,sEAAsE;IACtE,MAAM,EAAE,WAAW,CAAC;CACrB;AAqDD;;;;;GAKG;AACH,qBAAa,OAAQ,SAAQ,SAAS;IACpC,qDAAqD;IACrD,QAAQ,CAAC,KAAK,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,CAAC;IACpD,+CAA+C;IAC/C,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,oBAAoB,CAAC,CAAC;IAC5D,qDAAqD;IACrD,QAAQ,CAAC,aAAa,EAAE,WAAW,CAAC,MAAM,EAAE,yBAAyB,CAAC,CAAC;gBAE3D,KAAK,EAAE,SAAS,EAAE,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,YAAY;IAgH7D,wEAAwE;IACxE,OAAO,CAAC,YAAY;CA2BrB"}
@@ -0,0 +1,225 @@
1
+ /*
2
+ * Copyright 2026 The flow-as-code Authors
3
+ * SPDX-License-Identifier: Apache-2.0
4
+ */
5
+ // FlowSet: a construct that turns a set of FlowDocs into deployable
6
+ // AWS::Connect resources.
7
+ //
8
+ // Flows become AWS::Connect::ContactFlow and modules become
9
+ // AWS::Connect::ContactFlowModule, with content produced by @flow-as-code/core's
10
+ // materializeWithBinder and serializeContent so the deployed JSON is
11
+ // byte-stable for the same inputs.
12
+ // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-connect-contactflow.html
13
+ // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-connect-contactflowmodule.html
14
+ //
15
+ // Versioning model:
16
+ // - Modules are versioned. Each module gets an AWS::Connect::ContactFlowModuleVersion
17
+ // whose logical ID embeds a hash of the module's canonical content, so a
18
+ // content change replaces the version resource and publishes a new immutable
19
+ // version, and an AWS::Connect::ContactFlowModuleAlias per alias name used by
20
+ // the doc set (default "live") whose logical ID is stable and which repoints
21
+ // to the new version. Flows reference the ALIAS ARN, so repointing an alias
22
+ // to a new version changes no flow content.
23
+ // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-connect-contactflowmoduleversion.html
24
+ // https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-connect-contactflowmodulealias.html
25
+ // - Full flows are NOT versioned; their content updates in place. Connect's
26
+ // CreateContactFlowVersion API states: "This API only supports creating
27
+ // versions for flows of type Campaign."
28
+ // https://docs.aws.amazon.com/connect/latest/APIReference/API_CreateContactFlowVersion.html
29
+ // (verified 2026-08-31)
30
+ import { createHash } from "node:crypto";
31
+ import { readFileSync, readdirSync } from "node:fs";
32
+ import { join } from "node:path";
33
+ import { allRules, collectRefs, lint, materializeWithBinder, serializeContent, toText, } from "@flow-as-code/core";
34
+ import { CfnContactFlow, CfnContactFlowModule, CfnContactFlowModuleAlias, CfnContactFlowModuleVersion, } from "aws-cdk-lib/aws-connect";
35
+ import { Construct } from "constructs";
36
+ import { bindRef } from "./binder.js";
37
+ /** Alias created for a module no flow pins to a named alias. */
38
+ export const DEFAULT_MODULE_ALIAS = "live";
39
+ /** `name@alias` key for a module alias resource. */
40
+ const aliasKey = (name, alias) => `${name}@${alias ?? DEFAULT_MODULE_ALIAS}`;
41
+ function loadDocs(source) {
42
+ if (Array.isArray(source))
43
+ return [...source];
44
+ const files = readdirSync(source)
45
+ .filter((f) => f.endsWith(".flowdoc.json"))
46
+ .sort();
47
+ if (files.length === 0) {
48
+ throw new Error(`FlowSet source directory "${source}" contains no *.flowdoc.json files.`);
49
+ }
50
+ return files.map((f) => JSON.parse(readFileSync(join(source, f), "utf8")));
51
+ }
52
+ /**
53
+ * Modules ordered so every module is created after the modules it references
54
+ * ("up to five levels" of nesting, per the flow language contract). Stable:
55
+ * ties break on name. Throws on a reference cycle, which Connect could never
56
+ * execute anyway.
57
+ */
58
+ function topoSortModules(modules) {
59
+ const byName = new Map(modules.map((m) => [m.name, m]));
60
+ const deps = new Map(modules.map((m) => [
61
+ m.name,
62
+ collectRefs(m.content)
63
+ .filter((r) => r.type === "module" && byName.has(r.name))
64
+ .map((r) => r.name)
65
+ .sort(),
66
+ ]));
67
+ const done = new Set();
68
+ const visiting = new Set();
69
+ const ordered = [];
70
+ const visit = (name, path) => {
71
+ if (done.has(name))
72
+ return;
73
+ if (visiting.has(name)) {
74
+ throw new Error(`Module reference cycle: ${[...path, name].join(" -> ")}.`);
75
+ }
76
+ visiting.add(name);
77
+ for (const dep of deps.get(name) ?? [])
78
+ visit(dep, [...path, name]);
79
+ visiting.delete(name);
80
+ done.add(name);
81
+ ordered.push(byName.get(name));
82
+ };
83
+ for (const m of [...modules].sort((a, b) => a.name.localeCompare(b.name)))
84
+ visit(m.name, []);
85
+ return ordered;
86
+ }
87
+ /**
88
+ * Creates every flow and module in a FlowDoc set on a Connect instance, with
89
+ * references resolved to CloudFormation tokens: non-module refs through the
90
+ * user's TokenBinder, module refs to the alias ARN of the module this
91
+ * construct manages.
92
+ */
93
+ export class FlowSet extends Construct {
94
+ /** Created contact flows, keyed by document name. */
95
+ flows;
96
+ /** Created modules, keyed by document name. */
97
+ modules;
98
+ /** Created module aliases, keyed by `name@alias`. */
99
+ moduleAliases;
100
+ constructor(scope, id, props) {
101
+ super(scope, id);
102
+ const docs = loadDocs(props.source);
103
+ this.validateDocs(docs);
104
+ const moduleDocs = docs.filter((d) => d.kind === "module");
105
+ const flowDocs = docs
106
+ .filter((d) => d.kind === "flow")
107
+ .sort((a, b) => a.name.localeCompare(b.name));
108
+ const moduleNames = new Set(moduleDocs.map((d) => d.name));
109
+ // Which aliases each module needs: every alias the doc set pins, plus the
110
+ // default so an unreferenced module still deploys with a usable alias.
111
+ const wanted = new Map(moduleDocs.map((d) => [d.name, new Set()]));
112
+ for (const doc of docs) {
113
+ for (const ref of collectRefs(doc.content)) {
114
+ if (ref.type !== "module")
115
+ continue;
116
+ if (!moduleNames.has(ref.name)) {
117
+ throw new Error(`"${doc.name}" references ${ref.token}, but no module named "${ref.name}" is in this FlowSet.`);
118
+ }
119
+ wanted.get(ref.name).add(ref.alias ?? DEFAULT_MODULE_ALIAS);
120
+ }
121
+ }
122
+ for (const aliases of wanted.values()) {
123
+ if (aliases.size === 0)
124
+ aliases.add(DEFAULT_MODULE_ALIAS);
125
+ }
126
+ const flows = new Map();
127
+ const modules = new Map();
128
+ const aliases = new Map();
129
+ const materializeDoc = (doc) => {
130
+ const used = [];
131
+ const content = materializeWithBinder(doc, (ref) => {
132
+ if (ref.type !== "module")
133
+ return bindRef(props.binder, ref, doc.name);
134
+ const alias = aliases.get(aliasKey(ref.name, ref.alias));
135
+ if (alias === undefined) {
136
+ // Unreachable for modules (topological order) and flows (created
137
+ // after every module); kept as a guard with a real message.
138
+ throw new Error(`Internal: alias for ${ref.token} not yet created (doc "${doc.name}").`);
139
+ }
140
+ used.push(alias);
141
+ return alias.attrContactFlowModuleAliasArn;
142
+ });
143
+ return { content: serializeContent(content), used };
144
+ };
145
+ // Modules first, dependency-ordered, each with its version and aliases.
146
+ for (const doc of topoSortModules(moduleDocs)) {
147
+ const { content, used } = materializeDoc(doc);
148
+ const module = new CfnContactFlowModule(this, `Module-${doc.name}`, {
149
+ instanceArn: props.instanceArn,
150
+ name: doc.name,
151
+ content,
152
+ });
153
+ for (const dep of used)
154
+ module.addResourceDependency(dep);
155
+ modules.set(doc.name, module);
156
+ // The version's logical ID embeds the canonical content hash (computed
157
+ // over the doc with its own tokens left in place, so it is stable across
158
+ // processes): a content change replaces the resource, publishing a new
159
+ // immutable version; the alias below keeps a stable logical ID and
160
+ // repoints. See the header comment for the versioning model.
161
+ const discriminator = createHash("sha256")
162
+ .update(serializeContent(materializeWithBinder(doc, (r) => r.token)))
163
+ .digest("hex")
164
+ .slice(0, 8);
165
+ const version = new CfnContactFlowModuleVersion(this, `Module-${doc.name}-Version-${discriminator}`, {
166
+ contactFlowModuleId: module.attrContactFlowModuleArn,
167
+ });
168
+ version.addResourceDependency(module);
169
+ for (const aliasName of [...wanted.get(doc.name)].sort()) {
170
+ const alias = new CfnContactFlowModuleAlias(this, `Module-${doc.name}-Alias-${aliasName}`, {
171
+ contactFlowModuleId: module.attrContactFlowModuleArn,
172
+ contactFlowModuleVersion: version.attrVersion,
173
+ name: aliasName,
174
+ });
175
+ alias.addResourceDependency(version);
176
+ aliases.set(aliasKey(doc.name, aliasName), alias);
177
+ }
178
+ }
179
+ // Then flows. Alias references inside content already imply the ordering;
180
+ // the explicit dependency keeps it true even if content is later composed
181
+ // differently.
182
+ for (const doc of flowDocs) {
183
+ const { content, used } = materializeDoc(doc);
184
+ const flow = new CfnContactFlow(this, `Flow-${doc.name}`, {
185
+ instanceArn: props.instanceArn,
186
+ name: doc.name,
187
+ type: doc.connectType,
188
+ content,
189
+ });
190
+ for (const dep of used)
191
+ flow.addResourceDependency(dep);
192
+ flows.set(doc.name, flow);
193
+ }
194
+ this.flows = flows;
195
+ this.modules = modules;
196
+ this.moduleAliases = aliases;
197
+ }
198
+ /** Refuses duplicate names, kind mismatches, and hard lint findings. */
199
+ validateDocs(docs) {
200
+ const seen = new Set();
201
+ const duplicates = new Set();
202
+ for (const d of docs) {
203
+ if (seen.has(d.name))
204
+ duplicates.add(d.name);
205
+ seen.add(d.name);
206
+ if (d.kind === "module" && d.connectType !== "MODULE") {
207
+ throw new Error(`Module "${d.name}" must have connectType MODULE, got ${d.connectType}.`);
208
+ }
209
+ if (d.kind === "flow" && d.connectType === "MODULE") {
210
+ throw new Error(`Flow "${d.name}" cannot have connectType MODULE.`);
211
+ }
212
+ }
213
+ if (duplicates.size > 0) {
214
+ throw new Error(`Duplicate document name(s) in FlowSet: ${[...duplicates].sort().join(", ")}.`);
215
+ }
216
+ // Hard rules block deployment exactly as they block a studio save:
217
+ // no-literal-arn and no-unresolved-token (@flow-as-code/core lint).
218
+ const hard = new Set(allRules.filter((r) => r.hard).map((r) => r.id));
219
+ const blocking = lint(docs).filter((f) => hard.has(f.rule));
220
+ if (blocking.length > 0) {
221
+ throw new Error(`FlowSet refused ${blocking.length} document(s):\n${toText(blocking)}`);
222
+ }
223
+ }
224
+ }
225
+ //# sourceMappingURL=flow-set.js.map