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 type * as Credentials from "@distilled.cloud/aws/Credentials"; |
| 2 | import type * as Region from "@distilled.cloud/aws/Region"; |
| 3 | import * as Effect from "effect/Effect"; |
| 4 | import * as Layer from "effect/Layer"; |
| 5 | import * as AWSBindings from "../AWSBindings.ts"; |
| 6 | import { generateDbAuthToken } from "../Connection/DbAuthToken.ts"; |
| 7 | import type { SqlConnectionInfo } from "../Connection/internal.ts"; |
| 8 | import { formatSqlConnectionUrl } from "../Connection/internal.ts"; |
| 9 | import type { Cluster } from "./Cluster.ts"; |
| 10 | import { Connect, connectEnvPrefix, type ConnectOptions } from "./Connect.ts"; |
| 11 | |
| 12 | const DSQL_PORT = 5432; |
| 13 | |
| 14 | /** |
| 15 | * IAM-token implementation of {@link Connect}. At deploy time it grants |
| 16 | * `dsql:DbConnect` (or `dsql:DbConnectAdmin`) on the cluster to the host |
| 17 | * Function and publishes the endpoint as `DSQL_{LOGICAL_ID}_HOST`; at runtime |
| 18 | * it presigns a fresh auth token (client-side SigV4 — no API call) with the |
| 19 | * Function's own credentials and formats the full connection descriptor. |
| 20 | */ |
| 21 | export const ConnectHttp = Layer.effect( |
| 22 | Connect, |
| 23 | Effect.gen(function* () { |
| 24 | const bind = yield* AWSBindings.Bind; |
| 25 | // Captured at layer build so the runtime callable only requires |
| 26 | // RuntimeContext — the presign resolves credentials lazily from the |
| 27 | // service on every mint, so refreshed execution-role creds are honored. |
| 28 | const services = yield* AWSBindings.withCredentials( |
| 29 | Effect.context<Credentials.Credentials | Region.Region>(), |
| 30 | ); |
| 31 | |
| 32 | return Effect.fn(function* (cluster: Cluster, options?: ConnectOptions) { |
| 33 | const Host = yield* cluster.endpoint; |
| 34 | const admin = options?.admin ?? false; |
| 35 | |
| 36 | if (!globalThis.__ALCHEMY_RUNTIME__) { |
| 37 | const prefix = connectEnvPrefix(cluster.LogicalId); |
| 38 | yield* bind`AWS.DSQL.Connect(${cluster})`({ |
| 39 | env: { |
| 40 | [`${prefix}_HOST`]: cluster.endpoint, |
| 41 | }, |
| 42 | policyStatements: [ |
| 43 | { |
| 44 | Effect: "Allow", |
| 45 | Action: [admin ? "dsql:DbConnectAdmin" : "dsql:DbConnect"], |
| 46 | Resource: [cluster.clusterArn], |
| 47 | }, |
| 48 | ], |
| 49 | }); |
| 50 | } |
| 51 | |
| 52 | const username = admin ? "admin" : options?.username; |
| 53 | const database = options?.database ?? "postgres"; |
| 54 | |
| 55 | return Effect.gen(function* () { |
| 56 | const host = yield* Host; |
| 57 | if (!host) { |
| 58 | return yield* Effect.die(`DSQL endpoint for '${cluster.LogicalId}' is not available yet`); |
| 59 | } |
| 60 | const password = yield* generateDbAuthToken({ |
| 61 | service: "dsql", |
| 62 | hostname: host, |
| 63 | action: admin ? "DbConnectAdmin" : "DbConnect", |
| 64 | }).pipe(Effect.provideContext(services)); |
| 65 | return { |
| 66 | host, |
| 67 | port: DSQL_PORT, |
| 68 | database, |
| 69 | username, |
| 70 | password, |
| 71 | ssl: true, |
| 72 | url: formatSqlConnectionUrl({ |
| 73 | host, |
| 74 | port: DSQL_PORT, |
| 75 | database, |
| 76 | username, |
| 77 | password, |
| 78 | ssl: true, |
| 79 | }), |
| 80 | } satisfies SqlConnectionInfo; |
| 81 | }); |
| 82 | }); |
| 83 | }), |
| 84 | ); |