You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
V5 bringing RESP3, Sentinel and TypeMapping to node-redis
RESP3 Support - Some commands responses in RESP3 aren't stable yet and therefore return an "untyped" ReplyUnion. Sentinel TypeMapping Correctly types Multi commands Note: some API changes to be further documented in v4-to-v5.md
This commit is contained in:
@@ -1,104 +1,109 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
import { RedisArgument, NumberReply, Command } from '../RESP/types';
|
||||
|
||||
export enum ClientKillFilters {
|
||||
ADDRESS = 'ADDR',
|
||||
LOCAL_ADDRESS = 'LADDR',
|
||||
ID = 'ID',
|
||||
TYPE = 'TYPE',
|
||||
USER = 'USER',
|
||||
SKIP_ME = 'SKIPME',
|
||||
MAXAGE = 'MAXAGE'
|
||||
export const CLIENT_KILL_FILTERS = {
|
||||
ADDRESS: 'ADDR',
|
||||
LOCAL_ADDRESS: 'LADDR',
|
||||
ID: 'ID',
|
||||
TYPE: 'TYPE',
|
||||
USER: 'USER',
|
||||
SKIP_ME: 'SKIPME',
|
||||
MAXAGE: 'MAXAGE'
|
||||
} as const;
|
||||
|
||||
type CLIENT_KILL_FILTERS = typeof CLIENT_KILL_FILTERS;
|
||||
|
||||
export interface ClientKillFilterCommon<T extends CLIENT_KILL_FILTERS[keyof CLIENT_KILL_FILTERS]> {
|
||||
filter: T;
|
||||
}
|
||||
|
||||
interface KillFilter<T extends ClientKillFilters> {
|
||||
filter: T;
|
||||
export interface ClientKillAddress extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['ADDRESS']> {
|
||||
address: `${string}:${number}`;
|
||||
}
|
||||
|
||||
interface KillAddress extends KillFilter<ClientKillFilters.ADDRESS> {
|
||||
address: `${string}:${number}`;
|
||||
export interface ClientKillLocalAddress extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['LOCAL_ADDRESS']> {
|
||||
localAddress: `${string}:${number}`;
|
||||
}
|
||||
|
||||
interface KillLocalAddress extends KillFilter<ClientKillFilters.LOCAL_ADDRESS> {
|
||||
localAddress: `${string}:${number}`;
|
||||
export interface ClientKillId extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['ID']> {
|
||||
id: number | `${number}`;
|
||||
}
|
||||
|
||||
interface KillId extends KillFilter<ClientKillFilters.ID> {
|
||||
id: number | `${number}`;
|
||||
export interface ClientKillType extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['TYPE']> {
|
||||
type: 'normal' | 'master' | 'replica' | 'pubsub';
|
||||
}
|
||||
|
||||
interface KillType extends KillFilter<ClientKillFilters.TYPE> {
|
||||
type: 'normal' | 'master' | 'replica' | 'pubsub';
|
||||
export interface ClientKillUser extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['USER']> {
|
||||
username: string;
|
||||
}
|
||||
|
||||
interface KillUser extends KillFilter<ClientKillFilters.USER> {
|
||||
username: string;
|
||||
}
|
||||
|
||||
type KillSkipMe = ClientKillFilters.SKIP_ME | (KillFilter<ClientKillFilters.SKIP_ME> & {
|
||||
skipMe: boolean;
|
||||
export type ClientKillSkipMe = CLIENT_KILL_FILTERS['SKIP_ME'] | (ClientKillFilterCommon<CLIENT_KILL_FILTERS['SKIP_ME']> & {
|
||||
skipMe: boolean;
|
||||
});
|
||||
|
||||
interface KillMaxAge extends KillFilter<ClientKillFilters.MAXAGE> {
|
||||
maxAge: number;
|
||||
export interface ClientKillMaxAge extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['MAXAGE']> {
|
||||
maxAge: number;
|
||||
}
|
||||
|
||||
type KillFilters = KillAddress | KillLocalAddress | KillId | KillType | KillUser | KillSkipMe | KillMaxAge;
|
||||
export type ClientKillFilter = ClientKillAddress | ClientKillLocalAddress | ClientKillId | ClientKillType | ClientKillUser | ClientKillSkipMe | ClientKillMaxAge;
|
||||
|
||||
export function transformArguments(filters: KillFilters | Array<KillFilters>): RedisCommandArguments {
|
||||
export default {
|
||||
FIRST_KEY_INDEX: undefined,
|
||||
IS_READ_ONLY: true,
|
||||
transformArguments(filters: ClientKillFilter | Array<ClientKillFilter>) {
|
||||
const args = ['CLIENT', 'KILL'];
|
||||
|
||||
if (Array.isArray(filters)) {
|
||||
for (const filter of filters) {
|
||||
pushFilter(args, filter);
|
||||
}
|
||||
for (const filter of filters) {
|
||||
pushFilter(args, filter);
|
||||
}
|
||||
} else {
|
||||
pushFilter(args, filters);
|
||||
pushFilter(args, filters);
|
||||
}
|
||||
|
||||
return args;
|
||||
},
|
||||
transformReply: undefined as unknown as () => NumberReply
|
||||
} as const satisfies Command;
|
||||
|
||||
function pushFilter(args: Array<RedisArgument>, filter: ClientKillFilter): void {
|
||||
if (filter === CLIENT_KILL_FILTERS.SKIP_ME) {
|
||||
args.push('SKIPME');
|
||||
return;
|
||||
}
|
||||
|
||||
args.push(filter.filter);
|
||||
|
||||
switch (filter.filter) {
|
||||
case CLIENT_KILL_FILTERS.ADDRESS:
|
||||
args.push(filter.address);
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.LOCAL_ADDRESS:
|
||||
args.push(filter.localAddress);
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.ID:
|
||||
args.push(
|
||||
typeof filter.id === 'number' ?
|
||||
filter.id.toString() :
|
||||
filter.id
|
||||
);
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.TYPE:
|
||||
args.push(filter.type);
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.USER:
|
||||
args.push(filter.username);
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.SKIP_ME:
|
||||
args.push(filter.skipMe ? 'yes' : 'no');
|
||||
break;
|
||||
|
||||
case CLIENT_KILL_FILTERS.MAXAGE:
|
||||
args.push(filter.maxAge.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
function pushFilter(args: RedisCommandArguments, filter: KillFilters): void {
|
||||
if (filter === ClientKillFilters.SKIP_ME) {
|
||||
args.push('SKIPME');
|
||||
return;
|
||||
}
|
||||
|
||||
args.push(filter.filter);
|
||||
|
||||
switch(filter.filter) {
|
||||
case ClientKillFilters.ADDRESS:
|
||||
args.push(filter.address);
|
||||
break;
|
||||
|
||||
case ClientKillFilters.LOCAL_ADDRESS:
|
||||
args.push(filter.localAddress);
|
||||
break;
|
||||
|
||||
case ClientKillFilters.ID:
|
||||
args.push(
|
||||
typeof filter.id === 'number' ?
|
||||
filter.id.toString() :
|
||||
filter.id
|
||||
);
|
||||
break;
|
||||
|
||||
case ClientKillFilters.TYPE:
|
||||
args.push(filter.type);
|
||||
break;
|
||||
|
||||
case ClientKillFilters.USER:
|
||||
args.push(filter.username);
|
||||
break;
|
||||
|
||||
case ClientKillFilters.SKIP_ME:
|
||||
args.push(filter.skipMe ? 'yes' : 'no');
|
||||
break;
|
||||
|
||||
case ClientKillFilters.MAXAGE:
|
||||
args.push(filter.maxAge.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
export declare function transformReply(): number;
|
||||
|
Reference in New Issue
Block a user