Skip to content

Commit b76bbf4

Browse files
committed
Revert "tmp"
This reverts commit e85245a.
1 parent f719294 commit b76bbf4

File tree

6 files changed

+81
-97
lines changed

6 files changed

+81
-97
lines changed

src/Config.js

Lines changed: 46 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ function removeTrailingSlash(str) {
3232
return str;
3333
}
3434

35+
const asyncKeys = ['publicServerURL'];
3536
export class Config {
3637
static get(applicationId: string, mount: string) {
3738
const cacheInfo = AppCache.get(applicationId);
@@ -56,16 +57,33 @@ export class Config {
5657
return config;
5758
}
5859

59-
async getPublicServerURL() {
60-
if (typeof this.publicServerURL === 'function') {
61-
return await this.publicServerURL();
60+
async loadKeys() {
61+
const asyncKeys = ['publicServerURL'];
62+
63+
await Promise.all(
64+
asyncKeys.map(async key => {
65+
if (typeof this[`_${key}`] === 'function') {
66+
this[key] = await this[`_${key}`]();
67+
}
68+
})
69+
);
70+
71+
AppCache.put(this.appId, this);
72+
}
73+
74+
static transformConfiguration(serverConfiguration) {
75+
for (const key of Object.keys(serverConfiguration)) {
76+
if (asyncKeys.includes(key) && typeof serverConfiguration[key] === 'function') {
77+
serverConfiguration[`_${key}`] = serverConfiguration[key];
78+
delete serverConfiguration[key];
79+
}
6280
}
63-
return this.publicServerURL;
6481
}
6582

6683
static put(serverConfiguration) {
6784
Config.validateOptions(serverConfiguration);
6885
Config.validateControllers(serverConfiguration);
86+
Config.transformConfiguration(serverConfiguration);
6987
AppCache.put(serverConfiguration.appId, serverConfiguration);
7088
Config.setupPasswordValidator(serverConfiguration.passwordPolicy);
7189
return serverConfiguration;
@@ -456,7 +474,7 @@ export class Config {
456474
if (typeof appName !== 'string') {
457475
throw 'An app name is required for e-mail verification and password resets.';
458476
}
459-
if (!publicServerURL || (typeof publicServerURL !== 'string' && typeof publicServerURL !== 'function')) {
477+
if (typeof publicServerURL !== 'string') {
460478
throw 'A public server url is required for e-mail verification and password resets.';
461479
}
462480
if (emailVerifyTokenValidityDuration) {
@@ -528,7 +546,11 @@ export class Config {
528546
}
529547

530548
get mount() {
531-
return this._mount;
549+
var mount = this._mount;
550+
if (this.publicServerURL) {
551+
mount = this.publicServerURL;
552+
}
553+
return mount;
532554
}
533555

534556
set mount(newValue) {
@@ -692,64 +714,55 @@ export class Config {
692714
}
693715
}
694716

695-
async invalidLinkURL() {
696-
const publicServerURL = await this.getPublicServerURL();
697-
return this.customPages.invalidLink || `${publicServerURL}/apps/invalid_link.html`;
717+
get invalidLinkURL() {
718+
return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
698719
}
699720

700-
async invalidVerificationLinkURL() {
701-
const publicServerURL = await this.getPublicServerURL();
721+
get invalidVerificationLinkURL() {
702722
return (
703723
this.customPages.invalidVerificationLink ||
704-
`${publicServerURL}/apps/invalid_verification_link.html`
724+
`${this.publicServerURL}/apps/invalid_verification_link.html`
705725
);
706726
}
707727

708-
async linkSendSuccessURL() {
709-
const publicServerURL = await this.getPublicServerURL();
728+
get linkSendSuccessURL() {
710729
return (
711-
this.customPages.linkSendSuccess || `${publicServerURL}/apps/link_send_success.html`
730+
this.customPages.linkSendSuccess || `${this.publicServerURL}/apps/link_send_success.html`
712731
);
713732
}
714733

715-
async linkSendFailURL() {
716-
const publicServerURL = await this.getPublicServerURL();
717-
return this.customPages.linkSendFail || `${publicServerURL}/apps/link_send_fail.html`;
734+
get linkSendFailURL() {
735+
return this.customPages.linkSendFail || `${this.publicServerURL}/apps/link_send_fail.html`;
718736
}
719737

720-
async verifyEmailSuccessURL() {
721-
const publicServerURL = await this.getPublicServerURL();
738+
get verifyEmailSuccessURL() {
722739
return (
723740
this.customPages.verifyEmailSuccess ||
724-
`${publicServerURL}/apps/verify_email_success.html`
741+
`${this.publicServerURL}/apps/verify_email_success.html`
725742
);
726743
}
727744

728-
async choosePasswordURL() {
729-
const publicServerURL = await this.getPublicServerURL();
730-
return this.customPages.choosePassword || `${publicServerURL}/apps/choose_password`;
745+
get choosePasswordURL() {
746+
return this.customPages.choosePassword || `${this.publicServerURL}/apps/choose_password`;
731747
}
732748

733-
async requestResetPasswordURL() {
734-
const publicServerURL = await this.getPublicServerURL();
735-
return `${publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/request_password_reset`;
749+
get requestResetPasswordURL() {
750+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/request_password_reset`;
736751
}
737752

738-
async passwordResetSuccessURL() {
739-
const publicServerURL = await this.getPublicServerURL();
753+
get passwordResetSuccessURL() {
740754
return (
741755
this.customPages.passwordResetSuccess ||
742-
`${publicServerURL}/apps/password_reset_success.html`
756+
`${this.publicServerURL}/apps/password_reset_success.html`
743757
);
744758
}
745759

746760
get parseFrameURL() {
747761
return this.customPages.parseFrameURL;
748762
}
749763

750-
async verifyEmailURL() {
751-
const publicServerURL = await this.getPublicServerURL();
752-
return `${publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/verify_email`;
764+
get verifyEmailURL() {
765+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/verify_email`;
753766
}
754767

755768
async loadMasterKey() {

src/Controllers/UserController.js

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,7 @@ export class UserController extends AdaptableController {
171171
if (!shouldSendEmail) {
172172
return;
173173
}
174-
const verifyEmailURL = await this.config.verifyEmailURL();
175-
const link = await buildEmailLink(verifyEmailURL, token, this.config);
174+
const link = buildEmailLink(this.config.verifyEmailURL, token, this.config);
176175
const options = {
177176
appName: this.config.appName,
178177
link: link,
@@ -283,8 +282,7 @@ export class UserController extends AdaptableController {
283282
user = await this.setPasswordResetToken(email);
284283
}
285284
const token = encodeURIComponent(user._perishable_token);
286-
const requestResetPasswordURL = await this.config.requestResetPasswordURL();
287-
const link = await buildEmailLink(requestResetPasswordURL, token, this.config);
285+
const link = buildEmailLink(this.config.requestResetPasswordURL, token, this.config);
288286
const options = {
289287
appName: this.config.appName,
290288
link: link,
@@ -363,11 +361,10 @@ function updateUserPassword(user, password, config) {
363361
.then(() => user);
364362
}
365363

366-
async function buildEmailLink(destination, token, config) {
364+
function buildEmailLink(destination, token, config) {
367365
token = `token=${token}`;
368366
if (config.parseFrameURL) {
369-
const publicServerURL = await config.getPublicServerURL();
370-
const destinationWithoutHost = destination.replace(publicServerURL, '');
367+
const destinationWithoutHost = destination.replace(config.publicServerURL, '');
371368

372369
return `${config.parseFrameURL}?link=${encodeURIComponent(destinationWithoutHost)}&${token}`;
373370
} else {

src/Routers/PagesRouter.js

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,14 @@ export class PagesRouter extends PromiseRouter {
130130
);
131131
}
132132

133-
async passwordReset(req) {
133+
passwordReset(req) {
134134
const config = req.config;
135-
const publicServerURL = await config.getPublicServerURL();
136135
const params = {
137136
[pageParams.appId]: req.params.appId,
138137
[pageParams.appName]: config.appName,
139138
[pageParams.token]: req.query.token,
140139
[pageParams.username]: req.query.username,
141-
[pageParams.publicServerUrl]: publicServerURL,
140+
[pageParams.publicServerUrl]: config.publicServerURL,
142141
};
143142
return this.goToPage(req, pages.passwordReset, params);
144143
}
@@ -256,7 +255,7 @@ export class PagesRouter extends PromiseRouter {
256255
* - POST request -> redirect response (PRG pattern)
257256
* @returns {Promise<Object>} The PromiseRouter response.
258257
*/
259-
async goToPage(req, page, params = {}, responseType) {
258+
goToPage(req, page, params = {}, responseType) {
260259
const config = req.config;
261260

262261
// Determine redirect either by force, response setting or request method
@@ -267,7 +266,7 @@ export class PagesRouter extends PromiseRouter {
267266
: req.method == 'POST';
268267

269268
// Include default parameters
270-
const defaultParams = await this.getDefaultParams(config);
269+
const defaultParams = this.getDefaultParams(config);
271270
if (Object.values(defaultParams).includes(undefined)) {
272271
return this.notFound();
273272
}
@@ -282,8 +281,7 @@ export class PagesRouter extends PromiseRouter {
282281
// Compose paths and URLs
283282
const defaultFile = page.defaultFile;
284283
const defaultPath = this.defaultPagePath(defaultFile);
285-
const publicServerURL = await config.getPublicServerURL();
286-
const defaultUrl = this.composePageUrl(defaultFile, publicServerURL);
284+
const defaultUrl = this.composePageUrl(defaultFile, config.publicServerURL);
287285

288286
// If custom URL is set redirect to it without localization
289287
const customUrl = config.pages.customUrls[page.id];
@@ -302,7 +300,7 @@ export class PagesRouter extends PromiseRouter {
302300
return Utils.getLocalizedPath(defaultPath, locale).then(({ path, subdir }) =>
303301
redirect
304302
? this.redirectResponse(
305-
this.composePageUrl(defaultFile, publicServerURL, subdir),
303+
this.composePageUrl(defaultFile, config.publicServerURL, subdir),
306304
params
307305
)
308306
: this.pageResponse(path, params, placeholders)
@@ -531,16 +529,14 @@ export class PagesRouter extends PromiseRouter {
531529
* @param {Object} config The Parse Server configuration.
532530
* @returns {Object} The default parameters.
533531
*/
534-
async getDefaultParams(config) {
535-
if (!config) {
536-
return {};
537-
}
538-
const publicServerURL = await config.getPublicServerURL();
539-
return {
540-
[pageParams.appId]: config.appId,
541-
[pageParams.appName]: config.appName,
542-
[pageParams.publicServerUrl]: publicServerURL,
543-
};
532+
getDefaultParams(config) {
533+
return config
534+
? {
535+
[pageParams.appId]: config.appId,
536+
[pageParams.appName]: config.appName,
537+
[pageParams.publicServerUrl]: config.publicServerURL,
538+
}
539+
: {};
544540
}
545541

546542
/**

src/Routers/PublicAPIRouter.js

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,26 @@ export class PublicAPIRouter extends PromiseRouter {
8888
);
8989
}
9090

91-
async changePassword(req) {
92-
const config = Config.get(req.query.id);
93-
94-
if (!config) {
95-
this.invalidRequest();
96-
}
97-
98-
if (!config.publicServerURL) {
99-
return {
100-
status: 404,
101-
text: 'Not found.',
102-
};
103-
}
91+
changePassword(req) {
92+
return new Promise((resolve, reject) => {
93+
const config = Config.get(req.query.id);
10494

105-
const publicServerURL = await config.getPublicServerURL();
95+
if (!config) {
96+
this.invalidRequest();
97+
}
10698

107-
// Should we keep the file in memory or leave like that?
108-
return new Promise((resolve, reject) => {
99+
if (!config.publicServerURL) {
100+
return resolve({
101+
status: 404,
102+
text: 'Not found.',
103+
});
104+
}
105+
// Should we keep the file in memory or leave like that?
109106
fs.readFile(path.resolve(views, 'choose_password'), 'utf-8', (err, data) => {
110107
if (err) {
111108
return reject(err);
112109
}
113-
data = data.replace('PARSE_SERVER_URL', `'${publicServerURL}'`);
110+
data = data.replace('PARSE_SERVER_URL', `'${config.publicServerURL}'`);
114111
resolve({
115112
text: data,
116113
});

src/batch.js

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ function makeBatchRoutingPathFunction(originalUrl, serverURL, publicServerURL) {
6363

6464
// Returns a promise for a {response} object.
6565
// TODO: pass along auth correctly
66-
async function handleBatch(router, req) {
66+
function handleBatch(router, req) {
6767
if (!Array.isArray(req.body?.requests)) {
6868
throw new Parse.Error(Parse.Error.INVALID_JSON, 'requests must be an array');
6969
}
@@ -77,11 +77,10 @@ async function handleBatch(router, req) {
7777
throw 'internal routing problem - expected url to end with batch';
7878
}
7979

80-
const publicServerURL = await req.config.getPublicServerURL();
8180
const makeRoutablePath = makeBatchRoutingPathFunction(
8281
req.originalUrl,
8382
req.config.serverURL,
84-
publicServerURL
83+
req.config.publicServerURL
8584
);
8685

8786
const batch = transactionRetries => {

src/middlewares.js

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -213,25 +213,7 @@ export async function handleParseHeaders(req, res, next) {
213213
});
214214
return;
215215
}
216-
217-
// Execute publicServerURL function if it's a function
218-
if (typeof config.publicServerURL === 'function') {
219-
// Store the function for next request and resolve it for this request
220-
const urlFunction = config.publicServerURL;
221-
const resolvedURL = await urlFunction();
222-
config.publicServerURL = resolvedURL;
223-
// Update the cached config with resolved value
224-
const cachedConfig = AppCache.get(info.appId);
225-
cachedConfig.publicServerURL = resolvedURL;
226-
// But keep the function for next time
227-
cachedConfig._publicServerURLFunction = urlFunction;
228-
} else if (config._publicServerURLFunction) {
229-
// Function was previously stored, execute it again
230-
const resolvedURL = await config._publicServerURLFunction();
231-
config.publicServerURL = resolvedURL;
232-
const cachedConfig = AppCache.get(info.appId);
233-
cachedConfig.publicServerURL = resolvedURL;
234-
}
216+
await config.loadKeys();
235217

236218
info.app = AppCache.get(info.appId);
237219
req.config = config;

0 commit comments

Comments
 (0)