Skip to content

Commit 56caa8b

Browse files
committed
resolve commands and subcommands
1 parent 2ea355f commit 56caa8b

File tree

3 files changed

+27
-12
lines changed

3 files changed

+27
-12
lines changed

packages/client/lib/client/parser.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import { RedisArgument } from '../RESP/types';
22
import { RedisVariadicArgument } from '../commands/generic-transformers';
33

4+
export type CommandIdentifier = { command: string, subcommand: string };
45
export interface CommandParser {
56
redisArgs: ReadonlyArray<RedisArgument>;
67
keys: ReadonlyArray<RedisArgument>;
78
firstKey: RedisArgument | undefined;
89
preserve: unknown;
10+
commandIdentifier: CommandIdentifier;
911

1012
push: (...arg: Array<RedisArgument>) => unknown;
1113
pushVariadic: (vals: RedisVariadicArgument) => unknown;
@@ -44,12 +46,10 @@ export class BasicCommandParser implements CommandParser {
4446
return tmp.join('_');
4547
}
4648

47-
get commandName(): string | undefined {
48-
let cmdName = this.#redisArgs[0];
49-
if (cmdName instanceof Buffer) {
50-
return cmdName.toString();
51-
}
52-
return cmdName;
49+
get commandIdentifier(): CommandIdentifier {
50+
const command = this.#redisArgs[0] instanceof Buffer ? this.#redisArgs[0].toString() : this.#redisArgs[0];
51+
const subcommand = this.#redisArgs[1] instanceof Buffer ? this.#redisArgs[1].toString() : this.#redisArgs[1];
52+
return { command, subcommand };
5353
}
5454

5555
push(...arg: Array<RedisArgument>) {

packages/client/lib/cluster/request-response-policies/static-policy-resolver.ts

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { PolicyResult, PolicyResolver } from './types';
22
import { POLICIES } from './static-policies-data';
3+
import { CommandIdentifier } from '../../client/parser';
34

45
export class StaticPolicyResolver implements PolicyResolver {
56
private readonly fallbackResolver: PolicyResolver | null = null;
@@ -21,8 +22,8 @@ export class StaticPolicyResolver implements PolicyResolver {
2122
return new StaticPolicyResolver(this.policies, fallbackResolver);
2223
}
2324

24-
resolvePolicy(command: string): PolicyResult {
25-
const parts = command.toLowerCase().split('.');
25+
resolvePolicy(commandIdentifier: CommandIdentifier): PolicyResult {
26+
const parts = commandIdentifier.command.toLowerCase().split('.');
2627

2728

2829
if (parts.length > 2) {
@@ -37,7 +38,7 @@ export class StaticPolicyResolver implements PolicyResolver {
3738

3839
if (!this.policies[moduleName]) {
3940
if (this.fallbackResolver) {
40-
return this.fallbackResolver.resolvePolicy(commandName);
41+
return this.fallbackResolver.resolvePolicy(commandIdentifier);
4142
}
4243

4344
// For std module commands, return 'unknown-command' instead of 'unknown-module'
@@ -51,14 +52,26 @@ export class StaticPolicyResolver implements PolicyResolver {
5152
if (!this.policies[moduleName][commandName]) {
5253
// Try fallback resolver if available
5354
if (this.fallbackResolver) {
54-
return this.fallbackResolver.resolvePolicy(commandName);
55+
return this.fallbackResolver.resolvePolicy(commandIdentifier);
5556
}
5657
return { ok: false, error: 'unknown-command' };
5758
}
5859

60+
const policy = this.policies[moduleName][commandName];
61+
62+
if(policy.subcommands) {
63+
const subcommandPolicy = policy.subcommands[commandIdentifier.subcommand];
64+
if(subcommandPolicy) {
65+
return {
66+
ok: true,
67+
value: subcommandPolicy
68+
}
69+
}
70+
}
71+
5972
return {
6073
ok: true,
61-
value: this.policies[moduleName][commandName]
74+
value: policy
6275
}
6376
}
6477
}

packages/client/lib/cluster/request-response-policies/types.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { CommandIdentifier } from '../../client/parser';
12
import type { CommandPolicies } from './policies-constants';
23

34
export type Either<TOk, TError> =
@@ -6,11 +7,12 @@ export type Either<TOk, TError> =
67

78
export type PolicyResult = Either<CommandPolicies, 'policy-not-found' | 'unknown-command' | 'unknown-module' | 'wrong-command-or-module-name' | 'no-policy-resolved'>;
89

10+
911
export interface PolicyResolver {
1012
/**
1113
* The response of the COMMAND command uses "." to separate the module name from the command name.
1214
*/
13-
resolvePolicy(command: string): PolicyResult;
15+
resolvePolicy(commandIdentifier: CommandIdentifier): PolicyResult;
1416

1517
/**
1618
* Sets a fallback resolver to use when policies are not found in this resolver.

0 commit comments

Comments
 (0)