Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stream: add isReadable helper #41199

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions doc/api/stream.md
Original file line number Diff line number Diff line change
Expand Up @@ -2238,6 +2238,19 @@ added: REPLACEME

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 @@ -260,15 +262,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 @@ -246,6 +247,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 @@ -41,6 +41,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');
Stream.Writable = require('internal/streams/writable');
Stream.Duplex = require('internal/streams/duplex');
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());
}