forked from EduMake/RPi-WaterbearNodeJS
-
Notifications
You must be signed in to change notification settings - Fork 2
/
waterbear.js
134 lines (109 loc) · 4.07 KB
/
waterbear.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
var fs = require('fs'),
ws = require('ws'),
util = require('util'),
url = require('url'),
http = require('http'),
path = require('path'),
mime = require('mime'),
vm = require('vm');
var childprocesses = [];
process.on('SIGINT',function(){
for (var kid in childprocesses) {
if( childprocesses.hasOwnProperty( kid ) )
{
childprocesses[kid].kill('SIGINT');
}
}
process.exit();
});
var httpServer = http.createServer( function(request, response) {
var aURL = url.parse(request.url, true);
var pathname = aURL.pathname;
if (pathname === "/")
{
pathname = "index.html";
}
var filename = path.join(process.cwd(), 'waterbear', pathname);
fs.exists(filename, function(exists) {
if (!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found");
response.end();
return;
}
response.writeHead(200, {'Content-Type': mime.lookup(filename)});
fs.createReadStream(filename, {
'flags': 'r',
'encoding': 'binary',
'mode': 666,
'bufferSize': 4 * 1024
}).addListener("data", function(chunk) {
response.write(chunk, 'binary');
}).addListener("close",function() {
response.end();
});
});
});
httpServer.listen(8000);
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: 8080});
wss.on('connection', function(ws) {
ws.on('message', function(message) {
var msg = JSON.parse(message);
console.log("msg =", msg);
if(msg.command === "run")
{
if(typeof msg.code !== 'null')
{
var code = msg.code;
console.log("code =", code);
fs.writeFile("current.js", code);
var spawn = require('child_process').spawn,
ls = spawn('node', ['current.js']);
console.log("ls.pid =", ls.pid);
var reply = JSON.stringify({"type":"running", "pid":ls.pid});
ws.send(reply);
childprocesses[ls.pid] = ls;
childprocesses[ls.pid].stdout.on('data', function (data) {
console.log('stdout: ' + data);
reply = JSON.stringify({"type":"stdout", "pid":ls.pid, "data":data});
ws.send(reply);
});
childprocesses[ls.pid].stderr.on('data', function (data) {
console.log('stderr: ' + data);
if (/^execvp\(\)/.test(data)) {
reply = JSON.stringify({"type":"error", "pid":ls.pid, "data":data});
ws.send(reply);
}
else
{
reply = JSON.stringify({"type":"sterr", "pid":ls.pid, "data":data});
ws.send(reply);
}
});
childprocesses[ls.pid].on('exit', function (code) {
console.log('childprocesses process exited with code ' + code);
if(code === 0 )
{
reply = JSON.stringify({"type":"completed", "pid":ls.pid});
}
else
{
reply = JSON.stringify({"type":"exit", "pid":ls.pid, "data":code});
}
ws.send(reply);
});
}
}
else if(msg.command === "kill")
{
if(typeof msg.pid !== 'null')
{
if(typeof childprocesses[msg.pid] !== 'null')
{
childprocesses[msg.pid].kill('SIGINT');
}
}
}
});
});