/
webkit-devtools-agent.js
75 lines (61 loc) · 1.83 KB
/
webkit-devtools-agent.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
var WebSocketServer = require('ws').Server;
var agents = require('./lib');
var wss;
var port = process.env.DEBUG_PORT || 1337;
var host = process.env.DEBUG_HOST || '127.0.0.1';
function start() {
wss = new WebSocketServer({port: port, host: host});
console.log('webkit-devtools-agent started on %s:%s', host, port);
wss.on('connection', function(socket) {
function sendEvent(event) {
socket.send(JSON.stringify(event));
}
for (var agent in agents) {
if (typeof agents[agent] == 'function') {
agents[agent] = new agents[agent](sendEvent);
}
}
socket.on('message', function(message) {
try {
message = JSON.parse(message);
} catch(e) {
console.log(e);
return;
}
//console.log(message);
var id = message.id;
var command = message.method.split('.');
var domain = agents[command[0]];
var method = command[1];
var params = message.params;
if (!domain || !domain[method]) {
console.warn('%s is not implemented', message.method);
return;
}
domain[method](params, function(result) {
socket.send(JSON.stringify({ id: id, result: result }));
});
});
});
}
function stop() {
if (wss) {
wss.close();
console.log('webkit-devtools-agent stopped');
}
}
if (!module.parent) {
start();
} else {
process.on('SIGUSR2', function() {
if (wss) {
stop();
} else {
start();
}
});
}
process.on('uncaughtException', function (err) {
console.error('webkit-devtools-agent: uncaughtException: ');
console.error(err.stack);
});