Skip to content

Commit

Permalink
implementing recommended changes from matteo
Browse files Browse the repository at this point in the history
This returns the length of the buffer's head in with byteLength
function. It also updates a test to test for allocation of a
mixed, split buffer.
  • Loading branch information
jalafel committed Oct 26, 2016
1 parent e209d52 commit b587c1f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 18 deletions.
2 changes: 1 addition & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
49 changes: 32 additions & 17 deletions test/parallel/test-stream-readable-null-buffer-head.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

0 comments on commit b587c1f

Please sign in to comment.