Skip to content

Commit 3e3eced

Browse files
committed
stream: don't emit 'data' after 'error' or 'close'
As per doc we should not emit further events after 'close' and only 'close' after 'error'.
1 parent f2d02ab commit 3e3eced

File tree

3 files changed

+87
-13
lines changed

3 files changed

+87
-13
lines changed

deps/npm/node_modules/socks/typings/common/receivebuffer.d.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

lib/internal/streams/readable.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,8 @@ function readableAddChunk(stream, chunk, encoding, addToFront) {
276276
if (addToFront) {
277277
if (state.endEmitted)
278278
errorOrDestroy(stream, new ERR_STREAM_UNSHIFT_AFTER_END_EVENT());
279+
else if (state.destroyed || state.errored)
280+
return false;
279281
else
280282
addChunk(stream, state, chunk, true);
281283
} else if (state.ended) {
@@ -316,6 +318,7 @@ function addChunk(stream, state, chunk, addToFront) {
316318
} else {
317319
state.awaitDrainWriters = null;
318320
}
321+
319322
state.dataEmitted = true;
320323
stream.emit('data', chunk);
321324
} else {
@@ -542,7 +545,7 @@ Readable.prototype.read = function(n) {
542545
endReadable(this);
543546
}
544547

545-
if (ret !== null) {
548+
if (ret !== null && !state.errorEmitted && !state.closeEmitted) {
546549
state.dataEmitted = true;
547550
this.emit('data', ret);
548551
}

test/parallel/test-stream-readable-destroy.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,86 @@ const assert = require('assert');
319319
})(), /AbortError/);
320320
setTimeout(() => controller.abort(), 0);
321321
}
322+
323+
{
324+
const read = new Readable({
325+
read() {
326+
},
327+
});
328+
read.push('asd');
329+
330+
read.on('data', common.mustNotCall());
331+
read.on('error', common.mustCall((e) => {
332+
read.read();
333+
}));
334+
read.on('close', common.mustCall((e) => {
335+
read.read();
336+
}));
337+
read.destroy(new Error('asd'));
338+
}
339+
340+
{
341+
const read = new Readable({
342+
read() {
343+
},
344+
});
345+
read.push('asd');
346+
347+
read.on('data', common.mustNotCall());
348+
read.on('close', common.mustCall((e) => {
349+
read.read();
350+
}));
351+
read.destroy();
352+
}
353+
354+
{
355+
const read = new Readable({
356+
read() {
357+
},
358+
});
359+
read.push('asd');
360+
361+
read.on('data', common.mustNotCall());
362+
read.on('close', common.mustCall((e) => {
363+
read.unshift('asd');
364+
}));
365+
read.destroy();
366+
}
367+
368+
{
369+
const read = new Readable({
370+
read() {
371+
},
372+
});
373+
read.push('asd');
374+
375+
read.on('data', common.mustNotCall());
376+
read.destroy();
377+
read.unshift('asd');
378+
}
379+
380+
{
381+
const read = new Readable({
382+
read() {
383+
},
384+
});
385+
read.push('asd');
386+
387+
read.on('data', common.mustNotCall());
388+
read.on('close', common.mustCall((e) => {
389+
read.push('asd');
390+
}));
391+
read.destroy();
392+
}
393+
394+
{
395+
const read = new Readable({
396+
read() {
397+
},
398+
});
399+
read.push('asd');
400+
401+
read.on('data', common.mustNotCall());
402+
read.destroy();
403+
read.push('asd');
404+
}

0 commit comments

Comments
 (0)