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(); 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.json({limit: '500mb'}));
app.use(bodyParser.urlencoded({extended: false})); app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser()); app.use(cookieParser());
@ -120,4 +109,4 @@ require('./services/scheduler');
module.exports = { module.exports = {
app, app,
sessionParser sessionParser
}; };

View File

@ -68,7 +68,7 @@ async function loginToProtectedSession(req) {
const protectedSessionId = protectedSessionService.setDataKey(decryptedDataKey); const protectedSessionId = protectedSessionService.setDataKey(decryptedDataKey);
// this is set here so that event handlers have access to the protected session // 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); 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) { function route(method, path, middleware, routeHandler, resultHandler, transactional = true) {
router[method](path, ...middleware, async (req, res, next) => { router[method](path, ...middleware, async (req, res, next) => {
try { try {
cls.namespace.bindEmitter(req);
cls.namespace.bindEmitter(res);
const result = await cls.init(async () => { const result = await cls.init(async () => {
cls.namespace.set('sourceId', req.headers['trilium-source-id']); cls.set('sourceId', req.headers['trilium-source-id']);
cls.namespace.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']); cls.set('localNowDateTime', req.headers['`trilium-local-now-datetime`']);
protectedSessionService.setProtectedSessionId(req); protectedSessionService.setProtectedSessionId(req);
if (transactional) { if (transactional) {

View File

@ -9,6 +9,14 @@ function wrap(callback) {
return async () => await init(callback); return async () => await init(callback);
} }
function get(key) {
return namespace.get(key);
}
function set(key, value) {
namespace.set(key, value);
}
function getSourceId() { function getSourceId() {
return namespace.get('sourceId'); return namespace.get('sourceId');
} }
@ -52,6 +60,8 @@ function setEntityToCache(entityName, entityId, entity) {
module.exports = { module.exports = {
init, init,
wrap, wrap,
get,
set,
namespace, namespace,
getSourceId, getSourceId,
getLocalNowDateTime, getLocalNowDateTime,
@ -62,4 +72,4 @@ module.exports = {
addSyncRow, addSyncRow,
getEntityFromCache, getEntityFromCache,
setEntityToCache setEntityToCache
}; };

View File

@ -15,11 +15,11 @@ function setDataKey(decryptedDataKey) {
} }
function setProtectedSessionId(req) { function setProtectedSessionId(req) {
cls.namespace.set('protectedSessionId', req.cookies.protectedSessionId); cls.set('protectedSessionId', req.cookies.protectedSessionId);
} }
function getProtectedSessionId() { function getProtectedSessionId() {
return cls.namespace.get('protectedSessionId'); return cls.get('protectedSessionId');
} }
function getDataKey() { function getDataKey() {
@ -63,4 +63,4 @@ module.exports = {
decryptString, decryptString,
decryptNotes, decryptNotes,
setProtectedSessionId setProtectedSessionId
}; };

View File

@ -31,7 +31,7 @@ async function executeBundle(bundle, apiParams = {}) {
apiParams.startNote = bundle.note; 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 // last \r\n is necessary if script contains line comment on its last line
const script = "async function() {\r\n" + bundle.script + "\r\n}"; const script = "async function() {\r\n" + bundle.script + "\r\n}";
@ -187,4 +187,4 @@ module.exports = {
executeNoteNoException, executeNoteNoException,
executeScript, executeScript,
getScriptBundleForFrontend getScriptBundleForFrontend
}; };

View File

@ -9,7 +9,7 @@ function setDbConnection(connection) {
dbConnection = connection; dbConnection = connection;
} }
[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `uncaughtException`, `SIGTERM`].forEach(eventType => { [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach(eventType => {
process.on(eventType, () => { process.on(eventType, () => {
if (dbConnection) { if (dbConnection) {
// closing connection is especially important to fold -wal file into the main DB file // closing connection is especially important to fold -wal file into the main DB file
@ -213,8 +213,8 @@ let transactionPromise = null;
let transactionPromiseResolve = null; let transactionPromiseResolve = null;
async function startTransactionIfNecessary() { async function startTransactionIfNecessary() {
if (!cls.namespace.get('isTransactional') if (!cls.get('isTransactional')
|| cls.namespace.get('isInTransaction')) { || cls.get('isInTransaction')) {
return; return;
} }
@ -225,23 +225,23 @@ async function startTransactionIfNecessary() {
// first set semaphore (atomic operation and only then start transaction // first set semaphore (atomic operation and only then start transaction
transactionActive = true; transactionActive = true;
transactionPromise = new Promise(res => transactionPromiseResolve = res); transactionPromise = new Promise(res => transactionPromiseResolve = res);
cls.namespace.set('isInTransaction', true); cls.set('isInTransaction', true);
await beginTransaction(); await beginTransaction();
} }
async function transactional(func) { async function transactional(func) {
// if the CLS is already transactional then the whole transaction is handled by higher level transactional() call // 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(); 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 { try {
const ret = await func(); const ret = await func();
if (cls.namespace.get('isInTransaction')) { if (cls.get('isInTransaction')) {
await commit(); await commit();
// note that sync rows sent from this action will be sent again by scheduled periodic ping // 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; return ret;
} }
catch (e) { catch (e) {
if (cls.namespace.get('isInTransaction')) { if (cls.get('isInTransaction')) {
await rollback(); await rollback();
} }