import { Inject, Injectable, Logger, OnModuleDestroy, } from '@nestjs/common'; import type { createClient } from 'redis'; import { GARNET_CLIENT } from './garnet.constants'; export type GarnetRedisClient = ReturnType; export interface StreamEntry { deliveriesCounter?: number; id: string; message: Record; millisElapsedFromDelivery?: number; } export interface StreamReadResult { messages: StreamEntry[]; name: string; } interface RawStreamMessage { deliveriesCounter?: number; id: string; message: unknown; millisElapsedFromDelivery?: number; } interface RawStreamReadResult { messages: RawStreamMessage[]; name: string; } 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 {}; } @Injectable() export class GarnetService implements OnModuleDestroy { private readonly logger = new Logger(GarnetService.name); constructor( @Inject(GARNET_CLIENT) private readonly client: GarnetRedisClient, ) {} async onModuleDestroy() { try { await this.client.quit(); } catch { this.client.destroy(); } } public raw(): GarnetRedisClient { return this.client; } public async getJson(key: string): Promise { const raw = await this.client.get(key); if (!raw) return null; try { return JSON.parse(raw) as T; } catch (err) { this.logger.warn( `Failed to JSON.parse cached value for key="${key}": ${ (err as Error)?.message ?? err }`, ); return null; } } public async setJson( key: string, value: unknown, ttlSeconds?: number, ): Promise { const serialized = JSON.stringify(value); if (ttlSeconds && ttlSeconds > 0) { await this.client.set(key, serialized, { EX: ttlSeconds }); return; } await this.client.set(key, serialized); } public async getString(key: string): Promise { return this.client.get(key); } public async setString( key: string, value: string, ttlSeconds?: number, ): Promise { if (ttlSeconds && ttlSeconds > 0) { await this.client.set(key, value, { EX: ttlSeconds }); return; } await this.client.set(key, value); } public async setStringIfAbsent( key: string, value: string, ttlSeconds?: number, ): Promise { const options: { EX?: number; NX: true } = { NX: true }; if (ttlSeconds && ttlSeconds > 0) options.EX = ttlSeconds; const result = await this.client.set(key, value, options); return result === 'OK'; } public async delete(key: string): Promise { return this.client.del(key); } public async appendStream( key: string, fields: Record, options?: { maxLen?: number }, ): Promise { const trimOptions = options?.maxLen && options.maxLen > 0 ? { TRIM: { strategy: 'MAXLEN' as const, strategyModifier: '~' as const, threshold: options.maxLen, }, } : undefined; return this.client.xAdd(key, '*', fields, trimOptions); } public async createConsumerGroup( key: string, group: string, startId = '$', ): Promise { await this.client.xGroupCreate(key, group, startId, { MKSTREAM: true }); } public async readGroup( key: string, group: string, consumer: string, startId: string, options?: { blockMs?: number; count?: number }, ): 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.client.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, })), })); } public async acknowledgeStream( key: string, group: string, ids: string | string[], ): Promise { return this.client.xAck(key, group, ids); } public async streamLength(key: string): Promise { return this.client.xLen(key); } public async publish(channel: string, message: string): Promise { return this.client.publish(channel, message); } }