trilium/src/services/ws.js

140 lines
4.3 KiB
JavaScript
Raw Normal View History

const WebSocket = require('ws');
const utils = require('./utils');
const log = require('./log');
const sql = require('./sql');
2020-02-01 05:32:24 +08:00
const cls = require('./cls');
2019-10-23 03:59:51 +08:00
const syncMutexService = require('./sync_mutex');
const protectedSessionService = require('./protected_session');
let webSocketServer;
function init(httpServer, sessionParser) {
webSocketServer = new WebSocket.Server({
verifyClient: (info, done) => {
sessionParser(info.req, {}, () => {
const allowed = utils.isElectron() || info.req.session.loggedIn;
if (!allowed) {
log.error("WebSocket connection not allowed because session is neither electron nor logged in.");
}
done(allowed)
});
},
server: httpServer
});
2017-12-02 11:28:22 +08:00
webSocketServer.on('connection', (ws, req) => {
ws.id = utils.randomString(10);
console.log(`websocket client connected`);
2017-12-02 11:28:22 +08:00
2020-06-20 18:31:38 +08:00
ws.on('message', async messageJson => {
2017-12-02 11:28:22 +08:00
const message = JSON.parse(messageJson);
if (message.type === 'log-error') {
log.info('JS Error: ' + message.error + '\r\nStack: ' + message.stack);
2017-12-02 11:28:22 +08:00
}
else if (message.type === 'ping') {
2020-06-20 18:31:38 +08:00
await syncMutexService.doExclusively(() => sendPing(ws));
}
2017-12-02 11:28:22 +08:00
else {
log.error('Unrecognized message: ');
log.error(message);
}
});
});
}
2019-10-29 01:42:22 +08:00
function sendMessage(client, message) {
const jsonStr = JSON.stringify(message);
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
}
2019-10-29 01:42:22 +08:00
function sendMessageToAllClients(message) {
const jsonStr = JSON.stringify(message);
2019-01-03 05:36:06 +08:00
if (webSocketServer) {
log.info("Sending message to all clients: " + jsonStr);
2018-01-02 08:41:22 +08:00
2019-01-03 05:36:06 +08:00
webSocketServer.clients.forEach(function each(client) {
if (client.readyState === WebSocket.OPEN) {
client.send(jsonStr);
}
});
}
}
2020-06-20 18:31:38 +08:00
function fillInAdditionalProperties(sync) {
// fill in some extra data needed by the frontend
if (sync.entityName === 'attributes') {
2020-06-20 18:31:38 +08:00
sync.entity = sql.getRow(`SELECT * FROM attributes WHERE attributeId = ?`, [sync.entityId]);
2020-01-30 05:32:22 +08:00
} else if (sync.entityName === 'branches') {
2020-06-20 18:31:38 +08:00
sync.entity = sql.getRow(`SELECT * FROM branches WHERE branchId = ?`, [sync.entityId]);
2020-01-30 05:32:22 +08:00
} else if (sync.entityName === 'notes') {
2020-06-20 18:31:38 +08:00
sync.entity = sql.getRow(`SELECT * FROM notes WHERE noteId = ?`, [sync.entityId]);
if (sync.entity.isProtected) {
sync.entity.title = protectedSessionService.decryptString(sync.entity.title);
}
} else if (sync.entityName === 'note_revisions') {
2020-06-20 18:31:38 +08:00
sync.noteId = sql.getValue(`SELECT noteId
FROM note_revisions
WHERE noteRevisionId = ?`, [sync.entityId]);
2020-01-31 05:38:31 +08:00
} else if (sync.entityName === 'note_reordering') {
2020-06-20 18:31:38 +08:00
sync.positions = sql.getMap(`SELECT branchId, notePosition FROM branches WHERE isDeleted = 0 AND parentNoteId = ?`, [sync.entityId]);
}
else if (sync.entityName === 'options') {
2020-06-20 18:31:38 +08:00
sync.entity = sql.getRow(`SELECT * FROM options WHERE name = ?`, [sync.entityId]);
}
}
function sendPing(client, syncRows = []) {
2020-02-01 05:32:24 +08:00
for (const sync of syncRows) {
try {
2020-06-20 18:31:38 +08:00
fillInAdditionalProperties(sync);
}
catch (e) {
log.error("Could not fill additional properties for sync " + JSON.stringify(sync)
+ " because of error: " + e.message + ": " + e.stack);
2019-10-20 18:29:34 +08:00
}
}
const stats = require('./sync').stats;
2019-10-29 01:42:22 +08:00
sendMessage(client, {
type: 'sync',
2020-02-01 05:32:24 +08:00
data: syncRows,
outstandingSyncs: stats.outstandingPushes + stats.outstandingPulls
});
}
function sendTransactionSyncsToAllClients() {
if (webSocketServer) {
const syncRows = cls.getAndClearSyncRows();
webSocketServer.clients.forEach(function each(client) {
sendPing(client, syncRows);
});
}
}
2019-10-29 01:42:22 +08:00
function syncPullInProgress() {
sendMessageToAllClients({ type: 'sync-pull-in-progress' });
2019-10-26 04:20:14 +08:00
}
2019-10-29 02:45:36 +08:00
function syncPullFinished() {
2019-10-29 01:42:22 +08:00
sendMessageToAllClients({ type: 'sync-pull-finished' });
2019-10-26 04:20:14 +08:00
}
module.exports = {
init,
sendMessageToAllClients,
2019-10-26 04:20:14 +08:00
syncPullInProgress,
syncPullFinished,
sendTransactionSyncsToAllClients
};