server-ts: Port ws service & dependencies

This commit is contained in:
Elian Doran 2024-02-17 01:51:47 +02:00
parent 4b1d2c6bad
commit cf18e61a33
No known key found for this signature in database
11 changed files with 108 additions and 53 deletions

32
package-lock.json generated
View File

@ -92,8 +92,10 @@
"@types/better-sqlite3": "^7.6.9", "@types/better-sqlite3": "^7.6.9",
"@types/escape-html": "^1.0.4", "@types/escape-html": "^1.0.4",
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/ini": "^4.1.0",
"@types/mime-types": "^2.1.4", "@types/mime-types": "^2.1.4",
"@types/node": "^20.11.19", "@types/node": "^20.11.19",
"@types/ws": "^8.5.10",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"electron": "25.9.8", "electron": "25.9.8",
"electron-builder": "24.6.4", "electron-builder": "24.6.4",
@ -1444,6 +1446,12 @@
"integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==",
"dev": true "dev": true
}, },
"node_modules/@types/ini": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/@types/ini/-/ini-4.1.0.tgz",
"integrity": "sha512-mTehMtc+xtnWBBvqizcqYCktKDBH2WChvx1GU3Sfe4PysFDXiNe+1YwtpVX1MDtCa4NQrSPw2+3HmvXHY3gt1w==",
"dev": true
},
"node_modules/@types/json-schema": { "node_modules/@types/json-schema": {
"version": "7.0.9", "version": "7.0.9",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz",
@ -1589,6 +1597,15 @@
"dev": true, "dev": true,
"optional": true "optional": true
}, },
"node_modules/@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
"integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==",
"dev": true,
"dependencies": {
"@types/node": "*"
}
},
"node_modules/@types/yauzl": { "node_modules/@types/yauzl": {
"version": "2.9.2", "version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",
@ -16086,6 +16103,12 @@
"integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==", "integrity": "sha512-D0CFMMtydbJAegzOyHjtiKPLlvnm3iTZyZRSZoLq2mRhDdmLfIWOCYPfQJ4cu2erKghU++QvjcUjp/5h7hESpA==",
"dev": true "dev": true
}, },
"@types/ini": {
"version": "4.1.0",
"resolved": "https://registry.npmjs.org/@types/ini/-/ini-4.1.0.tgz",
"integrity": "sha512-mTehMtc+xtnWBBvqizcqYCktKDBH2WChvx1GU3Sfe4PysFDXiNe+1YwtpVX1MDtCa4NQrSPw2+3HmvXHY3gt1w==",
"dev": true
},
"@types/json-schema": { "@types/json-schema": {
"version": "7.0.9", "version": "7.0.9",
"resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.9.tgz",
@ -16231,6 +16254,15 @@
"dev": true, "dev": true,
"optional": true "optional": true
}, },
"@types/ws": {
"version": "8.5.10",
"resolved": "https://registry.npmjs.org/@types/ws/-/ws-8.5.10.tgz",
"integrity": "sha512-vmQSUcfalpIq0R9q7uTo2lXs6eGIpt9wtnLdMv9LVpIjCA/+ufZRozlVoVelIYixx1ugCBKDhn89vnsEGOCx9A==",
"dev": true,
"requires": {
"@types/node": "*"
}
},
"@types/yauzl": { "@types/yauzl": {
"version": "2.9.2", "version": "2.9.2",
"resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz", "resolved": "https://registry.npmjs.org/@types/yauzl/-/yauzl-2.9.2.tgz",

View File

@ -115,8 +115,10 @@
"@types/better-sqlite3": "^7.6.9", "@types/better-sqlite3": "^7.6.9",
"@types/escape-html": "^1.0.4", "@types/escape-html": "^1.0.4",
"@types/express": "^4.17.21", "@types/express": "^4.17.21",
"@types/ini": "^4.1.0",
"@types/mime-types": "^2.1.4", "@types/mime-types": "^2.1.4",
"@types/node": "^20.11.19", "@types/node": "^20.11.19",
"@types/ws": "^8.5.10",
"cross-env": "7.0.3", "cross-env": "7.0.3",
"electron": "25.9.8", "electron": "25.9.8",
"electron-builder": "24.6.4", "electron-builder": "24.6.4",

View File

@ -67,7 +67,7 @@ abstract class AbstractBeccaEntity {
return this.getPojo(); return this.getPojo();
} }
protected abstract getPojo(): {}; abstract getPojo(): {};
/** /**
* Saves entity - executes SQL, but doesn't commit the transaction on its own * Saves entity - executes SQL, but doesn't commit the transaction on its own

View File

@ -1,10 +1,10 @@
"use strict"; "use strict";
const ini = require('ini'); import ini = require('ini');
const fs = require('fs'); import fs = require('fs');
const dataDir = require('./data_dir'); import dataDir = require('./data_dir');
const path = require('path'); import path = require('path');
const resourceDir = require('./resource_dir'); import resourceDir = require('./resource_dir');
const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini"); const configSampleFilePath = path.resolve(resourceDir.RESOURCE_DIR, "config-sample.ini");
@ -16,4 +16,4 @@ if (!fs.existsSync(dataDir.CONFIG_INI_PATH)) {
const config = ini.parse(fs.readFileSync(dataDir.CONFIG_INI_PATH, 'utf-8')); const config = ini.parse(fs.readFileSync(dataDir.CONFIG_INI_PATH, 'utf-8'));
module.exports = config; export = config;

View File

@ -1,7 +1,10 @@
export interface EntityChange { export interface EntityChange {
id?: number | null; id?: number | null;
noteId?: string;
entityName: string; entityName: string;
entityId: string; entityId: string;
entity?: any;
positions?: Record<string, string>;
hash: string; hash: string;
utcDateChanged: string; utcDateChanged: string;
isSynced: boolean | 1 | 0; isSynced: boolean | 1 | 0;

View File

@ -1,5 +0,0 @@
module.exports = {
isDev: function () {
return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev');
}
};

7
src/services/env.ts Normal file
View File

@ -0,0 +1,7 @@
function isDev() {
return !!(process.env.TRILIUM_ENV && process.env.TRILIUM_ENV === 'dev');
}
export = {
isDev
};

View File

@ -20,7 +20,7 @@ if (!fs.existsSync(MIGRATIONS_DIR)) {
process.exit(1); process.exit(1);
} }
module.exports = { export = {
RESOURCE_DIR, RESOURCE_DIR,
MIGRATIONS_DIR, MIGRATIONS_DIR,
DB_INIT_DIR, DB_INIT_DIR,

View File

@ -110,7 +110,7 @@ function getValue<T>(query: string, params: Params = []): T {
// smaller values can result in better performance due to better usage of statement cache // smaller values can result in better performance due to better usage of statement cache
const PARAM_LIMIT = 100; const PARAM_LIMIT = 100;
function getManyRows(query: string, params: Params) { function getManyRows<T>(query: string, params: Params): T[] | null {
let results: unknown[] = []; let results: unknown[] = [];
while (params.length > 0) { while (params.length > 0) {
@ -136,7 +136,7 @@ function getManyRows(query: string, params: Params) {
results = results.concat(subResults); results = results.concat(subResults);
} }
return results; return results as (T[] | null);
} }
function getRows<T>(query: string, params: Params = []): T[] { function getRows<T>(query: string, params: Params = []): T[] {

View File

@ -6,7 +6,7 @@
const Mutex = require('async-mutex').Mutex; const Mutex = require('async-mutex').Mutex;
const instance = new Mutex(); const instance = new Mutex();
async function doExclusively(func) { async function doExclusively<T>(func: () => void) {
const releaseMutex = await instance.acquire(); const releaseMutex = await instance.acquire();
try { try {
@ -17,6 +17,6 @@ async function doExclusively(func) {
} }
} }
module.exports = { export = {
doExclusively doExclusively
}; };

View File

@ -1,15 +1,17 @@
const WebSocket = require('ws'); import WebSocket = require('ws');
const utils = require('./utils'); import utils = require('./utils');
const log = require('./log'); import log = require('./log');
const sql = require('./sql'); import sql = require('./sql');
const cls = require('./cls'); import cls = require('./cls');
const config = require('./config.js'); import config = require('./config');
const syncMutexService = require('./sync_mutex.js'); import syncMutexService = require('./sync_mutex.js');
const protectedSessionService = require('./protected_session'); import protectedSessionService = require('./protected_session');
const becca = require('../becca/becca.js'); import becca = require('../becca/becca.js');
const AbstractBeccaEntity = require('../becca/entities/abstract_becca_entity.js'); import AbstractBeccaEntity = require('../becca/entities/abstract_becca_entity.js');
const env = require('./env.js'); import env = require('./env');
import { IncomingMessage, Server } from 'http';
import { EntityChange } from './entity_changes_interface';
if (env.isDev()) { if (env.isDev()) {
const chokidar = require('chokidar'); const chokidar = require('chokidar');
const debounce = require('debounce'); const debounce = require('debounce');
@ -21,15 +23,26 @@ if (env.isDev()) {
.on('unlink', debouncedReloadFrontend); .on('unlink', debouncedReloadFrontend);
} }
let webSocketServer; let webSocketServer!: WebSocket.Server;
let lastSyncedPush = null; let lastSyncedPush: number | null = null;
function init(httpServer, sessionParser) { interface Message {
type: string;
reason?: string;
data?: {
lastSyncedPush?: number,
entityChanges?: any[]
},
lastSyncedPush?: number
}
type SessionParser = (req: IncomingMessage, params: {}, cb: () => void) => void;
function init(httpServer: Server, sessionParser: SessionParser) {
webSocketServer = new WebSocket.Server({ webSocketServer = new WebSocket.Server({
verifyClient: (info, done) => { verifyClient: (info, done) => {
sessionParser(info.req, {}, () => { sessionParser(info.req, {}, () => {
const allowed = utils.isElectron() const allowed = utils.isElectron()
|| info.req.session.loggedIn || (info.req as any).session.loggedIn
|| (config.General && config.General.noAuthentication); || (config.General && config.General.noAuthentication);
if (!allowed) { if (!allowed) {
@ -43,12 +56,12 @@ function init(httpServer, sessionParser) {
}); });
webSocketServer.on('connection', (ws, req) => { webSocketServer.on('connection', (ws, req) => {
ws.id = utils.randomString(10); (ws as any).id = utils.randomString(10);
console.log(`websocket client connected`); console.log(`websocket client connected`);
ws.on('message', async messageJson => { ws.on('message', async messageJson => {
const message = JSON.parse(messageJson); const message = JSON.parse(messageJson as any);
if (message.type === 'log-error') { if (message.type === 'log-error') {
log.info(`JS Error: ${message.error}\r log.info(`JS Error: ${message.error}\r
@ -73,7 +86,7 @@ Stack: ${message.stack}`);
}); });
} }
function sendMessage(client, message) { function sendMessage(client: WebSocket, message: Message) {
const jsonStr = JSON.stringify(message); const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) { if (client.readyState === WebSocket.OPEN) {
@ -81,7 +94,7 @@ function sendMessage(client, message) {
} }
} }
function sendMessageToAllClients(message) { function sendMessageToAllClients(message: Message) {
const jsonStr = JSON.stringify(message); const jsonStr = JSON.stringify(message);
if (webSocketServer) { if (webSocketServer) {
@ -97,7 +110,7 @@ function sendMessageToAllClients(message) {
} }
} }
function fillInAdditionalProperties(entityChange) { function fillInAdditionalProperties(entityChange: EntityChange) {
if (entityChange.isErased) { if (entityChange.isErased) {
return; return;
} }
@ -123,14 +136,14 @@ function fillInAdditionalProperties(entityChange) {
if (!entityChange.entity) { if (!entityChange.entity) {
entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]); entityChange.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [entityChange.entityId]);
if (entityChange.entity.isProtected) { if (entityChange.entity && entityChange.entity.isProtected) {
entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title); entityChange.entity.title = protectedSessionService.decryptString(entityChange.entity.title || "");
} }
} }
} else if (entityChange.entityName === 'revisions') { } else if (entityChange.entityName === 'revisions') {
entityChange.noteId = sql.getValue(`SELECT noteId entityChange.noteId = sql.getValue<string>(`SELECT noteId
FROM revisions FROM revisions
WHERE revisionId = ?`, [entityChange.entityId]); WHERE revisionId = ?`, [entityChange.entityId]);
} else if (entityChange.entityName === 'note_reordering') { } else if (entityChange.entityName === 'note_reordering') {
entityChange.positions = {}; entityChange.positions = {};
@ -160,7 +173,7 @@ function fillInAdditionalProperties(entityChange) {
} }
// entities with higher number can reference the entities with lower number // entities with higher number can reference the entities with lower number
const ORDERING = { const ORDERING: Record<string, number> = {
"etapi_tokens": 0, "etapi_tokens": 0,
"attributes": 2, "attributes": 2,
"branches": 2, "branches": 2,
@ -172,14 +185,17 @@ const ORDERING = {
"options": 0 "options": 0
}; };
function sendPing(client, entityChangeIds = []) { function sendPing(client: WebSocket, entityChangeIds = []) {
if (entityChangeIds.length === 0) { if (entityChangeIds.length === 0) {
sendMessage(client, { type: 'ping' }); sendMessage(client, { type: 'ping' });
return; return;
} }
const entityChanges = sql.getManyRows(`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds); const entityChanges = sql.getManyRows<EntityChange>(`SELECT * FROM entity_changes WHERE id IN (???)`, entityChangeIds);
if (!entityChanges) {
return;
}
// sort entity changes since froca expects "referential order", i.e. referenced entities should already exist // sort entity changes since froca expects "referential order", i.e. referenced entities should already exist
// in froca. // in froca.
@ -190,7 +206,7 @@ function sendPing(client, entityChangeIds = []) {
try { try {
fillInAdditionalProperties(entityChange); fillInAdditionalProperties(entityChange);
} }
catch (e) { catch (e: any) {
log.error(`Could not fill additional properties for entity change ${JSON.stringify(entityChange)} because of error: ${e.message}: ${e.stack}`); log.error(`Could not fill additional properties for entity change ${JSON.stringify(entityChange)} because of error: ${e.message}: ${e.stack}`);
} }
} }
@ -198,7 +214,7 @@ function sendPing(client, entityChangeIds = []) {
sendMessage(client, { sendMessage(client, {
type: 'frontend-update', type: 'frontend-update',
data: { data: {
lastSyncedPush, lastSyncedPush: lastSyncedPush || undefined,
entityChanges entityChanges
} }
}); });
@ -213,26 +229,26 @@ function sendTransactionEntityChangesToAllClients() {
} }
function syncPullInProgress() { function syncPullInProgress() {
sendMessageToAllClients({ type: 'sync-pull-in-progress', lastSyncedPush }); sendMessageToAllClients({ type: 'sync-pull-in-progress', lastSyncedPush: lastSyncedPush || undefined });
} }
function syncPushInProgress() { function syncPushInProgress() {
sendMessageToAllClients({ type: 'sync-push-in-progress', lastSyncedPush }); sendMessageToAllClients({ type: 'sync-push-in-progress', lastSyncedPush: lastSyncedPush || undefined });
} }
function syncFinished() { function syncFinished() {
sendMessageToAllClients({ type: 'sync-finished', lastSyncedPush }); sendMessageToAllClients({ type: 'sync-finished', lastSyncedPush: lastSyncedPush || undefined });
} }
function syncFailed() { function syncFailed() {
sendMessageToAllClients({ type: 'sync-failed', lastSyncedPush }); sendMessageToAllClients({ type: 'sync-failed', lastSyncedPush: lastSyncedPush || undefined });
} }
function reloadFrontend(reason) { function reloadFrontend(reason: string) {
sendMessageToAllClients({ type: 'reload-frontend', reason }); sendMessageToAllClients({ type: 'reload-frontend', reason });
} }
function setLastSyncedPush(entityChangeId) { function setLastSyncedPush(entityChangeId: number) {
lastSyncedPush = entityChangeId; lastSyncedPush = entityChangeId;
} }