Skip to content

Build your personal knowledge base with Trilium Notes

fixes and polish

zadam00faf758e8b2
diff --git a/src/app.js b/src/app.js
index 3ef1a7c0f5..0b6cc4dd21 100644
--- a/src/app.js
+++ b/src/app.js
@@ -28,17 +28,6 @@ app.use((req, res, next) => {
next();
});
-app.use((req, res, next) => {
- cls.namespace.bindEmitter(req);
- cls.namespace.bindEmitter(res);
-
- cls.init(() => {
- cls.namespace.set("Hi");
-
- next();
- });
-});
-
app.use(bodyParser.json({limit: '500mb'}));
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
@@ -120,4 +109,4 @@ require('./services/scheduler');
module.exports = {
app,
sessionParser
-};
\ No newline at end of file
+};
diff --git a/src/routes/api/login.js b/src/routes/api/login.js
index 2bd88efec4..3a520878ce 100644
--- a/src/routes/api/login.js
+++ b/src/routes/api/login.js
@@ -68,7 +68,7 @@ async function loginToProtectedSession(req) {
const protectedSessionId = protectedSessionService.setDataKey(decryptedDataKey);
// this is set here so that event handlers have access to the protected session
- cls.namespace.set('protectedSessionId', protectedSessionId);
+ cls.set('protectedSessionId', protectedSessionId);
await eventService.emit(eventService.ENTER_PROTECTED_SESSION);
diff --git a/src/routes/routes.js b/src/routes/routes.js
index cccfe39948..81133bb591 100644
--- a/src/routes/routes.js
+++ b/src/routes/routes.js
@@ -81,9 +81,12 @@ function apiRoute(method, path, routeHandler) {
function route(method, path, middleware, routeHandler, resultHandler, transactional = true) {
router[method](path, ...middleware, async (req, res, next) => {
try {
+ cls.namespace.bindEmitter(req);
+ cls.namespace.bindEmitter(res);
+
const result = await cls.init(async () => {
- cls.namespace.set('sourceId', req.headers['trilium-source-id']);
- cls.namespace.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
+ cls.set('sourceId', req.headers['trilium-source-id']);
+ cls.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
protectedSessionService.setProtectedSessionId(req);
if (transactional) {
diff --git a/src/services/cls.js b/src/services/cls.js
index 39092b7a0b..9d14d7b7ea 100644
--- a/src/services/cls.js
+++ b/src/services/cls.js
@@ -9,6 +9,14 @@ function wrap(callback) {
return async () => await init(callback);
}
+function get(key) {
+ return namespace.get(key);
+}
+
+function set(key, value) {
+ namespace.set(key, value);
+}
+
function getSourceId() {
return namespace.get('sourceId');
}
@@ -52,6 +60,8 @@ function setEntityToCache(entityName, entityId, entity) {
module.exports = {
init,
wrap,
+ get,
+ set,
namespace,
getSourceId,
getLocalNowDateTime,
@@ -62,4 +72,4 @@ module.exports = {
addSyncRow,
getEntityFromCache,
setEntityToCache
-};
\ No newline at end of file
+};
diff --git a/src/services/protected_session.js b/src/services/protected_session.js
index 79a07ad173..ebf4b998f1 100644
--- a/src/services/protected_session.js
+++ b/src/services/protected_session.js
@@ -15,11 +15,11 @@ function setDataKey(decryptedDataKey) {
}
function setProtectedSessionId(req) {
- cls.namespace.set('protectedSessionId', req.cookies.protectedSessionId);
+ cls.set('protectedSessionId', req.cookies.protectedSessionId);
}
function getProtectedSessionId() {
- return cls.namespace.get('protectedSessionId');
+ return cls.get('protectedSessionId');
}
function getDataKey() {
@@ -63,4 +63,4 @@ module.exports = {
decryptString,
decryptNotes,
setProtectedSessionId
-};
\ No newline at end of file
+};
diff --git a/src/services/script.js b/src/services/script.js
index 51b456eb93..cfb05a4c5e 100644
--- a/src/services/script.js
+++ b/src/services/script.js
@@ -31,7 +31,7 @@ async function executeBundle(bundle, apiParams = {}) {
apiParams.startNote = bundle.note;
}
- cls.namespace.set('sourceId', 'script');
+ cls.set('sourceId', 'script');
// last \r\n is necessary if script contains line comment on its last line
const script = "async function() {\r\n" + bundle.script + "\r\n}";
@@ -187,4 +187,4 @@ module.exports = {
executeNoteNoException,
executeScript,
getScriptBundleForFrontend
-};
\ No newline at end of file
+};
diff --git a/src/services/sql.js b/src/services/sql.js
index c5227e685b..f07144e971 100644
--- a/src/services/sql.js
+++ b/src/services/sql.js
@@ -9,7 +9,7 @@ function setDbConnection(connection) {
dbConnection = connection;
}
-[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach(eventType => {
+[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach(eventType => {
process.on(eventType, () => {
if (dbConnection) {
// closing connection is especially important to fold -wal file into the main DB file
@@ -213,8 +213,8 @@ let transactionPromise = null;
let transactionPromiseResolve = null;
async function startTransactionIfNecessary() {
- if (!cls.namespace.get('isTransactional')
- || cls.namespace.get('isInTransaction')) {
+ if (!cls.get('isTransactional')
+ || cls.get('isInTransaction')) {
return;
}
@@ -225,23 +225,23 @@ async function startTransactionIfNecessary() {
// first set semaphore (atomic operation and only then start transaction
transactionActive = true;
transactionPromise = new Promise(res => transactionPromiseResolve = res);
- cls.namespace.set('isInTransaction', true);
+ cls.set('isInTransaction', true);
await beginTransaction();
}
async function transactional(func) {
// if the CLS is already transactional then the whole transaction is handled by higher level transactional() call
- if (cls.namespace.get('isTransactional')) {
+ if (cls.get('isTransactional')) {
return await func();
}
- cls.namespace.set('isTransactional', true); // this signals that transaction will be needed if there's a write operation
+ cls.set('isTransactional', true); // this signals that transaction will be needed if there's a write operation
try {
const ret = await func();
- if (cls.namespace.get('isInTransaction')) {
+ if (cls.get('isInTransaction')) {
await commit();
// note that sync rows sent from this action will be sent again by scheduled periodic ping
@@ -251,7 +251,7 @@ async function transactional(func) {
return ret;
}
catch (e) {
- if (cls.namespace.get('isInTransaction')) {
+ if (cls.get('isInTransaction')) {
await rollback();
}