Notes/src/services/setup.ts

121 lines
3.7 KiB
TypeScript
Raw Normal View History

import syncService from "./sync.js";
import log from "./log.js";
import sqlInit from "./sql_init.js";
import optionService from "./options.js";
import syncOptions from "./sync_options.js";
import request from "./request.js";
import appInfo from "./app_info.js";
import { timeLimit } from "./utils.js";
import becca from "../becca/becca.js";
import type { SetupStatusResponse, SetupSyncSeedResponse } from "./api-interface.js";
2018-09-11 10:01:40 +02:00
async function hasSyncServerSchemaAndSeed() {
2025-01-09 18:07:02 +02:00
const response = await requestToSyncServer<SetupStatusResponse>("GET", "/api/setup/status");
if (response.syncVersion !== appInfo.syncVersion) {
2025-01-09 18:07:02 +02:00
throw new Error(
`Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${response.syncVersion}. To fix this issue, use same Trilium version on all instances.`
);
}
2018-09-11 10:01:40 +02:00
return response.schemaExists;
}
function triggerSync() {
log.info("Triggering sync.");
// it's ok to not wait for it here
2025-01-09 18:07:02 +02:00
syncService.sync().then((res) => {
if (res.success) {
2020-06-20 21:42:41 +02:00
sqlInit.setDbAsInitialized();
}
});
}
2018-09-11 10:01:40 +02:00
async function sendSeedToSyncServer() {
log.info("Initiating sync to server");
2025-01-09 18:07:02 +02:00
await requestToSyncServer<void>("POST", "/api/setup/sync-seed", {
2020-06-20 12:31:38 +02:00
options: getSyncSeedOptions(),
syncVersion: appInfo.syncVersion
});
2023-06-30 11:18:34 +02:00
// this is a completely new sync, need to reset counters. If this was not a new sync,
// the previous request would have failed.
2025-01-09 18:07:02 +02:00
optionService.setOption("lastSyncedPush", 0);
optionService.setOption("lastSyncedPull", 0);
}
2024-04-03 23:18:39 +03:00
async function requestToSyncServer<T>(method: string, path: string, body?: string | {}): Promise<T> {
2020-06-20 12:31:38 +02:00
const timeout = syncOptions.getSyncTimeout();
2025-01-09 18:07:02 +02:00
return (await timeLimit(
request.exec({
method,
url: syncOptions.getSyncServerHost() + path,
body,
proxy: syncOptions.getSyncProxy(),
timeout: timeout
}),
timeout
)) as T;
}
2024-04-03 23:18:39 +03:00
async function setupSyncFromSyncServer(syncServerHost: string, syncProxy: string, password: string) {
2020-06-20 12:31:38 +02:00
if (sqlInit.isDbInitialized()) {
return {
2025-01-09 18:07:02 +02:00
result: "failure",
error: "DB is already initialized."
};
}
try {
2020-08-06 00:06:42 +02:00
log.info("Getting document options FROM sync server.");
2023-06-30 11:18:34 +02:00
// the response is expected to contain documentId and documentSecret options
2024-04-03 23:18:39 +03:00
const resp = await request.exec<SetupSyncSeedResponse>({
2025-01-09 18:07:02 +02:00
method: "get",
url: `${syncServerHost}/api/setup/sync-seed`,
2021-12-29 23:37:12 +01:00
auth: { password },
proxy: syncProxy,
timeout: 30000 // seed request should not take long
});
if (resp.syncVersion !== appInfo.syncVersion) {
const message = `Could not setup sync since local sync protocol version is ${appInfo.syncVersion} while remote is ${resp.syncVersion}. To fix this issue, use same Trilium version on all instances.`;
log.error(message);
return {
2025-01-09 18:07:02 +02:00
result: "failure",
error: message
2025-01-09 18:07:02 +02:00
};
}
await sqlInit.createDatabaseForSync(resp.options, syncServerHost, syncProxy);
triggerSync();
2025-01-09 18:07:02 +02:00
return { result: "success" };
} catch (e: any) {
2023-09-21 11:16:03 +02:00
log.error(`Sync failed: '${e.message}', stack: ${e.stack}`);
return {
2025-01-09 18:07:02 +02:00
result: "failure",
error: e.message
};
}
}
2020-06-20 12:31:38 +02:00
function getSyncSeedOptions() {
2025-01-09 18:07:02 +02:00
return [becca.getOption("documentId"), becca.getOption("documentSecret")];
2018-07-25 09:46:57 +02:00
}
export default {
2018-09-11 10:01:40 +02:00
hasSyncServerSchemaAndSeed,
triggerSync,
2018-09-11 10:01:40 +02:00
sendSeedToSyncServer,
setupSyncFromSyncServer,
getSyncSeedOptions
};