1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-07 13:22:56 +03:00
This commit is contained in:
Leibale
2023-04-23 07:56:15 -04:00
parent 35d32e5b64
commit 9faa3e77c4
326 changed files with 14620 additions and 10931 deletions

View File

@@ -1,87 +1,60 @@
import { fCallArguments } from './commander';
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisFunction, RedisScript } from './commands';
import { Command, CommandArguments, RedisScript, TransformReply } from './RESP/types';
import { WatchError } from './errors';
export interface RedisMultiQueuedCommand {
args: RedisCommandArguments;
transformReply?: RedisCommand['transformReply'];
args: CommandArguments;
transformReply?: TransformReply;
}
export default class RedisMultiCommand {
static generateChainId(): symbol {
return Symbol('RedisMultiCommand Chain Id');
static generateChainId(): symbol {
return Symbol('RedisMultiCommand Chain Id');
}
readonly queue: Array<RedisMultiQueuedCommand> = [];
readonly scriptsInUse = new Set<string>();
addCommand(args: CommandArguments, transformReply?: TransformReply) {
this.queue.push({
args,
transformReply
});
return this;
}
addScript(script: RedisScript, args: CommandArguments, transformReply?: TransformReply) {
const redisArgs: CommandArguments = [];
if (this.scriptsInUse.has(script.SHA1)) {
redisArgs.push('EVALSHA', script.SHA1);
} else {
this.scriptsInUse.add(script.SHA1);
redisArgs.push('EVAL', script.SCRIPT);
}
readonly queue: Array<RedisMultiQueuedCommand> = [];
readonly scriptsInUse = new Set<string>();
addCommand(args: RedisCommandArguments, transformReply?: RedisCommand['transformReply']): void {
this.queue.push({
args,
transformReply
});
if (script.NUMBER_OF_KEYS !== undefined) {
redisArgs.push(script.NUMBER_OF_KEYS.toString());
}
addFunction(name: string, fn: RedisFunction, args: Array<unknown>): RedisCommandArguments {
const transformedArguments = fCallArguments(
name,
fn,
fn.transformArguments(...args)
);
this.queue.push({
args: transformedArguments,
transformReply: fn.transformReply
});
return transformedArguments;
redisArgs.push(...args);
redisArgs.preserve = args.preserve;
return this.addCommand(redisArgs, transformReply);
}
handleExecReplies(rawReplies: Array<unknown>): Array<unknown> {
const execReply = rawReplies[rawReplies.length - 1] as (null | Array<unknown>);
if (execReply === null) {
throw new WatchError();
}
addScript(script: RedisScript, args: Array<unknown>): RedisCommandArguments {
const transformedArguments: RedisCommandArguments = [];
if (this.scriptsInUse.has(script.SHA1)) {
transformedArguments.push(
'EVALSHA',
script.SHA1
);
} else {
this.scriptsInUse.add(script.SHA1);
transformedArguments.push(
'EVAL',
script.SCRIPT
);
}
return this.transformReplies(execReply);
}
if (script.NUMBER_OF_KEYS !== undefined) {
transformedArguments.push(script.NUMBER_OF_KEYS.toString());
}
const scriptArguments = script.transformArguments(...args);
transformedArguments.push(...scriptArguments);
if (scriptArguments.preserve) {
transformedArguments.preserve = scriptArguments.preserve;
}
this.addCommand(
transformedArguments,
script.transformReply
);
return transformedArguments;
}
handleExecReplies(rawReplies: Array<RedisCommandRawReply>): Array<RedisCommandRawReply> {
const execReply = rawReplies[rawReplies.length - 1] as (null | Array<RedisCommandRawReply>);
if (execReply === null) {
throw new WatchError();
}
return this.transformReplies(execReply);
}
transformReplies(rawReplies: Array<RedisCommandRawReply>): Array<RedisCommandRawReply> {
return rawReplies.map((reply, i) => {
const { transformReply, args } = this.queue[i];
return transformReply ? transformReply(reply, args.preserve) : reply;
});
}
transformReplies(rawReplies: Array<unknown>): Array<unknown> {
return rawReplies.map((reply, i) => {
const { transformReply, args } = this.queue[i];
return transformReply ? transformReply(reply, args.preserve) : reply;
});
}
}