Fix behaviour when there is no onEnd return value

master
Sven Slootweg 3 years ago
parent d42e73356a
commit f5256a2536

@ -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(() => {

Loading…
Cancel
Save