@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 160 lines 5.92 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as S3Tables from "@alchemy.run/aws/S3Tables";
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 path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
10
11 export class S3TablesBindingsFunction extends Lambda.Function<Lambda.Function>()(
12 "S3TablesBindingsFunction",
13 ) {}
14
15 export default S3TablesBindingsFunction.make(
16 {
17 main,
18 functionUrl: true,
19 },
20 Effect.gen(function* () {
21 // Deterministic names so a crashed previous run is reconciled (observed
22 // and reused) instead of orphaned by a fresh random instance suffix.
23 const bucket = yield* S3Tables.TableBucket("BindingsTableBucket", {
24 name: "alchemy-s3tables-bindings",
25 });
26 const namespace = yield* S3Tables.Namespace("BindingsNamespace", {
27 tableBucket: bucket.tableBucketArn,
28 namespace: "bindings",
29 });
30 const table = yield* S3Tables.Table("BindingsTable", {
31 tableBucket: bucket.tableBucketArn,
32 namespace: namespace.namespace,
33 name: "events",
34 schema: {
35 fields: [
36 { name: "id", type: "long", required: true },
37 { name: "payload", type: "string" },
38 ],
39 },
40 });
41
42 const listNamespaces = yield* S3Tables.ListNamespaces(bucket);
43 const listTables = yield* S3Tables.ListTables(bucket);
44 const getTable = yield* S3Tables.GetTable(table);
45 const getTableMetadataLocation = yield* S3Tables.GetTableMetadataLocation(table);
46 const updateTableMetadataLocation = yield* S3Tables.UpdateTableMetadataLocation(table);
47 const getTableMaintenanceJobStatus = yield* S3Tables.GetTableMaintenanceJobStatus(table);
48
49 const bound = {
50 listNamespaces,
51 listTables,
52 getTable,
53 getTableMetadataLocation,
54 updateTableMetadataLocation,
55 getTableMaintenanceJobStatus,
56 };
57
58 return {
59 fetch: Effect.gen(function* () {
60 const request = yield* HttpServerRequest;
61 const url = new URL(request.originalUrl);
62 const pathname = url.pathname;
63
64 if (request.method === "GET" && pathname === "/bindings") {
65 return yield* HttpServerResponse.json({
66 bound: Object.keys(bound),
67 });
68 }
69
70 if (request.method === "GET" && pathname === "/namespaces") {
71 // tableBucketARN injection scopes the listing to the bound bucket.
72 const response = yield* listNamespaces();
73 return yield* HttpServerResponse.json({
74 names: response.namespaces.map((ns) => ns.namespace[0]),
75 });
76 }
77
78 if (request.method === "GET" && pathname === "/tables") {
79 const response = yield* listTables({ namespace: "bindings" });
80 return yield* HttpServerResponse.json({
81 names: response.tables.map((t) => t.name),
82 });
83 }
84
85 if (request.method === "GET" && pathname === "/table") {
86 const response = yield* getTable();
87 return yield* HttpServerResponse.json({
88 name: response.name,
89 format: response.format,
90 versionToken: response.versionToken,
91 warehouseLocation: response.warehouseLocation,
92 });
93 }
94
95 if (request.method === "GET" && pathname === "/metadata-location") {
96 const response = yield* getTableMetadataLocation();
97 return yield* HttpServerResponse.json({
98 versionToken: response.versionToken,
99 metadataLocation: response.metadataLocation ?? null,
100 warehouseLocation: response.warehouseLocation,
101 });
102 }
103
104 if (request.method === "POST" && pathname === "/metadata-location/commit") {
105 // The Iceberg commit protocol round-trip: read the current version
106 // token, then commit a metadata pointer with it. The fixture
107 // doesn't write a real Iceberg metadata file, so the service may
108 // reject the pointer with a typed BadRequestException — either
109 // outcome proves the grant and identifier injection end-to-end (an
110 // IAM gap would surface AccessDeniedException as a 500 instead).
111 const current = yield* getTableMetadataLocation();
112 const result = yield* updateTableMetadataLocation({
113 versionToken: current.versionToken,
114 metadataLocation: `${current.warehouseLocation}/metadata/00001-alchemy-bindings-test.metadata.json`,
115 }).pipe(
116 Effect.map((r) => ({
117 committed: true,
118 errorTag: null as string | null,
119 versionTokenChanged: r.versionToken !== current.versionToken,
120 })),
121 Effect.catchTag(
122 ["BadRequestException", "ConflictException", "NotFoundException"],
123 (e) =>
124 Effect.succeed({
125 committed: false,
126 errorTag: e._tag as string | null,
127 versionTokenChanged: false,
128 }),
129 ),
130 );
131 return yield* HttpServerResponse.json(result);
132 }
133
134 if (request.method === "GET" && pathname === "/maintenance-jobs") {
135 const response = yield* getTableMaintenanceJobStatus();
136 return yield* HttpServerResponse.json({
137 tableArn: response.tableARN,
138 jobs: Object.keys(response.status),
139 });
140 }
141
142 return yield* HttpServerResponse.json(
143 { error: "Not found", method: request.method, pathname },
144 { status: 404 },
145 );
146 }).pipe(Effect.orDie),
147 };
148 }).pipe(
149 Effect.provide(
150 Layer.mergeAll(
151 S3Tables.ListNamespacesHttp,
152 S3Tables.ListTablesHttp,
153 S3Tables.GetTableHttp,
154 S3Tables.GetTableMetadataLocationHttp,
155 S3Tables.UpdateTableMetadataLocationHttp,
156 S3Tables.GetTableMaintenanceJobStatusHttp,
157 ),
158 ),
159 ),
160 );