Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
added express-promise-wrap to catch and respond to unhandled exceptions immediately, previously the requests just hanged
azivner743d72a0c35e
diff --git a/routes/api/anonymization.js b/routes/api/anonymization.jsindex 7774665ef5..930d8f0fe9 100644--- a/routes/api/anonymization.js+++ b/routes/api/anonymization.js@@ -4,11 +4,12 @@ const express = require('express');const router = express.Router();const anonymization = require('../../services/anonymization');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.post('/anonymize', auth.checkApiAuth, async (req, res, next) => {+router.post('/anonymize', auth.checkApiAuth, wrap(async (req, res, next) => {await anonymization.anonymize();res.send({});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/app_info.js b/routes/api/app_info.jsindex 41e7de07d4..ddb392700a 100644--- a/routes/api/app_info.js+++ b/routes/api/app_info.js@@ -4,9 +4,10 @@ const express = require('express');const router = express.Router();const app_info = require('../../services/app_info');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkApiAuth, async (req, res, next) => {+router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {res.send(app_info);-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/cleanup.js b/routes/api/cleanup.jsindex 40eef81772..cc3c1f1548 100644--- a/routes/api/cleanup.js+++ b/routes/api/cleanup.js@@ -7,8 +7,9 @@ const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const auth = require('../../services/auth');const log = require('../../services/log');+const wrap = require('express-promise-wrap').wrap;-router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, async (req, res, next) => {+router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, wrap(async (req, res, next) => {await sql.doInTransaction(async () => {const noteIdsToDelete = await sql.getFirstColumn("SELECT note_id FROM notes WHERE is_deleted = 1");const noteIdsSql = noteIdsToDelete@@ -34,14 +35,14 @@ router.post('/cleanup-soft-deleted-items', auth.checkApiAuth, async (req, res, n});res.send({});-});+}));-router.post('/vacuum-database', auth.checkApiAuth, async (req, res, next) => {+router.post('/vacuum-database', auth.checkApiAuth, wrap(async (req, res, next) => {await sql.execute("VACUUM");log.info("Database has been vacuumed.");res.send({});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/event_log.js b/routes/api/event_log.jsindex 037be74602..2be4b24a77 100644--- a/routes/api/event_log.js+++ b/routes/api/event_log.js@@ -4,14 +4,15 @@ const express = require('express');const router = express.Router();const sql = require('../../services/sql');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkApiAuth, async (req, res, next) => {+router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {await deleteOld();const result = await sql.getAll("SELECT * FROM event_log ORDER BY date_added DESC");res.send(result);-});+}));async function deleteOld() {const cutoffId = await sql.getFirstValue("SELECT id FROM event_log ORDER BY id DESC LIMIT 1000, 1");diff --git a/routes/api/export.js b/routes/api/export.jsindex d5c7b46f8d..07e1dbf51f 100644--- a/routes/api/export.js+++ b/routes/api/export.js@@ -8,8 +8,9 @@ const sql = require('../../services/sql');const data_dir = require('../../services/data_dir');const html = require('html');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('/:noteId/to/:directory', auth.checkApiAuth, async (req, res, next) => {+router.get('/:noteId/to/:directory', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');@@ -30,7 +31,7 @@ router.get('/:noteId/to/:directory', auth.checkApiAuth, async (req, res, next) =await exportNote(noteTreeId, completeExportDir);res.send({});-});+}));async function exportNote(noteTreeId, dir) {const noteTree = await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]);diff --git a/routes/api/image.js b/routes/api/image.jsindex e7e61680b4..353d1bc39c 100644--- a/routes/api/image.js+++ b/routes/api/image.js@@ -14,8 +14,9 @@ const imageminGifLossy = require('imagemin-giflossy');const jimp = require('jimp');const imageType = require('image-type');const sanitizeFilename = require('sanitize-filename');+const wrap = require('express-promise-wrap').wrap;-router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {+router.get('/:imageId/:filename', auth.checkApiAuth, wrap(async (req, res, next) => {const image = await sql.getFirst("SELECT * FROM images WHERE image_id = ?", [req.params.imageId]);if (!image) {@@ -25,9 +26,9 @@ router.get('/:imageId/:filename', auth.checkApiAuth, async (req, res, next) => {res.set('Content-Type', 'image/' + image.format);res.send(image.data);-});+}));-router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, next) => {+router.post('', auth.checkApiAuth, multer.single('upload'), wrap(async (req, res, next) => {const sourceId = req.headers.source_id;const noteId = req.query.noteId;const file = req.file;@@ -86,7 +87,7 @@ router.post('', auth.checkApiAuth, multer.single('upload'), async (req, res, nexuploaded: true,url: `/api/images/${imageId}/${fileName}`});-});+}));const MAX_SIZE = 1000;const MAX_BYTE_SIZE = 200000; // images should have under 100 KBsdiff --git a/routes/api/import.js b/routes/api/import.jsindex 9b6a927673..a8f011d0ed 100644--- a/routes/api/import.js+++ b/routes/api/import.js@@ -8,8 +8,9 @@ const data_dir = require('../../services/data_dir');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {+router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {const directory = req.params.directory.replace(/[^0-9a-zA-Z_-]/gi, '');const parentNoteId = req.params.parentNoteId;@@ -18,7 +19,7 @@ router.get('/:directory/to/:parentNoteId', auth.checkApiAuth, async (req, res, nawait sql.doInTransaction(async () => await importNotes(dir, parentNoteId));res.send({});-});+}));async function importNotes(dir, parentNoteId) {const parent = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [parentNoteId]);diff --git a/routes/api/login.js b/routes/api/login.jsindex e62d2053b1..c632c5665e 100644--- a/routes/api/login.js+++ b/routes/api/login.js@@ -9,8 +9,9 @@ const auth = require('../../services/auth');const password_encryption = require('../../services/password_encryption');const protected_session = require('../../services/protected_session');const app_info = require('../../services/app_info');+const wrap = require('express-promise-wrap').wrap;-router.post('/sync', async (req, res, next) => {+router.post('/sync', wrap(async (req, res, next) => {const timestampStr = req.body.timestamp;const timestamp = utils.parseDate(timestampStr);@@ -44,10 +45,10 @@ router.post('/sync', async (req, res, next) => {res.send({sourceId: source_id.getCurrentSourceId()});-});+}));// this is for entering protected mode so user has to be already logged-in (that's the reason we don't require username)-router.post('/protected', auth.checkApiAuth, async (req, res, next) => {+router.post('/protected', auth.checkApiAuth, wrap(async (req, res, next) => {const password = req.body.password;if (!await password_encryption.verifyPassword(password)) {@@ -67,6 +68,6 @@ router.post('/protected', auth.checkApiAuth, async (req, res, next) => {success: true,protectedSessionId: protectedSessionId});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/migration.js b/routes/api/migration.jsindex 9d75ceab22..332289aaf7 100644--- a/routes/api/migration.js+++ b/routes/api/migration.js@@ -6,20 +6,21 @@ const auth = require('../../services/auth');const options = require('../../services/options');const migration = require('../../services/migration');const app_info = require('../../services/app_info');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkApiAuthForMigrationPage, async (req, res, next) => {+router.get('', auth.checkApiAuthForMigrationPage, wrap(async (req, res, next) => {res.send({db_version: parseInt(await options.getOption('db_version')),app_db_version: app_info.db_version});-});+}));-router.post('', auth.checkApiAuthForMigrationPage, async (req, res, next) => {+router.post('', auth.checkApiAuthForMigrationPage, wrap(async (req, res, next) => {const migrations = await migration.migrate();res.send({migrations: migrations});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/note_history.js b/routes/api/note_history.jsindex 49a006d2b1..3a5feb2aea 100644--- a/routes/api/note_history.js+++ b/routes/api/note_history.js@@ -7,8 +7,9 @@ const auth = require('../../services/auth');const data_encryption = require('../../services/data_encryption');const protected_session = require('../../services/protected_session');const sync_table = require('../../services/sync_table');+const wrap = require('express-promise-wrap').wrap;-router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {+router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;const history = await sql.getAll("SELECT * FROM notes_history WHERE note_id = ? order by date_modified_to desc", [noteId]);@@ -22,9 +23,9 @@ router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {}res.send(history);-});+}));-router.put('', auth.checkApiAuth, async (req, res, next) => {+router.put('', auth.checkApiAuth, wrap(async (req, res, next) => {const sourceId = req.headers.source_id;await sql.doInTransaction(async () => {@@ -34,6 +35,6 @@ router.put('', auth.checkApiAuth, async (req, res, next) => {});res.send();-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/notes.js b/routes/api/notes.jsindex 4aa7bc4af4..8ef47052e1 100644--- a/routes/api/notes.js+++ b/routes/api/notes.js@@ -8,8 +8,9 @@ const notes = require('../../services/notes');const log = require('../../services/log');const protected_session = require('../../services/protected_session');const data_encryption = require('../../services/data_encryption');+const wrap = require('express-promise-wrap').wrap;-router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {+router.get('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;const detail = await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId]);@@ -30,9 +31,9 @@ router.get('/:noteId', auth.checkApiAuth, async (req, res, next) => {res.send({detail: detail});-});+}));-router.post('/:parentNoteId/children', auth.checkApiAuth, async (req, res, next) => {+router.post('/:parentNoteId/children', auth.checkApiAuth, wrap(async (req, res, next) => {const sourceId = req.headers.source_id;const parentNoteId = req.params.parentNoteId;const note = req.body;@@ -43,9 +44,9 @@ router.post('/:parentNoteId/children', auth.checkApiAuth, async (req, res, next)'note_id': noteId,'note_tree_id': noteTreeId});-});+}));-router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const note = req.body;const noteId = req.params.noteId;const sourceId = req.headers.source_id;@@ -54,17 +55,17 @@ router.put('/:noteId', auth.checkApiAuth, async (req, res, next) => {await notes.updateNote(noteId, note, dataKey, sourceId);res.send({});-});+}));-router.delete('/:noteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.delete('/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {await sql.doInTransaction(async () => {await notes.deleteNote(req.params.noteTreeId, req.headers.source_id);});res.send({});-});+}));-router.get('/', auth.checkApiAuth, async (req, res, next) => {+router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {const search = '%' + req.query.search + '%';const result = await sql.getAll("SELECT note_id FROM notes WHERE note_title LIKE ? OR note_text LIKE ?", [search, search]);@@ -76,6 +77,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {}res.send(noteIdList);-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/notes_move.js b/routes/api/notes_move.jsindex 21ee9327d5..b75b17ab78 100644--- a/routes/api/notes_move.js+++ b/routes/api/notes_move.js@@ -6,13 +6,14 @@ const sql = require('../../services/sql');const auth = require('../../services/auth');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');+const wrap = require('express-promise-wrap').wrap;/*** Code in this file deals with moving and cloning note tree rows. Relationship between note and parent note is unique* for not deleted note trees. There may be multiple deleted note-parent note relationships.*/-router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const parentNoteId = req.params.parentNoteId;const sourceId = req.headers.source_id;@@ -36,9 +37,9 @@ router.put('/:noteTreeId/move-to/:parentNoteId', auth.checkApiAuth, async (req,});res.send({ success: true });-});+}));-router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const beforeNoteTreeId = req.params.beforeNoteTreeId;const sourceId = req.headers.source_id;@@ -67,9 +68,9 @@ router.put('/:noteTreeId/move-before/:beforeNoteTreeId', auth.checkApiAuth, asyn});res.send({ success: true });-});+}));-router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const afterNoteTreeId = req.params.afterNoteTreeId;const sourceId = req.headers.source_id;@@ -96,9 +97,9 @@ router.put('/:noteTreeId/move-after/:afterNoteTreeId', auth.checkApiAuth, async});res.send({ success: true });-});+}));-router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, wrap(async (req, res, next) => {const parentNoteId = req.params.parentNoteId;const childNoteId = req.params.childNoteId;const prefix = req.body.prefix;@@ -131,9 +132,9 @@ router.put('/:childNoteId/clone-to/:parentNoteId', auth.checkApiAuth, async (req});res.send({ success: true });-});+}));-router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;const afterNoteTreeId = req.params.afterNoteTreeId;const sourceId = req.headers.source_id;@@ -168,7 +169,7 @@ router.put('/:noteId/clone-after/:afterNoteTreeId', auth.checkApiAuth, async (re});res.send({ success: true });-});+}));async function loadSubTreeNoteIds(parentNoteId, subTreeNoteIds) {subTreeNoteIds.push(parentNoteId);@@ -246,7 +247,7 @@ async function checkTreeCycle(parentNoteId, childNoteId) {return await checkTreeCycleInner(parentNoteId);}-router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const expanded = req.params.expanded;@@ -257,6 +258,6 @@ router.put('/:noteTreeId/expanded/:expanded', auth.checkApiAuth, async (req, res});res.send({});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/password.js b/routes/api/password.jsindex ebd6288d9d..74e20317a7 100644--- a/routes/api/password.js+++ b/routes/api/password.js@@ -5,11 +5,12 @@ const router = express.Router();const sql = require('../../services/sql');const changePassword = require('../../services/change_password');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.post('/change', auth.checkApiAuth, async (req, res, next) => {+router.post('/change', auth.checkApiAuth, wrap(async (req, res, next) => {const result = await changePassword.changePassword(req.body['current_password'], req.body['new_password'], req);res.send(result);-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/recent_changes.js b/routes/api/recent_changes.jsindex d1069d2ce0..b79b7ea617 100644--- a/routes/api/recent_changes.js+++ b/routes/api/recent_changes.js@@ -4,8 +4,9 @@ const express = require('express');const router = express.Router();const sql = require('../../services/sql');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('/', auth.checkApiAuth, async (req, res, next) => {+router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {const recentChanges = await sql.getAll(`SELECTnotes.is_deleted AS current_is_deleted,@@ -19,6 +20,6 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {LIMIT 1000`);res.send(recentChanges);-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/recent_notes.js b/routes/api/recent_notes.jsindex 4cf4003d61..33d4a2d64e 100644--- a/routes/api/recent_notes.js+++ b/routes/api/recent_notes.js@@ -7,12 +7,13 @@ const auth = require('../../services/auth');const utils = require('../../services/utils');const sync_table = require('../../services/sync_table');const options = require('../../services/options');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkApiAuth, async (req, res, next) => {+router.get('', auth.checkApiAuth, wrap(async (req, res, next) => {res.send(await getRecentNotes());-});+}));-router.put('/:noteTreeId/:notePath', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/:notePath', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const notePath = req.params.notePath;const sourceId = req.headers.source_id;@@ -31,7 +32,7 @@ router.put('/:noteTreeId/:notePath', auth.checkApiAuth, async (req, res, next) =});res.send(await getRecentNotes());-});+}));async function getRecentNotes() {return await sql.getAll(`diff --git a/routes/api/settings.js b/routes/api/settings.jsindex a4bf421fe2..af25439fbb 100644--- a/routes/api/settings.js+++ b/routes/api/settings.js@@ -5,24 +5,25 @@ const router = express.Router();const sql = require('../../services/sql');const options = require('../../services/options');const auth = require('../../services/auth');+const wrap = require('express-promise-wrap').wrap;// options allowed to be updated directly in settings dialogconst ALLOWED_OPTIONS = ['protected_session_timeout', 'history_snapshot_time_interval'];-router.get('/all', auth.checkApiAuth, async (req, res, next) => {+router.get('/all', auth.checkApiAuth, wrap(async (req, res, next) => {const settings = await sql.getMap("SELECT opt_name, opt_value FROM options");res.send(settings);-});+}));-router.get('/', auth.checkApiAuth, async (req, res, next) => {+router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {const settings = await sql.getMap("SELECT opt_name, opt_value FROM options WHERE opt_name IN ("+ ALLOWED_OPTIONS.map(x => '?').join(",") + ")", ALLOWED_OPTIONS);res.send(settings);-});+}));-router.post('/', auth.checkApiAuth, async (req, res, next) => {+router.post('/', auth.checkApiAuth, wrap(async (req, res, next) => {const body = req.body;const sourceId = req.headers.source_id;@@ -38,6 +39,6 @@ router.post('/', auth.checkApiAuth, async (req, res, next) => {else {res.send("not allowed option to set");}-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/setup.js b/routes/api/setup.jsindex ea31c84a77..7fbd539087 100644--- a/routes/api/setup.js+++ b/routes/api/setup.js@@ -8,8 +8,9 @@ const sql = require('../../services/sql');const utils = require('../../services/utils');const my_scrypt = require('../../services/my_scrypt');const password_encryption = require('../../services/password_encryption');+const wrap = require('express-promise-wrap').wrap;-router.post('', auth.checkAppNotInitialized, async (req, res, next) => {+router.post('', auth.checkAppNotInitialized, wrap(async (req, res, next) => {const { username, password } = req.body;await sql.doInTransaction(async () => {@@ -27,6 +28,6 @@ router.post('', auth.checkAppNotInitialized, async (req, res, next) => {sql.setDbReadyAsResolved();res.send({});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/sql.js b/routes/api/sql.jsindex 38ce811713..d5a1e47b3f 100644--- a/routes/api/sql.js+++ b/routes/api/sql.js@@ -4,8 +4,9 @@ const express = require('express');const router = express.Router();const auth = require('../../services/auth');const sql = require('../../services/sql');+const wrap = require('express-promise-wrap').wrap;-router.post('/execute', auth.checkApiAuth, async (req, res, next) => {+router.post('/execute', auth.checkApiAuth, wrap(async (req, res, next) => {const query = req.body.query;try {@@ -20,6 +21,6 @@ router.post('/execute', auth.checkApiAuth, async (req, res, next) => {error: e.message});}-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/sync.js b/routes/api/sync.jsindex 7cede92946..90b3e8d3cc 100644--- a/routes/api/sync.js+++ b/routes/api/sync.js@@ -10,19 +10,20 @@ const sql = require('../../services/sql');const options = require('../../services/options');const content_hash = require('../../services/content_hash');const log = require('../../services/log');+const wrap = require('express-promise-wrap').wrap;-router.get('/check', auth.checkApiAuth, async (req, res, next) => {+router.get('/check', auth.checkApiAuth, wrap(async (req, res, next) => {res.send({'hashes': await content_hash.getHashes(),'max_sync_id': await sql.getFirstValue('SELECT MAX(id) FROM sync')});-});+}));-router.post('/now', auth.checkApiAuth, async (req, res, next) => {+router.post('/now', auth.checkApiAuth, wrap(async (req, res, next) => {res.send(await sync.sync());-});+}));-router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {+router.post('/fill-sync-rows', auth.checkApiAuth, wrap(async (req, res, next) => {await sql.doInTransaction(async () => {await sync_table.fillAllSyncRows();});@@ -30,9 +31,9 @@ router.post('/fill-sync-rows', auth.checkApiAuth, async (req, res, next) => {log.info("Sync rows have been filled.");res.send({});-});+}));-router.post('/force-full-sync', auth.checkApiAuth, async (req, res, next) => {+router.post('/force-full-sync', auth.checkApiAuth, wrap(async (req, res, next) => {await sql.doInTransaction(async () => {await options.setOption('last_synced_pull', 0);await options.setOption('last_synced_push', 0);@@ -44,9 +45,9 @@ router.post('/force-full-sync', auth.checkApiAuth, async (req, res, next) => {sync.sync();res.send({});-});+}));-router.post('/force-note-sync/:noteId', auth.checkApiAuth, async (req, res, next) => {+router.post('/force-note-sync/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;await sql.doInTransaction(async () => {@@ -68,35 +69,35 @@ router.post('/force-note-sync/:noteId', auth.checkApiAuth, async (req, res, nextsync.sync();res.send({});-});+}));-router.get('/changed', auth.checkApiAuth, async (req, res, next) => {+router.get('/changed', auth.checkApiAuth, wrap(async (req, res, next) => {const lastSyncId = parseInt(req.query.lastSyncId);res.send(await sql.getAll("SELECT * FROM sync WHERE id > ?", [lastSyncId]));-});+}));-router.get('/notes/:noteId', auth.checkApiAuth, async (req, res, next) => {+router.get('/notes/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;res.send({entity: await sql.getFirst("SELECT * FROM notes WHERE note_id = ?", [noteId])});-});+}));-router.get('/notes_tree/:noteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.get('/notes_tree/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;res.send(await sql.getFirst("SELECT * FROM notes_tree WHERE note_tree_id = ?", [noteTreeId]));-});+}));-router.get('/notes_history/:noteHistoryId', auth.checkApiAuth, async (req, res, next) => {+router.get('/notes_history/:noteHistoryId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteHistoryId = req.params.noteHistoryId;res.send(await sql.getFirst("SELECT * FROM notes_history WHERE note_history_id = ?", [noteHistoryId]));-});+}));-router.get('/options/:optName', auth.checkApiAuth, async (req, res, next) => {+router.get('/options/:optName', auth.checkApiAuth, wrap(async (req, res, next) => {const optName = req.params.optName;if (!options.SYNCED_OPTIONS.includes(optName)) {@@ -105,24 +106,24 @@ router.get('/options/:optName', auth.checkApiAuth, async (req, res, next) => {else {res.send(await sql.getFirst("SELECT * FROM options WHERE opt_name = ?", [optName]));}-});+}));-router.get('/notes_reordering/:noteTreeParentId', auth.checkApiAuth, async (req, res, next) => {+router.get('/notes_reordering/:noteTreeParentId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeParentId = req.params.noteTreeParentId;res.send({parent_note_id: noteTreeParentId,ordering: await sql.getMap("SELECT note_tree_id, note_position FROM notes_tree WHERE parent_note_id = ? AND is_deleted = 0", [noteTreeParentId])});-});+}));-router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, async (req, res, next) => {+router.get('/recent_notes/:noteTreeId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;res.send(await sql.getFirst("SELECT * FROM recent_notes WHERE note_tree_id = ?", [noteTreeId]));-});+}));-router.get('/images/:imageId', auth.checkApiAuth, async (req, res, next) => {+router.get('/images/:imageId', auth.checkApiAuth, wrap(async (req, res, next) => {const imageId = req.params.imageId;const entity = await sql.getFirst("SELECT * FROM images WHERE image_id = ?", [imageId]);@@ -131,60 +132,60 @@ router.get('/images/:imageId', auth.checkApiAuth, async (req, res, next) => {}res.send(entity);-});+}));-router.get('/notes_image/:noteImageId', auth.checkApiAuth, async (req, res, next) => {+router.get('/notes_image/:noteImageId', auth.checkApiAuth, wrap(async (req, res, next) => {const noteImageId = req.params.noteImageId;res.send(await sql.getFirst("SELECT * FROM notes_image WHERE note_image_id = ?", [noteImageId]));-});+}));-router.put('/notes', auth.checkApiAuth, async (req, res, next) => {+router.put('/notes', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateNote(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/notes_tree', auth.checkApiAuth, async (req, res, next) => {+router.put('/notes_tree', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateNoteTree(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/notes_history', auth.checkApiAuth, async (req, res, next) => {+router.put('/notes_history', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateNoteHistory(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/notes_reordering', auth.checkApiAuth, async (req, res, next) => {+router.put('/notes_reordering', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateNoteReordering(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/options', auth.checkApiAuth, async (req, res, next) => {+router.put('/options', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateOptions(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/recent_notes', auth.checkApiAuth, async (req, res, next) => {+router.put('/recent_notes', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateRecentNotes(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/images', auth.checkApiAuth, async (req, res, next) => {+router.put('/images', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateImage(req.body.entity, req.body.sourceId);res.send({});-});+}));-router.put('/notes_image', auth.checkApiAuth, async (req, res, next) => {+router.put('/notes_image', auth.checkApiAuth, wrap(async (req, res, next) => {await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId);res.send({});-});+}));module.exports = router;\ No newline at end of filediff --git a/routes/api/tree.js b/routes/api/tree.jsindex 6bbdf7688e..f8539c3058 100644--- a/routes/api/tree.js+++ b/routes/api/tree.js@@ -10,8 +10,9 @@ const protected_session = require('../../services/protected_session');const data_encryption = require('../../services/data_encryption');const notes = require('../../services/notes');const sync_table = require('../../services/sync_table');+const wrap = require('express-promise-wrap').wrap;-router.get('/', auth.checkApiAuth, async (req, res, next) => {+router.get('/', auth.checkApiAuth, wrap(async (req, res, next) => {const notes = await sql.getAll(`SELECTnotes_tree.*,@@ -39,9 +40,9 @@ router.get('/', auth.checkApiAuth, async (req, res, next) => {notes: notes,start_note_path: await options.getOption('start_note_path')});-});+}));-router.put('/:noteId/protect-sub-tree/:isProtected', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteId/protect-sub-tree/:isProtected', auth.checkApiAuth, wrap(async (req, res, next) => {const noteId = req.params.noteId;const isProtected = !!parseInt(req.params.isProtected);const dataKey = protected_session.getDataKey(req);@@ -52,9 +53,9 @@ router.put('/:noteId/protect-sub-tree/:isProtected', auth.checkApiAuth, async (r});res.send({});-});+}));-router.put('/:noteTreeId/set-prefix', auth.checkApiAuth, async (req, res, next) => {+router.put('/:noteTreeId/set-prefix', auth.checkApiAuth, wrap(async (req, res, next) => {const noteTreeId = req.params.noteTreeId;const sourceId = req.headers.source_id;const prefix = utils.isEmptyOrWhitespace(req.body.prefix) ? null : req.body.prefix;@@ -66,6 +67,6 @@ router.put('/:noteTreeId/set-prefix', auth.checkApiAuth, async (req, res, next)});res.send({});-});+}));module.exports = router;diff --git a/routes/index.js b/routes/index.jsindex efe70b3181..162462e9d0 100644--- a/routes/index.js+++ b/routes/index.js@@ -5,12 +5,13 @@ const router = express.Router();const auth = require('../services/auth');const source_id = require('../services/source_id');const sql = require('../services/sql');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkAuth, async (req, res, next) => {+router.get('', auth.checkAuth, wrap(async (req, res, next) => {res.render('index', {sourceId: await source_id.generateSourceId(),maxSyncIdAtLoad: await sql.getFirstValue("SELECT MAX(id) FROM sync")});-});+}));module.exports = router;diff --git a/routes/login.js b/routes/login.jsindex e6c63c2f00..84a724877d 100644--- a/routes/login.js+++ b/routes/login.js@@ -5,12 +5,13 @@ const router = express.Router();const utils = require('../services/utils');const options = require('../services/options');const my_scrypt = require('../services/my_scrypt');+const wrap = require('express-promise-wrap').wrap;-router.get('', (req, res, next) => {+router.get('', wrap(async (req, res, next) => {res.render('login', { 'failedAuth': false });-});+}));-router.post('', async (req, res, next) => {+router.post('', wrap(async (req, res, next) => {const userName = await options.getOption('username');const guessedPassword = req.body.password;@@ -32,7 +33,7 @@ router.post('', async (req, res, next) => {else {res.render('login', {'failedAuth': true});}-});+}));async function verifyPassword(guessed_password) {diff --git a/routes/logout.js b/routes/logout.jsindex c93b98a93b..0c4b63bee4 100644--- a/routes/logout.js+++ b/routes/logout.js@@ -2,14 +2,15 @@const express = require('express');const router = express.Router();+const wrap = require('express-promise-wrap').wrap;-router.post('', async (req, res, next) => {+router.post('', wrap(async (req, res, next) => {req.session.regenerate(() => {req.session.loggedIn = false;res.redirect('/');});-});+}));module.exports = router;diff --git a/routes/migration.js b/routes/migration.jsindex 7f1b4e0e05..38218bb67c 100644--- a/routes/migration.js+++ b/routes/migration.js@@ -3,9 +3,10 @@const express = require('express');const router = express.Router();const auth = require('../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkAuthForMigrationPage, (req, res, next) => {+router.get('', auth.checkAuthForMigrationPage, wrap(async (req, res, next) => {res.render('migration', {});-});+}));module.exports = router;diff --git a/routes/setup.js b/routes/setup.jsindex 083c906117..6cdeb86724 100644--- a/routes/setup.js+++ b/routes/setup.js@@ -3,9 +3,10 @@const express = require('express');const router = express.Router();const auth = require('../services/auth');+const wrap = require('express-promise-wrap').wrap;-router.get('', auth.checkAppNotInitialized, (req, res, next) => {+router.get('', auth.checkAppNotInitialized, wrap(async (req, res, next) => {res.render('setup', {});-});+}));module.exports = router;diff --git a/services/notes.js b/services/notes.jsindex fb2822a952..d3278785d7 100644--- a/services/notes.js+++ b/services/notes.js@@ -166,8 +166,6 @@ async function saveNoteImages(noteId, noteText, sourceId) {let match;while (match = re.exec(noteText)) {- console.log(match);-const imageId = match[1];const existingNoteImage = existingNoteImages.find(ni => ni.image_id === imageId);