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

main
ts 98 lines 3.59 KB
Raw
1 import { Random } from "alchemy/Random";
2 import * as Context from "effect/Context";
3 import * as Effect from "effect/Effect";
4 import * as Secret from "../SecretsStore/Secret.ts";
5 import { Store as SecretsStore } from "../SecretsStore/SecretsStore.ts";
6
7 /**
8 * The account-wide Secrets Store that backs every secret used by the
9 * state store worker. `SecretsStore` adopts the single store that
10 * already exists on the account, or creates one if none exists.
11 */
12 export const Store = SecretsStore("StateStoreSecrets");
13
14 /** The default state store's Worker name. */
15 export const DEFAULT_STATE_STORE_NAME = "alchemy-state-store" as const;
16
17 /**
18 * Which state store a deploy builds: its Worker's name. The default store
19 * keeps the original Worker and secret names, so existing deploys don't move.
20 * Inside the deployed Worker it reads as the default; the runtime never needs
21 * its own name.
22 */
23 export const StateStoreName = Context.Reference<string>("alchemy/Cloudflare/StateStoreName", {
24 defaultValue: () => DEFAULT_STATE_STORE_NAME,
25 });
26
27 /**
28 * A state store's secret name in the account-wide Secrets Store: the
29 * default store's is `base`, a named store's is suffixed with its name.
30 */
31 export const stateStoreSecretName = (base: string, storeName: string) =>
32 storeName === DEFAULT_STATE_STORE_NAME
33 ? base
34 : `${base}_${storeName.replace(/[^A-Za-z0-9_]/g, "_")}`;
35
36 /**
37 * The randomly generated bearer token value. Generated once on create
38 * and persisted in alchemy state, so subsequent deploys keep the same
39 * value unless the resource is replaced.
40 */
41 export const TokenValue = Random("StateStoreAuthTokenValue");
42
43 /**
44 * The name of the secret in the Cloudflare Secrets Store that contains the bearer token.
45 */
46 export const AuthTokenSecretName = "AlchemyStateStoreToken" as const;
47
48 /**
49 * The Worker env bindings the runtime reads the two secrets through: each
50 * `Secret` resource's logical ID. `Deploy.ts` uploads bindings under these
51 * names, so they must not change while stores are deployed.
52 */
53 export const AuthTokenBinding = AuthTokenSecretName;
54 export const EncryptionKeyBinding = "StateStoreEncryptionKey" as const;
55
56 /**
57 * The bearer token used to authenticate every request to the state
58 * store worker. The value comes from {@link TokenValue} and lives in
59 * the account-wide Cloudflare Secrets Store so it can be bound into
60 * the worker without bundling the raw string.
61 */
62 export const AuthToken = Effect.gen(function* () {
63 const store = yield* Store;
64 const random = yield* TokenValue;
65 return yield* Secret.Secret(AuthTokenBinding, {
66 name: stateStoreSecretName(AuthTokenSecretName, yield* StateStoreName),
67 store,
68 value: random.text,
69 });
70 });
71
72 /**
73 * A 32-byte (256-bit) random value, hex-encoded, that seeds the
74 * AES-CTR key used to encrypt resource state at rest. Generated once
75 * and persisted, so the ciphertext stored by the Durable Object can
76 * always be decrypted by subsequent worker boots.
77 */
78 export const EncryptionKeyValue = Random("StateStoreEncryptionKeyValue", {
79 bytes: 32,
80 });
81
82 export const EncryptionKeySecretName = "AlchemyStateStoreEncryptionKey" as const;
83
84 /**
85 * The encryption key secret. The raw hex-encoded bytes live inside
86 * Cloudflare's Secrets Store; the Durable Object binds to it at
87 * runtime to derive an AES-CTR `CryptoKey` via Web Crypto's
88 * `subtle.importKey`.
89 */
90 export const EncryptionKey = Effect.gen(function* () {
91 const store = yield* Store;
92 const random = yield* EncryptionKeyValue;
93 return yield* Secret.Secret(EncryptionKeyBinding, {
94 name: stateStoreSecretName(EncryptionKeySecretName, yield* StateStoreName),
95 store,
96 value: random.text,
97 });
98 });