The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import * as sd from "@distilled.cloud/aws/servicediscovery"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import { createInternalTags, hasAlchemyTags } from "alchemy/Tags"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as Stream from "effect/Stream"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | awaitOperation, |
| 13 | ensureNamespace, |
| 14 | fetchObservedTags, |
| 15 | observeNamespace, |
| 16 | retryWhileResourceInUse, |
| 17 | syncTags, |
| 18 | } from "./internal.ts"; |
| 19 | |
| 20 | export interface HttpNamespaceProps { |
| 21 | /** |
| 22 | * Name of the namespace. Changing the name replaces the namespace. |
| 23 | * @default a generated DNS-compatible physical name |
| 24 | */ |
| 25 | name?: string; |
| 26 | /** |
| 27 | * A description for the namespace. |
| 28 | */ |
| 29 | description?: string; |
| 30 | /** |
| 31 | * Tags to apply to the namespace. Merged with internal Alchemy tags. |
| 32 | */ |
| 33 | tags?: Record<string, string>; |
| 34 | } |
| 35 | |
| 36 | export interface HttpNamespace extends Resource< |
| 37 | "AWS.CloudMap.HttpNamespace", |
| 38 | HttpNamespaceProps, |
| 39 | { |
| 40 | /** |
| 41 | * The unique identifier of the namespace. |
| 42 | */ |
| 43 | namespaceId: string; |
| 44 | /** |
| 45 | * The ARN of the namespace. |
| 46 | */ |
| 47 | namespaceArn: string; |
| 48 | /** |
| 49 | * Name of the namespace. |
| 50 | */ |
| 51 | namespaceName: string; |
| 52 | /** |
| 53 | * The name clients use to discover instances via the HTTP discovery API. |
| 54 | */ |
| 55 | httpName: string | undefined; |
| 56 | }, |
| 57 | {}, |
| 58 | Providers |
| 59 | > {} |
| 60 | |
| 61 | /** |
| 62 | * An AWS Cloud Map HTTP namespace — an API-only service registry. Instances |
| 63 | * registered in an HTTP namespace are discoverable via the |
| 64 | * `DiscoverInstances` API but not via DNS, so no VPC or hosted zone is |
| 65 | * required. |
| 66 | * |
| 67 | * Namespace creation and deletion are asynchronous — the provider polls the |
| 68 | * Cloud Map operations API (bounded) until they complete. |
| 69 | * ### Creating Namespaces |
| 70 | * **Example:** HTTP Namespace |
| 71 | * ```typescript |
| 72 | * import * as AWS from "@alchemy.run/aws"; |
| 73 | * |
| 74 | * const namespace = yield* AWS.CloudMap.HttpNamespace("AppNamespace", { |
| 75 | * description: "API-only service discovery", |
| 76 | * }); |
| 77 | * ``` |
| 78 | * |
| 79 | * ### Registering Services |
| 80 | * **Example:** API-only Service |
| 81 | * ```typescript |
| 82 | * const service = yield* AWS.CloudMap.Service("Backend", { |
| 83 | * namespaceId: namespace.namespaceId, |
| 84 | * }); |
| 85 | * ``` |
| 86 | * |
| 87 | * @resource |
| 88 | */ |
| 89 | export const HttpNamespace = Resource<HttpNamespace>("AWS.CloudMap.HttpNamespace"); |
| 90 | |
| 91 | export const HttpNamespaceProvider = () => |
| 92 | Provider.effect( |
| 93 | HttpNamespace, |
| 94 | Effect.gen(function* () { |
| 95 | const createName = Effect.fn(function* (id: string, props: { name?: string | undefined }) { |
| 96 | return props.name ?? (yield* createPhysicalName({ id, maxLength: 253, lowercase: true })); |
| 97 | }); |
| 98 | |
| 99 | const toAttributes = (namespace: sd.Namespace) => ({ |
| 100 | namespaceId: namespace.Id!, |
| 101 | namespaceArn: namespace.Arn!, |
| 102 | namespaceName: namespace.Name!, |
| 103 | httpName: namespace.Properties?.HttpProperties?.HttpName, |
| 104 | }); |
| 105 | |
| 106 | return HttpNamespace.Provider.of({ |
| 107 | stables: ["namespaceId", "namespaceArn", "namespaceName", "httpName"], |
| 108 | |
| 109 | list: () => |
| 110 | Effect.gen(function* () { |
| 111 | const pages = yield* sd.listNamespaces |
| 112 | .pages({ |
| 113 | Filters: [{ Name: "TYPE", Values: ["HTTP"], Condition: "EQ" }], |
| 114 | }) |
| 115 | .pipe(Stream.runCollect); |
| 116 | return Array.from(pages) |
| 117 | .flatMap((page) => page.Namespaces ?? []) |
| 118 | .filter((n) => n.Id !== undefined && n.Arn !== undefined && n.Name !== undefined) |
| 119 | .map((n) => ({ |
| 120 | namespaceId: n.Id!, |
| 121 | namespaceArn: n.Arn!, |
| 122 | namespaceName: n.Name!, |
| 123 | httpName: n.Properties?.HttpProperties?.HttpName, |
| 124 | })); |
| 125 | }), |
| 126 | |
| 127 | read: Effect.fn(function* ({ id, olds, output }) { |
| 128 | const name = output?.namespaceName ?? (yield* createName(id, olds ?? {})); |
| 129 | const namespace = yield* observeNamespace("HTTP", name, output?.namespaceId); |
| 130 | if (namespace?.Id === undefined) { |
| 131 | return undefined; |
| 132 | } |
| 133 | const attrs = toAttributes(namespace); |
| 134 | const tags = yield* fetchObservedTags(attrs.namespaceArn); |
| 135 | return (yield* hasAlchemyTags(id, tags)) ? attrs : Unowned(attrs); |
| 136 | }), |
| 137 | |
| 138 | diff: Effect.fn(function* ({ id, news = {}, olds = {} }) { |
| 139 | if (!isResolved(news)) return undefined; |
| 140 | const oldName = yield* createName(id, olds); |
| 141 | const newName = yield* createName(id, news); |
| 142 | if (oldName !== newName) { |
| 143 | return { action: "replace" } as const; |
| 144 | } |
| 145 | // description/tags fall through to the default update path |
| 146 | }), |
| 147 | |
| 148 | reconcile: Effect.fn(function* ({ id, news = {}, output, session }) { |
| 149 | const name = output?.namespaceName ?? (yield* createName(id, news)); |
| 150 | const internalTags = yield* createInternalTags(id); |
| 151 | const desiredTags = { ...news.tags, ...internalTags }; |
| 152 | |
| 153 | // 1. OBSERVE |
| 154 | let namespace = yield* observeNamespace("HTTP", name, output?.namespaceId); |
| 155 | |
| 156 | // 2. ENSURE — create if missing (async operation); the created |
| 157 | // namespace is observed by the operation's target id, riding out |
| 158 | // a same-name predecessor still deleting (see ensureNamespace) |
| 159 | if (namespace === undefined) { |
| 160 | yield* session.note(`creating HTTP namespace ${name} (async)...`); |
| 161 | namespace = yield* ensureNamespace( |
| 162 | "HTTP", |
| 163 | name, |
| 164 | sd.createHttpNamespace({ |
| 165 | Name: name, |
| 166 | Description: news.description, |
| 167 | Tags: Object.entries(desiredTags).map(([Key, Value]) => ({ |
| 168 | Key, |
| 169 | Value, |
| 170 | })), |
| 171 | }), |
| 172 | ); |
| 173 | } |
| 174 | if (namespace?.Id === undefined) { |
| 175 | return yield* sd.NamespaceNotFound.make({ |
| 176 | message: `namespace ${name} not visible after create`, |
| 177 | }); |
| 178 | } |
| 179 | |
| 180 | // 3. SYNC — description (updateHttpNamespace requires Description) |
| 181 | if (news.description !== undefined && news.description !== namespace.Description) { |
| 182 | const update = yield* sd.updateHttpNamespace({ |
| 183 | Id: namespace.Id, |
| 184 | Namespace: { Description: news.description }, |
| 185 | }); |
| 186 | if (update.OperationId !== undefined) { |
| 187 | yield* awaitOperation(update.OperationId); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // 3b. SYNC TAGS — diff against OBSERVED cloud tags |
| 192 | const observedTags = yield* fetchObservedTags(namespace.Arn!); |
| 193 | yield* syncTags(namespace.Arn!, observedTags, desiredTags); |
| 194 | |
| 195 | yield* session.note(namespace.Id); |
| 196 | return toAttributes(namespace); |
| 197 | }), |
| 198 | |
| 199 | delete: Effect.fn(function* ({ output }) { |
| 200 | const deleted = yield* retryWhileResourceInUse( |
| 201 | sd.deleteNamespace({ Id: output.namespaceId }), |
| 202 | ).pipe( |
| 203 | Effect.catchTags({ |
| 204 | NamespaceNotFound: () => Effect.succeed({ OperationId: undefined }), |
| 205 | // an identical delete is already in flight — await THAT operation |
| 206 | // instead of silently skipping the deletion |
| 207 | DuplicateRequest: (e) => Effect.succeed({ OperationId: e.DuplicateOperationId }), |
| 208 | }), |
| 209 | ); |
| 210 | if (deleted.OperationId !== undefined) { |
| 211 | yield* awaitOperation(deleted.OperationId); |
| 212 | } |
| 213 | }), |
| 214 | }); |
| 215 | }), |
| 216 | ); |