1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-06 02:15:48 +03:00

fix #1718 - add support for buffers in pubsub

This commit is contained in:
leibale
2021-11-16 21:14:47 -05:00
parent 51b640bca9
commit fc73cb8736
5 changed files with 244 additions and 88 deletions

12
.github/README.md vendored
View File

@@ -150,6 +150,18 @@ Publish a message on a channel:
await publisher.publish('channel', 'message'); await publisher.publish('channel', 'message');
``` ```
There is support for buffers as well:
```typescript
await subscriber.subscribe('channel', (message) => {
console.log(message); // <Buffer 6d 65 73 73 61 67 65>
}, true);
await subscriber.pSubscribe('channe*', (message, channel) => {
console.log(message, channel); // <Buffer 6d 65 73 73 61 67 65>, <Buffer 63 68 61 6e 6e 65 6c>
}, true);
```
### Scan Iterator ### Scan Iterator
[`SCAN`](https://redis.io/commands/scan) results can be looped over using [async iterators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator): [`SCAN`](https://redis.io/commands/scan) results can be looped over using [async iterators](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/asyncIterator):

View File

@@ -39,9 +39,29 @@ export enum PubSubUnsubscribeCommands {
PUNSUBSCRIBE = 'PUNSUBSCRIBE' PUNSUBSCRIBE = 'PUNSUBSCRIBE'
} }
export type PubSubListener = (message: string, channel: string) => unknown; type PubSubArgumentTypes = Buffer | string;
export type PubSubListenersMap = Map<string, Set<PubSubListener>>; export type PubSubListener<
BUFFER_MODE extends boolean = false,
T = BUFFER_MODE extends true ? Buffer : string
> = (message: T, channel: T) => unknown;
interface PubSubListeners {
buffers: Set<PubSubListener<true>>;
strings: Set<PubSubListener<false>>;
}
type PubSubListenersMap = Map<string, PubSubListeners>;
interface PubSubState {
subscribing: number;
subscribed: number;
unsubscribing: number;
listeners: {
channels: PubSubListenersMap;
patterns: PubSubListenersMap;
};
}
export default class RedisCommandsQueue { export default class RedisCommandsQueue {
static #flushQueue<T extends CommandWaitingForReply>(queue: LinkedList<T>, err: Error): void { static #flushQueue<T extends CommandWaitingForReply>(queue: LinkedList<T>, err: Error): void {
@@ -50,10 +70,20 @@ export default class RedisCommandsQueue {
} }
} }
static #emitPubSubMessage(listeners: Set<PubSubListener>, message: string, channel: string): void { static #emitPubSubMessage(listenersMap: PubSubListenersMap, message: Buffer, channel: Buffer, pattern?: Buffer): void {
for (const listener of listeners) { const keyString = (pattern || channel).toString(),
listeners = listenersMap.get(keyString)!;
for (const listener of listeners.buffers) {
listener(message, channel); listener(message, channel);
} }
if (!listeners.strings.size) return;
const messageString = message.toString(),
channelString = pattern ? channel.toString() : keyString;
for (const listener of listeners.strings) {
listener(messageString, channelString);
}
} }
readonly #maxLength: number | null | undefined; readonly #maxLength: number | null | undefined;
@@ -62,41 +92,43 @@ export default class RedisCommandsQueue {
readonly #waitingForReply = new LinkedList<CommandWaitingForReply>(); readonly #waitingForReply = new LinkedList<CommandWaitingForReply>();
readonly #pubSubState = { #pubSubState: PubSubState | undefined;
subscribing: 0,
subscribed: 0,
unsubscribing: 0
};
readonly #pubSubListeners = { static readonly #PUB_SUB_MESSAGES = {
channels: <PubSubListenersMap>new Map(), message: Buffer.from('message'),
patterns: <PubSubListenersMap>new Map() pMessage: Buffer.from('pmessage'),
subscribe: Buffer.from('subscribe'),
pSubscribe: Buffer.from('psubscribe'),
unsubscribe: Buffer.from('unsunscribe'),
pUnsubscribe: Buffer.from('punsubscribe')
}; };
readonly #parser = new RedisParser({ readonly #parser = new RedisParser({
returnReply: (reply: unknown) => { returnReply: (reply: unknown) => {
if ((this.#pubSubState.subscribing || this.#pubSubState.subscribed) && Array.isArray(reply)) { if (this.#pubSubState && Array.isArray(reply)) {
switch (reply[0]) { if (RedisCommandsQueue.#PUB_SUB_MESSAGES.message.equals(reply[0])) {
case 'message': return RedisCommandsQueue.#emitPubSubMessage(
return RedisCommandsQueue.#emitPubSubMessage( this.#pubSubState.listeners.channels,
this.#pubSubListeners.channels.get(reply[1])!, reply[2],
reply[2], reply[1]
reply[1] );
); } else if (RedisCommandsQueue.#PUB_SUB_MESSAGES.pMessage.equals(reply[0])) {
return RedisCommandsQueue.#emitPubSubMessage(
case 'pmessage': this.#pubSubState.listeners.patterns,
return RedisCommandsQueue.#emitPubSubMessage( reply[3],
this.#pubSubListeners.patterns.get(reply[1])!, reply[2],
reply[3], reply[1]
reply[2] );
); } else if (
RedisCommandsQueue.#PUB_SUB_MESSAGES.subscribe.equals(reply[0]) ||
case 'subscribe': RedisCommandsQueue.#PUB_SUB_MESSAGES.pSubscribe.equals(reply[0]) ||
case 'psubscribe': RedisCommandsQueue.#PUB_SUB_MESSAGES.unsubscribe.equals(reply[0]) ||
if (--this.#waitingForReply.head!.value.channelsCounter! === 0) { RedisCommandsQueue.#PUB_SUB_MESSAGES.pUnsubscribe.equals(reply[0])
this.#shiftWaitingForReply().resolve(); ) {
} if (--this.#waitingForReply.head!.value.channelsCounter! === 0) {
return; this.#shiftWaitingForReply().resolve();
}
return;
} }
} }
@@ -112,7 +144,7 @@ export default class RedisCommandsQueue {
} }
addCommand<T = RedisCommandRawReply>(args: RedisCommandArguments, options?: QueueCommandOptions, bufferMode?: boolean): Promise<T> { addCommand<T = RedisCommandRawReply>(args: RedisCommandArguments, options?: QueueCommandOptions, bufferMode?: boolean): Promise<T> {
if (this.#pubSubState.subscribing || this.#pubSubState.subscribed) { if (this.#pubSubState) {
return Promise.reject(new Error('Cannot send commands in PubSub mode')); return Promise.reject(new Error('Cannot send commands in PubSub mode'));
} else if (this.#maxLength && this.#waitingToBeSent.length + this.#waitingForReply.length >= this.#maxLength) { } else if (this.#maxLength && this.#waitingToBeSent.length + this.#waitingForReply.length >= this.#maxLength) {
return Promise.reject(new Error('The queue is full')); return Promise.reject(new Error('The queue is full'));
@@ -126,7 +158,7 @@ export default class RedisCommandsQueue {
chainId: options?.chainId, chainId: options?.chainId,
bufferMode, bufferMode,
resolve, resolve,
reject, reject
}); });
if (options?.signal) { if (options?.signal) {
@@ -153,17 +185,41 @@ export default class RedisCommandsQueue {
}); });
} }
subscribe(command: PubSubSubscribeCommands, channels: string | Array<string>, listener: PubSubListener): Promise<void> { #initiatePubSubState(): PubSubState {
const channelsToSubscribe: Array<string> = [], return this.#pubSubState ??= {
listeners = command === PubSubSubscribeCommands.SUBSCRIBE ? this.#pubSubListeners.channels : this.#pubSubListeners.patterns; subscribed: 0,
subscribing: 0,
unsubscribing: 0,
listeners: {
channels: new Map(),
patterns: new Map()
}
};
}
subscribe<T extends boolean>(
command: PubSubSubscribeCommands,
channels: PubSubArgumentTypes | Array<PubSubArgumentTypes>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
const pubSubState = this.#initiatePubSubState(),
channelsToSubscribe: Array<PubSubArgumentTypes> = [],
listenersMap = command === PubSubSubscribeCommands.SUBSCRIBE ? pubSubState.listeners.channels : pubSubState.listeners.patterns;
for (const channel of (Array.isArray(channels) ? channels : [channels])) { for (const channel of (Array.isArray(channels) ? channels : [channels])) {
if (listeners.has(channel)) { const channelString = typeof channel === 'string' ? channel : channel.toString();
listeners.get(channel)!.add(listener); let listeners = listenersMap.get(channelString);
continue; if (!listeners) {
listeners = {
buffers: new Set(),
strings: new Set()
};
listenersMap.set(channelString, listeners);
channelsToSubscribe.push(channel);
} }
listeners.set(channel, new Set([listener])); // https://github.com/microsoft/TypeScript/issues/23132
channelsToSubscribe.push(channel); (bufferMode ? listeners.buffers : listeners.strings).add(listener as any);
} }
if (!channelsToSubscribe.length) { if (!channelsToSubscribe.length) {
@@ -173,8 +229,20 @@ export default class RedisCommandsQueue {
return this.#pushPubSubCommand(command, channelsToSubscribe); return this.#pushPubSubCommand(command, channelsToSubscribe);
} }
unsubscribe(command: PubSubUnsubscribeCommands, channels?: string | Array<string>, listener?: PubSubListener): Promise<void> { unsubscribe<T extends boolean>(
const listeners = command === PubSubUnsubscribeCommands.UNSUBSCRIBE ? this.#pubSubListeners.channels : this.#pubSubListeners.patterns; command: PubSubUnsubscribeCommands,
channels?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
if (!this.#pubSubState) {
return Promise.resolve();
}
const listeners = command === PubSubUnsubscribeCommands.UNSUBSCRIBE ?
this.#pubSubState.listeners.channels :
this.#pubSubState.listeners.patterns;
if (!channels) { if (!channels) {
const size = listeners.size; const size = listeners.size;
listeners.clear(); listeners.clear();
@@ -183,13 +251,16 @@ export default class RedisCommandsQueue {
const channelsToUnsubscribe = []; const channelsToUnsubscribe = [];
for (const channel of (Array.isArray(channels) ? channels : [channels])) { for (const channel of (Array.isArray(channels) ? channels : [channels])) {
const set = listeners.get(channel); const sets = listeners.get(channel);
if (!set) continue; if (!sets) continue;
let shouldUnsubscribe = !listener; let shouldUnsubscribe;
if (listener) { if (listener) {
set.delete(listener); // https://github.com/microsoft/TypeScript/issues/23132
shouldUnsubscribe = set.size === 0; (bufferMode ? sets.buffers : sets.strings).delete(listener as any);
shouldUnsubscribe = !sets.buffers.size && !sets.strings.size;
} else {
shouldUnsubscribe = true;
} }
if (shouldUnsubscribe) { if (shouldUnsubscribe) {
@@ -205,11 +276,12 @@ export default class RedisCommandsQueue {
return this.#pushPubSubCommand(command, channelsToUnsubscribe); return this.#pushPubSubCommand(command, channelsToUnsubscribe);
} }
#pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels: number | Array<string>): Promise<void> { #pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels: number | Array<PubSubArgumentTypes>): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const isSubscribe = command === PubSubSubscribeCommands.SUBSCRIBE || command === PubSubSubscribeCommands.PSUBSCRIBE, const pubSubState = this.#initiatePubSubState(),
isSubscribe = command === PubSubSubscribeCommands.SUBSCRIBE || command === PubSubSubscribeCommands.PSUBSCRIBE,
inProgressKey = isSubscribe ? 'subscribing' : 'unsubscribing', inProgressKey = isSubscribe ? 'subscribing' : 'unsubscribing',
commandArgs: Array<string> = [command]; commandArgs: Array<PubSubArgumentTypes> = [command];
let channelsCounter: number; let channelsCounter: number;
if (typeof channels === 'number') { // unsubscribe only if (typeof channels === 'number') { // unsubscribe only
@@ -219,18 +291,26 @@ export default class RedisCommandsQueue {
channelsCounter = channels.length; channelsCounter = channels.length;
} }
this.#pubSubState[inProgressKey] += channelsCounter; pubSubState[inProgressKey] += channelsCounter;
this.#waitingToBeSent.push({ this.#waitingToBeSent.push({
args: commandArgs, args: commandArgs,
channelsCounter, channelsCounter,
bufferMode: true,
resolve: () => { resolve: () => {
this.#pubSubState[inProgressKey] -= channelsCounter; pubSubState[inProgressKey] -= channelsCounter;
this.#pubSubState.subscribed += channelsCounter * (isSubscribe ? 1 : -1); if (isSubscribe) {
pubSubState.subscribed += channelsCounter;
} else {
pubSubState.subscribed -= channelsCounter;
if (!pubSubState.subscribed && !pubSubState.subscribing && !pubSubState.subscribed) {
this.#pubSubState = undefined;
}
}
resolve(); resolve();
}, },
reject: () => { reject: () => {
this.#pubSubState[inProgressKey] -= channelsCounter; pubSubState[inProgressKey] -= channelsCounter * (isSubscribe ? 1 : -1);
reject(); reject();
} }
}); });
@@ -238,16 +318,14 @@ export default class RedisCommandsQueue {
} }
resubscribe(): Promise<any> | undefined { resubscribe(): Promise<any> | undefined {
if (!this.#pubSubState.subscribed && !this.#pubSubState.subscribing) { if (!this.#pubSubState) {
return; return;
} }
this.#pubSubState.subscribed = this.#pubSubState.subscribing = 0;
// TODO: acl error on one channel/pattern will reject the whole command // TODO: acl error on one channel/pattern will reject the whole command
return Promise.all([ return Promise.all([
this.#pushPubSubCommand(PubSubSubscribeCommands.SUBSCRIBE, [...this.#pubSubListeners.channels.keys()]), this.#pushPubSubCommand(PubSubSubscribeCommands.SUBSCRIBE, [...this.#pubSubState.listeners.channels.keys()]),
this.#pushPubSubCommand(PubSubSubscribeCommands.PSUBSCRIBE, [...this.#pubSubListeners.patterns.keys()]) this.#pushPubSubCommand(PubSubSubscribeCommands.PSUBSCRIBE, [...this.#pubSubState.listeners.patterns.keys()])
]); ]);
} }
@@ -269,7 +347,10 @@ export default class RedisCommandsQueue {
} }
parseResponse(data: Buffer): void { parseResponse(data: Buffer): void {
this.#parser.setReturnBuffers(!!this.#waitingForReply.head?.value.bufferMode); this.#parser.setReturnBuffers(
!!this.#waitingForReply.head?.value.bufferMode ||
!!this.#pubSubState?.subscribed
);
this.#parser.execute(data); this.#parser.execute(data);
} }

View File

@@ -561,17 +561,27 @@ describe('Client', () => {
}, GLOBAL.SERVERS.OPEN); }, GLOBAL.SERVERS.OPEN);
testUtils.testWithClient('PubSub', async publisher => { testUtils.testWithClient('PubSub', async publisher => {
function assertStringListener(message: string, channel: string) {
assert.ok(typeof message === 'string');
assert.ok(typeof channel === 'string');
}
function assertBufferListener(message: Buffer, channel: Buffer) {
assert.ok(Buffer.isBuffer(message));
assert.ok(Buffer.isBuffer(channel));
}
const subscriber = publisher.duplicate(); const subscriber = publisher.duplicate();
await subscriber.connect(); await subscriber.connect();
try { try {
const channelListener1 = spy(), const channelListener1 = spy(assertBufferListener),
channelListener2 = spy(), channelListener2 = spy(assertStringListener),
patternListener = spy(); patternListener = spy(assertStringListener);
await Promise.all([ await Promise.all([
subscriber.subscribe('channel', channelListener1), subscriber.subscribe('channel', channelListener1, true),
subscriber.subscribe('channel', channelListener2), subscriber.subscribe('channel', channelListener2),
subscriber.pSubscribe('channel*', patternListener) subscriber.pSubscribe('channel*', patternListener)
]); ]);
@@ -580,14 +590,14 @@ describe('Client', () => {
waitTillBeenCalled(channelListener1), waitTillBeenCalled(channelListener1),
waitTillBeenCalled(channelListener2), waitTillBeenCalled(channelListener2),
waitTillBeenCalled(patternListener), waitTillBeenCalled(patternListener),
publisher.publish('channel', 'message') publisher.publish(Buffer.from('channel'), Buffer.from('message'))
]); ]);
assert.ok(channelListener1.calledOnceWithExactly('message', 'channel')); assert.ok(channelListener1.calledOnceWithExactly(Buffer.from('message'), Buffer.from('channel')));
assert.ok(channelListener2.calledOnceWithExactly('message', 'channel')); assert.ok(channelListener2.calledOnceWithExactly('message', 'channel'));
assert.ok(patternListener.calledOnceWithExactly('message', 'channel')); assert.ok(patternListener.calledOnceWithExactly('message', 'channel'));
await subscriber.unsubscribe('channel', channelListener1); await subscriber.unsubscribe('channel', channelListener1, true);
await Promise.all([ await Promise.all([
waitTillBeenCalled(channelListener2), waitTillBeenCalled(channelListener2),
waitTillBeenCalled(patternListener), waitTillBeenCalled(patternListener),

View File

@@ -388,42 +388,93 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
select = this.SELECT; select = this.SELECT;
SUBSCRIBE(channels: string | Array<string>, listener: PubSubListener): Promise<void> { #subscribe<T extends boolean>(
return this.#subscribe(PubSubSubscribeCommands.SUBSCRIBE, channels, listener); command: PubSubSubscribeCommands,
channels: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
const promise = this.#queue.subscribe(
command,
channels,
listener,
bufferMode
);
this.#tick();
return promise;
}
SUBSCRIBE<T extends boolean = false>(
channels: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#subscribe(
PubSubSubscribeCommands.SUBSCRIBE,
channels,
listener,
bufferMode
);
} }
subscribe = this.SUBSCRIBE; subscribe = this.SUBSCRIBE;
PSUBSCRIBE(patterns: string | Array<string>, listener: PubSubListener): Promise<void> { PSUBSCRIBE<T extends boolean = false>(
return this.#subscribe(PubSubSubscribeCommands.PSUBSCRIBE, patterns, listener); patterns: string | Array<string>,
listener: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#subscribe(
PubSubSubscribeCommands.PSUBSCRIBE,
patterns,
listener,
bufferMode
);
} }
pSubscribe = this.PSUBSCRIBE; pSubscribe = this.PSUBSCRIBE;
#subscribe(command: PubSubSubscribeCommands, channels: string | Array<string>, listener: PubSubListener): Promise<void> { #unsubscribe<T extends boolean>(
const promise = this.#queue.subscribe(command, channels, listener); command: PubSubUnsubscribeCommands,
channels?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
const promise = this.#queue.unsubscribe(command, channels, listener, bufferMode);
this.#tick(); this.#tick();
return promise; return promise;
} }
UNSUBSCRIBE(channels?: string | Array<string>, listener?: PubSubListener): Promise<void> { UNSUBSCRIBE<T extends boolean = false>(
return this.#unsubscribe(PubSubUnsubscribeCommands.UNSUBSCRIBE, channels, listener); channels?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#unsubscribe(
PubSubUnsubscribeCommands.UNSUBSCRIBE,
channels,
listener,
bufferMode
);
} }
unsubscribe = this.UNSUBSCRIBE; unsubscribe = this.UNSUBSCRIBE;
PUNSUBSCRIBE(patterns?: string | Array<string>, listener?: PubSubListener): Promise<void> { PUNSUBSCRIBE<T extends boolean = false>(
return this.#unsubscribe(PubSubUnsubscribeCommands.PUNSUBSCRIBE, patterns, listener); patterns?: string | Array<string>,
listener?: PubSubListener<T>,
bufferMode?: T
): Promise<void> {
return this.#unsubscribe(
PubSubUnsubscribeCommands.PUNSUBSCRIBE,
patterns,
listener,
bufferMode
);
} }
pUnsubscribe = this.PUNSUBSCRIBE; pUnsubscribe = this.PUNSUBSCRIBE;
#unsubscribe(command: PubSubUnsubscribeCommands, channels?: string | Array<string>, listener?: PubSubListener): Promise<void> {
const promise = this.#queue.unsubscribe(command, channels, listener);
this.#tick();
return promise;
}
QUIT(): Promise<void> { QUIT(): Promise<void> {
return this.#socket.quit(() => { return this.#socket.quit(() => {
const quitPromise = this.#queue.addCommand(['QUIT']); const quitPromise = this.#queue.addCommand(['QUIT']);

View File

@@ -1,4 +1,6 @@
export function transformArguments(channel: string, message: string): Array<string> { import { RedisCommandArguments } from '.';
export function transformArguments(channel: string | Buffer, message: string | Buffer): RedisCommandArguments {
return ['PUBLISH', channel, message]; return ['PUBLISH', channel, message];
} }