Skip to content

Commit 3675e16

Browse files
committed
parse subcommands, update static policies
actually fetched from server using the dynamic resolver
1 parent 41b53aa commit 3675e16

File tree

8 files changed

+2901
-77
lines changed

8 files changed

+2901
-77
lines changed

packages/client/lib/cluster/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,8 +465,10 @@ export default class RedisCluster<
465465
commandName: string,
466466
fn: (client: RedisClientType<M, F, S, RESP, TYPE_MAPPING>, opts?: ClusterCommandOptions) => Promise<T>
467467
): Promise<T> {
468+
468469
const maxCommandRedirections = this._options.maxCommandRedirections ?? 16;
469470
const policyResult = this._policyResolver.resolvePolicy(commandName)
471+
470472
if(policyResult.ok) {
471473
//TODO
472474
} else {

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

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import type { CommandReply } from '../../commands/generic-transformers';
22
import type { CommandPolicies } from './policies-constants';
33
import { REQUEST_POLICIES_WITH_DEFAULTS, RESPONSE_POLICIES_WITH_DEFAULTS } from './policies-constants';
4-
import type { PolicyResolver } from './types';
4+
import type { PolicyResolver, ModulePolicyRecords } from './types';
55
import { StaticPolicyResolver } from './static-policy-resolver';
6-
import type { ModulePolicyRecords } from './static-policies-data';
76

87
/**
98
* Function type that returns command information from Redis
@@ -28,7 +27,7 @@ export class DynamicPolicyResolverFactory {
2827
static async create(
2928
commandFetcher: CommandFetcher,
3029
fallbackResolver?: PolicyResolver
31-
): Promise<StaticPolicyResolver> {
30+
): Promise<PolicyResolver> {
3231
const commands = await commandFetcher();
3332
const policies: ModulePolicyRecords = {};
3433

@@ -98,10 +97,28 @@ export class DynamicPolicyResolverFactory {
9897
const defaultResponse = isKeyless
9998
? RESPONSE_POLICIES_WITH_DEFAULTS.DEFAULT_KEYLESS
10099
: RESPONSE_POLICIES_WITH_DEFAULTS.DEFAULT_KEYED;
100+
101+
let subcommands: Record<string, CommandPolicies> | undefined;
102+
if(command.subcommands.length > 0) {
103+
subcommands = {};
104+
for (const subcommand of command.subcommands) {
105+
106+
// Subcommands are in format "parentCommand|subcommand"
107+
const parts = subcommand.name.split("\|")
108+
if(parts.length !== 2) {
109+
throw new Error(`Invalid subcommand name: ${subcommand.name}`);
110+
}
111+
const subcommandName = parts[1];
112+
113+
subcommands[subcommandName] = DynamicPolicyResolverFactory.#buildCommandPolicies(subcommand);
114+
}
115+
}
101116

102117
return {
103118
request: command.policies.request ?? defaultRequest,
104-
response: command.policies.response ?? defaultResponse
119+
response: command.policies.response ?? defaultResponse,
120+
isKeyless,
121+
subcommands
105122
};
106123
}
107124
}
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
export type { Either, PolicyResult, PolicyResolver } from './types';
1+
export type { Either, PolicyResult, PolicyResolver, ModulePolicyRecords, CommandPolicyRecords } from './types';
22

33
export { StaticPolicyResolver } from './static-policy-resolver';
44
export { DynamicPolicyResolverFactory, type CommandFetcher } from './dynamic-policy-resolver-factory';
55

66
export * from './policies-constants';
7-
export type { ModulePolicyRecords, CommandPolicyRecords } from './static-policies-data';
87
export { POLICIES } from './static-policies-data';
98

109
// export { type CommandRouter } from './command-router';

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,6 @@ export type ResponsePolicyWithDefaults = typeof RESPONSE_POLICIES_WITH_DEFAULTS[
2727
export interface CommandPolicies {
2828
readonly request: RequestPolicyWithDefaults;
2929
readonly response: ResponsePolicyWithDefaults;
30+
readonly subcommands?: Record<string, CommandPolicies>;
31+
readonly isKeyless: boolean;
3032
}

0 commit comments

Comments
 (0)