auth-yes/src/core/valkey.ts
google-labs-jules[bot] a613ed2b68 feat: execute Phase 1 architectural hypermedia framework
- Bootstraps `src/core/` foundation (`db.ts`, `valkey.ts`, `spire_ffi.ts`, `main.ts`).
- Adds `auth_guards.ts` for payload capping, CSRF check, and rate limiting.
- Adds `content_negotiation.ts` and `sse_adapter.ts` for Datastar transport helpers.
- Adds `error_fragments.tsx` for standardized Datastar error morphs.
- Introduces `scripts/lint_arch.ts` to block imperative DOM usage.
- Updates `deno.json` with src workspace configs and lint commands.

Co-authored-by: mrteye <1945243+mrteye@users.noreply.github.com>
2026-08-28 01:20:01 +00:00

43 lines
1.1 KiB
TypeScript

import { Redis } from "npm:ioredis";
const VALKEY_URL = Deno.env.get("VALKEY_URL") ||
(import.meta.main ? "redis://auth-valkey:6379" : "");
export const valkey = VALKEY_URL
? new Redis(VALKEY_URL, {
enableOfflineQueue: false,
maxRetriesPerRequest: 1,
retryStrategy: (times) => (times > 3 ? null : Math.min(times * 100, 1000)),
})
: new Redis({
lazyConnect: true,
enableOfflineQueue: false,
maxRetriesPerRequest: 1,
retryStrategy: () => null,
});
valkey.on("error", (err) => {
if (Deno.env.get("DEBUG_VALKEY")) {
console.warn("[Valkey] Connection warning:", err.message);
}
});
export async function pingValkey(): Promise<void> {
if (!VALKEY_URL) return; // Skip in test
try {
const result = await valkey.ping();
if (result !== "PONG") {
throw new Error(`Unexpected ping response: ${result}`);
}
} catch (error) {
if (error instanceof Error) {
throw new Error(
`Fatal: Failed to connect to Valkey session cache. Halting boot. ${error.message}`,
);
}
throw new Error(
`Fatal: Failed to connect to Valkey session cache. Halting boot.`,
);
}
}