Skip to content

Commit

Permalink
stream: eos premature close
Browse files Browse the repository at this point in the history
  • Loading branch information
ronag committed Aug 9, 2019
1 parent 885c644 commit 77ff0d3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
20 changes: 14 additions & 6 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,22 @@ function eos(stream, opts, callback) {
if (!stream.writable) onfinish();
};

var writableEnded = stream._writableState && stream._writableState.finished;
var writableFinished = (
stream._writableState &&
stream._writableState.finished
);

const onfinish = () => {
writable = false;
writableEnded = true;
writableFinished = true;
if (!readable) callback.call(stream);
};

var readableEnded = stream._readableState && stream._readableState.endEmitted;
var readableEnded = (
stream._readableState &&
stream._readableState.endEmitted
);

const onend = () => {
readable = false;
readableEnded = true;
Expand All @@ -56,12 +64,12 @@ function eos(stream, opts, callback) {
const onclose = () => {
let err;
if (readable && !readableEnded) {
if (!stream._readableState || !stream._readableState.ended)
if (!stream._readableState || !stream._readableState.endEmitted)
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
}
if (writable && !writableEnded) {
if (!stream._writableState || !stream._writableState.ended)
if (writable && !writableFinished) {
if (!stream._writableState || !stream._writableState.finished)
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
}
Expand Down
16 changes: 15 additions & 1 deletion test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,18 @@ const { promisify } = require('util');
}));
}

{
const rs = new Readable();

finished(rs, common.mustCall((err) => {
assert(err, 'premature close error');
}));

rs.push(null);
rs.emit('close');
rs.resume();
}

{
const rs = new Readable();

Expand All @@ -104,7 +116,9 @@ const { promisify } = require('util');
}));

rs.push(null);
rs.emit('close'); // Should not trigger an error
rs.on('end', common.mustCall(() => {
rs.emit('close'); // Should not trigger an error
}));
rs.resume();
}

Expand Down

0 comments on commit 77ff0d3

Please sign in to comment.