1
0
mirror of https://github.com/redis/node-redis.git synced 2025-08-04 15:02:09 +03:00
Files
node-redis/packages/client/lib/commands/CLIENT_KILL.ts
Shaya Potter 4708736f3b new "transform arguments" API for better key and metadata extraction (#2733)
* Parser support with all commands

* remove "dist" from all imports for consistency

* address most of my review comments

* small tweak to multi type mapping handling

* tweak multi commands / fix addScript cases

* nits

* addressed all in person review comments

* revert addCommand/addScript changes to multi-commands

addCommand needs to be there for sendCommand like ability within a multi.

If its there, it might as well be used by createCommand() et al, to avoid repeating code.

addScript is there (even though only used once), but now made private to keep the logic for bookkeeping near each other.
2024-10-31 12:16:59 -04:00

110 lines
2.9 KiB
TypeScript

import { CommandParser } from '../client/parser';
import { NumberReply, Command } from '../RESP/types';
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;
}
export interface ClientKillAddress extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['ADDRESS']> {
address: `${string}:${number}`;
}
export interface ClientKillLocalAddress extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['LOCAL_ADDRESS']> {
localAddress: `${string}:${number}`;
}
export interface ClientKillId extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['ID']> {
id: number | `${number}`;
}
export interface ClientKillType extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['TYPE']> {
type: 'normal' | 'master' | 'replica' | 'pubsub';
}
export interface ClientKillUser extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['USER']> {
username: string;
}
export type ClientKillSkipMe = CLIENT_KILL_FILTERS['SKIP_ME'] | (ClientKillFilterCommon<CLIENT_KILL_FILTERS['SKIP_ME']> & {
skipMe: boolean;
});
export interface ClientKillMaxAge extends ClientKillFilterCommon<CLIENT_KILL_FILTERS['MAXAGE']> {
maxAge: number;
}
export type ClientKillFilter = ClientKillAddress | ClientKillLocalAddress | ClientKillId | ClientKillType | ClientKillUser | ClientKillSkipMe | ClientKillMaxAge;
export default {
NOT_KEYED_COMMAND: true,
IS_READ_ONLY: true,
parseCommand(parser: CommandParser, filters: ClientKillFilter | Array<ClientKillFilter>) {
parser.push('CLIENT', 'KILL');
if (Array.isArray(filters)) {
for (const filter of filters) {
pushFilter(parser, filter);
}
} else {
pushFilter(parser, filters);
}
},
transformReply: undefined as unknown as () => NumberReply
} as const satisfies Command;
function pushFilter(parser: CommandParser, filter: ClientKillFilter): void {
if (filter === CLIENT_KILL_FILTERS.SKIP_ME) {
parser.push('SKIPME');
return;
}
parser.push(filter.filter);
switch (filter.filter) {
case CLIENT_KILL_FILTERS.ADDRESS:
parser.push(filter.address);
break;
case CLIENT_KILL_FILTERS.LOCAL_ADDRESS:
parser.push(filter.localAddress);
break;
case CLIENT_KILL_FILTERS.ID:
parser.push(
typeof filter.id === 'number' ?
filter.id.toString() :
filter.id
);
break;
case CLIENT_KILL_FILTERS.TYPE:
parser.push(filter.type);
break;
case CLIENT_KILL_FILTERS.USER:
parser.push(filter.username);
break;
case CLIENT_KILL_FILTERS.SKIP_ME:
parser.push(filter.skipMe ? 'yes' : 'no');
break;
case CLIENT_KILL_FILTERS.MAXAGE:
parser.push(filter.maxAge.toString());
break;
}
}