From 0ab224504961212fe44cf3972c7a2902346e1627 Mon Sep 17 00:00:00 2001 From: leibale Date: Sat, 18 Sep 2021 05:52:54 -0400 Subject: [PATCH] ref #1653 - better types --- lib/client.ts | 164 ++++++++++++++-------------- lib/cluster.ts | 89 +++++++-------- lib/commands/GEOSEARCHSTORE.spec.ts | 2 +- lib/commands/GETEX.ts | 3 +- lib/commands/index.ts | 7 +- lib/lua-script.ts | 4 +- lib/multi-command.ts | 89 ++++++++------- lib/test-utils.ts | 8 +- 8 files changed, 176 insertions(+), 190 deletions(-) diff --git a/lib/client.ts b/lib/client.ts index aaa982da1c..c9e9cecf92 100644 --- a/lib/client.ts +++ b/lib/client.ts @@ -13,7 +13,7 @@ import { encodeCommand, extendWithDefaultCommands, extendWithModulesAndScripts, import { Pool, Options as PoolOptions, createPool } from 'generic-pool'; import { ClientClosedError } from './errors'; -export interface RedisClientOptions { +export interface RedisClientOptions { socket?: RedisSocketOptions; modules?: M; scripts?: S; @@ -43,51 +43,25 @@ type WithScripts = { export type WithPlugins = WithCommands & WithModules & WithScripts; -export type RedisClientType = +export type RedisClientType = WithPlugins & RedisClient; export interface ClientCommandOptions extends QueueCommandOptions { isolated?: boolean; } -export default class RedisClient extends EventEmitter { +export default class RedisClient extends EventEmitter { static commandOptions(options: ClientCommandOptions): CommandOptions { return commandOptions(options); } - static async commandsExecutor( - this: RedisClient, - command: RedisCommand, - args: Array - ): Promise> { - const { args: redisArgs, options } = transformCommandArguments(command, args); - - return command.transformReply( - await this.#sendCommand(redisArgs, options, command.BUFFER_MODE), - redisArgs.preserve, - ); - } - - static async #scriptsExecutor( - this: RedisClient, - script: RedisLuaScript, - args: Array - ): Promise { - const { args: redisArgs, options } = transformCommandArguments(script, args); - - return script.transformReply( - await this.executeScript(script, redisArgs, options, script.BUFFER_MODE), - redisArgs.preserve - ); - } - - static create(options?: RedisClientOptions): RedisClientType { + static create(options?: RedisClientOptions): RedisClientType { const Client = (extendWithModulesAndScripts({ BaseClass: RedisClient, modules: options?.modules, - modulesCommandsExecutor: RedisClient.commandsExecutor, + modulesCommandsExecutor: RedisClient.prototype.commandsExecutor, scripts: options?.scripts, - scriptsExecutor: RedisClient.#scriptsExecutor + scriptsExecutor: RedisClient.prototype.scriptsExecutor })); if (Client !== RedisClient) { @@ -104,7 +78,7 @@ export default class RedisClient = {}; #selectedDB = 0; - get options(): RedisClientOptions | null | undefined { + get options(): RedisClientOptions | undefined { return this.#options; } @@ -240,6 +214,72 @@ export default class RedisClient): Promise> { + const { args: redisArgs, options } = transformCommandArguments(command, args); + + return command.transformReply( + await this.#sendCommand(redisArgs, options, command.BUFFER_MODE), + redisArgs.preserve, + ); + } + + sendCommand(args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise { + return this.#sendCommand(args, options, bufferMode); + } + + // using `#sendCommand` cause `sendCommand` is overwritten in legacy mode + async #sendCommand(args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise { + if (!this.#socket.isOpen) { + throw new ClientClosedError(); + } + + if (options?.isolated) { + return this.executeIsolated(isolatedClient => + isolatedClient.sendCommand(args, { + ...options, + isolated: false + }) + ); + } + + const promise = this.#queue.addCommand(args, options, bufferMode); + this.#tick(); + return await promise; + } + + async scriptsExecutor(script: RedisLuaScript, args: Array): Promise> { + const { args: redisArgs, options } = transformCommandArguments(script, args); + + return script.transformReply( + await this.executeScript(script, redisArgs, options, script.BUFFER_MODE), + redisArgs.preserve + ); + } + + async executeScript(script: RedisLuaScript, args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise> { + try { + return await this.#sendCommand([ + 'EVALSHA', + script.SHA1, + script.NUMBER_OF_KEYS.toString(), + ...args + ], options, bufferMode); + } catch (err: any) { + if (!err?.message?.startsWith?.('NOSCRIPT')) { + throw err; + } + + return await this.#sendCommand([ + 'EVAL', + script.SCRIPT, + script.NUMBER_OF_KEYS.toString(), + ...args + ], options, bufferMode); + } + } + + + async SELECT(db: number): Promise; async SELECT(options: CommandOptions, db: number): Promise; async SELECT(options?: any, db?: any): Promise { @@ -300,30 +340,6 @@ export default class RedisClient(args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise { - return this.#sendCommand(args, options, bufferMode); - } - - // using `#sendCommand` cause `sendCommand` is overwritten in legacy mode - async #sendCommand(args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise { - if (!this.#socket.isOpen) { - throw new ClientClosedError(); - } - - if (options?.isolated) { - return this.executeIsolated(isolatedClient => - isolatedClient.sendCommand(args, { - ...options, - isolated: false - }) - ); - } - - const promise = this.#queue.addCommand(args, options, bufferMode); - this.#tick(); - return await promise; - } - #tick(): void { if (!this.#socket.isSocketExists) { return; @@ -350,26 +366,11 @@ export default class RedisClient> { - try { - return await this.#sendCommand([ - 'EVALSHA', - script.SHA1, - script.NUMBER_OF_KEYS.toString(), - ...args - ], options, bufferMode); - } catch (err: any) { - if (!err?.message?.startsWith?.('NOSCRIPT')) { - throw err; - } - - return await this.#sendCommand([ - 'EVAL', - script.SCRIPT, - script.NUMBER_OF_KEYS.toString(), - ...args - ], options, bufferMode); - } + multi(): RedisMultiCommandType { + return new (this as any).Multi( + this.#multiExecutor.bind(this), + this.#options + ); } #multiExecutor(commands: Array, chainId?: symbol): Promise> { @@ -386,13 +387,6 @@ export default class RedisClient { - return new (this as any).Multi( - this.#multiExecutor.bind(this), - this.#options - ); - } - async* scanIterator(options?: ScanCommandOptions): AsyncIterable { let cursor = 0; do { @@ -451,5 +445,5 @@ export default class RedisClient { maxCommandRedirections?: number; } -export type RedisClusterType = - WithPlugins & RedisCluster; +export type RedisClusterType = + WithPlugins & RedisCluster; -export default class RedisCluster extends EventEmitter { +export default class RedisCluster extends EventEmitter { static #extractFirstKey(command: RedisCommand, originalArgs: Array, redisArgs: TransformArgumentsReply): string | Buffer | undefined { if (command.FIRST_KEY_INDEX === undefined) { return undefined; @@ -30,54 +30,13 @@ export default class RedisCluster - ): Promise> { - const { args: redisArgs, options } = transformCommandArguments(command, args); - - const reply = command.transformReply( - await this.sendCommand( - RedisCluster.#extractFirstKey(command, args, redisArgs), - command.IS_READ_ONLY, - redisArgs, - options, - command.BUFFER_MODE - ), - redisArgs.preserve - ); - - return reply; - } - - static async #scriptsExecutor( - this: RedisCluster, - script: RedisLuaScript, - args: Array - ): Promise { - const { args: redisArgs, options } = transformCommandArguments(script, args); - - const reply = script.transformReply( - await this.executeScript( - script, - args, - redisArgs, - options - ), - redisArgs.preserve - ); - - return reply; - } - - static create(options?: RedisClusterOptions): RedisClusterType { + static create(options?: RedisClusterOptions): RedisClusterType { return new (extendWithModulesAndScripts({ BaseClass: RedisCluster, modules: options?.modules, - modulesCommandsExecutor: RedisCluster.commandsExecutor, + modulesCommandsExecutor: RedisCluster.prototype.commandsExecutor, scripts: options?.scripts, - scriptsExecutor: RedisCluster.#scriptsExecutor + scriptsExecutor: RedisCluster.prototype.scriptsExecutor }))(options); } @@ -101,6 +60,23 @@ export default class RedisCluster): Promise> { + const { args: redisArgs, options } = transformCommandArguments(command, args); + + const reply = command.transformReply( + await this.sendCommand( + RedisCluster.#extractFirstKey(command, args, redisArgs), + command.IS_READ_ONLY, + redisArgs, + options, + command.BUFFER_MODE + ), + redisArgs.preserve + ); + + return reply; + } + async sendCommand( firstKey: string | Buffer | undefined, isReadonly: boolean | undefined, @@ -125,6 +101,22 @@ export default class RedisCluster): Promise> { + const { args: redisArgs, options } = transformCommandArguments(script, args); + + const reply = script.transformReply( + await this.executeScript( + script, + args, + redisArgs, + options + ), + redisArgs.preserve + ); + + return reply; + } + async executeScript( script: RedisLuaScript, originalArgs: Array, @@ -208,5 +200,4 @@ export default class RedisCluster { describe('transformArguments', () => { it('simple', () => { assert.deepEqual( - transformArguments('destination', 'source', 'member', { + transformArguments('destination', '/home/leibale/Workspace/node-redis/lib/commands/GEOSEARCHSTORE.spec.tssource', 'member', { radius: 1, unit: 'm' }, { diff --git a/lib/commands/GETEX.ts b/lib/commands/GETEX.ts index ca1465b7ee..214dae5c7a 100644 --- a/lib/commands/GETEX.ts +++ b/lib/commands/GETEX.ts @@ -1,3 +1,4 @@ +import { TransformArgumentsReply } from '.'; import { transformEXAT, transformPXAT, transformReplyStringNull } from './generic-transformers'; export const FIRST_KEY_INDEX = 1; @@ -14,7 +15,7 @@ type GetExModes = { PERSIST: true; }; -export function transformArguments(key: string, mode: GetExModes) { +export function transformArguments(key: string, mode: GetExModes): TransformArgumentsReply { const args = ['GETEX', key]; if ('EX' in mode) { diff --git a/lib/commands/index.ts b/lib/commands/index.ts index dce28ac093..6e5310b811 100644 --- a/lib/commands/index.ts +++ b/lib/commands/index.ts @@ -753,7 +753,10 @@ export interface RedisCommands { } export interface RedisModule { - [key: string]: RedisCommand; + [command: string]: RedisCommand; } -export type RedisModules = Record; +export interface RedisModules { + [module: string]: RedisModule; +} +// export type RedisModules = Record; diff --git a/lib/lua-script.ts b/lib/lua-script.ts index 183c42f219..be16f9b913 100644 --- a/lib/lua-script.ts +++ b/lib/lua-script.ts @@ -13,10 +13,10 @@ export interface SHA1 { export type RedisLuaScript = RedisLuaScriptConfig & SHA1; export interface RedisLuaScripts { - [key: string]: RedisLuaScript; + [script: string]: RedisLuaScript; } -export function defineScript(script: S): S & SHA1 { +export function defineScript(script: RedisLuaScriptConfig): typeof script & SHA1 { return { ...script, SHA1: scriptSha1(script.SCRIPT) diff --git a/lib/multi-command.ts b/lib/multi-command.ts index 53f439d8f3..a329a5dbf1 100644 --- a/lib/multi-command.ts +++ b/lib/multi-command.ts @@ -21,7 +21,8 @@ type WithScripts = { [P in keyof S]: RedisMultiCommandSignature }; -export type RedisMultiCommandType = RedisMultiCommand & WithCommands & WithModules & WithScripts; +export type RedisMultiCommandType = + RedisMultiCommand & WithCommands & WithModules & WithScripts; export interface MultiQueuedCommand { args: TransformArgumentsReply; @@ -31,60 +32,20 @@ export interface MultiQueuedCommand { export type RedisMultiExecutor = (queue: Array, chainId?: symbol) => Promise>; -export default class RedisMultiCommand { - static commandsExecutor(this: RedisMultiCommand, command: RedisCommand, args: Array): RedisMultiCommand { - return this.addCommand( - command.transformArguments(...args), - command.transformReply - ); - } - - static #scriptsExecutor( - this: RedisMultiCommand, - script: RedisLuaScript, - args: Array - ): RedisMultiCommand { - const transformedArguments: TransformArgumentsReply = []; - if (this.#scriptsInUse.has(script.SHA1)) { - transformedArguments.push( - 'EVALSHA', - script.SHA1 - ); - } else { - this.#scriptsInUse.add(script.SHA1); - transformedArguments.push( - 'EVAL', - script.SCRIPT - ); - } - - transformedArguments.push(script.NUMBER_OF_KEYS.toString()); - - const scriptArguments = script.transformArguments(...args); - transformedArguments.push(...scriptArguments); - if (scriptArguments.preserve) { - transformedArguments.preserve = scriptArguments.preserve; - } - - return this.addCommand( - transformedArguments, - script.transformReply - ); - } - +export default class RedisMultiCommand { static extend( clientOptions?: RedisClientOptions ): new (...args: ConstructorParameters) => RedisMultiCommandType { return extendWithModulesAndScripts({ BaseClass: RedisMultiCommand, modules: clientOptions?.modules, - modulesCommandsExecutor: RedisMultiCommand.commandsExecutor, + modulesCommandsExecutor: RedisMultiCommand.prototype.commandsExecutor, scripts: clientOptions?.scripts, - scriptsExecutor: RedisMultiCommand.#scriptsExecutor + scriptsExecutor: RedisMultiCommand.prototype.scriptsExecutor }); } - static create( + static create( executor: RedisMultiExecutor, clientOptions?: RedisClientOptions ): RedisMultiCommandType { @@ -153,6 +114,42 @@ export default class RedisMultiCommand): void => (this as any).addCommand(name, args); } + commandsExecutor(command: RedisCommand, args: Array): this { + return this.addCommand( + command.transformArguments(...args), + command.transformReply + ); + } + + scriptsExecutor(script: RedisLuaScript, args: Array): this { + const transformedArguments: TransformArgumentsReply = []; + if (this.#scriptsInUse.has(script.SHA1)) { + transformedArguments.push( + 'EVALSHA', + script.SHA1 + ); + } else { + this.#scriptsInUse.add(script.SHA1); + transformedArguments.push( + 'EVAL', + script.SCRIPT + ); + } + + transformedArguments.push(script.NUMBER_OF_KEYS.toString()); + + const scriptArguments = script.transformArguments(...args); + transformedArguments.push(...scriptArguments); + if (scriptArguments.preserve) { + transformedArguments.preserve = scriptArguments.preserve; + } + + return this.addCommand( + transformedArguments, + script.transformReply + ); + } + addCommand(args: TransformArgumentsReply, transformReply?: RedisCommand['transformReply']): this { this.#queue.push({ args, @@ -205,4 +202,4 @@ export default class RedisMultiCommand): Promise { const SLOTS = 16384; interface SpawnRedisClusterNodeResult extends SpawnRedisServerResult { - client: RedisClientType + client: RedisClientType } async function spawnRedisClusterNode( @@ -281,7 +281,7 @@ export function describeHandleMinimumRedisVersion(minimumVersion: PartialRedisVe export function itWithClient( type: TestRedisServers, title: string, - fn: (client: RedisClientType) => Promise, + fn: (client: RedisClientType) => Promise, options?: RedisTestOptions ): void { it(title, async function () { @@ -306,7 +306,7 @@ export function itWithClient( export function itWithCluster( type: TestRedisClusters, title: string, - fn: (cluster: RedisClusterType) => Promise, + fn: (cluster: RedisClusterType) => Promise, options?: RedisTestOptions ): void { it(title, async function () { @@ -328,7 +328,7 @@ export function itWithCluster( }); } -export function itWithDedicatedCluster(title: string, fn: (cluster: RedisClusterType) => Promise): void { +export function itWithDedicatedCluster(title: string, fn: (cluster: RedisClusterType) => Promise): void { it(title, async function () { this.timeout(10000);