The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as AWS from "@alchemy.run/aws"; |
| 2 | import * as Duration from "effect/Duration"; |
| 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 { FixtureCache, FixtureCacheLive } from "./fixture-cache.ts"; |
| 8 | |
| 9 | export class ElastiCacheSnapshotFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()( |
| 10 | "ElastiCacheSnapshotFunction", |
| 11 | ) {} |
| 12 | |
| 13 | /** |
| 14 | * Control-plane fixture for the cache-scoped CreateServerlessCacheSnapshot |
| 15 | * binding. Deliberately NOT VPC-attached: the default VPC has no NAT gateway |
| 16 | * or ElastiCache interface endpoint, so a VPC-attached Lambda cannot reach |
| 17 | * the ElastiCache API (the call hangs until the function times out and the |
| 18 | * Function URL answers 502). |
| 19 | * |
| 20 | * - `/snapshot?name=...` takes an on-demand snapshot of the fixture cache. |
| 21 | */ |
| 22 | export const ElastiCacheSnapshotFunctionLive = ElastiCacheSnapshotFunction.make( |
| 23 | { |
| 24 | main: import.meta.url, |
| 25 | functionUrl: true, |
| 26 | timeout: Duration.seconds(30), |
| 27 | }, |
| 28 | Effect.gen(function* () { |
| 29 | const { cache } = yield* FixtureCache; |
| 30 | const createSnapshot = yield* AWS.ElastiCache.CreateServerlessCacheSnapshot(cache); |
| 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 === "/snapshot") { |
| 39 | const name = url.searchParams.get("name") ?? "alchemy-fixture-snap"; |
| 40 | return yield* createSnapshot({ |
| 41 | ServerlessCacheSnapshotName: name, |
| 42 | }).pipe( |
| 43 | Effect.flatMap((result) => |
| 44 | HttpServerResponse.json({ |
| 45 | name: result.ServerlessCacheSnapshot?.ServerlessCacheSnapshotName, |
| 46 | status: result.ServerlessCacheSnapshot?.Status, |
| 47 | }), |
| 48 | ), |
| 49 | // Re-run after a crashed test: the snapshot already exists. |
| 50 | Effect.catchTag("ServerlessCacheSnapshotAlreadyExistsFault", () => |
| 51 | HttpServerResponse.json({ name, status: "exists" }), |
| 52 | ), |
| 53 | Effect.catch((error) => |
| 54 | HttpServerResponse.json({ error: error._tag }, { status: 500 }), |
| 55 | ), |
| 56 | ); |
| 57 | } |
| 58 | |
| 59 | return yield* HttpServerResponse.json( |
| 60 | { error: "Not found", method: request.method, pathname }, |
| 61 | { status: 404 }, |
| 62 | ); |
| 63 | }).pipe(Effect.orDie), |
| 64 | }; |
| 65 | }).pipe( |
| 66 | Effect.provide( |
| 67 | Layer.mergeAll(AWS.ElastiCache.CreateServerlessCacheSnapshotHttp, FixtureCacheLive), |
| 68 | ), |
| 69 | ), |
| 70 | ); |
| 71 | |
| 72 | export default ElastiCacheSnapshotFunctionLive; |