feat(api): add shared infrastructure
This commit is contained in:
1 parent
f24674b12c
commit
2e311d9cbf
10 files changed
+1088
No files matched your search
@@ -0,0 +1 @@
|
||||
export const GARNET_CLIENT = Symbol('GARNET_CLIENT');
|
||||
@@ -0,0 +1,57 @@
|
||||
import { Logger, Module } from '@nestjs/common';
|
||||
import { createClient } from 'redis';
|
||||
import { SecretModule } from '../secret/secret.module';
|
||||
import { SecretService } from '../secret/secret.service';
|
||||
import { GARNET_CLIENT } from './garnet.constants';
|
||||
import { GarnetService, type GarnetRedisClient } from './garnet.service';
|
||||
|
||||
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}`;
|
||||
}
|
||||
|
||||
@Module({
|
||||
imports: [SecretModule],
|
||||
providers: [
|
||||
{
|
||||
provide: GARNET_CLIENT,
|
||||
inject: [SecretService],
|
||||
useFactory: async (
|
||||
secretService: SecretService,
|
||||
): Promise<GarnetRedisClient> => {
|
||||
await secretService.ready();
|
||||
|
||||
const [endpoint, username, password] = await Promise.all([
|
||||
secretService.get<string>('GARNET_ENDPOINT'),
|
||||
secretService.get<string>('GARNET_USERID'),
|
||||
secretService.get<string>('GARNET_PASSWORD'),
|
||||
]);
|
||||
|
||||
const logger = new Logger('GarnetClient');
|
||||
const client = createClient({
|
||||
url: normalizeGarnetUrl(endpoint),
|
||||
username,
|
||||
password,
|
||||
});
|
||||
|
||||
client.on('error', (error) => {
|
||||
logger.error(`Garnet client error: ${error?.message ?? error}`);
|
||||
});
|
||||
|
||||
await client.connect();
|
||||
logger.log('Garnet connected');
|
||||
|
||||
return client;
|
||||
},
|
||||
},
|
||||
GarnetService,
|
||||
],
|
||||
exports: [GarnetService, GARNET_CLIENT],
|
||||
})
|
||||
export class GarnetModule {}
|
||||
@@ -0,0 +1,213 @@
|
||||
import {
|
||||
Inject,
|
||||
Injectable,
|
||||
Logger,
|
||||
OnModuleDestroy,
|
||||
} from '@nestjs/common';
|
||||
import type { createClient } from 'redis';
|
||||
import { GARNET_CLIENT } from './garnet.constants';
|
||||
|
||||
export type GarnetRedisClient = ReturnType<typeof createClient>;
|
||||
|
||||
export interface StreamEntry {
|
||||
deliveriesCounter?: number;
|
||||
id: string;
|
||||
message: Record<string, string>;
|
||||
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<string, string> {
|
||||
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<string, unknown>).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<T>(key: string): Promise<T | null> {
|
||||
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<void> {
|
||||
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<string | null> {
|
||||
return this.client.get(key);
|
||||
}
|
||||
|
||||
public async setString(
|
||||
key: string,
|
||||
value: string,
|
||||
ttlSeconds?: number,
|
||||
): Promise<void> {
|
||||
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<boolean> {
|
||||
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<number> {
|
||||
return this.client.del(key);
|
||||
}
|
||||
|
||||
public async appendStream(
|
||||
key: string,
|
||||
fields: Record<string, string>,
|
||||
options?: { maxLen?: number },
|
||||
): Promise<string> {
|
||||
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<void> {
|
||||
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<StreamReadResult[] | null> {
|
||||
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<number> {
|
||||
return this.client.xAck(key, group, ids);
|
||||
}
|
||||
|
||||
public async streamLength(key: string): Promise<number> {
|
||||
return this.client.xLen(key);
|
||||
}
|
||||
|
||||
public async publish(channel: string, message: string): Promise<number> {
|
||||
return this.client.publish(channel, message);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user