From 428485925dd5c4a96a8cafe28b2b04e0e7a06846 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 26 Jan 2020 23:07:17 +0100 Subject: [PATCH 1/2] stream: fix finished writable/readable state writable/readable does not indicate whether as stream is a Writable/Readable. This implements a better check for whether a object is a Writable/Readable. --- lib/internal/streams/end-of-stream.js | 18 ++++++++++++++++-- test/parallel/test-stream-finished.js | 9 +++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/internal/streams/end-of-stream.js b/lib/internal/streams/end-of-stream.js index bb01e800becdf3..877db6fbe1cb34 100644 --- a/lib/internal/streams/end-of-stream.js +++ b/lib/internal/streams/end-of-stream.js @@ -13,6 +13,18 @@ function isRequest(stream) { return stream.setHeader && typeof stream.abort === 'function'; } +function isReadable(stream) { + return typeof stream.readable === 'boolean' || + typeof stream.readableEnded === 'boolean' || + !!stream._readableState; +} + +function isWritable(stream) { + return typeof stream.writable === 'boolean' || + typeof stream.writableEnded === 'boolean' || + !!stream._writableState; +} + function eos(stream, opts, callback) { if (arguments.length === 2) { callback = opts; @@ -28,8 +40,10 @@ function eos(stream, opts, callback) { callback = once(callback); - let readable = opts.readable || (opts.readable !== false && stream.readable); - let writable = opts.writable || (opts.writable !== false && stream.writable); + let readable = opts.readable || + (opts.readable !== false && isReadable(stream)); + let writable = opts.writable || + (opts.writable !== false && isWritable(stream)); const onlegacyfinish = () => { if (!stream.writable) onfinish(); diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index d6361ea303635d..48b83d679e51eb 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -184,3 +184,12 @@ const { promisify } = require('util'); finished(streamLike, common.mustCall); streamLike.emit('close'); } + +{ + const writable = new Writable({ write() {} }); + writable.writable = false; + writable.destroy(); + finished(writable, common.mustCall((err) => { + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); + })); +} From 818e972f7d73620795c69f781b9eabac69805326 Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Sun, 26 Jan 2020 23:49:32 +0100 Subject: [PATCH 2/2] fixup: add Readdable test --- test/parallel/test-stream-finished.js | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/parallel/test-stream-finished.js b/test/parallel/test-stream-finished.js index 48b83d679e51eb..5b26349d7476e2 100644 --- a/test/parallel/test-stream-finished.js +++ b/test/parallel/test-stream-finished.js @@ -193,3 +193,12 @@ const { promisify } = require('util'); assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); })); } + +{ + const readable = new Readable(); + readable.readable = false; + readable.destroy(); + finished(readable, common.mustCall((err) => { + assert.strictEqual(err.code, 'ERR_STREAM_PREMATURE_CLOSE'); + })); +}