@@ -723,6 +723,18 @@ async function* createAsyncPipeline(source, transforms, signal) {
723723 }
724724}
725725
726+ /**
727+ * Check if a false sync write result means accepted backpressure.
728+ * @param {object } writer - The writer whose sync method returned.
729+ * @param {* } result - The return value from writeSync() or writevSync().
730+ * @returns {boolean }
731+ */
732+ function isAcceptedSyncWriteBackpressure ( writer , result ) {
733+ return result === false &&
734+ writer [ kSyncWriteAcceptedOnFalse ] === true &&
735+ writer . desiredSize === 0 ;
736+ }
737+
726738// =============================================================================
727739// Public API: pull() and pullSync()
728740// =============================================================================
@@ -810,16 +822,29 @@ function pipeToSync(source, ...args) {
810822 const hasEndSync = typeof writer . endSync === 'function' ;
811823
812824 try {
825+ let canContinue = true ;
813826 for ( const batch of pipeline ) {
827+ if ( ! canContinue ) {
828+ break ;
829+ }
814830 if ( hasWritevSync && batch . length > 1 ) {
815- writer . writevSync ( batch ) ;
831+ const result = writer . writevSync ( batch ) ;
832+ if ( result === false &&
833+ ! isAcceptedSyncWriteBackpressure ( writer , result ) ) {
834+ break ;
835+ }
816836 for ( let i = 0 ; i < batch . length ; i ++ ) {
817837 totalBytes += TypedArrayPrototypeGetByteLength ( batch [ i ] ) ;
818838 }
819839 } else {
820840 for ( let i = 0 ; i < batch . length ; i ++ ) {
821841 const chunk = batch [ i ] ;
822- writer . writeSync ( chunk ) ;
842+ const result = writer . writeSync ( chunk ) ;
843+ if ( result === false &&
844+ ! isAcceptedSyncWriteBackpressure ( writer , result ) ) {
845+ canContinue = false ;
846+ break ;
847+ }
823848 totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
824849 }
825850 }
@@ -871,11 +896,6 @@ async function pipeTo(source, ...args) {
871896 const hasWritev = typeof writer . writev === 'function' ;
872897 const hasWritevSync = typeof writer . writevSync === 'function' ;
873898 const hasEndSync = typeof writer . endSync === 'function' ;
874- const syncFalseCanBeAccepted = writer [ kSyncWriteAcceptedOnFalse ] === true ;
875-
876- function syncFalseWasAccepted ( ) {
877- return syncFalseCanBeAccepted && writer . desiredSize === 0 ;
878- }
879899
880900 function waitForSyncBackpressure ( ) {
881901 const ondrain = writer [ drainableProtocol ] ;
@@ -892,9 +912,10 @@ async function pipeTo(source, ...args) {
892912 async function writeBatchAsyncFallback ( batch , startIndex ) {
893913 for ( let i = startIndex ; i < batch . length ; i ++ ) {
894914 const chunk = batch [ i ] ;
895- if ( hasWriteSync && writer . writeSync ( chunk ) ) {
915+ const result = hasWriteSync && writer . writeSync ( chunk ) ;
916+ if ( result ) {
896917 // Sync retry succeeded
897- } else if ( syncFalseWasAccepted ( ) ) {
918+ } else if ( isAcceptedSyncWriteBackpressure ( writer , result ) ) {
898919 totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
899920 await waitForSyncBackpressure ( ) ;
900921 continue ;
@@ -914,8 +935,9 @@ async function pipeTo(source, ...args) {
914935 // is required. Callers must check: const p = writeBatch(b); if (p) await p;
915936 function writeBatch ( batch ) {
916937 if ( hasWritev && batch . length > 1 ) {
917- if ( ! hasWritevSync || ! writer . writevSync ( batch ) ) {
918- if ( hasWritevSync && syncFalseWasAccepted ( ) ) {
938+ const result = hasWritevSync && writer . writevSync ( batch ) ;
939+ if ( ! result ) {
940+ if ( isAcceptedSyncWriteBackpressure ( writer , result ) ) {
919941 for ( let i = 0 ; i < batch . length ; i ++ ) {
920942 totalBytes += TypedArrayPrototypeGetByteLength ( batch [ i ] ) ;
921943 }
@@ -935,8 +957,9 @@ async function pipeTo(source, ...args) {
935957 }
936958 for ( let i = 0 ; i < batch . length ; i ++ ) {
937959 const chunk = batch [ i ] ;
938- if ( ! hasWriteSync || ! writer . writeSync ( chunk ) ) {
939- if ( hasWriteSync && syncFalseWasAccepted ( ) ) {
960+ const result = hasWriteSync && writer . writeSync ( chunk ) ;
961+ if ( ! result ) {
962+ if ( isAcceptedSyncWriteBackpressure ( writer , result ) ) {
940963 totalBytes += TypedArrayPrototypeGetByteLength ( chunk ) ;
941964 return writeBatchAfterAcceptedBackpressure ( batch , i + 1 ) ;
942965 }
0 commit comments