diff --git a/lib/_debug_agent.js b/lib/_debug_agent.js index 58293b17f84db5..dbe7d8644b21e5 100644 --- a/lib/_debug_agent.js +++ b/lib/_debug_agent.js @@ -97,7 +97,7 @@ function Client(agent, socket) { // Parse incoming data this.state = 'headers'; this.headers = {}; - this.buffer = ''; + this.buffer = Buffer.alloc(0); socket.pipe(this); this.on('data', this.onCommand); @@ -117,7 +117,7 @@ Client.prototype.destroy = function destroy(msg) { Client.prototype._transform = function _transform(data, enc, cb) { cb(); - this.buffer += data; + this.buffer = Buffer.concat([this.buffer, data]); while (true) { if (this.state === 'headers') { @@ -125,7 +125,8 @@ Client.prototype._transform = function _transform(data, enc, cb) { if (!this.buffer.includes('\r\n')) break; - if (this.buffer.startsWith('\r\n')) { + var bufString = this.buffer.toString('utf8'); + if (bufString.startsWith('\r\n')) { this.buffer = this.buffer.slice(2); this.state = 'body'; continue; @@ -133,23 +134,24 @@ Client.prototype._transform = function _transform(data, enc, cb) { // Match: // Header-name: header-value\r\n - var match = this.buffer.match(/^([^:\s\r\n]+)\s*:\s*([^\s\r\n]+)\r\n/); + var match = bufString.match(/^([^:\s\r\n]+)\s*:\s*([^\s\r\n]+)\r\n/); if (!match) return this.destroy('Expected header, but failed to parse it'); this.headers[match[1].toLowerCase()] = match[2]; - this.buffer = this.buffer.slice(match[0].length); + this.buffer = this.buffer.slice(Buffer.byteLength(match[0], 'utf8')); } else { var len = this.headers['content-length']; if (len === undefined) return this.destroy('Expected content-length'); len = len | 0; - if (Buffer.byteLength(this.buffer) < len) + if (Buffer.byteLength(this.buffer, 'utf8') < len) break; - this.push(new Command(this.headers, this.buffer.slice(0, len))); + this.push(new Command(this.headers, + this.buffer.slice(0, len).toString('utf8'))); this.state = 'headers'; this.buffer = this.buffer.slice(len); this.headers = {};