From 513305c7d7a71d1dd696506941aa4e5389e6cdd2 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Wed, 1 Dec 2021 02:50:24 +0100 Subject: [PATCH] stream: cleanup eos PR-URL: https://github.com/nodejs/node/pull/40998 Reviewed-By: Ruben Bridgewater Reviewed-By: Benjamin Gruenbaum Reviewed-By: Luigi Pinca Reviewed-By: Matteo Collina Reviewed-By: James M Snell --- lib/internal/streams/end-of-stream.js | 51 ++++++++++++++++++--------- 1 file changed, 34 insertions(+), 17 deletions(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index f238b9c9734a06..b70d81e4b4e773 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -49,14 +49,10 @@ function eos(stream, options, callback) { callback = once(callback); - const readable = options.readable || - (options.readable !== false && isReadableNodeStream(stream)); - const writable = options.writable || - (options.writable !== false && isWritableNodeStream(stream)); + const readable = options.readable ?? isReadableNodeStream(stream); + const writable = options.writable ?? isWritableNodeStream(stream); - if (isNodeStream(stream)) { - // Do nothing... - } else { + if (!isNodeStream(stream)) { // TODO: Webstreams. // TODO: Throw INVALID_ARG_TYPE. } @@ -65,7 +61,9 @@ function eos(stream, options, callback) { const rState = stream._readableState; const onlegacyfinish = () => { - if (!stream.writable) onfinish(); + if (!stream.writable) { + onfinish(); + } }; // TODO (ronag): Improve soft detection to include core modules and @@ -83,10 +81,17 @@ function eos(stream, options, callback) { // Stream should not be destroyed here. If it is that // means that user space is doing something differently and // we cannot trust willEmitClose. - if (stream.destroyed) willEmitClose = false; + if (stream.destroyed) { + willEmitClose = false; + } - if (willEmitClose && (!stream.readable || readable)) return; - if (!readable || readableFinished) callback.call(stream); + if (willEmitClose && (!stream.readable || readable)) { + return; + } + + if (!readable || readableFinished) { + callback.call(stream); + } }; let readableFinished = isReadableFinished(stream, false); @@ -95,10 +100,17 @@ function eos(stream, options, callback) { // Stream should not be destroyed here. If it is that // means that user space is doing something differently and // we cannot trust willEmitClose. - if (stream.destroyed) willEmitClose = false; + if (stream.destroyed) { + willEmitClose = false; + } - if (willEmitClose && (!stream.writable || writable)) return; - if (!writable || writableFinished) callback.call(stream); + if (willEmitClose && (!stream.writable || writable)) { + return; + } + + if (!writable || writableFinished) { + callback.call(stream); + } }; const onerror = (err) => { @@ -139,8 +151,11 @@ function eos(stream, options, callback) { if (!willEmitClose) { stream.on('abort', onclose); } - if (stream.req) onrequest(); - else stream.on('request', onrequest); + if (stream.req) { + onrequest(); + } else { + stream.on('request', onrequest); + } } else if (writable && !wState) { // legacy streams stream.on('end', onlegacyfinish); stream.on('close', onlegacyfinish); @@ -153,7 +168,9 @@ function eos(stream, options, callback) { stream.on('end', onend); stream.on('finish', onfinish); - if (options.error !== false) stream.on('error', onerror); + if (options.error !== false) { + stream.on('error', onerror); + } stream.on('close', onclose); if (closed) {