@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, 07:15 UTC

main
ts 95 lines 3.57 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
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
8 /**
9 * Hard-coded values the integ test asserts against to prove the
10 * deploy-time bindings flow all the way through to the runtime.
11 */
12 export const LITERAL_SECRET_VALUE = "sk-literal-secret-abc";
13 export const STRING_VAR_VALUE = "plain-string-value";
14 export const NUMBER_VAR_VALUE = 4242;
15 export const OBJECT_VAR_VALUE = { host: "localhost", flags: { beta: true } };
16
17 /**
18 * Name of the deploy-time `process.env` variable the test populates
19 * before deploying — sourced via `Config.String(...)` inside the
20 * worker's init phase.
21 */
22
23 export default class SecretsTestWorker extends Cloudflare.Worker<SecretsTestWorker>()(
24 "SecretsTestWorker",
25 {
26 main: import.meta.url,
27 workersDev: { enabled: true, previewsEnabled: false },
28 },
29 Effect.gen(function* () {
30 // Secret from a literal — `Alchemy.Secret` coerces the literal to
31 // `Redacted` and the Worker provider deploys it as `secret_text`.
32 const literalSecret = yield* Config.Redacted("LITERAL_SECRET").pipe(
33 Config.withDefault(Redacted.make(LITERAL_SECRET_VALUE)),
34 );
35
36 // Secret from a `Config` — resolved against the active
37 // `ConfigProvider` (process.env) at deploy time.
38 const configSecret = yield* Config.Redacted("CONFIG_SECRET");
39
40 // Plain string variable — `plain_text` binding round-trip.
41 const stringVar = yield* Config.String("STRING_VAR").pipe(Config.withDefault(STRING_VAR_VALUE));
42
43 // Number variable — non-string values JSON.stringify on `set` and
44 // JSON.parse on the runtime accessor, so the accessor returns the
45 // original number.
46 const numberVar = yield* Config.Number("NUMBER_VAR").pipe(Config.withDefault(NUMBER_VAR_VALUE));
47
48 // Object variable — same JSON round-trip as above for nested data.
49 const objectVar = yield* Config.String("OBJECT_VAR").pipe(Config.withDefault(OBJECT_VAR_VALUE));
50
51 return {
52 fetch: Effect.gen(function* () {
53 const request = yield* HttpServerRequest;
54 // `request.url` on Cloudflare workers is the pathname+query
55 // (relative). Use `originalUrl` to get the absolute URL so
56 // `new URL(...)` doesn't throw.
57 const pathname = new URL(request.originalUrl).pathname;
58 switch (pathname) {
59 case "/secret/literal": {
60 return yield* HttpServerResponse.json({
61 isRedacted: Redacted.isRedacted(literalSecret),
62 value: Redacted.value(literalSecret),
63 });
64 }
65 case "/secret/config": {
66 return yield* HttpServerResponse.json({
67 isRedacted: Redacted.isRedacted(configSecret),
68 value: Redacted.value(configSecret),
69 });
70 }
71 case "/var/string": {
72 return yield* HttpServerResponse.json({
73 type: typeof stringVar,
74 value: stringVar,
75 });
76 }
77 case "/var/number": {
78 return yield* HttpServerResponse.json({
79 type: typeof numberVar,
80 value: numberVar,
81 });
82 }
83 case "/var/object": {
84 return yield* HttpServerResponse.json({
85 type: typeof objectVar,
86 value: objectVar,
87 });
88 }
89 default:
90 return HttpServerResponse.text("ok");
91 }
92 }),
93 };
94 }),
95 ) {}