-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
136 lines (121 loc) · 2.99 KB
/
server.js
File metadata and controls
136 lines (121 loc) · 2.99 KB
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
require('./mod/require-uncache')(require);
require('./mod/copy-modules')();
var express = require('express');
var redis = {};
try {
redis = {
ready: false,
client: require('redis').createClient({
retry_strategy : function(options){
if (options.error.code === 'ECONNREFUSED') {
// Do not retry
return 'ECONNREFUSED';
}
}
}),
api : null
};
redis.client.on('connect', function() {
redis.ready = true;
redis.api = require('./mod/redis')(redis);
scope.store = redis.api.setKey;
scope.fetch = redis.api.getKey;
});
} catch(e){
console.error(e);
}
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socket;
var print;
var fs = require('fs');
var codeChunks={}
var commands = [];
var scope = {
print : require('./mod/print').print,
error : require('./mod/print').error
};
function init(uncache) {
if(uncache)
require.uncache();
codeChunks = require("./code/index.js")();
var status={}
var i = 0;
for (var chunk in codeChunks) {
i++;
try {
codeChunks[chunk](null, scope);
}
catch(err) {
delete codeChunks[chunk];
status[chunk]='>> ERROR: '+err;
}
}
commands=[];
for (var c in codeChunks) {
commands.push(c);
status[c]="OK"
}
return status;
}
init();
for (var c in codeChunks) {
commands.push(c);
}
app.use(express.static('client'));
io.on('connection', function(s){
socket = s;
socket.on('disconnect', function(){
});
socket.on('inputEnter', function(string){
var res =runCommand(string);
io.emit('outputEnter', res);
});
socket.on('inputTab', function(string){
var res = tabFind(string, commands)
io.emit('outputTab', res);
});
require('./mod/print').bind(socket, io);
});
// Random crash
var random = Math.floor(Math.random() * 2);
var noCrash = process.argv.filter(function noCrash(arg){
return arg === '--no-crash';
});
if (random || noCrash.length){
var port = 3000;
http.listen(port, function(){
console.log('Junk OS CLI started on port ' + port);
});
} else {
console.error('Junk OS CLI has crashed !');
process.exit(1);
}
function runCommand(input) {
var command = input['_command'];
var cmd=command.split(' ');
var c = cmd[0];
cmd.shift();
input['_command']=c;
input['_args']=cmd;
if(commands.indexOf(c)>-1) {
return codeChunks[c](input, scope);
}
else if(c=="reload") {
res = init(true);
return res;
}
return 'Command '+c+' not found'
}
// Inefficient as hell
function tabFind(key, array) {
var results = [];
var re = new RegExp('^'+key);
for (var i = 0; i < array.length; i++) {
if (array[i].match(re)) {
results.push(array[i]);
}
}
return results;
}