Skip to content

Commit c17632c

Browse files
committed
fix
1 parent e85245a commit c17632c

File tree

4 files changed

+62
-59
lines changed

4 files changed

+62
-59
lines changed

spec/index.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ describe('server', () => {
686686
.catch(done.fail);
687687
});
688688

689-
fdescribe('publicServerURL', () => {
689+
describe('publicServerURL', () => {
690690
it('should load publicServerURL', async () => {
691691
await reconfigureServer({
692692
publicServerURL: () => 'https://example.com/1',

src/Config.js

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

35+
// List of config keys that can be async (functions or promises)
36+
const asyncKeys = ['publicServerURL'];
37+
38+
/**
39+
* Helper function to resolve an async config value.
40+
* If the value is a function, it executes it and returns the result.
41+
* If the value is a promise, it awaits it and returns the result.
42+
* Otherwise, it returns the raw value.
43+
*/
44+
async function resolveAsyncValue(value) {
45+
if (typeof value === 'function') {
46+
return await value();
47+
}
48+
if (value && typeof value.then === 'function') {
49+
return await value;
50+
}
51+
return value;
52+
}
53+
3554
export class Config {
3655
static get(applicationId: string, mount: string) {
3756
const cacheInfo = AppCache.get(applicationId);
@@ -53,14 +72,28 @@ export class Config {
5372
config
5473
);
5574
config.version = version;
75+
76+
// Transform async keys: store original in _[key]
77+
asyncKeys.forEach(key => {
78+
if (config[key] !== undefined && (typeof config[key] === 'function' || (config[key] && typeof config[key].then === 'function'))) {
79+
config[`_${key}`] = config[key];
80+
// Will be resolved in middleware
81+
delete config[key];
82+
}
83+
});
84+
5685
return config;
5786
}
5887

59-
async getPublicServerURL() {
60-
if (typeof this.publicServerURL === 'function') {
61-
return await this.publicServerURL();
62-
}
63-
return this.publicServerURL;
88+
async loadKeys() {
89+
await Promise.all(
90+
asyncKeys.map(async key => {
91+
if (this[`_${key}`] !== undefined) {
92+
this[key] = await resolveAsyncValue(this[`_${key}`]);
93+
}
94+
})
95+
);
96+
AppCache.put(this.appId, this);
6497
}
6598

6699
static put(serverConfiguration) {
@@ -692,64 +725,55 @@ export class Config {
692725
}
693726
}
694727

695-
async invalidLinkURL() {
696-
const publicServerURL = await this.getPublicServerURL();
697-
return this.customPages.invalidLink || `${publicServerURL}/apps/invalid_link.html`;
728+
get invalidLinkURL() {
729+
return this.customPages.invalidLink || `${this.publicServerURL}/apps/invalid_link.html`;
698730
}
699731

700-
async invalidVerificationLinkURL() {
701-
const publicServerURL = await this.getPublicServerURL();
732+
get invalidVerificationLinkURL() {
702733
return (
703734
this.customPages.invalidVerificationLink ||
704-
`${publicServerURL}/apps/invalid_verification_link.html`
735+
`${this.publicServerURL}/apps/invalid_verification_link.html`
705736
);
706737
}
707738

708-
async linkSendSuccessURL() {
709-
const publicServerURL = await this.getPublicServerURL();
739+
get linkSendSuccessURL() {
710740
return (
711-
this.customPages.linkSendSuccess || `${publicServerURL}/apps/link_send_success.html`
741+
this.customPages.linkSendSuccess || `${this.publicServerURL}/apps/link_send_success.html`
712742
);
713743
}
714744

715-
async linkSendFailURL() {
716-
const publicServerURL = await this.getPublicServerURL();
717-
return this.customPages.linkSendFail || `${publicServerURL}/apps/link_send_fail.html`;
745+
get linkSendFailURL() {
746+
return this.customPages.linkSendFail || `${this.publicServerURL}/apps/link_send_fail.html`;
718747
}
719748

720-
async verifyEmailSuccessURL() {
721-
const publicServerURL = await this.getPublicServerURL();
749+
get verifyEmailSuccessURL() {
722750
return (
723751
this.customPages.verifyEmailSuccess ||
724-
`${publicServerURL}/apps/verify_email_success.html`
752+
`${this.publicServerURL}/apps/verify_email_success.html`
725753
);
726754
}
727755

728-
async choosePasswordURL() {
729-
const publicServerURL = await this.getPublicServerURL();
730-
return this.customPages.choosePassword || `${publicServerURL}/apps/choose_password`;
756+
get choosePasswordURL() {
757+
return this.customPages.choosePassword || `${this.publicServerURL}/apps/choose_password`;
731758
}
732759

733-
async requestResetPasswordURL() {
734-
const publicServerURL = await this.getPublicServerURL();
735-
return `${publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/request_password_reset`;
760+
get requestResetPasswordURL() {
761+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/request_password_reset`;
736762
}
737763

738-
async passwordResetSuccessURL() {
739-
const publicServerURL = await this.getPublicServerURL();
764+
get passwordResetSuccessURL() {
740765
return (
741766
this.customPages.passwordResetSuccess ||
742-
`${publicServerURL}/apps/password_reset_success.html`
767+
`${this.publicServerURL}/apps/password_reset_success.html`
743768
);
744769
}
745770

746771
get parseFrameURL() {
747772
return this.customPages.parseFrameURL;
748773
}
749774

750-
async verifyEmailURL() {
751-
const publicServerURL = await this.getPublicServerURL();
752-
return `${publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/verify_email`;
775+
get verifyEmailURL() {
776+
return `${this.publicServerURL}/${this.pagesEndpoint}/${this.applicationId}/verify_email`;
753777
}
754778

755779
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/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)