-
Notifications
You must be signed in to change notification settings - Fork 0
/
requestHandler.js
52 lines (48 loc) · 1.46 KB
/
requestHandler.js
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
const store = require('./store');
const RESPParser = require('./respParser');
async function handleRequest(data, connection) {
const parser = new RESPParser(data);
const input = await parser.readNewRequest();
// console.log('RESP parsed request:', input);
const command = input[0].toLowerCase();
switch (command) {
case 'command': {
connection.write('+OK\r\n');
break;
}
case 'ping': {
connection.write('+PONG\r\n');
break;
}
case 'echo': {
const value = input[1];
connection.write(`$${value.length}\r\n${value}\r\n`);
break;
}
case 'set': {
const key = input[1];
const value = input[2];
store[key] = value;
connection.write('+OK\r\n');
break;
}
case 'get': {
const key = input[1];
const value = store[key];
if (!value) {
connection.write('$-1\r\n');
} else {
connection.write(`$${value.length}\r\n${value}\r\n`);
}
break;
}
// case 'config': {
// connection.write('*2\r\n$3\r\n900\r\n$1\r\n1\r\n');
// break;
// }
default: {
connection.write('-ERR unknown command\r\n');
}
}
}
module.exports = { handleRequest };