forked from faridnsh/node-webkit-agent
-
Notifications
You must be signed in to change notification settings - Fork 1
/
webkit-devtools-agent.js
executable file
·181 lines (152 loc) · 5 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
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
#!/usr/bin/env node
var Debugger = require('./lib/debugger');
var WebSocket = require('ws');
var WebSocketServer = WebSocket.Server;
var static = require('node-static');
var http = require('http');
var path = (function () {
var verMap = {
'0.6' : '19.0.1084.46',
'0.8' : '21.0.1180.57'
}
var ver = process.version.match(/^v(\d+\.\d+)/);
if (ver !== null) {
ver = ver[1];
if (verMap[ver]) {
return verMap[ver];
}
}
var vers = Object.keys(verMap);
// Unsupported version, use the last version.
return verMap[vers[vers.length]];
})();
var file = new static.Server('./public/' + path);
var DevToolsAgentProxy = module.exports = function() {
this.wss = null;
this.backend = null;
this.frontend = null;
this.debuggerAgent = null;
this.port = process.env.DEBUG_PORT || 9999;
this.host = process.env.DEBUG_HOST || '127.0.0.1';
};
(function() {
process.on('uncaughtException', function (err) {
console.error('webkit-devtools-agent: Websockets service uncaught exception: ');
console.error(err.stack);
});
this.onFrontendMessage = function(message) {
var self = this;
var data;
try {
data = JSON.parse(message);
} catch(e) {
console.log(e.stack);
}
var command = data.method.split('.');
var domainName = command[0];
if (domainName !== 'Debugger') {
this.backend.send(message);
return;
}
var id = data.id;
var method = command[1];
var params = data.params;
if (!this.debuggerAgent[method]) {
console.warn('%s is not implemented', data.method);
return;
}
this.debuggerAgent[method](params, function(result) {
var response = {
id: id,
result: result
};
self.frontend.send(JSON.stringify(response));
});
};
this.onFrontendConnection = function(socket) {
var self = this;
this.frontend = socket;
this.frontend.on('message', this.onFrontendMessage.bind(this));
console.log('webkit-devtools-agent: new frontend connection!');
this.debuggerAgent = new Debugger(process.env.PARENT_PID);
this.debuggerAgent.initialize(function(notification) {
self.frontend.send(JSON.stringify(notification));
});
};
/**
* Callback function invoked once
* a connection has been established
* with the backend process. It also
* initializes the websocket service
* for DevTools frontend to connect with.
*
* @api private
*/
this.onBackendOpen = function() {
//Starts websockets server for DevTools frontend
this.server = http.createServer(function (req, res) {
req.on('end', file.serve.bind(file, req, res));
}).listen(this.port, this.host);
this.wss = new WebSocketServer({
server : this.server
});
console.log('webkit-devtools-agent: Websockets ' +
'service started on %s:%s', this.host, this.port);
console.log('open http://%s:%s/inspector.html?host=%s:%s&page=0',
this.host, this.port, this.host, this.port);
this.wss.on('connection', this.onFrontendConnection.bind(this));
};
/**
* Callback function that forwards agent responses
* to DevTools frontend except for the Debugger
* agent which lives in the current process.
*
* @param {String} message JSON message as it comes from
* the main process.
* @api private
*/
this.onBackendMessage = function(message) {
if (!this.frontend) return;
this.frontend.send(message);
};
/**
* Starts proxy process.
* This process is in charge of receiving
* messages from DevTools frontend and forward them
* to the main process.
*
* DebuggerAgent is an exception to the rule of having
* all the agents in the main process. Given
* that the main process is going to be in debug mode
* we need the DebuggerAgent to live in a different process
* so that it can continue working and be responsive
* with DevTools frontend.
*
* @api public
**/
this.start = function() {
this.backend = new WebSocket('ws://localhost:3333');
this.backend.on('open', this.onBackendOpen.bind(this));
this.backend.on('message', this.onBackendMessage.bind(this));
};
/**
* Stops proxy process
*
* @api public
**/
this.stop = function() {
if (this.wss) {
this.wss.close();
this.wss = null;
console.log('webkit-devtools-agent: Websockets service with PID ' +
process.pid + ' has stopped');
}
};
}).call(DevToolsAgentProxy.prototype);
var proxy = new DevToolsAgentProxy();
proxy.start();
['exit', 'SIGTERM', 'SIGHUP'].forEach(function(s) {
process.on(s, function() {
proxy.stop();
});
});