You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
* fix #1906 - implement BITFIELD_RO * initial support for redis functions * fix test utils * redis functions commands and tests * upgrade deps * fix "Property 'uninstall' does not exist on type 'SinonFakeTimers'" * upgrade dockers version * Merge branch 'master' of github.com:redis/node-redis into functions * fix FUNCTION LIST WITHCODE and FUNCTION STATS * upgrade deps * set minimum version for FCALL and FCALL_RO * fix FUNCTION LOAD * FUNCTION LOAD * fix FUNCTION LOAD & FUNCTION LIST & FUNCTION LOAD WITHCODE * fix FUNCTION_LIST_WITHCODE test
99 lines
3.0 KiB
TypeScript
99 lines
3.0 KiB
TypeScript
import { fCallArguments } from './commander';
|
|
import { RedisCommand, RedisCommandArguments, RedisCommandRawReply, RedisFunction, RedisScript } from './commands';
|
|
import { WatchError } from './errors';
|
|
|
|
export interface RedisMultiQueuedCommand {
|
|
args: RedisCommandArguments;
|
|
transformReply?: RedisCommand['transformReply'];
|
|
}
|
|
|
|
export default class RedisMultiCommand {
|
|
static generateChainId(): symbol {
|
|
return Symbol('RedisMultiCommand Chain Id');
|
|
}
|
|
|
|
readonly queue: Array<RedisMultiQueuedCommand> = [];
|
|
|
|
readonly scriptsInUse = new Set<string>();
|
|
|
|
addCommand(args: RedisCommandArguments, transformReply?: RedisCommand['transformReply']): void {
|
|
this.queue.push({
|
|
args,
|
|
transformReply
|
|
});
|
|
}
|
|
|
|
addFunction(fn: RedisFunction, args: Array<unknown>): RedisCommandArguments {
|
|
const transformedArguments = fCallArguments(
|
|
fn,
|
|
fn.transformArguments(...args)
|
|
);
|
|
this.queue.push({
|
|
args: transformedArguments,
|
|
transformReply: fn.transformReply
|
|
});
|
|
return transformedArguments;
|
|
}
|
|
|
|
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
|
|
);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
exec(): undefined | Array<RedisMultiQueuedCommand> {
|
|
if (!this.queue.length) {
|
|
return;
|
|
}
|
|
|
|
return [
|
|
{ args: ['MULTI'] },
|
|
...this.queue,
|
|
{ args: ['EXEC'] }
|
|
];
|
|
}
|
|
|
|
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;
|
|
});
|
|
}
|
|
}
|