Skip to content

Commit 3b19515

Browse files
committed
stream: add isReadable helper
1 parent 752d75d commit 3b19515

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

doc/api/stream.md

+13
Original file line numberDiff line numberDiff line change
@@ -2238,6 +2238,19 @@ added: REPLACEME
22382238

22392239
Returns whether the stream has encountered an error.
22402240

2241+
### `stream.isReadable(stream)`
2242+
2243+
<!-- YAML
2244+
added: REPLACEME
2245+
-->
2246+
2247+
> Stability: 1 - Experimental
2248+
2249+
* `stream` {Readable|Duplex|ReadableStream}
2250+
* Returns: {boolean}
2251+
2252+
Returns whether the stream is readable.
2253+
22412254
### `stream.Readable.toWeb(streamReadable)`
22422255

22432256
<!-- YAML

lib/internal/streams/utils.js

+5-1
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, strict = false) {
@@ -116,6 +117,7 @@ function isReadableFinished(stream, strict) {
116117
}
117118

118119
function isReadable(stream) {
120+
if (stream[kIsReadable] != null) return stream[kIsReadable];
119121
const r = isReadableNodeStream(stream);
120122
if (r === null || typeof stream?.readable !== 'boolean') return null;
121123
if (isDestroyed(stream)) return false;
@@ -260,9 +262,11 @@ function isErrored(stream) {
260262
module.exports = {
261263
kDestroyed,
262264
isDisturbed,
263-
isErrored,
264265
kIsDisturbed,
266+
isErrored,
265267
kIsErrored,
268+
isReadable,
269+
kIsReadable,
266270
isClosed,
267271
isDestroyed,
268272
isDuplexNodeStream,

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}

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)