Skip to content

Commit 657167c

Browse files
committed
address review comments
1 parent d96aacd commit 657167c

File tree

11 files changed

+96
-87
lines changed

11 files changed

+96
-87
lines changed

package-lock.json

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

packages/client/lib/client/index.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ export default class RedisClient<
155155

156156
return async function (this: ProxyClient, ...args: Array<unknown>) {
157157
if (command.parseCommand) {
158-
const parser = this._self.#newCommandParser(resp);
158+
const parser = new BasicCommandParser(resp);
159+
159160
command.parseCommand(parser, ...args);
160161

161162
return this.executeCommand(parser, this._commandOptions, transformReply);
@@ -174,7 +175,7 @@ export default class RedisClient<
174175

175176
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
176177
if (command.parseCommand) {
177-
const parser = this._self._self.#newCommandParser(resp);
178+
const parser = new BasicCommandParser(resp);
178179
command.parseCommand(parser, ...args);
179180

180181
return this._self.executeCommand(parser, this._self._commandOptions, transformReply);
@@ -194,7 +195,7 @@ export default class RedisClient<
194195

195196
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
196197
if (fn.parseCommand) {
197-
const parser = this._self._self.#newCommandParser(resp);
198+
const parser = new BasicCommandParser(resp);
198199
parser.pushVariadic(prefix);
199200
fn.parseCommand(parser, ...args);
200201

@@ -218,7 +219,7 @@ export default class RedisClient<
218219

219220
return async function (this: ProxyClient, ...args: Array<unknown>) {
220221
if (script.parseCommand) {
221-
const parser = this._self.#newCommandParser(resp);
222+
const parser = new BasicCommandParser(resp);
222223
parser.pushVariadic(prefix);
223224
script.parseCommand(parser, ...args);
224225

@@ -344,10 +345,6 @@ export default class RedisClient<
344345
this._self.#dirtyWatch = msg;
345346
}
346347

347-
#newCommandParser(resp: RespVersions): CommandParser {
348-
return new BasicCommandParser(resp);
349-
}
350-
351348
constructor(options?: RedisClientOptions<M, F, S, RESP, TYPE_MAPPING>) {
352349
super();
353350
this.#options = this.#initiateOptions(options);

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,8 @@ export default class RedisClientMultiCommand<REPLIES = []> {
176176
redisArgs = parser.redisArgs;
177177
redisArgs.preserve = parser.preserve;
178178
} else {
179-
redisArgs = script.transformArguments(...args);
179+
redisArgs = prefix;
180+
redisArgs.push(...script.transformArguments(...args));
180181
}
181182

182183
return this.addCommand(

packages/client/lib/client/pool.ts

+5-8
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { TimeoutError } from '../errors';
77
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
88
import { CommandOptions } from './commands-queue';
99
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';
10-
import { CommandParser, BasicCommandParser } from './parser';
10+
import { BasicCommandParser } from './parser';
1111

1212
export interface RedisPoolOptions {
1313
/**
@@ -64,7 +64,7 @@ export class RedisClientPool<
6464

6565
return async function (this: ProxyPool, ...args: Array<unknown>) {
6666
if (command.parseCommand) {
67-
const parser = this._self.#newCommandParser(resp);
67+
const parser = new BasicCommandParser(resp);
6868
command.parseCommand(parser, ...args);
6969

7070
return this.execute(client => client.executeCommand(parser, this._commandOptions, transformReply))
@@ -83,7 +83,7 @@ export class RedisClientPool<
8383

8484
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
8585
if (command.parseCommand) {
86-
const parser = this._self._self.#newCommandParser(resp);
86+
const parser = new BasicCommandParser(resp);
8787
command.parseCommand(parser, ...args);
8888

8989
return this._self.execute(client => client.executeCommand(parser, this._self._commandOptions, transformReply))
@@ -103,7 +103,7 @@ export class RedisClientPool<
103103

104104
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
105105
if (fn.parseCommand) {
106-
const parser = this._self.#newCommandParser(resp);
106+
const parser = new BasicCommandParser(resp);
107107
parser.pushVariadic(prefix);
108108
fn.parseCommand(parser, ...args);
109109

@@ -127,7 +127,7 @@ export class RedisClientPool<
127127

128128
return async function (this: ProxyPool, ...args: Array<unknown>) {
129129
if (script.parseCommand) {
130-
const parser = this._self.#newCommandParser(resp);
130+
const parser = new BasicCommandParser(resp);
131131
parser.pushVariadic(prefix);
132132
script.parseCommand(parser, ...args);
133133

@@ -242,9 +242,6 @@ export class RedisClientPool<
242242
return this._self.#isClosing;
243243
}
244244

245-
#newCommandParser(resp: RespVersions): CommandParser {
246-
return new BasicCommandParser(resp);
247-
}
248245

249246
/**
250247
* You are probably looking for {@link RedisClient.createPool `RedisClient.createPool`},

packages/client/lib/cluster/index.ts

+17-32
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ import { PubSubListener } from '../client/pub-sub';
1010
import { ErrorReply } from '../errors';
1111
import { RedisTcpSocketOptions } from '../client/socket';
1212
import ASKING from '../commands/ASKING';
13-
import { BasicCommandParser, CommandParser } from '../client/parser';
13+
import { BasicCommandParser } from '../client/parser';
14+
import { parseArgs } from '../commands/generic-transformers';
1415
;
1516

1617
interface ClusterCommander<
@@ -175,7 +176,7 @@ export default class RedisCluster<
175176

176177
return async function (this: ProxyCluster, ...args: Array<unknown>) {
177178
if (command.parseCommand) {
178-
const parser = this._self.#newCommandParser(resp);
179+
const parser = new BasicCommandParser(resp);
179180
command.parseCommand(parser, ...args);
180181

181182
return this._self.#execute(
@@ -212,7 +213,7 @@ export default class RedisCluster<
212213

213214
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
214215
if (command.parseCommand) {
215-
const parser = this._self._self.#newCommandParser(resp);
216+
const parser = new BasicCommandParser(resp);
216217
command.parseCommand(parser, ...args);
217218

218219
return this._self.#execute(
@@ -249,7 +250,7 @@ export default class RedisCluster<
249250

250251
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
251252
if (fn.parseCommand) {
252-
const parser = this._self._self.#newCommandParser(resp);
253+
const parser = new BasicCommandParser(resp);
253254
parser.pushVariadic(prefix);
254255
fn.parseCommand(parser, ...args);
255256

@@ -288,7 +289,7 @@ export default class RedisCluster<
288289

289290
return async function (this: ProxyCluster, ...args: Array<unknown>) {
290291
if (script.parseCommand) {
291-
const parser = this._self.#newCommandParser(resp);
292+
const parser = new BasicCommandParser(resp);
292293
parser.pushVariadic(prefix);
293294
script.parseCommand(parser, ...args);
294295

@@ -409,10 +410,6 @@ export default class RedisCluster<
409410
return this._self.#slots.isOpen;
410411
}
411412

412-
#newCommandParser(resp: RespVersions): CommandParser {
413-
return new BasicCommandParser(resp);
414-
}
415-
416413
constructor(options: RedisClusterOptions<M, F, S, RESP, TYPE_MAPPING/*, POLICIES*/>) {
417414
super();
418415

@@ -495,25 +492,6 @@ export default class RedisCluster<
495492
// return this._commandOptionsProxy('policies', policies);
496493
// }
497494

498-
#handleAsk<T>(
499-
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
500-
) {
501-
return async (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, options?: ClusterCommandOptions) => {
502-
const chainId = Symbol("asking chain");
503-
const opts = options ? {...options} : {};
504-
opts.chainId = chainId;
505-
506-
const ret = await Promise.all(
507-
[
508-
client.sendCommand(ASKING.transformArguments(), {chainId: chainId}),
509-
fn(client, opts)
510-
]
511-
);
512-
513-
return ret[1];
514-
};
515-
}
516-
517495
async #execute<T>(
518496
firstKey: RedisArgument | undefined,
519497
isReadonly: boolean | undefined,
@@ -523,13 +501,14 @@ export default class RedisCluster<
523501
const maxCommandRedirections = this.#options.maxCommandRedirections ?? 16;
524502
let client = await this.#slots.getClient(firstKey, isReadonly);
525503
let i = 0;
526-
let myFn = fn;
504+
let myOpts = options;
527505

528506
while (true) {
529507
try {
530-
return await myFn(client, options);
508+
return await fn(client, myOpts);
531509
} catch (err) {
532-
myFn = fn;
510+
// reset to passed in options, if changed by an ask request
511+
myOpts = options;
533512
// TODO: error class
534513
if (++i > maxCommandRedirections || !(err instanceof Error)) {
535514
throw err;
@@ -547,8 +526,14 @@ export default class RedisCluster<
547526
throw new Error(`Cannot find node ${address}`);
548527
}
549528

550-
myFn = this.#handleAsk(fn);
551529
client = redirectTo;
530+
531+
const chainId = Symbol('Asking Chain');
532+
const myOpts = options ? {...options} : {};
533+
myOpts.chainId = chainId;
534+
535+
client.sendCommand(parseArgs(ASKING), {chainId: chainId}).catch(err => { console.log(`Asking Failed: ${err}`) } );
536+
552537
continue;
553538
}
554539

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { strict as assert } from 'node:assert';
2-
import testUtils, { GLOBAL, parseArgs } from '../test-utils';
2+
import testUtils, { GLOBAL } from '../test-utils';
3+
import { parseArgs } from './generic-transformers';
34
import GET from './GET';
45

56
describe('GET', () => {

packages/client/lib/commands/generic-transformers.ts

+29-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { UnwrapReply, ArrayReply, BlobStringReply, BooleanReply, CommandArguments, DoubleReply, NullReply, NumberReply, RedisArgument, TuplesReply } from '../RESP/types';
1+
import { UnwrapReply, ArrayReply, BlobStringReply, BooleanReply, CommandArguments, DoubleReply, NullReply, NumberReply, RedisArgument, TuplesReply, Command } from '../RESP/types';
2+
import { BasicCommandParser } from '../client/parser';
23

34
export function isNullReply(reply: unknown): reply is NullReply {
45
return reply === null;
@@ -446,3 +447,30 @@ function isPlainKey(key: RedisArgument | ZKeyAndWeight): key is RedisArgument {
446447
function isPlainKeys(keys: Array<RedisArgument> | Array<ZKeyAndWeight>): keys is Array<RedisArgument> {
447448
return isPlainKey(keys[0]);
448449
}
450+
451+
/**
452+
* @deprecated
453+
*/
454+
export function parseArgs(command: Command, ...args: Array<any>) {
455+
if (command.parseCommand) {
456+
const parser = new BasicCommandParser();
457+
command.parseCommand!(parser, ...args);
458+
459+
const redisArgs: CommandArguments = parser.redisArgs;
460+
if (parser.preserve) {
461+
redisArgs.preserve = parser.preserve;
462+
}
463+
return redisArgs;
464+
} else {
465+
return command.transformArguments(...args);
466+
}
467+
}
468+
469+
/**
470+
* @deprecated
471+
*/
472+
export function parseArgsWith(command: Command, ...args: Array<any>) {
473+
const parser = new BasicCommandParser();
474+
command.parseCommand!(parser, ...args);
475+
return parser.preserve;
476+
}

0 commit comments

Comments
 (0)