summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Nagy <ronagy@icloud.com>2019-08-05 13:26:29 +0200
committerRich Trott <rtrott@gmail.com>2019-08-09 11:11:53 -0700
commitc15b4969e9f79cbe6d436a223390dbacb5329dfc (patch)
tree904f66c51c40f08b1a2bc526b0b2773e0d448e22
parent8fdd6345175d7c7a02ede1ba7e70d6e79d167149 (diff)
downloadandroid-node-v8-c15b4969e9f79cbe6d436a223390dbacb5329dfc.tar.gz
android-node-v8-c15b4969e9f79cbe6d436a223390dbacb5329dfc.tar.bz2
android-node-v8-c15b4969e9f79cbe6d436a223390dbacb5329dfc.zip
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>
-rw-r--r--lib/_stream_readable.js13
-rw-r--r--lib/internal/streams/buffer_list.js6
-rw-r--r--test/parallel/test-stream-buffer-list.js18
3 files changed, 30 insertions, 7 deletions
diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js
index 754e45da64..1830eea66d 100644
--- a/lib/_stream_readable.js
+++ b/lib/_stream_readable.js
@@ -337,16 +337,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;
};
@@ -380,7 +379,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;
}
diff --git a/lib/internal/streams/buffer_list.js b/lib/internal/streams/buffer_list.js
index 5bfa762e85..9dbf6f6769 100644
--- a/lib/internal/streams/buffer_list.js
+++ b/lib/internal/streams/buffer_list.js
@@ -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;
diff --git a/test/parallel/test-stream-buffer-list.js b/test/parallel/test-stream-buffer-list.js
index d7728d1171..c78efeea24 100644
--- a/test/parallel/test-stream-buffer-list.js
+++ b/test/parallel/test-stream-buffer-list.js
@@ -16,11 +16,28 @@ 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);
@@ -28,5 +45,6 @@ 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());