refactored debug-server
This commit is contained in:
Родитель
98e9ab884a
Коммит
25670cbd19
|
@ -1,6 +1,6 @@
|
|||
#!/usr/bin/env node
|
||||
|
||||
var dserver = require('../lib/debug-server'),
|
||||
var DebugServer = require('../lib/debug-server').DebugServer,
|
||||
fs = require('fs'),
|
||||
path = require('path'),
|
||||
options = {};
|
||||
|
@ -12,7 +12,6 @@ process.argv.forEach(function (arg) {
|
|||
if (parts.length > 1) {
|
||||
switch (parts[0]) {
|
||||
case '--web-port':
|
||||
case '--agent-port':
|
||||
options.webPort = parseInt(parts[1], 10);
|
||||
break;
|
||||
default:
|
||||
|
@ -21,7 +20,7 @@ process.argv.forEach(function (arg) {
|
|||
}
|
||||
}
|
||||
else if (parts[0] === '--help') {
|
||||
console.log('Usage: node [node_options] debug-agent.js [options]');
|
||||
console.log('Usage: node-inspector [options]');
|
||||
console.log('Options:');
|
||||
console.log('--web-port=[port] port to host the inspector (default 8080)');
|
||||
process.exit();
|
||||
|
@ -30,7 +29,8 @@ process.argv.forEach(function (arg) {
|
|||
});
|
||||
|
||||
fs.readFile(path.join(__dirname, '../config.json'), function(err, data) {
|
||||
var config;
|
||||
var config,
|
||||
dserver;
|
||||
if (err) {
|
||||
console.warn("could not load config.json\n" + err.toString());
|
||||
config = {};
|
||||
|
@ -49,8 +49,11 @@ fs.readFile(path.join(__dirname, '../config.json'), function(err, data) {
|
|||
if (!config.debugPort) {
|
||||
config.debugPort = 5858;
|
||||
}
|
||||
dserver.create(options, config).on('close', function () {
|
||||
|
||||
dserver = new DebugServer();
|
||||
dserver.on('close', function () {
|
||||
console.log('session closed');
|
||||
process.exit();
|
||||
});
|
||||
dserver.listen(options.webPort || config.webPort);
|
||||
});
|
||||
|
|
|
@ -1,65 +1,63 @@
|
|||
var Http = require('http'),
|
||||
EventEmitter = require('events').EventEmitter,
|
||||
path = require('path'),
|
||||
WebSocket = require('websocket-server'),
|
||||
paperboy = require('paperboy'),
|
||||
Session = require('./session'),
|
||||
WEBROOT = path.join(__dirname, '../front-end'),
|
||||
sessions = {};
|
||||
inherits = require('util').inherits,
|
||||
WEBROOT = require('path').join(__dirname, '../front-end'),
|
||||
sessions = {},
|
||||
webPort;
|
||||
|
||||
function staticFile(req, res) {
|
||||
function serveStaticFiles(req, res) {
|
||||
req.url = req.url.replace(/^\/debug/, '/');
|
||||
paperboy.deliver(WEBROOT, req, res);
|
||||
}
|
||||
|
||||
function override(options, defaults) {
|
||||
var result = {};
|
||||
Object.keys(defaults).forEach(function(key) {
|
||||
result[key] = options[key] || defaults[key];
|
||||
});
|
||||
return result;
|
||||
function getDebuggerPort(url, defaultPort) {
|
||||
return parseInt((/\?port=(\d+)/.exec(url) || [null, defaultPort])[1], 10);
|
||||
}
|
||||
|
||||
exports.create = function(options, config) {
|
||||
var defaults = { webPort: config.webPort },
|
||||
settings = override(options || {}, defaults),
|
||||
httpServer = Http.createServer(staticFile),
|
||||
wsServer = WebSocket.createServer({server: httpServer}),
|
||||
debugPort = config.debugPort.toString();
|
||||
function getSession(debuggerPort) {
|
||||
var session = sessions[debuggerPort];
|
||||
if (!session) {
|
||||
session = Session.create(debuggerPort, {}); // TODO fix config
|
||||
sessions[debuggerPort] = session;
|
||||
// TODO session on close
|
||||
}
|
||||
return session;
|
||||
}
|
||||
|
||||
wsServer.on('connection', function(conn) {
|
||||
var port =
|
||||
parseInt((/\?port=(\d+)/.exec(conn._req.url) || [null, debugPort])[1], 10),
|
||||
session = sessions[port];
|
||||
if (!session) {
|
||||
session = Session.create(port, config);
|
||||
sessions[port] = session;
|
||||
}
|
||||
session.join(conn)
|
||||
// XXX should session and debugger be bound together?
|
||||
});
|
||||
function handleWebSocketConnection(conn) {
|
||||
var port = getDebuggerPort(conn._req.url, 5858); // TODO
|
||||
getSession(port).join(conn)
|
||||
}
|
||||
|
||||
wsServer.listen(settings.webPort);
|
||||
function handleServerListening() {
|
||||
console.log(
|
||||
'visit http://0.0.0.0:' +
|
||||
webPort +
|
||||
'/debug?port=5858 to start debugging'); // TODO port
|
||||
}
|
||||
|
||||
wsServer.on('listening', function() {
|
||||
console.log(
|
||||
'visit http://0.0.0.0:' +
|
||||
settings.webPort +
|
||||
'/debug?port=5858 to start debugging');
|
||||
});
|
||||
function DebugServer() {
|
||||
var httpServer = Http.createServer(serveStaticFiles);
|
||||
this.wsServer = WebSocket.createServer({ server: httpServer });
|
||||
this.wsServer.on('connection', handleWebSocketConnection);
|
||||
this.wsServer.on('listening', handleServerListening);
|
||||
}
|
||||
|
||||
return Object.create(EventEmitter.prototype, {
|
||||
close: {
|
||||
value: function()
|
||||
{
|
||||
if (wsServer) {
|
||||
wsServer.close();
|
||||
}
|
||||
this.emit('close');
|
||||
}
|
||||
},
|
||||
webPort: {
|
||||
get: function() { return settings.webPort; }
|
||||
}
|
||||
});
|
||||
};
|
||||
inherits(DebugServer, EventEmitter);
|
||||
|
||||
DebugServer.prototype.close = function() {
|
||||
if (this.wsServer) {
|
||||
this.wsServer.close();
|
||||
this.emit('close');
|
||||
}
|
||||
}
|
||||
|
||||
DebugServer.prototype.listen = function(port) {
|
||||
webPort = port;
|
||||
this.wsServer.listen(port);
|
||||
}
|
||||
|
||||
exports.DebugServer = DebugServer;
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
var http = require('http'),
|
||||
events = require('events'),
|
||||
debugr = require('./debugger'),
|
||||
path = require('path');
|
||||
var events = require('events'),
|
||||
debugr = require('./debugger');
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// exports
|
||||
|
|
Загрузка…
Ссылка в новой задаче