| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 112ec73 commit 647f3a8
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -331,16 +331,15 @@ Readable.prototype.setEncoding = function(enc) { | |||
| 331 | 331 | // If setEncoding(null), decoder.encoding equals utf8 | |
| 332 | 332 | this._readableState.encoding = this._readableState.decoder.encoding; | |
| 333 | 333 | ||
| 334 | + const buffer = this._readableState.buffer; | ||
| 334 | 335 | // Iterate over current buffer to convert already stored Buffers: | |
| 335 | - let p = this._readableState.buffer.head; | ||
| 336 | 336 | 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); | ||
| 340 | 339 | } | |
| 341 | - this._readableState.buffer.clear(); | ||
| 340 | + buffer.clear(); | ||
| 342 | 341 | if (content !== '') | |
| 343 | - this._readableState.buffer.push(content); | ||
| 342 | + buffer.push(content); | ||
| 344 | 343 | this._readableState.length = content.length; | |
| 345 | 344 | return this; | |
| 346 | 345 | }; | |
@@ -374,7 +373,7 @@ function howMuchToRead(n, state) { | |||
| 374 | 373 | if (Number.isNaN(n)) { | |
| 375 | 374 | // Only flow one buffer at a time | |
| 376 | 375 | if (state.flowing && state.length) | |
| 377 | - return state.buffer.head.data.length; | ||
| 376 | + return state.buffer.first().length; | ||
| 378 | 377 | else | |
| 379 | 378 | return state.length; | |
| 380 | 379 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -90,6 +90,12 @@ module.exports = class BufferList { | |||
| 90 | 90 | return this.head.data; | |
| 91 | 91 | } | |
| 92 | 92 | ||
| 93 | + *[Symbol.iterator]() { | ||
| 94 | + for (let p = this.head; p; p = p.next) { | ||
| 95 | + yield p.data; | ||
| 96 | + } | ||
| 97 | + } | ||
| 98 | + | ||
| 93 | 99 | // Consumes a specified amount of characters from the buffered data. | |
| 94 | 100 | _getString(n) { | |
| 95 | 101 | var p = this.head; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -16,17 +16,35 @@ assert.deepStrictEqual(emptyList.concat(0), Buffer.alloc(0)); | |||
| 16 | 16 | ||
| 17 | 17 | const buf = Buffer.from('foo'); | |
| 18 | 18 | ||
| 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 | + | ||
| 19 | 29 | // Test buffer list with one element. | |
| 20 | 30 | const list = new BufferList(); | |
| 31 | + testIterator(list, 0); | ||
| 32 | + | ||
| 21 | 33 | list.push(buf); | |
| 34 | + testIterator(list, 1); | ||
| 35 | + for (const x of list) { | ||
| 36 | + assert.strictEqual(x, buf); | ||
| 37 | + } | ||
| 22 | 38 | ||
| 23 | 39 | const copy = list.concat(3); | |
| 40 | + testIterator(copy, 3); | ||
| 24 | 41 | ||
| 25 | 42 | assert.notStrictEqual(copy, buf); | |
| 26 | 43 | assert.deepStrictEqual(copy, buf); | |
| 27 | 44 | ||
| 28 | 45 | assert.strictEqual(list.join(','), 'foo'); | |
| 29 | 46 | ||
| 30 | 47 | const shifted = list.shift(); | |
| 48 | + testIterator(list, 0); | ||
| 31 | 49 | assert.strictEqual(shifted, buf); | |
| 32 | 50 | assert.deepStrictEqual(list, new BufferList()); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments