Skip to content

Commit ff94204

Browse files
simplify code
1 parent 5e44d41 commit ff94204

File tree

1 file changed

+11
-43
lines changed

1 file changed

+11
-43
lines changed

lib/util.ts

Lines changed: 11 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,13 @@ export function hasBinary(obj: any, toJSON?: boolean): boolean {
3030
}
3131

3232
/**
33-
* Whether the client comes from version 5.x of the `redis` package
33+
* Whether the client comes from the `redis` package
3434
*
3535
* @param redisClient
3636
*
37-
* @see https://github.com/redis/node-redis/blob/master/docs/v5.md
37+
* @see https://github.com/redis/node-redis
3838
*/
39-
function isRedisV5Client(redisClient: any) {
40-
return typeof redisClient.createPool === "function";
41-
}
42-
43-
/**
44-
* Whether the client comes from version 4.x of the `redis` package
45-
*
46-
* @param redisClient
47-
*
48-
* @see https://github.com/redis/node-redis/blob/master/docs/v4-to-v5.md
49-
*/
50-
function isRedisV4Client(redisClient: any) {
39+
function isNodeRedisClient(redisClient: any) {
5140
return typeof redisClient.sSubscribe === "function";
5241
}
5342

@@ -68,7 +57,7 @@ function mapResult(result) {
6857
};
6958
}
7059

71-
const POOL = Symbol("redis_v5_pool");
60+
const BLOCK_DURATION_IN_MS = 200;
7261

7362
/**
7463
* @see https://redis.io/commands/xread/
@@ -79,28 +68,7 @@ export function XREAD(
7968
offset: string,
8069
readCount: number
8170
) {
82-
if (isRedisV5Client(redisClient)) {
83-
if (!redisClient[POOL]) {
84-
redisClient[POOL] = redisClient.createPool();
85-
86-
redisClient.on("end", () => {
87-
redisClient[POOL].destroy();
88-
});
89-
}
90-
91-
return redisClient[POOL].xRead(
92-
[
93-
{
94-
key: streamName,
95-
id: offset,
96-
},
97-
],
98-
{
99-
COUNT: readCount,
100-
BLOCK: 5000,
101-
}
102-
);
103-
} else if (isRedisV4Client(redisClient)) {
71+
if (isNodeRedisClient(redisClient)) {
10472
return import("redis").then((redisPackage) => {
10573
return redisClient.xRead(
10674
redisPackage.commandOptions({
@@ -114,13 +82,13 @@ export function XREAD(
11482
],
11583
{
11684
COUNT: readCount,
117-
BLOCK: 5000,
85+
BLOCK: BLOCK_DURATION_IN_MS,
11886
}
11987
);
12088
});
12189
} else {
12290
return redisClient
123-
.xread("BLOCK", 100, "COUNT", readCount, "STREAMS", streamName, offset)
91+
.xread("BLOCK", BLOCK_DURATION_IN_MS, "COUNT", readCount, "STREAMS", streamName, offset)
12492
.then((results) => {
12593
if (results === null) {
12694
return null;
@@ -143,7 +111,7 @@ export function XADD(
143111
payload: any,
144112
maxLenThreshold: number
145113
) {
146-
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
114+
if (isNodeRedisClient(redisClient)) {
147115
return redisClient.xAdd(streamName, "*", payload, {
148116
TRIM: {
149117
strategy: "MAXLEN",
@@ -170,7 +138,7 @@ export function XRANGE(
170138
start: string,
171139
end: string
172140
) {
173-
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
141+
if (isNodeRedisClient(redisClient)) {
174142
return redisClient.xRange(streamName, start, end);
175143
} else {
176144
return redisClient.xrange(streamName, start, end).then((res) => {
@@ -188,7 +156,7 @@ export function SET(
188156
value: string,
189157
expiryInSeconds: number
190158
) {
191-
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
159+
if (isNodeRedisClient(redisClient)) {
192160
return redisClient.set(key, value, {
193161
PX: expiryInSeconds,
194162
});
@@ -201,7 +169,7 @@ export function SET(
201169
* @see https://redis.io/commands/getdel/
202170
*/
203171
export function GETDEL(redisClient: any, key: string) {
204-
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
172+
if (isNodeRedisClient(redisClient)) {
205173
// note: GETDEL was added in Redis version 6.2
206174
return redisClient.multi().get(key).del(key).exec();
207175
} else {

0 commit comments

Comments
 (0)