File tree Expand file tree Collapse file tree 7 files changed +64
-7
lines changed Expand file tree Collapse file tree 7 files changed +64
-7
lines changed Original file line number Diff line number Diff line change @@ -2225,6 +2225,19 @@ added: v16.8.0
22252225
22262226Returns whether the stream has been read from or cancelled.
22272227
2228+ ### ` stream.isErrored(stream) `
2229+
2230+ <!-- YAML
2231+ added: REPLACEME
2232+ -->
2233+
2234+ > Stability: 1 - Experimental
2235+
2236+ * ` stream ` {Readable|Writable|Duplex|WritableStream|ReadableStream}
2237+ * Returns: {boolean}
2238+
2239+ Returns whether the stream has encountered an error.
2240+
22282241### ` stream.Readable.toWeb(streamReadable) `
22292242
22302243<!-- YAML
Original file line number Diff line number Diff line change 77} = primordials ;
88
99const kDestroyed = Symbol ( 'kDestroyed' ) ;
10+ const kIsErrored = Symbol ( 'kIsErrored' ) ;
1011const kIsDisturbed = Symbol ( 'kIsDisturbed' ) ;
1112
1213function isReadableNodeStream ( obj , strict = false ) {
@@ -239,16 +240,29 @@ function willEmitClose(stream) {
239240
240241function isDisturbed ( stream ) {
241242 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
245257 ) ) ;
246258}
247259
248260module . exports = {
249261 kDestroyed,
250262 isDisturbed,
263+ isErrored,
251264 kIsDisturbed,
265+ kIsErrored,
252266 isClosed,
253267 isDestroyed,
254268 isDuplexNodeStream,
Original file line number Diff line number Diff line change @@ -82,6 +82,7 @@ const {
8282
8383const {
8484 kIsDisturbed,
85+ kIsErrored,
8586} = require ( 'internal/streams/utils' ) ;
8687
8788const {
@@ -241,6 +242,10 @@ class ReadableStream {
241242 return this [ kState ] . disturbed ;
242243 }
243244
245+ get [ kIsErrored ] ( ) {
246+ return this [ kState ] . state === 'errored' ;
247+ }
248+
244249 /**
245250 * @readonly
246251 * @type {boolean }
Original file line number Diff line number Diff line change @@ -36,9 +36,11 @@ const eos = require('internal/streams/end-of-stream');
3636const internalBuffer = require ( 'internal/buffer' ) ;
3737
3838const promises = require ( 'stream/promises' ) ;
39+ const utils = require ( 'internal/streams/utils' ) ;
3940
4041const 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 ;
4244Stream . Readable = require ( 'internal/streams/readable' ) ;
4345Stream . Writable = require ( 'internal/streams/writable' ) ;
4446Stream . Duplex = require ( 'internal/streams/duplex' ) ;
Original file line number Diff line number Diff line change 11'use strict' ;
22const common = require ( '../common' ) ;
33const assert = require ( 'assert' ) ;
4- const { isDisturbed, Readable } = require ( 'stream' ) ;
4+ const { isDisturbed, isErrored , Readable } = require ( 'stream' ) ;
55
66function noop ( ) { }
77
88function check ( readable , data , fn ) {
99 assert . strictEqual ( readable . readableDidRead , false ) ;
1010 assert . strictEqual ( isDisturbed ( readable ) , false ) ;
11+ assert . strictEqual ( isErrored ( readable ) , false ) ;
1112 if ( data === - 1 ) {
12- readable . on ( 'error' , common . mustCall ( ) ) ;
13+ readable . on ( 'error' , common . mustCall ( ( ) => {
14+ assert . strictEqual ( isErrored ( readable ) , true ) ;
15+ } ) ) ;
1316 readable . on ( 'data' , common . mustNotCall ( ) ) ;
1417 readable . on ( 'end' , common . mustNotCall ( ) ) ;
1518 } else {
Original file line number Diff line number Diff line change 22'use strict' ;
33
44const common = require ( '../common' ) ;
5- const { isDisturbed } = require ( 'stream' ) ;
5+ const { isDisturbed, isErrored } = require ( 'stream' ) ;
66const assert = require ( 'assert' ) ;
77const {
88 isPromise,
@@ -1572,3 +1572,19 @@ class Source {
15721572 isDisturbed ( stream , true ) ;
15731573 } ) ( ) . then ( common . mustCall ( ) ) ;
15741574}
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 = {
211211 'stream.Readable' : 'stream.html#class-streamreadable' ,
212212 'stream.Transform' : 'stream.html#class-streamtransform' ,
213213 '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' ,
214218
215219 'Immediate' : 'timers.html#class-immediate' ,
216220 'Timeout' : 'timers.html#class-timeout' ,
You can’t perform that action at this time.
0 commit comments