alchemy 2.0.0-beta.17 → 2.0.0-beta.18

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 (57) hide show
  1. package/bin/alchemy.js +1 -1
  2. package/bin/alchemy.js.map +1 -1
  3. package/lib/AWS/CloudFront/CachePolicy.d.ts +106 -0
  4. package/lib/AWS/CloudFront/CachePolicy.d.ts.map +1 -0
  5. package/lib/AWS/CloudFront/CachePolicy.js +147 -0
  6. package/lib/AWS/CloudFront/CachePolicy.js.map +1 -0
  7. package/lib/AWS/CloudFront/KeyGroup.d.ts +72 -0
  8. package/lib/AWS/CloudFront/KeyGroup.d.ts.map +1 -0
  9. package/lib/AWS/CloudFront/KeyGroup.js +136 -0
  10. package/lib/AWS/CloudFront/KeyGroup.js.map +1 -0
  11. package/lib/AWS/CloudFront/OriginRequestPolicy.d.ts +84 -0
  12. package/lib/AWS/CloudFront/OriginRequestPolicy.d.ts.map +1 -0
  13. package/lib/AWS/CloudFront/OriginRequestPolicy.js +139 -0
  14. package/lib/AWS/CloudFront/OriginRequestPolicy.js.map +1 -0
  15. package/lib/AWS/CloudFront/PublicKey.d.ts +72 -0
  16. package/lib/AWS/CloudFront/PublicKey.d.ts.map +1 -0
  17. package/lib/AWS/CloudFront/PublicKey.js +137 -0
  18. package/lib/AWS/CloudFront/PublicKey.js.map +1 -0
  19. package/lib/AWS/CloudFront/ResponseHeadersPolicy.d.ts +114 -0
  20. package/lib/AWS/CloudFront/ResponseHeadersPolicy.d.ts.map +1 -0
  21. package/lib/AWS/CloudFront/ResponseHeadersPolicy.js +162 -0
  22. package/lib/AWS/CloudFront/ResponseHeadersPolicy.js.map +1 -0
  23. package/lib/AWS/CloudFront/index.d.ts +5 -0
  24. package/lib/AWS/CloudFront/index.d.ts.map +1 -1
  25. package/lib/AWS/CloudFront/index.js +5 -0
  26. package/lib/AWS/CloudFront/index.js.map +1 -1
  27. package/lib/AWS/ECS/CapacityProvider.d.ts +102 -0
  28. package/lib/AWS/ECS/CapacityProvider.d.ts.map +1 -0
  29. package/lib/AWS/ECS/CapacityProvider.js +217 -0
  30. package/lib/AWS/ECS/CapacityProvider.js.map +1 -0
  31. package/lib/AWS/ECS/index.d.ts +1 -0
  32. package/lib/AWS/ECS/index.d.ts.map +1 -1
  33. package/lib/AWS/ECS/index.js +1 -0
  34. package/lib/AWS/ECS/index.js.map +1 -1
  35. package/lib/AWS/Providers.d.ts.map +1 -1
  36. package/lib/AWS/Providers.js +7 -1
  37. package/lib/AWS/Providers.js.map +1 -1
  38. package/lib/GitHub/Secrets.d.ts +45 -0
  39. package/lib/GitHub/Secrets.d.ts.map +1 -0
  40. package/lib/GitHub/Secrets.js +30 -0
  41. package/lib/GitHub/Secrets.js.map +1 -0
  42. package/lib/GitHub/index.d.ts +1 -0
  43. package/lib/GitHub/index.d.ts.map +1 -1
  44. package/lib/GitHub/index.js +1 -0
  45. package/lib/GitHub/index.js.map +1 -1
  46. package/package.json +1 -1
  47. package/src/AWS/CloudFront/CachePolicy.ts +271 -0
  48. package/src/AWS/CloudFront/KeyGroup.ts +233 -0
  49. package/src/AWS/CloudFront/OriginRequestPolicy.ts +256 -0
  50. package/src/AWS/CloudFront/PublicKey.ts +245 -0
  51. package/src/AWS/CloudFront/ResponseHeadersPolicy.ts +308 -0
  52. package/src/AWS/CloudFront/index.ts +11 -0
  53. package/src/AWS/ECS/CapacityProvider.ts +336 -0
  54. package/src/AWS/ECS/index.ts +4 -0
  55. package/src/AWS/Providers.ts +12 -0
  56. package/src/GitHub/Secrets.ts +66 -0
  57. package/src/GitHub/index.ts +1 -0
@@ -0,0 +1,30 @@
1
+ import * as Effect from "effect/Effect";
2
+ import * as Redacted from "effect/Redacted";
3
+ import { Secret } from "./Secret.js";
4
+ /**
5
+ * Bulk-creates a set of {@link Secret}s in the same repository (and
6
+ * optionally the same environment).
7
+ *
8
+ * Each entry in `secrets` becomes one `GitHub.Secret` resource, using the
9
+ * map key as both the alchemy logical id and the secret name.
10
+ *
11
+ * @example
12
+ * ```ts
13
+ * yield* GitHub.Secrets({
14
+ * owner: "alchemy-run",
15
+ * repository: "alchemy-effect",
16
+ * secrets: {
17
+ * AXIOM_INGEST_TOKEN: tokenValue,
18
+ * AXIOM_DATASET_TRACES: traces.name,
19
+ * },
20
+ * });
21
+ * ```
22
+ */
23
+ export const Secrets = ({ owner, repository, environment, secrets, }) => Effect.all(Object.entries(secrets).map(([name, value]) => Secret(name, {
24
+ owner,
25
+ repository,
26
+ environment,
27
+ name,
28
+ value: Redacted.make(value),
29
+ })));
30
+ //# sourceMappingURL=Secrets.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Secrets.js","sourceRoot":"","sources":["../../src/GitHub/Secrets.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,MAAM,eAAe,CAAC;AACxC,OAAO,KAAK,QAAQ,MAAM,iBAAiB,CAAC;AAG5C,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AA0BrC;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,OAAO,GAAG,CAAC,EACtB,KAAK,EACL,UAAU,EACV,WAAW,EACX,OAAO,GACM,EAAE,EAAE,CACjB,MAAM,CAAC,GAAG,CACR,MAAM,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,EAAE,CAC5C,MAAM,CAAC,IAAI,EAAE;IACX,KAAK;IACL,UAAU;IACV,WAAW;IACX,IAAI;IACJ,KAAK,EAAE,QAAQ,CAAC,IAAI,CAAC,KAAK,CAAC;CAC5B,CAAC,CACH,CACF,CAAC"}
@@ -3,5 +3,6 @@ export * from "./Comment.ts";
3
3
  export { GitHubCredentials, fromEnv, fromToken } from "./Credentials.ts";
4
4
  export * from "./Providers.ts";
5
5
  export * from "./Secret.ts";
6
+ export * from "./Secrets.ts";
6
7
  export * from "./Variable.ts";
7
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/GitHub/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/GitHub/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
@@ -3,5 +3,6 @@ export * from "./Comment.js";
3
3
  export { GitHubCredentials, fromEnv, fromToken } from "./Credentials.js";
4
4
  export * from "./Providers.js";
5
5
  export * from "./Secret.js";
6
+ export * from "./Secrets.js";
6
7
  export * from "./Variable.js";
7
8
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/GitHub/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,eAAe,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/GitHub/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,mBAAmB,CAAC;AAC1C,cAAc,cAAc,CAAC;AAC7B,OAAO,EAAE,iBAAiB,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AACzE,cAAc,gBAAgB,CAAC;AAC/B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "alchemy",
3
- "version": "2.0.0-beta.17",
3
+ "version": "2.0.0-beta.18",
4
4
  "homepage": "https://alchemy.run",
5
5
  "license": "Apache-2.0",
6
6
  "author": "Sam Goodwin <sam@alchemy.run>",
@@ -0,0 +1,271 @@
1
+ import * as cloudfront from "@distilled.cloud/aws/cloudfront";
2
+ import * as Effect from "effect/Effect";
3
+ import { isResolved } from "../../Diff.ts";
4
+ import { createPhysicalName } from "../../PhysicalName.ts";
5
+ import * as Provider from "../../Provider.ts";
6
+ import { Resource } from "../../Resource.ts";
7
+ import type { Providers } from "../Providers.ts";
8
+
9
+ export interface CachePolicyProps {
10
+ /**
11
+ * Name of the cache policy. If omitted, a deterministic name is generated.
12
+ *
13
+ * Names must be unique per AWS account. Changing the name triggers
14
+ * a replacement.
15
+ */
16
+ name?: string;
17
+ /**
18
+ * Optional comment describing the policy.
19
+ */
20
+ comment?: string;
21
+ /**
22
+ * Minimum amount of time, in seconds, that objects stay in the cache.
23
+ */
24
+ minTTL: number;
25
+ /**
26
+ * Default amount of time, in seconds, that objects stay in the cache when
27
+ * the origin does not send `Cache-Control` or `Expires` headers.
28
+ */
29
+ defaultTTL?: number;
30
+ /**
31
+ * Maximum amount of time, in seconds, that objects stay in the cache.
32
+ */
33
+ maxTTL?: number;
34
+ /**
35
+ * Controls which request values become part of the cache key and which
36
+ * additional headers/cookies/query strings CloudFront forwards to the origin.
37
+ */
38
+ parametersInCacheKeyAndForwardedToOrigin?: cloudfront.ParametersInCacheKeyAndForwardedToOrigin;
39
+ }
40
+
41
+ export interface CachePolicy extends Resource<
42
+ "AWS.CloudFront.CachePolicy",
43
+ CachePolicyProps,
44
+ {
45
+ /**
46
+ * CloudFront-assigned cache policy identifier.
47
+ */
48
+ cachePolicyId: string;
49
+ /**
50
+ * Name of the cache policy.
51
+ */
52
+ name: string;
53
+ /**
54
+ * Most recent entity tag for update/delete operations.
55
+ */
56
+ etag: string | undefined;
57
+ /**
58
+ * Current comment on the policy.
59
+ */
60
+ comment: string | undefined;
61
+ /**
62
+ * Current minimum TTL.
63
+ */
64
+ minTTL: number;
65
+ /**
66
+ * Current default TTL.
67
+ */
68
+ defaultTTL: number | undefined;
69
+ /**
70
+ * Current maximum TTL.
71
+ */
72
+ maxTTL: number | undefined;
73
+ /**
74
+ * Current cache-key/forwarded-value configuration.
75
+ */
76
+ parametersInCacheKeyAndForwardedToOrigin:
77
+ | cloudfront.ParametersInCacheKeyAndForwardedToOrigin
78
+ | undefined;
79
+ },
80
+ never,
81
+ Providers
82
+ > {}
83
+
84
+ /**
85
+ * A CloudFront cache policy.
86
+ *
87
+ * Cache policies determine the values CloudFront includes in the cache key,
88
+ * the headers, cookies and query strings it forwards to the origin, and the
89
+ * TTL bounds for cached responses. Policies are referenced by ID on a
90
+ * Distribution's default behavior or per-path cache behaviors.
91
+ *
92
+ * For AWS-managed policies (CachingOptimized, CachingDisabled,
93
+ * AllViewerExceptHostHeader) reference them by ID via the constants in
94
+ * {@link ManagedPolicies} instead of creating a custom policy.
95
+ *
96
+ * @section Creating Cache Policies
97
+ * @example Cache by query string and Authorization header
98
+ * ```typescript
99
+ * const cachePolicy = yield* CachePolicy("ApiCachePolicy", {
100
+ * comment: "Cache GETs by query string + Authorization",
101
+ * minTTL: 0,
102
+ * defaultTTL: 60,
103
+ * maxTTL: 3600,
104
+ * parametersInCacheKeyAndForwardedToOrigin: {
105
+ * EnableAcceptEncodingGzip: true,
106
+ * EnableAcceptEncodingBrotli: true,
107
+ * HeadersConfig: {
108
+ * HeaderBehavior: "whitelist",
109
+ * Headers: { Quantity: 1, Items: ["Authorization"] },
110
+ * },
111
+ * CookiesConfig: { CookieBehavior: "none" },
112
+ * QueryStringsConfig: { QueryStringBehavior: "all" },
113
+ * },
114
+ * });
115
+ * ```
116
+ */
117
+ export const CachePolicy = Resource<CachePolicy>("AWS.CloudFront.CachePolicy");
118
+
119
+ export const CachePolicyProvider = () =>
120
+ Provider.effect(
121
+ CachePolicy,
122
+ Effect.gen(function* () {
123
+ const getById = Effect.fn(function* (id: string) {
124
+ const config = yield* cloudfront
125
+ .getCachePolicyConfig({ Id: id })
126
+ .pipe(
127
+ Effect.catchTag("NoSuchCachePolicy", () => Effect.succeed(undefined)),
128
+ );
129
+ if (!config?.CachePolicyConfig) return undefined;
130
+ return { config: config.CachePolicyConfig, etag: config.ETag };
131
+ });
132
+
133
+ const getByName = Effect.fn(function* (name: string) {
134
+ const listed = yield* cloudfront.listCachePolicies({ Type: "custom" });
135
+ const summary = listed.CachePolicyList?.Items?.find(
136
+ (item) => item.CachePolicy?.CachePolicyConfig?.Name === name,
137
+ );
138
+ if (!summary?.CachePolicy?.Id) return undefined;
139
+ return yield* getById(summary.CachePolicy.Id).pipe(
140
+ Effect.map((found) =>
141
+ found ? { id: summary.CachePolicy.Id, ...found } : undefined,
142
+ ),
143
+ );
144
+ });
145
+
146
+ const buildConfig = (
147
+ name: string,
148
+ props: CachePolicyProps,
149
+ ): cloudfront.CachePolicyConfig => ({
150
+ Name: name,
151
+ Comment: props.comment,
152
+ MinTTL: props.minTTL,
153
+ DefaultTTL: props.defaultTTL,
154
+ MaxTTL: props.maxTTL,
155
+ ParametersInCacheKeyAndForwardedToOrigin:
156
+ props.parametersInCacheKeyAndForwardedToOrigin,
157
+ });
158
+
159
+ const toAttrs = (
160
+ id: string,
161
+ config: cloudfront.CachePolicyConfig,
162
+ etag: string | undefined,
163
+ ) => ({
164
+ cachePolicyId: id,
165
+ name: config.Name,
166
+ etag,
167
+ comment: config.Comment,
168
+ minTTL: config.MinTTL,
169
+ defaultTTL: config.DefaultTTL,
170
+ maxTTL: config.MaxTTL,
171
+ parametersInCacheKeyAndForwardedToOrigin:
172
+ config.ParametersInCacheKeyAndForwardedToOrigin,
173
+ });
174
+
175
+ return {
176
+ stables: ["cachePolicyId"],
177
+ diff: Effect.fn(function* ({ id, news, olds }) {
178
+ if (!isResolved(news)) return undefined;
179
+ if (
180
+ (yield* createName(id, olds ?? {})) !==
181
+ (yield* createName(id, news))
182
+ ) {
183
+ return { action: "replace" } as const;
184
+ }
185
+ }),
186
+ read: Effect.fn(function* ({ id, olds, output }) {
187
+ if (output?.cachePolicyId) {
188
+ const found = yield* getById(output.cachePolicyId);
189
+ if (found) return toAttrs(output.cachePolicyId, found.config, found.etag);
190
+ }
191
+ const name = yield* createName(id, olds ?? {});
192
+ const found = yield* getByName(name);
193
+ if (!found) return undefined;
194
+ return toAttrs(found.id, found.config, found.etag);
195
+ }),
196
+ create: Effect.fn(function* ({ id, news, session }) {
197
+ const name = yield* createName(id, news);
198
+ const created = yield* cloudfront
199
+ .createCachePolicy({ CachePolicyConfig: buildConfig(name, news) })
200
+ .pipe(
201
+ Effect.catchTag("CachePolicyAlreadyExists", () =>
202
+ getByName(name).pipe(
203
+ Effect.flatMap((existing) =>
204
+ existing
205
+ ? Effect.succeed({
206
+ CachePolicy: {
207
+ Id: existing.id,
208
+ LastModifiedTime: new Date(),
209
+ CachePolicyConfig: existing.config,
210
+ },
211
+ ETag: existing.etag,
212
+ Location: undefined,
213
+ })
214
+ : Effect.fail(
215
+ new Error(
216
+ `Cache policy '${name}' already exists but could not be recovered`,
217
+ ),
218
+ ),
219
+ ),
220
+ ),
221
+ ),
222
+ );
223
+ if (!created.CachePolicy?.Id) {
224
+ return yield* Effect.fail(
225
+ new Error("createCachePolicy returned no identifier"),
226
+ );
227
+ }
228
+ yield* session.note(created.CachePolicy.Id);
229
+ return toAttrs(
230
+ created.CachePolicy.Id,
231
+ created.CachePolicy.CachePolicyConfig,
232
+ created.ETag,
233
+ );
234
+ }),
235
+ update: Effect.fn(function* ({ news, output, session }) {
236
+ const current = yield* getById(output.cachePolicyId);
237
+ const updated = yield* cloudfront.updateCachePolicy({
238
+ Id: output.cachePolicyId,
239
+ IfMatch: current?.etag ?? output.etag,
240
+ CachePolicyConfig: buildConfig(output.name, news),
241
+ });
242
+ if (!updated.CachePolicy?.Id) {
243
+ return yield* Effect.fail(
244
+ new Error("updateCachePolicy returned no identifier"),
245
+ );
246
+ }
247
+ yield* session.note(output.cachePolicyId);
248
+ return toAttrs(
249
+ updated.CachePolicy.Id,
250
+ updated.CachePolicy.CachePolicyConfig,
251
+ updated.ETag,
252
+ );
253
+ }),
254
+ delete: Effect.fn(function* ({ output }) {
255
+ const current = yield* getById(output.cachePolicyId);
256
+ if (!current) return;
257
+ yield* cloudfront
258
+ .deleteCachePolicy({
259
+ Id: output.cachePolicyId,
260
+ IfMatch: current.etag,
261
+ })
262
+ .pipe(Effect.catchTag("NoSuchCachePolicy", () => Effect.void));
263
+ }),
264
+ };
265
+ }),
266
+ );
267
+
268
+ const createName = (id: string, props: CachePolicyProps) =>
269
+ props.name
270
+ ? Effect.succeed(props.name)
271
+ : createPhysicalName({ id, maxLength: 128, lowercase: true });
@@ -0,0 +1,233 @@
1
+ import * as cloudfront from "@distilled.cloud/aws/cloudfront";
2
+ import * as Effect from "effect/Effect";
3
+ import { isResolved } from "../../Diff.ts";
4
+ import type { Input } from "../../Input.ts";
5
+ import { createPhysicalName } from "../../PhysicalName.ts";
6
+ import * as Provider from "../../Provider.ts";
7
+ import { Resource } from "../../Resource.ts";
8
+ import type { Providers } from "../Providers.ts";
9
+
10
+ export interface KeyGroupProps {
11
+ /**
12
+ * Name of the key group. If omitted, a deterministic name is generated.
13
+ *
14
+ * Names must be unique per AWS account. Changing the name triggers
15
+ * a replacement.
16
+ */
17
+ name?: string;
18
+ /**
19
+ * Public key IDs that belong to the group. Order is preserved.
20
+ */
21
+ items: Input<string>[];
22
+ /**
23
+ * Optional comment describing the group.
24
+ */
25
+ comment?: string;
26
+ }
27
+
28
+ export interface KeyGroup extends Resource<
29
+ "AWS.CloudFront.KeyGroup",
30
+ KeyGroupProps,
31
+ {
32
+ /**
33
+ * CloudFront-assigned key group identifier. Used by Distributions in
34
+ * `TrustedKeyGroups` to authorize signed URLs/cookies.
35
+ */
36
+ keyGroupId: string;
37
+ /**
38
+ * Name of the key group.
39
+ */
40
+ name: string;
41
+ /**
42
+ * Public key IDs that belong to the group.
43
+ */
44
+ items: string[];
45
+ /**
46
+ * Most recent entity tag for update/delete operations.
47
+ */
48
+ etag: string | undefined;
49
+ /**
50
+ * Current comment on the group.
51
+ */
52
+ comment: string | undefined;
53
+ },
54
+ never,
55
+ Providers
56
+ > {}
57
+
58
+ /**
59
+ * A CloudFront key group.
60
+ *
61
+ * Key groups bundle one or more {@link PublicKey} resources for use as
62
+ * `TrustedKeyGroups` on a Distribution's cache behavior. CloudFront uses
63
+ * the keys in the group to verify the signatures on signed URLs and
64
+ * signed cookies for that behavior.
65
+ *
66
+ * @section Creating Key Groups
67
+ * @example Group two public keys for signed URL verification
68
+ * ```typescript
69
+ * const primary = yield* PublicKey("PrimarySigningKey", {
70
+ * encodedKey: yield* fs.readFileString("./primary.pem"),
71
+ * });
72
+ * const secondary = yield* PublicKey("SecondarySigningKey", {
73
+ * encodedKey: yield* fs.readFileString("./secondary.pem"),
74
+ * });
75
+ *
76
+ * const keyGroup = yield* KeyGroup("SignedUrlKeys", {
77
+ * comment: "Trusted signers for /private",
78
+ * items: [primary.publicKeyId, secondary.publicKeyId],
79
+ * });
80
+ * ```
81
+ */
82
+ export const KeyGroup = Resource<KeyGroup>("AWS.CloudFront.KeyGroup");
83
+
84
+ export const KeyGroupProvider = () =>
85
+ Provider.effect(
86
+ KeyGroup,
87
+ Effect.gen(function* () {
88
+ const getById = Effect.fn(function* (id: string) {
89
+ const config = yield* cloudfront
90
+ .getKeyGroupConfig({ Id: id })
91
+ .pipe(
92
+ Effect.catchTag("NoSuchResource", () => Effect.succeed(undefined)),
93
+ );
94
+ if (!config?.KeyGroupConfig) return undefined;
95
+ return { config: config.KeyGroupConfig, etag: config.ETag };
96
+ });
97
+
98
+ const getByName = Effect.fn(function* (name: string) {
99
+ const listed = yield* cloudfront.listKeyGroups({});
100
+ const summary = listed.KeyGroupList?.Items?.find(
101
+ (item) => item.KeyGroup?.KeyGroupConfig?.Name === name,
102
+ );
103
+ if (!summary?.KeyGroup?.Id) return undefined;
104
+ return yield* getById(summary.KeyGroup.Id).pipe(
105
+ Effect.map((found) =>
106
+ found ? { id: summary.KeyGroup.Id, ...found } : undefined,
107
+ ),
108
+ );
109
+ });
110
+
111
+ const buildConfig = (
112
+ name: string,
113
+ items: string[],
114
+ comment: string | undefined,
115
+ ): cloudfront.KeyGroupConfig => ({
116
+ Name: name,
117
+ Items: items,
118
+ Comment: comment,
119
+ });
120
+
121
+ const toAttrs = (
122
+ id: string,
123
+ config: cloudfront.KeyGroupConfig,
124
+ etag: string | undefined,
125
+ ) => ({
126
+ keyGroupId: id,
127
+ name: config.Name,
128
+ items: config.Items,
129
+ etag,
130
+ comment: config.Comment,
131
+ });
132
+
133
+ return {
134
+ stables: ["keyGroupId"],
135
+ diff: Effect.fn(function* ({ id, news, olds }) {
136
+ if (!isResolved(news)) return undefined;
137
+ if (
138
+ (yield* createName(id, olds ?? {})) !==
139
+ (yield* createName(id, news))
140
+ ) {
141
+ return { action: "replace" } as const;
142
+ }
143
+ }),
144
+ read: Effect.fn(function* ({ id, olds, output }) {
145
+ if (output?.keyGroupId) {
146
+ const found = yield* getById(output.keyGroupId);
147
+ if (found) return toAttrs(output.keyGroupId, found.config, found.etag);
148
+ }
149
+ const name = yield* createName(id, olds ?? {});
150
+ const found = yield* getByName(name);
151
+ if (!found) return undefined;
152
+ return toAttrs(found.id, found.config, found.etag);
153
+ }),
154
+ create: Effect.fn(function* ({ id, news, session }) {
155
+ const name = yield* createName(id, news);
156
+ const items = news.items as string[];
157
+ const created = yield* cloudfront
158
+ .createKeyGroup({
159
+ KeyGroupConfig: buildConfig(name, items, news.comment),
160
+ })
161
+ .pipe(
162
+ Effect.catchTag("KeyGroupAlreadyExists", () =>
163
+ getByName(name).pipe(
164
+ Effect.flatMap((existing) =>
165
+ existing
166
+ ? Effect.succeed({
167
+ KeyGroup: {
168
+ Id: existing.id,
169
+ LastModifiedTime: new Date(),
170
+ KeyGroupConfig: existing.config,
171
+ },
172
+ ETag: existing.etag,
173
+ Location: undefined,
174
+ })
175
+ : Effect.fail(
176
+ new Error(
177
+ `Key group '${name}' already exists but could not be recovered`,
178
+ ),
179
+ ),
180
+ ),
181
+ ),
182
+ ),
183
+ );
184
+ if (!created.KeyGroup?.Id) {
185
+ return yield* Effect.fail(
186
+ new Error("createKeyGroup returned no identifier"),
187
+ );
188
+ }
189
+ yield* session.note(created.KeyGroup.Id);
190
+ return toAttrs(
191
+ created.KeyGroup.Id,
192
+ created.KeyGroup.KeyGroupConfig,
193
+ created.ETag,
194
+ );
195
+ }),
196
+ update: Effect.fn(function* ({ news, output, session }) {
197
+ const current = yield* getById(output.keyGroupId);
198
+ const items = news.items as string[];
199
+ const updated = yield* cloudfront.updateKeyGroup({
200
+ Id: output.keyGroupId,
201
+ IfMatch: current?.etag ?? output.etag,
202
+ KeyGroupConfig: buildConfig(output.name, items, news.comment),
203
+ });
204
+ if (!updated.KeyGroup?.Id) {
205
+ return yield* Effect.fail(
206
+ new Error("updateKeyGroup returned no identifier"),
207
+ );
208
+ }
209
+ yield* session.note(output.keyGroupId);
210
+ return toAttrs(
211
+ updated.KeyGroup.Id,
212
+ updated.KeyGroup.KeyGroupConfig,
213
+ updated.ETag,
214
+ );
215
+ }),
216
+ delete: Effect.fn(function* ({ output }) {
217
+ const current = yield* getById(output.keyGroupId);
218
+ if (!current) return;
219
+ yield* cloudfront
220
+ .deleteKeyGroup({
221
+ Id: output.keyGroupId,
222
+ IfMatch: current.etag,
223
+ })
224
+ .pipe(Effect.catchTag("NoSuchResource", () => Effect.void));
225
+ }),
226
+ };
227
+ }),
228
+ );
229
+
230
+ const createName = (id: string, props: { name?: string }) =>
231
+ props.name
232
+ ? Effect.succeed(props.name)
233
+ : createPhysicalName({ id, maxLength: 128, lowercase: true });