Skip to content

Commit 729d132

Browse files
committed
move csp
1 parent 5aa4ad7 commit 729d132

File tree

2 files changed

+33
-27
lines changed

2 files changed

+33
-27
lines changed

packages/compass/src/app/application.tsx

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import { createElectronFileInputBackend } from '@mongodb-js/compass-components';
2828
import { CompassRendererConnectionStorage } from '@mongodb-js/connection-storage/renderer';
2929
import type { SettingsTabId } from '@mongodb-js/compass-settings';
3030
import type { AutoConnectPreferences } from '../main/auto-connect';
31-
import { injectCSP } from './utils/csp';
31+
3232
const { log, mongoLogId } = createLogger('COMPASS-APP');
3333
const track = createIpcTrack();
3434

@@ -206,21 +206,6 @@ class Application {
206206
document.querySelector('#loading-placeholder')?.remove();
207207
}
208208

209-
private async updateAppVersion() {
210-
const { lastKnownVersion, highestInstalledVersion } =
211-
defaultPreferencesInstance.getPreferences();
212-
this.previousVersion = lastKnownVersion || DEFAULT_APP_VERSION;
213-
this.highestInstalledVersion =
214-
semver.sort([
215-
highestInstalledVersion || DEFAULT_APP_VERSION,
216-
this.version,
217-
])?.[1] ?? this.version;
218-
await defaultPreferencesInstance.savePreferences({
219-
lastKnownVersion: this.version,
220-
highestInstalledVersion: this.highestInstalledVersion,
221-
});
222-
}
223-
224209
private setupDataRefreshListener() {
225210
ipcRenderer?.on('app:refresh-data', () =>
226211
globalAppRegistry.emit('refresh-data')
@@ -454,20 +439,15 @@ class Application {
454439
// unhandled errors are properly logged and reported
455440
this.setupGlobalErrorHandling();
456441

457-
// Inject CSP first to prevent any CSP violations.
458-
injectCSP();
459-
460-
this.allowDevFeatureFlagsFromDevTools();
461-
this.preventDefaultBrowserBehaviorForDragAndDrop();
462-
463442
// Setup development environment
464443
this.enableDevelopmentDebug();
444+
this.allowDevFeatureFlagsFromDevTools();
445+
465446
this.patchNODE4281();
466447
this.setupWebVitals();
467448

468449
// Initialize preferences and version
469-
await defaultPreferencesInstance.refreshPreferences();
470-
await this.updateAppVersion();
450+
await this.initializePreferencesAndVersion();
471451

472452
void this.setupIntercomAndLogError();
473453

@@ -476,6 +456,7 @@ class Application {
476456
this.setupAutoUpdateListeners();
477457
this.setupConnectInNewWindowListeners();
478458
this.setupZoomControls();
459+
this.preventDefaultBrowserBehaviorForDragAndDrop();
479460

480461
// Render the application
481462
await this.render();
@@ -488,6 +469,27 @@ class Application {
488469
this.logVersionInfo();
489470
this.showUpdateToastIfNeeded();
490471
}
472+
473+
private async initializePreferencesAndVersion() {
474+
await defaultPreferencesInstance.refreshPreferences();
475+
476+
// This code updates the last known version and highest installed version in preferences after
477+
// the first installation, or a potential update or downgrade.
478+
// This is useful so that we can track the version history of the application, and
479+
// handle auto-updates differently in case of downgrades.
480+
const { lastKnownVersion, highestInstalledVersion } =
481+
defaultPreferencesInstance.getPreferences();
482+
this.previousVersion = lastKnownVersion || DEFAULT_APP_VERSION;
483+
this.highestInstalledVersion =
484+
semver.sort([
485+
highestInstalledVersion || DEFAULT_APP_VERSION,
486+
this.version,
487+
])?.[1] ?? this.version;
488+
await defaultPreferencesInstance.savePreferences({
489+
lastKnownVersion: this.version,
490+
highestInstalledVersion: this.highestInstalledVersion,
491+
});
492+
}
491493
}
492494

493495
export const app = Application.getInstance();

packages/compass/src/app/index.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,19 @@
1919
* Rule of thumb: If the code isn't required for other modules to function
2020
* correctly during import, it probably belongs in application.tsx.
2121
*/
22-
import { setupHadronDistribution } from '../setup-hadron-distribution';
23-
import dns from 'dns';
24-
import EventEmitter from 'events';
22+
23+
// Inject CSP to prevent any CSP violations.
24+
import { injectCSP } from './utils/csp';
25+
injectCSP();
2526

2627
// Setup paths and environment variables for electron globals
28+
import { setupHadronDistribution } from '../setup-hadron-distribution';
2729
setupHadronDistribution();
2830

2931
// DNS Configuration
3032
// Ensures IPv4 is preferred over IPv6 to avoid potential connection issues
3133
// See: https://github.com/nodejs/node/issues/40537
34+
import dns from 'dns';
3235
dns.setDefaultResultOrder('ipv4first');
3336

3437
// Configure NODE_OPTIONS to ensure sub-processes (like the shell) also prefer IPv4
@@ -40,6 +43,7 @@ if (!process.env.NODE_OPTIONS.includes('--dns-result-order')) {
4043
// Event Emitter Configuration
4144
// Increases the default maximum number of listeners to prevent
4245
// potential memory leaks in development mode
46+
import EventEmitter from 'events';
4347
EventEmitter.defaultMaxListeners = 100;
4448

4549
// ----------------------------------------------------------------------------

0 commit comments

Comments
 (0)