-
Notifications
You must be signed in to change notification settings - Fork 13
/
redis_deserializer.ts
284 lines (240 loc) · 5.79 KB
/
redis_deserializer.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import { RespArray, RespType } from './types';
export interface IRedisDeserializer {
/**
* This function parses the text input.
* It also raises Error if the input is invalid.
*
* @returns {RespType}
*/
parse(): RespType;
/**
* Returns the current position of the cursor while traversing the input.
*
* @returns {number}
*/
getPos(): number;
}
export class RedisDeserializer implements IRedisDeserializer {
private input: string;
private pos: number;
private inputLength: number;
private multipleCommands: boolean;
constructor(input: string, multipleCommands: boolean = false) {
this.input = input;
this.pos = 0;
this.inputLength = input.length;
this.multipleCommands = multipleCommands;
}
getPos(): number {
return this.pos;
}
parse(): RespType {
const output = this.parseValue();
// If the input text still has characters and this is a single command
if (this.hasNext() && !this.multipleCommands) {
throw new Error(
`Invalid token ${JSON.stringify(this.getCurrentToken())} at ${this.pos}`
);
}
return output;
}
/**
* This function parses a single value, which can be:
* Simple String
* Error
* Integer
* Bulk String
* Arrays
*
* @private
* @returns {RespType}
*/
private parseValue(): RespType {
const token = this.getCurrentToken();
switch (token) {
case '+':
return this.parseSimpleStrings();
case '-':
return this.parseError();
case ':':
return this.parseInteger();
case '$':
return this.parseBulkStrings();
case '*':
return this.parseArrays();
default:
throw new Error(`Invalid token ${token} at ${this.pos}`);
}
}
/**
* Checks whether this is end point input or not
*
* @private
* @returns {boolean}
*/
private hasNext(): boolean {
return this.input.codePointAt(this.pos) !== undefined;
}
/**
* Parses an Error
* e.g. - "-Error Message\r\n"
*
* @private
* @returns {Error}
*/
private parseError(): Error {
this.consumeToken('-');
let message = '';
while (this.getCurrentToken() !== '\r' && this.pos < this.inputLength) {
message += this.getCurrentToken();
this.consumeToken();
}
this.consumeToken('\r');
this.consumeToken('\n');
return new Error(message);
}
/**
* Parses an Integer
* e.g. - ":1000\r\n"
*
* @private
* @returns {number}
*/
private parseInteger(): number {
this.consumeToken(':');
let ans = 0;
while (this.getCurrentToken() !== '\r' && this.pos < this.inputLength) {
ans = ans * 10 + parseInt(this.getCurrentToken());
this.consumeToken();
}
this.consumeToken('\r');
this.consumeToken('\n');
return ans;
}
/**
* Parses a Simple string
* e.g - "+OK\r\n"
*
* @private
* @returns {string}
*/
private parseSimpleStrings(): string {
this.consumeToken('+');
let str = '';
while (this.getCurrentToken() !== '\r' && this.pos < this.inputLength) {
str += this.getCurrentToken();
this.consumeToken();
}
this.consumeToken('\r');
this.consumeToken('\n');
return str;
}
/**
* Parses a Bulk String
* e.g - "$5\r\nHello\r\n"
*
* @private
* @returns {(string | null)}
*/
private parseBulkStrings(): string | null {
this.consumeToken('$');
const length = this.getLength();
if (length === -1) {
return null;
}
let str = '';
let i = 0;
while (i < length && this.pos < this.inputLength) {
str += this.getCurrentToken();
this.consumeToken();
i++;
}
this.consumeToken('\r');
this.consumeToken('\n');
return str;
}
/**
* This function is used by the parseBulkStrings and parseArrays.
* It parses the length/size provided for Bulk String or Array respectively.
*
* @private
* @returns {number}
*/
private getLength(): number {
let ans = 0;
if (this.getCurrentToken() === '-') {
this.consumeToken('-');
this.consumeToken('1');
this.consumeToken('\r');
this.consumeToken('\n');
return -1;
}
while (this.pos < this.inputLength && this.getCurrentToken() !== '\r') {
ans = ans * 10 + parseInt(this.getCurrentToken());
this.consumeToken();
}
this.consumeToken('\r');
this.consumeToken('\n');
return ans;
}
/**
* Parses an Array.
* The elements of an Array can be:
* Simple String, Integer, Error, Bulk Strings or Arrays.
* e.g. - "*1\r\n+Hello\r\n"
*
* @private
* @returns {RespArray}
*/
private parseArrays(): RespArray {
this.consumeToken('*');
const arrayLength = this.getLength();
if (arrayLength === -1) {
return null;
}
if (arrayLength === 0) {
return [];
}
const arr = new Array<RespType>(arrayLength);
let i = 0;
while (i < arrayLength) {
if (this.pos >= this.inputLength) {
throw new Error(
`Index out of bounds ${this.pos} >= ${this.inputLength}`
);
}
const elem = this.parseValue();
arr[i] = elem;
i++;
}
return arr;
}
/**
* Consumes the given token if provided.
* Increments the index.
*
* @private
* @param {?string} [token]
*/
private consumeToken(token?: string) {
if (token) {
if (this.getCurrentToken() !== token) {
throw new Error(
`Invalid Token at ${this.pos}. Expected ${JSON.stringify(
token
)} but found ${JSON.stringify(this.getCurrentToken())}`
);
}
}
this.pos++;
}
/**
* Returns the character the current index is pointing to in the input.
*
* @private
* @returns {*}
*/
private getCurrentToken() {
return this.input[this.pos];
}
}