refactor: use registered remote configuration dispatch

This commit is contained in:
vorotamoroz
2026-08-02 07:49:41 +00:00
parent 9aefdc117f
commit 09ee6023ae
2 changed files with 0 additions and 140 deletions
@@ -1,88 +0,0 @@
import {
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
REMOTE_WEBDAV,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
ConnectionStringParser,
type RemoteConfigurationResult,
} from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import { parseWebDAVConnectionURI } from "@vrtmrz/livesync-commonlib/journal-storage";
export type ConfigurableRemoteType =
| typeof REMOTE_COUCHDB
| typeof REMOTE_MINIO
| typeof REMOTE_WEBDAV
| typeof REMOTE_P2P;
function publicOrigin(uri: string): string {
const url = new URL(uri);
return `${url.protocol}//${url.host}`;
}
export function serializeRemoteConfiguration(settings: ObsidianLiveSyncSettings): string {
switch (settings.remoteType) {
case REMOTE_COUCHDB:
return ConnectionStringParser.serialize({ type: "couchdb", settings });
case REMOTE_MINIO:
return ConnectionStringParser.serialize({ type: "s3", settings });
case REMOTE_WEBDAV:
return ConnectionStringParser.serialize({ type: "webdav", settings });
case REMOTE_P2P:
return ConnectionStringParser.serialize({ type: "p2p", settings });
default:
throw new Error("Unsupported remote type");
}
}
export function remoteTypeForRemoteConfiguration(parsed: RemoteConfigurationResult): ConfigurableRemoteType {
switch (parsed.type) {
case "couchdb":
return REMOTE_COUCHDB;
case "s3":
return REMOTE_MINIO;
case "webdav":
return REMOTE_WEBDAV;
case "p2p":
return REMOTE_P2P;
}
}
export function suggestRemoteConfigurationName(parsed: RemoteConfigurationResult): string {
if (parsed.type === "couchdb") {
try {
const url = new URL(parsed.settings.couchDB_URI);
return `CouchDB ${url.host}`;
} catch {
return "Imported CouchDB";
}
}
if (parsed.type === "s3") {
return `S3 ${parsed.settings.bucket || parsed.settings.endpoint}`;
}
if (parsed.type === "webdav") {
try {
const endpoint = new URL(parseWebDAVConnectionURI(parsed.settings.webDAVactiveConnectionURI).endpoint);
return `WebDAV ${endpoint.host}`;
} catch {
return "Imported WebDAV";
}
}
return `P2P ${parsed.settings.P2P_roomID || "Remote"}`;
}
export function describeRemoteConfiguration(uri: string): string {
try {
const parsed = ConnectionStringParser.parse(uri);
if (parsed.type === "couchdb") return publicOrigin(parsed.settings.couchDB_URI);
if (parsed.type === "s3") return publicOrigin(parsed.settings.endpoint);
if (parsed.type === "webdav") {
return publicOrigin(parseWebDAVConnectionURI(parsed.settings.webDAVactiveConnectionURI).endpoint);
}
return "P2P";
} catch {
return "";
}
}
@@ -1,52 +0,0 @@
import { describe, expect, it } from "vitest";
import {
DEFAULT_SETTINGS,
REMOTE_WEBDAV,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import {
describeRemoteConfiguration,
remoteTypeForRemoteConfiguration,
serializeRemoteConfiguration,
suggestRemoteConfigurationName,
} from "./remoteConfigurationEditor.ts";
describe("remote configuration editor helpers", () => {
it("maps, names, and serialises an Adaptive WebDAV profile", () => {
const repositoryId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
const settings = {
...DEFAULT_SETTINGS,
remoteType: REMOTE_WEBDAV,
webDAVactiveConnectionURI:
"sls+webdav://alice:secret@dav.example/remote.php/dav/files/alice?prefix=notes%2F",
expectedRepositoryId: repositoryId,
journalFormat: "adaptive-v1" as const,
packReadPolicy: "range" as const,
} as ObsidianLiveSyncSettings;
const uri = serializeRemoteConfiguration(settings);
const parsed = ConnectionStringParser.parse(uri);
expect(parsed.type).toBe("webdav");
expect(remoteTypeForRemoteConfiguration(parsed)).toBe(REMOTE_WEBDAV);
expect(suggestRemoteConfigurationName(parsed)).toBe("WebDAV dav.example");
expect(uri).toContain("journalFormat=adaptive-v1");
expect(uri).toContain("packReadPolicy=range");
expect(uri).toContain(`expectedRepositoryId=${repositoryId}`);
});
it("does not expose WebDAV credentials or custom headers in the saved-connection description", () => {
const uri =
"sls+webdav://alice:secret@dav.example/remote.php/dav/files/alice" +
"?prefix=notes%2F&headers=Authorization%3A+Bearer+private-token";
const description = describeRemoteConfiguration(uri);
expect(description).toBe("https://dav.example");
expect(description).not.toContain("alice");
expect(description).not.toContain("secret");
expect(description).not.toContain("private-token");
});
});