File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -65,6 +65,12 @@ exports.Readable = class Readable extends stream.Readable {
6565
6666 super . unshift ( chunk )
6767 }
68+
69+ async [ Symbol . asyncDispose ] ( ) {
70+ if ( ! this . destroyed ) this . destroy ( )
71+
72+ await new Promise ( ( resolve ) => exports . finished ( this , resolve ) )
73+ }
6874}
6975
7076exports . Writable = class Writable extends stream . Writable {
@@ -131,6 +137,12 @@ exports.Writable = class Writable extends stream.Writable {
131137
132138 return result
133139 }
140+
141+ async [ Symbol . asyncDispose ] ( ) {
142+ if ( ! this . destroyed ) this . destroy ( )
143+
144+ await new Promise ( ( resolve ) => exports . finished ( this , resolve ) )
145+ }
134146}
135147
136148exports . Duplex = class Duplex extends stream . Duplex {
Original file line number Diff line number Diff line change @@ -720,4 +720,19 @@ test('duplexPair', (t) => {
720720 pairA . end ( )
721721} )
722722
723+ test ( 'async disposable' , async ( t ) => {
724+ t . plan ( 2 )
725+
726+ const readable = new Readable ( )
727+ const writable = new Writable ( )
728+
729+ {
730+ await using disposableReadable = readable
731+ await using disposableWritable = writable
732+ }
733+
734+ t . ok ( readable . destroyed )
735+ t . ok ( writable . destroyed )
736+ } )
737+
723738function noop ( ) { }
You can’t perform that action at this time.
0 commit comments