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

main
ts 103 lines 3.38 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as MediaLive from "@alchemy.run/aws/MediaLive";
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 * as Stream from "effect/Stream";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class MediaLiveTestFunction extends Lambda.Function<Lambda.Function>()(
13 "MediaLiveTestFunction",
14 ) {}
15
16 export default MediaLiveTestFunction.make(
17 {
18 main,
19 functionUrl: true,
20 },
21 Effect.gen(function* () {
22 // The input the input-scoped binding is bound to. A URL_PULL input is
23 // free, provisions in seconds, and needs no security group.
24 const input = yield* MediaLive.Input("BindingInput", {
25 type: "URL_PULL",
26 sources: [{ Url: "https://example.com/stream/index.m3u8" }],
27 tags: { fixture: "medialive-bindings" },
28 });
29
30 // Event source: subscribe the host to MediaLive state-change/alert
31 // events. The deploy proves the EventBridge rule + invoke permission
32 // wiring.
33 yield* MediaLive.consumeChannelEvents({ kinds: ["state-change", "alert"] }, (events) =>
34 Stream.runForEach(events, (event) =>
35 Effect.log(
36 `medialive event: ${event["detail-type"]} ${event.detail.state ?? event.detail.message ?? ""}`,
37 ),
38 ),
39 );
40
41 const describeInput = yield* MediaLive.DescribeInput(input);
42 const listChannels = yield* MediaLive.ListChannels();
43 const listInputs = yield* MediaLive.ListInputs();
44
45 const bound = {
46 describeInput,
47 listChannels,
48 listInputs,
49 };
50
51 return {
52 fetch: Effect.gen(function* () {
53 const request = yield* HttpServerRequest;
54 const url = new URL(request.originalUrl);
55 const pathname = url.pathname;
56
57 if (request.method === "GET" && pathname === "/bindings") {
58 return yield* HttpServerResponse.json({
59 bound: Object.keys(bound),
60 });
61 }
62
63 // Read the input's live state — the fixture input idles DETACHED.
64 if (request.method === "GET" && pathname === "/input") {
65 const current = yield* describeInput();
66 return yield* HttpServerResponse.json({
67 state: current.State,
68 type: current.Type,
69 sourceUrls: (current.Sources ?? []).map((s) => s.Url),
70 });
71 }
72
73 // Account-level enumerations.
74 if (request.method === "GET" && pathname === "/channels") {
75 const { Channels } = yield* listChannels({ MaxResults: 20 });
76 return yield* HttpServerResponse.json({
77 count: (Channels ?? []).length,
78 });
79 }
80 if (request.method === "GET" && pathname === "/inputs") {
81 const { Inputs } = yield* listInputs({ MaxResults: 20 });
82 return yield* HttpServerResponse.json({
83 count: (Inputs ?? []).length,
84 });
85 }
86
87 return yield* HttpServerResponse.json(
88 { error: "Not found", method: request.method, pathname },
89 { status: 404 },
90 );
91 }).pipe(Effect.orDie),
92 };
93 }).pipe(
94 Effect.provide(
95 Layer.mergeAll(
96 Lambda.EventSource,
97 MediaLive.DescribeInputHttp,
98 MediaLive.ListChannelsHttp,
99 MediaLive.ListInputsHttp,
100 ),
101 ),
102 ),
103 );