@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 123 lines 4.6 KB
Raw
1 import * as AWS from "@alchemy.run/aws";
2 import * as Context from "effect/Context";
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 Redacted from "effect/Redacted";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "platform-handler.ts");
11
12 /**
13 * Gated fixture for the SNS mobile-push bindings. SNS validates platform
14 * credentials at CreatePlatformApplication time (probe-verified: a fake GCM
15 * key fails with `InvalidParameterException` "Platform credentials are
16 * invalid"), so this fixture only deploys when real credentials are supplied:
17 *
18 * AWS_TEST_SNS_PLATFORM=1
19 * AWS_TEST_SNS_PLATFORM_NAME (e.g. GCM)
20 * AWS_TEST_SNS_PLATFORM_CREDENTIAL
21 * AWS_TEST_SNS_PLATFORM_TOKEN (a registrable device token)
22 */
23 export class PlatformFixture extends Context.Service<
24 PlatformFixture,
25 { application: AWS.SNS.PlatformApplication }
26 >()("PlatformFixture") {}
27
28 export const PlatformFixtureLive = Layer.effect(
29 PlatformFixture,
30 Effect.gen(function* () {
31 const application = yield* AWS.SNS.PlatformApplication("TestPushApp", {
32 platform: process.env.AWS_TEST_SNS_PLATFORM_NAME ?? "GCM",
33 platformCredential: Redacted.make(process.env.AWS_TEST_SNS_PLATFORM_CREDENTIAL ?? ""),
34 });
35 return { application };
36 }),
37 );
38
39 export class PlatformApiFunction extends AWS.Lambda.Function<AWS.Lambda.Function>()(
40 "PlatformApiFunction",
41 ) {}
42
43 const formatError = (error: unknown) =>
44 typeof error === "object" && error !== null && "_tag" in error
45 ? { ok: false as const, error: (error as { _tag: string })._tag }
46 : { ok: false as const, error: `${error}` };
47
48 export const PlatformApiFunctionLive = PlatformApiFunction.make(
49 {
50 main,
51 functionUrl: true,
52 },
53 Effect.gen(function* () {
54 const { application } = yield* PlatformFixture;
55
56 const createEndpoint = yield* AWS.SNS.CreatePlatformEndpoint(application);
57 const getEndpointAttributes = yield* AWS.SNS.GetEndpointAttributes(application);
58 const setEndpointAttributes = yield* AWS.SNS.SetEndpointAttributes(application);
59 const deleteEndpoint = yield* AWS.SNS.DeleteEndpoint(application);
60 const listEndpoints = yield* AWS.SNS.ListEndpointsByPlatformApplication(application);
61 const publishToEndpoint = yield* AWS.SNS.PublishToEndpoint(application);
62
63 return {
64 fetch: Effect.gen(function* () {
65 const request = yield* HttpServerRequest;
66 const url = new URL(request.originalUrl);
67
68 if (request.method === "POST" && url.pathname === "/endpoint-cycle") {
69 const body = (yield* request.json) as { token: string };
70
71 // Create → read → disable → publish (tolerant; the device token
72 // may not be reachable) → list → delete.
73 const created = yield* createEndpoint({ Token: body.token });
74 if (!created.EndpointArn) {
75 return yield* HttpServerResponse.json(
76 { ok: false, error: "no EndpointArn" },
77 { status: 500 },
78 );
79 }
80 const attributes = yield* getEndpointAttributes({
81 EndpointArn: created.EndpointArn,
82 });
83 yield* setEndpointAttributes({
84 EndpointArn: created.EndpointArn,
85 Attributes: { Enabled: "true" },
86 });
87 const published = yield* publishToEndpoint({
88 TargetArn: created.EndpointArn,
89 Message: "alchemy sns platform binding test",
90 }).pipe(Effect.catch((error) => Effect.succeed(formatError(error))));
91 const listed = yield* listEndpoints();
92 yield* deleteEndpoint({ EndpointArn: created.EndpointArn });
93
94 return yield* HttpServerResponse.json({
95 ok: true,
96 endpointArn: created.EndpointArn,
97 attributes: attributes.Attributes,
98 published,
99 endpointCount: (listed.Endpoints ?? []).length,
100 });
101 }
102
103 return yield* HttpServerResponse.json({ error: "Not found" }, { status: 404 });
104 }).pipe(Effect.orDie),
105 };
106 }).pipe(
107 Effect.provide(
108 Layer.provideMerge(
109 PlatformFixtureLive,
110 Layer.mergeAll(
111 AWS.SNS.CreatePlatformEndpointHttp,
112 AWS.SNS.DeleteEndpointHttp,
113 AWS.SNS.GetEndpointAttributesHttp,
114 AWS.SNS.ListEndpointsByPlatformApplicationHttp,
115 AWS.SNS.PublishToEndpointHttp,
116 AWS.SNS.SetEndpointAttributesHttp,
117 ),
118 ),
119 ),
120 ),
121 ).pipe(Layer.provideMerge(PlatformFixtureLive));
122
123 export default PlatformApiFunctionLive;