-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.js
More file actions
76 lines (62 loc) · 1.5 KB
/
cli.js
File metadata and controls
76 lines (62 loc) · 1.5 KB
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
const net = require('net');
const readline = require('readline');
const { Deserialize } = require('./lib/serde');
const deserializer = new Deserialize();
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
const client = new net.Socket();
client.connect(9128, '127.0.0.1');
client.setKeepAlive(true, 900000);
client.setTimeout(900000);
function processCommand(req) {
req = req.trim();
let isCmd = false;
switch(req) {
case '.exit':
isCmd = true;
console.log('bye');
process.exit(0);
}
return isCmd;
}
function processQuery(query) {
client.write(`${query}\0`);
}
client.on('connect', () => {
console.log('Connection established');
prompt();
});
client.on('data', function(data) {
data = deserializer.parse(data);
data.forEach((res) => console.dir(res, {depth: null}));
prompt();
});
client.on('error', (err) => {
console.log('err!', err);
});
client.on('timeout', () => {
console.log('socket timeout');
client.destroy();
});
// Fired when server disconnects
client.on('end', () => {
console.log('Connection ended');
});
// Fired when socket is fully closed
client.on('close', () => {
console.log('Connection closed');
});
function prompt() {
rl.question("> ", (input) => {
if(!input) {
prompt();
return;
}
const cmdProcessed = processCommand(input);
if(!cmdProcessed) {
processQuery(input);
}
});
}