Skip to content

Commit e85245a

Browse files
committed
tmp
1 parent 44843e3 commit e85245a

File tree

6 files changed

+97
-81
lines changed

6 files changed

+97
-81
lines changed

src/Config.js

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

35-
const asyncKeys = ['publicServerURL'];
3635
export class Config {
3736
static get(applicationId: string, mount: string) {
3837
const cacheInfo = AppCache.get(applicationId);
@@ -57,33 +56,16 @@ export class Config {
5756
return config;
5857
}
5958

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-
}
59+
async getPublicServerURL() {
60+
if (typeof this.publicServerURL === 'function') {
61+
return await this.publicServerURL();
8062
}
63+
return this.publicServerURL;
8164
}
8265

8366
static put(serverConfiguration) {
8467
Config.validateOptions(serverConfiguration);
8568
Config.validateControllers(serverConfiguration);
86-
Config.transformConfiguration(serverConfiguration);
8769
AppCache.put(serverConfiguration.appId, serverConfiguration);
8870
Config.setupPasswordValidator(serverConfiguration.passwordPolicy);
8971
return serverConfiguration;
@@ -474,7 +456,7 @@ export class Config {
474456
if (typeof appName !== 'string') {
475457
throw 'An app name is required for e-mail verification and password resets.';
476458
}
477-
if (typeof publicServerURL !== 'string') {
459+
if (!publicServerURL || (typeof publicServerURL !== 'string' && typeof publicServerURL !== 'function')) {
478460
throw 'A public server url is required for e-mail verification and password resets.';
479461
}
480462
if (emailVerifyTokenValidityDuration) {
@@ -546,11 +528,7 @@ export class Config {
546528
}
547529

548530
get mount() {
549-
var mount = this._mount;
550-
if (this.publicServerURL) {
551-
mount = this.publicServerURL;
552-
}
553-
return mount;
531+
return this._mount;
554532
}
555533

556534
set mount(newValue) {
@@ -714,55 +692,64 @@ export class Config {
714692
}
715693
}
716694

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

721-
get invalidVerificationLinkURL() {
700+
async invalidVerificationLinkURL() {
701+
const publicServerURL = await this.getPublicServerURL();
722702
return (
723703
this.customPages.invalidVerificationLink ||
724-
`${this.publicServerURL}/apps/invalid_verification_link.html`
704+
`${publicServerURL}/apps/invalid_verification_link.html`
725705
);
726706
}
727707

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

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

738-
get verifyEmailSuccessURL() {
720+
async verifyEmailSuccessURL() {
721+
const publicServerURL = await this.getPublicServerURL();
739722
return (
740723
this.customPages.verifyEmailSuccess ||
741-
`${this.publicServerURL}/apps/verify_email_success.html`
724+
`${publicServerURL}/apps/verify_email_success.html`
742725
);
743726
}
744727

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

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

753-
get passwordResetSuccessURL() {
738+
async passwordResetSuccessURL() {
739+
const publicServerURL = await this.getPublicServerURL();
754740
return (
755741
this.customPages.passwordResetSuccess ||
756-
`${this.publicServerURL}/apps/password_reset_success.html`
742+
`${publicServerURL}/apps/password_reset_success.html`
757743
);
758744
}
759745

760746
get parseFrameURL() {
761747
return this.customPages.parseFrameURL;
762748
}
763749

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

768755
async loadMasterKey() {

src/Controllers/UserController.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ export class UserController extends AdaptableController {
171171
if (!shouldSendEmail) {
172172
return;
173173
}
174-
const link = buildEmailLink(this.config.verifyEmailURL, token, this.config);
174+
const verifyEmailURL = await this.config.verifyEmailURL();
175+
const link = await buildEmailLink(verifyEmailURL, token, this.config);
175176
const options = {
176177
appName: this.config.appName,
177178
link: link,
@@ -282,7 +283,8 @@ export class UserController extends AdaptableController {
282283
user = await this.setPasswordResetToken(email);
283284
}
284285
const token = encodeURIComponent(user._perishable_token);
285-
const link = buildEmailLink(this.config.requestResetPasswordURL, token, this.config);
286+
const requestResetPasswordURL = await this.config.requestResetPasswordURL();
287+
const link = await buildEmailLink(requestResetPasswordURL, token, this.config);
286288
const options = {
287289
appName: this.config.appName,
288290
link: link,
@@ -361,10 +363,11 @@ function updateUserPassword(user, password, config) {
361363
.then(() => user);
362364
}
363365

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

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

src/Routers/PagesRouter.js

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

133-
passwordReset(req) {
133+
async passwordReset(req) {
134134
const config = req.config;
135+
const publicServerURL = await config.getPublicServerURL();
135136
const params = {
136137
[pageParams.appId]: req.params.appId,
137138
[pageParams.appName]: config.appName,
138139
[pageParams.token]: req.query.token,
139140
[pageParams.username]: req.query.username,
140-
[pageParams.publicServerUrl]: config.publicServerURL,
141+
[pageParams.publicServerUrl]: publicServerURL,
141142
};
142143
return this.goToPage(req, pages.passwordReset, params);
143144
}
@@ -255,7 +256,7 @@ export class PagesRouter extends PromiseRouter {
255256
* - POST request -> redirect response (PRG pattern)
256257
* @returns {Promise<Object>} The PromiseRouter response.
257258
*/
258-
goToPage(req, page, params = {}, responseType) {
259+
async goToPage(req, page, params = {}, responseType) {
259260
const config = req.config;
260261

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

268269
// Include default parameters
269-
const defaultParams = this.getDefaultParams(config);
270+
const defaultParams = await this.getDefaultParams(config);
270271
if (Object.values(defaultParams).includes(undefined)) {
271272
return this.notFound();
272273
}
@@ -281,7 +282,8 @@ export class PagesRouter extends PromiseRouter {
281282
// Compose paths and URLs
282283
const defaultFile = page.defaultFile;
283284
const defaultPath = this.defaultPagePath(defaultFile);
284-
const defaultUrl = this.composePageUrl(defaultFile, config.publicServerURL);
285+
const publicServerURL = await config.getPublicServerURL();
286+
const defaultUrl = this.composePageUrl(defaultFile, publicServerURL);
285287

286288
// If custom URL is set redirect to it without localization
287289
const customUrl = config.pages.customUrls[page.id];
@@ -300,7 +302,7 @@ export class PagesRouter extends PromiseRouter {
300302
return Utils.getLocalizedPath(defaultPath, locale).then(({ path, subdir }) =>
301303
redirect
302304
? this.redirectResponse(
303-
this.composePageUrl(defaultFile, config.publicServerURL, subdir),
305+
this.composePageUrl(defaultFile, publicServerURL, subdir),
304306
params
305307
)
306308
: this.pageResponse(path, params, placeholders)
@@ -529,14 +531,16 @@ export class PagesRouter extends PromiseRouter {
529531
* @param {Object} config The Parse Server configuration.
530532
* @returns {Object} The default parameters.
531533
*/
532-
getDefaultParams(config) {
533-
return config
534-
? {
535-
[pageParams.appId]: config.appId,
536-
[pageParams.appName]: config.appName,
537-
[pageParams.publicServerUrl]: config.publicServerURL,
538-
}
539-
: {};
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+
};
540544
}
541545

542546
/**

src/Routers/PublicAPIRouter.js

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

91-
changePassword(req) {
92-
return new Promise((resolve, reject) => {
93-
const config = Config.get(req.query.id);
91+
async changePassword(req) {
92+
const config = Config.get(req.query.id);
9493

95-
if (!config) {
96-
this.invalidRequest();
97-
}
94+
if (!config) {
95+
this.invalidRequest();
96+
}
9897

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?
98+
if (!config.publicServerURL) {
99+
return {
100+
status: 404,
101+
text: 'Not found.',
102+
};
103+
}
104+
105+
const publicServerURL = await config.getPublicServerURL();
106+
107+
// Should we keep the file in memory or leave like that?
108+
return new Promise((resolve, reject) => {
106109
fs.readFile(path.resolve(views, 'choose_password'), 'utf-8', (err, data) => {
107110
if (err) {
108111
return reject(err);
109112
}
110-
data = data.replace('PARSE_SERVER_URL', `'${config.publicServerURL}'`);
113+
data = data.replace('PARSE_SERVER_URL', `'${publicServerURL}'`);
111114
resolve({
112115
text: data,
113116
});

src/batch.js

Lines changed: 3 additions & 2 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-
function handleBatch(router, req) {
66+
async 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,10 +77,11 @@ function handleBatch(router, req) {
7777
throw 'internal routing problem - expected url to end with batch';
7878
}
7979

80+
const publicServerURL = await req.config.getPublicServerURL();
8081
const makeRoutablePath = makeBatchRoutingPathFunction(
8182
req.originalUrl,
8283
req.config.serverURL,
83-
req.config.publicServerURL
84+
publicServerURL
8485
);
8586

8687
const batch = transactionRetries => {

src/middlewares.js

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,25 @@ export async function handleParseHeaders(req, res, next) {
213213
});
214214
return;
215215
}
216-
await config.loadKeys();
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+
}
217235

218236
info.app = AppCache.get(info.appId);
219237
req.config = config;

0 commit comments

Comments
 (0)