Skip to content

Commit

Permalink
replace empty "transformReply" functions with typescript "declare"
Browse files Browse the repository at this point in the history
  • Loading branch information
leibale committed Sep 29, 2021
1 parent 9a1beed commit c19d200
Show file tree
Hide file tree
Showing 174 changed files with 472 additions and 1,051 deletions.
215 changes: 68 additions & 147 deletions benchmark/package-lock.json

Large diffs are not rendered by default.

20 changes: 11 additions & 9 deletions lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RedisSocket, { RedisSocketOptions, RedisNetSocketOptions, RedisTlsSocketOptions } from './socket';
import RedisCommandsQueue, { PubSubListener, PubSubSubscribeCommands, PubSubUnsubscribeCommands, QueueCommandOptions } from './commands-queue';
import COMMANDS, { TransformArgumentsReply } from './commands';
import COMMANDS, { RedisCommandReply, TransformArgumentsReply } from './commands';
import { RedisCommand, RedisModules, RedisReply } from './commands';
import RedisMultiCommand, { MultiQueuedCommand, RedisMultiCommandType } from './multi-command';
import EventEmitter from 'events';
Expand All @@ -9,7 +9,7 @@ import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { ScanOptions, ZMember } from './commands/generic-transformers';
import { ScanCommandOptions } from './commands/SCAN';
import { HScanTuple } from './commands/HSCAN';
import { encodeCommand, extendWithDefaultCommands, extendWithModulesAndScripts, transformCommandArguments } from './commander';
import { encodeCommand, extendWithDefaultCommands, extendWithModulesAndScripts, transformCommandArguments, transformCommandReply } from './commander';
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
import { ClientClosedError } from './errors';
import { URL } from 'url';
Expand All @@ -29,7 +29,7 @@ export interface RedisClientOptions<M, S> {
}

export type RedisCommandSignature<C extends RedisCommand> =
(...args: Parameters<C['transformArguments']> | [options: CommandOptions<ClientCommandOptions>, ...rest: Parameters<C['transformArguments']>]) => Promise<ReturnType<C['transformReply']>>;
(...args: Parameters<C['transformArguments']> | [options: CommandOptions<ClientCommandOptions>, ...rest: Parameters<C['transformArguments']>]) => Promise<RedisCommandReply<C>>;

type WithCommands = {
[P in keyof typeof COMMANDS]: RedisCommandSignature<(typeof COMMANDS)[P]>;
Expand Down Expand Up @@ -275,12 +275,13 @@ export default class RedisClient<M extends RedisModules, S extends RedisLuaScrip
await this.#socket.connect();
}

async commandsExecutor(command: RedisCommand, args: Array<unknown>): Promise<ReturnType<typeof command['transformReply']>> {
async commandsExecutor(command: RedisCommand, args: Array<unknown>): Promise<RedisCommandReply<typeof command>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(command, args);

return command.transformReply(
return transformCommandReply(
command,
await this.#sendCommand(redisArgs, options, command.BUFFER_MODE),
redisArgs.preserve,
redisArgs.preserve
);
}

Expand Down Expand Up @@ -308,16 +309,17 @@ export default class RedisClient<M extends RedisModules, S extends RedisLuaScrip
return await promise;
}

async scriptsExecutor(script: RedisLuaScript, args: Array<unknown>): Promise<ReturnType<typeof script['transformReply']>> {
async scriptsExecutor(script: RedisLuaScript, args: Array<unknown>): Promise<RedisCommandReply<typeof script>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(script, args);

return script.transformReply(
return transformCommandReply(
script,
await this.executeScript(script, redisArgs, options, script.BUFFER_MODE),
redisArgs.preserve
);
}

async executeScript(script: RedisLuaScript, args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise<ReturnType<typeof script['transformReply']>> {
async executeScript(script: RedisLuaScript, args: TransformArgumentsReply, options?: ClientCommandOptions, bufferMode?: boolean): Promise<RedisCommandReply<typeof script>> {
try {
return await this.#sendCommand([
'EVALSHA',
Expand Down
22 changes: 10 additions & 12 deletions lib/cluster.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { RedisCommand, RedisModules, TransformArgumentsReply } from './commands';
import { RedisCommand, RedisCommandReply, RedisModules, TransformArgumentsReply } from './commands';
import RedisClient, { ClientCommandOptions, RedisClientType, WithPlugins } from './client';
import { RedisSocketOptions } from './socket';
import RedisClusterSlots, { ClusterNode } from './cluster-slots';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { extendWithModulesAndScripts, extendWithDefaultCommands, transformCommandArguments } from './commander';
import { extendWithModulesAndScripts, extendWithDefaultCommands, transformCommandArguments, transformCommandReply } from './commander';
import RedisMultiCommand, { MultiQueuedCommand, RedisMultiCommandType } from './multi-command';
import { EventEmitter } from 'events';

Expand Down Expand Up @@ -59,10 +59,11 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
return this.#slots.connect();
}

async commandsExecutor(command: RedisCommand, args: Array<unknown>): Promise<ReturnType<typeof command['transformReply']>> {
async commandsExecutor(command: RedisCommand, args: Array<unknown>): Promise<RedisCommandReply<typeof command>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(command, args);

const reply = command.transformReply(
return transformCommandReply(
command,
await this.sendCommand(
RedisCluster.#extractFirstKey(command, args, redisArgs),
command.IS_READ_ONLY,
Expand All @@ -72,8 +73,6 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
),
redisArgs.preserve
);

return reply;
}

async sendCommand<C extends RedisCommand>(
Expand All @@ -83,7 +82,7 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
options?: ClientCommandOptions,
bufferMode?: boolean,
redirections = 0
): Promise<ReturnType<C['transformReply']>> {
): Promise<RedisCommandReply<C>> {
const client = this.#slots.getClient(firstKey, isReadonly);

try {
Expand All @@ -100,10 +99,11 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
}
}

async scriptsExecutor(script: RedisLuaScript, args: Array<unknown>): Promise<ReturnType<typeof script['transformReply']>> {
async scriptsExecutor(script: RedisLuaScript, args: Array<unknown>): Promise<RedisCommandReply<typeof script>> {
const { args: redisArgs, options } = transformCommandArguments<ClientCommandOptions>(script, args);

const reply = script.transformReply(
return transformCommandReply(
script,
await this.executeScript(
script,
args,
Expand All @@ -112,8 +112,6 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
),
redisArgs.preserve
);

return reply;
}

async executeScript(
Expand All @@ -122,7 +120,7 @@ export default class RedisCluster<M extends RedisModules = {}, S extends RedisLu
redisArgs: TransformArgumentsReply,
options?: ClientCommandOptions,
redirections = 0
): Promise<ReturnType<typeof script['transformReply']>> {
): Promise<RedisCommandReply<typeof script>> {
const client = this.#slots.getClient(
RedisCluster.#extractFirstKey(script, originalArgs, redisArgs),
script.IS_READ_ONLY
Expand Down
10 changes: 9 additions & 1 deletion lib/commander.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

import COMMANDS, { RedisCommand, RedisModules, TransformArgumentsReply } from './commands';
import COMMANDS, { RedisCommand, RedisCommandReply, RedisModules, RedisReply, TransformArgumentsReply } from './commands';
import { RedisLuaScript, RedisLuaScripts } from './lua-script';
import { CommandOptions, isCommandOptions } from './command-options';

Expand Down Expand Up @@ -106,3 +106,11 @@ export function* encodeCommand(args: TransformArgumentsReply): IterableIterator<
yield DELIMITER;
}
}

export function transformCommandReply(command: RedisCommand, rawReply: RedisReply, preserved: unknown): RedisCommandReply<typeof command> {
if (!command.transformReply) {
return rawReply;
}

return command.transformReply(rawReply, preserved);
}
4 changes: 1 addition & 3 deletions lib/commands/ACL_CAT.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyStringArray } from './generic-transformers';

export function transformArguments(categoryName?: string): Array<string> {
const args = ['ACL', 'CAT'];

Expand All @@ -10,4 +8,4 @@ export function transformArguments(categoryName?: string): Array<string> {
return args;
}

export const transformReply = transformReplyStringArray;
export declare function transformReply(): Array<string>;
4 changes: 2 additions & 2 deletions lib/commands/ACL_DELUSER.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TransformArgumentsReply } from '.';
import { pushVerdictArguments, transformReplyNumber } from './generic-transformers';
import { pushVerdictArguments } from './generic-transformers';

export function transformArguments(username: string | Array<string>): TransformArgumentsReply {
return pushVerdictArguments(['ACL', 'DELUSER'], username);
}

export const transformReply = transformReplyNumber;
export declare const transformReply: (reply: number) => number;
4 changes: 1 addition & 3 deletions lib/commands/ACL_GENPASS.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(bits?: number): Array<string> {
const args = ['ACL', 'GENPASS'];

Expand All @@ -10,4 +8,4 @@ export function transformArguments(bits?: number): Array<string> {
return args;
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/ACL_LIST.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyStringArray } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'LIST'];
}

export const transformReply = transformReplyStringArray;
export declare function transformReply(): Array<string>;
4 changes: 1 addition & 3 deletions lib/commands/ACL_LOAD.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'LOAD'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/ACL_LOG_RESET.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'LOG', 'RESET'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/ACL_SAVE.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'SAVE'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 2 additions & 2 deletions lib/commands/ACL_SETUSER.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TransformArgumentsReply } from '.';
import { pushVerdictArguments, transformReplyString } from './generic-transformers';
import { pushVerdictArguments } from './generic-transformers';

export function transformArguments(username: string, rule: string | Array<string>): TransformArgumentsReply {
return pushVerdictArguments(['ACL', 'SETUSER', username], rule);
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/ACL_USERS.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyStringArray } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'USERS'];
}

export const transformReply = transformReplyStringArray;
export declare function transformReply(): Array<string>;
4 changes: 1 addition & 3 deletions lib/commands/ACL_WHOAMI.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ACL', 'WHOAMI'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/APPEND.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { transformReplyString } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export function transformArguments(key: string, value: string): Array<string> {
return ['APPEND', key, value];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/ASKING.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['ASKING'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/AUTH.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyString } from './generic-transformers';

export interface AuthOptions {
username?: string;
password: string;
Expand All @@ -13,4 +11,4 @@ export function transformArguments({username, password}: AuthOptions): Array<str
return ['AUTH', username, password];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/BGREWRITEAOF.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { transformReplyString } from './generic-transformers';

export function transformArguments(): Array<string> {
return ['BGREWRITEAOF'];
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/BGSAVE.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyString } from './generic-transformers';

interface BgSaveOptions {
SCHEDULE?: true;
}
Expand All @@ -14,4 +12,4 @@ export function transformArguments(options?: BgSaveOptions): Array<string> {
return args;
}

export const transformReply = transformReplyString;
export declare function transformReply(): string;
4 changes: 1 addition & 3 deletions lib/commands/BITCOUNT.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyNumber } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;
Expand All @@ -22,4 +20,4 @@ export function transformArguments(key: string, range?: BitCountRange): Array<st
return args;
}

export const transformReply = transformReplyNumber;
export declare function transformReply(): number;
4 changes: 1 addition & 3 deletions lib/commands/BITFIELD.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { transformReplyNumberNullArray } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export const IS_READ_ONLY = true;
Expand Down Expand Up @@ -81,4 +79,4 @@ export function transformArguments(key: string, operations: BitFieldOperations):
return args;
}

export const transformReply = transformReplyNumberNullArray;
export declare function transformReply(): Array<number | null>;
4 changes: 2 additions & 2 deletions lib/commands/BITOP.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { TransformArgumentsReply } from '.';
import { pushVerdictArguments, transformReplyNumber } from './generic-transformers';
import { pushVerdictArguments } from './generic-transformers';

export const FIRST_KEY_INDEX = 2;

Expand All @@ -9,4 +9,4 @@ export function transformArguments(operation: BitOperations, destKey: string, ke
return pushVerdictArguments(['BITOP', operation, destKey], key);
}

export const transformReply = transformReplyNumber;
export declare function transformReply(): number;
4 changes: 2 additions & 2 deletions lib/commands/BITPOS.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BitValue, transformReplyNumber } from './generic-transformers';
import { BitValue } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

Expand All @@ -18,4 +18,4 @@ export function transformArguments(key: string, bit: BitValue, start?: number, e
return args;
}

export const transformReply = transformReplyNumber;
export declare function transformReply(): number;
3 changes: 1 addition & 2 deletions lib/commands/BLMOVE.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { transformReplyStringNull } from './generic-transformers';
import { LMoveSide } from './LMOVE';

export const FIRST_KEY_INDEX = 1;
Expand All @@ -20,4 +19,4 @@ export function transformArguments(
];
}

export const transformReply = transformReplyStringNull;
export declare function transformReply(): string | null;
4 changes: 1 addition & 3 deletions lib/commands/BRPOPLPUSH.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { transformReplyNumberNull } from './generic-transformers';

export const FIRST_KEY_INDEX = 1;

export function transformArguments(source: string, destination: string, timeout: number): Array<string> {
return ['BRPOPLPUSH', source, destination, timeout.toString()];
}

export const transformReply = transformReplyNumberNull;
export declare function transformReply(): number | null;
4 changes: 1 addition & 3 deletions lib/commands/CLIENT_ID.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { transformReplyNumber } from './generic-transformers';

export const IS_READ_ONLY = true;

export function transformArguments(): Array<string> {
return ['CLIENT', 'ID'];
}

export const transformReply = transformReplyNumber;
export declare function transformReply(): number;
Loading

0 comments on commit c19d200

Please sign in to comment.