fixes and polish

This commit is contained in:
zadam 2020-06-15 17:56:53 +02:00
parent 6ba2e5cf73
commit 00faf758e8
7 changed files with 31 additions and 29 deletions

View file

@ -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
};
};

View file

@ -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);

View file

@ -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) {

View file

@ -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
};
};

View file

@ -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
};
};

View file

@ -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
};
};

View file

@ -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();
}