@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 92 lines 4.1 KB
Raw
1 import * as DSQL from "@alchemy.run/aws/DSQL";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as PgClient from "@effect/sql-pg/PgClient";
4 import * as Context from "effect/Context";
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 Layer from "effect/Layer";
10 import * as Redacted from "effect/Redacted";
11 import path from "pathe";
12 import { Db } from "./db.ts";
13
14 const main = path.resolve(import.meta.dirname, "direct-handler.ts");
15
16 export class DsqlDirectFunction extends Lambda.Function<Lambda.Function>()("DsqlDirectFunction") {}
17
18 /**
19 * Lambda fixture proving the DIRECT public-endpoint path: a raw Postgres
20 * client (`@effect/sql-pg`, no Drizzle, no Hyperdrive, no VPC) connects to
21 * `<clusterId>.dsql.<region>.on.aws:5432` over TLS with the IAM auth token
22 * that `DSQL.Connect` mints as the password.
23 */
24 export default DsqlDirectFunction.make(
25 {
26 main,
27 functionUrl: true,
28 timeout: Duration.seconds(30),
29 },
30 Effect.gen(function* () {
31 const cluster = yield* Db;
32 const conn = yield* DSQL.Connect(cluster, { admin: true });
33 const getVpcEndpointServiceName = yield* DSQL.GetVpcEndpointServiceName(cluster);
34
35 return {
36 fetch: Effect.gen(function* () {
37 const request = yield* HttpServerRequest;
38 const url = new URL(request.originalUrl);
39 const pathname = url.pathname;
40
41 // PrivateLink service-name lookup via the management binding.
42 if (request.method === "GET" && pathname === "/vpc-endpoint-service") {
43 const { serviceName, clusterVpcEndpoint } = yield* getVpcEndpointServiceName();
44 return yield* HttpServerResponse.json({
45 serviceName,
46 clusterVpcEndpoint: clusterVpcEndpoint ?? null,
47 });
48 }
49
50 // Connection descriptor shape — no socket opened. The password
51 // (auth token) is never echoed, only its presence.
52 if (request.method === "GET" && pathname === "/info") {
53 const info = yield* conn;
54 return yield* HttpServerResponse.json({
55 host: info.host,
56 port: info.port,
57 database: info.database,
58 username: info.username,
59 hasPassword: info.password !== undefined && Redacted.value(info.password).length > 0,
60 ssl: info.ssl,
61 urlScheme: Redacted.value(info.url).split("://")[0],
62 });
63 }
64
65 // Full round-trip over the public endpoint: mint token, open a TLS
66 // socket, CREATE TABLE / INSERT / SELECT. The pool is built on the
67 // per-event scope (`Layer.build` + ambient request Scope), so it is
68 // closed when the invocation settles.
69 if (request.method === "POST" && pathname === "/roundtrip") {
70 const info = yield* conn;
71 // Plain `PgClient` over the DSQL URL (`sslmode=require`): DSQL
72 // routes on TLS SNI, which `@effect/sql-pg` ≥ rc.115 sends for
73 // DNS hosts by default (Effect-TS/effect#8174).
74 const ctx = yield* Layer.build(PgClient.layer({ url: info.url }));
75 const sqlClient = Context.get(ctx, PgClient.PgClient);
76 // DSQL runs DDL as its own autocommit statement (no DDL+DML
77 // transactions) — each call below is a separate statement.
78 yield* sqlClient`CREATE TABLE IF NOT EXISTS dsql_direct_widgets (id integer PRIMARY KEY, title text NOT NULL)`;
79 yield* sqlClient`DELETE FROM dsql_direct_widgets WHERE id = 1`;
80 yield* sqlClient`INSERT INTO dsql_direct_widgets (id, title) VALUES (1, 'direct')`;
81 const rows = yield* sqlClient`SELECT id, title FROM dsql_direct_widgets WHERE id = 1`;
82 return yield* HttpServerResponse.json({ rows, host: info.host });
83 }
84
85 return yield* HttpServerResponse.json(
86 { error: "Not found", method: request.method, pathname },
87 { status: 404 },
88 );
89 }).pipe(Effect.orDie),
90 };
91 }).pipe(Effect.provide(Layer.mergeAll(DSQL.ConnectHttp, DSQL.GetVpcEndpointServiceNameHttp))),
92 );