Permalink
Browse files

Work in progress, major refactor

  • Loading branch information...
1 parent d223b4a commit 668b1eb7a4849f963cce6b079bf72ea8a6b300c0 @c4milo committed May 19, 2012
Showing with 525 additions and 145 deletions.
  1. +84 −35 index.js
  2. +4 −4 lib/console.js
  3. +233 −4 lib/debugger/debugger-client.js
  4. +35 −30 lib/debugger/index.js
  5. +46 −1 lib/debugger/translator.js
  6. +11 −11 lib/profiler.js
  7. +112 −60 webkit-devtools-agent.js
View
@@ -2,19 +2,28 @@ var agents = require('./lib');
var spawn = require('child_process').spawn;
var WebSocketServer = require('ws').Server;
-var DevToolsAgent = function() {
+/**
+ * DevToolsAgent
+ * @constructor
+ **/
+var DevToolsAgent = module.exports = function() {
+ this.loadedAgents = {};
this.proxy = null;
this.server = null;
this.socket = null;
- this.agents = {};
};
(function() {
- //Private variables
- var proxyPID = 0;
-
- //Private functions
- var spawnProxy = 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
@@ -24,7 +33,6 @@ var DevToolsAgent = function() {
env: process.env,
cwd: __dirname
});
- proxyPID = this.proxy.pid;
this.proxy.stderr.setEncoding('utf8');
this.proxy.stderr.on('data', function (data) {
@@ -35,20 +43,32 @@ var DevToolsAgent = function() {
this.proxy.stdout.on('data', function (data) {
console.log(data);
});
- }.bind(this);
+ };
- var onProxyConnection = function(socket) {
+ /**
+ * Proxy connection handler
+ *
+ * @param {net.Socket} socket The just opened network socket.
+ * @api private
+ **/
+ this.onProxyConnection = function(socket) {
console.log('webkit-devtools-agent: A proxy got connected.');
console.log('webkit-devtools-agent: Waiting for commands...');
this.socket = socket;
- this.socket.on('message', onProxyData);
+ this.socket.on('message', this.onProxyData.bind(this));
this.socket.on('error', function(error) {
console.error(error);
});
- }.bind(this);
+ };
- var onProxyData = function(message) {
+ /**
+ * 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 {
@@ -60,7 +80,7 @@ var DevToolsAgent = function() {
var id = data.id;
var command = data.method.split('.');
- var domain = this.agents[command[0]];
+ var domain = this.loadedAgents[command[0]];
var method = command[1];
var params = data.params;
@@ -77,28 +97,42 @@ var DevToolsAgent = function() {
self.socket.send(JSON.stringify(response));
});
- }.bind(this);
+ };
- var sendEvent = function(data) {
+ /**
+ * 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(data));
- }.bind(this);
+ this.socket.send(JSON.stringify(notification));
+ };
- var loadAgents = function() {
- var self = this;
+ /**
+ * Loads every agent required at the top of this file.
+ * @private
+ **/
- var runtimeAgent = new agents.Runtime(sendEvent);
- this.agents = {};
+ 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.agents[agent] = new agents[agent](sendEvent, runtimeAgent);
+ this.loadedAgents[agent] = new agents[agent](this.notify.bind(this), runtimeAgent);
}
}
- this.agents.Runtime = runtimeAgent;
- }.bind(this);
+ this.loadedAgents.Runtime = runtimeAgent;
+ };
- // Public functions
+ /**
+ * Starts node-webkit-agent
+ *
+ * @api public
+ **/
this.start = function() {
var self = this;
@@ -114,24 +148,29 @@ var DevToolsAgent = function() {
'service process...');
//Spawns webkit devtools proxy / websockets server
- spawnProxy();
+ self.spawnProxy();
- loadAgents();
+ self.loadAgents();
});
- this.server.on('connection', onProxyConnection);
+ this.server.on('connection', this.onProxyConnection.bind(this));
};
+ /**
+ * Stops node-webkit-agent
+ *
+ * @api public
+ **/
this.stop = function() {
console.log('webkit-devtools-agent: Terminating websockets service' +
- ' with PID: ' + proxyPID + '...');
+ ' with PID: ' + this.proxy.pid + '...');
if (this.socket) {
- this.socket.end();
+ this.socket.close();
+ this.socket = null;
}
- if (proxyPID) {
- process.kill(proxyPID, 'SIGTERM');
- proxyPID = 0;
+ if (this.proxy && this.proxy.pid) {
+ process.kill(this.proxy.pid, 'SIGTERM');
}
if (this.server) {
@@ -141,9 +180,16 @@ var DevToolsAgent = function() {
};
}).call(DevToolsAgent.prototype);
+/**
+ * Creates an instance of the main function.
+ **/
var nodeAgent = new DevToolsAgent();
-//Prepares signal handler to activate the agent
+/**
+ * Prepares signal handler to activate the agent
+ * upon `SIGUSR2` signal which usually is triggered by `kill -SIGUSR2`
+ * in unix environments.
+ **/
if (!module.parent) {
nodeAgent.start();
} else {
@@ -156,6 +202,9 @@ if (!module.parent) {
});
}
+/**
+ * Avoids process termination due to uncaught exceptions
+ **/
['exit', 'uncaughtException'].forEach(function(e) {
process.on(e, function(e) {
if (e) {
View
@@ -1,8 +1,8 @@
var util = require('util');
-function ConsoleAgent(sendEvent, runtimeAgent) {
+function ConsoleAgent(notify, runtimeAgent) {
var self = this;
- this.sendEvent = sendEvent;
+ this.notify = notify;
this.runtimeAgent = runtimeAgent;
this.enabled = false;
this.messages = [];
@@ -32,15 +32,15 @@ function ConsoleAgent(sendEvent, runtimeAgent) {
//TODO save messages when this agent is disabled.
//self.messages.push(message);
- sendEvent(message);
+ notify(message);
};
});
}
(function() {
this.enable = function(params, sendResult) {
for(var i = 0, len = this.messages.length; i < len; i++) {
- this.sendEvent(this.messages[i]);
+ this.notify(this.messages[i]);
}
sendResult({result: this.enabled});
};
Oops, something went wrong. Retry.

0 comments on commit 668b1eb

Please sign in to comment.