Skip to content

Commit aa681b3

Browse files
feat: add support for redis v4
Reference: https://github.com/redis/node-redis/blob/master/docs/v3-to-v4.md Related: - #427 - #423 - #417
1 parent b29d494 commit aa681b3

File tree

5 files changed

+418
-329
lines changed

5 files changed

+418
-329
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ jobs:
1212

1313
strategy:
1414
matrix:
15-
node-version: [10.x, 12.x, 14.x]
15+
node-version: [12.x, 14.x]
1616

1717
services:
1818
redis:

lib/index.ts

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,31 @@ export class RedisAdapter extends Adapter {
105105
}
106106
};
107107

108-
this.subClient.psubscribe(this.channel + "*", onError);
109-
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));
110-
111-
this.subClient.subscribe(
112-
[this.requestChannel, this.responseChannel],
113-
onError
114-
);
115-
this.subClient.on("messageBuffer", this.onrequest.bind(this));
108+
const isRedisV4 = typeof this.pubClient.pSubscribe === "function";
109+
if (isRedisV4) {
110+
this.subClient.pSubscribe(
111+
this.channel + "*",
112+
(msg, channel) => {
113+
this.onmessage(null, channel, msg);
114+
},
115+
true
116+
);
117+
this.subClient.subscribe(
118+
[this.requestChannel, this.responseChannel],
119+
(msg, channel) => {
120+
this.onrequest(channel, msg);
121+
}
122+
);
123+
} else {
124+
this.subClient.psubscribe(this.channel + "*", onError);
125+
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));
126+
127+
this.subClient.subscribe(
128+
[this.requestChannel, this.responseChannel],
129+
onError
130+
);
131+
this.subClient.on("messageBuffer", this.onrequest.bind(this));
132+
}
116133

117134
this.pubClient.on("error", onError);
118135
this.subClient.on("error", onError);
@@ -876,6 +893,10 @@ export class RedisAdapter extends Adapter {
876893
});
877894
return numSub;
878895
});
896+
} else if (typeof this.pubClient.pSubscribe === "function") {
897+
return this.pubClient
898+
.sendCommand(["pubsub", "numsub", this.requestChannel])
899+
.then((res) => parseInt(res[1], 10));
879900
} else {
880901
// RedisClient or Redis
881902
return new Promise((resolve, reject) => {

package-lock.json

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

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@
1313
"main": "./dist/index.js",
1414
"types": "./dist/index.d.ts",
1515
"scripts": {
16-
"test": "npm run format:check && tsc && nyc mocha --require ts-node/register test/index.ts",
16+
"test": "npm run format:check && tsc && npm run test:redis-v4 && npm run test:redis-v3 && npm run test:ioredis",
17+
"test:redis-v4": "nyc mocha --bail --require ts-node/register test/index.ts",
18+
"test:redis-v3": "REDIS_CLIENT=redis-v3 nyc mocha --bail --require ts-node/register test/index.ts",
19+
"test:ioredis": "REDIS_CLIENT=ioredis nyc mocha --bail --require ts-node/register test/index.ts",
1720
"format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'",
1821
"format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'",
1922
"prepack": "tsc"
@@ -28,13 +31,13 @@
2831
"@types/expect.js": "^0.3.29",
2932
"@types/mocha": "^8.2.1",
3033
"@types/node": "^14.14.7",
31-
"@types/redis": "^2.8.28",
3234
"expect.js": "0.3.1",
3335
"ioredis": "^4.0.0",
3436
"mocha": "^3.4.2",
3537
"nyc": "^15.1.0",
3638
"prettier": "^2.1.2",
37-
"redis": "^3.1.2",
39+
"redis": "^4.0.0",
40+
"redis-v3": "npm:redis@^3.1.2",
3841
"socket.io": "^4.1.1",
3942
"socket.io-client": "^4.1.1",
4043
"ts-node": "^9.1.1",

0 commit comments

Comments
 (0)