Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 1150963

Browse filesBrowse files
ronagtargos
authored andcommitted
stream: add isReadable helper
PR-URL: #41199 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 9f5a873 commit 1150963
Copy full SHA for 1150963

File tree

Expand file treeCollapse file tree

5 files changed

+40
-4
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+40
-4
lines changed
Open diff view settings
Collapse file

‎doc/api/stream.md‎

Copy file name to clipboardExpand all lines: doc/api/stream.md
+13Lines changed: 13 additions & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,19 @@ added: v17.3.0
22282228

22292229
Returns whether the stream has encountered an error.
22302230

2231+
### `stream.isReadable(stream)`
2232+
2233+
<!-- YAML
2234+
added: REPLACEME
2235+
-->
2236+
2237+
> Stability: 1 - Experimental
2238+
2239+
* `stream` {Readable|Duplex|ReadableStream}
2240+
* Returns: {boolean}
2241+
2242+
Returns whether the stream is readable.
2243+
22312244
### `stream.Readable.toWeb(streamReadable)`
22322245

22332246
<!-- YAML
Collapse file

‎lib/internal/streams/utils.js‎

Copy file name to clipboardExpand all lines: lib/internal/streams/utils.js
+5-2Lines changed: 5 additions & 2 deletions
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 && 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;
@@ -232,15 +234,16 @@ function isErrored(stream) {
232234
module.exports = {
233235
kDestroyed,
234236
isDisturbed,
235-
isErrored,
236237
kIsDisturbed,
238+
isErrored,
237239
kIsErrored,
240+
isReadable,
241+
kIsReadable,
238242
isClosed,
239243
isDestroyed,
240244
isDuplexNodeStream,
241245
isFinished,
242246
isIterable,
243-
isReadable,
244247
isReadableNodeStream,
245248
isReadableEnded,
246249
isReadableFinished,
Collapse file

‎lib/internal/webstreams/readablestream.js‎

Copy file name to clipboardExpand all lines: lib/internal/webstreams/readablestream.js
+5Lines changed: 5 additions & 0 deletions
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}
Collapse file

‎lib/stream.js‎

Copy file name to clipboardExpand all lines: lib/stream.js
+1Lines changed: 1 addition & 0 deletions
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];
Collapse file

‎test/parallel/test-whatwg-readablestream.js‎

Copy file name to clipboardExpand all lines: test/parallel/test-whatwg-readablestream.js
+16-2Lines changed: 16 additions & 2 deletions
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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.