@@ -1099,6 +1099,10 @@ function isomorphicEncode (input) {
10991099 return input
11001100}
11011101
1102+ const sleep = ( ms ) => {
1103+ return new Promise ( ( resolve ) => setTimeout ( ( ) => resolve ( { value : false } ) , ms ) )
1104+ }
1105+
11021106/**
11031107 * @see https://streams.spec.whatwg.org/#readablestreamdefaultreader-read-all-bytes
11041108 * @see https://streams.spec.whatwg.org/#read-loop
@@ -1107,9 +1111,51 @@ function isomorphicEncode (input) {
11071111async function readAllBytes ( reader ) {
11081112 const bytes = [ ]
11091113 let byteLength = 0
1114+ let times = 0
1115+
1116+ const timesInfo = [ ]
1117+ const timeout = 300000
1118+ const startTime = Date . now ( )
11101119
11111120 while ( true ) {
1112- const { done, value : chunk } = await reader . read ( )
1121+ times ++
1122+
1123+ if ( Date . now ( ) - startTime > timeout ) {
1124+ throw new Error ( `Timeout after ${ timeout } ms while reading response body` )
1125+ }
1126+
1127+ const readPromise = reader . read ( )
1128+ const timeoutPromise = sleep ( timeout )
1129+
1130+ const result = await Promise . race ( [ readPromise , timeoutPromise ] )
1131+
1132+ timesInfo . push ( {
1133+ time : new Date ( ) . toISOString ( ) ,
1134+ times,
1135+ length : byteLength
1136+ } )
1137+
1138+ if ( result && result . value === false ) {
1139+ try {
1140+ const waitingReqPromimse = reader . readRequests ?. [ 0 ] ?. promise
1141+ let waitingReqStatus = 'none'
1142+
1143+ if ( waitingReqPromimse ) {
1144+ waitingReqStatus = await Promise . race ( [
1145+ waitingReqPromimse . then ( ( ) => 'fulfilled' ) . catch ( ( ) => 'rejected' ) ,
1146+ new Promise ( resolve => setTimeout ( ( ) => resolve ( 'pending' ) , timeout ) )
1147+ ] )
1148+ }
1149+
1150+ if ( waitingReqStatus === 'pending' ) {
1151+ throw new Error ( 'Stream read operation is stuck in pending state' )
1152+ }
1153+ } catch ( e ) {
1154+ throw new Error ( `Error while checking read request status: ${ e . message } ` )
1155+ }
1156+ }
1157+
1158+ const { done, value : chunk } = result
11131159
11141160 if ( done ) {
11151161 // 1. Call successSteps with bytes.
0 commit comments