-
Notifications
You must be signed in to change notification settings - Fork 1
/
alaska.js
52 lines (44 loc) · 1.22 KB
/
alaska.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
var vm = require('vm');
var http = require("http");
var sandbox = vm.createContext({ console: {log: console.log}});
var contextOptions = {displayErrors: true};
var debugOpt = '--debug true';
var debug = false;
process.argv.forEach(function (val, index, array) {
if (debugOpt === val) {
debug = true;
}
});
var webPrint = function(res, respBody) {
res.writeHead(200, {'Connection': 'keep-alive'});
res.write(respBody);
res.end();
};
var server = http.createServer(function(req, res) {
var contents = '';
req.on('data', function (dataIn) {
contents += dataIn;
});
req.on('end', function () {
try {
var result = vm.runInContext(contents, sandbox, contextOptions);
if (typeof(result) == 'undefined' && result !== null) {
webPrint(res, '["ok"]');
} else {
try {
webPrint(res, JSON.stringify(['ok', result]));
} catch (err) {
webPrint(res, '["err"]');
}
}
} catch(err) {
webPrint(res, JSON.stringify(["err", err.toString()]));
}
});
});
var port = process.env.PORT || 3001;
server.listen(port);
if (debug) {
console.log('Listening on port:', port);
console.error('Alaska.js started. Piping coffee to warmer climates.');
}