feat: Sync Network Graph between Desktop and Remote Server (#1245)

https://github.com/Termix-SSH/Support/issues/1245
This commit is contained in:
LukeGus committed 2026-09-20 18:17:57 -05:00
1 parent d0c25cf183
commit 0b7e616419
4 files changed
+111 -2

No files matched your search

@@ -14,7 +14,8 @@ export type SyncEntityType =
| "vaultProfiles"
| "dashboardServiceLinks"
| "homepageItems"
| "userPreferences";
| "userPreferences"
| "networkTopology";
export class SyncTombstoneRepository {
constructor(
@@ -2,6 +2,79 @@ import type { SyncEntityType } from "../repositories/sync-tombstone-repository.j
export type SyncReferenceEntity = "hosts" | "sshCredentials" | "vaultProfiles";
interface TopologyNode {
data?: { id?: string; [key: string]: unknown };
[key: string]: unknown;
}
interface TopologyEdge {
data?: { source?: string; target?: string; [key: string]: unknown };
[key: string]: unknown;
}
interface Topology {
nodes?: TopologyNode[];
edges?: TopologyEdge[];
[key: string]: unknown;
}
/**
* Node/edge ids in a topology are either a host's local numeric `id` (as a
* string) or a client-generated `group-<timestamp>` id. Only the former is
* meaningful across sides, so translate just those through the mapper and
* leave group ids untouched. Nodes/edges that fail to resolve (host deleted,
* or not synced to this side yet) are dropped rather than left dangling.
*/
async function mapTopologyHostIds(
topologyJson: string | null | undefined,
mapId: (id: string) => Promise<string | null>,
): Promise<string | null> {
if (!topologyJson) return topologyJson ?? null;
let topology: Topology;
try {
topology = JSON.parse(topologyJson);
} catch {
return topologyJson;
}
const isGroupId = (id: string) => id.startsWith("group-");
const idMap = new Map<string, string | null>();
const resolve = async (id: string): Promise<string | null> => {
if (isGroupId(id)) return id;
if (!idMap.has(id)) idMap.set(id, await mapId(id));
return idMap.get(id) ?? null;
};
const nodes: TopologyNode[] = [];
for (const node of topology.nodes ?? []) {
const id = node.data?.id;
if (typeof id !== "string") continue;
const mapped = await resolve(id);
if (mapped === null) continue;
nodes.push({ ...node, data: { ...node.data, id: mapped } });
}
const nodeIds = new Set(nodes.map((n) => n.data?.id));
const edges: TopologyEdge[] = [];
for (const edge of topology.edges ?? []) {
const source = edge.data?.source;
const target = edge.data?.target;
if (typeof source !== "string" || typeof target !== "string") continue;
const mappedSource = await resolve(source);
const mappedTarget = await resolve(target);
if (mappedSource === null || mappedTarget === null) continue;
if (!nodeIds.has(mappedSource) || !nodeIds.has(mappedTarget)) continue;
edges.push({
...edge,
data: { ...edge.data, source: mappedSource, target: mappedTarget },
});
}
return JSON.stringify({ ...topology, nodes, edges });
}
interface SyncReference {
field: string;
syncField: string;
@@ -92,6 +165,19 @@ export async function serializeSyncReferences(
) => Promise<string | null>,
): Promise<Record<string, unknown>> {
const serialized = { ...row };
if (entityType === "networkTopology") {
serialized.topology = await mapTopologyHostIds(
serialized.topology as string | null | undefined,
async (id) => {
const numericId = Number(id);
if (!Number.isInteger(numericId)) return null;
return resolveSyncId("hosts", numericId);
},
);
return serialized;
}
for (const reference of REFERENCES[entityType] ?? []) {
const id = serialized[reference.field];
serialized[reference.syncField] =
@@ -112,6 +198,18 @@ export async function deserializeSyncReferences(
) => Promise<number | null>,
): Promise<Record<string, unknown>> {
const deserialized = { ...row };
if (entityType === "networkTopology") {
deserialized.topology = await mapTopologyHostIds(
deserialized.topology as string | null | undefined,
async (syncId) => {
const id = await resolveId("hosts", syncId);
return id === null ? null : String(id);
},
);
return deserialized;
}
for (const reference of REFERENCES[entityType] ?? []) {
const syncId = deserialized[reference.syncField];
delete deserialized[reference.syncField];
+8 -1
View File
@@ -10,6 +10,7 @@ import {
dashboardServiceLinks,
homepageItems,
userPreferences,
networkTopology,
} from "../db/schema.js";
import { AuthManager } from "../../utils/auth-manager.js";
import { DataCrypto } from "../../utils/data-crypto.js";
@@ -52,7 +53,8 @@ interface EntityConfig {
| typeof vaultProfiles
| typeof dashboardServiceLinks
| typeof homepageItems
| typeof userPreferences;
| typeof userPreferences
| typeof networkTopology;
// Fields that only make sense on the device that created the row, or
// that are managed elsewhere and must never be overwritten by a sync
// payload from the other side.
@@ -77,6 +79,11 @@ const ENTITY_CONFIG: Record<SyncEntityType, EntityConfig> = {
readOnlyFields: ["storageMode"],
singleton: true,
},
networkTopology: {
table: networkTopology,
readOnlyFields: [],
singleton: true,
},
};
const VALID_ENTITY_TYPES = new Set(Object.keys(ENTITY_CONFIG));