@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 72 lines 2.79 KB
Raw
1 import type { RuntimeContext } from "alchemy";
2 import * as Context from "effect/Context";
3 import * as Effect from "effect/Effect";
4 import type { BetterAuthStorageError } from "./Errors.ts";
5
6 /**
7 * Effect-native contract for Better Auth's `secondaryStorage` — the
8 * key-value store used for sessions, rate limiting, and OAuth state when
9 * configured.
10 *
11 * All five operations are required. TTLs are in SECONDS.
12 */
13 export interface SecondaryStorageService {
14 readonly get: (
15 key: string,
16 ) => Effect.Effect<string | null, BetterAuthStorageError, RuntimeContext>;
17 readonly set: (
18 key: string,
19 value: string,
20 ttlSeconds?: number,
21 ) => Effect.Effect<void, BetterAuthStorageError, RuntimeContext>;
22 readonly delete: (key: string) => Effect.Effect<void, BetterAuthStorageError, RuntimeContext>;
23 /** Atomic read-and-delete. */
24 readonly getAndDelete: (
25 key: string,
26 ) => Effect.Effect<string | null, BetterAuthStorageError, RuntimeContext>;
27 /**
28 * Atomically increment a counter. When the key is absent it is created
29 * at `1` with `ttlSeconds`; later increments never extend the TTL
30 * (fixed window).
31 */
32 readonly increment: (
33 key: string,
34 ttlSeconds: number,
35 ) => Effect.Effect<number, BetterAuthStorageError, RuntimeContext>;
36 }
37
38 /**
39 * Optional secondary-storage dependency of {@link BetterAuth}.
40 *
41 * When a layer providing this service is in context, Better Auth is
42 * configured with `secondaryStorage` and its rate limiter defaults to it.
43 * When absent, Better Auth runs database-only — the dependency is resolved
44 * with `Effect.serviceOption`, so it never appears in `BetterAuth`'s
45 * requirements.
46 *
47 * Better Auth's guidance is a Redis-style store: strongly consistent
48 * reads and precise sub-minute TTLs. Eventually-consistent stores
49 * (e.g. Cloudflare KV) are NOT a fit — session reads race cross-location
50 * propagation and rate-limit windows need atomic counters. A strongly
51 * consistent Cloudflare option would be a Durable Object-backed layer.
52 */
53 export class SecondaryStorage extends Context.Service<SecondaryStorage, SecondaryStorageService>()(
54 "BetterAuth.SecondaryStorage",
55 ) {}
56
57 /**
58 * Bridge the Effect-native service to the promise interface Better Auth
59 * expects, running each operation in the captured runtime context.
60 *
61 * @internal
62 */
63 export const toPromiseStorage = (
64 storage: SecondaryStorageService,
65 runPromise: <A, E>(effect: Effect.Effect<A, E, RuntimeContext>) => Promise<A>,
66 ) => ({
67 get: (key: string) => runPromise(storage.get(key)),
68 set: (key: string, value: string, ttl?: number) => runPromise(storage.set(key, value, ttl)),
69 delete: (key: string) => runPromise(storage.delete(key)),
70 getAndDelete: (key: string) => runPromise(storage.getAndDelete(key)),
71 increment: (key: string, ttl: number) => runPromise(storage.increment(key, ttl)),
72 });