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.
  • Loading branch information
ronag committed Nov 29, 2022
1 parent 69467fb commit f8fec8c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/internal/streams/destroy.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
Symbol,
} = primordials;
const {
kDestroyed,
kIsDestroyed,
isDestroyed,
isFinished,
isServerRequest
Expand Down Expand Up @@ -325,7 +325,7 @@ function destroyer(stream, err) {
}

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

Expand Down
15 changes: 9 additions & 6 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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) &&
Expand Down Expand Up @@ -262,13 +264,14 @@ function isErrored(stream) {
}

module.exports = {
kDestroyed,
isDisturbed,
kIsDestroyed,
kIsDisturbed,
isErrored,
kIsErrored,
isReadable,
kIsReadable,
kIsWritable,
isDisturbed,
isErrored,
isReadable,
isClosed,
isDestroyed,
isDuplexNodeStream,
Expand Down
9 changes: 9 additions & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand Down

0 comments on commit f8fec8c

Please sign in to comment.