1import * as Lambda from "@/AWS/Lambda";
2import * as S3Tables from "@/AWS/S3Tables";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import { HttpServerRequest } from "effect/http/HttpServerRequest";
6import * as HttpServerResponse from "effect/http/HttpServerResponse";
7import path from "pathe";
8
9const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
10
11export class S3TablesBindingsFunction extends Lambda.Function<Lambda.Function>()(
12 "S3TablesBindingsFunction",
13) {}
14
15export 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 =
46 yield* S3Tables.GetTableMetadataLocation(table);
47 const updateTableMetadataLocation =
48 yield* S3Tables.UpdateTableMetadataLocation(table);
49 const getTableMaintenanceJobStatus =
50 yield* S3Tables.GetTableMaintenanceJobStatus(table);
51
52 const bound = {
53 listNamespaces,
54 listTables,
55 getTable,
56 getTableMetadataLocation,
57 updateTableMetadataLocation,
58 getTableMaintenanceJobStatus,
59 };
60
61 return {
62 fetch: Effect.gen(function* () {
63 const request = yield* HttpServerRequest;
64 const url = new URL(request.originalUrl);
65 const pathname = url.pathname;
66
67 if (request.method === "GET" && pathname === "/bindings") {
68 return yield* HttpServerResponse.json({
69 bound: Object.keys(bound),
70 });
71 }
72
73 if (request.method === "GET" && pathname === "/namespaces") {
74 // tableBucketARN injection scopes the listing to the bound bucket.
75 const response = yield* listNamespaces();
76 return yield* HttpServerResponse.json({
77 names: response.namespaces.map((ns) => ns.namespace[0]),
78 });
79 }
80
81 if (request.method === "GET" && pathname === "/tables") {
82 const response = yield* listTables({ namespace: "bindings" });
83 return yield* HttpServerResponse.json({
84 names: response.tables.map((t) => t.name),
85 });
86 }
87
88 if (request.method === "GET" && pathname === "/table") {
89 const response = yield* getTable();
90 return yield* HttpServerResponse.json({
91 name: response.name,
92 format: response.format,
93 versionToken: response.versionToken,
94 warehouseLocation: response.warehouseLocation,
95 });
96 }
97
98 if (request.method === "GET" && pathname === "/metadata-location") {
99 const response = yield* getTableMetadataLocation();
100 return yield* HttpServerResponse.json({
101 versionToken: response.versionToken,
102 metadataLocation: response.metadataLocation ?? null,
103 warehouseLocation: response.warehouseLocation,
104 });
105 }
106
107 if (
108 request.method === "POST" &&
109 pathname === "/metadata-location/commit"
110 ) {
111 // The Iceberg commit protocol round-trip: read the current version
112 // token, then commit a metadata pointer with it. The fixture
113 // doesn't write a real Iceberg metadata file, so the service may
114 // reject the pointer with a typed BadRequestException — either
115 // outcome proves the grant and identifier injection end-to-end (an
116 // IAM gap would surface AccessDeniedException as a 500 instead).
117 const current = yield* getTableMetadataLocation();
118 const result = yield* updateTableMetadataLocation({
119 versionToken: current.versionToken,
120 metadataLocation: `${current.warehouseLocation}/metadata/00001-alchemy-bindings-test.metadata.json`,
121 }).pipe(
122 Effect.map((r) => ({
123 committed: true,
124 errorTag: null as string | null,
125 versionTokenChanged: r.versionToken !== current.versionToken,
126 })),
127 Effect.catchTag(
128 ["BadRequestException", "ConflictException", "NotFoundException"],
129 (e) =>
130 Effect.succeed({
131 committed: false,
132 errorTag: e._tag as string | null,
133 versionTokenChanged: false,
134 }),
135 ),
136 );
137 return yield* HttpServerResponse.json(result);
138 }
139
140 if (request.method === "GET" && pathname === "/maintenance-jobs") {
141 const response = yield* getTableMaintenanceJobStatus();
142 return yield* HttpServerResponse.json({
143 tableArn: response.tableARN,
144 jobs: Object.keys(response.status),
145 });
146 }
147
148 return yield* HttpServerResponse.json(
149 { error: "Not found", method: request.method, pathname },
150 { status: 404 },
151 );
152 }).pipe(Effect.orDie),
153 };
154 }).pipe(
155 Effect.provide(
156 Layer.mergeAll(
157 S3Tables.ListNamespacesHttp,
158 S3Tables.ListTablesHttp,
159 S3Tables.GetTableHttp,
160 S3Tables.GetTableMetadataLocationHttp,
161 S3Tables.UpdateTableMetadataLocationHttp,
162 S3Tables.GetTableMaintenanceJobStatusHttp,
163 ),
164 ),
165 ),
166);