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: don't emit 'data' after 'error' or 'close' #39639

Closed
wants to merge 2 commits into from
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
5 changes: 4 additions & 1 deletion lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
if (addToFront) {
if (state.endEmitted)
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
else if (state.destroyed || state.errored)
return false;
else
addChunk(stream, state, chunk, true);
} else if (state.ended) {
Expand Down Expand Up @@ -316,6 +318,7 @@ function addChunk(stream, state, chunk, addToFront) {
} else {
state.awaitDrainWriters = null;
}

state.dataEmitted = true;
stream.emit('data', chunk);
} else {
Expand Down Expand Up @@ -542,7 +545,7 @@ Readable.prototype.read = function(n) {
endReadable(this);
}

if (ret !== null) {
if (ret !== null && !state.errorEmitted && !state.closeEmitted) {
state.dataEmitted = true;
this.emit('data', ret);
}
Expand Down
82 changes: 82 additions & 0 deletions test/parallel/test-stream-readable-destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,85 @@ const assert = require('assert');
})(), /AbortError/);
setTimeout(() => controller.abort(), 0);
}

{
const read = new Readable({
read() {
},
});

read.on('data', common.mustNotCall());
read.on('error', common.mustCall((e) => {
read.push('asd');
read.read();
}));
read.on('close', common.mustCall((e) => {
read.push('asd');
read.read();
}));
read.destroy(new Error('asd'));
}

{
const read = new Readable({
read() {
},
});

read.on('data', common.mustNotCall());
read.on('close', common.mustCall((e) => {
read.push('asd');
read.read();
}));
read.destroy();
}

{
const read = new Readable({
read() {
},
});

read.on('data', common.mustNotCall());
read.on('close', common.mustCall((e) => {
read.push('asd');
read.unshift('asd');
}));
read.destroy();
}

{
const read = new Readable({
read() {
},
});

read.on('data', common.mustNotCall());
read.destroy();
read.unshift('asd');
}

{
const read = new Readable({
read() {
},
});

read.resume();
read.on('data', common.mustNotCall());
read.on('close', common.mustCall((e) => {
read.push('asd');
}));
read.destroy();
}

{
const read = new Readable({
read() {
},
});

read.on('data', common.mustNotCall());
read.destroy();
read.push('asd');
}