import { createClient } from "redis"; import { getSecret } from "@common/secret-manager"; import { getLogger } from "@obs/logger"; type GarnetRedisClient = ReturnType; export interface StreamEntry { deliveriesCounter?: number | undefined; id: string; message: Record; millisElapsedFromDelivery?: number | undefined; } export interface StreamReadResult { messages: StreamEntry[]; name: string; } export interface StreamPendingEntry { consumer: string; deliveriesCounter: number; id: string; millisecondsSinceLastDelivery: number; } interface RawStreamMessage { deliveriesCounter?: number | undefined; id: string; message: unknown; millisElapsedFromDelivery?: number | undefined; } interface RawStreamReadResult { messages: RawStreamMessage[]; name: string; } function normalizeGarnetUrl(endpoint: string): string { const trimmed = endpoint.trim(); if (!trimmed) { throw new Error("GARNET_ENDPOINT cannot be empty"); } if (/^redis(s)?:\/\//iu.test(trimmed)) { return trimmed; } return `redis://${trimmed}`; } function normalizeStreamFields(fields: unknown): Record { if (fields instanceof Map) { return Object.fromEntries( [...fields.entries()].map(([key, value]) => [String(key), String(value)]), ); } if (typeof fields === "object" && fields !== null) { return Object.fromEntries( Object.entries(fields as Record).map(([key, value]) => [ key, String(value), ]), ); } return {}; } export class GarnetClient { private client: GarnetRedisClient | null = null; private readonly logger = getLogger(); private getClient(): GarnetRedisClient { if (!this.client) { throw new Error("Garnet client not connected"); } return this.client; } async connect(): Promise { const endpoint = getSecret("GARNET_ENDPOINT"); const username = getSecret("GARNET_USERID"); const password = getSecret("GARNET_PASSWORD"); if (!endpoint || !username || !password) { throw new Error("Missing Garnet credentials in secrets"); } const client = createClient({ password, url: normalizeGarnetUrl(endpoint), username, }); client.on("error", (error) => { this.logger.error({ err: error }, "[Garnet] Client error"); }); await client.connect(); this.client = client; this.logger.info("✓ Garnet connected"); } async getJson(key: string): Promise { const rawValue = await this.getClient().get(key); if (!rawValue) { return null; } return JSON.parse(rawValue) as T; } async setJson( key: string, value: unknown, ttlSeconds?: number, ): Promise { const serialized = JSON.stringify(value); const client = this.getClient(); if (ttlSeconds && ttlSeconds > 0) { await client.set(key, serialized, { EX: ttlSeconds }); return; } await client.set(key, serialized); } async getString(key: string): Promise { return this.getClient().get(key); } async setString( key: string, value: string, ttlSeconds?: number, ): Promise { const client = this.getClient(); if (ttlSeconds && ttlSeconds > 0) { await client.set(key, value, { EX: ttlSeconds }); return; } await client.set(key, value); } async setStringIfAbsent( key: string, value: string, ttlSeconds?: number, ): Promise { const client = this.getClient(); const setOptions: { EX?: number; NX: true; } = { NX: true, }; if (ttlSeconds && ttlSeconds > 0) { setOptions.EX = ttlSeconds; } const result = await client.set(key, value, { ...setOptions, }); return result === "OK"; } async delete(key: string): Promise { return this.getClient().del(key); } async getHash(key: string): Promise> { return this.getClient().hGetAll(key); } async setHash(key: string, fields: Record): Promise { await this.getClient().hSet(key, fields); } async appendStream( key: string, fields: Record, options?: { maxLen?: number | undefined }, ): Promise { const trimOptions = options?.maxLen && options.maxLen > 0 ? { TRIM: { strategy: "MAXLEN" as const, strategyModifier: "~" as const, threshold: options.maxLen, }, } : undefined; return this.getClient().xAdd(key, "*", fields, trimOptions); } async createConsumerGroup( key: string, group: string, startId = "$", ): Promise { await this.getClient().xGroupCreate(key, group, startId, { MKSTREAM: true, }); } async readGroup( key: string, group: string, consumer: string, startId: string, options?: { blockMs?: number | undefined; count?: number | undefined; }, ): Promise { const readOptions: { BLOCK?: number; COUNT?: number; } = {}; if (options?.blockMs !== undefined) { readOptions.BLOCK = options.blockMs; } if (options?.count !== undefined) { readOptions.COUNT = options.count; } const reply = await this.getClient().xReadGroup( group, consumer, { key, id: startId, }, readOptions, ); if (!reply) { return null; } return (reply as RawStreamReadResult[]).map((stream) => ({ name: String(stream.name), messages: stream.messages.map((message) => ({ deliveriesCounter: typeof message.deliveriesCounter === "number" ? message.deliveriesCounter : undefined, id: String(message.id), message: normalizeStreamFields(message.message), millisElapsedFromDelivery: typeof message.millisElapsedFromDelivery === "number" ? message.millisElapsedFromDelivery : undefined, })), })); } async acknowledgeStream( key: string, group: string, ids: string | string[], ): Promise { return this.getClient().xAck(key, group, ids); } async getPendingStreamRange( key: string, group: string, count: number, options?: { consumer?: string | undefined; idleMs?: number | undefined; start?: string | undefined; end?: string | undefined; }, ): Promise { const pendingOptions: { IDLE?: number; consumer?: string; } = {}; if (options?.idleMs !== undefined) { pendingOptions.IDLE = options.idleMs; } if (options?.consumer !== undefined) { pendingOptions.consumer = options.consumer; } const reply = await this.getClient().xPendingRange( key, group, options?.start ?? "-", options?.end ?? "+", count, pendingOptions, ); return reply.map((entry) => ({ consumer: String(entry.consumer), deliveriesCounter: entry.deliveriesCounter, id: String(entry.id), millisecondsSinceLastDelivery: entry.millisecondsSinceLastDelivery, })); } async claimPendingStream( key: string, group: string, consumer: string, minIdleTimeMs: number, ids: string | string[], options?: { retryCount?: number | undefined; }, ): Promise { const claimOptions: { RETRYCOUNT?: number; } = {}; if (options?.retryCount !== undefined) { claimOptions.RETRYCOUNT = options.retryCount; } const claimed = await this.getClient().xClaim( key, group, consumer, minIdleTimeMs, ids, claimOptions, ); return claimed.flatMap((message) => { if (!message) { return []; } return [ { deliveriesCounter: typeof message.deliveriesCounter === "number" ? message.deliveriesCounter : undefined, id: String(message.id), message: normalizeStreamFields(message.message), millisElapsedFromDelivery: typeof message.millisElapsedFromDelivery === "number" ? message.millisElapsedFromDelivery : undefined, }, ]; }); } async scanKeys(pattern: string): Promise { const keys: string[] = []; for await (const batch of this.getClient().scanIterator({ COUNT: 100, MATCH: pattern, })) { for (const key of batch) { keys.push(String(key)); } } return keys; } async close(): Promise { if (!this.client) { return; } const client = this.client; this.client = null; try { await client.quit(); } catch { client.destroy(); } } }