Skip to content

Commit 6e7844e

Browse files
committed
add all COMMAND commands
1 parent 5eb06bc commit 6e7844e

11 files changed

+265
-1
lines changed

lib/commands/COMMAND.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { strict as assert } from 'assert';
2+
import { itWithClient, TestRedisServers } from '../test-utils';
3+
import { transformArguments } from './COMMAND';
4+
import { CommandCategories, CommandFlags } from './generic-transformers';
5+
6+
describe('COMMAND', () => {
7+
it('transformArguments', () => {
8+
assert.deepEqual(
9+
transformArguments(),
10+
['COMMAND']
11+
);
12+
});
13+
14+
itWithClient(TestRedisServers.OPEN, 'client.command', async client => {
15+
assert.deepEqual(
16+
(await client.command()).find(command => command.name === 'ping'),
17+
{
18+
name: 'ping',
19+
arity: -1,
20+
flags: new Set([CommandFlags.STALE, CommandFlags.FAST]),
21+
firstKeyIndex: 0,
22+
lastKeyIndex: 0,
23+
step: 0,
24+
categories: new Set([CommandCategories.FAST, CommandCategories.CONNECTION])
25+
}
26+
);
27+
});
28+
});

lib/commands/COMMAND.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TransformArgumentsReply } from '.';
2+
import { CommandRawReply, CommandReply, transformCommandReply } from './generic-transformers';
3+
4+
export const IS_READ_ONLY = true;
5+
6+
export function transformArguments(): TransformArgumentsReply {
7+
return ['COMMAND'];
8+
}
9+
10+
export function transformReply(reply: Array<CommandRawReply>): Array<CommandReply> {
11+
return reply.map(transformCommandReply);
12+
}

lib/commands/COMMAND_COUNT.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { strict as assert } from 'assert';
2+
import { itWithClient, itWithCluster, TestRedisClusters, TestRedisServers } from '../test-utils';
3+
import { transformArguments } from './COMMAND_COUNT';
4+
5+
describe('COMMAND COUNT', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
transformArguments(),
9+
['COMMAND', 'COUNT']
10+
);
11+
});
12+
13+
itWithClient(TestRedisServers.OPEN, 'client.commandCount', async client => {
14+
assert.equal(
15+
typeof await client.commandCount(),
16+
'number'
17+
);
18+
});
19+
20+
itWithCluster(TestRedisClusters.OPEN, 'cluster.commandCount', async cluster => {
21+
assert.equal(
22+
typeof await cluster.commandCount(),
23+
'number'
24+
);
25+
});
26+
});

lib/commands/COMMAND_COUNT.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TransformArgumentsReply } from '.';
2+
3+
export const IS_READ_ONLY = true;
4+
5+
export function transformArguments(): TransformArgumentsReply {
6+
return ['COMMAND', 'COUNT'];
7+
}
8+
9+
declare function transformReply(): number;

lib/commands/COMMAND_GETKEYS.spec.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { strict as assert } from 'assert';
2+
import { itWithClient, itWithCluster, TestRedisClusters, TestRedisServers } from '../test-utils';
3+
import { transformArguments } from './COMMAND_GETKEYS';
4+
5+
describe('COMMAND GETKEYS', () => {
6+
it('transformArguments', () => {
7+
assert.deepEqual(
8+
transformArguments(['GET', 'key']),
9+
['COMMAND', 'GETKEYS', 'GET', 'key']
10+
);
11+
});
12+
13+
itWithClient(TestRedisServers.OPEN, 'client.commandGetKeys', async client => {
14+
assert.deepEqual(
15+
await client.commandGetKeys(['GET', 'key']),
16+
['key']
17+
);
18+
});
19+
20+
itWithCluster(TestRedisClusters.OPEN, 'cluster.commandGetKeys', async cluster => {
21+
assert.deepEqual(
22+
await cluster.commandGetKeys(['GET', 'key']),
23+
['key']
24+
);
25+
});
26+
});

lib/commands/COMMAND_GETKEYS.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TransformArgumentsReply } from '.';
2+
3+
export const IS_READ_ONLY = true;
4+
5+
export function transformArguments(args: Array<string>): TransformArgumentsReply {
6+
return ['COMMAND', 'GETKEYS', ...args];
7+
}
8+
9+
declare function transformReply(): Array<string>;

lib/commands/COMMAND_INFO.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { strict as assert } from 'assert';
2+
import { itWithClient, TestRedisServers } from '../test-utils';
3+
import { transformArguments } from './COMMAND_INFO';
4+
import { CommandCategories, CommandFlags } from './generic-transformers';
5+
6+
describe('COMMAND INFO', () => {
7+
it('transformArguments', () => {
8+
assert.deepEqual(
9+
transformArguments(['PING']),
10+
['COMMAND', 'INFO', 'PING']
11+
);
12+
});
13+
14+
itWithClient(TestRedisServers.OPEN, 'client.commandInfo', async client => {
15+
assert.deepEqual(
16+
await client.commandInfo(['PING']),
17+
[{
18+
name: 'ping',
19+
arity: -1,
20+
flags: new Set([CommandFlags.STALE, CommandFlags.FAST]),
21+
firstKeyIndex: 0,
22+
lastKeyIndex: 0,
23+
step: 0,
24+
categories: new Set([CommandCategories.FAST, CommandCategories.CONNECTION])
25+
}]
26+
);
27+
});
28+
});

lib/commands/COMMAND_INFO.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { TransformArgumentsReply } from '.';
2+
import { CommandRawReply, CommandReply, transformCommandReply } from './generic-transformers';
3+
4+
export const IS_READ_ONLY = true;
5+
6+
export function transformArguments(commands: Array<string>): TransformArgumentsReply {
7+
return ['COMMAND', 'INFO', ...commands];
8+
}
9+
10+
export function transformReply(reply: Array<CommandRawReply | null>): Array<CommandReply | null> {
11+
return reply.map(command => command ? transformCommandReply(command) : null);
12+
}

lib/commands/generic-transformers.spec.ts

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ import {
2121
pushStringTuplesArguments,
2222
pushVerdictArguments,
2323
pushVerdictArgument,
24-
pushOptionalVerdictArgument
24+
pushOptionalVerdictArgument,
25+
transformCommandReply,
26+
CommandFlags,
27+
CommandCategories
2528
} from './generic-transformers';
2629

2730
describe('Generic Transformers', () => {
@@ -626,4 +629,27 @@ describe('Generic Transformers', () => {
626629
);
627630
});
628631
});
632+
633+
it('transformCommandReply', () => {
634+
assert.deepEqual(
635+
transformCommandReply([
636+
'ping',
637+
-1,
638+
[CommandFlags.STALE, CommandFlags.FAST],
639+
0,
640+
0,
641+
0,
642+
[CommandCategories.FAST, CommandCategories.CONNECTION]
643+
]),
644+
{
645+
name: 'ping',
646+
arity: -1,
647+
flags: new Set([CommandFlags.STALE, CommandFlags.FAST]),
648+
firstKeyIndex: 0,
649+
lastKeyIndex: 0,
650+
step: 0,
651+
categories: new Set([CommandCategories.FAST, CommandCategories.CONNECTION])
652+
}
653+
);
654+
});
629655
});

lib/commands/generic-transformers.ts

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,3 +335,79 @@ export function pushOptionalVerdictArgument(args: TransformArgumentsReply, name:
335335

336336
return pushVerdictArgument(args, value);
337337
}
338+
339+
export enum CommandFlags {
340+
WRITE = 'write', // command may result in modifications
341+
READONLY = 'readonly', // command will never modify keys
342+
DENYOOM = 'denyoom', // reject command if currently out of memory
343+
ADMIN = 'admin', // server admin command
344+
PUBSUB = 'pubsub', // pubsub-related command
345+
NOSCRIPT = 'noscript', // deny this command from scripts
346+
RANDOM = 'random', // command has random results, dangerous for scripts
347+
SORT_FOR_SCRIPT = 'sort_for_script', // if called from script, sort output
348+
LOADING = 'loading', // allow command while database is loading
349+
STALE = 'stale', // allow command while replica has stale data
350+
SKIP_MONITOR = 'skip_monitor', // do not show this command in MONITOR
351+
ASKING = 'asking', // cluster related - accept even if importing
352+
FAST = 'fast', // command operates in constant or log(N) time. Used for latency monitoring.
353+
MOVABLEKEYS = 'movablekeys' // keys have no pre-determined position. You must discover keys yourself.
354+
}
355+
356+
export enum CommandCategories {
357+
KEYSPACE = '@keyspace',
358+
READ = '@read',
359+
WRITE = '@write',
360+
SET = '@set',
361+
SORTEDSET = '@sortedset',
362+
LIST = '@list',
363+
HASH = '@hash',
364+
STRING = '@string',
365+
BITMAP = '@bitmap',
366+
HYPERLOGLOG = '@hyperloglog',
367+
GEO = '@geo',
368+
STREAM = '@stream',
369+
PUBSUB = '@pubsub',
370+
ADMIN = '@admin',
371+
FAST = '@fast',
372+
SLOW = '@slow',
373+
BLOCKING = '@blocking',
374+
DANGEROUS = '@dangerous',
375+
CONNECTION = '@connection',
376+
TRANSACTION = '@transaction',
377+
SCRIPTING = '@scripting'
378+
}
379+
380+
export type CommandRawReply = [
381+
name: string,
382+
arity: number,
383+
flags: Array<CommandFlags>,
384+
firstKeyIndex: number,
385+
lastKeyIndex: number,
386+
step: number,
387+
categories: Array<CommandCategories>
388+
];
389+
390+
export type CommandReply = {
391+
name: string,
392+
arity: number,
393+
flags: Set<CommandFlags>,
394+
firstKeyIndex: number,
395+
lastKeyIndex: number,
396+
step: number,
397+
categories: Set<CommandCategories>
398+
};
399+
400+
export function transformCommandReply(
401+
this: void,
402+
[name, arity, flags, firstKeyIndex, lastKeyIndex, step, categories]: CommandRawReply
403+
): CommandReply {
404+
return {
405+
name,
406+
arity,
407+
flags: new Set(flags),
408+
firstKeyIndex,
409+
lastKeyIndex,
410+
step,
411+
categories: new Set(categories)
412+
};
413+
}

lib/commands/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ import * as CLUSTER_MEET from './CLUSTER_MEET';
3535
import * as CLUSTER_RESET from './CLUSTER_RESET';
3636
import * as CLUSTER_SETSLOT from './CLUSTER_SETSLOT';
3737
import * as CLUSTER_SLOTS from './CLUSTER_SLOTS';
38+
import * as COMMAND_COUNT from './COMMAND_COUNT';
39+
import * as COMMAND_GETKEYS from './COMMAND_GETKEYS';
40+
import * as COMMAND_INFO from './COMMAND_INFO';
41+
import * as COMMAND from './COMMAND';
3842
import * as CONFIG_GET from './CONFIG_GET';
3943
import * as CONFIG_RESETASTAT from './CONFIG_RESETSTAT';
4044
import * as CONFIG_REWRITE from './CONFIG_REWRITE';
@@ -323,6 +327,14 @@ export default {
323327
clusterSetSlot: CLUSTER_SETSLOT,
324328
CLUSTER_SLOTS,
325329
clusterSlots: CLUSTER_SLOTS,
330+
COMMAND_COUNT,
331+
commandCount: COMMAND_COUNT,
332+
COMMAND_GETKEYS,
333+
commandGetKeys: COMMAND_GETKEYS,
334+
COMMAND_INFO,
335+
commandInfo: COMMAND_INFO,
336+
COMMAND,
337+
command: COMMAND,
326338
CONFIG_GET,
327339
configGet: CONFIG_GET,
328340
CONFIG_RESETASTAT,

0 commit comments

Comments
 (0)