88 DecryptionError , FileUnreadableError ,
99 GoogleDriveAuthenticationError , HttpError , CancelledSyncError , MissingPermissionsError ,
1010 NetworkError ,
11- OAuthTokenError , ResourceLockedError , GoogleDriveSearchError
11+ OAuthTokenError , ResourceLockedError , GoogleDriveSearchError , RequestTimeoutError
1212} from '../../errors/Error'
1313import { OAuth2Client } from '@byteowls/capacitor-oauth2'
1414import { Capacitor , CapacitorHttp as Http } from '@capacitor/core'
@@ -41,6 +41,7 @@ declare const chrome: any
4141
4242const LOCK_INTERVAL = 2 * 60 * 1000 // Lock every two minutes while syncing
4343const LOCK_TIMEOUT = 15 * 60 * 1000 // Override lock 15min after last time it was set
44+ const HTTP_TIMEOUT = 60000
4445export default class GoogleDriveAdapter extends CachingAdapter {
4546 static SCOPES = [ 'https://www.googleapis.com/auth/drive.metadata.readonly' ]
4647
@@ -362,20 +363,29 @@ export default class GoogleDriveAdapter extends CachingAdapter {
362363 }
363364
364365 async requestWeb ( method : string , url : string , body : any = null , contentType : string = null ) : Promise < CustomResponse > {
365- let resp
366+ let resp , timedOut = false
366367 try {
367- resp = await fetch ( url , {
368- method,
369- credentials : 'omit' ,
370- headers : {
371- ...( this . accessToken && { Authorization : 'Bearer ' + this . accessToken } ) ,
372- ...( contentType && { 'Content-type' : contentType } )
373- } ,
374- ...( body && { body} ) ,
375- } )
368+ resp = await Promise . race ( [
369+ fetch ( url , {
370+ method,
371+ credentials : 'omit' ,
372+ headers : {
373+ ...( this . accessToken && { Authorization : 'Bearer ' + this . accessToken } ) ,
374+ ...( contentType && { 'Content-type' : contentType } )
375+ } ,
376+ ...( body && { body} ) ,
377+ } ) ,
378+ new Promise ( ( resolve , reject ) =>
379+ setTimeout ( ( ) => {
380+ timedOut = true
381+ reject ( new RequestTimeoutError ( ) )
382+ } , HTTP_TIMEOUT )
383+ )
384+ ] )
376385 } catch ( e ) {
377386 Logger . log ( 'Error Caught' )
378387 Logger . log ( e )
388+ if ( timedOut ) throw e
379389 throw new NetworkError ( )
380390 }
381391 if ( resp . status === 401 || resp . status === 403 ) {
@@ -388,6 +398,8 @@ export default class GoogleDriveAdapter extends CachingAdapter {
388398 async requestNative ( method : string , url : string , body : any = null , contentType : string = null ) : Promise < CustomResponse > {
389399 let res
390400
401+ Logger . log ( `FETCHING ${ method } ${ url } ` )
402+
391403 if ( contentType === 'application/x-www-form-urlencoded' ) {
392404 const params = new URLSearchParams ( )
393405 for ( const [ key , value ] of Object . entries ( body || { } ) ) {
@@ -413,6 +425,8 @@ export default class GoogleDriveAdapter extends CachingAdapter {
413425 throw new NetworkError ( )
414426 }
415427
428+ Logger . log ( `Receiving response for ${ method } ${ url } ` )
429+
416430 if ( res . status === 401 ) {
417431 Logger . log ( 'Failed to authenticate to Google API: ' + JSON . stringify ( res . data ) )
418432 throw new AuthenticationError ( )
0 commit comments