|
| 1 | +var assert = require('assert'); |
| 2 | +var net = require('net'); |
| 3 | +var util = require('util'); |
| 4 | +var Buffer = require('buffer').Buffer; |
| 5 | + |
| 6 | +var Transform = require('stream').Transform; |
| 7 | + |
| 8 | +exports.start = function start() { |
| 9 | + var agent = new Agent(); |
| 10 | + |
| 11 | + // Do not let `agent.listen()` request listening from cluster master |
| 12 | + var cluster = require('cluster'); |
| 13 | + cluster.isWorker = false; |
| 14 | + cluster.isMaster = true; |
| 15 | + |
| 16 | + agent.on('error', function(err) { |
| 17 | + process._rawDebug(err.stack || err); |
| 18 | + }); |
| 19 | + |
| 20 | + agent.listen(process._debugAPI.port, function() { |
| 21 | + var addr = this.address(); |
| 22 | + process._rawDebug('Debugger listening on port %d', addr.port); |
| 23 | + process._debugAPI.notifyListen(); |
| 24 | + }); |
| 25 | + |
| 26 | + // Just to spin-off events |
| 27 | + // TODO(indutny): Figure out why node.cc isn't doing this |
| 28 | + setImmediate(function() { |
| 29 | + }); |
| 30 | + |
| 31 | + process._debugAPI.onclose = function() { |
| 32 | + // We don't care about it, but it prevents loop from cleaning up gently |
| 33 | + // NOTE: removeAllListeners won't work, as it doesn't call `removeListener` |
| 34 | + process.listeners('SIGWINCH').forEach(function(fn) { |
| 35 | + process.removeListener('SIGWINCH', fn); |
| 36 | + }); |
| 37 | + |
| 38 | + agent.close(); |
| 39 | + }; |
| 40 | + |
| 41 | + // Not used now, but anyway |
| 42 | + return agent; |
| 43 | +}; |
| 44 | + |
| 45 | +function Agent() { |
| 46 | + net.Server.call(this, this.onConnection); |
| 47 | + |
| 48 | + this.first = true; |
| 49 | + this.binding = process._debugAPI; |
| 50 | + |
| 51 | + var self = this; |
| 52 | + this.binding.onmessage = function(msg) { |
| 53 | + self.clients.forEach(function(client) { |
| 54 | + client.send({}, msg); |
| 55 | + }); |
| 56 | + }; |
| 57 | + |
| 58 | + this.clients = []; |
| 59 | + assert(this.binding, 'Debugger agent running without bindings!'); |
| 60 | +} |
| 61 | +util.inherits(Agent, net.Server); |
| 62 | + |
| 63 | +Agent.prototype.onConnection = function onConnection(socket) { |
| 64 | + var c = new Client(this, socket); |
| 65 | + |
| 66 | + c.start(); |
| 67 | + this.clients.push(c); |
| 68 | + |
| 69 | + var self = this; |
| 70 | + c.once('close', function() { |
| 71 | + var index = self.clients.indexOf(c); |
| 72 | + assert(index !== -1); |
| 73 | + self.clients.splice(index, 1); |
| 74 | + }); |
| 75 | +}; |
| 76 | + |
| 77 | +Agent.prototype.notifyWait = function notifyWait() { |
| 78 | + if (this.first) |
| 79 | + this.binding.notifyWait(); |
| 80 | + this.first = false; |
| 81 | +}; |
| 82 | + |
| 83 | +function Client(agent, socket) { |
| 84 | + Transform.call(this); |
| 85 | + this._readableState.objectMode = true; |
| 86 | + |
| 87 | + this.agent = agent; |
| 88 | + this.binding = this.agent.binding; |
| 89 | + this.socket = socket; |
| 90 | + |
| 91 | + // Parse incoming data |
| 92 | + this.state = 'headers'; |
| 93 | + this.headers = {}; |
| 94 | + this.buffer = ''; |
| 95 | + socket.pipe(this); |
| 96 | + |
| 97 | + this.on('data', this.onCommand); |
| 98 | + |
| 99 | + var self = this; |
| 100 | + this.socket.on('close', function() { |
| 101 | + self.destroy(); |
| 102 | + }); |
| 103 | +} |
| 104 | +util.inherits(Client, Transform); |
| 105 | + |
| 106 | +Client.prototype.destroy = function destroy(msg) { |
| 107 | + this.socket.destroy(); |
| 108 | + |
| 109 | + this.emit('close'); |
| 110 | +}; |
| 111 | + |
| 112 | +Client.prototype._transform = function _transform(data, enc, cb) { |
| 113 | + cb(); |
| 114 | + |
| 115 | + this.buffer += data; |
| 116 | + |
| 117 | + while (true) { |
| 118 | + if (this.state === 'headers') { |
| 119 | + // Not enough data |
| 120 | + if (!/\r\n/.test(this.buffer)) |
| 121 | + break; |
| 122 | + |
| 123 | + if (/^\r\n/.test(this.buffer)) { |
| 124 | + this.buffer = this.buffer.slice(2); |
| 125 | + this.state = 'body'; |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + // Match: |
| 130 | + // Header-name: header-value\r\n |
| 131 | + var match = this.buffer.match(/^([^:\s\r\n]+)\s*:\s*([^\s\r\n]+)\r\n/); |
| 132 | + if (!match) |
| 133 | + return this.destroy('Expected header, but failed to parse it'); |
| 134 | + |
| 135 | + this.headers[match[1].toLowerCase()] = match[2]; |
| 136 | + |
| 137 | + this.buffer = this.buffer.slice(match[0].length); |
| 138 | + } else { |
| 139 | + var len = this.headers['content-length']; |
| 140 | + if (len === undefined) |
| 141 | + return this.destroy('Expected content-length'); |
| 142 | + |
| 143 | + len = len | 0; |
| 144 | + if (Buffer.byteLength(this.buffer) < len) |
| 145 | + break; |
| 146 | + |
| 147 | + this.push(new Command(this.headers, this.buffer.slice(0, len))); |
| 148 | + this.state = 'headers'; |
| 149 | + this.buffer = this.buffer.slice(len); |
| 150 | + this.headers = {}; |
| 151 | + } |
| 152 | + } |
| 153 | +}; |
| 154 | + |
| 155 | +Client.prototype.send = function send(headers, data) { |
| 156 | + if (!data) |
| 157 | + data = ''; |
| 158 | + |
| 159 | + var out = []; |
| 160 | + Object.keys(headers).forEach(function(key) { |
| 161 | + out.push(key + ': ' + headers[key]); |
| 162 | + }); |
| 163 | + out.push('Content-Length: ' + Buffer.byteLength(data), ''); |
| 164 | + |
| 165 | + this.socket.cork(); |
| 166 | + this.socket.write(out.join('\r\n') + '\r\n'); |
| 167 | + |
| 168 | + if (data.length > 0) |
| 169 | + this.socket.write(data); |
| 170 | + this.socket.uncork(); |
| 171 | +}; |
| 172 | + |
| 173 | +Client.prototype.start = function start() { |
| 174 | + this.send({ |
| 175 | + Type: 'connect', |
| 176 | + 'V8-Version': process.versions.v8, |
| 177 | + 'Protocol-Version': 1, |
| 178 | + 'Embedding-Host': 'node ' + process.version |
| 179 | + }); |
| 180 | +}; |
| 181 | + |
| 182 | +Client.prototype.onCommand = function onCommand(cmd) { |
| 183 | + this.binding.sendCommand(cmd.body); |
| 184 | + |
| 185 | + this.agent.notifyWait(); |
| 186 | +}; |
| 187 | + |
| 188 | +function Command(headers, body) { |
| 189 | + this.headers = headers; |
| 190 | + this.body = body; |
| 191 | +} |
0 commit comments