-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.js
204 lines (174 loc) · 5.51 KB
/
index.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
var agents = require('./lib');
var spawn = require('child_process').spawn;
var WebSocketServer = require('ws').Server;
/**
* DevToolsAgent
* @constructor
**/
var DevToolsAgent = function () {
this.loadedAgents = {};
this.proxy = null;
this.server = null;
this.socket = null;
};
(function () {
/**
* Spawns a new process with a websockets service proxy
* to serve devtools front-end requests.
*
* All the messages but debugging messages
* are sent to the main process. Debugger Agent lives in this proxy.
*
* @api private
**/
this.spawnProxy = function () {
var self = this;
//Parent PID for the proxy to know to whom to send the SIGUSR1 signal
process.env.PARENT_PID = process.pid;
this.proxy = spawn('node', [__dirname + '/webkit-devtools-agent.js',
this.port, this.bind_to, this.ipc_port,
this.verbose], process.argv, {
env: process.env,
cwd: __dirname
});
this.proxy.stderr.setEncoding('utf8');
this.proxy.stderr.on('data', function (data) {
console.error(data);
});
this.proxy.stdout.setEncoding('utf8');
this.proxy.stdout.on('data', function (data) {
if (this.verbose) {
console.log(data);
}
});
};
/**
* Proxy connection handler
*
* @param {net.Socket} socket The just opened network socket.
* @api private
**/
this.onProxyConnection = function (socket) {
if (this.verbose) {
console.log('webkit-devtools-agent: A proxy got connected.');
console.log('webkit-devtools-agent: Waiting for commands...');
}
this.socket = socket;
this.socket.on('message', this.onProxyData.bind(this));
this.socket.on('error', function(error) {
console.error(error);
});
};
/**
* Handler for data events coming from the proxy process.
*
* @param {String} message A message coming from the proxy process.
* @api private
**/
this.onProxyData = function (message) {
var self = this;
try {
data = JSON.parse(message);
} catch(e) {
console.log(e.stack);
return;
}
var id = data.id;
var command = data.method.split('.');
var domain = this.loadedAgents[command[0]];
var method = command[1];
var params = data.params;
if (!domain || !domain[method]) {
console.warn('%s is not implemented', data.method);
return;
}
domain[method](params, function(result) {
var response = {
id: id,
result: result
};
self.socket.send(JSON.stringify(response));
});
};
/**
* Notification function in charge of sending events
* to the front-end following the protocol specified
* at https://developers.google.com/chrome-developer-tools/docs/protocol/1.0
*
* @param {Object} A notification object that follows devtools protocol 1.0
* @api private
**/
this.notify = function (notification) {
if (!this.socket) return;
this.socket.send(JSON.stringify(notification));
};
/**
* Loads every agent required at the top of this file.
* @private
**/
this.loadAgents = function () {
var runtimeAgent = new agents.Runtime(this.notify.bind(this));
for (var agent in agents) {
if (typeof agents[agent] == 'function' && agent != 'Runtime') {
this.loadedAgents[agent] = new agents[agent](this.notify.bind(this), runtimeAgent);
}
}
this.loadedAgents.Runtime = runtimeAgent;
};
/**
* Starts node-webkit-agent
*
* @api public
**/
this.start = function (config) {
var self = this;
config = config || {}
this.port = config.port || 9999;
this.bind_to = config.bind_to || 'localhost';
this.ipc_port = config.ipc_port || 3333;
this.verbose = config.verbose || false;
if (this.server) {
return;
}
this.server = new WebSocketServer({
port: this.ipc_port,
bind_to: this.bind_to
});
this.server.on('listening', function() {
if (this.verbose) {
console.log('webkit-devtools-agent: Spawning websocket ' +
'service process...');
}
//Spawns webkit devtools proxy / websockets server
self.spawnProxy();
self.loadAgents();
});
this.server.on('connection', this.onProxyConnection.bind(this));
};
/**
* Stops node-webkit-agent
*
* @api public
**/
this.stop = function () {
if (this.socket) {
this.socket.close();
this.socket = null;
}
if (this.proxy && this.proxy.pid) {
if (this.verbose) {
console.log('webkit-devtools-agent: Terminating websockets service' +
' with PID: ' + this.proxy.pid + '...');
}
process.kill(this.proxy.pid, 'SIGTERM');
}
if (this.server) {
this.server.close();
this.server = null;
if (this.verbose) {
console.log('webkit-devtools-agent: stopped');
}
}
};
}).call(DevToolsAgent.prototype);
module.exports = new DevToolsAgent();