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

main
ts 129 lines 4.35 KB
Raw
1 import * as CloudFront from "@alchemy.run/aws/CloudFront";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as S3 from "@alchemy.run/aws/S3";
4 import type * as cloudfront from "@distilled.cloud/aws/cloudfront";
5 import * as Duration from "effect/Duration";
6 import * as Effect from "effect/Effect";
7 import { HttpServerRequest } from "effect/http/HttpServerRequest";
8 import * as HttpServerResponse from "effect/http/HttpServerResponse";
9 import * as Layer from "effect/Layer";
10 import * as Schedule from "effect/Schedule";
11 import path from "pathe";
12
13 const main = path.resolve(import.meta.dirname, "handler.ts");
14
15 // Distribution grants can still be propagating after the function URL is ready.
16 const authorizationPolicy = {
17 while: (
18 error:
19 | cloudfront.CreateInvalidationError
20 | cloudfront.GetInvalidationError
21 | cloudfront.ListInvalidationsError,
22 ) => error._tag === "AccessDenied",
23 schedule: Schedule.spaced("3 seconds"),
24 times: 8,
25 };
26
27 export class CloudFrontTestFunction extends Lambda.Function<Lambda.Function>()(
28 "CloudFrontTestFunction",
29 ) {}
30
31 export default CloudFrontTestFunction.make(
32 {
33 main,
34 functionUrl: true,
35 timeout: Duration.seconds(30),
36 },
37 Effect.gen(function* () {
38 const bucket = yield* S3.Bucket("InvalidationOriginBucket", {
39 forceDestroy: true,
40 });
41 const distribution = yield* CloudFront.Distribution("TestDistribution", {
42 // Deterministic marker so the test can find this distribution
43 // out-of-band via listDistributions.
44 comment: "alchemy-cf-bindings-fixture",
45 origins: [
46 {
47 id: "origin",
48 domainName: bucket.bucketRegionalDomainName,
49 s3Origin: true,
50 },
51 ],
52 defaultCacheBehavior: {
53 targetOriginId: "origin",
54 viewerProtocolPolicy: "redirect-to-https",
55 allowedMethods: ["GET", "HEAD"],
56 cachedMethods: ["GET", "HEAD"],
57 minTtl: 0,
58 forwardedValues: {
59 QueryString: false,
60 Cookies: { Forward: "none" },
61 },
62 },
63 });
64
65 const createInvalidation = yield* CloudFront.CreateInvalidation(distribution);
66 const getInvalidation = yield* CloudFront.GetInvalidation(distribution);
67 const listInvalidations = yield* CloudFront.ListInvalidations(distribution);
68
69 return {
70 fetch: Effect.gen(function* () {
71 const request = yield* HttpServerRequest;
72 const url = new URL(request.originalUrl);
73 const pathname = url.pathname;
74
75 if (request.method === "GET" && pathname === "/health") {
76 return yield* HttpServerResponse.json({ ok: true });
77 }
78
79 if (request.method === "POST" && pathname === "/invalidate") {
80 const body = (yield* request.json) as unknown as {
81 callerReference: string;
82 paths: string[];
83 };
84 const response = yield* createInvalidation({
85 InvalidationBatch: {
86 CallerReference: body.callerReference,
87 Paths: {
88 Quantity: body.paths.length,
89 Items: body.paths,
90 },
91 },
92 }).pipe(Effect.retry(authorizationPolicy));
93 return yield* HttpServerResponse.json({
94 invalidationId: response.Invalidation?.Id,
95 status: response.Invalidation?.Status,
96 });
97 }
98
99 if (request.method === "GET" && pathname === "/invalidation") {
100 const id = url.searchParams.get("id")!;
101 const response = yield* getInvalidation({ Id: id }).pipe(
102 Effect.retry(authorizationPolicy),
103 );
104 return yield* HttpServerResponse.json({
105 invalidationId: response.Invalidation?.Id,
106 status: response.Invalidation?.Status,
107 });
108 }
109
110 if (request.method === "GET" && pathname === "/invalidations") {
111 const response = yield* listInvalidations({}).pipe(Effect.retry(authorizationPolicy));
112 return yield* HttpServerResponse.json({
113 invalidationIds: (response.InvalidationList?.Items ?? []).map((item) => item.Id),
114 });
115 }
116
117 return yield* HttpServerResponse.json({ error: "Not found" }, { status: 404 });
118 }).pipe(Effect.orDie),
119 };
120 }).pipe(
121 Effect.provide(
122 Layer.mergeAll(
123 CloudFront.CreateInvalidationHttp,
124 CloudFront.GetInvalidationHttp,
125 CloudFront.ListInvalidationsHttp,
126 ),
127 ),
128 ),
129 );