@socket.io/redis-adapter
Advanced tools
Comparing version 8.2.0 to 8.2.1
@@ -677,37 +677,3 @@ "use strict"; | ||
serverCount() { | ||
if (this.pubClient.constructor.name === "Cluster" || | ||
this.pubClient.isCluster) { | ||
// ioredis cluster | ||
const nodes = this.pubClient.nodes(); | ||
return Promise.all(nodes.map((node) => node | ||
.send_command("pubsub", ["numsub", this.requestChannel]) | ||
.then(util_1.parseNumSubResponse))).then(util_1.sumValues); | ||
} | ||
else if (typeof this.pubClient.pSubscribe === "function") { | ||
// node-redis client | ||
const isCluster = Array.isArray(this.pubClient.masters); | ||
if (isCluster) { | ||
const nodes = this.pubClient.masters; | ||
return Promise.all(nodes.map((node) => { | ||
return node.client | ||
.sendCommand(["pubsub", "numsub", this.requestChannel]) | ||
.then(util_1.parseNumSubResponse); | ||
})).then(util_1.sumValues); | ||
} | ||
else { | ||
return this.pubClient | ||
.sendCommand(["pubsub", "numsub", this.requestChannel]) | ||
.then(util_1.parseNumSubResponse); | ||
} | ||
} | ||
else { | ||
// ioredis or node-redis v3 client | ||
return new Promise((resolve, reject) => { | ||
this.pubClient.send_command("pubsub", ["numsub", this.requestChannel], (err, numSub) => { | ||
if (err) | ||
return reject(err); | ||
resolve((0, util_1.parseNumSubResponse)(numSub)); | ||
}); | ||
}); | ||
} | ||
return (0, util_1.PUBSUB)(this.pubClient, "NUMSUB", this.requestChannel); | ||
} | ||
@@ -714,0 +680,0 @@ close() { |
@@ -9,3 +9,2 @@ "use strict"; | ||
const debug = (0, debug_1.default)("socket.io-redis"); | ||
const RETURN_BUFFERS = true; | ||
/** | ||
@@ -38,4 +37,4 @@ * Create a new Adapter based on Redis sharded Pub/Sub introduced in Redis 7.0. | ||
const handler = (message, channel) => this.onRawMessage(message, channel); | ||
this.subClient.sSubscribe(this.channel, handler, RETURN_BUFFERS); | ||
this.subClient.sSubscribe(this.responseChannel, handler, RETURN_BUFFERS); | ||
(0, util_1.SSUBSCRIBE)(this.subClient, this.channel, handler); | ||
(0, util_1.SSUBSCRIBE)(this.subClient, this.responseChannel, handler); | ||
if (this.opts.subscriptionMode === "dynamic") { | ||
@@ -45,3 +44,3 @@ this.on("create-room", (room) => { | ||
if (isPublicRoom) { | ||
this.subClient.sSubscribe(this.dynamicChannel(room), handler, RETURN_BUFFERS); | ||
(0, util_1.SSUBSCRIBE)(this.subClient, this.dynamicChannel(room), handler); | ||
} | ||
@@ -52,3 +51,3 @@ }); | ||
if (isPublicRoom) { | ||
this.subClient.sUnsubscribe(this.dynamicChannel(room)); | ||
(0, util_1.SUNSUBSCRIBE)(this.subClient, this.dynamicChannel(room)); | ||
} | ||
@@ -68,3 +67,3 @@ }); | ||
} | ||
return this.subClient.sUnsubscribe(channels); | ||
return Promise.all(channels.map((channel) => (0, util_1.SUNSUBSCRIBE)(this.subClient, channel))).then(); | ||
} | ||
@@ -74,3 +73,3 @@ publishMessage(message) { | ||
debug("publishing message of type %s to %s", message.type, channel); | ||
this.pubClient.sPublish(channel, this.encode(message)); | ||
(0, util_1.SPUBLISH)(this.pubClient, channel, this.encode(message)); | ||
return Promise.resolve(""); | ||
@@ -97,3 +96,3 @@ } | ||
debug("publishing response of type %s to %s", response.type, requesterUid); | ||
this.pubClient.sPublish(`${this.channel}${requesterUid}#`, this.encode(response)); | ||
(0, util_1.SPUBLISH)(this.pubClient, `${this.channel}${requesterUid}#`, this.encode(response)); | ||
} | ||
@@ -136,16 +135,4 @@ encode(message) { | ||
serverCount() { | ||
if (this.pubClient.constructor.name === "Cluster" || | ||
this.pubClient.isCluster) { | ||
return Promise.all(this.pubClient.nodes().map((node) => { | ||
return node | ||
.sendCommand(["PUBSUB", "SHARDNUMSUB", this.channel]) | ||
.then(util_1.parseNumSubResponse); | ||
})).then(util_1.sumValues); | ||
} | ||
else { | ||
return this.pubClient | ||
.sendCommand(["PUBSUB", "SHARDNUMSUB", this.channel]) | ||
.then(util_1.parseNumSubResponse); | ||
} | ||
return (0, util_1.PUBSUB)(this.pubClient, "SHARDNUMSUB", this.channel); | ||
} | ||
} |
@@ -0,1 +1,2 @@ | ||
/// <reference types="node" /> | ||
export declare function hasBinary(obj: any, toJSON?: boolean): boolean; | ||
@@ -5,1 +6,5 @@ export declare function randomId(): string; | ||
export declare function sumValues(values: any): any; | ||
export declare function SSUBSCRIBE(redisClient: any, channel: string, handler: (rawMessage: Buffer, channel: Buffer) => void): void; | ||
export declare function SUNSUBSCRIBE(redisClient: any, channel: string | string[]): void; | ||
export declare function SPUBLISH(redisClient: any, channel: string, payload: string | Uint8Array): void; | ||
export declare function PUBSUB(redisClient: any, arg: string, channel: string): any; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.sumValues = exports.parseNumSubResponse = exports.randomId = exports.hasBinary = void 0; | ||
exports.PUBSUB = exports.SPUBLISH = exports.SUNSUBSCRIBE = exports.SSUBSCRIBE = exports.sumValues = exports.parseNumSubResponse = exports.randomId = exports.hasBinary = void 0; | ||
const crypto_1 = require("crypto"); | ||
@@ -45,1 +45,83 @@ function hasBinary(obj, toJSON) { | ||
exports.sumValues = sumValues; | ||
const RETURN_BUFFERS = true; | ||
/** | ||
* Whether the client comes from the `redis` package | ||
* | ||
* @param redisClient | ||
* | ||
* @see https://github.com/redis/node-redis | ||
*/ | ||
function isRedisV4Client(redisClient) { | ||
return typeof redisClient.sSubscribe === "function"; | ||
} | ||
function SSUBSCRIBE(redisClient, channel, handler) { | ||
if (isRedisV4Client(redisClient)) { | ||
redisClient.sSubscribe(channel, handler, RETURN_BUFFERS); | ||
} | ||
else { | ||
redisClient.ssubscribe(channel); | ||
redisClient.on("smessageBuffer", (rawChannel, message) => { | ||
if (rawChannel.toString() === channel) { | ||
handler(message, rawChannel); | ||
} | ||
}); | ||
} | ||
} | ||
exports.SSUBSCRIBE = SSUBSCRIBE; | ||
function SUNSUBSCRIBE(redisClient, channel) { | ||
if (isRedisV4Client(redisClient)) { | ||
redisClient.sUnsubscribe(channel); | ||
} | ||
else { | ||
redisClient.sunsubscribe(channel); | ||
} | ||
} | ||
exports.SUNSUBSCRIBE = SUNSUBSCRIBE; | ||
function SPUBLISH(redisClient, channel, payload) { | ||
if (isRedisV4Client(redisClient)) { | ||
redisClient.sPublish(channel, payload); | ||
} | ||
else { | ||
redisClient.spublish(channel, payload); | ||
} | ||
} | ||
exports.SPUBLISH = SPUBLISH; | ||
function PUBSUB(redisClient, arg, channel) { | ||
if (redisClient.constructor.name === "Cluster" || redisClient.isCluster) { | ||
// ioredis cluster | ||
return Promise.all(redisClient.nodes().map((node) => { | ||
return node | ||
.send_command("PUBSUB", [arg, channel]) | ||
.then(parseNumSubResponse); | ||
})).then(sumValues); | ||
} | ||
else if (isRedisV4Client(redisClient)) { | ||
const isCluster = Array.isArray(redisClient.masters); | ||
if (isCluster) { | ||
// redis@4 cluster | ||
const nodes = redisClient.masters; | ||
return Promise.all(nodes.map((node) => { | ||
return node.client | ||
.sendCommand(["PUBSUB", arg, channel]) | ||
.then(parseNumSubResponse); | ||
})).then(sumValues); | ||
} | ||
else { | ||
// redis@4 standalone | ||
return redisClient | ||
.sendCommand(["PUBSUB", arg, channel]) | ||
.then(parseNumSubResponse); | ||
} | ||
} | ||
else { | ||
// ioredis / redis@3 standalone | ||
return new Promise((resolve, reject) => { | ||
redisClient.send_command("PUBSUB", [arg, channel], (err, numSub) => { | ||
if (err) | ||
return reject(err); | ||
resolve(parseNumSubResponse(numSub)); | ||
}); | ||
}); | ||
} | ||
} | ||
exports.PUBSUB = PUBSUB; |
{ | ||
"name": "@socket.io/redis-adapter", | ||
"version": "8.2.0", | ||
"version": "8.2.1", | ||
"description": "The Socket.IO Redis adapter, allowing to broadcast events between several Socket.IO servers", | ||
@@ -16,8 +16,3 @@ "license": "MIT", | ||
"scripts": { | ||
"test": "npm run format:check && tsc && npm run test:default && npm run test:redis-v4-specific-channel && npm run test:redis-v3 && npm run test:ioredis && npm run test:sharded", | ||
"test:default": "nyc mocha --bail --require ts-node/register test/*.ts", | ||
"test:redis-v4-specific-channel": "SPECIFIC_CHANNEL=1 npm run test:default", | ||
"test:redis-v3": "REDIS_CLIENT=redis-v3 npm run test:default", | ||
"test:ioredis": "REDIS_CLIENT=ioredis npm run test:default", | ||
"test:sharded": "SHARDED=1 npm run test:default", | ||
"test": "npm run format:check && tsc && nyc mocha --bail --require ts-node/register test/test-runner.ts", | ||
"format:check": "prettier --parser typescript --check 'lib/**/*.ts' 'test/**/*.ts'", | ||
@@ -40,3 +35,3 @@ "format:fix": "prettier --parser typescript --write 'lib/**/*.ts' 'test/**/*.ts'", | ||
"expect.js": "0.3.1", | ||
"ioredis": "^4.0.0", | ||
"ioredis": "^5.3.2", | ||
"mocha": "^10.1.0", | ||
@@ -43,0 +38,0 @@ "nyc": "^15.1.0", |
79227
1740
11