From f70d9924410da2907d6170776fa74eadd9d2334c Mon Sep 17 00:00:00 2001 From: Tommy Atkinson Date: Wed, 10 Feb 2016 13:14:40 +0100 Subject: [PATCH 1/2] Avoid leaving an empty buffer on complete consume --- bl.js | 2 +- test/test.js | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/bl.js b/bl.js index b979ba8..f0232e4 100644 --- a/bl.js +++ b/bl.js @@ -162,7 +162,7 @@ BufferList.prototype.toString = function (encoding, start, end) { BufferList.prototype.consume = function (bytes) { while (this._bufs.length) { - if (bytes > this._bufs[0].length) { + if (bytes >= this._bufs[0].length) { bytes -= this._bufs[0].length this.length -= this._bufs[0].length this._bufs.shift() diff --git a/test/test.js b/test/test.js index 8c8d0cf..664cae5 100644 --- a/test/test.js +++ b/test/test.js @@ -125,6 +125,10 @@ tape('consuming from multiple buffers', function (t) { t.equal(bl.length, 1) t.equal(bl.slice(0, 1).toString('ascii'), 'j') + bl.consume(1) + t.equal(bl.length, 0) + t.equal(bl._bufs.length, 0) + t.end() }) From 7cd5609ac8e232455761b4b80e4db1beef29137a Mon Sep 17 00:00:00 2001 From: Tommy Atkinson Date: Wed, 10 Feb 2016 16:02:22 +0100 Subject: [PATCH 2/2] Use separate test for complete consumption --- test/test.js | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/test/test.js b/test/test.js index 664cae5..2f0c6d3 100644 --- a/test/test.js +++ b/test/test.js @@ -125,7 +125,17 @@ tape('consuming from multiple buffers', function (t) { t.equal(bl.length, 1) t.equal(bl.slice(0, 1).toString('ascii'), 'j') - bl.consume(1) + t.end() +}) + +tape('complete consumption', function (t) { + var bl = new BufferList() + + bl.append(new Buffer('a')) + bl.append(new Buffer('b')) + + bl.consume(2) + t.equal(bl.length, 0) t.equal(bl._bufs.length, 0)