The best way to control Claude Code, Codex and more from any computer or phone

main
ts 157 lines 5.46 KB
Raw
1 import * as sdb from "@distilled.cloud/aws/simpledb";
2 import { isResolved } from "alchemy/Diff";
3 import { createPhysicalName } from "alchemy/PhysicalName";
4 import * as Provider from "alchemy/Provider";
5 import { Resource } from "alchemy/Resource";
6 import * as Effect from "effect/Effect";
7 import * as Schedule from "effect/Schedule";
8 import * as Stream from "effect/Stream";
9 import { AWSEnvironment } from "../Environment.ts";
10 import type { Providers } from "../Providers.ts";
11
12 export interface DomainProps {
13 /**
14 * Name of the SimpleDB domain. 3-255 characters; letters, digits, `_`,
15 * `-`, and `.` are allowed. Changing the name replaces the domain.
16 * @default a generated physical name
17 */
18 domainName?: string;
19 }
20
21 export interface Domain extends Resource<
22 "AWS.SimpleDB.Domain",
23 DomainProps,
24 {
25 domainName: string;
26 domainArn: string;
27 },
28 {},
29 Providers
30 > {}
31
32 /**
33 * An Amazon SimpleDB domain — the container for SimpleDB items and
34 * attributes, analogous to a table.
35 *
36 * SimpleDB is a legacy service (closed to accounts that never used it and
37 * slated for migration via the SimpleDBv2 export API), but domains remain
38 * fully manageable on grandfathered accounts. A domain has no mutable
39 * configuration: the name is its identity, so any name change replaces the
40 * domain. SimpleDB has no tagging API, so Alchemy cannot brand domains for
41 * ownership detection.
42 * ### Creating Domains
43 * **Example:** Basic Domain
44 * ```typescript
45 * import * as AWS from "@alchemy.run/aws";
46 *
47 * const domain = yield* AWS.SimpleDB.Domain("MyDomain", {});
48 * ```
49 *
50 * **Example:** Named Domain
51 * ```typescript
52 * const domain = yield* AWS.SimpleDB.Domain("MyDomain", {
53 * domainName: "my-application-data",
54 * });
55 * ```
56 *
57 * @resource
58 */
59 export const Domain = Resource<Domain>("AWS.SimpleDB.Domain");
60
61 /**
62 * CreateDomain is eventually consistent — DomainMetadata directly after a
63 * create can briefly report NoSuchDomain. Explicitly typed so the
64 * conditional `Retry.Return` type does not leak into declaration emit.
65 */
66 const retryWhileNoSuchDomain = <A, E extends { readonly _tag: string }, R>(
67 self: Effect.Effect<A, E, R>,
68 ): Effect.Effect<A, E, R> =>
69 Effect.retry(self, {
70 while: (e) => e._tag === "NoSuchDomain",
71 schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]),
72 });
73
74 export const DomainProvider = () =>
75 Provider.effect(
76 Domain,
77 Effect.gen(function* () {
78 const createName = Effect.fn(function* (id: string, props: DomainProps) {
79 return props.domainName ?? (yield* createPhysicalName({ id, maxLength: 255 }));
80 });
81
82 const domainArn = (region: string, accountId: string, name: string) =>
83 `arn:aws:sdb:${region}:${accountId}:domain/${name}`;
84
85 const observeDomain = Effect.fn(function* (name: string) {
86 return yield* sdb
87 .domainMetadata({ DomainName: name })
88 .pipe(Effect.catchTag("NoSuchDomain", () => Effect.undefined));
89 });
90
91 return Domain.Provider.of({
92 stables: ["domainName", "domainArn"],
93
94 list: () =>
95 Effect.gen(function* () {
96 const { accountId, region } = yield* AWSEnvironment.current;
97 const names = yield* sdb.listDomains.items({}).pipe(Stream.runCollect);
98 return Array.from(names).map((name) => ({
99 domainName: name,
100 domainArn: domainArn(region, accountId, name),
101 }));
102 }),
103
104 read: Effect.fn(function* ({ id, olds, output }) {
105 const { accountId, region } = yield* AWSEnvironment.current;
106 const name = output?.domainName ?? (yield* createName(id, olds ?? {}));
107 const metadata = yield* observeDomain(name);
108 if (metadata === undefined) {
109 return undefined;
110 }
111 // SimpleDB has no tagging API, so ownership cannot be verified —
112 // an existing domain with the derived name is treated as ours.
113 return {
114 domainName: name,
115 domainArn: domainArn(region, accountId, name),
116 };
117 }),
118
119 diff: Effect.fn(function* ({ id, news, olds }) {
120 if (!isResolved(news)) return undefined;
121 const oldName = yield* createName(id, olds ?? {});
122 const newName = yield* createName(id, news);
123 if (oldName !== newName) {
124 return { action: "replace" } as const;
125 }
126 }),
127
128 reconcile: Effect.fn(function* ({ id, news, output, session }) {
129 const { accountId, region } = yield* AWSEnvironment.current;
130 const name = output?.domainName ?? (yield* createName(id, news));
131
132 // 1. OBSERVE — cloud state is authoritative
133 const metadata = yield* observeDomain(name);
134
135 // 2. ENSURE — CreateDomain is idempotent (creating an existing
136 // domain succeeds); wait until the domain is visible.
137 if (metadata === undefined) {
138 yield* sdb.createDomain({ DomainName: name });
139 yield* retryWhileNoSuchDomain(sdb.domainMetadata({ DomainName: name }));
140 }
141
142 // 3. SYNC — a domain has no mutable configuration and no tags.
143
144 yield* session.note(name);
145 return {
146 domainName: name,
147 domainArn: domainArn(region, accountId, name),
148 };
149 }),
150
151 delete: Effect.fn(function* ({ output }) {
152 // DeleteDomain is idempotent — deleting a missing domain succeeds.
153 yield* sdb.deleteDomain({ DomainName: output.domainName });
154 }),
155 });
156 }),
157 );