Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid leaving an empty buffer on complete consume #25

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ tape('consuming from multiple buffers', function (t) {
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)

t.end()
})

tape('test readUInt8 / readInt8', function (t) {
var buf1 = new Buffer(1)
, buf2 = new Buffer(3)
Expand Down