Skip to content

Commit b375570

Browse files
committed
fixes for scripting, tests
1 parent c9f08e0 commit b375570

18 files changed

+61
-116
lines changed

packages/client/lib/client/index.ts

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -154,23 +154,21 @@ export default class RedisClient<
154154
const transformReply = getTransformReply(command, resp);
155155

156156
return async function (this: ProxyClient, ...args: Array<unknown>) {
157-
const typeMapping = this._self._commandOptions?.typeMapping;
158157
const parser = new BasicCommandParser(resp);
159158
command.parseCommand(parser, ...args);
160159

161-
return this._self.executeCommand(parser, this._self._commandOptions, transformReply, typeMapping);
160+
return this._self.executeCommand(parser, this._commandOptions, transformReply);
162161
}
163162
}
164163

165164
static #createModuleCommand(command: Command, resp: RespVersions) {
166165
const transformReply = getTransformReply(command, resp);
167166

168167
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
169-
const typeMapping = this._self._commandOptions?.typeMapping;
170168
const parser = new BasicCommandParser(resp);
171169
command.parseCommand(parser, ...args);
172170

173-
return this._self.executeCommand(parser, this._self._commandOptions, transformReply, typeMapping);
171+
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
174172
};
175173
}
176174

@@ -179,12 +177,11 @@ export default class RedisClient<
179177
const transformReply = getTransformReply(fn, resp);
180178

181179
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
182-
const typeMapping = this._self._commandOptions?.typeMapping;
183180
const parser = new BasicCommandParser(resp);
184181
parser.pushVariadic(prefix);
185182
fn.parseCommand(parser, ...args);
186183

187-
return this._self.executeCommand(parser, this._self._commandOptions, transformReply, typeMapping);
184+
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
188185
};
189186
}
190187

@@ -193,12 +190,11 @@ export default class RedisClient<
193190
const transformReply = getTransformReply(script, resp);
194191

195192
return async function (this: ProxyClient, ...args: Array<unknown>) {
196-
const typeMapping = this._commandOptions?.typeMapping;
197193
const parser = new BasicCommandParser(resp);
198194
parser.pushVariadic(prefix);
199195
script.parseCommand(parser, ...args);
200196

201-
return this.executeCommand(parser, this._commandOptions, transformReply, typeMapping);
197+
return this.executeScript(script, parser, this._commandOptions, transformReply);
202198
}
203199
}
204200

@@ -580,17 +576,40 @@ export default class RedisClient<
580576
parser: CommandParser,
581577
commandOptions: CommandOptions<TYPE_MAPPING> | undefined,
582578
transformReply: TransformReply | undefined,
583-
typeMapping: TypeMapping | undefined
584579
) {
585580
const reply = await this.sendCommand(parser.redisArgs, commandOptions);
586581

587582
if (transformReply) {
588-
return transformReply(reply, parser.preserve, typeMapping);
583+
return transformReply(reply, parser.preserve, commandOptions?.typeMapping);
589584
}
590585

591586
return reply;
592587
}
593-
588+
589+
async executeScript(
590+
script: RedisScript,
591+
parser: CommandParser,
592+
options: CommandOptions | undefined,
593+
transformReply: TransformReply | undefined,
594+
) {
595+
const args = parser.redisArgs as Array<RedisArgument>;
596+
597+
let reply: ReplyUnion;
598+
try {
599+
reply = await this.sendCommand(args, options);
600+
} catch (err) {
601+
if (!(err as Error)?.message?.startsWith?.('NOSCRIPT')) throw err;
602+
603+
args[0] = 'EVAL';
604+
args[1] = script.SCRIPT;
605+
reply = await this.sendCommand(args, options);
606+
}
607+
608+
return transformReply ?
609+
transformReply(reply, parser.preserve, options?.typeMapping) :
610+
reply;
611+
}
612+
594613
sendCommand<T = ReplyUnion>(
595614
args: ReadonlyArray<RedisArgument>,
596615
options?: CommandOptions
@@ -606,22 +625,6 @@ export default class RedisClient<
606625
return promise;
607626
}
608627

609-
async executeScript(
610-
script: RedisScript,
611-
args: Array<RedisArgument>,
612-
options?: CommandOptions
613-
) {
614-
try {
615-
return await this.sendCommand(args, options);
616-
} catch (err) {
617-
if (!(err as Error)?.message?.startsWith?.('NOSCRIPT')) throw err;
618-
619-
args[0] = 'EVAL';
620-
args[1] = script.SCRIPT;
621-
return await this.sendCommand(args, options);
622-
}
623-
}
624-
625628
async SELECT(db: number): Promise<void> {
626629
await this.sendCommand(['SELECT', db.toString()]);
627630
this._self.#selectedDB = db;

packages/client/lib/client/pool.ts

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,21 @@ export class RedisClientPool<
6767
const transformReply = getTransformReply(command, resp);
6868

6969
return async function (this: ProxyPool, ...args: Array<unknown>) {
70-
const typeMapping = this._commandOptions?.typeMapping;
7170
const parser = new BasicCommandParser(resp);
7271
command.parseCommand(parser, ...args);
7372

74-
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply, typeMapping))
73+
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
7574
};
7675
}
7776

7877
static #createModuleCommand(command: Command, resp: RespVersions) {
7978
const transformReply = getTransformReply(command, resp);
8079

8180
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
82-
const typeMapping = this._self._commandOptions?.typeMapping;
8381
const parser = new BasicCommandParser(resp);
8482
command.parseCommand(parser, ...args);
8583

86-
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply, typeMapping))
84+
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
8785
};
8886
}
8987

@@ -92,25 +90,23 @@ export class RedisClientPool<
9290
const transformReply = getTransformReply(fn, resp);
9391

9492
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
95-
const typeMapping = this._self._commandOptions?.typeMapping;
9693
const parser = new BasicCommandParser(resp);
9794
parser.pushVariadic(prefix);
9895
fn.parseCommand(parser, ...args);
9996

100-
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply, typeMapping)) };
97+
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply)) };
10198
}
10299

103100
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
104101
const prefix = scriptArgumentsPrefix(script);
105102
const transformReply = getTransformReply(script, resp);
106103

107104
return async function (this: ProxyPool, ...args: Array<unknown>) {
108-
const typeMapping = this._commandOptions?.typeMapping;
109105
const parser = new BasicCommandParser(resp);
110106
parser.pushVariadic(prefix);
111107
script.parseCommand(parser, ...args);
112108

113-
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply, typeMapping))
109+
return this.execute(client => client.executeScript(script, parser, this._commandOptions, transformReply))
114110
};
115111
}
116112

@@ -417,14 +413,6 @@ export class RedisClientPool<
417413
return this.execute(client => client.sendCommand(args, options));
418414
}
419415

420-
executeScript(
421-
script: RedisScript,
422-
args: Array<RedisArgument>,
423-
options?: CommandOptions
424-
) {
425-
return this.execute(client => client.executeScript(script, args, options));
426-
}
427-
428416
MULTI() {
429417
type Multi = new (...args: ConstructorParameters<typeof RedisClientMultiCommand>) => RedisClientMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
430418
return new ((this as any).Multi as Multi)(

packages/client/lib/cluster/index.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -153,15 +153,14 @@ export default class RedisCluster<
153153
static #createCommand(command: Command, resp: RespVersions) {
154154
const transformReply = getTransformReply(command, resp);
155155
return async function (this: ProxyCluster, ...args: Array<unknown>) {
156-
const typeMapping = this._commandOptions?.typeMapping;
157156
const parser = new BasicCommandParser(resp);
158157
command.parseCommand(parser, ...args);
159158

160159
return this._self.#execute(
161160
parser.firstKey,
162161
command.IS_READ_ONLY,
163162
this._commandOptions,
164-
(client, opts) => client.executeCommand(parser, opts, transformReply, typeMapping)
163+
(client, opts) => client.executeCommand(parser, opts, transformReply)
165164
);
166165
};
167166
}
@@ -170,15 +169,14 @@ export default class RedisCluster<
170169
const transformReply = getTransformReply(command, resp);
171170

172171
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
173-
const typeMapping = this._self._commandOptions?.typeMapping;
174172
const parser = new BasicCommandParser(resp);
175173
command.parseCommand(parser, ...args);
176174

177175
return this._self.#execute(
178176
parser.firstKey,
179177
command.IS_READ_ONLY,
180178
this._self._commandOptions,
181-
(client, opts) => client.executeCommand(parser, opts, transformReply, typeMapping)
179+
(client, opts) => client.executeCommand(parser, opts, transformReply)
182180
);
183181
};
184182
}
@@ -188,7 +186,6 @@ export default class RedisCluster<
188186
const transformReply = getTransformReply(fn, resp);
189187

190188
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
191-
const typeMapping = this._self._commandOptions?.typeMapping;
192189
const parser = new BasicCommandParser(resp);
193190
parser.pushVariadic(prefix);
194191
fn.parseCommand(parser, ...args);
@@ -197,7 +194,7 @@ export default class RedisCluster<
197194
parser.firstKey,
198195
fn.IS_READ_ONLY,
199196
this._self._commandOptions,
200-
(client, opts) => client.executeCommand(parser, opts, transformReply, typeMapping)
197+
(client, opts) => client.executeCommand(parser, opts, transformReply)
201198
);
202199
};
203200
}
@@ -207,7 +204,6 @@ export default class RedisCluster<
207204
const transformReply = getTransformReply(script, resp);
208205

209206
return async function (this: ProxyCluster, ...args: Array<unknown>) {
210-
const typeMapping = this._commandOptions?.typeMapping;
211207
const parser = new BasicCommandParser(resp);
212208
parser.pushVariadic(prefix);
213209
script.parseCommand(parser, ...args);
@@ -216,7 +212,7 @@ export default class RedisCluster<
216212
parser.firstKey,
217213
script.IS_READ_ONLY,
218214
this._commandOptions,
219-
(client, opts) => client.executeCommand(parser, opts, transformReply, typeMapping)
215+
(client, opts) => client.executeScript(script, parser, opts, transformReply)
220216
);
221217
};
222218
}
@@ -461,21 +457,6 @@ export default class RedisCluster<
461457
);
462458
}
463459

464-
executeScript(
465-
script: RedisScript,
466-
firstKey: RedisArgument | undefined,
467-
isReadonly: boolean | undefined,
468-
args: Array<RedisArgument>,
469-
options?: CommandOptions
470-
) {
471-
return this._self.#execute(
472-
firstKey,
473-
isReadonly,
474-
options,
475-
(client, opts) => client.executeScript(script, args, options)
476-
);
477-
}
478-
479460
MULTI(routing?: RedisArgument) {
480461
type Multi = new (...args: ConstructorParameters<typeof RedisClusterMultiCommand>) => RedisClusterMultiCommandType<[], M, F, S, RESP, TYPE_MAPPING>;
481462
return new ((this as any).Multi as Multi)(

packages/client/lib/commands/CLIENT_KILL.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('CLIENT KILL', () => {
6868

6969
it('MAXAGE', () => {
7070
assert.deepEqual(
71-
CLIENT_KILL.transformArguments({
71+
parseArgs(CLIENT_KILL, {
7272
filter: CLIENT_KILL_FILTERS.MAXAGE,
7373
maxAge: 10
7474
}),

packages/client/lib/commands/HEXPIRE.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default {
2020

2121
parser.push('FIELDS');
2222

23-
parser.pushVariadic(fields);
23+
parser.pushVariadicWithLength(fields);
2424
},
2525
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
2626
} as const satisfies Command;

packages/client/lib/commands/HEXPIREAT.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default {
2020

2121
parser.push('FIELDS')
2222

23-
parser.pushVariadic(fields);
23+
parser.pushVariadicWithLength(fields);
2424
},
2525
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
2626
} as const satisfies Command;

packages/client/lib/commands/HEXPIRETIME.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export default {
1919
parser.push('HEXPIRETIME');
2020
parser.pushKey(key);
2121
parser.push('FIELDS');
22-
parser.pushVariadic(fields);
22+
parser.pushVariadicWithLength(fields);
2323
},
2424
transformReply: undefined as unknown as () => ArrayReply<NumberReply>
2525
} as const satisfies Command;

packages/client/lib/commands/HPERSIST.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
parser.push('HPERSIST');
1212
parser.pushKey(key);
1313
parser.push('FIELDS');
14-
parser.pushVariadic(fields);
14+
parser.pushVariadicWithLength(fields);
1515
},
1616
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | NullReply
1717
} as const satisfies Command;

packages/client/lib/commands/HPEXPIRE.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export default {
2020

2121
parser.push('FIELDS')
2222

23-
parser.pushVariadic(fields);
23+
parser.pushVariadicWithLength(fields);
2424
},
2525
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | NullReply
2626
} as const satisfies Command;

packages/client/lib/commands/HPEXPIREAT.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export default {
2121

2222
parser.push('FIELDS')
2323

24-
parser.pushVariadic(fields);
24+
parser.pushVariadicWithLength(fields);
2525
},
2626
transformReply: undefined as unknown as () => ArrayReply<NumberReply> | NullReply
2727
} as const satisfies Command;

0 commit comments

Comments
 (0)