You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-04 15:02:09 +03:00
fix #1758 - implement some CLIENT commands, add name
to RedisClientOptions
This commit is contained in:
20
packages/client/lib/commands/CLIENT_CACHING.spec.ts
Normal file
20
packages/client/lib/commands/CLIENT_CACHING.spec.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { transformArguments } from './CLIENT_CACHING';
|
||||
|
||||
describe('CLIENT CACHING', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(true),
|
||||
['CLIENT', 'CACHING', 'YES']
|
||||
);
|
||||
});
|
||||
|
||||
it('false', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(false),
|
||||
['CLIENT', 'CACHING', 'NO']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
11
packages/client/lib/commands/CLIENT_CACHING.ts
Normal file
11
packages/client/lib/commands/CLIENT_CACHING.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
|
||||
export function transformArguments(value: boolean): RedisCommandArguments {
|
||||
return [
|
||||
'CLIENT',
|
||||
'CACHING',
|
||||
value ? 'YES' : 'NO'
|
||||
];
|
||||
}
|
||||
|
||||
export declare function transformReply(): 'OK';
|
11
packages/client/lib/commands/CLIENT_GETNAME.spec.ts
Normal file
11
packages/client/lib/commands/CLIENT_GETNAME.spec.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { transformArguments } from './CLIENT_GETNAME';
|
||||
|
||||
describe('CLIENT GETNAME', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(),
|
||||
['CLIENT', 'GETNAME']
|
||||
);
|
||||
});
|
||||
});
|
7
packages/client/lib/commands/CLIENT_GETNAME.ts
Normal file
7
packages/client/lib/commands/CLIENT_GETNAME.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
|
||||
export function transformArguments(): RedisCommandArguments {
|
||||
return ['CLIENT', 'GETNAME'];
|
||||
}
|
||||
|
||||
export declare function transformReply(): string | null;
|
11
packages/client/lib/commands/CLIENT_GETREDIR.spec.ts
Normal file
11
packages/client/lib/commands/CLIENT_GETREDIR.spec.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { transformArguments } from './CLIENT_GETREDIR';
|
||||
|
||||
describe('CLIENT GETREDIR', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(),
|
||||
['CLIENT', 'GETREDIR']
|
||||
);
|
||||
});
|
||||
});
|
7
packages/client/lib/commands/CLIENT_GETREDIR.ts
Normal file
7
packages/client/lib/commands/CLIENT_GETREDIR.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
|
||||
export function transformArguments(): RedisCommandArguments {
|
||||
return ['CLIENT', 'GETREDIR'];
|
||||
}
|
||||
|
||||
export declare function transformReply(): number;
|
97
packages/client/lib/commands/CLIENT_KILL.spec.ts
Normal file
97
packages/client/lib/commands/CLIENT_KILL.spec.ts
Normal file
@@ -0,0 +1,97 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { ClientKillFilters, transformArguments } from './CLIENT_KILL';
|
||||
|
||||
describe('CLIENT KILL', () => {
|
||||
describe('transformArguments', () => {
|
||||
it('ADDRESS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.ADDRESS,
|
||||
address: 'ip:6379'
|
||||
}),
|
||||
['CLIENT', 'KILL', 'ADDR', 'ip:6379']
|
||||
);
|
||||
});
|
||||
|
||||
it('LOCAL_ADDRESS', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.LOCAL_ADDRESS,
|
||||
localAddress: 'ip:6379'
|
||||
}),
|
||||
['CLIENT', 'KILL', 'LADDR', 'ip:6379']
|
||||
);
|
||||
});
|
||||
|
||||
describe('ID', () => {
|
||||
it('string', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.ID,
|
||||
id: '1'
|
||||
}),
|
||||
['CLIENT', 'KILL', 'ID', '1']
|
||||
);
|
||||
});
|
||||
|
||||
it('number', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.ID,
|
||||
id: 1
|
||||
}),
|
||||
['CLIENT', 'KILL', 'ID', '1']
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
it('TYPE', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.TYPE,
|
||||
type: 'master'
|
||||
}),
|
||||
['CLIENT', 'KILL', 'TYPE', 'master']
|
||||
);
|
||||
});
|
||||
|
||||
it('USER', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.USER,
|
||||
username: 'username'
|
||||
}),
|
||||
['CLIENT', 'KILL', 'USER', 'username']
|
||||
);
|
||||
});
|
||||
|
||||
describe('SKIP_ME', () => {
|
||||
it('undefined', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments(ClientKillFilters.SKIP_ME),
|
||||
['CLIENT', 'KILL', 'SKIPME']
|
||||
);
|
||||
});
|
||||
|
||||
it('true', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.SKIP_ME,
|
||||
skipMe: true
|
||||
}),
|
||||
['CLIENT', 'KILL', 'SKIPME', 'yes']
|
||||
);
|
||||
});
|
||||
|
||||
it('false', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments({
|
||||
filter: ClientKillFilters.SKIP_ME,
|
||||
skipMe: false
|
||||
}),
|
||||
['CLIENT', 'KILL', 'SKIPME', 'no']
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
95
packages/client/lib/commands/CLIENT_KILL.ts
Normal file
95
packages/client/lib/commands/CLIENT_KILL.ts
Normal file
@@ -0,0 +1,95 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
|
||||
export enum ClientKillFilters {
|
||||
ADDRESS = 'ADDR',
|
||||
LOCAL_ADDRESS = 'LADDR',
|
||||
ID = 'ID',
|
||||
TYPE = 'TYPE',
|
||||
USER = 'USER',
|
||||
SKIP_ME = 'SKIPME'
|
||||
}
|
||||
|
||||
interface KillFilter<T extends ClientKillFilters> {
|
||||
filter: T;
|
||||
}
|
||||
|
||||
interface KillAddress extends KillFilter<ClientKillFilters.ADDRESS> {
|
||||
address: `${string}:${number}`;
|
||||
}
|
||||
|
||||
interface KillLocalAddress extends KillFilter<ClientKillFilters.LOCAL_ADDRESS> {
|
||||
localAddress: `${string}:${number}`;
|
||||
}
|
||||
|
||||
interface KillId extends KillFilter<ClientKillFilters.ID> {
|
||||
id: number | `${number}`;
|
||||
}
|
||||
|
||||
interface KillType extends KillFilter<ClientKillFilters.TYPE> {
|
||||
type: 'normal' | 'master' | 'replica' | 'pubsub';
|
||||
}
|
||||
|
||||
interface KillUser extends KillFilter<ClientKillFilters.USER> {
|
||||
username: string;
|
||||
}
|
||||
|
||||
type KillSkipMe = ClientKillFilters.SKIP_ME | (KillFilter<ClientKillFilters.SKIP_ME> & {
|
||||
skipMe: boolean;
|
||||
});
|
||||
|
||||
type KillFilters = KillAddress | KillLocalAddress | KillId | KillType | KillUser | KillSkipMe;
|
||||
|
||||
export function transformArguments(filters: KillFilters | Array<KillFilters>): RedisCommandArguments {
|
||||
const args = ['CLIENT', 'KILL'];
|
||||
|
||||
if (Array.isArray(filters)) {
|
||||
for (const filter of filters) {
|
||||
pushFilter(args, filter);
|
||||
}
|
||||
} else {
|
||||
pushFilter(args, filters);
|
||||
}
|
||||
|
||||
return args;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
export declare function transformReply(): number;
|
11
packages/client/lib/commands/CLIENT_SETNAME.spec.ts
Normal file
11
packages/client/lib/commands/CLIENT_SETNAME.spec.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import { transformArguments } from './CLIENT_SETNAME';
|
||||
|
||||
describe('CLIENT SETNAME', () => {
|
||||
it('transformArguments', () => {
|
||||
assert.deepEqual(
|
||||
transformArguments('name'),
|
||||
['CLIENT', 'SETNAME', 'name']
|
||||
);
|
||||
});
|
||||
});
|
7
packages/client/lib/commands/CLIENT_SETNAME.ts
Normal file
7
packages/client/lib/commands/CLIENT_SETNAME.ts
Normal file
@@ -0,0 +1,7 @@
|
||||
import { RedisCommandArguments } from '.';
|
||||
|
||||
export function transformArguments(name: string): RedisCommandArguments {
|
||||
return ['CLIENT', 'SETNAME', name];
|
||||
}
|
||||
|
||||
export declare function transformReply(): string | null;
|
Reference in New Issue
Block a user