Skip to content

Commit bb873d0

Browse files
authored
Merge pull request #2058 from floccusaddon/enh/gdrive-timeout
enh(GoogleDrive): Add a HTTP request timeout
2 parents daec553 + 389a43d commit bb873d0

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/lib/adapters/GoogleDrive.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
DecryptionError, FileUnreadableError,
99
GoogleDriveAuthenticationError, HttpError, CancelledSyncError, MissingPermissionsError,
1010
NetworkError,
11-
OAuthTokenError, ResourceLockedError, GoogleDriveSearchError
11+
OAuthTokenError, ResourceLockedError, GoogleDriveSearchError, RequestTimeoutError
1212
} from '../../errors/Error'
1313
import { OAuth2Client } from '@byteowls/capacitor-oauth2'
1414
import { Capacitor, CapacitorHttp as Http } from '@capacitor/core'
@@ -41,6 +41,7 @@ declare const chrome: any
4141

4242
const LOCK_INTERVAL = 2 * 60 * 1000 // Lock every two minutes while syncing
4343
const LOCK_TIMEOUT = 15 * 60 * 1000 // Override lock 15min after last time it was set
44+
const HTTP_TIMEOUT = 60000
4445
export 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

Comments
 (0)