@@ -212,19 +212,30 @@ async function downloadWork(
212212 const outFilePath = path . join ( tempOutputDirPath , fileEntry . uuid ) ;
213213 let dledDataSize = 0 ; // bytes
214214 let retriedCount = 0 ;
215+ const rateMeterFile = new rateMeterUtils . RateMeter ( 1000 * rateMeterAvgFactor , true ) ;
215216 const progBarSub =
216217 process . stdout . rows > queue . concurrency + 3
217218 ? progBar ?. create (
218219 fileEntry . size ,
219220 dledDataSize ,
220- termPrettyUtils . progBarTextFmter . download . sub ( dledDataSize , fileEntry . size , fileEntry . path . at ( - 1 ) ?? '' ) ,
221+ termPrettyUtils . progBarTextFmter . download . sub (
222+ dledDataSize ,
223+ fileEntry . size ,
224+ rateMeterFile . getRate ( ) ,
225+ fileEntry . path . at ( - 1 ) ?? '' ,
226+ ) ,
221227 { format : termPrettyUtils . progBarFmtCfg . download . sub } ,
222228 )
223229 : undefined ;
224230 const progBarUpdateFunc = ( subCurrent : number ) => {
225231 progBarSub ?. update (
226232 subCurrent ,
227- termPrettyUtils . progBarTextFmter . download . sub ( subCurrent , fileEntry . size , fileEntry . path . at ( - 1 ) ?? '' ) ,
233+ termPrettyUtils . progBarTextFmter . download . sub (
234+ subCurrent ,
235+ fileEntry . size ,
236+ rateMeterFile . getRate ( ) ,
237+ fileEntry . path . at ( - 1 ) ?? '' ,
238+ ) ,
228239 ) ;
229240 const tmpRootPayload = termPrettyUtils . progBarTextFmter . download . root (
230241 dledFileEntry . length ,
@@ -242,20 +253,17 @@ async function downloadWork(
242253
243254 while ( retriedCount <= appConfig . network . retryCount ) {
244255 const abortController = new AbortController ( ) ;
245- const rateMeterFile = new rateMeterUtils . RateMeter ( 1000 , true ) ;
246256 let timeoutTimer : NodeJS . Timeout | null = null ;
247257 let lastNonZeroRateTime = Date . now ( ) ;
248-
258+ rateMeterFile . reset ( ) ;
249259 try {
250260 timeoutTimer = setInterval ( ( ) => {
251- if ( rateMeterFile . getRate ( ) === 0 ) {
252- if ( Date . now ( ) - lastNonZeroRateTime > appConfig . network . timeout ) {
253- abortController . abort ( new Error ( 'Download timeout due to zero speed' ) ) ;
254- }
255- } else {
256- lastNonZeroRateTime = Date . now ( ) ;
261+ rateMeterFile . increment ( 0 ) ;
262+ progBarUpdateFunc ( dledDataSize ) ;
263+ if ( Date . now ( ) - lastNonZeroRateTime > appConfig . network . timeout ) {
264+ abortController . abort ( ) ;
257265 }
258- } , 1000 ) ;
266+ } , 250 ) ;
259267
260268 const progressStream = new stream . Transform ( {
261269 transform ( chunk , _encoding , callback ) {
@@ -264,9 +272,7 @@ async function downloadWork(
264272 rateMeterFile . increment ( chunk . length ) ;
265273 dledDataSize += chunk . length ;
266274 progBarUpdateFunc ( dledDataSize ) ;
267- if ( rateMeterFile . getRate ( ) > 0 ) {
268- lastNonZeroRateTime = Date . now ( ) ;
269- }
275+ lastNonZeroRateTime = Date . now ( ) ;
270276 this . push ( chunk ) ;
271277 callback ( ) ;
272278 } ,
@@ -281,6 +287,7 @@ async function downloadWork(
281287 stream . Readable . fromWeb ( response . body as any ) ,
282288 progressStream ,
283289 fs . createWriteStream ( outFilePath , { flags : 'wx' } ) ,
290+ { signal : abortController . signal } ,
284291 ) ;
285292 progBarUpdateFunc ( fileEntry . size ) ;
286293 dledFileEntry . push ( fileEntry ) ;
@@ -307,6 +314,7 @@ async function downloadWork(
307314 // `Download failed, retrying... (${retriedCount}/${appConfig.network.retryCount}): ${fileEntry.path.join('/')}`,
308315 // );
309316 dledDataSizeGlobal -= dledDataSize ;
317+ dledDataSize = 0 ;
310318 }
311319
312320 if ( timeoutTimer ) clearInterval ( timeoutTimer ) ;
0 commit comments