Skip to content

Commit

Permalink
refactor: configureCORS
Browse files Browse the repository at this point in the history
  • Loading branch information
sampaiodiego committed Dec 17, 2024
1 parent d7160e1 commit 63fef6e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 11 deletions.
18 changes: 7 additions & 11 deletions apps/meteor/app/cors/server/cors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,17 @@ type NextFunction = (err?: any) => void;

const logger = new Logger('CORS');

let templatePromise: Promise<void> | void;

declare module 'meteor/webapp' {
// eslint-disable-next-line @typescript-eslint/no-namespace
namespace WebApp {
function setInlineScriptsAllowed(allowed: boolean): Promise<void>;
}
}

settings.watch<boolean>(
'Enable_CSP',
Meteor.bindEnvironment(async (enabled) => {
templatePromise = WebAppInternals.setInlineScriptsAllowed(!enabled);
}),
);
let templatePromise: Promise<void> | void;
export async function setInlineScriptsAllowed(allowed: boolean): Promise<void> {
templatePromise = WebApp.setInlineScriptsAllowed(!allowed);
}

WebApp.rawConnectHandlers.use(async (_req: http.IncomingMessage, res: http.ServerResponse, next: NextFunction) => {
if (templatePromise) {
Expand Down Expand Up @@ -104,9 +100,9 @@ declare module 'meteor/webapp' {
}

let cachingVersion = '';
settings.watch<string>('Troubleshoot_Force_Caching_Version', (value) => {
cachingVersion = String(value).trim();
});
export function setCachingVersion(value: string): void {
cachingVersion = value.trim();
}

// @ts-expect-error - accessing internal property of webapp
WebAppInternals.staticFilesMiddleware = function (
Expand Down
2 changes: 2 additions & 0 deletions apps/meteor/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { registerServices } from './services/startup';
import { startup } from './startup';
import { configureBoilerplate } from './startup/configureBoilerplate';
import { configureCDN } from './startup/configureCDN';
import { configureCORS } from './startup/configureCORS';
import { configureDirectReply } from './startup/configureDirectReply';
import { configureIRC } from './startup/configureIRC';
import { configureFederation } from './startup/settings';
Expand Down Expand Up @@ -42,6 +43,7 @@ await Promise.all([configureLoginServices(), startFederationService()]);

await Promise.all([
configureAssets(settings),
configureCORS(settings),
configureCDN(settings),
configurePushNotifications(settings),
configureBoilerplate(settings),
Expand Down
11 changes: 11 additions & 0 deletions apps/meteor/server/startup/configureCORS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { setCachingVersion, setInlineScriptsAllowed } from '../../app/cors/server/cors';
import type { ICachedSettings } from '../../app/settings/server/CachedSettings';

export async function configureCORS(settings: ICachedSettings): Promise<void> {
settings.watch<boolean>('Enable_CSP', async (enabled) => {
await setInlineScriptsAllowed(!enabled);
});
settings.watch<string>('Troubleshoot_Force_Caching_Version', (value) => {
setCachingVersion(value);
});
}

0 comments on commit 63fef6e

Please sign in to comment.