The best way to control Claude Code, Codex and more from any computer or phone

main
ts 233 lines 9.25 KB
Raw
1 import * as Glacier from "@alchemy.run/aws/Glacier";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
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 // A syntactically-valid-looking but nonexistent 138-char job/upload id.
12 const BOGUS_ID = "alchemy-nonexistent-glacier-id-000000000000000000000000000000000000";
13
14 export class GlacierBindingsFunction extends Lambda.Function<Lambda.Function>()(
15 "GlacierBindingsFunction",
16 ) {}
17
18 export default GlacierBindingsFunction.make(
19 {
20 main,
21 functionUrl: true,
22 },
23 Effect.gen(function* () {
24 const vault = yield* Glacier.Vault("BindingsVault");
25
26 const describeVault = yield* Glacier.DescribeVault(vault);
27 const uploadArchive = yield* Glacier.UploadArchive(vault);
28 const deleteArchive = yield* Glacier.DeleteArchive(vault);
29 const initiateJob = yield* Glacier.InitiateJob(vault);
30 const describeJob = yield* Glacier.DescribeJob(vault);
31 const listJobs = yield* Glacier.ListJobs(vault);
32 const getJobOutput = yield* Glacier.GetJobOutput(vault);
33 const initiateMultipartUpload = yield* Glacier.InitiateMultipartUpload(vault);
34 const uploadMultipartPart = yield* Glacier.UploadMultipartPart(vault);
35 const completeMultipartUpload = yield* Glacier.CompleteMultipartUpload(vault);
36 const abortMultipartUpload = yield* Glacier.AbortMultipartUpload(vault);
37 const listMultipartUploads = yield* Glacier.ListMultipartUploads(vault);
38 const listParts = yield* Glacier.ListParts(vault);
39
40 const bound = {
41 describeVault,
42 uploadArchive,
43 deleteArchive,
44 initiateJob,
45 describeJob,
46 listJobs,
47 getJobOutput,
48 initiateMultipartUpload,
49 uploadMultipartPart,
50 completeMultipartUpload,
51 abortMultipartUpload,
52 listMultipartUploads,
53 listParts,
54 };
55
56 return {
57 fetch: Effect.gen(function* () {
58 const request = yield* HttpServerRequest;
59 const url = new URL(request.originalUrl);
60 const pathname = url.pathname;
61
62 if (request.method === "GET" && pathname === "/bindings") {
63 return yield* HttpServerResponse.json({
64 bound: Object.keys(bound),
65 });
66 }
67
68 if (request.method === "GET" && pathname === "/vault") {
69 // vaultName injection scopes the call to the bound vault.
70 const response = yield* describeVault();
71 return yield* HttpServerResponse.json({
72 vaultName: response.VaultName,
73 numberOfArchives: response.NumberOfArchives ?? 0,
74 });
75 }
76
77 if (request.method === "GET" && pathname === "/jobs") {
78 const response = yield* listJobs();
79 return yield* HttpServerResponse.json({
80 count: (response.JobList ?? []).length,
81 });
82 }
83
84 if (request.method === "GET" && pathname === "/uploads") {
85 const response = yield* listMultipartUploads();
86 return yield* HttpServerResponse.json({
87 count: (response.UploadsList ?? []).length,
88 });
89 }
90
91 // Each typed-not-found route proves the grant + injection
92 // end-to-end: an IAM gap would surface AccessDeniedException (a
93 // 500), while the typed ResourceNotFoundException proves the request
94 // reached the vault-scoped API.
95 if (request.method === "GET" && pathname === "/jobs/typed-not-found") {
96 const typed = yield* describeJob({ jobId: BOGUS_ID }).pipe(
97 Effect.map(() => false),
98 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
99 );
100 return yield* HttpServerResponse.json({ typed });
101 }
102
103 if (request.method === "GET" && pathname === "/job-output/typed-not-found") {
104 const typed = yield* getJobOutput({ jobId: BOGUS_ID }).pipe(
105 Effect.map(() => false),
106 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
107 );
108 return yield* HttpServerResponse.json({ typed });
109 }
110
111 if (request.method === "GET" && pathname === "/parts/typed-not-found") {
112 const typed = yield* listParts({ uploadId: BOGUS_ID }).pipe(
113 Effect.map(() => false),
114 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
115 );
116 return yield* HttpServerResponse.json({ typed });
117 }
118
119 if (request.method === "POST" && pathname === "/multipart/typed-not-found") {
120 // All three mutation ops on a nonexistent upload id round-trip to
121 // the typed ResourceNotFoundException without writing anything —
122 // the vault stays empty so stack.destroy can delete it.
123 const upload = yield* uploadMultipartPart({
124 uploadId: BOGUS_ID,
125 range: "bytes 0-1048575/*",
126 }).pipe(
127 Effect.map(() => false),
128 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
129 );
130 const complete = yield* completeMultipartUpload({
131 uploadId: BOGUS_ID,
132 }).pipe(
133 Effect.map(() => false),
134 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
135 );
136 const abort = yield* abortMultipartUpload({
137 uploadId: BOGUS_ID,
138 }).pipe(
139 Effect.map(() => false),
140 Effect.catchTag("ResourceNotFoundException", () => Effect.succeed(true)),
141 );
142 return yield* HttpServerResponse.json({ upload, complete, abort });
143 }
144
145 if (request.method === "POST" && pathname === "/archive/typed-not-found") {
146 // DeleteArchive is documented idempotent for already-deleted
147 // archives, but a syntactically-bogus id surfaces the typed
148 // not-found / bad-parameter union — either tag proves the grant.
149 const typed = yield* deleteArchive({ archiveId: BOGUS_ID }).pipe(
150 Effect.map(() => false),
151 Effect.catchTag(["ResourceNotFoundException", "InvalidParameterValueException"], () =>
152 Effect.succeed(true),
153 ),
154 );
155 return yield* HttpServerResponse.json({ typed });
156 }
157
158 if (request.method === "POST" && pathname === "/archive/probe") {
159 // UploadArchive with a deliberately-wrong checksum: the service
160 // rejects the write with a typed 400 before storing anything, so
161 // the vault deletes cleanly at stack.destroy. A successful write
162 // here would block DeleteVault for ~24h (until the next
163 // inventory).
164 const typed = yield* uploadArchive({
165 checksum: "0".repeat(64),
166 body: "alchemy-glacier-binding-probe",
167 }).pipe(
168 Effect.map(() => false),
169 Effect.catchTag(
170 ["InvalidParameterValueException", "MissingParameterValueException"],
171 () => Effect.succeed(true),
172 ),
173 );
174 return yield* HttpServerResponse.json({ typed });
175 }
176
177 if (request.method === "POST" && pathname === "/multipart/probe") {
178 // Initiate requires a power-of-two MiB part size; a size of 1 is
179 // rejected with the typed 400 before any upload is created.
180 const typed = yield* initiateMultipartUpload({
181 archiveDescription: "alchemy-glacier-binding-probe",
182 partSize: "1",
183 }).pipe(
184 Effect.map(() => false),
185 Effect.catchTag(
186 ["InvalidParameterValueException", "MissingParameterValueException"],
187 () => Effect.succeed(true),
188 ),
189 );
190 return yield* HttpServerResponse.json({ typed });
191 }
192
193 if (request.method === "POST" && pathname === "/jobs/inventory") {
194 // A brand-new vault has no inventory yet — Glacier rejects the
195 // inventory-retrieval with a typed 400 until the first nightly
196 // inventory runs. Either branch proves InitiateJob's grant.
197 const result = yield* initiateJob({
198 jobParameters: { Type: "inventory-retrieval" },
199 }).pipe(
200 Effect.map((r) => ({ started: true, jobId: r.jobId })),
201 Effect.catchTag(["InvalidParameterValueException", "ResourceNotFoundException"], (e) =>
202 Effect.succeed({ started: false, error: e._tag }),
203 ),
204 );
205 return yield* HttpServerResponse.json(result);
206 }
207
208 return yield* HttpServerResponse.json(
209 { error: "Not found", method: request.method, pathname },
210 { status: 404 },
211 );
212 }).pipe(Effect.orDie),
213 };
214 }).pipe(
215 Effect.provide(
216 Layer.mergeAll(
217 Glacier.DescribeVaultHttp,
218 Glacier.UploadArchiveHttp,
219 Glacier.DeleteArchiveHttp,
220 Glacier.InitiateJobHttp,
221 Glacier.DescribeJobHttp,
222 Glacier.ListJobsHttp,
223 Glacier.GetJobOutputHttp,
224 Glacier.InitiateMultipartUploadHttp,
225 Glacier.UploadMultipartPartHttp,
226 Glacier.CompleteMultipartUploadHttp,
227 Glacier.AbortMultipartUploadHttp,
228 Glacier.ListMultipartUploadsHttp,
229 Glacier.ListPartsHttp,
230 ),
231 ),
232 ),
233 );