Skip to content

Commit 7d3a3b3

Browse files
add tests
1 parent efa8b02 commit 7d3a3b3

File tree

5 files changed

+287
-87
lines changed

5 files changed

+287
-87
lines changed

lib/adapter.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,6 @@ export function createAdapter(
5555
redisClient: any,
5656
opts?: RedisStreamsAdapterOptions & ClusterAdapterOptions
5757
) {
58-
const internalRedisClient =
59-
typeof redisClient.createPool === "function"
60-
? redisClient.createPool()
61-
: redisClient;
6258
const namespaceToAdapters = new Map<string, RedisStreamsAdapter>();
6359
const options = Object.assign(
6460
{
@@ -78,7 +74,7 @@ export function createAdapter(
7874
async function poll() {
7975
try {
8076
let response = await XREAD(
81-
internalRedisClient,
77+
redisClient,
8278
options.streamName,
8379
offset,
8480
options.readCount
@@ -110,7 +106,7 @@ export function createAdapter(
110106
}
111107

112108
return function (nsp) {
113-
const adapter = new RedisStreamsAdapter(nsp, internalRedisClient, options);
109+
const adapter = new RedisStreamsAdapter(nsp, redisClient, options);
114110
namespaceToAdapters.set(nsp.name, adapter);
115111

116112
if (!polling) {

lib/util.ts

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,25 @@ export function hasBinary(obj: any, toJSON?: boolean): boolean {
3030
}
3131

3232
/**
33-
* Whether the client comes from the `redis` package
33+
* Whether the client comes from version 5.x of the `redis` package
3434
*
3535
* @param redisClient
3636
*
37-
* @see https://github.com/redis/node-redis
37+
* @see https://github.com/redis/node-redis/blob/master/docs/v5.md
38+
*/
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
3849
*/
3950
function isRedisV4Client(redisClient: any) {
40-
return (
41-
typeof redisClient.sSubscribe === "function" ||
42-
typeof redisClient.totalClients === "number"
43-
);
51+
return typeof redisClient.sSubscribe === "function";
4452
}
4553

4654
/**
@@ -60,6 +68,8 @@ function mapResult(result) {
6068
};
6169
}
6270

71+
const POOL = Symbol("redis_v5_pool");
72+
6373
/**
6474
* @see https://redis.io/commands/xread/
6575
*/
@@ -69,29 +79,40 @@ export function XREAD(
6979
offset: string,
7080
readCount: number
7181
) {
72-
if (isRedisV4Client(redisClient)) {
73-
return import("redis").then((redisPackage) => {
74-
const streams = [
82+
if (isRedisV5Client(redisClient)) {
83+
if (!redisClient[POOL]) {
84+
redisClient[POOL] = redisClient.createPool();
85+
}
86+
87+
return redisClient[POOL].xRead(
88+
[
7589
{
7690
key: streamName,
7791
id: offset,
7892
},
79-
];
80-
const options = {
93+
],
94+
{
8195
COUNT: readCount,
8296
BLOCK: 5000,
83-
};
84-
if (redisPackage.commandOptions) {
85-
return redisClient.xRead(
86-
redisPackage.commandOptions({
87-
isolated: true,
88-
}),
89-
streams,
90-
options
91-
);
92-
} else {
93-
return redisClient.xRead(streams, options);
9497
}
98+
);
99+
} else if (isRedisV4Client(redisClient)) {
100+
return import("redis").then((redisPackage) => {
101+
return redisClient.xRead(
102+
redisPackage.commandOptions({
103+
isolated: true,
104+
}),
105+
[
106+
{
107+
key: streamName,
108+
id: offset,
109+
},
110+
],
111+
{
112+
COUNT: readCount,
113+
BLOCK: 5000,
114+
}
115+
);
95116
});
96117
} else {
97118
return redisClient
@@ -118,7 +139,7 @@ export function XADD(
118139
payload: any,
119140
maxLenThreshold: number
120141
) {
121-
if (isRedisV4Client(redisClient)) {
142+
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
122143
return redisClient.xAdd(streamName, "*", payload, {
123144
TRIM: {
124145
strategy: "MAXLEN",
@@ -145,7 +166,7 @@ export function XRANGE(
145166
start: string,
146167
end: string
147168
) {
148-
if (isRedisV4Client(redisClient)) {
169+
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
149170
return redisClient.xRange(streamName, start, end);
150171
} else {
151172
return redisClient.xrange(streamName, start, end).then((res) => {
@@ -163,7 +184,7 @@ export function SET(
163184
value: string,
164185
expiryInSeconds: number
165186
) {
166-
if (isRedisV4Client(redisClient)) {
187+
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
167188
return redisClient.set(key, value, {
168189
PX: expiryInSeconds,
169190
});
@@ -176,7 +197,7 @@ export function SET(
176197
* @see https://redis.io/commands/getdel/
177198
*/
178199
export function GETDEL(redisClient: any, key: string) {
179-
if (isRedisV4Client(redisClient)) {
200+
if (isRedisV4Client(redisClient) || isRedisV5Client(redisClient)) {
180201
// note: GETDEL was added in Redis version 6.2
181202
return redisClient.multi().get(key).del(key).exec();
182203
} else {

package-lock.json

Lines changed: 138 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
"test": "npm run format:check && npm run compile && npm run test:redis-standalone && npm run test:redis-cluster && npm run test:ioredis-standalone && npm run test:ioredis-cluster && npm run test:valkey-standalone",
2121
"test:redis-standalone": "nyc mocha --require ts-node/register test/**/*.ts",
2222
"test:redis-cluster": "cross-env REDIS_CLUSTER=1 mocha --require ts-node/register test/**/*.ts",
23+
"test:redis-v5-standalone": "cross-env REDIS_LIB=redis-v5 mocha --require ts-node/register test/**/*.ts",
24+
"test:redis-v5-cluster": "cross-env REDIS_LIB=redis-v5 REDIS_CLUSTER=1 mocha --require ts-node/register test/**/*.ts",
2325
"test:ioredis-standalone": "cross-env REDIS_LIB=ioredis mocha --require ts-node/register test/**/*.ts",
2426
"test:ioredis-cluster": "cross-env REDIS_LIB=ioredis REDIS_CLUSTER=1 mocha --require ts-node/register test/**/*.ts",
2527
"test:valkey-standalone": "cross-env VALKEY=1 mocha --require ts-node/register test/**/*.ts"
@@ -45,6 +47,7 @@
4547
"nyc": "^15.1.0",
4648
"prettier": "^2.8.7",
4749
"redis": "^4.6.5",
50+
"redis-v5": "npm:redis@~5.8.3",
4851
"rimraf": "^5.0.5",
4952
"socket.io": "^4.6.1",
5053
"socket.io-client": "^4.6.1",

0 commit comments

Comments
 (0)