Skip to content

Commit

Permalink
stream: expose stream symbols
Browse files Browse the repository at this point in the history
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.

PR-URL: #45671
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
Reviewed-By: Debadree Chatterjee <debadree333@gmail.com>
  • Loading branch information
ronag authored and RafaelGSS committed Apr 7, 2023
1 parent dc024d9 commit b8c6ced
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions lib/internal/streams/destroy.js
Expand Up @@ -11,7 +11,7 @@ const {
Symbol,
} = primordials;
const {
kDestroyed,
kIsDestroyed,
isDestroyed,
isFinished,
isServerRequest,
Expand Down Expand Up @@ -327,7 +327,7 @@ function destroyer(stream, err) {
}

if (!stream.destroyed) {
stream[kDestroyed] = true;
stream[kIsDestroyed] = true;
}
}

Expand Down
22 changes: 14 additions & 8 deletions lib/internal/streams/utils.js
@@ -1,16 +1,20 @@
'use strict';

const {
Symbol,
SymbolAsyncIterator,
SymbolIterator,
SymbolFor,
} = primordials;

const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
const kIsDisturbed = Symbol('kIsDisturbed');
// We need to use SymbolFor to make these globally available
// for interopt with readable-stream, i.e. readable-stream
// and node core needs to be able to read/write private state
// from each other for proper interoperability.
const kIsDestroyed = SymbolFor('nodejs.stream.destroyed');
const kIsErrored = SymbolFor('nodejs.stream.errored');
const kIsReadable = SymbolFor('nodejs.stream.readable');
const kIsWritable = SymbolFor('nodejs.stream.writable');
const kIsDisturbed = SymbolFor('nodejs.stream.disturbed');

const kIsClosedPromise = SymbolFor('nodejs.webstream.isClosedPromise');
const kControllerErrorFunction = SymbolFor('nodejs.webstream.controllerErrorFunction');
Expand Down Expand Up @@ -104,7 +108,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.
Expand Down Expand Up @@ -162,6 +166,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) &&
Expand Down Expand Up @@ -298,7 +303,8 @@ function isErrored(stream) {
}

module.exports = {
kDestroyed,
isDestroyed,
kIsDestroyed,
isDisturbed,
kIsDisturbed,
isErrored,
Expand All @@ -307,8 +313,8 @@ module.exports = {
kIsReadable,
kIsClosedPromise,
kControllerErrorFunction,
kIsWritable,
isClosed,
isDestroyed,
isDuplexNodeStream,
isFinished,
isIterable,
Expand Down
4 changes: 4 additions & 0 deletions lib/stream.js
Expand Up @@ -51,9 +51,13 @@ 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.isWritable = utils.isWritable;

Stream.Readable = require('internal/streams/readable');
for (const key of ObjectKeys(streamReturningOperators)) {
const op = streamReturningOperators[key];
Expand Down

0 comments on commit b8c6ced

Please sign in to comment.