@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 100 lines 2.95 KB
Raw
1 import * as ApiGatewayV2 from "@alchemy.run/aws/ApiGatewayV2";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as Context from "effect/Context";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "ws-handler.ts");
11
12 export class WebSocketTestFunction extends Lambda.Function<Lambda.Function>()(
13 "WebSocketTestFunction",
14 ) {}
15
16 export class WsApiAndStage extends Context.Service<
17 WsApiAndStage,
18 {
19 api: ApiGatewayV2.Api;
20 stage: ApiGatewayV2.ApiGatewayV2Stage;
21 }
22 >()("WsApiAndStage") {}
23
24 export const WsApiAndStageLive = Layer.effect(
25 WsApiAndStage,
26 Effect.gen(function* () {
27 const api = yield* ApiGatewayV2.Api("WsTestApi", {
28 protocolType: "WEBSOCKET",
29 routeSelectionExpression: "$request.body.action",
30 });
31 const stage = yield* ApiGatewayV2.Stage("WsTestStage", {
32 api,
33 stageName: "test",
34 autoDeploy: true,
35 });
36 return { api, stage };
37 }),
38 );
39
40 /**
41 * Fixture Lambda for the WebSocket e2e test:
42 *
43 * - `$connect` → accept the connection
44 * - `$disconnect` → no-op
45 * - `$default` → echo the received frame back to the sender via the
46 * ManageConnections binding (`postToConnection`)
47 *
48 * The function URL exposes the stage's `wss://` URL so the test can
49 * discover it after deploy.
50 */
51 export default WebSocketTestFunction.make(
52 {
53 main,
54 functionUrl: true,
55 timeout: Duration.seconds(30),
56 },
57 Effect.gen(function* () {
58 const { api, stage } = yield* WsApiAndStage;
59 const connections = yield* ApiGatewayV2.ManageConnections(stage);
60 const WsUrl = yield* stage.invokeUrl;
61 const CallbackUrl = yield* stage.callbackUrl;
62
63 yield* ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$connect" }, () =>
64 Effect.succeed({ statusCode: 200 }),
65 );
66
67 yield* ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$disconnect" }, () => Effect.void);
68
69 yield* ApiGatewayV2.onWebSocketRoute(api, { routeKey: "$default" }, (event) =>
70 connections
71 .postToConnection({
72 ConnectionId: event.requestContext.connectionId,
73 Data: `echo:${event.body ?? ""}`,
74 })
75 .pipe(
76 Effect.asVoid,
77 // The peer may disconnect between send and receive — not a
78 // failure for the echo fixture.
79 Effect.catchTag("GoneException", () => Effect.void),
80 Effect.orDie,
81 ),
82 );
83
84 return {
85 fetch: Effect.gen(function* () {
86 return yield* HttpServerResponse.json({
87 wsUrl: yield* WsUrl,
88 callbackUrl: yield* CallbackUrl,
89 });
90 }).pipe(Effect.orDie),
91 };
92 }).pipe(
93 Effect.provide(
94 Layer.provideMerge(
95 Layer.mergeAll(Lambda.WebSocketEventSource, ApiGatewayV2.ManageConnectionsHttp),
96 WsApiAndStageLive,
97 ),
98 ),
99 ),
100 );