Compare commits

...
Author SHA1 Message Date
vorotamoroz f93b5ac8e4 test(cli): focus adaptive PostgREST on consumer behaviour 2026-08-02 07:51:15 +00:00
vorotamoroz ae9dd6f79b test(cli): cover Adaptive PostgREST remotes 2026-08-02 07:51:07 +00:00
vorotamoroz 09ee6023ae refactor: use registered remote configuration dispatch 2026-08-02 07:49:41 +00:00
vorotamoroz 9aefdc117f feat: preselect WebDAV repository identity 2026-08-02 07:49:41 +00:00
vorotamoroz 34255e5736 feat: configure WebDAV journal remotes 2026-08-02 07:49:41 +00:00
vorotamoroz 5db64f6faa test(cli): focus adaptive WebDAV on consumer behaviour 2026-08-02 07:49:19 +00:00
vorotamoroz 032ace8f53 feat(cli): support adaptive WebDAV remotes 2026-08-02 07:49:09 +00:00
vorotamoroz 1543a53263 refactor: compose Journal remotes generically 2026-08-02 07:48:52 +00:00
vorotamoroz 9715d44fb6 feat: preselect adaptive repository identity 2026-08-02 07:48:33 +00:00
vorotamoroz dd8c795210 feat: expose adaptive journal options for S3 2026-08-02 07:48:33 +00:00
vorotamoroz fff8103e40 test(cli): focus adaptive S3 on consumer behaviour 2026-08-02 07:48:08 +00:00
vorotamoroz aeb97c2b13 test(cli): pin Adaptive S3 service images 2026-08-02 07:47:59 +00:00
vorotamoroz 44c52aa2dd test(cli): exercise adaptive S3 pack reads 2026-08-02 07:47:59 +00:00
vorotamoroz d74e37559c test(cli): cover adaptive journal S3 end to end 2026-08-02 07:47:59 +00:00
vorotamoroz 5c7af20ad2 fix(cli): verify adaptive journal remotes 2026-08-02 07:47:59 +00:00
vorotamoroz 0f2efbd670 fix(cli): persist adaptive writer state 2026-08-02 07:47:59 +00:00
vorotamoroz a9283be9d2 refactor: compose Journal sync as a service feature 2026-08-02 04:08:52 +00:00
vorotamoroz 4d6a794f9d refactor: register remote setup presentations 2026-08-02 04:08:52 +00:00
vorotamoroz c4419a837c fix: preserve native HTTP body semantics 2026-08-02 04:08:52 +00:00
46 changed files with 3019 additions and 315 deletions
+52 -2
View File
@@ -301,11 +301,61 @@ Setting key: bucketCustomHeaders
Custom HTTP headers to include in every request sent to the Object Storage bucket. Specify them in the format `Header-Name: Value`, with each header on a new line.
#### Journal data format
Setting key: journalFormat
Existing Object Storage profiles use `opaque-v1` unless Adaptive Journal is selected explicitly. `adaptive-v1` uses an authenticated manifest and immutable Commit Bundles under a separate namespace, with larger Packs stored separately. Changing between formats requires an explicit remote Rebuild; LiveSync does not migrate or read both representations.
#### Pack retrieval
Setting key: packReadPolicy
Adaptive Journal can download a complete immutable Pack (`whole-pack`) or request only the required byte ranges (`range`). Complete Pack retrieval is the portable, throughput-oriented default. Selecting Range requires exact byte-range support from the configured S3-compatible endpoint. The connection test verifies that capability, and synchronisation refuses an unsupported selection before writing.
#### Test Connection
#### Apply Settings
### 6. CouchDB
### 6. WebDAV Journal
WebDAV Journal is an experimental remote provider configured when adding (``) or editing (`🔧`) a saved connection profile. Use a dedicated WebDAV collection or prefix rather than mixing Journal objects with unrelated files.
#### WebDAV connection
Setting key: webDAVactiveConnectionURI
The saved `sls+webdav` value contains the HTTP or HTTPS endpoint, optional username and password, collection prefix, custom headers, and internal-API preference. The setup dialogue parses and serialises this value; do not edit it manually or share it as plain text. Only HTTPS endpoints work on Obsidian Mobile.
**Use internal API** routes requests through Obsidian when browser-compatible requests are blocked by CORS. It is a compatibility option with different behaviour from standard browser fetch.
#### Journal data format
Setting key: journalFormat
`opaque-v1` is the compatible default. `adaptive-v1` uses authenticated immutable Commit Bundles and Packs under a separate remote representation. Existing Opaque Journal data is neither migrated nor read as Adaptive data. Changing formats requires an explicit remote Rebuild or a separate collection prefix.
#### Expected repository ID
Setting key: expectedRepositoryId
This optional base64url identity pins a trusted Adaptive repository. A trusted Setup URI can supply it. Leave it blank only when creating a repository or intentionally trusting the first compatible Adaptive repository reached.
#### Pack retrieval
Setting key: packReadPolicy
Complete Pack retrieval (`whole-pack`) is the portable, throughput-oriented default. Range retrieval (`range`) can reduce transferred bytes, but the endpoint must pass the exact byte-range check. Range support is optional and does not affect the safety of complete Pack retrieval.
#### Endpoint safety check
Adaptive setup writes, reads, lists, and removes disposable objects under a random reserved probe prefix. It checks binary fidelity, read-after-write and delete visibility, complete listing of the probe objects, and conditional creation which does not replace an existing object. Exact HTTP byte-range behaviour is reported separately as optional.
The dialogue reports required operations and Range support independently. Authentication, permission, availability, and invalid-response failures remain explicit failures rather than being reported as unsupported features. A format mismatch requires a remote Rebuild or restoration of the matching profile format.
Onboarding accepts the profile only after the selected policy passes. Adding or editing a profile in Settings also offers **Save without connecting**, which preserves a locally valid but unverified profile.
### 7. CouchDB
These settings are configured within the CouchDB Setup dialogue when adding (``) or editing (`🔧`) a CouchDB connection profile.
@@ -399,7 +449,7 @@ This optional check reads the CouchDB server configuration through Obsidian's in
#### Apply Settings
### 7. Peer-to-Peer (P2P) Synchronisation
### 8. Peer-to-Peer (P2P) Synchronisation
#### Enable P2P Synchronisation
+5
View File
@@ -44,6 +44,9 @@
"test:contract:context:obsidian": "npm run build && npm run test:e2e:obsidian:smoke",
"test:e2e:cli": "npm run test:e2e:ci --workspace self-hosted-livesync-cli",
"test:e2e:cli:p2p": "npm run test:e2e:p2p --workspace self-hosted-livesync-cli",
"test:e2e:cli:adaptive-s3": "npm run test:e2e:adaptive-s3 --workspace self-hosted-livesync-cli",
"test:e2e:cli:adaptive-webdav": "npm run test:e2e:adaptive-webdav --workspace self-hosted-livesync-cli",
"test:e2e:cli:adaptive-postgrest": "npm run test:e2e:adaptive-postgrest --workspace self-hosted-livesync-cli",
"test:e2e:cli:all": "npm run test:e2e:all --workspace self-hosted-livesync-cli",
"test:integration": "npx dotenv-cli -e .env -e .test.env -- vitest run --config vitest.config.integration.ts",
"test:unit:coverage": "vitest run --config vitest.config.unit.ts --coverage",
@@ -94,6 +97,8 @@
"test:docker-s3:start": "npm run test:docker-s3:up && sleep 3 && npm run test:docker-s3:init",
"test:docker-s3:down": "npx dotenv-cli -e .env -e .test.env -- ./test/shell/minio-stop.sh",
"test:docker-s3:stop": "npm run test:docker-s3:down",
"test:docker-postgrest:start": "npx dotenv-cli -e .env -e .test.env -- deno run --env-file=src/apps/cli/testdeno/.test.env -A --no-check src/apps/cli/testdeno/manage-postgrest-fixture.ts start",
"test:docker-postgrest:stop": "npx dotenv-cli -e .env -e .test.env -- deno run --env-file=src/apps/cli/testdeno/.test.env -A --no-check src/apps/cli/testdeno/manage-postgrest-fixture.ts stop",
"test:docker-all:up": "npm run test:docker-couchdb:up ; npm run test:docker-s3:up",
"test:docker-all:init": "npm run test:docker-couchdb:init ; npm run test:docker-s3:init",
"test:docker-all:down": "npm run test:docker-couchdb:down ; npm run test:docker-s3:down",
+3 -2
View File
@@ -15,13 +15,13 @@ import type { LiveSyncJournalReplicatorEnv } from "@vrtmrz/livesync-commonlib/co
import type { LiveSyncReplicatorEnv } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import { useTargetFilters } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/targetFilter";
import { useRemoteConfigurationMigration } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/remoteConfig";
import { useJournalSyncFeature } from "@vrtmrz/livesync-commonlib/journal-sync";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import type { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import { AbstractModule } from "./modules/AbstractModule";
import { ModulePeriodicProcess } from "./modules/core/ModulePeriodicProcess";
import { ModuleReplicator } from "./modules/core/ModuleReplicator";
import { ModuleReplicatorCouchDB } from "./modules/core/ModuleReplicatorCouchDB";
import { ModuleReplicatorMinIO } from "./modules/core/ModuleReplicatorMinIO";
import { ModuleConflictChecker } from "./modules/coreFeatures/ModuleConflictChecker";
import { ModuleConflictResolver } from "./modules/coreFeatures/ModuleConflictResolver";
import { ModuleResolvingMismatchedTweaks } from "./modules/coreFeatures/ModuleResolveMismatchedTweaks";
@@ -139,7 +139,6 @@ export class LiveSyncBaseCore<
public registerModules(extraModules: AbstractModule[] = []) {
this._registerModule(new ModuleLiveSyncMain(this));
this._registerModule(new ModuleConflictChecker(this));
this._registerModule(new ModuleReplicatorMinIO(this));
this._registerModule(new ModuleReplicatorCouchDB(this));
this._registerModule(new ModuleReplicator(this));
this._registerModule(new ModuleConflictResolver(this));
@@ -279,6 +278,8 @@ export class LiveSyncBaseCore<
usePrepareDatabaseForUse(this);
// Migration to multiple remote configurations
useRemoteConfigurationMigration(this);
// Journal providers are selected through the composed remote-provider registry.
useJournalSyncFeature(this);
}
}
+17 -3
View File
@@ -6,7 +6,7 @@ import {
type FilePathWithPrefix,
type ObsidianLiveSyncSettings,
REMOTE_COUCHDB,
REMOTE_MINIO,
isJournalRemoteType,
type EntryMilestoneInfo,
type EntryDoc,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
@@ -25,6 +25,8 @@ import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFu
import { fsPromises as fs, path } from "@vrtmrz/livesync-commonlib/node";
import type { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import type { JournalSyncCore } from "@vrtmrz/livesync-commonlib/compat/replication/journal/JournalSyncCore";
import { journalProtocolConfigurationForSettings } from "@vrtmrz/livesync-commonlib/journal-storage";
import { writeStderrLine, writeStdoutLine } from "@/apps/cli/cliOutput";
function redactConnectionString(uri: string): string {
@@ -59,8 +61,20 @@ async function verifyRemoteState(
return false;
}
milestone = await dbRet.db.get(MILESTONE_DOCID);
} else if (settings.remoteType === REMOTE_MINIO) {
milestone = await (replicator as LiveSyncJournalReplicator).client.downloadJson("_00000000-milestone.json");
} else if (isJournalRemoteType(settings.remoteType)) {
const journalReplicator = replicator as LiveSyncJournalReplicator;
if (journalProtocolConfigurationForSettings(settings).journalFormat === "adaptive-v1") {
try {
await journalReplicator.client.ensureCheckpointCachesAreFresh();
standardIo.writeStderr("[Verification] Adaptive Journal repository is available.\n");
return true;
} catch (error) {
const message = error instanceof Error ? error.message : String(error);
standardIo.writeStderr(`[Verification] Failed to verify Adaptive Journal repository: ${message}\n`);
return false;
}
}
milestone = await (journalReplicator.client as JournalSyncCore).downloadJson("_00000000-milestone.json");
}
if (milestone) {
+61 -1
View File
@@ -2,7 +2,13 @@ import { fsPromises as fs, os, path } from "@vrtmrz/livesync-commonlib/node";
import * as processSetting from "@vrtmrz/livesync-commonlib/compat/API/processSetting";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import { configURIBase } from "@vrtmrz/livesync-commonlib/compat/common/models/shared.const";
import { DEFAULT_SETTINGS, REMOTE_COUCHDB, REMOTE_MINIO, REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
REMOTE_WEBDAV,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest";
import { runCommand } from "./runCommand";
import type { CLIOptions } from "./types";
@@ -717,6 +723,60 @@ describe("runCommand abnormal cases", () => {
expect(core.services.control.applySettings).not.toHaveBeenCalled();
});
it("verifies an Adaptive Journal repository without reading the legacy milestone", async () => {
const core = createCoreMock();
const settings = core.services.setting.currentSettings();
settings.remoteType = REMOTE_MINIO;
settings.journalFormat = "adaptive-v1";
settings.packReadPolicy = "whole-pack";
const ensureCheckpointCachesAreFresh = vi.fn(async () => {});
core.services.replicator.getActiveReplicator.mockReturnValue({
nodeid: "test-node-id",
initializeDatabaseForReplication: vi.fn(async () => {}),
client: {
ensureCheckpointCachesAreFresh,
},
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(true);
expect(ensureCheckpointCachesAreFresh).toHaveBeenCalledTimes(1);
expect(core.services.context.standardIo.writeStderr).toHaveBeenCalledWith(
"[Verification] Adaptive Journal repository is available.\n"
);
});
it("uses the Adaptive verification path for a WebDAV remote", async () => {
const core = createCoreMock();
const settings = core.services.setting.currentSettings();
settings.remoteType = REMOTE_WEBDAV;
settings.webDAVactiveConnectionURI = "sls+webdav://dav.example/dav";
settings.journalFormat = "adaptive-v1";
settings.packReadPolicy = "whole-pack";
const ensureCheckpointCachesAreFresh = vi.fn(async () => {});
core.services.replicator.getActiveReplicator.mockReturnValue({
nodeid: "test-node-id",
initializeDatabaseForReplication: vi.fn(async () => {}),
client: {
ensureCheckpointCachesAreFresh,
},
});
const result = await runCommand(makeOptions("mark-resolved", []), {
...context,
core,
});
expect(result).toBe(true);
expect(ensureCheckpointCachesAreFresh).toHaveBeenCalledTimes(1);
});
it("mark-resolved with remote-id temporarily activates it and runs markResolved", async () => {
const core = createCoreMock();
const settings = core.services.setting.currentSettings();
+26 -3
View File
@@ -37,6 +37,14 @@ defaultLoggerEnv.minLogLevel = LOG_LEVEL_DEBUG;
/** Injectable command boundary used by CLI integration probes. */
export type CliCommandRunner = (options: CLIOptions, context: CLICommandContext) => Promise<boolean>;
const SETTINGS_MANAGEMENT_COMMANDS: ReadonlySet<CLICommand> = new Set([
"setup",
"remote-add",
"remote-rm",
"remote-set",
"remote-activate",
]);
function printHelp(standardIo: StandardIo): void {
writeStdoutLine(
standardIo,
@@ -274,7 +282,10 @@ export async function main(
) {
const options = parseArgs(standardIo);
if (options.interval && options.command !== "daemon") {
writeStderrLine(standardIo, `Warning: --interval is only used in daemon mode, ignored for '${options.command}'`);
writeStderrLine(
standardIo,
`Warning: --interval is only used in daemon mode, ignored for '${options.command}'`
);
}
const avoidStdoutNoise =
options.command === "cat" ||
@@ -404,7 +415,10 @@ export async function main(
// In daemon mode the default handler must run so changes are applied to the filesystem.
if (options.command !== "daemon") {
serviceHubInstance.replication.processSynchroniseResult.addHandler(async () => {
writeStderrLine(standardIo, `[Info] Replication result received, but not processed automatically in CLI mode.`);
writeStderrLine(
standardIo,
`[Info] Replication result received, but not processed automatically in CLI mode.`
);
return await Promise.resolve(true);
}, -100);
}
@@ -506,7 +520,7 @@ export async function main(
// Save the settings file before any lifecycle events can mutate and persist them.
// suspendAllSync and other lifecycle hooks clobber sync settings in memory, and
// various code paths persist the clobbered state to disk. We restore on shutdown.
const settingsBackup = await fs.readFile(settingsPath, "utf-8").catch(() => null!);
let settingsBackup: string | null = await fs.readFile(settingsPath, "utf-8").catch(() => null);
// Restore settings file on any exit to undo lifecycle mutations.
// Write to a temp path first so a crash mid-write doesn't leave a truncated file.
@@ -576,6 +590,15 @@ export async function main(
settingsPath,
originalSyncSettings,
});
if (result && SETTINGS_MANAGEMENT_COMMANDS.has(options.command)) {
// Settings management is intentional, unlike the temporary changes made by suspendAllSync().
// setup replaces the complete configuration, while remote profile commands must retain the
// synchronisation mode which was active before the command lifecycle suspended it.
if (options.command !== "setup") {
await core.services.setting.applyPartial(originalSyncSettings, true);
}
settingsBackup = await fs.readFile(settingsPath, "utf-8");
}
if (!result) {
writeStderrLine(standardIo, `[Error] Command '${options.command}' failed`);
process.exitCode = 1;
+6
View File
@@ -22,6 +22,12 @@
"pretest:e2e:ci": "npm run build",
"test:e2e:ci": "deno task --cwd testdeno test:ci",
"test:e2e:p2p": "deno task --cwd testdeno test:p2p:compose",
"pretest:e2e:adaptive-s3": "npm run build",
"test:e2e:adaptive-s3": "deno task --cwd testdeno test:adaptive-journal-s3",
"pretest:e2e:adaptive-webdav": "npm run build",
"test:e2e:adaptive-webdav": "deno task --cwd testdeno test:adaptive-journal-webdav",
"pretest:e2e:adaptive-postgrest": "npm run build",
"test:e2e:adaptive-postgrest": "deno task --cwd testdeno test:adaptive-journal-postgrest",
"test:e2e:mirror": "bash test/test-mirror-linux.sh",
"test:e2e:remote-commands": "bash test/test-remote-commands-linux.sh",
"pretest:e2e:all": "npm run build",
@@ -25,6 +25,10 @@ type SerializableContainer =
| {
[NODE_KV_TYPED_KEY]: "ArrayBuffer";
[NODE_KV_VALUES_KEY]: number[];
}
| {
[NODE_KV_TYPED_KEY]: "BigInt";
[NODE_KV_VALUES_KEY]: string;
};
function isRecord(value: unknown): value is Record<string, unknown> {
@@ -32,6 +36,12 @@ function isRecord(value: unknown): value is Record<string, unknown> {
}
function serializeForNodeKV(value: unknown): unknown {
if (typeof value === "bigint") {
return {
[NODE_KV_TYPED_KEY]: "BigInt",
[NODE_KV_VALUES_KEY]: value.toString(10),
} satisfies SerializableContainer;
}
if (value instanceof Set) {
return {
[NODE_KV_TYPED_KEY]: "Set",
@@ -78,6 +88,9 @@ function deserializeFromNodeKV(value: unknown): unknown {
if (taggedType === "ArrayBuffer" && Array.isArray(taggedValues)) {
return Uint8Array.from(taggedValues).buffer;
}
if (taggedType === "BigInt" && typeof taggedValues === "string" && /^-?(?:0|[1-9]\d*)$/u.test(taggedValues)) {
return BigInt(taggedValues);
}
return Object.fromEntries(Object.entries(value).map(([k, v]) => [k, deserializeFromNodeKV(v)]));
}
@@ -1,8 +1,39 @@
import { describe, expect, it, vi } from "vitest";
import { createServiceContext } from "@vrtmrz/livesync-commonlib/compat/services/base/ServiceBase";
import { fsPromises as fs, os, path } from "@vrtmrz/livesync-commonlib/node";
import type { NodeKeyValueDBDependencies } from "./NodeKeyValueDBService";
import { NodeKeyValueDBService } from "./NodeKeyValueDBService";
function createInitialisableDependencies(): {
dependencies: NodeKeyValueDBDependencies;
initialise: () => Promise<boolean>;
} {
let initialise: (() => Promise<boolean>) | undefined;
const dependencies = {
appLifecycle: {
onSettingLoaded: {
addHandler: vi.fn((handler: () => Promise<boolean>) => {
initialise = handler;
}),
},
},
databaseEvents: {
onResetDatabase: { addHandler: vi.fn() },
onDatabaseInitialisation: { addHandler: vi.fn() },
onUnloadDatabase: { addHandler: vi.fn() },
onCloseDatabase: { addHandler: vi.fn() },
},
vault: {},
} as unknown as NodeKeyValueDBDependencies;
return {
dependencies,
initialise: async () => {
if (!initialise) throw new Error("Initialisation handler was not registered");
return await initialise();
},
};
}
describe("NodeKeyValueDBService.openSimpleStore", () => {
it("creates a namespaced store handle before the backing database is initialised", () => {
const dependencies = {
@@ -44,4 +75,29 @@ describe("NodeKeyValueDBService.openSimpleStore", () => {
await expect(store.get("key")).rejects.toThrow("KeyValueDB is not initialized yet");
});
it("preserves bigint values used by Adaptive Journal state", async () => {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "livesync-node-kv-bigint-"));
const filePath = path.join(tempDir, "keyvalue-db.json");
const writerState = {
lastCommittedSequence: 9007199254740993n,
pendingCommit: { sequence: 18446744073709551615n },
writerEpoch: "test-writer-epoch",
};
try {
const firstLifecycle = createInitialisableDependencies();
const first = new NodeKeyValueDBService(createServiceContext(), firstLifecycle.dependencies, filePath);
await expect(firstLifecycle.initialise()).resolves.toBe(true);
await first.openSimpleStore("adaptive").set("writer-state", writerState);
const secondLifecycle = createInitialisableDependencies();
const second = new NodeKeyValueDBService(createServiceContext(), secondLifecycle.dependencies, filePath);
await expect(secondLifecycle.initialise()).resolves.toBe(true);
await expect(second.openSimpleStore("adaptive").get("writer-state")).resolves.toEqual(writerState);
} finally {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
});
+4 -1
View File
@@ -34,7 +34,10 @@
"test:e2e-matrix:couchdb-enc0": "deno test --env-file=.test.env -A --no-check --filter='e2e matrix: COUCHDB-enc0' test-e2e-two-vaults-matrix.ts",
"test:e2e-matrix:couchdb-enc1": "deno test --env-file=.test.env -A --no-check --filter='e2e matrix: COUCHDB-enc1' test-e2e-two-vaults-matrix.ts",
"test:e2e-matrix:minio-enc0": "deno test --env-file=.test.env -A --no-check --filter='e2e matrix: MINIO-enc0' test-e2e-two-vaults-matrix.ts",
"test:e2e-matrix:minio-enc1": "deno test --env-file=.test.env -A --no-check --filter='e2e matrix: MINIO-enc1' test-e2e-two-vaults-matrix.ts"
"test:e2e-matrix:minio-enc1": "deno test --env-file=.test.env -A --no-check --filter='e2e matrix: MINIO-enc1' test-e2e-two-vaults-matrix.ts",
"test:adaptive-journal-s3": "deno test --env-file=.test.env -A --no-check test-adaptive-journal-s3.ts",
"test:adaptive-journal-webdav": "deno test --env-file=.test.env -A --no-check test-adaptive-journal-webdav.ts",
"test:adaptive-journal-postgrest": "deno test --env-file=.test.env -A --no-check test-adaptive-journal-postgrest.ts"
},
"imports": {
"@std/assert": "jsr:@std/assert@^1.0.13",
@@ -0,0 +1,12 @@
do $$
begin
if not exists (select 1 from pg_roles where rolname = 'livesync_postgrest_anon') then
create role livesync_postgrest_anon nologin;
end if;
if not exists (select 1 from pg_roles where rolname = 'livesync_postgrest_authenticator') then
create role livesync_postgrest_authenticator noinherit login password 'integration-password';
end if;
end
$$;
grant livesync_postgrest_anon to livesync_postgrest_authenticator;
@@ -0,0 +1,6 @@
insert into livesync_private.adaptive_vaults (vault_id, credential_digest)
values (
'adaptive-cli-vault-01',
livesync_private.sha256(convert_to('adaptive-cli-vault-credential-0000000000001', 'UTF8'))
)
on conflict (vault_id) do update set credential_digest = excluded.credential_digest;
+11 -2
View File
@@ -23,6 +23,7 @@ export interface CliResult {
export const TEE_ENABLED = Deno.env.get("LIVESYNC_TEST_TEE") === "1";
const VERBOSE_ENABLED = Deno.env.get("LIVESYNC_CLI_VERBOSE") === "1";
const DEBUG_ENABLED = Deno.env.get("LIVESYNC_CLI_DEBUG") === "1";
const SETUP_URI_PREFIX = "obsidian://setuplivesync?settings=";
function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
@@ -39,8 +40,16 @@ function concatChunks(chunks: Uint8Array[]): Uint8Array {
return out;
}
export function redactCliSensitiveText(value: string): string {
return value
.replace(/(obsidian:\/\/setuplivesync\?settings=)[^\s"']+/gu, "$1<redacted>")
.replace(/(sls\+[^:\s]+:\/\/)[^/?#@\s]*@/gu, "$1<redacted>@");
}
export function formatTeeCommand(args: string[]): string {
return ["node", CLI_DIST, ...args].map((part) => JSON.stringify(part)).join(" ");
const redactArgument = (argument: string): string =>
argument.startsWith(SETUP_URI_PREFIX) ? `${SETUP_URI_PREFIX}<redacted>` : redactCliSensitiveText(argument);
return ["node", CLI_DIST, ...args.map(redactArgument)].map((part) => JSON.stringify(part)).join(" ");
}
export function createLineTeeWriter(
@@ -57,7 +66,7 @@ export function createLineTeeWriter(
writer(enc.encode(`[CLI tee pid=${pid}:${streamName}]\n`));
headerWritten = true;
}
writer(enc.encode(`[CLI tee pid=${pid}:${streamName}] ${line}\n`));
writer(enc.encode(`[CLI tee pid=${pid}:${streamName}] ${redactCliSensitiveText(line)}\n`));
};
const flush = (final = false) => {
+487 -12
View File
@@ -6,6 +6,8 @@
* available — including Windows — without needing bash.
*/
import { join } from "@std/path";
type DockerInvoker = {
bin: string;
prefix: string[];
@@ -15,6 +17,7 @@ type DockerInvoker = {
let dockerInvokerPromise: Promise<DockerInvoker> | null = null;
const DOCKER_TEE = Deno.env.get("LIVESYNC_DOCKER_TEE") === "1" || Deno.env.get("LIVESYNC_TEST_TEE") === "1";
const trackedContainers = new Set<string>();
const trackedNetworks = new Set<string>();
const CLEANUP_SIGNALS: Deno.Signal[] = ["SIGINT", "SIGTERM"];
let signalCleanupHandlersInstalled = false;
let signalCleanupInProgress = false;
@@ -62,17 +65,28 @@ async function collectStream(
return out;
}
async function runCommand(bin: string, args: string[]): Promise<{ code: number; stdout: string; stderr: string }> {
async function runCommand(
bin: string,
args: string[],
stdinData?: Uint8Array
): Promise<{ code: number; stdout: string; stderr: string }> {
try {
const child = new Deno.Command(bin, {
args,
stdin: "null",
stdin: stdinData ? "piped" : "null",
stdout: "piped",
stderr: "piped",
}).spawn();
const stdoutPromise = collectStream(child.stdout, DOCKER_TEE ? (chunk) => Deno.stdout.writeSync(chunk) : null);
const stderrPromise = collectStream(child.stderr, DOCKER_TEE ? (chunk) => Deno.stderr.writeSync(chunk) : null);
const [status, stdout, stderr] = await Promise.all([child.status, stdoutPromise, stderrPromise]);
const stdinPromise = stdinData
? (async () => {
const writer = child.stdin.getWriter();
await writer.write(stdinData);
await writer.close();
})()
: Promise.resolve();
const [status, stdout, stderr] = await Promise.all([child.status, stdoutPromise, stderrPromise, stdinPromise]);
const dec = new TextDecoder();
const result = {
code: status.code,
@@ -163,6 +177,20 @@ async function getDockerInvoker(): Promise<DockerInvoker> {
}
async function docker(...args: string[]): Promise<{ code: number; stdout: string; stderr: string }> {
return await dockerCommand(undefined, ...args);
}
async function dockerWithInput(
input: Uint8Array,
...args: string[]
): Promise<{ code: number; stdout: string; stderr: string }> {
return await dockerCommand(input, ...args);
}
async function dockerCommand(
input: Uint8Array | undefined,
...args: string[]
): Promise<{ code: number; stdout: string; stderr: string }> {
const invoker = await getDockerInvoker();
// Either:
@@ -176,7 +204,7 @@ async function docker(...args: string[]): Promise<{ code: number; stdout: string
: args
: [...invoker.prefix, ...args];
const r = await runCommand(invoker.bin, finalArgs);
const r = await runCommand(invoker.bin, finalArgs, input);
return { code: r.code, stdout: r.stdout, stderr: r.stderr };
}
@@ -195,12 +223,21 @@ async function stopAndRemoveContainer(container: string): Promise<void> {
async function cleanupTrackedContainers(reason: string): Promise<void> {
const names = [...trackedContainers];
if (names.length === 0) return;
if (names.length > 0) {
console.warn(`[WARN] cleaning up tracked containers on ${reason}: ${names.join(", ")}`);
for (const container of names.reverse()) {
await stopAndRemoveContainer(container);
trackedContainers.delete(container);
}
}
console.warn(`[WARN] cleaning up tracked containers on ${reason}: ${names.join(", ")}`);
for (const container of names.reverse()) {
await stopAndRemoveContainer(container);
trackedContainers.delete(container);
const networks = [...trackedNetworks];
if (networks.length > 0) {
console.warn(`[WARN] cleaning up tracked networks on ${reason}: ${networks.join(", ")}`);
for (const network of networks.reverse()) {
await docker("network", "rm", network).catch(() => {});
trackedNetworks.delete(network);
}
}
}
@@ -250,7 +287,19 @@ function trackContainer(container: string): void {
function untrackContainer(container: string): void {
trackedContainers.delete(container);
if (trackedContainers.size === 0) {
if (trackedContainers.size === 0 && trackedNetworks.size === 0) {
removeSignalCleanupHandlers();
}
}
function trackNetwork(network: string): void {
ensureSignalCleanupHandlers();
trackedNetworks.add(network);
}
function untrackNetwork(network: string): void {
trackedNetworks.delete(network);
if (trackedContainers.size === 0 && trackedNetworks.size === 0) {
removeSignalCleanupHandlers();
}
}
@@ -327,8 +376,49 @@ const COUCHDB_CONTAINER = "couchdb-test";
const COUCHDB_IMAGE = "couchdb:3.5.0";
const MINIO_CONTAINER = "minio-test";
const MINIO_IMAGE = "minio/minio";
const MINIO_MC_IMAGE = "minio/mc";
const MINIO_IMAGE = "minio/minio:RELEASE.2025-04-22T22-12-26Z";
const MINIO_MC_IMAGE = "minio/mc:RELEASE.2025-04-16T18-13-26Z";
const WEBDAV_CONTAINER = "webdav-test";
const WEBDAV_IMAGE = "httpd:2.4.68";
const POSTGRES_CONTAINER = "postgrest-postgres-test";
const POSTGRES_IMAGE = "postgres:15-alpine";
const POSTGREST_CONTAINER = "postgrest-test";
const POSTGREST_IMAGE = "postgrest/postgrest:v14.16";
const POSTGREST_NETWORK = "postgrest-test";
const POSTGREST_DATABASE_PASSWORD = "integration-password";
const POSTGREST_VAULT_ID = "adaptive-cli-vault-01";
const POSTGREST_VAULT_CREDENTIAL = "adaptive-cli-vault-credential-0000000000001";
const WEBDAV_HTTPD_CONFIG = `ServerRoot "/usr/local/apache2"
Listen 80
LoadModule mpm_event_module modules/mod_mpm_event.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule dav_module modules/mod_dav.so
LoadModule dav_fs_module modules/mod_dav_fs.so
LoadModule unixd_module modules/mod_unixd.so
User www-data
Group www-data
ServerName localhost
DocumentRoot "/usr/local/apache2/htdocs"
PidFile "/tmp/httpd.pid"
ErrorLog "/proc/self/fd/2"
LogLevel warn
DavLockDB "/usr/local/apache2/var/DavLock"
DavLockDiscovery Off
<Directory "/usr/local/apache2/htdocs">
AllowOverride None
Require all granted
</Directory>
<Directory "/usr/local/apache2/htdocs/dav">
Dav On
</Directory>
`;
export async function stopCouchdb(): Promise<void> {
await stopAndRemoveContainer(COUCHDB_CONTAINER);
@@ -466,6 +556,70 @@ export async function stopMinio(): Promise<void> {
untrackContainer(MINIO_CONTAINER);
}
export async function listMinioObjectKeys(
minioEndpoint: string,
accessKey: string,
secretKey: string,
bucket: string
): Promise<string[]> {
const cmd =
`mc alias set myminio ${shQuote(minioEndpoint)} ${shQuote(accessKey)} ${shQuote(secretKey)} >/dev/null 2>&1 && ` +
`mc ls --recursive --json myminio/${shQuote(bucket)}`;
const result = await docker(
"run",
"--rm",
"--network",
"host",
"--entrypoint",
"/bin/sh",
MINIO_MC_IMAGE,
"-c",
cmd
);
if (result.code !== 0) {
throw new Error(`Could not list MinIO objects: ${result.stderr.trim()}`);
}
return result.stdout
.split(/\r?\n/u)
.filter((line) => line.trim().length > 0)
.map((line) => JSON.parse(line) as { key?: unknown })
.map(({ key }) => {
if (typeof key !== "string") {
throw new Error("MinIO returned an object without a string key");
}
return key;
})
.sort();
}
export async function readMinioObjectText(
minioEndpoint: string,
accessKey: string,
secretKey: string,
bucket: string,
key: string
): Promise<string> {
const cmd =
`mc alias set myminio ${shQuote(minioEndpoint)} ${shQuote(accessKey)} ${shQuote(secretKey)} >/dev/null 2>&1 && ` +
`mc cat myminio/${shQuote(bucket)}/${shQuote(key)}`;
const result = await docker(
"run",
"--rm",
"--network",
"host",
"--entrypoint",
"/bin/sh",
MINIO_MC_IMAGE,
"-c",
cmd
);
if (result.code !== 0) {
throw new Error(`Could not read MinIO object ${key}: ${result.stderr.trim()}`);
}
return result.stdout;
}
async function initMinioBucket(
minioEndpoint: string,
accessKey: string,
@@ -561,6 +715,327 @@ export async function startMinio(
await waitForMinioBucket(minioEndpoint, accessKey, secretKey, bucket);
}
// ---------------------------------------------------------------------------
// WebDAV
// ---------------------------------------------------------------------------
export async function stopWebDAV(): Promise<void> {
await stopAndRemoveContainer(WEBDAV_CONTAINER);
untrackContainer(WEBDAV_CONTAINER);
}
async function waitForWebDAV(endpoint: string): Promise<void> {
for (let attempt = 0; attempt < 30; attempt += 1) {
try {
const response = await fetch(endpoint, {
method: "PROPFIND",
headers: { Depth: "0" },
signal: AbortSignal.timeout(3000),
});
await response.body?.cancel().catch(() => {});
if (response.status === 207) return;
} catch {
// The container is still starting.
}
await sleep(500);
}
throw new Error(`WebDAV collection did not become ready: ${endpoint}`);
}
export async function startWebDAV(endpoint: string): Promise<void> {
const url = new URL(endpoint);
if (url.protocol !== "http:" || (url.hostname !== "127.0.0.1" && url.hostname !== "localhost")) {
throw new Error(`Managed WebDAV requires a local HTTP endpoint, received: ${endpoint}`);
}
if (url.pathname.replace(/\/+$/u, "") !== "/dav") {
throw new Error(`Managed WebDAV requires the /dav collection, received: ${endpoint}`);
}
const hostPort = url.port || "80";
const encodedConfig = btoa(WEBDAV_HTTPD_CONFIG);
const startCommand = `printf '%s' '${encodedConfig}' | base64 -d > /tmp/httpd.conf && exec httpd -DFOREGROUND -f /tmp/httpd.conf`;
console.log("[INFO] stopping leftover WebDAV container if present");
await stopWebDAV().catch(() => {});
console.log("[INFO] starting Apache WebDAV test container");
await dockerOrFail(
"run",
"-d",
"--name",
WEBDAV_CONTAINER,
"-p",
`${hostPort}:80`,
"--tmpfs",
"/usr/local/apache2/htdocs/dav:mode=0777",
"--tmpfs",
"/usr/local/apache2/var:mode=0777",
"--entrypoint",
"/bin/sh",
WEBDAV_IMAGE,
"-c",
startCommand
);
trackContainer(WEBDAV_CONTAINER);
await waitForWebDAV(endpoint);
}
function directWebDAVObjectUrl(collectionEndpoint: string, key: string): string {
return `${collectionEndpoint.replace(/\/+$/u, "")}/${encodeURIComponent(key)}`;
}
function webDAVRequestHeaders(): HeadersInit {
const username = Deno.env.get("WEBDAV_USERNAME") ?? "";
const password = Deno.env.get("WEBDAV_PASSWORD") ?? "";
if (!username && !password) return {};
return { Authorization: `Basic ${btoa(`${username}:${password}`)}` };
}
export async function listWebDAVObjectKeys(collectionEndpoint: string): Promise<string[]> {
const collectionUrl = new URL(`${collectionEndpoint.replace(/\/+$/u, "")}/`);
const response = await fetch(collectionUrl, {
method: "PROPFIND",
headers: { ...webDAVRequestHeaders(), Depth: "1" },
});
if (response.status !== 207) {
throw new Error(`Could not list WebDAV objects: HTTP ${response.status}`);
}
const xml = await response.text();
const hrefs = [
...xml.matchAll(/<(?:[A-Za-z_][\w.-]*:)?href\b[^>]*>([\s\S]*?)<\/(?:[A-Za-z_][\w.-]*:)?href>/giu),
].map((match) => match[1].trim());
const basePath = decodeURIComponent(collectionUrl.pathname);
const keys = new Set<string>();
for (const href of hrefs) {
const path = decodeURIComponent(new URL(href, collectionUrl).pathname);
if (!path.startsWith(basePath)) continue;
const key = path.slice(basePath.length).replace(/\/$/u, "");
if (key && !key.includes("/")) keys.add(key);
}
return [...keys].sort();
}
export async function readWebDAVObjectText(collectionEndpoint: string, key: string): Promise<string> {
const response = await fetch(directWebDAVObjectUrl(collectionEndpoint, key), { headers: webDAVRequestHeaders() });
if (!response.ok) {
throw new Error(`Could not read WebDAV object ${key}: HTTP ${response.status}`);
}
return await response.text();
}
// ---------------------------------------------------------------------------
// PostgREST
// ---------------------------------------------------------------------------
export interface PostgRESTFixture {
endpoint: string;
vaultCredential: string;
vaultId: string;
}
export interface PostgRESTAdaptiveRowCounts {
chunks: number;
commits: number;
manifests: number;
writers: number;
}
async function waitForPostgres(): Promise<void> {
let consecutiveReadyChecks = 0;
for (let attempt = 0; attempt < 60; attempt += 1) {
const result = await docker("exec", POSTGRES_CONTAINER, "pg_isready", "-U", "postgres");
if (result.code === 0) {
consecutiveReadyChecks += 1;
if (consecutiveReadyChecks >= 3) return;
} else {
consecutiveReadyChecks = 0;
}
await sleep(500);
}
throw new Error("PostgreSQL did not become ready in time");
}
async function applyPostgresSql(sql: string): Promise<void> {
const result = await dockerWithInput(
new TextEncoder().encode(sql),
"exec",
"-i",
POSTGRES_CONTAINER,
"psql",
"-v",
"ON_ERROR_STOP=1",
"-U",
"postgres",
"-d",
"postgres"
);
if (result.code !== 0) {
throw new Error(`Could not initialise PostgreSQL for PostgREST: ${result.stderr.trim()}`);
}
}
async function waitForPostgREST(fixture: PostgRESTFixture): Promise<void> {
const capabilityUrl = `${fixture.endpoint.replace(/\/+$/u, "")}/rpc/livesync_adaptive_capabilities`;
for (let attempt = 0; attempt < 30; attempt += 1) {
try {
const response = await fetch(capabilityUrl, {
method: "POST",
headers: {
Accept: "application/json",
"Accept-Profile": "livesync_api",
"Content-Profile": "livesync_api",
"Content-Type": "application/json",
"X-LiveSync-Vault-Credential": fixture.vaultCredential,
"X-LiveSync-Vault-ID": fixture.vaultId,
},
body: "{}",
signal: AbortSignal.timeout(3000),
});
await response.body?.cancel().catch(() => {});
if (response.ok) return;
} catch {
// PostgREST or its schema cache is still starting.
}
await sleep(500);
}
throw new Error(`PostgREST did not become ready: ${fixture.endpoint}`);
}
export async function stopPostgREST(): Promise<void> {
await stopAndRemoveContainer(POSTGREST_CONTAINER);
untrackContainer(POSTGREST_CONTAINER);
await stopAndRemoveContainer(POSTGRES_CONTAINER);
untrackContainer(POSTGRES_CONTAINER);
await docker("network", "rm", POSTGREST_NETWORK).catch(() => {});
untrackNetwork(POSTGREST_NETWORK);
}
export async function startPostgREST(endpoint: string): Promise<PostgRESTFixture> {
const endpointUrl = new URL(endpoint);
if (
endpointUrl.protocol !== "http:" ||
(endpointUrl.hostname !== "127.0.0.1" && endpointUrl.hostname !== "localhost")
) {
throw new Error(`Managed PostgREST requires a local HTTP endpoint, received: ${endpoint}`);
}
if (endpointUrl.pathname !== "/" || endpointUrl.search || endpointUrl.hash) {
throw new Error(`Managed PostgREST requires a root endpoint without query parameters, received: ${endpoint}`);
}
const fixture: PostgRESTFixture = {
endpoint: endpoint.replace(/\/+$/u, ""),
vaultCredential: POSTGREST_VAULT_CREDENTIAL,
vaultId: POSTGREST_VAULT_ID,
};
const fixtureDirectory = join(import.meta.dirname!, "..", "fixtures", "postgrest");
const commonlibSqlPath = join(
import.meta.dirname!,
"..",
"..",
"..",
"..",
"..",
"node_modules",
"@vrtmrz",
"livesync-commonlib",
"sql",
"postgrest",
"adaptive_journal_v1.sql"
);
console.log("[INFO] stopping leftover PostgREST test services if present");
await stopPostgREST().catch(() => {});
try {
await dockerOrFail("network", "create", POSTGREST_NETWORK);
trackNetwork(POSTGREST_NETWORK);
console.log("[INFO] starting PostgreSQL test container");
await dockerOrFail(
"run",
"-d",
"--name",
POSTGRES_CONTAINER,
"--network",
POSTGREST_NETWORK,
"--network-alias",
"postgres",
"-e",
`POSTGRES_PASSWORD=${POSTGREST_DATABASE_PASSWORD}`,
POSTGRES_IMAGE
);
trackContainer(POSTGRES_CONTAINER);
await waitForPostgres();
for (const sqlPath of [
join(fixtureDirectory, "00_roles.sql"),
commonlibSqlPath,
join(fixtureDirectory, "02_vault.sql"),
]) {
await applyPostgresSql(await Deno.readTextFile(sqlPath));
}
console.log("[INFO] starting PostgREST test container");
await dockerOrFail(
"run",
"-d",
"--name",
POSTGREST_CONTAINER,
"--network",
POSTGREST_NETWORK,
"-p",
`${endpointUrl.port || "80"}:3000`,
"-e",
"PGRST_DB_ANON_ROLE=livesync_postgrest_anon",
"-e",
"PGRST_DB_SCHEMAS=livesync_api",
"-e",
`PGRST_DB_URI=postgres://livesync_postgrest_authenticator:${POSTGREST_DATABASE_PASSWORD}@postgres:5432/postgres`,
"-e",
"PGRST_SERVER_PORT=3000",
POSTGREST_IMAGE
);
trackContainer(POSTGREST_CONTAINER);
await waitForPostgREST(fixture);
return fixture;
} catch (error) {
await stopPostgREST().catch(() => {});
throw error;
}
}
export async function readPostgRESTAdaptiveRowCounts(vaultId: string): Promise<PostgRESTAdaptiveRowCounts> {
if (!/^[A-Za-z0-9_-]{16,128}$/u.test(vaultId)) {
throw new Error(`Invalid PostgREST test Vault ID: ${vaultId}`);
}
const query = `select
(select count(*) from livesync_private.adaptive_v1_manifests where vault_id = '${vaultId}'),
(select count(*) from livesync_private.adaptive_v1_chunks where vault_id = '${vaultId}'),
(select count(*) from livesync_private.adaptive_v1_writers where vault_id = '${vaultId}'),
(select count(*) from livesync_private.adaptive_v1_commits where vault_id = '${vaultId}')`;
const output = await dockerOrFail(
"exec",
POSTGRES_CONTAINER,
"psql",
"-At",
"-F",
",",
"-U",
"postgres",
"-d",
"postgres",
"-c",
query
);
const [manifests, chunks, writers, commits] = output
.trim()
.split(",")
.map((value) => Number(value));
if (![manifests, chunks, writers, commits].every(Number.isSafeInteger)) {
throw new Error(`Could not parse PostgREST row counts: ${output}`);
}
return { chunks, commits, manifests, writers };
}
// ---------------------------------------------------------------------------
// P2P relay (strfry)
// ---------------------------------------------------------------------------
+49 -14
View File
@@ -13,7 +13,12 @@ export async function initSettingsFile(settingsFile: string): Promise<void> {
* Generate a full setup URI from a settings file via the Commonlib package API.
* Mirrors the bash flow in test-setup-put-cat-linux.sh.
*/
export async function generateSetupUriFromSettings(settingsFile: string, setupPassphrase: string): Promise<string> {
export async function generateSetupUriFromSettings(
settingsFile: string,
setupPassphrase: string,
preserveSettings = false,
runtimeVaultPassphrase?: string
): Promise<string> {
const script = [
"import { fs } from '@vrtmrz/livesync-commonlib/node';",
"import { encodeSettingsToSetupURI } from '@vrtmrz/livesync-commonlib/compat/API/processSetting';",
@@ -21,13 +26,18 @@ export async function generateSetupUriFromSettings(settingsFile: string, setupPa
" const settingsPath = process.env.SETTINGS_FILE;",
" const passphrase = process.env.SETUP_PASSPHRASE;",
" const settings = JSON.parse(fs.readFileSync(settingsPath, 'utf-8'));",
" settings.couchDB_DBNAME = 'setup-put-cat-db';",
" settings.couchDB_URI = 'http://127.0.0.1:5999';",
" settings.couchDB_USER = 'dummy';",
" settings.couchDB_PASSWORD = 'dummy';",
" settings.liveSync = false;",
" settings.syncOnStart = false;",
" settings.syncOnSave = false;",
" if (process.env.RUNTIME_VAULT_PASSPHRASE !== undefined) {",
" settings.passphrase = process.env.RUNTIME_VAULT_PASSPHRASE;",
" }",
" if (process.env.PRESERVE_SETTINGS !== 'true') {",
" settings.couchDB_DBNAME = 'setup-put-cat-db';",
" settings.couchDB_URI = 'http://127.0.0.1:5999';",
" settings.couchDB_USER = 'dummy';",
" settings.couchDB_PASSWORD = 'dummy';",
" settings.liveSync = false;",
" settings.syncOnStart = false;",
" settings.syncOnSave = false;",
" }",
" const uri = await encodeSettingsToSetupURI(settings, passphrase);",
" process.stdout.write(uri.trim());",
"})();",
@@ -41,13 +51,18 @@ export async function generateSetupUriFromSettings(settingsFile: string, setupPa
await Deno.writeTextFile(scriptPath, script);
try {
const env: Record<string, string> = {
SETTINGS_FILE: settingsFile,
SETUP_PASSPHRASE: setupPassphrase,
PRESERVE_SETTINGS: preserveSettings ? "true" : "false",
};
if (runtimeVaultPassphrase !== undefined) {
env.RUNTIME_VAULT_PASSPHRASE = runtimeVaultPassphrase;
}
const cmd = new Deno.Command("npx", {
args: ["tsx", scriptPath],
cwd: CLI_DIR,
env: {
SETTINGS_FILE: settingsFile,
SETUP_PASSPHRASE: setupPassphrase,
},
env,
stdin: "null",
stdout: "piped",
stderr: "piped",
@@ -112,7 +127,7 @@ export async function applyCouchdbSettings(
export async function applyRemoteSyncSettings(
settingsFile: string,
options: {
remoteType: "COUCHDB" | "MINIO";
remoteType: "COUCHDB" | "MINIO" | "POSTGREST" | "WEBDAV";
couchdbUri?: string;
couchdbUser?: string;
couchdbPassword?: string;
@@ -121,10 +136,15 @@ export async function applyRemoteSyncSettings(
minioEndpoint?: string;
minioAccessKey?: string;
minioSecretKey?: string;
postgRESTConnectionURI?: string;
webDAVConnectionURI?: string;
encrypt?: boolean;
passphrase?: string;
enableCompression?: boolean;
usePathObfuscation?: boolean;
journalFormat?: "adaptive-v1" | "opaque-v1";
expectedRepositoryId?: string;
packReadPolicy?: "range" | "whole-pack";
}
): Promise<void> {
const data = JSON.parse(await Deno.readTextFile(settingsFile));
@@ -135,7 +155,7 @@ export async function applyRemoteSyncSettings(
data.couchDB_USER = options.couchdbUser;
data.couchDB_PASSWORD = options.couchdbPassword;
data.couchDB_DBNAME = options.couchdbDbname;
} else {
} else if (options.remoteType === "MINIO") {
data.remoteType = "MINIO";
data.bucket = options.minioBucket;
data.endpoint = options.minioEndpoint;
@@ -143,6 +163,21 @@ export async function applyRemoteSyncSettings(
data.secretKey = options.minioSecretKey;
data.region = "auto";
data.forcePathStyle = true;
} else if (options.remoteType === "POSTGREST") {
data.remoteType = "POSTGREST";
data.postgrestActiveConnectionURI = options.postgRESTConnectionURI;
} else {
data.remoteType = "WEBDAV";
data.webDAVactiveConnectionURI = options.webDAVConnectionURI;
}
if (options.journalFormat !== undefined) {
data.journalFormat = options.journalFormat;
}
if (options.expectedRepositoryId !== undefined) {
data.expectedRepositoryId = options.expectedRepositoryId;
}
if (options.packReadPolicy !== undefined) {
data.packReadPolicy = options.packReadPolicy;
}
data.liveSync = true;
@@ -0,0 +1,26 @@
import { startPostgREST, stopPostgREST } from "./helpers/docker.ts";
const action = Deno.args[0];
const endpoint = (
Deno.env.get("POSTGREST_ENDPOINT") ??
Deno.env.get("postgrestEndpoint") ??
"http://127.0.0.1:3001"
).replace(/\/+$/u, "");
try {
if (action === "start") {
await startPostgREST(endpoint);
} else if (action === "stop") {
await stopPostgREST();
} else {
throw new Error("Usage: manage-postgrest-fixture.ts <start|stop>");
}
} catch (error) {
if (action === "start") await stopPostgREST().catch(() => undefined);
console.error(error instanceof Error ? error.stack : error);
Deno.exit(1);
}
// The one-shot command intentionally leaves successful services running after
// 'start'; the matching 'stop' command owns their removal.
Deno.exit(0);
+3
View File
@@ -11,6 +11,9 @@ const TASKS = [
"test:e2e-matrix:couchdb-enc1",
"test:e2e-matrix:minio-enc0",
"test:e2e-matrix:minio-enc1",
"test:adaptive-journal-s3",
"test:adaptive-journal-webdav",
"test:adaptive-journal-postgrest",
] as const;
for (const [index, task] of TASKS.entries()) {
@@ -0,0 +1,190 @@
import { assert, assertEquals } from "@std/assert";
import { TempDir } from "./helpers/temp.ts";
import {
assertFilesEqual,
redactCliSensitiveText,
runCli,
runCliOrFail,
runCliWithInputOrFail,
sanitiseCatStdout,
} from "./helpers/cli.ts";
import { applyRemoteSyncSettings, generateSetupUriFromSettings, initSettingsFile } from "./helpers/settings.ts";
import { type PostgRESTFixture, startPostgREST, stopPostgREST } from "./helpers/docker.ts";
const BINARY_TEST_BYTES = 2 * 1024 * 1024;
function deterministicBytes(length: number, seed: number): Uint8Array {
const bytes = new Uint8Array(length);
let state = seed;
for (let index = 0; index < bytes.byteLength; index += 1) {
state ^= state << 13;
state ^= state >>> 17;
state ^= state << 5;
bytes[index] = state & 0xff;
}
return bytes;
}
function postgRESTConnectionURI(fixture: PostgRESTFixture): string {
const endpointUrl = new URL(fixture.endpoint);
const proxyUrl = new URL(`https://${endpointUrl.host}${endpointUrl.pathname}`);
proxyUrl.username = fixture.vaultId;
proxyUrl.password = fixture.vaultCredential;
if (endpointUrl.protocol === "http:") proxyUrl.searchParams.set("insecure", "true");
return `sls+postgrest:${proxyUrl.toString().slice("https:".length)}`;
}
function remoteIdFromListing(listing: string): string {
const line = listing
.split(/\r?\n/u)
.find((candidate) => candidate.includes("\tPostgREST E2E\t") || candidate.includes("\tPostgREST "));
const id = line?.split("\t", 1)[0];
if (!id) throw new Error(`PostgREST remote profile was not listed:\n${listing}`);
return id;
}
function externalFixture(endpoint: string): PostgRESTFixture {
return {
endpoint,
vaultCredential:
Deno.env.get("POSTGREST_VAULT_CREDENTIAL") ??
Deno.env.get("postgrestVaultCredential") ??
"adaptive-cli-vault-credential-0000000000001",
vaultId: Deno.env.get("POSTGREST_VAULT_ID") ?? Deno.env.get("postgrestVaultId") ?? "adaptive-cli-vault-01",
};
}
Deno.test("CLI tee output redacts a PostgREST Vault credential", () => {
assertEquals(
redactCliSensitiveText(
"exported sls+postgrest://adaptive-cli-vault-01:adaptive-cli-vault-credential@example.test/rest/v1"
),
"exported sls+postgrest://<redacted>@example.test/rest/v1"
);
});
Deno.test("e2e: two CLI vaults synchronise through Adaptive Journal PostgREST", async () => {
const suffix = `${Date.now()}-${Math.floor(Math.random() * 100000)}`;
const endpoint = (
Deno.env.get("POSTGREST_ENDPOINT") ??
Deno.env.get("postgrestEndpoint") ??
"http://127.0.0.1:3001"
).replace(/\/+$/u, "");
const vaultPassphrase = "adaptive-journal-postgrest-cli-e2ee";
const setupPassphrase = "adaptive-journal-postgrest-cli-setup";
const shouldStartDocker = Deno.env.get("LIVESYNC_START_DOCKER") !== "0";
const keepDocker = Deno.env.get("LIVESYNC_DEBUG_KEEP_DOCKER") === "1";
const fixture = shouldStartDocker ? await startPostgREST(endpoint) : externalFixture(endpoint);
const connectionURI = postgRESTConnectionURI(fixture);
await using workDir = await TempDir.create("livesync-cli-adaptive-journal-postgrest");
const vaultA = workDir.join("vault-a");
const vaultB = workDir.join("vault-b");
const settingsA = workDir.join("settings-a.json");
const settingsB = workDir.join("settings-b.json");
const binarySourceA = workDir.join("source-a.bin");
const binarySourceB = workDir.join("source-b.bin");
const binaryDestinationA = workDir.join("destination-a.bin");
const binaryDestinationB = workDir.join("destination-b.bin");
await Deno.mkdir(vaultA, { recursive: true });
await Deno.mkdir(vaultB, { recursive: true });
try {
await initSettingsFile(settingsA);
await applyRemoteSyncSettings(settingsA, {
remoteType: "POSTGREST",
postgRESTConnectionURI: connectionURI,
encrypt: true,
passphrase: vaultPassphrase,
enableCompression: false,
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
});
const addedRemote = await runCliOrFail(
vaultA,
"--settings",
settingsA,
"remote-add",
"PostgREST E2E",
connectionURI
);
const remoteId = addedRemote.trim().split("\t", 1)[0];
assert(remoteId, `remote-add did not return a profile ID:\n${addedRemote}`);
await runCliOrFail(vaultA, "--settings", settingsA, "remote-activate", remoteId);
const textPath = "adaptive/text.md";
const binaryPath = "adaptive/data.bin";
await runCliWithInputOrFail(`created-by-a-${suffix}\n`, vaultA, "--settings", settingsA, "put", textPath);
await Deno.writeFile(binarySourceA, deterministicBytes(BINARY_TEST_BYTES, 0x1a2b3c4d));
await runCliOrFail(vaultA, "--settings", settingsA, "push", binarySourceA, binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
const remoteListing = await runCliOrFail(vaultA, "--settings", settingsA, "remote-ls");
assertEquals(remoteIdFromListing(remoteListing), remoteId);
assert(
remoteListing
.split(/\r?\n/u)
.some((line) => line.startsWith(`${remoteId}\t`) && line.includes("\tactive\t")),
`Activated PostgREST profile was not listed as active:\n${remoteListing}`
);
const exportedConnection = (
await runCliOrFail(vaultA, "--settings", settingsA, "remote-export", remoteId)
).trim();
assert(exportedConnection.startsWith("sls+postgrest://"));
assert(exportedConnection.includes("journalFormat=adaptive-v1"));
assert(!exportedConnection.includes("packReadPolicy="));
const setupURI = await generateSetupUriFromSettings(settingsA, setupPassphrase, true, vaultPassphrase);
await initSettingsFile(settingsB);
await runCliWithInputOrFail(`${setupPassphrase}\n`, vaultB, "--settings", settingsB, "setup", setupURI);
const settingsAfterSetup = JSON.parse(await Deno.readTextFile(settingsB)) as {
encryptedPassphrase?: string;
postgrestActiveConnectionURI?: string;
};
assert(
typeof settingsAfterSetup.encryptedPassphrase === "string" &&
settingsAfterSetup.encryptedPassphrase.length > 0,
"setup did not persist the encrypted Vault passphrase"
);
assertEquals(settingsAfterSetup.postgrestActiveConnectionURI, connectionURI);
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultB, "--settings", settingsB, "cat", textPath)).trimEnd(),
`created-by-a-${suffix}`
);
await runCliOrFail(vaultB, "--settings", settingsB, "pull", binaryPath, binaryDestinationB);
await assertFilesEqual(binarySourceA, binaryDestinationB, "Adaptive Journal PostgREST transfer differs");
await runCliWithInputOrFail(`updated-by-b-${suffix}\n`, vaultB, "--settings", settingsB, "put", textPath);
await Deno.writeFile(binarySourceB, deterministicBytes(BINARY_TEST_BYTES, 0x5e6f7788));
await runCliOrFail(vaultB, "--settings", settingsB, "push", binarySourceB, binaryPath);
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultA, "--settings", settingsA, "cat", textPath)).trimEnd(),
`updated-by-b-${suffix}`
);
await runCliOrFail(vaultA, "--settings", settingsA, "pull", binaryPath, binaryDestinationA);
await assertFilesEqual(binarySourceB, binaryDestinationA, "Adaptive Journal PostgREST return transfer differs");
await runCliOrFail(vaultA, "--settings", settingsA, "rm", binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
const deleted = await runCli(vaultB, "--settings", settingsB, "cat", binaryPath);
assert(deleted.code !== 0, `Deleted binary remained readable:\n${deleted.combined}`);
const statusOutput = await runCliOrFail(vaultA, "--settings", settingsA, "remote-status", remoteId);
const statusJsonStart = statusOutput.indexOf("{");
assert(statusJsonStart >= 0, `PostgREST remote status did not contain JSON:\n${statusOutput}`);
const status = JSON.parse(statusOutput.slice(statusJsonStart)) as { estimatedSize?: unknown };
assert(
typeof status.estimatedSize === "number" && status.estimatedSize > 0,
`PostgREST remote status did not report a positive size: ${JSON.stringify(status)}`
);
} finally {
if (shouldStartDocker && !keepDocker) {
await stopPostgREST().catch(() => {});
}
}
});
@@ -0,0 +1,117 @@
import { assert, assertEquals } from "@std/assert";
import { TempDir } from "./helpers/temp.ts";
import { assertFilesEqual, runCli, runCliOrFail, runCliWithInputOrFail, sanitiseCatStdout } from "./helpers/cli.ts";
import { applyRemoteSyncSettings, initSettingsFile } from "./helpers/settings.ts";
import { startMinio, stopMinio } from "./helpers/docker.ts";
const EXTERNAL_PACK_TEST_BYTES = 9 * 1024 * 1024;
function deterministicBytes(length: number, seed: number): Uint8Array {
const bytes = new Uint8Array(length);
let state = seed;
for (let index = 0; index < bytes.byteLength; index += 1) {
state ^= state << 13;
state ^= state >>> 17;
state ^= state << 5;
bytes[index] = state & 0xff;
}
return bytes;
}
function requireEnv(...keys: string[]): string {
for (const key of keys) {
const value = Deno.env.get(key)?.trim();
if (value) return value;
}
throw new Error(`Required environment variable is missing: ${keys.join(" or ")}`);
}
Deno.test("e2e: two CLI vaults synchronise through Adaptive Journal S3", async () => {
const suffix = `${Date.now()}-${Math.floor(Math.random() * 100000)}`;
const endpoint = requireEnv("MINIO_ENDPOINT", "minioEndpoint").replace(/\/$/u, "");
const accessKey = requireEnv("MINIO_ACCESS_KEY", "accessKey");
const secretKey = requireEnv("MINIO_SECRET_KEY", "secretKey");
const bucket = `${requireEnv("MINIO_BUCKET_NAME", "bucketName")}-${suffix}`;
const passphrase = "adaptive-journal-cli-e2e-passphrase";
await using workDir = await TempDir.create("livesync-cli-adaptive-journal-s3");
const vaultA = workDir.join("vault-a");
const vaultB = workDir.join("vault-b");
const settingsA = workDir.join("settings-a.json");
const settingsB = workDir.join("settings-b.json");
const binarySourceA = workDir.join("source-a.bin");
const binarySourceB = workDir.join("source-b.bin");
const binaryDestinationA = workDir.join("destination-a.bin");
const binaryDestinationB = workDir.join("destination-b.bin");
await Deno.mkdir(vaultA, { recursive: true });
await Deno.mkdir(vaultB, { recursive: true });
const keepDocker = Deno.env.get("LIVESYNC_DEBUG_KEEP_DOCKER") === "1";
await startMinio(endpoint, accessKey, secretKey, bucket);
try {
await initSettingsFile(settingsA);
await initSettingsFile(settingsB);
await applyRemoteSyncSettings(settingsA, {
remoteType: "MINIO",
minioBucket: bucket,
minioEndpoint: endpoint,
minioAccessKey: accessKey,
minioSecretKey: secretKey,
encrypt: true,
passphrase,
enableCompression: false,
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
});
await applyRemoteSyncSettings(settingsB, {
remoteType: "MINIO",
minioBucket: bucket,
minioEndpoint: endpoint,
minioAccessKey: accessKey,
minioSecretKey: secretKey,
encrypt: true,
passphrase,
enableCompression: false,
journalFormat: "adaptive-v1",
packReadPolicy: "range",
});
const textPath = "adaptive/text.md";
const binaryPath = "adaptive/data.bin";
await runCliWithInputOrFail(`created-by-a-${suffix}\n`, vaultA, "--settings", settingsA, "put", textPath);
await Deno.writeFile(binarySourceA, deterministicBytes(EXTERNAL_PACK_TEST_BYTES, 0x1a2b3c4d));
await runCliOrFail(vaultA, "--settings", settingsA, "push", binarySourceA, binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultB, "--settings", settingsB, "cat", textPath)).trimEnd(),
`created-by-a-${suffix}`
);
await runCliOrFail(vaultB, "--settings", settingsB, "pull", binaryPath, binaryDestinationB);
await assertFilesEqual(binarySourceA, binaryDestinationB, "Adaptive Journal Range transfer differs");
await runCliWithInputOrFail(`updated-by-b-${suffix}\n`, vaultB, "--settings", settingsB, "put", textPath);
await Deno.writeFile(binarySourceB, deterministicBytes(EXTERNAL_PACK_TEST_BYTES, 0x5e6f7788));
await runCliOrFail(vaultB, "--settings", settingsB, "push", binarySourceB, binaryPath);
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultA, "--settings", settingsA, "cat", textPath)).trimEnd(),
`updated-by-b-${suffix}`
);
await runCliOrFail(vaultA, "--settings", settingsA, "pull", binaryPath, binaryDestinationA);
await assertFilesEqual(binarySourceB, binaryDestinationA, "Adaptive Journal whole-Pack transfer differs");
await runCliOrFail(vaultA, "--settings", settingsA, "rm", binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
const deleted = await runCli(vaultB, "--settings", settingsB, "cat", binaryPath);
assert(deleted.code !== 0, `Deleted binary remained readable:\n${deleted.combined}`);
} finally {
if (!keepDocker) {
await stopMinio().catch(() => {});
}
}
});
@@ -0,0 +1,185 @@
import { assert, assertEquals } from "@std/assert";
import { TempDir } from "./helpers/temp.ts";
import { assertFilesEqual, runCli, runCliOrFail, runCliWithInputOrFail, sanitiseCatStdout } from "./helpers/cli.ts";
import { applyRemoteSyncSettings, generateSetupUriFromSettings, initSettingsFile } from "./helpers/settings.ts";
import { startWebDAV, stopWebDAV } from "./helpers/docker.ts";
const EXTERNAL_PACK_TEST_BYTES = 9 * 1024 * 1024;
function deterministicBytes(length: number, seed: number): Uint8Array {
const bytes = new Uint8Array(length);
let state = seed;
for (let index = 0; index < bytes.byteLength; index += 1) {
state ^= state << 13;
state ^= state >>> 17;
state ^= state << 5;
bytes[index] = state & 0xff;
}
return bytes;
}
function webDAVConnectionURI(endpoint: string, prefix: string): string {
const endpointUrl = new URL(endpoint);
const proxyUrl = new URL(`https://${endpointUrl.host}${endpointUrl.pathname}`);
const username = Deno.env.get("WEBDAV_USERNAME") ?? "";
const password = Deno.env.get("WEBDAV_PASSWORD") ?? "";
proxyUrl.username = username;
proxyUrl.password = password;
if (endpointUrl.protocol === "http:") proxyUrl.searchParams.set("insecure", "true");
proxyUrl.searchParams.set("prefix", prefix);
return `sls+webdav:${proxyUrl.toString().slice("https:".length)}`;
}
function setPackReadPolicy(connectionURI: string, policy: "range" | "whole-pack"): string {
const url = new URL(connectionURI);
url.searchParams.set("packReadPolicy", policy);
return url.toString();
}
function selectAdaptiveJournal(connectionURI: string): string {
const url = new URL(connectionURI);
url.searchParams.set("journalFormat", "adaptive-v1");
return url.toString();
}
function remoteIdFromListing(listing: string): string {
const line = listing
.split(/\r?\n/u)
.find((candidate) => candidate.includes("\tWebDAV Remote\t") || candidate.includes("\tWebDAV "));
const id = line?.split("\t", 1)[0];
if (!id) throw new Error(`WebDAV remote profile was not listed:\n${listing}`);
return id;
}
Deno.test("e2e: two CLI vaults synchronise through Adaptive Journal WebDAV", async () => {
const suffix = `${Date.now()}-${Math.floor(Math.random() * 100000)}`;
const endpoint = (Deno.env.get("WEBDAV_ENDPOINT") ?? "http://127.0.0.1:8088/dav").replace(/\/+$/u, "");
const prefix = `adaptive-cli-${suffix}/`;
const connectionURI = webDAVConnectionURI(endpoint, prefix);
const adaptiveConnectionURI = selectAdaptiveJournal(connectionURI);
const vaultPassphrase = "adaptive-journal-webdav-cli-e2ee";
const setupPassphrase = "adaptive-journal-webdav-cli-setup";
await using workDir = await TempDir.create("livesync-cli-adaptive-journal-webdav");
const vaultA = workDir.join("vault-a");
const vaultB = workDir.join("vault-b");
const settingsA = workDir.join("settings-a.json");
const settingsB = workDir.join("settings-b.json");
const binarySourceA = workDir.join("source-a.bin");
const binarySourceB = workDir.join("source-b.bin");
const binaryDestinationA = workDir.join("destination-a.bin");
const binaryDestinationB = workDir.join("destination-b.bin");
await Deno.mkdir(vaultA, { recursive: true });
await Deno.mkdir(vaultB, { recursive: true });
const shouldStartDocker = Deno.env.get("LIVESYNC_START_DOCKER") !== "0";
const keepDocker = Deno.env.get("LIVESYNC_DEBUG_KEEP_DOCKER") === "1";
if (shouldStartDocker) await startWebDAV(endpoint);
try {
await initSettingsFile(settingsA);
await applyRemoteSyncSettings(settingsA, {
remoteType: "WEBDAV",
webDAVConnectionURI: connectionURI,
encrypt: true,
passphrase: vaultPassphrase,
enableCompression: false,
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
});
const addedRemote = await runCliOrFail(
vaultA,
"--settings",
settingsA,
"remote-add",
"WebDAV E2E",
adaptiveConnectionURI
);
const remoteId = addedRemote.trim().split("\t", 1)[0];
assert(remoteId, `remote-add did not return a profile ID:\n${addedRemote}`);
const settingsAfterRemoteAdd = JSON.parse(await Deno.readTextFile(settingsA)) as {
remoteConfigurations?: Record<string, unknown>;
liveSync?: boolean;
};
assert(
settingsAfterRemoteAdd.remoteConfigurations?.[remoteId],
`remote-add did not persist profile ${remoteId}: ${Object.keys(settingsAfterRemoteAdd.remoteConfigurations ?? {}).join(", ")}`
);
assertEquals(settingsAfterRemoteAdd.liveSync, true, "remote-add changed the persisted synchronisation mode");
await runCliOrFail(vaultA, "--settings", settingsA, "remote-activate", remoteId);
const textPath = "adaptive/text.md";
const binaryPath = "adaptive/data.bin";
await runCliWithInputOrFail(`created-by-a-${suffix}\n`, vaultA, "--settings", settingsA, "put", textPath);
await Deno.writeFile(binarySourceA, deterministicBytes(EXTERNAL_PACK_TEST_BYTES, 0x1a2b3c4d));
await runCliOrFail(vaultA, "--settings", settingsA, "push", binarySourceA, binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
const remoteListing = await runCliOrFail(vaultA, "--settings", settingsA, "remote-ls");
assertEquals(remoteIdFromListing(remoteListing), remoteId);
assert(
remoteListing
.split(/\r?\n/u)
.some((line) => line.startsWith(`${remoteId}\t`) && line.includes("\tactive\t")),
`Activated WebDAV profile was not listed as active:\n${remoteListing}`
);
const exportedConnection = (
await runCliOrFail(vaultA, "--settings", settingsA, "remote-export", remoteId)
).trim();
assert(exportedConnection.startsWith("sls+webdav://"));
assert(exportedConnection.includes("journalFormat=adaptive-v1"));
assert(!exportedConnection.includes("packReadPolicy="));
const setupURI = await generateSetupUriFromSettings(settingsA, setupPassphrase, true, vaultPassphrase);
await initSettingsFile(settingsB);
await runCliWithInputOrFail(`${setupPassphrase}\n`, vaultB, "--settings", settingsB, "setup", setupURI);
const settingsAfterSetup = JSON.parse(await Deno.readTextFile(settingsB)) as {
encryptedPassphrase?: string;
};
assert(
typeof settingsAfterSetup.encryptedPassphrase === "string" &&
settingsAfterSetup.encryptedPassphrase.length > 0,
"setup did not persist the encrypted Vault passphrase"
);
await runCliOrFail(
vaultB,
"--settings",
settingsB,
"remote-set",
remoteId,
setPackReadPolicy(exportedConnection, "range")
);
const rangeConnection = (await runCliOrFail(vaultB, "--settings", settingsB, "remote-export", remoteId)).trim();
assert(rangeConnection.includes("packReadPolicy=range"));
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultB, "--settings", settingsB, "cat", textPath)).trimEnd(),
`created-by-a-${suffix}`
);
await runCliOrFail(vaultB, "--settings", settingsB, "pull", binaryPath, binaryDestinationB);
await assertFilesEqual(binarySourceA, binaryDestinationB, "Adaptive Journal Range transfer differs");
await runCliWithInputOrFail(`updated-by-b-${suffix}\n`, vaultB, "--settings", settingsB, "put", textPath);
await Deno.writeFile(binarySourceB, deterministicBytes(EXTERNAL_PACK_TEST_BYTES, 0x5e6f7788));
await runCliOrFail(vaultB, "--settings", settingsB, "push", binarySourceB, binaryPath);
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
assertEquals(
sanitiseCatStdout(await runCliOrFail(vaultA, "--settings", settingsA, "cat", textPath)).trimEnd(),
`updated-by-b-${suffix}`
);
await runCliOrFail(vaultA, "--settings", settingsA, "pull", binaryPath, binaryDestinationA);
await assertFilesEqual(binarySourceB, binaryDestinationA, "Adaptive Journal whole-Pack transfer differs");
await runCliOrFail(vaultA, "--settings", settingsA, "rm", binaryPath);
await runCliOrFail(vaultA, "--settings", settingsA, "sync");
await runCliOrFail(vaultB, "--settings", settingsB, "sync");
const deleted = await runCli(vaultB, "--settings", settingsB, "cat", binaryPath);
assert(deleted.code !== 0, `Deleted binary remained readable:\n${deleted.combined}`);
} finally {
if (shouldStartDocker && !keepDocker) {
await stopWebDAV().catch(() => {});
}
}
});
@@ -49,6 +49,54 @@ export const liveSyncProvisionalEnglishMessages = {
"Connect to existing database and continue": "Connect to existing database and continue",
"Test connection and save": "Test connection and save",
"Save without connecting": "Save without connecting",
"WebDAV Journal Configuration": "WebDAV Journal Configuration",
"Configure a dedicated WebDAV collection for Journal synchronisation. Opaque Journal needs ordinary WebDAV access. Adaptive Journal additionally runs an endpoint safety check before the profile is accepted.":
"Configure a dedicated WebDAV collection for Journal synchronisation. Opaque Journal needs ordinary WebDAV access. Adaptive Journal additionally runs an endpoint safety check before the profile is accepted.",
"Collection prefix": "Collection prefix",
"Use a dedicated prefix. WebDAV listing scans the collection, so unrelated files and a long Journal history increase discovery work.":
"Use a dedicated prefix. WebDAV listing scans the collection, so unrelated files and a long Journal history increase discovery work.",
"Enable this when browser-compatible requests are blocked by CORS. It uses Obsidian's internal request API and may behave differently from standard browser fetch.":
"Enable this when browser-compatible requests are blocked by CORS. It uses Obsidian's internal request API and may behave differently from standard browser fetch.",
"Enter a complete HTTP or HTTPS endpoint without a query string or fragment.":
"Enter a complete HTTP or HTTPS endpoint without a query string or fragment.",
"Use HTTP Range requests": "Use HTTP Range requests",
"Complete Pack reads favour throughput and are the portable default. Range reads can reduce transferred bytes, but this endpoint must pass the exact byte-range check.":
"Complete Pack reads favour throughput and are the portable default. Range reads can reduce transferred bytes, but this endpoint must pass the exact byte-range check.",
"The Adaptive safety check writes, reads, lists, and removes disposable objects under a random probe prefix. It does not inspect Vault data.":
"The Adaptive safety check writes, reads, lists, and removes disposable objects under a random probe prefix. It does not inspect Vault data.",
"Required Adaptive operations are supported by this WebDAV endpoint.":
"Required Adaptive operations are supported by this WebDAV endpoint.",
"The WebDAV endpoint is missing required Adaptive operations: ${CAPABILITIES}.":
"The WebDAV endpoint is missing required Adaptive operations: ${CAPABILITIES}.",
"The Adaptive safety check failed (${CATEGORY}; retry ${RETRY}).":
"The Adaptive safety check failed (${CATEGORY}; retry ${RETRY}).",
"Required Adaptive operations were not checked.": "Required Adaptive operations were not checked.",
"Exact HTTP byte-range retrieval is supported.": "Exact HTTP byte-range retrieval is supported.",
"HTTP byte-range retrieval is not supported. Complete Pack retrieval remains available.":
"HTTP byte-range retrieval is not supported. Complete Pack retrieval remains available.",
"HTTP byte-range retrieval was not checked because the required safety check did not complete.":
"HTTP byte-range retrieval was not checked because the required safety check did not complete.",
"The selected WebDAV Journal policy is not supported by this endpoint.":
"The selected WebDAV Journal policy is not supported by this endpoint.",
"The remote contains ${REMOTE_FORMAT} data, but this profile selects ${SELECTED_FORMAT}. Rebuild the remote or restore the matching format.":
"The remote contains ${REMOTE_FORMAT} data, but this profile selects ${SELECTED_FORMAT}. Rebuild the remote or restore the matching format.",
"This build cannot inspect WebDAV Journal capabilities.": "This build cannot inspect WebDAV Journal capabilities.",
"Invalid WebDAV settings: ${REASON}": "Invalid WebDAV settings: ${REASON}",
"The saved connection contains credentials and custom headers. Configuration encryption protects exported Setup data when it is enabled; do not share a plain connection string.":
"The saved connection contains credentials and custom headers. Configuration encryption protects exported Setup data when it is enabled; do not share a plain connection string.",
"WebDAV Journal": "WebDAV Journal",
"Store Journal data in a dedicated WebDAV collection. Adaptive mode is experimental and requires an endpoint safety check.":
"Store Journal data in a dedicated WebDAV collection. Adaptive mode is experimental and requires an endpoint safety check.",
"Continue to WebDAV setup": "Continue to WebDAV setup",
"Expected repository ID": "Expected repository ID",
"This optional identity pins a trusted Adaptive repository. A Setup URI can supply it; leave it blank only when creating a repository or intentionally trusting the first compatible repository reached.":
"This optional identity pins a trusted Adaptive repository. A Setup URI can supply it; leave it blank only when creating a repository or intentionally trusting the first compatible repository reached.",
"Run endpoint safety check": "Run endpoint safety check",
"Test WebDAV connection": "Test WebDAV connection",
"Continue with verified settings": "Continue with verified settings",
"Save verified settings": "Save verified settings",
"WebDAV access and the selected Journal format were verified.":
"WebDAV access and the selected Journal format were verified.",
"Enter a complete HTTP or HTTPS URL.": "Enter a complete HTTP or HTTPS URL.",
"CouchDB validates the database name when you connect. The name must not be empty.":
"CouchDB validates the database name when you connect. The name must not be empty.",
@@ -288,6 +288,13 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
zh: "生效中的远程配置",
"zh-tw": "目前啟用的遠端設定",
},
"Adaptive Journal (experimental)": {
def: "Adaptive Journal (experimental)",
},
"Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats.":
{
def: "Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats.",
},
"Add default patterns": {
def: "Add default patterns",
es: "Añadir patrones predeterminados",
@@ -784,6 +791,10 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
zh: "兼容性(问题修复)",
"zh-tw": "相容性(問題修復)",
},
"Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing.":
{
def: "Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing.",
},
"Compute revisions for chunks": {
def: "Compute revisions for chunks",
es: "Calcular revisiones para los chunks",
@@ -1585,6 +1596,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
ko: "문서 기록",
"zh-tw": "文件歷程",
},
"Download complete Packs": {
def: "Download complete Packs",
},
Duplicate: {
def: "Duplicate",
es: "Duplicar",
@@ -2605,6 +2619,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
ru: "Интервал (сек)",
zh: "间隔(秒)",
},
"Invalid Object Storage settings: ${reason}": {
def: "Invalid Object Storage settings: ${reason}",
},
INVERTED: {
def: "INVERTED",
es: "INVERTIDO",
@@ -2617,6 +2634,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
def: "It is strongly advised to create a backup before proceeding. Continuing without a backup may lead to data loss.",
es: "Se recomienda encarecidamente crear una copia de seguridad antes de continuar. Continuar sin copia de seguridad puede provocar pérdida de datos.",
},
"Journal data format": {
def: "Journal data format",
},
"Just for a minute, please!": {
def: "Just for a minute, please!",
es: "¡Solo un momento, por favor!",
@@ -6187,6 +6207,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
def: "On this device, switch to the camera app or use a QR code scanner to scan the displayed QR code.",
es: "En este dispositivo, cambia a la aplicación de cámara o usa un lector de QR para escanear el código mostrado.",
},
"Opaque Journal (current format)": {
def: "Opaque Journal (current format)",
},
Open: {
def: "Open",
es: "Abrir",
@@ -6447,6 +6470,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
ru: "P2P Sync с name начат.",
zh: "P2P Sync with ${name} have been started.",
},
"Pack retrieval": {
def: "Pack retrieval",
},
"paneMaintenance.markDeviceResolvedAfterBackup": {
def: "paneMaintenance.markDeviceResolvedAfterBackup",
es: "Marcar el dispositivo como resuelto después de hacer una copia de seguridad",
@@ -11069,6 +11095,9 @@ export const allMessages: Readonly<Record<string, Readonly<Record<string, string
def: "Use Random Number",
es: "Usar número aleatorio",
},
"Use S3 Range requests": {
def: "Use S3 Range requests",
},
"Use Segmented-splitter": {
def: "Use Segmented-splitter",
es: "Usar divisor segmentado",
+9
View File
@@ -44,6 +44,8 @@
"Action": "Action",
"Activate": "Activate",
"Active Remote Configuration": "Active Remote Configuration",
"Adaptive Journal (experimental)": "Adaptive Journal (experimental)",
"Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats.": "Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats.",
"Add default patterns": "Add default patterns",
"Add new connection": "Add new connection",
"AddOn Module (ConfigSync) has not been loaded. This is very unexpected situation. Please report this issue.": "AddOn Module (ConfigSync) has not been loaded. This is very unexpected situation. Please report this issue.",
@@ -112,6 +114,7 @@
"Compatibility (Metadata)": "Compatibility (Metadata)",
"Compatibility (Remote Database)": "Compatibility (Remote Database)",
"Compatibility (Trouble addressed)": "Compatibility (Trouble addressed)",
"Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing.": "Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing.",
"Compute revisions for chunks": "Compute revisions for chunks",
"Configuration": "Configuration",
"Configuration Encryption": "Configuration Encryption",
@@ -212,6 +215,7 @@
"Doctor.Message.SomeSkipped": "We left some issues as is. Shall I ask you again on next startup?",
"Doctor.RULES.E2EE_V02500.REASON": "The End-to-End Encryption has got now more robust and faster. Also because, the previous E2EE was found to be compromised in a re-conducted code review. It should be applied as soon as possible. Really apologises for your inconvenience. And, this setting is not forward compatible. All synchronised devices must be updated to v0.25.0 or higher. Rebuilds are not required and will be converted from the new transfer to the new format, However, it is recommended to rebuild whenever possible.",
"Document History": "Document History",
"Download complete Packs": "Download complete Packs",
"Duplicate": "Duplicate",
"Duplicate remote": "Duplicate remote",
"E2EE Configuration": "E2EE Configuration",
@@ -355,9 +359,11 @@
"Initialise journal received history. On the next sync, every item except this device sent will be downloaded again.": "Initialise journal received history. On the next sync, every item except this device sent will be downloaded again.",
"Initialise journal sent history. On the next sync, every item except this device received will be sent again.": "Initialise journal sent history. On the next sync, every item except this device received will be sent again.",
"Interval (sec)": "Interval (sec)",
"Invalid Object Storage settings: ${reason}": "Invalid Object Storage settings: ${reason}",
"INVERTED": "INVERTED",
"Issue detection log:": "Issue detection log:",
"It is strongly advised to create a backup before proceeding. Continuing without a backup may lead to data loss.": "It is strongly advised to create a backup before proceeding. Continuing without a backup may lead to data loss.",
"Journal data format": "Journal data format",
"Just for a minute, please!": "Just for a minute, please!",
"JWT (JSON Web Token) authentication allows you to securely authenticate with the CouchDB server using tokens. Ensure that your CouchDB server is configured to accept JWTs and that the provided key and settings match the server's configuration. Incidentally, I have not verified it very thoroughly.": "JWT (JSON Web Token) authentication allows you to securely authenticate with the CouchDB server using tokens. Ensure that your CouchDB server is configured to accept JWTs and that the provided key and settings match the server's configuration. Incidentally, I have not verified it very thoroughly.",
"JWT Algorithm": "JWT Algorithm",
@@ -738,6 +744,7 @@
"On the source device, open Obsidian.": "On the source device, open Obsidian.",
"On this device, please keep this Vault open.": "On this device, please keep this Vault open.",
"On this device, switch to the camera app or use a QR code scanner to scan the displayed QR code.": "On this device, switch to the camera app or use a QR code scanner to scan the displayed QR code.",
"Opaque Journal (current format)": "Opaque Journal (current format)",
"Open": "Open",
"Open connection": "Open connection",
"Open P2P Setup...": "Open P2P Setup...",
@@ -768,6 +775,7 @@
"P2P.SyncAlreadyRunning": "P2P Sync is already running.",
"P2P.SyncCompleted": "P2P Sync completed.",
"P2P.SyncStartedWith": "P2P Sync with ${name} have been started.",
"Pack retrieval": "Pack retrieval",
"paneMaintenance.markDeviceResolvedAfterBackup": "paneMaintenance.markDeviceResolvedAfterBackup",
"paneMaintenance.remoteLockedAndDeviceNotAccepted": "paneMaintenance.remoteLockedAndDeviceNotAccepted",
"paneMaintenance.remoteLockedResolvedDevice": "paneMaintenance.remoteLockedResolvedDevice",
@@ -1418,6 +1426,7 @@
"Use JWT Authentication": "Use JWT Authentication",
"Use Path-Style Access": "Use Path-Style Access",
"Use Random Number": "Use Random Number",
"Use S3 Range requests": "Use S3 Range requests",
"Use Segmented-splitter": "Use Segmented-splitter",
"Use splitting-limit-capped chunk splitter": "Use splitting-limit-capped chunk splitter",
"Use the trash bin": "Use the trash bin",
+15
View File
@@ -66,6 +66,21 @@ AddOn Module (HiddenFileSync) has not been loaded. This is very unexpected situa
situation. Please report this issue.
Advanced: Advanced
Advanced Settings: Advanced Settings
Adaptive Journal (experimental): Adaptive Journal (experimental)
Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats.:
Adaptive Journal uses immutable objects and a separate remote format.
Existing Opaque Journal data is not migrated or read. Rebuild the remote when
changing formats.
Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing.:
Complete Pack reads favour throughput. Range reads can reduce transferred
bytes. The connection test verifies exact Range support on this endpoint, and
synchronisation refuses an unsupported selection before writing.
Download complete Packs: Download complete Packs
"Invalid Object Storage settings: ${reason}": "Invalid Object Storage settings: ${reason}"
Journal data format: Journal data format
Opaque Journal (current format): Opaque Journal (current format)
Pack retrieval: Pack retrieval
Use S3 Range requests: Use S3 Range requests
After restarting, the data on this device will be uploaded to the server as the 'master copy'. Please be aware that any unintended data currently on the server will be completely overwritten.:
After restarting, the data on this device will be uploaded to the server as
the 'master copy'. Please be aware that any unintended data currently on the
+4 -4
View File
@@ -1,5 +1,6 @@
import { fireAndForget } from "octagonal-wheels/promises";
import { REMOTE_MINIO, REMOTE_P2P, type RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { defaultRemoteProviderRegistry } from "@vrtmrz/livesync-commonlib/remote-configurations";
import { LiveSyncCouchDBReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/couchdb/LiveSyncReplicator";
import type { LiveSyncAbstractReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import { AbstractModule } from "@/modules/AbstractModule";
@@ -8,8 +9,7 @@ import type { LiveSyncCore } from "@/main";
export class ModuleReplicatorCouchDB extends AbstractModule {
_anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator | false> {
const settings = { ...this.settings, ...settingOverride };
// If new remote types were added, add them here. Do not use `REMOTE_COUCHDB` directly for the safety valve.
if (settings.remoteType == REMOTE_MINIO || settings.remoteType == REMOTE_P2P) {
if (!defaultRemoteProviderRegistry.isRemoteTypeInFamily(settings.remoteType, "couchdb")) {
return Promise.resolve(false);
}
return Promise.resolve(new LiveSyncCouchDBReplicator(this.core));
@@ -17,7 +17,7 @@ export class ModuleReplicatorCouchDB extends AbstractModule {
_everyAfterResumeProcess(): Promise<boolean> {
if (this.services.appLifecycle.isSuspended()) return Promise.resolve(true);
if (!this.services.appLifecycle.isReady()) return Promise.resolve(true);
if (this.settings.remoteType != REMOTE_MINIO && this.settings.remoteType != REMOTE_P2P) {
if (defaultRemoteProviderRegistry.isRemoteTypeInFamily(this.settings.remoteType, "couchdb")) {
const LiveSyncEnabled = this.settings.liveSync;
const continuous = LiveSyncEnabled;
const eventualOnStart = !LiveSyncEnabled && this.settings.syncOnStart;
@@ -1,7 +1,11 @@
import { describe, expect, it, vi } from "vitest";
import { REMOTE_COUCHDB, REMOTE_MINIO, REMOTE_WEBDAV } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ModuleReplicatorCouchDB } from "./ModuleReplicatorCouchDB.ts";
function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isReplicationReady = true) {
function createModule(
settings: { liveSync: boolean; remoteType?: string; syncOnStart: boolean },
isReplicationReady = true
) {
const openReplication = vi.fn(async () => true);
const runFiniteReplicationActivity = vi.fn(async (task: () => unknown) => await task());
const services = {
@@ -30,7 +34,7 @@ function createModule(settings: { liveSync: boolean; syncOnStart: boolean }, isR
_services: services,
services,
settings: {
remoteType: "",
remoteType: REMOTE_COUCHDB,
...settings,
},
replicator: { openReplication },
@@ -86,4 +90,19 @@ describe("ModuleReplicatorCouchDB resume replication activity", () => {
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).not.toHaveBeenCalled();
});
it.each([REMOTE_MINIO, REMOTE_WEBDAV])("does not claim or resume registered Journal provider %s", async (remoteType) => {
const { module, openReplication, runFiniteReplicationActivity } = createModule({
liveSync: true,
remoteType,
syncOnStart: true,
});
await expect(module._anyNewReplicator()).resolves.toBe(false);
await module._everyAfterResumeProcess();
await new Promise((resolve) => setTimeout(resolve, 0));
expect(runFiniteReplicationActivity).not.toHaveBeenCalled();
expect(openReplication).not.toHaveBeenCalled();
});
});
-18
View File
@@ -1,18 +0,0 @@
import { REMOTE_MINIO, type RemoteDBSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LiveSyncJournalReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/journal/LiveSyncJournalReplicator";
import type { LiveSyncAbstractReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/LiveSyncAbstractReplicator";
import type { LiveSyncCore } from "@/main";
import { AbstractModule } from "@/modules/AbstractModule";
export class ModuleReplicatorMinIO extends AbstractModule {
_anyNewReplicator(settingOverride: Partial<RemoteDBSettings> = {}): Promise<LiveSyncAbstractReplicator | false> {
const settings = { ...this.settings, ...settingOverride };
if (settings.remoteType == REMOTE_MINIO) {
return Promise.resolve(new LiveSyncJournalReplicator(this.core));
}
return Promise.resolve(false);
}
override onBindFunction(core: LiveSyncCore, services: typeof core.services): void {
services.replicator.getNewReplicator.addHandler(this._anyNewReplicator.bind(this));
}
}
@@ -1,7 +1,4 @@
import {
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
DEFAULT_SETTINGS,
LOG_LEVEL_NOTICE,
type ObsidianLiveSyncSettings,
@@ -21,18 +18,12 @@ import { SetupManager, UserMode } from "@/modules/features/SetupManager.ts";
import { OnDialogSettingsDefault, type AllSettings } from "./settingConstants.ts";
import {
activateRemoteConfiguration,
createRemoteConfigurationId,
defaultRemoteProviderRegistry,
suggestRemoteConfigurationName,
type BuiltInRemoteConfiguration,
type RemoteConfiguration,
} from "@vrtmrz/livesync-commonlib/remote-configurations";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import type { RemoteConfigurationResult } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
import SetupRemote from "@/modules/features/SetupWizard/dialogs/SetupRemote.svelte";
import SetupRemoteCouchDB from "@/modules/features/SetupWizard/dialogs/SetupRemoteCouchDB.svelte";
import SetupRemoteBucket from "@/modules/features/SetupWizard/dialogs/SetupRemoteBucket.svelte";
import SetupRemoteP2P from "@/modules/features/SetupWizard/dialogs/SetupRemoteP2P.svelte";
import type {
SetupRemoteCouchDBInitialData,
SetupRemoteCouchDBResultType,
} from "@/modules/features/SetupWizard/dialogs/setupDialogTypes.ts";
import { syncActivatedRemoteSettings } from "./remoteConfigBuffer.ts";
function getSettingsFromEditingSettings(editingSettings: AllSettings): ObsidianLiveSyncSettings {
@@ -43,10 +34,6 @@ function getSettingsFromEditingSettings(editingSettings: AllSettings): ObsidianL
}
return workObj;
}
function createRemoteConfigurationId(): string {
return `remote-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}`;
}
function cloneRemoteConfigurations(
configs: Record<string, RemoteConfiguration> | undefined
): Record<string, RemoteConfiguration> {
@@ -54,15 +41,11 @@ function cloneRemoteConfigurations(
}
function serializeRemoteConfiguration(settings: ObsidianLiveSyncSettings): string {
if (settings.remoteType === REMOTE_MINIO) {
return ConnectionStringParser.serialize({ type: "s3", settings });
}
if (settings.remoteType === REMOTE_P2P) {
return ConnectionStringParser.serialize({ type: "p2p", settings });
}
return ConnectionStringParser.serialize({ type: "couchdb", settings });
const type = defaultRemoteProviderRegistry.typeForRemoteType(settings.remoteType);
if (!type) throw new Error(`Unsupported remote type: ${settings.remoteType}`);
const configuration = defaultRemoteProviderRegistry.configurationFromSettings(type, settings);
return defaultRemoteProviderRegistry.serialise(configuration);
}
function setEmojiButton(button: ButtonComponent, emoji: string, tooltip: string) {
button.setButtonText(emoji);
button.setTooltip(tooltip, { delay: 10, placement: "top" });
@@ -71,21 +54,6 @@ function setEmojiButton(button: ButtonComponent, emoji: string, tooltip: string)
return button;
}
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}`;
}
return `P2P ${parsed.settings.P2P_roomID || "Remote"}`;
}
export function paneRemoteConfig(
this: ObsidianLiveSyncSettingTab,
paneEl: HTMLElement,
@@ -189,48 +157,10 @@ export function paneRemoteConfig(
};
const runRemoteSetup = async (
baseSettings: ObsidianLiveSyncSettings,
remoteType?: typeof REMOTE_COUCHDB | typeof REMOTE_MINIO | typeof REMOTE_P2P
type?: BuiltInRemoteConfiguration["type"]
): Promise<ObsidianLiveSyncSettings | false> => {
const setupManager = this.core.getModule(SetupManager);
const dialogManager = setupManager.dialogManager;
let targetRemoteType = remoteType;
if (targetRemoteType === undefined) {
const method = await dialogManager.openWithExplicitCancel(SetupRemote);
if (method === "cancelled") {
return false;
}
targetRemoteType =
method === "bucket" ? REMOTE_MINIO : method === "p2p" ? REMOTE_P2P : REMOTE_COUCHDB;
}
if (targetRemoteType === REMOTE_MINIO) {
const bucketConf = await dialogManager.openWithExplicitCancel(SetupRemoteBucket, baseSettings);
if (bucketConf === "cancelled" || typeof bucketConf !== "object") {
return false;
}
return { ...baseSettings, ...bucketConf, remoteType: REMOTE_MINIO };
}
if (targetRemoteType === REMOTE_P2P) {
const p2pConf = await dialogManager.openWithExplicitCancel(SetupRemoteP2P, baseSettings);
if (p2pConf === "cancelled" || typeof p2pConf !== "object") {
return false;
}
return { ...baseSettings, ...p2pConf, remoteType: REMOTE_P2P };
}
const couchConf = await dialogManager.openWithExplicitCancel<
SetupRemoteCouchDBResultType,
SetupRemoteCouchDBInitialData
>(SetupRemoteCouchDB, {
settings: baseSettings,
mode: "settings",
});
if (couchConf === "cancelled" || typeof couchConf !== "object") {
return false;
}
return { ...baseSettings, ...couchConf, remoteType: REMOTE_COUCHDB };
return await setupManager.configureRemoteForSettings(baseSettings, type);
};
const createBaseRemoteSettings = (): ObsidianLiveSyncSettings => ({
...DEFAULT_SETTINGS,
@@ -282,9 +212,9 @@ export function paneRemoteConfig(
return;
}
let parsed: RemoteConfigurationResult;
let parsed: BuiltInRemoteConfiguration;
try {
parsed = ConnectionStringParser.parse(trimmedURI);
parsed = defaultRemoteProviderRegistry.parse(trimmedURI);
} catch (ex) {
this.services.API.addLog(`Failed to import remote configuration!`, LOG_LEVEL_NOTICE);
this.services.API.addLog(ex, LOG_LEVEL_VERBOSE);
@@ -302,7 +232,7 @@ export function paneRemoteConfig(
configs[id] = {
id,
name: name.trim() || defaultName,
uri: ConnectionStringParser.serialize(parsed),
uri: defaultRemoteProviderRegistry.serialise(parsed),
isEncrypted: false,
};
this.editingSettings.remoteConfigurations = configs;
@@ -328,7 +258,7 @@ export function paneRemoteConfig(
for (const config of Object.values(configs)) {
const row = new Setting(listContainer)
.setName(config.name)
.setDesc(config.uri.split("@").pop() || ""); // Show host part for privacy
.setDesc(describeRemoteConfiguration(config.uri));
if (config.id === this.editingSettings.activeConfigurationId) {
row.nameEl.addClass("sls-active-remote-name");
@@ -337,9 +267,9 @@ export function paneRemoteConfig(
row.addButton((btn) =>
setEmojiButton(btn, "🔧", "Configure").onClick(async () => {
let parsed: RemoteConfigurationResult;
let parsed: BuiltInRemoteConfiguration;
try {
parsed = ConnectionStringParser.parse(config.uri);
parsed = defaultRemoteProviderRegistry.parse(config.uri);
} catch (ex) {
this.services.API.addLog(
`Failed to parse remote configuration '${config.id}' for editing!`,
@@ -349,16 +279,9 @@ export function paneRemoteConfig(
return;
}
const workSettings = createBaseRemoteSettings();
if (parsed.type === "couchdb") {
workSettings.remoteType = REMOTE_COUCHDB;
} else if (parsed.type === "s3") {
workSettings.remoteType = REMOTE_MINIO;
} else {
workSettings.remoteType = REMOTE_P2P;
}
Object.assign(workSettings, parsed.settings);
defaultRemoteProviderRegistry.applyConfiguration(workSettings, parsed);
const nextSettings = await runRemoteSetup(workSettings, workSettings.remoteType);
const nextSettings = await runRemoteSetup(workSettings, parsed.type);
if (!nextSettings) {
return;
}
@@ -447,9 +370,9 @@ export function paneRemoteConfig(
.addSeparator()
.addItem((item) => {
item.setTitle("📡 Fetch remote settings").onClick(async () => {
let parsed: RemoteConfigurationResult;
let parsed: BuiltInRemoteConfiguration;
try {
parsed = ConnectionStringParser.parse(config.uri);
parsed = defaultRemoteProviderRegistry.parse(config.uri);
} catch (ex) {
this.services.API.addLog(
`Failed to parse remote configuration '${config.id}' for fetching settings!`,
@@ -459,14 +382,7 @@ export function paneRemoteConfig(
return;
}
const workSettings = createBaseRemoteSettings();
if (parsed.type === "couchdb") {
workSettings.remoteType = REMOTE_COUCHDB;
} else if (parsed.type === "s3") {
workSettings.remoteType = REMOTE_MINIO;
} else {
workSettings.remoteType = REMOTE_P2P;
}
Object.assign(workSettings, parsed.settings);
defaultRemoteProviderRegistry.applyConfiguration(workSettings, parsed);
const newTweaks =
await this.services.tweakValue.checkAndAskUseRemoteConfiguration(
workSettings
@@ -1,4 +1,9 @@
import { pickBucketSyncSettings, pickCouchDBSyncSettings, pickP2PSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import {
pickBucketSyncSettings,
pickCouchDBSyncSettings,
pickP2PSyncSettings,
pickWebDAVSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/utils";
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
// Keep the setting dialogue buffer aligned with the current core settings before persisting other dirty keys.
@@ -11,6 +16,7 @@ export function syncActivatedRemoteSettings(
remoteType: source.remoteType,
activeConfigurationId: source.activeConfigurationId,
...pickBucketSyncSettings(source),
...pickWebDAVSyncSettings(source),
...pickCouchDBSyncSettings(source),
...pickP2PSyncSettings(source),
});
@@ -1,5 +1,10 @@
import { describe, expect, it } from "vitest";
import { DEFAULT_SETTINGS, REMOTE_COUCHDB, REMOTE_MINIO } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_WEBDAV,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { syncActivatedRemoteSettings } from "./remoteConfigBuffer";
describe("syncActivatedRemoteSettings", () => {
@@ -28,6 +33,9 @@ describe("syncActivatedRemoteSettings", () => {
useCustomRequestHandler: false,
forcePathStyle: true,
bucketCustomHeaders: "",
expectedRepositoryId: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
journalFormat: "adaptive-v1" as const,
packReadPolicy: "range" as const,
};
syncActivatedRemoteSettings(target, source);
@@ -40,6 +48,9 @@ describe("syncActivatedRemoteSettings", () => {
expect(target.bucket).toBe("vault");
expect(target.region).toBe("sz-hq");
expect(target.bucketPrefix).toBe("folder/");
expect(target.expectedRepositoryId).toBe("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
expect(target.journalFormat).toBe("adaptive-v1");
expect(target.packReadPolicy).toBe("range");
expect(target.encrypt).toBe(true);
});
@@ -80,4 +91,34 @@ describe("syncActivatedRemoteSettings", () => {
expect(target.couchDB_PASSWORD).toBe("current-pass");
expect(target.couchDB_DBNAME).toBe("current-db");
});
it("should copy the active WebDAV connection and Adaptive protocol into the editing buffer", () => {
const target = {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
activeConfigurationId: "old-remote",
webDAVactiveConnectionURI: "sls+webdav://stale.invalid/",
expectedRepositoryId: "",
journalFormat: "opaque-v1" as const,
packReadPolicy: "whole-pack" as const,
};
const source = {
...DEFAULT_SETTINGS,
remoteType: REMOTE_WEBDAV,
activeConfigurationId: "remote-webdav",
webDAVactiveConnectionURI: "sls+webdav://alice:secret@dav.example/dav?prefix=notes%2F",
expectedRepositoryId: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
journalFormat: "adaptive-v1" as const,
packReadPolicy: "range" as const,
};
syncActivatedRemoteSettings(target, source);
expect(target.remoteType).toBe(REMOTE_WEBDAV);
expect(target.activeConfigurationId).toBe("remote-webdav");
expect(target.webDAVactiveConnectionURI).toBe(source.webDAVactiveConnectionURI);
expect(target.expectedRepositoryId).toBe(source.expectedRepositoryId);
expect(target.journalFormat).toBe("adaptive-v1");
expect(target.packReadPolicy).toBe("range");
});
});
+131 -82
View File
@@ -1,15 +1,16 @@
import {
type BucketSyncSetting,
type EncryptionSettings,
type ObsidianLiveSyncSettings,
type P2PSyncSetting,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
REMOTE_COUCHDB,
REMOTE_MINIO,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
import { upsertRemoteConfigurationInPlace } from "@vrtmrz/livesync-commonlib/remote-configurations";
import { generateAdaptiveJournalRepositoryIdV1 } from "@vrtmrz/livesync-commonlib/adaptive-journal";
import {
defaultRemoteProviderRegistry,
upsertRemoteConfigurationInPlace,
type BuiltInRemoteConfiguration,
} from "@vrtmrz/livesync-commonlib/remote-configurations";
import { isObjectDifferent } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import Intro from "./SetupWizard/dialogs/Intro.svelte";
import SelectMethodNewUser from "./SetupWizard/dialogs/SelectMethodNewUser.svelte";
@@ -20,9 +21,6 @@ import OutroNewUser from "./SetupWizard/dialogs/OutroNewUser.svelte";
import OutroExistingUser from "./SetupWizard/dialogs/OutroExistingUser.svelte";
import OutroAskUserMode from "./SetupWizard/dialogs/OutroAskUserMode.svelte";
import SetupRemote from "./SetupWizard/dialogs/SetupRemote.svelte";
import SetupRemoteCouchDB from "./SetupWizard/dialogs/SetupRemoteCouchDB.svelte";
import SetupRemoteBucket from "./SetupWizard/dialogs/SetupRemoteBucket.svelte";
import SetupRemoteP2P from "./SetupWizard/dialogs/SetupRemoteP2P.svelte";
import SetupRemoteE2EE from "./SetupWizard/dialogs/SetupRemoteE2EE.svelte";
import { decodeSettingsFromQRCodeData } from "@vrtmrz/livesync-commonlib/compat/API/processSetting";
import { AbstractModule } from "@/modules/AbstractModule.ts";
@@ -31,11 +29,8 @@ import type {
OutroExistingUserResultType,
OutroNewUserResultType,
ScanQRCodeResultType,
SetupRemoteBucketResultType,
SetupRemoteCouchDBResultType,
SetupRemoteCouchDBInitialData,
SetupRemoteE2EEResultType,
SetupRemoteP2PResultType,
SetupRemoteInitialData,
SetupRemoteResultType,
UseSetupURIResultType,
} from "./SetupWizard/dialogs/setupDialogTypes.ts";
@@ -44,6 +39,8 @@ import {
applySettingsWithScheduledInitialisation,
} from "@/serviceFeatures/setupObsidian/setupActivationLifecycle.ts";
import { isP2PMainRemote } from "@/common/remoteConfiguration.ts";
import { builtInRemoteSetupRegistry } from "./SetupWizard/builtInRemoteSetupProviders.ts";
import type { RemoteSetupIntent } from "./SetupWizard/RemoteSetupRegistry.ts";
function copySettingsForRemoteProfileUpdate(settings: ObsidianLiveSyncSettings): ObsidianLiveSyncSettings {
return {
@@ -75,10 +72,35 @@ export const enum UserMode {
Update = "unknown", // Alias for Unknown for better readability
}
type AdaptiveJournalIdentitySetting = {
expectedRepositoryId?: string;
journalFormat?: string;
};
async function prepareAdaptiveRepositoryIdentityForSetup<T extends object>(
settings: T,
userMode: UserMode
): Promise<T> {
const adaptiveSettings = settings as T & AdaptiveJournalIdentitySetting;
if (
userMode !== UserMode.NewUser ||
adaptiveSettings.journalFormat !== "adaptive-v1" ||
(adaptiveSettings.expectedRepositoryId ?? "").trim() !== ""
) {
return settings;
}
return {
...settings,
expectedRepositoryId: await generateAdaptiveJournalRepositoryIdV1(),
};
}
/**
* Setup Manager to handle onboarding and configuration setup
*/
export class SetupManager extends AbstractModule {
readonly remoteSetupRegistry = builtInRemoteSetupRegistry;
// /**
// * Dialog manager for handling Svelte dialogs
// */
@@ -157,6 +179,81 @@ export class SetupManager extends AbstractModule {
return await this.onConfirmApplySettingsFromWizard(newSetting, userMode);
}
private setupIntent(userMode: UserMode): RemoteSetupIntent {
if (userMode === UserMode.NewUser) return "create-or-connect";
if (userMode === UserMode.ExistingUser) return "connect-existing";
return "settings";
}
private async selectRemoteSetupProvider(): Promise<BuiltInRemoteConfiguration["type"] | false> {
const method = await this.dialogManager.openWithExplicitCancel<SetupRemoteResultType, SetupRemoteInitialData>(
SetupRemote,
this.remoteSetupRegistry.choices()
);
return method === "cancelled" ? false : method;
}
/**
* Opens a provider-specific dialogue without applying or storing the resulting settings.
*
* The settings pane uses this boundary to prepare a remote profile in its own edit buffer.
*/
async configureRemoteForSettings(
currentSetting: ObsidianLiveSyncSettings,
type?: BuiltInRemoteConfiguration["type"]
): Promise<ObsidianLiveSyncSettings | false> {
const selectedType = type ?? (await this.selectRemoteSetupProvider());
if (selectedType === false) return false;
const configuration = await this.remoteSetupRegistry.open(selectedType, {
dialogManager: this.dialogManager,
intent: "settings",
settings: currentSetting,
});
if (configuration === "cancelled") return false;
const newSetting = copySettingsForRemoteProfileUpdate(currentSetting);
defaultRemoteProviderRegistry.applyConfiguration(newSetting, configuration);
return newSetting;
}
private async onRemoteManualSetup(
type: BuiltInRemoteConfiguration["type"],
userMode: UserMode,
currentSetting: ObsidianLiveSyncSettings,
activate: boolean
): Promise<boolean> {
const configuration = await this.remoteSetupRegistry.open(type, {
dialogManager: this.dialogManager,
intent: this.setupIntent(userMode),
settings: currentSetting,
});
if (configuration === "cancelled") {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
return await this.onOnboard(userMode);
}
const newSetting = copySettingsForRemoteProfileUpdate(currentSetting);
const preparedSettings = await prepareAdaptiveRepositoryIdentityForSetup(configuration.settings, userMode);
const preparedConfiguration = {
...configuration,
settings: preparedSettings,
} as BuiltInRemoteConfiguration;
if (activate) {
defaultRemoteProviderRegistry.applyConfiguration(newSetting, preparedConfiguration);
} else {
Object.assign(newSetting, preparedSettings);
}
const activateForP2P = defaultRemoteProviderRegistry.supportsActivationRole(type, "p2p");
upsertRemoteConfigurationInPlace(newSetting, type, {
id: activateForP2P ? newSetting.P2P_ActiveRemoteConfigurationId || undefined : undefined,
activate,
activateForP2P,
});
return await this.onConfirmApplySettingsFromWizard(newSetting, userMode, activate);
}
/**
* Handles manual setup for CouchDB
* @param userMode
@@ -169,31 +266,7 @@ export class SetupManager extends AbstractModule {
currentSetting: ObsidianLiveSyncSettings,
activate = true
): Promise<boolean> {
const couchConf = await this.dialogManager.openWithExplicitCancel<
SetupRemoteCouchDBResultType,
SetupRemoteCouchDBInitialData
>(SetupRemoteCouchDB, {
settings: currentSetting,
mode:
userMode === UserMode.NewUser
? "create-or-connect"
: userMode === UserMode.ExistingUser
? "connect-existing"
: "settings",
});
if (couchConf === "cancelled") {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
return await this.onOnboard(userMode);
}
const newSetting = {
...copySettingsForRemoteProfileUpdate(currentSetting),
...couchConf,
} as ObsidianLiveSyncSettings;
if (activate) {
newSetting.remoteType = REMOTE_COUCHDB;
}
upsertRemoteConfigurationInPlace(newSetting, "couchdb", { activate });
return await this.onConfirmApplySettingsFromWizard(newSetting, userMode, activate);
return await this.onRemoteManualSetup("couchdb", userMode, currentSetting, activate);
}
/**
@@ -208,23 +281,22 @@ export class SetupManager extends AbstractModule {
currentSetting: ObsidianLiveSyncSettings,
activate = true
): Promise<boolean> {
const bucketConf = await this.dialogManager.openWithExplicitCancel<
SetupRemoteBucketResultType,
BucketSyncSetting
>(SetupRemoteBucket, currentSetting);
if (bucketConf === "cancelled") {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
return await this.onOnboard(userMode);
}
const newSetting = {
...copySettingsForRemoteProfileUpdate(currentSetting),
...bucketConf,
} as ObsidianLiveSyncSettings;
if (activate) {
newSetting.remoteType = REMOTE_MINIO;
}
upsertRemoteConfigurationInPlace(newSetting, "s3", { activate });
return await this.onConfirmApplySettingsFromWizard(newSetting, userMode, activate);
return await this.onRemoteManualSetup("s3", userMode, currentSetting, activate);
}
/**
* Handles manual setup for WebDAV Journal storage.
* @param userMode
* @param currentSetting
* @param activate Whether to activate WebDAV as the main remote type
* @returns Promise that resolves to true if setup completed successfully, false otherwise
*/
async onWebDAVManualSetup(
userMode: UserMode,
currentSetting: ObsidianLiveSyncSettings,
activate = true
): Promise<boolean> {
return await this.onRemoteManualSetup("webdav", userMode, currentSetting, activate);
}
/**
@@ -239,24 +311,7 @@ export class SetupManager extends AbstractModule {
currentSetting: ObsidianLiveSyncSettings,
activate = true
): Promise<boolean> {
const p2pConf = await this.dialogManager.openWithExplicitCancel<SetupRemoteP2PResultType, P2PSyncSetting>(
SetupRemoteP2P,
currentSetting
);
if (p2pConf === "cancelled") {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
return await this.onOnboard(userMode);
}
const newSetting = {
...copySettingsForRemoteProfileUpdate(currentSetting),
...p2pConf,
} as ObsidianLiveSyncSettings;
upsertRemoteConfigurationInPlace(newSetting, "p2p", {
id: newSetting.P2P_ActiveRemoteConfigurationId || undefined,
activate,
activateForP2P: true,
});
return await this.onConfirmApplySettingsFromWizard(newSetting, userMode, activate);
return await this.onRemoteManualSetup("p2p", userMode, currentSetting, activate);
}
/**
@@ -310,21 +365,15 @@ export class SetupManager extends AbstractModule {
* @returns
*/
async onSelectServer(currentSetting: ObsidianLiveSyncSettings, userMode: UserMode): Promise<boolean> {
const method = await this.dialogManager.openWithExplicitCancel<SetupRemoteResultType>(SetupRemote);
if (method === "couchdb") {
return await this.onCouchDBManualSetup(userMode, currentSetting, true);
} else if (method === "bucket") {
return await this.onBucketManualSetup(userMode, currentSetting, true);
} else if (method === "p2p") {
return await this.onP2PManualSetup(userMode, currentSetting, true);
} else if (method === "cancelled") {
const method = await this.selectRemoteSetupProvider();
if (method === false) {
this._log("Manual configuration cancelled.", LOG_LEVEL_NOTICE);
if (userMode !== UserMode.Unknown) {
return await this.onOnboard(userMode);
}
return false;
}
// Should not reach here.
return false;
return await this.onRemoteManualSetup(method, userMode, currentSetting, true);
}
/**
* Confirms and applies settings obtained from the wizard
@@ -2,12 +2,15 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
REMOTE_WEBDAV,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { SettingService } from "@vrtmrz/livesync-commonlib/compat/services/base/SettingService";
import { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { createNewVaultSettings } from "@vrtmrz/livesync-commonlib/settings";
import { ConnectionStringParser } from "@vrtmrz/livesync-commonlib/compat/common/ConnectionString";
vi.mock("./SetupWizard/dialogs/Intro.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SelectMethodNewUser.svelte", () => ({ default: {} }));
@@ -20,6 +23,7 @@ vi.mock("./SetupWizard/dialogs/OutroAskUserMode.svelte", () => ({ default: {} })
vi.mock("./SetupWizard/dialogs/SetupRemote.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SetupRemoteCouchDB.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SetupRemoteBucket.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SetupRemoteWebDAV.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SetupRemoteP2P.svelte", () => ({ default: {} }));
vi.mock("./SetupWizard/dialogs/SetupRemoteE2EE.svelte", () => ({ default: {} }));
@@ -305,6 +309,44 @@ describe("SetupManager", () => {
expect(Object.keys(current.remoteConfigurations).some((id) => id.startsWith("legacy-"))).toBe(false);
});
it("preserves Adaptive WebDAV fields imported through a Setup URI profile", async () => {
const { manager, setting, dialogManager } = createSetupManager();
const repositoryId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
const imported = {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
remoteConfigurations: {
webdav: {
id: "webdav",
name: "WebDAV notes",
uri:
"sls+webdav://alice:secret@dav.example/dav?prefix=notes%2F" +
`&journalFormat=adaptive-v1&expectedRepositoryId=${repositoryId}&packReadPolicy=range`,
isEncrypted: false,
},
},
activeConfigurationId: "webdav",
} as ObsidianLiveSyncSettings;
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce(imported)
.mockResolvedValueOnce("compatible-existing-user");
await manager.onUseSetupURI(UserMode.Unknown, "mock-config://webdav-settings");
const current = setting.currentSettings();
expect(current.activeConfigurationId).toBe("webdav");
const parsed = ConnectionStringParser.parse(current.remoteConfigurations.webdav.uri);
expect(parsed).toMatchObject({
type: "webdav",
settings: {
webDAVactiveConnectionURI: "sls+webdav://alice:secret@dav.example/dav?prefix=notes%2F",
expectedRepositoryId: repositoryId,
journalFormat: "adaptive-v1",
packReadPolicy: "range",
},
});
});
it("adds and activates a manually configured CouchDB without replacing existing profiles", async () => {
const { manager, setting, dialogManager } = createSetupManager();
setting.settings = {
@@ -407,6 +449,9 @@ describe("SetupManager", () => {
useCustomRequestHandler: false,
bucketCustomHeaders: "",
forcePathStyle: true,
expectedRepositoryId: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
journalFormat: "adaptive-v1",
packReadPolicy: "range",
})
.mockResolvedValueOnce(true);
@@ -419,6 +464,193 @@ describe("SetupManager", () => {
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.name).toBe("S3 notes");
expect(activeProfile?.uri).toContain("sls+s3://key:secret@storage.example");
expect(activeProfile?.uri).toContain("journalFormat=adaptive-v1");
expect(activeProfile?.uri).toContain("packReadPolicy=range");
expect(activeProfile?.uri).toContain("expectedRepositoryId=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
});
it("uses the registered provider choices when configuring a Settings edit buffer", async () => {
const { manager, setting, dialogManager } = createSetupManager();
const bucketSettings = {
endpoint: "https://storage.example",
accessKey: "key",
secretKey: "secret",
bucket: "notes",
region: "auto",
bucketPrefix: "",
useCustomRequestHandler: false,
bucketCustomHeaders: "",
forcePathStyle: true,
};
dialogManager.openWithExplicitCancel.mockResolvedValueOnce("s3").mockResolvedValueOnce(bucketSettings);
const nextSettings = await manager.configureRemoteForSettings(setting.currentSettings());
expect(dialogManager.openWithExplicitCancel).toHaveBeenNthCalledWith(
1,
expect.anything(),
expect.arrayContaining([
expect.objectContaining({ type: "couchdb" }),
expect.objectContaining({ type: "s3" }),
expect.objectContaining({ type: "p2p" }),
])
);
expect(dialogManager.openWithExplicitCancel).toHaveBeenNthCalledWith(
2,
expect.anything(),
setting.currentSettings()
);
expect(nextSettings).toEqual(expect.objectContaining({ ...bucketSettings, remoteType: REMOTE_MINIO }));
});
it("preselects a repository identity during fresh Adaptive Object Storage onboarding", async () => {
const { manager, setting, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce({
endpoint: "https://storage.example",
accessKey: "key",
secretKey: "secret",
bucket: "notes",
region: "auto",
bucketPrefix: "",
useCustomRequestHandler: false,
bucketCustomHeaders: "",
forcePathStyle: true,
expectedRepositoryId: "",
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
})
.mockResolvedValueOnce(true);
await manager.onBucketManualSetup(UserMode.NewUser, setting.currentSettings());
const current = setting.currentSettings();
expect(current.expectedRepositoryId).toMatch(/^[A-Za-z0-9_-]{43}$/u);
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.uri).toContain(`expectedRepositoryId=${current.expectedRepositoryId}`);
});
it("leaves an existing-device Adaptive Object Storage attachment on trust on first use", async () => {
const { manager, setting, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce({
endpoint: "https://storage.example",
accessKey: "key",
secretKey: "secret",
bucket: "notes",
region: "auto",
bucketPrefix: "",
useCustomRequestHandler: false,
bucketCustomHeaders: "",
forcePathStyle: true,
expectedRepositoryId: "",
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
})
.mockResolvedValueOnce(true);
await manager.onBucketManualSetup(UserMode.ExistingUser, setting.currentSettings());
const current = setting.currentSettings();
expect(current.expectedRepositoryId).toBe("");
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.uri).not.toContain("expectedRepositoryId=");
});
it("adds and activates a manually configured WebDAV profile without replacing existing profiles", async () => {
const { manager, setting, dialogManager } = createSetupManager();
setting.settings = {
...setting.currentSettings(),
isConfigured: true,
remoteConfigurations: {
existing: {
id: "existing",
name: "Existing remote",
uri: "sls+http://old:secret@old.example/?db=old",
isEncrypted: false,
},
},
activeConfigurationId: "existing",
};
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce({
webDAVactiveConnectionURI:
"sls+webdav://alice:secret@dav.example/remote.php/dav/files/alice?prefix=notes%2F",
expectedRepositoryId: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
journalFormat: "adaptive-v1",
packReadPolicy: "range",
})
.mockResolvedValueOnce(true);
await manager.onWebDAVManualSetup(UserMode.ExistingUser, setting.currentSettings());
const current = setting.currentSettings();
expect(current.remoteType).toBe(REMOTE_WEBDAV);
expect(current.remoteConfigurations.existing).toBeDefined();
expect(Object.keys(current.remoteConfigurations)).toHaveLength(2);
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.name).toBe("WebDAV dav.example");
expect(activeProfile?.uri).toContain("sls+webdav://alice:secret@dav.example");
expect(activeProfile?.uri).toContain("journalFormat=adaptive-v1");
expect(activeProfile?.uri).toContain("packReadPolicy=range");
expect(activeProfile?.uri).toContain("expectedRepositoryId=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
});
it("preselects a repository identity during fresh Adaptive WebDAV onboarding", async () => {
const { manager, setting, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce({
webDAVactiveConnectionURI:
"sls+webdav://alice:secret@dav.example/remote.php/dav/files/alice?prefix=notes%2F",
expectedRepositoryId: "",
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
})
.mockResolvedValueOnce(true);
await manager.onWebDAVManualSetup(UserMode.NewUser, setting.currentSettings());
const current = setting.currentSettings();
expect(current.expectedRepositoryId).toMatch(/^[A-Za-z0-9_-]{43}$/u);
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.uri).toContain(`expectedRepositoryId=${current.expectedRepositoryId}`);
});
it("leaves an existing-device Adaptive WebDAV attachment on trust on first use", async () => {
const { manager, setting, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel
.mockResolvedValueOnce({
webDAVactiveConnectionURI:
"sls+webdav://alice:secret@dav.example/remote.php/dav/files/alice?prefix=notes%2F",
expectedRepositoryId: "",
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
})
.mockResolvedValueOnce(true);
await manager.onWebDAVManualSetup(UserMode.ExistingUser, setting.currentSettings());
const current = setting.currentSettings();
expect(current.expectedRepositoryId).toBe("");
const activeProfile = current.remoteConfigurations[current.activeConfigurationId];
expect(activeProfile?.uri).not.toContain("expectedRepositoryId=");
});
it.each([
[UserMode.NewUser, "onboarding"],
[UserMode.ExistingUser, "onboarding"],
[UserMode.Update, "settings"],
] as const)("passes the %s WebDAV verification policy to the manual setup dialogue", async (userMode, mode) => {
const { manager, setting, dialogManager } = createSetupManager();
dialogManager.openWithExplicitCancel.mockResolvedValueOnce("cancelled");
vi.spyOn(manager, "onOnboard").mockResolvedValue(false);
await manager.onWebDAVManualSetup(userMode, setting.currentSettings());
expect(dialogManager.openWithExplicitCancel).toHaveBeenCalledWith(expect.anything(), {
settings: setting.currentSettings(),
mode,
});
});
it("creates and selects a P2P profile during fresh manual onboarding", async () => {
@@ -0,0 +1,84 @@
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type {
BuiltInRemoteConfiguration,
RemoteProviderConfiguration,
} from "@vrtmrz/livesync-commonlib/remote-configurations";
import type { ComponentHasResult } from "@/modules/services/LiveSyncUI/svelteDialog";
export type RemoteSetupIntent = "create-or-connect" | "connect-existing" | "settings";
export interface RemoteSetupDialogManager {
openWithExplicitCancel<TResult, TInitial = TResult>(
component: ComponentHasResult<TResult, TInitial>,
initialData?: TInitial
): Promise<TResult>;
}
export interface RemoteSetupContext {
readonly dialogManager: RemoteSetupDialogManager;
readonly intent: RemoteSetupIntent;
readonly settings: ObsidianLiveSyncSettings;
}
export interface RemoteSetupChoice<TType extends string = string> {
readonly description: string;
readonly proceedTitle: string;
readonly title: string;
readonly type: TType;
}
export interface RemoteSetupProviderDescriptor<
TConfiguration extends RemoteProviderConfiguration = RemoteProviderConfiguration,
> {
readonly type: TConfiguration["type"];
choice(): Omit<RemoteSetupChoice<TConfiguration["type"]>, "type">;
open(context: RemoteSetupContext): Promise<TConfiguration | "cancelled">;
}
/**
* Host-side presentation for the remote providers compiled into this plug-in.
*
* Connection semantics remain in Commonlib's remote provider registry. This registry only owns
* the Obsidian-specific choice text and setup dialogue for each provider.
*/
export class RemoteSetupRegistry<TConfiguration extends RemoteProviderConfiguration = BuiltInRemoteConfiguration> {
private readonly providersByType = new Map<string, RemoteSetupProviderDescriptor<TConfiguration>>();
private frozen = false;
register<TProviderConfiguration extends TConfiguration>(
descriptor: RemoteSetupProviderDescriptor<TProviderConfiguration>
): this {
if (this.frozen) throw new Error("The remote setup registry is frozen");
if (this.providersByType.has(descriptor.type)) {
throw new Error(`Remote setup provider '${descriptor.type}' is already registered`);
}
this.providersByType.set(descriptor.type, descriptor);
return this;
}
freeze(): this {
this.frozen = true;
return this;
}
isFrozen(): boolean {
return this.frozen;
}
choices(): RemoteSetupChoice<TConfiguration["type"]>[] {
return [...this.providersByType.values()].map((provider) => ({
...provider.choice(),
type: provider.type,
}));
}
has(type: string): type is TConfiguration["type"] {
return this.providersByType.has(type);
}
open(type: TConfiguration["type"], context: RemoteSetupContext): Promise<TConfiguration | "cancelled"> {
const provider = this.providersByType.get(type);
if (!provider) throw new Error(`Unsupported remote setup provider: ${type}`);
return provider.open(context);
}
}
@@ -0,0 +1,69 @@
import { describe, expect, it, vi } from "vitest";
import { RemoteSetupRegistry, type RemoteSetupProviderDescriptor } from "./RemoteSetupRegistry";
type TestConfiguration =
| { type: "alpha"; settings: { value: string } }
| { type: "beta"; settings: { enabled: boolean } };
function alphaProvider(
open: RemoteSetupProviderDescriptor<Extract<TestConfiguration, { type: "alpha" }>>["open"] = async () => ({
type: "alpha",
settings: { value: "configured" },
})
): RemoteSetupProviderDescriptor<Extract<TestConfiguration, { type: "alpha" }>> {
return {
type: "alpha",
choice: () => ({
title: "Alpha",
description: "Alpha description",
proceedTitle: "Configure Alpha",
}),
open,
};
}
describe("RemoteSetupRegistry", () => {
it("preserves registration order and dispatches setup through the selected provider", async () => {
const openAlpha = vi.fn(alphaProvider().open);
const registry = new RemoteSetupRegistry<TestConfiguration>()
.register(alphaProvider(openAlpha))
.register({
type: "beta",
choice: () => ({
title: "Beta",
description: "Beta description",
proceedTitle: "Configure Beta",
}),
open: async () => ({ type: "beta", settings: { enabled: true } }),
})
.freeze();
const context = {
dialogManager: { openWithExplicitCancel: vi.fn() },
intent: "settings" as const,
settings: {} as never,
};
expect(registry.choices().map((choice) => choice.type)).toEqual(["alpha", "beta"]);
await expect(registry.open("alpha", context)).resolves.toEqual({
type: "alpha",
settings: { value: "configured" },
});
expect(openAlpha).toHaveBeenCalledWith(context);
});
it("rejects duplicate and late registrations", () => {
const registry = new RemoteSetupRegistry<TestConfiguration>().register(alphaProvider());
expect(() => registry.register(alphaProvider())).toThrow("already registered");
registry.freeze();
expect(registry.isFrozen()).toBe(true);
expect(() =>
registry.register({
type: "beta",
choice: () => ({ title: "Beta", description: "", proceedTitle: "" }),
open: async () => "cancelled",
})
).toThrow("frozen");
});
});
@@ -0,0 +1,133 @@
import type { BucketSyncSetting, P2PSyncSetting } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { BuiltInRemoteConfiguration } from "@vrtmrz/livesync-commonlib/remote-configurations";
import { defaultRemoteProviderRegistry } from "@vrtmrz/livesync-commonlib/remote-configurations";
import { $msg as translateMessage } from "@/common/translation";
import SetupRemoteBucket from "./dialogs/SetupRemoteBucket.svelte";
import SetupRemoteCouchDB from "./dialogs/SetupRemoteCouchDB.svelte";
import SetupRemoteP2P from "./dialogs/SetupRemoteP2P.svelte";
import SetupRemoteWebDAV from "./dialogs/SetupRemoteWebDAV.svelte";
import type {
SetupRemoteBucketResultType,
SetupRemoteCouchDBInitialData,
SetupRemoteCouchDBResultType,
SetupRemoteP2PResultType,
SetupRemoteWebDAVInitialData,
SetupRemoteWebDAVResultType,
} from "./dialogs/setupDialogTypes";
import { RemoteSetupRegistry, type RemoteSetupProviderDescriptor } from "./RemoteSetupRegistry";
type ConfigurationOf<TType extends BuiltInRemoteConfiguration["type"]> = Extract<
BuiltInRemoteConfiguration,
{ type: TType }
>;
function assertSemanticProvider(type: BuiltInRemoteConfiguration["type"]): void {
const isRegistered = defaultRemoteProviderRegistry.providerSummaries().some((provider) => provider.type === type);
if (!isRegistered) throw new Error(`Remote setup provider '${type}' has no Commonlib provider`);
}
export function useCouchDBRemoteSetup(
registry: RemoteSetupRegistry<BuiltInRemoteConfiguration>
): RemoteSetupRegistry<BuiltInRemoteConfiguration> {
const descriptor: RemoteSetupProviderDescriptor<ConfigurationOf<"couchdb">> = {
type: "couchdb",
choice: () => ({
title: "CouchDB",
description: translateMessage("Ui.SetupWizard.SetupRemote.CouchDbOptionDesc"),
proceedTitle: translateMessage("Continue to CouchDB setup"),
}),
open: async ({ dialogManager, intent, settings }) => {
const result = await dialogManager.openWithExplicitCancel<
SetupRemoteCouchDBResultType,
SetupRemoteCouchDBInitialData
>(SetupRemoteCouchDB, { settings, mode: intent });
return result === "cancelled" ? result : { type: "couchdb", settings: result };
},
};
assertSemanticProvider(descriptor.type);
return registry.register(descriptor);
}
export function useS3RemoteSetup(
registry: RemoteSetupRegistry<BuiltInRemoteConfiguration>
): RemoteSetupRegistry<BuiltInRemoteConfiguration> {
const descriptor: RemoteSetupProviderDescriptor<ConfigurationOf<"s3">> = {
type: "s3",
choice: () => ({
title: translateMessage("Ui.SetupWizard.SetupRemote.BucketOption"),
description: translateMessage("Ui.SetupWizard.SetupRemote.BucketOptionDesc"),
proceedTitle: translateMessage("Ui.SetupWizard.SetupRemote.ProceedBucket"),
}),
open: async ({ dialogManager, settings }) => {
const result = await dialogManager.openWithExplicitCancel<SetupRemoteBucketResultType, BucketSyncSetting>(
SetupRemoteBucket,
settings
);
return result === "cancelled" ? result : { type: "s3", settings: result };
},
};
assertSemanticProvider(descriptor.type);
return registry.register(descriptor);
}
export function useP2PRemoteSetup(
registry: RemoteSetupRegistry<BuiltInRemoteConfiguration>
): RemoteSetupRegistry<BuiltInRemoteConfiguration> {
const descriptor: RemoteSetupProviderDescriptor<ConfigurationOf<"p2p">> = {
type: "p2p",
choice: () => ({
title: translateMessage("Ui.SetupWizard.SetupRemote.P2POption"),
description: translateMessage(
"No central data-storage server is required, but a signalling relay is required for peer discovery. Both devices must be online at the same time. Vault data travels through the encrypted P2P connection, not through the signalling relay. Some features may be limited."
),
proceedTitle: translateMessage("Ui.SetupWizard.SetupRemote.ProceedP2P"),
}),
open: async ({ dialogManager, settings }) => {
const result = await dialogManager.openWithExplicitCancel<SetupRemoteP2PResultType, P2PSyncSetting>(
SetupRemoteP2P,
settings
);
return result === "cancelled" ? result : { type: "p2p", settings: result };
},
};
assertSemanticProvider(descriptor.type);
return registry.register(descriptor);
}
export function useWebDAVRemoteSetup(
registry: RemoteSetupRegistry<BuiltInRemoteConfiguration>
): RemoteSetupRegistry<BuiltInRemoteConfiguration> {
const descriptor: RemoteSetupProviderDescriptor<ConfigurationOf<"webdav">> = {
type: "webdav",
choice: () => ({
title: translateMessage("WebDAV Journal"),
description: translateMessage(
"Store Journal data in a dedicated WebDAV collection. Adaptive mode is experimental and requires an endpoint safety check."
),
proceedTitle: translateMessage("Continue to WebDAV setup"),
}),
open: async ({ dialogManager, intent, settings }) => {
const result = await dialogManager.openWithExplicitCancel<
SetupRemoteWebDAVResultType,
SetupRemoteWebDAVInitialData
>(SetupRemoteWebDAV, {
settings,
mode: intent === "settings" ? "settings" : "onboarding",
});
return result === "cancelled" ? result : { type: "webdav", settings: result };
},
};
assertSemanticProvider(descriptor.type);
return registry.register(descriptor);
}
export function createBuiltInRemoteSetupRegistry(): RemoteSetupRegistry<BuiltInRemoteConfiguration> {
const registry = new RemoteSetupRegistry<BuiltInRemoteConfiguration>();
useCouchDBRemoteSetup(registry);
useS3RemoteSetup(registry);
useWebDAVRemoteSetup(registry);
useP2PRemoteSetup(registry);
return registry;
}
export const builtInRemoteSetupRegistry = createBuiltInRemoteSetupRegistry().freeze();
@@ -7,58 +7,33 @@
import Instruction from "@/modules/services/LiveSyncUI/components/Instruction.svelte";
import UserDecisions from "@/modules/services/LiveSyncUI/components/UserDecisions.svelte";
import { $msg as translateMessage } from "@/common/translation";
import {
TYPE_COUCHDB,
TYPE_BUCKET,
TYPE_P2P,
TYPE_CANCELLED,
type SetupRemoteResultType,
} from "./setupDialogTypes";
import { onMount } from "svelte";
import { type GuestDialogProps } from "@/modules/services/LiveSyncUI/svelteDialog";
import { TYPE_CANCELLED, type SetupRemoteInitialData, type SetupRemoteResultType } from "./setupDialogTypes";
type Props = {
setResult: (result: SetupRemoteResultType) => void;
};
const { setResult }: Props = $props();
type Props = GuestDialogProps<SetupRemoteResultType, SetupRemoteInitialData>;
const { setResult, getInitialData }: Props = $props();
let choices = $state<SetupRemoteInitialData>([]);
let userType = $state<SetupRemoteResultType>(TYPE_CANCELLED);
let proceedTitle = $derived.by(() => {
if (userType === TYPE_COUCHDB) {
return translateMessage("Continue to CouchDB setup");
} else if (userType === TYPE_BUCKET) {
return translateMessage("Ui.SetupWizard.SetupRemote.ProceedBucket");
} else if (userType === TYPE_P2P) {
return translateMessage("Ui.SetupWizard.SetupRemote.ProceedP2P");
} else {
return translateMessage("Please select an option to proceed");
}
});
const canProceed = $derived.by(() => {
return userType === TYPE_COUCHDB || userType === TYPE_BUCKET || userType === TYPE_P2P;
onMount(() => {
choices = getInitialData?.() ?? [];
});
const selectedChoice = $derived(choices.find((choice) => choice.type === userType));
const proceedTitle = $derived(
selectedChoice?.proceedTitle ?? translateMessage("Please select an option to proceed")
);
const canProceed = $derived(selectedChoice !== undefined);
</script>
<DialogHeader title={translateMessage("Ui.SetupWizard.SetupRemote.Title")} />
<Instruction>
<Question>{translateMessage("Ui.SetupWizard.SetupRemote.Guidance")}</Question>
<Options>
<Option selectedValue={TYPE_COUCHDB} title="CouchDB" bind:value={userType}>
{translateMessage("Ui.SetupWizard.SetupRemote.CouchDbOptionDesc")}
</Option>
<Option
selectedValue={TYPE_BUCKET}
title={translateMessage("Ui.SetupWizard.SetupRemote.BucketOption")}
bind:value={userType}
>
{translateMessage("Ui.SetupWizard.SetupRemote.BucketOptionDesc")}
</Option>
<Option
selectedValue={TYPE_P2P}
title={translateMessage("Ui.SetupWizard.SetupRemote.P2POption")}
bind:value={userType}
>
{translateMessage(
"No central data-storage server is required, but a signalling relay is required for peer discovery. Both devices must be online at the same time. Vault data travels through the encrypted P2P connection, not through the signalling relay. Some features may be limited."
)}
</Option>
{#each choices as choice (choice.type)}
<Option selectedValue={choice.type} title={choice.title} bind:value={userType}>
{choice.description}
</Option>
{/each}
</Options>
</Instruction>
<UserDecisions>
@@ -20,10 +20,16 @@
import { copyTo, pickBucketSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { TYPE_CANCELLED, type SetupRemoteBucketResultType } from "./setupDialogTypes";
import { $msg as translateMessage } from "@/common/translation";
import { normaliseS3JournalSettings } from "./s3JournalSettings";
const default_setting = pickBucketSyncSettings(DEFAULT_SETTINGS);
let syncSetting = $state<BucketSyncSetting>({ ...default_setting });
let syncSetting = $state<BucketSyncSetting>({
...default_setting,
expectedRepositoryId: default_setting.expectedRepositoryId ?? "",
journalFormat: default_setting.journalFormat ?? "opaque-v1",
packReadPolicy: default_setting.packReadPolicy ?? "whole-pack",
});
type Props = GuestDialogProps<SetupRemoteBucketResultType, BucketSyncSetting>;
@@ -58,11 +64,10 @@
isEndpointSupplied
);
});
const isAdaptive = $derived(syncSetting.journalFormat === "adaptive-v1");
function generateSetting() {
const connSetting: BucketSyncSetting = {
...syncSetting,
};
const connSetting = normaliseS3JournalSettings(syncSetting);
const trialSettings: BucketSyncSetting = {
...connSetting,
};
@@ -115,8 +120,13 @@
}
}
function commit() {
const setting = pickBucketSyncSettings(generateSetting());
setResult(setting);
error = "";
try {
const setting = pickBucketSyncSettings(generateSetting());
setResult(setting);
} catch (e) {
error = translateMessage("Invalid Object Storage settings: ${reason}", { reason: `${e}` });
}
}
function cancel() {
setResult(TYPE_CANCELLED);
@@ -220,6 +230,30 @@
</InfoNote>
<ExtraItems title={translateMessage("Advanced Settings")}>
<InputRow label={translateMessage("Journal data format")}>
<select name="s3-journal-format" bind:value={syncSetting.journalFormat}>
<option value="opaque-v1">{translateMessage("Opaque Journal (current format)")}</option>
<option value="adaptive-v1">{translateMessage("Adaptive Journal (experimental)")}</option>
</select>
</InputRow>
<InfoNote warning visible={isAdaptive}>
{translateMessage(
"Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats."
)}
</InfoNote>
{#if isAdaptive}
<InputRow label={translateMessage("Pack retrieval")}>
<select name="s3-pack-read-policy" bind:value={syncSetting.packReadPolicy}>
<option value="whole-pack">{translateMessage("Download complete Packs")}</option>
<option value="range">{translateMessage("Use S3 Range requests")}</option>
</select>
</InputRow>
<InfoNote>
{translateMessage(
"Complete Pack reads favour throughput. Range reads can reduce transferred bytes. The connection test verifies exact Range support on this endpoint, and synchronisation refuses an unsupported selection before writing."
)}
</InfoNote>
{/if}
<InputRow label={translateMessage("Custom Headers")}>
<textarea
name="bucket-custom-headers"
@@ -0,0 +1,403 @@
<script lang="ts">
import { onMount } from "svelte";
import DialogHeader from "@/modules/services/LiveSyncUI/components/DialogHeader.svelte";
import Guidance from "@/modules/services/LiveSyncUI/components/Guidance.svelte";
import Decision from "@/modules/services/LiveSyncUI/components/Decision.svelte";
import UserDecisions from "@/modules/services/LiveSyncUI/components/UserDecisions.svelte";
import InfoNote from "@/modules/services/LiveSyncUI/components/InfoNote.svelte";
import ExtraItems from "@/modules/services/LiveSyncUI/components/ExtraItems.svelte";
import InputRow from "@/modules/services/LiveSyncUI/components/InputRow.svelte";
import Password from "@/modules/services/LiveSyncUI/components/Password.svelte";
import { getDialogContext, type GuestDialogProps } from "@/modules/services/LiveSyncUI/svelteDialog";
import {
DEFAULT_SETTINGS,
PREFERRED_JOURNAL_SYNC,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
REMOTE_WEBDAV,
isJournalStorageConnectionInspector,
type JournalStorageConnectivityResult,
type WebDAVSyncSetting,
} from "@vrtmrz/livesync-commonlib/journal-storage";
import {
TYPE_CANCELLED,
type SetupRemoteWebDAVInitialData,
type SetupRemoteWebDAVResultType,
type WebDAVSetupMode,
} from "./setupDialogTypes";
import {
summariseAdaptiveCapabilityInspection,
webDAVJournalFormFromSettings,
webDAVSyncSettingsFromForm,
type WebDAVJournalForm,
} from "./webDAVJournalSettings";
import { $msg as translateMessage } from "@/common/translation";
let syncSetting = $state<WebDAVJournalForm>(
webDAVJournalFormFromSettings({
webDAVactiveConnectionURI: "",
expectedRepositoryId: "",
journalFormat: "opaque-v1",
packReadPolicy: "whole-pack",
})
);
let setupMode = $state<WebDAVSetupMode>("settings");
let error = $state("");
let processing = $state(false);
let inspection = $state<JournalStorageConnectivityResult | undefined>();
let inspectionFingerprint = $state("");
let inspectedSettings = $state<WebDAVSyncSetting | undefined>();
type Props = GuestDialogProps<SetupRemoteWebDAVResultType, SetupRemoteWebDAVInitialData>;
const { setResult, getInitialData }: Props = $props();
const context = getDialogContext();
onMount(() => {
const initialData = getInitialData?.();
if (!initialData) return;
setupMode = initialData.mode;
try {
Object.assign(syncSetting, webDAVJournalFormFromSettings(initialData.settings));
} catch (ex) {
error = translateMessage("Invalid WebDAV settings: ${REASON}", {
REASON: ex instanceof Error ? ex.message : `${ex}`,
});
}
});
const isAdaptive = $derived(syncSetting.journalFormat === "adaptive-v1");
const isEndpointInsecure = $derived.by(() => syncSetting.endpoint.trim().toLowerCase().startsWith("http://"));
const isEndpointValid = $derived.by(() => {
try {
const endpoint = new URL(syncSetting.endpoint.trim());
return (
(endpoint.protocol === "http:" || endpoint.protocol === "https:") &&
endpoint.search === "" &&
endpoint.hash === ""
);
} catch {
return false;
}
});
const isEndpointInvalid = $derived(syncSetting.endpoint.trim() !== "" && !isEndpointValid);
const formFingerprint = $derived.by(() => JSON.stringify(syncSetting));
const inspectionIsCurrent = $derived(
inspection !== undefined && inspectionFingerprint !== "" && inspectionFingerprint === formFingerprint
);
const adaptiveSummary = $derived.by(() => {
if (!inspectionIsCurrent || !inspection?.adaptiveCapabilities) return undefined;
return summariseAdaptiveCapabilityInspection(inspection.adaptiveCapabilities);
});
function generateSetting(webDAVSettings: WebDAVSyncSetting): ObsidianLiveSyncSettings {
return {
...DEFAULT_SETTINGS,
...PREFERRED_JOURNAL_SYNC,
remoteType: REMOTE_WEBDAV,
...webDAVSettings,
};
}
function explainUnavailable(result: JournalStorageConnectivityResult): string {
if (
result.remoteFormat !== undefined &&
result.remoteFormat !== "empty" &&
result.remoteFormat !== syncSetting.journalFormat
) {
return translateMessage(
"The remote contains ${REMOTE_FORMAT} data, but this profile selects ${SELECTED_FORMAT}. Rebuild the remote or restore the matching format.",
{
REMOTE_FORMAT: result.remoteFormat,
SELECTED_FORMAT: syncSetting.journalFormat,
}
);
}
return translateMessage("The selected WebDAV Journal policy is not supported by this endpoint.");
}
async function inspectConnection(trialRemoteSetting: ObsidianLiveSyncSettings, testedFingerprint: string) {
const replicator = await context.services.replicator.getNewReplicator(trialRemoteSetting);
if (!replicator) {
throw new Error(translateMessage("Failed to create replicator instance."));
}
if (!isJournalStorageConnectionInspector(replicator)) {
throw new Error(translateMessage("This build cannot inspect WebDAV Journal capabilities."));
}
const result = await replicator.inspectJournalStorageConnection(trialRemoteSetting);
inspection = result;
inspectionFingerprint = testedFingerprint;
return result;
}
async function checkConnection() {
error = "";
inspection = undefined;
inspectionFingerprint = "";
inspectedSettings = undefined;
processing = true;
try {
const testedFingerprint = formFingerprint;
const candidate = webDAVSyncSettingsFromForm(syncSetting);
const trialRemoteSetting = generateSetting(candidate);
const result = await inspectConnection(trialRemoteSetting, testedFingerprint);
if (testedFingerprint !== formFingerprint) return;
if (!result.available) {
error = explainUnavailable(result);
return;
}
inspectedSettings = candidate;
} catch (ex) {
error = translateMessage("Error during connection test: ${reason}", {
reason: ex instanceof Error ? ex.message : `${ex}`,
});
} finally {
processing = false;
}
}
function commitVerified() {
if (!inspectionIsCurrent || !inspection?.available || !inspectedSettings) return;
setResult(inspectedSettings);
}
function commit() {
error = "";
try {
setResult(webDAVSyncSettingsFromForm(syncSetting));
} catch (ex) {
error = translateMessage("Invalid WebDAV settings: ${REASON}", {
REASON: ex instanceof Error ? ex.message : `${ex}`,
});
}
}
</script>
<DialogHeader title={translateMessage("WebDAV Journal Configuration")} />
<Guidance>
{translateMessage(
"Configure a dedicated WebDAV collection for Journal synchronisation. Opaque Journal needs ordinary WebDAV access. Adaptive Journal additionally runs an endpoint safety check before the profile is accepted."
)}
</Guidance>
<InputRow label={translateMessage("Endpoint URL")}>
<input
type="text"
name="webdav-endpoint"
placeholder="https://dav.example/remote.php/dav/files/alice"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
required
pattern="^https?://.+"
bind:value={syncSetting.endpoint}
/>
</InputRow>
<InfoNote warning visible={isEndpointInsecure}>
{translateMessage("We can use only Secure (HTTPS) connections on Obsidian Mobile.")}
</InfoNote>
<InfoNote error visible={isEndpointInvalid}>
{translateMessage("Enter a complete HTTP or HTTPS endpoint without a query string or fragment.")}
</InfoNote>
<InputRow label={translateMessage("Username")}>
<input
type="text"
name="webdav-username"
placeholder={translateMessage("Enter your username")}
autocorrect="off"
autocapitalize="off"
spellcheck="false"
bind:value={syncSetting.username}
/>
</InputRow>
<InputRow label={translateMessage("Password")}>
<Password
name="webdav-password"
placeholder={translateMessage("Enter your password")}
bind:value={syncSetting.password}
/>
</InputRow>
<InputRow label={translateMessage("Collection prefix")}>
<input
type="text"
name="webdav-prefix"
placeholder="livesync-journal/"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
bind:value={syncSetting.prefix}
/>
</InputRow>
<InfoNote>
{translateMessage(
"Use a dedicated prefix. WebDAV listing scans the collection, so unrelated files and a long Journal history increase discovery work."
)}
</InfoNote>
<InputRow label={translateMessage("Use internal API")}>
<input type="checkbox" name="webdav-use-internal-api" bind:checked={syncSetting.useCustomRequestHandler} />
</InputRow>
<InfoNote>
{translateMessage(
"Enable this when browser-compatible requests are blocked by CORS. It uses Obsidian's internal request API and may behave differently from standard browser fetch."
)}
</InfoNote>
<ExtraItems title={translateMessage("Advanced Settings")}>
<InputRow label={translateMessage("Journal data format")}>
<select name="webdav-journal-format" bind:value={syncSetting.journalFormat}>
<option value="opaque-v1">{translateMessage("Opaque Journal (current format)")}</option>
<option value="adaptive-v1">{translateMessage("Adaptive Journal (experimental)")}</option>
</select>
</InputRow>
<InfoNote warning visible={isAdaptive}>
{translateMessage(
"Adaptive Journal uses immutable objects and a separate remote format. Existing Opaque Journal data is not migrated or read. Rebuild the remote when changing formats."
)}
</InfoNote>
{#if isAdaptive}
<InputRow label={translateMessage("Expected repository ID")}>
<input
type="text"
name="webdav-expected-repository-id"
autocorrect="off"
autocapitalize="off"
spellcheck="false"
bind:value={syncSetting.expectedRepositoryId}
/>
</InputRow>
<InfoNote>
{translateMessage(
"This optional identity pins a trusted Adaptive repository. A Setup URI can supply it; leave it blank only when creating a repository or intentionally trusting the first compatible repository reached."
)}
</InfoNote>
<InputRow label={translateMessage("Pack retrieval")}>
<select name="webdav-pack-read-policy" bind:value={syncSetting.packReadPolicy}>
<option value="whole-pack">{translateMessage("Download complete Packs")}</option>
<option value="range">{translateMessage("Use HTTP Range requests")}</option>
</select>
</InputRow>
<InfoNote>
{translateMessage(
"Complete Pack reads favour throughput and are the portable default. Range reads can reduce transferred bytes, but this endpoint must pass the exact byte-range check."
)}
</InfoNote>
<InfoNote caution>
{translateMessage(
"The Adaptive safety check writes, reads, lists, and removes disposable objects under a random probe prefix. It does not inspect Vault data."
)}
</InfoNote>
{/if}
<InputRow label={translateMessage("Custom Headers")}>
<textarea
name="webdav-custom-headers"
placeholder="e.g., x-example-header: value"
bind:value={syncSetting.customHeaders}
autocapitalize="off"
spellcheck="false"
rows="4"
></textarea>
</InputRow>
</ExtraItems>
{#if adaptiveSummary}
{#if adaptiveSummary.required.kind === "verified"}
<InfoNote notice>
{translateMessage("Required Adaptive operations are supported by this WebDAV endpoint.")}
</InfoNote>
{:else if adaptiveSummary.required.kind === "unsupported"}
<InfoNote error>
{translateMessage("The WebDAV endpoint is missing required Adaptive operations: ${CAPABILITIES}.", {
CAPABILITIES: adaptiveSummary.required.missing.join(", "),
})}
</InfoNote>
{:else if adaptiveSummary.required.kind === "failed"}
<InfoNote error>
{translateMessage("The Adaptive safety check failed (${CATEGORY}; retry ${RETRY}).", {
CATEGORY: adaptiveSummary.required.category,
RETRY: adaptiveSummary.required.retry,
})}
</InfoNote>
{:else}
<InfoNote warning>{translateMessage("Required Adaptive operations were not checked.")}</InfoNote>
{/if}
{#if adaptiveSummary.byteRange.kind === "verified"}
<InfoNote notice>{translateMessage("Exact HTTP byte-range retrieval is supported.")}</InfoNote>
{:else if adaptiveSummary.byteRange.kind === "unsupported"}
<InfoNote warning>
{translateMessage("HTTP byte-range retrieval is not supported. Complete Pack retrieval remains available.")}
</InfoNote>
{:else if adaptiveSummary.byteRange.kind === "failed"}
<InfoNote warning>
{translateMessage("The Adaptive safety check failed (${CATEGORY}; retry ${RETRY}).", {
CATEGORY: adaptiveSummary.byteRange.category,
RETRY: adaptiveSummary.byteRange.retry,
})}
</InfoNote>
{:else}
<InfoNote warning>
{translateMessage(
"HTTP byte-range retrieval was not checked because the required safety check did not complete."
)}
</InfoNote>
{/if}
{/if}
{#if inspectionIsCurrent && inspection?.available && !isAdaptive}
<InfoNote notice>
{translateMessage("WebDAV access and the selected Journal format were verified.")}
</InfoNote>
{/if}
<InfoNote>
{translateMessage(
"The saved connection contains credentials and custom headers. Configuration encryption protects exported Setup data when it is enabled; do not share a plain connection string."
)}
</InfoNote>
<InfoNote error visible={error !== ""}>{error}</InfoNote>
{#if processing}
{translateMessage("Checking connection... Please wait.")}
{:else}
<UserDecisions>
{#if inspectionIsCurrent && inspection?.available && inspectedSettings}
<Decision
title={setupMode === "settings"
? translateMessage("Save verified settings")
: translateMessage("Continue with verified settings")}
important
commit={() => commitVerified()}
/>
<Decision
title={isAdaptive
? translateMessage("Run endpoint safety check")
: translateMessage("Test WebDAV connection")}
disabled={!isEndpointValid}
commit={() => checkConnection()}
/>
{:else}
<Decision
title={isAdaptive
? translateMessage("Run endpoint safety check")
: translateMessage("Test WebDAV connection")}
important
disabled={!isEndpointValid}
commit={() => checkConnection()}
/>
{/if}
{#if setupMode === "settings"}
<InfoNote warning>
{translateMessage(
"Saving without a successful connection test keeps this profile, but automatic synchronisation may fail until the connection is corrected."
)}
</InfoNote>
<Decision
title={translateMessage("Save without connecting")}
disabled={!isEndpointValid}
commit={() => commit()}
/>
{/if}
<Decision title={translateMessage("Cancel")} commit={() => setResult(TYPE_CANCELLED)} />
</UserDecisions>
{/if}
@@ -0,0 +1,25 @@
import { DEFAULT_SETTINGS, REMOTE_MINIO, type BucketSyncSetting } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { journalProtocolConfigurationForSettings } from "@vrtmrz/livesync-commonlib/journal-storage";
export function normaliseS3JournalSettings(settings: BucketSyncSetting): BucketSyncSetting {
const journalFormat = settings.journalFormat ?? "opaque-v1";
const candidate: BucketSyncSetting = {
...settings,
bucket: settings.bucket.trim(),
bucketPrefix: settings.bucketPrefix.trim(),
endpoint: settings.endpoint.trim(),
expectedRepositoryId: journalFormat === "adaptive-v1" ? (settings.expectedRepositoryId ?? "").trim() : "",
journalFormat,
packReadPolicy: journalFormat === "adaptive-v1" ? (settings.packReadPolicy ?? "whole-pack") : "whole-pack",
region: settings.region.trim(),
};
const protocol = journalProtocolConfigurationForSettings({
...DEFAULT_SETTINGS,
...candidate,
remoteType: REMOTE_MINIO,
});
return {
...candidate,
...protocol,
};
}
@@ -0,0 +1,79 @@
import { describe, expect, it } from "vitest";
import { DEFAULT_SETTINGS, type BucketSyncSetting } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { pickBucketSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { normaliseS3JournalSettings } from "./s3JournalSettings";
function settings(overrides: Partial<BucketSyncSetting> = {}): BucketSyncSetting {
return {
...pickBucketSyncSettings(DEFAULT_SETTINGS),
...overrides,
};
}
describe("normaliseS3JournalSettings", () => {
it("retains and validates Adaptive repository options", () => {
const repositoryId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
const result = normaliseS3JournalSettings(
settings({
bucket: " vault ",
bucketPrefix: " journals/ ",
endpoint: " https://storage.example ",
expectedRepositoryId: ` ${repositoryId} `,
journalFormat: "adaptive-v1",
packReadPolicy: "range",
region: " auto ",
})
);
expect(result).toMatchObject({
bucket: "vault",
bucketPrefix: "journals/",
endpoint: "https://storage.example",
expectedRepositoryId: repositoryId,
journalFormat: "adaptive-v1",
packReadPolicy: "range",
region: "auto",
});
});
it("uses conservative Opaque defaults for older profiles", () => {
const legacy = settings();
delete legacy.expectedRepositoryId;
delete legacy.journalFormat;
delete legacy.packReadPolicy;
expect(normaliseS3JournalSettings(legacy)).toMatchObject({
expectedRepositoryId: "",
journalFormat: "opaque-v1",
packReadPolicy: "whole-pack",
});
});
it("clears Adaptive-only options when Opaque Journal is selected", () => {
expect(
normaliseS3JournalSettings(
settings({
expectedRepositoryId: "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA",
journalFormat: "opaque-v1",
packReadPolicy: "range",
})
)
).toMatchObject({
expectedRepositoryId: "",
journalFormat: "opaque-v1",
packReadPolicy: "whole-pack",
});
});
it("rejects a non-canonical expected repository ID", () => {
expect(() =>
normaliseS3JournalSettings(
settings({
expectedRepositoryId: "not-a-repository-id",
journalFormat: "adaptive-v1",
})
)
).toThrow("expectedRepositoryId must be a canonical base64url-encoded 32-byte value");
});
});
@@ -4,7 +4,10 @@ import type {
EncryptionSettings,
ObsidianLiveSyncSettings,
P2PConnectionInfo,
WebDAVSyncSetting,
} from "@vrtmrz/livesync-commonlib/compat/common/models/setting.type";
import type { BuiltInRemoteConfiguration } from "@vrtmrz/livesync-commonlib/remote-configurations";
import type { RemoteSetupChoice } from "@/modules/features/SetupWizard/RemoteSetupRegistry";
export const TYPE_IDENTICAL = "identical";
export const TYPE_INDEPENDENT = "independent";
@@ -38,7 +41,7 @@ export const TYPE_CLOSE = "close";
// SetupRemote
export const TYPE_COUCHDB = "couchdb";
export const TYPE_BUCKET = "bucket";
export const TYPE_BUCKET = "s3";
export const TYPE_P2P = "p2p";
export type ResultTypeVault =
@@ -93,7 +96,8 @@ export type SelectMethodExistingResultType =
| typeof TYPE_CONFIGURE_MANUALLY
| typeof TYPE_CANCELLED;
export type SetupRemoteResultType = typeof TYPE_COUCHDB | typeof TYPE_BUCKET | typeof TYPE_P2P | typeof TYPE_CANCELLED;
export type SetupRemoteResultType = BuiltInRemoteConfiguration["type"] | typeof TYPE_CANCELLED;
export type SetupRemoteInitialData = RemoteSetupChoice<BuiltInRemoteConfiguration["type"]>[];
export type UseSetupURIResultType = typeof TYPE_CANCELLED | ObsidianLiveSyncSettings;
@@ -101,6 +105,13 @@ export type SetupRemoteE2EEResultType = typeof TYPE_CANCELLED | EncryptionSettin
export type SetupRemoteBucketResultType = typeof TYPE_CANCELLED | BucketSyncSetting;
export type SetupRemoteWebDAVResultType = typeof TYPE_CANCELLED | WebDAVSyncSetting;
export type WebDAVSetupMode = "onboarding" | "settings";
export type SetupRemoteWebDAVInitialData = {
settings: WebDAVSyncSetting;
mode: WebDAVSetupMode;
};
export type SetupRemoteCouchDBResultType = typeof TYPE_CANCELLED | CouchDBConnection;
export type CouchDBSetupMode = "create-or-connect" | "connect-existing" | "settings";
export type SetupRemoteCouchDBInitialData = {
@@ -0,0 +1,107 @@
import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
REMOTE_WEBDAV,
journalProtocolConfigurationForSettings,
parseWebDAVConnectionURI,
serialiseWebDAVConnectionURI,
type AdaptiveJournalPackReadPolicyV1,
type JournalFormatV1,
type JournalStorageAdaptiveCapabilityInspection,
type JournalStorageCapabilityInspection,
type WebDAVConnection,
type WebDAVSyncSetting,
} from "@vrtmrz/livesync-commonlib/journal-storage";
export type WebDAVJournalForm = WebDAVConnection & {
expectedRepositoryId: string;
journalFormat: JournalFormatV1;
packReadPolicy: AdaptiveJournalPackReadPolicyV1;
};
export type WebDAVCapabilitySummary =
| { kind: "verified" }
| { kind: "not-checked" }
| { kind: "unsupported"; missing: string[] }
| {
kind: "failed";
category: "authentication" | "invalid-response" | "permission" | "rate-limited" | "unavailable" | "unknown";
retry: "later" | "never" | "verify-first";
};
export type WebDAVAdaptiveCapabilitySummary = {
byteRange: WebDAVCapabilitySummary;
required: WebDAVCapabilitySummary;
};
const emptyWebDAVConnection: WebDAVConnection = {
customHeaders: "",
endpoint: "",
password: "",
prefix: "",
useCustomRequestHandler: false,
username: "",
};
function resolveProtocol(settings: WebDAVSyncSetting) {
return journalProtocolConfigurationForSettings({
...DEFAULT_SETTINGS,
remoteType: REMOTE_WEBDAV,
...settings,
});
}
export function webDAVJournalFormFromSettings(settings: WebDAVSyncSetting): WebDAVJournalForm {
const connection = settings.webDAVactiveConnectionURI.trim()
? parseWebDAVConnectionURI(settings.webDAVactiveConnectionURI.trim())
: emptyWebDAVConnection;
const protocol = resolveProtocol(settings);
return {
...connection,
...protocol,
};
}
export function webDAVSyncSettingsFromForm(form: WebDAVJournalForm): WebDAVSyncSetting {
const journalFormat = form.journalFormat;
const settings: WebDAVSyncSetting = {
webDAVactiveConnectionURI: serialiseWebDAVConnectionURI({
customHeaders: form.customHeaders.trim(),
endpoint: form.endpoint.trim(),
password: form.password,
prefix: form.prefix.trim(),
useCustomRequestHandler: form.useCustomRequestHandler,
username: form.username.trim(),
}),
expectedRepositoryId: journalFormat === "adaptive-v1" ? form.expectedRepositoryId.trim() : "",
journalFormat,
packReadPolicy: journalFormat === "adaptive-v1" ? form.packReadPolicy : "whole-pack",
};
resolveProtocol(settings);
return settings;
}
function summariseCapabilityInspection(inspection: JournalStorageCapabilityInspection): WebDAVCapabilitySummary {
switch (inspection.status) {
case "verified":
return { kind: "verified" };
case "not-checked":
return { kind: "not-checked" };
case "unsupported":
return { kind: "unsupported", missing: [...inspection.missing] };
case "failed":
return {
category: inspection.failure.category,
kind: "failed",
retry: inspection.failure.retry,
};
}
}
export function summariseAdaptiveCapabilityInspection(
inspection: JournalStorageAdaptiveCapabilityInspection
): WebDAVAdaptiveCapabilitySummary {
return {
byteRange: summariseCapabilityInspection(inspection.byteRange),
required: summariseCapabilityInspection(inspection.required),
};
}
@@ -0,0 +1,106 @@
import { describe, expect, it } from "vitest";
import { serialiseWebDAVConnectionURI } from "@vrtmrz/livesync-commonlib/journal-storage";
import {
summariseAdaptiveCapabilityInspection,
webDAVJournalFormFromSettings,
webDAVSyncSettingsFromForm,
} from "./webDAVJournalSettings.ts";
const repositoryId = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
describe("WebDAV Journal settings", () => {
it("round-trips connection fields separately from Adaptive protocol fields", () => {
const settings = {
webDAVactiveConnectionURI: serialiseWebDAVConnectionURI({
customHeaders: "X-Vault: notes",
endpoint: "https://dav.example/remote.php/dav/files/alice",
password: "p@ss word",
prefix: "vault/notes/",
useCustomRequestHandler: true,
username: "alice@example.com",
}),
expectedRepositoryId: repositoryId,
journalFormat: "adaptive-v1" as const,
packReadPolicy: "range" as const,
};
const form = webDAVJournalFormFromSettings(settings);
expect(form).toEqual({
customHeaders: "X-Vault: notes",
endpoint: "https://dav.example/remote.php/dav/files/alice",
expectedRepositoryId: repositoryId,
journalFormat: "adaptive-v1",
packReadPolicy: "range",
password: "p@ss word",
prefix: "vault/notes/",
useCustomRequestHandler: true,
username: "alice@example.com",
});
expect(webDAVSyncSettingsFromForm(form)).toEqual(settings);
});
it("normalises editable text and removes Adaptive-only options from Opaque settings", () => {
const settings = webDAVSyncSettingsFromForm({
customHeaders: " X-Vault: notes ",
endpoint: " http://localhost:8080/dav ",
expectedRepositoryId: ` ${repositoryId} `,
journalFormat: "opaque-v1",
packReadPolicy: "range",
password: " password with spaces ",
prefix: " vault/notes/ ",
useCustomRequestHandler: false,
username: " alice ",
});
expect(settings).toEqual({
webDAVactiveConnectionURI:
"sls+webdav://alice:%20password%20with%20spaces%20@localhost:8080/dav?insecure=true&prefix=vault%2Fnotes%2F&headers=X-Vault%3A+notes",
expectedRepositoryId: "",
journalFormat: "opaque-v1",
packReadPolicy: "whole-pack",
});
});
it("rejects an invalid pinned repository identity", () => {
expect(() =>
webDAVSyncSettingsFromForm({
customHeaders: "",
endpoint: "https://dav.example/vault",
expectedRepositoryId: "AA",
journalFormat: "adaptive-v1",
packReadPolicy: "whole-pack",
password: "secret",
prefix: "",
useCustomRequestHandler: false,
username: "alice",
})
).toThrow("expectedRepositoryId must be a canonical base64url-encoded 32-byte value");
});
it("presents required capabilities and optional byte-range support independently", () => {
expect(
summariseAdaptiveCapabilityInspection({
required: { status: "verified" },
byteRange: { missing: ["byte-range"], status: "unsupported" },
})
).toEqual({
required: { kind: "verified" },
byteRange: { kind: "unsupported", missing: ["byte-range"] },
});
expect(
summariseAdaptiveCapabilityInspection({
required: {
failure: { category: "authentication", retry: "never" },
status: "failed",
},
byteRange: { status: "not-checked" },
})
).toEqual({
required: { category: "authentication", kind: "failed", retry: "never" },
byteRange: { kind: "not-checked" },
});
});
});
+6 -9
View File
@@ -147,14 +147,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
: req instanceof Request && typeof req.method === "string"
? req.method
: "GET";
if (typeof req !== "string") {
if (opts?.body) {
body = typeof opts.body === "string" ? opts.body : await new Response(opts.body).arrayBuffer();
} else if (req.body) {
body = await new Response(req.body).arrayBuffer();
}
} else {
body = opts?.body as string;
const suppliedBody = opts?.body ?? (req instanceof Request ? req.body : undefined);
if (suppliedBody !== undefined && suppliedBody !== null) {
body = typeof suppliedBody === "string" ? suppliedBody : await new Response(suppliedBody).arrayBuffer();
}
const reqHeaders = new Headers(req instanceof Request ? req.headers : {});
@@ -192,7 +187,9 @@ export class ObsidianAPIService extends InjectableAPIService<ObsidianServiceCont
contentType: contentType,
};
const r = await requestUrl({ ...requestParam, throw: false });
return new Response(r.arrayBuffer, {
const responseHasNoBody =
method.toUpperCase() === "HEAD" || r.status === 204 || r.status === 205 || r.status === 304;
return new Response(responseHasNoBody ? null : r.arrayBuffer, {
headers: r.headers,
status: r.status,
statusText: `${r.status}`,
@@ -4,16 +4,17 @@ const mocks = vi.hoisted(() => ({
platform: {
isMobile: false,
},
requestUrl: vi.fn(),
}));
vi.mock("@/deps.ts", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
requestUrl: mocks.requestUrl,
}));
vi.mock("@/deps", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
requestUrl: mocks.requestUrl,
}));
vi.mock("@/modules/essentialObsidian/APILib/ObsHttpHandler", () => ({
@@ -65,3 +66,41 @@ describe("ObsidianAPIService.showWindowOnRight", () => {
expect(workspace.revealLeaf).toHaveBeenCalledWith(rightLeaf);
});
});
describe("ObsidianAPIService.nativeFetch", () => {
it("normalises a typed-array body when the request URL is a string", async () => {
mocks.requestUrl.mockResolvedValue({
arrayBuffer: new Uint8Array([9, 8, 7]).buffer,
headers: { etag: '"created"' },
status: 201,
});
const source = new Uint8Array([0, 1, 2, 3, 4]);
const body = source.subarray(1, 4);
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
body: body as unknown as BodyInit,
headers: { "Content-Type": "application/octet-stream" },
method: "PUT",
});
expect(response.status).toBe(201);
const request = mocks.requestUrl.mock.calls[0][0] as { body?: unknown };
expect(request.body).toBeInstanceOf(ArrayBuffer);
expect([...new Uint8Array(request.body as ArrayBuffer)]).toEqual([1, 2, 3]);
});
it("constructs a bodyless response for a successful DELETE", async () => {
mocks.requestUrl.mockResolvedValue({
arrayBuffer: new ArrayBuffer(0),
headers: {},
status: 204,
});
const response = await createService({}).nativeFetch("http://127.0.0.1:8088/dav/probe.bin", {
method: "DELETE",
});
expect(response.status).toBe(204);
expect(await response.text()).toBe("");
});
});
+7
View File
@@ -12,6 +12,13 @@ Earlier releases remain available in the 0.25 release history and the legacy rel
## Unreleased
### Synchronisation and storage
#### Improved
- Object Storage setup can select the experimental Adaptive Journal format and choose complete Pack or verified Range retrieval. Existing Opaque Journal repositories remain the default and require an explicit remote Rebuild before changing formats.
- Saved connections and Setup now support experimental WebDAV Journal profiles. Adaptive WebDAV runs an endpoint safety check, reports required immutable-object behaviour separately from optional Range support, and permits an unverified save only from Settings.
### P2P and experimental browser applications
#### Improved