File tree 5 files changed +40
-4
lines changed
5 files changed +40
-4
lines changed Original file line number Diff line number Diff line change @@ -2223,6 +2223,19 @@ added: REPLACEME
2223
2223
2224
2224
Returns whether the stream has encountered an error.
2225
2225
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
+
2226
2239
### ` stream.Readable.toWeb(streamReadable) `
2227
2240
2228
2241
<!-- YAML
Original file line number Diff line number Diff line change 7
7
} = primordials ;
8
8
9
9
const kIsErrored = Symbol ( 'kIsErrored' ) ;
10
+ const kIsReadable = Symbol ( 'kIsReadable' ) ;
10
11
const kIsDisturbed = Symbol ( 'kIsDisturbed' ) ;
11
12
12
13
function isReadableNodeStream ( obj ) {
@@ -119,6 +120,7 @@ function isDisturbed(stream) {
119
120
}
120
121
121
122
function isReadable ( stream ) {
123
+ if ( stream && stream [ kIsReadable ] != null ) return stream [ kIsReadable ] ;
122
124
const r = isReadableNodeStream ( stream ) ;
123
125
if ( r === null || typeof stream ?. readable !== 'boolean' ) return null ;
124
126
if ( isDestroyed ( stream ) ) return false ;
@@ -234,15 +236,16 @@ function isErrored(stream) {
234
236
235
237
module . exports = {
236
238
isDisturbed,
237
- isErrored,
238
239
kIsDisturbed,
240
+ isErrored,
239
241
kIsErrored,
242
+ isReadable,
243
+ kIsReadable,
240
244
isClosed,
241
245
isDestroyed,
242
246
isDuplexNodeStream,
243
247
isFinished,
244
248
isIterable,
245
- isReadable,
246
249
isReadableNodeStream,
247
250
isReadableEnded,
248
251
isReadableFinished,
Original file line number Diff line number Diff line change @@ -83,6 +83,7 @@ const {
83
83
const {
84
84
kIsDisturbed,
85
85
kIsErrored,
86
+ kIsReadable,
86
87
} = require ( 'internal/streams/utils' ) ;
87
88
88
89
const {
@@ -246,6 +247,10 @@ class ReadableStream {
246
247
return this [ kState ] . state === 'errored' ;
247
248
}
248
249
250
+ get [ kIsReadable ] ( ) {
251
+ return this [ kState ] . state === 'readable' ;
252
+ }
253
+
249
254
/**
250
255
* @readonly
251
256
* @type {boolean }
Original file line number Diff line number Diff line change @@ -44,6 +44,7 @@ const utils = require('internal/streams/utils');
44
44
const Stream = module . exports = require ( 'internal/streams/legacy' ) . Stream ;
45
45
Stream . isDisturbed = utils . isDisturbed ;
46
46
Stream . isErrored = utils . isErrored ;
47
+ Stream . isReadable = utils . isReadable ;
47
48
Stream . Readable = require ( 'internal/streams/readable' ) ;
48
49
for ( const key of ObjectKeys ( operators ) ) {
49
50
const op = operators [ key ] ;
Original file line number Diff line number Diff line change 2
2
'use strict' ;
3
3
4
4
const common = require ( '../common' ) ;
5
- const { isDisturbed, isErrored } = require ( 'stream' ) ;
5
+ const { isDisturbed, isErrored, isReadable } = require ( 'stream' ) ;
6
6
const assert = require ( 'assert' ) ;
7
7
const {
8
8
isPromise,
@@ -1573,7 +1573,6 @@ class Source {
1573
1573
} ) ( ) . then ( common . mustCall ( ) ) ;
1574
1574
}
1575
1575
1576
-
1577
1576
{
1578
1577
const stream = new ReadableStream ( {
1579
1578
pull : common . mustCall ( ( controller ) => {
@@ -1588,3 +1587,18 @@ class Source {
1588
1587
isErrored ( stream , true ) ;
1589
1588
} ) ( ) . then ( common . mustCall ( ) ) ;
1590
1589
}
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
+ }
You can’t perform that action at this time.
0 commit comments