Skip to content

Commit 7cef769

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 9c718f8 commit 7cef769

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
@@ -2211,6 +2211,19 @@ added: REPLACEME
22112211

22122212
Returns whether the stream has encountered an error.
22132213

2214+
### `stream.isReadable(stream)`
2215+
2216+
<!-- YAML
2217+
added: REPLACEME
2218+
-->
2219+
2220+
> Stability: 1 - Experimental
2221+
2222+
* `stream` {Readable|Duplex|ReadableStream}
2223+
* Returns: {boolean}
2224+
2225+
Returns whether the stream is readable.
2226+
22142227
### `stream.Readable.toWeb(streamReadable)`
22152228

22162229
<!-- YAML

lib/internal/streams/utils.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const {
88

99
const kDestroyed = Symbol('kDestroyed');
1010
const kIsErrored = Symbol('kIsErrored');
11+
const kIsReadable = Symbol('kIsReadable');
1112
const kIsDisturbed = Symbol('kIsDisturbed');
1213

1314
function isReadableNodeStream(obj) {
@@ -120,6 +121,7 @@ function isDisturbed(stream) {
120121
}
121122

122123
function isReadable(stream) {
124+
if (stream && stream[kIsReadable] != null) return stream[kIsReadable];
123125
const r = isReadableNodeStream(stream);
124126
if (r === null || typeof stream?.readable !== 'boolean') return null;
125127
if (isDestroyed(stream)) return false;
@@ -235,15 +237,16 @@ function isErrored(stream) {
235237

236238
module.exports = {
237239
isDisturbed,
238-
isErrored,
239240
kIsDisturbed,
241+
isErrored,
240242
kIsErrored,
243+
isReadable,
244+
kIsReadable,
241245
isClosed,
242246
isDestroyed,
243247
isDuplexNodeStream,
244248
isFinished,
245249
isIterable,
246-
isReadable,
247250
isReadableNodeStream,
248251
isReadableEnded,
249252
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 {
@@ -261,6 +262,10 @@ class ReadableStream {
261262
return this[kState].state === 'errored';
262263
}
263264

265+
get [kIsReadable]() {
266+
return this[kState].state === 'readable';
267+
}
268+
264269
/**
265270
* @readonly
266271
* @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)