Skip to content

Commit 2a4fa57

Browse files
authored
Add nestjs 11 support (#27)
1 parent d05fd00 commit 2a4fa57

File tree

5 files changed

+270
-143
lines changed

5 files changed

+270
-143
lines changed

lib/redis.client.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ClientProxy, ReadPacket, WritePacket } from '@nestjs/microservices';
22
import { Injectable, Logger } from '@nestjs/common';
3-
import { CONNECT_EVENT, ERROR_EVENT } from '@nestjs/microservices/constants';
3+
import { RedisEventsMap } from '@nestjs/microservices/events/redis.events';
44
import { ClientConstructorOptions, RedisInstance } from './interfaces';
55
import { createRedisConnection } from './redis.utils';
66
import { RequestsMap } from './requests-map';
@@ -37,7 +37,7 @@ export class RedisStreamClient extends ClientProxy {
3737
this.handleError(this.redis);
3838

3939
// when server instance connect, bind handlers.
40-
this.redis.on(CONNECT_EVENT, () => {
40+
this.redis.on(RedisEventsMap.CONNECT, () => {
4141
this.logger.log(
4242
'Redis Client Responses Listener connected successfully on ' +
4343
(this.options.connection?.url ??
@@ -69,7 +69,7 @@ export class RedisStreamClient extends ClientProxy {
6969

7070
this.client = createRedisConnection(this.options?.connection);
7171
this.connection = await firstValueFrom(
72-
this.connect$(this.client, ERROR_EVENT, CONNECT_EVENT).pipe(share()),
72+
this.connect$(this.client, RedisEventsMap.ERROR, RedisEventsMap.CONNECT).pipe(share()),
7373
);
7474
this.handleError(this.client);
7575
return this.connection;
@@ -408,9 +408,13 @@ export class RedisStreamClient extends ClientProxy {
408408
}
409409

410410
public handleError(stream: any) {
411-
stream.on(ERROR_EVENT, (err: any) => {
411+
stream.on(RedisEventsMap.ERROR, (err: any) => {
412412
this.logger.error('Redis Streams Client ' + err);
413413
this.close();
414414
});
415415
}
416+
417+
public unwrap<T>(): T {
418+
return this.client as T;
419+
}
416420
}

lib/redis.server.ts

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from './interfaces';
99

1010
import { createRedisConnection } from './redis.utils';
11-
import { CONNECT_EVENT, ERROR_EVENT } from '@nestjs/microservices/constants';
11+
import { RedisEventsMap } from '@nestjs/microservices/events/redis.events';
1212
import { deserialize, serialize } from './streams.utils';
1313
import { RedisStreamContext } from './stream.context';
1414
import { Observable } from 'rxjs';
@@ -37,7 +37,7 @@ export class RedisStreamStrategy
3737
this.handleError(this.client);
3838

3939
// when server instance connect, bind handlers.
40-
this.redis.on(CONNECT_EVENT, () => {
40+
this.redis.on(RedisEventsMap.CONNECT, () => {
4141
this.logger.log(
4242
'Redis connected successfully on ' +
4343
(this.options.connection?.url ??
@@ -150,13 +150,13 @@ export class RedisStreamStrategy
150150
if (!this.client) throw new Error('Redis client instance not found.');
151151

152152
const commandArgs: RedisValue[] = [];
153-
if(this.options.streams?.maxLen){
154-
commandArgs.push("MAXLEN")
155-
commandArgs.push("~")
156-
commandArgs.push(this.options.streams.maxLen.toString())
153+
if (this.options.streams?.maxLen) {
154+
commandArgs.push('MAXLEN');
155+
commandArgs.push('~');
156+
commandArgs.push(this.options.streams.maxLen.toString());
157157
}
158-
commandArgs.push("*")
159-
158+
commandArgs.push('*');
159+
160160
await this.client.xadd(
161161
responseObj.stream,
162162
...commandArgs,
@@ -339,7 +339,7 @@ export class RedisStreamStrategy
339339

340340
// for redis instances. need to add mechanism to try to connect back.
341341
public handleError(stream: any) {
342-
stream.on(ERROR_EVENT, (err: any) => {
342+
stream.on(RedisEventsMap.ERROR, (err: any) => {
343343
this.logger.error('Redis instance error: ' + err);
344344
this.close();
345345
});
@@ -350,4 +350,18 @@ export class RedisStreamStrategy
350350
this.redis && this.redis.quit();
351351
this.client && this.client.quit();
352352
}
353+
354+
public on<
355+
EventKey extends string = string,
356+
EventCallback extends Function = Function,
357+
>(event: EventKey, callback: EventCallback): this {
358+
if (this.redis) {
359+
this.redis.on(event, callback as unknown as (...args: unknown[]) => void);
360+
}
361+
return this;
362+
}
363+
364+
public unwrap<T>(): T {
365+
return this.redis as T;
366+
}
353367
}

lib/streams.utils.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RedisStreamContext } from './stream.context';
22
import { Logger } from '@nestjs/common';
3-
import { v4 as uuidv4 } from 'uuid';
3+
import { randomUUID } from 'node:crypto';
44

55
let logger = new Logger('RedisStreams/streams-utils');
66

@@ -87,5 +87,5 @@ export function stringifyMessage(messageObj: Record<string, string>): string[] {
8787
}
8888

8989
export function generateCorrelationId() {
90-
return uuidv4();
90+
return randomUUID();
9191
}

0 commit comments

Comments
 (0)