@@ -392,30 +392,45 @@ class ParseServer {
392
392
return server ;
393
393
}
394
394
395
- static verifyServerUrl ( callback ) {
395
+ static async verifyServerUrl ( ) {
396
396
// perform a health check on the serverURL value
397
397
if ( Parse . serverURL ) {
398
+ const isValidHttpUrl = string => {
399
+ let url ;
400
+ try {
401
+ url = new URL ( string ) ;
402
+ } catch ( _ ) {
403
+ return false ;
404
+ }
405
+ return url . protocol === 'http:' || url . protocol === 'https:' ;
406
+ } ;
407
+ const url = `${ Parse . serverURL . replace ( / \/ $ / , '' ) } /health` ;
408
+ if ( ! isValidHttpUrl ( url ) ) {
409
+ console . warn (
410
+ `\nWARNING, Unable to connect to '${ Parse . serverURL } ' as the URL is invalid.` +
411
+ ` Cloud code and push notifications may be unavailable!\n`
412
+ ) ;
413
+ return ;
414
+ }
398
415
const request = require ( './request' ) ;
399
- request ( { url : Parse . serverURL . replace ( / \/ $ / , '' ) + '/health' } )
400
- . catch ( response => response )
401
- . then ( response => {
402
- const json = response . data || null ;
403
- if ( response . status !== 200 || ! json || ( json && json . status !== 'ok' ) ) {
404
- /* eslint-disable no-console */
405
- console . warn (
406
- `\nWARNING, Unable to connect to '${ Parse . serverURL } '.` +
407
- ` Cloud code and push notifications may be unavailable!\n`
408
- ) ;
409
- /* eslint-enable no-console */
410
- if ( callback ) {
411
- callback ( false ) ;
412
- }
413
- } else {
414
- if ( callback ) {
415
- callback ( true ) ;
416
- }
417
- }
418
- } ) ;
416
+ const response = await request ( { url } ) . catch ( response => response ) ;
417
+ const json = response . data || null ;
418
+ console . log ( response . status , { json } ) ;
419
+ const retry = response . headers [ 'retry-after' ] ;
420
+ if ( retry ) {
421
+ await new Promise ( resolve => setTimeout ( resolve , retry * 1000 ) ) ;
422
+ return this . verifyServerUrl ( ) ;
423
+ }
424
+ if ( response . status !== 200 || json ?. status !== 'ok' ) {
425
+ /* eslint-disable no-console */
426
+ console . warn (
427
+ `\nWARNING, Unable to connect to '${ Parse . serverURL } '.` +
428
+ ` Cloud code and push notifications may be unavailable!\n`
429
+ ) ;
430
+ /* eslint-enable no-console */
431
+ return ;
432
+ }
433
+ return true ;
419
434
}
420
435
}
421
436
}
0 commit comments