@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 114 lines 4.01 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Config from "effect/Config";
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 Redacted from "effect/Redacted";
7 import path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "handler.ts");
10
11 /**
12 * Hard-coded values the integ test asserts against to prove the
13 * deploy-time bindings flow all the way through to the runtime.
14 */
15 export const LITERAL_SECRET_VALUE = "sk-aws-literal-secret-abc";
16 export const STRING_VAR_VALUE = "plain-aws-string-value";
17 export const NUMBER_VAR_VALUE = 9090;
18 export const OBJECT_VAR_VALUE = {
19 region: "us-east-1",
20 flags: { canary: true },
21 };
22
23 /**
24 * Name of the deploy-time `process.env` variable the test populates
25 * before deploying — sourced via `Config.String(...)` inside the
26 * Lambda's init phase.
27 */
28 export const CONFIG_SECRET_ENV_KEY = "ALCHEMY_AWS_SECRET_TEST_SOURCE";
29
30 export class SecretsTestFunction extends Lambda.Function<SecretsTestFunction>()(
31 "SecretsTestFunction",
32 ) {}
33
34 export const SecretsTestFunctionLive = SecretsTestFunction.make(
35 {
36 main,
37 functionUrl: true,
38 },
39 Effect.gen(function* () {
40 // Secret from a literal — `Alchemy.Secret` coerces the literal to
41 // `Redacted` and the Lambda runtime accessor rebuilds the wrapper
42 // from the JSON marker stored in env.
43 const literalSecret = yield* Config.Redacted("LITERAL_SECRET").pipe(
44 Config.withDefault(Redacted.make(LITERAL_SECRET_VALUE)),
45 );
46
47 // Secret from a `Config` — resolved against the active
48 // `ConfigProvider` (process.env) at deploy time. The test populates
49 // `process.env[CONFIG_SECRET_ENV_KEY]` before deploying, so source
50 // from that same key (not a hard-coded literal).
51 const configSecret = yield* Config.Redacted(CONFIG_SECRET_ENV_KEY);
52
53 // Plain string variable — string round-trip.
54 const stringVar = yield* Config.String("STRING_VAR").pipe(Config.withDefault(STRING_VAR_VALUE));
55
56 // Number variable — non-string values JSON.stringify on `set` and
57 // JSON.parse on the runtime accessor, so the accessor returns the
58 // original number.
59 const numberVar = yield* Config.Number("NUMBER_VAR").pipe(Config.withDefault(NUMBER_VAR_VALUE));
60
61 // Object variable — same JSON round-trip as above for nested data.
62 const objectVar = yield* Config.String("OBJECT_VAR").pipe(Config.withDefault(OBJECT_VAR_VALUE));
63
64 return {
65 fetch: Effect.gen(function* () {
66 const request = yield* HttpServerRequest;
67 const url = new URL(request.originalUrl);
68 const pathname = url.pathname;
69
70 switch (pathname) {
71 case "/ready":
72 return HttpServerResponse.text("ok");
73 case "/secret/literal": {
74 return yield* HttpServerResponse.json({
75 isRedacted: Redacted.isRedacted(literalSecret),
76 value: Redacted.value(literalSecret),
77 });
78 }
79 case "/secret/config": {
80 return yield* HttpServerResponse.json({
81 isRedacted: Redacted.isRedacted(configSecret),
82 value: Redacted.value(configSecret),
83 });
84 }
85 case "/var/string": {
86 return yield* HttpServerResponse.json({
87 type: typeof stringVar,
88 value: stringVar,
89 });
90 }
91 case "/var/number": {
92 return yield* HttpServerResponse.json({
93 type: typeof numberVar,
94 value: numberVar,
95 });
96 }
97 case "/var/object": {
98 return yield* HttpServerResponse.json({
99 type: typeof objectVar,
100 value: objectVar,
101 });
102 }
103 default:
104 return yield* HttpServerResponse.json(
105 { error: "Not found", pathname },
106 { status: 404 },
107 );
108 }
109 }).pipe(Effect.orDie),
110 };
111 }),
112 );
113
114 export default SecretsTestFunctionLive;