Skip to content

Build your personal knowledge base with Trilium Notes

fix(core): server not starting due to crypto not initialized

Elian Doran6a0f6fab8348
diff --git a/apps/server/src/routes/api/login.ts b/apps/server/src/routes/api/login.ts
index b7dc4eace9..6106fc52d2 100644
--- a/apps/server/src/routes/api/login.ts
+++ b/apps/server/src/routes/api/login.ts
@@ -1,4 +1,4 @@
-import { events as eventService } from "@triliumnext/core";
+import { events as eventService, getInstanceId } from "@triliumnext/core";
import type { Request } from "express";
import appInfo from "../../services/app_info.js";
@@ -6,7 +6,6 @@ import dateUtils from "../../services/date_utils.js";
import passwordEncryptionService from "../../services/encryption/password_encryption.js";
import recoveryCodeService from "../../services/encryption/recovery_codes";
import etapiTokenService from "../../services/etapi_tokens.js";
-import instanceId from "../../services/instance_id.js";
import options from "../../services/options.js";
import protectedSessionService from "../../services/protected_session.js";
import sql from "../../services/sql.js";
@@ -114,7 +113,7 @@ function loginSync(req: Request) {
req.session.loggedIn = true;
return {
- instanceId,
+ instanceId: getInstanceId(),
maxEntityChangeId: sql.getValue("SELECT COALESCE(MAX(id), 0) FROM entity_changes WHERE isSynced = 1")
};
}
diff --git a/apps/server/src/services/instance_id.ts b/apps/server/src/services/instance_id.ts
deleted file mode 100644
index a612180200..0000000000
--- a/apps/server/src/services/instance_id.ts
+++ /dev/null
@@ -1,2 +0,0 @@
-import { instance_id } from "@triliumnext/core";
-export default instance_id;
diff --git a/apps/server/src/services/sync.ts b/apps/server/src/services/sync.ts
index 6b52fc60df..7720e13f71 100644
--- a/apps/server/src/services/sync.ts
+++ b/apps/server/src/services/sync.ts
@@ -1,7 +1,5 @@
-
-
import type { EntityChange, EntityChangeRecord, EntityRow } from "@triliumnext/commons";
-import { becca_loader, binary_utils, entity_constructor } from "@triliumnext/core";
+import { becca_loader, binary_utils, entity_constructor, getInstanceId } from "@triliumnext/core";
import becca from "../becca/becca.js";
import appInfo from "./app_info.js";
@@ -10,7 +8,6 @@ import consistency_checks from "./consistency_checks.js";
import contentHashService from "./content_hash.js";
import dateUtils from "./date_utils.js";
import entityChangesService from "./entity_changes.js";
-import instanceId from "./instance_id.js";
import log from "./log.js";
import optionService from "./options.js";
import request from "./request.js";
@@ -132,7 +129,7 @@ async function doLogin(): Promise<SyncContext> {
throw new Error("Got no response.");
}
- if (resp.instanceId === instanceId) {
+ if (resp.instanceId === getInstanceId()) {
throw new Error(
`Sync server has instance ID '${resp.instanceId}' which is also local. This usually happens when the sync client is (mis)configured to sync with itself (URL points back to client) instead of the correct sync server.`
);
@@ -157,7 +154,7 @@ async function pullChanges(syncContext: SyncContext) {
while (true) {
const lastSyncedPull = getLastSyncedPull();
const logMarkerId = randomString(10); // to easily pair sync events between client and server logs
- const changesUri = `/api/sync/changed?instanceId=${instanceId}&lastEntityChangeId=${lastSyncedPull}&logMarkerId=${logMarkerId}`;
+ const changesUri = `/api/sync/changed?instanceId=${getInstanceId()}&lastEntityChangeId=${lastSyncedPull}&logMarkerId=${logMarkerId}`;
const startDate = Date.now();
@@ -239,7 +236,7 @@ async function pushChanges(syncContext: SyncContext) {
await syncRequest(syncContext, "PUT", `/api/sync/update?logMarkerId=${logMarkerId}`, {
entities: entityChangesRecords,
- instanceId
+ instanceId: getInstanceId()
});
ws.syncPushInProgress();
diff --git a/packages/trilium-core/src/index.ts b/packages/trilium-core/src/index.ts
index e6940a14f6..096820bdb0 100644
--- a/packages/trilium-core/src/index.ts
+++ b/packages/trilium-core/src/index.ts
@@ -22,7 +22,7 @@ export { default as entity_changes } from "./services/entity_changes";
export { getContext, type ExecutionContext } from "./services/context";
export * as cls from "./services/context";
export * from "./errors";
-export { default as instance_id } from "./services/instance_id";
+export { default as getInstanceId } from "./services/instance_id";
export type { CryptoProvider } from "./services/encryption/crypto";
export { default as becca } from "./becca/becca";
diff --git a/packages/trilium-core/src/services/entity_changes.ts b/packages/trilium-core/src/services/entity_changes.ts
index b389806d42..d77bd4a58a 100644
--- a/packages/trilium-core/src/services/entity_changes.ts
+++ b/packages/trilium-core/src/services/entity_changes.ts
@@ -2,13 +2,13 @@ import type { BlobRow, EntityChange } from "@triliumnext/commons";
import becca from "../becca/becca.js";
import dateUtils from "./utils/date.js";
-import instanceId from "./instance_id.js";
-import log, { getLog } from "./log.js";
+import { getLog } from "./log.js";
import { randomString } from "./utils/index.js";
import { getSql } from "./sql/index.js";
import { getComponentId } from "./context.js";
import events from "./events.js";
import blobService from "./blob.js";
+import getInstanceId from "./instance_id.js";
let maxEntityChangeId = 0;
@@ -34,7 +34,7 @@ function putEntityChange(origEntityChange: EntityChange) {
}
ec.componentId = ec.componentId || getComponentId() || "NA"; // NA = not available
- ec.instanceId = ec.instanceId || instanceId;
+ ec.instanceId = ec.instanceId || getInstanceId();
ec.isSynced = ec.isSynced ? 1 : 0;
ec.isErased = ec.isErased ? 1 : 0;
ec.id = getSql().replace("entity_changes", ec);
@@ -55,7 +55,7 @@ function putNoteReorderingEntityChange(parentNoteId: string, componentId?: strin
utcDateChanged: dateUtils.utcNowDateTime(),
isSynced: true,
componentId,
- instanceId
+ instanceId: getInstanceId()
});
events.emit(events.ENTITY_CHANGED, {
diff --git a/packages/trilium-core/src/services/instance_id.ts b/packages/trilium-core/src/services/instance_id.ts
index cd8257edff..7925d54655 100644
--- a/packages/trilium-core/src/services/instance_id.ts
+++ b/packages/trilium-core/src/services/instance_id.ts
@@ -1,5 +1,11 @@
import { randomString } from "./utils";
-const instanceId = randomString(12);
+let instanceId: string | null = null;
-export default instanceId;
+export default function getInstanceId() {
+ if (instanceId === null) {
+ instanceId = randomString(12);
+ }
+
+ return instanceId;
+}