The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 06:00 UTC
| 1 | import * as EMR from "@alchemy.run/aws/EMR"; |
| 2 | import * as Lambda from "@alchemy.run/aws/Lambda"; |
| 3 | import * as Duration from "effect/Duration"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import { HttpServerRequest } from "effect/http/HttpServerRequest"; |
| 6 | import * as HttpServerResponse from "effect/http/HttpServerResponse"; |
| 7 | import * as Layer from "effect/Layer"; |
| 8 | import * as Stream from "effect/Stream"; |
| 9 | import path from "pathe"; |
| 10 | |
| 11 | const main = path.resolve(import.meta.dirname, "handler.ts"); |
| 12 | |
| 13 | /** |
| 14 | * Deterministic release label the catalog routes pin to — old enough to be |
| 15 | * available in every region the tests run in. |
| 16 | */ |
| 17 | export const PROBE_RELEASE_LABEL = "emr-7.5.0"; |
| 18 | |
| 19 | export class EmrTestFunction extends Lambda.Function<Lambda.Function>()("EmrTestFunction") {} |
| 20 | |
| 21 | /** |
| 22 | * Account-scoped binding fixture: no EMR cluster is ever created. The four |
| 23 | * discovery/catalog bindings and the EventBridge event source deploy and are |
| 24 | * driven at zero cost. |
| 25 | */ |
| 26 | export default EmrTestFunction.make( |
| 27 | { |
| 28 | main, |
| 29 | functionUrl: true, |
| 30 | timeout: Duration.seconds(30), |
| 31 | }, |
| 32 | Effect.gen(function* () { |
| 33 | // Event source: subscribe the host to EMR cluster + step state changes. |
| 34 | // The deploy proves the EventBridge rule + invoke permission wiring. |
| 35 | yield* EMR.consumeClusterEvents({ kinds: ["cluster", "step"] }, (events) => |
| 36 | Stream.runForEach(events, (event) => |
| 37 | Effect.log(`emr event: ${event.detail.clusterId} -> ${event.detail.state}`), |
| 38 | ), |
| 39 | ); |
| 40 | |
| 41 | const listClusters = yield* EMR.ListClusters(); |
| 42 | const listReleaseLabels = yield* EMR.ListReleaseLabels(); |
| 43 | const describeReleaseLabel = yield* EMR.DescribeReleaseLabel(); |
| 44 | const listSupportedInstanceTypes = yield* EMR.ListSupportedInstanceTypes(); |
| 45 | |
| 46 | const bound = { |
| 47 | listClusters, |
| 48 | listReleaseLabels, |
| 49 | describeReleaseLabel, |
| 50 | listSupportedInstanceTypes, |
| 51 | }; |
| 52 | |
| 53 | return { |
| 54 | fetch: Effect.gen(function* () { |
| 55 | const request = yield* HttpServerRequest; |
| 56 | const url = new URL(request.originalUrl); |
| 57 | const pathname = url.pathname; |
| 58 | |
| 59 | if (request.method === "GET" && pathname === "/bindings") { |
| 60 | return yield* HttpServerResponse.json({ |
| 61 | bound: Object.keys(bound), |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | // Account inventory (possibly empty) — proves the `*` grant and the |
| 66 | // response schema decode. |
| 67 | if (request.method === "GET" && pathname === "/clusters") { |
| 68 | const { Clusters } = yield* listClusters({ |
| 69 | ClusterStates: ["STARTING", "RUNNING", "WAITING"], |
| 70 | }); |
| 71 | return yield* HttpServerResponse.json({ |
| 72 | count: (Clusters ?? []).length, |
| 73 | }); |
| 74 | } |
| 75 | |
| 76 | if (request.method === "GET" && pathname === "/releases") { |
| 77 | const { ReleaseLabels } = yield* listReleaseLabels(); |
| 78 | return yield* HttpServerResponse.json({ |
| 79 | count: (ReleaseLabels ?? []).length, |
| 80 | latest: ReleaseLabels?.[0], |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | if (request.method === "GET" && pathname === "/release") { |
| 85 | const { Applications } = yield* describeReleaseLabel({ |
| 86 | ReleaseLabel: PROBE_RELEASE_LABEL, |
| 87 | }); |
| 88 | return yield* HttpServerResponse.json({ |
| 89 | applications: (Applications ?? []).map((app) => app.Name), |
| 90 | }); |
| 91 | } |
| 92 | |
| 93 | if (request.method === "GET" && pathname === "/instance-types") { |
| 94 | const { SupportedInstanceTypes } = yield* listSupportedInstanceTypes({ |
| 95 | ReleaseLabel: PROBE_RELEASE_LABEL, |
| 96 | }); |
| 97 | return yield* HttpServerResponse.json({ |
| 98 | types: (SupportedInstanceTypes ?? []).map((t) => t.Type), |
| 99 | }); |
| 100 | } |
| 101 | |
| 102 | return yield* HttpServerResponse.json( |
| 103 | { error: "Not found", method: request.method, pathname }, |
| 104 | { status: 404 }, |
| 105 | ); |
| 106 | }).pipe(Effect.orDie), |
| 107 | }; |
| 108 | }).pipe( |
| 109 | Effect.provide( |
| 110 | Layer.mergeAll( |
| 111 | Lambda.EventSource, |
| 112 | EMR.ListClustersHttp, |
| 113 | EMR.ListReleaseLabelsHttp, |
| 114 | EMR.DescribeReleaseLabelHttp, |
| 115 | EMR.ListSupportedInstanceTypesHttp, |
| 116 | ), |
| 117 | ), |
| 118 | ), |
| 119 | ); |