Skip to content

Commit 647f3a8

Browse files
ronagtargos
authored andcommitted
stream: encapsulate buffer-list
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]>
1 parent 112ec73 commit 647f3a8

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

lib/_stream_readable.js

+6-7
Original file line numberDiff line numberDiff line change
@@ -331,16 +331,15 @@ Readable.prototype.setEncoding = function(enc) {
331331
// If setEncoding(null), decoder.encoding equals utf8
332332
this._readableState.encoding = this._readableState.decoder.encoding;
333333

334+
const buffer = this._readableState.buffer;
334335
// Iterate over current buffer to convert already stored Buffers:
335-
let p = this._readableState.buffer.head;
336336
let content = '';
337-
while (p !== null) {
338-
content += decoder.write(p.data);
339-
p = p.next;
337+
for (const data of buffer) {
338+
content += decoder.write(data);
340339
}
341-
this._readableState.buffer.clear();
340+
buffer.clear();
342341
if (content !== '')
343-
this._readableState.buffer.push(content);
342+
buffer.push(content);
344343
this._readableState.length = content.length;
345344
return this;
346345
};
@@ -374,7 +373,7 @@ function howMuchToRead(n, state) {
374373
if (Number.isNaN(n)) {
375374
// Only flow one buffer at a time
376375
if (state.flowing && state.length)
377-
return state.buffer.head.data.length;
376+
return state.buffer.first().length;
378377
else
379378
return state.length;
380379
}

lib/internal/streams/buffer_list.js

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ module.exports = class BufferList {
9090
return this.head.data;
9191
}
9292

93+
*[Symbol.iterator]() {
94+
for (let p = this.head; p; p = p.next) {
95+
yield p.data;
96+
}
97+
}
98+
9399
// Consumes a specified amount of characters from the buffered data.
94100
_getString(n) {
95101
var p = this.head;

test/parallel/test-stream-buffer-list.js

+18
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,35 @@ assert.deepStrictEqual(emptyList.concat(0), Buffer.alloc(0));
1616

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

19+
function testIterator(list, count) {
20+
// test iterator
21+
let len = 0;
22+
// eslint-disable-next-line no-unused-vars
23+
for (const x of list) {
24+
len++;
25+
}
26+
assert.strictEqual(len, count);
27+
}
28+
1929
// Test buffer list with one element.
2030
const list = new BufferList();
31+
testIterator(list, 0);
32+
2133
list.push(buf);
34+
testIterator(list, 1);
35+
for (const x of list) {
36+
assert.strictEqual(x, buf);
37+
}
2238

2339
const copy = list.concat(3);
40+
testIterator(copy, 3);
2441

2542
assert.notStrictEqual(copy, buf);
2643
assert.deepStrictEqual(copy, buf);
2744

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

3047
const shifted = list.shift();
48+
testIterator(list, 0);
3149
assert.strictEqual(shifted, buf);
3250
assert.deepStrictEqual(list, new BufferList());

0 commit comments

Comments
 (0)