Skip to content

Commit

Permalink
addressed all in person review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
sjpotter committed Oct 28, 2024
1 parent 21ce0cb commit c1d69ae
Show file tree
Hide file tree
Showing 71 changed files with 922 additions and 2,016 deletions.
2,511 changes: 754 additions & 1,757 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions packages/client/lib/RESP/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ export type CommandArguments = Array<RedisArgument> & { preserve?: unknown };
// };

export type Command = {
CACHEABLE?: boolean;
IS_READ_ONLY?: boolean;
/**
* @internal
Expand Down
15 changes: 8 additions & 7 deletions packages/client/lib/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ export default class RedisClient<
const transformReply = getTransformReply(command, resp);

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

return this._self._executeCommand(parser, this._commandOptions, transformReply);
return this._self._executeCommand(command, parser, this._commandOptions, transformReply);
}
}

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

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

return this._self._executeCommand(parser, this._self._commandOptions, transformReply);
return this._self._executeCommand(command, parser, this._self._commandOptions, transformReply);
};
}

Expand All @@ -177,11 +177,11 @@ export default class RedisClient<
const transformReply = getTransformReply(fn, resp);

return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);

return this._self._executeCommand(parser, this._self._commandOptions, transformReply);
return this._self._executeCommand(fn, parser, this._self._commandOptions, transformReply);
};
}

Expand All @@ -190,7 +190,7 @@ export default class RedisClient<
const transformReply = getTransformReply(script, resp);

return async function (this: ProxyClient, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.push(...prefix);
script.parseCommand(parser, ...args)

Expand Down Expand Up @@ -576,6 +576,7 @@ export default class RedisClient<
* @internal
*/
async _executeCommand(
command: Command,
parser: CommandParser,
commandOptions: CommandOptions<TYPE_MAPPING> | undefined,
transformReply: TransformReply | undefined,
Expand Down
45 changes: 19 additions & 26 deletions packages/client/lib/client/multi-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,71 +91,79 @@ export default class RedisClientMultiCommand<REPLIES = []> {
static #createCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);

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

const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;

return this.addCommand(
this.#multi.addCommand(
redisArgs,
transformReply
);

return this;
};
}

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

return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);

const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;

return this._self.addCommand(
this._self.#multi.addCommand(
redisArgs,
transformReply
);

return this._self;
};
}

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

return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>): RedisClientMultiCommand {
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);

const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;

return this._self.addCommand(
this._self.#multi.addCommand(
redisArgs,
transformReply
);

return this._self;
};
}

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

return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
script.parseCommand(parser, ...args);

const redisArgs: CommandArguments = parser.redisArgs;
redisArgs.preserve = parser.preserve;

return this.addScript(
this.#multi.addScript(
script,
redisArgs,
transformReply
);

return this;
};
}

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

select = this.SELECT;

addCommand(args: CommandArguments, transformReply?: TransformReply) {
this.#multi.addCommand(args, transformReply);
return this;
}

addScript(
script: RedisScript,
args: CommandArguments,
transformReply?: TransformReply
) {
this.#multi.addScript(script, args, transformReply);

return this;
}

async exec<T extends MultiReply = MULTI_REPLY['GENERIC']>(execAsPipeline = false): Promise<MultiReplyType<T, REPLIES>> {
if (execAsPipeline) return this.execAsPipeline<T>();

Expand Down
40 changes: 5 additions & 35 deletions packages/client/lib/client/parser.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { RedisArgument, RespVersions } from "../RESP/types";
import { RedisVariadicArgument } from "../commands/generic-transformers";
import { RedisArgument } from '../RESP/types';
import { RedisVariadicArgument } from '../commands/generic-transformers';

export interface CommandParser {
redisArgs: ReadonlyArray<RedisArgument>;
keys: ReadonlyArray<RedisArgument>;
firstKey: RedisArgument | undefined;
respVersion: RespVersions;
preserve: unknown;
cachable: boolean;

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

export class BasicCommandParser implements CommandParser {
#redisArgs: Array<RedisArgument> = [];
#keys: Array<RedisArgument> = [];
#respVersion: RespVersions;
#preserve: unknown;
#cachable: boolean = false;

constructor(respVersion: RespVersions = 2) {
this.#respVersion = respVersion;
}
preserve: unknown;

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

get firstKey() {
return this.#keys.length != 0 ? this.#keys[0] : undefined;
}

get respVersion() {
return this.#respVersion;
}

get preserve() {
return this.#preserve;
}

get cachable() {
return this.#cachable
return this.#keys[0];
}

push(...arg: Array<RedisArgument>) {
Expand Down Expand Up @@ -91,7 +69,7 @@ export class BasicCommandParser implements CommandParser {
pushKey(key: RedisArgument) {
this.#keys.push(key);
this.#redisArgs.push(key);
};
}

pushKeysLength(keys: RedisVariadicArgument) {
if (Array.isArray(keys)) {
Expand All @@ -111,12 +89,4 @@ export class BasicCommandParser implements CommandParser {
this.#redisArgs.push(keys);
}
}

setPreserve(val: unknown) {
this.#preserve = val;
}

setCachable() {
this.#cachable = true;
};
}
14 changes: 7 additions & 7 deletions packages/client/lib/client/pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,21 @@ export class RedisClientPool<
const transformReply = getTransformReply(command, resp);

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

return this.execute(client => client._executeCommand(parser, this._commandOptions, transformReply))
return this.execute(client => client._executeCommand(command, parser, this._commandOptions, transformReply))
};
}

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

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

return this._self.execute(client => client._executeCommand(parser, this._self._commandOptions, transformReply))
return this._self.execute(client => client._executeCommand(command, parser, this._self._commandOptions, transformReply))
};
}

Expand All @@ -90,19 +90,19 @@ export class RedisClientPool<
const transformReply = getTransformReply(fn, resp);

return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);

return this._self.execute(client => client._executeCommand(parser, this._self._commandOptions, transformReply)) };
return this._self.execute(client => client._executeCommand(fn, parser, this._self._commandOptions, transformReply)) };
}

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

return async function (this: ProxyPool, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.pushVariadic(prefix);
script.parseCommand(parser, ...args);

Expand Down
16 changes: 8 additions & 8 deletions packages/client/lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,14 @@ export default class RedisCluster<
static #createCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: ProxyCluster, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
command.parseCommand(parser, ...args);

return this._self.#execute(
parser.firstKey,
command.IS_READ_ONLY,
this._commandOptions,
(client, opts) => client._executeCommand(parser, opts, transformReply)
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
);
};
}
Expand All @@ -165,14 +165,14 @@ export default class RedisCluster<
const transformReply = getTransformReply(command, resp);

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

return this._self.#execute(
parser.firstKey,
command.IS_READ_ONLY,
this._self._commandOptions,
(client, opts) => client._executeCommand(parser, opts, transformReply)
(client, opts) => client._executeCommand(command, parser, opts, transformReply)
);
};
}
Expand All @@ -182,15 +182,15 @@ export default class RedisCluster<
const transformReply = getTransformReply(fn, resp);

return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.push(...prefix);
fn.parseCommand(parser, ...args);

return this._self.#execute(
parser.firstKey,
fn.IS_READ_ONLY,
this._self._commandOptions,
(client, opts) => client._executeCommand(parser, opts, transformReply)
(client, opts) => client._executeCommand(fn, parser, opts, transformReply)
);
};
}
Expand All @@ -200,7 +200,7 @@ export default class RedisCluster<
const transformReply = getTransformReply(script, resp);

return async function (this: ProxyCluster, ...args: Array<unknown>) {
const parser = new BasicCommandParser(resp);
const parser = new BasicCommandParser();
parser.push(...prefix);
script.parseCommand(parser, ...args);

Expand Down Expand Up @@ -419,7 +419,7 @@ export default class RedisCluster<
client = redirectTo;

const chainId = Symbol('Asking Chain');
const myOpts = options ? {...options} : {};
myOpts = options ? {...options} : {};
myOpts.chainId = chainId;

client.sendCommand(parseArgs(ASKING), {chainId: chainId}).catch(err => { console.log(`Asking Failed: ${err}`) } );
Expand Down
Loading

0 comments on commit c1d69ae

Please sign in to comment.