forked from Connorhd/node_debug
-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.js
219 lines (183 loc) · 4.96 KB
/
debug.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
var fu = require("./fu");
var debug = exports;
debug.listen = function (port, host) {
fu.listen(port, host);
};
fu.get("/", fu.staticHandler("index.html"));
fu.get("/screen.css", fu.staticHandler("screen.css"));
fu.get("/jquery.treeview.css", fu.staticHandler("jquery.treeview.css"));
fu.get("/jquery.js", fu.staticHandler("jquery.js"));
fu.get("/jquery.cookie.js", fu.staticHandler("jquery.cookie.js"));
fu.get("/jquery.treeview.js", fu.staticHandler("jquery.treeview.js"));
fu.get("/jquery.treeview.async.js", fu.staticHandler("jquery.treeview.async.js"));
fu.get("/treeview-default.gif", fu.staticHandler("treeview-default.gif"));
fu.get("/treeview-default-line.gif", fu.staticHandler("treeview-default-line.gif"));
fu.get("/eval", function (req, res) {
if (req.uri.params.eval !== undefined && req.uri.params.id !== undefined) {
res.simpleJSON(200, evalStr(req.uri.params.eval, req.uri.params.id));
} else {
res.simpleText(200, 'Error');
}
});
fu.get("/tree", function (req, res) {
if (req.uri.params.id !== undefined && req.uri.params.cmd !== undefined) {
if (req.uri.params.root !== undefined) {
res.simpleJSON(200, getObj(req.uri.params.id, req.uri.params.cmd, req.uri.params.root));
}
} else {
res.simpleText(200, 'Error');
}
});
fu.get("/console", function (req, res) {
if (req.uri.params.id !== undefined) {
handleConsole(req.uri.params.id, res);
} else {
res.simpleText(200, 'Error');
}
});
var SESSION_TIMEOUT = 60 * 1000;
var sessions = [];
function createSession (id) {
if (sessions[id] === undefined) {
// Create session
sessions[id] = {
timestamp: 0,
queue: [],
result: [],
cmd: 0
};
}
}
function handleConsole (id, res) {
createSession(id);
var session = sessions[id];
session.timestamp = new Date();
if (session.queue.length > 0) {
res.simpleJSON(200, session.queue);
delete sessions[id].res;
session.queue = [];
} else {
session.res = res;
session.timeout = setTimeout(function () { closeReq(res); delete sessions[id].res; }, 30000);
}
}
function closeReq (res) {
res.simpleJSON(200, []);
}
debug.log = function (msg) {
for (var id in sessions) {
if (!sessions.hasOwnProperty(id)) continue;
var session = sessions[id];
session.cmd += 1;
session.result[session.cmd] = msg;
session.queue.push(session.cmd);
if (session.timeout !== undefined) {
clearTimeout(session.timeout);
}
if (session.res !== undefined) {
session.res.simpleJSON(200, session.queue);
delete sessions[id].res;
session.queue = [];
}
}
}
// interval to kill off old sessions
setInterval(function () {
var now = new Date();
for (var id in sessions) {
if (!sessions.hasOwnProperty(id)) continue;
var session = sessions[id];
if (now - session.timestamp > SESSION_TIMEOUT) {
delete sessions[id];
}
}
}, 1000);
// Determine whether or not an object has children
function hasChildren(obj) {
var children = false
if (typeof(obj) !== 'string' && !(obj instanceof String)) {
for (var j in obj) {
children = true;
break;
}
}
return children;
}
function getObj(id, cmd, key) {
if (sessions[id] === undefined || sessions[id].result[cmd] === undefined)
return {};
// node.Timer causes a segfault
if (key === "Timer") {
return {};
}
var obj = sessions[id].result[cmd];
if (key === 'source') {
var output = {};
output.id = '_root';
output.text = obj.toString();
output.text = '<pre class="result">'+output.text+'</pre>'
if (hasChildren(obj) === true) {
output.hasChildren = true;
}
return [output];
}
if (key === '_root') {
delete key;
}
if (key !== undefined) {
var keys = key.split('.');
keys.forEach(function (x) {
obj = obj[x];
});
}
var output = [];
var cur;
var children = false;
for (var i in obj) {
// Prevent loops
if (obj === obj[i])
continue;
if (key !== undefined)
cur = {id: key+'.'+i};
else
cur = {id: i};
cur.text = '<strong><span class="type">'+typeof(obj[i])+'</span>'+i+'</strong>';
var str;
try {
str = JSON.stringify(obj[i]);
} catch (e) {
str = undefined;
}
if (str == undefined) {
try {
str = obj[i].toString();
} catch (e) {
str = '';
}
}
cur.text += '<pre>'+str+'</pre>';
if (hasChildren(obj[i]) === true)
cur.hasChildren = true;
output.push(cur);
}
return output;
}
function evalStr (str, id) {
createSession(id);
var result;
var error = false;
var output = {};
try {
result = eval(str);
sessions[id].cmd += 1;
sessions[id].result[sessions[id].cmd] = result;
} catch (err) {
error = true;
output.result = err;
output.result.stack = output.result.stack.substring(0,output.result.stack.indexOf('at eval at evalStr')-4);
}
output.error = error;
output.str = str;
output.cmd = sessions[id].cmd;
return output;
}