From 0c659edf5fe6b9fb82f11c60adf8a1569ea6df7a Mon Sep 17 00:00:00 2001 From: Sergey Date: Tue, 14 Mar 2017 15:38:36 +0400 Subject: [PATCH 1/3] debugger: fix debugger failure when passing commands with non-latin characters --- lib/_debug_agent.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/_debug_agent.js b/lib/_debug_agent.js index 58293b17f84db5..4f14091161714a 100644 --- a/lib/_debug_agent.js +++ b/lib/_debug_agent.js @@ -146,12 +146,17 @@ Client.prototype._transform = function _transform(data, enc, cb) { return this.destroy('Expected content-length'); len = len | 0; - if (Buffer.byteLength(this.buffer) < len) + var bufLength = Buffer.byteLength(this.buffer, 'utf8'); + if (bufLength < len) break; - this.push(new Command(this.headers, this.buffer.slice(0, len))); + var buf = Buffer.allocUnsafe(bufLength); + buf.write(this.buffer, 0, bufLength, 'utf8'); + var body = buf.slice(0, len).toString('utf8'); + + this.push(new Command(this.headers, body)); this.state = 'headers'; - this.buffer = this.buffer.slice(len); + this.buffer = buf.slice(len).toString('utf8'); this.headers = {}; } } From 2ef054d3c231fd4994c1ab3af6a2e97a831394ba Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 16 Mar 2017 09:51:24 +0400 Subject: [PATCH 2/3] change Client.buffer type from string to Buffer --- lib/_debug_agent.js | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/lib/_debug_agent.js b/lib/_debug_agent.js index 4f14091161714a..84b89a14f95122 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 = new Buffer(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,30 +134,25 @@ 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; - var bufLength = Buffer.byteLength(this.buffer, 'utf8'); - if (bufLength < len) + if (Buffer.byteLength(this.buffer, 'utf8') < len) break; - var buf = Buffer.allocUnsafe(bufLength); - buf.write(this.buffer, 0, bufLength, 'utf8'); - var body = buf.slice(0, len).toString('utf8'); - - this.push(new Command(this.headers, body)); + this.push(new Command(this.headers, this.buffer.slice(0, len).toString('utf8'))); this.state = 'headers'; - this.buffer = buf.slice(len).toString('utf8'); + this.buffer = this.buffer.slice(len); this.headers = {}; } } From a3cd3cf9d26b28a072f76deecab208fbce8be48d Mon Sep 17 00:00:00 2001 From: Sergey Date: Thu, 16 Mar 2017 13:30:46 +0400 Subject: [PATCH 3/3] fix --- lib/_debug_agent.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/_debug_agent.js b/lib/_debug_agent.js index 84b89a14f95122..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 = new Buffer(0); + this.buffer = Buffer.alloc(0); socket.pipe(this); this.on('data', this.onCommand); @@ -150,7 +150,8 @@ Client.prototype._transform = function _transform(data, enc, cb) { if (Buffer.byteLength(this.buffer, 'utf8') < len) break; - this.push(new Command(this.headers, this.buffer.slice(0, len).toString('utf8'))); + this.push(new Command(this.headers, + this.buffer.slice(0, len).toString('utf8'))); this.state = 'headers'; this.buffer = this.buffer.slice(len); this.headers = {};