@@ -32,8 +32,10 @@ const {
32
32
ObjectDefineProperty,
33
33
ObjectDefineProperties,
34
34
ObjectSetPrototypeOf,
35
+ Promise,
35
36
StringPrototypeToLowerCase,
36
37
Symbol,
38
+ SymbolAsyncDispose,
37
39
SymbolHasInstance,
38
40
} = primordials ;
39
41
@@ -44,6 +46,7 @@ const EE = require('events');
44
46
const Stream = require ( 'internal/streams/legacy' ) . Stream ;
45
47
const { Buffer } = require ( 'buffer' ) ;
46
48
const destroyImpl = require ( 'internal/streams/destroy' ) ;
49
+ const eos = require ( 'internal/streams/end-of-stream' ) ;
47
50
48
51
const {
49
52
addAbortSignal,
@@ -54,16 +57,19 @@ const {
54
57
getDefaultHighWaterMark,
55
58
} = require ( 'internal/streams/state' ) ;
56
59
const {
57
- ERR_INVALID_ARG_TYPE ,
58
- ERR_METHOD_NOT_IMPLEMENTED ,
59
- ERR_MULTIPLE_CALLBACK ,
60
- ERR_STREAM_CANNOT_PIPE ,
61
- ERR_STREAM_DESTROYED ,
62
- ERR_STREAM_ALREADY_FINISHED ,
63
- ERR_STREAM_NULL_VALUES ,
64
- ERR_STREAM_WRITE_AFTER_END ,
65
- ERR_UNKNOWN_ENCODING ,
66
- } = require ( 'internal/errors' ) . codes ;
60
+ AbortError,
61
+ codes : {
62
+ ERR_INVALID_ARG_TYPE ,
63
+ ERR_METHOD_NOT_IMPLEMENTED ,
64
+ ERR_MULTIPLE_CALLBACK ,
65
+ ERR_STREAM_CANNOT_PIPE ,
66
+ ERR_STREAM_DESTROYED ,
67
+ ERR_STREAM_ALREADY_FINISHED ,
68
+ ERR_STREAM_NULL_VALUES ,
69
+ ERR_STREAM_WRITE_AFTER_END ,
70
+ ERR_UNKNOWN_ENCODING ,
71
+ } ,
72
+ } = require ( 'internal/errors' ) ;
67
73
68
74
const { errorOrDestroy } = destroyImpl ;
69
75
@@ -477,7 +483,7 @@ function onwrite(stream, er) {
477
483
// rather just increase a counter, to improve performance and avoid
478
484
// memory allocations.
479
485
if ( state . afterWriteTickInfo !== null &&
480
- state . afterWriteTickInfo . cb === cb ) {
486
+ state . afterWriteTickInfo . cb === cb ) {
481
487
state . afterWriteTickInfo . count ++ ;
482
488
} else {
483
489
state . afterWriteTickInfo = { count : 1 , cb, stream, state } ;
@@ -538,9 +544,9 @@ function errorBuffer(state) {
538
544
// If there's something in the buffer waiting, then process it.
539
545
function clearBuffer ( stream , state ) {
540
546
if ( state . corked ||
541
- state . bufferProcessing ||
542
- state . destroyed ||
543
- ! state . constructed ) {
547
+ state . bufferProcessing ||
548
+ state . destroyed ||
549
+ ! state . constructed ) {
544
550
return ;
545
551
}
546
552
@@ -934,3 +940,12 @@ Writable.fromWeb = function(writableStream, options) {
934
940
Writable . toWeb = function ( streamWritable ) {
935
941
return lazyWebStreams ( ) . newWritableStreamFromStreamWritable ( streamWritable ) ;
936
942
} ;
943
+
944
+ Writable . prototype [ SymbolAsyncDispose ] = function ( ) {
945
+ let error ;
946
+ if ( ! this . destroyed ) {
947
+ error = this . readableEnded ? null : new AbortError ( ) ;
948
+ this . destroy ( error ) ;
949
+ }
950
+ return new Promise ( ( resolve , reject ) => eos ( this , ( err ) => ( err && err !== error ? reject ( err ) : resolve ( null ) ) ) ) ;
951
+ } ;
0 commit comments