From 4e6df0f4e477f4a3e100001e7a7bf39816a57d65 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 23 Jun 2017 16:25:51 +0300 Subject: [PATCH 1/5] Skip content-length header for messages with string body --- .gitignore | 1 + lib/frame.js | 8 +++--- test/frame.test.js | 64 +++++++++++++++++++++++++++++++++++++++++----- 3 files changed, 63 insertions(+), 10 deletions(-) diff --git a/.gitignore b/.gitignore index 07e6e47..81211c4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /node_modules +.vscode \ No newline at end of file diff --git a/lib/frame.js b/lib/frame.js index 65f0fd5..cfcef39 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -23,10 +23,10 @@ StompFrame.prototype.send = function(stream) { for (var key in this.headers) { frame += key + ':' + this.headers[key] + '\n'; } - if (this.body.length > 0) { - if (!this.headers.hasOwnProperty('suppress-content-length')) { - frame += 'content-length:' + Buffer.byteLength(this.body) + '\n'; - } + // User content-length for non-strings and if suppress-content-length header doesn't exist + let addContentLength = !(typeof this.body === 'string') && !this.headers.hasOwnProperty('suppress-content-length'); + if (addContentLength && this.body.length > 0) { + frame += 'content-length:' + Buffer.byteLength(this.body) + '\n'; } frame += '\n'; if (this.body.length > 0) { diff --git a/test/frame.test.js b/test/frame.test.js index a278ddc..f1515cd 100644 --- a/test/frame.test.js +++ b/test/frame.test.js @@ -61,7 +61,7 @@ module.exports = testCase({ 'header1': 'value1', 'header2': 'value2' }, - 'body': 'wewp de doo' + 'body': Buffer.from('wewp de doo') }); // Command before headers, content-length auto-inserted, and terminating with null char (line feed chars for each line too) @@ -125,7 +125,7 @@ module.exports = testCase({ 'test content-length header is present when suppress-content-length is not': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : 'Content length is 20' + 'body' : Buffer.from('Content length is 20') }); frame.send(connectionObserver); @@ -152,10 +152,24 @@ module.exports = testCase({ test.equal(containsContentLengthHeader, false, "Content length header should not exist since we are suppressing it"); test.done(); }, - 'test stream write correctly handles single-byte UTF-8 characters': function(test) { + 'test content-length is not present when frame.body is string': function(test) { + var frame = new StompFrame({ + 'command': 'SEND', + 'headers': {}, + 'body' : 'Content length is 20' + }); + frame.send(connectionObserver); + + //Check the headers for the content-length header + var writtenString = connectionObserver.writeBuffer.join(''); + var containsContentLengthHeader = (writtenString.split("\n").indexOf("content-length:20") == -1 ? false : true); + test.equal(containsContentLengthHeader, false, "Content length header should not exist since we are suppressing it"); + test.done(); + }, + 'test stream write correctly handles single-byte UTF-8 characters as a buffer': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : 'Welcome!' + 'body' : Buffer.from('Welcome!') }); frame.send(connectionObserver); @@ -168,10 +182,29 @@ module.exports = testCase({ test.done(); }, - 'test stream write correctly handles multi-byte UTF-8 characters': function(test) { + 'test stream write correctly handles single-byte UTF-8 characters as a string': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : 'Ẇḗḽḉớḿẽ☃' + 'body' : 'Welcome!' + }); + frame.send(connectionObserver); + + var expectedStream = [ + 'SEND\n\n', + 'Welcome!', + '\0' + ]; + + var writtenString = connectionObserver.writeBuffer.join(''); + + test.equal(expectedStream.join(''), writtenString, 'Sent content should be eqaul to expected content'); + + test.done(); + }, + 'test stream write correctly handles multi-byte UTF-8 characters as a buffer': function(test) { + var frame = new StompFrame({ + 'command': 'SEND', + 'body' : Buffer.from('Ẇḗḽḉớḿẽ☃') }); frame.send(connectionObserver); @@ -182,6 +215,25 @@ module.exports = testCase({ test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server"); + test.done(); + }, + 'test stream write correctly handles multi-byte UTF-8 characters as a string': function(test) { + var frame = new StompFrame({ + 'command': 'SEND', + 'body' : 'Ẇḗḽḉớḿẽ☃' + }); + frame.send(connectionObserver); + + var expectedStream = [ + 'SEND\n\n', + 'Ẇḗḽḉớḿẽ☃', + '\0' + ]; + + var writtenString = connectionObserver.writeBuffer.join(''); + + test.equal(expectedStream.join(''), writtenString, 'Sent content should be eqaul to expected content'); + test.done(); } From 73c87389d593395bca8492d50e3d1da5e7bf3ff9 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 23 Jun 2017 17:44:35 +0300 Subject: [PATCH 2/5] fix es6 compatibility --- lib/frame.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/frame.js b/lib/frame.js index cfcef39..5f62441 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -24,7 +24,7 @@ StompFrame.prototype.send = function(stream) { frame += key + ':' + this.headers[key] + '\n'; } // User content-length for non-strings and if suppress-content-length header doesn't exist - let addContentLength = !(typeof this.body === 'string') && !this.headers.hasOwnProperty('suppress-content-length'); + var addContentLength = !(typeof this.body === 'string') && !this.headers.hasOwnProperty('suppress-content-length'); if (addContentLength && this.body.length > 0) { frame += 'content-length:' + Buffer.byteLength(this.body) + '\n'; } From fe4a5be7942af12da96b7eae7a8a2aca93e0763b Mon Sep 17 00:00:00 2001 From: max Date: Fri, 23 Jun 2017 17:54:01 +0300 Subject: [PATCH 3/5] Support old and new nodejs Buffer api --- test/frame.test.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/test/frame.test.js b/test/frame.test.js index f1515cd..bf68eaa 100644 --- a/test/frame.test.js +++ b/test/frame.test.js @@ -11,6 +11,11 @@ connectionObserver.write = function(data) { this.writeBuffer.push(data); }; +// To support old and new nodejs Buffer api +var bufferFromString = function(str) { + return Buffer.from ? Buffer.from(str) : new Buffer(str); +} + module.exports = testCase({ setUp: function(callback) { @@ -61,7 +66,7 @@ module.exports = testCase({ 'header1': 'value1', 'header2': 'value2' }, - 'body': Buffer.from('wewp de doo') + 'body': bufferFromString('wewp de doo') }); // Command before headers, content-length auto-inserted, and terminating with null char (line feed chars for each line too) @@ -125,7 +130,7 @@ module.exports = testCase({ 'test content-length header is present when suppress-content-length is not': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : Buffer.from('Content length is 20') + 'body' : bufferFromString('Content length is 20') }); frame.send(connectionObserver); @@ -169,7 +174,7 @@ module.exports = testCase({ 'test stream write correctly handles single-byte UTF-8 characters as a buffer': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : Buffer.from('Welcome!') + 'body' : bufferFromString('Welcome!') }); frame.send(connectionObserver); @@ -204,7 +209,7 @@ module.exports = testCase({ 'test stream write correctly handles multi-byte UTF-8 characters as a buffer': function(test) { var frame = new StompFrame({ 'command': 'SEND', - 'body' : Buffer.from('Ẇḗḽḉớḿẽ☃') + 'body' : bufferFromString('Ẇḗḽḉớḿẽ☃') }); frame.send(connectionObserver); From 2b52f9b4d93988ea9174f44b0faf0d2fde7ca348 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 23 Jun 2017 18:25:14 +0300 Subject: [PATCH 4/5] Get buffer/array length instead of Buffer.byteLength --- lib/frame.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/frame.js b/lib/frame.js index 5f62441..1777a58 100644 --- a/lib/frame.js +++ b/lib/frame.js @@ -26,7 +26,8 @@ StompFrame.prototype.send = function(stream) { // User content-length for non-strings and if suppress-content-length header doesn't exist var addContentLength = !(typeof this.body === 'string') && !this.headers.hasOwnProperty('suppress-content-length'); if (addContentLength && this.body.length > 0) { - frame += 'content-length:' + Buffer.byteLength(this.body) + '\n'; + // this.body is Buffer or ArrayBuffer + frame += 'content-length:' + this.body.length + '\n'; } frame += '\n'; if (this.body.length > 0) { From b7b43f845c82488739a8a244862fecdce1af24e5 Mon Sep 17 00:00:00 2001 From: max Date: Fri, 23 Jun 2017 18:29:27 +0300 Subject: [PATCH 5/5] Fix tests on nodejs 0.10 --- test/frame.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/frame.test.js b/test/frame.test.js index bf68eaa..e2b751d 100644 --- a/test/frame.test.js +++ b/test/frame.test.js @@ -183,7 +183,7 @@ module.exports = testCase({ var contentLengthHeaderLine = writtenString.split("\n")[1]; var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim(); - test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server"); + test.equal(frame.body.length, contentLengthValue, "We should be truthful about how much data we plan to send to the server"); test.done(); }, @@ -218,7 +218,7 @@ module.exports = testCase({ var contentLengthHeaderLine = writtenString.split("\n")[1]; var contentLengthValue = contentLengthHeaderLine.split(":")[1].trim(); - test.equal(Buffer.byteLength(frame.body), contentLengthValue, "We should be truthful about how much data we plan to send to the server"); + test.equal(frame.body.length, contentLengthValue, "We should be truthful about how much data we plan to send to the server"); test.done(); },