refactored backend to use new naming convention for modules

This commit is contained in:
azivner
2018-04-01 21:27:46 -04:00
parent c765dbc5cf
commit e2921a648d
44 changed files with 305 additions and 310 deletions

View File

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

View File

@@ -1,6 +1,6 @@
"use strict";
const migration = require('./migration');
const migrationService = require('./migration');
const sql = require('./sql');
const utils = require('./utils');

View File

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

View File

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

View File

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

View File

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

View File

@@ -1,6 +1,5 @@
const sql = require('./sql');
const utils = require('./utils');
const options = require('./options');
const log = require('./log');
function getHash(rows) {

View File

@@ -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','Satur
const 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;

View File

@@ -1,7 +1,6 @@
"use strict";
const sql = require('./sql');
const utils = require('./utils');
const repository = require('./repository');
const Label = require('../entities/label');

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -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 DB
await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('last_synced_push', 'last_synced_pull')");

View File

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

View File

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