Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: allow using .push()/.unshift() during once('data') #34957

Closed
Closed
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
3 changes: 2 additions & 1 deletion lib/_stream_readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
}

function addChunk(stream, state, chunk, addToFront) {
if (state.flowing && state.length === 0 && !state.sync) {
if (state.flowing && state.length === 0 && !state.sync &&
stream.listenerCount('data') > 0) {
// Use the guard to avoid creating `Set()` repeatedly
// when we have multiple pipes.
if (state.multiAwaitDrain) {
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-stream-readable-add-chunk-during-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';
const common = require('../common');
const assert = require('assert');
const { Readable } = require('stream');

// Verify that .push() and .unshift() can be called from 'data' listeners.

for (const method of ['push', 'unshift']) {
const r = new Readable({ read() {} });
r.once('data', common.mustCall((chunk) => {
assert.strictEqual(r.readableLength, 0);
r[method](chunk);
assert.strictEqual(r.readableLength, chunk.length);

r.on('data', common.mustCall((chunk) => {
assert.strictEqual(chunk.toString(), 'Hello, world');
}));
}));

r.push('Hello, world');
}