diff --git a/lib/_stream_readable.js b/lib/_stream_readable.js index 6cfdb754f8c033..ae1b49ba4f6c4c 100644 --- a/lib/_stream_readable.js +++ b/lib/_stream_readable.js @@ -250,7 +250,7 @@ function howMuchToRead(n, state) { if (n !== n) { // Only flow one buffer at a time if (state.flowing && state.length) - return state.buffer.head.data.length; + return Buffer.byteLength(state.buffer.head.data); else return state.length; } diff --git a/test/parallel/test-stream-readable-null-buffer-head.js b/test/parallel/test-stream-readable-null-buffer-head.js index f45b95cac002d3..62b9469975ffe5 100644 --- a/test/parallel/test-stream-readable-null-buffer-head.js +++ b/test/parallel/test-stream-readable-null-buffer-head.js @@ -3,30 +3,45 @@ const common = require('../common'); const Readable = require('_stream_readable'); const assert = require('assert'); -var buf = ''; const emdash = new Buffer([0xE2, 0x80, 0x94]); const euro = new Buffer([0xE2, 0x82, 0xAC]); -const source = Buffer.concat([emdash, euro]); +const source = Buffer.concat([emdash, euro, Buffer.from('asdfasl')]); +const errorMsg = 'howMuchToRead attempts to retrieve data from an empty buffer.head'; -const readable = Readable({ encoding: 'utf8' }); +exactBuffer(); +oddNumberBuffer(); -readable.push(source.slice(0, 4)); -readable.push(source.slice(4, 6)); -readable.push(null); +var exactBuf = ''; +function exactBuffer() { + var readable = Readable({ encoding: 'utf8' }); + readable.push(source); + readable.push(null); -readable.on('data', data => { - console.log(data); - process.nextTick = () => { - if (readable._readableState.length !== 0 && - readable._readableState.flowing && - readable._readableState.buffer.head == null) { - throw new Error(`howMuchToRead attempts to retrieve data from an empty buffer.head`); - } - }; + readable.on('data', data => { + exactBuf += data; + }); +} - buf += data; +var oddBuf = ''; +function oddNumberBuffer() { + var readable = Readable({ encoding: 'utf8' }); + + readable.push(source.slice(0, 2)); + readable.push(source.slice(2, 4)); + readable.push(source.slice(4, 14)); + readable.push(null); + + readable.on('data', data => { + oddBuf += data; + }); +} + +process.on('uncaughtException', (e) => { + console.log(e); + throw new Error(errorMsg); }); process.on('exit', function() { - assert.strictEqual(buf, '—€'); + assert.strictEqual(exactBuf, '—€asdfasl'); + assert.strictEqual(oddBuf, '—€asdfasl'); });