Compare commits

..

1 Commits

Author SHA1 Message Date
github-actions[bot] b281fe9fe4 Releasing 1.0.0-beta.3 2026-07-24 11:07:41 +00:00
159 changed files with 3491 additions and 11951 deletions
+1 -40
View File
@@ -7,18 +7,10 @@ on:
- main
paths:
- 'aggregator.html'
- 'src/**'
- 'test/browser-apps/**'
- 'package.json'
- 'package-lock.json'
- '.github/workflows/deploy-pages.yml'
pull_request:
paths:
- 'aggregator.html'
- 'src/**'
- 'test/browser-apps/**'
- 'package.json'
- 'package-lock.json'
- '.github/workflows/deploy-pages.yml'
permissions:
@@ -38,29 +30,6 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: '24.x'
cache: 'npm'
- name: Setup Deno
uses: denoland/setup-deno@v2
with:
deno-version: v2.9.2
- name: Install dependencies
run: npm ci
- name: Install Chromium
run: npx playwright@1.62.0 install --with-deps chromium
- name: Validate browser applications
run: npm run test:browser-apps
- name: Validate browser application interoperability
run: npm run test:e2e:browser-apps:interop
- name: Validate aggregator
run: |
test -s aggregator.html
@@ -72,18 +41,10 @@ jobs:
- name: Prepare Pages site
run: |
mkdir -p _site/webapp _site/webpeer
mkdir -p _site
cp aggregator.html _site/aggregator.html
cp -R src/apps/webapp/dist/. _site/webapp/
cp -R src/apps/webpeer/dist/. _site/webpeer/
test -s _site/webapp/index.html
test -s _site/webapp/webapp.html
test -s _site/webpeer/index.html
touch _site/.nojekyll
- name: Validate packaged Pages site
run: npm run test:browser-apps:pages
- name: Configure GitHub Pages
uses: actions/configure-pages@v5
+3 -21
View File
@@ -62,7 +62,6 @@ jobs:
VERSION: ${{ inputs.version }}
EXPECTED_HEAD_SHA: ${{ inputs.expected_head_sha }}
PRERELEASE: ${{ inputs.prerelease }}
PUBLISH_CLI: ${{ inputs.publish_cli }}
run: |
set -euo pipefail
ACTUAL_HEAD_SHA="$(git rev-parse HEAD)"
@@ -74,10 +73,6 @@ jobs:
echo "Version ${VERSION} is a pre-release version, but prerelease was not enabled." >&2
exit 1
fi
if [[ "${VERSION}" != *-* && "${PRERELEASE}" == "true" && "${PUBLISH_CLI}" == "true" ]]; then
echo "A stable version staged as a pre-release must use publish_cli=false so that the CLI latest and major-minor image tags do not advance before BRAT validation." >&2
exit 1
fi
node utils/release-notes.mjs validate "${VERSION}"
- name: Ensure and push release tags
@@ -101,15 +96,8 @@ jobs:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ inputs.version }}
PRERELEASE: ${{ inputs.prerelease }}
PUBLISH_CLI: ${{ inputs.publish_cli }}
run: |
set -euo pipefail
if [[ "${PUBLISH_CLI}" == "true" ]]; then
gh workflow run cli-docker.yml \
--ref "${VERSION}-cli" \
--field dry_run=false \
--field force=false
fi
gh workflow run release.yml \
--ref "${VERSION}" \
--field tag="${VERSION}" \
@@ -125,21 +113,15 @@ jobs:
{
echo "Ensured the plug-in tag \`${VERSION}\` points to the reviewed release commit."
if [[ "${PUBLISH_CLI}" == "true" ]]; then
echo "The CLI tag \`${VERSION}-cli\` was also created, and finalisation explicitly dispatched the CLI container workflow."
echo "The CLI tag \`${VERSION}-cli\` was also created; its tag event starts the container workflow."
else
echo "CLI publication was omitted."
fi
echo ""
echo "Dispatched the plug-in release workflow for \`${VERSION}\`. After approval for the release environment, it creates a draft GitHub Release."
echo ""
if [[ "${VERSION}" == *-* ]]; then
echo "Publish the draft as a pre-release without replacing the latest stable release."
echo "Keep the release pull request in draft and unmerged after BRAT validation; close it only through a separate maintainer action."
elif [[ "${PRERELEASE}" == "true" ]]; then
echo "Publish the draft initially as a pre-release without replacing the latest stable release."
echo "After BRAT validation, merge the release pull request into its reviewed base branch and integrate the exact release commit into the default branch."
echo "Only after the default branch contains the exact release metadata, remove the pre-release designation and make this exact release the latest stable release."
echo "Create the stable CLI tag and publish its latest and major-minor image tags through a separate maintainer gate."
if [[ "${PRERELEASE}" == "true" ]]; then
echo "Publish the draft as a pre-release, keep the release pull request in draft, and merge only after BRAT validation succeeds."
else
echo "Publish the draft as the latest stable release, keep the release pull request in draft, and merge only after BRAT validation succeeds."
fi
+3 -4
View File
@@ -1,12 +1,11 @@
import { writeFileSync } from "fs";
import { allMessages } from "../src/common/messages/combinedMessages.dev.ts";
const { writeFileSync } = process.getBuiltinModule("node:fs");
const path = process.getBuiltinModule("node:path");
import path from "path";
const __dirname = import.meta.dirname;
const currentPath = __dirname;
const outDir = path.resolve(currentPath, "../src/common/messages/combinedMessages.prod.ts");
process.stdout.write(`Writing to ${outDir}\n`);
console.log(`Writing to ${outDir}`);
writeFileSync(
outDir,
`export const allMessages: Readonly<Record<string, Readonly<Record<string, string>>>> = ${JSON.stringify(allMessages, null, 4)};`
+8 -13
View File
@@ -1,14 +1,14 @@
import { readFile } from "fs/promises";
import { join, resolve } from "path";
import { glob } from "tinyglobby";
import { parse } from "yaml";
import { objectToDotted } from "./messagelib.ts";
const fsPromises = process.getBuiltinModule("node:fs/promises");
const path = process.getBuiltinModule("node:path");
const __dirname = import.meta.dirname;
const targetDir = path.resolve(path.join(__dirname, "../src/common/messagesYAML/"));
const targetDir = resolve(join(__dirname, "../src/common/messagesYAML/"));
const files = (await glob(`*.yaml`, { expandDirectories: false, absolute: true, cwd: targetDir })).sort();
function flattenMessages(src: unknown) {
function flattenMessages(src: Record<string, unknown>) {
return Object.fromEntries(
Object.entries(objectToDotted(src))
.map(([key, value]) => [key.endsWith("._value") ? key.slice(0, -7) : key, value] as const)
@@ -20,14 +20,9 @@ function flattenMessages(src: unknown) {
const localeData = new Map<string, Record<string, string>>();
for (const file of files) {
const segments = file.split(/[/\\]/);
const localeFilename = segments[segments.length - 1];
if (localeFilename === undefined) {
throw new Error(`Could not determine the locale name for ${file}`);
}
const locale = localeFilename.replace(/\.yaml$/, "");
const content = await fsPromises.readFile(file, "utf-8");
const parsed: unknown = parse(content);
localeData.set(locale, flattenMessages(parsed ?? {}));
const locale = segments[segments.length - 1]!.replace(/\.yaml$/, "");
const content = await readFile(file, "utf-8");
localeData.set(locale, flattenMessages(parse(content) ?? {}));
}
const baseLocale = "en";
@@ -60,4 +55,4 @@ const report = Object.fromEntries(
})
);
process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
console.log(JSON.stringify(report, null, 2));
+3 -2
View File
@@ -1,8 +1,9 @@
import { writeFileSync } from "fs";
import { SUPPORTED_I18N_LANGS, type I18N_LANGS } from "../src/common/rosetta";
import { allMessages } from "../src/common/messages/combinedMessages.dev.ts";
const { writeFileSync } = process.getBuiltinModule("node:fs");
const path = process.getBuiltinModule("node:path");
import path from "path";
const thisFileDir = __dirname;
const outDir = path.join(thisFileDir, "i18n");
+14 -5
View File
@@ -1,17 +1,26 @@
import { writeFileSync } from "fs";
import { allMessages } from "../src/common/messages/combinedMessages.prod.ts";
const __dirname = import.meta.dirname;
const { writeFileSync } = process.getBuiltinModule("node:fs");
const path = process.getBuiltinModule("node:path");
import path from "path";
const thisFileDir = __dirname;
const outDir = path.resolve(thisFileDir, "../src/common/messagesJson");
const out = {} as Record<string, { [key: string]: string | undefined }>;
for (const [key, value] of Object.entries(allMessages)) {
for (const [lang, langValue] of Object.entries(value)) {
//@ts-ignore
for (const [lang, langValue] of Object.entries(allMessages[key])) {
if (!out[lang]) out[lang] = {};
out[lang][key] = langValue;
if (lang in value) {
out[lang][key] = langValue as string;
} else {
if (lang === "def") {
out[lang][key] = key;
} else {
out[lang][key] = undefined;
}
}
}
}
+13 -12
View File
@@ -1,6 +1,6 @@
const fsPromises = process.getBuiltinModule("node:fs/promises");
const path = process.getBuiltinModule("node:path");
const url = process.getBuiltinModule("node:url");
import { access, readFile } from "node:fs/promises";
import { dirname, relative, resolve } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
type InspectionError = {
check: "current-label" | "local-reference" | "retired-label";
@@ -20,7 +20,7 @@ const messageCataloguePath = "src/common/messagesJson/en.json";
const markdownLinkPattern = /!?\[[^\]]*\]\(([^)\s]+)(?:\s+["'][^)]*["'])?\)/gu;
function repositoryRootFromThisFile(): string {
return path.resolve(path.dirname(url.fileURLToPath(import.meta.url)), "..");
return resolve(dirname(fileURLToPath(import.meta.url)), "..");
}
function normaliseReferenceTarget(rawTarget: string): string {
@@ -48,14 +48,14 @@ async function inspectLocalReferences(
const [pathPart] = target.split("#", 1);
if (!pathPart) continue;
checked++;
const referencedPath = path.resolve(repositoryRoot, path.dirname(documentPath), pathPart);
const referencedPath = resolve(repositoryRoot, dirname(documentPath), pathPart);
try {
await fsPromises.access(referencedPath);
await access(referencedPath);
} catch {
errors.push({
check: "local-reference",
file: documentPath,
detail: `Missing local reference: ${path.relative(repositoryRoot, referencedPath)}`,
detail: `Missing local reference: ${relative(repositoryRoot, referencedPath)}`,
});
}
}
@@ -68,13 +68,14 @@ export async function inspectTroubleshootingDocs(
const errors: InspectionError[] = [];
const documents = new Map<string, string>();
for (const guidePath of guidePaths) {
documents.set(guidePath, await fsPromises.readFile(path.resolve(repositoryRoot, guidePath), "utf8"));
documents.set(guidePath, await readFile(resolve(repositoryRoot, guidePath), "utf8"));
}
const troubleshooting = documents.get("docs/troubleshooting.md")!;
const catalogue = JSON.parse(
await fsPromises.readFile(path.resolve(repositoryRoot, messageCataloguePath), "utf8")
) as Record<string, string>;
const catalogue = JSON.parse(await readFile(resolve(repositoryRoot, messageCataloguePath), "utf8")) as Record<
string,
string
>;
const requiredMessageKeys = [
"TweakMismatchResolve.Action.UseConfigured",
"TweakMismatchResolve.Action.UseMine",
@@ -131,7 +132,7 @@ async function runCli(): Promise<void> {
if (!result.ok) process.exitCode = 1;
}
const invokedPath = process.argv[1] ? url.pathToFileURL(path.resolve(process.argv[1])).href : undefined;
const invokedPath = process.argv[1] ? pathToFileURL(resolve(process.argv[1])).href : undefined;
if (invokedPath === import.meta.url) {
await runCli();
}
+10 -14
View File
@@ -1,31 +1,27 @@
// Convert Application convenient Message Resources (JSON) to Human-Editable format (YAML)
import { readFile, writeFile } from "fs/promises";
import { join, resolve } from "path";
import { stringify } from "yaml";
import { glob } from "tinyglobby";
import { dottedToObject } from "./messagelib";
const fsPromises = process.getBuiltinModule("node:fs/promises");
const path = process.getBuiltinModule("node:path");
const __dirname = import.meta.dirname;
const targetDir = path.resolve(path.join(__dirname, "../src/common/messagesJson/"));
process.stdout.write(`Target directory: ${targetDir}\n`);
const targetDir = resolve(join(__dirname, "../src/common/messagesJson/"));
console.log(`Target directory: ${targetDir}`);
const files = await glob(`*.json`, { expandDirectories: false, absolute: true, cwd: targetDir });
for (const file of files) {
const filePath = path.resolve(file);
process.stdout.write(`Processing file: ${filePath}\n`);
const content = await fsPromises.readFile(filePath, "utf-8");
const jsonDataSrc: unknown = JSON.parse(content);
if (typeof jsonDataSrc !== "object" || jsonDataSrc === null || Array.isArray(jsonDataSrc)) {
throw new TypeError(`Expected ${filePath} to contain a JSON object`);
}
const filePath = resolve(file);
console.log(`Processing file: ${filePath}`);
const content = await readFile(filePath, "utf-8");
const jsonDataSrc = JSON.parse(content);
const jsonDataD2 = Object.fromEntries(
Object.entries(jsonDataSrc).sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
);
const jsonData = dottedToObject(jsonDataD2);
const yamlData = stringify(jsonData, { indent: 2 });
const yamlFilePath = filePath.replace(/\.json$/, ".yaml").replace("Json", "YAML");
await fsPromises.writeFile(yamlFilePath, yamlData, "utf-8");
process.stdout.write(`Converted ${filePath} to ${yamlFilePath}\n`);
await writeFile(yamlFilePath, yamlData, "utf-8");
console.log(`Converted ${filePath} to ${yamlFilePath}`);
}
// console.dir(files, { depth: 0 });
+36 -47
View File
@@ -1,49 +1,38 @@
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
export function objectToDotted(obj: unknown, prefix = ""): Record<string, unknown> {
if (!isRecord(obj)) {
throw new TypeError("Expected a message catalogue object");
}
const flattened: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
const newKey = prefix ? `${prefix}.${key}` : key;
if (isRecord(value)) {
Object.assign(flattened, objectToDotted(value, newKey));
} else {
flattened[newKey] = value;
}
}
return flattened;
}
export function dottedToObject(obj: unknown): Record<string, unknown> {
if (!isRecord(obj)) {
throw new TypeError("Expected a dotted message catalogue object");
}
const nestedResult: Record<string, unknown> = {};
for (const [key, value] of Object.entries(obj)) {
if (key.includes(" ")) {
nestedResult[key] = value;
continue;
}
const keys = key.split(".");
let nested = nestedResult;
for (const [index, currentKey] of keys.entries()) {
if (index === keys.length - 1) {
nested[currentKey] = value;
continue;
export function objectToDotted(obj: any, prefix = ""): Record<string, any> {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
const newKey = prefix ? `${prefix}.${key}` : key;
if (typeof value === "object" && value !== null && !Array.isArray(value)) {
Object.assign(acc, objectToDotted(value, newKey));
} else {
acc[newKey] = value;
}
const currentValue = nested[currentKey];
if (isRecord(currentValue)) {
nested = currentValue;
continue;
}
const replacement = currentValue === undefined || currentValue === null ? {} : { _value: currentValue };
nested[currentKey] = replacement;
nested = replacement;
}
}
return nestedResult;
return acc;
},
{} as Record<string, any>
);
}
export function dottedToObject(obj: Record<string, any>): Record<string, any> {
return Object.entries(obj).reduce(
(acc, [key, value]) => {
if (key.includes(" ")) {
// Return as is.
return { ...acc, [key]: value }; // Skip keys with spaces
}
const keys = key.split(".");
keys.reduce((nestedAcc, currKey, index) => {
if (currKey in nestedAcc && typeof nestedAcc[currKey] !== "object") {
nestedAcc[currKey] = { _value: nestedAcc[currKey] }; // Convert to object if not already
}
if (index === keys.length - 1) {
nestedAcc[currKey] = value;
} else {
nestedAcc[currKey] = nestedAcc[currKey] || {};
}
return nestedAcc[currKey];
}, acc);
return acc;
},
{} as Record<string, any>
);
}
-41
View File
@@ -1,41 +0,0 @@
import { describe, expect, it } from "vitest";
import { dottedToObject, objectToDotted } from "./messagelib";
describe("message catalogue conversion", () => {
it("flattens nested objects while preserving leaf values", () => {
expect(
objectToDotted({
dialogue: {
title: "Title",
options: ["first", "second"],
},
"literal key": "Literal",
})
).toEqual({
"dialogue.title": "Title",
"dialogue.options": ["first", "second"],
"literal key": "Literal",
});
});
it("preserves an existing leaf under _value when a dotted child follows it", () => {
expect(
dottedToObject({
section: "Base value",
"section.child": "Child value",
"literal key": "Literal",
})
).toEqual({
section: {
_value: "Base value",
child: "Child value",
},
"literal key": "Literal",
});
});
it("rejects non-object catalogue roots", () => {
expect(() => objectToDotted("not an object")).toThrow("Expected a message catalogue object");
expect(() => dottedToObject(["not", "an", "object"])).toThrow("Expected a dotted message catalogue object");
});
});
+10 -11
View File
@@ -1,30 +1,29 @@
// Convert Human-Editable format (YAML) to Application convenient Message Resources (JSON)
import { readFile, writeFile } from "fs/promises";
import { join, resolve } from "path";
import { parse } from "yaml";
import { glob } from "tinyglobby";
import { objectToDotted } from "./messagelib";
const fsPromises = process.getBuiltinModule("node:fs/promises");
const path = process.getBuiltinModule("node:path");
const __dirname = import.meta.dirname;
const targetDir = path.resolve(path.join(__dirname, "../src/common/messagesYAML/"));
process.stdout.write(`Target directory: ${targetDir}\n`);
const targetDir = resolve(join(__dirname, "../src/common/messagesYAML/"));
console.log(`Target directory: ${targetDir}`);
const files = await glob(`*.yaml`, { expandDirectories: false, absolute: true, cwd: targetDir });
for (const file of files) {
const filePath = path.resolve(file);
const content = await fsPromises.readFile(filePath, "utf-8");
const jsonDataSrc: unknown = parse(content);
const filePath = resolve(file);
const content = await readFile(filePath, "utf-8");
const jsonDataSrc = parse(content);
const jsonDataD2 = objectToDotted(jsonDataSrc);
const jsonData = Object.fromEntries(
Object.entries(jsonDataD2)
.map(([key, value]): [string, unknown] => [key.endsWith("._value") ? key.slice(0, -7) : key, value])
.map(([key, value]) => [key.endsWith("._value") ? key.slice(0, -7) : key, value])
.sort(([keyA], [keyB]) => keyA.localeCompare(keyB))
);
const yamlData = JSON.stringify(jsonData, null, 4) + "\n";
const yamlFilePath = filePath.replace(/\.yaml$/, ".json").replace("YAML", "Json");
await fsPromises.writeFile(yamlFilePath, yamlData, "utf-8");
process.stdout.write(`Converted ${filePath} to ${yamlFilePath}\n`);
await writeFile(yamlFilePath, yamlData, "utf-8");
console.log(`Converted ${filePath} to ${yamlFilePath}`);
}
// console.dir(files, { depth: 0 });
+17 -27
View File
@@ -38,8 +38,6 @@ npm run buildDev # Development build (one-time)
npm run test:integration # Run CouchDB-backed integration tests
npm run test:setup-tools # Check provisioning and Setup URI package contracts
npm run test:e2e:cli:p2p # Run canonical P2P validation in Compose
npm run test:browser-apps # Build and run the WebApp and WebPeer app-owned Chromium tests
npm run test:e2e:browser-apps:interop # Run WebApp → WebPeer → CLI in Compose
npm run test:e2e:obsidian:local-suite # Run the real Obsidian local suite
```
@@ -68,7 +66,7 @@ To facilitate development and testing, the build process can automatically copy
- If you add a feature that interacts with the remote database (e.g., replication changes, custom changes feed parameters, or custom HTTP queries), you are strongly expected to write an integration test to verify the behaviour against a real CouchDB server.
- **Commonlib Tests**: Commonlib owns unit and package tests for shared RPC, storage, replication, and platform contracts. LiveSync CI verifies the exact packed dependency as a downstream consumer.
Regression tests remain in the suite owned by the implementation under test. Plug-in tests may be co-located with their source, while independent application tests remain under `test/apps/` or `test/browser-apps/` so that they stay outside the Community Review source boundary. Prefix a case or group with `compatibility:` when it protects a persisted input or state which current releases still accept, and with `retirement guard:` when it prevents a removed setting, control, or notification from returning. Remove or replace a compatibility case only when the corresponding input is no longer accepted or an equivalent maintained case preserves the contract. Remove a retirement guard only when another current contract makes the old behaviour unreachable. Do not preserve a disconnected historical test as an executable specification when no maintained runner invokes it; Git history is the reference for retired test infrastructure.
Regression tests remain beside the implementation which owns their contract. Prefix a case or group with `compatibility:` when it protects a persisted input or state which current releases still accept, and with `retirement guard:` when it prevents a removed setting, control, or notification from returning. Remove or replace a compatibility case only when the corresponding input is no longer accepted or an equivalent maintained case preserves the contract. Remove a retirement guard only when another current contract makes the old behaviour unreachable. Do not preserve a disconnected historical test as an executable specification when no maintained runner invokes it; Git history is the reference for retired test infrastructure.
- **CLI E2E** (`src/apps/cli/testdeno/`): Host-independent consumer workflows. The canonical Compose P2P suite covers ordinary two-peer synchronisation, replacement of the current replicator followed by transfer with the same peer, and explicit relay disconnection followed by paused and resumed reconnection. Its lifecycle entry point is included only in the Docker test build and does not add a public CLI command. Run `npm run test:e2e:cli` for the ordinary suite or `npm run test:e2e:cli:p2p` for P2P validation.
- **Self-hosted setup tools** (`utils/couchdb/`, `utils/setup/`, and `utils/flyio/`): Deno contract tests consume the exact locked Commonlib registry package, verify current CouchDB, Object Storage, and random-room P2P Setup URI defaults and remote profiles, and keep CouchDB administration separate from package-owned LiveSync database-version negotiation. `unit-ci` also provisions a real temporary CouchDB database and verifies its version document against the installed Commonlib package. Run `npm run test:setup-tools` for the local contract gate.
@@ -87,11 +85,9 @@ Regression tests remain in the suite owned by the implementation under test. Plu
- **Test Structure**:
- `test/e2e-obsidian/` - Real Obsidian E2E scripts for local verification
- `test/apps/webapp/` and `test/apps/webpeer/` - app-owned unit tests outside the Community Review source boundary
- `test/browser-apps/webapp/` and `test/browser-apps/webpeer/` - app-owned Deno and Chromium tests outside the Community Review source boundary
- `test/browser-apps/` - Compose-owned WebApp → WebPeer → CLI P2P interoperability and shared browser-test support
- co-located `*.unit.spec.ts` files - Node-based unit tests
- co-located `*.integration.spec.ts` files - service-backed integration tests
- `src/apps/webapp/obsidianMock.ts` - Webapp-only Obsidian compatibility adapter; it is not an E2E Harness
### Import Path Normalisation
@@ -148,7 +144,7 @@ Hence, the new feature should be implemented as follows:
- **LiveSyncLocalDB** (`@vrtmrz/livesync-commonlib/compat/pouchdb/LiveSyncLocalDB`): Local PouchDB database wrapper
- **Replicators** (`@vrtmrz/livesync-commonlib/compat/replication/*`): CouchDB, Journal, and P2P synchronisation engines
- **Service Hub** (`src/modules/services/`): Central service registry using dependency injection
- **Common Library** (`@vrtmrz/livesync-commonlib`): Platform-independent synchronisation logic, shared with the CLI, WebApp, WebPeer, and external tools
- **Common Library** (`@vrtmrz/livesync-commonlib`): Platform-independent synchronisation logic, shared with the CLI, Webapp, WebPeer, and external tools
Commonlib owns the P2P replicator and Trystero transport lifecycle. Host commands, event handlers, and views must retain the Commonlib service-feature result and resolve its current `replicator` at the point of use. They must not snapshot an instance which can be replaced when settings or the local database change, close Trystero-owned raw peers, or install another Trystero transport generation at the application root.
@@ -248,10 +244,7 @@ export class ModuleExample extends AbstractObsidianModule {
- Use SemVer beta identifiers such as `1.0.0-beta.0` for immutable integration previews. Increment the beta number when a published preview needs a correction. Reserve `1.0.0-rc.0` for the first feature- and contract-frozen release candidate. Historical `-patchedN` releases remain unchanged in the release history.
- Publish a pre-release from an immutable reviewed tag, mark its GitHub Release as a pre-release, and do not replace the latest stable release.
- A plug-in review release may omit the CLI image when the CLI artefact is not part of the required validation. When a pre-release CLI image is published, it receives immutable version and SHA-qualified tags only; it must not advance `latest` or a stable major-minor tag.
- Keep a hyphenated pre-release's release pull request in draft and unmerged after BRAT validation. Reconcile the published version's metadata into its base branch through a reviewed metadata-only commit, then close the release pull request only through a separate maintainer action.
- Stage a stable version for BRAT by publishing its exact `x.y.z` tag initially as a GitHub pre-release with `prerelease=true` and `publish_cli=false`. The stable manifest version would otherwise make the CLI workflow advance `latest` and the major-minor image tag before validation.
- After a staged stable version passes BRAT validation, merge its exact release commit into the reviewed base branch and integrate it through the reviewed branch chain into the repository's default branch. Promote the GitHub Release only after the default branch contains the exact stable metadata; publish stable CLI tags through a separate maintainer gate.
- If validation fails, leave every published tag unchanged and prepare the next pre-release or patch version.
- Keep the release pull request in draft until the exact published plug-in has passed BRAT validation. If validation fails, prepare the next pre-release version rather than moving the existing tag.
## Release Notes
@@ -269,17 +262,15 @@ The `Finalise Release Tags` and `Release Obsidian Plugin` workflows use the `rel
- Run the `Prepare Release PR` workflow with the target version and selected base branch. It creates the release branch, updates versions, confirms that Commonlib is locked to an immutable package version, moves the `## Unreleased` notes to the target version, commits the release preparation, pushes the branch, and opens a draft release PR. The base branch may already select the target development version; the workflow still runs the version lifecycle so that release-only metadata such as `versions.json` is recorded in the release commit.
- Do not tag the release branch when the PR is first created. Polish the release PR first, especially `updates.md`.
- Once the release PR head is fixed, run the `Finalise Release Tags` workflow with its full head commit SHA. It validates the release branch, ensures that the plug-in tag points to that commit, optionally creates the corresponding CLI tag, and explicitly dispatches the selected plug-in and CLI release workflows. The finalisation workflow can be retried when existing tags already point to the reviewed commit, but stops if a selected tag points elsewhere.
- The plug-in publishing workflow is intentionally dispatch-only. Pushing a plug-in tag directly does not publish a GitHub Release; use `Finalise Release Tags`, or dispatch `Release Obsidian Plugin` explicitly for recovery or a pre-release. When CLI publication is selected, finalisation dispatches the CLI Docker workflow against the reviewed CLI tag instead of relying on a tag created by `GITHUB_TOKEN` to start another workflow.
- For a hyphenated pre-release, run finalisation with `prerelease=true`; CLI publication remains optional. For a stable version awaiting BRAT validation, use `prerelease=true` and `publish_cli=false`.
- Approve the `Release Obsidian Plugin` workflow for the `release` environment, then inspect the generated draft GitHub Release. When a hyphenated pre-release includes the CLI, confirm that it received only its immutable version and SHA-qualified image tags.
- Publish the draft as a GitHub pre-release without replacing the latest stable release. Keep its release pull request in draft and leave its base branch unchanged throughout BRAT validation. Record that state in the pull request.
- Once the release PR head is fixed, run the `Finalise Release Tags` workflow with its full head commit SHA. It validates the release branch, ensures that the plug-in tag points to that commit, optionally creates the corresponding CLI tag, and dispatches the plug-in release workflow. A CLI tag starts its own container workflow. The finalisation workflow can be retried when existing tags already point to the reviewed commit, but stops if a selected tag points elsewhere.
- The plug-in publishing workflow is intentionally dispatch-only. Pushing a plug-in tag directly does not publish a GitHub Release; use `Finalise Release Tags`, or dispatch `Release Obsidian Plugin` explicitly for recovery or a pre-release. The CLI Docker workflow retains its documented branch, tag, and manual triggers.
- Approve the `Release Obsidian Plugin` workflow for the `release` environment, then inspect the generated draft GitHub Release. For a selected CLI publication, confirm the image tags appropriate to a stable or pre-release version.
- Publish a stable draft as the latest release, or publish a pre-release draft without replacing the latest stable release. In either case, keep the release PR in draft and leave its base branch unchanged until BRAT validation succeeds. Record that state in the PR.
- Validate the published release through BRAT. Confirm start-up, ordinary bidirectional synchronisation, and any regression scenario relevant to the release.
- After a hyphenated pre-release passes, keep its release pull request unmerged. Add a reviewed metadata-only commit to the selected base branch which records the published version in `versions.json` and moves its exact tagged release notes out of `## Unreleased`, then close the release pull request only through a separate maintainer action.
- After a stable version passes, mark its release pull request ready and merge the exact release commit into the selected base branch with a merge commit. Integrate that exact commit through the reviewed branch chain into the repository's default branch. Only after the default branch contains the matching stable metadata, remove the GitHub pre-release designation and make that exact release the latest stable release. Create the stable CLI tag and publish its `latest` and major-minor image tags, if selected, through a separate maintainer gate.
- After BRAT validation succeeds, mark the release PR ready and merge it into the selected base branch with a merge commit. This keeps the tagged release commit in that branch's history.
- If BRAT validation fails, keep the release PR in draft and do not move published tags. Before preparing the next version, add a reviewed metadata-only commit to the selected base branch which records the published version in `versions.json` and moves its exact tagged release notes out of `## Unreleased`. Keep only changes made after that tag under `## Unreleased`. Compare the historical section with `git show <tag>:updates.md`; do not merge the failed release PR or describe it as validated. The next release PR can then rotate only the correction notes while preserving the immutable release history.
- Prepare and publish the next patch or pre-release version from that reconciled base. Leave the failed release PR draft until it is deliberately closed as superseded under a separate maintainer action.
- A hyphenated version is rejected unless `prerelease=true`. A stable version staged with `prerelease=true` is rejected unless `publish_cli=false`.
- For a pre-release, set `prerelease=true` in `Finalise Release Tags`. A hyphenated version is rejected unless that input is enabled.
### Release Cheat Sheet
@@ -299,16 +290,15 @@ The `Finalise Release Tags` and `Release Obsidian Plugin` workflows use the `rel
- `version`: the same target version.
- `release_branch`: leave blank unless the release branch used a custom name.
- `expected_head_sha`: the full head commit SHA reviewed in the release PR.
- `prerelease`: enable for a version such as `1.0.0-rc.0`, and also when staging a stable version for BRAT.
- `publish_cli`: optional for a hyphenated pre-release, but disable it when staging a stable version.
- `prerelease`: enable for a version such as `1.0.0-rc.0`.
- `publish_cli`: disable when the reviewed release is plug-in-only.
5. Approve the `Release Obsidian Plugin` workflow for the `release` environment, then check the generated draft GitHub Release.
6. If a hyphenated pre-release includes the CLI, confirm that the explicitly dispatched CLI workflow published only immutable version and SHA-qualified image tags.
7. Publish the draft as a GitHub pre-release without replacing the latest stable release, but keep the release PR in draft and leave its base branch unchanged.
8. Update the PR state message to describe the published pre-release and state that merging remains on hold.
6. If CLI publication was selected, confirm that the CLI tag event published the expected image tags.
7. Publish the draft as a stable release or pre-release as selected, but keep the release PR in draft and leave its base branch unchanged.
8. Update the PR state message to describe the published release and state that merging remains on hold until BRAT validation is complete.
9. Validate the published release through BRAT, including start-up, ordinary bidirectional synchronisation, and any release-specific regression scenario.
10. After a hyphenated pre-release passes, keep its release PR unmerged, reconcile its `versions.json` entry and exact release-note section into the selected base branch as metadata only, then close the PR through a separate maintainer action.
11. After a stable version passes, mark the release PR ready and merge the exact release commit into the selected base branch. Integrate that commit through the reviewed branch chain into the repository's default branch. Once the default branch contains the matching stable metadata, remove the pre-release designation, make the exact release the latest stable release, and publish stable CLI tags through a separate maintainer gate if selected.
12. If validation fails, leave the PR in draft and do not move the published tags. Reconcile the published version's `updates.md` section and `versions.json` entry into the base branch as metadata only, then prepare the next patch or pre-release version from the remaining `## Unreleased` entries.
10. After BRAT validation succeeds, mark the release PR ready and merge it into the selected base branch with a merge commit.
11. If validation fails, leave the PR in draft and do not move the published tags. Reconcile the published version's `updates.md` section and `versions.json` entry into the base branch as metadata only, then prepare the next patch or pre-release version from the remaining `## Unreleased` entries.
## Contribution Guidelines
@@ -65,7 +65,7 @@ The special meaning would duplicate `activeConfigurationId`, make a user-visible
### Add profile naming and full list editing to onboarding
That would make the first-run path longer and duplicate the established Saved connections interface. Automatic descriptive names and later renaming keep this change limited to data integrity and consistent selection.
That would make the first-run path longer and duplicate the established Remote Databases interface. Automatic descriptive names and later renaming keep this change limited to data integrity and consistent selection.
### Replace the compatibility fields immediately
@@ -77,11 +77,11 @@ Commonlib unit tests cover preserving existing profiles, opaque-ID insertion, ge
Self-hosted LiveSync unit tests cover preserving modern Setup URI profiles and their active selection, retaining legacy Setup URI and QR migration, adding CouchDB and Object Storage profiles beside an existing profile, independent P2P selection, fresh P2P selection as both main and P2P remote, and cancellation without mutation.
The real-Obsidian onboarding E2E owns the invitation, dialogue presentation, safe-area and touch-target checks, cancellation, and reopening from the Setup pane. It does not contact a remote or submit credentials. Remote connection correctness remains owned by the CouchDB, Object Storage, P2P, and two-Vault suites. The end-to-end Setup URI and provisioning acceptance workflow remains a separate release gate.
The real-Obsidian onboarding E2E owns the invitation, dialogue presentation, safe-area and touch-target checks, cancellation, and command reopening. It does not contact a remote or submit credentials. Remote connection correctness remains owned by the CouchDB, Object Storage, P2P, and two-Vault suites. The end-to-end Setup URI and provisioning acceptance workflow remains a separate release gate.
## Consequences
- Manual onboarding and the Saved connections list share one Commonlib profile contract.
- Manual onboarding and the Remote Databases pane share one Commonlib profile contract.
- Existing profiles survive reconfiguration, and a newly configured connection becomes explicitly selectable.
- Modern imports retain user-assigned profile identity and names.
- Legacy Setup URIs continue to work through an isolated compatibility boundary.
@@ -60,7 +60,7 @@ Keep configured-state inference separate from new-Vault initialisation. If an ex
### Onboarding activation and initialisation
- Keep an unconfigured Vault outside database initialisation, offline scanning, and configured-only checks. Offer setup through the long-lived onboarding Notice, and allow the wizard to be reopened from the Setup pane instead of opening a competing dialogue automatically.
- Keep an unconfigured Vault outside database initialisation, offline scanning, and configured-only checks. Offer setup through the long-lived onboarding Notice and the permanent command instead of opening a competing dialogue automatically.
- For new-device onboarding, reserve Rebuild before enabling and saving the accepted settings.
- For an unconfigured existing device, reserve Fetch before enabling and saving imported or manually confirmed settings.
- Suspend the current runtime after the flag has been written, apply the accepted settings through the scheduler's preparation callback, and request restart only after that callback succeeds.
+1 -1
View File
@@ -48,7 +48,7 @@ A TURN provider cannot read LiveSync's encrypted Vault contents, but it can obse
The **P2P Status** pane is the current Obsidian interface for P2P connections.
- After a P2P configuration exists, the command **Self-hosted LiveSync: P2P Sync : Open P2P Status** is available from the command palette.
- The command **Self-hosted LiveSync: P2P Sync : Open P2P Status** remains available from the command palette.
- The P2P ribbon icon appears only after a P2P configuration exists.
- LiveSync does not open the pane merely because Obsidian has started. If the pane was already part of the saved Obsidian workspace, Obsidian may restore it.
- Workspaces containing the retired P2P pane are migrated to the current status pane. The retired command is no longer exposed.
+1 -23
View File
@@ -32,28 +32,6 @@ While suspended:
The flag deliberately enables file logging, which may affect performance. Remove it after the emergency has been understood.
## Recover a conflicted or mismatched file
Use this workflow when one file, or a small number of known files, has conflicts, missing chunks, or a difference between the current Vault file and the local LiveSync database. The inspection is device-local: it does not query a remote database or prove that another device has the same chunks.
The `Hatch` recovery controls are ordered by escalation. Running **Recreate chunks for current Vault files** again with unchanged chunk settings and file contents produces the same chunks, and does not alter the revision tree. **Inspect conflicts and file/database differences** then provides actions for exact revisions. **Resolve All conflicted files by the newer one** is last because it applies a modification-time policy in bulk and logically deletes every other live version.
1. Stop editing the affected file, pause replication on the participating devices, and keep a separate copy of every readable version.
2. If another device or backup has the intended content, preserve that copy before changing any revision.
3. If the current Vault file is readable, select **Recreate current chunks**. This can restore only chunks derived from the current Vault contents; it cannot reconstruct unique bytes from an unavailable historical or conflict revision.
4. Select **Inspect conflicts and file/database differences****Begin inspection**.
5. Review the database winner, every conflict revision, and any unavailable shared ancestor separately. Revision identifiers, `Δsize`, `Δtime`, and chunk availability are diagnostic evidence; they do not decide which content is correct.
6. Use the wrench menu on the exact revision:
- **Compare with Vault** opens a read-only comparison for readable text.
- **Apply this revision to Vault** replaces the Vault file with that readable database revision.
- **Mark this revision as the Vault version** records an exact byte-for-byte match without creating a child revision.
- **Store Vault file as a child of this revision** preserves the current Vault bytes on that selected branch.
- **Retry reading revision** retries configured chunk retrieval without changing the revision tree.
- **Apply logical deletion to Vault**, **Discard this branch**, and **Discard unreadable revision** are destructive decisions. Use them only after preserving every version which may still be needed.
7. Synchronise the healthy source if chunks were restored, scan again, and confirm that the expected conflict or difference has disappeared before resuming ordinary editing.
An absent Vault file and a logical-deletion winner already agree and do not require a repair card unless another live branch remains. If the scan reports many unrelated files, or the local database itself is incomplete or corrupt, stop the per-file workflow and use [Reset synchronisation on this device](#reset-synchronisation-on-this-device) from a trusted remote. If the central remote must instead be reconstructed from an authoritative Vault, use [Overwrite server data with this device's files](#overwrite-server-data-with-this-devices-files).
## Reset synchronisation on this device
Use this when the remote copy is trusted but this device's local LiveSync database is incomplete, corrupt, or no longer aligned with it.
@@ -91,7 +69,7 @@ Garbage Collection removes unreferenced chunks while preserving the current data
- all relevant devices have synchronised; and
- the remaining historical and deletion state is understood.
Deleted documents, tombstones, live conflicts, and retained metadata are not free. Live conflict branches keep the chunks needed for review, while an ordinary superseded linear revision does not protect its former chunks. Garbage Collection can therefore make old content unreadable and cannot promise the smallest possible remote. Review the [Garbage Collection V3 specification](specs_garbage_collection.md) before using it.
Deleted documents and tombstones are not free, and historical revisions may keep chunks reachable. Garbage Collection therefore cannot promise the smallest possible remote.
Rebuild is a different operation. It reconstructs the database from a chosen authoritative state and is the more certain way to remove unwanted history or repair a damaged remote, but it is also more disruptive and can discard changes which exist only elsewhere.
-161
View File
@@ -1,161 +0,0 @@
# 1.0 preview release history
This document records the opt-in beta and release-candidate builds published before 1.0.0. Most users upgrading from 0.25.83 only need the consolidated [1.0.0 release notes](../../updates.md).
The prepared `1.0.0-rc.0` tag was not published as a plug-in release and is therefore omitted.
## 1.0.0-rc.1
27th July, 2026
The work towards 1.0 has become so substantial that I have written [an article about it](https://fancy-syncing.vrtmrz.net/blog/0036-livesync-1_0_0-en.html) (linked again here).
### Important
- This candidate retains the plug-in behaviour prepared for rc.0. The version was advanced because release tags are immutable; rc.0 was stopped during CLI validation before a plug-in release was published.
- This remains an opt-in pre-release for BRAT validation and does not replace the latest stable release. The exact rc.1 plug-in and CLI artefacts will be validated separately after publication.
### CLI and release validation
- CLI release validation now generates Setup URIs through the supported ESM package interface, allowing the Docker test to reach the CLI container instead of stopping during test preparation.
- The CLI Docker image now assigns its entrypoint permissions explicitly, so non-root execution does not depend on permissions inherited from the source checkout.
- Release finalisation now explicitly dispatches the CLI container workflow when CLI publication is selected, rather than relying on a workflow-created tag to start another workflow.
- Focused regression tests guard the ESM execution mode and deterministic container entrypoint permissions, while the existing release-workflow tests now require explicit CLI dispatch with non-dry-run, immutable-tag inputs.
### Testing
- The native CLI setup, put, cat, list, information, deletion, conflict-resolution, and revision-retrieval scenario completed with the packaged Commonlib dependency.
- The same scenario completed through the rebuilt non-root Docker image.
- The focused CLI and release-workflow unit tests passed after first demonstrating all three regressions against the unmodified implementation.
## 1.0.0-beta.5
26th July, 2026
### Improved
- **Inspect conflicts and file/database differences** now compares the current Vault file with the database winner and every live conflict revision. Compact, mobile-friendly indicators show missing chunks, `Δsize`, `Δtime`, whether the Vault matches the winner, and whether conflict branches remain.
- Each reported file and live revision now has a compact wrench menu. Its available actions can compare readable text, apply the selected revision to the Vault, record an exact byte match, store the Vault content as a child of the selected branch, retry retrieving missing chunks without changing the revision tree, or discard only the selected live branch after confirmation.
### Fixed
- A winning logical deletion is no longer reported as a missing Vault file when the file is already absent.
### Testing
- Strengthened Real Obsidian coverage for start-up with an existing configuration, Security Seed readiness, failure diagnostics, and P2P status pane placement in separate desktop and mobile sessions.
## 1.0.0-beta.4
25th July, 2026
### Improved
- **Verify and repair all files** now reports the database winner, every conflict revision, missing chunks, and unavailable shared ancestors separately. It can retry an exact revision without changing the tree, while discarding an unreadable live revision requires explicit confirmation.
- Command-palette actions now use clearer names and appear only when their feature and current context make them usable. Renamed commands keep their identifiers, so hotkeys already assigned to them continue to work. The onboarding wizard can be reopened from **Self-hosted LiveSync settings****Setup**.
- Text in setup and review dialogues can now be selected for copying or translation.
- When LiveSync adopts an available interface translation on first start-up, it now continues initialisation and leaves a persistent Notice from which the translation details can be opened, instead of waiting for an unsolicited dialogue.
### Fixed
- An unreadable conflict revision is no longer deleted automatically merely because its chunks are unavailable on the current device.
- Garbage Collection V3 now protects chunks required by every live conflict branch and the available revision ancestry needed to review and merge conflicts, instead of considering only the database winner. The action is offered only for CouchDB because P2P has no central database to compact and does not provide the device inventory required by the workflow. Collection now stops when device progress cannot be verified, and a compaction timeout is no longer followed by a contradictory success message.
### Testing
- Added regressions for revision repair, command availability, selectable dialogues, conflict-aware chunk reachability, device-progress safeguards, and compaction timeouts.
- Added a real CouchDB integration test for logical chunk deletion, shared and conflict chunk retention, compaction completion, downstream replication, and content-addressed chunk recreation.
- Added a real Obsidian encrypted reconnect scenario which replaces the remote Security Seed while one client retains the previous value, verifies that synchronisation refreshes it without restoring the old value, and proves a bidirectional encrypted round-trip.
## 1.0.0-beta.3
24th July, 2026
### Improved
- Enabling Hidden File Sync now opens one progress Notice before its setting is saved and reuses that Notice throughout the initial file scan, instead of stacking separate phase and restart Notices.
- P2P is now presented only after it has been configured: its status pane no longer opens at start-up, its ribbon icon remains hidden for CouchDB-only Vaults, and the retired P2P pane command has been removed. The current pane distinguishes announcing changes, following a peer, and persistent per-device actions. Setup and guidance now distinguish the required signalling relay from optional TURN, and describe the public signalling relay's privacy and availability limits.
- First-device P2P setup now accepts a successfully opened signalling room without requiring another peer to be online. Additional-device Fetch still requires selecting a source peer and completing `P2P Rebuild`.
- Manual CouchDB setup now distinguishes creating a first database from connecting an additional device to an existing one. Settings mode can save an unverified profile explicitly, while onboarding requires a successful connection, and each proposed server-configuration fix requires separate confirmation.
- Differences limited to the chunk hash algorithm, chunk size, or splitter version are now aligned automatically by default. Existing content remains readable, while an explicit opt-out and any difference which also involves an incompatible setting retain manual review.
### Fixed
- Choosing **Apply settings to this device, and fetch again** for a compatible configuration mismatch now applies the remote settings before Fetch, instead of updating the remote database with this device's settings.
- Accepted settings which control how new chunks are created now take effect before synchronisation is retried, rather than leaving the previous hash or splitter active until restart.
### Testing
- Added regressions for P2P configuration, the distinction between setting up the first device and using Fetch on an additional device, the P2P status pane, CouchDB setup policy, and mobile dialogues.
## 1.0.0-beta.2
23rd July, 2026
### Improved
- Choosing **Not now** on a merge conflict now postpones repeated dialogues for that conflict while the active file retains an unresolved-conflict warning. Three or more live versions show their current count and are reviewed one deterministic pair at a time; completed pairs remain resolved across restart. The existing conflict commands can reopen a postponed conflict explicitly, and a later conflict prompts again after the current one has been resolved.
### Fixed
- Answering or externally closing a merge dialogue immediately no longer leaves conflict processing waiting for a response which has already occurred.
### Testing
- Added revision-tree regressions and focused real-Obsidian scenarios for multiple-version review and restart between resolution stages.
## 1.0.0-beta.1
22nd July, 2026
### Important
- This corrected opt-in integration preview follows `1.0.0-beta.0` and does not replace the latest stable release. Update every participating device before resuming synchronisation, and continue to use a current backup while testing with an existing Vault.
### Fixed
- Conflict resolutions made on another device no longer recreate the same conflict when the receiving Vault still contains the exact content of the deleted losing revision. Automatic text and structured-data merge now uses the nearest revision actually shared by both branches instead of inferring ancestry from revision generation numbers.
- Edits, deletions, and renames made while a file is conflicted now extend the exact revision displayed on that device. If LiveSync cannot prove the displayed branch, it preserves the affected branches for review instead of silently applying the operation to the database winner.
### Testing
- Added revision-tree regressions and focused real-Obsidian scenarios for propagated resolutions and file operations performed while a conflict remains active.
## 1.0.0-beta.0
22nd July, 2026
### Important
- This is an opt-in 1.0 integration preview for BRAT and testing with existing Vaults. It does not replace the latest stable release. Use it with a current backup, and update every participating device before resuming synchronisation.
- An upgraded, copied, or restored Vault may pause replication for an explicit compatibility review. The review preserves the existing automatic synchronisation choices and resumes them only after the decision has been saved successfully.
### Improved
- An unconfigured installation now waits for you to start setup. A long-lived Notice offers the setup action, and **Open onboarding wizard** remains available from the command palette instead of the dialogue opening automatically.
- The setup wizard now creates named remote profiles for CouchDB, Object Storage, and P2P. Current Setup URIs preserve their profile names and selections, and the wizard reserves Rebuild or Fetch before the ordinary start-up scan begins.
- Peer-to-Peer Synchronisation (P2P) and Hidden File Sync are supported opt-in features. JWT authentication, ignore files, automatic newer-file conflict resolution, and Garbage Collection V3 remain previews. Customisation Sync remains a supported advanced workflow.
- Data Compression remains available after measurement showed a modest, workload-dependent reduction in stored and transferred chunk data. Its benefits, costs, and reason for remaining disabled by default in 1.0 are described in the Data Compression specification.
- Compatibility review now runs before Config Doctor without overlapping it. Existing Vaults retain their automatic synchronisation choices and explicit file-name case setting. For installations created by earlier releases, LiveSync preserves whether setup had been completed and saves a missing legacy case setting as case-insensitive.
- P2P connections now restart reliably after settings are reapplied or the local database is reset. Setup on an additional device asks you to select the source device once. Disconnecting leaves the LiveSync room and closes its signalling relay connections so that reconnecting can establish a new room.
- Action buttons are stacked vertically, long setup dialogues keep their controls reachable on mobile screens, and persistent Notices no longer cover close controls. Hidden File Sync reload and restart requests are grouped into one message, including the case reported in issue #555.
- Warnings about estimated remote storage size now appear as long-lived clickable Notices instead of timed dialogues. Initial uploads and Rebuild operations no longer prompt to send every chunk in advance; ordinary replication completes the transfer.
- Removed the obsolete **Use the trash bin** control and the setting for fixed chunk revisions. Remote deletion still follows Obsidian's preference, and chunk revisions remain content-derived. The Change Log remains available but no longer opens automatically or tracks unread versions.
### Fixed
- The optional Custom HTTP Handler used by Object Storage now sends the correct byte range from binary request bodies and reports unsupported body types instead of silently sending an empty request.
- When selectors, ignore files, size limits, modification-time limits, or file-name case settings are broadened, LiveSync now rechecks previously received files without requiring another remote update.
- P2P setup on the first device no longer displays reset or upload steps for a central database, and Config Doctor now offers its chunk size recommendation for CouchDB only when a CouchDB remote profile is selected.
### Security
- Fly.io setup now generates CouchDB and Vault encryption secrets with cryptographically secure randomness. Dependency updates prevent excessive CPU use from specially crafted path patterns and `mailto:` links. The CLI rejects path traversal and symbolic-link components detected before Vault operations.
### Miscellaneous
- Self-hosted LiveSync now owns its translation catalogue. Commonlib provides English messages to other applications, and translation contributions can be made directly to the Self-hosted LiveSync repository.
### Testing
- Expanded automated testing in Obsidian for upgrades, synchronisation between two devices, CouchDB, Object Storage, P2P, Hidden File Sync, mobile dialogues, and clean-up after failures.
+12 -26
View File
@@ -12,7 +12,7 @@ The following status applies to optional and compatibility features in the 1.0 l
| -------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Supported, opt-in | Peer-to-Peer Synchronisation, Hidden File Sync, and Customisation Sync | Maintained and covered by focused real-runtime tests. Enable them only where their separate setup and operational constraints are acceptable. |
| Maintained, advanced | Data Compression | Available as an explicit storage and bandwidth trade-off. It remains disabled by default because the measured processing and memory costs outweigh the mixed-dataset saving. |
| Beta or experimental | JWT authentication, ignore files, automatic newer-file conflict resolution, and Garbage Collection V3 for CouchDB | Retained for explicit testing and specialised use. They remain disabled by default and are not part of the minimum supported setup. |
| Beta or experimental | JWT authentication, ignore files, automatic newer-file conflict resolution, and Garbage Collection V3 | Retained for explicit testing and specialised use. They remain disabled by default and are not part of the minimum supported setup. |
| Compatibility only | V1 dynamic iteration counts, the old IndexedDB adapter, non-current hash algorithms, Eden chunks, and the stored `doNotUseFixedRevisionForChunks` key | Existing settings and data remain readable. New Vaults use the current defaults, and compatibility controls are shown only where a migration or recovery path still needs them. |
| Icon | Description |
@@ -40,7 +40,7 @@ Internal database or settings compatibility reviews use a separate safety dialog
This pane is used for setting up Self-hosted LiveSync. There are several options to set up Self-hosted LiveSync.
An unconfigured installation does not open the onboarding dialogue automatically or scan the Vault into the local database. A long-lived Notice offers the onboarding action. If the Notice is dismissed, open **Self-hosted LiveSync settings****Setup****Rerun Onboarding Wizard**.
An unconfigured installation does not open the onboarding dialogue automatically or scan the Vault into the local database. A long-lived Notice offers the onboarding action, and **Open onboarding wizard** remains available from the command palette after that Notice closes.
Choose the new-device path when this device owns the files which should initialise synchronisation. Choose the existing-device path when it should receive an established remote state. The wizard reserves Rebuild or Fetch respectively before enabling the settings and requesting a restart, so the selected initialisation runs before the ordinary start-up scan.
@@ -169,9 +169,9 @@ Show verbose log. Please enable when you report the logs
## 3. Remote Configuration
### 1. Connection settings
### 1. Remote Server
Self-hosted LiveSync stores multiple remote connection profiles under **Connection settings** **Saved connections**. Each profile represents a CouchDB database, an Object Storage connection, or a P2P configuration, and several profiles can be kept in one Vault.
Self-hosted LiveSync supports multiple remote connection profiles under **Remote Server** -> **Remote Databases**. This allows you to save and switch between multiple databases or bucket configurations in a single vault.
Each profile has an opaque identifier and a presentation name. The name does not need to be unique and is not used to select the profile. The main remote and the P2P remote are selected independently, so code and settings imports must preserve both selections rather than relying on a special identifier such as `default`.
@@ -185,7 +185,7 @@ Each profile has an opaque identifier and a presentation name. The name does not
Setting key: remoteType
The active connection type. This is automatically projected to the legacy configuration when you activate a connection profile.
The active remote server type. This is automatically projected to the legacy configuration when you activate a connection profile.
### 2. Notification
@@ -698,12 +698,6 @@ Open the dialogue
#### Make report to inform the issue
#### Copy database information for a file
Select a file to copy its local database information. The command **Copy database information for the active file** performs the same inspection for the file open in the editor.
The report includes the Vault-relative path, document and chunk identifiers, local database revisions, conflicts, and local chunk availability. It does not query the remote or include file contents. Paths and identifiers can still be private metadata, so review the report before sharing it.
#### Write logs into the file
Setting key: writeLogToTheFile
@@ -727,19 +721,17 @@ Stop reflecting database changes to storage files.
### 3. Recovery and Repair
#### Recreate chunks for current Vault files
#### Recreate missing chunks for all files
Recreate chunks from files currently present in the Vault. This can repair missing chunks for those exact current contents after they have been confirmed as authoritative. It cannot reconstruct unavailable historical or conflict content.
#### Inspect conflicts and file/database differences
Compare each Vault file with every current live revision in the local database. Each winner and conflict revision is shown separately with its exact revision identifier, local chunk availability, and relationship to the current Vault file. Unavailable shared ancestors are reported separately because they prevent conservative three-way merging but are not live revisions which can be discarded.
Select **Begin inspection** to run the inspection. Each reported file and live revision has a wrench menu for read-only comparison, applying an exact database revision to the Vault, recording an exact byte match, preserving the Vault file as a child of a selected branch, retrying chunk retrieval, or explicitly discarding a branch. Destructive actions require confirmation. Follow [Recover a conflicted or mismatched file](recovery.md#recover-a-conflicted-or-mismatched-file) before changing revision history.
This will recreate chunks for all files. If there were missing chunks, this may fix the errors.
#### Resolve All conflicted files by the newer one
After confirmation, resolve every conflict by modification time. This logically deletes every version except the newest one. It is a destructive policy choice and cannot recover content which is already unavailable.
Resolve all conflicted files by the newer one. Caution: This will overwrite the older one, and cannot resurrect the overwritten one.
#### Verify and repair all files
Compare the content of files between on local database and storage. If not matched, you will be asked which one you want to keep.
#### Check and convert non-path-obfuscated files
@@ -1047,12 +1039,6 @@ Purge all download/upload cache.
Delete all data on the remote server.
### 6. Garbage Collection V3 (CouchDB only)
Garbage Collection V3 identifies chunk documents which are not reachable from any current file or live conflict branch, creates logical deletions for those chunks locally, propagates the deletions to CouchDB, and requests remote compaction.
Use it only when the Vault, local database, and remote are healthy, and every relevant device has synchronised. It can make an ordinary superseded file revision unreadable when no live state still needs its chunks. It does not repair corruption or replace a deliberate rebuild. See the [Garbage Collection V3 specification](specs_garbage_collection.md).
### 7. Reset
#### Delete local database to reset or uninstall Self-hosted LiveSync
+1 -1
View File
@@ -34,7 +34,7 @@ Before starting:
![P2P local database confirmation on the first device](../images/p2p-setup/guide-p2p-setup-first-rebuild-confirmation.png)
7. Keep optional features disabled until ordinary note synchronisation works.
8. After saving the P2P profile, open `Self-hosted LiveSync: P2P Sync : Open P2P Status` from the command palette. The P2P ribbon icon provides the same destination. Select `Open connection` if signalling is disconnected.
8. Open `Self-hosted LiveSync: P2P Sync : Open P2P Status` from the command palette. After a P2P profile exists, the P2P ribbon icon provides the same destination. Select `Open connection` if signalling is disconnected.
![First P2P device connected to the signalling relay](../images/p2p-setup/guide-p2p-setup-first-device-connected.png)
+2 -33
View File
@@ -46,37 +46,6 @@ The all-branch history check prevents a resolved conflict from being recreated m
The compatibility implementation currently selects the newer modification time for differing binary conflicts even when the general **Always overwrite with a newer file** option is disabled. This is existing behaviour, not a new 1.0 guarantee. Changing it to explicit selection only is a separate compatibility decision.
## Unreadable revisions and repair
A document revision can remain in the PouchDB tree while one or more chunks needed to reconstruct its content are unavailable. Missing content is not evidence that the revision is obsolete. LiveSync therefore leaves an unreadable winner or conflict revision in the tree instead of deleting it during automatic conflict processing.
**Hatch** → **Inspect conflicts and file/database differences** inspects the current winner, every current conflict revision, and the nearest shared ancestor for each conflict. A logical-deletion winner and an absent Vault file already agree, so that state is not reported unless another live branch still requires attention. When the Vault already matches the winner but conflict branches remain, the card shows the compact status `✅ Vault matches winner · ⚠️ Conflicts: N`; matching the winner does not mean that the conflict has been resolved.
Each reported live revision has a compact wrench menu. The available actions depend on the exact revision and current Vault state:
- **Compare with Vault** opens the existing difference dialogue in read-only mode for differing text files.
- **Apply this revision to Vault** writes the selected readable revision, even when it is not the database winner. Replacing an existing file requires confirmation.
- **Mark this revision as the Vault version** is offered when the bytes already match. It records exact device-local provenance without creating a child revision, and refuses the operation if the file changed after inspection.
- **Store Vault file as a child of this revision** preserves the current Vault bytes on the explicitly selected live branch.
- **Apply logical deletion to Vault** removes an existing Vault file after confirmation. An absent file needs no retained deletion provenance.
- **Retry reading revision** attempts the configured chunk-retrieval path again. It does not change the revision tree.
- **Discard this branch** is available for each exact live revision while at least one other live branch remains. It requires confirmation and creates a logical deletion on only the selected branch without changing the current Vault file.
- **Discard unreadable revision** remains available as a recovery action when an unreadable revision is the only live leaf. It requires confirmation because no other database branch remains.
Every mutating action rechecks that the selected revision is still a current live leaf. If another operation resolved or replaced it, the action fails and the card is refreshed instead of extending an obsolete branch.
The card uses compact, mobile-friendly diagnostic rows with an emoji and a text label. `🧩 Missing chunks: N` identifies an unreadable revision. In the database row, `Δsize` is decoded size minus recorded size; in the Vault row, `Δsize vs DB` is Vault size minus decoded database size. `Δtime` is Vault modification time minus database modification time. The ordinary two-second comparison window still labels which side is newer. These values help diagnose a mismatch; path, size, and modification time do not prove revision identity or decide which content should win.
A shared ancestor is informational. An ancestor which is no longer a live revision cannot be discarded independently through this workflow. If its body is unavailable, conservative three-way merge remains disabled, although readable live revisions can still be selected manually.
Logical deletion does not recreate missing bytes, purge the document history, or prove that the deleted version was unimportant. Another replica or backup may still contain the missing chunks. Recover from that source before discarding a revision whenever possible.
**Recreate chunks for current Vault files** can recreate chunks only from files which are readable in the current Vault. It cannot reconstruct unique bytes from an unavailable historical or conflict revision.
Garbage Collection V3 treats every live conflict revision and its nearest available shared ancestor as reachable. Their locally available chunks are retained until the conflict is resolved. After resolution, chunks used only by the discarded branch or no-longer-needed merge ancestry can become eligible for collection. See the [Garbage Collection V3 specification](specs_garbage_collection.md).
A generation-one revision has no parent. When its body is unavailable, LiveSync cannot preserve a changed Vault file as a sibling branch without inventing ancestry. It leaves the operation unresolved. Recover the missing chunks from another replica or backup, or explicitly discard that live revision. If the current Vault file is the intended replacement, it can be stored after the unreadable revision has been logically deleted.
### Two devices independently create the same path
If two devices create the same full synchronised path before either device has
@@ -251,8 +220,8 @@ Do not:
## Verification
Commonlib's real-PouchDB and injected-boundary unit tests cover unequal branch lengths, exact shared ancestry, deterministic ordering of multiple live leaves, a sensible stage followed by reconstruction of a manual pair, content below a deleted losing leaf, recorded and reconstructed branch identity, ambiguous matches, conflict-time editing, missing-body preservation when parent metadata is available, refusal to invent a parent for a generation-one revision, logical deletion, case-only rename, cross-path rename, and safe unproven fallbacks.
Commonlib's real-PouchDB and injected-boundary unit tests cover unequal branch lengths, exact shared ancestry, deterministic ordering of multiple live leaves, a sensible stage followed by reconstruction of a manual pair, content below a deleted losing leaf, recorded and reconstructed branch identity, ambiguous matches, conflict-time editing, logical deletion, case-only rename, cross-path rename, and safe unproven fallbacks.
LiveSync's optional real-Obsidian two-Vault checks have two scopes. `E2E_OBSIDIAN_INCLUDE_MARKDOWN_CONFLICT=true` resolves and edits a Markdown conflict, propagates it to a Vault which still displays the deleted losing content, and requires one live result to remain. `E2E_OBSIDIAN_INCLUDE_CONFLICT_OPERATIONS=true` edits, deletes, case-renames, and cross-path-renames files while conflicts remain active; it verifies the parent revision of each resulting branch, replicates those exact trees, and confirms that the other live branches remain intact.
The focused `test:e2e:obsidian:conflict-dialog-policy` scenario creates three live versions in one real Obsidian Vault. It verifies the count warning, commits a concatenated child of the displayed winner, confirms that the untouched leaf remains as one conflict, postpones that remaining pair, restarts the isolated Obsidian profile, and confirms that only the live pair is reconstructed. It also verifies that an incoming resolution closes a stale dialogue, completes the waiting conflict operation, and clears the warning. The repair scenario removes a referenced local chunk, confirms that the exact unreadable live revision remains in the tree, and exercises explicit retry and discard controls without deleting another live revision.
The focused `test:e2e:obsidian:conflict-dialog-policy` scenario creates three live versions in one real Obsidian Vault. It verifies the count warning, commits a concatenated child of the displayed winner, confirms that the untouched leaf remains as one conflict, postpones that remaining pair, restarts the isolated Obsidian profile, and confirms that only the live pair is reconstructed. It also verifies that an incoming resolution closes a stale dialogue, completes the waiting conflict operation, and clears the warning.
-65
View File
@@ -1,65 +0,0 @@
# Garbage Collection V3
Garbage Collection V3 is a beta maintenance operation for CouchDB remotes. It removes chunk documents which are no longer required by the current local revision tree, propagates those logical deletions to CouchDB, and then requests remote compaction.
It is not a repair operation. Use it only when the Vault, the local LiveSync database, and the CouchDB remote are healthy, every relevant device has synchronised, and recoverable backups exist.
## Supported scope
Garbage Collection V3 is available in Edge Case mode for CouchDB. It is not offered for Object Storage or P2P:
- Object Storage has a different journal and object lifecycle.
- P2P has no central database to compact and cannot provide the accepted-device progress information required by this workflow.
The operation requires **Fetch chunks on demand** to be off so that its local reachability result is not confused with chunks which exist only on the remote.
## Workflow
After the user starts Garbage Collection V3, LiveSync:
1. completes a one-shot bidirectional CouchDB synchronisation;
2. reads the accepted-device list and current progress recorded on the remote;
3. requires parseable progress information, warns when an accepted device has no current information or device progress differs, then requires explicit confirmation;
4. computes the chunks reachable from the local PouchDB revision tree;
5. creates a logical deletion for each locally present chunk which is not reachable;
6. completes a push-only replication so that those deletions reach CouchDB;
7. requests CouchDB compaction and waits for it for up to two minutes; and
8. clears the local chunk caches.
If the initial synchronisation, device inspection, or confirmation fails, the workflow stops before collection. Missing or invalid device progress is treated as a failed inspection rather than offered as a confirmation override. If push-only replication fails, the local logical deletions have already been created, but remote compaction is not started; synchronise again before retrying or using another device. A compaction failure or completion timeout is reported separately and is not also reported as a successful completion.
## Reachability rules
A chunk remains reachable when it is referenced by any of the following:
- the current database winner for a file;
- any other live conflict revision for that file;
- an available revision on either side of a live conflict which is required to describe the divergence; or
- the nearest available revision shared by both live conflict branches.
Chunk identifiers are content-derived and shared between files. Reachability is therefore collected into one set across the database. A chunk used by two or more current files remains protected even when one file is updated or deleted.
An ordinary superseded linear revision does not protect its former chunks. Once no current file or live conflict branch references a chunk, it can be collected. After a conflict is resolved, chunks unique to the discarded branch and to no-longer-needed merge ancestry can also become eligible.
## Consequences
Garbage Collection deliberately trades historical recoverability for storage. A metadata revision may remain in the revision tree after a chunk which only that superseded revision used has been collected, so that historical body can become unreadable. Remote compaction can then discard old CouchDB revision bodies. Tombstones and retained metadata also consume storage, so the operation does not promise the smallest possible database.
Writing the same bytes again produces the same content-derived chunk identifier. If that chunk was collected previously, the normal chunk-writing path creates a new live revision for it, and ordinary replication can transfer it again. This does not recover an older file revision automatically; it only makes the newly written content available.
Garbage Collection does not reconstruct a chunk which is already missing, determine whether an unreadable revision is important, or repair a damaged local database. Use **Inspect conflicts and file/database differences**, another healthy replica, or a backup for those cases. Use **Overwrite Server Data with This Device's Files** only when a chosen Vault is authoritative and a deliberate remote rebuild is required.
## Verification
Commonlib tests use real in-memory PouchDB revision trees to verify:
- collection eligibility after a normal file update;
- protection of chunks shared by multiple current files;
- protection of all live conflict branches and their nearest available shared ancestor;
- eligibility of losing-branch and ancestor-only chunks after conflict resolution;
- propagation of chunk deletion to another PouchDB database; and
- recreation and propagation when the same content is written again.
Self-hosted LiveSync unit tests verify that Garbage Collection V3 uses Commonlib's revision-aware result, deletes only unreachable chunks, performs the initial bidirectional and final push-only replications in order, and requests remote compaction.
A disposable real-CouchDB integration test verifies logical deletion on the server, retention of shared and conflict chunks, compaction completion, replication after collection, and recreation of a content-addressed chunk. CouchDB's choice and timing of physical byte reclamation remain an external database boundary, so the test does not assert a particular reduction in database size.
+1 -1
View File
@@ -27,7 +27,7 @@ All guidelines and conventions listed below are disclosed and maintained solely
- Boot-up sequence (boot-sequence)
- The initialisation process of the plug-in when Obsidian starts. It starts with the loading of the plug-in, setting up core services, loading saved settings, and opening the local database. Once the layout is ready, the plug-in checks for the presence of flag files, runs configuration diagnostics, connects to the remote database, and begins file watching. The sequence finishes once the plug-in is fully ready and operational.
- Broken files (Size mismatch)
- A state where a file's metadata and the actual content stored in its chunks do not match, causing file retrieval or synchronisation failures. These mismatches can be inspected with `Inspect conflicts and file/database differences` on the Hatch pane, then handled one exact revision at a time.
- A state where a file's metadata and the actual content stored in its chunks do not match, causing file retrieval or synchronisation failures. These mismatches can be detected and resolved by running validation tools such as `Verify and repair all files` on the Hatch pane.
- Chunk / Chunks
- Divided units of data stored in the database or object storage to facilitate efficient synchronisation.
- Compaction
+4 -15
View File
@@ -53,18 +53,9 @@ Check Obsidian's `Detect all file extensions`, LiveSync selectors, ignore files,
If the log reports missing chunks or a size mismatch:
1. stop editing the affected file and keep a separate copy of any readable content;
2. restart Obsidian once to rule out an interrupted fetch;
3. synchronise a device or restore a backup which still has the correct content;
4. on that healthy device, run `Recreate chunks for current Vault files`, then synchronise;
5. follow [Recover a conflicted or mismatched file](recovery.md#recover-a-conflicted-or-mismatched-file); run `Inspect conflicts and file/database differences` from `Hatch`, then use each revision's wrench menu to review and act on that exact branch; and
6. use `Discard this branch` only after confirming that the exact live branch is no longer wanted. Use the separate `Discard unreadable revision` recovery action only when an unreadable revision is the sole live leaf.
The repair card uses compact diagnostic rows which remain readable in a narrow mobile settings pane. `🧩 Missing chunks: N` marks an unreadable revision. In the database row, `Δsize` means decoded size minus recorded size; `Δsize vs DB` means Vault size minus decoded database size; and `Δtime` means Vault modification time minus database modification time. These are diagnostic values, not a rule for deciding which revision is correct. `✅ Vault matches winner · ⚠️ Conflicts: N` means that the current Vault bytes agree with the database winner while other live branches still need a decision. Every mutating action rechecks that its selected revision is still live. Applying a logical deletion to an existing Vault file requires confirmation; a logical-deletion winner with no Vault file already agrees and is omitted.
`Retry reading revision` does not change the revision tree. `Discard this branch` creates a logical deletion on one exact live revision while another live branch remains and leaves the current Vault file unchanged. If the discarded revision was recorded as the Vault's exact source, that stale device-local provenance is removed. `Discard unreadable revision` provides the corresponding explicit escape hatch for a sole unreadable live leaf. Neither action purges history or reconstructs missing content. An unavailable non-live ancestor cannot be deleted through this workflow; it disables conservative three-way merge but does not prevent explicit selection between readable live revisions.
`Recreate chunks for current Vault files` uses current Vault content. It cannot recreate unique bytes which exist only in an unreadable historical or conflict revision.
1. restart Obsidian once to rule out an interrupted fetch;
2. on a device which has the correct file, run `Recreate missing chunks for all files`, then synchronise; and
3. if the mismatch remains, run `Verify and repair all files` from `Hatch` and review which copy is authoritative.
## A configuration mismatch dialogue blocks synchronisation
@@ -119,8 +110,6 @@ Enable Obsidian's `Detect all file extensions`, then check LiveSync selectors, i
Run `Generate full report for opening the issue with debug info` to copy the current settings summary and recent verbose log lines. Remove credentials, remote URLs, Vault names, file contents, and other private information before sharing it.
When a problem concerns one file, run **Copy database information for the active file**, or use **Hatch****Copy database information for a file** to select another file. The report describes this device's local database view, including the Vault-relative path, document and chunk identifiers, local database revisions, conflicts, and local chunk availability. It does not query the remote server or include file contents. Treat paths and identifiers as private metadata before sharing.
Use `Show log` for live inspection. Logs are intentionally kept in memory for a limited time to reduce accidental disclosure. Enable `Write logs into the file` only while reproducing a problem, then disable it and remove the file after review because persistent logging affects performance and may contain private data.
![Write logs into the file](../images/write_logs_into_the_file.png)
@@ -131,7 +120,7 @@ Browser security errors, particularly CORS failures, may reach the plug-in only
LiveSync stores file metadata, chunks, revision history, conflicts, deletions, and tombstones. Deleting or shortening a file therefore does not immediately remove every object which once represented it.
Garbage Collection V3 can remove unreferenced chunks from a healthy CouchDB setup, but it is appropriate only when the Vault and local database are healthy and all relevant devices have synchronised. Current files and live conflict branches protect their required chunks; an ordinary superseded revision does not. Tombstones and retained metadata are not free, so Garbage Collection does not guarantee a minimal database. Review the [Garbage Collection V3 specification](specs_garbage_collection.md) before using it.
Garbage Collection can remove unreferenced chunks, but it is appropriate only when the Vault and local database are healthy and all relevant devices have synchronised. Tombstones and retained revisions are not free, so Garbage Collection does not guarantee a minimal database.
`Overwrite Server Data with This Device's Files` is a separate rebuild operation and is the more certain way to reconstruct a central remote from a chosen authoritative Vault. It is also destructive and may discard changes which exist only on another device. Review [Recovery and flag files](recovery.md#garbage-collection-is-not-rebuild) before choosing between them.
+1 -1
View File
@@ -1,7 +1,7 @@
{
"id": "obsidian-livesync",
"name": "Self-hosted LiveSync",
"version": "1.0.1",
"version": "1.0.0-beta.3",
"minAppVersion": "1.7.2",
"description": "Community implementation of self-hosted livesync. Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"author": "vorotamoroz",
+32 -43
View File
@@ -1,12 +1,12 @@
{
"name": "obsidian-livesync",
"version": "1.0.1",
"version": "1.0.0-beta.3",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "obsidian-livesync",
"version": "1.0.1",
"version": "1.0.0-beta.3",
"license": "MIT",
"workspaces": [
"src/apps/cli",
@@ -22,17 +22,15 @@
"@smithy/querystring-builder": "^4.2.9",
"@smithy/types": "^4.14.3",
"@smithy/util-retry": "^4.4.5",
"@vrtmrz/browser-ui-kit": "0.1.0",
"@vrtmrz/livesync-commonlib": "0.1.0",
"@vrtmrz/obsidian-plugin-kit": "0.1.3",
"@vrtmrz/ui-interactions": "0.1.2",
"@vrtmrz/livesync-commonlib": "0.1.0-rc.11",
"@vrtmrz/obsidian-plugin-kit": "0.1.2",
"diff-match-patch": "^1.0.5",
"fflate": "^0.8.2",
"idb": "^8.0.3",
"markdown-it": "^14.2.0",
"minimatch": "^10.2.5",
"obsidian": "^1.13.1",
"octagonal-wheels": "^0.1.52",
"octagonal-wheels": "^0.1.51",
"qrcode-generator": "^1.4.4",
"xxhash-wasm-102": "npm:xxhash-wasm@^1.0.2"
},
@@ -58,7 +56,7 @@
"@types/transform-pouch": "^1.0.6",
"@typescript-eslint/parser": "8.56.1",
"@vitest/coverage-v8": "^4.1.8",
"@vrtmrz/obsidian-test-session": "0.2.6",
"@vrtmrz/obsidian-test-session": "0.2.5",
"dotenv-cli": "^11.0.0",
"esbuild": "0.28.1",
"esbuild-plugin-inline-worker": "^0.1.1",
@@ -4765,19 +4763,10 @@
"url": "https://opencollective.com/vitest"
}
},
"node_modules/@vrtmrz/browser-ui-kit": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@vrtmrz/browser-ui-kit/-/browser-ui-kit-0.1.0.tgz",
"integrity": "sha512-UlMPRZS3lLFcuEKBaAPcSkIY0we3+kj32GHLcOAfxmooy8XaJWh9/VP5MCC+lo06GrZ6graE3vwmAiFsAHwgng==",
"license": "MIT",
"dependencies": {
"@vrtmrz/ui-interactions": "0.1.2"
}
},
"node_modules/@vrtmrz/livesync-commonlib": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@vrtmrz/livesync-commonlib/-/livesync-commonlib-0.1.0.tgz",
"integrity": "sha512-rdzEubzLStioanE67pps2XSGZ2UGyMIyeEoKsdPztQLLW8wM05PWApOPbJujIydHcDr2hFanbDFe89Lk7Mn4XQ==",
"version": "0.1.0-rc.11",
"resolved": "https://registry.npmjs.org/@vrtmrz/livesync-commonlib/-/livesync-commonlib-0.1.0-rc.11.tgz",
"integrity": "sha512-o811duZFajxDFI6cy7zwtXPg6lihx5e1MH6Xse1sx4HseYurbaDf9DtZJdAtfkjTxl5wOZRSnVMoMLn+I/xD+g==",
"license": "MIT",
"dependencies": {
"@aws-sdk/client-s3": "^3.808.0",
@@ -4838,21 +4827,21 @@
}
},
"node_modules/@vrtmrz/obsidian-plugin-kit": {
"version": "0.1.3",
"resolved": "https://registry.npmjs.org/@vrtmrz/obsidian-plugin-kit/-/obsidian-plugin-kit-0.1.3.tgz",
"integrity": "sha512-6fsKdhFZtBv6FXlZHtSmpqwROohFzDmres6q08nr2xYGVeh2ooBGU3zJS94WN/tOjDT+wa/Vr3yE42wmI0pIZA==",
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/@vrtmrz/obsidian-plugin-kit/-/obsidian-plugin-kit-0.1.2.tgz",
"integrity": "sha512-LNNV8QCaN6FvRA+of96GiqI8atAbnMKQvLbqpi3nsepEe6tN8SmXO3P7pqBl3axNXEHhQfU5ggj5HAq9GJPgwQ==",
"license": "MIT",
"dependencies": {
"@vrtmrz/ui-interactions": "0.1.2"
"@vrtmrz/ui-interactions": "0.1.1"
},
"peerDependencies": {
"obsidian": ">=1.8.7"
}
},
"node_modules/@vrtmrz/obsidian-test-session": {
"version": "0.2.6",
"resolved": "https://registry.npmjs.org/@vrtmrz/obsidian-test-session/-/obsidian-test-session-0.2.6.tgz",
"integrity": "sha512-7xDTmTW3igBxwQGgbbpoRZU0JvOBpGgdRT4qF2WVrKz03OtKMJdkfJILY04/HZ+n4tvn9Ze8phJBr0dx3wewcQ==",
"version": "0.2.5",
"resolved": "https://registry.npmjs.org/@vrtmrz/obsidian-test-session/-/obsidian-test-session-0.2.5.tgz",
"integrity": "sha512-ZsI+Yx3z6IEFfh5Ey5mEUBNI0SUD6oDhP7D9LSZVEqPmdJz2JMiag7d8u/p1i6KpsoI2HbDvtw4RHpB+BQReAw==",
"dev": true,
"license": "MIT",
"engines": {
@@ -4864,9 +4853,9 @@
}
},
"node_modules/@vrtmrz/ui-interactions": {
"version": "0.1.2",
"resolved": "https://registry.npmjs.org/@vrtmrz/ui-interactions/-/ui-interactions-0.1.2.tgz",
"integrity": "sha512-njT2BSFh57imwGDxWXJOcGbZOYFVNGmeXMrS7/RUdBoAeOjWzJB9TxI7WcR1sgQIXKh8TE9hqALU8lue5b8dRw==",
"version": "0.1.1",
"resolved": "https://registry.npmjs.org/@vrtmrz/ui-interactions/-/ui-interactions-0.1.1.tgz",
"integrity": "sha512-XpO5fQzC7jyOW3xVF7YtFHI7X1BPQ7hJW56uw8atx8mDR7C9ejG2YSn0XcZ1H5FOCPgJbmkh/FLQRKLqSK8jkw==",
"license": "MIT"
},
"node_modules/@wdio/config": {
@@ -5986,15 +5975,15 @@
"license": "MIT"
},
"node_modules/brace-expansion": {
"version": "5.0.8",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.8.tgz",
"integrity": "sha512-JZyDyq3D4AUifKTPOB7DELf6XsB3WdPuNxCtob1vFXPsSXhdAiHBWJ/tJ8HAc9aH84BK+5JFZLNkJKx3G9kzQg==",
"version": "5.0.7",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-5.0.7.tgz",
"integrity": "sha512-7oFy703dxfY3/NLxC1fh2SUCQ0H9rmAY+5EpDVfXjUTTs+HEwR2nYaqLv+GWcTsumwxPfiz6CzCNkwXwBUwqCA==",
"license": "MIT",
"dependencies": {
"balanced-match": "^4.0.2"
},
"engines": {
"node": "20 || >=22"
"node": "18 || 20 || >=22"
}
},
"node_modules/braces": {
@@ -11532,9 +11521,9 @@
"license": "MIT"
},
"node_modules/octagonal-wheels": {
"version": "0.1.52",
"resolved": "https://registry.npmjs.org/octagonal-wheels/-/octagonal-wheels-0.1.52.tgz",
"integrity": "sha512-9WJN2UveNh90Op1S07cIso1WyNrQbO/unibDLfUGnpomIcU4g6F+p8reZHW0Ed8sGzKF5qGnQp991Y9MB1TwNg==",
"version": "0.1.51",
"resolved": "https://registry.npmjs.org/octagonal-wheels/-/octagonal-wheels-0.1.51.tgz",
"integrity": "sha512-KTlfqKPjobHJg/t3A539srnFf+VHr1aXkHSmsNDDpiI5UFC7FamZ95dWpJfGE2EI/HULR5hveQDgkazmz8SAcg==",
"license": "MIT",
"dependencies": {
"idb": "^8.0.3"
@@ -15924,11 +15913,11 @@
},
"src/apps/cli": {
"name": "self-hosted-livesync-cli",
"version": "1.0.1-cli",
"version": "1.0.0-beta.3-cli",
"dependencies": {
"chokidar": "^4.0.0",
"minimatch": "^10.2.5",
"octagonal-wheels": "^0.1.52",
"octagonal-wheels": "^0.1.51",
"pouchdb-adapter-http": "^9.0.0",
"pouchdb-adapter-leveldb": "^9.0.0",
"pouchdb-core": "^9.0.0",
@@ -15949,9 +15938,9 @@
},
"src/apps/webapp": {
"name": "livesync-webapp",
"version": "1.0.1-webapp",
"version": "1.0.0-beta.3-webapp",
"dependencies": {
"octagonal-wheels": "^0.1.52"
"octagonal-wheels": "^0.1.51"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^7.1.2",
@@ -15961,9 +15950,9 @@
}
},
"src/apps/webpeer": {
"version": "1.0.1-webpeer",
"version": "1.0.0-beta.3-webpeer",
"dependencies": {
"octagonal-wheels": "^0.1.52"
"octagonal-wheels": "^0.1.51"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^7.1.2",
+6 -17
View File
@@ -1,6 +1,6 @@
{
"name": "obsidian-livesync",
"version": "1.0.1",
"version": "1.0.0-beta.3",
"description": "Reflect your vault changes to some other devices immediately. Please make sure to disable other synchronize solutions to avoid content corruption or duplication.",
"main": "main.js",
"type": "module",
@@ -12,7 +12,6 @@
"buildDev": "node esbuild.config.mjs dev",
"lint": "eslint --cache --cache-strategy content --concurrency off src",
"lint:community": "eslint --config eslint.community.config.mjs --concurrency off src",
"lint:community:tools": "eslint --config eslint.community.config.mjs --concurrency off --max-warnings 0 _tools",
"svelte-check": "svelte-check --tsconfig ./tsconfig.json --fail-on-warnings",
"tsc-check": "tsc --noEmit",
"tsc-check:apps": "tsc --noEmit -p src/apps/browser/tsconfig.json && tsc --noEmit -p src/apps/cli/tsconfig.json && tsc --noEmit -p src/apps/webapp/tsconfig.json && tsc --noEmit -p src/apps/webpeer/tsconfig.app.json && tsc --noEmit -p src/apps/webpeer/tsconfig.node.json",
@@ -23,19 +22,13 @@
"prettyNoWrite": "prettier --config ./.prettierrc.mjs \"**/*.js\" \"**/*.ts\" \"**/*.json\" ",
"precheck:compatibility": "npm run build",
"check:compatibility": "node utils/check-compatibility.js --file main.js --ios 15",
"check": "npm run tsc-check && npm run tsc-check:apps && npm run lint && npm run lint:community -- --quiet && npm run lint:community:tools && npm run svelte-check && npm run check:compatibility",
"check": "npm run tsc-check && npm run tsc-check:apps && npm run lint && npm run lint:community -- --quiet && npm run svelte-check && npm run check:compatibility",
"i18n:bake": "npm run i18n:yaml2json && npm run i18n:bakejson && npm run i18n:format",
"i18n:bakejson": "tsx _tools/bakei18n.ts",
"i18n:format": "prettier --config .prettierrc.mjs --write --log-level error 'src/common/messagesJson/*.json' 'src/common/messages/*.ts'",
"i18n:json2yaml": "tsx _tools/json2yaml.ts",
"i18n:yaml2json": "tsx _tools/yaml2json.ts",
"test:unit": "vitest run --config vitest.config.unit.ts",
"build:browser-apps": "npm run build --workspace livesync-webapp --workspace webpeer",
"test:browser-apps": "npm run test:browser --workspace livesync-webapp && npm run test:browser --workspace webpeer",
"test:browser-apps:pages": "deno test -A --no-check --frozen --config test/browser-apps/deno.json --lock test/browser-apps/deno.lock test/browser-apps/pages/browser-smoke.test.ts",
"test:e2e:browser-apps": "npm run test:browser-apps",
"pretest:e2e:browser-apps:interop": "npm run build:browser-apps && npm run build --workspace self-hosted-livesync-cli",
"test:e2e:browser-apps:interop": "deno run -A --no-check --frozen --config test/browser-apps/deno.json --lock test/browser-apps/deno.lock test/browser-apps/run-compose-interop.ts",
"inspect:troubleshooting": "tsx _tools/inspect-troubleshooting-docs.ts",
"test:setup-tools": "bash utils/flyio/setenv.test.sh && deno test -A --config=utils/flyio/deno.jsonc --frozen --lock=utils/flyio/deno.lock utils/livesync-commonlib-version.test.ts utils/couchdb/provision.test.ts utils/setup/generate_setup_uri.test.ts utils/flyio/generate_setupuri.test.ts",
"test:contract:contexts": "vitest run --config vitest.config.unit.ts src/modules/services/ObsidianServiceContext.unit.spec.ts src/apps/cli/services/NodeServiceContext.unit.spec.ts src/apps/browser/createLiveSyncBrowserServiceHub.unit.spec.ts",
@@ -57,7 +50,6 @@
"test:e2e:obsidian:onboarding-invitation": "tsx test/e2e-obsidian/scripts/onboarding-invitation.ts",
"test:e2e:obsidian:dialog-mounts": "tsx test/e2e-obsidian/scripts/dialog-mounts.ts",
"test:e2e:obsidian:conflict-dialog-policy": "tsx test/e2e-obsidian/scripts/conflict-dialog-policy.ts",
"test:e2e:obsidian:revision-repair": "tsx test/e2e-obsidian/scripts/revision-repair.ts",
"test:e2e:obsidian:settings-ui": "tsx test/e2e-obsidian/scripts/settings-ui.ts",
"test:e2e:obsidian:review-harness": "tsx test/e2e-obsidian/scripts/review-harness.ts",
"test:e2e:obsidian:p2p-pane": "tsx test/e2e-obsidian/scripts/p2p-pane.ts",
@@ -71,7 +63,6 @@
"test:e2e:obsidian:startup-scan": "tsx test/e2e-obsidian/scripts/startup-scan.ts",
"test:e2e:obsidian:setup-uri-workflow": "tsx test/e2e-obsidian/scripts/setup-uri-workflow.ts",
"test:e2e:obsidian:two-vault-sync": "tsx test/e2e-obsidian/scripts/two-vault-sync.ts",
"test:e2e:obsidian:security-seed-reconnect": "tsx test/e2e-obsidian/scripts/security-seed-reconnect.ts",
"test:e2e:obsidian:hidden-file-snippet-sync": "tsx test/e2e-obsidian/scripts/hidden-file-snippet-sync.ts",
"test:e2e:obsidian:customisation-sync": "tsx test/e2e-obsidian/scripts/customisation-sync.ts",
"test:e2e:obsidian:setting-markdown-export": "tsx test/e2e-obsidian/scripts/setting-markdown-export.ts",
@@ -123,7 +114,7 @@
"@types/transform-pouch": "^1.0.6",
"@typescript-eslint/parser": "8.56.1",
"@vitest/coverage-v8": "^4.1.8",
"@vrtmrz/obsidian-test-session": "0.2.6",
"@vrtmrz/obsidian-test-session": "0.2.5",
"dotenv-cli": "^11.0.0",
"esbuild": "0.28.1",
"esbuild-plugin-inline-worker": "^0.1.1",
@@ -172,17 +163,15 @@
"@smithy/querystring-builder": "^4.2.9",
"@smithy/types": "^4.14.3",
"@smithy/util-retry": "^4.4.5",
"@vrtmrz/browser-ui-kit": "0.1.0",
"@vrtmrz/livesync-commonlib": "0.1.0",
"@vrtmrz/obsidian-plugin-kit": "0.1.3",
"@vrtmrz/ui-interactions": "0.1.2",
"@vrtmrz/livesync-commonlib": "0.1.0-rc.11",
"@vrtmrz/obsidian-plugin-kit": "0.1.2",
"diff-match-patch": "^1.0.5",
"fflate": "^0.8.2",
"idb": "^8.0.3",
"markdown-it": "^14.2.0",
"minimatch": "^10.2.5",
"obsidian": "^1.13.1",
"octagonal-wheels": "^0.1.52",
"octagonal-wheels": "^0.1.51",
"qrcode-generator": "^1.4.4",
"xxhash-wasm-102": "npm:xxhash-wasm@^1.0.2"
},
+130 -33
View File
@@ -1,39 +1,136 @@
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { _activeDocument } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { EVENT_PLUGIN_UNLOADED } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import { BrowserUiNotifications, createBrowserUi } from "@vrtmrz/browser-ui-kit";
import type { Confirm, ConfirmActionLayout } from "@vrtmrz/livesync-commonlib/compat/interfaces/Confirm";
import { createNativeElement } from "@/apps/browserDom";
import { renderMessageMarkdownInto } from "./ui/renderMessageMarkdown";
import { UiInteractionsConfirm } from "./UiInteractionsConfirm";
/**
* Compatibility facade consumed by Commonlib while browser presentation is
* implemented through Fancy Kit's neutral `UiInteractions` contract.
*/
export class BrowserConfirm<T extends ServiceContext> extends UiInteractionsConfirm {
readonly context: T;
import MessageBox from "./ui/MessageBox.svelte";
import TextInputBox from "./ui/TextInputBox.svelte";
import { mount } from "svelte";
import { promiseWithResolvers } from "octagonal-wheels/promises";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { _activeDocument, compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
function displayMessageBox<T, U extends string[]>(
message: string,
buttons: U,
title: string,
commit: (ret: U[number]) => T,
actionLayout: ConfirmActionLayout = "vertical"
): Promise<T> {
const el = createNativeElement(_activeDocument, "div");
const p = promiseWithResolvers<T>();
mount(MessageBox, {
target: el,
props: {
message,
buttons: buttons as string[],
title: title,
actionLayout,
commit: (action: U[number]) => {
const ret = commit(action);
p.resolve(ret);
},
},
});
_activeDocument.body.appendChild(el);
void p.promise.finally(() => {
el.remove();
});
return p.promise;
}
function promptForInput(
title: string,
key: string,
placeholder: string,
isPassword?: boolean
): Promise<string | false> {
const el = createNativeElement(_activeDocument, "div");
const p = promiseWithResolvers<string | false>();
mount(TextInputBox, {
target: el,
props: {
title,
message: key,
placeholder,
isPassword,
commit: (text: string | false) => {
p.resolve(text);
},
},
});
_activeDocument.body.appendChild(el);
void p.promise.finally(() => {
el.remove();
});
return p.promise;
}
export class BrowserConfirm<T extends ServiceContext> implements Confirm {
_context: T;
constructor(context: T) {
const dialogueController = new AbortController();
const notifications = new BrowserUiNotifications({
document: _activeDocument,
});
super({
ui: createBrowserUi({
document: _activeDocument,
signal: dialogueController.signal,
renderMarkdown: ({ container, markdown }) => {
renderMessageMarkdownInto(container, markdown);
},
}),
notifications,
createActionAnchor: () => createNativeElement(_activeDocument, "a"),
});
this.context = context;
context.events.onceEvent(EVENT_PLUGIN_UNLOADED, () => {
dialogueController.abort();
notifications.dispose();
});
this._context = context;
}
askYesNo(message: string): Promise<"yes" | "no"> {
return displayMessageBox(message, ["Yes", "No"] as const, "Confirm", (action) =>
action == "Yes" ? "yes" : "no"
);
}
askString(title: string, key: string, placeholder: string, isPassword?: boolean): Promise<string | false> {
return promptForInput(title, key, placeholder, isPassword);
}
askYesNoDialog(
message: string,
opt: { title?: string; defaultOption?: "Yes" | "No"; timeout?: number }
): Promise<"yes" | "no"> {
return displayMessageBox(message, ["Yes", "No"] as const, opt.title ?? "Confirm", (action) =>
action == "Yes" ? "yes" : "no"
);
}
askSelectString(message: string, items: string[]): Promise<string> {
return displayMessageBox(message, [...items] as const, "Confirm", (action) => action);
}
askSelectStringDialogue<T extends readonly string[]>(
message: string,
buttons: T,
opt: { title?: string; defaultAction: T[number]; timeout?: number }
): Promise<T[number] | false> {
return displayMessageBox(message, [...buttons] as const, opt.title ?? "Confirm", (action) => action);
}
askInPopup(
key: string,
dialogText: string,
anchorCallback: (anchor: HTMLAnchorElement) => void,
durationMs: number = 20000
): void {
const existing = _activeDocument.querySelector(`[data-livesync-popup="${CSS.escape(key)}"]`);
existing?.remove();
const notice = createNativeElement(_activeDocument, "div");
notice.className = "livesync-browser-notice";
notice.dataset.livesyncPopup = key;
const [beforeText, afterText] = dialogText.split("{HERE}", 2);
notice.append(beforeText);
const anchor = createNativeElement(_activeDocument, "a");
anchor.href = "#";
anchorCallback(anchor);
anchor.addEventListener("click", () => notice.remove());
notice.append(anchor, afterText ?? "");
_activeDocument.body.appendChild(notice);
compatGlobal.setTimeout(() => notice.remove(), durationMs);
}
confirmWithMessage(
title: string,
contentMd: string,
buttons: string[],
defaultAction: (typeof buttons)[number],
timeout?: number,
actionLayout?: ConfirmActionLayout
): Promise<(typeof buttons)[number] | false> {
return displayMessageBox(
contentMd,
[...buttons] as const,
title ?? "Confirm",
(action) => action,
actionLayout
);
}
}
-122
View File
@@ -1,122 +0,0 @@
import type {
KeyValueDatabase,
KeyValueDatabaseFactory,
} from "@vrtmrz/livesync-commonlib/compat/interfaces/KeyValueDatabase";
import { deleteDB, openDB, type IDBPDatabase } from "idb";
import { serialized } from "octagonal-wheels/concurrency/lock";
/** Creates an application-owned IndexedDB key-value factory for browser runtimes. */
export function createBrowserKeyValueDatabaseFactory(): KeyValueDatabaseFactory {
const cache = new Map<string, BrowserKeyValueDatabase>();
return async (databaseKey) =>
await serialized(`OpenBrowserKeyValueDatabase-${databaseKey}`, async () => {
const cached = cache.get(databaseKey);
if (cached && !cached.isDestroyed) {
return cached;
}
if (cached) {
await cached.ensuredDestroyed;
cache.delete(databaseKey);
}
const database = new BrowserKeyValueDatabase(databaseKey);
await database.getIsReady();
cache.set(databaseKey, database);
return database;
});
}
class BrowserKeyValueDatabase implements KeyValueDatabase {
private databasePromise?: Promise<IDBPDatabase<unknown>>;
private destroyed = false;
private destroyedPromise?: Promise<void>;
constructor(private readonly databaseKey: string) {}
get isDestroyed(): boolean {
return this.destroyed;
}
get ensuredDestroyed(): Promise<void> {
return this.destroyedPromise ?? Promise.resolve();
}
async getIsReady(): Promise<boolean> {
await this.ensureDatabase();
return !this.destroyed;
}
private ensureDatabase(): Promise<IDBPDatabase<unknown>> {
if (this.destroyed) {
throw new Error("Database is destroyed");
}
this.databasePromise ??= openDB(this.databaseKey, undefined, {
upgrade: (database) => {
if (!database.objectStoreNames.contains(this.databaseKey)) {
database.createObjectStore(this.databaseKey);
}
},
blocking: () => {
void this.closeDatabase();
},
terminated: () => {
this.databasePromise = undefined;
},
}).catch((error: unknown) => {
this.databasePromise = undefined;
throw error;
});
return this.databasePromise;
}
private get database(): Promise<IDBPDatabase<unknown>> {
return this.ensureDatabase();
}
private async closeDatabase(): Promise<void> {
const databasePromise = this.databasePromise;
this.databasePromise = undefined;
if (databasePromise) {
(await databasePromise).close();
}
}
async get<T>(key: IDBValidKey): Promise<T> {
return (await (await this.database).get(this.databaseKey, key)) as T;
}
async set<T>(key: IDBValidKey, value: T): Promise<IDBValidKey> {
await (await this.database).put(this.databaseKey, value, key);
return key;
}
async del(key: IDBValidKey): Promise<void> {
await (await this.database).delete(this.databaseKey, key);
}
async clear(): Promise<void> {
await (await this.database).clear(this.databaseKey);
}
async keys(query?: IDBValidKey | IDBKeyRange, count?: number): Promise<IDBValidKey[]> {
return await (await this.database).getAllKeys(this.databaseKey, query, count);
}
async close(): Promise<void> {
await this.closeDatabase();
}
async destroy(): Promise<void> {
if (this.destroyedPromise) {
await this.destroyedPromise;
return;
}
this.destroyed = true;
this.destroyedPromise = (async () => {
await this.closeDatabase();
await deleteDB(this.databaseKey);
})();
await this.destroyedPromise;
}
}
@@ -1,133 +0,0 @@
<script lang="ts">
import { onMount } from "svelte";
import type { P2PSyncSetting } from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { P2PReplicatorPaneHost } from "@/features/P2PSync/P2PReplicator/P2PReplicatorPaneHost";
interface Props {
host: P2PReplicatorPaneHost;
}
let { host }: Props = $props();
const currentSettings = () => host.services.setting.currentSettings() as P2PSyncSetting;
const initialSettings = currentSettings();
let savedTurnServers = $state(initialSettings.P2P_turnServers);
let savedTurnUsername = $state(initialSettings.P2P_turnUsername);
let savedTurnCredential = $state(initialSettings.P2P_turnCredential);
let turnServers = $state(initialSettings.P2P_turnServers);
let turnUsername = $state(initialSettings.P2P_turnUsername);
let turnCredential = $state(initialSettings.P2P_turnCredential);
const isTurnServersModified = $derived(turnServers !== savedTurnServers);
const isTurnUsernameModified = $derived(turnUsername !== savedTurnUsername);
const isTurnCredentialModified = $derived(turnCredential !== savedTurnCredential);
const isModified = $derived(
isTurnServersModified || isTurnUsernameModified || isTurnCredentialModified
);
function loadSettings(settings: P2PSyncSetting): void {
savedTurnServers = settings.P2P_turnServers;
savedTurnUsername = settings.P2P_turnUsername;
savedTurnCredential = settings.P2P_turnCredential;
turnServers = savedTurnServers;
turnUsername = savedTurnUsername;
turnCredential = savedTurnCredential;
}
onMount(() =>
host.services.context.events.onEvent("setting-saved", (settings) => {
loadSettings(settings as P2PSyncSetting);
})
);
async function save(): Promise<void> {
await host.services.setting.applyPartial(
{
P2P_turnServers: turnServers,
P2P_turnUsername: turnUsername,
P2P_turnCredential: turnCredential,
},
true
);
loadSettings(currentSettings());
}
function revert(): void {
turnServers = savedTurnServers;
turnUsername = savedTurnUsername;
turnCredential = savedTurnCredential;
}
</script>
<section class="browser-p2p-transport-settings">
<details>
<summary>Optional TURN server settings</summary>
<p>
Configure TURN only when a direct peer-to-peer connection cannot be established.
</p>
<label class:is-dirty={isTurnServersModified}>
<span>TURN Server URLs (comma-separated)</span>
<input
type="text"
placeholder="turn:turn.example.com:3478"
bind:value={turnServers}
autocomplete="off"
spellcheck="false"
autocorrect="off"
/>
</label>
<label class:is-dirty={isTurnUsernameModified}>
<span>TURN Username</span>
<input
type="text"
placeholder="Enter TURN username"
bind:value={turnUsername}
autocomplete="off"
/>
</label>
<label class:is-dirty={isTurnCredentialModified}>
<span>TURN Credential</span>
<input
type="password"
placeholder="Enter TURN credential"
bind:value={turnCredential}
autocomplete="new-password"
/>
</label>
<div class="actions">
<button type="button" class="button mod-cta" disabled={!isModified} onclick={save}>
Save TURN settings
</button>
<button type="button" class="button" disabled={!isModified} onclick={revert}>
Revert TURN settings
</button>
</div>
</details>
</section>
<style>
.browser-p2p-transport-settings {
margin-bottom: 1rem;
}
p {
margin: 0.75rem 0;
}
label {
display: grid;
gap: 0.25rem;
margin: 0.75rem 0;
}
label.is-dirty {
background-color: var(--background-modifier-error);
}
input {
box-sizing: border-box;
width: 100%;
}
.actions {
display: flex;
flex-wrap: wrap;
gap: 0.5rem;
}
</style>
+10 -24
View File
@@ -1,15 +1,15 @@
import {
type ComponentHasResult,
SvelteDialogManagerBase,
SvelteDialogMixIn,
} from "@vrtmrz/livesync-commonlib/compat/services/implements/base/SvelteDialog";
import { createNativeElement } from "@/apps/browserDom";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import type { SvelteDialogManagerDependencies } from "@vrtmrz/livesync-commonlib/compat/services/implements/base/SvelteDialog";
import { _activeDocument } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import DialogHost from "@/modules/services/LiveSyncUI/DialogHost.svelte";
import { SvelteDialogSession } from "@/modules/services/SvelteDialogSession";
export class BrowserModal {
export class ShimModal {
contentEl: HTMLElement;
titleEl: HTMLElement;
modalEl: HTMLElement;
@@ -45,14 +45,19 @@ export class BrowserModal {
}
onOpen() {}
onClose() {}
setPlaceholder(p: string) {}
setTitle(t: string) {
this.titleEl.textContent = t;
}
}
export class LiveSyncBrowserDialog<T, U, C extends ServiceContext = ServiceContext> extends BrowserModal {
private readonly session: SvelteDialogSession<T, U, C>;
const BrowserSvelteDialogBase = SvelteDialogMixIn(ShimModal, DialogHost);
export class LiveSyncBrowserDialog<T, U, C extends ServiceContext = ServiceContext> extends BrowserSvelteDialogBase<
T,
U,
C
> {
constructor(
context: C,
dependents: SvelteDialogManagerDependencies<C>,
@@ -60,26 +65,7 @@ export class LiveSyncBrowserDialog<T, U, C extends ServiceContext = ServiceConte
initialData?: U
) {
super();
this.session = new SvelteDialogSession({
surface: this,
context,
dependencies: dependents,
dialogHost: DialogHost,
component,
initialData,
});
}
override onOpen(): void {
this.session.onOpen();
}
override onClose(): void {
this.session.onClose();
}
waitForClose(): Promise<T | undefined> {
return this.session.waitForClose();
this.initDialog(context, dependents, component, initialData);
}
}
export class BrowserSvelteDialogManager<T extends ServiceContext> extends SvelteDialogManagerBase<T> {
@@ -1,110 +0,0 @@
import type { LOG_LEVEL } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import type { Confirm } from "@vrtmrz/livesync-commonlib/compat/interfaces/Confirm";
import type { ICommandCompat } from "@vrtmrz/livesync-commonlib/compat/services/base/IService";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { InjectableAPIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableAPIService";
import { FetchHttpHandler } from "@smithy/fetch-http-handler";
import { _fetch } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
declare const MANIFEST_VERSION: string | undefined;
declare const PACKAGE_VERSION: string | undefined;
export interface LiveSyncBrowserAPIServiceOptions {
confirm: Confirm;
getSystemVaultName(): string;
appId?: string;
isMobile?: () => boolean;
fetch?: typeof _fetch;
addLog?: (message: unknown, level: LOG_LEVEL, key?: string) => void;
addCommand?: <TCommand extends ICommandCompat>(command: TCommand) => TCommand;
showWindow?: (type: string) => Promise<void>;
registerWindow?: <T>(type: string, factory: (leaf: T) => unknown) => void;
addRibbonIcon?: (icon: string, title: string, callback: (event: MouseEvent) => unknown) => HTMLElement;
registerProtocolHandler?: (action: string, handler: (params: Record<string, string>) => unknown) => void;
addStatusBarItem?: () => HTMLElement | undefined;
}
/** Browser application implementation of Commonlib's injected host API contract. */
export class LiveSyncBrowserAPIService<T extends ServiceContext> extends InjectableAPIService<T> {
private readonly options: LiveSyncBrowserAPIServiceOptions;
constructor(context: T, options: LiveSyncBrowserAPIServiceOptions) {
super(context);
this.options = options;
this.addLog.setHandler((message, level, key) => {
options.addLog?.(message, level, key);
});
}
get confirm(): Confirm {
return this.options.confirm;
}
getCustomFetchHandler(): FetchHttpHandler {
return new FetchHttpHandler();
}
isMobile(): boolean {
return this.options.isMobile?.() ?? false;
}
showWindow(type: string): Promise<void> {
return this.options.showWindow?.(type) ?? Promise.resolve();
}
getAppID(): string {
return this.options.appId ?? this.options.getSystemVaultName();
}
getSystemVaultName(): string {
return this.options.getSystemVaultName();
}
override getPlatform(): string {
return "browser";
}
getAppVersion(): string {
return MANIFEST_VERSION ?? "0.0.0";
}
getPluginVersion(): string {
return PACKAGE_VERSION ?? "0.0.0";
}
addCommand<TCommand extends ICommandCompat>(command: TCommand): TCommand {
return this.options.addCommand?.(command) ?? command;
}
registerWindow<T>(type: string, factory: (leaf: T) => unknown): void {
this.options.registerWindow?.(type, factory);
}
addRibbonIcon(
icon: string,
title: string,
callback: (event: MouseEvent) => unknown
): HTMLElement {
const element = this.options.addRibbonIcon?.(icon, title, callback);
if (!element) {
throw new Error("Ribbon icons are not supported by this browser application");
}
return element;
}
registerProtocolHandler(
action: string,
handler: (params: Record<string, string>) => unknown
): void {
this.options.registerProtocolHandler?.(action, handler);
}
override nativeFetch(request: string | Request, options?: RequestInit): Promise<Response> {
const fetchImplementation = this.options.fetch ?? _fetch;
return fetchImplementation(request, options);
}
addStatusBarItem(): HTMLElement | undefined {
return this.options.addStatusBarItem?.();
}
}
+2 -14
View File
@@ -1,26 +1,14 @@
import type { BrowserServiceHostDependencies } from "@vrtmrz/livesync-commonlib/compat/services/BrowserServices";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import type { AppLifecycleService } from "@vrtmrz/livesync-commonlib/compat/services/base/AppLifecycleService";
import type { ConfigService } from "@vrtmrz/livesync-commonlib/compat/services/base/ConfigService";
import type { ControlService } from "@vrtmrz/livesync-commonlib/compat/services/base/ControlService";
import type { ReplicatorService } from "@vrtmrz/livesync-commonlib/compat/services/base/ReplicatorService";
import type { InjectableAPIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableAPIService";
import { UIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/base/UIService";
import DialogToCopy from "@/modules/services/LiveSyncUI/dialogues/DialogueToCopy.svelte";
import { BrowserSvelteDialogManager } from "./BrowserSvelteDialogManager";
export interface LiveSyncBrowserUIServiceDependencies<T extends ServiceContext> {
API: InjectableAPIService<T>;
appLifecycle: AppLifecycleService<T>;
config: ConfigService<T>;
control: ControlService<T>;
replicator: ReplicatorService<T>;
}
export class LiveSyncBrowserUIService<T extends ServiceContext> extends UIService<T> {
override get dialogToCopy() {
return DialogToCopy;
}
constructor(context: T, dependents: LiveSyncBrowserUIServiceDependencies<T>) {
constructor(context: T, dependents: BrowserServiceHostDependencies<T>) {
const browserConfirm = dependents.API.confirm;
const obsidianSvelteDialogManager = new BrowserSvelteDialogManager<T>(context, {
appLifecycle: dependents.appLifecycle,
-158
View File
@@ -1,158 +0,0 @@
import type {
Confirm,
ConfirmActionLayout,
} from "@vrtmrz/livesync-commonlib/compat/interfaces/Confirm";
import type { UiInteractions, UiNotifications } from "@vrtmrz/ui-interactions";
const DEFAULT_LABELS = {
confirmationTitle: "Confirmation",
selectionTitle: "Select",
yes: "Yes",
no: "No",
} as const;
export interface UiInteractionsConfirmOptions {
ui: UiInteractions;
notifications: UiNotifications;
createActionAnchor: () => HTMLAnchorElement;
}
function timeoutOption(timeoutSeconds?: number): { timeoutMs?: number } {
return timeoutSeconds !== undefined && timeoutSeconds > 0
? { timeoutMs: timeoutSeconds * 1_000 }
: {};
}
/** Adapts Commonlib's legacy confirmation contract to Fancy Kit capabilities. */
export class UiInteractionsConfirm implements Confirm {
readonly notifications: UiNotifications;
constructor(private readonly options: UiInteractionsConfirmOptions) {
this.notifications = options.notifications;
}
async askYesNo(message: string): Promise<"yes" | "no"> {
const result = await this.options.ui.confirmAction(
{
title: DEFAULT_LABELS.confirmationTitle,
message,
actions: ["yes", "no"],
labels: { yes: DEFAULT_LABELS.yes, no: DEFAULT_LABELS.no },
defaultAction: "no",
actionLayout: "vertical",
},
"legacy-confirm.ask-yes-no"
);
return result === "yes" ? "yes" : "no";
}
async askString(
title: string,
key: string,
placeholder: string,
isPassword = false
): Promise<string | false> {
const prompt = isPassword
? this.options.ui.promptPassword.bind(this.options.ui)
: this.options.ui.promptText.bind(this.options.ui);
const result = await prompt(
{
title,
label: key,
placeholder,
},
"legacy-confirm.ask-string"
);
return result ?? false;
}
async askYesNoDialog(
message: string,
opt: { title?: string; defaultOption?: "Yes" | "No"; timeout?: number } = {}
): Promise<"yes" | "no"> {
const result = await this.options.ui.confirmAction(
{
title: opt.title ?? DEFAULT_LABELS.confirmationTitle,
message,
actions: ["Yes", "No"],
labels: { Yes: DEFAULT_LABELS.yes, No: DEFAULT_LABELS.no },
defaultAction: opt.defaultOption ?? "No",
actionLayout: "vertical",
...timeoutOption(opt.timeout),
},
"legacy-confirm.ask-yes-no-dialog"
);
return result === "Yes" ? "yes" : "no";
}
async askSelectString(message: string, items: string[]): Promise<string> {
const result = await this.options.ui.pickOne(
{
items,
getText: (item) => item,
placeholder: message,
},
"legacy-confirm.ask-select-string"
);
return result ?? "";
}
async askSelectStringDialogue<T extends readonly string[]>(
message: string,
buttons: T,
opt: { title?: string; defaultAction: T[number]; timeout?: number }
): Promise<T[number] | false> {
const result = await this.options.ui.confirmAction(
{
title: opt.title ?? DEFAULT_LABELS.selectionTitle,
message,
actions: buttons,
defaultAction: opt.defaultAction,
actionLayout: "vertical",
...timeoutOption(opt.timeout),
},
"legacy-confirm.ask-select-string-dialogue"
);
return result ?? false;
}
askInPopup(
key: string,
dialogText: string,
anchorCallback: (anchor: HTMLAnchorElement) => void,
durationMs?: number
): void {
const anchor = this.options.createActionAnchor();
anchorCallback(anchor);
this.options.notifications.show(key, {
message: dialogText.replace("{HERE}", "").trim(),
action: {
label: anchor.textContent?.trim() || "Open",
onSelect: () => anchor.click(),
},
durationMs,
});
}
async confirmWithMessage(
title: string,
contentMd: string,
buttons: string[],
defaultAction: (typeof buttons)[number],
timeout?: number,
actionLayout?: ConfirmActionLayout
): Promise<(typeof buttons)[number] | false> {
const result = await this.options.ui.confirmAction(
{
title,
message: contentMd,
actions: buttons,
defaultAction,
actionLayout: actionLayout ?? "vertical",
...timeoutOption(timeout),
},
"legacy-confirm.confirm-with-message"
);
return result ?? false;
}
}
@@ -1,215 +1,37 @@
import type { ObsidianLiveSyncSettings } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { BrowserServiceHub, type BrowserServiceHost } from "@vrtmrz/livesync-commonlib/compat/services/BrowserServices";
import type { KeyValueDatabaseFactory } from "@vrtmrz/livesync-commonlib/compat/interfaces/KeyValueDatabase";
import { PouchDB } from "@vrtmrz/livesync-commonlib/compat/pouchdb/pouchdb-browser";
import { ConfigService } from "@vrtmrz/livesync-commonlib/compat/services/base/ConfigService";
import { ControlService } from "@vrtmrz/livesync-commonlib/compat/services/base/ControlService";
import { DatabaseService } from "@vrtmrz/livesync-commonlib/compat/services/base/DatabaseService";
import { KeyValueDBService } from "@vrtmrz/livesync-commonlib/compat/services/base/KeyValueDBService";
import type { ISettingService } from "@vrtmrz/livesync-commonlib/compat/services/base/IService";
import { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { InjectableAppLifecycleService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableAppLifecycleService";
import { InjectableConflictService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableConflictService";
import { InjectableDatabaseEventService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableDatabaseEventService";
import { InjectableFileProcessingService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableFileProcessingService";
import { PathServiceCompat } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectablePathService";
import { InjectableRemoteService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableRemoteService";
import { InjectableReplicationService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableReplicationService";
import { InjectableReplicatorService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableReplicatorService";
import { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import { InjectableSettingService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableSettingService";
import { InjectableTestService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableTestService";
import { InjectableTweakValueService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableTweakValueService";
import { InjectableVaultServiceCompat } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableVaultService";
import { setLang, translateLiveSyncMessage } from "@/common/translation";
import { BrowserAPIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/browser/BrowserAPIService";
import { BrowserConfirm } from "./BrowserConfirm";
import { createBrowserKeyValueDatabaseFactory } from "./BrowserKeyValueDatabase";
import {
LiveSyncBrowserAPIService,
type LiveSyncBrowserAPIServiceOptions,
} from "./LiveSyncBrowserAPIService";
import { LiveSyncBrowserUIService } from "./LiveSyncBrowserUIService";
import { setLang, translateLiveSyncMessage } from "@/common/translation";
export interface LiveSyncBrowserSettingsPersistence {
load(): Promise<ObsidianLiveSyncSettings | undefined>;
save(settings: ObsidianLiveSyncSettings): Promise<void>;
}
export interface LiveSyncBrowserRestartPolicy {
schedule(): void;
perform?: () => void;
ask?: (message?: string) => void;
isScheduled?: () => boolean;
}
export interface LiveSyncBrowserServiceHubOptions<T extends ServiceContext> {
export type LiveSyncBrowserServiceHubOptions<T extends ServiceContext> = {
context?: T;
getSystemVaultName?: () => string;
settings?: LiveSyncBrowserSettingsPersistence;
restart?: LiveSyncBrowserRestartPolicy;
openKeyValueDatabase?: KeyValueDatabaseFactory;
API?: Omit<LiveSyncBrowserAPIServiceOptions, "confirm" | "getSystemVaultName">;
}
};
class LiveSyncBrowserAppLifecycleService<
T extends ServiceContext,
> extends InjectableAppLifecycleService<T> {}
class LiveSyncBrowserDatabaseService<T extends ServiceContext> extends DatabaseService<T> {}
class LiveSyncBrowserKeyValueDBService<T extends ServiceContext> extends KeyValueDBService<T> {}
class LiveSyncBrowserConfigService<T extends ServiceContext> extends ConfigService<T> {
constructor(
context: T,
private readonly setting: ISettingService
) {
super(context);
}
getSmallConfig(key: string): string | null {
return this.setting.getSmallConfig(key);
}
setSmallConfig(key: string, value: string): void {
this.setting.setSmallConfig(key, value);
}
deleteSmallConfig(key: string): void {
this.setting.deleteSmallConfig(key);
}
}
/** LiveSync-owned service composition shared by WebApp and WebPeer. */
export class LiveSyncBrowserServiceHub<T extends ServiceContext> extends InjectableServiceHub<T> {
constructor(options: LiveSyncBrowserServiceHubOptions<T>) {
const context =
options.context ??
(new ServiceContext({
translate: translateLiveSyncMessage,
}) as T);
const API = new LiveSyncBrowserAPIService(context, {
...options.API,
confirm: new BrowserConfirm(context),
getSystemVaultName: options.getSystemVaultName ?? (() => "livesync-browser"),
});
const conflict = new InjectableConflictService(context);
const fileProcessing = new InjectableFileProcessingService(context);
const setting = new InjectableSettingService(context, {
APIService: API,
onDisplayLanguageChanged: setLang,
});
const settingsPersistence = options.settings;
setting.loadData.setHandler(
settingsPersistence
? () => settingsPersistence.load()
: () => Promise.resolve(undefined)
);
setting.saveData.setHandler(
settingsPersistence
? (settings) => settingsPersistence.save(settings)
: () => Promise.resolve()
);
const appLifecycle = new LiveSyncBrowserAppLifecycleService(context, {
settingService: setting,
});
const restartPolicy = options.restart;
const scheduleRestart = restartPolicy ? () => restartPolicy.schedule() : () => {};
appLifecycle.scheduleRestart.setHandler(scheduleRestart);
appLifecycle.performRestart.setHandler(
restartPolicy?.perform ? () => restartPolicy.perform?.() : scheduleRestart
);
appLifecycle.askRestart.setHandler(
restartPolicy?.ask ? (message) => restartPolicy.ask?.(message) : scheduleRestart
);
appLifecycle.isReloadingScheduled.setHandler(
restartPolicy?.isScheduled ? () => restartPolicy.isScheduled?.() ?? false : () => false
);
const databaseEvents = new InjectableDatabaseEventService(context);
const path = new PathServiceCompat(context, {
settingService: setting,
});
const vault = new InjectableVaultServiceCompat(context, {
settingService: setting,
APIService: API,
});
const database = new LiveSyncBrowserDatabaseService(context, {
pouchDB: PouchDB,
path,
vault,
setting,
API,
});
const config = new LiveSyncBrowserConfigService(context, setting);
const replicator = new InjectableReplicatorService(context, {
settingService: setting,
appLifecycleService: appLifecycle,
databaseEventService: databaseEvents,
});
const remote = new InjectableRemoteService(context, {
pouchDB: PouchDB,
APIService: API,
appLifecycle,
setting,
});
const replication = new InjectableReplicationService(context, {
APIService: API,
appLifecycleService: appLifecycle,
replicatorService: replicator,
settingService: setting,
fileProcessingService: fileProcessing,
databaseService: database,
});
const keyValueDB = new LiveSyncBrowserKeyValueDBService(context, {
openKeyValueDatabase:
options.openKeyValueDatabase ?? createBrowserKeyValueDatabaseFactory(),
appLifecycle,
databaseEvents,
vault,
});
const control = new ControlService(context, {
appLifecycleService: appLifecycle,
databaseService: database,
fileProcessingService: fileProcessing,
settingService: setting,
APIService: API,
replicatorService: replicator,
});
const ui = new LiveSyncBrowserUIService(context, {
API,
appLifecycle,
config,
control,
replicator,
});
super(context, {
API,
appLifecycle,
conflict,
config,
control,
database,
databaseEvents,
fileProcessing,
keyValueDB,
path,
remote,
replication,
replicator,
setting,
test: new InjectableTestService(context),
tweakValue: new InjectableTweakValueService(context),
ui,
vault,
});
}
function createLiveSyncBrowserHost<T extends ServiceContext>(): BrowserServiceHost<T> {
return {
createAPI(context) {
return new BrowserAPIService(context, {
confirm: new BrowserConfirm(context),
});
},
createUI(context, dependencies) {
return new LiveSyncBrowserUIService(context, dependencies);
},
};
}
export function createLiveSyncBrowserServiceHub<T extends ServiceContext>(
options: LiveSyncBrowserServiceHubOptions<T> = {}
): LiveSyncBrowserServiceHub<T> {
return new LiveSyncBrowserServiceHub(options);
): BrowserServiceHub<T> {
const context = options.context ?? (new ServiceContext({ translate: translateLiveSyncMessage }) as T);
return new BrowserServiceHub<T>({
...options,
context,
onDisplayLanguageChanged: setLang,
host: createLiveSyncBrowserHost<T>(),
});
}
@@ -1,20 +1,12 @@
import { createServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { afterEach, describe, expect, it, vi } from "vitest";
import { describe, expect, it } from "vitest";
import {
observeServiceComposition,
observeServiceContext,
SERVICE_CONTEXT_MEMBERS,
} from "../../../test/contracts/serviceContext";
import {
createLiveSyncBrowserServiceHub,
type LiveSyncBrowserServiceHubOptions,
} from "./createLiveSyncBrowserServiceHub";
afterEach(() => {
vi.unstubAllGlobals();
});
import { createLiveSyncBrowserServiceHub } from "./createLiveSyncBrowserServiceHub";
describe("LiveSync browser service context contract", () => {
it("preserves one injected context and its API results throughout the Webapp composition", () => {
@@ -33,57 +25,4 @@ describe("LiveSync browser service context contract", () => {
[]
);
});
it("binds browser identity, persistence, restart policy, and native Fetch while composing the hub", async () => {
const deviceLocalSettings = new Map<string, string>();
vi.stubGlobal("localStorage", {
getItem: (key: string) => deviceLocalSettings.get(key) ?? null,
setItem: (key: string, value: string) => deviceLocalSettings.set(key, value),
removeItem: (key: string) => deviceLocalSettings.delete(key),
clear: () => deviceLocalSettings.clear(),
});
const savedSettings: (typeof DEFAULT_SETTINGS)[] = [];
const restartCalls: string[] = [];
const nativeFetch = vi.fn(async () => new Response("ok"));
const options: LiveSyncBrowserServiceHubOptions<ReturnType<typeof createServiceContext>> = {
getSystemVaultName: () => "browser-vault",
settings: {
load: async () => ({
...DEFAULT_SETTINGS,
couchDB_DBNAME: "browser-database",
}),
save: async (settings) => {
savedSettings.push(settings);
},
},
restart: {
schedule: () => restartCalls.push("schedule"),
perform: () => restartCalls.push("perform"),
ask: (message) => restartCalls.push(`ask:${message ?? ""}`),
isScheduled: () => true,
},
API: {
fetch: nativeFetch as typeof fetch,
},
};
const hub = createLiveSyncBrowserServiceHub(options);
expect(hub.API.getSystemVaultName()).toBe("browser-vault");
expect(hub.API.getPlatform()).toBe("browser");
const response = await hub.API.nativeFetch("https://example.invalid/");
expect(await response.text()).toBe("ok");
expect(nativeFetch).toHaveBeenCalledWith("https://example.invalid/", undefined);
await hub.setting.loadSettings();
expect(hub.setting.currentSettings().couchDB_DBNAME).toBe("browser-database");
savedSettings.length = 0;
await hub.setting.saveSettingData();
expect(savedSettings).toHaveLength(1);
hub.appLifecycle.scheduleRestart();
hub.appLifecycle.performRestart();
hub.appLifecycle.askRestart("restart now");
expect(hub.appLifecycle.isReloadingScheduled()).toBe(true);
expect(restartCalls).toEqual(["schedule", "perform", "ask:restart now"]);
});
});
+137
View File
@@ -0,0 +1,137 @@
<script lang="ts">
import { renderMessageMarkdown } from "./renderMessageMarkdown";
type Props = {
title: string;
message: string;
buttons: string[];
actionLayout?: ConfirmActionLayout;
commit: (button: string) => void;
};
type ConfirmActionLayout = "auto" | "vertical";
let { title, message, buttons, actionLayout, commit }: Props = $props();
const renderedMessage = $derived(renderMessageMarkdown(message));
function handleEsc(event: KeyboardEvent) {
if (event.key === "Escape") {
commit("");
}
}
</script>
<popup>
<header>{title}</header>
<article><div class="msg">{@html renderedMessage}</div></article>
<div class:vertical={actionLayout === "vertical"} class="buttons">
{#each buttons as button}
<button onclick={() => commit(button)}>{button}</button>
{/each}
</div>
</popup>
<div class="background" onclick={() => commit("")} onkeydown={handleEsc} role="none"></div>
<style>
popup {
z-index: 1000;
position: fixed;
background: rgba(255, 255, 255, 0.8);
max-width: 70vw;
max-height: 80vh;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
min-width: 50vw;
min-height: 50vh;
backdrop-filter: blur(5px);
display: flex;
flex-direction: column;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
border: 1px solid var(--background-primary-alt);
justify-content: space-between;
}
popup header {
background: var(--background-primary);
color: var(--text-normal);
padding: 1em;
border-bottom: 1px solid var(--background-primary-alt);
font: size 1.4em;
}
popup article {
padding: 1em;
display: flex;
justify-content: center;
overflow-y: auto;
}
popup article .msg {
width: 100%;
line-height: 1.5;
overflow-wrap: anywhere;
}
popup article .msg :global(:first-child) {
margin-top: 0;
}
popup article .msg :global(:last-child) {
margin-bottom: 0;
}
popup article .msg :global(pre) {
overflow-x: auto;
padding: 0.75em;
border-radius: 4px;
background: var(--background-secondary);
}
popup article .msg :global(code) {
font-family: var(--font-monospace);
}
popup article .msg :global(blockquote) {
margin: 0;
padding-left: 1em;
border-left: 3px solid var(--background-modifier-border);
color: var(--text-muted);
}
popup article .msg :global(ul),
popup article .msg :global(ol) {
padding-left: 1.5em;
}
popup article .msg :global(table) {
width: 100%;
border-collapse: collapse;
}
popup article .msg :global(th),
popup article .msg :global(td) {
padding: 0.4em 0.6em;
border: 1px solid var(--background-modifier-border);
}
popup article .msg :global(a) {
color: var(--text-accent);
}
popup .buttons {
border-top: 1px solid var(--background-primary-alt);
display: flex;
justify-content: center;
align-items: center;
padding: 1em;
}
popup .buttons button {
margin: 0 0.5em;
background-color: var(--background-primary-alt);
}
popup .buttons.vertical {
align-items: stretch;
flex-direction: column;
}
popup .buttons.vertical button {
margin: 0.25em 0;
width: 100%;
}
popup ~ .background {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.125);
}
</style>
+126
View File
@@ -0,0 +1,126 @@
<script lang="ts">
type Props = {
title: string;
message: string;
initialText?: string;
placeholder?: string;
isPassword?: boolean;
commit: (text: string | false) => void;
};
const { title, message, commit, initialText, placeholder, isPassword }: Props = $props();
function initialTextSeed(): string {
return initialText ?? "";
}
let text = $state(initialTextSeed());
const type = $derived(isPassword ? "password" : "text");
function cancel() {
commit(false);
}
function handleKey(event: KeyboardEvent) {
if (event.key === "Escape") {
handleCancel(event);
} else if (event.key === "Enter") {
handleCommit(event);
}
}
function handleCancel(event: KeyboardEvent | MouseEvent) {
cancel();
event.preventDefault();
}
function handleCommit(event: KeyboardEvent | MouseEvent) {
commit(text);
event.preventDefault();
}
let textEl: HTMLInputElement;
$effect(() => {
textEl.focus();
});
</script>
<popup>
<header>{title}</header>
<article>
<div class="msg">{message}</div>
<div class="input">
<input
bind:this={textEl}
{type}
bind:value={text}
{placeholder}
onkeydown={handleKey}
onkeyup={handleKey}
/>
</div>
</article>
<div class="buttons">
<button onclick={handleCommit}>OK</button>
<button onclick={handleCancel}>Cancel</button>
</div>
</popup>
<div class="background" onclick={handleCancel} onkeydown={handleKey} role="none"></div>
<style>
popup {
z-index: 1000;
position: fixed;
background: rgba(255, 255, 255, 0.8);
max-width: 70vw;
max-height: 80vh;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
min-width: 50vw;
min-height: 50vh;
backdrop-filter: blur(5px);
display: flex;
flex-direction: column;
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
border: 1px solid var(--background-primary-alt);
justify-content: space-between;
}
popup header {
background: var(--background-primary);
color: var(--text-normal);
padding: 1em;
border-bottom: 1px solid var(--background-primary-alt);
font: size 1.4em;
}
popup article {
align-items: center;
padding: 1em;
display: flex;
justify-content: center;
flex-direction: column;
}
popup article .msg {
overflow-y: auto;
white-space: pre-wrap;
}
popup .buttons {
border-top: 1px solid var(--background-primary-alt);
display: flex;
justify-content: center;
align-items: center;
padding: 1em;
}
popup .buttons button {
margin: 0 0.5em;
background-color: var(--background-primary-alt);
}
popup ~ .background {
position: fixed;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: rgba(0, 0, 0, 0.125);
}
</style>
@@ -19,13 +19,3 @@ markdownRenderer.renderer.rules.link_open = (tokens, idx, options, env, self) =>
export function renderMessageMarkdown(message: string): string {
return markdownRenderer.render(message);
}
export function renderMessageMarkdownInto(container: HTMLElement, message: string): void {
const DOMParserConstructor = container.ownerDocument.defaultView?.DOMParser;
if (!DOMParserConstructor) {
container.textContent = message;
return;
}
const parsed = new DOMParserConstructor().parseFromString(renderMessageMarkdown(message), "text/html");
container.replaceChildren(...Array.from(parsed.body.childNodes));
}
+2 -2
View File
@@ -2,8 +2,8 @@
* Native DOM creation for browser applications which run outside Obsidian.
*
* Obsidian adds creation helpers to its own DOM environment. The standalone
* WebApp and WebPeer hosts use this native boundary instead of installing
* Obsidian-shaped prototype extensions.
* Webapp and WebPeer hosts do not own those prototype extensions, and the
* Webapp compatibility layer implements them on top of this native boundary.
*/
type NativeDocumentCreation = Pick<Document, "createElement" | "createDocumentFragment">;
+3 -3
View File
@@ -101,9 +101,9 @@ COPY --from=runtime-deps /deps/node_modules ./node_modules
# Copy the built CLI bundle from builder stage
COPY --from=builder /build/src/apps/cli/dist ./dist
# Install the entrypoint wrapper with a deterministic mode, regardless of
# source checkout permissions.
COPY --chmod=755 src/apps/cli/docker-entrypoint.sh /usr/local/bin/livesync-cli
# Install entrypoint wrapper
COPY src/apps/cli/docker-entrypoint.sh /usr/local/bin/livesync-cli
RUN chmod +x /usr/local/bin/livesync-cli
# Mount your vault / local database directory here
VOLUME ["/data"]
-11
View File
@@ -1,11 +0,0 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const dockerfile = readFileSync(new URL("./Dockerfile", import.meta.url), "utf8");
describe("CLI Docker image", () => {
it("sets a deterministic readable and executable entrypoint mode", () => {
expect(dockerfile).toContain("COPY --chmod=755 src/apps/cli/docker-entrypoint.sh /usr/local/bin/livesync-cli");
expect(dockerfile).not.toContain("RUN chmod +x /usr/local/bin/livesync-cli");
});
});
+2 -2
View File
@@ -1,7 +1,7 @@
{
"name": "self-hosted-livesync-cli",
"private": true,
"version": "1.0.1-cli",
"version": "1.0.0-beta.3-cli",
"main": "dist/index.cjs",
"type": "module",
"scripts": {
@@ -37,7 +37,7 @@
"dependencies": {
"chokidar": "^4.0.0",
"minimatch": "^10.2.5",
"octagonal-wheels": "^0.1.52",
"octagonal-wheels": "^0.1.51",
"pouchdb-adapter-http": "^9.0.0",
"pouchdb-adapter-leveldb": "^9.0.0",
"pouchdb-core": "^9.0.0",
@@ -1,11 +0,0 @@
import { readFileSync } from "node:fs";
import { describe, expect, it } from "vitest";
const setupPutCatHelper = readFileSync(new URL("./test/test-setup-put-cat-linux.sh", import.meta.url), "utf8");
describe("CLI setup URI E2E helper", () => {
it("evaluates Commonlib package imports as ESM", () => {
expect(setupPutCatHelper).toContain("node --input-type=module -e");
expect(setupPutCatHelper).not.toContain("npx tsx -e");
});
});
@@ -27,7 +27,7 @@ cli_test_init_settings_file "$SETTINGS_FILE"
echo "[INFO] creating setup URI from settings"
SETUP_URI="$(
SETTINGS_FILE="$SETTINGS_FILE" SETUP_PASSPHRASE="$SETUP_PASSPHRASE" node --input-type=module -e '
SETTINGS_FILE="$SETTINGS_FILE" SETUP_PASSPHRASE="$SETUP_PASSPHRASE" npx tsx -e '
import { fs } from "@vrtmrz/livesync-commonlib/node";
import { encodeSettingsToSetupURI } from "@vrtmrz/livesync-commonlib/compat/API/processSetting";
(async () => {
+161 -75
View File
@@ -1,112 +1,198 @@
# Self-hosted LiveSync WebApp
# LiveSync WebApp
Browser-based implementation of Self-hosted LiveSync using the FileSystem API.
Note: (I vrtmrz have not tested this so much yet).
WebApp is an experimental proof of concept for running Self-hosted LiveSync against a browser-authorised local Vault. It is not a replacement for the Obsidian plug-in, and it does not provide the plug-in's settings screen, command palette, or setup wizard.
## Features
## Capabilities
- Runs as a static application in the browser.
- Reads and writes a user-selected Vault through the File System Access API.
- Keeps previously selected directory handles in origin-scoped IndexedDB.
- Loads and saves LiveSync settings in `.livesync/settings.json` inside the selected Vault.
- Uses CouchDB as its primary continuous remote.
- Provides optional P2P controls as a secondary connection.
- Scans the Vault on start-up and when **Scan local files** is selected.
- Watches external file changes automatically when `FileSystemObserver` is available.
The WebApp itself does not require an application server, but synchronisation still requires a compatible remote or P2P peer.
- 🌐 Runs entirely in the browser
- 📁 Uses FileSystem API to access your local vault
- 🔄 Syncs with CouchDB, Object Storage server (compatible with Self-hosted LiveSync plug-in)
- 🚫 No server-side code required!!
- 💾 Settings stored in `.livesync/settings.json` within your vault
- 👁️ Real-time file watching (Chrome 124+ with FileSystemObserver)
## Requirements
WebApp requires:
- **FileSystem API support**:
- Chrome/Edge 86+ (required)
- Opera 72+ (required)
- Safari 15.2+ (experimental, limited support)
- Firefox: Not supported yet
- a secure context, such as HTTPS or `localhost`;
- `showDirectoryPicker()` and read-write access to the selected directory;
- IndexedDB for saved directory handles and the local database; and
- a CouchDB server which permits requests from the WebApp origin when CouchDB is used.
- **FileSystemObserver support** (optional, for real-time file watching):
- Chrome 124+ (recommended)
- Without this, files are only scanned on startup
Automated browser coverage uses Chromium. Other browsers, background execution, and remote types other than CouchDB remain experimental. Use feature detection rather than relying on a fixed browser-version list.
## Getting Started
## Use
### Installation
Serve the built files over HTTPS, or from `localhost`, then open `webapp.html`.
```bash
# Install dependencies (ensure you are in repository root directory, not src/apps/cli)
# due to shared dependencies with webapp and main library
npm install
```
1. Select the local Vault root.
2. Create or edit `.livesync/settings.json` in that Vault.
3. Reload WebApp to apply the settings.
### Development
For example, a minimal manually configured CouchDB connection includes:
From the repository root:
```bash
npm run dev -w livesync-webapp
```
Or from the package directory:
```bash
cd src/apps/webapp
npm run dev
```
This will start a development server at `http://localhost:3000`.
### Build
From the repository root:
```bash
npm run build -w livesync-webapp
```
Or from the package directory:
```bash
cd src/apps/webapp
npm run build
```
The built files will be in the `dist` directory.
### Usage
1. Open the webapp in your browser (`webapp.html`)
2. Select a vault from history or grant access to a new directory
3. Configure CouchDB connection by editing `.livesync/settings.json` in your vault
- You can also copy data.json from Obsidian's plug-in folder.
Example `.livesync/settings.json`:
```json
{
"couchDB_URI": "https://couchdb.example.com",
"couchDB_USER": "username",
"couchDB_PASSWORD": "password",
"couchDB_DBNAME": "vault",
"couchDB_URI": "https://your-couchdb-server.com",
"couchDB_USER": "your-username",
"couchDB_PASSWORD": "your-password",
"couchDB_DBNAME": "your-database",
"isConfigured": true,
"liveSync": true,
"syncOnSave": true
}
```
The settings must match the remote database, including any encryption and compatibility settings which are already in use. The file may contain credentials and passphrases, so protect it accordingly. Review individual values instead of copying an Obsidian `data.json` file wholesale.
After editing, reload the page.
### Optional P2P
## Architecture
The visible P2P controls are optional. Saving them does not select P2P as the main remote or mark an otherwise unconfigured Vault as configured. Use **Scan local files** before offering existing Vault content to a peer when automatic file observation is unavailable.
### Directory Structure
```
webapp/
├── adapters/ # FileSystem API adapters
│ ├── FSAPITypes.ts
│ ├── FSAPIPathAdapter.ts
│ ├── FSAPITypeGuardAdapter.ts
│ ├── FSAPIConversionAdapter.ts
│ ├── FSAPIStorageAdapter.ts
│ ├── FSAPIVaultAdapter.ts
│ └── FSAPIFileSystemAdapter.ts
├── managers/ # Event managers
│ ├── FSAPIStorageEventManagerAdapter.ts
│ └── StorageEventManagerFSAPI.ts
├── serviceModules/ # Service implementations
│ ├── FileAccessFSAPI.ts
│ ├── ServiceFileAccessImpl.ts
│ ├── DatabaseFileAccess.ts
│ └── FSAPIServiceModules.ts
├── bootstrap.ts # Vault picker + startup orchestration
├── main.ts # LiveSync core bootstrap (after vault selected)
├── vaultSelector.ts # FileSystem handle history and permission flow
├── webapp.html # Main HTML entry
├── index.html # Redirect entry for compatibility
├── package.json
├── vite.config.ts
└── README.md
```
### Key Components
1. **Adapters**: Implement `IFileSystemAdapter` interface using FileSystem API
2. **Managers**: Handle storage events and file watching
3. **Service Modules**: Integrate with LiveSyncBaseCore
4. **Main**: Application initialisation and lifecycle management
### Service Hub
Uses `BrowserServiceHub` which provides:
- Database service (IndexedDB via PouchDB)
- Settings service (file-based in `.livesync/settings.json`)
- Replication service
- File processing service
- And more...
## Limitations
- There is no general settings screen, setup wizard, or command palette.
- Object Storage and other main-remote configurations remain experimental and are not covered by the current WebApp browser tests.
- Without `FileSystemObserver`, external changes are detected only by a start-up or manual scan.
- A browser may suspend the page, discard permissions, or evict origin-scoped storage.
- The File System Access API is not available in every browser.
- Customisation Sync and Obsidian-specific Vault features are unavailable.
- **Real-time file watching**: Requires Chrome 124+ with FileSystemObserver
- Without it, changes are only detected on manual refresh
- **Performance**: Slower than native file system access
- **Permissions**: Requires user to grant directory access (cached via IndexedDB)
- **Browser support**: Limited to browsers with FileSystem API support
## Differences from CLI Version
- Uses `BrowserServiceHub` instead of `HeadlessServiceHub`
- Uses FileSystem API instead of Node.js `fs`
- Settings stored in `.livesync/settings.json` in vault
- Real-time file watching only with FileSystemObserver (Chrome 124+)
## Differences from Obsidian Plug-in
- No Obsidian-specific modules (UI, settings dialogue, etc.)
- Simplified configuration
- No plug-in/theme sync features
- No internal file handling (`.obsidian` folder)
## Development Notes
- TypeScript configuration: Uses project's tsconfig.json
- Module resolution: Aliased paths via Vite config
- External dependencies: Bundled by Vite
## Troubleshooting
### A Vault cannot be opened
### "Failed to get directory access"
- Confirm that WebApp is served over HTTPS or from `localhost`.
- Grant read-write access when the browser prompts.
- If a saved handle no longer has permission, select **Choose new vault folder** and choose the same directory again.
- Make sure you're using a supported browser
- Try refreshing the page
- Clear browser cache and IndexedDB
### Settings are not loaded
### "Settings not found"
- Confirm that `.livesync/settings.json` exists under the selected Vault root.
- Confirm that the file contains one valid JSON object.
- Reload WebApp after editing the file.
- Check the status line and browser console for the first reported error.
- Check that `.livesync/settings.json` exists in your vault directory
- Verify the JSON format is valid
- Create the file manually if needed
### External file changes are not detected
### "File watching not working"
- Select **Scan local files** after making changes outside WebApp.
- Check whether the browser provides `FileSystemObserver`.
- Keep the page open while automatic watching or synchronisation is expected.
- Make sure you're using Chrome 124 or later
- Check browser console for FileSystemObserver messages
- Try manually triggering sync if automatic watching isn't available
### CouchDB does not synchronise
### "Sync not working"
- Confirm the URL, credentials, database name, encryption settings, and compatibility settings.
- Confirm that CouchDB permits requests from the WebApp origin.
- Check the browser network inspector and console for the rejected request.
- Verify CouchDB credentials
- Check browser console for errors
- Ensure CouchDB server is accessible (CORS enabled)
## Development
## License
From the repository root:
```bash
npm run dev --workspace livesync-webapp
npm run build --workspace livesync-webapp
npm run test:unit --workspace livesync-webapp
npm run test:browser --workspace livesync-webapp
```
The production files are written to `src/apps/webapp/dist/`. App-owned unit tests are stored in `test/apps/webapp/`, outside the Community Review source boundary. The browser test builds the production artefact, selects an OPFS-backed test Vault in Chromium, and verifies start-up and P2P setting isolation.
## Composition
`WebAppRuntime.ts` owns the LiveSync service composition and lifecycle. `main.ts` owns Vault selection and the small browser shell, while `adapters/`, `managers/`, and `serviceModules/` provide the File System Access API boundary.
## Licence
The same licence as the main Self-hosted LiveSync project applies.
Same as the main Self-hosted LiveSync project.
-47
View File
@@ -1,47 +0,0 @@
<script lang="ts">
import P2PReplicatorPane from "@/features/P2PSync/P2PReplicator/P2PReplicatorPane.svelte";
import BrowserP2PTransportSettings from "@/apps/browser/BrowserP2PTransportSettings.svelte";
import type { WebAppRuntime } from "./WebAppRuntime";
interface Props {
runtime: WebAppRuntime;
}
let { runtime }: Props = $props();
let isScanning = $state(false);
let scanStatus = $state("");
async function scanLocalFiles() {
isScanning = true;
scanStatus = "Scanning local files…";
try {
scanStatus = (await runtime.scanLocalFiles())
? "Local files are ready for synchronisation."
: "The local file scan could not be completed.";
} catch (error) {
scanStatus = `The local file scan failed: ${String(error)}`;
} finally {
isScanning = false;
}
}
</script>
<div class="local-file-actions">
<button type="button" disabled={isScanning} onclick={scanLocalFiles}>Scan local files</button>
<span role="status" aria-live="polite">{scanStatus}</span>
</div>
<BrowserP2PTransportSettings host={runtime.p2pPaneHost} />
<P2PReplicatorPane
host={runtime.p2pPaneHost}
/>
<style>
.local-file-actions {
display: flex;
align-items: center;
flex-wrap: wrap;
gap: 0.75rem;
margin-bottom: 1rem;
}
</style>
-329
View File
@@ -1,329 +0,0 @@
/** Browser runtime for Self-hosted LiveSync over the File System Access API. */
import { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
import { ServiceContext, type LiveSyncEventHub } from "@vrtmrz/livesync-commonlib/context";
import { initialiseServiceModulesFSAPI, type FSAPIServiceModules } from "./serviceModules/FSAPIServiceModules";
import {
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
type LOG_LEVEL,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
collectFilesOnStorage,
updateToDatabase,
useOfflineScanner,
} from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/offlineScanner";
import { useRedFlagFeatures } from "@/serviceFeatures/redFlag";
import { useCheckRemoteSize } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/checkRemoteSize";
import { useRemoteConfiguration } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/remoteConfig";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import {
createLiveSyncBrowserServiceHub,
type LiveSyncBrowserServiceHub,
type LiveSyncBrowserServiceHubOptions,
} from "@/apps/browser/createLiveSyncBrowserServiceHub";
import type { P2PReplicatorPaneHost } from "@/features/P2PSync/P2PReplicator/P2PReplicatorPaneHost";
const SETTINGS_DIR = ".livesync";
const SETTINGS_FILE = "settings.json";
const DEFAULT_SETTINGS: Partial<ObsidianLiveSyncSettings> = {
liveSync: false,
syncOnSave: true,
syncOnStart: false,
savingDelay: 200,
lessInformationInLog: false,
gcDelay: 0,
periodicReplication: false,
periodicReplicationInterval: 60,
isConfigured: false,
// CouchDB settings - user needs to configure these
couchDB_URI: "",
couchDB_USER: "",
couchDB_PASSWORD: "",
couchDB_DBNAME: "",
// Disable features which are not available in the WebApp.
usePluginSync: false,
autoSweepPlugins: false,
autoSweepPluginsPeriodic: false,
};
export type WebAppRuntimeStatusKind = "info" | "warning" | "error" | "success";
export interface WebAppRuntimeOptions {
reportStatus?: (kind: WebAppRuntimeStatusKind, message: string) => void;
scheduleReload?: (delayMilliseconds: number) => void;
}
export class WebAppRuntime {
private readonly rootHandle: FileSystemDirectoryHandle;
private readonly reportStatus: NonNullable<WebAppRuntimeOptions["reportStatus"]>;
private readonly scheduleReload: NonNullable<WebAppRuntimeOptions["scheduleReload"]>;
private core: LiveSyncBaseCore<ServiceContext, never> | null = null;
private serviceHub: LiveSyncBrowserServiceHub<ServiceContext> | null = null;
private platformServiceModules: FSAPIServiceModules | null = null;
private p2p: UseP2PReplicatorResult | null = null;
private paneHost: P2PReplicatorPaneHost | null = null;
private restartScheduled = false;
constructor(rootHandle: FileSystemDirectoryHandle, options: WebAppRuntimeOptions = {}) {
this.rootHandle = rootHandle;
this.reportStatus = options.reportStatus ?? (() => {});
this.scheduleReload =
options.scheduleReload ??
((delayMilliseconds) => {
compatGlobal.setTimeout(() => {
compatGlobal.location.reload();
}, delayMilliseconds);
});
}
private addLog(message: unknown, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string): void {
this.serviceHub?.API.addLog(message, level, key);
}
private createServiceOptions(): LiveSyncBrowserServiceHubOptions<ServiceContext> {
return {
getSystemVaultName: () => this.rootHandle.name || "livesync-webapp",
settings: {
save: async (data) => {
try {
await this.saveSettingsToFile(data);
this.addLog("Saved to .livesync/settings.json", LOG_LEVEL_VERBOSE, "settings");
} catch (error) {
this.addLog(`Failed to save settings: ${String(error)}`, LOG_LEVEL_NOTICE, "settings");
}
},
load: async () => {
try {
const data = await this.loadSettingsFromFile();
if (data) {
this.addLog("Loaded from .livesync/settings.json", LOG_LEVEL_VERBOSE, "settings");
return { ...DEFAULT_SETTINGS, ...data } as ObsidianLiveSyncSettings;
}
} catch {
this.addLog("Failed to load settings; using defaults", LOG_LEVEL_NOTICE, "settings");
}
return DEFAULT_SETTINGS as ObsidianLiveSyncSettings;
},
},
restart: {
schedule: () => this.scheduleRestart(),
perform: () => this.scheduleRestart(),
ask: () => this.scheduleRestart(),
isScheduled: () => this.restartScheduled,
},
};
}
private scheduleRestart(): void {
if (this.restartScheduled) {
return;
}
this.restartScheduled = true;
void (async () => {
this.addLog("Restart requested", LOG_LEVEL_INFO, "app-lifecycle");
await this.shutdown();
this.scheduleReload(1000);
})();
}
get events(): LiveSyncEventHub {
if (!this.serviceHub) {
throw new Error("The WebApp service hub is not initialised");
}
return this.serviceHub.context.events;
}
get p2pPaneHost(): P2PReplicatorPaneHost {
if (!this.paneHost) {
throw new Error("The WebApp P2P pane host is not initialised");
}
return this.paneHost;
}
async scanLocalFiles(): Promise<boolean> {
const core = this.core;
const fileAccess = this.platformServiceModules?.vaultAccess;
if (!core || !fileAccess) {
throw new Error("The WebApp core is not initialised");
}
fileAccess.fsapiAdapter.clearCache();
await fileAccess.fsapiAdapter.scanDirectory();
const log = (message: unknown, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string): void => {
this.addLog(message, level, key);
};
const settings = core.services.setting.currentSettings();
const { storageFileNameMap, storageFileNames } = await collectFilesOnStorage(core, settings, log);
let succeeded = true;
for (const path of storageFileNames) {
try {
await updateToDatabase(core, log, LOG_LEVEL_INFO, storageFileNameMap[path]);
} catch (error) {
succeeded = false;
this.addLog(`Failed to import ${path}: ${String(error)}`, LOG_LEVEL_NOTICE, "scan");
}
}
return succeeded;
}
async start(): Promise<void> {
if (this.core) {
throw new Error("The WebApp runtime has already been started");
}
// Create service context and hub
this.serviceHub = createLiveSyncBrowserServiceHub<ServiceContext>(this.createServiceOptions());
this.addLog("Self-hosted LiveSync WebApp", LOG_LEVEL_INFO, "initialise");
this.addLog("Initialising...", LOG_LEVEL_VERBOSE, "initialise");
this.addLog(`Vault directory: ${this.rootHandle.name}`, LOG_LEVEL_VERBOSE, "initialise");
// Create LiveSync core
this.core = new LiveSyncBaseCore<ServiceContext, never>(
this.serviceHub,
(core, serviceHub) => {
const serviceModules = initialiseServiceModulesFSAPI(this.rootHandle, core, serviceHub);
this.platformServiceModules = serviceModules;
return serviceModules;
},
() => [],
() => [] as never[], // No add-ons
(core) => {
useOfflineScanner(core);
useRedFlagFeatures(core);
useCheckRemoteSize(core);
useRemoteConfiguration(core);
this.p2p = useP2PReplicatorFeature(core);
this.paneHost = {
services: core.services,
p2p: this.p2p,
};
}
);
try {
await this.startCore();
} catch (error) {
try {
await this.shutdown();
} catch (shutdownError) {
this.addLog(`Failed to clean up after start failure: ${String(shutdownError)}`, LOG_LEVEL_NOTICE);
}
throw error;
}
}
private async saveSettingsToFile(data: ObsidianLiveSyncSettings): Promise<void> {
// Create .livesync directory if it does not exist
const livesyncDir = await this.rootHandle.getDirectoryHandle(SETTINGS_DIR, { create: true });
// Create/overwrite settings.json
const fileHandle = await livesyncDir.getFileHandle(SETTINGS_FILE, { create: true });
const writable = await fileHandle.createWritable();
await writable.write(JSON.stringify(data, null, 2));
await writable.close();
}
private async loadSettingsFromFile(): Promise<Partial<ObsidianLiveSyncSettings> | null> {
try {
const livesyncDir = await this.rootHandle.getDirectoryHandle(SETTINGS_DIR);
const fileHandle = await livesyncDir.getFileHandle(SETTINGS_FILE);
const file = await fileHandle.getFile();
const text = await file.text();
const parsed: unknown = JSON.parse(text);
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
throw new Error("The WebApp settings file does not contain an object");
}
return parsed;
} catch {
// The file does not exist yet.
return null;
}
}
private async startCore(): Promise<void> {
if (!this.core) {
throw new Error("Core not initialised");
}
try {
this.addLog("Initialising LiveSync...", LOG_LEVEL_INFO, "start");
const loadResult = await this.core.services.control.onLoad();
if (!loadResult) {
this.addLog("Failed to initialise LiveSync", LOG_LEVEL_NOTICE, "start");
throw new Error("Failed to initialise LiveSync");
}
await this.core.services.control.onReady();
this.addLog("LiveSync is running", LOG_LEVEL_INFO, "ready");
// Check if configured
const settings = this.core.services.setting.currentSettings();
if (!settings.isConfigured) {
this.addLog("LiveSync is not configured yet", LOG_LEVEL_NOTICE, "configuration");
this.showWarning("Please configure CouchDB connection in settings");
} else {
this.addLog("LiveSync is configured and ready", LOG_LEVEL_INFO, "configuration");
this.addLog(`Database: ${settings.couchDB_DBNAME}`, LOG_LEVEL_VERBOSE, "configuration");
this.showSuccess("LiveSync is ready!");
}
// Scan the directory to populate file cache
const fileAccess = this.platformServiceModules?.vaultAccess;
if (fileAccess) {
this.addLog("Scanning vault directory...", LOG_LEVEL_VERBOSE, "scan");
await fileAccess.fsapiAdapter.scanDirectory();
const files = await fileAccess.fsapiAdapter.getFiles();
this.addLog(`Found ${files.length} files`, LOG_LEVEL_VERBOSE, "scan");
}
} catch (error) {
this.addLog(`Failed to start: ${String(error)}`, LOG_LEVEL_NOTICE, "start");
this.showError(`Failed to start: ${String(error)}`);
throw error;
}
}
async shutdown(): Promise<void> {
const core = this.core;
if (!core) {
return;
}
this.core = null;
this.paneHost = null;
this.addLog("Shutting down...", LOG_LEVEL_INFO, "shutdown");
const storageEventManager = this.platformServiceModules?.storageEventManager;
this.platformServiceModules = null;
try {
if (storageEventManager) {
await storageEventManager.cleanup();
}
} finally {
await core.services.control.onUnload();
this.p2p = null;
this.addLog("Shutdown complete", LOG_LEVEL_INFO, "shutdown");
this.serviceHub = null;
}
}
private showError(message: string): void {
this.reportStatus("error", `Error: ${message}`);
}
private showWarning(message: string): void {
this.reportStatus("warning", `Warning: ${message}`);
}
private showSuccess(message: string): void {
this.reportStatus("success", message);
}
}
+144
View File
@@ -0,0 +1,144 @@
import { LiveSyncWebApp } from "./main";
import { createNativeElement } from "@/apps/browserDom";
import { VaultHistoryStore, type VaultHistoryItem } from "./vaultSelector";
import { compatGlobal, _activeDocument } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
const historyStore = new VaultHistoryStore();
let app: LiveSyncWebApp | null = null;
type LiveSyncWebAppDebugApi = {
getApp: () => LiveSyncWebApp | null;
historyStore: VaultHistoryStore;
};
type LiveSyncWebAppGlobal = typeof compatGlobal & {
livesyncApp?: LiveSyncWebAppDebugApi;
};
function getRequiredElement<T extends HTMLElement>(id: string): T {
const element = _activeDocument.getElementById(id);
if (!element) {
throw new Error(`Missing element: #${id}`);
}
return element as T;
}
function setStatus(kind: "info" | "warning" | "error" | "success", message: string): void {
const statusEl = getRequiredElement<HTMLDivElement>("status");
statusEl.className = kind;
statusEl.textContent = message;
}
function setBusyState(isBusy: boolean): void {
const pickNewBtn = getRequiredElement<HTMLButtonElement>("pick-new-vault");
pickNewBtn.disabled = isBusy;
const historyButtons = _activeDocument.querySelectorAll<HTMLButtonElement>(".vault-item button");
historyButtons.forEach((button) => {
button.disabled = isBusy;
});
}
function formatLastUsed(unixMillis: number): string {
if (!unixMillis) {
return "unknown";
}
return new Date(unixMillis).toLocaleString();
}
async function renderHistoryList(): Promise<VaultHistoryItem[]> {
const listEl = getRequiredElement<HTMLDivElement>("vault-history-list");
const emptyEl = getRequiredElement<HTMLParagraphElement>("vault-history-empty");
const [items, lastUsedId] = await Promise.all([historyStore.getVaultHistory(), historyStore.getLastUsedVaultId()]);
listEl.replaceChildren();
emptyEl.classList.toggle("is-hidden", items.length > 0);
for (const item of items) {
const row = createNativeElement(_activeDocument, "div");
row.className = "vault-item";
const info = createNativeElement(_activeDocument, "div");
info.className = "vault-item-info";
const name = createNativeElement(_activeDocument, "div");
name.className = "vault-item-name";
name.textContent = item.name;
const meta = createNativeElement(_activeDocument, "div");
meta.className = "vault-item-meta";
const label = item.id === lastUsedId ? "Last used" : "Used";
meta.textContent = `${label}: ${formatLastUsed(item.lastUsedAt)}`;
info.append(name, meta);
const useButton = createNativeElement(_activeDocument, "button");
useButton.type = "button";
useButton.textContent = "Use this vault";
useButton.addEventListener("click", () => {
void startWithHistory(item);
});
row.append(info, useButton);
listEl.appendChild(row);
}
return items;
}
async function startWithHandle(handle: FileSystemDirectoryHandle): Promise<void> {
setStatus("info", `Starting LiveSync with vault: ${handle.name}`);
app = new LiveSyncWebApp(handle);
await app.initialize();
const selectorEl = getRequiredElement<HTMLDivElement>("vault-selector");
selectorEl.classList.add("is-hidden");
}
async function startWithHistory(item: VaultHistoryItem): Promise<void> {
setBusyState(true);
try {
const handle = await historyStore.activateHistoryItem(item);
await startWithHandle(handle);
} catch (error) {
setStatus("error", `Failed to open saved vault: ${String(error)}`);
setBusyState(false);
}
}
async function startWithNewPicker(): Promise<void> {
setBusyState(true);
try {
const handle = await historyStore.pickNewVault();
await startWithHandle(handle);
} catch (error) {
setStatus("warning", `Vault selection was cancelled or failed: ${String(error)}`);
setBusyState(false);
}
}
async function initializeVaultSelector(): Promise<void> {
setStatus("info", "Select a vault folder to start LiveSync.");
const pickNewBtn = getRequiredElement<HTMLButtonElement>("pick-new-vault");
pickNewBtn.addEventListener("click", () => {
void startWithNewPicker();
});
await renderHistoryList();
}
compatGlobal.addEventListener("load", () => {
initializeVaultSelector().catch((error) => {
setStatus("error", `Initialization failed: ${String(error)}`);
});
});
compatGlobal.addEventListener("beforeunload", () => {
void app?.shutdown();
});
(compatGlobal as LiveSyncWebAppGlobal).livesyncApp = {
getApp: () => app,
historyStore,
};
+253 -150
View File
@@ -1,172 +1,275 @@
import { createNativeElement } from "@/apps/browserDom";
/**
* Self-hosted LiveSync WebApp
* Browser-based version of Self-hosted LiveSync plugin using FileSystem API
*/
import type { BrowserServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/BrowserServices";
import { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
import { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import { initialiseServiceModulesFSAPI, type FSAPIServiceModules } from "./serviceModules/FSAPIServiceModules";
import {
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
type LOG_LEVEL,
type ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { BrowserAPIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/browser/BrowserAPIService";
import type { InjectableSettingService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableSettingService";
import { useOfflineScanner } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/offlineScanner";
import { useRedFlagFeatures } from "@/serviceFeatures/redFlag";
import { useCheckRemoteSize } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/checkRemoteSize";
import { useSetupURIFeature } from "@/serviceFeatures/setupObsidian/setupUri";
import { useRemoteConfiguration } from "@vrtmrz/livesync-commonlib/compat/serviceFeatures/remoteConfig";
import { SetupManager } from "@/modules/features/SetupManager";
import { useSetupManagerHandlersFeature } from "@/serviceFeatures/setupObsidian/setupManagerHandlers";
import { useP2PReplicatorCommands } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorCommands";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import { compatGlobal, _activeDocument } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { mount } from "svelte";
import { createLiveSyncBrowserServiceHub } from "@/apps/browser/createLiveSyncBrowserServiceHub";
import { WebAppRuntime, type WebAppRuntimeStatusKind } from "./WebAppRuntime";
import WebAppP2P from "./WebAppP2P.svelte";
import { VaultHistoryStore, type VaultHistoryItem } from "./vaultSelector";
const SETTINGS_DIR = ".livesync";
const SETTINGS_FILE = "settings.json";
const historyStore = new VaultHistoryStore();
let runtime: WebAppRuntime | null = null;
type LiveSyncWebAppDebugApi = {
getRuntime: () => WebAppRuntime | null;
historyStore: VaultHistoryStore;
/**
* Default settings for the webapp
*/
const DEFAULT_SETTINGS: Partial<ObsidianLiveSyncSettings> = {
liveSync: false,
syncOnSave: true,
syncOnStart: false,
savingDelay: 200,
lessInformationInLog: false,
gcDelay: 0,
periodicReplication: false,
periodicReplicationInterval: 60,
isConfigured: false,
// CouchDB settings - user needs to configure these
couchDB_URI: "",
couchDB_USER: "",
couchDB_PASSWORD: "",
couchDB_DBNAME: "",
// Disable features not needed in webapp
usePluginSync: false,
autoSweepPlugins: false,
autoSweepPluginsPeriodic: false,
};
type LiveSyncWebAppGlobal = typeof compatGlobal & {
livesyncApp?: LiveSyncWebAppDebugApi;
};
class LiveSyncWebApp {
private rootHandle: FileSystemDirectoryHandle;
private core: LiveSyncBaseCore<ServiceContext, never> | null = null;
private serviceHub: BrowserServiceHub<ServiceContext> | null = null;
private platformServiceModules: FSAPIServiceModules | null = null;
function getRequiredElement<T extends HTMLElement>(id: string): T {
const element = _activeDocument.getElementById(id);
if (!element) {
throw new Error(`Missing element: #${id}`);
constructor(rootHandle: FileSystemDirectoryHandle) {
this.rootHandle = rootHandle;
}
return element as T;
}
function setStatus(kind: WebAppRuntimeStatusKind, message: string): void {
const statusEl = getRequiredElement<HTMLDivElement>("status");
statusEl.className = kind;
statusEl.textContent = message;
}
function setBusyState(isBusy: boolean): void {
const pickNewBtn = getRequiredElement<HTMLButtonElement>("pick-new-vault");
pickNewBtn.disabled = isBusy;
const historyButtons = _activeDocument.querySelectorAll<HTMLButtonElement>(".vault-item button");
historyButtons.forEach((button) => {
button.disabled = isBusy;
});
}
function formatLastUsed(unixMillis: number): string {
if (!unixMillis) {
return "unknown";
private addLog(message: unknown, level: LOG_LEVEL = LOG_LEVEL_INFO, key?: string): void {
this.serviceHub?.API.addLog(message, level, key);
}
return new Date(unixMillis).toLocaleString();
}
async function renderHistoryList(): Promise<VaultHistoryItem[]> {
const listEl = getRequiredElement<HTMLDivElement>("vault-history-list");
const emptyEl = getRequiredElement<HTMLParagraphElement>("vault-history-empty");
async initialize() {
// Create service context and hub
this.serviceHub = createLiveSyncBrowserServiceHub<ServiceContext>();
this.addLog("Self-hosted LiveSync WebApp", LOG_LEVEL_INFO, "initialise");
this.addLog("Initialising...", LOG_LEVEL_VERBOSE, "initialise");
this.addLog(`Vault directory: ${this.rootHandle.name}`, LOG_LEVEL_VERBOSE, "initialise");
const [items, lastUsedId] = await Promise.all([historyStore.getVaultHistory(), historyStore.getLastUsedVaultId()]);
// Setup API service
(this.serviceHub.API as BrowserAPIService<ServiceContext>).getSystemVaultName.setHandler(
() => this.rootHandle?.name || "livesync-webapp"
);
listEl.replaceChildren();
emptyEl.classList.toggle("is-hidden", items.length > 0);
// Setup settings handlers - save to .livesync folder
const settingService = this.serviceHub.setting as InjectableSettingService<ServiceContext>;
for (const item of items) {
const row = createNativeElement(_activeDocument, "div");
row.className = "vault-item";
const info = createNativeElement(_activeDocument, "div");
info.className = "vault-item-info";
const name = createNativeElement(_activeDocument, "div");
name.className = "vault-item-name";
name.textContent = item.name;
const meta = createNativeElement(_activeDocument, "div");
meta.className = "vault-item-meta";
const label = item.id === lastUsedId ? "Last used" : "Used";
meta.textContent = `${label}: ${formatLastUsed(item.lastUsedAt)}`;
info.append(name, meta);
const useButton = createNativeElement(_activeDocument, "button");
useButton.type = "button";
useButton.textContent = "Use this vault";
useButton.addEventListener("click", () => {
void startWithHistory(item);
settingService.saveData.setHandler(async (data: ObsidianLiveSyncSettings) => {
try {
await this.saveSettingsToFile(data);
this.addLog("Saved to .livesync/settings.json", LOG_LEVEL_VERBOSE, "settings");
} catch (error) {
this.addLog(`Failed to save settings: ${String(error)}`, LOG_LEVEL_NOTICE, "settings");
}
});
row.append(info, useButton);
listEl.appendChild(row);
settingService.loadData.setHandler(async (): Promise<ObsidianLiveSyncSettings | undefined> => {
try {
const data = await this.loadSettingsFromFile();
if (data) {
this.addLog("Loaded from .livesync/settings.json", LOG_LEVEL_VERBOSE, "settings");
return { ...DEFAULT_SETTINGS, ...data } as ObsidianLiveSyncSettings;
}
} catch {
this.addLog("Failed to load settings; using defaults", LOG_LEVEL_NOTICE, "settings");
}
return DEFAULT_SETTINGS as ObsidianLiveSyncSettings;
});
// App lifecycle handlers
this.serviceHub.appLifecycle.scheduleRestart.setHandler(() => {
void (async () => {
this.addLog("Restart requested", LOG_LEVEL_INFO, "app-lifecycle");
await this.shutdown();
await this.initialize();
compatGlobal.setTimeout(() => {
compatGlobal.location.reload();
}, 1000);
})();
});
// Create LiveSync core
this.core = new LiveSyncBaseCore<ServiceContext, never>(
this.serviceHub,
(core, serviceHub) => {
const serviceModules = initialiseServiceModulesFSAPI(this.rootHandle, core, serviceHub);
this.platformServiceModules = serviceModules;
return serviceModules;
},
(core) => [
// new ModuleObsidianEvents(this, core),
// new ModuleObsidianSettingDialogue(this, core),
// new ModuleObsidianMenu(core),
// new ModuleObsidianSettingsAsMarkdown(core),
// new ModuleLog(this, core),
// new ModuleObsidianDocumentHistory(this, core),
// new ModuleInteractiveConflictResolver(this, core),
// new ModuleObsidianGlobalHistory(this, core),
// new ModuleDev(this, core),
// new ModuleReplicateTest(this, core),
// new ModuleIntegratedTest(this, core),
// new ModuleReplicatorP2P(core), // Register P2P replicator for CLI (useP2PReplicator is not used here)
new SetupManager(core),
],
() => [] as never[], // No add-ons
(core) => {
useOfflineScanner(core);
useRedFlagFeatures(core);
useCheckRemoteSize(core);
useRemoteConfiguration(core);
const replicator = useP2PReplicatorFeature(core);
useP2PReplicatorCommands(core, replicator);
const setupManager = core.getModule(SetupManager);
useSetupManagerHandlersFeature(core, setupManager);
useSetupURIFeature(core);
}
);
// Start the core
await this.start();
}
return items;
}
private async saveSettingsToFile(data: ObsidianLiveSyncSettings): Promise<void> {
// Create .livesync directory if it does not exist
const livesyncDir = await this.rootHandle.getDirectoryHandle(SETTINGS_DIR, { create: true });
async function startWithHandle(handle: FileSystemDirectoryHandle): Promise<void> {
setStatus("info", `Starting LiveSync with vault: ${handle.name}`);
const nextRuntime = new WebAppRuntime(handle, { reportStatus: setStatus });
await nextRuntime.start();
runtime = nextRuntime;
const selectorEl = getRequiredElement<HTMLDivElement>("vault-selector");
selectorEl.classList.add("is-hidden");
const p2pControl = getRequiredElement<HTMLElement>("p2p-control");
mount(WebAppP2P, {
target: p2pControl,
props: {
runtime: nextRuntime,
},
});
p2pControl.classList.remove("is-hidden");
}
async function startWithHistory(item: VaultHistoryItem): Promise<void> {
setBusyState(true);
let handle: FileSystemDirectoryHandle;
try {
handle = await historyStore.activateHistoryItem(item);
} catch (error) {
setStatus("error", `Failed to open saved vault: ${String(error)}`);
setBusyState(false);
return;
// Create/overwrite settings.json
const fileHandle = await livesyncDir.getFileHandle(SETTINGS_FILE, { create: true });
const writable = await fileHandle.createWritable();
await writable.write(JSON.stringify(data, null, 2));
await writable.close();
}
try {
await startWithHandle(handle);
} catch (error) {
setStatus("error", `Failed to start LiveSync: ${String(error)}`);
setBusyState(false);
private async loadSettingsFromFile(): Promise<Partial<ObsidianLiveSyncSettings> | null> {
try {
const livesyncDir = await this.rootHandle.getDirectoryHandle(SETTINGS_DIR);
const fileHandle = await livesyncDir.getFileHandle(SETTINGS_FILE);
const file = await fileHandle.getFile();
const text = await file.text();
const parsed: unknown = JSON.parse(text);
if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed)) {
throw new Error("The WebApp settings file does not contain an object");
}
return parsed;
} catch {
// File doesn't exist yet
return null;
}
}
private async start() {
if (!this.core) {
throw new Error("Core not initialized");
}
try {
this.addLog("Initialising LiveSync...", LOG_LEVEL_INFO, "start");
const loadResult = await this.core.services.control.onLoad();
if (!loadResult) {
this.addLog("Failed to initialise LiveSync", LOG_LEVEL_NOTICE, "start");
this.showError("Failed to initialize LiveSync");
return;
}
await this.core.services.control.onReady();
this.addLog("LiveSync is running", LOG_LEVEL_INFO, "ready");
// Check if configured
const settings = this.core.services.setting.currentSettings();
if (!settings.isConfigured) {
this.addLog("LiveSync is not configured yet", LOG_LEVEL_NOTICE, "configuration");
this.showWarning("Please configure CouchDB connection in settings");
} else {
this.addLog("LiveSync is configured and ready", LOG_LEVEL_INFO, "configuration");
this.addLog(`Database: ${settings.couchDB_DBNAME}`, LOG_LEVEL_VERBOSE, "configuration");
this.showSuccess("LiveSync is ready!");
}
// Scan the directory to populate file cache
const fileAccess = this.platformServiceModules?.vaultAccess;
if (fileAccess) {
this.addLog("Scanning vault directory...", LOG_LEVEL_VERBOSE, "scan");
await fileAccess.fsapiAdapter.scanDirectory();
const files = await fileAccess.fsapiAdapter.getFiles();
this.addLog(`Found ${files.length} files`, LOG_LEVEL_VERBOSE, "scan");
}
} catch (error) {
this.addLog(`Failed to start: ${String(error)}`, LOG_LEVEL_NOTICE, "start");
this.showError(`Failed to start: ${error}`);
}
}
async shutdown() {
if (this.core) {
this.addLog("Shutting down...", LOG_LEVEL_INFO, "shutdown");
// Stop file watching
const storageEventManager = this.platformServiceModules?.storageEventManager;
if (storageEventManager) {
await storageEventManager.cleanup();
}
await this.core.services.control.onUnload();
this.platformServiceModules = null;
this.addLog("Shutdown complete", LOG_LEVEL_INFO, "shutdown");
}
}
private showError(message: string) {
const statusEl = _activeDocument.getElementById("status");
if (statusEl) {
statusEl.className = "error";
statusEl.textContent = `Error: ${message}`;
}
}
private showWarning(message: string) {
const statusEl = _activeDocument.getElementById("status");
if (statusEl) {
statusEl.className = "warning";
statusEl.textContent = `Warning: ${message}`;
}
}
private showSuccess(message: string) {
const statusEl = _activeDocument.getElementById("status");
if (statusEl) {
statusEl.className = "success";
statusEl.textContent = message;
}
}
}
async function startWithNewPicker(): Promise<void> {
setBusyState(true);
let handle: FileSystemDirectoryHandle;
try {
handle = await historyStore.pickNewVault();
} catch (error) {
setStatus("warning", `Vault selection was cancelled or failed: ${String(error)}`);
setBusyState(false);
return;
}
try {
await startWithHandle(handle);
} catch (error) {
setStatus("error", `Failed to start LiveSync: ${String(error)}`);
setBusyState(false);
}
}
async function initialiseVaultSelector(): Promise<void> {
setStatus("info", "Select a vault folder to start LiveSync.");
const pickNewBtn = getRequiredElement<HTMLButtonElement>("pick-new-vault");
pickNewBtn.addEventListener("click", () => {
void startWithNewPicker();
});
await renderHistoryList();
}
compatGlobal.addEventListener("load", () => {
initialiseVaultSelector().catch((error) => {
setStatus("error", `Initialisation failed: ${String(error)}`);
});
});
compatGlobal.addEventListener("beforeunload", () => {
void runtime?.shutdown();
});
(compatGlobal as LiveSyncWebAppGlobal).livesyncApp = {
getRuntime: () => runtime,
historyStore,
};
export { LiveSyncWebApp };
@@ -74,8 +74,7 @@ class FSAPIPersistenceAdapter implements IStorageEventPersistenceAdapter {
return new Promise((resolve, reject) => {
const request = indexedDB.open(this.dbName, 1);
request.onerror = () =>
reject(request.error ?? new Error("Failed to open the WebApp snapshot database"));
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
request.onupgradeneeded = (event) => {
@@ -96,8 +95,7 @@ class FSAPIPersistenceAdapter implements IStorageEventPersistenceAdapter {
await new Promise<void>((resolve, reject) => {
const request = store.put(snapshot, this.snapshotKey);
request.onsuccess = () => resolve();
request.onerror = () =>
reject(request.error ?? new Error("Failed to save the WebApp storage-event snapshot"));
request.onerror = () => reject(request.error);
});
db.close();
@@ -116,8 +114,7 @@ class FSAPIPersistenceAdapter implements IStorageEventPersistenceAdapter {
const request = store.get(this.snapshotKey);
request.onsuccess = () =>
resolve((request.result as (FileEventItem | FileEventItemSentinel)[] | undefined) ?? null);
request.onerror = () =>
reject(request.error ?? new Error("Failed to load the WebApp storage-event snapshot"));
request.onerror = () => reject(request.error);
});
db.close();
@@ -194,15 +191,11 @@ class FSAPIWatchAdapter implements IStorageEventWatchAdapter {
) {}
async beginWatch(handlers: IStorageEventWatchHandlers): Promise<void> {
// Use FileSystemObserver when the browser provides it.
// Use FileSystemObserver if available (Chrome 124+)
const FileSystemObserver = (compatGlobal as GlobalWithFileSystemObserver).FileSystemObserver;
if (!FileSystemObserver) {
this.addLog(
"FileSystemObserver is not available; file watching is disabled",
LOG_LEVEL_INFO,
"fsapi-watch"
);
this.addLog("Use 'Scan local files' after external changes", LOG_LEVEL_INFO, "fsapi-watch");
this.addLog("FileSystemObserver is not available; file watching is disabled", LOG_LEVEL_INFO, "fsapi-watch");
this.addLog("Chrome 124 or later supports real-time file watching", LOG_LEVEL_INFO, "fsapi-watch");
return Promise.resolve();
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,85 @@
import { describe, expect, it } from "vitest";
import { fileURLToPath, path } from "@vrtmrz/livesync-commonlib/node";
import * as ts from "typescript";
const repositoryRoot = path.resolve(path.dirname(fileURLToPath(import.meta.url)), "../../..");
const dependencyFacadePath = path.resolve(repositoryRoot, "src/deps.ts");
const obsidianMockPath = path.resolve(repositoryRoot, "src/apps/webapp/obsidianMock.ts");
function parseSourceFile(filePath: string): ts.SourceFile {
const source = ts.sys.readFile(filePath);
if (source === undefined) throw new Error(`Could not read ${filePath}`);
return ts.createSourceFile(filePath, source, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS);
}
function hasExportModifier(statement: ts.Statement): boolean {
if (!ts.canHaveModifiers(statement)) return false;
return ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword) ?? false;
}
function addBindingNames(name: ts.BindingName, names: Set<string>): void {
if (ts.isIdentifier(name)) {
names.add(name.text);
return;
}
for (const element of name.elements) {
if (!ts.isOmittedExpression(element)) addBindingNames(element.name, names);
}
}
describe("Webapp Obsidian mock exports", () => {
it("provides every value re-exported from Obsidian", () => {
const dependencyFacade = parseSourceFile(dependencyFacadePath);
const obsidianMock = parseSourceFile(obsidianMockPath);
const requiredExports = new Set<string>();
for (const statement of dependencyFacade.statements) {
if (
ts.isExportDeclaration(statement) &&
statement.moduleSpecifier &&
ts.isStringLiteral(statement.moduleSpecifier) &&
statement.moduleSpecifier.text === "obsidian" &&
statement.exportClause &&
ts.isNamedExports(statement.exportClause) &&
!statement.isTypeOnly
) {
for (const element of statement.exportClause.elements) {
if (!element.isTypeOnly) {
requiredExports.add((element.propertyName ?? element.name).text);
}
}
}
}
const availableExports = new Set<string>();
for (const statement of obsidianMock.statements) {
if (
ts.isExportDeclaration(statement) &&
!statement.isTypeOnly &&
statement.exportClause &&
ts.isNamedExports(statement.exportClause)
) {
for (const element of statement.exportClause.elements) {
if (!element.isTypeOnly) availableExports.add(element.name.text);
}
continue;
}
if (!hasExportModifier(statement)) continue;
if (
(ts.isClassDeclaration(statement) ||
ts.isFunctionDeclaration(statement) ||
ts.isEnumDeclaration(statement)) &&
statement.name
) {
availableExports.add(statement.name.text);
} else if (ts.isVariableStatement(statement)) {
for (const declaration of statement.declarationList.declarations) {
addBindingNames(declaration.name, availableExports);
}
}
}
const missingExports = [...requiredExports].filter((name) => !availableExports.has(name)).sort();
expect(missingExports).toEqual([]);
});
});
+3 -6
View File
@@ -1,7 +1,7 @@
{
"name": "livesync-webapp",
"private": true,
"version": "1.0.1-webapp",
"version": "1.0.0-beta.3-webapp",
"type": "module",
"description": "Browser-based Self-hosted LiveSync using FileSystem API",
"scripts": {
@@ -9,13 +9,10 @@
"build": "vite build",
"build:docker": "docker build -f Dockerfile -t livesync-webapp ../../..",
"run:docker": "docker run -p 8002:80 livesync-webapp",
"preview": "vite preview",
"test:unit": "npm --prefix ../../.. run test:unit -- test/apps/webapp",
"pretest:browser": "npm run build",
"test:browser": "deno test -A --no-check --frozen --config ../../../test/browser-apps/deno.json --lock ../../../test/browser-apps/deno.lock ../../../test/browser-apps/webapp/browser-smoke.test.ts"
"preview": "vite preview"
},
"dependencies": {
"octagonal-wheels": "^0.1.52"
"octagonal-wheels": "^0.1.51"
},
"devDependencies": {
"@sveltejs/vite-plugin-svelte": "^7.1.2",
+2 -3
View File
@@ -68,8 +68,7 @@ export class VaultHistoryStore {
private async openHandleDB(): Promise<IDBDatabase> {
return new Promise((resolve, reject) => {
const request = indexedDB.open(HANDLE_DB_NAME, 1);
request.onerror = () =>
reject(request.error ?? new Error("Could not open the WebApp Vault history database"));
request.onerror = () => reject(request.error);
request.onsuccess = () => resolve(request.result);
request.onupgradeneeded = (event) => {
const db = (event.target as IDBOpenDBRequest).result;
@@ -94,7 +93,7 @@ export class VaultHistoryStore {
private async requestAsPromise<T>(request: IDBRequest<T>): Promise<T> {
return new Promise((resolve, reject) => {
request.onsuccess = () => resolve(request.result);
request.onerror = () => reject(request.error ?? new Error("The WebApp Vault history request failed"));
request.onerror = () => reject(request.error);
});
}
+2
View File
@@ -20,6 +20,7 @@ export default defineConfig({
resolve: {
alias: {
"@": path.resolve(__dirname, "../../"),
obsidian: path.resolve(__dirname, "./obsidianMock.ts"),
},
},
base: "./",
@@ -38,6 +39,7 @@ export default defineConfig({
MANIFEST_VERSION: JSON.stringify(process.env.MANIFEST_VERSION || manifestVersion || "0.0.0"),
PACKAGE_VERSION: JSON.stringify(process.env.PACKAGE_VERSION || packageVersion || "0.0.0"),
global: "globalThis",
hostPlatform: JSON.stringify(process.platform || "linux"),
},
server: {
port: 3000,
+3 -12
View File
@@ -31,7 +31,7 @@ body {
border-radius: 12px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 40px;
max-width: 1100px;
max-width: 700px;
width: 100%;
}
@@ -161,19 +161,10 @@ button:disabled {
}
.empty-note.is-hidden,
.vault-selector.is-hidden,
.p2p-control.is-hidden {
.vault-selector.is-hidden {
display: none;
}
.p2p-control {
margin-bottom: 22px;
padding: 16px;
border: 1px solid #e6e9f2;
border-radius: 8px;
background: #fbfcff;
}
.info-section {
margin-top: 20px;
padding: 20px;
@@ -406,4 +397,4 @@ popup {
align-items: center;
justify-content: center;
backdrop-filter: blur(15px);
}
}
+38 -40
View File
@@ -1,47 +1,45 @@
<!doctype html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Self-hosted LiveSync WebApp</title>
<link rel="stylesheet" href="./webapp.css" />
</head>
<body>
<div class="container">
<h1>Self-hosted LiveSync on Web</h1>
<p class="subtitle">Experimental browser proof of concept using the File System Access API</p>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Self-hosted LiveSync WebApp</title>
<link rel="stylesheet" href="./webapp.css">
</head>
<body>
<div class="container">
<h1>Self-hosted LiveSync on Web</h1>
<p class="subtitle">Browser-based Self-hosted LiveSync using FileSystem API</p>
<div id="status" class="info">Initialising...</div>
<div id="status" class="info">Initialising...</div>
<div id="vault-selector" class="vault-selector">
<h2>Select Vault Folder</h2>
<p>Open a vault you already used, or pick a new folder.</p>
<div id="vault-selector" class="vault-selector">
<h2>Select Vault Folder</h2>
<p>Open a vault you already used, or pick a new folder.</p>
<div id="vault-history-list" class="vault-list"></div>
<p id="vault-history-empty" class="empty-note">No saved vaults yet.</p>
<button id="pick-new-vault" type="button">Choose new vault folder</button>
</div>
<section id="p2p-control" class="p2p-control is-hidden"></section>
<div class="info-section">
<h2>How to Use</h2>
<ul>
<li>Select the local Vault root and grant access.</li>
<li>Create or edit <code>.livesync/settings.json</code> in that Vault.</li>
<li>Reload this page to apply the settings.</li>
<li>Use the P2P controls only as an optional secondary connection.</li>
</ul>
</div>
<div class="footer">
<p>
Powered by
<a href="https://github.com/vrtmrz/obsidian-livesync" target="_blank">Self-hosted LiveSync</a>
</p>
</div>
<div id="vault-history-list" class="vault-list"></div>
<p id="vault-history-empty" class="empty-note">No saved vaults yet.</p>
<button id="pick-new-vault" type="button">Choose new vault folder</button>
</div>
<script type="module" src="./main.ts"></script>
</body>
<div class="info-section">
<h2>How to Use</h2>
<ul>
<li>Select a vault folder and grant permission</li>
<li>Create <code>.livesync/settings.json</code> in your vault folder</li>
<li>Or use Setup-URI to apply settings</li>
<li>Your files will be synced after "replicate now"</li>
</ul>
</div>
<div class="footer">
<p>
Powered by
<a href="https://github.com/vrtmrz/obsidian-livesync" target="_blank">Self-hosted LiveSync</a>
</p>
</div>
</div>
<script type="module" src="./bootstrap.ts"></script>
</body>
</html>
+18 -71
View File
@@ -1,90 +1,37 @@
# Self-hosted LiveSync WebPeer
# A pseudo client for Self-hosted LiveSync Peer-to-Peer Sync mode
WebPeer is an experimental, browser-hosted, P2P-only Self-hosted LiveSync peer. It can receive database changes from one peer and provide them to another without materialising ordinary Vault files.
## What is it for?
It stores LiveSync metadata and chunks in an origin-scoped local database, but it does not present them as a Vault. This makes it useful as a temporary browser peer or transfer bridge. It is not a durable replacement for CouchDB, a backup, or an always-on server.
This is a pseudo client for the Self-hosted LiveSync Peer-to-Peer Sync mode. It is a simple pure-client-side web-application that can be connected to the Self-hosted LiveSync in peer-to-peer.
## Requirements
As long as you have a browser, it starts up, so if you leave it opened some device, it can replace your existing remote servers such as CouchDB.
WebPeer requires:
> [!IMPORTANT]
> Of course, it has not been fully tested. Rather, it was created to be tested.
- a secure context, such as HTTPS or `localhost`;
- IndexedDB, WebRTC, WebSocket, and Web Crypto support;
- access to the configured signalling relay; and
- the same Group ID, passphrase, and compatible P2P settings as the other peers.
This pseudo client actually receives the data from other devices, and sends if some device requests it. However, it does not store **files** in the local storage. If you want to purge the data, please purge the browser's cache and indexedDB, local storage, etc.
TURN is optional and is needed only when a direct WebRTC connection cannot be established. The signalling relay is required for peer discovery, but it does not store or transfer Vault contents.
## How to use it?
## Use
Serve `src/apps/webpeer/dist/` over HTTPS, or from `localhost`, then open `index.html`.
1. Enable the P2P replicator.
2. Enter the signalling relay, Group ID, passphrase, and a unique device name.
3. Select **Save and Apply**.
4. Select **Connect** and wait for the intended peers to appear.
5. Use the peer actions to fetch, send, watch, or configure automatic synchronisation as required.
**Start change-broadcasting on Connect** allows watching peers to notice changes and fetch them. It does not send Vault data through the signalling relay. Optional TURN settings are available separately.
Keep the page open while WebPeer is expected to announce or transfer changes.
## Storage and lifecycle
WebPeer stores its settings, metadata, and chunks in storage belonging to the page origin. Consequently:
- changing the scheme, host, or port creates a different WebPeer state;
- clearing site data removes the local database and settings;
- browser storage eviction can remove the state; and
- page suspension, tab closure, device sleep, and network policy can interrupt P2P activity.
Use a separately deployed origin when isolation from the public Pages deployment is required. Do not treat WebPeer browser storage as the only copy of any data.
## Troubleshooting
### No peers appear
- Confirm that every peer uses the same signalling relay, Group ID, and passphrase.
- Confirm that each peer has a distinct device name.
- Confirm that P2P is enabled and that the signalling connection reports **Connected**.
- Check whether the browser or network blocks the relay WebSocket.
### Peers connect but changes do not transfer
- Confirm which peer should fetch and which should send.
- Start broadcasting on the source when another peer is watching it.
- Use the explicit fetch or send action to test one direction.
- Confirm that the peers use compatible Self-hosted LiveSync versions and P2P settings.
### Saved settings or data appear to be missing
- Confirm that the page is using the same origin as before.
- Check whether site data was cleared or evicted.
- Check the status line and WebPeer log for the first reported error.
For more detail about relay, TURN, announcing, and following behaviour, see the [P2P guide](../../../docs/p2p.md).
## Development
Build it from the repository root:
We can build the application from the repository root by running the following command:
```bash
npm run build --workspace webpeer
npm run build -w webpeer
```
The app-owned unit and Chromium tests can be run with:
Or from the package directory:
```bash
npm run test:unit --workspace webpeer
npm run test:browser --workspace webpeer
cd src/apps/webpeer
npm run build
```
The unit tests are stored in `test/apps/webpeer/`, outside the Community Review source boundary.
Then, open `dist/index.html` in the browser. It can be configured in the same way as Self-hosted LiveSync (the same components are used[^1]).
## Composition
## Some notes
`WebPeerRuntime.ts` owns the browser service composition, local database lifecycle, P2P replicator, and peer actions. `WebPeerPersistence.ts` owns origin-scoped settings persistence, while the shared P2P pane supplies the connection and peer controls.
I will launch this application in the github pages later, so will be able to use it without building it. However, that shares the origin. Hence, the application that your have built and deployed would be more secure.
## Licence
The same licence as the main Self-hosted LiveSync project applies.
[^1]: Congrats! I made it modular. Finally...
+3 -6
View File
@@ -1,7 +1,7 @@
{
"name": "webpeer",
"private": true,
"version": "1.0.1-webpeer",
"version": "1.0.0-beta.3-webpeer",
"type": "module",
"scripts": {
"dev": "vite",
@@ -9,13 +9,10 @@
"build:docker": "docker build -f Dockerfile -t livesync-webpeer ../../..",
"run:docker": "docker run -p 8001:80 livesync-webpeer",
"preview": "vite preview",
"check": "svelte-check --tsconfig ./tsconfig.app.json && tsc -p tsconfig.node.json",
"test:unit": "npm --prefix ../../.. run test:unit -- test/apps/webpeer",
"pretest:browser": "npm run build",
"test:browser": "deno test -A --no-check --frozen --config ../../../test/browser-apps/deno.json --lock ../../../test/browser-apps/deno.lock ../../../test/browser-apps/webpeer/browser-smoke.test.ts"
"check": "svelte-check --tsconfig ./tsconfig.app.json && tsc -p tsconfig.node.json"
},
"dependencies": {
"octagonal-wheels": "^0.1.52"
"octagonal-wheels": "^0.1.51"
},
"devDependencies": {
"eslint-plugin-svelte": "^3.19.0",
+22
View File
@@ -0,0 +1,22 @@
import { LOG_LEVEL_VERBOSE } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { defaultLoggerEnv, setGlobalLogFunction } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import { writable } from "svelte/store";
export const logs = writable([] as string[]);
let _logs = [] as string[];
const maxLines = 10000;
setGlobalLogFunction((msg, level) => {
const msgstr = typeof msg === "string" ? msg : JSON.stringify(msg);
const strLog = `${new Date().toISOString()}\u2001${msgstr}`;
_logs.push(strLog);
if (_logs.length > maxLines) {
_logs = _logs.slice(_logs.length - maxLines);
}
logs.set(_logs);
});
defaultLoggerEnv.minLogLevel = LOG_LEVEL_VERBOSE;
export const storeP2PStatusLine = writable("");
+341
View File
@@ -0,0 +1,341 @@
import { PouchDB } from "@vrtmrz/livesync-commonlib/compat/pouchdb/pouchdb-browser";
import {
type EntryDoc,
type ObsidianLiveSyncSettings,
type P2PSyncSetting,
LOG_LEVEL_VERBOSE,
P2P_DEFAULT_SETTINGS,
REMOTE_P2P,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { Confirm } from "@vrtmrz/livesync-commonlib/compat/interfaces/Confirm";
import { LOG_LEVEL_NOTICE, Logger, type LOG_LEVEL } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import {
EVENT_P2P_PEER_SHOW_EXTRA_MENU,
type PeerStatus,
type PluginShim,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import { useP2PReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorCore";
import { P2PLogCollector } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PLogCollector";
import type { P2PReplicatorBase } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorBase";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
import { reactiveSource } from "octagonal-wheels/dataobject/reactive_v2";
import { EVENT_SETTING_SAVED } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import { unique } from "octagonal-wheels/collection";
import { SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import type { InjectableServiceHub } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableServiceHub";
import { Menu } from "@/apps/browser/BrowserMenu";
import { SimpleStoreIDBv2 } from "octagonal-wheels/databases/SimpleStoreIDBv2";
import type { BrowserAPIService } from "@vrtmrz/livesync-commonlib/compat/services/implements/browser/BrowserAPIService";
import type { InjectableSettingService } from "@vrtmrz/livesync-commonlib/compat/services/implements/injectable/InjectableSettingService";
import { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { createLiveSyncBrowserServiceHub } from "@/apps/browser/createLiveSyncBrowserServiceHub";
function addToList(item: string, list: string) {
return unique(
list
.split(",")
.map((e) => e.trim())
.concat(item)
.filter((p) => p)
).join(",");
}
function removeFromList(item: string, list: string) {
return list
.split(",")
.map((e) => e.trim())
.filter((p) => p !== item)
.filter((p) => p)
.join(",");
}
export class P2PReplicatorShim implements P2PReplicatorBase {
storeP2PStatusLine = reactiveSource("");
plugin!: PluginShim;
confirm!: Confirm;
db?: PouchDB.Database<EntryDoc>;
services: InjectableServiceHub<ServiceContext>;
getDB() {
if (!this.db) {
throw new Error("DB not initialized");
}
return this.db;
}
_simpleStore!: SimpleStore<unknown>;
async closeDB() {
if (this.db) {
await this.db.close();
this.db = undefined;
}
}
private _liveSyncReplicator?: LiveSyncTrysteroReplicator;
p2pLogCollector!: P2PLogCollector;
private _initP2PReplicator() {
const {
replicator,
p2pLogCollector,
storeP2PStatusLine: p2pStatusLine,
} = useP2PReplicator({ services: this.services } as unknown as Parameters<typeof useP2PReplicator>[0]);
this._liveSyncReplicator = replicator;
this.p2pLogCollector = p2pLogCollector;
p2pLogCollector.p2pReplicationLine.onChanged((line) => {
p2pStatusLine.value = line.value;
});
}
constructor() {
const browserServiceHub = createLiveSyncBrowserServiceHub<ServiceContext>();
this.services = browserServiceHub;
(this.services.API as BrowserAPIService<ServiceContext>).getSystemVaultName.setHandler(
() => "p2p-livesync-web-peer"
);
const repStore = SimpleStoreIDBv2.open<unknown>("p2p-livesync-web-peer");
this._simpleStore = repStore;
let _settings = { ...P2P_DEFAULT_SETTINGS, additionalSuffixOfDatabaseName: "" } as ObsidianLiveSyncSettings;
this.services.setting.settings = _settings;
(this.services.setting as InjectableSettingService<ServiceContext>).saveData.setHandler(async (data) => {
await repStore.set("settings", data);
this.services.context.events.emitEvent(EVENT_SETTING_SAVED, data);
});
(this.services.setting as InjectableSettingService<ServiceContext>).loadData.setHandler(async () => {
const settings = { ..._settings, ...((await repStore.get("settings")) as ObsidianLiveSyncSettings) };
return settings;
});
}
get settings() {
return this.services.setting.currentSettings() as P2PSyncSetting;
}
async init() {
this.confirm = this.services.UI.confirm;
if (this.db) {
try {
await this.closeDB();
} catch (ex) {
Logger("Error closing db", LOG_LEVEL_VERBOSE);
Logger(ex, LOG_LEVEL_VERBOSE);
}
}
await this.services.setting.loadSettings();
this.plugin = {
services: this.services,
core: {
services: this.services,
},
};
const database_name = this.settings.P2P_AppID + "-" + this.settings.P2P_roomID + "p2p-livesync-web-peer";
this.db = new PouchDB<EntryDoc>(database_name);
this._initP2PReplicator();
compatGlobal.setTimeout(() => {
if (this.settings.P2P_AutoStart && this.settings.P2P_Enabled) {
void this.open();
}
}, 1000);
return this;
}
_log(msg: unknown, level?: LOG_LEVEL): void {
Logger(msg, level);
}
_notice(msg: string, key?: string): void {
Logger(msg, LOG_LEVEL_NOTICE, key);
}
getSettings(): P2PSyncSetting {
return this.settings;
}
simpleStore(): SimpleStore<unknown> {
return this._simpleStore;
}
handleReplicatedDocuments(_docs: EntryDoc[]): Promise<boolean> {
return Promise.resolve(true);
}
getConfig(key: string) {
const vaultName = this.services.vault.getVaultName();
const dbKey = `${vaultName}-${key}`;
return compatGlobal.localStorage.getItem(dbKey);
}
setConfig(key: string, value: string) {
const vaultName = this.services.vault.getVaultName();
const dbKey = `${vaultName}-${key}`;
compatGlobal.localStorage.setItem(dbKey, value);
}
getDeviceName(): string {
return this.getConfig(SETTING_KEY_P2P_DEVICE_NAME) ?? this.plugin.services.vault.getVaultName();
}
m?: Menu;
afterConstructor(): void {
this.services.context.events.onEvent(EVENT_P2P_PEER_SHOW_EXTRA_MENU, ({ peer, event }) => {
if (this.m) {
this.m.hide();
}
this.m = new Menu()
.addItem((item) => item.setTitle("📥 Only Fetch").onClick(() => this.replicateFrom(peer)))
.addItem((item) => item.setTitle("📤 Only Send").onClick(() => this.replicateTo(peer)))
.addSeparator()
.addItem((item) => {
const mark = peer.syncOnConnect ? "checkmark" : null;
item.setTitle("Toggle Sync on connect")
.onClick(async () => {
await this.toggleProp(peer, "syncOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.watchOnConnect ? "checkmark" : null;
item.setTitle("Toggle Watch on connect")
.onClick(async () => {
await this.toggleProp(peer, "watchOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.syncOnReplicationCommand ? "checkmark" : null;
item.setTitle("Toggle Sync on `Replicate now` command")
.onClick(async () => {
await this.toggleProp(peer, "syncOnReplicationCommand");
})
.setIcon(mark);
});
void this.m.showAtPosition({ x: event.x, y: event.y });
});
}
async open() {
await this._liveSyncReplicator?.open();
}
async close() {
await this._liveSyncReplicator?.close();
}
enableBroadcastCastings() {
return this._liveSyncReplicator?.enableBroadcastChanges();
}
disableBroadcastCastings() {
return this._liveSyncReplicator?.disableBroadcastChanges();
}
enableBroadcastChanges() {
return this._liveSyncReplicator?.enableBroadcastChanges();
}
disableBroadcastChanges() {
return this._liveSyncReplicator?.disableBroadcastChanges();
}
async makeDecision(decision: Parameters<LiveSyncTrysteroReplicator["makeDecision"]>[0]): Promise<void> {
await this._liveSyncReplicator?.makeDecision(decision);
}
async revokeDecision(decision: Parameters<LiveSyncTrysteroReplicator["revokeDecision"]>[0]): Promise<void> {
await this._liveSyncReplicator?.revokeDecision(decision);
}
watchPeer(peerId: string): void {
this._liveSyncReplicator?.watchPeer(peerId);
}
unwatchPeer(peerId: string): void {
this._liveSyncReplicator?.unwatchPeer(peerId);
}
async sync(peerId: string, showNotice?: boolean): Promise<unknown> {
return await this._liveSyncReplicator?.sync(peerId, showNotice);
}
get replicator() {
return this._liveSyncReplicator;
}
async replicateFrom(peer: PeerStatus) {
const r = this._liveSyncReplicator;
if (!r) return;
await r.replicateFrom(peer.peerId);
}
async replicateTo(peer: PeerStatus) {
await this._liveSyncReplicator?.requestSynchroniseToPeer(peer.peerId);
}
async getRemoteConfig(peer: PeerStatus) {
Logger(
`Requesting remote config for ${peer.name}. Please input the passphrase on the remote device`,
LOG_LEVEL_NOTICE
);
const remoteConfig = await this._liveSyncReplicator?.getRemoteConfig(peer.peerId);
if (remoteConfig) {
Logger(`Remote config for ${peer.name} is retrieved successfully`);
const DROP = "Yes, and drop local database";
const KEEP = "Yes, but keep local database";
const CANCEL = "No, cancel";
const yn = await this.confirm.askSelectStringDialogue(
`Do you really want to apply the remote config? This will overwrite your current config immediately and restart.
And you can also drop the local database to rebuild from the remote device.`,
[DROP, KEEP, CANCEL] as const,
{
defaultAction: CANCEL,
title: "Apply Remote Config ",
}
);
if (yn === DROP || yn === KEEP) {
if (yn === DROP) {
if (remoteConfig.remoteType !== REMOTE_P2P) {
const yn2 = await this.confirm.askYesNoDialog(
`Do you want to set the remote type to "P2P Sync" to rebuild by "P2P replication"?`,
{ title: "Rebuild from remote device" }
);
if (yn2 === "yes") {
remoteConfig.remoteType = REMOTE_P2P;
remoteConfig.P2P_RebuildFrom = peer.name;
}
}
}
await this.services.setting.applyExternalSettings(remoteConfig, true);
if (yn !== DROP) {
this.plugin.core.services.appLifecycle.scheduleRestart();
}
} else {
Logger(`Cancelled\nRemote config for ${peer.name} is not applied`, LOG_LEVEL_NOTICE);
}
} else {
Logger(`Cannot retrieve remote config for ${peer.peerId}`);
}
}
async toggleProp(peer: PeerStatus, prop: "syncOnConnect" | "watchOnConnect" | "syncOnReplicationCommand") {
const settingMap = {
syncOnConnect: "P2P_AutoSyncPeers",
watchOnConnect: "P2P_AutoWatchPeers",
syncOnReplicationCommand: "P2P_SyncOnReplication",
} as const;
const targetSetting = settingMap[prop];
const currentSettingAll = this.plugin.core.services.setting.currentSettings();
const currentSetting = {
[targetSetting]: currentSettingAll ? currentSettingAll[targetSetting] : "",
};
if (peer[prop]) {
currentSetting[targetSetting] = removeFromList(peer.name, currentSetting[targetSetting]);
} else {
currentSetting[targetSetting] = addToList(peer.name, currentSetting[targetSetting]);
}
await this.plugin.core.services.setting.applyPartial(currentSetting, true);
}
}
export const cmdSyncShim = new P2PReplicatorShim();
+17 -21
View File
@@ -1,38 +1,34 @@
<script lang="ts">
import { logs } from "./WebPeerLogs";
import BrowserP2PTransportSettings from "@/apps/browser/BrowserP2PTransportSettings.svelte";
import { storeP2PStatusLine, logs } from "./CommandsShim";
import P2PReplicatorPane from "@/features/P2PSync/P2PReplicator/P2PReplicatorPane.svelte";
import { onMount, tick } from "svelte";
import { WebPeerRuntime } from "./WebPeerRuntime";
import { cmdSyncShim } from "./P2PReplicatorShim";
import { EVENT_LAYOUT_READY } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
const runtime = new WebPeerRuntime();
const synchronised = runtime.start();
let elP: HTMLDivElement;
let statusLine = $state(runtime.statusLine.value);
let synchronised = $state(cmdSyncShim.init());
onMount(() => {
const onStatusLineChanged = (line: { readonly value: string }) => {
statusLine = line.value;
};
runtime.statusLine.onChanged(onStatusLineChanged);
const unsubscribeLogs = logs.subscribe(() => {
void tick().then(() => elP?.scrollTo({ top: elP.scrollHeight }));
});
void synchronised.then((shim) => shim.services.context.events.emitEvent(EVENT_LAYOUT_READY));
return () => {
runtime.statusLine.offChanged(onStatusLineChanged);
unsubscribeLogs();
void runtime.shutdown();
synchronised.then((e) => e.close());
};
});
let elP: HTMLDivElement;
logs.subscribe((log) => {
tick().then(() => elP?.scrollTo({ top: elP.scrollHeight }));
});
let statusLine = $state("");
storeP2PStatusLine.subscribe((status) => {
statusLine = status;
});
</script>
<main>
<div class="control">
{#await synchronised then activeRuntime}
<BrowserP2PTransportSettings host={activeRuntime.paneHost} />
<P2PReplicatorPane host={activeRuntime.paneHost}></P2PReplicatorPane>
{#await synchronised then cmdSync}
<P2PReplicatorPane {cmdSync} core={cmdSync.plugin.core}></P2PReplicatorPane>
{:catch error}
<p>{error instanceof Error ? error.message : String(error)}</p>
<p>{error.message}</p>
{/await}
</div>
<div class="log">
+6 -6
View File
@@ -19,16 +19,16 @@
async function testMenu(event: MouseEvent) {
const m = new Menu()
.addItem((item) => item.setTitle("📥 Only fetch").onClick(() => {}))
.addItem((item) => item.setTitle("📤 Only send").onClick(() => {}))
.addItem((item) => item.setTitle("📥 Only Fetch").onClick(() => {}))
.addItem((item) => item.setTitle("📤 Only Send").onClick(() => {}))
.addSeparator()
.addItem((item) => {
item.setTitle("🔧 Get configuration").onClick(async () => {});
item.setTitle("🔧 Get Configuration").onClick(async () => {});
})
.addSeparator()
.addItem((item) => {
const mark = "checkmark";
item.setTitle("Toggle sync on connect")
item.setTitle("Toggle Sync on connect")
.onClick(async () => {
// await this.toggleProp(peer, "syncOnConnect");
})
@@ -36,7 +36,7 @@
})
.addItem((item) => {
const mark = null;
item.setTitle("Toggle watch on connect")
item.setTitle("Toggle Watch on connect")
.onClick(async () => {
// await this.toggleProp(peer, "watchOnConnect");
})
@@ -44,7 +44,7 @@
})
.addItem((item) => {
const mark = null;
item.setTitle("Toggle sync on `Replicate now` command")
item.setTitle("Toggle Sync on `Replicate now` command")
.onClick(async () => {})
.setIcon(mark);
});
-18
View File
@@ -1,18 +0,0 @@
import { LOG_LEVEL_VERBOSE } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { defaultLoggerEnv, setGlobalLogFunction } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import { writable } from "svelte/store";
export const logs = writable<string[]>([]);
let bufferedLogs: string[] = [];
const maxLines = 10_000;
setGlobalLogFunction((message) => {
const messageText = typeof message === "string" ? message : JSON.stringify(message);
bufferedLogs.push(`${new Date().toISOString()}\u2001${messageText}`);
if (bufferedLogs.length > maxLines) {
bufferedLogs = bufferedLogs.slice(bufferedLogs.length - maxLines);
}
logs.set(bufferedLogs);
});
defaultLoggerEnv.minLogLevel = LOG_LEVEL_VERBOSE;
@@ -1,47 +0,0 @@
import {
DEFAULT_SETTINGS,
P2P_DEFAULT_SETTINGS,
REMOTE_P2P,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
import { SimpleStoreIDBv2 } from "octagonal-wheels/databases/SimpleStoreIDBv2";
import type { LiveSyncBrowserSettingsPersistence } from "@/apps/browser/createLiveSyncBrowserServiceHub";
export const WEBPEER_STORE_NAME = "p2p-livesync-web-peer";
export const WEBPEER_SETTINGS_KEY = "settings";
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
/** Creates WebPeer-owned settings persistence without retaining legacy database names. */
export function createWebPeerPersistence(
store: SimpleStore<unknown> = SimpleStoreIDBv2.open<unknown>(WEBPEER_STORE_NAME)
): {
readonly store: SimpleStore<unknown>;
readonly settings: LiveSyncBrowserSettingsPersistence;
} {
const settings: LiveSyncBrowserSettingsPersistence = {
async load() {
const savedSettings = await store.get(WEBPEER_SETTINGS_KEY);
return {
...DEFAULT_SETTINGS,
...P2P_DEFAULT_SETTINGS,
additionalSuffixOfDatabaseName: "",
suspendParseReplicationResult: true,
...(isRecord(savedSettings) ? savedSettings : {}),
remoteType: REMOTE_P2P,
isConfigured: true,
};
},
async save(currentSettings) {
await store.set(WEBPEER_SETTINGS_KEY, currentSettings);
},
};
return {
store,
settings,
};
}
-199
View File
@@ -1,199 +0,0 @@
import { type P2PSyncSetting, SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { compatGlobal } from "@vrtmrz/livesync-commonlib/compat/common/coreEnvFunctions";
import { EVENT_LAYOUT_READY } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import { P2PLogCollector } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PLogCollector";
import type { LiveSyncTrysteroReplicator } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/LiveSyncTrysteroReplicator";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
import { useP2PReplicatorFeature } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/useP2PReplicatorFeature";
import { ServiceContext, type LiveSyncEventHub } from "@vrtmrz/livesync-commonlib/context";
import { unique } from "octagonal-wheels/collection";
import type { SimpleStore } from "octagonal-wheels/databases/SimpleStoreBase";
import {
createLiveSyncBrowserServiceHub,
type LiveSyncBrowserServiceHub,
} from "@/apps/browser/createLiveSyncBrowserServiceHub";
import { Menu } from "@/apps/browser/BrowserMenu";
import type { P2PReplicatorPaneHost } from "@/features/P2PSync/P2PReplicator/P2PReplicatorPaneHost";
import { translateLiveSyncMessage } from "@/common/translation";
import { WEBPEER_STORE_NAME, createWebPeerPersistence } from "./WebPeerPersistence";
export interface WebPeerRuntimeOptions {
context?: ServiceContext;
store?: SimpleStore<unknown>;
}
function addToList(item: string, list: string): string {
return unique(
list
.split(",")
.map((entry) => entry.trim())
.concat(item)
.filter(Boolean)
).join(",");
}
function removeFromList(item: string, list: string): string {
return list
.split(",")
.map((entry) => entry.trim())
.filter((entry) => entry !== item)
.filter(Boolean)
.join(",");
}
export class WebPeerRuntime {
readonly context: ServiceContext;
readonly services: LiveSyncBrowserServiceHub<ServiceContext>;
readonly p2p: UseP2PReplicatorResult;
readonly p2pLogCollector: P2PLogCollector;
readonly paneHost: P2PReplicatorPaneHost;
private menu?: Menu;
private restartScheduled = false;
private startPromise?: Promise<this>;
private shutdownPromise?: Promise<void>;
constructor(options: WebPeerRuntimeOptions = {}) {
const persistence = createWebPeerPersistence(options.store);
this.context = options.context ?? new ServiceContext({ translate: translateLiveSyncMessage });
this.services = createLiveSyncBrowserServiceHub<ServiceContext>({
context: this.context,
getSystemVaultName: () => WEBPEER_STORE_NAME,
settings: persistence.settings,
restart: {
schedule: () => this.scheduleRestart(),
perform: () => this.scheduleRestart(),
ask: () => this.scheduleRestart(),
isScheduled: () => this.restartScheduled,
},
});
this.p2p = useP2PReplicatorFeature({
services: this.services,
serviceModules: {},
});
this.p2pLogCollector = new P2PLogCollector(this.events);
this.paneHost = {
services: this.services,
p2p: this.p2p,
showPeerMenu: (peer, event) => this.showPeerMenu(peer, event),
};
}
get events(): LiveSyncEventHub {
return this.context.events;
}
get currentReplicator(): LiveSyncTrysteroReplicator {
return this.p2p.replicator;
}
get settings(): P2PSyncSetting {
return this.services.setting.currentSettings();
}
get statusLine() {
return this.p2pLogCollector.p2pReplicationLine;
}
start(): Promise<this> {
this.startPromise ??= this.startRuntime();
return this.startPromise;
}
private async startRuntime(): Promise<this> {
await this.services.setting.loadSettings();
const opened = await this.services.database.openDatabase({
replicator: this.services.replicator,
databaseEvents: this.services.databaseEvents,
});
if (!opened) {
throw new Error("WebPeer local database could not be opened");
}
this.services.appLifecycle.markIsReady();
this.events.emitEvent(EVENT_LAYOUT_READY);
if (this.settings.P2P_AutoStart && this.settings.P2P_Enabled) {
compatGlobal.setTimeout(() => void this.currentReplicator.open(), 100);
}
return this;
}
shutdown(): Promise<void> {
this.shutdownPromise ??= this.shutdownRuntime();
return this.shutdownPromise;
}
private async shutdownRuntime(): Promise<void> {
this.menu?.hide();
this.menu = undefined;
if (!this.services.control.hasUnloaded()) {
await this.services.control.onUnload();
}
}
private scheduleRestart(): void {
if (this.restartScheduled) {
return;
}
this.restartScheduled = true;
compatGlobal.setTimeout(() => compatGlobal.location.reload(), 0);
}
private showPeerMenu(peer: PeerStatus, event: MouseEvent): void {
this.menu?.hide();
this.menu = new Menu()
.addItem((item) =>
item.setTitle("📥 Only fetch").onClick(async () => {
await this.currentReplicator.replicateFrom(peer.peerId);
})
)
.addItem((item) =>
item.setTitle("📤 Only send").onClick(async () => {
await this.currentReplicator.requestSynchroniseToPeer(peer.peerId);
})
)
.addSeparator()
.addItem((item) => {
item.setTitle("Toggle sync on connect")
.onClick(() => this.togglePeerSetting(peer, "syncOnConnect"))
.setIcon(peer.syncOnConnect ? "checkmark" : null);
})
.addItem((item) => {
item.setTitle("Toggle watch on connect")
.onClick(() => this.togglePeerSetting(peer, "watchOnConnect"))
.setIcon(peer.watchOnConnect ? "checkmark" : null);
})
.addItem((item) => {
item.setTitle("Toggle sync on `Replicate now` command")
.onClick(() => this.togglePeerSetting(peer, "syncOnReplicationCommand"))
.setIcon(peer.syncOnReplicationCommand ? "checkmark" : null);
});
void this.menu.showAtPosition({ x: event.x, y: event.y });
}
private async togglePeerSetting(
peer: PeerStatus,
property: "syncOnConnect" | "watchOnConnect" | "syncOnReplicationCommand"
): Promise<void> {
const settingMap = {
syncOnConnect: "P2P_AutoSyncPeers",
watchOnConnect: "P2P_AutoWatchPeers",
syncOnReplicationCommand: "P2P_SyncOnReplication",
} as const;
const settingKey = settingMap[property];
const currentValue = this.services.setting.currentSettings()[settingKey] ?? "";
await this.services.setting.applyPartial(
{
[settingKey]: peer[property]
? removeFromList(peer.name, currentValue)
: addToList(peer.name, currentValue),
},
true
);
}
getDeviceName(): string {
return this.services.config.getSmallConfig(SETTING_KEY_P2P_DEVICE_NAME) || this.services.vault.getVaultName();
}
}
@@ -9,7 +9,8 @@
export const liveSyncProvisionalEnglishMessages = {
"This first setup has several short steps because it confirms encryption, the connection method, and which device provides the initial data. Once it is complete, additional devices can reuse a Setup URI.":
"This first setup has several short steps because it confirms encryption, the connection method, and which device provides the initial data. Once it is complete, additional devices can reuse a Setup URI.",
"Setup Complete: Preparing to Fetch from Another Device": "Setup Complete: Preparing to Fetch from Another Device",
"Setup Complete: Preparing to Fetch from Another Device":
"Setup Complete: Preparing to Fetch from Another Device",
"The P2P connection has been configured successfully. The initial synchronisation data must now be fetched from an online source device.":
"The P2P connection has been configured successfully. The initial synchronisation data must now be fetched from an online source device.",
"After restarting, select an online source device for the initial Fetch. The local LiveSync database on this device will be rebuilt from that source. Unsynchronised files in this Vault may conflict with the fetched data.":
@@ -63,90 +64,6 @@ export const liveSyncProvisionalEnglishMessages = {
"This file has unresolved conflicts.": "This file has unresolved conflicts.",
"This file has ${COUNT} unresolved versions. They will be reviewed one pair at a time.":
"This file has ${COUNT} unresolved versions. They will be reviewed one pair at a time.",
"Sync now": "Sync now",
"Apply pending changes now": "Apply pending changes now",
"Copy database information for the active file": "Copy database information for the active file",
"Copy database information for a file": "Copy database information for a file",
"Copy revision, conflict, and local chunk availability information, including document and chunk identifiers but not file contents.":
"Copy revision, conflict, and local chunk availability information, including document and chunk identifiers but not file contents.",
"Choose file": "Choose file",
"Choose a file to inspect": "Choose a file to inspect",
"Database information for ${FILE}": "Database information for ${FILE}",
"All revisions and chunk availability below are a snapshot of this device's local database; the remote is not queried. Review the Vault-relative path, document identifier, content-derived chunk identifiers, and metadata before sharing this report. File contents are omitted.":
"All revisions and chunk availability below are a snapshot of this device's local database; the remote is not queried. Review the Vault-relative path, document identifier, content-derived chunk identifiers, and metadata before sharing this report. File contents are omitted.",
"📁 Vault: ${SIZE} B · ${TIME}": "📁 Vault: ${SIZE} B · ${TIME}",
"📁 Vault: missing": "📁 Vault: missing",
"🗄️ Local DB: missing": "🗄️ Local DB: missing",
"Vault and database revision": "Vault and database revision",
"Vault file": "Vault file",
"Database revision": "Database revision",
"Vault file is newer": "Vault file is newer",
"Database revision is newer": "Database revision is newer",
"Within the two-second comparison window": "Within the two-second comparison window",
"Timestamp comparison unavailable": "Timestamp comparison unavailable",
"${ROLE}: ${REVISION}": "${ROLE}: ${REVISION}",
"Winner revision": "Winner revision",
"Conflict revision": "Conflict revision",
"Unknown revision": "Unknown revision",
"🗑️ Logical deletion": "🗑️ Logical deletion",
"Readable on this device; recorded size ${RECORDED}, decoded size ${ACTUAL}":
"Readable on this device; recorded size ${RECORDED}, decoded size ${ACTUAL}",
"🧩 Missing chunks: ${COUNT}": "🧩 Missing chunks: ${COUNT}",
"📦 DB: recorded ${RECORDED} B · decoded ${DECODED} B · Δsize ${DIFFERENCE} B":
"📦 DB: recorded ${RECORDED} B · decoded ${DECODED} B · Δsize ${DIFFERENCE} B",
"📦 DB: recorded ${RECORDED} B · decoded unavailable":
"📦 DB: recorded ${RECORDED} B · decoded unavailable",
"📁 Vault: ${VAULT} B · Δsize vs DB ${DIFFERENCE} B":
"📁 Vault: ${VAULT} B · Δsize vs DB ${DIFFERENCE} B",
"🕒 DB ${DATABASE_TIME} · Vault ${VAULT_TIME} · Δtime ${DIFFERENCE} ms (${RELATION})":
"🕒 DB ${DATABASE_TIME} · Vault ${VAULT_TIME} · Δtime ${DIFFERENCE} ms (${RELATION})",
"✅ Matches Vault": "✅ Matches Vault",
"⚠️ Differs from Vault": "⚠️ Differs from Vault",
"✅ Vault matches winner": "✅ Vault matches winner",
"⚠️ Conflicts: ${COUNT}": "⚠️ Conflicts: ${COUNT}",
"Compare with Vault": "Compare with Vault",
"Apply this revision to Vault": "Apply this revision to Vault",
"Apply database revision ${REVISION} to ${FILE}? The current Vault file will be overwritten.":
"Apply database revision ${REVISION} to ${FILE}? The current Vault file will be overwritten.",
"Apply database revision to Vault": "Apply database revision to Vault",
"Mark this revision as the Vault version": "Mark this revision as the Vault version",
"Store Vault file as a child of this revision": "Store Vault file as a child of this revision",
"Apply logical deletion to Vault": "Apply logical deletion to Vault",
"Apply logical deletion ${REVISION} to ${FILE}? The current Vault file will be removed.":
"Apply logical deletion ${REVISION} to ${FILE}? The current Vault file will be removed.",
"Retry reading revision": "Retry reading revision",
"Discard this branch": "Discard this branch",
"Discard branch": "Discard branch",
"Discard database branch ${REVISION} of ${FILE}? This creates a logical deletion for that exact live branch. The current Vault file will not be changed.":
"Discard database branch ${REVISION} of ${FILE}? This creates a logical deletion for that exact live branch. The current Vault file will not be changed.",
"Discard unreadable revision": "Discard unreadable revision",
"Discard database revision ${REVISION} of ${FILE}? This creates a logical deletion for that exact live revision. Missing content cannot be recovered by this action.":
"Discard database revision ${REVISION} of ${FILE}? This creates a logical deletion for that exact live revision. Missing content cannot be recovered by this action.",
"Revision metadata is unavailable on this device": "Revision metadata is unavailable on this device",
"Shared ancestor ${REVISION} is not readable on this device. Automatic three-way merging may be unavailable, but the live revisions remain available for explicit review.":
"Shared ancestor ${REVISION} is not readable on this device. Automatic three-way merging may be unavailable, but the live revisions remain available for explicit review.",
"No shared ancestor is available for this conflict. The live revisions remain available for explicit review.":
"No shared ancestor is available for this conflict. The live revisions remain available for explicit review.",
"More actions for revision ${REVISION}": "More actions for revision ${REVISION}",
"More actions for ${FILE}": "More actions for ${FILE}",
"Show revision history": "Show revision history",
"Store Vault file as a new local database document":
"Store Vault file as a new local database document",
"Copy database information": "Copy database information",
"Recreate chunks for current Vault files": "Recreate chunks for current Vault files",
"Recreate chunks from the files currently present in this Vault. This cannot reconstruct unavailable historical or conflict content.":
"Recreate chunks from the files currently present in this Vault. This cannot reconstruct unavailable historical or conflict content.",
"Recreate current chunks": "Recreate current chunks",
"Resolve every conflict by modification time? This logically deletes every version except the newest one and cannot recover content which is already unavailable.":
"Resolve every conflict by modification time? This logically deletes every version except the newest one and cannot recover content which is already unavailable.",
"Resolve all conflicts by the newest version": "Resolve all conflicts by the newest version",
"Inspect conflicts and file/database differences":
"Inspect conflicts and file/database differences",
"Scan every Vault file and live local-database revision for conflicts, missing chunks, and differences. Each result provides actions for the exact revision.":
"Scan every Vault file and live local-database revision for conflicts, missing chunks, and differences. Each result provides actions for the exact revision.",
"Begin inspection": "Begin inspection",
"Connection settings": "Connection settings",
"Saved connections": "Saved connections",
} as const;
export type LiveSyncProvisionalMessageKey = keyof typeof liveSyncProvisionalEnglishMessages;
-2
View File
@@ -30,8 +30,6 @@ describe("LiveSync-owned translation catalogue", () => {
it("uses LiveSync-owned provisional English without extending Commonlib's message contract", () => {
expect($msg("This file has unresolved conflicts.")).toBe("This file has unresolved conflicts.");
expect($msg("More actions for ${DEVICE}", { DEVICE: "phone" })).toBe("More actions for phone");
expect($msg("Connection settings")).toBe("Connection settings");
expect($msg("Saved connections")).toBe("Saved connections");
expect(
$msg("This file has ${COUNT} unresolved versions. They will be reviewed one pair at a time.", {
COUNT: "3",
+1 -1
View File
@@ -1,4 +1,4 @@
import type { PluginManifest, TFile } from "@/deps.ts";
import { type PluginManifest, TFile } from "@/deps.ts";
import { type DatabaseEntry, type EntryBody, type FilePath } from "@vrtmrz/livesync-commonlib/compat/common/types";
export type { CacheData, FileEventItem } from "@vrtmrz/livesync-commonlib/compat/common/types";
-1
View File
@@ -26,7 +26,6 @@ export {
WorkspaceLeaf,
Menu,
request,
setIcon,
getLanguage,
requireApiVersion,
ButtonComponent,
@@ -1,96 +0,0 @@
import { describe, expect, it, vi } from "vitest";
vi.mock("@/deps.ts", () => ({
addIcon: vi.fn(),
diff_match_patch: class DiffMatchPatch {},
normalizePath: vi.fn((path: string) => path),
Notice: class Notice {},
parseYaml: vi.fn(),
Platform: {},
}));
vi.mock("./PluginDialogModal.ts", () => ({
PluginDialogModal: class PluginDialogModal {},
}));
vi.mock("@/features/HiddenFileCommon/JsonResolveModal.ts", () => ({
JsonResolveModal: class JsonResolveModal {},
}));
vi.mock("@/modules/features/InteractiveConflictResolving/ConflictResolveModal.ts", () => ({
ConflictResolveModal: class ConflictResolveModal {},
}));
vi.mock("@/features/LiveSyncCommands.ts", () => ({
LiveSyncCommands: class LiveSyncCommands {
core!: { services: unknown };
get services() {
return this.core.services;
}
},
}));
vi.mock("@/common/types.ts", () => ({
ICXHeader: "ix:",
PERIODIC_PLUGIN_SWEEP: 60,
}));
vi.mock("@/common/utils.ts", () => ({
EVEN: Symbol("even"),
disposeMemoObject: vi.fn(),
isCustomisationSyncMetadata: vi.fn(),
isPluginMetadata: vi.fn(),
memoIfNotExist: vi.fn(),
memoObject: vi.fn(),
retrieveMemoObject: vi.fn(),
scheduleTask: vi.fn(),
}));
vi.mock("@/common/PeriodicProcessor.ts", () => ({
PeriodicProcessor: class PeriodicProcessor {},
}));
vi.mock("@/common/events.ts", () => ({
EVENT_REQUEST_OPEN_PLUGIN_SYNC_DIALOG: "open-plugin-sync",
eventHub: {
onEvent: vi.fn(),
},
}));
vi.mock("@/common/translation", () => ({
$msg: vi.fn((message: string) => message),
}));
vi.mock("@/common/obsidianCommunityPlugins.ts", () => ({
getObsidianCommunityPluginManager: vi.fn(),
}));
import { ConfigSync } from "./CmdConfigSync";
describe("ConfigSync commands", () => {
it("shows the Customisation Sync command only whilst the feature is enabled", () => {
const commands: Array<{
id: string;
checkCallback?: (checking: boolean) => boolean | void;
}> = [];
const settings = {
usePluginSync: false,
};
const showPluginSyncModal = vi.fn();
const configSync = Object.create(ConfigSync.prototype) as ConfigSync;
Object.assign(configSync, {
core: {
settings,
services: {
API: {
addCommand: vi.fn((command) => commands.push(command)),
},
},
},
addRibbonIcon: vi.fn(() => ({
addClass: vi.fn(),
})),
showPluginSyncModal,
});
configSync.onload();
const command = commands.find(({ id }) => id === "livesync-plugin-dialog-ex");
expect(command?.checkCallback?.(true)).toBe(false);
settings.usePluginSync = true;
expect(command?.checkCallback?.(true)).toBe(true);
expect(command?.checkCallback?.(false)).toBe(true);
expect(showPluginSyncModal).toHaveBeenCalledOnce();
});
});
+2 -8
View File
@@ -454,14 +454,8 @@ export class ConfigSync extends LiveSyncCommands {
this.services.API.addCommand({
id: "livesync-plugin-dialog-ex",
name: "Show customization sync dialog",
checkCallback: (checking) => {
if (!this.isThisModuleEnabled()) {
return false;
}
if (!checking) {
this.showPluginSyncModal();
}
return true;
callback: () => {
this.showPluginSyncModal();
},
});
this.addRibbonIcon("custom-sync", $msg("cmdConfigSync.showCustomizationSync"), () => {
+17 -143
View File
@@ -54,7 +54,10 @@ import { EVENT_SETTING_SAVED, eventHub } from "@/common/events.ts";
import { Semaphore } from "octagonal-wheels/concurrency/semaphore";
import type { LiveSyncCore } from "@/main.ts";
import { tryGetFilePath } from "@vrtmrz/livesync-commonlib/compat/common/utils.doc";
import { configureHiddenFileSyncMode, type ConfigureHiddenFileSyncResult } from "./configureHiddenFileSyncMode.ts";
import {
configureHiddenFileSyncMode,
type ConfigureHiddenFileSyncResult,
} from "./configureHiddenFileSyncMode.ts";
import type { OptionalSyncFeatureMode } from "@/features/optionalSyncFeatures.ts";
import { getObsidianCommunityPluginManager } from "@/common/obsidianCommunityPlugins.ts";
type SyncDirection = "push" | "pull" | "safe" | "pullForce" | "pushForce";
@@ -106,45 +109,37 @@ export class HiddenFileSync extends LiveSyncCommands {
this.services.API.addCommand({
id: "livesync-sync-internal",
name: "(re)initialise hidden files between storage and database",
checkCallback: (checking) => {
if (!this.isManualCommandAvailable()) return false;
if (!checking) {
callback: () => {
if (this.isReady()) {
void this.initialiseInternalFileSync("safe", true);
}
return true;
},
});
this.services.API.addCommand({
id: "livesync-scaninternal-storage",
name: "Scan hidden file changes on the storage",
checkCallback: (checking) => {
if (!this.isManualCommandAvailable()) return false;
if (!checking) {
callback: () => {
if (this.isReady()) {
void this.scanAllStorageChanges(true);
}
return true;
},
});
this.services.API.addCommand({
id: "livesync-scaninternal-database",
name: "Scan hidden file changes on the local database",
checkCallback: (checking) => {
if (!this.isManualCommandAvailable()) return false;
if (!checking) {
callback: () => {
if (this.isReady()) {
void this.scanAllDatabaseChanges(true);
}
return true;
},
});
this.services.API.addCommand({
id: "livesync-internal-scan-offline-changes",
name: "Scan and apply all offline hidden-file changes",
checkCallback: (checking) => {
if (!this.isManualCommandAvailable()) return false;
if (!checking) {
callback: () => {
if (this.isReady()) {
void this.applyOfflineChanges(true);
}
return true;
},
});
eventHub.onEvent(EVENT_SETTING_SAVED, () => {
@@ -196,16 +191,12 @@ export class HiddenFileSync extends LiveSyncCommands {
}
isReady() {
if (!this._isMainReady()) return false;
if (!this._isMainReady) return false;
if (this._isMainSuspended()) return false;
if (!this.isThisModuleEnabled()) return false;
return true;
}
private isManualCommandAvailable() {
return this.settings.useAdvancedMode && this.isReady() && this._isDatabaseReady();
}
async performStartupScan(showNotice: boolean) {
await this.applyOfflineChanges(showNotice);
}
@@ -1530,29 +1521,6 @@ Offline Changed files: ${files.length}`;
}
}
private async getLiveInternalRevision(
prefixedFileName: FilePathWithPrefix,
revision: string
): Promise<MetaEntry | false> {
const [selected, current, conflicts] = await Promise.all([
this.core.databaseFileAccess.fetchEntryMeta(prefixedFileName, revision, true),
this.core.databaseFileAccess.fetchEntryMeta(prefixedFileName, undefined, true),
this.core.databaseFileAccess.getConflictedRevs(prefixedFileName),
]);
const liveRevisions = new Set([
...(current && current._rev ? [current._rev] : []),
...conflicts,
]);
if (!selected || selected._rev !== revision || !liveRevisions.has(revision)) {
this._log(
`Could not use hidden-file revision ${revision} of ${stripAllPrefixes(prefixedFileName)}; the selected revision is no longer live`,
LOG_LEVEL_NOTICE
);
return false;
}
return selected;
}
async storeInternalFileToDatabase(file: InternalFileInfo | UXFileInfo, forceWrite = false) {
const storeFilePath = stripAllPrefixes(file.path);
const storageFilePath = file.path;
@@ -1604,79 +1572,6 @@ Offline Changed files: ${files.length}`;
});
}
async storeInternalFileToDatabaseWithBaseRevision(
file: InternalFileInfo | UXFileInfo,
baseRevision: string,
createIfDifferent = true
): Promise<boolean> {
const storeFilePath = stripAllPrefixes(file.path);
const storageFilePath = file.path;
if (await this.services.vault.isIgnoredByIgnoreFile(storageFilePath)) {
return false;
}
const prefixedFileName = addPrefix(storeFilePath, ICHeader);
return await serialized("file-" + prefixedFileName, async () => {
try {
const baseData = await this.getLiveInternalRevision(prefixedFileName, baseRevision);
if (baseData === false) {
return false;
}
const fileInfo = "stat" in file && "body" in file ? file : await this.loadFileWithInfo(storeFilePath);
if (fileInfo.deleted) {
throw new Error(`Hidden file:${storeFilePath} is deleted. This should not be occurred.`);
}
if (!baseData.deleted && !baseData._deleted) {
const loadedBase = await this.core.databaseFileAccess.fetchEntryFromMeta(baseData, true, true);
if (loadedBase && (await isDocContentSame(readAsBlob(loadedBase), fileInfo.body))) {
this.updateLastProcessed(storeFilePath, baseData, fileInfo.stat);
return true;
}
}
if (!createIfDifferent) {
this._log(
`Could not mark hidden file ${storeFilePath} as revision ${baseRevision}; the storage content differs`,
LOG_LEVEL_NOTICE
);
return false;
}
const storedRevision = await this.core.databaseFileAccess.storeWithBaseRevision(
{
...fileInfo,
path: storeFilePath,
name: fileInfo.name || storeFilePath.split("/").pop() || "",
isInternal: true,
},
baseRevision,
true
);
if (storedRevision === false) {
return false;
}
this.updateLastProcessed(
storeFilePath,
{
...baseData,
_rev: storedRevision,
path: prefixedFileName,
ctime: fileInfo.stat.ctime,
mtime: fileInfo.stat.mtime,
size: fileInfo.stat.size,
deleted: false,
},
fileInfo.stat
);
this._log(`STORAGE --> DB:${storageFilePath}: (hidden, selected branch) Done`);
return true;
} catch (ex) {
this._log(`STORAGE --> DB:${storageFilePath}: (hidden, selected branch) Failed`);
this._log(ex, LOG_LEVEL_VERBOSE);
return false;
}
});
}
async deleteInternalFileOnDatabase(filenameSrc: FilePath, forceWrite = false) {
const storeFilePath = filenameSrc;
const storageFilePath = filenameSrc;
@@ -1740,8 +1635,7 @@ Offline Changed files: ${files.length}`;
metaEntry?: MetaEntry | LoadedEntry,
preventDoubleProcess = true,
onlyNew = false,
includeDeletion = true,
requiredLiveRevision?: string
includeDeletion = true
) {
const prefixedFileName = addPrefix(storageFilePath, ICHeader);
if (await this.services.vault.isIgnoredByIgnoreFile(storageFilePath)) {
@@ -1750,11 +1644,9 @@ Offline Changed files: ${files.length}`;
return await serialized("file-" + prefixedFileName, async () => {
try {
// Check conflicted status
const metaOnDB = requiredLiveRevision
? await this.getLiveInternalRevision(prefixedFileName, requiredLiveRevision)
: metaEntry
? metaEntry
: await this.localDatabase.getDBEntryMeta(prefixedFileName, { conflicts: true }, true);
const metaOnDB = metaEntry
? metaEntry
: await this.localDatabase.getDBEntryMeta(prefixedFileName, { conflicts: true }, true);
if (metaOnDB === false) throw new Error(`File not found on database.:${storageFilePath}`);
// Prevent overwrite for Prevent overwriting while some conflicted revision exists.
if (metaOnDB?._conflicts?.length) {
@@ -1828,24 +1720,6 @@ Offline Changed files: ${files.length}`;
});
}
async extractInternalFileRevisionFromDatabase(
storageFilePath: FilePath,
revision: string,
force = false
): Promise<boolean> {
return Boolean(
await this.extractInternalFileFromDatabase(
storageFilePath,
force,
undefined,
true,
false,
true,
revision
)
);
}
async __checkIsNeedToWriteFile(storageFilePath: FilePath, content: string | ArrayBuffer): Promise<boolean> {
try {
const storageContent = await this.core.storageAccess.readHiddenFileAuto(storageFilePath);
@@ -1,12 +1,5 @@
import { describe, expect, it, vi } from "vitest";
import {
type DocumentID,
LOG_LEVEL_NOTICE,
type FilePath,
type FilePathWithPrefix,
type MetaEntry,
type UXFileInfo,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LOG_LEVEL_NOTICE } from "@vrtmrz/livesync-commonlib/compat/common/types";
vi.mock("@/deps.ts", () => ({}));
vi.mock("@/features/HiddenFileCommon/JsonResolveModal.ts", () => ({
@@ -15,16 +8,13 @@ vi.mock("@/features/HiddenFileCommon/JsonResolveModal.ts", () => ({
vi.mock("@/features/LiveSyncCommands.ts", () => ({
LiveSyncCommands: class LiveSyncCommands {
plugin!: { app: unknown };
core!: { services: unknown; settings: unknown };
core!: { services: unknown };
get app() {
return this.plugin.app;
}
get services() {
return this.core.services;
}
get settings() {
return this.core.settings;
}
},
}));
vi.mock("./configureHiddenFileSyncMode.ts", () => ({
@@ -34,131 +24,7 @@ vi.mock("./configureHiddenFileSyncMode.ts", () => ({
import { HiddenFileSync } from "./CmdHiddenFileSync.ts";
import { configureHiddenFileSyncMode } from "./configureHiddenFileSyncMode.ts";
function createHiddenRevisionOperation() {
const path = ".obsidian/plugins/example/data.json" as FilePath;
const file = {
path,
name: "data.json",
isInternal: true,
body: new Blob(["{\"value\":\"vault\"}"]),
stat: {
ctime: 1,
mtime: 2,
size: 17,
type: "file",
},
} as UXFileInfo;
const selected = {
_id: "i:example" as DocumentID,
_rev: "2-selected",
path: `i:${path}` as FilePathWithPrefix,
ctime: 1,
mtime: 2,
size: 17,
type: "plain",
datatype: "plain",
children: [],
eden: {},
deleted: false,
} as MetaEntry;
const winner = {
...selected,
_rev: "3-winner",
} as MetaEntry;
const databaseFileAccess = {
fetchEntryMeta: vi.fn(
async (_path: unknown, revision?: string) =>
revision === selected._rev ? selected : winner
),
getConflictedRevs: vi.fn(async () => [selected._rev]),
fetchEntryFromMeta: vi.fn(async () => ({ ...selected, data: "{\"value\":\"database\"}" })),
storeWithBaseRevision: vi.fn(async () => "3-vault-child"),
};
const hiddenFileSync = Object.create(HiddenFileSync.prototype) as HiddenFileSync;
Object.assign(hiddenFileSync, {
core: {
services: {
vault: {
isIgnoredByIgnoreFile: vi.fn(async () => false),
},
},
databaseFileAccess,
},
loadFileWithInfo: vi.fn(async () => file),
updateLastProcessed: vi.fn(),
_log: vi.fn(),
});
return {
hiddenFileSync,
path,
file,
selected,
winner,
databaseFileAccess,
};
}
describe("HiddenFileSync configuration-change notices", () => {
it("shows manual Hidden File Sync commands only when the feature, Advanced mode, and runtime are ready", () => {
const commands: Array<{
id: string;
checkCallback?: (checking: boolean) => boolean | void;
}> = [];
const settings = {
syncInternalFiles: false,
useAdvancedMode: false,
};
const hiddenFileSync = Object.create(HiddenFileSync.prototype) as HiddenFileSync;
Object.assign(hiddenFileSync, {
core: {
settings,
services: {
API: {
addCommand: vi.fn((command) => commands.push(command)),
},
},
},
_isMainReady: vi.fn(() => true),
_isMainSuspended: vi.fn(() => false),
_isDatabaseReady: vi.fn(() => true),
});
hiddenFileSync.onload();
const commandIds = [
"livesync-sync-internal",
"livesync-scaninternal-storage",
"livesync-scaninternal-database",
"livesync-internal-scan-offline-changes",
];
for (const commandId of commandIds) {
const command = commands.find(({ id }) => id === commandId);
expect(command?.checkCallback?.(true)).toBe(false);
}
settings.syncInternalFiles = true;
settings.useAdvancedMode = true;
for (const commandId of commandIds) {
const command = commands.find(({ id }) => id === commandId);
expect(command?.checkCallback?.(true)).toBe(true);
}
});
it("does not report Hidden File Sync as ready before the main runtime is ready", () => {
const hiddenFileSync = Object.create(HiddenFileSync.prototype) as HiddenFileSync;
Object.assign(hiddenFileSync, {
core: {
settings: {
syncInternalFiles: true,
},
},
_isMainReady: vi.fn(() => false),
_isMainSuspended: vi.fn(() => false),
});
expect(hiddenFileSync.isReady()).toBe(false);
});
it("groups plug-in reloads and an Obsidian restart into one finished Notice", async () => {
const noticeGroups = {
setItem: vi.fn(),
@@ -341,130 +207,3 @@ describe("HiddenFileSync configuration-change notices", () => {
expect(progress.done).toHaveBeenCalledWith("Failed");
});
});
describe("HiddenFileSync exact revision repair operations", () => {
it("stores the current hidden Vault file as a child of the selected live revision", async () => {
const {
hiddenFileSync,
file,
selected,
databaseFileAccess,
} = createHiddenRevisionOperation();
await expect(
hiddenFileSync.storeInternalFileToDatabaseWithBaseRevision(file, selected._rev!)
).resolves.toBe(true);
expect(databaseFileAccess.storeWithBaseRevision).toHaveBeenCalledWith(
expect.objectContaining({
path: file.path,
body: file.body,
isInternal: true,
}),
selected._rev,
true
);
expect(hiddenFileSync.updateLastProcessed).toHaveBeenCalledWith(
file.path,
expect.objectContaining({ _rev: "3-vault-child" }),
file.stat
);
});
it("refuses to extend a hidden-file revision which is no longer live", async () => {
const {
hiddenFileSync,
file,
selected,
databaseFileAccess,
} = createHiddenRevisionOperation();
databaseFileAccess.getConflictedRevs.mockResolvedValue([]);
await expect(
hiddenFileSync.storeInternalFileToDatabaseWithBaseRevision(file, selected._rev!)
).resolves.toBe(false);
expect(databaseFileAccess.storeWithBaseRevision).not.toHaveBeenCalled();
expect(hiddenFileSync.updateLastProcessed).not.toHaveBeenCalled();
});
it("does not create a hidden-file child when asked only to mark a revision which differs from the Vault", async () => {
const {
hiddenFileSync,
file,
selected,
databaseFileAccess,
} = createHiddenRevisionOperation();
await expect(
hiddenFileSync.storeInternalFileToDatabaseWithBaseRevision(
file,
selected._rev!,
false
)
).resolves.toBe(false);
expect(databaseFileAccess.storeWithBaseRevision).not.toHaveBeenCalled();
expect(hiddenFileSync.updateLastProcessed).not.toHaveBeenCalled();
});
it("marks a matching hidden-file revision without creating a child", async () => {
const {
hiddenFileSync,
file,
selected,
databaseFileAccess,
} = createHiddenRevisionOperation();
databaseFileAccess.fetchEntryFromMeta.mockResolvedValue({
...selected,
data: "{\"value\":\"vault\"}",
});
await expect(
hiddenFileSync.storeInternalFileToDatabaseWithBaseRevision(
file,
selected._rev!,
false
)
).resolves.toBe(true);
expect(databaseFileAccess.storeWithBaseRevision).not.toHaveBeenCalled();
expect(hiddenFileSync.updateLastProcessed).toHaveBeenCalledWith(
file.path,
selected,
file.stat
);
});
it("applies the selected live hidden-file revision through the existing extraction path", async () => {
const {
hiddenFileSync,
path,
selected,
} = createHiddenRevisionOperation();
const extract = vi.fn(async () => true);
hiddenFileSync.extractInternalFileFromDatabase = extract;
await expect(
hiddenFileSync.extractInternalFileRevisionFromDatabase(path, selected._rev!, true)
).resolves.toBe(true);
expect(extract).toHaveBeenCalledWith(path, true, undefined, true, false, true, selected._rev);
});
it("does not apply a hidden-file revision which ceased to be live", async () => {
const {
hiddenFileSync,
path,
selected,
databaseFileAccess,
} = createHiddenRevisionOperation();
databaseFileAccess.getConflictedRevs.mockResolvedValue([]);
await expect(
hiddenFileSync.extractInternalFileRevisionFromDatabase(path, selected._rev!, true)
).resolves.toBe(false);
expect(databaseFileAccess.fetchEntryFromMeta).not.toHaveBeenCalled();
});
});
@@ -1,257 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import PouchDB from "pouchdb-core";
import HttpPouch from "pouchdb-adapter-http";
import MemoryAdapter from "pouchdb-adapter-memory";
import replication from "pouchdb-replication";
vi.mock("@/features/LiveSyncCommands", () => ({
LiveSyncCommands: class LiveSyncCommands {
core!: { settings: unknown };
get settings() {
return this.core.settings;
}
},
}));
vi.mock("@/common/events", () => ({
EVENT_ANALYSE_DB_USAGE: "analyse",
EVENT_REQUEST_PERFORM_GC_V3: "gc",
eventHub: {
onEvent: vi.fn(),
},
}));
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
type DocumentID,
type EntryDoc,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LiveSyncLocalDB } from "@vrtmrz/livesync-commonlib/compat/pouchdb/LiveSyncLocalDB";
import { LocalDatabaseMaintenance } from "./CmdLocalDatabaseMainte";
PouchDB.plugin(HttpPouch).plugin(MemoryAdapter).plugin(replication);
type FixtureContent = {
type: "leaf" | "plain";
data?: string;
path?: string;
children?: string[];
ctime?: number;
mtime?: number;
size?: number;
eden?: Record<string, never>;
};
type FixtureDocument = PouchDB.Core.PutDocument<FixtureContent> & {
_id: DocumentID;
_revisions?: {
start: number;
ids: string[];
};
};
function chunk(id: string, data = id): FixtureDocument {
return {
_id: id as DocumentID,
type: "leaf",
data,
};
}
function revision(id: string, rev: string, history: string[], children: string[]): FixtureDocument {
return {
_id: id as DocumentID,
_rev: rev,
_revisions: {
start: Number(rev.split("-")[0]),
ids: history,
},
type: "plain",
path: id,
children,
ctime: 1,
mtime: 1,
size: children.length,
eden: {},
} as unknown as FixtureDocument;
}
function liveSyncDatabaseFor(database: PouchDB.Database<FixtureContent>): LiveSyncLocalDB {
const subject = Object.create(LiveSyncLocalDB.prototype) as LiveSyncLocalDB;
Object.assign(subject, {
localDatabase: database as unknown as PouchDB.Database<EntryDoc>,
});
return subject;
}
function requiredEnvironment(name: "hostname" | "username" | "password"): string {
const value = process.env[name];
if (!value) {
throw new Error(`Missing required integration-test environment variable: ${name}`);
}
return value;
}
describe("LocalDatabaseMaintenance Garbage Collection V3 with CouchDB", () => {
it("propagates collection safely, completes compaction, and permits content-addressed chunk recreation", async () => {
const databaseName = `livesync-gcv3-${crypto.randomUUID()}`;
const local = new PouchDB<FixtureContent>(`${databaseName}-source`, { adapter: "memory" });
const replica = new PouchDB<FixtureContent>(`${databaseName}-replica`, { adapter: "memory" });
const remote = new PouchDB<FixtureContent>(
`${requiredEnvironment("hostname").replace(/\/+$/u, "")}/${databaseName}`,
{
adapter: "http",
auth: {
username: requiredEnvironment("username"),
password: requiredEnvironment("password"),
},
}
);
try {
await remote.info();
await local.bulkDocs([
chunk("h:obsolete"),
chunk("h:current"),
chunk("h:shared"),
chunk("h:base"),
chunk("h:left"),
chunk("h:right"),
]);
const firstRevision = revision("first.md", "1-first", ["first"], ["h:obsolete"]);
delete firstRevision._rev;
delete firstRevision._revisions;
await local.put(firstRevision);
await local.put({
...(await local.get("first.md")),
children: ["h:current"],
});
for (const id of ["second.md", "third.md"]) {
const sharedRevision = revision(id, `1-${id}`, [id], ["h:shared"]);
delete sharedRevision._rev;
delete sharedRevision._revisions;
await local.put(sharedRevision);
}
await local.bulkDocs(
[
revision("conflicted.md", "1-base", ["base"], ["h:base"]),
revision("conflicted.md", "2-left", ["left", "base"], ["h:left"]),
revision("conflicted.md", "2-right", ["right", "base"], ["h:right"]),
],
{ new_edits: false }
);
const liveSyncDatabase = liveSyncDatabaseFor(local);
const replicationModes: string[] = [];
const replicator = {
openOneShotReplication: vi.fn(
async (
_settings: typeof DEFAULT_SETTINGS,
_showResult: boolean,
_ignoreCleanLock: boolean,
mode: string
) => {
replicationModes.push(mode);
if (mode === "sync") {
await local.sync(remote);
} else if (mode === "pushOnly") {
await local.replicate.to(remote);
} else {
throw new Error(`Unexpected replication mode: ${mode}`);
}
return true;
}
),
getConnectedDeviceList: vi.fn(() =>
Promise.resolve({
accepted_nodes: ["integration-device"],
node_info: {
"integration-device": {
progress: "10-local",
device_name: "Integration device",
app_version: "1.12.7",
plugin_version: "1.0.0-beta.0",
},
},
})
),
connectRemoteCouchDBWithSetting: vi.fn(() => Promise.resolve({ db: remote })),
};
const notice = vi.fn();
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
Object.assign(maintenance, {
core: {
settings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
replicator,
confirm: {
askSelectStringDialogue: vi.fn(() => Promise.resolve("Proceed Garbage Collection")),
},
},
localDatabase: liveSyncDatabase,
_notice: notice,
});
vi.spyOn(maintenance, "ensureAvailable").mockResolvedValue(true);
const clearHash = vi.spyOn(maintenance, "clearHash").mockImplementation(() => undefined);
await maintenance.gcv3();
expect(replicationModes).toEqual(["sync", "pushOnly"]);
expect(clearHash).toHaveBeenCalledOnce();
expect(notice).toHaveBeenCalledWith("Compaction on remote database completed successfully.", "gc-compact");
expect(notice).not.toHaveBeenCalledWith("Compaction on remote database timed out.", "gc-compact");
expect(notice).not.toHaveBeenCalledWith("Compaction on remote database failed.", "gc-compact");
const obsoleteRow = (await remote.allDocs({ keys: ["h:obsolete"] })).rows[0];
expect(obsoleteRow).toMatchObject({
id: "h:obsolete",
value: {
deleted: true,
},
});
for (const retainedChunk of ["h:current", "h:shared", "h:base", "h:left", "h:right"]) {
await expect(remote.get(retainedChunk)).resolves.toMatchObject({
_id: retainedChunk,
type: "leaf",
});
}
await expect(remote.get("second.md")).resolves.toMatchObject({ children: ["h:shared"] });
await expect(remote.get("third.md")).resolves.toMatchObject({ children: ["h:shared"] });
await expect(remote.get("conflicted.md", { conflicts: true })).resolves.toMatchObject({
_conflicts: [expect.any(String)],
});
await remote.replicate.to(replica);
await expect(replica.get("h:obsolete")).rejects.toMatchObject({ status: 404 });
for (const retainedChunk of ["h:current", "h:shared", "h:base", "h:left", "h:right"]) {
await expect(replica.get(retainedChunk)).resolves.toMatchObject({
_id: retainedChunk,
type: "leaf",
});
}
await local.put(chunk("h:obsolete", "recreated"));
await local.replicate.to(remote);
await remote.replicate.to(replica);
await expect(remote.get("h:obsolete")).resolves.toMatchObject({
_id: "h:obsolete",
type: "leaf",
data: "recreated",
});
await expect(replica.get("h:obsolete")).resolves.toMatchObject({
_id: "h:obsolete",
type: "leaf",
data: "recreated",
});
} finally {
await Promise.all([local.destroy(), replica.destroy(), remote.destroy()]);
}
}, 30_000);
});
@@ -4,7 +4,6 @@ import {
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
LOG_LEVEL_VERBOSE,
REMOTE_COUCHDB,
type DocumentID,
type EntryDoc,
type EntryLeaf,
@@ -39,27 +38,16 @@ export class LocalDatabaseMaintenance extends LiveSyncCommands {
id: "analyse-database",
name: "Analyse Database Usage (advanced)",
icon: "database-search",
checkCallback: (checking) => {
if (!this.settings.useAdvancedMode || !this._isDatabaseReady()) return false;
if (!checking) {
void this.analyseDatabase();
}
return true;
callback: async () => {
await this.analyseDatabase();
},
});
this.plugin.addCommand({
id: "gc-v3",
name: "Garbage Collection V3 (advanced, beta)",
icon: "trash-2",
checkCallback: (checking) => {
const isApplicableRemote = this.settings.remoteType === REMOTE_COUCHDB;
if (!this.settings.useEdgeCaseMode || !this._isDatabaseReady() || !isApplicableRemote) {
return false;
}
if (!checking) {
void this.gcv3();
}
return true;
callback: async () => {
await this.gcv3();
},
});
eventHub.onEvent(EVENT_ANALYSE_DB_USAGE, () => this.analyseDatabase());
@@ -462,7 +450,7 @@ Note: **Make sure to synchronise all devices before deletion.**
const confirmMessage = `This function deletes unused chunks from the device. If there are differences between devices, some chunks may be missing when resolving conflicts.
Be sure to synchronise before executing.
If chunks used by current Vault files are deleted, Hatch -> Recreate chunks for current Vault files can recreate them only from files currently present in the Vault. It cannot recover unreadable historical or conflict content.
However, if you have deleted them, you may be able to recover them by performing Hatch -> Recreate missing chunks for all files.
Are you ready to delete unused chunks?`;
@@ -760,7 +748,7 @@ Success: ${successCount}, Errored: ${errored}`;
timeout -= 2000;
if (timeout <= 0) {
this._notice("Compaction on remote database timed out.", "gc-compact");
return;
break;
}
} else {
break;
@@ -883,14 +871,9 @@ It is preferable to update all devices if possible. If you have any devices that
}
//2. Check whether the progress values in NodeData are roughly the same (only the numerical part is needed).
const progressValues = Object.values(node_info).map((entry) => {
const progress = typeof entry.progress === "string" ? entry.progress.split("-")[0] : "";
return /^\d+$/u.test(progress) ? Number(progress) : Number.NaN;
});
if (progressValues.length === 0 || progressValues.some((progress) => !Number.isSafeInteger(progress))) {
this._notice("No connected device information found. Cancelling Garbage Collection.");
return;
}
const progressValues = Object.values(node_info)
.map((e) => e.progress.split("-")[0])
.map((e) => parseInt(e));
const maxProgress = Math.max(...progressValues);
const minProgress = Math.min(...progressValues);
const progressDifference = maxProgress - minProgress;
@@ -929,22 +912,41 @@ This may indicate that some devices have not completed synchronisation, which co
const gcStartTime = Date.now();
// Perform Garbage Collection (new implementation).
const localDatabase = this.localDatabase.localDatabase;
// Use the revision-aware reachability scan. Reading only winning revisions
// would make chunks used exclusively by live conflict branches look unused.
const { used: usedChunks, existing: allChunks } = await this.localDatabase.allChunks();
const usedChunks = new Set<DocumentID>();
const allChunks = new Map<DocumentID, string>();
const IDs = this.localDatabase.findEntryNames("", "", {});
let i = 0;
const doc_count = (await localDatabase.info()).doc_count;
for await (const id of IDs) {
const doc = await this.localDatabase.getRaw(id as DocumentID);
i++;
if (i % 100 == 0) {
this._notice(`Garbage Collection: Scanned ${i} / ~${doc_count} `, "gc-scanning");
}
if (!doc) continue;
if ("children" in doc) {
const children = (doc.children || []) as DocumentID[];
for (const chunkId of children) {
usedChunks.add(chunkId);
}
} else if (doc.type === EntryTypes.CHUNK) {
allChunks.set(doc._id, doc._rev);
}
}
this._notice(
`Garbage Collection: Scanning completed. Total chunks: ${allChunks.size}, Used chunks: ${usedChunks.size}`,
"gc-scanning"
);
const unusedChunks = [...allChunks.entries()].filter(([chunkId]) => !usedChunks.has(chunkId));
const unusedChunks = [...allChunks.keys()].filter((e) => !usedChunks.has(e));
this._notice(`Garbage Collection: Found ${unusedChunks.length} unused chunks to delete.`, "gc-scanning");
const deleteChunkDocs = unusedChunks.map(
([chunkId, chunk]) =>
(chunkId) =>
({
_id: chunkId as DocumentID,
_id: chunkId,
_deleted: true,
_rev: chunk._rev,
_rev: allChunks.get(chunkId),
}) as EntryLeaf
);
const response = await localDatabase.bulkDocs(deleteChunkDocs);
@@ -1,43 +1,5 @@
import { describe, expect, it, vi } from "vitest";
vi.mock("octagonal-wheels/number", () => ({
sizeToHumanReadable: vi.fn((value: number) => `${value} B`),
}));
vi.mock("octagonal-wheels/concurrency/lock_v2", () => ({
serialized: vi.fn((_key: string, task: () => unknown) => task()),
}));
vi.mock("octagonal-wheels/collection", () => ({
arrayToChunkedArray: vi.fn((values: unknown[]) => [values]),
}));
vi.mock("@vrtmrz/livesync-commonlib/compat/common/utils", async (importOriginal) => {
const actual = await importOriginal<typeof import("@vrtmrz/livesync-commonlib/compat/common/utils")>();
return {
...actual,
delay: vi.fn(async () => undefined),
};
});
vi.mock("@/features/LiveSyncCommands", () => ({
LiveSyncCommands: class LiveSyncCommands {
core!: { settings: unknown };
get settings() {
return this.core.settings;
}
},
}));
vi.mock("@/common/events", () => ({
EVENT_ANALYSE_DB_USAGE: "analyse",
EVENT_REQUEST_PERFORM_GC_V3: "gc",
eventHub: {
onEvent: vi.fn(),
},
}));
import {
DEFAULT_SETTINGS,
REMOTE_COUCHDB,
REMOTE_MINIO,
REMOTE_P2P,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { LocalDatabaseMaintenance } from "./CmdLocalDatabaseMainte";
import { DEFAULT_SETTINGS } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { ensureLocalDatabaseMaintenancePrerequisites } from "./maintenancePrerequisites";
function createPrerequisites(settingsOverride: Partial<typeof DEFAULT_SETTINGS> = {}) {
@@ -60,52 +22,6 @@ function createPrerequisites(settingsOverride: Partial<typeof DEFAULT_SETTINGS>
}
describe("LocalDatabaseMaintenance prerequisites", () => {
it("shows database analysis in Advanced mode and Garbage Collection only in applicable Edge Case mode", () => {
const commands: Array<{
id: string;
checkCallback?: (checking: boolean) => boolean | void;
}> = [];
const settings: {
useAdvancedMode: boolean;
useEdgeCaseMode: boolean;
remoteType: string;
} = {
useAdvancedMode: false,
useEdgeCaseMode: false,
remoteType: REMOTE_COUCHDB,
};
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
Object.assign(maintenance, {
plugin: {
addCommand: vi.fn((command) => commands.push(command)),
},
core: {
settings,
},
_isDatabaseReady: vi.fn(() => true),
});
maintenance.onload();
const analyse = commands.find(({ id }) => id === "analyse-database");
const garbageCollect = commands.find(({ id }) => id === "gc-v3");
expect(analyse?.checkCallback?.(true)).toBe(false);
expect(garbageCollect?.checkCallback?.(true)).toBe(false);
settings.useAdvancedMode = true;
expect(analyse?.checkCallback?.(true)).toBe(true);
expect(garbageCollect?.checkCallback?.(true)).toBe(false);
settings.useEdgeCaseMode = true;
expect(garbageCollect?.checkCallback?.(true)).toBe(true);
settings.remoteType = REMOTE_P2P;
expect(garbageCollect?.checkCallback?.(true)).toBe(false);
settings.remoteType = REMOTE_MINIO;
expect(garbageCollect?.checkCallback?.(true)).toBe(false);
});
it("asks to disable on-demand chunk fetching before maintenance actions", async () => {
const { settings, askSelectStringDialogue, applyPartial } = createPrerequisites();
@@ -191,231 +107,4 @@ describe("LocalDatabaseMaintenance prerequisites", () => {
expect(askSelectStringDialogue).not.toHaveBeenCalled();
expect(applyPartial).not.toHaveBeenCalled();
});
it("describes the current chunk-recreation action without promising historical recovery", async () => {
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
const askSelectStringDialogue = vi.fn().mockResolvedValue("Cancel");
Object.assign(maintenance, {
core: {
confirm: {
askSelectStringDialogue,
},
},
_log: vi.fn(),
});
vi.spyOn(maintenance, "ensureAvailable").mockResolvedValue(true);
vi.spyOn(maintenance, "trackChanges").mockResolvedValue(undefined);
await maintenance.performGC();
const message = vi.mocked(askSelectStringDialogue).mock.calls[0]?.[0] as string;
expect(message).toContain("Hatch -> Recreate chunks for current Vault files");
expect(message).toContain("only from files currently present in the Vault");
expect(message).not.toContain("Recreate missing chunks for all files");
});
});
describe("LocalDatabaseMaintenance Garbage Collection V3", () => {
it("does not report remote compaction as successful after its completion wait times out", async () => {
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
const notice = vi.fn();
const remoteDatabase = {
compact: vi.fn(async () => ({ ok: true })),
info: vi.fn(async () => ({ compact_running: true })),
};
Object.assign(maintenance, {
core: {
replicator: {
connectRemoteCouchDBWithSetting: vi.fn(async () => ({ db: remoteDatabase })),
},
settings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
},
_notice: notice,
});
await maintenance.compactDatabase();
expect(notice).toHaveBeenCalledWith("Compaction on remote database timed out.", "gc-compact");
expect(notice).not.toHaveBeenCalledWith(
"Compaction on remote database completed successfully.",
"gc-compact"
);
});
it.each([
["no device progress entries", {}],
[
"an unparseable device progress entry",
{
"device-a": {
progress: "",
device_name: "Device A",
app_version: "1.12.7",
plugin_version: "1.0.0-beta.0",
},
},
],
[
"a missing device progress entry",
{
"device-a": {
device_name: "Device A",
app_version: "1.12.7",
plugin_version: "1.0.0-beta.0",
},
},
],
] as const)("cancels before collection when the milestone has %s", async (_case, nodeInfo) => {
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
const pushModes: string[] = [];
const allChunks = vi.fn(async () => ({
used: new Set<string>(),
existing: new Map(),
}));
const replicator = {
openOneShotReplication: vi.fn(async (...args: unknown[]) => {
pushModes.push(String(args[3]));
return true;
}),
getConnectedDeviceList: vi.fn(async () => ({
accepted_nodes: Object.keys(nodeInfo),
node_info: nodeInfo,
})),
};
const notice = vi.fn();
Object.assign(maintenance, {
core: {
settings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
replicator,
confirm: {
askSelectStringDialogue: vi.fn(async () => "Proceed Garbage Collection"),
},
},
localDatabase: {
allChunks,
localDatabase: {
bulkDocs: vi.fn(async () => []),
},
},
_notice: notice,
});
vi.spyOn(maintenance, "ensureAvailable").mockResolvedValue(true);
vi.spyOn(maintenance, "compactDatabase").mockResolvedValue(undefined);
vi.spyOn(maintenance, "clearHash").mockImplementation(() => undefined);
await maintenance.gcv3();
expect(allChunks).not.toHaveBeenCalled();
expect(pushModes).toEqual(["sync"]);
expect(notice).toHaveBeenCalledWith(
"No connected device information found. Cancelling Garbage Collection."
);
});
it("keeps chunks referenced by a live conflict revision and deletes only unreachable chunks", async () => {
const maintenance = Object.create(LocalDatabaseMaintenance.prototype) as LocalDatabaseMaintenance;
const pushModes: string[] = [];
const deletedChunks: Array<{ _id: string; _rev?: string; _deleted?: boolean }> = [];
const allChunks = vi.fn(async () => ({
used: new Set(["h:winner", "h:conflict"]),
existing: new Map([
["h:winner", { _id: "h:winner", _rev: "1-winner", type: "leaf", data: "winner" }],
["h:conflict", { _id: "h:conflict", _rev: "1-conflict", type: "leaf", data: "conflict" }],
["h:obsolete", { _id: "h:obsolete", _rev: "1-obsolete", type: "leaf", data: "obsolete" }],
]),
}));
const rawDocuments = new Map<string, object>([
[
"note.md",
{
_id: "note.md",
_rev: "2-winner",
_conflicts: ["2-conflict"],
type: "plain",
children: ["h:winner"],
},
],
["h:winner", { _id: "h:winner", _rev: "1-winner", type: "leaf", data: "winner" }],
["h:conflict", { _id: "h:conflict", _rev: "1-conflict", type: "leaf", data: "conflict" }],
["h:obsolete", { _id: "h:obsolete", _rev: "1-obsolete", type: "leaf", data: "obsolete" }],
]);
const findEntryNames = vi.fn(async function* () {
yield* rawDocuments.keys();
});
const getRaw = vi.fn(async (id: string) => rawDocuments.get(id));
const localDatabase = {
allChunks,
localDatabase: {
info: vi.fn(async () => ({ doc_count: rawDocuments.size })),
bulkDocs: vi.fn(async (docs: Array<{ _id: string; _rev?: string; _deleted?: boolean }>) => {
deletedChunks.push(...docs);
return docs.map(({ _id }) => ({ ok: true, id: _id, rev: "2-deleted" }));
}),
},
findEntryNames,
getRaw,
};
const replicator = {
openOneShotReplication: vi.fn(
async (
_settings: typeof DEFAULT_SETTINGS,
_showResult: boolean,
_ignoreCleanLock: boolean,
mode: string
) => {
pushModes.push(mode);
return true;
}
),
getConnectedDeviceList: vi.fn(async () => ({
accepted_nodes: ["device-a"],
node_info: {
"device-a": {
progress: "10-local",
device_name: "Device A",
app_version: "1.12.7",
plugin_version: "1.0.0-beta.0",
},
},
})),
};
Object.assign(maintenance, {
core: {
settings: {
...DEFAULT_SETTINGS,
remoteType: REMOTE_COUCHDB,
},
replicator,
confirm: {
askSelectStringDialogue: vi.fn(async () => "Proceed Garbage Collection"),
},
},
localDatabase,
_notice: vi.fn(),
});
vi.spyOn(maintenance, "ensureAvailable").mockResolvedValue(true);
vi.spyOn(maintenance, "compactDatabase").mockResolvedValue(undefined);
vi.spyOn(maintenance, "clearHash").mockImplementation(() => undefined);
await maintenance.gcv3();
expect(allChunks).toHaveBeenCalledOnce();
expect(findEntryNames).not.toHaveBeenCalled();
expect(getRaw).not.toHaveBeenCalled();
expect(deletedChunks).toEqual([
{
_id: "h:obsolete",
_rev: "1-obsolete",
_deleted: true,
},
]);
expect(pushModes).toEqual(["sync", "pushOnly"]);
expect(maintenance.compactDatabase).toHaveBeenCalledOnce();
});
});
@@ -1,14 +1,14 @@
<script lang="ts">
import { onMount } from "svelte";
import { onMount, setContext } from "svelte";
import { AutoAccepting, DEFAULT_SETTINGS, type P2PSyncSetting } from "@vrtmrz/livesync-commonlib/compat/common/types";
import {
AcceptedStatus,
ConnectionStatus,
type PeerStatus,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { P2PReplicatorPaneHost } from "./P2PReplicatorPaneHost";
import type { P2PReplicatorPaneController } from "./P2PReplicatorPaneController";
import PeerStatusRow from "@/features/P2PSync/P2PReplicator/PeerStatusRow.svelte";
import { EVENT_LAYOUT_READY } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import { EVENT_LAYOUT_READY, eventHub } from "@/common/events";
import {
type PeerInfo,
type P2PServerInfo,
@@ -20,16 +20,16 @@
import { $msg as _msg } from "@/common/translation";
import { SETTING_KEY_P2P_DEVICE_NAME } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { generateP2PRoomId } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore";
interface Props {
host: P2PReplicatorPaneHost;
getCmdSync: () => P2PReplicatorPaneController;
core: Pick<LiveSyncBaseCore, "services">;
}
let { host }: Props = $props();
let services = $derived(host.services);
let events = $derived(services.context.events);
const currentSettings = () => services.setting.currentSettings() as P2PSyncSetting;
const currentReplicator = () => host.p2p.replicator;
let { getCmdSync, core }: Props = $props();
setContext("getReplicator", () => getCmdSync());
const currentSettings = () => core.services.setting.currentSettings() as P2PSyncSetting;
const initialSettings = { ...currentSettings() } as P2PSyncSetting;
let settings = $state<P2PSyncSetting>(initialSettings);
@@ -81,7 +81,7 @@
// P2P_AutoStart: eAutoStart,
// P2P_AutoBroadcast: eAutoBroadcast,
// };
await services.setting.applyPartial(
await core.services.setting.applyPartial(
{
P2P_Enabled: eP2PEnabled,
P2P_relays: eRelay,
@@ -94,7 +94,7 @@
},
true
);
services.config.setSmallConfig(SETTING_KEY_P2P_DEVICE_NAME, eDeviceName);
core.services.config.setSmallConfig(SETTING_KEY_P2P_DEVICE_NAME, eDeviceName);
deviceName = eDeviceName;
}
async function revert() {
@@ -113,7 +113,7 @@
const applyLoadSettings = (d: P2PSyncSetting, force: boolean) => {
if (force) {
const initDeviceName =
services.config.getSmallConfig(SETTING_KEY_P2P_DEVICE_NAME) ?? services.vault.getVaultName();
core.services.config.getSmallConfig(SETTING_KEY_P2P_DEVICE_NAME) ?? core.services.vault.getVaultName();
deviceName = initDeviceName;
eDeviceName = initDeviceName;
}
@@ -131,22 +131,21 @@
settings = d;
};
onMount(() => {
const r = events.onEvent("setting-saved", async (d) => {
const r = eventHub.onEvent("setting-saved", async (d) => {
applyLoadSettings(d, false);
closeServer();
});
const rx = events.onEvent(EVENT_LAYOUT_READY, () => {
const rx = eventHub.onEvent(EVENT_LAYOUT_READY, () => {
applyLoadSettings(currentSettings(), true);
});
const r2 = events.onEvent(EVENT_SERVER_STATUS, (status) => {
const r2 = eventHub.onEvent(EVENT_SERVER_STATUS, (status) => {
serverInfo = status;
advertisements = status?.knownAdvertisements ?? [];
});
const r3 = events.onEvent(EVENT_P2P_REPLICATOR_STATUS, (status) => {
const r3 = eventHub.onEvent(EVENT_P2P_REPLICATOR_STATUS, (status) => {
replicatorInfo = status;
});
applyLoadSettings(currentSettings(), true);
events.emitEvent(EVENT_REQUEST_STATUS);
eventHub.emitEvent(EVENT_REQUEST_STATUS);
return () => {
r();
rx();
@@ -223,22 +222,22 @@
}
async function openServer() {
await currentReplicator().open();
await getCmdSync().open();
}
async function closeServer() {
await currentReplicator().close();
await getCmdSync().close();
}
function startBroadcasting() {
currentReplicator().enableBroadcastChanges();
void getCmdSync().enableBroadcastChanges();
}
function stopBroadcasting() {
currentReplicator().disableBroadcastChanges();
void getCmdSync().disableBroadcastChanges();
}
const initialDialogStatusKey = `p2p-dialog-status`;
const getDialogStatus = () => {
try {
const initialDialogStatus = JSON.parse(services.config.getSmallConfig(initialDialogStatusKey) ?? "{}") as {
const initialDialogStatus = JSON.parse(core.services.config.getSmallConfig(initialDialogStatusKey) ?? "{}") as {
notice?: boolean;
setting?: boolean;
};
@@ -255,10 +254,10 @@
notice: isNoticeOpened,
setting: isSettingOpened,
};
services.config.setSmallConfig(initialDialogStatusKey, JSON.stringify(dialogStatus));
core.services.config.setSmallConfig(initialDialogStatusKey, JSON.stringify(dialogStatus));
});
let isObsidian = $derived.by(() => {
return services.API.getPlatform() === "obsidian";
return core.services.API.getPlatform() === "obsidian";
});
</script>
@@ -435,7 +434,7 @@
</thead>
<tbody>
{#each peers as peer}
<PeerStatusRow peerStatus={peer} p2p={host.p2p} showPeerMenu={host.showPeerMenu}></PeerStatusRow>
<PeerStatusRow peerStatus={peer}></PeerStatusRow>
{/each}
</tbody>
</table>
@@ -0,0 +1,17 @@
import type {
AcceptanceDecision,
RevokeAcceptanceDecision,
} from "@vrtmrz/livesync-commonlib/compat/replication/trystero/TrysteroReplicatorP2PServer";
/** The operations used by the shared P2P pane, independent of its replicator implementation. */
export interface P2PReplicatorPaneController {
open(): Promise<void>;
close(): Promise<void>;
enableBroadcastChanges(): void;
disableBroadcastChanges(): void;
makeDecision(decision: AcceptanceDecision): Promise<void>;
revokeDecision(decision: RevokeAcceptanceDecision): Promise<void>;
watchPeer(peerId: string): void;
unwatchPeer(peerId: string): void;
sync(peerId: string, showNotice?: boolean): Promise<unknown>;
}
@@ -1,12 +0,0 @@
import type { RequiredServices } from "@vrtmrz/livesync-commonlib/compat/interfaces/ServiceModule";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { UseP2PReplicatorResult } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
export type P2PReplicatorHandle = Pick<UseP2PReplicatorResult, "replicator">;
/** Host capabilities consumed by the shared P2P pane. */
export interface P2PReplicatorPaneHost {
readonly services: RequiredServices<"API" | "config" | "setting" | "vault">;
readonly p2p: P2PReplicatorHandle;
readonly showPeerMenu?: (peer: PeerStatus, event: MouseEvent) => void;
}
@@ -2,11 +2,12 @@ import { Menu, WorkspaceLeaf } from "@/deps.ts";
import ReplicatorPaneComponent from "./P2PReplicatorPane.svelte";
import { mount } from "svelte";
import { SvelteItemView } from "@/common/SvelteItemView.ts";
import { eventHub } from "@/common/events.ts";
import { unique } from "octagonal-wheels/collection";
import { LOG_LEVEL_NOTICE, REMOTE_P2P } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { Logger } from "@vrtmrz/livesync-commonlib/compat/common/logger";
import type { PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import { EVENT_P2P_PEER_SHOW_EXTRA_MENU, type PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { LiveSyncBaseCore } from "@/LiveSyncBaseCore.ts";
import type { P2PPaneParams } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/UseP2PReplicatorResult";
export const VIEW_TYPE_P2P = "p2p-replicator";
@@ -126,45 +127,46 @@ And you can also drop the local database to rebuild from the remote device.`,
super(leaf);
this.core = core;
this._p2pResult = p2pResult;
}
private showPeerMenu(peer: PeerStatus, event: MouseEvent): void {
this.m?.hide();
this.m = new Menu()
.addItem((item) => item.setTitle("📥 Only Fetch").onClick(() => this.replicateFrom(peer)))
.addItem((item) => item.setTitle("📤 Only Send").onClick(() => this.replicateTo(peer)))
.addSeparator()
.addItem((item) => {
item.setTitle("🔧 Get Configuration").onClick(async () => {
await this.getRemoteConfig(peer);
eventHub.onEvent(EVENT_P2P_PEER_SHOW_EXTRA_MENU, ({ peer, event }) => {
if (this.m) {
this.m.hide();
}
this.m = new Menu()
.addItem((item) => item.setTitle("📥 Only Fetch").onClick(() => this.replicateFrom(peer)))
.addItem((item) => item.setTitle("📤 Only Send").onClick(() => this.replicateTo(peer)))
.addSeparator()
.addItem((item) => {
item.setTitle("🔧 Get Configuration").onClick(async () => {
await this.getRemoteConfig(peer);
});
})
.addSeparator()
.addItem((item) => {
const mark = peer.syncOnConnect ? "checkmark" : null;
item.setTitle("Toggle Sync on connect")
.onClick(async () => {
await this.toggleProp(peer, "syncOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.watchOnConnect ? "checkmark" : null;
item.setTitle("Toggle Watch on connect")
.onClick(async () => {
await this.toggleProp(peer, "watchOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.syncOnReplicationCommand ? "checkmark" : null;
item.setTitle("Toggle Sync on `Replicate now` command")
.onClick(async () => {
await this.toggleProp(peer, "syncOnReplicationCommand");
})
.setIcon(mark);
});
})
.addSeparator()
.addItem((item) => {
const mark = peer.syncOnConnect ? "checkmark" : null;
item.setTitle("Toggle Sync on connect")
.onClick(async () => {
await this.toggleProp(peer, "syncOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.watchOnConnect ? "checkmark" : null;
item.setTitle("Toggle Watch on connect")
.onClick(async () => {
await this.toggleProp(peer, "watchOnConnect");
})
.setIcon(mark);
})
.addItem((item) => {
const mark = peer.syncOnReplicationCommand ? "checkmark" : null;
item.setTitle("Toggle Sync on `Replicate now` command")
.onClick(async () => {
await this.toggleProp(peer, "syncOnReplicationCommand");
})
.setIcon(mark);
});
void this.m.showAtPosition({ x: event.x, y: event.y });
this.m.showAtPosition({ x: event.x, y: event.y });
});
}
getViewType() {
@@ -185,11 +187,8 @@ And you can also drop the local database to rebuild from the remote device.`,
return mount(ReplicatorPaneComponent, {
target: target,
props: {
host: {
services: this.core.services,
p2p: this._p2pResult,
showPeerMenu: (peer: PeerStatus, event: MouseEvent) => this.showPeerMenu(peer, event),
},
getCmdSync: () => this._p2pResult.replicator,
core: this.core,
},
});
}
@@ -1,22 +1,20 @@
<script lang="ts">
import { getContext } from "svelte";
import { AcceptedStatus, type PeerStatus } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
import type { P2PReplicatorHandle } from "./P2PReplicatorPaneHost";
import type { P2PReplicatorPaneController } from "./P2PReplicatorPaneController";
import { eventHub } from "@/common/events";
import { EVENT_P2P_PEER_SHOW_EXTRA_MENU } from "@vrtmrz/livesync-commonlib/compat/replication/trystero/P2PReplicatorPaneCommon";
interface Props {
peerStatus: PeerStatus;
p2p: P2PReplicatorHandle;
showPeerMenu?: (peer: PeerStatus, event: MouseEvent) => void;
}
let { peerStatus, p2p, showPeerMenu }: Props = $props();
let { peerStatus }: Props = $props();
let peer = $derived(peerStatus);
const currentReplicator = () => p2p.replicator;
function select<T extends PropertyKey, U, V = undefined>(
d: T,
cond: Partial<Record<T, U>>,
def: V | undefined = undefined
): U | V | undefined {
function select<T extends string | number | symbol, U>(d: T, cond: Record<T, U>): U;
function select<T extends string | number | symbol, U, V>(d: T, cond: Record<T, U>, def: V): U | V;
function select<T extends string | number | symbol, U>(d: T, cond: Record<T, U>, def?: U): U | undefined {
return d in cond ? cond[d] : def;
}
@@ -38,7 +36,7 @@
[AcceptedStatus.UNKNOWN]: "NEW",
},
""
) ?? ""
)
);
const classList = {
["SENDING"]: "connected",
@@ -59,7 +57,7 @@
let isNew = $derived.by(() => peer.accepted === AcceptedStatus.UNKNOWN);
function makeDecision(isAccepted: boolean, isTemporary: boolean) {
currentReplicator().makeDecision({
getReplicator().makeDecision({
peerId: peer.peerId,
name: peer.name,
decision: isAccepted,
@@ -67,11 +65,13 @@
});
}
function revokeDecision() {
currentReplicator().revokeDecision({
getReplicator().revokeDecision({
peerId: peer.peerId,
name: peer.name,
});
}
const getReplicator = getContext<() => P2PReplicatorPaneController>("getReplicator");
const peerAttrLabels = $derived.by(() => {
const attrs = [];
if (peer.syncOnConnect) {
@@ -86,18 +86,18 @@
return attrs;
});
function startWatching() {
currentReplicator().watchPeer(peer.peerId);
getReplicator().watchPeer(peer.peerId);
}
function stopWatching() {
currentReplicator().unwatchPeer(peer.peerId);
getReplicator().unwatchPeer(peer.peerId);
}
function sync() {
void currentReplicator().sync(peer.peerId, false);
void getReplicator().sync(peer.peerId, false);
}
function moreMenu(evt: MouseEvent) {
showPeerMenu?.(peer, evt);
eventHub.emitEvent(EVENT_P2P_PEER_SHOW_EXTRA_MENU, { peer, event: evt });
}
</script>
@@ -159,9 +159,7 @@
{:else}
<button class="button" onclick={startWatching} title="live"></button>
{/if}
{#if showPeerMenu}
<button class="button" onclick={moreMenu}>...</button>
{/if}
<button class="button" onclick={moreMenu}>...</button>
</div>
</div>
{/if}
@@ -86,11 +86,8 @@ export class ModuleConflictResolver extends AbstractModule {
return MISSING_OR_ERROR;
}
if (rightLeaf == false) {
// A locally unreadable conflict leaf may still be recoverable from another
// replica or backup. Keep it visible for explicit repair instead of treating
// missing chunks as evidence that the branch is obsolete.
this._log(`could not read conflicted revision ${rightRev}:${path}`, LOG_LEVEL_NOTICE);
return MISSING_OR_ERROR;
// Conflicted item could not load, delete this.
return await this.services.conflict.resolveByDeletingRevision(path, rightRev, "MISSING OLD REV");
}
const isSame = leftLeaf.data == rightLeaf.data && leftLeaf.deleted == rightLeaf.deleted;
@@ -4,7 +4,6 @@ import {
DEFAULT_SETTINGS,
LOG_LEVEL_INFO,
LOG_LEVEL_NOTICE,
MISSING_OR_ERROR,
type FilePathWithPrefix,
type MetaEntry,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
@@ -190,28 +189,6 @@ describe("ModuleConflictResolver independent same-path creation", () => {
});
describe("ModuleConflictResolver sensible merge hand-off", () => {
it("keeps an unreadable non-winner revision unresolved", async () => {
const path = "missing-conflict-body.md" as FilePathWithPrefix;
const { module, resolveByDeletingRevision, tryAutoMerge } = createModule();
tryAutoMerge.mockResolvedValue({
leftRev: "3-current",
rightRev: "2-unreadable",
leftLeaf: {
rev: "3-current",
data: "Readable current body\n",
ctime: 1,
mtime: 3,
deleted: false,
},
rightLeaf: false,
});
const result = await module.checkConflictAndPerformAutoMerge(path);
expect(result).toBe(MISSING_OR_ERROR);
expect(resolveByDeletingRevision).not.toHaveBeenCalled();
});
it("stores the merged body and removes the resolved conflict leaf", async () => {
const path = "sensible.md" as FilePathWithPrefix;
const { module, resolveByDeletingRevision, tryAutoMerge } = createModule();
+10 -23
View File
@@ -2,29 +2,24 @@ import type { LiveSyncCore } from "@/main";
import { LOG_LEVEL_NOTICE } from "octagonal-wheels/common/logger";
import { fireAndForget } from "octagonal-wheels/promises";
import { AbstractModule } from "@/modules/AbstractModule";
import { $msg } from "@/common/translation";
import { copyFileDatabaseInfo } from "@/serviceFeatures/fileDatabaseInfo";
// Separated Module for basic menu commands, which are not related to obsidian specific features. It is expected to be used in other platforms with minimal changes.
// However, it is odd that it has here at all; it really ought to be in each respective feature. It will likely be moved eventually. Until now, addCommand pointed to Obsidian's version.
export class ModuleBasicMenu extends AbstractModule {
_everyOnloadStart(): Promise<boolean> {
this.addCommand({
id: "livesync-replicate",
name: $msg("Sync now"),
name: "Replicate now",
callback: async () => {
await this.services.replication.replicate();
},
});
this.addCommand({
id: "livesync-dump",
name: $msg("Copy database information for the active file"),
checkCallback: (checking) => {
name: "Dump information of this doc ",
callback: () => {
const file = this.services.vault.getActiveFilePath();
if (!file) return false;
if (!checking) {
fireAndForget(() => copyFileDatabaseInfo(this.core, file));
}
return true;
if (!file) return;
fireAndForget(() => this.localDatabase.getDBEntry(file, {}, true, false));
},
});
this.addCommand({
@@ -61,18 +56,14 @@ export class ModuleBasicMenu extends AbstractModule {
this.addCommand({
id: "livesync-scan-files",
name: "Scan storage and database again",
checkCallback: (checking) => {
if (!this.settings.useAdvancedMode) return false;
if (!checking) {
fireAndForget(() => this.services.vault.scanVault(true));
}
return true;
callback: async () => {
await this.services.vault.scanVault(true);
},
});
this.addCommand({
id: "livesync-runbatch",
name: $msg("Apply pending changes now"),
name: "Run pended batch processes",
callback: async () => {
await this.services.fileProcessing.commitPendingFileEvents();
},
@@ -82,12 +73,8 @@ export class ModuleBasicMenu extends AbstractModule {
this.addCommand({
id: "livesync-abortsync",
name: "Abort synchronization immediately",
checkCallback: (checking) => {
if (!this.settings.useAdvancedMode) return false;
if (!checking) {
this.core.replicator.terminateSync();
}
return true;
callback: () => {
this.core.replicator.terminateSync();
},
});
return Promise.resolve(true);
@@ -1,170 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import type { Command } from "@/deps";
import { ModuleBasicMenu } from "./ModuleBasicMenu";
type RegisteredCommand = Command & {
checkCallback?: (checking: boolean) => boolean | void;
};
function createFixture() {
const commands: RegisteredCommand[] = [];
const settings = {
liveSync: false,
useAdvancedMode: false,
enableDebugTools: false,
};
const services = {
API: {
addLog: vi.fn(),
addCommand: vi.fn((command: RegisteredCommand) => {
commands.push(command);
return command;
}),
registerWindow: vi.fn(),
addRibbonIcon: vi.fn(),
registerProtocolHandler: vi.fn(),
},
replication: {
replicate: vi.fn(async () => undefined),
},
vault: {
getActiveFilePath: vi.fn((): string | null => "note.md"),
scanVault: vi.fn(async () => undefined),
},
control: {
applySettings: vi.fn(async () => undefined),
},
setting: {
saveSettingData: vi.fn(async () => undefined),
},
appLifecycle: {
isSuspended: vi.fn(() => false),
setSuspended: vi.fn(),
},
fileProcessing: {
commitPendingFileEvents: vi.fn(async () => true),
},
UI: {
promptCopyToClipboard: vi.fn(async (_title: string, _value: string) => true),
},
path: {
path2id: vi.fn(async () => "f:note"),
},
};
const core = {
settings,
_services: services,
services,
localDatabase: {
getDBEntry: vi.fn(async () => false),
localDatabase: {
get: vi.fn(async () => ({
_id: "f:note",
_rev: "2-current",
_conflicts: [],
path: "note.md",
ctime: 100,
mtime: 200,
size: 12,
type: "plain",
children: ["h:private-chunk-id"],
eden: {},
})),
},
getDBEntryMeta: vi.fn(async () => ({
_id: "f:note",
_rev: "2-current",
_conflicts: [],
path: "note.md",
ctime: 100,
mtime: 200,
size: 12,
type: "plain",
datatype: "plain",
data: "",
children: ["h:private-chunk-id"],
eden: {},
})),
allDocsRaw: vi.fn(async () => ({
rows: [{ id: "h:private-chunk-id", key: "h:private-chunk-id", value: { rev: "1-chunk" } }],
})),
},
storageAccess: {
isExistsIncludeHidden: vi.fn(async () => true),
statHidden: vi.fn(async () => ({ ctime: 100, mtime: 200, size: 12, type: "file" })),
},
replicator: {
terminateSync: vi.fn(),
},
};
const module = new ModuleBasicMenu(core as never);
return {
commands,
core,
module,
services,
settings,
getCommand(id: string) {
const command = commands.find((candidate) => candidate.id === id);
expect(command, `command ${id}`).toBeDefined();
return command!;
},
};
}
describe("ModuleBasicMenu command palette", () => {
it("uses clear user-facing names without changing the established command IDs", async () => {
const fixture = createFixture();
await fixture.module._everyOnloadStart();
expect(fixture.getCommand("livesync-replicate").name).toBe("Sync now");
expect(fixture.getCommand("livesync-runbatch").name).toBe("Apply pending changes now");
});
it("keeps maintenance commands out of the normal palette", async () => {
const fixture = createFixture();
await fixture.module._everyOnloadStart();
expect(fixture.getCommand("livesync-scan-files").checkCallback?.(true)).toBe(false);
expect(fixture.getCommand("livesync-abortsync").checkCallback?.(true)).toBe(false);
fixture.settings.useAdvancedMode = true;
expect(fixture.getCommand("livesync-scan-files").checkCallback?.(true)).toBe(true);
expect(fixture.getCommand("livesync-abortsync").checkCallback?.(true)).toBe(true);
});
it("keeps active-file database information available and opens it in a copy dialogue", async () => {
const fixture = createFixture();
await fixture.module._everyOnloadStart();
const command = fixture.getCommand("livesync-dump");
expect(command.name).toBe("Copy database information for the active file");
expect(command.checkCallback?.(true)).toBe(true);
command.checkCallback?.(false);
await vi.waitFor(() => {
expect(fixture.services.UI.promptCopyToClipboard).toHaveBeenCalledOnce();
});
const [title, report] = fixture.services.UI.promptCopyToClipboard.mock.calls[0];
expect(title).toBe("Database information for note.md");
expect(report).toContain("note.md");
expect(report).toContain("2-current");
expect(report).toContain("h:private-chunk-id");
expect(report).toContain("1-chunk");
expect(fixture.core.localDatabase.getDBEntry).not.toHaveBeenCalled();
});
it("hides the active-file database report when no file is active", async () => {
const fixture = createFixture();
fixture.services.vault.getActiveFilePath.mockReturnValue(null);
await fixture.module._everyOnloadStart();
expect(fixture.getCommand("livesync-dump").checkCallback?.(true)).toBe(false);
});
});
@@ -13,13 +13,6 @@ export const POSTPONED = Symbol("postponed");
export type MergeDialogResult = typeof CANCELLED | typeof POSTPONED | typeof LEAVE_TO_SUBSEQUENT | string;
export type ConflictResolveModalOptions = {
readOnly?: boolean;
title?: string;
localName?: string;
remoteName?: string;
};
export class ConflictResolveModal extends Modal {
result: diff_result;
filename: FilePathWithPrefix;
@@ -32,7 +25,6 @@ export class ConflictResolveModal extends Modal {
title: string = "Conflicting changes";
pluginPickMode: boolean = false;
readOnly: boolean = false;
localName: string = "Base";
remoteName: string = "Conflicted";
offEvent?: ReturnType<typeof eventHub.onEvent>;
@@ -45,22 +37,16 @@ export class ConflictResolveModal extends Modal {
filename: FilePathWithPrefix,
diff: diff_result,
pluginPickMode?: boolean,
remoteName?: string,
options?: ConflictResolveModalOptions
remoteName?: string
) {
super(app);
this.result = diff;
this.filename = filename;
this.pluginPickMode = pluginPickMode || false;
this.readOnly = options?.readOnly ?? false;
if (this.pluginPickMode) {
this.title = "Pick a version";
this.remoteName = `${remoteName || "Remote"}`;
this.localName = "Local";
} else if (this.readOnly) {
this.title = options?.title ?? "Vault and database revision";
this.localName = options?.localName ?? "Vault file";
this.remoteName = options?.remoteName ?? "Database revision";
}
}
@@ -115,18 +101,16 @@ export class ConflictResolveModal extends Modal {
if (this.offEvent) {
this.offEvent();
}
if (!this.readOnly) {
// Cancel an older dialogue for this path before subscribing this
// instance. Emitting after subscription would close the replacement
// itself; the instance-owned result promise then completes the older
// caller even when it only begins waiting after this event.
eventHub.emitEvent(EVENT_CONFLICT_CANCELLED, this.filename);
this.offEvent = eventHub.onEvent(EVENT_CONFLICT_CANCELLED, (path) => {
if (path === this.filename) {
this.sendResponse(CANCELLED);
}
});
}
// Cancel an older dialogue for this path before subscribing this
// instance. Emitting after subscription would close the replacement
// itself; the instance-owned result promise then completes the older
// caller even when it only begins waiting after this event.
eventHub.emitEvent(EVENT_CONFLICT_CANCELLED, this.filename);
this.offEvent = eventHub.onEvent(EVENT_CONFLICT_CANCELLED, (path) => {
if (path === this.filename) {
this.sendResponse(CANCELLED);
}
});
this.titleEl.setText(this.title);
contentEl.empty();
const diffOptionsRow = contentEl.createDiv("");
@@ -175,31 +159,24 @@ export class ConflictResolveModal extends Modal {
this.appendVersionInfo(div2, "deleted", this.localName, date1);
this.appendVersionInfo(div2, "added", this.remoteName, date2);
const actionContainer = contentEl.createDiv("conflict-action-container");
if (this.readOnly) {
actionContainer.createEl("button", { text: "Close" }, (e) => {
actionContainer.createEl("button", { text: `Use ${this.localName}` }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.result.right.rev));
});
actionContainer.createEl("button", { text: `Use ${this.remoteName}` }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.result.left.rev));
});
if (!this.pluginPickMode) {
actionContainer.createEl("button", { text: "Concat both" }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(CANCELLED));
});
} else {
actionContainer.createEl("button", { text: `Use ${this.localName}` }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.result.right.rev));
});
actionContainer.createEl("button", { text: `Use ${this.remoteName}` }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.result.left.rev));
});
if (!this.pluginPickMode) {
actionContainer.createEl("button", { text: "Concat both" }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(LEAVE_TO_SUBSEQUENT));
});
}
actionContainer.createEl("button", { text: !this.pluginPickMode ? "Not now" : "Cancel" }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.pluginPickMode ? CANCELLED : POSTPONED));
e.addEventListener("click", () => this.sendResponse(LEAVE_TO_SUBSEQUENT));
});
}
actionContainer.createEl("button", { text: !this.pluginPickMode ? "Not now" : "Cancel" }, (e) => {
e.addClass("conflict-action-button");
e.addEventListener("click", () => this.sendResponse(this.pluginPickMode ? CANCELLED : POSTPONED));
});
if (diffLength > 100 * 1024) {
this.diffView.empty();
this.diffView.setText("(Too large diff to display)");
@@ -5,8 +5,6 @@ import { CANCELLED, type diff_result, type FilePathWithPrefix } from "@vrtmrz/li
vi.mock("@/deps.ts", () => ({
App: class App {},
Modal: class Modal {
createdButtons: string[] = [];
private createElement(): Record<string, unknown> {
const element: Record<string, unknown> = {
addClass: vi.fn(),
@@ -24,14 +22,6 @@ vi.mock("@/deps.ts", () => ({
};
element.createDiv = vi.fn(() => this.createElement());
element.createEl = vi.fn((_tag: string, _options?: unknown, callback?: (child: unknown) => void) => {
if (
_tag === "button" &&
typeof _options === "object" &&
_options !== null &&
"text" in _options
) {
this.createdButtons.push(String((_options as { text: unknown }).text));
}
const child = this.createElement();
callback?.(child);
return child;
@@ -92,55 +82,4 @@ describe("ConflictResolveModal result lifecycle", () => {
expect(previousResult).toBe(CANCELLED);
expect(replacementState).toBe("still-open");
});
it("renders a read-only comparison with no resolution actions", () => {
const ReadOnlyModal = ConflictResolveModal as unknown as new (
...args: unknown[]
) => ConflictResolveModal & { createdButtons: string[] };
const modal = new ReadOnlyModal(
{},
"repair-preview.md",
conflict,
false,
undefined,
{
readOnly: true,
title: "Vault and database revision",
localName: "Vault file",
remoteName: "Database revision",
}
);
modal.onOpen();
expect(modal.createdButtons).toContain("Close");
expect(modal.createdButtons).not.toContain("Use Vault file");
expect(modal.createdButtons).not.toContain("Use Database revision");
expect(modal.createdButtons).not.toContain("Concat both");
expect(modal.createdButtons).not.toContain("Not now");
modal.close();
});
it("does not cancel an active conflict dialogue when a read-only comparison opens for the same file", async () => {
const filename = "repair-alongside-conflict.md" as FilePathWithPrefix;
const previous = new ConflictResolveModal({} as never, filename, conflict);
const ReadOnlyModal = ConflictResolveModal as unknown as new (
...args: unknown[]
) => ConflictResolveModal;
const comparison = new ReadOnlyModal({}, filename, conflict, false, undefined, {
readOnly: true,
});
previous.onOpen();
comparison.onOpen();
const previousState = await Promise.race([
previous.waitForResult(),
new Promise<"still-open">((resolve) => setTimeout(() => resolve("still-open"), 25)),
]);
previous.sendResponse(CANCELLED);
comparison.close();
expect(previousState).toBe("still-open");
});
});
File diff suppressed because it is too large Load Diff
@@ -187,7 +187,7 @@ export function paneMaintenance(
)
.addOnUpdate(this.onlyOnMinIO);
});
void addPanel(paneEl, "Garbage Collection V3 (Beta)", (e) => e, this.onlyOnCouchDB).then((paneEl) => {
void addPanel(paneEl, "Garbage Collection V3 (Beta)", (e) => e, this.onlyOnP2POrCouchDB).then((paneEl) => {
new Setting(paneEl)
.setName("Perform Garbage Collection")
.setDesc("Perform Garbage Collection to remove unused chunks and reduce database size.")
@@ -133,8 +133,8 @@ export function paneRemoteConfig(
}
{
// TODO: very WIP. need to refactor the UI.
void addPanel(paneEl, $msg("Connection settings"), () => {}).then((paneEl) => {
const actions = new Setting(paneEl).setName($msg("Saved connections"));
void addPanel(paneEl, $msg("obsidianLiveSyncSettingTab.titleRemoteServer"), () => {}).then((paneEl) => {
const actions = new Setting(paneEl).setName("Remote Databases");
// actions.addButton((button) =>
// button
// .setButtonText("Change Remote and Setup")
@@ -12,7 +12,7 @@ import type { ObsidianLiveSyncSettingTab } from "./ObsidianLiveSyncSettingTab.ts
import type { PageFunctions } from "./SettingPane.ts";
import { visibleOnly } from "./SettingPane.ts";
import { request } from "@/deps.ts";
import { SetupManager } from "@/modules/features/SetupManager.ts";
import { SetupManager, UserMode } from "@/modules/features/SetupManager.ts";
import { LiveSyncError } from "@vrtmrz/livesync-commonlib/compat/common/LSError";
import {
createCoreSettingsAfterFullReset,
@@ -40,7 +40,8 @@ export function paneSetup(
.addButton((text) => {
text.setButtonText($msg("Rerun Wizard")).onClick(async () => {
const setupManager = this.core.getModule(SetupManager);
await setupManager.startOnBoarding();
await setupManager.onOnboard(UserMode.ExistingUser);
// await this.plugin.moduleSetupObsidian.onBoardingWizard(true);
});
});
@@ -41,7 +41,8 @@
<Question>{translateMessage("Ui.SetupWizard.SetupRemote.Guidance")}</Question>
<Options>
<Option selectedValue={TYPE_COUCHDB} title="CouchDB" bind:value={userType}>
{translateMessage("Ui.SetupWizard.SetupRemote.CouchDbOptionDesc")}
This is the most suitable synchronisation method for the design. All functions are available. You must have
set up a CouchDB instance.
</Option>
<Option
selectedValue={TYPE_BUCKET}
@@ -68,8 +68,6 @@
display: flex;
flex-direction: column;
padding-bottom: var(--keyboard-height, 0px);
user-select: text;
-webkit-user-select: text;
}
.dialog-host :global(button) {
@@ -1,67 +0,0 @@
import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
platform: {
isMobile: false,
},
}));
vi.mock("@/deps.ts", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
}));
vi.mock("@/deps", () => ({
Platform: mocks.platform,
requestUrl: vi.fn(),
}));
vi.mock("@/modules/essentialObsidian/APILib/ObsHttpHandler", () => ({
ObsHttpHandler: class {},
}));
vi.mock("./ObsidianConfirm", () => ({
ObsidianConfirm: class {},
}));
import { ObsidianAPIService } from "./ObsidianAPIService";
import type { ObsidianServiceContext } from "./ObsidianServiceContext";
function createService(workspace: Record<string, unknown>, isMobile = false): ObsidianAPIService {
return new ObsidianAPIService({
app: { workspace, isMobile },
} as unknown as ObsidianServiceContext);
}
beforeEach(() => {
mocks.platform.isMobile = false;
vi.clearAllMocks();
});
describe("ObsidianAPIService.showWindowOnRight", () => {
it("keeps the status view in the right leaf on mobile", async () => {
mocks.platform.isMobile = true;
const rightLeaf = {
setViewState: vi.fn().mockResolvedValue(undefined),
};
const workspace = {
getLeavesOfType: vi.fn(() => []),
getLeaf: vi.fn(),
getRightLeaf: vi.fn(() => rightLeaf),
revealLeaf: vi.fn().mockResolvedValue(undefined),
};
const service = createService(workspace, true);
expect(service.isMobile()).toBe(true);
await service.showWindowOnRight("p2p-status");
expect(workspace.getLeavesOfType).toHaveBeenCalledWith("p2p-status");
expect(workspace.getRightLeaf).toHaveBeenCalledWith(false);
expect(workspace.getLeaf).not.toHaveBeenCalled();
expect(rightLeaf.setViewState).toHaveBeenCalledWith({
type: "p2p-status",
active: false,
});
expect(workspace.revealLeaf).toHaveBeenCalledWith(rightLeaf);
});
});
+9 -22
View File
@@ -2,16 +2,18 @@ import { Modal } from "@/deps";
import {
SvelteDialogManagerBase,
SvelteDialogMixIn,
type ComponentHasResult,
type SvelteDialogManagerDependencies,
} from "@vrtmrz/livesync-commonlib/compat/services/implements/base/SvelteDialog";
import type { ObsidianServiceContext } from "@/modules/services/ObsidianServiceContext";
import DialogHost from "@/modules/services/LiveSyncUI/DialogHost.svelte";
import { SvelteDialogSession } from "@/modules/services/SvelteDialogSession";
export class SvelteDialogObsidian<T, U, C extends ObsidianServiceContext = ObsidianServiceContext> extends Modal {
private readonly session: SvelteDialogSession<T, U, C>;
export const SvelteDialogBase = SvelteDialogMixIn(Modal, DialogHost);
export class SvelteDialogObsidian<
T,
U,
C extends ObsidianServiceContext = ObsidianServiceContext,
> extends SvelteDialogBase<T, U, C> {
constructor(
context: C,
dependents: SvelteDialogManagerDependencies<C>,
@@ -19,28 +21,13 @@ export class SvelteDialogObsidian<T, U, C extends ObsidianServiceContext = Obsid
initialData?: U
) {
super(context.app);
this.session = new SvelteDialogSession({
surface: this,
context,
dependencies: dependents,
dialogHost: DialogHost,
component,
initialData,
});
this.initDialog(context, dependents, component, initialData);
}
override onOpen(): void {
this.session.onOpen();
super.onOpen();
this.contentEl.closest(".modal-container")?.classList.add("livesync-svelte-dialog-container");
}
override onClose(): void {
this.session.onClose();
}
waitForClose(): Promise<T | undefined> {
return this.session.waitForClose();
}
}
export class ObsidianSvelteDialogManager<T extends ObsidianServiceContext> extends SvelteDialogManagerBase<T> {
-138
View File
@@ -1,138 +0,0 @@
import { EVENT_PLUGIN_UNLOADED } from "@vrtmrz/livesync-commonlib/compat/events/coreEvents";
import type { ServiceContext } from "@vrtmrz/livesync-commonlib/context";
import {
setupDialogContext,
type ComponentHasResult,
type DialogContext,
type DialogHostProps,
type DialogSvelteComponentBaseProps,
type SvelteDialogManagerDependencies,
} from "@vrtmrz/livesync-commonlib/compat/services/implements/base/SvelteDialog";
import { fireAndForget, promiseWithResolvers, type PromiseWithResolvers } from "octagonal-wheels/promises";
import { mount, unmount, type Component } from "svelte";
/** Host dialogue surface controlled by an explicit session. */
export interface SvelteDialogSurface {
readonly contentEl: HTMLElement;
setTitle(title: string): unknown;
close(): void;
}
type MountedDialog = ReturnType<typeof mount>;
/** Injectable renderer used to keep the dialogue lifecycle independently testable. */
export interface SvelteDialogRenderer {
mount(dialogHost: Component<DialogHostProps>, target: HTMLElement, props: DialogHostProps): MountedDialog;
unmount(component: MountedDialog): Promise<void>;
}
export interface SvelteDialogSessionOptions<TResult, TInitial, TContext extends ServiceContext> {
surface: SvelteDialogSurface;
context: TContext;
dependencies: SvelteDialogManagerDependencies<TContext>;
dialogHost: Component<DialogHostProps>;
component: ComponentHasResult<TResult, TInitial>;
initialData?: TInitial;
renderer?: SvelteDialogRenderer;
}
const svelteDialogRenderer: SvelteDialogRenderer = {
mount(dialogHost, target, props) {
return mount(dialogHost, { target, props });
},
async unmount(component) {
await unmount(component);
},
};
/** Owns one Svelte dialogue result and mount lifecycle for a concrete host surface. */
export class SvelteDialogSession<
TResult,
TInitial,
TContext extends ServiceContext = ServiceContext,
> {
private readonly surface: SvelteDialogSurface;
private readonly context: TContext;
private readonly dependencies: SvelteDialogManagerDependencies<TContext>;
private readonly dialogHost: Component<DialogHostProps>;
private readonly component: ComponentHasResult<TResult, TInitial>;
private readonly initialData?: TInitial;
private readonly renderer: SvelteDialogRenderer;
private result?: TResult;
private pendingResult?: PromiseWithResolvers<TResult | undefined>;
private mountedDialog?: MountedDialog;
private unregisterUnload?: () => void;
constructor(options: SvelteDialogSessionOptions<TResult, TInitial, TContext>) {
this.surface = options.surface;
this.context = options.context;
this.dependencies = options.dependencies;
this.dialogHost = options.dialogHost;
this.component = options.component;
this.initialData = options.initialData;
this.renderer = options.renderer ?? svelteDialogRenderer;
}
onOpen(): void {
this.surface.contentEl.replaceChildren();
this.unregisterUnload?.();
this.unregisterUnload = undefined;
if (this.pendingResult) {
this.pendingResult.reject(new Error("Dialogue opened again"));
}
this.result = undefined;
const pendingResult = promiseWithResolvers<TResult | undefined>();
this.pendingResult = pendingResult;
this.unregisterUnload = this.context.events.onceEvent(EVENT_PLUGIN_UNLOADED, () => {
if (this.pendingResult !== pendingResult) {
return;
}
this.pendingResult = undefined;
pendingResult.reject(new Error("Plug-in unloaded"));
this.surface.close();
});
this.mountedDialog = this.renderer.mount(this.dialogHost, this.surface.contentEl, {
onSetupContext: (props: DialogSvelteComponentBaseProps<TResult, TInitial>) => {
setupDialogContext({
...props,
context: this.context,
services: this.dependencies,
} satisfies DialogContext<TContext, TResult, TInitial>);
},
setTitle: (title: string) => {
this.surface.setTitle(title);
},
closeDialog: () => {
this.surface.close();
},
setResult: (result: TResult) => {
this.result = result;
},
getInitialData: () => this.initialData,
mountComponent: this.component,
});
}
waitForClose(): Promise<TResult | undefined> {
if (!this.pendingResult) {
throw new Error("Dialogue has not been opened");
}
return this.pendingResult.promise;
}
onClose(): void {
this.unregisterUnload?.();
this.unregisterUnload = undefined;
this.pendingResult?.resolve(this.result);
this.pendingResult = undefined;
const mountedDialog = this.mountedDialog;
this.mountedDialog = undefined;
if (mountedDialog) {
fireAndForget(() => this.renderer.unmount(mountedDialog));
}
}
}
-454
View File
@@ -1,454 +0,0 @@
import { $msg } from "@/common/translation";
import type {
FilePath,
FilePathWithPrefix,
LoadedEntry,
ObsidianLiveSyncSettings,
} from "@vrtmrz/livesync-commonlib/compat/common/types";
import { getFileRegExp } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import { isNotFoundError } from "@vrtmrz/livesync-commonlib/compat/common/utils.doc";
import { ICHeader, ICXHeader, PSCHeader } from "@vrtmrz/livesync-commonlib/compat/common/models/fileaccess.const";
import type { StorageAccess } from "@vrtmrz/livesync-commonlib/compat/interfaces/StorageAccess";
import type { LiveSyncLocalDB } from "@vrtmrz/livesync-commonlib/compat/pouchdb/LiveSyncLocalDB";
import type { IPathService, IUIService } from "@vrtmrz/livesync-commonlib/compat/services/base/IService";
import { addPrefix, stripAllPrefixes } from "@vrtmrz/livesync-commonlib/compat/string_and_binary/path";
type DatabaseMeta = LoadedEntry & {
_rawStorageType: string | null;
_legacyBodyPresent: boolean;
_revs_info?: Array<{
rev: string;
status: string;
}>;
};
export type FileDatabaseInfoCore = {
localDatabase: Pick<
LiveSyncLocalDB,
"allDocsRaw" | "findAllDocs" | "getDBEntryFromMeta" | "getDBEntry" | "localDatabase"
>;
services: {
path: Pick<IPathService, "path2id">;
UI: IUIService;
};
settings: ObsidianLiveSyncSettings;
storageAccess: Pick<
StorageAccess,
"getFileNames" | "getFilesIncludeHidden" | "isExistsIncludeHidden" | "statHidden"
>;
};
export type RevisionDatabaseInfo = {
documentId: string;
revision: string | null;
current: boolean;
deleted: boolean;
storageType: string;
storageLayout: "chunked" | "legacy-inline";
ctime: number;
mtime: number;
recordedSize: number;
revisionHistory: Array<{
revision: string;
status: string;
}>;
chunkReferences: number;
uniqueChunkReferences: number;
embeddedChunkReferences: number;
locallyStoredChunkReferences: number;
contentAvailableLocally: boolean;
chunks: Array<{
id: string;
referenceCount: number;
embedded: boolean;
storedInLocalDatabase: boolean;
localDatabaseState: "available" | "deleted" | "missing";
localDatabaseRevision: string | null;
}>;
};
export type FileDatabaseMergeBaseInfo = {
winnerRevision: string;
conflictRevision: string;
revision: string | null;
metadataAvailableLocally: boolean;
contentAvailableLocally: boolean;
missingChunkIds: string[];
unavailableSharedRevisions: string[];
};
export type FileDatabaseInfo = {
path: string;
databasePath: FilePathWithPrefix | FilePath;
storage: {
exists: boolean;
ctime?: number;
mtime?: number;
size?: number;
};
database: {
source: "local database on this device";
remoteQueried: false;
exists: boolean;
currentRevision: string | null;
conflictCount: number;
conflictRevisions: string[];
unavailableConflictRevisions: string[];
revisions: RevisionDatabaseInfo[];
mergeBases: FileDatabaseMergeBaseInfo[];
};
};
const REPORT_WARNING =
"All revisions and chunk availability below are a snapshot of this device's local database; the remote is not queried. Review the Vault-relative path, document identifier, content-derived chunk identifiers, and metadata before sharing this report. File contents are omitted.";
function toDatabasePath(path: string): FilePathWithPrefix | FilePath {
if (path.startsWith(".")) {
return addPrefix(path as FilePath, ICHeader);
}
return path as FilePath;
}
type RawDatabaseDocument = {
_id: string;
_rev?: string;
_conflicts?: string[];
_deleted?: boolean;
_revs_info?: Array<{
rev: string;
status: string;
}>;
children?: string[];
ctime?: number;
deleted?: boolean;
data?: string | string[];
eden?: Record<string, unknown>;
mtime?: number;
size?: number;
type?: string;
};
async function getLocalDatabaseMeta(
core: FileDatabaseInfoCore,
path: FilePathWithPrefix | FilePath,
options: PouchDB.Core.GetOptions
): Promise<DatabaseMeta | false> {
const documentId = await core.services.path.path2id(path);
let raw: RawDatabaseDocument;
try {
raw = await core.localDatabase.localDatabase.get(documentId, options);
} catch (error) {
if (isNotFoundError(error)) {
return false;
}
throw error;
}
if (raw.type === "leaf") {
return false;
}
if (raw.type && raw.type !== "notes" && raw.type !== "newnote" && raw.type !== "plain") {
return false;
}
const rawStorageType = raw.type ?? null;
const legacy = rawStorageType === null || rawStorageType === "notes";
const type = legacy ? "notes" : rawStorageType;
const legacyBodyPresent =
legacy && (typeof raw.data === "string" || (Array.isArray(raw.data) && raw.data.every((item) => typeof item === "string")));
return {
_id: raw._id,
_rev: raw._rev,
_conflicts: raw._conflicts,
_revs_info: raw._revs_info,
path,
data: legacyBodyPresent ? raw.data : "",
ctime: raw.ctime ?? 0,
mtime: raw.mtime ?? 0,
size: raw.size ?? 0,
children: type === "newnote" || type === "plain" ? (raw.children ?? []) : [],
datatype: type === "newnote" ? "newnote" : "plain",
deleted: raw.deleted ?? raw._deleted,
type,
eden: raw.eden ?? {},
_rawStorageType: rawStorageType,
_legacyBodyPresent: legacyBodyPresent,
} as DatabaseMeta;
}
async function collectRevisionDatabaseInfo(
core: FileDatabaseInfoCore,
meta: DatabaseMeta,
current: boolean
): Promise<RevisionDatabaseInfo> {
const legacy = meta._rawStorageType === null || meta._rawStorageType === "notes";
const children = legacy ? [] : "children" in meta ? meta.children : [];
const uniqueChildren = [...new Set(children)];
const referenceCounts = new Map<string, number>();
for (const child of children) {
referenceCounts.set(child, (referenceCounts.get(child) ?? 0) + 1);
}
const embeddedChildren = new Set(
Object.keys("eden" in meta && meta.eden ? meta.eden : {}).filter((id) => uniqueChildren.includes(id))
);
const localRows =
uniqueChildren.length === 0
? []
: (
await core.localDatabase.allDocsRaw({
keys: uniqueChildren,
include_docs: false,
})
).rows;
const localChunkStates = new Map(
localRows
.filter((row) => "value" in row)
.map(
(row) =>
[
row.key,
{
state: row.value.deleted ? ("deleted" as const) : ("available" as const),
revision: row.value.rev,
},
] as const
)
);
return {
documentId: meta._id,
revision: meta._rev ?? null,
current,
deleted: Boolean(meta.deleted ?? meta._deleted),
storageType: meta._rawStorageType ?? "absent",
storageLayout: legacy ? "legacy-inline" : "chunked",
ctime: meta.ctime,
mtime: meta.mtime,
recordedSize: meta.size,
revisionHistory: (meta._revs_info ?? []).map(({ rev, status }) => ({
revision: rev,
status,
})),
chunkReferences: children.length,
uniqueChunkReferences: uniqueChildren.length,
embeddedChunkReferences: children.filter((id) => embeddedChildren.has(id)).length,
locallyStoredChunkReferences: children.filter((id) => localChunkStates.get(id)?.state === "available").length,
contentAvailableLocally: legacy
? meta._legacyBodyPresent
: uniqueChildren.every(
(id) => embeddedChildren.has(id) || localChunkStates.get(id)?.state === "available"
),
chunks: uniqueChildren.map((id) => {
const localState = localChunkStates.get(id);
return {
id,
referenceCount: referenceCounts.get(id) ?? 0,
embedded: embeddedChildren.has(id),
storedInLocalDatabase: localState?.state === "available",
localDatabaseState: localState?.state ?? "missing",
localDatabaseRevision: localState?.revision ?? null,
};
}),
};
}
function revisionHistory(meta: DatabaseMeta): Array<{ revision: string; status: string }> {
const history = (meta._revs_info ?? []).map(({ rev, status }) => ({
revision: rev,
status,
}));
if (meta._rev && !history.some(({ revision }) => revision === meta._rev)) {
history.unshift({
revision: meta._rev,
status: "available",
});
}
return history;
}
function missingChunkIds(info: RevisionDatabaseInfo): string[] {
return info.chunks
.filter(({ embedded, localDatabaseState }) => !embedded && localDatabaseState !== "available")
.map(({ id }) => id);
}
export async function inspectFileDatabaseInfo(core: FileDatabaseInfoCore, path: string): Promise<FileDatabaseInfo> {
const storageExists = await core.storageAccess.isExistsIncludeHidden(path);
const storageStat = storageExists ? await core.storageAccess.statHidden(path) : null;
const databasePath = toDatabasePath(path);
const currentMeta = await getLocalDatabaseMeta(core, databasePath, {
conflicts: true,
revs: true,
revs_info: true,
});
const revisions: RevisionDatabaseInfo[] = [];
const conflictRevisions = currentMeta === false ? [] : (currentMeta._conflicts ?? []);
const unavailableConflictRevisions: string[] = [];
const mergeBases: FileDatabaseMergeBaseInfo[] = [];
const metadataByRevision = new Map<string, DatabaseMeta | false>();
if (currentMeta !== false && currentMeta._rev) {
metadataByRevision.set(currentMeta._rev, currentMeta);
}
const getRevisionMeta = async (revision: string): Promise<DatabaseMeta | false> => {
const cached = metadataByRevision.get(revision);
if (cached !== undefined) {
return cached;
}
const meta = await getLocalDatabaseMeta(core, databasePath, {
rev: revision,
revs: true,
revs_info: true,
});
metadataByRevision.set(revision, meta);
return meta;
};
if (currentMeta) {
revisions.push(await collectRevisionDatabaseInfo(core, currentMeta, true));
for (const revision of conflictRevisions) {
const conflictMeta = await getRevisionMeta(revision);
if (conflictMeta) {
revisions.push(await collectRevisionDatabaseInfo(core, conflictMeta, false));
const winnerHistory = revisionHistory(currentMeta);
const conflictHistory = revisionHistory(conflictMeta);
const conflictHistoryByRevision = new Map(
conflictHistory.map(({ revision: historyRevision, status }) => [historyRevision, status])
);
const sharedHistory = winnerHistory.filter(({ revision: historyRevision }) =>
conflictHistoryByRevision.has(historyRevision)
);
const sharedRevision = sharedHistory[0]?.revision ?? null;
const unavailableSharedRevisions = sharedHistory
.filter(
({ revision: historyRevision, status }) =>
status !== "available" ||
conflictHistoryByRevision.get(historyRevision) !== "available"
)
.map(({ revision: historyRevision }) => historyRevision);
const sharedMeta = sharedRevision ? await getRevisionMeta(sharedRevision) : false;
const sharedInfo = sharedMeta
? await collectRevisionDatabaseInfo(core, sharedMeta, false)
: undefined;
mergeBases.push({
winnerRevision: currentMeta._rev ?? "",
conflictRevision: revision,
revision: sharedRevision,
metadataAvailableLocally: Boolean(sharedMeta),
contentAvailableLocally: sharedInfo?.contentAvailableLocally ?? false,
missingChunkIds: sharedInfo ? missingChunkIds(sharedInfo) : [],
unavailableSharedRevisions,
});
} else {
unavailableConflictRevisions.push(revision);
}
}
}
const report: FileDatabaseInfo = {
path,
databasePath,
storage: storageStat
? {
exists: true,
ctime: storageStat.ctime,
mtime: storageStat.mtime,
size: storageStat.size,
}
: {
exists: false,
},
database: {
source: "local database on this device",
remoteQueried: false,
exists: currentMeta !== false,
currentRevision: currentMeta ? (currentMeta._rev ?? null) : null,
conflictCount: conflictRevisions.length,
conflictRevisions,
unavailableConflictRevisions,
revisions,
mergeBases,
},
};
return report;
}
export async function readFileDatabaseRevisionLocally(
core: FileDatabaseInfoCore,
path: string,
revision: string
): Promise<LoadedEntry | false> {
const databasePath = toDatabasePath(path);
const meta = await getLocalDatabaseMeta(core, databasePath, {
rev: revision,
revs: true,
revs_info: true,
});
if (!meta) {
return false;
}
const info = await collectRevisionDatabaseInfo(core, meta, false);
if (info.deleted || !info.contentAvailableLocally) {
return false;
}
return await core.localDatabase.getDBEntryFromMeta(meta, false, false);
}
export async function retryReadFileDatabaseRevision(
core: FileDatabaseInfoCore,
path: string,
revision: string
): Promise<LoadedEntry | false> {
return await core.localDatabase.getDBEntry(toDatabasePath(path), { rev: revision }, false, true, true);
}
export async function buildFileDatabaseInfoReport(core: FileDatabaseInfoCore, path: string): Promise<string> {
const report = await inspectFileDatabaseInfo(core, path);
return `${$msg(REPORT_WARNING)}
\`\`\`json
${JSON.stringify(report, null, 2)}
\`\`\``;
}
export async function copyFileDatabaseInfo(core: FileDatabaseInfoCore, path: string): Promise<boolean> {
const report = await buildFileDatabaseInfoReport(core, path);
return await core.services.UI.promptCopyToClipboard(
$msg("Database information for ${FILE}", { FILE: path }),
report
);
}
export async function collectFileDatabaseInfoPaths(core: FileDatabaseInfoCore): Promise<string[]> {
const ignorePatterns = getFileRegExp(core.settings, "syncInternalFilesIgnorePatterns");
const targetPatterns = getFileRegExp(core.settings, "syncInternalFilesTargetPatterns");
const storagePaths = core.settings.syncInternalFiles
? await core.storageAccess.getFilesIncludeHidden("/", targetPatterns, ignorePatterns)
: await core.storageAccess.getFileNames();
const databasePaths: string[] = [];
for await (const entry of core.localDatabase.findAllDocs()) {
const prefixedPath = entry.path;
if (prefixedPath.startsWith(ICXHeader) || prefixedPath.startsWith(PSCHeader)) {
continue;
}
if (!core.settings.syncInternalFiles && prefixedPath.startsWith(ICHeader)) {
continue;
}
databasePaths.push(stripAllPrefixes(prefixedPath));
}
return [...new Set([...storagePaths, ...databasePaths])].sort((left, right) =>
left < right ? -1 : left > right ? 1 : 0
);
}
export async function chooseAndCopyFileDatabaseInfo(core: FileDatabaseInfoCore): Promise<boolean> {
const paths = await collectFileDatabaseInfoPaths(core);
const selected = await core.services.UI.confirm.askSelectString($msg("Choose a file to inspect"), paths);
if (!selected) {
return false;
}
return await copyFileDatabaseInfo(core, selected);
}
@@ -1,413 +0,0 @@
import { describe, expect, it, vi } from "vitest";
import {
buildFileDatabaseInfoReport,
chooseAndCopyFileDatabaseInfo,
collectFileDatabaseInfoPaths,
inspectFileDatabaseInfo,
readFileDatabaseRevisionLocally,
retryReadFileDatabaseRevision,
} from "./fileDatabaseInfo";
async function* documents(paths: string[]) {
for (const path of paths) {
yield {
_id: `f:${path}`,
path,
};
}
}
function createCore() {
const current = {
_id: "f:note",
_rev: "3-current",
_conflicts: ["2-conflict"],
_revs_info: [
{ rev: "3-current", status: "available" },
{ rev: "2-parent", status: "missing" },
],
path: "note.md",
ctime: 100,
mtime: 300,
size: 42,
type: "plain",
datatype: "plain",
data: "secret current body",
children: ["h:private-current", "h:private-current", "h:private-embedded", "h:private-deleted"],
eden: {
"h:private-embedded": {
data: "secret embedded body",
epoch: 1,
},
},
};
const conflict = {
...current,
_rev: "2-conflict",
_conflicts: undefined,
_revs_info: [{ rev: "2-conflict", status: "available" }],
mtime: 200,
data: "secret conflict body",
children: ["h:private-missing"],
eden: {},
};
const promptCopyToClipboard = vi.fn(async (_title: string, _value: string) => true);
const askSelectString = vi.fn(async () => "db-only.md");
const core = {
settings: {
syncInternalFiles: false,
syncInternalFilesIgnorePatterns: "",
syncInternalFilesTargetPatterns: "",
},
storageAccess: {
isExistsIncludeHidden: vi.fn(async () => true),
statHidden: vi.fn(async () => ({
ctime: 90,
mtime: 310,
size: 45,
type: "file",
})),
getFileNames: vi.fn(async () => ["z.md", "a.md"]),
getFilesIncludeHidden: vi.fn(async () => [".obsidian/app.json", "a.md"]),
},
localDatabase: {
getDBEntryFromMeta: vi.fn(async (meta: typeof current) => ({
...meta,
data: ["loaded body"],
})),
getDBEntry: vi.fn(async () => current),
localDatabase: {
get: vi.fn(async (_id: string, options?: { rev?: string }) =>
options?.rev === "2-conflict" ? conflict : current
),
},
allDocsRaw: vi.fn(async ({ keys }: { keys: string[] }) => ({
rows: [
...(keys.includes("h:private-current")
? [
{
id: "h:private-current",
key: "h:private-current",
value: { rev: "1-chunk" },
},
]
: []),
...(keys.includes("h:private-deleted")
? [
{
id: "h:private-deleted",
key: "h:private-deleted",
value: { rev: "4-deleted-chunk", deleted: true },
},
]
: []),
],
})),
findAllDocs: vi.fn(() => documents(["db-only.md", "i:.obsidian/app.json", "ix:ignore", "ps:setting"])),
},
services: {
path: {
path2id: vi.fn(async () => "f:note"),
},
UI: {
promptCopyToClipboard,
confirm: {
askSelectString,
},
},
},
};
return {
askSelectString,
conflict,
core,
current,
promptCopyToClipboard,
};
}
describe("file database information", () => {
it("reports document and chunk revisions without exposing file contents", async () => {
const { core } = createCore();
const report = await buildFileDatabaseInfoReport(core as never, "note.md");
expect(report).toContain('"path": "note.md"');
expect(report).toContain('"documentId": "f:note"');
expect(report).toContain('"revision": "3-current"');
expect(report).toContain('"revision": "2-conflict"');
expect(report).toContain('"storageType": "plain"');
expect(report).toContain('"storageLayout": "chunked"');
expect(report).toContain('"contentAvailableLocally": false');
expect(report).toContain('"id": "h:private-current"');
expect(report).toContain('"localDatabaseRevision": "1-chunk"');
expect(report).toContain('"referenceCount": 2');
expect(report).toContain('"id": "h:private-embedded"');
expect(report).toContain('"embedded": true');
expect(report).toContain('"id": "h:private-deleted"');
expect(report).toContain('"localDatabaseState": "deleted"');
expect(report).toContain('"localDatabaseRevision": "4-deleted-chunk"');
expect(report).toContain('"id": "h:private-missing"');
expect(report).toContain('"localDatabaseState": "missing"');
expect(report).toContain('"localDatabaseRevision": null');
expect(report).not.toContain("secret current body");
expect(report).not.toContain("secret conflict body");
expect(report).not.toContain("secret embedded body");
});
it.each([
{
name: "notes",
document: {
type: "notes",
data: "secret legacy body",
},
storageType: "notes",
},
{
name: "an absent type",
document: {
type: undefined,
data: ["secret", " legacy body"],
},
storageType: "absent",
},
])("reports $name as legacy inline storage without exposing its body", async ({ document, storageType }) => {
const { core, current } = createCore();
core.localDatabase.localDatabase.get.mockResolvedValue({
...current,
...document,
_conflicts: [],
children: ["h:must-not-be-treated-as-a-chunk"],
} as never);
const info = await inspectFileDatabaseInfo(core as never, "note.md");
const report = await buildFileDatabaseInfoReport(core as never, "note.md");
expect(info.database.revisions).toEqual([
expect.objectContaining({
storageType,
storageLayout: "legacy-inline",
chunkReferences: 0,
contentAvailableLocally: true,
}),
]);
expect(report).not.toContain("secret legacy body");
expect(report).not.toContain("h:must-not-be-treated-as-a-chunk");
});
it("reports the exact shared ancestor and its missing chunks for each conflict", async () => {
const { conflict, core, current } = createCore();
const parent = {
...current,
_rev: "2-parent",
_conflicts: undefined,
_revs_info: [
{ rev: "2-parent", status: "available" },
{ rev: "1-root", status: "missing" },
],
children: ["h:missing-parent"],
eden: {},
};
core.localDatabase.localDatabase.get.mockImplementation(async (_id: string, options?: { rev?: string }) => {
if (options?.rev === "2-conflict") {
return {
...conflict,
_revs_info: [
{ rev: "2-conflict", status: "available" },
{ rev: "2-parent", status: "available" },
{ rev: "1-root", status: "missing" },
],
};
}
if (options?.rev === "2-parent") {
return parent;
}
return {
...current,
_revs_info: [
{ rev: "3-current", status: "available" },
{ rev: "2-parent", status: "available" },
{ rev: "1-root", status: "missing" },
],
};
});
const info = await inspectFileDatabaseInfo(core as never, "note.md");
expect(info.database.mergeBases).toEqual([
{
winnerRevision: "3-current",
conflictRevision: "2-conflict",
revision: "2-parent",
metadataAvailableLocally: true,
contentAvailableLocally: false,
missingChunkIds: ["h:missing-parent"],
unavailableSharedRevisions: ["1-root"],
},
]);
});
it("does not decode a revision whose chunks are not all available locally", async () => {
const { core } = createCore();
await expect(readFileDatabaseRevisionLocally(core as never, "note.md", "3-current")).resolves.toBe(false);
expect(core.localDatabase.getDBEntryFromMeta).not.toHaveBeenCalled();
});
it("decodes an exact revision after confirming that every chunk is available locally", async () => {
const { core, current } = createCore();
core.localDatabase.localDatabase.get.mockResolvedValue({
...current,
children: ["h:available"],
eden: {},
} as never);
core.localDatabase.allDocsRaw.mockResolvedValue({
rows: [
{
id: "h:available",
key: "h:available",
value: { rev: "1-available" },
},
],
});
await expect(readFileDatabaseRevisionLocally(core as never, "note.md", "3-current")).resolves.toEqual(
expect.objectContaining({
data: ["loaded body"],
})
);
expect(core.localDatabase.getDBEntryFromMeta).toHaveBeenCalledWith(
expect.objectContaining({
_rev: "3-current",
}),
false,
false
);
});
it("retries an exact revision through the configured chunk retrieval path", async () => {
const { core } = createCore();
await retryReadFileDatabaseRevision(core as never, "note.md", "2-conflict");
expect(core.localDatabase.getDBEntry).toHaveBeenCalledWith(
"note.md",
{ rev: "2-conflict" },
false,
true,
true
);
});
it("reports the exact revision as locally available after retry recovers its missing chunk", async () => {
const { conflict, core } = createCore();
let recovered = false;
core.localDatabase.getDBEntry.mockImplementation(async () => {
recovered = true;
return conflict as never;
});
core.localDatabase.allDocsRaw.mockImplementation(async ({ keys }: { keys: string[] }) => ({
rows:
recovered && keys.includes("h:private-missing")
? [
{
id: "h:private-missing",
key: "h:private-missing",
value: { rev: "1-recovered" },
},
]
: [],
}));
await expect(
retryReadFileDatabaseRevision(core as never, "note.md", "2-conflict")
).resolves.not.toBe(false);
const information = await inspectFileDatabaseInfo(core as never, "note.md");
expect(
information.database.revisions.find(({ revision }) => revision === "2-conflict")
).toEqual(
expect.objectContaining({
contentAvailableLocally: true,
chunks: [
expect.objectContaining({
id: "h:private-missing",
localDatabaseState: "available",
localDatabaseRevision: "1-recovered",
}),
],
})
);
});
it("keeps the exact revision identifiers when conflict metadata is unavailable", async () => {
const { conflict, core, current } = createCore();
core.localDatabase.localDatabase.get.mockImplementation(async (_id: string, options?: { rev?: string }) => {
if (options?.rev === "2-unavailable") {
throw Object.assign(new Error("missing"), { status: 404 });
}
if (options?.rev === "2-conflict") {
return conflict;
}
return { ...current, _conflicts: ["2-conflict", "2-unavailable"] };
});
const report = await buildFileDatabaseInfoReport(core as never, "note.md");
expect(report).toContain('"conflictRevisions"');
expect(report).toContain('"2-conflict"');
expect(report).toContain('"2-unavailable"');
expect(report).toContain('"unavailableConflictRevisions"');
});
it("reads an existing local document even when current synchronisation filters exclude its path", async () => {
const { core, current } = createCore();
core.services.path.path2id.mockResolvedValue("f:ignored");
core.localDatabase.localDatabase.get.mockResolvedValue({
...current,
_id: "f:ignored",
_rev: "5-ignored",
_conflicts: [],
_revs_info: [],
path: "ignored.md",
ctime: 10,
mtime: 20,
size: 30,
children: [],
});
const report = await buildFileDatabaseInfoReport(core as never, "ignored.md");
expect(report).toContain('"exists": true');
expect(report).toContain('"documentId": "f:ignored"');
expect(report).toContain('"revision": "5-ignored"');
});
it("offers the union of storage and database paths and excludes inactive internal namespaces", async () => {
const { core } = createCore();
await expect(collectFileDatabaseInfoPaths(core as never)).resolves.toEqual(["a.md", "db-only.md", "z.md"]);
core.settings.syncInternalFiles = true;
await expect(collectFileDatabaseInfoPaths(core as never)).resolves.toEqual([
".obsidian/app.json",
"a.md",
"db-only.md",
]);
});
it("copies the selected file report through the existing copy dialogue", async () => {
const { askSelectString, core, promptCopyToClipboard } = createCore();
await expect(chooseAndCopyFileDatabaseInfo(core as never)).resolves.toBe(true);
expect(askSelectString).toHaveBeenCalledWith("Choose a file to inspect", ["a.md", "db-only.md", "z.md"]);
expect(promptCopyToClipboard).toHaveBeenCalledWith(
"Database information for db-only.md",
expect.stringContaining('"path": "db-only.md"')
);
});
});
-132
View File
@@ -1,132 +0,0 @@
import type { LoadedEntry } from "@vrtmrz/livesync-commonlib/compat/common/types";
import { createBlob, isDocContentSame, readAsBlob } from "@vrtmrz/livesync-commonlib/compat/common/utils";
import type { StorageAccess } from "@vrtmrz/livesync-commonlib/compat/interfaces/StorageAccess";
import type { IFileHandler } from "@vrtmrz/livesync-commonlib/compat/interfaces/FileHandler";
import {
inspectFileDatabaseInfo,
readFileDatabaseRevisionLocally,
type FileDatabaseInfo,
type FileDatabaseInfoCore,
type RevisionDatabaseInfo,
} from "./fileDatabaseInfo";
export type FileRepairCore = FileDatabaseInfoCore & {
fileHandler: Pick<IFileHandler, "deleteRevisionFromDB">;
storageAccess: FileDatabaseInfoCore["storageAccess"] & Pick<StorageAccess, "readHiddenFileBinary">;
};
export type FileRepairRevision = {
role: "winner" | "conflict";
metadata: RevisionDatabaseInfo;
contentReadable: boolean;
contentMatchesStorage: boolean | null;
loadedEntry: LoadedEntry | false;
};
export type FileRepairInspection = {
information: FileDatabaseInfo;
revisions: FileRepairRevision[];
requiresAttention: boolean;
};
export type DiscardUnreadableRevisionResult =
| "discarded"
| "failed"
| "no-longer-live"
| "revision-is-readable";
export type DiscardLiveBranchResult = "discarded" | "failed" | "no-longer-live" | "only-live-revision";
export async function inspectFileRepair(core: FileRepairCore, path: string): Promise<FileRepairInspection> {
const information = await inspectFileDatabaseInfo(core, path);
const storageContent = information.storage.exists
? createBlob(await core.storageAccess.readHiddenFileBinary(path))
: undefined;
const revisions: FileRepairRevision[] = [];
for (const metadata of information.database.revisions) {
const loadedEntry =
metadata.deleted || !metadata.contentAvailableLocally
? false
: await readFileDatabaseRevisionLocally(core, path, metadata.revision ?? "");
const contentReadable = metadata.deleted || loadedEntry !== false;
const contentMatchesStorage =
storageContent && loadedEntry !== false
? await isDocContentSame(storageContent, readAsBlob(loadedEntry))
: null;
revisions.push({
role: metadata.current ? "winner" : "conflict",
metadata,
contentReadable,
contentMatchesStorage,
loadedEntry,
});
}
const winner = revisions.find(({ role }) => role === "winner");
const winnerRepresentsStoredFile = winner !== undefined && !winner.metadata.deleted;
const databaseAndStorageDiffer =
information.storage.exists !== winnerRepresentsStoredFile ||
(information.storage.exists &&
winnerRepresentsStoredFile &&
winner.contentMatchesStorage === false);
const unreadableLiveRevision =
information.database.unavailableConflictRevisions.length > 0 ||
revisions.some(({ contentReadable }) => !contentReadable);
const requiresAttention =
databaseAndStorageDiffer ||
information.database.conflictCount > 0 ||
unreadableLiveRevision ||
(information.database.exists && winner === undefined);
return {
information,
revisions,
requiresAttention,
};
}
export async function discardUnreadableLiveRevision(
core: FileRepairCore,
path: string,
revision: string
): Promise<DiscardUnreadableRevisionResult> {
const latest = await inspectFileDatabaseInfo(core, path);
const liveRevisions = [
latest.database.currentRevision,
...latest.database.conflictRevisions,
].filter((candidate): candidate is string => candidate !== null);
if (!liveRevisions.includes(revision)) {
return "no-longer-live";
}
const metadata = latest.database.revisions.find((candidate) => candidate.revision === revision);
const metadataUnavailable = latest.database.unavailableConflictRevisions.includes(revision);
if (!metadataUnavailable && (metadata?.deleted || metadata?.contentAvailableLocally)) {
return "revision-is-readable";
}
const deleted = await core.fileHandler.deleteRevisionFromDB(latest.databasePath, revision);
return deleted ? "discarded" : "failed";
}
export async function discardLiveBranch(
core: FileRepairCore,
path: string,
revision: string
): Promise<DiscardLiveBranchResult> {
const latest = await inspectFileDatabaseInfo(core, path);
const liveRevisions = [
latest.database.currentRevision,
...latest.database.conflictRevisions,
].filter((candidate): candidate is string => candidate !== null);
if (!liveRevisions.includes(revision)) {
return "no-longer-live";
}
if (liveRevisions.length < 2) {
return "only-live-revision";
}
const deleted = await core.fileHandler.deleteRevisionFromDB(latest.databasePath, revision);
return deleted ? "discarded" : "failed";
}

Some files were not shown because too many files have changed in this diff Show More