Skip to content

Commit

Permalink
stream: add isReadable helper
Browse files Browse the repository at this point in the history
PR-URL: #41199
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
ronag authored and targos committed Jan 14, 2022
1 parent 9f5a873 commit 1150963
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
13 changes: 13 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2228,6 +2228,19 @@ added: v17.3.0

Returns whether the stream has encountered an error.

### `stream.isReadable(stream)`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental
* `stream` {Readable|Duplex|ReadableStream}
* Returns: {boolean}

Returns whether the stream is readable.

### `stream.Readable.toWeb(streamReadable)`

<!-- YAML
Expand Down
7 changes: 5 additions & 2 deletions lib/internal/streams/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const {

const kDestroyed = Symbol('kDestroyed');
const kIsErrored = Symbol('kIsErrored');
const kIsReadable = Symbol('kIsReadable');
const kIsDisturbed = Symbol('kIsDisturbed');

function isReadableNodeStream(obj, strict = false) {
Expand Down Expand Up @@ -116,6 +117,7 @@ function isReadableFinished(stream, strict) {
}

function isReadable(stream) {
if (stream && stream[kIsReadable] != null) return stream[kIsReadable];
const r = isReadableNodeStream(stream);
if (r === null || typeof stream?.readable !== 'boolean') return null;
if (isDestroyed(stream)) return false;
Expand Down Expand Up @@ -232,15 +234,16 @@ function isErrored(stream) {
module.exports = {
kDestroyed,
isDisturbed,
isErrored,
kIsDisturbed,
isErrored,
kIsErrored,
isReadable,
kIsReadable,
isClosed,
isDestroyed,
isDuplexNodeStream,
isFinished,
isIterable,
isReadable,
isReadableNodeStream,
isReadableEnded,
isReadableFinished,
Expand Down
5 changes: 5 additions & 0 deletions lib/internal/webstreams/readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const {
const {
kIsDisturbed,
kIsErrored,
kIsReadable,
} = require('internal/streams/utils');

const {
Expand Down Expand Up @@ -261,6 +262,10 @@ class ReadableStream {
return this[kState].state === 'errored';
}

get [kIsReadable]() {
return this[kState].state === 'readable';
}

/**
* @readonly
* @type {boolean}
Expand Down
1 change: 1 addition & 0 deletions lib/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ const utils = require('internal/streams/utils');
const Stream = module.exports = require('internal/streams/legacy').Stream;
Stream.isDisturbed = utils.isDisturbed;
Stream.isErrored = utils.isErrored;
Stream.isReadable = utils.isReadable;
Stream.Readable = require('internal/streams/readable');
for (const key of ObjectKeys(operators)) {
const op = operators[key];
Expand Down
18 changes: 16 additions & 2 deletions test/parallel/test-whatwg-readablestream.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const common = require('../common');
const { isDisturbed, isErrored } = require('stream');
const { isDisturbed, isErrored, isReadable } = require('stream');
const assert = require('assert');
const {
isPromise,
Expand Down Expand Up @@ -1573,7 +1573,6 @@ class Source {
})().then(common.mustCall());
}


{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
Expand All @@ -1588,3 +1587,18 @@ class Source {
isErrored(stream, true);
})().then(common.mustCall());
}

{
const stream = new ReadableStream({
pull: common.mustCall((controller) => {
controller.error(new Error());
}),
});

const reader = stream.getReader();
(async () => {
isReadable(stream, true);
await reader.read().catch(common.mustCall());
isReadable(stream, false);
})().then(common.mustCall());
}

0 comments on commit 1150963

Please sign in to comment.