-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
99 lines (94 loc) · 2.51 KB
/
index.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
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
#!/usr/bin/env node
var chalk = require('chalk'); // console colors
const WebSocket = require('ws'); // websocket
var { getReference, getItems, createReference, createCatalog, editReference } = require("./lib/reffs");
var wss = new WebSocket.Server({
port: process.env.CANDY_PORT || 4540
});
wss.on('connection', function connection(ws) {
ws.on('error', () => console.log(chalk.yellow("Something happened. Continuing...")));
ws.on('message', function incoming(message) {
var messageObj = JSON.parse(message);
switch (messageObj.type) {
case "reference":
console.log(chalk.cyan(`Request to read "${messageObj.path}".`))
var ref = getReference(messageObj.path);
if (typeof ref != "undefined") {
ws.send(JSON.stringify({
type: messageObj.type,
reference: ref
}));
} else {
ws.send(JSON.stringify({
type: messageObj.type,
error: 404
}));
}
break;
case "list":
var items = getItems(messageObj.path);
if (typeof items != "undefined") {
ws.send(JSON.stringify({
type: messageObj.type,
items: items
}));
} else {
ws.send(JSON.stringify({
type: messageObj.type,
error: 404
}));
}
break;
case "newCatalog":
var result = createCatalog(messageObj.path);
if (typeof result != "undefined") {
ws.send(JSON.stringify({
type: messageObj.type,
path: messageObj.path
}));
} else {
ws.send(JSON.stringify({
type: messageObj.type,
error: 400
}));
}
break;
case "newReference":
var result = createReference(messageObj.path, messageObj.data);
if (typeof result != "undefined") {
ws.send(JSON.stringify({
type: messageObj.type,
path: messageObj.path
}));
} else {
ws.send(JSON.stringify({
type: messageObj.type,
error: 400
}));
}
break;
case "edit":
var result = editReference(messageObj.path, messageObj.data);
if (typeof result != "undefined") {
ws.send(JSON.stringify({
type: messageObj.type,
path: messageObj.path
}));
} else {
ws.send(JSON.stringify({
type: messageObj.type,
error: 404
}));
}
break;
default:
ws.send("{}");
break;
}
});
});
wss.on('error', () => console.log(chalk.yellow("Something happened. Continuing...")));
console.log(chalk.cyan("Testing getItems:"));
console.log(JSON.stringify(getItems("/")));
console.log(chalk.cyan("Testing getReference:"));
console.log(JSON.stringify(getReference("/helloWorld")));