@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 111 lines 4.64 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as RDS from "@alchemy.run/aws/RDS";
3 import * as Drizzle from "@alchemy.run/drizzle/Postgres";
4 import { sql } from "drizzle-orm";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Redacted from "effect/Redacted";
10 import path from "pathe";
11 import { RDSDataInfra } from "./infra.ts";
12
13 const main = path.resolve(import.meta.dirname, "drizzle-iam-handler.ts");
14
15 export class RDSDrizzleIamFunction extends Lambda.Function<Lambda.Function>()(
16 "RDSDrizzleIamFunction",
17 ) {}
18
19 /**
20 * Lambda fixture for the `RDS.Connect` IAM-auth path (db-drivers §3
21 * "RDS/Aurora"):
22 *
23 * - `auth: "iam"` — the deploy half binds `rds-db:connect` on the cluster's
24 * `dbuser:{resourceId}/app_iam`; the runtime half presigns a 15-minute
25 * token as the password (no Secrets Manager call).
26 * - `subnetIds`/`securityGroupIds` attach THIS function to the fixture VPC
27 * through the Function binding contract's `vpc` channel (DECISION #5) —
28 * the isolated VPC is fine because IAM-auth needs zero AWS API calls at
29 * runtime (the presign is local; credentials come from the Lambda env).
30 * - `Drizzle.Postgres(url)` proves the `ConnectionInfo.url` composition:
31 * the pool dials the cluster endpoint over a real socket per execution.
32 *
33 * Shares the Aurora infra with the RDSData fixture (`infra.ts`); the
34 * `app_iam` user is bootstrapped by that fixture's `/setup-iam-user` route
35 * (Data API), since this VPC-attached function cannot reach the Data API.
36 */
37 export default RDSDrizzleIamFunction.make(
38 {
39 main,
40 functionUrl: true,
41 // First query per execution builds the pool + TLS handshake while the
42 // serverless cluster may be scaling from idle.
43 timeout: Duration.seconds(60),
44 },
45 Effect.gen(function* () {
46 const { cluster, subnetA, subnetB, lambdaSecurityGroup } = yield* RDSDataInfra;
47
48 const connect = yield* RDS.Connect(cluster, {
49 auth: "iam",
50 username: "app_iam",
51 database: "app",
52 subnetIds: [subnetA.subnetId, subnetB.subnetId],
53 securityGroupIds: [lambdaSecurityGroup.groupId],
54 });
55
56 // `url` embeds the freshly minted token; the pool is per-execution
57 // (ExecutionMemo), so every invoke re-runs `connect` and re-mints.
58 const db = yield* Drizzle.Postgres(Effect.map(connect, (info) => info.url));
59
60 return {
61 fetch: Effect.gen(function* () {
62 const request = yield* HttpServerRequest;
63 const url = new URL(request.originalUrl);
64 const pathname = url.pathname;
65
66 // Token-minting proof without opening a socket: the password is a
67 // presigned `rds-db` URL (never echoed — only its shape), and
68 // `refreshPassword` re-mints for serverful lazy-password hooks.
69 if (request.method === "GET" && pathname === "/connect-info") {
70 const info = yield* connect;
71 const refreshed = info.refreshPassword
72 ? Redacted.value(yield* info.refreshPassword)
73 : undefined;
74 return yield* HttpServerResponse.json({
75 host: info.host,
76 port: info.port,
77 database: info.database,
78 username: info.username,
79 hasToken:
80 typeof info.password === "string" && info.password.includes("X-Amz-Signature="),
81 ssl: info.ssl,
82 canRefresh: refreshed !== undefined && refreshed.includes("X-Amz-Signature="),
83 });
84 }
85
86 // Socket round-trip: IAM token auth + TLS + in-VPC connectivity.
87 if (request.method === "GET" && pathname === "/drizzle-health") {
88 // `"objects"` mode resolves to the row array; the default `"raw"`
89 // mode resolves to pg's whole QueryResult (`{ rows, rowCount, ... }`).
90 const rows = yield* db.execute<{ one: number }>(sql`select 1 as one`, "objects");
91 return yield* HttpServerResponse.json({ rows });
92 }
93
94 // Cross-path consistency: rows written via the Data API fixture are
95 // visible over the wire-protocol connection.
96 if (request.method === "GET" && pathname === "/drizzle-todos") {
97 const rows = yield* db.execute<{ count: number }>(
98 sql`select count(*)::int as count from todos`,
99 "objects",
100 );
101 return yield* HttpServerResponse.json({ rows });
102 }
103
104 return yield* HttpServerResponse.json(
105 { error: "Not found", method: request.method, pathname },
106 { status: 404 },
107 );
108 }).pipe(Effect.orDie),
109 };
110 }).pipe(Effect.provide(RDS.ConnectHttp)),
111 );