Skip to content

Commit ffa4974

Browse files
authored
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.
1 parent 76c7a6f commit ffa4974

File tree

3 files changed

+53
-37
lines changed

3 files changed

+53
-37
lines changed

spec/ParseServer.spec.js

+9-16
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,20 @@ describe('Server Url Checks', () => {
2929
server.close(done);
3030
});
3131

32-
it('validate good server url', done => {
32+
it('validate good server url', async () => {
3333
Parse.serverURL = 'http://localhost:13376';
34-
ParseServer.verifyServerUrl(async result => {
35-
if (!result) {
36-
done.fail('Did not pass valid url');
37-
}
38-
await reconfigureServer();
39-
done();
40-
});
34+
const response = await ParseServer.verifyServerUrl();
35+
expect(response).toBeTrue();
4136
});
4237

43-
it('mark bad server url', done => {
38+
it('mark bad server url', async () => {
4439
spyOn(console, 'warn').and.callFake(() => {});
4540
Parse.serverURL = 'notavalidurl';
46-
ParseServer.verifyServerUrl(async result => {
47-
if (result) {
48-
done.fail('Did not mark invalid url');
49-
}
50-
await reconfigureServer();
51-
done();
52-
});
41+
const response = await ParseServer.verifyServerUrl();
42+
expect(response).not.toBeTrue();
43+
expect(console.warn).toHaveBeenCalledWith(
44+
`\nWARNING, Unable to connect to 'notavalidurl' as the URL is invalid. Cloud code and push notifications may be unavailable!\n`
45+
);
5346
});
5447

5548
xit('handleShutdown, close connection', done => {

spec/index.spec.js

+8
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,12 @@ describe('server', () => {
546546
const health = await request({
547547
url: 'http://localhost:12701/parse/health',
548548
}).catch(e => e);
549+
spyOn(console, 'warn').and.callFake(() => {});
550+
const verify = await ParseServer.default.verifyServerUrl();
551+
expect(verify).not.toBeTrue();
552+
expect(console.warn).toHaveBeenCalledWith(
553+
`\nWARNING, Unable to connect to 'http://localhost:12701/parse'. Cloud code and push notifications may be unavailable!\n`
554+
);
549555
expect(health.data.status).toBe('initialized');
550556
expect(health.status).toBe(503);
551557
await new Promise(resolve => server.close(resolve));
@@ -573,6 +579,8 @@ describe('server', () => {
573579
expect(health.data.status).toBe('starting');
574580
expect(health.status).toBe(503);
575581
expect(health.headers['retry-after']).toBe('1');
582+
const response = await ParseServer.default.verifyServerUrl();
583+
expect(response).toBeTrue();
576584
await startingPromise;
577585
await new Promise(resolve => server.close(resolve));
578586
});

src/ParseServer.js

+36-21
Original file line numberDiff line numberDiff line change
@@ -392,30 +392,45 @@ class ParseServer {
392392
return server;
393393
}
394394

395-
static verifyServerUrl(callback) {
395+
static async verifyServerUrl() {
396396
// perform a health check on the serverURL value
397397
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+
}
398415
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;
419434
}
420435
}
421436
}

0 commit comments

Comments
 (0)