added express-promise-wrap to catch and respond to unhandled exceptions immediately, previously the requests just hanged

This commit is contained in:
azivner 2018-01-07 09:35:44 -05:00
parent 20b1357be6
commit 743d72a0c3
26 changed files with 157 additions and 134 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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, nex
uploaded: true,
url: `/api/images/${imageId}/${fileName}`
});
});
}));
const MAX_SIZE = 1000;
const MAX_BYTE_SIZE = 200000; // images should have under 100 KBs

View File

@ -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, n
await 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]);

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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(
`SELECT
notes.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;

View File

@ -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(`

View File

@ -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 dialog
const 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;

View File

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

View File

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

View File

@ -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, next
sync.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;

View File

@ -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(`
SELECT
notes_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;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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