1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-11 22:42:42 +03:00

replace callbackify with legacyMode

This commit is contained in:
leibale
2021-06-01 16:51:22 -04:00
parent 4cbcc90bbb
commit f62b68d672
4 changed files with 258 additions and 84 deletions

View File

@@ -2,6 +2,7 @@ import COMMANDS from './commands/client';
import { RedisCommand, RedisModules, RedisReply } from './commands';
import RedisCommandsQueue from './commands-queue';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { RedisClientOptions } from './client';
type RedisMultiCommandSignature<C extends RedisCommand, M extends RedisModules, S extends RedisLuaScripts> = (...args: Parameters<C['transformArguments']>) => RedisMultiCommandType<M, S>;
@@ -29,71 +30,136 @@ export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId: Sym
export default class RedisMultiCommand<M extends RedisModules = RedisModules, S extends RedisLuaScripts = RedisLuaScripts> {
static defineCommand(on: any, name: string, command: RedisCommand): void {
on[name] = function (...args: Parameters<typeof command.transformArguments>) {
return this.addCommand(command.transformArguments(...args), command.transformReply);
// do not return `this.addCommand` directly cause in legacy mode it's binded to the legacy version
this.addCommand(command.transformArguments(...args), command.transformReply);
return this;
};
}
static defineLuaScript(on: any, name: string, script: RedisLuaScript): void {
on[name] = function (...args: Array<unknown>) {
let evalArgs;
if (this.#scriptsInUse.has(name)) {
evalArgs = [
'EVALSHA',
script.SHA
];
} else {
this.#scriptsInUse.add(name);
evalArgs = [
'EVAL',
script.SCRIPT
];
}
return this.addCommand(
[
...evalArgs,
script.NUMBER_OF_KEYS,
...script.transformArguments(...args)
],
script.transformReply
);
};
}
static create<M extends RedisModules, S extends RedisLuaScripts>(executor: RedisMultiExecutor, modules?: M, scripts?: S): RedisMultiCommandType<M, S> {
return <any>new RedisMultiCommand<M, S>(executor, modules, scripts);
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);
}
readonly #executor: RedisMultiExecutor;
readonly #clientOptions: RedisClientOptions<M, S> | undefined;
readonly #queue: Array<MultiQueuedCommand> = [];
readonly #scriptsInUse = new Set<string>();
constructor(executor: RedisMultiExecutor, modules?: RedisModules, scripts?: RedisLuaScripts) {
this.#executor = executor;
this.#initiateModules(modules);
this.#initiateScripts(scripts);
readonly #modern: Record<string, Function> = {};
get modern(): Record<string, Function> {
if (!this.#clientOptions?.legacyMode) {
throw new Error('client is not in "legacy mode"');
}
return this.#modern;
}
#initiateModules(modules?: RedisModules): void {
if (!modules) return;
constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) {
this.#executor = executor;
this.#clientOptions = clientOptions;
this.#initiateModules();
this.#initiateScripts();
this.#legacyMode();
}
for (const m of modules) {
#initiateModules(): void {
if (!this.#clientOptions?.modules) return;
for (const m of this.#clientOptions.modules) {
for (const [name, command] of Object.entries(m)) {
RedisMultiCommand.defineCommand(this, name, command);
}
}
}
#initiateScripts(scripts?: RedisLuaScripts): void {
if (!scripts) return;
#initiateScripts(): void {
if (!this.#clientOptions?.scripts) return;
for (const [name, script] of Object.entries(scripts)) {
RedisMultiCommand.defineLuaScript(this, name, script);
for (const [name, script] of Object.entries(this.#clientOptions.scripts)) {
(this as any)[name] = function (...args: Array<unknown>) {
let evalArgs;
if (this.#scriptsInUse.has(name)) {
evalArgs = [
'EVALSHA',
script.SHA
];
} else {
this.#scriptsInUse.add(name);
evalArgs = [
'EVAL',
script.SCRIPT
];
}
return this.addCommand(
[
...evalArgs,
script.NUMBER_OF_KEYS,
...script.transformArguments(...args)
],
script.transformReply
);
};
}
}
#legacyMode(): Record<string, Function> | undefined {
if (!this.#clientOptions?.legacyMode) return;
this.#modern.exec = this.exec.bind(this);
this.#modern.addCommand = this.addCommand.bind(this);
(this as any).exec = function (...args: Array<unknown>): void {
const callback = typeof args[args.length - 1] === 'function' && args.pop() as Function;
this.#modern.exec()
.then((reply: unknown) => {
if (!callback) return;
callback(null, reply);
})
.catch((err: Error) => {
if (!callback) {
// this.emit('error', err);
return;
}
callback(err);
});
};
for (const name of Object.keys(COMMANDS)) {
this.#defineLegacyCommand(name);
}
if (this.#clientOptions.modules) {
for (const m of this.#clientOptions.modules) {
for (const name of Object.keys(m)) {
this.#defineLegacyCommand(name);
}
}
}
if (this.#clientOptions.scripts) {
for (const name of Object.keys(this.#clientOptions.scripts)) {
this.#defineLegacyCommand(name);
}
}
}
#defineLegacyCommand(name: string): void {
this.#modern[name] = (this as any)[name];
// TODO: https://github.com/NodeRedis/node-redis#commands:~:text=minimal%20parsing
(this as any)[name] = function (...args: Array<unknown>) {
return this.addCommand([name, ...args.flat()]);
};
}
addCommand(args: Array<string>, transformReply?: RedisCommand['transformReply']): this {
this.#queue.push({
encodedCommand: RedisCommandsQueue.encodeCommand(args),