import { Inject, Injectable, Logger, OnModuleDestroy } from '@nestjs/common'; import type { createClient } from 'redis'; import { REDIS_CLIENT } from './redis.constants'; export type RedisClient = ReturnType; export interface StreamEntry { deliveriesCounter?: number; id: string; message: Record; millisElapsedFromDelivery?: number; } export interface StreamReadResult { messages: StreamEntry[]; name: string; } export interface ClaimStalePendingStreamMessagesResult { messages: StreamEntry[]; nextStartId: string; } interface RawStreamMessage { deliveriesCounter?: number; id: string; message: unknown; millisElapsedFromDelivery?: number; } interface RawStreamReadResult { messages: RawStreamMessage[]; name: string; } function normalizeStreamFields(fields: unknown): Record { if (Array.isArray(fields)) { const values = fields as unknown[]; const normalized: Record = {}; for (let index = 0; index < values.length; index += 2) { const key = values[index]; if (key === undefined) continue; normalized[redisValueToString(key)] = redisValueToString( values[index + 1], ); } return normalized; } 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 RedisService implements OnModuleDestroy { private readonly logger = new Logger(RedisService.name); constructor(@Inject(REDIS_CLIENT) private readonly client: RedisClient) {} async onModuleDestroy() { try { await this.client.quit(); } catch { this.client.destroy(); } } public raw(): RedisClient { 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 claimStalePendingStreamMessages(options: { key: string; group: string; consumer: string; stalePendingMessageMinimumIdleMilliseconds: number; startId?: string; count?: number; }): Promise { const response = (await this.client.sendCommand([ 'XAUTOCLAIM', options.key, options.group, options.consumer, String(options.stalePendingMessageMinimumIdleMilliseconds), options.startId ?? '0-0', 'COUNT', String(options.count ?? 1), ])) as unknown; const responseItems = Array.isArray(response) ? response : []; const rawMessages = Array.isArray(responseItems[1]) ? responseItems[1] : []; return { nextStartId: String(responseItems[0] ?? '0-0'), messages: rawMessages .map((rawMessage) => this.normalizeClaimedStreamMessage(rawMessage)) .filter((message): message is StreamEntry => Boolean(message)), }; } public async acknowledgeStream( key: string, group: string, ids: string | string[], ): Promise { return this.client.xAck(key, group, ids); } public async deleteStreamEntries( key: string, ids: string | string[], ): Promise { return this.client.xDel(key, 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); } private normalizeClaimedStreamMessage( rawMessage: unknown, ): StreamEntry | null { if (Array.isArray(rawMessage)) { const [id, fields] = rawMessage as unknown[]; if (id === undefined) return null; return { id: redisValueToString(id), message: normalizeStreamFields(fields), }; } if ( typeof rawMessage === 'object' && rawMessage !== null && 'id' in rawMessage ) { const message = rawMessage as RawStreamMessage; 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, }; } return null; } } function redisValueToString(value: unknown): string { if (typeof value === 'string') return value; if ( typeof value === 'number' || typeof value === 'boolean' || typeof value === 'bigint' ) { return String(value); } if (Buffer.isBuffer(value)) return value.toString('utf8'); if (value === undefined || value === null) return ''; return JSON.stringify(value) ?? ''; }