1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-17 19:41:06 +03:00

enhance the way commanders (client/multi/cluster) get extended with modules and scripts

This commit is contained in:
leibale
2021-07-26 17:36:03 -04:00
parent 51d05dae78
commit 62e0cd354b
5 changed files with 326 additions and 408 deletions

View File

@@ -6,6 +6,17 @@ import { AbortError } from './errors';
import { defineScript } from './lua-script';
import { spy } from 'sinon';
const SQUARE_SCRIPT = defineScript({
NUMBER_OF_KEYS: 0,
SCRIPT: 'return ARGV[1] * ARGV[1];',
transformArguments(number: number): Array<string> {
return [number.toString()];
},
transformReply(reply: number): number {
return reply;
}
});
describe('Client', () => {
describe('authentication', () => {
itWithClient(TestRedisServers.PASSWORD, 'Client should be authenticated', async client => {
@@ -26,7 +37,6 @@ describe('Client', () => {
await assert.rejects(
client.connect(),
{
message: isRedisVersionGreaterThan([6]) ?
'WRONGPASS invalid username-password pair or user is disabled.' :
'ERR invalid password'
@@ -40,17 +50,8 @@ describe('Client', () => {
describe('legacyMode', () => {
const client = RedisClient.create({
socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN],
modules: {
testModule: {
echo: {
transformArguments(message: string): Array<string> {
return ['ECHO', message];
},
transformReply(reply: string): string {
return reply;
}
}
}
scripts: {
square: SQUARE_SCRIPT
},
legacyMode: true
});
@@ -165,54 +166,8 @@ describe('Client', () => {
);
});
it('client.testModule.echo should call the callback', done => {
(client as any).testModule.echo('message', (err?: Error, reply?: string) => {
if (err) {
return done(err);
}
try {
assert.deepEqual(reply, 'message');
done();
} catch (err) {
done(err);
}
});
});
it('client.v4.testModule.echo should return a promise', async () => {
assert.equal(
await (client as any).v4.testModule.echo('message'),
'message'
);
});
it('client.multi.testModule.echo.v4.testModule.echo.exec should call the callback', done => {
(client as any).multi()
.testModule.echo('message')
.v4.testModule.echo('message')
.exec((err?: Error, replies?: Array<string>) => {
if (err) {
return done(err);
}
try {
assert.deepEqual(replies, ['message', 'message']);
done();
} catch (err) {
done(err);
}
});
});
it('client.multi.testModule.echo.v4.testModule.echo.v4.exec should return a promise', async () => {
assert.deepEqual(
await ((client as any).multi()
.testModule.echo('message')
.v4.testModule.echo('message')
.v4.exec()),
['message', 'message']
);
it('client.{script} should return a promise', async () => {
assert.equal(await client.square(2), 4);
});
});
@@ -296,18 +251,7 @@ describe('Client', () => {
it('with script', async () => {
const client = RedisClient.create({
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 0,
SCRIPT: 'return ARGV[1] + 1;',
transformArguments(number: number): Array<string> {
assert.equal(number, 1);
return [number.toString()];
},
transformReply(reply: number): number {
assert.equal(reply, 2);
return reply;
}
})
square: SQUARE_SCRIPT
}
});
@@ -316,9 +260,9 @@ describe('Client', () => {
try {
assert.deepEqual(
await client.multi()
.add(1)
.square(2)
.exec(),
[2]
[4]
);
} finally {
await client.disconnect();
@@ -329,18 +273,7 @@ describe('Client', () => {
it('scripts', async () => {
const client = RedisClient.create({
scripts: {
add: defineScript({
NUMBER_OF_KEYS: 0,
SCRIPT: 'return ARGV[1] + 1;',
transformArguments(number: number): Array<string> {
assert.equal(number, 1);
return [number.toString()];
},
transformReply(reply: number): number {
assert.equal(reply, 2);
return reply;
}
})
square: SQUARE_SCRIPT
}
});
@@ -348,8 +281,8 @@ describe('Client', () => {
try {
assert.equal(
await client.add(1),
2
await client.square(2),
4
);
} finally {
await client.disconnect();

View File

@@ -9,6 +9,7 @@ import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { ScanOptions, ZMember } from './commands/generic-transformers';
import { ScanCommandOptions } from './commands/SCAN';
import { HScanTuple } from './commands/HSCAN';
import { extendWithDefaultCommands, extendWithModulesAndScripts, transformCommandArguments } from './commander';
export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> {
socket?: RedisSocketOptions;
@@ -47,18 +48,54 @@ export interface ClientCommandOptions extends QueueCommandOptions {
}
export default class RedisClient<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> extends EventEmitter {
static create<M extends RedisModules, S extends RedisLuaScripts>(options?: RedisClientOptions<M, S>): RedisClientType<M, S> {
return <any>new RedisClient<M, S>(options);
}
static commandOptions(options: ClientCommandOptions): CommandOptions<ClientCommandOptions> {
return commandOptions(options);
}
static async commandsExecutor(
this: RedisClient,
command: RedisCommand,
args: Array<unknown>
): Promise<ReturnType<typeof command['transformReply']>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(command, args);
const reply = command.transformReply(
await this.#sendCommand(redisArgs, options),
redisArgs.preserve
);
return reply;
}
static async #scriptsExecutor(
this: RedisClient,
script: RedisLuaScript,
args: Array<unknown>
): Promise<typeof script['transformArguments']> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(script, args);
const reply = script.transformReply(
await this.executeScript(script, redisArgs, options),
redisArgs.preserve
);
return reply;
}
static create<M extends RedisModules, S extends RedisLuaScripts>(options?: RedisClientOptions<M, S>): RedisClientType<M, S> {
return new (<any>extendWithModulesAndScripts({
BaseClass: RedisClient,
modules: options?.modules,
modulesCommandsExecutor: RedisClient.commandsExecutor,
scripts: options?.scripts,
scriptsExecutor: RedisClient.#scriptsExecutor
}))(options);
}
readonly #options?: RedisClientOptions<M, S>;
readonly #socket: RedisSocket;
readonly #queue: RedisCommandsQueue;
readonly #Multi: typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> };
readonly #Multi: new (...args: ConstructorParameters<typeof RedisMultiCommand>) => RedisMultiCommandType<M, S>;
readonly #v4: Record<string, any> = {};
#selectedDB = 0;
@@ -83,9 +120,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
this.#options = options;
this.#socket = this.#initiateSocket();
this.#queue = this.#initiateQueue();
this.#Multi = this.#initiateMulti();
this.#initiateModules();
this.#initiateScripts();
this.#Multi = RedisMultiCommand.extend(options);
this.#legacyMode();
}
@@ -137,98 +172,14 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
);
}
#initiateMulti(): typeof RedisMultiCommand & { new(): RedisMultiCommandType<M, S> } {
const executor = async (commands: Array<MultiQueuedCommand>): Promise<Array<RedisReply>> => {
const promise = Promise.all(
commands.map(({encodedCommand}) => {
return this.#queue.addEncodedCommand(encodedCommand);
})
);
this.#tick();
return await promise;
};
const options = this.#options;
return <any>class extends RedisMultiCommand {
constructor() {
super(executor, options);
}
};
}
#initiateModules(): void {
if (!this.#options?.modules) return;
for (const [moduleName, commands] of Object.entries(this.#options.modules)) {
const module: {
[P in keyof typeof commands]: RedisCommandSignature<(typeof commands)[P]>;
} = {};
for (const [commandName, command] of Object.entries(commands)) {
module[commandName] = (...args) => this.executeCommand(command, args);
}
(this as any)[moduleName] = module;
}
}
#initiateScripts(): void {
if (!this.#options?.scripts) return;
for (const [name, script] of Object.entries(this.#options.scripts)) {
(this as any)[name] = async function (...args: Parameters<typeof script.transformArguments>): Promise<ReturnType<typeof script.transformReply>> {
let options;
if (isCommandOptions<ClientCommandOptions>(args[0])) {
options = args[0];
args = args.slice(1);
}
const transformedArguments = script.transformArguments(...args);
return script.transformReply(
await this.executeScript(
script,
transformedArguments,
options
),
transformedArguments.preserve
);
};
}
}
async executeScript<S extends RedisLuaScript>(script: S, args: Array<string>, options?: ClientCommandOptions): Promise<ReturnType<S['transformReply']>> {
try {
return await this.#sendCommand([
'EVALSHA',
script.SHA,
script.NUMBER_OF_KEYS.toString(),
...args
], options);
} catch (err: any) {
if (!err?.message?.startsWith?.('NOSCRIPT')) {
throw err;
}
return await this.#sendCommand([
'EVAL',
script.SCRIPT,
script.NUMBER_OF_KEYS.toString(),
...args
], options);
}
}
#legacyMode(): void {
if (!this.#options?.legacyMode) return;
(this as any).#v4.sendCommand = this.sendCommand.bind(this);
(this as any).#v4.sendCommand = this.#sendCommand.bind(this);
(this as any).sendCommand = (...args: Array<unknown>): void => {
const options = isCommandOptions<ClientCommandOptions>(args[0]) ? args[0] : undefined,
callback = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as Function : undefined,
actualArgs = !options && !callback ? args : args.slice(options ? 1 : 0, callback ? -1 : Infinity);
this.#sendCommand(actualArgs.flat() as Array<string>, options)
const callback = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as Function : undefined,
actualArgs = !callback ? args : args.slice(0, -1);
this.#sendCommand(actualArgs.flat() as Array<string>)
.then((reply: unknown) => {
if (!callback) return;
@@ -244,7 +195,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
callback(err);
});
}
};
for (const name of Object.keys(COMMANDS)) {
this.#defineLegacyCommand(name);
@@ -261,39 +212,17 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
this.#defineLegacyCommand('unsubscribe');
this.#defineLegacyCommand('PUNSUBSCRIBE');
this.#defineLegacyCommand('pUnsubscribe');
if (this.#options?.modules) {
for (const [module, commands] of Object.entries(this.#options.modules)) {
for (const name of Object.keys(commands)) {
this.#v4[module] = {};
this.#defineLegacyCommand(name, module);
}
}
}
if (this.#options?.scripts) {
for (const name of Object.keys(this.#options.scripts)) {
this.#defineLegacyCommand(name);
}
}
}
#defineLegacyCommand(name: string, moduleName?: string): void {
const handler = (...args: Array<unknown>): void => {
#defineLegacyCommand(name: string): void {
(this as any).#v4[name] = (this as any)[name].bind(this);
(this as any)[name] = (...args: Array<unknown>): void => {
(this as any).sendCommand(name, ...args);
};
if (moduleName) {
(this as any).#v4[moduleName][name] = (this as any)[moduleName][name];
(this as any)[moduleName][name] = handler;
} else {
(this as any).#v4[name] = (this as any)[name].bind(this);
(this as any)[name] = handler;
}
}
duplicate(): RedisClientType<M, S> {
return RedisClient.create(this.#options);
return new (Object.getPrototypeOf(this).constructor)(this.#options);
}
async connect(): Promise<void> {
@@ -354,13 +283,14 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
return this.#sendCommand(args, options);
}
// using `#sendCommand` cause `sendCommand` is overwritten in legacy mode
async #sendCommand<T = unknown>(args: Array<string>, options?: ClientCommandOptions): Promise<T> {
if (options?.duplicateConnection) {
const duplicate = this.duplicate();
await duplicate.connect();
try {
return await duplicate.#sendCommand(args, {
return await duplicate.sendCommand(args, {
...options,
duplicateConnection: false
});
@@ -374,25 +304,45 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
return await promise;
}
async executeCommand(command: RedisCommand, args: Array<unknown>): Promise<unknown> {
let options;
if (isCommandOptions<ClientCommandOptions>(args[0])) {
options = args[0];
args = args.slice(1);
async executeScript(script: RedisLuaScript, args: Array<string>, options?: ClientCommandOptions): Promise<ReturnType<typeof script['transformReply']>> {
try {
return await this.#sendCommand([
'EVALSHA',
script.SHA,
script.NUMBER_OF_KEYS.toString(),
...args
], options);
} catch (err: any) {
if (!err?.message?.startsWith?.('NOSCRIPT')) {
throw err;
}
const transformedArguments = command.transformArguments(...args);
return command.transformReply(
await this.#sendCommand(
transformedArguments,
options
),
transformedArguments.preserve
return await this.#sendCommand([
'EVAL',
script.SCRIPT,
script.NUMBER_OF_KEYS.toString(),
...args
], options);
}
}
async #multiExecutor(commands: Array<MultiQueuedCommand>): Promise<Array<RedisReply>> {
const promise = Promise.all(
commands.map(({encodedCommand}) => {
return this.#queue.addEncodedCommand(encodedCommand);
})
);
this.#tick();
return await promise;
}
multi(): RedisMultiCommandType<M, S> {
return new this.#Multi();
return new this.#Multi(
this.#multiExecutor.bind(this),
this.#options
);
}
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> {
@@ -470,8 +420,4 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
}
}
for (const [name, command] of Object.entries(COMMANDS)) {
(RedisClient.prototype as any)[name] = async function (this: RedisClient, ...args: Array<unknown>): Promise<unknown> {
return this.executeCommand(command, args);
};
}
extendWithDefaultCommands(RedisClient, RedisClient.commandsExecutor);

View File

@@ -1,10 +1,10 @@
import COMMANDS from './commands';
import { RedisCommand, RedisModules } from './commands';
import { ClientCommandOptions, RedisClientType, RedisCommandSignature, WithPlugins } from './client';
import { ClientCommandOptions, RedisClientType, WithPlugins } from './client';
import { RedisSocketOptions } from './socket';
import RedisClusterSlots, { ClusterNode } from './cluster-slots';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { commandOptions, CommandOptions, isCommandOptions } from './command-options';
import { commandOptions, CommandOptions } from './command-options';
import { extendWithModulesAndScripts, extendWithDefaultCommands, transformCommandArguments } from './commander';
export interface RedisClusterOptions<M = RedisModules, S = RedisLuaScripts> {
rootNodes: Array<RedisSocketOptions>;
@@ -28,8 +28,54 @@ export default class RedisCluster<M extends RedisModules = RedisModules, S exten
return commandOrScript.FIRST_KEY_INDEX(...originalArgs);
}
static create<M extends RedisModules, S extends RedisLuaScripts>(options: RedisClusterOptions): RedisClusterType<M, S> {
return <any>new RedisCluster(options);
static async commandsExecutor(
this: RedisCluster,
command: RedisCommand,
args: Array<unknown>
): Promise<ReturnType<typeof command['transformReply']>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(command, args);
const reply = command.transformReply(
await this.sendCommand(
RedisCluster.#extractFirstKey(command, args, redisArgs),
command.IS_READ_ONLY,
redisArgs,
options
),
redisArgs.preserve
);
return reply;
}
static async #scriptsExecutor(
this: RedisCluster,
script: RedisLuaScript,
args: Array<unknown>
): Promise<typeof script['transformArguments']> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(script, args);
const reply = script.transformReply(
await this.executeScript(
script,
args,
redisArgs,
options
),
redisArgs.preserve
);
return reply;
}
static create<M extends RedisModules, S extends RedisLuaScripts>(options?: RedisClusterOptions<M, S>): RedisClusterType<M, S> {
return new (<any>extendWithModulesAndScripts({
BaseClass: RedisCluster,
modules: options?.modules,
modulesCommandsExecutor: RedisCluster.commandsExecutor,
scripts: options?.scripts,
scriptsExecutor: RedisCluster.#scriptsExecutor
}))(options);
}
static commandOptions(options: ClientCommandOptions): CommandOptions<ClientCommandOptions> {
@@ -42,49 +88,6 @@ export default class RedisCluster<M extends RedisModules = RedisModules, S exten
constructor(options: RedisClusterOptions<M, S>) {
this.#options = options;
this.#slots = new RedisClusterSlots(options);
this.#initiateModules();
this.#initiateScripts();
}
#initiateModules(): void {
if (!this.#options.modules) return;
for (const [moduleName, commands] of Object.entries(this.#options.modules)) {
const module: {
[P in keyof typeof commands]: RedisCommandSignature<(typeof commands)[P]>;
} = {};
for (const [commandName, command] of Object.entries(commands)) {
module[commandName] = (...args) => this.executeCommand(command, args);
}
(this as any)[moduleName] = module;
}
}
#initiateScripts(): void {
if (!this.#options.scripts) return;
for (const [name, script] of Object.entries(this.#options.scripts)) {
(this as any)[name] = async function (...args: Parameters<typeof script.transformArguments>): Promise<ReturnType<typeof script.transformReply>> {
let options;
if (isCommandOptions<ClientCommandOptions>(args[0])) {
options = args[0];
args = args.slice(1);
}
const transformedArguments = script.transformArguments(...args);
return script.transformReply(
await this.executeScript(
script,
args,
transformedArguments,
options
),
transformedArguments.preserve
);
};
}
}
async connect(): Promise<void> {
@@ -114,32 +117,13 @@ export default class RedisCluster<M extends RedisModules = RedisModules, S exten
}
}
async executeCommand(command: RedisCommand, args: Array<unknown>): Promise<(typeof command)['transformReply']> {
let options;
if (isCommandOptions<ClientCommandOptions>(args[0])) {
options = args[0];
args = args.slice(1);
}
const redisArgs = command.transformArguments(...args);
return command.transformReply(
await this.sendCommand(
RedisCluster.#extractFirstKey(command, args, redisArgs),
command.IS_READ_ONLY,
redisArgs,
options
),
redisArgs.preserve
);
}
async executeScript<S extends RedisLuaScript>(
script: S,
async executeScript(
script: RedisLuaScript,
originalArgs: Array<unknown>,
redisArgs: Array<string>,
options?: ClientCommandOptions,
redirections = 0
): Promise<ReturnType<S['transformReply']>> {
): Promise<ReturnType<typeof script['transformReply']>> {
const client = this.#slots.getClient(
RedisCluster.#extractFirstKey(script, originalArgs, redisArgs),
script.IS_READ_ONLY
@@ -199,8 +183,5 @@ export default class RedisCluster<M extends RedisModules = RedisModules, S exten
}
}
for (const [name, command] of Object.entries(COMMANDS)) {
(RedisCluster.prototype as any)[name] = function (this: RedisCluster, ...args: Array<unknown>) {
return this.executeCommand(command, args);
};
}
extendWithDefaultCommands(RedisCluster, RedisCluster.commandsExecutor);

88
lib/commander.ts Normal file
View File

@@ -0,0 +1,88 @@
import COMMANDS, { RedisCommand, RedisModules, TransformArgumentsReply } from './commands';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { CommandOptions, isCommandOptions } from './command-options';
type Instantiable<T = any> = new(...args: Array<any>) => T;
type CommandExecutor<T extends Instantiable = Instantiable> = (this: InstanceType<T>, command: RedisCommand, args: Array<unknown>) => unknown;
export function extendWithDefaultCommands<T extends Instantiable>(BaseClass: T, executor: CommandExecutor<T>): void {
for (const [name, command] of Object.entries(COMMANDS)) {
BaseClass.prototype[name] = function (...args: Array<unknown>): unknown {
return executor.call(this, command, args);
};
}
}
interface ExtendWithModulesAndScriptsConfig<
T extends Instantiable,
M extends RedisModules,
S extends RedisLuaScripts
> {
BaseClass: T;
modules: M | undefined;
modulesCommandsExecutor: CommandExecutor<T>;
scripts: S | undefined;
scriptsExecutor(this: InstanceType<T>, script: RedisLuaScript, args: Array<unknown>): unknown;
}
export function extendWithModulesAndScripts<
T extends Instantiable,
M extends RedisModules,
S extends RedisLuaScripts,
>(config: ExtendWithModulesAndScriptsConfig<T, M, S>): T {
let Commander: T | undefined,
modulesBaseObject: Record<string, any>;
if (config.modules) {
modulesBaseObject = Object.create(null);
Commander = class extends config.BaseClass {
constructor(...args: Array<any>) {
super(...args);
modulesBaseObject.self = this;
}
};
for (const [moduleName, module] of Object.entries(config.modules)) {
Commander.prototype[moduleName] = Object.create(modulesBaseObject);
for (const [commandName, command] of Object.entries(module)) {
Commander.prototype[moduleName][commandName] = function (...args: Array<unknown>): unknown {
return config.modulesCommandsExecutor.call(this.self, command, args);
};
}
}
}
if (config.scripts) {
Commander ??= class extends config.BaseClass {};
for (const [name, script] of Object.entries(config.scripts)) {
Commander.prototype[name] = function (...args: Array<unknown>): unknown {
return config.scriptsExecutor.call(this, script, args);
};
}
}
return (Commander ?? config.BaseClass) as any;
}
export function transformCommandArguments<T = unknown>(
command: RedisCommand,
args: Array<unknown>
): {
args: TransformArgumentsReply;
options: CommandOptions<T> | undefined;
} {
let options;
if (isCommandOptions<T>(args[0])) {
options = args[0];
args = args.slice(1);
}
return {
args: command.transformArguments(...args),
options
};
}

View File

@@ -1,8 +1,9 @@
import COMMANDS, { TransformArgumentsReply } from './commands';
import { RedisCommand, RedisModules, RedisReply } from './commands';
import RedisCommandsQueue from './commands-queue';
import { RedisLuaScripts } from './lua-script';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { RedisClientOptions } from './client';
import { extendWithModulesAndScripts, extendWithDefaultCommands } from './commander';
type RedisMultiCommandSignature<C extends RedisCommand, M extends RedisModules, S extends RedisLuaScripts> = (...args: Parameters<C['transformArguments']>) => RedisMultiCommandType<M, S>;
@@ -31,10 +32,62 @@ export interface MultiQueuedCommand {
export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId?: symbol) => Promise<Array<RedisReply>>;
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
static create<M extends RedisModules, S extends RedisLuaScripts>(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>): RedisMultiCommandType<M, S> {
return <any>new RedisMultiCommand<M, S>(executor, clientOptions);
static commandsExecutor(this: RedisMultiCommand, command: RedisCommand, args: Array<unknown>): RedisMultiCommand {
return this.addCommand(
command.transformArguments(...args),
command.transformReply
);
}
static #scriptsExecutor(
this: RedisMultiCommand,
script: RedisLuaScript,
args: Array<unknown>
): RedisMultiCommand {
const transformedArguments: TransformArgumentsReply = [];
if (this.#scriptsInUse.has(script.SHA)) {
transformedArguments.push(
'EVALSHA',
script.SHA
);
} else {
this.#scriptsInUse.add(script.SHA);
transformedArguments.push(
'EVAL',
script.SCRIPT
);
}
transformedArguments.push(script.NUMBER_OF_KEYS.toString());
const scriptArguments = script.transformArguments(...args);
transformedArguments.push(...scriptArguments);
transformedArguments.preserve = scriptArguments.preserve;
return this.addCommand(
transformedArguments,
script.transformReply
);
}
static extend<M extends RedisModules, S extends RedisLuaScripts>(
clientOptions?: RedisClientOptions<M, S>
): new (...args: ConstructorParameters<typeof RedisMultiCommand>) => RedisMultiCommandType<M, S> {
return <any>extendWithModulesAndScripts({
BaseClass: RedisMultiCommand,
modules: clientOptions?.modules,
modulesCommandsExecutor: RedisMultiCommand.commandsExecutor,
scripts: clientOptions?.scripts,
scriptsExecutor: RedisMultiCommand.#scriptsExecutor
});
}
static create<M extends RedisModules, S extends RedisLuaScripts>(
executor: RedisMultiExecutor,
clientOptions?: RedisClientOptions<M, S>
): RedisMultiCommandType<M, S> {
return <any>new this(executor, clientOptions);
}
readonly #executor: RedisMultiExecutor;
readonly #clientOptions: RedisClientOptions<M, S> | undefined;
@@ -56,65 +109,20 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
this.#executor = executor;
this.#clientOptions = clientOptions;
this.#initiateModules();
this.#initiateScripts();
this.#legacyMode();
}
#initiateModules(): void {
if (!this.#clientOptions?.modules) return;
for (const [moduleName, commands] of Object.entries(this.#clientOptions.modules)) {
const module: {
[P in keyof typeof commands]: RedisMultiCommandSignature<(typeof commands)[P], M, S>
} = {};
for (const [commandName, command] of Object.entries(commands)) {
module[commandName] = (...args) => this.executeCommand(command, args);
}
(this as any)[moduleName] = module;
}
}
#initiateScripts(): void {
if (!this.#clientOptions?.scripts) return;
for (const [name, script] of Object.entries(this.#clientOptions.scripts)) {
(this as any)[name] = function (...args: Array<unknown>) {
const transformedArgs: TransformArgumentsReply = [];
if (this.#scriptsInUse.has(name)) {
transformedArgs.push(
'EVALSHA',
script.SHA
);
} else {
this.#scriptsInUse.add(name);
transformedArgs.push(
'EVAL',
script.SCRIPT
);
}
transformedArgs.push(script.NUMBER_OF_KEYS.toString());
const scriptArgs = script.transformArguments(...args);
transformedArgs.push(...scriptArgs);
transformedArgs.preserve = scriptArgs.preserve;
return this.addCommand(
transformedArgs,
script.transformReply
);
};
}
}
#legacyMode(): void {
if (!this.#clientOptions?.legacyMode) return;
this.#v4.addCommand = this.addCommand.bind(this);
(this as any).addCommand = (...args: Array<unknown>): this => {
this.#queue.push({
encodedCommand: RedisCommandsQueue.encodeCommand(args.flat() as Array<string>)
});
return this;
}
this.#v4.exec = this.exec.bind(this);
(this as any).exec = (callback?: (err: Error | null, replies?: Array<unknown>) => unknown): void => {
this.#v4.exec()
.then((reply: Array<unknown>) => {
@@ -135,55 +143,21 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
for (const name of Object.keys(COMMANDS)) {
this.#defineLegacyCommand(name);
}
if (this.#clientOptions?.modules) {
for (const [module, commands] of Object.entries(this.#clientOptions.modules)) {
for (const name of Object.keys(commands)) {
this.#v4[module] = {};
this.#defineLegacyCommand(name, module);
}
}
}
if (this.#clientOptions.scripts) {
for (const name of Object.keys(this.#clientOptions.scripts)) {
this.#defineLegacyCommand(name);
}
}
#defineLegacyCommand(name: string): void {
(this as any).#v4[name] = (this as any)[name].bind(this.#v4);
(this as any)[name] = (...args: Array<unknown>): void => (this as any).addCommand(name, args);
}
#defineLegacyCommand(name: string, moduleName?: string): void {
const handler = (...args: Array<unknown>): RedisMultiCommandType<M, S> => {
return this.addCommand([
name,
...args.flat() as Array<string>
]);
};
if (moduleName) {
this.#v4[moduleName][name] = (this as any)[moduleName][name];
(this as any)[moduleName][name] = handler;
} else {
this.#v4[name] = (this as any)[name].bind(this);
(this as any)[name] = handler;
}
}
addCommand(args: TransformArgumentsReply, transformReply?: RedisCommand['transformReply']): RedisMultiCommandType<M, S> {
addCommand(args: TransformArgumentsReply, transformReply?: RedisCommand['transformReply']): this {
this.#queue.push({
encodedCommand: RedisCommandsQueue.encodeCommand(args),
preservedArguments: args.preserve,
transformReply
});
return <any>this;
}
executeCommand(command: RedisCommand, args: Array<unknown>): RedisMultiCommandType<M, S> {
return this.addCommand(
command.transformArguments(...args),
command.transformReply
);
return this;
}
async exec(execAsPipeline = false): Promise<Array<unknown>> {
@@ -217,8 +191,4 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
}
}
for (const [name, command] of Object.entries(COMMANDS)) {
(RedisMultiCommand.prototype as any)[name] = function (...args: Array<unknown>): RedisMultiCommand {
return this.executeCommand(command, args);
};
}
extendWithDefaultCommands(RedisMultiCommand, RedisMultiCommand.commandsExecutor);