Skip to content

Commit 12c3b1b

Browse files
committed
Merge branch 'cmd-parser-with-commands' into csc-1
2 parents 8edc753 + c1d69ae commit 12c3b1b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

70 files changed

+167
-258
lines changed

packages/client/lib/RESP/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ export type CommandArguments = Array<RedisArgument> & { preserve?: unknown };
274274
// };
275275

276276
export type Command = {
277+
CACHEABLE?: boolean;
277278
IS_READ_ONLY?: boolean;
278279
/**
279280
* @internal

packages/client/lib/client/index.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,21 +159,21 @@ export default class RedisClient<
159159
const transformReply = getTransformReply(command, resp);
160160

161161
return async function (this: ProxyClient, ...args: Array<unknown>) {
162-
const parser = new BasicCommandParser(resp);
162+
const parser = new BasicCommandParser();
163163
command.parseCommand(parser, ...args);
164164

165-
return this._self._executeCommand(parser, this._commandOptions, transformReply);
165+
return this._self._executeCommand(command, parser, this._commandOptions, transformReply);
166166
}
167167
}
168168

169169
static #createModuleCommand(command: Command, resp: RespVersions) {
170170
const transformReply = getTransformReply(command, resp);
171171

172172
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
173-
const parser = new BasicCommandParser(resp);
173+
const parser = new BasicCommandParser();
174174
command.parseCommand(parser, ...args);
175175

176-
return this._self._executeCommand(parser, this._self._commandOptions, transformReply);
176+
return this._self._executeCommand(command, parser, this._self._commandOptions, transformReply);
177177
};
178178
}
179179

@@ -182,11 +182,11 @@ export default class RedisClient<
182182
const transformReply = getTransformReply(fn, resp);
183183

184184
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
185-
const parser = new BasicCommandParser(resp);
185+
const parser = new BasicCommandParser();
186186
parser.push(...prefix);
187187
fn.parseCommand(parser, ...args);
188188

189-
return this._self._executeCommand(parser, this._self._commandOptions, transformReply);
189+
return this._self._executeCommand(fn, parser, this._self._commandOptions, transformReply);
190190
};
191191
}
192192

@@ -195,7 +195,7 @@ export default class RedisClient<
195195
const transformReply = getTransformReply(script, resp);
196196

197197
return async function (this: ProxyClient, ...args: Array<unknown>) {
198-
const parser = new BasicCommandParser(resp);
198+
const parser = new BasicCommandParser();
199199
parser.push(...prefix);
200200
script.parseCommand(parser, ...args)
201201

@@ -604,6 +604,7 @@ export default class RedisClient<
604604
* @internal
605605
*/
606606
async _executeCommand(
607+
command: Command,
607608
parser: CommandParser,
608609
commandOptions: CommandOptions<TYPE_MAPPING> | undefined,
609610
transformReply: TransformReply | undefined,

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

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -91,71 +91,79 @@ export default class RedisClientMultiCommand<REPLIES = []> {
9191
static #createCommand(command: Command, resp: RespVersions) {
9292
const transformReply = getTransformReply(command, resp);
9393

94-
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
95-
const parser = new BasicCommandParser(resp);
94+
return function (this: RedisClientMultiCommand, ...args: Array<unknown>): RedisClientMultiCommand {
95+
const parser = new BasicCommandParser();
9696
command.parseCommand(parser, ...args);
9797

9898
const redisArgs: CommandArguments = parser.redisArgs;
9999
redisArgs.preserve = parser.preserve;
100100

101-
return this.addCommand(
101+
this.#multi.addCommand(
102102
redisArgs,
103103
transformReply
104104
);
105+
106+
return this;
105107
};
106108
}
107109

108110
static #createModuleCommand(command: Command, resp: RespVersions) {
109111
const transformReply = getTransformReply(command, resp);
110112

111-
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
112-
const parser = new BasicCommandParser(resp);
113+
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
114+
const parser = new BasicCommandParser();
113115
command.parseCommand(parser, ...args);
114116

115117
const redisArgs: CommandArguments = parser.redisArgs;
116118
redisArgs.preserve = parser.preserve;
117119

118-
return this._self.addCommand(
120+
this._self.#multi.addCommand(
119121
redisArgs,
120122
transformReply
121123
);
124+
125+
return this._self;
122126
};
123127
}
124128

125129
static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
126130
const prefix = functionArgumentsPrefix(name, fn);
127131
const transformReply = getTransformReply(fn, resp);
128132

129-
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
130-
const parser = new BasicCommandParser(resp);
133+
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
134+
const parser = new BasicCommandParser();
131135
parser.push(...prefix);
132136
fn.parseCommand(parser, ...args);
133137

134138
const redisArgs: CommandArguments = parser.redisArgs;
135139
redisArgs.preserve = parser.preserve;
136140

137-
return this._self.addCommand(
141+
this._self.#multi.addCommand(
138142
redisArgs,
139143
transformReply
140144
);
145+
146+
return this._self;
141147
};
142148
}
143149

144150
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
145151
const transformReply = getTransformReply(script, resp);
146152

147153
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
148-
const parser = new BasicCommandParser(resp);
154+
const parser = new BasicCommandParser();
149155
script.parseCommand(parser, ...args);
150156

151157
const redisArgs: CommandArguments = parser.redisArgs;
152158
redisArgs.preserve = parser.preserve;
153159

154-
return this.addScript(
160+
this.#multi.addScript(
155161
script,
156162
redisArgs,
157163
transformReply
158164
);
165+
166+
return this;
159167
};
160168
}
161169

@@ -196,21 +204,6 @@ export default class RedisClientMultiCommand<REPLIES = []> {
196204

197205
select = this.SELECT;
198206

199-
addCommand(args: CommandArguments, transformReply?: TransformReply) {
200-
this.#multi.addCommand(args, transformReply);
201-
return this;
202-
}
203-
204-
addScript(
205-
script: RedisScript,
206-
args: CommandArguments,
207-
transformReply?: TransformReply
208-
) {
209-
this.#multi.addScript(script, args, transformReply);
210-
211-
return this;
212-
}
213-
214207
async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false): Promise<MultiReplyType<T, REPLIES>> {
215208
if (execAsPipeline) return this.execAsPipeline<T>();
216209

packages/client/lib/client/parser.ts

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

44
export interface CommandParser {
55
redisArgs: ReadonlyArray<RedisArgument>;
66
keys: ReadonlyArray<RedisArgument>;
77
firstKey: RedisArgument | undefined;
8-
respVersion: RespVersions;
98
preserve: unknown;
10-
cachable: boolean;
119

1210
push: (...arg: Array<RedisArgument>) => unknown;
1311
pushVariadic: (vals: RedisVariadicArgument) => unknown;
@@ -16,20 +14,12 @@ export interface CommandParser {
1614
pushKey: (key: RedisArgument) => unknown; // normal push of keys
1715
pushKeys: (keys: RedisVariadicArgument) => unknown; // push multiple keys at a time
1816
pushKeysLength: (keys: RedisVariadicArgument) => unknown; // push multiple keys at a time
19-
setCachable: () => unknown;
20-
setPreserve: (val: unknown) => unknown;
2117
}
2218

2319
export class BasicCommandParser implements CommandParser {
2420
#redisArgs: Array<RedisArgument> = [];
2521
#keys: Array<RedisArgument> = [];
26-
#respVersion: RespVersions;
27-
#preserve: unknown;
28-
#cachable: boolean = false;
29-
30-
constructor(respVersion: RespVersions = 2) {
31-
this.#respVersion = respVersion;
32-
}
22+
preserve: unknown;
3323

3424
get redisArgs() {
3525
return this.#redisArgs;
@@ -40,19 +30,7 @@ export class BasicCommandParser implements CommandParser {
4030
}
4131

4232
get firstKey() {
43-
return this.#keys.length != 0 ? this.#keys[0] : undefined;
44-
}
45-
46-
get respVersion() {
47-
return this.#respVersion;
48-
}
49-
50-
get preserve() {
51-
return this.#preserve;
52-
}
53-
54-
get cachable() {
55-
return this.#cachable
33+
return this.#keys[0];
5634
}
5735

5836
get cacheKey() {
@@ -96,7 +74,7 @@ export class BasicCommandParser implements CommandParser {
9674
pushKey(key: RedisArgument) {
9775
this.#keys.push(key);
9876
this.#redisArgs.push(key);
99-
};
77+
}
10078

10179
pushKeysLength(keys: RedisVariadicArgument) {
10280
if (Array.isArray(keys)) {
@@ -116,12 +94,4 @@ export class BasicCommandParser implements CommandParser {
11694
this.#redisArgs.push(keys);
11795
}
11896
}
119-
120-
setPreserve(val: unknown) {
121-
this.#preserve = val;
122-
}
123-
124-
setCachable() {
125-
this.#cachable = true;
126-
};
12797
}

packages/client/lib/client/pool.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,21 +72,21 @@ export class RedisClientPool<
7272
const transformReply = getTransformReply(command, resp);
7373

7474
return async function (this: ProxyPool, ...args: Array<unknown>) {
75-
const parser = new BasicCommandParser(resp);
75+
const parser = new BasicCommandParser();
7676
command.parseCommand(parser, ...args);
7777

78-
return this.execute(client => client._executeCommand(parser, this._commandOptions, transformReply))
78+
return this.execute(client => client._executeCommand(command, parser, this._commandOptions, transformReply))
7979
};
8080
}
8181

8282
static #createModuleCommand(command: Command, resp: RespVersions) {
8383
const transformReply = getTransformReply(command, resp);
8484

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

89-
return this._self.execute(client => client._executeCommand(parser, this._self._commandOptions, transformReply))
89+
return this._self.execute(client => client._executeCommand(command, parser, this._self._commandOptions, transformReply))
9090
};
9191
}
9292

@@ -95,19 +95,19 @@ export class RedisClientPool<
9595
const transformReply = getTransformReply(fn, resp);
9696

9797
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
98-
const parser = new BasicCommandParser(resp);
98+
const parser = new BasicCommandParser();
9999
parser.push(...prefix);
100100
fn.parseCommand(parser, ...args);
101101

102-
return this._self.execute(client => client._executeCommand(parser, this._self._commandOptions, transformReply)) };
102+
return this._self.execute(client => client._executeCommand(fn, parser, this._self._commandOptions, transformReply)) };
103103
}
104104

105105
static #createScriptCommand(script: RedisScript, resp: RespVersions) {
106106
const prefix = scriptArgumentsPrefix(script);
107107
const transformReply = getTransformReply(script, resp);
108108

109109
return async function (this: ProxyPool, ...args: Array<unknown>) {
110-
const parser = new BasicCommandParser(resp);
110+
const parser = new BasicCommandParser();
111111
parser.pushVariadic(prefix);
112112
script.parseCommand(parser, ...args);
113113

packages/client/lib/cluster/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -154,14 +154,14 @@ export default class RedisCluster<
154154
const transformReply = getTransformReply(command, resp);
155155

156156
return async function (this: ProxyCluster, ...args: Array<unknown>) {
157-
const parser = new BasicCommandParser(resp);
157+
const parser = new BasicCommandParser();
158158
command.parseCommand(parser, ...args);
159159

160160
return this._self.#execute(
161161
parser.firstKey,
162162
command.IS_READ_ONLY,
163163
this._commandOptions,
164-
(client, opts) => client._executeCommand(parser, opts, transformReply)
164+
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
165165
);
166166
};
167167
}
@@ -170,14 +170,14 @@ export default class RedisCluster<
170170
const transformReply = getTransformReply(command, resp);
171171

172172
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
173-
const parser = new BasicCommandParser(resp);
173+
const parser = new BasicCommandParser();
174174
command.parseCommand(parser, ...args);
175175

176176
return this._self.#execute(
177177
parser.firstKey,
178178
command.IS_READ_ONLY,
179179
this._self._commandOptions,
180-
(client, opts) => client._executeCommand(parser, opts, transformReply)
180+
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
181181
);
182182
};
183183
}
@@ -187,15 +187,15 @@ export default class RedisCluster<
187187
const transformReply = getTransformReply(fn, resp);
188188

189189
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
190-
const parser = new BasicCommandParser(resp);
190+
const parser = new BasicCommandParser();
191191
parser.push(...prefix);
192192
fn.parseCommand(parser, ...args);
193193

194194
return this._self.#execute(
195195
parser.firstKey,
196196
fn.IS_READ_ONLY,
197197
this._self._commandOptions,
198-
(client, opts) => client._executeCommand(parser, opts, transformReply)
198+
(client, opts) => client._executeCommand(fn, parser, opts, transformReply)
199199
);
200200
};
201201
}
@@ -205,7 +205,7 @@ export default class RedisCluster<
205205
const transformReply = getTransformReply(script, resp);
206206

207207
return async function (this: ProxyCluster, ...args: Array<unknown>) {
208-
const parser = new BasicCommandParser(resp);
208+
const parser = new BasicCommandParser();
209209
parser.push(...prefix);
210210
script.parseCommand(parser, ...args);
211211

0 commit comments

Comments
 (0)