Skip to content

Commit a1bed9a

Browse files
committed
add buffer support to a bunch of commands
1 parent 72072f6 commit a1bed9a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+818
-378
lines changed

package-lock.json

+121-161
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as LinkedList from 'yallist';
22
import { AbortError } from '../errors';
3-
import { RedisCommandArguments, RedisCommandRawReply } from '../commands';
3+
import { RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply } from '../commands';
44

55
// We need to use 'require', because it's not possible with Typescript to import
66
// classes that are exported as 'module.exports = class`, without esModuleInterop
@@ -40,8 +40,6 @@ export enum PubSubUnsubscribeCommands {
4040
PUNSUBSCRIBE = 'PUNSUBSCRIBE'
4141
}
4242

43-
type PubSubArgumentTypes = Buffer | string;
44-
4543
export type PubSubListener<
4644
BUFFER_MODE extends boolean = false,
4745
T = BUFFER_MODE extends true ? Buffer : string
@@ -197,12 +195,12 @@ export default class RedisCommandsQueue {
197195

198196
subscribe<T extends boolean>(
199197
command: PubSubSubscribeCommands,
200-
channels: PubSubArgumentTypes | Array<PubSubArgumentTypes>,
198+
channels: RedisCommandArgument | Array<RedisCommandArgument>,
201199
listener: PubSubListener<T>,
202200
bufferMode?: T
203201
): Promise<void> {
204202
const pubSubState = this.#initiatePubSubState(),
205-
channelsToSubscribe: Array<PubSubArgumentTypes> = [],
203+
channelsToSubscribe: Array<RedisCommandArgument> = [],
206204
listenersMap = command === PubSubSubscribeCommands.SUBSCRIBE ? pubSubState.listeners.channels : pubSubState.listeners.patterns;
207205
for (const channel of (Array.isArray(channels) ? channels : [channels])) {
208206
const channelString = typeof channel === 'string' ? channel : channel.toString();
@@ -271,12 +269,12 @@ export default class RedisCommandsQueue {
271269
return this.#pushPubSubCommand(command, channelsToUnsubscribe);
272270
}
273271

274-
#pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels: number | Array<PubSubArgumentTypes>): Promise<void> {
272+
#pushPubSubCommand(command: PubSubSubscribeCommands | PubSubUnsubscribeCommands, channels: number | Array<RedisCommandArgument>): Promise<void> {
275273
return new Promise((resolve, reject) => {
276274
const pubSubState = this.#initiatePubSubState(),
277275
isSubscribe = command === PubSubSubscribeCommands.SUBSCRIBE || command === PubSubSubscribeCommands.PSUBSCRIBE,
278276
inProgressKey = isSubscribe ? 'subscribing' : 'unsubscribing',
279-
commandArgs: Array<PubSubArgumentTypes> = [command];
277+
commandArgs: Array<RedisCommandArgument> = [command];
280278

281279
let channelsCounter: number;
282280
if (typeof channels === 'number') { // unsubscribe only

packages/client/lib/client/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -575,7 +575,7 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
575575
} while (cursor !== 0);
576576
}
577577

578-
async* zScanIterator(key: string, options?: ScanOptions): AsyncIterable<ZMember> {
578+
async* zScanIterator(key: string, options?: ScanOptions): AsyncIterable<ZMember<string>> {
579579
let cursor = 0;
580580
do {
581581
const reply = await (this as any).zScan(key, cursor, options);

packages/client/lib/cluster/cluster-slots.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import RedisClient, { InstantiableRedisClient, RedisClientType } from '../client';
22
import { RedisClusterMasterNode, RedisClusterReplicaNode } from '../commands/CLUSTER_NODES';
33
import { RedisClusterClientOptions, RedisClusterOptions } from '.';
4-
import { RedisModules, RedisScripts } from '../commands';
4+
import { RedisCommandArgument, RedisModules, RedisScripts } from '../commands';
55

66
// We need to use 'require', because it's not possible with Typescript to import
77
// function that are exported as 'module.exports = function`, without esModuleInterop
@@ -202,7 +202,7 @@ export default class RedisClusterSlots<M extends RedisModules, S extends RedisSc
202202
return value.client;
203203
}
204204

205-
getClient(firstKey?: string | Buffer, isReadonly?: boolean): RedisClientType<M, S> {
205+
getClient(firstKey?: RedisCommandArgument, isReadonly?: boolean): RedisClientType<M, S> {
206206
if (!firstKey) {
207207
return this.#getRandomClient();
208208
}

packages/client/lib/cluster/commands.ts

+46-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import * as BITCOUNT from '../commands/BITCOUNT';
44
import * as BITFIELD from '../commands/BITFIELD';
55
import * as BITOP from '../commands/BITOP';
66
import * as BITPOS from '../commands/BITPOS';
7+
import * as BLMOVE_BUFFER from '../commands/BLMOVE_BUFFER';
78
import * as BLMOVE from '../commands/BLMOVE';
9+
import * as BLPOP_BUFFER from '../commands/BLPOP_BUFFER';
810
import * as BLPOP from '../commands/BLPOP';
11+
import * as BRPOP_BUFFER from '../commands/BRPOP_BUFFER';
912
import * as BRPOP from '../commands/BRPOP';
13+
import * as BRPOPLPUSH_BUFFER from '../commands/BRPOPLPUSH_BUFFER';
1014
import * as BRPOPLPUSH from '../commands/BRPOPLPUSH';
1115
import * as BZPOPMAX from '../commands/BZPOPMAX';
1216
import * as BZPOPMIN from '../commands/BZPOPMIN';
@@ -36,13 +40,16 @@ import * as GETRANGE from '../commands/GETRANGE';
3640
import * as GETSET from '../commands/GETSET';
3741
import * as HDEL from '../commands/HDEL';
3842
import * as HEXISTS from '../commands/HEXISTS';
43+
import * as HGET_BUFFER from '../commands/HGET_BUFFER';
3944
import * as HGET from '../commands/HGET';
45+
import * as HGETALL_BUFFER from '../commands/HGETALL_BUFFER';
4046
import * as HGETALL from '../commands/HGETALL';
4147
import * as HINCRBY from '../commands/HINCRBY';
4248
import * as HINCRBYFLOAT from '../commands/HINCRBYFLOAT';
4349
import * as HKEYS from '../commands/HKEYS';
4450
import * as HLEN from '../commands/HLEN';
4551
import * as HMGET from '../commands/HMGET';
52+
import * as HRANDFIELD_COUNT_WITHVALUES_BUFFER from '../commands/HRANDFIELD_COUNT_WITHVALUES_BUFFER';
4653
import * as HRANDFIELD_COUNT_WITHVALUES from '../commands/HRANDFIELD_COUNT_WITHVALUES';
4754
import * as HRANDFIELD_COUNT from '../commands/HRANDFIELD_COUNT';
4855
import * as HRANDFIELD from '../commands/HRANDFIELD';
@@ -119,10 +126,14 @@ import * as UNLINK from '../commands/UNLINK';
119126
import * as WATCH from '../commands/WATCH';
120127
import * as XACK from '../commands/XACK';
121128
import * as XADD from '../commands/XADD';
129+
import * as XAUTOCLAIM_JUSTID_BUFFER from '../commands/XAUTOCLAIM_JUSTID_BUFFER';
130+
import * as XAUTOCLAIM_BUFFER from '../commands/XAUTOCLAIM_BUFFER';
122131
import * as XAUTOCLAIM_JUSTID from '../commands/XAUTOCLAIM_JUSTID';
123132
import * as XAUTOCLAIM from '../commands/XAUTOCLAIM';
124-
import * as XCLAIM from '../commands/XCLAIM';
133+
import * as XCLAIM_JUSTID_BUFFER from '../commands/XCLAIM_JUSTID_BUFFER';
134+
import * as XCLAIM_BUFFER from '../commands/XCLAIM_BUFFER';
125135
import * as XCLAIM_JUSTID from '../commands/XCLAIM_JUSTID';
136+
import * as XCLAIM from '../commands/XCLAIM';
126137
import * as XDEL from '../commands/XDEL';
127138
import * as XGROUP_CREATE from '../commands/XGROUP_CREATE';
128139
import * as XGROUP_CREATECONSUMER from '../commands/XGROUP_CREATECONSUMER';
@@ -135,9 +146,13 @@ import * as XINFO_STREAM from '../commands/XINFO_STREAM';
135146
import * as XLEN from '../commands/XLEN';
136147
import * as XPENDING_RANGE from '../commands/XPENDING_RANGE';
137148
import * as XPENDING from '../commands/XPENDING';
149+
import * as XRANGE_BUFFER from '../commands/XRANGE_BUFFER';
138150
import * as XRANGE from '../commands/XRANGE';
151+
import * as XREAD_BUFFER from '../commands/XREAD_BUFFER';
139152
import * as XREAD from '../commands/XREAD';
153+
import * as XREADGROUP_BUFFER from '../commands/XREADGROUP_BUFFER';
140154
import * as XREADGROUP from '../commands/XREADGROUP';
155+
import * as XREVRANGE_BUFFER from '../commands/XREVRANGE_BUFFER';
141156
import * as XREVRANGE from '../commands/XREVRANGE';
142157
import * as XTRIM from '../commands/XTRIM';
143158
import * as ZADD from '../commands/ZADD';
@@ -188,12 +203,20 @@ export default {
188203
bitOp: BITOP,
189204
BITPOS,
190205
bitPos: BITPOS,
206+
BLMOVE_BUFFER,
207+
blMoveBuffer: BLMOVE_BUFFER,
191208
BLMOVE,
192209
blMove: BLMOVE,
210+
BLPOP_BUFFER,
211+
blPopBuffer: BLPOP_BUFFER,
193212
BLPOP,
194213
blPop: BLPOP,
214+
BRPOP_BUFFER,
215+
brPopBuffer: BRPOP_BUFFER,
195216
BRPOP,
196217
brPop: BRPOP,
218+
BRPOPLPUSH_BUFFER,
219+
brPopLPushBuffer: BRPOPLPUSH_BUFFER,
197220
BRPOPLPUSH,
198221
brPopLPush: BRPOPLPUSH,
199222
BZPOPMAX,
@@ -252,8 +275,12 @@ export default {
252275
hDel: HDEL,
253276
HEXISTS,
254277
hExists: HEXISTS,
278+
HGET_BUFFER,
279+
hGetBuffer: HGET_BUFFER,
255280
HGET,
256281
hGet: HGET,
282+
HGETALL_BUFFER,
283+
hGetAllBuffer: HGETALL_BUFFER,
257284
HGETALL,
258285
hGetAll: HGETALL,
259286
HINCRBY,
@@ -266,6 +293,8 @@ export default {
266293
hLen: HLEN,
267294
HMGET,
268295
hmGet: HMGET,
296+
HRANDFIELD_COUNT_WITHVALUES_BUFFER,
297+
hRandFieldCountWithValuesBuffer: HRANDFIELD_COUNT_WITHVALUES_BUFFER,
269298
HRANDFIELD_COUNT_WITHVALUES,
270299
hRandFieldCountWithValues: HRANDFIELD_COUNT_WITHVALUES,
271300
HRANDFIELD_COUNT,
@@ -418,12 +447,20 @@ export default {
418447
xAck: XACK,
419448
XADD,
420449
xAdd: XADD,
450+
XAUTOCLAIM_JUSTID_BUFFER,
451+
xAutoClaimJustIdBuffer: XAUTOCLAIM_JUSTID_BUFFER,
452+
XAUTOCLAIM_BUFFER,
453+
xAutoClaimBuffer: XAUTOCLAIM_BUFFER,
421454
XAUTOCLAIM_JUSTID,
422455
xAutoClaimJustId: XAUTOCLAIM_JUSTID,
423456
XAUTOCLAIM,
424457
xAutoClaim: XAUTOCLAIM,
425458
XCLAIM,
426459
xClaim: XCLAIM,
460+
XCLAIM_JUSTID_BUFFER,
461+
xClaimJustIdBuffer: XCLAIM_JUSTID_BUFFER,
462+
XCLAIM_BUFFER,
463+
xClaimBuffer: XCLAIM_BUFFER,
427464
XCLAIM_JUSTID,
428465
xClaimJustId: XCLAIM_JUSTID,
429466
XDEL,
@@ -450,12 +487,20 @@ export default {
450487
xPendingRange: XPENDING_RANGE,
451488
XPENDING,
452489
xPending: XPENDING,
490+
XRANGE_BUFFER,
491+
xRangeBuffer: XRANGE_BUFFER,
453492
XRANGE,
454493
xRange: XRANGE,
494+
XREAD_BUFFER,
495+
xReadBuffer: XREAD_BUFFER,
455496
XREAD,
456497
xRead: XREAD,
498+
XREADGROUP_BUFFER,
499+
xReadGroupBuffer: XREADGROUP_BUFFER,
457500
XREADGROUP,
458501
xReadGroup: XREADGROUP,
502+
XREVRANGE_BUFFER,
503+
xRevRangeBuffer: XREVRANGE_BUFFER,
459504
XREVRANGE,
460505
xRevRange: XREVRANGE,
461506
XTRIM,

packages/client/lib/cluster/index.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import COMMANDS from './commands';
2-
import { RedisCommand, RedisCommandArguments, RedisCommandReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
2+
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
33
import { ClientCommandOptions, RedisClientCommandSignature, RedisClientOptions, RedisClientType, WithModules, WithScripts } from '../client';
44
import RedisClusterSlots, { ClusterNode } from './cluster-slots';
55
import { extendWithModulesAndScripts, transformCommandArguments, transformCommandReply, extendWithCommands } from '../commander';
@@ -24,7 +24,7 @@ export type RedisClusterType<M extends RedisModules = Record<string, never>, S e
2424
RedisCluster<M, S> & WithCommands & WithModules<M> & WithScripts<S>;
2525

2626
export default class RedisCluster<M extends RedisModules = Record<string, never>, S extends RedisScripts = Record<string, never>> extends EventEmitter {
27-
static extractFirstKey(command: RedisCommand, originalArgs: Array<unknown>, redisArgs: RedisCommandArguments): string | Buffer | undefined {
27+
static extractFirstKey(command: RedisCommand, originalArgs: Array<unknown>, redisArgs: RedisCommandArguments): RedisCommandArgument | undefined {
2828
if (command.FIRST_KEY_INDEX === undefined) {
2929
return undefined;
3030
} else if (typeof command.FIRST_KEY_INDEX === 'number') {
@@ -84,7 +84,7 @@ export default class RedisCluster<M extends RedisModules = Record<string, never>
8484
}
8585

8686
async sendCommand<C extends RedisCommand>(
87-
firstKey: string | Buffer | undefined,
87+
firstKey: RedisCommandArgument | undefined,
8888
isReadonly: boolean | undefined,
8989
args: RedisCommandArguments,
9090
options?: ClientCommandOptions,
@@ -175,9 +175,9 @@ export default class RedisCluster<M extends RedisModules = Record<string, never>
175175
throw err;
176176
}
177177

178-
multi(routing?: string | Buffer): RedisClusterMultiCommandType<M, S> {
178+
multi(routing?: RedisCommandArgument): RedisClusterMultiCommandType<M, S> {
179179
return new this.#Multi(
180-
async (commands: Array<RedisMultiQueuedCommand>, firstKey?: string | Buffer, chainId?: symbol) => {
180+
async (commands: Array<RedisMultiQueuedCommand>, firstKey?: RedisCommandArgument, chainId?: symbol) => {
181181
return this.#slots
182182
.getClient(firstKey)
183183
.multiExecutor(commands, chainId);

packages/client/lib/cluster/multi-command.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import COMMANDS from './commands';
2-
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
2+
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisModules, RedisPlugins, RedisScript, RedisScripts } from '../commands';
33
import RedisMultiCommand, { RedisMultiQueuedCommand } from '../multi-command';
44
import { extendWithCommands, extendWithModulesAndScripts } from '../commander';
55
import RedisCluster from '.';
@@ -24,12 +24,12 @@ type WithScripts<M extends RedisModules, S extends RedisScripts> = {
2424
export type RedisClusterMultiCommandType<M extends RedisModules = Record<string, never>, S extends RedisScripts = Record<string, never>> =
2525
RedisClusterMultiCommand & WithCommands<M, S> & WithModules<M, S> & WithScripts<M, S>;
2626

27-
export type RedisClusterMultiExecutor = (queue: Array<RedisMultiQueuedCommand>, firstKey?: string | Buffer, chainId?: symbol) => Promise<Array<RedisCommandRawReply>>;
27+
export type RedisClusterMultiExecutor = (queue: Array<RedisMultiQueuedCommand>, firstKey?: RedisCommandArgument, chainId?: symbol) => Promise<Array<RedisCommandRawReply>>;
2828

2929
export default class RedisClusterMultiCommand {
3030
readonly #multi = new RedisMultiCommand();
3131
readonly #executor: RedisClusterMultiExecutor;
32-
#firstKey: string | Buffer | undefined;
32+
#firstKey: RedisCommandArgument | undefined;
3333

3434
static extend<M extends RedisModules, S extends RedisScripts>(
3535
plugins?: RedisPlugins<M, S>
@@ -43,7 +43,7 @@ export default class RedisClusterMultiCommand {
4343
});
4444
}
4545

46-
constructor(executor: RedisClusterMultiExecutor, firstKey?: string | Buffer) {
46+
constructor(executor: RedisClusterMultiExecutor, firstKey?: RedisCommandArgument) {
4747
this.#executor = executor;
4848
this.#firstKey = firstKey;
4949
}
@@ -62,7 +62,7 @@ export default class RedisClusterMultiCommand {
6262
}
6363

6464
addCommand(
65-
firstKey: string | Buffer | undefined,
65+
firstKey: RedisCommandArgument | undefined,
6666
args: RedisCommandArguments,
6767
transformReply?: RedisCommand['transformReply']
6868
): this {

packages/client/lib/commander.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
import { CommandOptions, isCommandOptions } from './command-options';
3-
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisCommands, RedisModules, RedisScript, RedisScripts } from './commands';
3+
import { RedisCommand, RedisCommandArgument, RedisCommandArguments, RedisCommandRawReply, RedisCommandReply, RedisCommands, RedisModules, RedisScript, RedisScripts } from './commands';
44

55
type Instantiable<T = any> = new(...args: Array<any>) => T;
66

@@ -91,7 +91,7 @@ export function transformCommandArguments<T = unknown>(
9191

9292
const DELIMITER = '\r\n';
9393

94-
export function* encodeCommand(args: RedisCommandArguments): IterableIterator<string | Buffer> {
94+
export function* encodeCommand(args: RedisCommandArguments): IterableIterator<RedisCommandArgument> {
9595
let strings = `*${args.length}${DELIMITER}`,
9696
stringsLength = 0;
9797
for (const arg of args) {
+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
2+
13
export const FIRST_KEY_INDEX = 1;
24

3-
export function transformArguments(key: string, value: string): Array<string> {
5+
export function transformArguments(
6+
key: RedisCommandArgument,
7+
value: RedisCommandArgument
8+
): RedisCommandArguments {
49
return ['APPEND', key, value];
510
}
611

7-
export declare function transformReply(): string;
12+
export declare function transformReply(): number;

packages/client/lib/commands/BLMOVE.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
12
import { LMoveSide } from './LMOVE';
23

34
export const FIRST_KEY_INDEX = 1;
45

56
export function transformArguments(
6-
source: string,
7-
destination: string,
7+
source: RedisCommandArgument,
8+
destination: RedisCommandArgument,
89
sourceDirection: LMoveSide,
910
destinationDirection: LMoveSide,
1011
timeout: number
11-
): Array<string> {
12+
): RedisCommandArguments {
1213
return [
1314
'BLMOVE',
1415
source,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export { FIRST_KEY_INDEX, transformArguments } from './BLMOVE';
2+
3+
export const BUFFER_MODE = true;
4+
5+
export declare function transformReply(): Buffer | null;

packages/client/lib/commands/BLPOP.ts

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
1-
import { RedisCommandArguments } from '.';
1+
import { RedisCommandArgument, RedisCommandArguments } from '.';
22
import { pushVerdictArguments } from './generic-transformers';
33

44
export const FIRST_KEY_INDEX = 1;
55

6-
export function transformArguments(keys: string | Buffer | Array<string | Buffer>, timeout: number): RedisCommandArguments {
6+
export function transformArguments(
7+
keys: RedisCommandArgument | Array<RedisCommandArgument>,
8+
timeout: number
9+
): RedisCommandArguments {
710
const args = pushVerdictArguments(['BLPOP'], keys);
811

912
args.push(timeout.toString());
1013

1114
return args;
1215
}
1316

14-
type BLPOPReply = null | {
17+
type BLPopReply = null | {
1518
key: string;
1619
element: string;
1720
};
1821

19-
export function transformReply(reply: null | [string, string]): BLPOPReply {
22+
export function transformReply(reply: null | [string, string]): BLPopReply {
2023
if (reply === null) return null;
2124

2225
return {

0 commit comments

Comments
 (0)