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

main
ts 300 lines 12 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as S3 from "@alchemy.run/aws/S3";
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
8 export class S3PresignTestFunction extends Lambda.Function<S3PresignTestFunction>()(
9 "S3PresignTestFunction",
10 ) {}
11
12 export default S3PresignTestFunction.make(
13 {
14 main: import.meta.url,
15 functionUrl: true,
16 },
17 Effect.gen(function* () {
18 const bucket = yield* S3.Bucket("PresignBucket", {
19 forceDestroy: true,
20 });
21
22 const presignGetObject = yield* S3.PresignGetObject(bucket);
23 const presignPutObject = yield* S3.PresignPutObject(bucket);
24 const deleteObjects = yield* S3.DeleteObjects(bucket);
25 const getObjectTagging = yield* S3.GetObjectTagging(bucket);
26 const putObjectTagging = yield* S3.PutObjectTagging(bucket);
27 const deleteObjectTagging = yield* S3.DeleteObjectTagging(bucket);
28 const getObjectAttributes = yield* S3.GetObjectAttributes(bucket);
29 const listObjectVersions = yield* S3.ListObjectVersions(bucket);
30 const createMultipartUpload = yield* S3.CreateMultipartUpload(bucket);
31 const uploadPart = yield* S3.UploadPart(bucket);
32 const uploadPartCopy = yield* S3.UploadPartCopy(bucket);
33 const listParts = yield* S3.ListParts(bucket);
34 const listMultipartUploads = yield* S3.ListMultipartUploads(bucket);
35 const completeMultipartUpload = yield* S3.CompleteMultipartUpload(bucket);
36 const abortMultipartUpload = yield* S3.AbortMultipartUpload(bucket);
37 const restoreObject = yield* S3.RestoreObject(bucket);
38 const getObjectRetention = yield* S3.GetObjectRetention(bucket);
39 const putObjectRetention = yield* S3.PutObjectRetention(bucket);
40 const getObjectLegalHold = yield* S3.GetObjectLegalHold(bucket);
41 const putObjectLegalHold = yield* S3.PutObjectLegalHold(bucket);
42 const BucketName = yield* bucket.bucketName;
43
44 return {
45 fetch: Effect.gen(function* () {
46 const request = yield* HttpServerRequest;
47 const url = new URL(request.originalUrl);
48 const pathname = url.pathname;
49
50 // The first event after a cold start can observe not-yet-hydrated
51 // resource Outputs (they resolve to `undefined` until the runtime
52 // finishes hydrating resource state). Answer 503 so callers retry
53 // instead of receiving presigned URLs for a bucket named
54 // "undefined".
55 const bucketName = yield* BucketName;
56 if (!bucketName) {
57 return HttpServerResponse.text("Outputs not hydrated yet", {
58 status: 503,
59 });
60 }
61
62 const key = url.searchParams.get("key");
63 const requireKey = () => HttpServerResponse.text("Missing key", { status: 400 });
64
65 if (request.method === "GET" && pathname === "/bucket-name") {
66 return yield* HttpServerResponse.json({ bucketName });
67 }
68
69 if (request.method === "GET" && pathname === "/presign-get") {
70 if (!key) return requireKey();
71 const expiresIn = url.searchParams.get("expiresIn");
72 const contentType = url.searchParams.get("contentType");
73 const presignedUrl = yield* presignGetObject({
74 key,
75 expiresIn: expiresIn ? Number(expiresIn) : undefined,
76 contentType: contentType ?? undefined,
77 });
78 return yield* HttpServerResponse.json({ url: presignedUrl });
79 }
80
81 if (request.method === "GET" && pathname === "/presign-put") {
82 if (!key) return requireKey();
83 const expiresIn = url.searchParams.get("expiresIn");
84 const contentType = url.searchParams.get("contentType");
85 const presignedUrl = yield* presignPutObject({
86 key,
87 expiresIn: expiresIn ? Number(expiresIn) : undefined,
88 contentType: contentType ?? undefined,
89 });
90 return yield* HttpServerResponse.json({ url: presignedUrl });
91 }
92
93 if (request.method === "GET" && pathname === "/delete-objects") {
94 const keys = (url.searchParams.get("keys") ?? "").split(",").filter((k) => k.length > 0);
95 const result = yield* deleteObjects({
96 Delete: { Objects: keys.map((Key) => ({ Key })) },
97 });
98 return yield* HttpServerResponse.json({
99 deleted: (result.Deleted ?? []).map((d) => d.Key),
100 errors: (result.Errors ?? []).map((e) => e.Code),
101 });
102 }
103
104 if (request.method === "GET" && pathname === "/put-tagging") {
105 if (!key) return requireKey();
106 const tagKey = url.searchParams.get("tagKey") ?? "status";
107 const tagValue = url.searchParams.get("tagValue") ?? "test";
108 yield* putObjectTagging({
109 Key: key,
110 Tagging: { TagSet: [{ Key: tagKey, Value: tagValue }] },
111 });
112 return yield* HttpServerResponse.json({ ok: true });
113 }
114
115 if (request.method === "GET" && pathname === "/get-tagging") {
116 if (!key) return requireKey();
117 const result = yield* getObjectTagging({ Key: key });
118 return yield* HttpServerResponse.json({
119 tags: Object.fromEntries((result.TagSet ?? []).map((t) => [t.Key, t.Value])),
120 });
121 }
122
123 if (request.method === "GET" && pathname === "/delete-tagging") {
124 if (!key) return requireKey();
125 yield* deleteObjectTagging({ Key: key });
126 return yield* HttpServerResponse.json({ ok: true });
127 }
128
129 if (request.method === "GET" && pathname === "/attributes") {
130 if (!key) return requireKey();
131 const result = yield* getObjectAttributes({
132 Key: key,
133 ObjectAttributes: ["ObjectSize", "ETag", "StorageClass"],
134 });
135 return yield* HttpServerResponse.json({
136 size: result.ObjectSize,
137 etag: result.ETag,
138 storageClass: result.StorageClass ?? "STANDARD",
139 });
140 }
141
142 if (request.method === "GET" && pathname === "/versions") {
143 const prefix = url.searchParams.get("prefix") ?? undefined;
144 const result = yield* listObjectVersions({ Prefix: prefix });
145 return yield* HttpServerResponse.json({
146 versions: (result.Versions ?? []).map((v) => v.Key),
147 deleteMarkers: (result.DeleteMarkers ?? []).length,
148 });
149 }
150
151 if (request.method === "GET" && pathname === "/multipart-list") {
152 if (!key) return requireKey();
153 // create → upload one part → observe via ListParts and
154 // ListMultipartUploads → abort (leave nothing behind).
155 const created = yield* createMultipartUpload({ Key: key });
156 const uploadId = created.UploadId!;
157 yield* uploadPart({
158 Key: key,
159 UploadId: uploadId,
160 PartNumber: 1,
161 Body: "multipart list probe",
162 });
163 const parts = yield* listParts({ Key: key, UploadId: uploadId });
164 const uploads = yield* listMultipartUploads({ Prefix: key });
165 yield* abortMultipartUpload({ Key: key, UploadId: uploadId });
166 return yield* HttpServerResponse.json({
167 parts: (parts.Parts ?? []).map((p) => p.PartNumber),
168 uploads: (uploads.Uploads ?? []).map((u) => u.Key),
169 });
170 }
171
172 if (request.method === "GET" && pathname === "/multipart-copy") {
173 const src = url.searchParams.get("src");
174 const dest = url.searchParams.get("dest");
175 if (!src || !dest) {
176 return HttpServerResponse.text("Missing src/dest", {
177 status: 400,
178 });
179 }
180 // single part sourced via UploadPartCopy (the only part may be
181 // any size), then complete.
182 const created = yield* createMultipartUpload({ Key: dest });
183 const uploadId = created.UploadId!;
184 const copied = yield* uploadPartCopy({
185 Key: dest,
186 UploadId: uploadId,
187 PartNumber: 1,
188 CopySource: `${bucketName}/${src}`,
189 });
190 const completed = yield* completeMultipartUpload({
191 Key: dest,
192 UploadId: uploadId,
193 MultipartUpload: {
194 Parts: [{ ETag: copied.CopyPartResult!.ETag!, PartNumber: 1 }],
195 },
196 });
197 return yield* HttpServerResponse.json({ etag: completed.ETag });
198 }
199
200 // The routes below exercise bindings whose success path needs an
201 // archived object or an Object Lock bucket. They return the typed
202 // error tag so tests can assert the binding + IAM wiring works and
203 // the failure is the *expected, typed* platform rejection.
204 if (request.method === "GET" && pathname === "/restore") {
205 if (!key) return requireKey();
206 const outcome = yield* restoreObject({
207 Key: key,
208 RestoreRequest: {
209 Days: 1,
210 GlacierJobParameters: { Tier: "Standard" },
211 },
212 }).pipe(
213 Effect.map((): { tag: string } => ({ tag: "success" })),
214 Effect.catch((e) => Effect.succeed<{ tag: string }>({ tag: e._tag })),
215 );
216 return yield* HttpServerResponse.json(outcome);
217 }
218
219 if (request.method === "GET" && pathname === "/retention") {
220 if (!key) return requireKey();
221 const outcome = yield* getObjectRetention({ Key: key }).pipe(
222 Effect.map((): { tag: string } => ({ tag: "success" })),
223 Effect.catch((e) => Effect.succeed<{ tag: string }>({ tag: e._tag })),
224 );
225 return yield* HttpServerResponse.json(outcome);
226 }
227
228 if (request.method === "GET" && pathname === "/retention-put") {
229 if (!key) return requireKey();
230 const outcome = yield* putObjectRetention({
231 Key: key,
232 Retention: {
233 Mode: "GOVERNANCE",
234 RetainUntilDate: new Date(Date.now() + 60_000),
235 },
236 }).pipe(
237 Effect.map((): { tag: string } => ({ tag: "success" })),
238 Effect.catch((e) => Effect.succeed<{ tag: string }>({ tag: e._tag })),
239 );
240 return yield* HttpServerResponse.json(outcome);
241 }
242
243 if (request.method === "GET" && pathname === "/legal-hold") {
244 if (!key) return requireKey();
245 const outcome = yield* getObjectLegalHold({ Key: key }).pipe(
246 Effect.map((): { tag: string } => ({ tag: "success" })),
247 Effect.catch((e) => Effect.succeed<{ tag: string }>({ tag: e._tag })),
248 );
249 return yield* HttpServerResponse.json(outcome);
250 }
251
252 if (request.method === "GET" && pathname === "/legal-hold-put") {
253 if (!key) return requireKey();
254 const outcome = yield* putObjectLegalHold({
255 Key: key,
256 LegalHold: { Status: "ON" },
257 }).pipe(
258 Effect.map((): { tag: string } => ({ tag: "success" })),
259 Effect.catch((e) => Effect.succeed<{ tag: string }>({ tag: e._tag })),
260 );
261 return yield* HttpServerResponse.json(outcome);
262 }
263
264 return yield* HttpServerResponse.json(
265 {
266 error: "Not found",
267 method: request.method,
268 pathname,
269 },
270 { status: 404 },
271 );
272 }).pipe(Effect.orDie),
273 };
274 }).pipe(
275 Effect.provide(
276 Layer.mergeAll(
277 S3.PresignGetObjectHttp,
278 S3.PresignPutObjectHttp,
279 S3.DeleteObjectsHttp,
280 S3.GetObjectTaggingHttp,
281 S3.PutObjectTaggingHttp,
282 S3.DeleteObjectTaggingHttp,
283 S3.GetObjectAttributesHttp,
284 S3.ListObjectVersionsHttp,
285 S3.CreateMultipartUploadHttp,
286 S3.UploadPartHttp,
287 S3.UploadPartCopyHttp,
288 S3.ListPartsHttp,
289 S3.ListMultipartUploadsHttp,
290 S3.CompleteMultipartUploadHttp,
291 S3.AbortMultipartUploadHttp,
292 S3.RestoreObjectHttp,
293 S3.GetObjectRetentionHttp,
294 S3.PutObjectRetentionHttp,
295 S3.GetObjectLegalHoldHttp,
296 S3.PutObjectLegalHoldHttp,
297 ),
298 ),
299 ),
300 );