-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathutils.ts
177 lines (157 loc) · 3.38 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/**
* This interface corresponds to the args passed with SUB command.
*
* @export
* @interface SubArg
*/
export interface SubArg {
subject: Buffer;
group?: Buffer;
sid: number;
}
/**
* This interface corresponds to the args passed with PUB command.
*
* @export
* @interface PubArg
*/
export interface PubArg {
subject: Buffer;
replyTo?: Buffer;
payloadSize: number;
payload?: Buffer;
}
/**
* This interface corresponds to the args passed with UNSUB command.
*
* @export
* @interface UnsubArg
*/
export interface UnsubArg {
sid: number;
maxMsgs?: number;
}
/**
* Returns a list of Buffer after performing the split operation.
* The Whitespace characters include SPACE, CR, NL and TAB.
*
* @export
* @param {Buffer} arg
* @returns {Buffer[]}
*/
export function splitArgs(arg: Buffer): Buffer[] {
const args: Buffer[] = [];
let i = 0;
// Start corresponds to the start index of an arg
let start = -1;
for (i; i < arg.length; i++) {
const b = arg[i];
switch (b) {
case WhiteSpace.SPACE:
case WhiteSpace.NL:
case WhiteSpace.TAB:
case WhiteSpace.CR:
if (start >= 0) {
// A new arg found between start and i
args.push(arg.subarray(start, i));
start = -1;
}
break;
default:
if (start < 0) {
start = i;
}
}
}
// When the arg is at the end of the input Buffer
if (start >= 0) {
args.push(arg.subarray(start));
}
return args;
}
/**
* Parses the args for SUB command.
*
* @export
* @param {Buffer} data
* @returns {SubArg}
*/
export function parseSub(data: Buffer): SubArg {
const args = splitArgs(data);
const subArg: SubArg = {
subject: Buffer.from(''),
group: Buffer.from(''),
sid: -1
};
switch (args.length) {
case 2:
subArg.subject = args[0];
subArg.group = undefined;
subArg.sid = parseInt(args[1].toString(), 10);
break;
case 3:
subArg.subject = args[0];
subArg.group = args[1];
subArg.sid = parseInt(args[2].toString(), 10);
break;
}
return subArg;
}
/**
* Parses the args for PUB command.
*
* @export
* @param {Buffer} data
* @returns {PubArg}
*/
export function preparePub(data: Buffer): PubArg {
const args = splitArgs(data);
const pubArg: PubArg = {
subject: Buffer.from(''),
replyTo: undefined,
payloadSize: 0,
payload: undefined
};
switch (args.length) {
case 2:
pubArg.subject = args[0];
pubArg.replyTo = undefined;
pubArg.payloadSize = parseInt(args[1].toString(), 10);
break;
case 3:
pubArg.subject = args[0];
pubArg.replyTo = args[1];
pubArg.payloadSize = parseInt(args[2].toString(), 10);
}
return pubArg;
}
/**
* Parses the args for UNSUB command.
*
* @export
* @param {Buffer} data
* @returns {UnsubArg}
*/
export function parseUnsubArg(data: Buffer): UnsubArg {
const args = splitArgs(data);
const unsubArg: UnsubArg = {
sid: -1,
maxMsgs: undefined
};
switch (args.length) {
case 1:
unsubArg.sid = parseInt(args[0].toString(), 10);
break;
case 2:
unsubArg.sid = parseInt(args[0].toString(), 10);
unsubArg.maxMsgs = parseInt(args[1].toString(), 10);
break;
}
return unsubArg;
}
enum WhiteSpace {
SPACE = ' '.charCodeAt(0),
NL = '\n'.charCodeAt(0),
TAB = '\t'.charCodeAt(0),
CR = '\r'.charCodeAt(0)
}