From f8fec8cf030667971ea68b1f6ae931c2d5ae4ddb Mon Sep 17 00:00:00 2001 From: Robert Nagy Date: Tue, 29 Nov 2022 11:31:13 +0100 Subject: [PATCH] stream: expose stream symbols This is required for streams interop with e.g. readable-stream. Currently readable-stream helpers will not work with normal node streams which is confusing and bad for the ecosystem. --- lib/internal/streams/destroy.js | 4 ++-- lib/internal/streams/utils.js | 15 +++++++++------ lib/stream.js | 9 +++++++++ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/lib/internal/streams/destroy.js b/lib/internal/streams/destroy.js index 10f5471e21d3eb..a345b871dbe2ba 100644 --- a/lib/internal/streams/destroy.js +++ b/lib/internal/streams/destroy.js @@ -11,7 +11,7 @@ const { Symbol, } = primordials; const { - kDestroyed, + kIsDestroyed, isDestroyed, isFinished, isServerRequest @@ -325,7 +325,7 @@ function destroyer(stream, err) { } if (!stream.destroyed) { - stream[kDestroyed] = true; + stream[kIsDestroyed] = true; } } diff --git a/lib/internal/streams/utils.js b/lib/internal/streams/utils.js index 4d4f00ab456fa7..8ecdc78177725a 100644 --- a/lib/internal/streams/utils.js +++ b/lib/internal/streams/utils.js @@ -6,9 +6,10 @@ const { SymbolIterator, } = primordials; -const kDestroyed = Symbol('kDestroyed'); +const kIsDestroyed = Symbol('kIsDestroyed'); const kIsErrored = Symbol('kIsErrored'); const kIsReadable = Symbol('kIsReadable'); +const kIsWritable = Symbol('kIsWritable'); const kIsDisturbed = Symbol('kIsDisturbed'); function isReadableNodeStream(obj, strict = false) { @@ -68,7 +69,7 @@ function isDestroyed(stream) { const wState = stream._writableState; const rState = stream._readableState; const state = wState || rState; - return !!(stream.destroyed || stream[kDestroyed] || state?.destroyed); + return !!(stream.destroyed || stream[kIsDestroyed] || state?.destroyed); } // Have been end():d. @@ -126,6 +127,7 @@ function isReadable(stream) { } function isWritable(stream) { + if (stream && stream[kIsWritable] != null) return stream[kIsWritable]; if (typeof stream?.writable !== 'boolean') return null; if (isDestroyed(stream)) return false; return isWritableNodeStream(stream) && @@ -262,13 +264,14 @@ function isErrored(stream) { } module.exports = { - kDestroyed, - isDisturbed, + kIsDestroyed, kIsDisturbed, - isErrored, kIsErrored, - isReadable, kIsReadable, + kIsWritable, + isDisturbed, + isErrored, + isReadable, isClosed, isDestroyed, isDuplexNodeStream, diff --git a/lib/stream.js b/lib/stream.js index 55f903f295fecf..e580da725bfa21 100644 --- a/lib/stream.js +++ b/lib/stream.js @@ -51,9 +51,18 @@ const promises = require('stream/promises'); const utils = require('internal/streams/utils'); const Stream = module.exports = require('internal/streams/legacy').Stream; + +Stream.isDestroyed = utils.isDestroyed; Stream.isDisturbed = utils.isDisturbed; Stream.isErrored = utils.isErrored; Stream.isReadable = utils.isReadable; + +Stream.kIsDestroyed = utils.kIsDestroyed; +Stream.kIsErrored = utils.kIsErrored; +Stream.kIsReadable = utils.kIsReadable; +Stream.kIsWritable = utils.kIsWritable; +Stream.kIsDisturbed = utils.kIsDisturbed; + Stream.Readable = require('internal/streams/readable'); for (const key of ObjectKeys(streamReturningOperators)) { const op = streamReturningOperators[key];