auth-yes/server/valkey.ts

30 lines
838 B
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,
})
: new Redis({ lazyConnect: true, enableOfflineQueue: false });
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.`,
);
}
}