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
refactored backend to use new naming convention for modules
azivnere2921a648d05
diff --git a/src/routes/api/cleanup.js b/src/routes/api/cleanup.jsindex 02968799f0..4304b28ed5 100644--- a/src/routes/api/cleanup.js+++ b/src/routes/api/cleanup.js@@ -2,7 +2,7 @@const sql = require('../../services/sql');const utils = require('../../services/utils');-const sync_table = require('../../services/sync_table');+const syncTable = require('../../services/sync_table');const log = require('../../services/log');const repository = require('../../services/repository');@@ -30,13 +30,13 @@ async function cleanupSoftDeletedItems() {await sql.execute("DELETE FROM recent_notes");- await sync_table.cleanupSyncRowsForMissingEntities("notes", "noteId");- await sync_table.cleanupSyncRowsForMissingEntities("branches", "branchId");- await sync_table.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");- await sync_table.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");- await sync_table.cleanupSyncRowsForMissingEntities("images", "imageId");- await sync_table.cleanupSyncRowsForMissingEntities("note_images", "noteImageId");- await sync_table.cleanupSyncRowsForMissingEntities("labels", "labelId");+ await syncTable.cleanupSyncRowsForMissingEntities("notes", "noteId");+ await syncTable.cleanupSyncRowsForMissingEntities("branches", "branchId");+ await syncTable.cleanupSyncRowsForMissingEntities("note_revisions", "noteRevisionId");+ await syncTable.cleanupSyncRowsForMissingEntities("recent_notes", "branchId");+ await syncTable.cleanupSyncRowsForMissingEntities("images", "imageId");+ await syncTable.cleanupSyncRowsForMissingEntities("note_images", "noteImageId");+ await syncTable.cleanupSyncRowsForMissingEntities("labels", "labelId");log.info("Following notes has been completely cleaned from database: " + noteIdsSql);}diff --git a/src/routes/api/cloning.js b/src/routes/api/cloning.jsindex c6d3bceae1..99e4c4b2d9 100644--- a/src/routes/api/cloning.js+++ b/src/routes/api/cloning.js@@ -1,7 +1,7 @@"use strict";const sql = require('../../services/sql');-const sync_table = require('../../services/sync_table');+const syncTable = require('../../services/sync_table');const tree = require('../../services/tree');const Branch = require('../../entities/branch');@@ -51,7 +51,7 @@ async function cloneNoteAfter(req) {await sql.execute("UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0",[afterNote.parentNoteId, afterNote.notePosition]);- await sync_table.addNoteReorderingSync(afterNote.parentNoteId);+ await syncTable.addNoteReorderingSync(afterNote.parentNoteId);const branch = new Branch({noteId: noteId,diff --git a/src/routes/api/file_upload.js b/src/routes/api/file_upload.jsindex b1ac17afe2..a3b978e09f 100644--- a/src/routes/api/file_upload.js+++ b/src/routes/api/file_upload.js@@ -1,8 +1,8 @@"use strict";-const notes = require('../../services/notes');-const labels = require('../../services/labels');-const protected_session = require('../../services/protected_session');+const noteService = require('../../services/notes');+const labelService = require('../../services/labels');+const protectedSessionService = require('../../services/protected_session');const repository = require('../../services/repository');async function uploadFile(req) {@@ -17,7 +17,7 @@ async function uploadFile(req) {return [404, `Note ${parentNoteId} doesn't exist.`];}- const {note} = await notes.createNewNote(parentNoteId, {+ const {note} = await noteService.createNewNote(parentNoteId, {title: originalName,content: file.buffer,target: 'into',@@ -26,8 +26,8 @@ async function uploadFile(req) {mime: file.mimetype});- await labels.createLabel(note.noteId, "original_file_name", originalName);- await labels.createLabel(note.noteId, "file_size", size);+ await labelService.createLabel(note.noteId, "original_file_name", originalName);+ await labelService.createLabel(note.noteId, "file_size", size);return {noteId: note.noteId@@ -42,7 +42,7 @@ async function downloadFile(req, res) {return res.status(404).send(`Note ${noteId} doesn't exist.`);}- if (note.isProtected && !protected_session.isProtectedSessionAvailable()) {+ if (note.isProtected && !protectedSessionService.isProtectedSessionAvailable()) {res.status(401).send("Protected session not available");return;}diff --git a/src/routes/api/import.js b/src/routes/api/import.jsindex ca5c1b64c1..71e4dedbf4 100644--- a/src/routes/api/import.js+++ b/src/routes/api/import.js@@ -1,8 +1,8 @@"use strict";const repository = require('../../services/repository');-const labels = require('../../services/labels');-const notes = require('../../services/notes');+const labelService = require('../../services/labels');+const noteService = require('../../services/notes');const tar = require('tar-stream');const stream = require('stream');const path = require('path');@@ -110,13 +110,13 @@ async function importNotes(files, parentNoteId) {file.data = file.data.toString("UTF-8");}- const noteId = await notes.createNote(parentNoteId, file.meta.title, file.data, {+ const noteId = await noteService.createNote(parentNoteId, file.meta.title, file.data, {type: file.meta.type,mime: file.meta.mime});for (const label of file.meta.labels) {- await labels.createLabel(noteId, label.name, label.value);+ await labelService.createLabel(noteId, label.name, label.value);}if (file.children.length > 0) {diff --git a/src/routes/api/labels.js b/src/routes/api/labels.jsindex 073bea483c..c23faafeba 100644--- a/src/routes/api/labels.js+++ b/src/routes/api/labels.js@@ -1,8 +1,7 @@"use strict";const sql = require('../../services/sql');-const utils = require('../../services/utils');-const labels = require('../../services/labels');+const labelService = require('../../services/labels');const repository = require('../../services/repository');const Label = require('../../entities/label');@@ -46,7 +45,7 @@ async function updateNoteLabels(req) {async function getAllLabelNames() {const names = await sql.getColumn("SELECT DISTINCT name FROM labels WHERE isDeleted = 0");- for (const label of labels.BUILTIN_LABELS) {+ for (const label of labelService.BUILTIN_LABELS) {if (!names.includes(label)) {names.push(label);}diff --git a/src/routes/api/login.js b/src/routes/api/login.jsindex 044fb572c6..39caa2c2ed 100644--- a/src/routes/api/login.js+++ b/src/routes/api/login.js@@ -2,10 +2,10 @@const options = require('../../services/options');const utils = require('../../services/utils');-const source_id = require('../../services/source_id');-const password_encryption = require('../../services/password_encryption');-const protected_session = require('../../services/protected_session');-const app_info = require('../../services/app_info');+const sourceIdService = require('../../services/source_id');+const passwordEncryptionService = require('../../services/password_encryption');+const protectedSessionService = require('../../services/protected_session');+const appInfo = require('../../services/app_info');async function loginSync(req) {const timestampStr = req.body.timestamp;@@ -20,8 +20,8 @@ async function loginSync(req) {const dbVersion = req.body.dbVersion;- if (dbVersion !== app_info.db_version) {- return [400, { message: 'Non-matching db versions, local is version ' + app_info.db_version }];+ if (dbVersion !== appInfo.db_version) {+ return [400, { message: 'Non-matching db versions, local is version ' + appInfo.db_version }];}const documentSecret = await options.getOption('document_secret');@@ -36,23 +36,23 @@ async function loginSync(req) {req.session.loggedIn = true;return {- sourceId: source_id.getCurrentSourceId()+ sourceId: sourceIdService.getCurrentSourceId()};}async function loginToProtectedSession(req) {const password = req.body.password;- if (!await password_encryption.verifyPassword(password)) {+ if (!await passwordEncryptionService.verifyPassword(password)) {return {success: false,message: "Given current password doesn't match hash"};}- const decryptedDataKey = await password_encryption.getDataKey(password);+ const decryptedDataKey = await passwordEncryptionService.getDataKey(password);- const protectedSessionId = protected_session.setDataKey(req, decryptedDataKey);+ const protectedSessionId = protectedSessionService.setDataKey(req, decryptedDataKey);return {success: true,diff --git a/src/routes/api/migration.js b/src/routes/api/migration.jsindex 5210e18d5b..a6c6b64036 100644--- a/src/routes/api/migration.js+++ b/src/routes/api/migration.js@@ -1,18 +1,18 @@"use strict";-const options = require('../../services/options');-const migration = require('../../services/migration');-const app_info = require('../../services/app_info');+const optionService = require('../../services/options');+const migrationService = require('../../services/migration');+const appInfo = require('../../services/app_info');async function getMigrationInfo() {return {- db_version: parseInt(await options.getOption('db_version')),- app_db_version: app_info.db_version+ db_version: parseInt(await optionService.getOption('db_version')),+ app_db_version: appInfo.db_version};}async function executeMigration() {- const migrations = await migration.migrate();+ const migrations = await migrationService.migrate();return {migrations: migrationsdiff --git a/src/routes/api/notes.js b/src/routes/api/notes.jsindex a77df96678..d73cc538f7 100644--- a/src/routes/api/notes.js+++ b/src/routes/api/notes.js@@ -1,7 +1,7 @@"use strict";-const notes = require('../../services/notes');-const tree = require('../../services/tree');+const noteService = require('../../services/notes');+const treeService = require('../../services/tree');const repository = require('../../services/repository');async function getNote(req) {@@ -24,7 +24,7 @@ async function createNote(req) {const parentNoteId = req.params.parentNoteId;const newNote = req.body;- const { note, branch } = await notes.createNewNote(parentNoteId, newNote, req);+ const { note, branch } = await noteService.createNewNote(parentNoteId, newNote, req);return {note,@@ -36,13 +36,13 @@ async function updateNote(req) {const note = req.body;const noteId = req.params.noteId;- await notes.updateNote(noteId, note);+ await noteService.updateNote(noteId, note);}async function sortNotes(req) {const noteId = req.params.noteId;- await tree.sortNotesAlphabetically(noteId);+ await treeService.sortNotesAlphabetically(noteId);}async function protectBranch(req) {@@ -50,7 +50,7 @@ async function protectBranch(req) {const note = repository.getNote(noteId);const protect = !!parseInt(req.params.isProtected);- await notes.protectNoteRecursively(note, protect);+ await noteService.protectNoteRecursively(note, protect);}async function setNoteTypeMime(req) {diff --git a/src/routes/api/options.js b/src/routes/api/options.jsindex 6ccaafe91b..2e3fefe93e 100644--- a/src/routes/api/options.js+++ b/src/routes/api/options.js@@ -1,7 +1,7 @@"use strict";const sql = require('../../services/sql');-const options = require('../../services/options');+const optionService = require('../../services/options');// options allowed to be updated directly in options dialogconst ALLOWED_OPTIONS = ['protected_session_timeout', 'note_revision_snapshot_time_interval'];@@ -20,7 +20,7 @@ async function updateOption(req) {return [400, "not allowed option to set"];}- await options.setOption(name, value);+ await optionService.setOption(name, value);}module.exports = {diff --git a/src/routes/api/recent_notes.js b/src/routes/api/recent_notes.jsindex 55ef25157b..f6dccf8aee 100644--- a/src/routes/api/recent_notes.js+++ b/src/routes/api/recent_notes.js@@ -2,7 +2,7 @@const repository = require('../../services/repository');const utils = require('../../services/utils');-const options = require('../../services/options');+const optionService = require('../../services/options');const RecentNote = require('../../entities/recent_note');async function getRecentNotes() {@@ -33,7 +33,7 @@ async function addRecentNote(req) {await recentNote.save();- await options.setOption('start_note_path', notePath);+ await optionService.setOption('start_note_path', notePath);return await getRecentNotes();}diff --git a/src/routes/api/script.js b/src/routes/api/script.jsindex 39a552410b..4759ad8bd6 100644--- a/src/routes/api/script.js+++ b/src/routes/api/script.js@@ -1,11 +1,11 @@"use strict";-const labels = require('../../services/labels');-const script = require('../../services/script');+const labelService = require('../../services/labels');+const scriptService = require('../../services/script');const repository = require('../../services/repository');async function exec(req) {- const result = await script.executeScript(req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId);+ const result = await scriptService.executeScript(req.body.script, req.body.params, req.body.startNoteId, req.body.currentNoteId);return { executionResult: result };}@@ -13,18 +13,18 @@ async function exec(req) {async function run(req) {const note = await repository.getNote(req.params.noteId);- const result = await script.executeNote(req, note);+ const result = await scriptService.executeNote(req, note);return { executionResult: result };}async function getStartupBundles(req) {- const notes = await labels.getNotesWithLabel("run", "frontend_startup");+ const notes = await labelService.getNotesWithLabel("run", "frontend_startup");const bundles = [];for (const note of notes) {- const bundle = await script.getScriptBundle(note);+ const bundle = await scriptService.getScriptBundle(note);if (bundle) {bundles.push(bundle);@@ -36,7 +36,7 @@ async function getStartupBundles(req) {async function getBundle(req) {const note = await repository.getNote(req.params.noteId);- return await script.getScriptBundle(note);+ return await scriptService.getScriptBundle(note);}module.exports = {diff --git a/src/routes/api/search.js b/src/routes/api/search.jsindex ded2af556e..da60d7edbb 100644--- a/src/routes/api/search.js+++ b/src/routes/api/search.js@@ -1,7 +1,7 @@"use strict";const sql = require('../../services/sql');-const notes = require('../../services/notes');+const noteService = require('../../services/notes');const parseFilters = require('../../services/parse_filters');const buildSearchQuery = require('../../services/build_search_query');@@ -20,7 +20,7 @@ async function saveSearchToNote(req) {searchString: req.params.searchString};- const noteId = await notes.createNote('root', 'Search note', noteContent, {+ const noteId = await noteService.createNote('root', 'Search note', noteContent, {json: true,type: 'search',mime: "application/json"diff --git a/src/routes/api/sender.js b/src/routes/api/sender.jsindex 5a709a75a5..313d51db31 100644--- a/src/routes/api/sender.js+++ b/src/routes/api/sender.js@@ -1,20 +1,20 @@"use strict";-const image = require('../../services/image');+const imageService = require('../../services/image');const utils = require('../../services/utils');-const date_notes = require('../../services/date_notes');+const dateNoteService = require('../../services/date_notes');const sql = require('../../services/sql');-const notes = require('../../services/notes');-const password_encryption = require('../../services/password_encryption');-const options = require('../../services/options');+const noteService = require('../../services/notes');+const passwordEncryptionService = require('../../services/password_encryption');+const optionService = require('../../services/options');const ApiToken = require('../../entities/api_token');async function login(req) {const username = req.body.username;const password = req.body.password;- const isUsernameValid = username === await options.getOption('username');- const isPasswordValid = await password_encryption.verifyPassword(password);+ const isUsernameValid = username === await optionService.getOption('username');+ const isPasswordValid = await passwordEncryptionService.verifyPassword(password);if (!isUsernameValid || !isPasswordValid) {return [401, "Incorrect username/password"];@@ -35,9 +35,9 @@ async function uploadImage(req) {return [400, "Unknown image type: " + file.mimetype];}- const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);+ const parentNoteId = await dateNoteService.getDateNoteId(req.headers['x-local-date']);- const {note} = await notes.createNewNote(parentNoteId, {+ const {note} = await noteService.createNewNote(parentNoteId, {title: "Sender image",content: "",target: 'into',@@ -46,7 +46,7 @@ async function uploadImage(req) {mime: 'text/html'});- const {fileName, imageId} = await image.saveImage(file, null, note.noteId);+ const {fileName, imageId} = await imageService.saveImage(file, null, note.noteId);const url = `/api/images/${imageId}/${fileName}`;@@ -56,9 +56,9 @@ async function uploadImage(req) {}async function saveNote(req) {- const parentNoteId = await date_notes.getDateNoteId(req.headers['x-local-date']);+ const parentNoteId = await dateNoteService.getDateNoteId(req.headers['x-local-date']);- await notes.createNewNote(parentNoteId, {+ await noteService.createNewNote(parentNoteId, {title: req.body.title,content: req.body.content,target: 'into',diff --git a/src/routes/api/setup.js b/src/routes/api/setup.jsindex d9bb665102..005e2e0644 100644--- a/src/routes/api/setup.js+++ b/src/routes/api/setup.js@@ -1,23 +1,23 @@"use strict";-const options = require('../../services/options');+const optionService = require('../../services/options');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 myScryptService = require('../../services/my_scrypt');+const passwordEncryptionService = require('../../services/password_encryption');async function setup(req) {const { username, password } = req.body;- await options.setOption('username', username);+ await optionService.setOption('username', username);- await options.setOption('password_verification_salt', utils.randomSecureToken(32));- await options.setOption('password_derived_key_salt', utils.randomSecureToken(32));+ await optionService.setOption('password_verification_salt', utils.randomSecureToken(32));+ await optionService.setOption('password_derived_key_salt', utils.randomSecureToken(32));- const passwordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(password));- await options.setOption('password_verification_hash', passwordVerificationKey);+ const passwordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(password));+ await optionService.setOption('password_verification_hash', passwordVerificationKey);- await password_encryption.setDataKey(password, utils.randomSecureToken(16));+ await passwordEncryptionService.setDataKey(password, utils.randomSecureToken(16));sql.setDbReadyAsResolved();}diff --git a/src/routes/api/sync.js b/src/routes/api/sync.jsindex e8651d41c0..10728ec9a0 100644--- a/src/routes/api/sync.js+++ b/src/routes/api/sync.js@@ -1,58 +1,58 @@"use strict";-const sync = require('../../services/sync');-const syncUpdate = require('../../services/sync_update');-const sync_table = require('../../services/sync_table');+const syncService = require('../../services/sync');+const syncUpdateService = require('../../services/sync_update');+const syncTableService = require('../../services/sync_table');const sql = require('../../services/sql');-const options = require('../../services/options');-const content_hash = require('../../services/content_hash');+const optionService = require('../../services/options');+const contentHashService = require('../../services/content_hash');const log = require('../../services/log');async function checkSync() {return {- 'hashes': await content_hash.getHashes(),+ 'hashes': await contentHashService.getHashes(),'max_sync_id': await sql.getValue('SELECT MAX(id) FROM sync')};}async function syncNow() {- return await sync.sync();+ return await syncService.sync();}async function fillSyncRows() {- await sync_table.fillAllSyncRows();+ await syncTableService.fillAllSyncRows();log.info("Sync rows have been filled.");}async function forceFullSync() {- await options.setOption('last_synced_pull', 0);- await options.setOption('last_synced_push', 0);+ await optionService.setOption('last_synced_pull', 0);+ await optionService.setOption('last_synced_push', 0);log.info("Forcing full sync.");// not awaiting for the job to finish (will probably take a long time)- sync.sync();+ syncService.sync();}async function forceNoteSync(req) {const noteId = req.params.noteId;- await sync_table.addNoteSync(noteId);+ await syncTableService.addNoteSync(noteId);for (const branchId of await sql.getColumn("SELECT branchId FROM branches WHERE isDeleted = 0 AND noteId = ?", [noteId])) {- await sync_table.addBranchSync(branchId);- await sync_table.addRecentNoteSync(branchId);+ await syncTableService.addBranchSync(branchId);+ await syncTableService.addRecentNoteSync(branchId);}for (const noteRevisionId of await sql.getColumn("SELECT noteRevisionId FROM note_revisions WHERE noteId = ?", [noteId])) {- await sync_table.addNoteRevisionSync(noteRevisionId);+ await syncTableService.addNoteRevisionSync(noteRevisionId);}log.info("Forcing note sync for " + noteId);// not awaiting for the job to finish (will probably take a long time)- sync.sync();+ syncService.sync();}async function getChanged() {@@ -65,7 +65,7 @@ async function getNote(req) {const noteId = req.params.noteId;const entity = await sql.getRow("SELECT * FROM notes WHERE noteId = ?", [noteId]);- sync.serializeNoteContentBuffer(entity);+ syncService.serializeNoteContentBuffer(entity);return {entity: entity@@ -141,43 +141,43 @@ async function getApiToken(req) {}async function updateNote(req) {- await syncUpdate.updateNote(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateNote(req.body.entity, req.body.sourceId);}async function updateBranch(req) {- await syncUpdate.updateBranch(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateBranch(req.body.entity, req.body.sourceId);}async function updateNoteRevision(req) {- await syncUpdate.updateNoteRevision(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateNoteRevision(req.body.entity, req.body.sourceId);}async function updateNoteReordering(req) {- await syncUpdate.updateNoteReordering(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateNoteReordering(req.body.entity, req.body.sourceId);}async function updateOption(req) {- await syncUpdate.updateOptions(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateOptions(req.body.entity, req.body.sourceId);}async function updateRecentNote(req) {- await syncUpdate.updateRecentNotes(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateRecentNotes(req.body.entity, req.body.sourceId);}async function updateImage(req) {- await syncUpdate.updateImage(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateImage(req.body.entity, req.body.sourceId);}async function updateNoteImage(req) {- await syncUpdate.updateNoteImage(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateNoteImage(req.body.entity, req.body.sourceId);}async function updateLabel(req) {- await syncUpdate.updateLabel(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateLabel(req.body.entity, req.body.sourceId);}async function updateApiToken(req) {- await syncUpdate.updateApiToken(req.body.entity, req.body.sourceId);+ await syncUpdateService.updateApiToken(req.body.entity, req.body.sourceId);}module.exports = {diff --git a/src/routes/api/tree.js b/src/routes/api/tree.jsindex 6f3398b8c7..b5eb01e9d0 100644--- a/src/routes/api/tree.js+++ b/src/routes/api/tree.js@@ -1,11 +1,9 @@"use strict";const sql = require('../../services/sql');-const options = require('../../services/options');-const utils = require('../../services/utils');+const optionService = require('../../services/options');const config = require('../../services/config');-const protected_session = require('../../services/protected_session');-const repository = require('../../services/repository');+const protectedSessionService = require('../../services/protected_session');async function getTree() {const branches = await sql.getRows(`@@ -45,7 +43,7 @@ async function getTree() {WHEREnotes.isDeleted = 0`));- protected_session.decryptNotes(notes);+ protectedSessionService.decryptNotes(notes);notes.forEach(note => {note.hideInAutocomplete = !!note.hideInAutocomplete;@@ -56,7 +54,7 @@ async function getTree() {instanceName: config.General ? config.General.instanceName : null,branches: branches,notes: notes,- start_note_path: await options.getOption('start_note_path')+ start_note_path: await optionService.getOption('start_note_path')};}diff --git a/src/routes/index.js b/src/routes/index.jsindex 33564a0994..373819a192 100644--- a/src/routes/index.js+++ b/src/routes/index.js@@ -1,13 +1,12 @@"use strict";-const source_id = require('../services/source_id');+const sourceIdService = require('../services/source_id');const sql = require('../services/sql');-const repository = require('../services/repository');-const labels = require('../services/labels');+const labelService = require('../services/labels');async function index(req, res) {res.render('index', {- sourceId: await source_id.generateSourceId(),+ sourceId: await sourceIdService.generateSourceId(),maxSyncIdAtLoad: await sql.getValue("SELECT MAX(id) FROM sync"),appCss: await getAppCss()});@@ -15,7 +14,7 @@ async function index(req, res) {async function getAppCss() {let css = '';- const notes = labels.getNotesWithLabel('app_css');+ const notes = labelService.getNotesWithLabel('app_css');for (const note of await notes) {css += `/* ${note.noteId} */diff --git a/src/routes/login.js b/src/routes/login.jsindex 7c38b56237..27425c68bc 100644--- a/src/routes/login.js+++ b/src/routes/login.js@@ -1,15 +1,15 @@"use strict";const utils = require('../services/utils');-const options = require('../services/options');-const my_scrypt = require('../services/my_scrypt');+const optionService = require('../services/options');+const myScryptService = require('../services/my_scrypt');function loginPage(req, res) {res.render('login', { failedAuth: false });}async function login(req, res) {- const userName = await options.getOption('username');+ const userName = await optionService.getOption('username');const guessedPassword = req.body.password;@@ -33,9 +33,9 @@ async function login(req, res) {}async function verifyPassword(guessedPassword) {- const hashed_password = utils.fromBase64(await options.getOption('password_verification_hash'));+ const hashed_password = utils.fromBase64(await optionService.getOption('password_verification_hash'));- const guess_hashed = await my_scrypt.getVerificationHash(guessedPassword);+ const guess_hashed = await myScryptService.getVerificationHash(guessedPassword);return guess_hashed.equals(hashed_password);}diff --git a/src/services/anonymization.js b/src/services/anonymization.jsindex 304f73c978..37f0dd80a8 100644--- a/src/services/anonymization.js+++ b/src/services/anonymization.js@@ -1,18 +1,18 @@"use strict";-const data_dir = require('./data_dir');+const dataDir = require('./data_dir');const utils = require('./utils');const fs = require('fs-extra');const sqlite = require('sqlite');async function anonymize() {- if (!fs.existsSync(data_dir.ANONYMIZED_DB_DIR)) {- fs.mkdirSync(data_dir.ANONYMIZED_DB_DIR, 0o700);+ if (!fs.existsSync(dataDir.ANONYMIZED_DB_DIR)) {+ fs.mkdirSync(dataDir.ANONYMIZED_DB_DIR, 0o700);}- const anonymizedFile = data_dir.ANONYMIZED_DB_DIR + "/" + "backup-" + utils.getDateTimeForFile() + ".db";+ const anonymizedFile = dataDir.ANONYMIZED_DB_DIR + "/" + "backup-" + utils.getDateTimeForFile() + ".db";- fs.copySync(data_dir.DOCUMENT_PATH, anonymizedFile);+ fs.copySync(dataDir.DOCUMENT_PATH, anonymizedFile);const db = await sqlite.open(anonymizedFile, {Promise});diff --git a/src/services/auth.js b/src/services/auth.jsindex 0cb574ee1f..445f464cfd 100644--- a/src/services/auth.js+++ b/src/services/auth.js@@ -1,6 +1,6 @@"use strict";-const migration = require('./migration');+const migrationService = require('./migration');const sql = require('./sql');const utils = require('./utils');diff --git a/src/services/backup.js b/src/services/backup.jsindex 82dd746541..e4db022140 100644--- a/src/services/backup.js+++ b/src/services/backup.js@@ -1,17 +1,17 @@"use strict";const utils = require('./utils');-const options = require('./options');+const optionService = require('./options');const fs = require('fs-extra');const dataDir = require('./data_dir');const log = require('./log');const sql = require('./sql');-const sync_mutex = require('./sync_mutex');+const syncMutexService = require('./sync_mutex');const cls = require('./cls');async function regularBackup() {const now = new Date();- const lastBackupDate = utils.parseDateTime(await options.getOption('last_backup_date'));+ const lastBackupDate = utils.parseDateTime(await optionService.getOption('last_backup_date'));console.log(lastBackupDate);@@ -25,7 +25,7 @@ async function regularBackup() {async function backupNow() {// we don't want to backup DB in the middle of sync with potentially inconsistent DB state- await sync_mutex.doExclusively(async () => {+ await syncMutexService.doExclusively(async () => {const now = utils.nowDate();const backupFile = dataDir.BACKUP_DIR + "/" + "backup-" + utils.getDateTimeForFile() + ".db";@@ -34,7 +34,7 @@ async function backupNow() {log.info("Created backup at " + backupFile);- await options.setOption('last_backup_date', now);+ await optionService.setOption('last_backup_date', now);});}diff --git a/src/services/change_password.js b/src/services/change_password.jsindex 0669a0e40f..9aa4c6950b 100644--- a/src/services/change_password.js+++ b/src/services/change_password.js@@ -1,26 +1,26 @@"use strict";const sql = require('./sql');-const options = require('./options');-const my_scrypt = require('./my_scrypt');+const optionService = require('./options');+const myScryptService = require('./my_scrypt');const utils = require('./utils');-const password_encryption = require('./password_encryption');+const passwordEncryptionService = require('./password_encryption');async function changePassword(currentPassword, newPassword) {- if (!await password_encryption.verifyPassword(currentPassword)) {+ if (!await passwordEncryptionService.verifyPassword(currentPassword)) {return {success: false,message: "Given current password doesn't match hash"};}- const newPasswordVerificationKey = utils.toBase64(await my_scrypt.getVerificationHash(newPassword));- const decryptedDataKey = await password_encryption.getDataKey(currentPassword);+ const newPasswordVerificationKey = utils.toBase64(await myScryptService.getVerificationHash(newPassword));+ const decryptedDataKey = await passwordEncryptionService.getDataKey(currentPassword);await sql.doInTransaction(async () => {- await password_encryption.setDataKey(newPassword, decryptedDataKey);+ await passwordEncryptionService.setDataKey(newPassword, decryptedDataKey);- await options.setOption('password_verification_hash', newPasswordVerificationKey);+ await optionService.setOption('password_verification_hash', newPasswordVerificationKey);});return {diff --git a/src/services/config.js b/src/services/config.jsindex 29f5d83bbe..ffa957e5b0 100644--- a/src/services/config.js+++ b/src/services/config.js@@ -4,9 +4,9 @@ const ini = require('ini');const fs = require('fs');const dataDir = require('./data_dir');const path = require('path');-const resource_dir = require('./resource_dir');+const resourceDir = require('./resource_dir');-const configSampleFilePath = path.resolve(resource_dir.RESOURCE_DIR, "config-sample.ini");+const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini");const configFilePath = dataDir.TRILIUM_DATA_DIR + '/config.ini';diff --git a/src/services/consistency_checks.js b/src/services/consistency_checks.jsindex 666a20a8d3..58aafd6099 100644--- a/src/services/consistency_checks.js+++ b/src/services/consistency_checks.js@@ -2,8 +2,8 @@const sql = require('./sql');const log = require('./log');-const messaging = require('./messaging');-const sync_mutex = require('./sync_mutex');+const messagingService = require('./messaging');+const syncMutexService = require('./sync_mutex');const utils = require('./utils');const cls = require('./cls');@@ -250,7 +250,7 @@ async function runChecks() {let errorList;let elapsedTimeMs;- await sync_mutex.doExclusively(async () => {+ await syncMutexService.doExclusively(async () => {const startTime = new Date();errorList = await runAllChecks();@@ -261,7 +261,7 @@ async function runChecks() {if (errorList.length > 0) {log.info(`Consistency checks failed (took ${elapsedTimeMs}ms) with these errors: ` + JSON.stringify(errorList));- messaging.sendMessageToAllClients({type: 'consistency-checks-failed'});+ messagingService.sendMessageToAllClients({type: 'consistency-checks-failed'});}else {log.info(`All consistency checks passed (took ${elapsedTimeMs}ms)`);diff --git a/src/services/content_hash.js b/src/services/content_hash.jsindex 3a400a909f..a9b114875b 100644--- a/src/services/content_hash.js+++ b/src/services/content_hash.js@@ -1,6 +1,5 @@const sql = require('./sql');const utils = require('./utils');-const options = require('./options');const log = require('./log');function getHash(rows) {diff --git a/src/services/date_notes.js b/src/services/date_notes.jsindex 26effc8b61..8b04eddb1a 100644--- a/src/services/date_notes.js+++ b/src/services/date_notes.js@@ -1,8 +1,8 @@"use strict";const sql = require('./sql');-const notes = require('./notes');-const labels = require('./labels');+const noteService = require('./notes');+const labelService = require('./labels');const utils = require('./utils');const CALENDAR_ROOT_LABEL = 'calendar_root';@@ -14,7 +14,7 @@ const DAYS = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturconst MONTHS = ['January','February','March','April','May','June','July','August','September','October','November','December'];async function createNote(parentNoteId, noteTitle, noteText) {- const {note} = await notes.createNewNote(parentNoteId, {+ const {note} = await noteService.createNewNote(parentNoteId, {title: noteTitle,content: noteText,target: 'into',@@ -36,7 +36,7 @@ async function getRootCalendarNoteId() {WHERE labels.name = '${CALENDAR_ROOT_LABEL}' AND notes.isDeleted = 0`);if (!rootNoteId) {- const {rootNote} = await notes.createNewNote('root', {+ const {rootNote} = await noteService.createNewNote('root', {title: 'Calendar',target: 'into',isProtected: false@@ -44,7 +44,7 @@ async function getRootCalendarNoteId() {const rootNoteId = rootNote.noteId;- await labels.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);+ await labelService.createLabel(rootNoteId, CALENDAR_ROOT_LABEL);}return rootNoteId;@@ -53,7 +53,7 @@ async function getRootCalendarNoteId() {async function getYearNoteId(dateTimeStr, rootNoteId) {const yearStr = dateTimeStr.substr(0, 4);- let yearNoteId = await labels.getNoteIdWithLabel(YEAR_LABEL, yearStr);+ let yearNoteId = await labelService.getNoteIdWithLabel(YEAR_LABEL, yearStr);if (!yearNoteId) {yearNoteId = await getNoteStartingWith(rootNoteId, yearStr);@@ -62,7 +62,7 @@ async function getYearNoteId(dateTimeStr, rootNoteId) {yearNoteId = await createNote(rootNoteId, yearStr);}- await labels.createLabel(yearNoteId, YEAR_LABEL, yearStr);+ await labelService.createLabel(yearNoteId, YEAR_LABEL, yearStr);}return yearNoteId;@@ -72,7 +72,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {const monthStr = dateTimeStr.substr(0, 7);const monthNumber = dateTimeStr.substr(5, 2);- let monthNoteId = await labels.getNoteIdWithLabel(MONTH_LABEL, monthStr);+ let monthNoteId = await labelService.getNoteIdWithLabel(MONTH_LABEL, monthStr);if (!monthNoteId) {const yearNoteId = await getYearNoteId(dateTimeStr, rootNoteId);@@ -87,7 +87,7 @@ async function getMonthNoteId(dateTimeStr, rootNoteId) {monthNoteId = await createNote(yearNoteId, noteTitle);}- await labels.createLabel(monthNoteId, MONTH_LABEL, monthStr);+ await labelService.createLabel(monthNoteId, MONTH_LABEL, monthStr);}return monthNoteId;@@ -101,7 +101,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {const dateStr = dateTimeStr.substr(0, 10);const dayNumber = dateTimeStr.substr(8, 2);- let dateNoteId = await labels.getNoteIdWithLabel(DATE_LABEL, dateStr);+ let dateNoteId = await labelService.getNoteIdWithLabel(DATE_LABEL, dateStr);if (!dateNoteId) {const monthNoteId = await getMonthNoteId(dateTimeStr, rootNoteId);@@ -116,7 +116,7 @@ async function getDateNoteId(dateTimeStr, rootNoteId = null) {dateNoteId = await createNote(monthNoteId, noteTitle);}- await labels.createLabel(dateNoteId, DATE_LABEL, dateStr);+ await labelService.createLabel(dateNoteId, DATE_LABEL, dateStr);}return dateNoteId;diff --git a/src/services/labels.js b/src/services/labels.jsindex 407e94e657..1a15a03060 100644--- a/src/services/labels.js+++ b/src/services/labels.js@@ -1,7 +1,6 @@"use strict";const sql = require('./sql');-const utils = require('./utils');const repository = require('./repository');const Label = require('../entities/label');diff --git a/src/services/log.js b/src/services/log.jsindex ee423d1158..4d0abc21e6 100644--- a/src/services/log.js+++ b/src/services/log.js@@ -1,15 +1,15 @@"use strict";const fs = require('fs');-const data_dir = require('./data_dir');+const dataDir = require('./data_dir');-if (!fs.existsSync(data_dir.LOG_DIR)) {- fs.mkdirSync(data_dir.LOG_DIR, 0o700);+if (!fs.existsSync(dataDir.LOG_DIR)) {+ fs.mkdirSync(dataDir.LOG_DIR, 0o700);}const logger = require('simple-node-logger').createRollingFileLogger({errorEventName: 'error',- logDirectory: data_dir.LOG_DIR,+ logDirectory: dataDir.LOG_DIR,fileNamePattern: 'trilium-<DATE>.log',dateFormat:'YYYY-MM-DD'});@@ -37,7 +37,7 @@ function request(req) {logger.info(req.method + " " + req.url);}-info("Using data dir: " + data_dir.TRILIUM_DATA_DIR);+info("Using data dir: " + dataDir.TRILIUM_DATA_DIR);module.exports = {info,diff --git a/src/services/messaging.js b/src/services/messaging.jsindex 21fc5dc4c5..8e5e8168b4 100644--- a/src/services/messaging.js+++ b/src/services/messaging.js@@ -2,8 +2,8 @@ const WebSocket = require('ws');const utils = require('./utils');const log = require('./log');const sql = require('./sql');-const options = require('./options');-const sync_setup = require('./sync_setup');+const optionService = require('./options');+const syncSetup = require('./sync_setup');let webSocketServer;@@ -66,14 +66,14 @@ async function sendMessageToAllClients(message) {async function sendPing(client, lastSentSyncId) {const syncData = await sql.getRows("SELECT * FROM sync WHERE id > ?", [lastSentSyncId]);- const lastSyncedPush = await options.getOption('last_synced_push');+ const lastSyncedPush = await optionService.getOption('last_synced_push');const changesToPushCount = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]);await sendMessage(client, {type: 'sync',data: syncData,- changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0+ changesToPushCount: syncSetup.isSyncSetup ? changesToPushCount : 0});}diff --git a/src/services/migration.js b/src/services/migration.jsindex 22faebe4c1..207347a165 100644--- a/src/services/migration.js+++ b/src/services/migration.js@@ -1,19 +1,19 @@-const backup = require('./backup');+const backupService = require('./backup');const sql = require('./sql');-const options = require('./options');+const optionService = require('./options');const fs = require('fs-extra');const log = require('./log');-const resource_dir = require('./resource_dir');+const resourceDir = require('./resource_dir');async function migrate() {const migrations = [];// backup before attempting migration- await backup.backupNow();+ await backupService.backupNow();- const currentDbVersion = parseInt(await options.getOption('db_version'));+ const currentDbVersion = parseInt(await optionService.getOption('db_version'));- fs.readdirSync(resource_dir.MIGRATIONS_DIR).forEach(file => {+ fs.readdirSync(resourceDir.MIGRATIONS_DIR).forEach(file => {const match = file.match(/([0-9]{4})__([a-zA-Z0-9_ ]+)\.(sql|js)/);if (match) {@@ -46,7 +46,7 @@ async function migrate() {await sql.doInTransaction(async () => {if (mig.type === 'sql') {- const migrationSql = fs.readFileSync(resource_dir.MIGRATIONS_DIR + "/" + mig.file).toString('utf8');+ const migrationSql = fs.readFileSync(resourceDir.MIGRATIONS_DIR + "/" + mig.file).toString('utf8');console.log("Migration with SQL script: " + migrationSql);@@ -55,14 +55,14 @@ async function migrate() {else if (mig.type === 'js') {console.log("Migration with JS module");- const migrationModule = require("../" + resource_dir.MIGRATIONS_DIR + "/" + mig.file);+ const migrationModule = require("../" + resourceDir.MIGRATIONS_DIR + "/" + mig.file);await migrationModule(db);}else {throw new Error("Unknown migration type " + mig.type);}- await options.setOption("db_version", mig.dbVersion);+ await optionService.setOption("db_version", mig.dbVersion);});diff --git a/src/services/my_scrypt.js b/src/services/my_scrypt.jsindex 96de4e252b..44f311259e 100644--- a/src/services/my_scrypt.js+++ b/src/services/my_scrypt.js@@ -1,16 +1,16 @@"use strict";-const options = require('./options');+const optionService = require('./options');const scrypt = require('scrypt');async function getVerificationHash(password) {- const salt = await options.getOption('password_verification_salt');+ const salt = await optionService.getOption('password_verification_salt');return getScryptHash(password, salt);}async function getPasswordDerivedKey(password) {- const salt = await options.getOption('password_derived_key_salt');+ const salt = await optionService.getOption('password_derived_key_salt');return getScryptHash(password, salt);}diff --git a/src/services/notes.js b/src/services/notes.jsindex 856e10d41c..e49150ef83 100644--- a/src/services/notes.js+++ b/src/services/notes.js@@ -1,8 +1,8 @@const sql = require('./sql');-const options = require('./options');+const optionService = require('./options');const utils = require('./utils');-const sync_table = require('./sync_table');-const labels = require('./labels');+const syncTableService = require('./sync_table');+const labelService = require('./labels');const repository = require('./repository');const Note = require('../entities/note');const NoteImage = require('../entities/note_image');@@ -26,7 +26,7 @@ async function getNewNotePosition(parentNoteId, noteData) {await sql.execute('UPDATE branches SET notePosition = notePosition + 1 WHERE parentNoteId = ? AND notePosition > ? AND isDeleted = 0',[parentNoteId, afterNote.notePosition]);- await sync_table.addNoteReorderingSync(parentNoteId);+ await syncTableService.addNoteReorderingSync(parentNoteId);}else {throw new Error('Unknown target: ' + noteData.target);@@ -91,7 +91,7 @@ async function createNote(parentNoteId, title, content = "", extraOptions = {})if (extraOptions.labels) {for (const labelName in extraOptions.labels) {- await labels.createLabel(note.noteId, labelName, extraOptions.labels[labelName]);+ await labelService.createLabel(note.noteId, labelName, extraOptions.labels[labelName]);}}@@ -165,7 +165,7 @@ async function saveNoteRevision(note) {const labelsMap = await note.getLabelMap();const now = new Date();- const noteRevisionSnapshotTimeInterval = parseInt(await options.getOption('note_revision_snapshot_time_interval'));+ const noteRevisionSnapshotTimeInterval = parseInt(await optionService.getOption('note_revision_snapshot_time_interval'));const revisionCutoff = utils.dateStr(new Date(now.getTime() - noteRevisionSnapshotTimeInterval * 1000));diff --git a/src/services/options.js b/src/services/options.jsindex 112e5ccc57..e661dfc543 100644--- a/src/services/options.js+++ b/src/services/options.js@@ -1,7 +1,7 @@const sql = require('./sql');const utils = require('./utils');-const sync_table = require('./sync_table');-const app_info = require('./app_info');+const syncTableService = require('./sync_table');+const appInfo = require('./app_info');async function getOptionOrNull(name) {return await sql.getRowOrNull("SELECT value FROM options WHERE name = ?", [name]);@@ -25,7 +25,7 @@ async function setOption(name, value) {}if (opt.isSynced) {- await sync_table.addOptionsSync(name);+ await syncTableService.addOptionsSync(name);}await sql.execute("UPDATE options SET value = ?, dateModified = ? WHERE name = ?",@@ -41,7 +41,7 @@ async function createOption(name, value, isSynced) {});if (isSynced) {- await sync_table.addOptionsSync(name);+ await syncTableService.addOptionsSync(name);}}@@ -60,9 +60,9 @@ async function initOptions(startNotePath) {await createOption('protected_session_timeout', 600, true);await createOption('note_revision_snapshot_time_interval', 600, true);await createOption('last_backup_date', utils.nowDate(), false);- await createOption('db_version', app_info.db_version, false);+ await createOption('db_version', appInfo.db_version, false);- await createOption('last_synced_pull', app_info.db_version, false);+ await createOption('last_synced_pull', appInfo.db_version, false);await createOption('last_synced_push', 0, false);}diff --git a/src/services/password_encryption.js b/src/services/password_encryption.jsindex 7c617bac3f..2ebe8e3264 100644--- a/src/services/password_encryption.js+++ b/src/services/password_encryption.js@@ -1,37 +1,37 @@-const options = require('./options');-const my_scrypt = require('./my_scrypt');+const optionService = require('./options');+const myScryptService = require('./my_scrypt');const utils = require('./utils');-const data_encryption = require('./data_encryption');+const dataEncryptionService = require('./data_encryption');async function verifyPassword(password) {- const givenPasswordHash = utils.toBase64(await my_scrypt.getVerificationHash(password));+ const givenPasswordHash = utils.toBase64(await myScryptService.getVerificationHash(password));- const dbPasswordHash = await options.getOption('password_verification_hash');+ const dbPasswordHash = await optionService.getOption('password_verification_hash');return givenPasswordHash === dbPasswordHash;}async function setDataKey(password, plainTextDataKey) {- const passwordDerivedKey = await my_scrypt.getPasswordDerivedKey(password);+ const passwordDerivedKey = await myScryptService.getPasswordDerivedKey(password);const encryptedDataKeyIv = utils.randomString(16);- await options.setOption('encrypted_data_key_iv', encryptedDataKeyIv);+ await optionService.setOption('encrypted_data_key_iv', encryptedDataKeyIv);const buffer = Buffer.from(plainTextDataKey);- const newEncryptedDataKey = data_encryption.encrypt(passwordDerivedKey, encryptedDataKeyIv, buffer);+ const newEncryptedDataKey = dataEncryptionService.encrypt(passwordDerivedKey, encryptedDataKeyIv, buffer);- await options.setOption('encrypted_data_key', newEncryptedDataKey);+ await optionService.setOption('encrypted_data_key', newEncryptedDataKey);}async function getDataKey(password) {- const passwordDerivedKey = await my_scrypt.getPasswordDerivedKey(password);+ const passwordDerivedKey = await myScryptService.getPasswordDerivedKey(password);- const encryptedDataKeyIv = await options.getOption('encrypted_data_key_iv');- const encryptedDataKey = await options.getOption('encrypted_data_key');+ const encryptedDataKeyIv = await optionService.getOption('encrypted_data_key_iv');+ const encryptedDataKey = await optionService.getOption('encrypted_data_key');- const decryptedDataKey = data_encryption.decrypt(passwordDerivedKey, encryptedDataKeyIv, encryptedDataKey);+ const decryptedDataKey = dataEncryptionService.decrypt(passwordDerivedKey, encryptedDataKeyIv, encryptedDataKey);return decryptedDataKey;}diff --git a/src/services/protected_session.js b/src/services/protected_session.jsindex c0fff7f0b0..58fc777753 100644--- a/src/services/protected_session.js+++ b/src/services/protected_session.js@@ -1,10 +1,11 @@"use strict";const utils = require('./utils');-const data_encryption = require('./data_encryption');-const dataKeyMap = {};+const dataEncryptionService = require('./data_encryption');const cls = require('./cls');+const dataKeyMap = {};+function setDataKey(req, decryptedDataKey) {const protectedSessionId = utils.randomSecureToken(32);@@ -41,17 +42,17 @@ function decryptNote(note) {}if (note.title) {- note.title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(note.noteId), note.title);+ note.title = dataEncryptionService.decryptString(dataKey, dataEncryptionService.noteTitleIv(note.noteId), note.title);}if (note.content) {- const contentIv = data_encryption.noteContentIv(note.noteId);+ const contentIv = dataEncryptionService.noteContentIv(note.noteId);if (note.type === 'file') {- note.content = data_encryption.decrypt(dataKey, contentIv, note.content);+ note.content = dataEncryptionService.decrypt(dataKey, contentIv, note.content);}else {- note.content = data_encryption.decryptString(dataKey, contentIv, note.content);+ note.content = dataEncryptionService.decryptString(dataKey, contentIv, note.content);}}}@@ -72,26 +73,26 @@ function decryptNoteRevision(hist) {}if (hist.title) {- hist.title = data_encryption.decryptString(dataKey, data_encryption.noteTitleIv(hist.noteRevisionId), hist.title);+ hist.title = dataEncryptionService.decryptString(dataKey, dataEncryptionService.noteTitleIv(hist.noteRevisionId), hist.title);}if (hist.content) {- hist.content = data_encryption.decryptString(dataKey, data_encryption.noteContentIv(hist.noteRevisionId), hist.content);+ hist.content = dataEncryptionService.decryptString(dataKey, dataEncryptionService.noteContentIv(hist.noteRevisionId), hist.content);}}function encryptNote(note) {const dataKey = getDataKey();- note.title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(note.noteId), note.title);- note.content = data_encryption.encrypt(dataKey, data_encryption.noteContentIv(note.noteId), note.content);+ note.title = dataEncryptionService.encrypt(dataKey, dataEncryptionService.noteTitleIv(note.noteId), note.title);+ note.content = dataEncryptionService.encrypt(dataKey, dataEncryptionService.noteContentIv(note.noteId), note.content);}function encryptNoteRevision(revision) {const dataKey = getDataKey();- revision.title = data_encryption.encrypt(dataKey, data_encryption.noteTitleIv(revision.noteRevisionId), revision.title);- revision.content = data_encryption.encrypt(dataKey, data_encryption.noteContentIv(revision.noteRevisionId), revision.content);+ revision.title = dataEncryptionService.encrypt(dataKey, dataEncryptionService.noteTitleIv(revision.noteRevisionId), revision.title);+ revision.content = dataEncryptionService.encrypt(dataKey, dataEncryptionService.noteContentIv(revision.noteRevisionId), revision.content);}module.exports = {diff --git a/src/services/repository.js b/src/services/repository.jsindex 504e6c6488..dea33e70ec 100644--- a/src/services/repository.js+++ b/src/services/repository.js@@ -1,7 +1,7 @@"use strict";const sql = require('./sql');-const sync_table = require('../services/sync_table');+const syncTableService = require('../services/sync_table');let entityConstructor;@@ -55,7 +55,7 @@ async function updateEntity(entity) {const primaryKey = entity[entity.constructor.primaryKeyName];- await sync_table.addEntitySync(entity.constructor.tableName, primaryKey);+ await syncTableService.addEntitySync(entity.constructor.tableName, primaryKey);});}diff --git a/src/services/scheduler.js b/src/services/scheduler.jsindex 72ff18a3b2..98fd4be1b9 100644--- a/src/services/scheduler.js+++ b/src/services/scheduler.js@@ -1,4 +1,4 @@-const script = require('./script');+const scriptService = require('./script');const repository = require('./repository');const cls = require('./cls');@@ -15,7 +15,7 @@ async function runNotesWithLabel(runAttrValue) {AND notes.isDeleted = 0`, [runAttrValue]);for (const note of notes) {- script.executeNote(note);+ scriptService.executeNote(note);}}diff --git a/src/services/script_context.js b/src/services/script_context.jsindex cf38410b23..aa3f398c1c 100644--- a/src/services/script_context.js+++ b/src/services/script_context.js@@ -1,9 +1,9 @@const log = require('./log');-const notes = require('./notes');+const noteService = require('./notes');const sql = require('./sql');const utils = require('./utils');-const labels = require('./labels');-const date_notes = require('./date_notes');+const labelService = require('./labels');+const dateNoteService = require('./date_notes');const config = require('./config');const repository = require('./repository');const axios = require('axios');@@ -48,7 +48,7 @@ function ScriptApi(startNote, currentNote) {this.getEntities = repository.getEntities;this.getNotesWithLabel = async function (labelName, labelValue) {- return await labels.getNotesWithLabel(labelName, labelValue);+ return await labelService.getNotesWithLabel(labelName, labelValue);};this.getNoteWithLabel = async function (labelName, labelValue) {@@ -58,15 +58,15 @@ function ScriptApi(startNote, currentNote) {};this.createNote = async function(parentNoteId, title, content = "", extraOptions = {}) {- return await notes.createNote(parentNoteId, title, content, extraOptions);+ return await noteService.createNote(parentNoteId, title, content, extraOptions);};- this.createLabel = labels.createLabel;+ this.createLabel = labelService.createLabel;this.log = message => log.info(`Script ${currentNote.noteId}: ${message}`);- this.getRootCalendarNoteId = date_notes.getRootCalendarNoteId;- this.getDateNoteId = date_notes.getDateNoteId;+ this.getRootCalendarNoteId = dateNoteService.getRootCalendarNoteId;+ this.getDateNoteId = dateNoteService.getDateNoteId;this.transaction = sql.doInTransaction;}diff --git a/src/services/sql.js b/src/services/sql.jsindex a85044feb1..67b6662e40 100644--- a/src/services/sql.js+++ b/src/services/sql.js@@ -4,8 +4,8 @@ const log = require('./log');const dataDir = require('./data_dir');const fs = require('fs');const sqlite = require('sqlite');-const app_info = require('./app_info');-const resource_dir = require('./resource_dir');+const appInfo = require('./app_info');+const resourceDir = require('./resource_dir');const cls = require('./cls');async function createConnection() {@@ -29,11 +29,11 @@ const dbReady = new Promise((resolve, reject) => {if (tableResults.length !== 1) {log.info("Connected to db, but schema doesn't exist. Initializing schema ...");- const schema = fs.readFileSync(resource_dir.DB_INIT_DIR + '/schema.sql', 'UTF-8');- const notesSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_notes.sql', 'UTF-8');- const notesTreeSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_branches.sql', 'UTF-8');- const imagesSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_images.sql', 'UTF-8');- const notesImageSql = fs.readFileSync(resource_dir.DB_INIT_DIR + '/main_note_images.sql', 'UTF-8');+ const schema = fs.readFileSync(resourceDir.DB_INIT_DIR + '/schema.sql', 'UTF-8');+ const notesSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_notes.sql', 'UTF-8');+ const notesTreeSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_branches.sql', 'UTF-8');+ const imagesSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_images.sql', 'UTF-8');+ const notesImageSql = fs.readFileSync(resourceDir.DB_INIT_DIR + '/main_note_images.sql', 'UTF-8');await doInTransaction(async () => {await executeScript(schema);@@ -241,10 +241,10 @@ async function doInTransaction(func) {async function isDbUpToDate() {const dbVersion = parseInt(await getValue("SELECT value FROM options WHERE name = 'db_version'"));- const upToDate = dbVersion >= app_info.db_version;+ const upToDate = dbVersion >= appInfo.db_version;if (!upToDate) {- log.info("App db version is " + app_info.db_version + ", while db version is " + dbVersion + ". Migration needed.");+ log.info("App db version is " + appInfo.db_version + ", while db version is " + dbVersion + ". Migration needed.");}return upToDate;diff --git a/src/services/sync.js b/src/services/sync.jsindex 239beb3674..e2f3d42bfc 100644--- a/src/services/sync.js+++ b/src/services/sync.js@@ -3,18 +3,18 @@const log = require('./log');const rp = require('request-promise');const sql = require('./sql');-const options = require('./options');+const optionService = require('./options');const utils = require('./utils');-const source_id = require('./source_id');-const notes = require('./notes');-const syncUpdate = require('./sync_update');-const content_hash = require('./content_hash');-const event_log = require('./event_log');+const sourceIdService = require('./source_id');+const noteService = require('./notes');+const syncUpdateService = require('./sync_update');+const contentHashService = require('./content_hash');+const eventLogService = require('./event_log');const fs = require('fs');-const app_info = require('./app_info');-const messaging = require('./messaging');-const sync_setup = require('./sync_setup');-const sync_mutex = require('./sync_mutex');+const appInfo = require('./app_info');+const messagingService = require('./messaging');+const syncSetup = require('./sync_setup');+const syncMutexService = require('./sync_mutex');const cls = require('./cls');let proxyToggle = true;@@ -22,7 +22,7 @@ let syncServerCertificate = null;async function sync() {try {- await sync_mutex.doExclusively(async () => {+ await syncMutexService.doExclusively(async () => {if (!await sql.isDbUpToDate()) {return {success: false,@@ -70,18 +70,18 @@ async function sync() {async function login() {const timestamp = utils.nowDate();- const documentSecret = await options.getOption('document_secret');+ const documentSecret = await optionService.getOption('document_secret');const hash = utils.hmac(documentSecret, timestamp);const syncContext = { cookieJar: rp.jar() };const resp = await syncRequest(syncContext, 'POST', '/api/login/sync', {timestamp: timestamp,- dbVersion: app_info.db_version,+ dbVersion: appInfo.db_version,hash: hash});- if (source_id.isLocalSourceId(resp.sourceId)) {+ if (sourceIdService.isLocalSourceId(resp.sourceId)) {throw new Error(`Sync server has source ID ${resp.sourceId} which is also local. Try restarting sync server.`);}@@ -91,11 +91,11 @@ async function login() {}async function getLastSyncedPull() {- return parseInt(await options.getOption('last_synced_pull'));+ return parseInt(await optionService.getOption('last_synced_pull'));}async function setLastSyncedPull(syncId) {- await options.setOption('last_synced_pull', syncId);+ await optionService.setOption('last_synced_pull', syncId);}async function pullSync(syncContext) {@@ -108,7 +108,7 @@ async function pullSync(syncContext) {log.info("Pulled " + syncRows.length + " changes from " + changesUri);for (const sync of syncRows) {- if (source_id.isLocalSourceId(sync.sourceId)) {+ if (sourceIdService.isLocalSourceId(sync.sourceId)) {log.info(`Skipping pull #${sync.id} ${sync.entityName} ${sync.entityId} because ${sync.sourceId} is a local source id.`);await setLastSyncedPull(sync.id);@@ -122,34 +122,34 @@ async function pullSync(syncContext) {log.error(`Empty response to pull for sync #${sync.id} ${sync.entityName}, id=${sync.entityId}`);}else if (sync.entityName === 'notes') {- await syncUpdate.updateNote(resp.entity, syncContext.sourceId);+ await syncUpdateService.updateNote(resp.entity, syncContext.sourceId);}else if (sync.entityName === 'branches') {- await syncUpdate.updateBranch(resp, syncContext.sourceId);+ await syncUpdateService.updateBranch(resp, syncContext.sourceId);}else if (sync.entityName === 'note_revisions') {- await syncUpdate.updateNoteRevision(resp, syncContext.sourceId);+ await syncUpdateService.updateNoteRevision(resp, syncContext.sourceId);}else if (sync.entityName === 'note_reordering') {- await syncUpdate.updateNoteReordering(resp, syncContext.sourceId);+ await syncUpdateService.updateNoteReordering(resp, syncContext.sourceId);}else if (sync.entityName === 'options') {- await syncUpdate.updateOptions(resp, syncContext.sourceId);+ await syncUpdateService.updateOptions(resp, syncContext.sourceId);}else if (sync.entityName === 'recent_notes') {- await syncUpdate.updateRecentNotes(resp, syncContext.sourceId);+ await syncUpdateService.updateRecentNotes(resp, syncContext.sourceId);}else if (sync.entityName === 'images') {- await syncUpdate.updateImage(resp, syncContext.sourceId);+ await syncUpdateService.updateImage(resp, syncContext.sourceId);}else if (sync.entityName === 'note_images') {- await syncUpdate.updateNoteImage(resp, syncContext.sourceId);+ await syncUpdateService.updateNoteImage(resp, syncContext.sourceId);}else if (sync.entityName === 'labels') {- await syncUpdate.updateLabel(resp, syncContext.sourceId);+ await syncUpdateService.updateLabel(resp, syncContext.sourceId);}else if (sync.entityName === 'api_tokens') {- await syncUpdate.updateApiToken(resp, syncContext.sourceId);+ await syncUpdateService.updateApiToken(resp, syncContext.sourceId);}else {throw new Error(`Unrecognized entity type ${sync.entityName} in sync #${sync.id}`);@@ -162,11 +162,11 @@ async function pullSync(syncContext) {}async function getLastSyncedPush() {- return parseInt(await options.getOption('last_synced_push'));+ return parseInt(await optionService.getOption('last_synced_push'));}async function setLastSyncedPush(lastSyncedPush) {- await options.setOption('last_synced_push', lastSyncedPush);+ await optionService.setOption('last_synced_push', lastSyncedPush);}async function pushSync(syncContext) {@@ -250,7 +250,7 @@ async function pushEntity(sync, syncContext) {log.info(`Pushing changes in sync #${sync.id} ${sync.entityName} ${sync.entityId}`);const payload = {- sourceId: source_id.getCurrentSourceId(),+ sourceId: sourceIdService.getCurrentSourceId(),entity: entity};@@ -281,18 +281,18 @@ async function checkContentHash(syncContext) {return;}- const hashes = await content_hash.getHashes();+ const hashes = await contentHashService.getHashes();let allChecksPassed = true;for (const key in hashes) {if (hashes[key] !== resp.hashes[key]) {allChecksPassed = false;- await event_log.addEvent(`Content hash check for ${key} FAILED. Local is ${hashes[key]}, remote is ${resp.hashes[key]}`);+ await eventLogService.addEvent(`Content hash check for ${key} FAILED. Local is ${hashes[key]}, remote is ${resp.hashes[key]}`);if (key !== 'recent_notes') {// let's not get alarmed about recent notes which get updated often and can cause failures in race conditions- await messaging.sendMessageToAllClients({type: 'sync-hash-check-failed'});+ await messagingService.sendMessageToAllClients({type: 'sync-hash-check-failed'});}}}@@ -303,7 +303,7 @@ async function checkContentHash(syncContext) {}async function syncRequest(syncContext, method, uri, body) {- const fullUri = sync_setup.SYNC_SERVER + uri;+ const fullUri = syncSetup.SYNC_SERVER + uri;try {const options = {@@ -312,15 +312,15 @@ async function syncRequest(syncContext, method, uri, body) {jar: syncContext.cookieJar,json: true,body: body,- timeout: sync_setup.SYNC_TIMEOUT+ timeout: syncSetup.SYNC_TIMEOUT};if (syncServerCertificate) {options.ca = syncServerCertificate;}- if (sync_setup.SYNC_PROXY && proxyToggle) {- options.proxy = sync_setup.SYNC_PROXY;+ if (syncSetup.SYNC_PROXY && proxyToggle) {+ options.proxy = syncSetup.SYNC_PROXY;}return await rp(options);@@ -331,17 +331,17 @@ async function syncRequest(syncContext, method, uri, body) {}sql.dbReady.then(() => {- if (sync_setup.isSyncSetup) {- log.info("Setting up sync to " + sync_setup.SYNC_SERVER + " with timeout " + sync_setup.SYNC_TIMEOUT);+ if (syncSetup.isSyncSetup) {+ log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);- if (sync_setup.SYNC_PROXY) {- log.info("Sync proxy: " + sync_setup.SYNC_PROXY);+ if (syncSetup.SYNC_PROXY) {+ log.info("Sync proxy: " + syncSetup.SYNC_PROXY);}- if (sync_setup.SYNC_CERT_PATH) {- log.info('Sync certificate: ' + sync_setup.SYNC_CERT_PATH);+ if (syncSetup.SYNC_CERT_PATH) {+ log.info('Sync certificate: ' + syncSetup.SYNC_CERT_PATH);- syncServerCertificate = fs.readFileSync(sync_setup.SYNC_CERT_PATH);+ syncServerCertificate = fs.readFileSync(syncSetup.SYNC_CERT_PATH);}setInterval(cls.wrap(sync), 60000);diff --git a/src/services/sync_table.js b/src/services/sync_table.jsindex df21291315..ca22530f53 100644--- a/src/services/sync_table.js+++ b/src/services/sync_table.js@@ -1,7 +1,7 @@const sql = require('./sql');-const source_id = require('./source_id');+const sourceIdService = require('./source_id');const utils = require('./utils');-const sync_setup = require('./sync_setup');+const syncSetup = require('./sync_setup');const log = require('./log');const cls = require('./cls');@@ -50,10 +50,10 @@ async function addEntitySync(entityName, entityId, sourceId) {entityName: entityName,entityId: entityId,syncDate: utils.nowDate(),- sourceId: sourceId || cls.getSourceId() || source_id.getCurrentSourceId()+ sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()});- if (!sync_setup.isSyncSetup) {+ if (!syncSetup.isSyncSetup) {// this is because the "server" instances shouldn't have outstanding pushes// useful when you fork the DB for new "client" instance, it won't try to sync the whole DBawait sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('last_synced_push', 'last_synced_pull')");diff --git a/src/services/sync_update.js b/src/services/sync_update.jsindex 684fa89b59..6635f37f21 100644--- a/src/services/sync_update.js+++ b/src/services/sync_update.js@@ -1,7 +1,7 @@const sql = require('./sql');const log = require('./log');-const eventLog = require('./event_log');-const sync_table = require('./sync_table');+const eventLogService = require('./event_log');+const syncTableService = require('./sync_table');function deserializeNoteContentBuffer(note) {if (note.type === 'file') {@@ -18,8 +18,8 @@ async function updateNote(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace("notes", entity);- await sync_table.addNoteSync(entity.noteId, sourceId);- await eventLog.addNoteEvent(entity.noteId, "Synced note <note>");+ await syncTableService.addNoteSync(entity.noteId, sourceId);+ await eventLogService.addNoteEvent(entity.noteId, "Synced note <note>");});log.info("Update/sync note " + entity.noteId);@@ -35,7 +35,7 @@ async function updateBranch(entity, sourceId) {await sql.replace('branches', entity);- await sync_table.addBranchSync(entity.branchId, sourceId);+ await syncTableService.addBranchSync(entity.branchId, sourceId);log.info("Update/sync note tree " + entity.branchId);}@@ -51,7 +51,7 @@ async function updateNoteRevision(entity, sourceId) {if (orig === null || orig.dateModifiedTo <= entity.dateModifiedTo) {await sql.replace('note_revisions', entity);- await sync_table.addNoteRevisionSync(entity.noteRevisionId, sourceId);+ await syncTableService.addNoteRevisionSync(entity.noteRevisionId, sourceId);log.info("Update/sync note revision " + entity.noteRevisionId);}@@ -64,7 +64,7 @@ async function updateNoteReordering(entity, sourceId) {await sql.execute("UPDATE branches SET notePosition = ? WHERE branchId = ?", [entity.ordering[key], key]);});- await sync_table.addNoteReorderingSync(entity.parentNoteId, sourceId);+ await syncTableService.addNoteReorderingSync(entity.parentNoteId, sourceId);});}@@ -79,9 +79,9 @@ async function updateOptions(entity, sourceId) {if (orig === null || orig.dateModified < entity.dateModified) {await sql.replace('options', entity);- await sync_table.addOptionsSync(entity.name, sourceId);+ await syncTableService.addOptionsSync(entity.name, sourceId);- await eventLog.addEvent("Synced option " + entity.name);+ await eventLogService.addEvent("Synced option " + entity.name);}});}@@ -93,7 +93,7 @@ async function updateRecentNotes(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace('recent_notes', entity);- await sync_table.addRecentNoteSync(entity.branchId, sourceId);+ await syncTableService.addRecentNoteSync(entity.branchId, sourceId);});}}@@ -109,7 +109,7 @@ async function updateImage(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace("images", entity);- await sync_table.addImageSync(entity.imageId, sourceId);+ await syncTableService.addImageSync(entity.imageId, sourceId);});log.info("Update/sync image " + entity.imageId);@@ -123,7 +123,7 @@ async function updateNoteImage(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace("note_images", entity);- await sync_table.addNoteImageSync(entity.noteImageId, sourceId);+ await syncTableService.addNoteImageSync(entity.noteImageId, sourceId);});log.info("Update/sync note image " + entity.noteImageId);@@ -137,7 +137,7 @@ async function updateLabel(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace("labels", entity);- await sync_table.addLabelSync(entity.labelId, sourceId);+ await syncTableService.addLabelSync(entity.labelId, sourceId);});log.info("Update/sync label " + entity.labelId);@@ -151,7 +151,7 @@ async function updateApiToken(entity, sourceId) {await sql.doInTransaction(async () => {await sql.replace("api_tokens", entity);- await sync_table.addApiTokenSync(entity.apiTokenId, sourceId);+ await syncTableService.addApiTokenSync(entity.apiTokenId, sourceId);});log.info("Update/sync API token " + entity.apiTokenId);diff --git a/src/services/tree.js b/src/services/tree.jsindex 38888cdfaf..fecc7a5b95 100644--- a/src/services/tree.js+++ b/src/services/tree.js@@ -1,8 +1,8 @@"use strict";const sql = require('./sql');-const sync_table = require('./sync_table');-const protected_session = require('./protected_session');+const syncTableService = require('./sync_table');+const protectedSessionService = require('./protected_session');async function validateParentChild(parentNoteId, childNoteId, branchId = null) {const existing = await getExistingBranch(parentNoteId, childNoteId);@@ -82,7 +82,7 @@ async function sortNotesAlphabetically(parentNoteId) {FROM notes JOIN branches USING(noteId)WHERE branches.isDeleted = 0 AND parentNoteId = ?`, [parentNoteId]);- protected_session.decryptNotes(notes);+ protectedSessionService.decryptNotes(notes);notes.sort((a, b) => a.title.toLowerCase() < b.title.toLowerCase() ? -1 : 1);@@ -95,7 +95,7 @@ async function sortNotesAlphabetically(parentNoteId) {position++;}- await sync_table.addNoteReorderingSync(parentNoteId);+ await syncTableService.addNoteReorderingSync(parentNoteId);});}diff --git a/src/www b/src/wwwindex 0c159ce209..0ee2031f81 100755--- a/src/www+++ b/src/www@@ -15,8 +15,8 @@ const http = require('http');const https = require('https');const config = require('./services/config');const log = require('./services/log');-const app_info = require('./services/app_info');-const messaging = require('./services/messaging');+const appInfo = require('./services/app_info');+const messagingService = require('./services/messaging');const utils = require('./services/utils');const sql = require('./services/sql');@@ -44,7 +44,7 @@ else {log.info("App HTTP server starting up at port " + port);}-log.info(JSON.stringify(app_info, null, 2));+log.info(JSON.stringify(appInfo, null, 2));/*** Listen on provided port, on all network interfaces.@@ -55,7 +55,7 @@ httpServer.listen(port);httpServer.on('error', onError);httpServer.on('listening', onListening);-sql.dbReady.then(() => messaging.init(httpServer, sessionParser));+sql.dbReady.then(() => messagingService.init(httpServer, sessionParser));if (utils.isElectron()) {const electronRouting = require('./routes/electron');