Skip to content

Commit

Permalink
feat: add support for redis v4
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Nov 29, 2021
1 parent b29d494 commit aa681b3
Show file tree
Hide file tree
Showing 5 changed files with 418 additions and 329 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 14.x]
node-version: [12.x, 14.x]

services:
redis:
Expand Down
37 changes: 29 additions & 8 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,31 @@ export class RedisAdapter extends Adapter {
}
};

this.subClient.psubscribe(this.channel + "*", onError);
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));

this.subClient.subscribe(
[this.requestChannel, this.responseChannel],
onError
);
this.subClient.on("messageBuffer", this.onrequest.bind(this));
const isRedisV4 = typeof this.pubClient.pSubscribe === "function";
if (isRedisV4) {
this.subClient.pSubscribe(
this.channel + "*",
(msg, channel) => {
this.onmessage(null, channel, msg);
},
true
);
this.subClient.subscribe(
[this.requestChannel, this.responseChannel],
(msg, channel) => {
this.onrequest(channel, msg);
}
);
} else {
this.subClient.psubscribe(this.channel + "*", onError);
this.subClient.on("pmessageBuffer", this.onmessage.bind(this));

this.subClient.subscribe(
[this.requestChannel, this.responseChannel],
onError
);
this.subClient.on("messageBuffer", this.onrequest.bind(this));
}

this.pubClient.on("error", onError);
this.subClient.on("error", onError);
Expand Down Expand Up @@ -876,6 +893,10 @@ export class RedisAdapter extends Adapter {
});
return numSub;
});
} else if (typeof this.pubClient.pSubscribe === "function") {
return this.pubClient
.sendCommand(["pubsub", "numsub", this.requestChannel])
.then((res) => parseInt(res[1], 10));
} else {
// RedisClient or Redis
return new Promise((resolve, reject) => {
Expand Down
98 changes: 68 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"scripts": {
"test": "npm run format:check && tsc && nyc mocha --require ts-node/register test/index.ts",
"test": "npm run format:check && tsc && npm run test:redis-v4 && npm run test:redis-v3 && npm run test:ioredis",
"test:redis-v4": "nyc mocha --bail --require ts-node/register test/index.ts",
"test:redis-v3": "REDIS_CLIENT=redis-v3 nyc mocha --bail --require ts-node/register test/index.ts",
"test:ioredis": "REDIS_CLIENT=ioredis nyc mocha --bail --require ts-node/register test/index.ts",
"format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'",
"format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'",
"prepack": "tsc"
Expand All @@ -28,13 +31,13 @@
"@types/expect.js": "^0.3.29",
"@types/mocha": "^8.2.1",
"@types/node": "^14.14.7",
"@types/redis": "^2.8.28",
"expect.js": "0.3.1",
"ioredis": "^4.0.0",
"mocha": "^3.4.2",
"nyc": "^15.1.0",
"prettier": "^2.1.2",
"redis": "^3.1.2",
"redis": "^4.0.0",
"redis-v3": "npm:redis@^3.1.2",
"socket.io": "^4.1.1",
"socket.io-client": "^4.1.1",
"ts-node": "^9.1.1",
Expand Down
Loading

0 comments on commit aa681b3

Please sign in to comment.