Skip to content
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
18 changes: 7 additions & 11 deletions lib/internal/streams/end-of-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ function eos(stream, opts, callback) {

callback = once(callback);

let readable = opts.readable ||
const readable = opts.readable ||
(opts.readable !== false && isReadable(stream));
let writable = opts.writable ||
const writable = opts.writable ||
(opts.writable !== false && isWritable(stream));

const wState = stream._writableState;
Expand All @@ -71,35 +71,31 @@ function eos(stream, opts, callback) {
let writableFinished = stream.writableFinished ||
(wState && wState.finished);
const onfinish = () => {
writable = false;
writableFinished = true;
if (!readable) callback.call(stream);
if (!readable || readableEnded) callback.call(stream);
};

let readableEnded = stream.readableEnded ||
(rState && rState.endEmitted);
const onend = () => {
readable = false;
readableEnded = true;
if (!writable) callback.call(stream);
if (!writable || writableFinished) callback.call(stream);
};

const onerror = (err) => {
callback.call(stream, err);
};

const onclose = () => {
let err;
if (readable && !readableEnded) {
if (!isReadableEnded(stream))
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
if (writable && !writableFinished) {
if (!isWritableFinished(stream))
err = new ERR_STREAM_PREMATURE_CLOSE();
return callback.call(stream, err);
return callback.call(stream, new ERR_STREAM_PREMATURE_CLOSE());
}
callback.call(stream);
};

const onrequest = () => {
Expand Down
2 changes: 1 addition & 1 deletion test/parallel/test-stream-finished.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ const { promisify } = require('util');
const streamLike = new EE();
streamLike.readableEnded = true;
streamLike.readable = true;
finished(streamLike, common.mustCall);
finished(streamLike, common.mustCall());
streamLike.emit('close');
}

Expand Down