Skip to content

Build your personal knowledge base with Trilium Notes

refactor(server): fix a few cyclic dependencies regarding protected_session

Elian Dorana8011e4755ba
diff --git a/apps/server/src/services/protected_session.ts b/apps/server/src/services/protected_session.ts
index 57a163e186..d69d318d1b 100644
--- a/apps/server/src/services/protected_session.ts
+++ b/apps/server/src/services/protected_session.ts
@@ -1,9 +1,6 @@
"use strict";
-import log from "./log.js";
import dataEncryptionService from "./encryption/data_encryption.js";
-import options from "./options.js";
-import ws from "./ws.js";
let dataKey: Buffer | null = null;
@@ -15,11 +12,11 @@ function getDataKey() {
return dataKey;
}
-function resetDataKey() {
+export function resetDataKey() {
dataKey = null;
}
-function isProtectedSessionAvailable() {
+export function isProtectedSessionAvailable() {
return !!dataKey;
}
@@ -57,15 +54,8 @@ function touchProtectedSession() {
}
}
-function checkProtectedSessionExpiration() {
- const protectedSessionTimeout = options.getOptionInt("protectedSessionTimeout");
- if (isProtectedSessionAvailable() && lastProtectedSessionOperationDate && Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
- resetDataKey();
-
- log.info("Expiring protected session");
-
- ws.reloadFrontend("leaving protected session");
- }
+export function getLastProtectedSessionOperationDate() {
+ return lastProtectedSessionOperationDate;
}
export default {
@@ -75,6 +65,5 @@ export default {
encrypt,
decrypt,
decryptString,
- touchProtectedSession,
- checkProtectedSessionExpiration
+ touchProtectedSession
};
diff --git a/apps/server/src/services/scheduler.ts b/apps/server/src/services/scheduler.ts
index 8035074427..e58ef07b42 100644
--- a/apps/server/src/services/scheduler.ts
+++ b/apps/server/src/services/scheduler.ts
@@ -4,9 +4,11 @@ import sqlInit from "./sql_init.js";
import config from "./config.js";
import log from "./log.js";
import attributeService from "../services/attributes.js";
-import protectedSessionService from "../services/protected_session.js";
import hiddenSubtreeService from "./hidden_subtree.js";
import type BNote from "../becca/entities/bnote.js";
+import options from "./options.js";
+import { getLastProtectedSessionOperationDate, isProtectedSessionAvailable, resetDataKey } from "./protected_session.js";
+import ws from "./ws.js";
function getRunAtHours(note: BNote): number[] {
try {
@@ -64,5 +66,15 @@ sqlInit.dbReady.then(() => {
);
}
- setInterval(() => protectedSessionService.checkProtectedSessionExpiration(), 30000);
+ setInterval(() => checkProtectedSessionExpiration(), 1);
});
+
+function checkProtectedSessionExpiration() {
+ const protectedSessionTimeout = options.getOptionInt("protectedSessionTimeout");
+ const lastProtectedSessionOperationDate = getLastProtectedSessionOperationDate();
+ if (isProtectedSessionAvailable() && lastProtectedSessionOperationDate && Date.now() - lastProtectedSessionOperationDate > protectedSessionTimeout * 1000) {
+ resetDataKey();
+ log.info("Expiring protected session");
+ ws.reloadFrontend("leaving protected session");
+ }
+}