Skip to content

Commit

Permalink
stream: encapsulate buffer-list
Browse files Browse the repository at this point in the history
PR-URL: #28974
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: James M Snell <[email protected]>
Reviewed-By: Luigi Pinca <[email protected]>
Reviewed-By: Matteo Collina <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
  • Loading branch information
ronag authored and targos committed Aug 19, 2019
1 parent 112ec73 commit 647f3a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
13 changes: 6 additions & 7 deletions lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,16 +331,15 @@ Readable.prototype.setEncoding = function(enc) {
// If setEncoding(null), decoder.encoding equals utf8
this._readableState.encoding = this._readableState.decoder.encoding;

const buffer = this._readableState.buffer;
// Iterate over current buffer to convert already stored Buffers:
let p = this._readableState.buffer.head;
let content = '';
while (p !== null) {
content += decoder.write(p.data);
p = p.next;
for (const data of buffer) {
content += decoder.write(data);
}
this._readableState.buffer.clear();
buffer.clear();
if (content !== '')
this._readableState.buffer.push(content);
buffer.push(content);
this._readableState.length = content.length;
return this;
};
Expand Down Expand Up @@ -374,7 +373,7 @@ function howMuchToRead(n, state) {
if (Number.isNaN(n)) {
// Only flow one buffer at a time
if (state.flowing && state.length)
return state.buffer.head.data.length;
return state.buffer.first().length;
else
return state.length;
}
Expand Down
6 changes: 6 additions & 0 deletions lib/internal/streams/buffer_list.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ module.exports = class BufferList {
return this.head.data;
}

*[Symbol.iterator]() {
for (let p = this.head; p; p = p.next) {
yield p.data;
}
}

// Consumes a specified amount of characters from the buffered data.
_getString(n) {
var p = this.head;
Expand Down
18 changes: 18 additions & 0 deletions test/parallel/test-stream-buffer-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,35 @@ assert.deepStrictEqual(emptyList.concat(0), Buffer.alloc(0));

const buf = Buffer.from('foo');

function testIterator(list, count) {
// test iterator
let len = 0;
// eslint-disable-next-line no-unused-vars
for (const x of list) {
len++;
}
assert.strictEqual(len, count);
}

// Test buffer list with one element.
const list = new BufferList();
testIterator(list, 0);

list.push(buf);
testIterator(list, 1);
for (const x of list) {
assert.strictEqual(x, buf);
}

const copy = list.concat(3);
testIterator(copy, 3);

assert.notStrictEqual(copy, buf);
assert.deepStrictEqual(copy, buf);

assert.strictEqual(list.join(','), 'foo');

const shifted = list.shift();
testIterator(list, 0);
assert.strictEqual(shifted, buf);
assert.deepStrictEqual(list, new BufferList());

0 comments on commit 647f3a8

Please sign in to comment.