Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | 1x 1x 1x 76x 7x 7x 1x 7x 7x 7x 1x 1x 7x 7x 7x 7x 7x 7x 7x 1x 1x 1x 1x 1x 1x 7x 3x 3x 3x 2x 2x 2x 1x 3x 228x 3x 3x 228x 228x 2x 11x 11x 7x 7x 7x 7x 7x 6x 8x 8x 1x 76x | 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>; type WithCommands<M extends RedisModules, S extends RedisLuaScripts> = { [P in keyof typeof COMMANDS]: RedisMultiCommandSignature<(typeof COMMANDS)[P], M, S> }; type WithModules<M extends RedisModules, S extends RedisLuaScripts> = { [P in keyof M[number]]: RedisMultiCommandSignature<M[number][P], M, S> }; type WithScripts<M extends RedisModules, S extends RedisLuaScripts> = { [P in keyof S]: RedisMultiCommandSignature<S[P], M, S> }; export type RedisMultiCommandType<M extends RedisModules, S extends RedisLuaScripts> = RedisMultiCommand & WithCommands<M, S> & WithModules<M, S> & WithScripts<M, S>; export interface MultiQueuedCommand { encodedCommand: string; transformReply?: RedisCommand['transformReply']; } export type RedisMultiExecutor = (queue: Array<MultiQueuedCommand>, chainId: Symbol) => Promise<Array<RedisReply>>; 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>) { // 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 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>(); readonly #modern: Record<string, Function> = {}; get modern(): Record<string, Function> { Iif (!this.#clientOptions?.legacyMode) { throw new Error('client is not in "legacy mode"'); } return this.#modern; } constructor(executor: RedisMultiExecutor, clientOptions?: RedisClientOptions<M, S>) { this.#executor = executor; this.#clientOptions = clientOptions; this.#initiateModules(); this.#initiateScripts(); this.#legacyMode(); } #initiateModules(): void { Eif (!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(): void { if (!this.#clientOptions?.scripts) return; for (const [name, script] of Object.entries(this.#clientOptions.scripts)) { (this as any)[name] = function (...args: Array<unknown>) { let evalArgs; Iif (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); } Iif (this.#clientOptions.modules) { for (const m of this.#clientOptions.modules) { for (const name of Object.keys(m)) { this.#defineLegacyCommand(name); } } } Iif (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), transformReply }); return this; } async exec(): Promise<Array<unknown>> { Iif (!this.#queue.length) { return []; } const queue = this.#queue.splice(0); queue.unshift({ encodedCommand: RedisCommandsQueue.encodeCommand(['MULTI']) }); queue.push({ encodedCommand: RedisCommandsQueue.encodeCommand(['EXEC']) }); const rawReplies = await this.#executor(queue, Symbol('[RedisMultiCommand] Chain ID')); return rawReplies.map((reply, i) => { const { transformReply } = queue[i + 1]; return transformReply ? transformReply(reply) : reply; }); }; } for (const [name, command] of Object.entries(COMMANDS)) { RedisMultiCommand.defineCommand(RedisMultiCommand.prototype, name, command); } |