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 { defineScript } from './lua-script';
import { spy } from 'sinon'; 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('Client', () => {
describe('authentication', () => { describe('authentication', () => {
itWithClient(TestRedisServers.PASSWORD, 'Client should be authenticated', async client => { itWithClient(TestRedisServers.PASSWORD, 'Client should be authenticated', async client => {
@@ -26,7 +37,6 @@ describe('Client', () => {
await assert.rejects( await assert.rejects(
client.connect(), client.connect(),
{ {
message: isRedisVersionGreaterThan([6]) ? message: isRedisVersionGreaterThan([6]) ?
'WRONGPASS invalid username-password pair or user is disabled.' : 'WRONGPASS invalid username-password pair or user is disabled.' :
'ERR invalid password' 'ERR invalid password'
@@ -40,17 +50,8 @@ describe('Client', () => {
describe('legacyMode', () => { describe('legacyMode', () => {
const client = RedisClient.create({ const client = RedisClient.create({
socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN], socket: TEST_REDIS_SERVERS[TestRedisServers.OPEN],
modules: { scripts: {
testModule: { square: SQUARE_SCRIPT
echo: {
transformArguments(message: string): Array<string> {
return ['ECHO', message];
},
transformReply(reply: string): string {
return reply;
}
}
}
}, },
legacyMode: true legacyMode: true
}); });
@@ -164,55 +165,9 @@ describe('Client', () => {
['PONG', 'PONG'] ['PONG', 'PONG']
); );
}); });
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 { it('client.{script} should return a promise', async () => {
assert.deepEqual(reply, 'message'); assert.equal(await client.square(2), 4);
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']
);
}); });
}); });
@@ -296,51 +251,29 @@ describe('Client', () => {
it('with script', async () => { it('with script', async () => {
const client = RedisClient.create({ const client = RedisClient.create({
scripts: { scripts: {
add: defineScript({ square: SQUARE_SCRIPT
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;
}
})
} }
}); });
await client.connect(); await client.connect();
try { try {
assert.deepEqual( assert.deepEqual(
await client.multi() await client.multi()
.add(1) .square(2)
.exec(), .exec(),
[2] [4]
); );
} finally { } finally {
await client.disconnect(); await client.disconnect();
} }
}); });
}); });
it('scripts', async () => { it('scripts', async () => {
const client = RedisClient.create({ const client = RedisClient.create({
scripts: { scripts: {
add: defineScript({ square: SQUARE_SCRIPT
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;
}
})
} }
}); });
@@ -348,8 +281,8 @@ describe('Client', () => {
try { try {
assert.equal( assert.equal(
await client.add(1), await client.square(2),
2 4
); );
} finally { } finally {
await client.disconnect(); await client.disconnect();
@@ -514,7 +447,7 @@ describe('Client', () => {
await subscriber.pUnsubscribe(); await subscriber.pUnsubscribe();
await publisher.publish('channel', 'message'); await publisher.publish('channel', 'message');
assert.ok(channelListener1.calledOnce); assert.ok(channelListener1.calledOnce);
assert.ok(channelListener2.calledTwice); assert.ok(channelListener2.calledTwice);
assert.ok(patternListener.calledThrice); assert.ok(patternListener.calledThrice);

View File

@@ -9,6 +9,7 @@ import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { ScanOptions, ZMember } from './commands/generic-transformers'; import { ScanOptions, ZMember } from './commands/generic-transformers';
import { ScanCommandOptions } from './commands/SCAN'; import { ScanCommandOptions } from './commands/SCAN';
import { HScanTuple } from './commands/HSCAN'; import { HScanTuple } from './commands/HSCAN';
import { extendWithDefaultCommands, extendWithModulesAndScripts, transformCommandArguments } from './commander';
export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> { export interface RedisClientOptions<M = RedisModules, S = RedisLuaScripts> {
socket?: RedisSocketOptions; 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 { 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> { static commandOptions(options: ClientCommandOptions): CommandOptions<ClientCommandOptions> {
return commandOptions(options); 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 #options?: RedisClientOptions<M, S>;
readonly #socket: RedisSocket; readonly #socket: RedisSocket;
readonly #queue: RedisCommandsQueue; 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> = {}; readonly #v4: Record<string, any> = {};
#selectedDB = 0; #selectedDB = 0;
@@ -83,9 +120,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
this.#options = options; this.#options = options;
this.#socket = this.#initiateSocket(); this.#socket = this.#initiateSocket();
this.#queue = this.#initiateQueue(); this.#queue = this.#initiateQueue();
this.#Multi = this.#initiateMulti(); this.#Multi = RedisMultiCommand.extend(options);
this.#initiateModules();
this.#initiateScripts();
this.#legacyMode(); 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 { #legacyMode(): void {
if (!this.#options?.legacyMode) return; 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 => { (this as any).sendCommand = (...args: Array<unknown>): void => {
const options = isCommandOptions<ClientCommandOptions>(args[0]) ? args[0] : undefined, const callback = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as Function : undefined,
callback = typeof args[args.length - 1] === 'function' ? args[args.length - 1] as Function : undefined, actualArgs = !callback ? args : args.slice(0, -1);
actualArgs = !options && !callback ? args : args.slice(options ? 1 : 0, callback ? -1 : Infinity); this.#sendCommand(actualArgs.flat() as Array<string>)
this.#sendCommand(actualArgs.flat() as Array<string>, options)
.then((reply: unknown) => { .then((reply: unknown) => {
if (!callback) return; if (!callback) return;
@@ -244,7 +195,7 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
callback(err); callback(err);
}); });
} };
for (const name of Object.keys(COMMANDS)) { for (const name of Object.keys(COMMANDS)) {
this.#defineLegacyCommand(name); this.#defineLegacyCommand(name);
@@ -261,39 +212,17 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
this.#defineLegacyCommand('unsubscribe'); this.#defineLegacyCommand('unsubscribe');
this.#defineLegacyCommand('PUNSUBSCRIBE'); this.#defineLegacyCommand('PUNSUBSCRIBE');
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 { #defineLegacyCommand(name: string): void {
const handler = (...args: Array<unknown>): 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); (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> { duplicate(): RedisClientType<M, S> {
return RedisClient.create(this.#options); return new (Object.getPrototypeOf(this).constructor)(this.#options);
} }
async connect(): Promise<void> { async connect(): Promise<void> {
@@ -354,13 +283,14 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
return this.#sendCommand(args, options); 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> { async #sendCommand<T = unknown>(args: Array<string>, options?: ClientCommandOptions): Promise<T> {
if (options?.duplicateConnection) { if (options?.duplicateConnection) {
const duplicate = this.duplicate(); const duplicate = this.duplicate();
await duplicate.connect(); await duplicate.connect();
try { try {
return await duplicate.#sendCommand(args, { return await duplicate.sendCommand(args, {
...options, ...options,
duplicateConnection: false duplicateConnection: false
}); });
@@ -374,25 +304,45 @@ export default class RedisClient<M extends RedisModules = RedisModules, S extend
return await promise; return await promise;
} }
async executeCommand(command: RedisCommand, args: Array<unknown>): Promise<unknown> { async executeScript(script: RedisLuaScript, args: Array<string>, options?: ClientCommandOptions): Promise<ReturnType<typeof script['transformReply']>> {
let options; try {
if (isCommandOptions<ClientCommandOptions>(args[0])) { return await this.#sendCommand([
options = args[0]; 'EVALSHA',
args = args.slice(1); 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 await this.#sendCommand([
return command.transformReply( 'EVAL',
await this.#sendCommand( script.SCRIPT,
transformedArguments, script.NUMBER_OF_KEYS.toString(),
options ...args
), ], options);
transformedArguments.preserve }
}
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> { multi(): RedisMultiCommandType<M, S> {
return new this.#Multi(); return new this.#Multi(
this.#multiExecutor.bind(this),
this.#options
);
} }
async* scanIterator(options?: ScanCommandOptions): AsyncIterable<string> { 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)) { extendWithDefaultCommands(RedisClient, RedisClient.commandsExecutor);
(RedisClient.prototype as any)[name] = async function (this: RedisClient, ...args: Array<unknown>): Promise<unknown> {
return this.executeCommand(command, args);
};
}

View File

@@ -1,10 +1,10 @@
import COMMANDS from './commands';
import { RedisCommand, RedisModules } 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 { RedisSocketOptions } from './socket';
import RedisClusterSlots, { ClusterNode } from './cluster-slots'; import RedisClusterSlots, { ClusterNode } from './cluster-slots';
import { RedisLuaScript, RedisLuaScripts } from './lua-script'; 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> { export interface RedisClusterOptions<M = RedisModules, S = RedisLuaScripts> {
rootNodes: Array<RedisSocketOptions>; rootNodes: Array<RedisSocketOptions>;
@@ -28,8 +28,54 @@ export default class RedisCluster<M extends RedisModules = RedisModules, S exten
return commandOrScript.FIRST_KEY_INDEX(...originalArgs); return commandOrScript.FIRST_KEY_INDEX(...originalArgs);
} }
static create<M extends RedisModules, S extends RedisLuaScripts>(options: RedisClusterOptions): RedisClusterType<M, S> { static async commandsExecutor(
return <any>new RedisCluster(options); 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> { 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>) { constructor(options: RedisClusterOptions<M, S>) {
this.#options = options; this.#options = options;
this.#slots = new RedisClusterSlots(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> { 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']> { async executeScript(
let options; script: RedisLuaScript,
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,
originalArgs: Array<unknown>, originalArgs: Array<unknown>,
redisArgs: Array<string>, redisArgs: Array<string>,
options?: ClientCommandOptions, options?: ClientCommandOptions,
redirections = 0 redirections = 0
): Promise<ReturnType<S['transformReply']>> { ): Promise<ReturnType<typeof script['transformReply']>> {
const client = this.#slots.getClient( const client = this.#slots.getClient(
RedisCluster.#extractFirstKey(script, originalArgs, redisArgs), RedisCluster.#extractFirstKey(script, originalArgs, redisArgs),
script.IS_READ_ONLY 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)) { extendWithDefaultCommands(RedisCluster, RedisCluster.commandsExecutor);
(RedisCluster.prototype as any)[name] = function (this: RedisCluster, ...args: Array<unknown>) {
return this.executeCommand(command, args);
};
}

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 COMMANDS, { TransformArgumentsReply } from './commands';
import { RedisCommand, RedisModules, RedisReply } from './commands'; import { RedisCommand, RedisModules, RedisReply } from './commands';
import RedisCommandsQueue from './commands-queue'; import RedisCommandsQueue from './commands-queue';
import { RedisLuaScripts } from './lua-script'; import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { RedisClientOptions } from './client'; 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>; 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 type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId?: symbol) => Promise<Array<RedisReply>>;
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> { 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> { static commandsExecutor(this: RedisMultiCommand, command: RedisCommand, args: Array<unknown>): RedisMultiCommand {
return <any>new RedisMultiCommand<M, S>(executor, clientOptions); 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 #executor: RedisMultiExecutor;
readonly #clientOptions: RedisClientOptions<M, S> | undefined; 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>) { constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
this.#executor = executor; this.#executor = executor;
this.#clientOptions = clientOptions; this.#clientOptions = clientOptions;
this.#initiateModules();
this.#initiateScripts();
this.#legacyMode(); 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 { #legacyMode(): void {
if (!this.#clientOptions?.legacyMode) return; 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.#v4.exec = this.exec.bind(this);
(this as any).exec = (callback?: (err: Error | null, replies?: Array<unknown>) => unknown): void => { (this as any).exec = (callback?: (err: Error | null, replies?: Array<unknown>) => unknown): void => {
this.#v4.exec() this.#v4.exec()
.then((reply: Array<unknown>) => { .then((reply: Array<unknown>) => {
@@ -135,55 +143,21 @@ export default class RedisMultiCommand<M extends RedisModules = RedisModules, S
for (const name of Object.keys(COMMANDS)) { for (const name of Object.keys(COMMANDS)) {
this.#defineLegacyCommand(name); 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, moduleName?: string): void { #defineLegacyCommand(name: string): void {
const handler = (...args: Array<unknown>): RedisMultiCommandType<M, S> => { (this as any).#v4[name] = (this as any)[name].bind(this.#v4);
return this.addCommand([ (this as any)[name] = (...args: Array<unknown>): void => (this as any).addCommand(name, args);
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({ this.#queue.push({
encodedCommand: RedisCommandsQueue.encodeCommand(args), encodedCommand: RedisCommandsQueue.encodeCommand(args),
preservedArguments: args.preserve, preservedArguments: args.preserve,
transformReply transformReply
}); });
return <any>this; return this;
}
executeCommand(command: RedisCommand, args: Array<unknown>): RedisMultiCommandType<M, S> {
return this.addCommand(
command.transformArguments(...args),
command.transformReply
);
} }
async exec(execAsPipeline = false): Promise<Array<unknown>> { 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)) { extendWithDefaultCommands(RedisMultiCommand, RedisMultiCommand.commandsExecutor);
(RedisMultiCommand.prototype as any)[name] = function (...args: Array<unknown>): RedisMultiCommand {
return this.executeCommand(command, args);
};
}