Skip to content

Commit 14b6ad7

Browse files
committed
missed handling for multi in cluster
1 parent 605d5c1 commit 14b6ad7

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

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

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import RedisMultiCommand, { MULTI_REPLY, MultiReply, MultiReplyType, RedisMultiQ
33
import { ReplyWithTypeMapping, CommandReply, Command, CommandArguments, CommanderConfig, RedisFunctions, RedisModules, RedisScripts, RespVersions, TransformReply, RedisScript, RedisFunction, TypeMapping, RedisArgument } from '../RESP/types';
44
import { attachConfig, functionArgumentsPrefix, getTransformReply } from '../commander';
55
import RedisCluster from '.';
6+
import { BasicClusterCommandParser } from '../client/parser';
67

78
type CommandSignature<
89
REPLIES extends Array<unknown>,
@@ -93,13 +94,24 @@ export type ClusterMultiExecute = (
9394
export default class RedisClusterMultiCommand<REPLIES = []> {
9495
static #createCommand(command: Command, resp: RespVersions) {
9596
const transformReply = getTransformReply(command, resp);
97+
9698
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
97-
const redisArgs = command.transformArguments(...args),
99+
let redisArgs: CommandArguments = [];
100+
let firstKey: RedisArgument | undefined;
101+
102+
if (command.parseCommand) {
103+
const parser = new BasicClusterCommandParser(resp);
104+
command.parseCommand(parser, ...args);
105+
firstKey = parser.firstKey;
106+
} else {
107+
redisArgs = command.transformArguments(...args);
98108
firstKey = RedisCluster.extractFirstKey(
99109
command,
100110
args,
101111
redisArgs
102112
);
113+
}
114+
103115
return this.addCommand(
104116
firstKey,
105117
command.IS_READ_ONLY,
@@ -111,13 +123,23 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
111123

112124
static #createModuleCommand(command: Command, resp: RespVersions) {
113125
const transformReply = getTransformReply(command, resp);
126+
114127
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
115-
const redisArgs = command.transformArguments(...args),
128+
let redisArgs: CommandArguments = [];
129+
let firstKey: RedisArgument | undefined;
130+
131+
if (command.parseCommand) {
132+
const parser = new BasicClusterCommandParser(resp);
133+
command.parseCommand(parser, ...args);
134+
firstKey = parser.firstKey;
135+
} else {
136+
redisArgs = command.transformArguments(...args);
116137
firstKey = RedisCluster.extractFirstKey(
117138
command,
118139
args,
119140
redisArgs
120141
);
142+
}
121143
return this._self.addCommand(
122144
firstKey,
123145
command.IS_READ_ONLY,
@@ -128,17 +150,29 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
128150
}
129151

130152
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
131-
const prefix = functionArgumentsPrefix(name, fn),
132-
transformReply = getTransformReply(fn, resp);
153+
const prefix = functionArgumentsPrefix(name, fn);
154+
const transformReply = getTransformReply(fn, resp);
155+
133156
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
134-
const fnArgs = fn.transformArguments(...args),
135-
redisArgs: CommandArguments = prefix.concat(fnArgs),
157+
let fnArgs: CommandArguments = [];
158+
let firstKey: RedisArgument | undefined;
159+
160+
if (fn.parseCommand) {
161+
const parser = new BasicClusterCommandParser(resp);
162+
fn.parseCommand(parser, ...args);
163+
firstKey = parser.firstKey;
164+
} else {
165+
fnArgs = fn.transformArguments(...args);
136166
firstKey = RedisCluster.extractFirstKey(
137167
fn,
138168
args,
139169
fnArgs
140170
);
171+
}
172+
173+
const redisArgs: CommandArguments = prefix.concat(fnArgs);
141174
redisArgs.preserve = fnArgs.preserve;
175+
142176
return this._self.addCommand(
143177
firstKey,
144178
fn.IS_READ_ONLY,
@@ -150,21 +184,34 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
150184

151185
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
152186
const transformReply = getTransformReply(script, resp);
187+
153188
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
154-
const scriptArgs = script.transformArguments(...args);
155-
this.#setState(
156-
RedisCluster.extractFirstKey(
189+
let scriptArgs: CommandArguments = [];
190+
let firstKey: RedisArgument | undefined;
191+
192+
if (script.parseCommand) {
193+
const parser = new BasicClusterCommandParser(resp);
194+
script.parseCommand(parser, ...args);
195+
firstKey = parser.firstKey;
196+
} else {
197+
scriptArgs = script.transformArguments(...args);
198+
firstKey = RedisCluster.extractFirstKey(
157199
script,
158200
args,
159201
scriptArgs
160-
),
202+
)
203+
}
204+
205+
this.#setState(
206+
firstKey,
161207
script.IS_READ_ONLY
162208
);
163209
this.#multi.addScript(
164210
script,
165211
scriptArgs,
166212
transformReply
167213
);
214+
168215
return this;
169216
};
170217
}

0 commit comments

Comments
 (0)