-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
185 lines (172 loc) · 6.41 KB
/
server.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
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
var http = require('http');
var url = require('url');
var fs = require('fs');
var path = require('path');
var MarkdownIt = require('markdown-it');
var WebSocketServer = require('websocket').server;
var pl = require('tau-prolog');
require('tau-prolog/modules/lists')(pl);
var tau_version = pl.version.major + "." + pl.version.minor + "." + pl.version.patch;
var files = {
listing: fs.readFileSync("./client/html/listing.html"),
notebook: fs.readFileSync("./client/html/notebook.html")
};
function json_to_html(data) {
var code = "<script type=\"text/javascript\">window.addEventListener(\"load\", function() {";
for(var i = 0; i < data.blocks.length; i++) {
var block = data.blocks[i];
code += "add_" + block.type + "_block(" + JSON.stringify(block) + ");";
}
code += "});</script>";
return code;
}
// HTTP SERVER
var http_server = http.createServer(function(req, res) {
var q = url.parse(req.url, true);
var pathname = q.pathname.substr(1);
if(pathname === "")
pathname = ".";
if(pathname[0] === ":")
pathname = pathname.substr(1);
// if file doesn't exist
try {
if(!fs.existsSync(pathname)) {
res.writeHead(404);
res.write('No encontrado');
res.end();
return;
}
} catch(err) {
console.log(err);
return;
}
// if pathname is a directory
if(fs.lstatSync(pathname).isDirectory()) {
var ls = fs.readdirSync(pathname).map(file =>
"<div>" + (fs.lstatSync(pathname + "/" + file).isDirectory() ?
"<i class=\"fas fa-folder\"></i>" :
"<i class=\"far fa-file-alt\"></i>") +
" <a href=\"" + pathname + "/" + file + "\">" + file + "</a></div>").join("");
res.writeHead(200, {"Content-Type": "text/html"});
res.write(files.listing.toString()
.replace(/\(\$version\)/g, tau_version)
.replace(/\(\$content\)/g, ls)
.replace(/\(\$dirname\)/g, __dirname));
res.end();
// if pathname is a regular file
} else {
// if pathname is a Tau Prolog notebook
if(path.extname(pathname) === ".plnotebook") {
var data = JSON.parse(fs.readFileSync(pathname));
var blocks = json_to_html(data);
res.writeHead(200, {"Content-Type": "text/html"});
res.write(files.notebook.toString()
.replace(/\(\$version\)/g, tau_version)
.replace(/\(\$content\)/g, blocks)
.replace(/\(\$dirname\)/g, __dirname));
res.end();
// if pathname is an image
} else if([".png", ".jpg", ".gif", ".jpeg"].indexOf(path.extname(pathname)) !== -1) {
var extension = path.extname(pathname).substr(1);
res.writeHead(200, {"Content-Type": "image/" + extension});
res.write(fs.readFileSync(pathname), "binary");
res.end();
// if pathname is a plain-text file
} else {
var extension = path.extname(pathname).substr(1);
res.writeHead(200, {"Content-Type": "text/" + extension});
res.write(fs.readFileSync(pathname));
res.end();
}
}
}).listen(8080, '127.0.0.1');
// WEBSOCKET SERVER
var ws_server = new WebSocketServer({
httpServer: http_server
});
ws_server.on('request', function(request) {
var connection = request.accept(null, request.origin);
var session = pl.create();
var threads = {};
var md = new MarkdownIt();
connection.on('message', function(message) {
if(message.type !== 'utf8') {
connection.close();
return;
}
var data = JSON.parse(message.utf8Data);
if(threads[data.id] === undefined)
threads[data.id] = new pl.type.Thread(session);
var thread = threads[data.id];
var result = "";
// consult block
if(data.type === "consult") {
var raw_result = thread.consult(data.content);
result = pl.format_answer(raw_result, thread);
var warnings = thread.get_warnings();
for(var i = 0; i < warnings.length; i++)
result += "<br />" + warnings[i].toString();
// send response
connection.send(JSON.stringify({
type: data.type,
id: data.id,
content: result,
execution: data.execution,
status: raw_result === true
}));
// query block
} else if(data.type === "query") {
var raw_result = thread.query(data.content);
result = pl.format_answer(raw_result, thread);
var warnings = thread.get_warnings();
for(var i = 0; i < warnings.length; i++)
result += "<br />" + warnings[i].toString();
// send response
connection.send(JSON.stringify({
type: data.type,
id: data.id,
content: result,
execution: data.execution,
status: raw_result === true
}));
// answer block
} else if(data.type === "answer") {
thread.answer(function(answer) {
// send response
connection.send(JSON.stringify({
type: data.type,
id: data.id,
content: pl.format_answer(answer, thread),
execution: data.execution,
status: !pl.type.is_error(answer) && answer != null && answer !== false
}));
});
// markdown block
} else if(data.type === "markdown") {
// send response
connection.send(JSON.stringify({
type: data.type,
id: data.id,
content: md.render(data.content),
status: true
}));
// save
} else if(data.type === "save") {
try {
fs.writeFile(data.path, data.content, function() {
// send response
connection.send(JSON.stringify({
type: data.type,
content: "saved",
status: "success"
}));
});
} catch(err) {
// An error occurred
console.error(err);
}
}
});
connection.on('close', function(connection) {
});
});