From 8a30663d1e8b44d95f24b776d8bf0ddee88cc09b Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Thu, 18 Jul 2024 23:24:36 +0300 Subject: [PATCH] server-esm: Start with some fundamental imports --- electron.ts | 2 +- src/anonymize.ts | 2 +- src/app.ts | 26 ++++++++++++--------- src/becca/entities/abstract_becca_entity.ts | 6 +---- src/www.ts | 10 ++++---- 5 files changed, 23 insertions(+), 23 deletions(-) diff --git a/electron.ts b/electron.ts index d4e995b3a..86dee85f3 100644 --- a/electron.ts +++ b/electron.ts @@ -62,4 +62,4 @@ electron.app.on("will-quit", () => { // this is to disable electron warning spam in the dev console (local development only) process.env["ELECTRON_DISABLE_SECURITY_WARNINGS"] = "true"; -require('./src/www.js'); +await import('./src/www.js'); diff --git a/src/anonymize.ts b/src/anonymize.ts index 89f9d2d0c..15fb4dde0 100644 --- a/src/anonymize.ts +++ b/src/anonymize.ts @@ -1,6 +1,6 @@ import anonymizationService from "./services/anonymization.js"; import sqlInit from "./services/sql_init.js"; -require('./becca/entity_constructor'); +await import("./becca/entity_constructor"); sqlInit.dbReady.then(async () => { try { diff --git a/src/app.ts b/src/app.ts index e6da1bcfe..181f3aff1 100644 --- a/src/app.ts +++ b/src/app.ts @@ -6,9 +6,13 @@ import helmet from "helmet"; import compression from "compression"; import sessionParser from "./routes/session_parser.js"; import utils from "./services/utils.js"; +import assets from "./routes/assets.js"; +import routes from "./routes/routes.js"; +import custom from "./routes/custom.js"; +import error_handlers from "./routes/error_handlers.js"; -require('./services/handlers'); -require('./becca/becca_loader'); +await import('./services/handlers'); +await import('./becca/becca_loader'); const app = express(); @@ -37,24 +41,24 @@ app.use(`/robots.txt`, express.static(path.join(__dirname, 'public/robots.txt')) app.use(sessionParser); app.use(favicon(`${__dirname}/../images/app-icons/win/icon.ico`)); -require('./routes/assets').register(app); -require('./routes/routes').register(app); -require('./routes/custom').register(app); -require('./routes/error_handlers').register(app); +assets.register(app); +routes.register(app); +custom.register(app); +error_handlers.register(app); // triggers sync timer -require('./services/sync'); +await import("./services/sync"); // triggers backup timer -require('./services/backup'); +await import('./services/backup'); // trigger consistency checks timer -require('./services/consistency_checks'); +await import('./services/consistency_checks'); -require('./services/scheduler'); +await import('./services/scheduler'); if (utils.isElectron()) { - require('@electron/remote/main').initialize(); + (await import('@electron/remote/main')).initialize(); } export default app; diff --git a/src/becca/entities/abstract_becca_entity.ts b/src/becca/entities/abstract_becca_entity.ts index 67d7d6031..d72de28cb 100644 --- a/src/becca/entities/abstract_becca_entity.ts +++ b/src/becca/entities/abstract_becca_entity.ts @@ -47,11 +47,7 @@ abstract class AbstractBeccaEntity> { } protected get becca(): Becca { - if (!becca) { - becca = require('../becca'); - } - - return becca as Becca; + return becca; } protected putEntityChange(isDeleted: boolean) { diff --git a/src/www.ts b/src/www.ts index 7f0b14aa5..609551a0a 100644 --- a/src/www.ts +++ b/src/www.ts @@ -39,7 +39,7 @@ if (!semver.satisfies(process.version, ">=10.5.0")) { startTrilium(); -function startTrilium() { +async function startTrilium() { /** * The intended behavior is to detect when a second instance is running, in that case open the old instance * instead of the new one. This is complicated by the fact that it is possible to run multiple instances of Trilium @@ -54,12 +54,12 @@ function startTrilium() { * to do a complex evaluation. */ if (utils.isElectron()) { - require('electron').app.requestSingleInstanceLock(); + (await import('electron')).app.requestSingleInstanceLock(); } log.info(JSON.stringify(appInfo, null, 2)); - const cpuInfos = require('os').cpus(); + const cpuInfos = (await import('os')).cpus(); if (cpuInfos && cpuInfos[0] !== undefined) { // https://github.com/zadam/trilium/pull/3957 log.info(`CPU model: ${cpuInfos[0].model}, logical cores: ${cpuInfos.length} freq: ${cpuInfos[0].speed} Mhz`); // for perf. issues it's good to know the rough configuration } @@ -69,8 +69,8 @@ function startTrilium() { ws.init(httpServer, sessionParser as any); // TODO: Not sure why session parser is incompatible. if (utils.isElectron()) { - const electronRouting = require('./routes/electron'); - electronRouting(app); + const electronRouting = await import('./routes/electron'); + electronRouting.default(app); } }