Skip to content

Commit 0bebd47

Browse files
ronagdanielleadams
authored andcommitted
stream: add isReadable helper
PR-URL: #41199 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 32afd27 commit 0bebd47

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

doc/api/stream.md

+13
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,19 @@ added: REPLACEME
22232223

22242224
Returns whether the stream has encountered an error.
22252225

2226+
### `stream.isReadable(stream)`
2227+
2228+
<!-- YAML
2229+
added: REPLACEME
2230+
-->
2231+
2232+
> Stability: 1 - Experimental
2233+
2234+
* `stream` {Readable|Duplex|ReadableStream}
2235+
* Returns: {boolean}
2236+
2237+
Returns whether the stream is readable.
2238+
22262239
### `stream.Readable.toWeb(streamReadable)`
22272240

22282241
<!-- YAML

lib/internal/streams/utils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
} = primordials;
88

99
const kIsErrored = Symbol('kIsErrored');
10+
const kIsReadable = Symbol('kIsReadable');
1011
const kIsDisturbed = Symbol('kIsDisturbed');
1112

1213
function isReadableNodeStream(obj) {
@@ -119,6 +120,7 @@ function isDisturbed(stream) {
119120
}
120121

121122
function isReadable(stream) {
123+
if (stream && stream[kIsReadable] != null) return stream[kIsReadable];
122124
const r = isReadableNodeStream(stream);
123125
if (r === null || typeof stream?.readable !== 'boolean') return null;
124126
if (isDestroyed(stream)) return false;
@@ -234,15 +236,16 @@ function isErrored(stream) {
234236

235237
module.exports = {
236238
isDisturbed,
237-
isErrored,
238239
kIsDisturbed,
240+
isErrored,
239241
kIsErrored,
242+
isReadable,
243+
kIsReadable,
240244
isClosed,
241245
isDestroyed,
242246
isDuplexNodeStream,
243247
isFinished,
244248
isIterable,
245-
isReadable,
246249
isReadableNodeStream,
247250
isReadableEnded,
248251
isReadableFinished,

lib/internal/webstreams/readablestream.js

+5
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ const {
8383
const {
8484
kIsDisturbed,
8585
kIsErrored,
86+
kIsReadable,
8687
} = require('internal/streams/utils');
8788

8889
const {
@@ -246,6 +247,10 @@ class ReadableStream {
246247
return this[kState].state === 'errored';
247248
}
248249

250+
get [kIsReadable]() {
251+
return this[kState].state === 'readable';
252+
}
253+
249254
/**
250255
* @readonly
251256
* @type {boolean}

lib/stream.js

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const utils = require('internal/streams/utils');
4444
const Stream = module.exports = require('internal/streams/legacy').Stream;
4545
Stream.isDisturbed = utils.isDisturbed;
4646
Stream.isErrored = utils.isErrored;
47+
Stream.isReadable = utils.isReadable;
4748
Stream.Readable = require('internal/streams/readable');
4849
for (const key of ObjectKeys(operators)) {
4950
const op = operators[key];

test/parallel/test-whatwg-readablestream.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
'use strict';
33

44
const common = require('../common');
5-
const { isDisturbed, isErrored } = require('stream');
5+
const { isDisturbed, isErrored, isReadable } = require('stream');
66
const assert = require('assert');
77
const {
88
isPromise,
@@ -1573,7 +1573,6 @@ class Source {
15731573
})().then(common.mustCall());
15741574
}
15751575

1576-
15771576
{
15781577
const stream = new ReadableStream({
15791578
pull: common.mustCall((controller) => {
@@ -1588,3 +1587,18 @@ class Source {
15881587
isErrored(stream, true);
15891588
})().then(common.mustCall());
15901589
}
1590+
1591+
{
1592+
const stream = new ReadableStream({
1593+
pull: common.mustCall((controller) => {
1594+
controller.error(new Error());
1595+
}),
1596+
});
1597+
1598+
const reader = stream.getReader();
1599+
(async () => {
1600+
isReadable(stream, true);
1601+
await reader.read().catch(common.mustCall());
1602+
isReadable(stream, false);
1603+
})().then(common.mustCall());
1604+
}

0 commit comments

Comments
 (0)