forked from ianb/jsshell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
126 lines (126 loc) · 3.36 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
(function() {
var envProgram, fs, http, jsonpRunProgram, mainServer, nullString, path, requestJSON, serveStatic, spawn, sys, url;
sys = require('sys');
http = require('http');
path = require('path');
fs = require('fs');
url = require('url');
spawn = require('child_process').spawn;
nullString = (new Buffer([0])).toString();
requestJSON = function(req, func) {
var allData;
allData = '';
req.on('data', function(data) {
return allData += data;
});
return req.on('end', function() {
var reqBody;
reqBody = JSON.parse(allData);
return func(reqBody);
});
};
jsonpRunProgram = function(req, res) {
var args, callback, callbackId, cwd, env, name, params, proc, sendData, value, _ref;
params = url.parse(req.url, true).query;
cwd = params.cwd;
env = process.env;
if (params.env) {
_ref = JSON.parse(params.env);
for (name in _ref) {
value = _ref[name];
if (value === null) {
delete env[name];
} else {
env[name] = value;
}
}
}
args = params.args ? JSON.parse(params.args) : [];
callback = params.callback;
callbackId = params.callbackId ? JSON.parse(params.callbackId) : null;
console.log('args', JSON.stringify([
params, params.command, args, {
cwd: cwd,
env: params.env
}
]));
proc = spawn(params.command, args, {
cwd: cwd,
env: env
});
res.writeHead(200, {
'Content-Type': 'text/javascript'
});
sendData = function(data) {
return res.write(callback + '(' + JSON.stringify(callbackId) + ', ' + JSON.stringify(data) + ')\n');
};
sendData({
pid: proc.pid
});
res.write('\n');
proc.stdout.on('data', function(data) {
return sendData({
stdout: data.toString()
});
});
proc.stderr.on('data', function(data) {
return sendData({
stderr: data.toString()
});
});
return proc.on('exit', function(code, signal) {
sendData({
code: code
});
return res.end();
});
};
envProgram = function(req, res) {
res.writeHead(200, {
'Content-Type': 'application/json'
});
return res.end(JSON.stringify(process.env));
};
serveStatic = function(req, res) {
var filename, p;
p = url.parse(req.url).pathname;
if (!p || p === "/") {
p = "/index.html";
}
filename = path.join(process.cwd(), 'static', p);
return path.exists(filename, function(exists) {
if (!exists) {
res.writeHead(404, {
"Content-Type": "text/plain"
});
res.end("404 Not Found\n");
return;
}
return fs.readFile(filename, "binary", function(err, file) {
if (err) {
res.writeHead(500, {
"Content-Type": "text/plain"
});
res.write(err + "\n");
return;
}
res.writeHead(200);
return res.end(file, "binary");
});
});
};
mainServer = function(req, res) {
var p;
p = url.parse(req.url).pathname;
if (req.method === 'POST') {
return xhrRunProgram(req, res);
} else if (p === '/call') {
return jsonpRunProgram(req, res);
} else if (p === '/env') {
return envProgram(req, res);
} else {
return serveStatic(req, res);
}
};
http.createServer(mainServer).listen(8000);
}).call(this);