Merge branch 'feature/db_session_store' into client_vite

This commit is contained in:
Elian Doran
2025-05-16 23:57:32 +03:00
8 changed files with 134 additions and 225 deletions

View File

@@ -0,0 +1,5 @@
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
data TEXT,
expires INTEGER
);

View File

@@ -187,3 +187,9 @@ CREATE TABLE IF NOT EXISTS "embedding_providers" (
"dateModified" TEXT NOT NULL,
"utcDateModified" TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS sessions (
id TEXT PRIMARY KEY,
data TEXT,
expires INTEGER
);

View File

@@ -1,10 +1,55 @@
import session from "express-session";
import sessionFileStore from "session-file-store";
import sql from "../services/sql.js";
import session, { Store } from "express-session";
import sessionSecret from "../services/session_secret.js";
import dataDir from "../services/data_dir.js";
import config from "../services/config.js";
import log from "../services/log.js";
const FileStore = sessionFileStore(session);
class SQLiteSessionStore extends Store {
get(sid: string, callback: (err: any, session?: session.SessionData | null) => void): void {
try {
const data = sql.getValue<string>(/*sql*/`SELECT data FROM sessions WHERE id = ?`, sid);
let session = null;
if (data) {
session = JSON.parse(data);
}
return callback(null, session);
} catch (e: unknown) {
log.error(e);
return callback(e);
}
}
set(id: string, session: session.SessionData, callback?: (err?: any) => void): void {
try {
const expires = session.cookie?.expires
? new Date(session.cookie.expires).getTime()
: Date.now() + 3600000; // fallback to 1 hour
const data = JSON.stringify(session);
sql.upsert("sessions", "id", {
id,
expires,
data
});
callback?.();
} catch (e) {
log.error(e);
return callback?.(e);
}
}
destroy(sid: string, callback?: (err?: any) => void): void {
try {
sql.execute(/*sql*/`DELETE FROM sessions WHERE id = ?`, sid);
callback?.();
} catch (e) {
log.error(e);
callback?.(e);
}
}
}
const sessionParser = session({
secret: sessionSecret,
@@ -16,10 +61,14 @@ const sessionParser = session({
maxAge: config.Session.cookieMaxAge * 1000 // needs value in milliseconds
},
name: "trilium.sid",
store: new FileStore({
ttl: config.Session.cookieMaxAge,
path: `${dataDir.TRILIUM_DATA_DIR}/sessions`
})
store: new SQLiteSessionStore()
});
setInterval(() => {
// Clean up expired sesions.
const now = Date.now();
const result = sql.execute(/*sql*/`DELETE FROM sessions WHERE expires < ?`, now);
console.log("Cleaning up expired sessions: ", result.changes);
}, 60 * 60 * 1000);
export default sessionParser;

View File

@@ -3,7 +3,7 @@ import build from "./build.js";
import packageJson from "../../package.json" with { type: "json" };
import dataDir from "./data_dir.js";
const APP_DB_VERSION = 230;
const APP_DB_VERSION = 231;
const SYNC_VERSION = 35;
const CLIPPER_PROTOCOL_VERSION = "1.0";

View File

@@ -69,7 +69,7 @@ function info(message: string | Error) {
log(message);
}
function error(message: string | Error) {
function error(message: string | Error | unknown) {
log(`ERROR: ${message}`);
}