Fix syncing when network latency is present (#4)

* WIP

* Add debug

* Dedupe inserts

* Add deterministic ordering

* Fix whitespaces

* Update insta

* Add integration test script

* Rename

* Add test

* Working for non-deletes

* omg it mostly works for deletes

* Isdeleted fix

* remove created dates

* update api

* Take document id

* No max attempt

* works

* Use string uuids

* .

* working!!!! (hopefully)

* Improve bundling

* Add module

* lint

* .

* lint

* Fix CI

* use toolchain

* clean up

* Add useSlowFileEvents

* Delete fuzz

* Fix CI

* use docker

* fix script

* clean up

* Clean up

* change node version

* Build docker image on every commit

* fix ci

* 1 db per vault

* Add scritps folder

* Bump versions

* Lint

* .

* Fix tests for real

* Style

* .

* try

* Consistent ordering

* Fix tests

* hmm

* .

* Clean up diff

* Fixes

* .

* Fix version bump

* .

* .

* .
This commit is contained in:
Andras Schmelczer 2025-03-16 20:13:49 +00:00 • committed by GitHub
commit 8b8f1d91d9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 2216 additions and 1550 deletions

View file

@ -1,10 +0,0 @@
{
"id": "vault-link",
"name": "VaultLink",
"version": "0.0.30",
"minAppVersion": "0.0.0",
"description": "Self-hosted synchronization and collaboration for your Vault.",
"author": "Andras Schmelczer",
"authorUrl": "https://schmelczer.dev",
"isDesktopOnly": false
}

View file

@ -14,7 +14,7 @@
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.13.5",
"@types/node": "^22.13.10",
"css-loader": "^7.1.2",
"date-fns": "^4.1.0",
"file-loader": "^6.2.0",
@ -23,14 +23,14 @@
"mini-css-extract-plugin": "^2.9.2",
"obsidian": "1.8.7",
"resolve-url-loader": "^5.0.0",
"sass": "^1.85.0",
"sass": "^1.85.1",
"sass-loader": "^16.0.5",
"sync-client": "file:../sync-client",
"terser-webpack-plugin": "^5.3.11",
"terser-webpack-plugin": "^5.3.14",
"ts-jest": "^29.2.6",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"typescript": "5.7.3",
"typescript": "5.8.2",
"url": "^0.11.4",
"virtual-scroller": "^1.13.1",
"webpack": "^5.98.0",

View file

@ -9,10 +9,7 @@ export class ObsidianFileEventHandler {
if (file instanceof TFile) {
this.client.logger.info(`File created: ${file.path}`);
await this.client.syncer.syncLocallyCreatedFile(
file.path,
new Date(file.stat.ctime)
);
await this.client.syncer.syncLocallyCreatedFile(file.path);
} else {
this.client.logger.debug(`Folder created: ${file.path}, ignored`);
}
@ -34,8 +31,7 @@ export class ObsidianFileEventHandler {
await this.client.syncer.syncLocallyUpdatedFile({
oldPath,
relativePath: file.path,
updateTime: new Date(file.stat.ctime)
relativePath: file.path
});
} else {
this.client.logger.debug(
@ -53,8 +49,7 @@ export class ObsidianFileEventHandler {
this.client.logger.info(`File modified: ${file.path}`);
await this.client.syncer.syncLocallyUpdatedFile({
relativePath: file.path,
updateTime: new Date(file.stat.ctime)
relativePath: file.path
});
} else {
this.client.logger.debug(`Folder modified: ${file.path}, ignored`);

View file

@ -60,7 +60,6 @@ export class HistoryView extends ItemView {
}
element.createEl("span", {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
text: entry.relativePath
});

View file

@ -1,6 +1,6 @@
import type { WorkspaceLeaf } from "obsidian";
import { ItemView } from "obsidian";
import type VaultLinkPlugin from "src/vault-link-plugin";
import type VaultLinkPlugin from "../vault-link-plugin";
import type { SyncClient } from "sync-client";
export class LogsView extends ItemView {

View file

@ -1,7 +1,7 @@
import type { App } from "obsidian";
import { Notice, PluginSettingTab, Setting } from "obsidian";
import type VaultLinkPlugin from "src/vault-link-plugin";
import type VaultLinkPlugin from "../vault-link-plugin";
import type { StatusDescription } from "./status-description";
import { LogsView } from "./logs-view";
import { HistoryView } from "./history-view";

View file

@ -1,5 +1,5 @@
import type { HistoryStats, SyncClient } from "sync-client";
import type VaultLinkPlugin from "src/vault-link-plugin";
import type VaultLinkPlugin from "../vault-link-plugin";
export class StatusBar {
private readonly statusBarItem: HTMLElement;

View file

@ -94,9 +94,6 @@ module.exports = (env, argv) => ({
alias: {
root: __dirname,
src: path.resolve(__dirname, "src")
},
fallback: {
url: require.resolve("url")
}
},
output: {

View file

@ -12,11 +12,11 @@
],
"devDependencies": {
"concurrently": "^9.1.2",
"eslint": "9.21.0",
"eslint": "9.22.0",
"eslint-plugin-unused-imports": "^4.1.4",
"npm-check-updates": "^17.1.14",
"prettier": "^3.5.2",
"typescript-eslint": "8.24.1"
"npm-check-updates": "^17.1.15",
"prettier": "^3.5.3",
"typescript-eslint": "8.26.1"
}
},
"../backend/sync_lib/pkg": {
@ -39,7 +39,6 @@
},
"node_modules/@babel/code-frame": {
"version": "7.26.2",
"dev": true,
"license": "MIT",
"dependencies": {
"@babel/helper-validator-identifier": "^7.25.9",
@ -179,7 +178,6 @@
},
"node_modules/@babel/helper-validator-identifier": {
"version": "7.25.9",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=6.9.0"
@ -194,12 +192,14 @@
}
},
"node_modules/@babel/helpers": {
"version": "7.26.9",
"version": "7.26.10",
"resolved": "https://registry.npmjs.org/@babel/helpers/-/helpers-7.26.10.tgz",
"integrity": "sha512-UPYc3SauzZ3JGgj87GgZ89JVdC5dj0AoetR5Bw6wj4niittNyFh6+eOGonYvJ1ao6B8lEa3Q3klS7ADZ53bc5g==",
"dev": true,
"license": "MIT",
"dependencies": {
"@babel/template": "^7.26.9",
"@babel/types": "^7.26.9"
"@babel/types": "^7.26.10"
},
"engines": {
"node": ">=6.9.0"
@ -463,7 +463,9 @@
}
},
"node_modules/@babel/types": {
"version": "7.26.9",
"version": "7.26.10",
"resolved": "https://registry.npmjs.org/@babel/types/-/types-7.26.10.tgz",
"integrity": "sha512-emqcG3vHrpxUKTrxcblR36dcrcoRDvKmnL/dCL6ZsHaShW80qxCAcNhzQZrpeM765VzEos+xOi4s+r4IXzTwdQ==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -580,6 +582,16 @@
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
}
},
"node_modules/@eslint/config-helpers": {
"version": "0.1.0",
"resolved": "https://registry.npmjs.org/@eslint/config-helpers/-/config-helpers-0.1.0.tgz",
"integrity": "sha512-kLrdPDJE1ckPo94kmPPf9Hfd0DU0Jw6oKYrhe+pwSC0iTUInmTa+w6fw8sGgcfkFJGNdWOUeOaDM4quW4a7OkA==",
"dev": true,
"license": "Apache-2.0",
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
}
},
"node_modules/@eslint/core": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@eslint/core/-/core-0.12.0.tgz",
@ -618,9 +630,9 @@
}
},
"node_modules/@eslint/js": {
"version": "9.21.0",
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.21.0.tgz",
"integrity": "sha512-BqStZ3HX8Yz6LvsF5ByXYrtigrV5AXADWLAGc7PH/1SxOb7/FIYYMszZZWiUou/GB9P2lXWk2SV4d+Z8h0nknw==",
"version": "9.22.0",
"resolved": "https://registry.npmjs.org/@eslint/js/-/js-9.22.0.tgz",
"integrity": "sha512-vLFajx9o8d1/oL2ZkpMYbkLv8nDB6yaIwFNt7nI4+I80U/z03SxmfOMsLbvWr3p7C+Wnoh//aOu2pQW8cS0HCQ==",
"dev": true,
"license": "MIT",
"engines": {
@ -1130,6 +1142,8 @@
},
"node_modules/@nodelib/fs.scandir": {
"version": "2.1.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.scandir/-/fs.scandir-2.1.5.tgz",
"integrity": "sha512-vq24Bq3ym5HEQm2NKCr3yXDwjc7vTsEThRDnkp2DK9p1uqLR+DHurm/NOTo0KG7HYHU7eppKZj3MyqYuMBf62g==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -1142,6 +1156,8 @@
},
"node_modules/@nodelib/fs.stat": {
"version": "2.0.5",
"resolved": "https://registry.npmjs.org/@nodelib/fs.stat/-/fs.stat-2.0.5.tgz",
"integrity": "sha512-RkhPPp2zrqDAQA/2jNhnztcPAlv64XdhIp7a7454A5ovI7Bukxgt7MX7udwAu3zg1DcpPU0rz3VV1SeaqvY4+A==",
"dev": true,
"license": "MIT",
"engines": {
@ -1150,6 +1166,8 @@
},
"node_modules/@nodelib/fs.walk": {
"version": "1.2.8",
"resolved": "https://registry.npmjs.org/@nodelib/fs.walk/-/fs.walk-1.2.8.tgz",
"integrity": "sha512-oGB+UxlgWcgQkgwo8GcEGwemoTFt3FIO9ababBmaGwXIoBKZ+GTy0pP185beGg7Llih/NSHSV2XAs1lnznocSg==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -1235,7 +1253,6 @@
},
"node_modules/@redocly/ajv": {
"version": "8.11.2",
"dev": true,
"license": "MIT",
"dependencies": {
"fast-deep-equal": "^3.1.1",
@ -1250,17 +1267,14 @@
},
"node_modules/@redocly/ajv/node_modules/json-schema-traverse": {
"version": "1.0.0",
"dev": true,
"license": "MIT"
},
"node_modules/@redocly/config": {
"version": "0.20.3",
"dev": true,
"license": "MIT"
},
"node_modules/@redocly/openapi-core": {
"version": "1.29.0",
"dev": true,
"license": "MIT",
"dependencies": {
"@redocly/ajv": "^8.11.2",
@ -1280,7 +1294,6 @@
},
"node_modules/@redocly/openapi-core/node_modules/brace-expansion": {
"version": "2.0.1",
"dev": true,
"license": "MIT",
"dependencies": {
"balanced-match": "^1.0.0"
@ -1288,7 +1301,6 @@
},
"node_modules/@redocly/openapi-core/node_modules/minimatch": {
"version": "5.1.6",
"dev": true,
"license": "ISC",
"dependencies": {
"brace-expansion": "^2.0.1"
@ -1458,9 +1470,9 @@
"license": "MIT"
},
"node_modules/@types/node": {
"version": "22.13.5",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.5.tgz",
"integrity": "sha512-+lTU0PxZXn0Dr1NBtC7Y8cR21AJr87dLLU953CWA6pMxxv/UDc7jYAY90upcrie1nRcD6XNG5HOYEDtgW5TxAg==",
"version": "22.13.10",
"resolved": "https://registry.npmjs.org/@types/node/-/node-22.13.10.tgz",
"integrity": "sha512-I6LPUvlRH+O6VRUqYOcMudhaIdUVWfsjnZavnsraHvpBwaEyMN29ry+0UVJhImYL16xsscu0aske3yA+uPOWfw==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -1494,15 +1506,17 @@
"license": "MIT"
},
"node_modules/@typescript-eslint/eslint-plugin": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.26.1.tgz",
"integrity": "sha512-2X3mwqsj9Bd3Ciz508ZUtoQQYpOhU/kWoUqIf49H8Z0+Vbh6UF/y0OEYp0Q0axOGzaBGs7QxRwq0knSQ8khQNA==",
"dev": true,
"license": "MIT",
"dependencies": {
"@eslint-community/regexpp": "^4.10.0",
"@typescript-eslint/scope-manager": "8.24.1",
"@typescript-eslint/type-utils": "8.24.1",
"@typescript-eslint/utils": "8.24.1",
"@typescript-eslint/visitor-keys": "8.24.1",
"@typescript-eslint/scope-manager": "8.26.1",
"@typescript-eslint/type-utils": "8.26.1",
"@typescript-eslint/utils": "8.26.1",
"@typescript-eslint/visitor-keys": "8.26.1",
"graphemer": "^1.4.0",
"ignore": "^5.3.1",
"natural-compare": "^1.4.0",
@ -1518,18 +1532,20 @@
"peerDependencies": {
"@typescript-eslint/parser": "^8.0.0 || ^8.0.0-alpha.0",
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/@typescript-eslint/parser": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/parser/-/parser-8.26.1.tgz",
"integrity": "sha512-w6HZUV4NWxqd8BdeFf81t07d7/YV9s7TCWrQQbG5uhuvGUAW+fq1usZ1Hmz9UPNLniFnD8GLSsDpjP0hm1S4lQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/scope-manager": "8.24.1",
"@typescript-eslint/types": "8.24.1",
"@typescript-eslint/typescript-estree": "8.24.1",
"@typescript-eslint/visitor-keys": "8.24.1",
"@typescript-eslint/scope-manager": "8.26.1",
"@typescript-eslint/types": "8.26.1",
"@typescript-eslint/typescript-estree": "8.26.1",
"@typescript-eslint/visitor-keys": "8.26.1",
"debug": "^4.3.4"
},
"engines": {
@ -1541,16 +1557,18 @@
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/@typescript-eslint/scope-manager": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/scope-manager/-/scope-manager-8.26.1.tgz",
"integrity": "sha512-6EIvbE5cNER8sqBu6V7+KeMZIC1664d2Yjt+B9EWUXrsyWpxx4lEZrmvxgSKRC6gX+efDL/UY9OpPZ267io3mg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/types": "8.24.1",
"@typescript-eslint/visitor-keys": "8.24.1"
"@typescript-eslint/types": "8.26.1",
"@typescript-eslint/visitor-keys": "8.26.1"
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
@ -1561,12 +1579,14 @@
}
},
"node_modules/@typescript-eslint/type-utils": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/type-utils/-/type-utils-8.26.1.tgz",
"integrity": "sha512-Kcj/TagJLwoY/5w9JGEFV0dclQdyqw9+VMndxOJKtoFSjfZhLXhYjzsQEeyza03rwHx2vFEGvrJWJBXKleRvZg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/typescript-estree": "8.24.1",
"@typescript-eslint/utils": "8.24.1",
"@typescript-eslint/typescript-estree": "8.26.1",
"@typescript-eslint/utils": "8.26.1",
"debug": "^4.3.4",
"ts-api-utils": "^2.0.1"
},
@ -1579,11 +1599,13 @@
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/@typescript-eslint/types": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/types/-/types-8.26.1.tgz",
"integrity": "sha512-n4THUQW27VmQMx+3P+B0Yptl7ydfceUj4ON/AQILAASwgYdZ/2dhfymRMh5egRUrvK5lSmaOm77Ry+lmXPOgBQ==",
"dev": true,
"license": "MIT",
"engines": {
@ -1595,12 +1617,14 @@
}
},
"node_modules/@typescript-eslint/typescript-estree": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/typescript-estree/-/typescript-estree-8.26.1.tgz",
"integrity": "sha512-yUwPpUHDgdrv1QJ7YQal3cMVBGWfnuCdKbXw1yyjArax3353rEJP1ZA+4F8nOlQ3RfS2hUN/wze3nlY+ZOhvoA==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/types": "8.24.1",
"@typescript-eslint/visitor-keys": "8.24.1",
"@typescript-eslint/types": "8.26.1",
"@typescript-eslint/visitor-keys": "8.26.1",
"debug": "^4.3.4",
"fast-glob": "^3.3.2",
"is-glob": "^4.0.3",
@ -1616,11 +1640,13 @@
"url": "https://opencollective.com/typescript-eslint"
},
"peerDependencies": {
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/@typescript-eslint/typescript-estree/node_modules/brace-expansion": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-2.0.1.tgz",
"integrity": "sha512-XnAIvQ8eM+kC6aULx6wuQiwVsnzsi9d3WxzV3FpWTGA19F621kwdbsAcFKXgKUHZWsy+mY6iL1sHTxWEFCytDA==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -1629,6 +1655,8 @@
},
"node_modules/@typescript-eslint/typescript-estree/node_modules/minimatch": {
"version": "9.0.5",
"resolved": "https://registry.npmjs.org/minimatch/-/minimatch-9.0.5.tgz",
"integrity": "sha512-G6T0ZX48xgozx7587koeX9Ys2NYy6Gmv//P89sEte9V9whIapMNF4idKxnW2QtCcLiTWlb/wfCabAtAFWhhBow==",
"dev": true,
"license": "ISC",
"dependencies": {
@ -1642,14 +1670,16 @@
}
},
"node_modules/@typescript-eslint/utils": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/utils/-/utils-8.26.1.tgz",
"integrity": "sha512-V4Urxa/XtSUroUrnI7q6yUTD3hDtfJ2jzVfeT3VK0ciizfK2q/zGC0iDh1lFMUZR8cImRrep6/q0xd/1ZGPQpg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@eslint-community/eslint-utils": "^4.4.0",
"@typescript-eslint/scope-manager": "8.24.1",
"@typescript-eslint/types": "8.24.1",
"@typescript-eslint/typescript-estree": "8.24.1"
"@typescript-eslint/scope-manager": "8.26.1",
"@typescript-eslint/types": "8.26.1",
"@typescript-eslint/typescript-estree": "8.26.1"
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
@ -1660,15 +1690,17 @@
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/@typescript-eslint/visitor-keys": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/@typescript-eslint/visitor-keys/-/visitor-keys-8.26.1.tgz",
"integrity": "sha512-AjOC3zfnxd6S4Eiy3jwktJPclqhFHNyd8L6Gycf9WUPoKZpgM5PjkxY1X7uSy61xVpiJDhhk7XT2NVsN3ALTWg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/types": "8.24.1",
"@typescript-eslint/types": "8.26.1",
"eslint-visitor-keys": "^4.2.0"
},
"engines": {
@ -1909,7 +1941,6 @@
},
"node_modules/agent-base": {
"version": "7.1.3",
"dev": true,
"license": "MIT",
"engines": {
"node": ">= 14"
@ -1976,7 +2007,6 @@
},
"node_modules/ansi-colors": {
"version": "4.1.3",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=6"
@ -2039,7 +2069,6 @@
},
"node_modules/argparse": {
"version": "2.0.1",
"dev": true,
"license": "Python-2.0"
},
"node_modules/async": {
@ -2161,7 +2190,6 @@
},
"node_modules/balanced-match": {
"version": "1.0.2",
"dev": true,
"license": "MIT"
},
"node_modules/big.js": {
@ -2249,7 +2277,6 @@
},
"node_modules/byte-base64": {
"version": "1.1.0",
"dev": true,
"license": "MIT"
},
"node_modules/call-bind-apply-helpers": {
@ -2335,7 +2362,6 @@
},
"node_modules/change-case": {
"version": "5.4.4",
"dev": true,
"license": "MIT"
},
"node_modules/char-regex": {
@ -2445,7 +2471,6 @@
},
"node_modules/colorette": {
"version": "1.4.0",
"dev": true,
"license": "MIT"
},
"node_modules/commander": {
@ -2597,7 +2622,6 @@
},
"node_modules/debug": {
"version": "4.4.0",
"dev": true,
"license": "MIT",
"dependencies": {
"ms": "^2.1.3"
@ -2822,18 +2846,19 @@
}
},
"node_modules/eslint": {
"version": "9.21.0",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-9.21.0.tgz",
"integrity": "sha512-KjeihdFqTPhOMXTt7StsDxriV4n66ueuF/jfPNC3j/lduHwr/ijDwJMsF+wyMJethgiKi5wniIE243vi07d3pg==",
"version": "9.22.0",
"resolved": "https://registry.npmjs.org/eslint/-/eslint-9.22.0.tgz",
"integrity": "sha512-9V/QURhsRN40xuHXWjV64yvrzMjcz7ZyNoF2jJFmy9j/SLk0u1OLSZgXi28MrXjymnjEGSR80WCdab3RGMDveQ==",
"dev": true,
"license": "MIT",
"dependencies": {
"@eslint-community/eslint-utils": "^4.2.0",
"@eslint-community/regexpp": "^4.12.1",
"@eslint/config-array": "^0.19.2",
"@eslint/config-helpers": "^0.1.0",
"@eslint/core": "^0.12.0",
"@eslint/eslintrc": "^3.3.0",
"@eslint/js": "9.21.0",
"@eslint/js": "9.22.0",
"@eslint/plugin-kit": "^0.2.7",
"@humanfs/node": "^0.16.6",
"@humanwhocodes/module-importer": "^1.0.1",
@ -2845,7 +2870,7 @@
"cross-spawn": "^7.0.6",
"debug": "^4.3.2",
"escape-string-regexp": "^4.0.0",
"eslint-scope": "^8.2.0",
"eslint-scope": "^8.3.0",
"eslint-visitor-keys": "^4.2.0",
"espree": "^10.3.0",
"esquery": "^1.5.0",
@ -2896,7 +2921,9 @@
}
},
"node_modules/eslint-scope": {
"version": "8.2.0",
"version": "8.3.0",
"resolved": "https://registry.npmjs.org/eslint-scope/-/eslint-scope-8.3.0.tgz",
"integrity": "sha512-pUNxi75F8MJ/GdeKtVLSbYg4ZI34J6C0C7sbL4YOp2exGwen7ZsuBqKzUhXd0qMQ362yET3z+uPwKeg/0C2XCQ==",
"dev": true,
"license": "BSD-2-Clause",
"dependencies": {
@ -2991,7 +3018,6 @@
},
"node_modules/eventemitter3": {
"version": "5.0.1",
"dev": true,
"license": "MIT"
},
"node_modules/events": {
@ -3048,11 +3074,12 @@
},
"node_modules/fast-deep-equal": {
"version": "3.1.3",
"dev": true,
"license": "MIT"
},
"node_modules/fast-glob": {
"version": "3.3.3",
"resolved": "https://registry.npmjs.org/fast-glob/-/fast-glob-3.3.3.tgz",
"integrity": "sha512-7MptL8U0cqcFdzIzwOTHoilX9x5BrNqye7Z/LuC7kCMRio1EMSyqRK3BEAUD7sXRq4iT4AzTVuZdhgQ2TCvYLg==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -3068,6 +3095,8 @@
},
"node_modules/fast-glob/node_modules/glob-parent": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
"integrity": "sha512-AOIgSQCepiJYwP3ARnGx+5VnTu2HBYdzbGP45eLw1vr3zB3vZLeyed1sC9hnbcOc9/SrMyM5RPQrkGz4aS9Zow==",
"dev": true,
"license": "ISC",
"dependencies": {
@ -3111,7 +3140,9 @@
}
},
"node_modules/fastq": {
"version": "1.19.0",
"version": "1.19.1",
"resolved": "https://registry.npmjs.org/fastq/-/fastq-1.19.1.tgz",
"integrity": "sha512-GwLTyxkCXjXbxqIhTsMI2Nui8huMPtnxg7krajPJAjnEG/iiOS7i+zCtWGZR9G0NBKbXKh6X9m9UIsYX/N6vvQ==",
"dev": true,
"license": "ISC",
"dependencies": {
@ -3128,7 +3159,6 @@
},
"node_modules/fetch-retry": {
"version": "6.0.0",
"dev": true,
"license": "MIT"
},
"node_modules/file-entry-cache": {
@ -3449,7 +3479,6 @@
},
"node_modules/https-proxy-agent": {
"version": "7.0.6",
"dev": true,
"license": "MIT",
"dependencies": {
"agent-base": "^7.1.2",
@ -3536,7 +3565,6 @@
},
"node_modules/index-to-position": {
"version": "0.1.2",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=18"
@ -4271,7 +4299,6 @@
},
"node_modules/js-levenshtein": {
"version": "1.1.6",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
@ -4279,12 +4306,10 @@
},
"node_modules/js-tokens": {
"version": "4.0.0",
"dev": true,
"license": "MIT"
},
"node_modules/js-yaml": {
"version": "4.1.0",
"dev": true,
"license": "MIT",
"dependencies": {
"argparse": "^2.0.1"
@ -4497,6 +4522,8 @@
},
"node_modules/merge2": {
"version": "1.4.1",
"resolved": "https://registry.npmjs.org/merge2/-/merge2-1.4.1.tgz",
"integrity": "sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==",
"dev": true,
"license": "MIT",
"engines": {
@ -4631,7 +4658,6 @@
},
"node_modules/ms": {
"version": "2.1.3",
"dev": true,
"license": "MIT"
},
"node_modules/nanoid": {
@ -4686,7 +4712,9 @@
}
},
"node_modules/npm-check-updates": {
"version": "17.1.14",
"version": "17.1.15",
"resolved": "https://registry.npmjs.org/npm-check-updates/-/npm-check-updates-17.1.15.tgz",
"integrity": "sha512-miATvKu5rjec/1wxc5TGDjpsucgtCHwRVZorZpDkS6NzdWXfnUWlN4abZddWb7XSijAuBNzzYglIdTm9SbgMVg==",
"dev": true,
"license": "Apache-2.0",
"bin": {
@ -4758,8 +4786,9 @@
}
},
"node_modules/openapi-fetch": {
"version": "0.13.4",
"dev": true,
"version": "0.13.5",
"resolved": "https://registry.npmjs.org/openapi-fetch/-/openapi-fetch-0.13.5.tgz",
"integrity": "sha512-AQK8T9GSKFREFlN1DBXTYsLjs7YV2tZcJ7zUWxbjMoQmj8dDSFRrzhLCbHPZWA1TMV3vACqfCxLEZcwf2wxV6Q==",
"license": "MIT",
"dependencies": {
"openapi-typescript-helpers": "^0.0.15"
@ -4767,7 +4796,6 @@
},
"node_modules/openapi-typescript": {
"version": "7.6.1",
"dev": true,
"license": "MIT",
"dependencies": {
"@redocly/openapi-core": "^1.28.0",
@ -4786,12 +4814,10 @@
},
"node_modules/openapi-typescript-helpers": {
"version": "0.0.15",
"dev": true,
"license": "MIT"
},
"node_modules/openapi-typescript/node_modules/parse-json": {
"version": "8.1.0",
"dev": true,
"license": "MIT",
"dependencies": {
"@babel/code-frame": "^7.22.13",
@ -4807,7 +4833,6 @@
},
"node_modules/openapi-typescript/node_modules/supports-color": {
"version": "9.4.0",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=12"
@ -4818,7 +4843,6 @@
},
"node_modules/openapi-typescript/node_modules/type-fest": {
"version": "4.35.0",
"dev": true,
"license": "(MIT OR CC0-1.0)",
"engines": {
"node": ">=16"
@ -4873,7 +4897,6 @@
},
"node_modules/p-queue": {
"version": "8.1.0",
"dev": true,
"license": "MIT",
"dependencies": {
"eventemitter3": "^5.0.1",
@ -4888,7 +4911,6 @@
},
"node_modules/p-timeout": {
"version": "6.1.4",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=14.16"
@ -4966,7 +4988,6 @@
},
"node_modules/picocolors": {
"version": "1.1.1",
"dev": true,
"license": "ISC"
},
"node_modules/picomatch": {
@ -5049,7 +5070,6 @@
},
"node_modules/pluralize": {
"version": "8.0.0",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=4"
@ -5163,9 +5183,9 @@
}
},
"node_modules/prettier": {
"version": "3.5.2",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.2.tgz",
"integrity": "sha512-lc6npv5PH7hVqozBR7lkBNOGXV9vMwROAPlumdBkX0wTbbzPu/U1hk5yL8p2pt4Xoc+2mkT8t/sow2YrV/M5qg==",
"version": "3.5.3",
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.5.3.tgz",
"integrity": "sha512-QQtaxnoDJeAkDvDKWCLiwIXkTgRhwYDEQCghU9Z6q03iyek/rxRh/2lC3HB7P8sWT2xC/y5JDctPLBIGzHKbhw==",
"dev": true,
"license": "MIT",
"bin": {
@ -5255,6 +5275,8 @@
},
"node_modules/queue-microtask": {
"version": "1.2.3",
"resolved": "https://registry.npmjs.org/queue-microtask/-/queue-microtask-1.2.3.tgz",
"integrity": "sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==",
"dev": true,
"funding": [
{
@ -5328,7 +5350,6 @@
},
"node_modules/require-from-string": {
"version": "2.0.2",
"dev": true,
"license": "MIT",
"engines": {
"node": ">=0.10.0"
@ -5411,7 +5432,9 @@
}
},
"node_modules/reusify": {
"version": "1.0.4",
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/reusify/-/reusify-1.1.0.tgz",
"integrity": "sha512-g6QUff04oZpHs0eG5p83rFLhHeV00ug/Yf9nZM6fLeUrPguBTkTQOdpAWWspMh55TZfVQDPaN3NQJfbVRAxdIw==",
"dev": true,
"license": "MIT",
"engines": {
@ -5421,6 +5444,8 @@
},
"node_modules/run-parallel": {
"version": "1.2.0",
"resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz",
"integrity": "sha512-5l4VyZR86LZ/lDxZTR6jqL8AFE2S0IFLMP26AbjsLVADxHdhB/c0GUsH+y39UfCi3dzz8OlQuPmnaJOMoDHQBA==",
"dev": true,
"funding": [
{
@ -5469,7 +5494,9 @@
"license": "MIT"
},
"node_modules/sass": {
"version": "1.85.0",
"version": "1.85.1",
"resolved": "https://registry.npmjs.org/sass/-/sass-1.85.1.tgz",
"integrity": "sha512-Uk8WpxM5v+0cMR0XjX9KfRIacmSG86RH4DCCZjLU2rFh5tyutt9siAXJ7G+YfxQ99Q6wrRMbMlVl6KqUms71ag==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -5871,7 +5898,9 @@
}
},
"node_modules/terser-webpack-plugin": {
"version": "5.3.11",
"version": "5.3.14",
"resolved": "https://registry.npmjs.org/terser-webpack-plugin/-/terser-webpack-plugin-5.3.14.tgz",
"integrity": "sha512-vkZjpUjb6OMS7dhV+tILUW6BhpDR7P2L/aQSAv+Uwk+m8KATX9EccViHTJR2qDtACKPIYndLGCyl3FMo+r2LMw==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -6196,8 +6225,9 @@
}
},
"node_modules/typescript": {
"version": "5.7.3",
"dev": true,
"version": "5.8.2",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.8.2.tgz",
"integrity": "sha512-aJn6wq13/afZp/jT9QZmwEjDqqvSGp1VT5GVg+f/t6/oVyrgXM6BY1h9BRh/O5p3PlUPAe+WuiEZOmb/49RqoQ==",
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
@ -6208,13 +6238,15 @@
}
},
"node_modules/typescript-eslint": {
"version": "8.24.1",
"version": "8.26.1",
"resolved": "https://registry.npmjs.org/typescript-eslint/-/typescript-eslint-8.26.1.tgz",
"integrity": "sha512-t/oIs9mYyrwZGRpDv3g+3K6nZ5uhKEMt2oNmAPwaY4/ye0+EH4nXIPYNtkYFS6QHm+1DFg34DbglYBz5P9Xysg==",
"dev": true,
"license": "MIT",
"dependencies": {
"@typescript-eslint/eslint-plugin": "8.24.1",
"@typescript-eslint/parser": "8.24.1",
"@typescript-eslint/utils": "8.24.1"
"@typescript-eslint/eslint-plugin": "8.26.1",
"@typescript-eslint/parser": "8.26.1",
"@typescript-eslint/utils": "8.26.1"
},
"engines": {
"node": "^18.18.0 || ^20.9.0 || >=21.1.0"
@ -6225,7 +6257,7 @@
},
"peerDependencies": {
"eslint": "^8.57.0 || ^9.0.0",
"typescript": ">=4.8.4 <5.8.0"
"typescript": ">=4.8.4 <5.9.0"
}
},
"node_modules/undici-types": {
@ -6280,7 +6312,6 @@
},
"node_modules/uri-js-replace": {
"version": "1.0.1",
"dev": true,
"license": "MIT"
},
"node_modules/url": {
@ -6311,7 +6342,6 @@
},
"node_modules/uuid": {
"version": "11.1.0",
"dev": true,
"funding": [
"https://github.com/sponsors/broofa",
"https://github.com/sponsors/ctavan"
@ -6480,6 +6510,8 @@
},
"node_modules/webpack-merge": {
"version": "6.0.1",
"resolved": "https://registry.npmjs.org/webpack-merge/-/webpack-merge-6.0.1.tgz",
"integrity": "sha512-hXXvrjtx2PLYx4qruKl+kyRSLc52V+cCvMxRjmKwoA+CBbbF5GfIBtR6kCvl0fYGqTUPKB+1ktVmTHqMOzgCBg==",
"dev": true,
"license": "MIT",
"dependencies": {
@ -6643,7 +6675,6 @@
},
"node_modules/yaml-ast-parser": {
"version": "0.0.43",
"dev": true,
"license": "Apache-2.0"
},
"node_modules/yargs": {
@ -6665,7 +6696,6 @@
},
"node_modules/yargs-parser": {
"version": "21.1.1",
"dev": true,
"license": "ISC",
"engines": {
"node": ">=12"
@ -6698,7 +6728,7 @@
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.13.5",
"@types/node": "^22.13.10",
"css-loader": "^7.1.2",
"date-fns": "^4.1.0",
"file-loader": "^6.2.0",
@ -6707,14 +6737,14 @@
"mini-css-extract-plugin": "^2.9.2",
"obsidian": "1.8.7",
"resolve-url-loader": "^5.0.0",
"sass": "^1.85.0",
"sass": "^1.85.1",
"sass-loader": "^16.0.5",
"sync-client": "file:../sync-client",
"terser-webpack-plugin": "^5.3.11",
"terser-webpack-plugin": "^5.3.14",
"ts-jest": "^29.2.6",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"typescript": "5.7.3",
"typescript": "5.8.2",
"url": "^0.11.4",
"virtual-scroller": "^1.13.1",
"webpack": "^5.98.0",
@ -6723,22 +6753,26 @@
},
"sync-client": {
"version": "0.0.0",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.13.5",
"dependencies": {
"byte-base64": "^1.1.0",
"fetch-retry": "^6.0.0",
"jest": "^29.7.0",
"openapi-fetch": "0.13.4",
"openapi-fetch": "0.13.5",
"openapi-typescript": "7.6.1",
"p-queue": "^8.1.0",
"uuid": "^11.1.0"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.13.10",
"jest": "^29.7.0",
"sync_lib": "file:../../backend/sync_lib/pkg",
"ts-jest": "^29.2.6",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"typescript": "5.7.3",
"typescript": "5.8.2",
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1"
"webpack-cli": "^6.0.1",
"webpack-merge": "^6.0.1"
}
},
"test-client": {
@ -6747,11 +6781,11 @@
"test-client": "dist/cli.js"
},
"devDependencies": {
"@types/node": "^22.13.5",
"@types/node": "^22.13.10",
"sync-client": "file:../sync-client",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"typescript": "5.7.3",
"typescript": "5.8.2",
"uuid": "^11.1.0",
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1"

View file

@ -21,10 +21,10 @@
},
"devDependencies": {
"concurrently": "^9.1.2",
"eslint": "9.21.0",
"eslint": "9.22.0",
"eslint-plugin-unused-imports": "^4.1.4",
"npm-check-updates": "^17.1.14",
"prettier": "^3.5.2",
"typescript-eslint": "8.24.1"
"npm-check-updates": "^17.1.15",
"prettier": "^3.5.3",
"typescript-eslint": "8.26.1"
}
}

View file

@ -1,29 +1,36 @@
{
"name": "sync-client",
"version": "0.0.0",
"private": true,
"main": "dist/index.js",
"version": "0.0.30",
"main": "dist/sync-client.node.js",
"browser": "dist/sync-client.web.js",
"types": "dist/types/index.d.ts",
"files": [
"dist/**/*"
],
"scripts": {
"dev": "webpack watch --mode development",
"build": "webpack --mode production",
"test": "NODE_OPTIONS=\"$NODE_OPTIONS --experimental-vm-modules\" jest"
},
"dependencies": {
"byte-base64": "^1.1.0",
"fetch-retry": "^6.0.0",
"openapi-fetch": "0.13.5",
"openapi-typescript": "7.6.1",
"p-queue": "^8.1.0",
"uuid": "^11.1.0"
},
"devDependencies": {
"tslib": "2.8.1",
"typescript": "5.7.3",
"sync_lib": "file:../../backend/sync_lib/pkg",
"@types/jest": "^29.5.14",
"@types/node": "^22.13.5",
"@types/node": "^22.13.10",
"jest": "^29.7.0",
"ts-jest": "^29.2.6",
"p-queue": "^8.1.0",
"fetch-retry": "^6.0.0",
"byte-base64": "^1.1.0",
"openapi-fetch": "0.13.4",
"openapi-typescript": "7.6.1",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"typescript": "5.8.2",
"webpack": "^5.98.0",
"webpack-cli": "^6.0.1"
"webpack-cli": "^6.0.1",
"webpack-merge": "^6.0.1",
"sync_lib": "file:../../backend/sync_lib/pkg"
}
}
}

View file

@ -1,6 +1,9 @@
import type { Logger } from "../tracing/logger";
import type { RelativePath } from "../persistence/database";
// Manages locks on documents to prevent concurrent modifications
// allowing the client's FileOperations implementation to be simpler.
// Locks are granted in a first-in-first-out order.
export class DocumentLocks {
private readonly locked = new Set<RelativePath>();
private readonly waiters = new Map<RelativePath, (() => void)[]>();

View file

@ -1,16 +1,27 @@
import type { FileSystemOperations } from "sync-client";
import type { Database, RelativePath } from "../persistence/database";
import type {
Database,
DocumentRecord,
RelativePath
} from "../persistence/database";
import { FileOperations } from "./file-operations";
import { Logger } from "../tracing/logger";
import { assertSetContainsExactly } from "../utils/assert-set-contains-exactly";
import type { FileSystemOperations } from "./filesystem-operations";
describe("File operations", () => {
class MockDatabase {
public async updatePath(
class MockDatabase implements Partial<Database> {
public getLatestDocumentByRelativePath(
_find: RelativePath
): DocumentRecord | undefined {
// no-op
return undefined;
}
public move(
_oldRelativePath: RelativePath,
_newRelativePath: RelativePath
): Promise<void> {
// this is called but irrelevant for this mock
): void {
// no-op
}
}

View file

@ -1,10 +1,6 @@
import type { Logger } from "src/tracing/logger";
import type { Logger } from "../tracing/logger";
import type { FileSystemOperations } from "./filesystem-operations";
import type {
Database,
DocumentId,
RelativePath
} from "src/persistence/database";
import type { Database, RelativePath } from "../persistence/database";
import { isBinary, isFileTypeMergable, mergeText } from "sync_lib";
import { SafeFileSystemOperations } from "./safe-filesystem-operations";
@ -17,7 +13,7 @@ export class FileOperations {
private readonly database: Database,
fs: FileSystemOperations
) {
this.fs = new SafeFileSystemOperations(fs);
this.fs = new SafeFileSystemOperations(fs, logger);
}
public async listAllFiles(): Promise<RelativePath[]> {
@ -35,7 +31,7 @@ export class FileOperations {
const decoder = new TextDecoder("utf-8");
// Normalize line endings to LF on Windows
// Normalize line-endings to LF on Windows
let text = decoder.decode(content);
text = text.replace(/\r\n/g, "\n");
@ -46,10 +42,6 @@ export class FileOperations {
return this.fs.getFileSize(path);
}
public async getModificationTime(path: RelativePath): Promise<Date> {
return this.fs.getModificationTime(path);
}
public async exists(path: RelativePath): Promise<boolean> {
return this.fs.exists(path);
}
@ -60,18 +52,23 @@ export class FileOperations {
path: RelativePath,
newContent: Uint8Array
): Promise<void> {
this.logger.debug(`Creating file: ${path}`);
await this.fs.write(path, newContent);
}
public async ensureClearPath(path: RelativePath): Promise<void> {
if (await this.fs.exists(path)) {
const deconflictedPath = await this.deconflictPath(path);
this.logger.debug(
`Didn't expect ${path} to exist, deconflicting by moving it to '${deconflictedPath}'`
);
await this.database.updatePath(path, deconflictedPath);
this.database.move(path, deconflictedPath);
await this.fs.rename(path, deconflictedPath);
} else {
await this.createParentDirectories(path);
}
await this.fs.write(path, newContent);
}
// Update the file at the given path.
@ -126,40 +123,25 @@ export class FileOperations {
return new TextEncoder().encode(resultText);
}
public async remove(path: RelativePath): Promise<void> {
this.logger.debug(`Deleting file: ${path}`);
return this.fs.delete(path);
public async delete(path: RelativePath): Promise<void> {
if (await this.exists(path)) {
this.logger.debug(`Deleting file: ${path}`);
return this.fs.delete(path);
} else {
this.logger.debug(`No need to delete '${path}', it doesn't exist`);
}
}
public async move(
oldPath: RelativePath,
newPath: RelativePath,
documentId?: DocumentId
newPath: RelativePath
): Promise<void> {
if (oldPath === newPath) {
return;
}
await this.ensureClearPath(newPath);
if (await this.fs.exists(newPath)) {
const deconflictedPath = await this.deconflictPath(newPath);
this.logger.debug(
`Conflict when moving '${oldPath}' to '${newPath}', the latter already exists, deconflicting by moving it to '${deconflictedPath}'`
);
const existingMetadata = this.database.getDocument(newPath);
if (
existingMetadata === undefined ||
existingMetadata.documentId !== documentId
) {
await this.database.updatePath(newPath, deconflictedPath);
await this.fs.rename(newPath, deconflictedPath);
} else {
await this.database.deleteDocument(newPath);
}
} else {
await this.createParentDirectories(newPath);
}
this.database.move(oldPath, newPath);
await this.fs.rename(oldPath, newPath);
}
@ -201,17 +183,12 @@ export class FileOperations {
);
stem = stem.replace(FileOperations.PARENTHESES_REGEX, "");
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
while (true) {
const newName =
currentCount === 0
? `${directory}${stem}${extension}`
: `${directory}${stem} (${currentCount})${extension}`;
if (await this.fs.exists(newName)) {
currentCount++;
} else {
return newName;
}
}
let newName = path;
do {
currentCount++;
newName = `${directory}${stem} (${currentCount})${extension}`;
} while (await this.fs.exists(newName));
return newName;
}
}

View file

@ -1,4 +1,4 @@
import type { RelativePath } from "src/persistence/database";
import type { RelativePath } from "../persistence/database";
export interface FileSystemOperations {
listAllFiles: () => Promise<RelativePath[]>;
@ -9,11 +9,8 @@ export interface FileSystemOperations {
updater: (currentContent: string) => string
) => Promise<string>;
getFileSize: (path: RelativePath) => Promise<number>;
getModificationTime: (path: RelativePath) => Promise<Date>;
exists: (path: RelativePath) => Promise<boolean>;
createDirectory: (path: RelativePath) => Promise<void>;
delete: (path: RelativePath) => Promise<void>;
// Must be able to handle renaming to a file that already exists
rename: (oldPath: RelativePath, newPath: RelativePath) => Promise<void>;
}

View file

@ -1,5 +1,7 @@
import type { RelativePath } from "src/persistence/database";
import type { RelativePath } from "../persistence/database";
import type { FileSystemOperations } from "./filesystem-operations";
import type { Logger } from "../tracing/logger";
import { DocumentLocks } from "./document-locks";
export class FileNotFoundError extends Error {
public constructor(message: string) {
@ -9,71 +11,134 @@ export class FileNotFoundError extends Error {
}
// Decorate FileSystemOperations replacing errors with FileNotFoundError
// if the accessed file doesn't exist.
// if the accessed file doesn't exist. It also ensures that there's only
// ever a single request in-flight for any one file through the use of
// DocumentLocks.
export class SafeFileSystemOperations implements FileSystemOperations {
public constructor(private readonly fs: FileSystemOperations) {}
private readonly locks: DocumentLocks;
public constructor(
private readonly fs: FileSystemOperations,
private readonly logger: Logger
) {
this.locks = new DocumentLocks(logger);
}
public async listAllFiles(): Promise<RelativePath[]> {
return this.fs.listAllFiles();
}
public async read(path: RelativePath): Promise<Uint8Array> {
return this.safeOperation(path, async () => this.fs.read(path));
this.logger.debug(`Reading file: ${path}`);
return this.safeOperation(
path,
this.decorateToHoldLock(path, async () => this.fs.read(path)),
"read"
);
}
public async write(path: RelativePath, content: Uint8Array): Promise<void> {
return this.fs.write(path, content);
this.logger.debug(`Writing file: ${path}`);
return this.decorateToHoldLock(path, async () =>
this.fs.write(path, content)
)();
}
public async atomicUpdateText(
path: RelativePath,
updater: (currentContent: string) => string
): Promise<string> {
return this.safeOperation(path, async () =>
this.fs.atomicUpdateText(path, updater)
this.logger.debug(`Atomic update of file: ${path}`);
return this.safeOperation(
path,
this.decorateToHoldLock(path, async () =>
this.fs.atomicUpdateText(path, updater)
),
"atomicUpdateText"
);
}
public async getFileSize(path: RelativePath): Promise<number> {
return this.safeOperation(path, async () => this.fs.getFileSize(path));
}
public async getModificationTime(path: RelativePath): Promise<Date> {
return this.safeOperation(path, async () =>
this.fs.getModificationTime(path)
this.logger.debug(`Getting file size: ${path}`);
return this.safeOperation(
path,
this.decorateToHoldLock(path, async () =>
this.fs.getFileSize(path)
),
"getFileSize"
);
}
public async exists(path: RelativePath): Promise<boolean> {
return this.fs.exists(path);
this.logger.debug(`Checking if file exists: ${path}`);
return this.decorateToHoldLock(path, async () =>
this.fs.exists(path)
)();
}
public async createDirectory(path: RelativePath): Promise<void> {
return this.fs.createDirectory(path);
this.logger.debug(`Creating directory: ${path}`);
return this.decorateToHoldLock(path, async () =>
this.fs.createDirectory(path)
)();
}
public async delete(path: RelativePath): Promise<void> {
return this.fs.delete(path);
this.logger.debug(`Deleting file: ${path}`);
return this.decorateToHoldLock(path, async () =>
this.fs.delete(path)
)();
}
public async rename(
oldPath: RelativePath,
newPath: RelativePath
): Promise<void> {
return this.safeOperation(oldPath, async () =>
this.fs.rename(oldPath, newPath)
this.logger.debug(`Renaming file: ${oldPath} to ${newPath}`);
return this.safeOperation(
oldPath,
this.decorateToHoldLock([oldPath, newPath], async () =>
this.fs.rename(oldPath, newPath)
),
"rename"
);
}
private decorateToHoldLock<T>(
pathOrPaths: RelativePath | RelativePath[],
operation: () => Promise<T>
): () => Promise<T> {
return async () => {
const paths = Array.isArray(pathOrPaths)
? pathOrPaths
: [pathOrPaths];
await Promise.all(
paths.map(async (path) => this.locks.waitForDocumentLock(path))
);
try {
return await operation();
} finally {
await Promise.all(
paths.map((path) => {
this.locks.unlockDocument(path);
})
);
}
};
}
private async safeOperation<T>(
path: RelativePath,
operation: () => Promise<T>
operation: () => Promise<T>,
operationName: string
): Promise<T> {
// Without locking the file, this isn't atomic, however, it's good enough practicaly.
// This will only break if the file exists, gets deleted and then immediately
// recreated while `operation` is running.
if (!(await this.fs.exists(path))) {
throw new FileNotFoundError(path);
throw new FileNotFoundError(
`File not found: ${path} before trying to ${operationName}`
);
}
try {
return await operation();
@ -81,7 +146,9 @@ export class SafeFileSystemOperations implements FileSystemOperations {
if (await this.fs.exists(path)) {
throw error;
} else {
throw new FileNotFoundError(path);
throw new FileNotFoundError(
`File not found: ${path} when trying to ${operationName}`
);
}
}
}

View file

@ -1,23 +1,43 @@
import type { Logger } from "../tracing/logger";
export type VaultUpdateId = number;
export type DocumentId = string;
export type RelativePath = string;
export interface DocumentMetadata {
parentVersionId: VaultUpdateId;
documentId: DocumentId;
hash: string;
}
import type { Logger } from "src/tracing/logger";
export interface StoredDocumentMetadata {
relativePath: RelativePath;
documentId: DocumentId;
parentVersionId: VaultUpdateId;
hash: string;
}
export interface StoredDatabase {
documents: Record<RelativePath, DocumentMetadata>;
documents: StoredDocumentMetadata[];
lastSeenUpdateId: VaultUpdateId | undefined;
}
export class Database {
private documents = new Map<RelativePath, DocumentMetadata>();
/**
* Represents a document in the database.
*
* It is mutable and its content should always represent the latest
* state of the document on disk based on the update events we have seen.
*/
export interface DocumentRecord {
relativePath: RelativePath;
documentId: DocumentId;
metadata: DocumentMetadata | undefined;
isDeleted: boolean;
updates: Promise<void>[];
parallelVersion: number;
}
export class Database {
private documents: DocumentRecord[];
private lastSeenUpdateId: VaultUpdateId | undefined;
public constructor(
@ -26,16 +46,21 @@ export class Database {
private readonly saveData: (data: StoredDatabase) => Promise<void>
) {
initialState ??= {};
if (initialState.documents) {
for (const [relativePath, metadata] of Object.entries(
initialState.documents
)) {
this.documents.set(relativePath, metadata);
}
}
this.ensureConsistency();
this.logger.debug(`Loaded ${this.documents.size} documents`);
this.documents =
initialState.documents?.map(
({ relativePath, documentId, ...metadata }) => ({
relativePath,
documentId,
metadata,
isDeleted: false,
updates: [],
parallelVersion: 0
})
) ?? [];
this.ensureConsistency();
this.logger.debug(`Loaded ${this.documents.length} documents`);
this.lastSeenUpdateId = initialState.lastSeenUpdateId;
this.logger.debug(
@ -43,109 +68,213 @@ export class Database {
);
}
public getDocuments(): Map<RelativePath, DocumentMetadata> {
return this.documents;
public get length(): number {
return this.documents.length;
}
public get resolvedDocuments(): DocumentRecord[] {
const paths = new Map<string, DocumentRecord[]>();
this.documents
.filter(({ metadata }) => metadata !== undefined)
.forEach((record) =>
paths.set(record.relativePath, [
record,
...(paths.get(record.relativePath) ?? [])
])
);
return Array.from(paths.values()).map((records) => {
records.sort(
(a, b) => b.parallelVersion - a.parallelVersion // descending
);
if (
records.length > 1 &&
records.some((current, i) =>
i === 0
? false
: records[i - 1].parallelVersion ===
current.parallelVersion
)
) {
throw new Error(
`Multiple documents with the same parallel version and path at ${records[0].relativePath}`
);
}
return records[0];
});
}
public getLastSeenUpdateId(): VaultUpdateId | undefined {
return this.lastSeenUpdateId;
}
public async setLastSeenUpdateId(
value: VaultUpdateId | undefined
): Promise<void> {
public setLastSeenUpdateId(value: VaultUpdateId | undefined): void {
this.lastSeenUpdateId = value;
await this.save();
this.save();
}
public async resetSyncState(): Promise<void> {
this.documents = new Map();
public resetSyncState(): void {
this.documents = [];
this.lastSeenUpdateId = 0;
await this.save();
this.save();
}
public updateDocumentMetadata(
metadata: {
parentVersionId: VaultUpdateId;
hash: string;
},
toUpdate: DocumentRecord
): void {
if (!this.documents.includes(toUpdate)) {
throw new Error("Document not found in database");
}
toUpdate.metadata = metadata;
this.save();
}
public removeDocumentPromise(promise: Promise<void>): void {
const entry = this.documents.find(({ updates }) =>
updates.includes(promise)
);
if (entry === undefined) {
throw new Error("Document not found by update promise");
}
entry.updates = entry.updates.filter((update) => update !== promise);
// No need to save as Promises don't get serialized
}
public getLatestDocumentByRelativePath(
find: RelativePath
): DocumentRecord | undefined {
const candidates = this.documents.filter(
({ relativePath }) => relativePath === find
);
candidates.sort((a, b) => b.parallelVersion - a.parallelVersion); // descending
return candidates[0];
}
public async getResolvedDocumentByRelativePath(
relativePath: RelativePath,
promise: Promise<void>
): Promise<DocumentRecord> {
const entry = this.getLatestDocumentByRelativePath(relativePath);
if (entry === undefined) {
throw new Error(
`Document not found by relative path: ${relativePath}, ${JSON.stringify(
this.documents,
null,
2
)}`
);
}
const currentPromises = entry.updates;
entry.updates = [...currentPromises, promise];
await Promise.all(currentPromises);
return entry;
}
public createNewPendingDocument(
documentId: DocumentId,
relativePath: RelativePath,
promise: Promise<void>
): DocumentRecord {
const previousEntry =
this.getLatestDocumentByRelativePath(relativePath);
const entry = {
relativePath,
documentId,
metadata: undefined,
isDeleted: false,
updates: [promise],
parallelVersion:
previousEntry?.parallelVersion === undefined
? 0
: previousEntry.parallelVersion + 1
};
this.documents.push(entry);
this.save();
return entry;
}
public getDocumentByDocumentId(
documentId: DocumentId
): [RelativePath, DocumentMetadata] | undefined {
return [...this.documents.entries()].find(
([_, metadata]) => metadata.documentId === documentId
);
find: DocumentId
): DocumentRecord | undefined {
return this.documents.find(({ documentId }) => documentId === find);
}
public async setDocument({
documentId,
relativePath,
parentVersionId,
hash
}: {
documentId: DocumentId;
relativePath: RelativePath;
parentVersionId: VaultUpdateId;
hash: string;
}): Promise<void> {
this.documents.set(relativePath, {
documentId,
parentVersionId,
hash
});
await this.save();
}
public async removeDocument(relativePath: RelativePath): Promise<void> {
this.documents.delete(relativePath);
await this.save();
}
public getDocument(
relativePath: RelativePath
): DocumentMetadata | undefined {
return this.documents.get(relativePath);
}
public async deleteDocument(relativePath: RelativePath): Promise<void> {
this.documents.delete(relativePath);
await this.save();
}
public async updatePath(
public move(
oldRelativePath: RelativePath,
newRelativePath: RelativePath
): Promise<void> {
const document = this.documents.get(oldRelativePath);
if (!document) {
): void {
const oldDocument =
this.getLatestDocumentByRelativePath(oldRelativePath);
if (oldDocument === undefined) {
return;
}
const newDocument =
this.getLatestDocumentByRelativePath(newRelativePath);
if (newDocument?.isDeleted === false) {
throw new Error(
`Cannot update physical path for document that does not exist: ${oldRelativePath}`
`Document already exists at new location: ${newRelativePath}`
);
}
if (this.documents.has(newRelativePath)) {
throw new Error(
`Cannot update physical path to path that is already in use: ${newRelativePath}`
);
}
oldDocument.relativePath = newRelativePath;
// We're in a strange state where the target of the move has just got deleted,
// however, its metadata might already have a bunch of updates queued up for
// the document at the new location. We need to keep these updates.
oldDocument.parallelVersion =
newDocument !== undefined ? newDocument.parallelVersion + 1 : 0;
this.documents.delete(oldRelativePath);
this.documents.set(newRelativePath, document);
await this.save();
this.save();
}
private async save(): Promise<void> {
public delete(relativePath: RelativePath): void {
const candidate = this.getLatestDocumentByRelativePath(relativePath);
if (candidate === undefined) {
throw new Error(
`Document not found by relative path: ${relativePath}`
);
}
candidate.isDeleted = true;
}
private save(): void {
this.ensureConsistency();
await this.saveData({
documents: Object.fromEntries(this.documents.entries()),
void this.saveData({
documents: this.resolvedDocuments.map(
({ relativePath, documentId, metadata }) => ({
documentId,
relativePath,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
...metadata! // resolvedDocuments only returns docs with metadata set
})
),
lastSeenUpdateId: this.lastSeenUpdateId
});
}
private ensureConsistency(): void {
const allMetadata = Array.from(this.documents.entries());
const idToPath = new Map<string, Array<string>>();
const idToPath = new Map<string, string[]>();
allMetadata.forEach(([name, metadata]) => {
idToPath.set(metadata.documentId, [
...(idToPath.get(metadata.documentId) ?? []),
name
this.resolvedDocuments.forEach(({ relativePath, documentId }) => {
idToPath.set(documentId, [
...(idToPath.get(documentId) ?? []),
relativePath
]);
});

View file

@ -1,5 +1,5 @@
import type { Logger } from "src/tracing/logger";
import { LogLevel } from "src/tracing/logger";
import type { Logger } from "../tracing/logger";
import { LogLevel } from "../tracing/logger";
export interface SyncSettings {
remoteUri: string;

View file

@ -0,0 +1,51 @@
import type { Settings } from "../persistence/settings";
import type { Logger } from "../tracing/logger";
import { createPromise } from "../utils/create-promise";
import { retriedFetchFactory } from "../utils/retried-fetch";
export class ConnectedState {
private resolveIsSyncEnabled: (() => void) | undefined;
private syncIsEnabled: Promise<void> | undefined;
public constructor(
settings: Settings,
private readonly logger: Logger
) {
settings.addOnSettingsChangeHandlers((newSettings, oldSettings) => {
if (!oldSettings.isSyncEnabled && newSettings.isSyncEnabled) {
this.handleComingOnline();
} else if (
oldSettings.isSyncEnabled &&
!newSettings.isSyncEnabled
) {
this.handleGoingOffline();
}
});
}
public getFetchImplementation(
fetch: typeof globalThis.fetch,
{ doRetries = true }: { doRetries: boolean } = { doRetries: true }
): typeof globalThis.fetch {
const retriedFetch = doRetries
? retriedFetchFactory(this.logger, fetch)
: fetch;
return async (input: RequestInfo | URL): Promise<Response> => {
if (this.syncIsEnabled !== undefined) {
await this.syncIsEnabled;
}
return retriedFetch(input);
};
}
private handleComingOnline(): void {
this.logger.debug("Sync is enabled");
this.resolveIsSyncEnabled?.();
}
private handleGoingOffline(): void {
this.logger.debug("Sync is disabled");
[this.syncIsEnabled, this.resolveIsSyncEnabled] = createPromise();
}
}

View file

@ -6,20 +6,22 @@ import type {
RelativePath,
VaultUpdateId
} from "../persistence/database";
import type { Logger } from "src/tracing/logger";
import { retriedFetchFactory } from "src/utils/retried-fetch";
import type { Settings } from "src/persistence/settings";
import type { Logger } from "../tracing/logger";
import type { Settings } from "../persistence/settings";
import type { ConnectedState } from "./connected-state";
export interface CheckConnectionResult {
isSuccessful: boolean;
message: string;
}
export class SyncService {
private client!: Client<paths>;
private clientWithoutRetries!: Client<paths>;
private _fetchImplementation: typeof globalThis.fetch = globalThis.fetch;
public constructor(
private readonly connectedState: ConnectedState,
private readonly settings: Settings,
private readonly logger: Logger
) {
@ -52,17 +54,19 @@ export class SyncService {
}
public async create({
documentId,
relativePath,
contentBytes,
createdDate
contentBytes
}: {
documentId?: DocumentId;
relativePath: RelativePath;
contentBytes: Uint8Array;
createdDate: Date;
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
}): Promise<components["schemas"]["DocumentVersionWithoutContent"]> {
const formData = new FormData();
if (documentId !== undefined) {
formData.append("document_id", documentId);
}
formData.append("relative_path", relativePath);
formData.append("created_date", createdDate.toISOString());
formData.append("content", new Blob([contentBytes]));
const response = await this.client.POST(
@ -100,18 +104,18 @@ export class SyncService {
parentVersionId,
documentId,
relativePath,
contentBytes,
createdDate
contentBytes
}: {
parentVersionId: VaultUpdateId;
documentId: DocumentId;
relativePath: RelativePath;
contentBytes: Uint8Array;
createdDate: Date;
}): Promise<components["schemas"]["DocumentUpdateResponse"]> {
this.logger.debug(
`Updating document ${documentId} with parent version ${parentVersionId} and relative path ${relativePath}`
);
const formData = new FormData();
formData.append("parent_version_id", parentVersionId.toString());
formData.append("created_date", createdDate.toISOString());
formData.append("relative_path", relativePath);
formData.append("content", new Blob([contentBytes]));
@ -149,13 +153,11 @@ export class SyncService {
public async delete({
documentId,
relativePath,
createdDate
relativePath
}: {
documentId: DocumentId;
relativePath: RelativePath;
createdDate: Date;
}): Promise<void> {
}): Promise<components["schemas"]["DocumentVersionWithoutContent"]> {
const response = await this.client.DELETE(
"/vaults/{vault_id}/documents/{document_id}",
{
@ -169,7 +171,6 @@ export class SyncService {
}
},
body: {
createdDate: createdDate.toISOString(),
relativePath
}
}
@ -295,11 +296,17 @@ export class SyncService {
private createClient(remoteUri: string): void {
this.client = createClient<paths>({
baseUrl: remoteUri,
fetch: retriedFetchFactory(this.logger, this._fetchImplementation)
fetch: this.connectedState.getFetchImplementation(
this._fetchImplementation
)
});
this.clientWithoutRetries = createClient<paths>({
baseUrl: remoteUri
baseUrl: remoteUri,
fetch: this.connectedState.getFetchImplementation(
this._fetchImplementation,
{ doRetries: false }
)
});
}
}

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