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, 06:00 UTC
| 1 | import * as EFS from "@alchemy.run/aws/EFS"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 5 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 6 | import * as Layer from "effect/Layer"; |
| 7 | import * as Schedule from "effect/Schedule"; |
| 8 | import path from "pathe"; |
| 9 | |
| 10 | const main = path.resolve(import.meta.dirname, "bindings-handler.ts"); |
| 11 | |
| 12 | // Deterministic idempotency token for the runtime create/delete round-trip. |
| 13 | const ACCESS_POINT_CLIENT_TOKEN = "alchemy-efs-bindings-access-point"; |
| 14 | |
| 15 | export class EfsBindingsFunction extends Lambda.Function<Lambda.Function>()( |
| 16 | "EfsBindingsFunction", |
| 17 | ) {} |
| 18 | |
| 19 | export default EfsBindingsFunction.make( |
| 20 | { |
| 21 | main, |
| 22 | functionUrl: true, |
| 23 | }, |
| 24 | Effect.gen(function* () { |
| 25 | // Control-plane bindings only — no VPC/mount target needed: EFS's data |
| 26 | // plane is NFS (covered by the Lambda `fileSystemConfigs` mount), while |
| 27 | // these bindings drive the management API over HTTPS. |
| 28 | const files = yield* EFS.FileSystem("BindingsFileSystem"); |
| 29 | |
| 30 | const describeFileSystem = yield* EFS.DescribeFileSystem(files); |
| 31 | const describeMountTargets = yield* EFS.DescribeMountTargets(files); |
| 32 | const describeAccessPoints = yield* EFS.DescribeAccessPoints(files); |
| 33 | const describeBackupPolicy = yield* EFS.DescribeBackupPolicy(files); |
| 34 | const putBackupPolicy = yield* EFS.PutBackupPolicy(files); |
| 35 | const describeLifecycleConfiguration = yield* EFS.DescribeLifecycleConfiguration(files); |
| 36 | const putLifecycleConfiguration = yield* EFS.PutLifecycleConfiguration(files); |
| 37 | const createAccessPoint = yield* EFS.CreateAccessPoint(files); |
| 38 | const deleteAccessPoint = yield* EFS.DeleteAccessPoint(); |
| 39 | const describeReplicationConfigurations = yield* EFS.DescribeReplicationConfigurations(files); |
| 40 | |
| 41 | const bound = { |
| 42 | describeFileSystem, |
| 43 | describeMountTargets, |
| 44 | describeAccessPoints, |
| 45 | describeBackupPolicy, |
| 46 | putBackupPolicy, |
| 47 | describeLifecycleConfiguration, |
| 48 | putLifecycleConfiguration, |
| 49 | createAccessPoint, |
| 50 | deleteAccessPoint, |
| 51 | describeReplicationConfigurations, |
| 52 | }; |
| 53 | |
| 54 | // A put racing a still-transitioning previous change surfaces the typed |
| 55 | // IncorrectFileSystemLifeCycleState — retry it bounded (~20s). |
| 56 | const retryWhileUpdating = <A, E extends { _tag: string }, R>( |
| 57 | self: Effect.Effect<A, E, R>, |
| 58 | ): Effect.Effect<A, E, R> => |
| 59 | Effect.retry(self, { |
| 60 | while: (e): boolean => e._tag === "IncorrectFileSystemLifeCycleState", |
| 61 | schedule: Schedule.max([Schedule.fixed("2 seconds"), Schedule.recurs(10)]), |
| 62 | }); |
| 63 | |
| 64 | return { |
| 65 | fetch: Effect.gen(function* () { |
| 66 | const request = yield* HttpServerRequest; |
| 67 | const url = new URL(request.originalUrl); |
| 68 | const pathname = url.pathname; |
| 69 | |
| 70 | if (request.method === "GET" && pathname === "/bindings") { |
| 71 | return yield* HttpServerResponse.json({ |
| 72 | bound: Object.keys(bound), |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | if (request.method === "GET" && pathname === "/file-system") { |
| 77 | // FileSystemId injection scopes the response to the bound file |
| 78 | // system. |
| 79 | const response = yield* describeFileSystem(); |
| 80 | const fs = response.FileSystems?.[0]; |
| 81 | return yield* HttpServerResponse.json({ |
| 82 | fileSystemId: fs?.FileSystemId, |
| 83 | state: fs?.LifeCycleState, |
| 84 | encrypted: fs?.Encrypted, |
| 85 | }); |
| 86 | } |
| 87 | |
| 88 | if (request.method === "GET" && pathname === "/mount-targets") { |
| 89 | const response = yield* describeMountTargets(); |
| 90 | return yield* HttpServerResponse.json({ |
| 91 | count: (response.MountTargets ?? []).length, |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | if (request.method === "GET" && pathname === "/access-points") { |
| 96 | const response = yield* describeAccessPoints(); |
| 97 | return yield* HttpServerResponse.json({ |
| 98 | count: (response.AccessPoints ?? []).length, |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | if (request.method === "GET" && pathname === "/backup-policy") { |
| 103 | // A file system that never had a backup policy fails with the |
| 104 | // typed PolicyNotFound — normalize it to "NONE". |
| 105 | const status = yield* describeBackupPolicy().pipe( |
| 106 | Effect.map((r) => r.BackupPolicy?.Status ?? "NONE"), |
| 107 | Effect.catchTag("PolicyNotFound", () => Effect.succeed("NONE")), |
| 108 | ); |
| 109 | return yield* HttpServerResponse.json({ status }); |
| 110 | } |
| 111 | |
| 112 | if (request.method === "POST" && pathname === "/backup-policy/enable") { |
| 113 | const response = yield* retryWhileUpdating( |
| 114 | putBackupPolicy({ BackupPolicy: { Status: "ENABLED" } }), |
| 115 | ); |
| 116 | return yield* HttpServerResponse.json({ |
| 117 | status: response.BackupPolicy?.Status, |
| 118 | }); |
| 119 | } |
| 120 | |
| 121 | if (request.method === "POST" && pathname === "/backup-policy/disable") { |
| 122 | const response = yield* retryWhileUpdating( |
| 123 | putBackupPolicy({ BackupPolicy: { Status: "DISABLED" } }), |
| 124 | ); |
| 125 | return yield* HttpServerResponse.json({ |
| 126 | status: response.BackupPolicy?.Status, |
| 127 | }); |
| 128 | } |
| 129 | |
| 130 | if (request.method === "GET" && pathname === "/lifecycle") { |
| 131 | const response = yield* describeLifecycleConfiguration(); |
| 132 | return yield* HttpServerResponse.json({ |
| 133 | count: (response.LifecyclePolicies ?? []).length, |
| 134 | }); |
| 135 | } |
| 136 | |
| 137 | if (request.method === "POST" && pathname === "/lifecycle/set") { |
| 138 | const response = yield* retryWhileUpdating( |
| 139 | putLifecycleConfiguration({ |
| 140 | LifecyclePolicies: [{ TransitionToIA: "AFTER_30_DAYS" }], |
| 141 | }), |
| 142 | ); |
| 143 | return yield* HttpServerResponse.json({ |
| 144 | count: (response.LifecyclePolicies ?? []).length, |
| 145 | }); |
| 146 | } |
| 147 | |
| 148 | if (request.method === "POST" && pathname === "/lifecycle/clear") { |
| 149 | const response = yield* retryWhileUpdating( |
| 150 | putLifecycleConfiguration({ LifecyclePolicies: [] }), |
| 151 | ); |
| 152 | return yield* HttpServerResponse.json({ |
| 153 | count: (response.LifecyclePolicies ?? []).length, |
| 154 | }); |
| 155 | } |
| 156 | |
| 157 | if (request.method === "POST" && pathname === "/access-point") { |
| 158 | // Create-then-delete round-trip: exercises the runtime |
| 159 | // multi-tenant pattern end-to-end and leaves no orphan. A crashed |
| 160 | // previous invocation surfaces the typed AccessPointAlreadyExists |
| 161 | // (same client token) — resolve it by describing the winner. |
| 162 | const accessPoint = yield* createAccessPoint({ |
| 163 | ClientToken: ACCESS_POINT_CLIENT_TOKEN, |
| 164 | PosixUser: { Uid: 1000, Gid: 1000 }, |
| 165 | RootDirectory: { |
| 166 | Path: "/bindings-test", |
| 167 | CreationInfo: { |
| 168 | OwnerUid: 1000, |
| 169 | OwnerGid: 1000, |
| 170 | Permissions: "750", |
| 171 | }, |
| 172 | }, |
| 173 | }).pipe( |
| 174 | Effect.catchTag("AccessPointAlreadyExists", () => |
| 175 | describeAccessPoints().pipe( |
| 176 | Effect.map((r) => |
| 177 | r.AccessPoints?.find((ap) => ap.ClientToken === ACCESS_POINT_CLIENT_TOKEN)!, |
| 178 | ), |
| 179 | ), |
| 180 | ), |
| 181 | ); |
| 182 | yield* deleteAccessPoint({ |
| 183 | AccessPointId: accessPoint.AccessPointId!, |
| 184 | }).pipe(Effect.catchTag("AccessPointNotFound", () => Effect.void)); |
| 185 | return yield* HttpServerResponse.json({ |
| 186 | accessPointId: accessPoint.AccessPointId, |
| 187 | deleted: true, |
| 188 | }); |
| 189 | } |
| 190 | |
| 191 | if (request.method === "GET" && pathname === "/replication") { |
| 192 | // The fixture file system has no replication configuration — the |
| 193 | // typed ReplicationNotFound proves the grant end-to-end (an IAM |
| 194 | // gap would surface AccessDeniedException, a 500). |
| 195 | const hasReplication = yield* describeReplicationConfigurations().pipe( |
| 196 | Effect.map((r) => (r.Replications ?? []).length > 0), |
| 197 | Effect.catchTag("ReplicationNotFound", () => Effect.succeed(false)), |
| 198 | ); |
| 199 | return yield* HttpServerResponse.json({ hasReplication }); |
| 200 | } |
| 201 | |
| 202 | return yield* HttpServerResponse.json( |
| 203 | { error: "Not found", method: request.method, pathname }, |
| 204 | { status: 404 }, |
| 205 | ); |
| 206 | }).pipe(Effect.orDie), |
| 207 | }; |
| 208 | }).pipe( |
| 209 | Effect.provide( |
| 210 | Layer.mergeAll( |
| 211 | EFS.DescribeFileSystemHttp, |
| 212 | EFS.DescribeMountTargetsHttp, |
| 213 | EFS.DescribeAccessPointsHttp, |
| 214 | EFS.DescribeBackupPolicyHttp, |
| 215 | EFS.PutBackupPolicyHttp, |
| 216 | EFS.DescribeLifecycleConfigurationHttp, |
| 217 | EFS.PutLifecycleConfigurationHttp, |
| 218 | EFS.CreateAccessPointHttp, |
| 219 | EFS.DeleteAccessPointHttp, |
| 220 | EFS.DescribeReplicationConfigurationsHttp, |
| 221 | ), |
| 222 | ), |
| 223 | ), |
| 224 | ); |