UNPKG

1.05 kBPlain TextView Raw
1import { makeErrorWithCode } from '@neo-one/utils-esnext-esm';
2
3export enum Command {
4 addr = 'addr',
5 block = 'block',
6 consensus = 'consensus',
7 filteradd = 'filteradd',
8 filterclear = 'filterclear',
9 filterload = 'filterload',
10 getaddr = 'getaddr',
11 getblocks = 'getblocks',
12 getdata = 'getdata',
13 getheaders = 'getheaders',
14 headers = 'headers',
15 inv = 'inv',
16 mempool = 'mempool',
17 tx = 'tx',
18 verack = 'verack',
19 version = 'version',
20 alert = 'alert',
21 merkleblock = 'merkleblock',
22 notfound = 'notfound',
23 ping = 'ping',
24 pong = 'pong',
25 reject = 'reject',
26}
27
28export const InvalidCommandError = makeErrorWithCode(
29 'INVALID_COMMAND',
30 (command: string) => `Invalid Command. Found: ${command}`,
31);
32
33const isCommand = (command: string): command is Command =>
34 // tslint:disable-next-line strict-type-predicates no-any
35 Command[command as any] !== undefined;
36
37export const assertCommand = (command: string): Command => {
38 if (isCommand(command)) {
39 return command;
40 }
41
42 throw new InvalidCommandError(command);
43};