@@ -136,7 +136,7 @@ export class DownloadManagerService {
136136 this . httpService . get ( url , {
137137 responseType : 'stream' ,
138138 signal : controller . signal ,
139- } ) ,
139+ } )
140140 ) ;
141141
142142 // check if response is success
@@ -164,6 +164,24 @@ export class DownloadManagerService {
164164
165165 console . log ( 'Downloading' , basename ( destination ) ) ;
166166
167+ const timeout = 20000 ; // Timeout period for receiving new data
168+ let timeoutId : NodeJS . Timeout ;
169+ const resetTimeout = ( ) => {
170+ if ( timeoutId ) clearTimeout ( timeoutId ) ;
171+ timeoutId = setTimeout ( ( ) => {
172+ try {
173+ this . handleError (
174+ new Error ( 'Download timeout' ) ,
175+ downloadId ,
176+ destination ,
177+ )
178+ } finally {
179+ bar . stop ( ) ;
180+ resolve ( ) ;
181+ }
182+ } , timeout ) ;
183+ } ;
184+
167185 let transferredBytes = 0 ;
168186 const bar = new SingleBar ( { } , Presets . shades_classic ) ;
169187 bar . start ( 100 , 0 ) ;
@@ -190,38 +208,17 @@ export class DownloadManagerService {
190208 resolve ( ) ;
191209 }
192210 } ) ;
193-
194211 writer . on ( 'error' , ( error ) => {
195212 try {
196- delete this . abortControllers [ downloadId ] [ destination ] ;
197- const currentDownloadState = this . allDownloadStates . find (
198- ( downloadState ) => downloadState . id === downloadId ,
199- ) ;
200- if ( ! currentDownloadState ) return ;
201-
202- const downloadItem = currentDownloadState ?. children . find (
203- ( downloadItem ) => downloadItem . id === destination ,
204- ) ;
205- if ( downloadItem ) {
206- downloadItem . status = DownloadStatus . Error ;
207- downloadItem . error = error . message ;
208- }
209-
210- currentDownloadState . status = DownloadStatus . Error ;
211- currentDownloadState . error = error . message ;
212-
213- // remove download state if all children is downloaded
214- this . allDownloadStates = this . allDownloadStates . filter (
215- ( downloadState ) => downloadState . id !== downloadId ,
216- ) ;
217- this . eventEmitter . emit ( 'download.event' , this . allDownloadStates ) ;
213+ this . handleError ( error , downloadId , destination ) ;
218214 } finally {
219215 bar . stop ( ) ;
220216 resolve ( ) ;
221217 }
222218 } ) ;
223219
224220 response . data . on ( 'data' , ( chunk : any ) => {
221+ resetTimeout ( ) ;
225222 transferredBytes += chunk . length ;
226223
227224 const currentDownloadState = this . allDownloadStates . find (
@@ -266,4 +263,31 @@ export class DownloadManagerService {
266263 getDownloadStates ( ) {
267264 return this . allDownloadStates ;
268265 }
266+
267+ private handleError ( error : Error , downloadId : string , destination : string ) {
268+ console . log ( this . allDownloadStates , downloadId , destination )
269+ delete this . abortControllers [ downloadId ] [ destination ] ;
270+ const currentDownloadState = this . allDownloadStates . find (
271+ ( downloadState ) => downloadState . id === downloadId ,
272+ ) ;
273+ if ( ! currentDownloadState ) return ;
274+
275+ const downloadItem = currentDownloadState ?. children . find (
276+ ( downloadItem ) => downloadItem . id === destination ,
277+ ) ;
278+ if ( downloadItem ) {
279+ downloadItem . status = DownloadStatus . Error ;
280+ downloadItem . error = error . message ;
281+ }
282+
283+ currentDownloadState . status = DownloadStatus . Error ;
284+ currentDownloadState . error = error . message ;
285+
286+ // remove download state if all children is downloaded
287+ this . allDownloadStates = this . allDownloadStates . filter (
288+ ( downloadState ) => downloadState . id !== downloadId ,
289+ ) ;
290+ this . eventEmitter . emit ( 'download.event' , [ currentDownloadState ] ) ;
291+ this . eventEmitter . emit ( 'download.event' , this . allDownloadStates ) ;
292+ }
269293}
0 commit comments