Skip to content

Commit ff9b309

Browse files
committed
fix: handle string values in push message reply comparison
Buffer.equals() was failing when reply[0] was a string instead of a Buffer, causing hangs on push notifications. Now converts strings to Buffers before comparison in PubSub and commands-queue handlers. Changes: - PubSub.isStatusReply: convert reply[0] to Buffer if string - PubSub.isShardedUnsubscribe: convert reply[0] to Buffer if string - PubSub.handleMessageReply: convert reply[0] to Buffer if string - commands-queue PONG handler: convert reply[0] to Buffer if string
1 parent 76743d2 commit ff9b309

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

packages/client/lib/client/commands-queue.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,8 @@ export default class RedisCommandsQueue {
290290
if (Array.isArray(reply)) {
291291
if (this.#onPush(reply)) return;
292292

293-
if (PONG.equals(reply[0] as Buffer)) {
293+
const firstElement = typeof reply[0] === 'string' ? Buffer.from(reply[0]) : reply[0];
294+
if (PONG.equals(firstElement as Buffer)) {
294295
const { resolve, typeMapping } = this.#waitingForReply.shift()!,
295296
buffer = ((reply[1] as Buffer).length === 0 ? reply[0] : reply[1]) as Buffer;
296297
resolve(typeMapping?.[RESP_TYPES.SIMPLE_STRING] === Buffer ? buffer : buffer.toString());

packages/client/lib/client/pub-sub.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,17 +52,19 @@ export type PubSubCommand = (
5252

5353
export class PubSub {
5454
static isStatusReply(reply: Array<Buffer>): boolean {
55+
const firstElement = typeof reply[0] === 'string' ? Buffer.from(reply[0]) : reply[0];
5556
return (
56-
COMMANDS[PUBSUB_TYPE.CHANNELS].subscribe.equals(reply[0]) ||
57-
COMMANDS[PUBSUB_TYPE.CHANNELS].unsubscribe.equals(reply[0]) ||
58-
COMMANDS[PUBSUB_TYPE.PATTERNS].subscribe.equals(reply[0]) ||
59-
COMMANDS[PUBSUB_TYPE.PATTERNS].unsubscribe.equals(reply[0]) ||
60-
COMMANDS[PUBSUB_TYPE.SHARDED].subscribe.equals(reply[0])
57+
COMMANDS[PUBSUB_TYPE.CHANNELS].subscribe.equals(firstElement) ||
58+
COMMANDS[PUBSUB_TYPE.CHANNELS].unsubscribe.equals(firstElement) ||
59+
COMMANDS[PUBSUB_TYPE.PATTERNS].subscribe.equals(firstElement) ||
60+
COMMANDS[PUBSUB_TYPE.PATTERNS].unsubscribe.equals(firstElement) ||
61+
COMMANDS[PUBSUB_TYPE.SHARDED].subscribe.equals(firstElement)
6162
);
6263
}
6364

6465
static isShardedUnsubscribe(reply: Array<Buffer>): boolean {
65-
return COMMANDS[PUBSUB_TYPE.SHARDED].unsubscribe.equals(reply[0]);
66+
const firstElement = typeof reply[0] === 'string' ? Buffer.from(reply[0]) : reply[0];
67+
return COMMANDS[PUBSUB_TYPE.SHARDED].unsubscribe.equals(firstElement);
6668
}
6769

6870
static #channelsArray(channels: string | Array<string>) {
@@ -371,22 +373,23 @@ export class PubSub {
371373
}
372374

373375
handleMessageReply(reply: Array<Buffer>): boolean {
374-
if (COMMANDS[PUBSUB_TYPE.CHANNELS].message.equals(reply[0])) {
376+
const firstElement = typeof reply[0] === 'string' ? Buffer.from(reply[0]) : reply[0];
377+
if (COMMANDS[PUBSUB_TYPE.CHANNELS].message.equals(firstElement)) {
375378
this.#emitPubSubMessage(
376379
PUBSUB_TYPE.CHANNELS,
377380
reply[2],
378381
reply[1]
379382
);
380383
return true;
381-
} else if (COMMANDS[PUBSUB_TYPE.PATTERNS].message.equals(reply[0])) {
384+
} else if (COMMANDS[PUBSUB_TYPE.PATTERNS].message.equals(firstElement)) {
382385
this.#emitPubSubMessage(
383386
PUBSUB_TYPE.PATTERNS,
384387
reply[3],
385388
reply[2],
386389
reply[1]
387390
);
388391
return true;
389-
} else if (COMMANDS[PUBSUB_TYPE.SHARDED].message.equals(reply[0])) {
392+
} else if (COMMANDS[PUBSUB_TYPE.SHARDED].message.equals(firstElement)) {
390393
this.#emitPubSubMessage(
391394
PUBSUB_TYPE.SHARDED,
392395
reply[2],

0 commit comments

Comments
 (0)