-
Notifications
You must be signed in to change notification settings - Fork 29.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
debug-agent: improvements #1977
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ const util = require('util'); | |
const Buffer = require('buffer').Buffer; | ||
const Transform = require('stream').Transform; | ||
|
||
const disconnectRequest = "{\"seq\":1,\"type\":\"request\",\"command\":\"disconnect\"}"; | ||
|
||
exports.start = function start() { | ||
var agent = new Agent(); | ||
|
||
|
@@ -45,18 +47,21 @@ exports.start = function start() { | |
|
||
function Agent() { | ||
net.Server.call(this, this.onConnection); | ||
this.maxConnections = 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where are we using this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This prevent debugger from accepting more than one client. Please, read #858 to understand, why we need to limit clients number. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add a comment about that, to someone unfamiliar with the entire |
||
|
||
this.first = true; | ||
this.binding = process._debugAPI; | ||
|
||
var self = this; | ||
this.binding.onmessage = function(msg) { | ||
self.clients.forEach(function(client) { | ||
client.send({}, msg); | ||
}); | ||
if (self.client) | ||
self.client.send({}, msg); | ||
}; | ||
|
||
this.clients = []; | ||
this.client = null; | ||
this.on('close', function() { | ||
self.client = null; | ||
}); | ||
assert(this.binding, 'Debugger agent running without bindings!'); | ||
} | ||
util.inherits(Agent, net.Server); | ||
|
@@ -65,13 +70,11 @@ Agent.prototype.onConnection = function onConnection(socket) { | |
var c = new Client(this, socket); | ||
|
||
c.start(); | ||
this.clients.push(c); | ||
this.client = c; | ||
|
||
var self = this; | ||
c.once('close', function() { | ||
var index = self.clients.indexOf(c); | ||
assert(index !== -1); | ||
self.clients.splice(index, 1); | ||
self.client = null; | ||
}); | ||
}; | ||
|
||
|
@@ -89,6 +92,7 @@ function Client(agent, socket) { | |
this.agent = agent; | ||
this.binding = this.agent.binding; | ||
this.socket = socket; | ||
this.disconnected = false; | ||
|
||
// Parse incoming data | ||
this.state = 'headers'; | ||
|
@@ -102,11 +106,17 @@ function Client(agent, socket) { | |
this.socket.on('close', function() { | ||
self.destroy(); | ||
}); | ||
this.socket.on('error', function(err) { | ||
// Silently handle socket errors | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we are silently ignoring the errors then why do we need this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unhandled error will be thrown and kills app. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also this is a place for discussion. I don't know how we need to handle debugger connection errors (My opinion is - we don't need to do anything on debugger connection errors, because this is debugging of debugger) |
||
}); | ||
} | ||
util.inherits(Client, Transform); | ||
|
||
Client.prototype.destroy = function destroy(msg) { | ||
this.socket.destroy(); | ||
|
||
if (!this.disconnected) | ||
this.binding.sendCommand(disconnectRequest); | ||
|
||
this.emit('close'); | ||
}; | ||
|
@@ -185,6 +195,11 @@ Client.prototype.onCommand = function onCommand(cmd) { | |
this.binding.sendCommand(cmd.body); | ||
|
||
this.agent.notifyWait(); | ||
|
||
if (cmd.body == disconnectRequest) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be better to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I won't use JSON.parse here because I'm worried about performance. So, how I can understand you worried about situation, when we receive from user a Unless you want to send two |
||
this.disconnected = true; | ||
this.destroy(); | ||
} | ||
}; | ||
|
||
function Command(headers, body) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any point in not using
JSON.stringify()
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is backported from old debugger implementation, there is no other reasons.