File tree 7 files changed +64
-7
lines changed
7 files changed +64
-7
lines changed Original file line number Diff line number Diff line change @@ -2171,6 +2171,19 @@ added: v16.8.0
2171
2171
2172
2172
Returns whether the stream has been read from or cancelled.
2173
2173
2174
+ ### ` stream.isErrored(stream) `
2175
+
2176
+ <!-- YAML
2177
+ added: REPLACEME
2178
+ -->
2179
+
2180
+ > Stability: 1 - Experimental
2181
+
2182
+ * ` stream ` {Readable|Writable|Duplex|WritableStream|ReadableStream}
2183
+ * Returns: {boolean}
2184
+
2185
+ Returns whether the stream has encountered an error.
2186
+
2174
2187
### ` stream.Readable.toWeb(streamReadable) `
2175
2188
2176
2189
<!-- 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 ) {
@@ -211,16 +212,29 @@ function willEmitClose(stream) {
211
212
212
213
function isDisturbed ( stream ) {
213
214
return ! ! ( stream && (
214
- stream . readableDidRead ||
215
- stream . readableAborted ||
216
- stream [ kIsDisturbed ]
215
+ stream [ kIsDisturbed ] ??
216
+ ( stream . readableDidRead || stream . readableAborted )
217
+ ) ) ;
218
+ }
219
+
220
+ function isErrored ( stream ) {
221
+ return ! ! ( stream && (
222
+ stream [ kIsErrored ] ??
223
+ stream . readableErrored ??
224
+ stream . writableErrored ??
225
+ stream . _readableState ?. errorEmitted ??
226
+ stream . _writableState ?. errorEmitted ??
227
+ stream . _readableState ?. errored ??
228
+ stream . _writableState ?. errored
217
229
) ) ;
218
230
}
219
231
220
232
module . exports = {
221
233
kDestroyed,
222
234
isDisturbed,
235
+ isErrored,
223
236
kIsDisturbed,
237
+ kIsErrored,
224
238
isClosed,
225
239
isDestroyed,
226
240
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,19 @@ class Source {
1572
1572
isDisturbed ( stream , true ) ;
1573
1573
} ) ( ) . then ( common . mustCall ( ) ) ;
1574
1574
}
1575
+
1576
+
1577
+ {
1578
+ const stream = new ReadableStream ( {
1579
+ pull : common . mustCall ( ( controller ) => {
1580
+ controller . error ( new Error ( ) ) ;
1581
+ } ) ,
1582
+ } ) ;
1583
+
1584
+ const reader = stream . getReader ( ) ;
1585
+ ( async ( ) => {
1586
+ isErrored ( stream , false ) ;
1587
+ await reader . read ( ) . catch ( common . mustCall ( ) ) ;
1588
+ isErrored ( stream , true ) ;
1589
+ } ) ( ) . then ( common . mustCall ( ) ) ;
1590
+ }
Original file line number Diff line number Diff line change @@ -211,6 +211,10 @@ const customTypesMap = {
211
211
'stream.Readable' : 'stream.html#class-streamreadable' ,
212
212
'stream.Transform' : 'stream.html#class-streamtransform' ,
213
213
'stream.Writable' : 'stream.html#class-streamwritable' ,
214
+ 'Duplex' : 'stream.html#class-streamduplex' ,
215
+ 'Readable' : 'stream.html#class-streamreadable' ,
216
+ 'Transform' : 'stream.html#class-streamtransform' ,
217
+ 'Writable' : 'stream.html#class-streamwritable' ,
214
218
215
219
'Immediate' : 'timers.html#class-immediate' ,
216
220
'Timeout' : 'timers.html#class-timeout' ,
You can’t perform that action at this time.
0 commit comments