stream: encapsulate buffer-list

PR-URL: https://github.com/nodejs/node/pull/28974
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
Robert Nagy 2019-08-05 13:26:29 +02:00 committed by Rich Trott
parent 8fdd634517
commit c15b4969e9
3 changed files with 30 additions and 7 deletions

View File

@ -337,16 +337,15 @@ Readable.prototype.setEncoding = function(enc) {
// If setEncoding(null), decoder.encoding equals utf8 // If setEncoding(null), decoder.encoding equals utf8
this._readableState.encoding = this._readableState.decoder.encoding; this._readableState.encoding = this._readableState.decoder.encoding;
const buffer = this._readableState.buffer;
// Iterate over current buffer to convert already stored Buffers: // Iterate over current buffer to convert already stored Buffers:
let p = this._readableState.buffer.head;
let content = ''; let content = '';
while (p !== null) { for (const data of buffer) {
content += decoder.write(p.data); content += decoder.write(data);
p = p.next;
} }
this._readableState.buffer.clear(); buffer.clear();
if (content !== '') if (content !== '')
this._readableState.buffer.push(content); buffer.push(content);
this._readableState.length = content.length; this._readableState.length = content.length;
return this; return this;
}; };
@ -380,7 +379,7 @@ function howMuchToRead(n, state) {
if (Number.isNaN(n)) { if (Number.isNaN(n)) {
// Only flow one buffer at a time // Only flow one buffer at a time
if (state.flowing && state.length) if (state.flowing && state.length)
return state.buffer.head.data.length; return state.buffer.first().length;
else else
return state.length; return state.length;
} }

View File

@ -90,6 +90,12 @@ module.exports = class BufferList {
return this.head.data; 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. // Consumes a specified amount of characters from the buffered data.
_getString(n) { _getString(n) {
var p = this.head; var p = this.head;

View File

@ -16,11 +16,28 @@ assert.deepStrictEqual(emptyList.concat(0), Buffer.alloc(0));
const buf = Buffer.from('foo'); 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. // Test buffer list with one element.
const list = new BufferList(); const list = new BufferList();
testIterator(list, 0);
list.push(buf); list.push(buf);
testIterator(list, 1);
for (const x of list) {
assert.strictEqual(x, buf);
}
const copy = list.concat(3); const copy = list.concat(3);
testIterator(copy, 3);
assert.notStrictEqual(copy, buf); assert.notStrictEqual(copy, buf);
assert.deepStrictEqual(copy, buf); assert.deepStrictEqual(copy, buf);
@ -28,5 +45,6 @@ assert.deepStrictEqual(copy, buf);
assert.strictEqual(list.join(','), 'foo'); assert.strictEqual(list.join(','), 'foo');
const shifted = list.shift(); const shifted = list.shift();
testIterator(list, 0);
assert.strictEqual(shifted, buf); assert.strictEqual(shifted, buf);
assert.deepStrictEqual(list, new BufferList()); assert.deepStrictEqual(list, new BufferList());