1import * as Lambda from "@/AWS/Lambda";
2import * as OAM from "@/AWS/OAM";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
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, "handler.ts");
10
11export class OamTestFunction extends Lambda.Function<Lambda.Function>()(
12 "OamTestFunction",
13) {}
14
15export default OamTestFunction.make(
16 {
17 main,
18 functionUrl: true,
19 timeout: Duration.seconds(30),
20 },
21 Effect.gen(function* () {
22 // The monitoring-account sink the binding is bound to. Nothing ever
23 // links to it (OAM rejects same-account links), so ListAttachedLinks
24 // legitimately returns an empty item list — which still proves the IAM
25 // grant and the SinkIdentifier injection end-to-end.
26 const sink = yield* OAM.Sink("BindingSink");
27
28 const listAttachedLinks = yield* OAM.ListAttachedLinks(sink);
29
30 const bound = { listAttachedLinks };
31
32 return {
33 fetch: Effect.gen(function* () {
34 const request = yield* HttpServerRequest;
35 const url = new URL(request.originalUrl);
36 const pathname = url.pathname;
37
38 if (request.method === "GET" && pathname === "/bindings") {
39 return yield* HttpServerResponse.json({
40 bound: Object.keys(bound),
41 });
42 }
43
44 // Sink-scoped list: the SinkIdentifier is injected from the binding.
45 if (request.method === "GET" && pathname === "/attached-links") {
46 const { Items } = yield* listAttachedLinks();
47 return yield* HttpServerResponse.json({
48 count: Items.length,
49 linkArns: Items.map((item) => item.LinkArn),
50 });
51 }
52
53 return yield* HttpServerResponse.json(
54 { error: "Not found", method: request.method, pathname },
55 { status: 404 },
56 );
57 }).pipe(Effect.orDie),
58 };
59 }).pipe(Effect.provide(OAM.ListAttachedLinksHttp)),
60);