| 1 | import * as Lambda from "@/AWS/Lambda"; |
| 2 | import * as S3 from "@/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 | |
| 7 | export class HeadObjectTestFunction extends Lambda.Function<HeadObjectTestFunction>()( |
| 8 | "HeadObjectTestFunction", |
| 9 | ) {} |
| 10 | |
| 11 | export default HeadObjectTestFunction.make( |
| 12 | { main: import.meta.url, functionUrl: true }, |
| 13 | Effect.gen(function* () { |
| 14 | const bucket = yield* S3.Bucket("HeadObjectBucket", { |
| 15 | forceDestroy: true, |
| 16 | versioning: "Enabled", |
| 17 | }); |
| 18 | const headObject = yield* S3.HeadObject(bucket); |
| 19 | const bucketName = yield* bucket.bucketName; |
| 20 | const bucketArn = yield* bucket.bucketArn; |
| 21 | |
| 22 | return { |
| 23 | fetch: Effect.gen(function* () { |
| 24 | const request = yield* HttpServerRequest; |
| 25 | const url = yield* Effect.sync(() => new URL(request.originalUrl)); |
| 26 | if (url.pathname === "/info") { |
| 27 | return yield* HttpServerResponse.json({ |
| 28 | bucketName: yield* bucketName, |
| 29 | bucketArn: yield* bucketArn, |
| 30 | }); |
| 31 | } |
| 32 | const key = url.searchParams.get("key"); |
| 33 | if (!key) |
| 34 | return HttpServerResponse.text("Missing key", { status: 400 }); |
| 35 | return yield* headObject({ |
| 36 | Key: key, |
| 37 | VersionId: url.searchParams.get("versionId") ?? undefined, |
| 38 | }).pipe( |
| 39 | Effect.flatMap((result) => |
| 40 | HttpServerResponse.json({ |
| 41 | contentLength: result.ContentLength, |
| 42 | contentType: result.ContentType, |
| 43 | versionId: result.VersionId, |
| 44 | }), |
| 45 | ), |
| 46 | Effect.catchTag("NotFound", () => |
| 47 | HttpServerResponse.json({ tag: "NotFound" }, { status: 404 }), |
| 48 | ), |
| 49 | ); |
| 50 | }).pipe(Effect.orDie), |
| 51 | }; |
| 52 | }).pipe(Effect.provide(S3.HeadObjectHttp)), |
| 53 | ); |