feat: Adapt verifyServerUrl for new asynchronous Parse Server start-up states (#8366)

BREAKING CHANGE: The method `ParseServer.verifyServerUrl` now returns a promise instead of a callback.
This commit is contained in:
Daniel
2023-01-09 04:23:01 +11:00
committed by GitHub
parent 76c7a6fbd0
commit ffa4974158
3 changed files with 53 additions and 37 deletions

View File

@@ -392,30 +392,45 @@ class ParseServer {
return server;
}
static verifyServerUrl(callback) {
static async verifyServerUrl() {
// perform a health check on the serverURL value
if (Parse.serverURL) {
const isValidHttpUrl = string => {
let url;
try {
url = new URL(string);
} catch (_) {
return false;
}
return url.protocol === 'http:' || url.protocol === 'https:';
};
const url = `${Parse.serverURL.replace(/\/$/, '')}/health`;
if (!isValidHttpUrl(url)) {
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}' as the URL is invalid.` +
` Cloud code and push notifications may be unavailable!\n`
);
return;
}
const request = require('./request');
request({ url: Parse.serverURL.replace(/\/$/, '') + '/health' })
.catch(response => response)
.then(response => {
const json = response.data || null;
if (response.status !== 200 || !json || (json && json.status !== 'ok')) {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
if (callback) {
callback(false);
}
} else {
if (callback) {
callback(true);
}
}
});
const response = await request({ url }).catch(response => response);
const json = response.data || null;
console.log(response.status, { json });
const retry = response.headers['retry-after'];
if (retry) {
await new Promise(resolve => setTimeout(resolve, retry * 1000));
return this.verifyServerUrl();
}
if (response.status !== 200 || json?.status !== 'ok') {
/* eslint-disable no-console */
console.warn(
`\nWARNING, Unable to connect to '${Parse.serverURL}'.` +
` Cloud code and push notifications may be unavailable!\n`
);
/* eslint-enable no-console */
return;
}
return true;
}
}
}