From f5256a2536091c0b5c8cec07dbe09c33220f2019 Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Sun, 10 Oct 2021 01:03:36 +0200 Subject: [PATCH] Fix behaviour when there is no onEnd return value --- index.js | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/index.js b/index.js index 569b4d6..b340a12 100644 --- a/index.js +++ b/index.js @@ -67,16 +67,19 @@ module.exports = function simpleSinkStream(_options) { }).catch(isEndOfStream, (error) => { /* Don't attempt to do another read, we're done. */ return Promise.try(() => { - if (!onEndCalled && onEnd != null) { + // Note: we *always* push a value to the buffer when handling the end of the stream - that is intentional. This is necessary because the end can only ever be reached as the ultimate result of a read call on this stream, and that call is expected to return a value. If we only pushed a value to the buffer when there's an onEnd handler producing one, then it would be possible in some cases for the buffer to remain empty, causing an entirely valid read call to result in a thrown EndOfStream error. That error should only be thrown if something *continues* to try and read from it, not at the first moment that the end is reached. + if (!onEndCalled) { onEndCalled = true; - - return Promise.try(() => { - return onEnd(); - }).then((result) => { - if (result !== undefined) { + + if (onEnd != null) { + return Promise.try(() => { + return onEnd(); + }).then((result) => { resultBuffer.push(result); - } - }); + }); + } else { + resultBuffer.push(undefined); + } } }).then(() => { return resultBuffer.maybeRead(() => {