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