Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,13 @@ function howMuchToRead(n, state) {
if ((state[kState] & kObjectMode) !== 0)
return 1;
if (NumberIsNaN(n)) {
// Fast path for buffers.
if ((state[kState] & kDecoder) === 0) {
return state.length
? state.buffer[state.bufferIndex].length
: state.length;
}

// Only flow one buffer at a time.
if ((state[kState] & kFlowing) !== 0 && state.length)
return state.buffer[state.bufferIndex].length;
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-compose.js
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ const assert = require('assert');

newStream.end();

assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve RogersOn your left')]);
assert.deepStrictEqual(await newStream.toArray(), [Buffer.from('Steve Rogers'), Buffer.from('On your left')]);
})().then(common.mustCall());
}

Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-push-strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ ms.on('readable', function() {
results.push(String(chunk));
});

const expect = [ 'first chunksecond to last chunk', 'last chunk' ];
const expect = [ 'first chunk', 'second to last chunk', 'last chunk' ];
process.on('exit', function() {
assert.strictEqual(ms._chunks, -1);
assert.deepStrictEqual(results, expect);
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-readable-emittedReadable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const readable = new Readable({
// Initialized to false.
assert.strictEqual(readable._readableState.emittedReadable, false);

const expected = [Buffer.from('foobar'), Buffer.from('quo'), null];
const expected = [Buffer.from('foo'), Buffer.from('bar'), Buffer.from('quo'), null];
readable.on('readable', common.mustCall(() => {
// emittedReadable should be true when the readable event is emitted
assert.strictEqual(readable._readableState.emittedReadable, true);
Expand Down
4 changes: 2 additions & 2 deletions test/parallel/test-stream-readable-infinite-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ readable.on('readable', common.mustCall(function() {
// TODO(mcollina): there is something odd in the highWaterMark logic
// investigate.
if (i === 1) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if seems to be redundant now?

assert.strictEqual(data.length, 8192 * 2);
assert.strictEqual(data.length, 8192);
} else {
assert.strictEqual(data.length, 8192 * 3);
assert.strictEqual(data.length, 8192);
}
}, 11));
20 changes: 20 additions & 0 deletions test/parallel/test-stream-readable-readable-one.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';
const common = require('../common');
const assert = require('assert');

const { Readable } = require('stream');

// Read one buffer at a time and don't waste cycles allocating
// and copying into a new larger buffer.
{
const r = new Readable({
read() {}
});
const buffers = [Buffer.allocUnsafe(5), Buffer.allocUnsafe(10)];
for (const buf of buffers) {
r.push(buf);
}
for (const buf of buffers) {
assert.strictEqual(r.read(), buf);
}
}
14 changes: 12 additions & 2 deletions test/parallel/test-stream-typedarray.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,19 @@ const views = common.getArrayBufferViews(buffer);
readable.push(views[2]);
readable.unshift(views[0]);

const buf = readable.read();
let buf;

buf = readable.read();
assert(buf instanceof Buffer);
assert.deepStrictEqual([...buf], [...views[0]]);

buf = readable.read();
assert(buf instanceof Buffer);
assert.deepStrictEqual([...buf], [...views[1]]);

buf = readable.read();
assert(buf instanceof Buffer);
assert.deepStrictEqual([...buf], [...views[0], ...views[1], ...views[2]]);
assert.deepStrictEqual([...buf], [...views[2]]);
}

{
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-stream-uint8array.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,15 @@ const GHI = new Uint8Array([0x47, 0x48, 0x49]);
readable.push(DEF);
readable.unshift(ABC);

const buf = readable.read();
let buf;

buf = readable.read();
assert(buf instanceof Buffer);
assert.deepStrictEqual([...buf], [...ABC]);

buf = readable.read();
assert(buf instanceof Buffer);
assert.deepStrictEqual([...buf], [...ABC, ...DEF]);
assert.deepStrictEqual([...buf], [...DEF]);
}

{
Expand Down
4 changes: 3 additions & 1 deletion test/parallel/test-stream2-transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,9 @@ const { PassThrough, Transform } = require('stream');
pt.write(Buffer.from('ef'), common.mustCall(function() {
pt.end();
}));
assert.strictEqual(pt.read().toString(), 'abcdef');
assert.strictEqual(pt.read().toString(), 'abc');
assert.strictEqual(pt.read().toString(), 'd');
assert.strictEqual(pt.read().toString(), 'ef');
assert.strictEqual(pt.read(), null);
});
});
Expand Down