Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restructure createCommand type functions to enable direct passthrough of functions #2699

Draft
wants to merge 2 commits into
base: v5
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 5 additions & 5 deletions packages/client/lib/client/index.ts
Expand Up @@ -147,7 +147,7 @@ export default class RedisClient<
RESP extends RespVersions,
TYPE_MAPPING extends TypeMapping
> extends EventEmitter {
static #createCommand(command: Command, resp: RespVersions) {
static #createCommand(name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: ProxyClient, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -158,7 +158,7 @@ export default class RedisClient<
};
}

static #createModuleCommand(command: Command, resp: RespVersions) {
static #createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -169,7 +169,7 @@ export default class RedisClient<
};
}

static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
static #createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
return async function (this: NamespaceProxyClient, ...args: Array<unknown>) {
Expand All @@ -184,7 +184,7 @@ export default class RedisClient<
};
}

static #createScriptCommand(script: RedisScript, resp: RespVersions) {
static #createScriptCommand(name: string, script: RedisScript, resp: RespVersions) {
const prefix = scriptArgumentsPrefix(script),
transformReply = getTransformReply(script, resp);
return async function (this: ProxyClient, ...args: Array<unknown>) {
Expand Down Expand Up @@ -460,7 +460,7 @@ export default class RedisClient<
withCommandOptions<
OPTIONS extends CommandOptions<TYPE_MAPPING>,
TYPE_MAPPING extends TypeMapping
>(options: OPTIONS) {
>(options?: OPTIONS) {
const proxy = Object.create(this._self);
proxy._commandOptions = options;
return proxy as RedisClientType<
Expand Down
8 changes: 4 additions & 4 deletions packages/client/lib/client/multi-command.ts
Expand Up @@ -86,7 +86,7 @@ export type RedisClientMultiCommandType<
type ExecuteMulti = (commands: Array<RedisMultiQueuedCommand>, selectedDB?: number) => Promise<Array<unknown>>;

export default class RedisClientMultiCommand<REPLIES = []> {
static #createCommand(command: Command, resp: RespVersions) {
static #createCommand(name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
return this.addCommand(
Expand All @@ -96,7 +96,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
};
}

static #createModuleCommand(command: Command, resp: RespVersions) {
static #createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
return this._self.addCommand(
Expand All @@ -106,7 +106,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
};
}

static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
static #createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
return function (this: { _self: RedisClientMultiCommand }, ...args: Array<unknown>) {
Expand All @@ -120,7 +120,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
};
}

static #createScriptCommand(script: RedisScript, resp: RespVersions) {
static #createScriptCommand(name: string, script: RedisScript, resp: RespVersions) {
const transformReply = getTransformReply(script, resp);
return function (this: RedisClientMultiCommand, ...args: Array<unknown>) {
this.#multi.addScript(
Expand Down
90 changes: 55 additions & 35 deletions packages/client/lib/client/pool.ts
Expand Up @@ -4,7 +4,7 @@ import RedisClient, { RedisClientType, RedisClientOptions, RedisClientExtensions
import { EventEmitter } from 'node:events';
import { DoublyLinkedNode, DoublyLinkedList, SinglyLinkedList } from './linked-list';
import { TimeoutError } from '../errors';
import { attachConfig, functionArgumentsPrefix, getTransformReply, scriptArgumentsPrefix } from '../commander';
import { attachConfig } from '../commander';
import { CommandOptions } from './commands-queue';
import RedisClientMultiCommand, { RedisClientMultiCommandType } from './multi-command';

Expand Down Expand Up @@ -58,53 +58,73 @@ export class RedisClientPool<
RESP extends RespVersions = 2,
TYPE_MAPPING extends TypeMapping = {}
> extends EventEmitter {
static #createCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
static #createCommand(name: string, command: Command, resp: RespVersions) {
//const transformReply = getTransformReply(command, resp);
return async function (this: ProxyPool, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
reply = await this.sendCommand(redisArgs, this._commandOptions);
return transformReply ?
transformReply(reply, redisArgs.preserve) :
reply;
//const redisArgs = command.transformArguments(...args),
//reply = await this.sendCommand(redisArgs, this._commandOptions);
if (this._commandOptions) {
return this.execute(client => client.withCommandOptions(this._commandOptions)[name](...args));
} else {
return this.execute(client => client[name](...args));
}
//return transformReply ?
//transformReply(reply, redisArgs.preserve) :
//reply;
};
}

static #createModuleCommand(command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
static #createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions) {
//const transformReply = getTransformReply(command, resp);
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
reply = await this._self.sendCommand(redisArgs, this._self._commandOptions);
return transformReply ?
transformReply(reply, redisArgs.preserve) :
reply;
// const redisArgs = command.transformArguments(...args),
// reply = await this._self.sendCommand(redisArgs, this._self._commandOptions);
if (this._self._commandOptions) {
return this._self.execute(client => client.withCommandOptions(this._self._commandOptions)[moduleName][name](...args));
} else {
return this._self.execute(client => client[moduleName][name](...args));
}
// return transformReply ?
// transformReply(reply, redisArgs.preserve) :
// reply;
};
}

static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
static #createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions) {
// const prefix = functionArgumentsPrefix(name, fn),
// transformReply = getTransformReply(fn, resp);
return async function (this: NamespaceProxyPool, ...args: Array<unknown>) {
const fnArgs = fn.transformArguments(...args),
reply = await this._self.sendCommand(
prefix.concat(fnArgs),
this._self._commandOptions
);
return transformReply ?
transformReply(reply, fnArgs.preserve) :
reply;
// const fnArgs = fn.transformArguments(...args),
// reply = await this._self.sendCommand(
// prefix.concat(fnArgs),
// this._self._commandOptions
// );
if (this._self._commandOptions) {
return this._self.execute(client => client.withCommandOptions(this._self._commandOptions)[libName][name](...args));
} else {
return this._self.execute(client => client[libName][name](...args));
}
// return transformReply ?
// transformReply(reply, fnArgs.preserve) :
// reply;
};
}

static #createScriptCommand(script: RedisScript, resp: RespVersions) {
const prefix = scriptArgumentsPrefix(script),
transformReply = getTransformReply(script, resp);
static #createScriptCommand(name: string, script: RedisScript, resp: RespVersions) {
// const prefix = scriptArgumentsPrefix(script),
// transformReply = getTransformReply(script, resp);
return async function (this: ProxyPool, ...args: Array<unknown>) {
const scriptArgs = script.transformArguments(...args),
redisArgs = prefix.concat(scriptArgs),
reply = await this.executeScript(script, redisArgs, this._commandOptions);
return transformReply ?
transformReply(reply, scriptArgs.preserve) :
reply;
// const scriptArgs = script.transformArguments(...args),
// redisArgs = prefix.concat(scriptArgs),
// reply = await this.executeScript(script, redisArgs, this._commandOptions);
if (this._self._commandOptions) {
return this.execute(client => client.withCommandOptions(this._commandOptions)[name](...args));
} else {
return this._self.execute(client => client[name](...args));
}
// return transformReply ?
// transformReply(reply, scriptArgs.preserve) :
// reply;
};
}

Expand Down
8 changes: 4 additions & 4 deletions packages/client/lib/cluster/index.ts
Expand Up @@ -166,7 +166,7 @@ export default class RedisCluster<
return key;
}

static #createCommand(command: Command, resp: RespVersions) {
static #createCommand(name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: ProxyCluster, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -189,7 +189,7 @@ export default class RedisCluster<
};
}

static #createModuleCommand(command: Command, resp: RespVersions) {
static #createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -212,7 +212,7 @@ export default class RedisCluster<
};
}

static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
static #createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
return async function (this: NamespaceProxyCluster, ...args: Array<unknown>) {
Expand All @@ -237,7 +237,7 @@ export default class RedisCluster<
};
}

static #createScriptCommand(script: RedisScript, resp: RespVersions) {
static #createScriptCommand(name: string, script: RedisScript, resp: RespVersions) {
const prefix = scriptArgumentsPrefix(script),
transformReply = getTransformReply(script, resp);
return async function (this: ProxyCluster, ...args: Array<unknown>) {
Expand Down
8 changes: 4 additions & 4 deletions packages/client/lib/cluster/multi-command.ts
Expand Up @@ -91,7 +91,7 @@ export type ClusterMultiExecute = (
) => Promise<Array<unknown>>;

export default class RedisClusterMultiCommand<REPLIES = []> {
static #createCommand(command: Command, resp: RespVersions) {
static #createCommand(name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -109,7 +109,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
};
}

static #createModuleCommand(command: Command, resp: RespVersions) {
static #createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions) {
const transformReply = getTransformReply(command, resp);
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
const redisArgs = command.transformArguments(...args),
Expand All @@ -127,7 +127,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
};
}

static #createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions) {
static #createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions) {
const prefix = functionArgumentsPrefix(name, fn),
transformReply = getTransformReply(fn, resp);
return function (this: { _self: RedisClusterMultiCommand }, ...args: Array<unknown>) {
Expand All @@ -148,7 +148,7 @@ export default class RedisClusterMultiCommand<REPLIES = []> {
};
}

static #createScriptCommand(script: RedisScript, resp: RespVersions) {
static #createScriptCommand(name: string, script: RedisScript, resp: RespVersions) {
const transformReply = getTransformReply(script, resp);
return function (this: RedisClusterMultiCommand, ...args: Array<unknown>) {
const scriptArgs = script.transformArguments(...args);
Expand Down
16 changes: 8 additions & 8 deletions packages/client/lib/commander.ts
Expand Up @@ -8,10 +8,10 @@ interface AttachConfigOptions<
> {
BaseClass: new (...args: any) => any;
commands: RedisCommands;
createCommand(command: Command, resp: RespVersions): (...args: any) => any;
createModuleCommand(command: Command, resp: RespVersions): (...args: any) => any;
createFunctionCommand(name: string, fn: RedisFunction, resp: RespVersions): (...args: any) => any;
createScriptCommand(script: RedisScript, resp: RespVersions): (...args: any) => any;
createCommand(name: string, command: Command, resp: RespVersions): (...args: any) => any;
createModuleCommand(moduleName: string, name: string, command: Command, resp: RespVersions): (...args: any) => any;
createFunctionCommand(libName: string, name: string, fn: RedisFunction, resp: RespVersions): (...args: any) => any;
createScriptCommand(name: string, script: RedisScript, resp: RespVersions): (...args: any) => any;
config?: CommanderConfig<M, F, S, RESP>;
}

Expand All @@ -33,14 +33,14 @@ export function attachConfig<
Class: any = class extends BaseClass {};

for (const [name, command] of Object.entries(commands)) {
Class.prototype[name] = createCommand(command, RESP);
Class.prototype[name] = createCommand(name, command, RESP);
}

if (config?.modules) {
for (const [moduleName, module] of Object.entries(config.modules)) {
const fns = Object.create(null);
for (const [name, command] of Object.entries(module)) {
fns[name] = createModuleCommand(command, RESP);
fns[name] = createModuleCommand(moduleName, name, command, RESP);
}

attachNamespace(Class.prototype, moduleName, fns);
Expand All @@ -51,7 +51,7 @@ export function attachConfig<
for (const [library, commands] of Object.entries(config.functions)) {
const fns = Object.create(null);
for (const [name, command] of Object.entries(commands)) {
fns[name] = createFunctionCommand(name, command, RESP);
fns[name] = createFunctionCommand(library, name, command, RESP);
}

attachNamespace(Class.prototype, library, fns);
Expand All @@ -60,7 +60,7 @@ export function attachConfig<

if (config?.scripts) {
for (const [name, script] of Object.entries(config.scripts)) {
Class.prototype[name] = createScriptCommand(script, RESP);
Class.prototype[name] = createScriptCommand(name, script, RESP);
}
}

Expand Down