File tree 6 files changed +61
-7
lines changed
6 files changed +61
-7
lines changed Original file line number Diff line number Diff line change @@ -2225,6 +2225,19 @@ added: v16.8.0
2225
2225
2226
2226
Returns whether the stream has been read from or cancelled.
2227
2227
2228
+ ### ` stream.Readable.isErrored(stream) `
2229
+
2230
+ <!-- YAML
2231
+ added: v16.8.0
2232
+ -->
2233
+
2234
+ > Stability: 1 - Experimental
2235
+
2236
+ * ` stream ` {stream.Readable|ReadableStream}
2237
+ * Returns: ` boolean `
2238
+
2239
+ Returns whether the stream has been errored.
2240
+
2228
2241
### ` stream.Readable.toWeb(streamReadable) `
2229
2242
2230
2243
<!-- YAML
Original file line number Diff line number Diff line change 7
7
} = primordials ;
8
8
9
9
const kDestroyed = Symbol ( 'kDestroyed' ) ;
10
+ const kIsErrored = Symbol ( 'kIsErrored' ) ;
10
11
const kIsDisturbed = Symbol ( 'kIsDisturbed' ) ;
11
12
12
13
function isReadableNodeStream ( obj , strict = false ) {
@@ -239,16 +240,29 @@ function willEmitClose(stream) {
239
240
240
241
function isDisturbed ( stream ) {
241
242
return ! ! ( stream && (
242
- stream . readableDidRead ||
243
- stream . readableAborted ||
244
- stream [ kIsDisturbed ]
243
+ stream [ kIsDisturbed ] ??
244
+ ( stream . readableDidRead || stream . readableAborted )
245
+ ) ) ;
246
+ }
247
+
248
+ function isErrored ( stream ) {
249
+ return ! ! ( stream && (
250
+ stream [ kIsErrored ] ??
251
+ stream . readableErrored ??
252
+ stream . writableErrored ??
253
+ stream . _readableState ?. errorEmitted ??
254
+ stream . _writableState ?. errorEmitted ??
255
+ stream . _readableState ?. errored ??
256
+ stream . _writableState ?. errored
245
257
) ) ;
246
258
}
247
259
248
260
module . exports = {
249
261
kDestroyed,
250
262
isDisturbed,
263
+ isErrored,
251
264
kIsDisturbed,
265
+ kIsErrored,
252
266
isClosed,
253
267
isDestroyed,
254
268
isDuplexNodeStream,
Original file line number Diff line number Diff line change @@ -82,6 +82,7 @@ const {
82
82
83
83
const {
84
84
kIsDisturbed,
85
+ kIsErrored,
85
86
} = require ( 'internal/streams/utils' ) ;
86
87
87
88
const {
@@ -241,6 +242,10 @@ class ReadableStream {
241
242
return this [ kState ] . disturbed ;
242
243
}
243
244
245
+ get [ kIsErrored ] ( ) {
246
+ return this [ kState ] . state === 'errored' ;
247
+ }
248
+
244
249
/**
245
250
* @readonly
246
251
* @type {boolean }
Original file line number Diff line number Diff line change @@ -36,9 +36,11 @@ const eos = require('internal/streams/end-of-stream');
36
36
const internalBuffer = require ( 'internal/buffer' ) ;
37
37
38
38
const promises = require ( 'stream/promises' ) ;
39
+ const utils = require ( 'internal/streams/utils' ) ;
39
40
40
41
const Stream = module . exports = require ( 'internal/streams/legacy' ) . Stream ;
41
- Stream . isDisturbed = require ( 'internal/streams/utils' ) . isDisturbed ;
42
+ Stream . isDisturbed = utils . isDisturbed ;
43
+ Stream . isErrored = utils . isErrored ;
42
44
Stream . Readable = require ( 'internal/streams/readable' ) ;
43
45
Stream . Writable = require ( 'internal/streams/writable' ) ;
44
46
Stream . Duplex = require ( 'internal/streams/duplex' ) ;
Original file line number Diff line number Diff line change 1
1
'use strict' ;
2
2
const common = require ( '../common' ) ;
3
3
const assert = require ( 'assert' ) ;
4
- const { isDisturbed, Readable } = require ( 'stream' ) ;
4
+ const { isDisturbed, isErrored , Readable } = require ( 'stream' ) ;
5
5
6
6
function noop ( ) { }
7
7
8
8
function check ( readable , data , fn ) {
9
9
assert . strictEqual ( readable . readableDidRead , false ) ;
10
10
assert . strictEqual ( isDisturbed ( readable ) , false ) ;
11
+ assert . strictEqual ( isErrored ( readable ) , false ) ;
11
12
if ( data === - 1 ) {
12
- readable . on ( 'error' , common . mustCall ( ) ) ;
13
+ readable . on ( 'error' , common . mustCall ( ( ) => {
14
+ assert . strictEqual ( isErrored ( readable ) , true ) ;
15
+ } ) ) ;
13
16
readable . on ( 'data' , common . mustNotCall ( ) ) ;
14
17
readable . on ( 'end' , common . mustNotCall ( ) ) ;
15
18
} else {
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 } = require ( 'stream' ) ;
5
+ const { isDisturbed, isErrored } = require ( 'stream' ) ;
6
6
const assert = require ( 'assert' ) ;
7
7
const {
8
8
isPromise,
@@ -1572,3 +1572,20 @@ class Source {
1572
1572
isDisturbed ( stream , true ) ;
1573
1573
} ) ( ) . then ( common . mustCall ( ) ) ;
1574
1574
}
1575
+
1576
+
1577
+ {
1578
+ const stream = new ReadableStream ( {
1579
+ start ( controller ) {
1580
+ controller . error ( new Error ( ) ) ;
1581
+ } ,
1582
+ pull : common . mustNotCall ( ) ,
1583
+ } ) ;
1584
+
1585
+ const reader = stream . getReader ( ) ;
1586
+ ( async ( ) => {
1587
+ isErrored ( stream , false ) ;
1588
+ await reader . read ( ) ;
1589
+ isErrored ( stream , true ) ;
1590
+ } ) ( ) . then ( common . mustCall ( ) ) ;
1591
+ }
You can’t perform that action at this time.
0 commit comments