Merge branch 'main' into asch/multi-arch

This commit is contained in:
Andras Schmelczer 2025-08-30 11:03:30 +01:00
commit 436e900e3b
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
56 changed files with 1436 additions and 3064 deletions

99
CLAUDE.md Normal file
View file

@ -0,0 +1,99 @@
# CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
## Project Overview
VaultLink is a self-hosted Obsidian plugin for real-time collaborative file syncing. The project consists of a Rust-based sync server and a TypeScript frontend with three main components: an Obsidian plugin, a sync client library, and a test client.
## Architecture
### Core Components
- **sync-server/**: Rust-based WebSocket server with SQLite database for document versioning and real-time synchronization
- **frontend/sync-client/**: TypeScript library providing core sync functionality, WebSocket management, and file operations
- **frontend/obsidian-plugin/**: Obsidian plugin that integrates the sync client with Obsidian's API
- **frontend/test-client/**: CLI testing tool for the sync functionality
### Key Technologies
- **Backend**: Rust with Axum framework, SQLite with SQLx, WebSockets for real-time sync
- **Frontend**: TypeScript, Webpack for bundling, Jest for testing
- **Sync Algorithm**: Uses reconcile-text library for operational transformation
## Development Commands
### Server Development
```bash
cd sync-server
cargo run config-e2e.yml # Start development server
cargo test --verbose # Run Rust tests
cargo clippy --all-targets --all-features # Lint Rust code
cargo fmt --all -- --check # Check Rust formatting
```
### Frontend Development
```bash
cd frontend
npm run dev # Start development mode (watches sync-client and obsidian-plugin)
npm run build # Build all workspaces
npm run test # Run all tests
npm run lint # Lint and format TypeScript code
```
### Database Setup (Development)
```bash
cd sync-server
sqlx database create --database-url sqlite://db.sqlite3
sqlx migrate run --source src/app_state/database/migrations --database-url sqlite://db.sqlite3
cargo sqlx prepare --workspace
```
### Scripts
- `scripts/check.sh`: Full CI check (builds, lints, tests both server and frontend)
- `scripts/e2e.sh`: End-to-end testing
- `scripts/clean-up.sh`: Clean logs and database files
- `scripts/bump-version.sh patch`: Publish new version
- `scripts/update-api-types.sh`: Update TypeScript bindings from Rust types
## Code Structure
### Workspace Configuration
The frontend uses npm workspaces with three packages:
- `sync-client`: Core synchronization logic
- `obsidian-plugin`: Obsidian-specific integration
- `test-client`: Testing utilities
### Type Generation
Rust structs generate TypeScript types via ts-rs crate, stored in `sync-server/bindings/` and used by frontend packages.
### Key Files
- `sync-server/src/`: Rust server implementation with WebSocket handlers
- `frontend/sync-client/src/sync-client.ts`: Main sync client entry point
- `frontend/obsidian-plugin/src/vault-link-plugin.ts`: Main Obsidian plugin class
- `frontend/sync-client/src/services/sync-service.ts`: Core synchronization logic
## Testing
### Running Tests
- Server: `cargo test --verbose`
- Frontend: `npm run test` (runs Jest across all workspaces)
- E2E: `scripts/e2e.sh`
### Test Structure
- Rust: Unit tests alongside source files
- TypeScript: `.test.ts` files using Jest
- E2E: Uses test-client to simulate multiple concurrent users
## Code Style
### Rust
- Uses extensive Clippy lints (see Cargo.toml)
- Follows pedantic linting rules
- Forbids unsafe code
- Uses cargo fmt with default settings
### TypeScript
- Prettier configuration: 4-space tabs, trailing commas removed, LF line endings
- ESLint with unused imports plugin
- Consistent across all three frontend packages

View file

@ -20,7 +20,14 @@ export default [
"no-unused-vars": "off",
"@typescript-eslint/restrict-template-expressions": "off",
"@typescript-eslint/no-unused-vars": "off",
"@typescript-eslint/no-floating-promises": "error",
"@typescript-eslint/no-floating-promises": [
"error",
{
allowForKnownSafeCalls: [
{ from: "package", name: ["suite", "test"], package: "node:test" },
],
},
],
"@typescript-eslint/parameter-properties": "off",
"@typescript-eslint/require-await": "off",
"@typescript-eslint/class-methods-use-this": "off",

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -1,7 +1,7 @@
{
"id": "vault-link",
"name": "VaultLink",
"version": "0.6.1",
"version": "0.6.3",
"minAppVersion": "0.0.0",
"description": "Self-hosted synchronization and collaboration for your Vault.",
"author": "Andras Schmelczer",

View file

@ -1,40 +1,38 @@
{
"name": "vault-link-obsidian-plugin",
"version": "0.6.1",
"version": "0.6.3",
"description": "This is a sample plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
"dev": "webpack watch --mode development",
"build": "webpack --mode production",
"test": "jest",
"test": "echo \"no tests defined\" && exit 0",
"version": "node version-bump.mjs"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.15.30",
"css-loader": "^7.1.2",
"date-fns": "^4.1.0",
"file-loader": "^6.2.0",
"fs-extra": "^11.3.0",
"jest": "^29.7.0",
"mini-css-extract-plugin": "^2.9.2",
"obsidian": "1.8.7",
"reconcile-text": "^0.5.0",
"resolve-url-loader": "^5.0.0",
"sass": "^1.89.1",
"sass": "^1.91.0",
"sass-loader": "^16.0.5",
"sync-client": "file:../sync-client",
"terser-webpack-plugin": "^5.3.14",
"ts-jest": "^29.3.4",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3",
"url": "^0.11.4",
"virtual-scroller": "^1.13.1",
"webpack": "^5.99.9",
"webpack-cli": "^6.0.1",
"reconcile-text": "^0.5.0"
"webpack-cli": "^6.0.1"
}
}
}

View file

@ -1,7 +1,10 @@
import type { Stat, Vault, Workspace } from "obsidian";
import { MarkdownView, normalizePath } from "obsidian";
import type { FileSystemOperations, RelativePath } from "sync-client";
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
import {
utils,
type FileSystemOperations,
type RelativePath
} from "sync-client";
import { getSelectionsFromEditor } from "./views/cursors/get-selections-from-editor";
import type { TextWithCursors, CursorPosition } from "reconcile-text";
@ -105,10 +108,10 @@ export class ObsidianFileSystemOperations implements FileSystemOperations {
const from = result.cursors[2 * i];
const to = result.cursors[2 * i + 1];
const { line: fromLine, column: fromColumn } =
positionToLineAndColumn(result.text, from.position);
utils.positionToLineAndColumn(result.text, from.position);
const { line: toLine, column: toColumn } =
positionToLineAndColumn(result.text, to.position);
utils.positionToLineAndColumn(result.text, to.position);
selections.push({
anchor: { line: fromLine, ch: fromColumn },

View file

@ -1,61 +0,0 @@
import { positionToLineAndColumn } from "./position-to-line-and-column";
describe("positionToLineAndColumn", () => {
test("converts position to line and column in multi-line text", () => {
const text = "ab\ncd\n";
expect(positionToLineAndColumn(text, 0)).toEqual({
line: 0,
column: 0
});
expect(positionToLineAndColumn(text, 1)).toEqual({
line: 0,
column: 1
});
expect(positionToLineAndColumn(text, 2)).toEqual({
line: 0,
column: 2
});
expect(positionToLineAndColumn(text, 3)).toEqual({
line: 1,
column: 0
});
expect(positionToLineAndColumn(text, 4)).toEqual({
line: 1,
column: 1
});
expect(positionToLineAndColumn(text, 6)).toEqual({
line: 2,
column: 0
});
});
test("with carrige returns", () => {
expect(positionToLineAndColumn("a\nb", 3)).toEqual({
line: 1,
column: 1
});
expect(positionToLineAndColumn("a\r\nb", 3)).toEqual({
line: 1,
column: 1
});
});
test("handles empty input", () => {
expect(positionToLineAndColumn("", 0)).toEqual({ line: 0, column: 0 });
});
test("handles positions at the end of text", () => {
const text = "End";
expect(positionToLineAndColumn(text, 3)).toEqual({
line: 0,
column: 3
});
});
test("throws error for position out of range", () => {
const text = "Short text";
expect(() => positionToLineAndColumn(text, 15)).toThrow();
expect(() => positionToLineAndColumn(text, -1)).toThrow();
});
});

View file

@ -1,3 +0,0 @@
export async function sleep(ms: number): Promise<void> {
return new Promise((resolve) => setTimeout(resolve, ms));
}

View file

@ -1,29 +1,33 @@
import type {
MarkdownView,
Editor,
MarkdownFileInfo,
TAbstractFile,
WorkspaceLeaf
} from "obsidian";
import type { MarkdownView } from "obsidian";
import { Platform, Plugin, TFile } from "obsidian";
import "../manifest.json";
import { HistoryView } from "./views/history/history-view";
import { StatusBar } from "./views/status-bar/status-bar";
import { LogsView } from "./views/logs/logs-view";
import { StatusDescription } from "./views/status-description/status-description";
import { SyncClient, rateLimit, DEFAULT_SETTINGS, Logger } from "sync-client";
import {
SyncClient,
rateLimit,
DEFAULT_SETTINGS,
Logger,
debugging
} from "sync-client";
import { ObsidianFileSystemOperations } from "./obsidian-file-system";
import { SyncSettingsTab } from "./views/settings/settings-tab";
import { logToConsole } from "./utils/log-to-console";
import { updateEditorStatusDisplay } from "./views/editor-sync-line/editor-sync-line";
import { EditorStatusDisplayManager } from "./views/editor-status-display-manager/editor-status-display-manager";
import { remoteCursorsTheme } from "./views/cursors/remote-cursor-theme";
import {
remoteCursorsPlugin,
RemoteCursorsPluginValue
} from "./views/cursors/remote-cursors-plugin";
import { LocalCursorUpdateListener } from "./views/cursors/local-cursor-update-listener";
import { slowFetchFactory } from "./debugging/slow-fetch-factory";
import { flakyWebSocketFactory } from "./debugging/flaky-websocket-factory";
import { renderCursorsInFileExplorer } from "./views/cursors/file-explorer";
const MIN_WAIT_BETWEEN_UPDATES_IN_MS = 250;
@ -48,8 +52,8 @@ export default class VaultLinkPlugin extends Plugin {
const debugOptions = isDebugBuild
? {
fetch: slowFetchFactory(1),
webSocket: flakyWebSocketFactory(1, new Logger())
fetch: debugging.slowFetchFactory(1),
webSocket: debugging.slowWebSocketFactory(1, new Logger())
}
: {};
@ -66,7 +70,9 @@ export default class VaultLinkPlugin extends Plugin {
...debugOptions
});
logToConsole(this.client);
if (isDebugBuild) {
debugging.logToConsole(this.client);
}
const statusDescription = new StatusDescription(this.client);
@ -94,6 +100,7 @@ export default class VaultLinkPlugin extends Plugin {
this.client.addRemoteCursorsUpdateListener((cursors) => {
RemoteCursorsPluginValue.setCursors(cursors, this.app);
renderCursorsInFileExplorer(cursors, this.app);
});
const cursorListener = new LocalCursorUpdateListener(
@ -122,17 +129,23 @@ export default class VaultLinkPlugin extends Plugin {
this.registerEditorEvents();
await this.client.start();
const interval = setInterval(() => {
updateEditorStatusDisplay(this.app.workspace, this.client);
}, 200);
const editorStatusDisplayManager = new EditorStatusDisplayManager(
this,
this.app.workspace,
this.client
);
this.disposables.push(() => {
clearInterval(interval);
editorStatusDisplayManager.stop();
});
});
}
public onunload(): void {
this.client.stop();
this.client.waitAndStop().catch((err: unknown) => {
this.client.logger.error(
`Error while stopping the sync client: ${err}`
);
});
this.disposables.forEach((disposable) => {
disposable();
});

View file

@ -0,0 +1,15 @@
.remote-users {
display: flex;
flex-wrap: wrap;
gap: var(--size-4-2);
margin-left: var(--size-4-2);
span {
border-radius: var(--radius-l);
padding: 0 var(--size-4-1);
border-width: 1.4px;
border-style: solid;
font-size: var(--font-smallest);
font-style: italic;
}
}

View file

@ -0,0 +1,55 @@
import "./file-explorer.scss";
import type { App, View } from "obsidian";
import {
utils,
type MaybeOutdatedClientCursors,
type RelativePath
} from "sync-client";
const REMOTE_USER_CONTAINER_CLASS = "remote-users";
export function renderCursorsInFileExplorer(
cursors: MaybeOutdatedClientCursors[],
app: App
): void {
const fileExplorers = app.workspace.getLeavesOfType("file-explorer");
if (fileExplorers.length == 0) return;
const [fileExplorer] = fileExplorers;
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
const fileExplorerView: View & {
fileItems: Record<RelativePath, { el: Element }>; // it's an internal API
} = fileExplorer.view as any; // eslint-disable-line
for (const key in fileExplorerView.fileItems) {
const element =
fileExplorerView.fileItems[key].el.querySelector(".tree-item-self");
const customElement = createDiv(
{
cls: REMOTE_USER_CONTAINER_CLASS
},
(parent) => {
cursors.forEach((cursor) => {
cursor.documentsWithCursors.forEach((document) => {
if (document.relative_path === key) {
parent.appendChild(
createSpan({
text: cursor.userName,
attr: {
style: `border-color: ${utils.getRandomColor(cursor.userName)}`
}
})
);
}
});
});
}
);
element?.querySelector("." + REMOTE_USER_CONTAINER_CLASS)?.remove();
element?.appendChild(customElement);
}
}

View file

@ -1,5 +1,5 @@
import type { Editor } from "obsidian";
import { lineAndColumnToPosition } from "../../utils/line-and-column-to-position";
import { utils } from "sync-client";
export interface Selection {
id: number;
@ -11,7 +11,7 @@ export function getSelectionsFromEditor(editor: Editor): Selection[] {
const text = editor.getValue();
return editor.listSelections().map(({ anchor, head }, i) => ({
id: i,
start: lineAndColumnToPosition(text, anchor.line, anchor.ch),
end: lineAndColumnToPosition(text, head.line, head.ch)
start: utils.lineAndColumnToPosition(text, anchor.line, anchor.ch),
end: utils.lineAndColumnToPosition(text, head.line, head.ch)
}));
}

View file

@ -9,12 +9,15 @@ import type {
ViewUpdate
} from "@codemirror/view";
import { RemoteCursorWidget } from "./remote-cursor-widget";
import type { CursorSpan, MaybeOutdatedClientCursors } from "sync-client";
import {
utils,
type CursorSpan,
type MaybeOutdatedClientCursors
} from "sync-client";
import type { App } from "obsidian";
import { MarkdownView } from "obsidian";
import { StateEffect } from "@codemirror/state";
import { getRandomColor } from "src/utils/get-random-color";
import type { SpanWithHistory } from "reconcile-text";
import { reconcileWithHistory } from "reconcile-text";
@ -155,7 +158,7 @@ export class RemoteCursorsPluginValue implements PluginValue {
RemoteCursorsPluginValue.cursors.forEach(
({ name, span: { start, end } }) => {
const color = getRandomColor(name);
const color = utils.getRandomColor(name);
const startLine = update.view.state.doc.lineAt(start);
const endLine = update.view.state.doc.lineAt(end);

View file

@ -0,0 +1,97 @@
import type { Workspace } from "obsidian";
import { FileView, setIcon } from "obsidian";
import type { SyncClient } from "sync-client";
import { DocumentSyncStatus } from "sync-client";
import "./editor-status-display-manager.scss";
import type VaultLinkPlugin from "src/vault-link-plugin";
import { HistoryView } from "../history/history-view";
export class EditorStatusDisplayManager {
private static readonly UPDATE_INTERVAL_IN_MS = 100;
private readonly intervalId: NodeJS.Timeout;
private readonly lastStatuses = new Map<string, DocumentSyncStatus>();
public constructor(
private readonly plugin: VaultLinkPlugin,
private readonly workspace: Workspace,
private readonly client: SyncClient
) {
this.intervalId = setInterval(() => {
this.updateEditorStatusDisplay();
}, EditorStatusDisplayManager.UPDATE_INTERVAL_IN_MS);
}
public stop(): void {
clearInterval(this.intervalId);
}
private updateEditorStatusDisplay(): void {
this.workspace.iterateAllLeaves((leaf) => {
if (leaf.view instanceof FileView) {
const filePath = leaf.view.file?.path;
if (filePath == null) {
return;
}
const element = this.getElementFromLeaf(leaf.view);
if (element == null) {
return;
}
const previousStatus = this.lastStatuses.get(filePath);
const currentStatus =
this.client.getDocumentSyncingStatus(filePath);
if (previousStatus === currentStatus) {
return;
}
this.lastStatuses.set(filePath, currentStatus);
if (currentStatus == DocumentSyncStatus.SYNCING_IS_DISABLED) {
element.remove();
return;
}
if (currentStatus == DocumentSyncStatus.SYNCING) {
element.classList.add("loading");
} else {
element.classList.remove("loading");
}
const iconContainer = element.querySelector(".icon");
if (iconContainer != null) {
setIcon(
iconContainer as HTMLElement, // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
currentStatus == DocumentSyncStatus.SYNCING
? "loader"
: "circle-check"
);
}
}
});
}
private getElementFromLeaf(fileView: FileView): Element | undefined {
const parent = fileView.contentEl.querySelector(".cm-editor");
if (parent == null) {
return;
}
return (
parent.querySelector(".vault-link-sync-status") ??
parent.createDiv(
{
cls: "vault-link-sync-status"
},
(el) => {
el.createSpan({ text: "VaultLink sync state" });
el.createDiv({
cls: "icon"
});
el.onclick = async (): Promise<void> =>
this.plugin.activateView(HistoryView.TYPE);
}
)
);
}
}

View file

@ -1,55 +0,0 @@
import type { Workspace } from "obsidian";
import { FileView, setIcon } from "obsidian";
import type { SyncClient } from "sync-client";
import { DocumentSyncStatus } from "sync-client";
import "./editor-sync-line.scss";
export function updateEditorStatusDisplay(
workspace: Workspace,
client: SyncClient
): void {
workspace.iterateAllLeaves((leaf) => {
if (leaf.view instanceof FileView) {
const filePath = leaf.view.file?.path;
if (filePath == null) {
return;
}
const parent = leaf.view.contentEl.querySelector(".cm-editor");
if (parent == null) {
return;
}
const element =
parent.querySelector(".vault-link-sync-status") ??
parent.createDiv(
{
cls: "vault-link-sync-status"
},
(el) => {
el.createSpan({ text: "VaultLink sync state" });
el.createDiv({
cls: "icon"
});
}
);
const isLoading =
client.getDocumentSyncingStatus(filePath) ==
DocumentSyncStatus.SYNCING;
if (isLoading) {
element.classList.add("loading");
} else {
element.classList.remove("loading");
}
const iconContainer = element.querySelector(".icon");
if (iconContainer != null) {
setIcon(
iconContainer as HTMLElement, // eslint-disable-line @typescript-eslint/no-unsafe-type-assertion
isLoading ? "loader" : "circle-check"
);
}
}
});
}

View file

@ -8,7 +8,7 @@
"allowSyntheticDefaultImports": true,
"lib": [
"DOM",
"ESNext"
"ES2024"
]
},
"exclude": [

File diff suppressed because it is too large Load diff

View file

@ -24,7 +24,7 @@
"eslint": "9.28.0",
"eslint-plugin-unused-imports": "^4.1.4",
"npm-check-updates": "^18.0.1",
"prettier": "^3.5.3",
"typescript-eslint": "8.33.1"
"prettier": "^3.6.2",
"typescript-eslint": "8.41.0"
}
}

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -1,6 +1,6 @@
{
"name": "sync-client",
"version": "0.6.1",
"version": "0.6.3",
"main": "dist/sync-client.node.js",
"browser": "dist/sync-client.web.js",
"types": "dist/types/index.d.ts",
@ -10,26 +10,24 @@
"scripts": {
"dev": "webpack watch --mode development",
"build": "webpack --mode production",
"test": "jest"
"test": "tsx --test src/**/*.test.ts"
},
"dependencies": {
"byte-base64": "^1.1.0",
"minimatch": "^10.0.1",
"p-queue": "^8.1.0",
"uuid": "^11.1.0",
"reconcile-text": "^0.5.0"
"reconcile-text": "^0.5.0",
"uuid": "^11.1.0"
},
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/node": "^22.15.30",
"jest": "^29.7.0",
"ts-jest": "^29.3.4",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3",
"webpack": "^5.99.9",
"webpack-cli": "^6.0.1",
"webpack-merge": "^6.0.1",
"ws": "^8.18.2"
"ws": "^8.18.3"
}
}

View file

@ -1,5 +1,6 @@
import type { LogLine, SyncClient } from "sync-client";
import { LogLevel } from "sync-client";
import type { SyncClient } from "../sync-client";
import type { LogLine } from "../tracing/logger";
import { LogLevel } from "../tracing/logger";
export function logToConsole(client: SyncClient): void {
client.logger.addOnMessageListener((logLine: LogLine) => {

View file

@ -1,3 +1,5 @@
import { sleep } from "../utils/sleep";
export const slowFetchFactory =
(jitterScaleInSeconds: number) =>
async (

View file

@ -1,7 +1,8 @@
import type { Logger } from "sync-client";
import { helpers } from "sync-client";
import { sleep } from "../utils/sleep";
import { Locks } from "../utils/locks";
import type { Logger } from "../tracing/logger";
export function flakyWebSocketFactory(
export function slowWebSocketFactory(
jitterScaleInSeconds: number,
logger: Logger
): typeof WebSocket {
@ -10,7 +11,7 @@ export function flakyWebSocketFactory(
private static readonly RECEIVE_KEY = "websocket-receive";
private static readonly SEND_KEY = "websocket-send";
private readonly locks = new helpers.Locks(logger);
private readonly locks = new Locks(logger);
public set onopen(callback: (event: Event) => void) {
super.onopen = async (event: Event): Promise<void> => {

View file

@ -1,3 +1,4 @@
import { describe, it } from "node:test";
import type {
Database,
DocumentRecord,

View file

@ -1,3 +1,10 @@
import { logToConsole } from "./debugging/log-to-console";
import { slowFetchFactory } from "./debugging/slow-fetch-factory";
import { slowWebSocketFactory } from "./debugging/slow-web-socket-factory";
import { getRandomColor } from "./utils/get-random-color";
import { lineAndColumnToPosition } from "./utils/line-and-column-to-position";
import { positionToLineAndColumn } from "./utils/position-to-line-and-column";
export {
SyncType,
SyncStatus,
@ -22,7 +29,14 @@ export type { MaybeOutdatedClientCursors } from "./types/maybe-outdated-client-c
export { DocumentSyncStatus } from "./types/document-sync-status";
export { SyncClient } from "./sync-client";
import { Locks } from "./utils/locks";
export const helpers = {
Locks
export const debugging = {
slowFetchFactory,
slowWebSocketFactory,
logToConsole
};
export const utils = {
getRandomColor,
positionToLineAndColumn,
lineAndColumnToPosition
};

View file

@ -70,7 +70,10 @@ export class SyncService {
formData.append("document_id", documentId);
}
formData.append("relative_path", relativePath);
formData.append("content", new Blob([contentBytes]));
formData.append(
"content",
new Blob([new Uint8Array(contentBytes)])
);
const response = await this.client(this.getUrl("/documents"), {
method: "POST",
@ -117,7 +120,10 @@ export class SyncService {
const formData = new FormData();
formData.append("parent_version_id", parentVersionId.toString());
formData.append("relative_path", relativePath);
formData.append("content", new Blob([contentBytes]));
formData.append(
"content",
new Blob([new Uint8Array(contentBytes)])
);
const response = await this.client(
this.getUrl(`/documents/${documentId}`),

View file

@ -13,10 +13,11 @@ export class WebSocketManager {
cursors: ClientCursors[]
) => unknown)[] = [];
private refreshWebSocketInterval: NodeJS.Timeout | undefined;
private webSocket: WebSocket | undefined;
private isStopped = true;
private _isFirstSyncCompleted = false;
private readonly webSocketFactoryImplementation: typeof globalThis.WebSocket;
public constructor(
@ -41,20 +42,15 @@ export class WebSocketManager {
}
}
this.updateWebSocket(settings.getSettings());
settings.addOnSettingsChangeListener((newSettings, oldSettings) => {
if (
newSettings.remoteUri !== oldSettings.remoteUri ||
newSettings.vaultName !== oldSettings.vaultName ||
newSettings.token !== oldSettings.token ||
newSettings.isSyncEnabled !== oldSettings.isSyncEnabled
newSettings.token !== oldSettings.token
) {
this.updateWebSocket(newSettings);
this.initializeWebSocket(newSettings);
}
});
this.setWebSocketRefreshInterval();
}
public get isWebSocketConnected(): boolean {
@ -64,6 +60,10 @@ export class WebSocketManager {
);
}
public get isFirstSyncCompleted(): boolean {
return this._isFirstSyncCompleted;
}
public addWebSocketStatusChangeListener(listener: () => unknown): void {
this.webSocketStatusChangeListeners.push(listener);
}
@ -74,19 +74,15 @@ export class WebSocketManager {
this.remoteCursorsUpdateListeners.push(listener);
}
public async reset(): Promise<void> {
this.setWebSocketRefreshInterval();
this.updateWebSocket(this.settings.getSettings());
public start(): void {
this.isStopped = false;
this._isFirstSyncCompleted = false;
this.initializeWebSocket(this.settings.getSettings());
}
public stop(): void {
clearInterval(this.refreshWebSocketInterval);
try {
this.webSocket?.close();
} catch (e) {
this.logger.warn(`Failed to close WebSocket: ${e}`);
}
this.isStopped = true;
this.webSocket?.close(1000, "WebSocketManager has been stopped");
}
public updateLocalCursors(cursorPositions: CursorPositionFromClient): void {
@ -101,23 +97,22 @@ export class WebSocketManager {
...cursorPositions
};
this.webSocket?.send(JSON.stringify(message));
this.logger.info(
this.logger.debug(
`Sent cursor positions: ${JSON.stringify(cursorPositions)}`
);
}
private updateWebSocket(settings: SyncSettings): void {
private initializeWebSocket(settings: SyncSettings): void {
if (this.isStopped) {
return;
}
try {
this.webSocket?.close();
} catch (e) {
this.logger.warn(`Failed to close WebSocket: ${e}`);
}
if (!settings.isSyncEnabled) {
this.webSocket = undefined;
return;
}
const wsUri = new URL(settings.remoteUri);
wsUri.protocol = wsUri.protocol === "https" ? "wss" : "ws";
wsUri.pathname = `/vaults/${settings.vaultName}/ws`;
@ -126,55 +121,10 @@ export class WebSocketManager {
this.webSocket = new this.webSocketFactoryImplementation(wsUri);
this.webSocket.onmessage = async (event): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const message = JSON.parse(event.data) as WebSocketServerMessage;
if (message.type === "vaultUpdate") {
try {
await Promise.all(
message.documents.map(async (document) =>
this.syncer.syncRemotelyUpdatedFile(document)
)
);
if (message.isInitialSync && message.documents.length > 0) {
this.database.setLastSeenUpdateId(
message.documents
.map((document) => document.vaultUpdateId)
.reduce((a, b) => Math.max(a, b))
);
}
} catch (e) {
this.logger.error(
`Failed to sync remotely updated file: ${e}`
);
}
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
} else if (message.type === "cursorPositions") {
this.logger.debug(
`Received cursor positions for ${JSON.stringify(message.clients)}`
);
this.remoteCursorsUpdateListeners.forEach((listener) => {
listener(
message.clients.filter(
(client) => client.deviceId !== this.deviceId
)
);
});
} else {
this.logger.warn(
`Received unknown message type: ${JSON.stringify(message)}`
);
}
};
// The JS WebSocket API doesn't support setting headers, so we have to send the token as a message
this.webSocket.onopen = (): void => {
this.logger.info("WebSocket connection opened");
this.webSocketStatusChangeListeners.forEach((listener) => {
listener();
});
this.webSocketStatusChangeListeners.forEach((l) => l());
const message: WebSocketClientMessage = {
type: "handshake",
@ -185,25 +135,65 @@ export class WebSocketManager {
this.webSocket?.send(JSON.stringify(message));
};
this.webSocket.onmessage = async (event): Promise<void> => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
const message = JSON.parse(event.data) as WebSocketServerMessage;
return this.handleWebSocketMessage(message);
};
this.webSocket.onclose = (event): void => {
this.logger.warn(
`WebSocket closed with code ${event.code} (${event.reason == "" ? "unknown reason" : event.reason})`
);
this.webSocketStatusChangeListeners.forEach((listener) => {
listener();
});
this.webSocketStatusChangeListeners.forEach((l) => l());
if (!this.isStopped) {
setTimeout(() => {
this.initializeWebSocket(this.settings.getSettings());
}, this.settings.getSettings().webSocketRetryIntervalMs);
}
};
}
private setWebSocketRefreshInterval(): void {
this.refreshWebSocketInterval = setInterval(() => {
if (
this.webSocket?.readyState ===
this.webSocketFactoryImplementation.CLOSED
) {
this.logger.info("WebSocket is closed, reconnecting...");
this.updateWebSocket(this.settings.getSettings());
private async handleWebSocketMessage(
message: WebSocketServerMessage
): Promise<void> {
if (message.type === "vaultUpdate") {
try {
await Promise.all(
message.documents.map(async (document) =>
this.syncer.syncRemotelyUpdatedFile(document)
)
);
if (message.isInitialSync && message.documents.length > 0) {
this.database.setLastSeenUpdateId(
message.documents
.map((document) => document.vaultUpdateId)
.reduce((a, b) => Math.max(a, b))
);
}
this._isFirstSyncCompleted = true;
} catch (e) {
this.logger.error(`Failed to sync remotely updated file: ${e}`);
}
}, this.settings.getSettings().webSocketRetryIntervalMs);
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
} else if (message.type === "cursorPositions") {
this.logger.debug(
`Received cursor positions for ${JSON.stringify(message.clients)}`
);
this.remoteCursorsUpdateListeners.forEach((listener) => {
listener(
message.clients.filter(
(client) => client.deviceId !== this.deviceId
)
);
});
} else {
this.logger.warn(
`Received unknown message type: ${JSON.stringify(message)}`
);
}
}
}

View file

@ -24,6 +24,8 @@ import { FileChangeNotifier } from "./sync-operations/file-change-notifier";
export class SyncClient {
private static readonly MINIMUM_SAVE_INTERVAL_MS = 1000;
private hasStartedOfflineSync = false;
private hasFinishedOfflineSync = false;
// eslint-disable-next-line @typescript-eslint/max-params
private constructor(
@ -43,6 +45,14 @@ export class SyncClient {
if (newSettings.vaultName !== oldSettings.vaultName) {
await this.reset();
}
if (newSettings.isSyncEnabled !== oldSettings.isSyncEnabled) {
if (newSettings.isSyncEnabled) {
await this.start();
} else {
this.stop();
}
}
}
);
}
@ -197,10 +207,17 @@ export class SyncClient {
}
public async start(): Promise<void> {
await this.syncer.scheduleSyncForOfflineChanges();
if (!this.hasStartedOfflineSync) {
await this.syncer.scheduleSyncForOfflineChanges();
this.hasStartedOfflineSync = true;
}
this.hasFinishedOfflineSync = true;
this.webSocketManager.start();
}
public stop(): void {
this.hasFinishedOfflineSync = false;
this.webSocketManager.stop();
}
@ -216,7 +233,6 @@ export class SyncClient {
this.stop();
this.connectionStatus.startReset();
await this.syncer.reset();
await this.webSocketManager.reset();
this.history.reset();
this.database.reset();
this._logger.reset();
@ -286,6 +302,17 @@ export class SyncClient {
public getDocumentSyncingStatus(
relativePath: RelativePath
): DocumentSyncStatus {
if (!this.settings.getSettings().isSyncEnabled) {
return DocumentSyncStatus.SYNCING_IS_DISABLED;
}
if (
!this.webSocketManager.isFirstSyncCompleted ||
!this.hasFinishedOfflineSync
) {
return DocumentSyncStatus.SYNCING;
}
const document =
this.database.getLatestDocumentByRelativePath(relativePath);
if (document === undefined) {

View file

@ -1,4 +1,5 @@
export enum DocumentSyncStatus {
UP_TO_DATE = "UP_TO_DATE",
SYNCING = "SYNCING"
SYNCING = "SYNCING",
SYNCING_IS_DISABLED = "SYNCING_IS_DISABLED"
}

View file

@ -1,7 +1,7 @@
import * as assert from "assert";
import assert from "node:assert";
export function assertSetContainsExactly<T>(set: Set<T>, ...values: T[]): void {
assert(
assert.ok(
set.size === values.length &&
Array.from(set).every((value) => values.includes(value)),
`Expected set to contain only ${values.map((v) => '"' + v + '"').join(", ")}, but it contained ${Array.from(

View file

@ -16,10 +16,8 @@ export function createPromise<T = unknown>(): [
const creationPromise = new Promise<T>(
(resolve_, reject_) =>
(
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
(resolve = resolve_ as ResolveFunction<T>), (reject = reject_)
)
// eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion
((resolve = resolve_ as ResolveFunction<T>), (reject = reject_))
);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

View file

@ -5,5 +5,5 @@ export function getRandomColor(name: string): string {
hash |= 0; // Convert to 32bit integer
}
const normalised = hash / 0x7fffffff;
return `hsl(${Math.abs(normalised * 360)}, 55%, 55%)`; // HSL color
return `oklch(0.58 0.15 ${Math.round(Math.abs(normalised * 360))})`;
}

View file

@ -1,3 +1,5 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { Logger } from "../tracing/logger";
import { globsToRegexes } from "./globs-to-regexes";
@ -5,7 +7,7 @@ describe("globsToRegexes", () => {
it("basicExample", async () => {
const [regex] = globsToRegexes([".git/**"], new Logger());
expect(regex.test(".git/objects/object")).toBeTruthy();
expect(regex.test(".git/objects/.object")).toBeTruthy();
assert.ok(regex.test(".git/objects/object"));
assert.ok(regex.test(".git/objects/.object"));
});
});

View file

@ -1,27 +1,29 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { isEqualBytes } from "./is-equal-bytes";
describe("isEqualBytes", () => {
it("should return true for equal byte arrays", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([1, 2, 3, 4]);
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
assert.strictEqual(isEqualBytes(bytes1, bytes2), true);
});
it("should return false for byte arrays of different lengths", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([1, 2, 3]);
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
assert.strictEqual(isEqualBytes(bytes1, bytes2), false);
});
it("should return true for empty byte arrays", () => {
const bytes1 = new Uint8Array([]);
const bytes2 = new Uint8Array([]);
expect(isEqualBytes(bytes1, bytes2)).toBe(true);
assert.strictEqual(isEqualBytes(bytes1, bytes2), true);
});
it("should return false for byte arrays with same length but different content", () => {
const bytes1 = new Uint8Array([1, 2, 3, 4]);
const bytes2 = new Uint8Array([4, 3, 2, 1]);
expect(isEqualBytes(bytes1, bytes2)).toBe(false);
assert.strictEqual(isEqualBytes(bytes1, bytes2), false);
});
});

View file

@ -1,28 +1,42 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { isFileTypeMergable } from "./is-file-type-mergable";
describe("isFileTypeMergable", () => {
it("should return true for .md files", () => {
expect(isFileTypeMergable(".md")).toBe(true);
expect(isFileTypeMergable("hi.md")).toBe(true);
expect(isFileTypeMergable("my/path/to/my/document.md")).toBe(true);
assert.strictEqual(isFileTypeMergable(".md"), true);
assert.strictEqual(isFileTypeMergable("hi.md"), true);
assert.strictEqual(
isFileTypeMergable("my/path/to/my/document.md"),
true
);
});
it("should return true for .txt files", () => {
expect(isFileTypeMergable(".txt")).toBe(true);
expect(isFileTypeMergable("hi.txt")).toBe(true);
expect(isFileTypeMergable("my/path/to/my/document.txt")).toBe(true);
assert.strictEqual(isFileTypeMergable(".txt"), true);
assert.strictEqual(isFileTypeMergable("hi.txt"), true);
assert.strictEqual(
isFileTypeMergable("my/path/to/my/document.txt"),
true
);
});
it("should be case insensitive", () => {
expect(isFileTypeMergable("hi.MD")).toBe(true);
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.MD")).toBe(true);
expect(isFileTypeMergable("hi.TXT")).toBe(true);
expect(isFileTypeMergable("my/path/to/my/DOCUMENT.TXT")).toBe(true);
assert.strictEqual(isFileTypeMergable("hi.MD"), true);
assert.strictEqual(
isFileTypeMergable("my/path/to/my/DOCUMENT.MD"),
true
);
assert.strictEqual(isFileTypeMergable("hi.TXT"), true);
assert.strictEqual(
isFileTypeMergable("my/path/to/my/DOCUMENT.TXT"),
true
);
});
it("should return false for non-mergable file types", () => {
expect(isFileTypeMergable(".json")).toBe(false);
expect(isFileTypeMergable("HELLO.JSON")).toBe(false);
expect(isFileTypeMergable("my/config.yml")).toBe(false);
assert.strictEqual(isFileTypeMergable(".json"), false);
assert.strictEqual(isFileTypeMergable("HELLO.JSON"), false);
assert.strictEqual(isFileTypeMergable("my/config.yml"), false);
});
});

View file

@ -1,42 +1,44 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { lineAndColumnToPosition } from "./line-and-column-to-position";
describe("lineAndColumnToPosition", () => {
it("should return the correct position for the first line", () => {
const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 0, 3);
expect(position).toBe(3);
assert.strictEqual(position, 3);
});
it("should return the correct position for the second line", () => {
const text = "Hello\nWorld";
const position = lineAndColumnToPosition(text, 1, 2);
expect(position).toBe(8);
assert.strictEqual(position, 8);
});
it("should return the correct position for an empty string", () => {
const text = "";
const position = lineAndColumnToPosition(text, 0, 0);
expect(position).toBe(0);
assert.strictEqual(position, 0);
});
it("with carrige return", () => {
expect(lineAndColumnToPosition("a\nb", 1, 1)).toBe(3);
expect(lineAndColumnToPosition("a\r\nb", 1, 1)).toBe(3);
assert.strictEqual(lineAndColumnToPosition("a\nb", 1, 1), 3);
assert.strictEqual(lineAndColumnToPosition("a\r\nb", 1, 1), 3);
});
it("should handle multi-line strings with varying lengths", () => {
const text = "Line1\nLongerLine2\nShort3";
const position = lineAndColumnToPosition(text, 2, 4);
expect(position).toBe(22);
assert.strictEqual(position, 22);
});
it("should throw an error if the line number is out of range", () => {
const text = "Line1\nLine2";
expect(() => lineAndColumnToPosition(text, 3, 0)).toThrow();
assert.throws(() => lineAndColumnToPosition(text, 3, 0));
});
it("should throw an error if the column number is out of range", () => {
const text = "Line1\nLine2";
expect(() => lineAndColumnToPosition(text, 1, 10)).toThrow();
assert.throws(() => lineAndColumnToPosition(text, 1, 10));
});
});

View file

@ -1,3 +1,5 @@
import { describe, it, beforeEach } from "node:test";
import assert from "node:assert";
import { Logger } from "../tracing/logger";
import type { RelativePath } from "../persistence/database";
import { Locks } from "./locks";
@ -14,18 +16,18 @@ describe("withLock", () => {
locks = new Locks<RelativePath>(logger);
});
test("should execute function with single key lock", async () => {
it("should execute function with single key lock", async () => {
let executionCount = 0;
const result = await locks.withLock(testPath, () => {
executionCount++;
return "success";
});
expect(result).toBe("success");
expect(executionCount).toBe(1);
assert.strictEqual(result, "success");
assert.strictEqual(executionCount, 1);
});
test("should execute async function with single key lock", async () => {
it("should execute async function with single key lock", async () => {
let executionCount = 0;
const result = await locks.withLock(testPath, async () => {
executionCount++;
@ -33,22 +35,22 @@ describe("withLock", () => {
return "async-success";
});
expect(result).toBe("async-success");
expect(executionCount).toBe(1);
assert.strictEqual(result, "async-success");
assert.strictEqual(executionCount, 1);
});
test("should execute function with multiple key locks", async () => {
it("should execute function with multiple key locks", async () => {
let executionCount = 0;
const result = await locks.withLock([testPath, testPath2], () => {
executionCount++;
return "multi-success";
});
expect(result).toBe("multi-success");
expect(executionCount).toBe(1);
assert.strictEqual(result, "multi-success");
assert.strictEqual(executionCount, 1);
});
test("should sort multiple keys to prevent deadlocks", async () => {
it("should sort multiple keys to prevent deadlocks", async () => {
const executionOrder: string[] = [];
// Start two concurrent operations with keys in different orders
@ -68,10 +70,10 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1");
expect(result2).toBe("result2");
assert.strictEqual(result1, "result1");
assert.strictEqual(result2, "result2");
// One operation should complete entirely before the other starts
expect(executionOrder).toEqual([
assert.deepStrictEqual(executionOrder, [
"operation1-start",
"operation1-end",
"operation2-start",
@ -79,7 +81,7 @@ describe("withLock", () => {
]);
});
test("should serialize access to same key", async () => {
it("should serialize access to same key", async () => {
const executionOrder: string[] = [];
const promise1 = locks.withLock(testPath, async () => {
@ -98,9 +100,9 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1");
expect(result2).toBe("result2");
expect(executionOrder).toEqual([
assert.strictEqual(result1, "result1");
assert.strictEqual(result2, "result2");
assert.deepStrictEqual(executionOrder, [
"operation1-start",
"operation1-end",
"operation2-start",
@ -108,7 +110,7 @@ describe("withLock", () => {
]);
});
test("should allow concurrent access to different keys", async () => {
it("should allow concurrent access to different keys", async () => {
const executionOrder: string[] = [];
const promise1 = locks.withLock(testPath, async () => {
@ -127,54 +129,56 @@ describe("withLock", () => {
const [result1, result2] = await Promise.all([promise1, promise2]);
expect(result1).toBe("result1");
expect(result2).toBe("result2");
assert.strictEqual(result1, "result1");
assert.strictEqual(result2, "result2");
// Both operations should run concurrently
expect(executionOrder[0]).toBe("operation1-start");
expect(executionOrder[1]).toBe("operation2-start");
assert.strictEqual(executionOrder[0], "operation1-start");
assert.strictEqual(executionOrder[1], "operation2-start");
});
test("should release locks even if function throws", async () => {
it("should release locks even if function throws", async () => {
const error = new Error("test error");
await expect(
await assert.rejects(
locks.withLock(testPath, () => {
throw error;
})
).rejects.toThrow("test error");
}),
{ message: "test error" }
);
// Lock should be released, allowing another operation
const result = await locks.withLock(
testPath,
() => "success-after-error"
);
expect(result).toBe("success-after-error");
assert.strictEqual(result, "success-after-error");
});
test("should release locks even if async function throws", async () => {
it("should release locks even if async function throws", async () => {
const error = new Error("async test error");
await expect(
await assert.rejects(
locks.withLock(testPath, async () => {
await new Promise((resolve) => setTimeout(resolve, 10));
throw error;
})
).rejects.toThrow("async test error");
}),
{ message: "async test error" }
);
// Lock should be released, allowing another operation
const result = await locks.withLock(
testPath,
() => "success-after-async-error"
);
expect(result).toBe("success-after-async-error");
assert.strictEqual(result, "success-after-async-error");
});
test("should handle empty array of keys", async () => {
it("should handle empty array of keys", async () => {
const result = await locks.withLock([], () => "empty-keys");
expect(result).toBe("empty-keys");
assert.strictEqual(result, "empty-keys");
});
test("should maintain FIFO order for multiple waiters", async () => {
it("should maintain FIFO order for multiple waiters", async () => {
const executionOrder: string[] = [];
// Start first operation that holds the lock
@ -209,10 +213,10 @@ describe("withLock", () => {
thirdPromise
]);
expect(first).toBe("first");
expect(second).toBe("second");
expect(third).toBe("third");
expect(executionOrder).toEqual([
assert.strictEqual(first, "first");
assert.strictEqual(second, "second");
assert.strictEqual(third, "third");
assert.deepStrictEqual(executionOrder, [
"first-start",
"first-end",
"second-start",

View file

@ -1,60 +1,62 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { CoveredValues } from "./min-covered";
describe("CoveredValues", () => {
test("should initialize with the given min value", () => {
it("should initialize with the given min value", () => {
const covered = new CoveredValues(5);
expect(covered.min).toBe(5);
assert.strictEqual(covered.min, 5);
});
test("should add values greater than min", () => {
it("should add values greater than min", () => {
const covered = new CoveredValues(0);
covered.add(3);
expect(covered.min).toBe(0);
assert.strictEqual(covered.min, 0);
covered.add(1);
expect(covered.min).toBe(1);
assert.strictEqual(covered.min, 1);
covered.add(4);
expect(covered.min).toBe(1);
assert.strictEqual(covered.min, 1);
covered.add(2);
expect(covered.min).toBe(4);
assert.strictEqual(covered.min, 4);
});
test("should ignore duplicate values", () => {
it("should ignore duplicate values", () => {
const covered = new CoveredValues(0);
covered.add(3);
covered.add(3);
covered.add(3);
expect(covered.min).toBe(0);
assert.strictEqual(covered.min, 0);
covered.add(1);
covered.add(2);
expect(covered.min).toBe(3);
assert.strictEqual(covered.min, 3);
});
test("should handle multiple consecutive values", () => {
it("should handle multiple consecutive values", () => {
const covered = new CoveredValues(132);
for (let i = 250; i > 132; i--) {
expect(covered.min).toBe(132);
assert.strictEqual(covered.min, 132);
covered.add(i);
}
expect(covered.min).toBe(250);
assert.strictEqual(covered.min, 250);
});
test("should handle adding values lower than current min", () => {
it("should handle adding values lower than current min", () => {
const covered = new CoveredValues(5);
covered.add(3);
expect(covered.min).toBe(5);
assert.strictEqual(covered.min, 5);
covered.add(6);
expect(covered.min).toBe(6);
assert.strictEqual(covered.min, 6);
});
test("should handle force setting min value", () => {
it("should handle force setting min value", () => {
const covered = new CoveredValues(5);
covered.add(7);
covered.add(8);
covered.add(9);
expect(covered.min).toBe(5);
assert.strictEqual(covered.min, 5);
covered.min = 6;
expect(covered.min).toBe(6);
assert.strictEqual(covered.min, 6);
covered.add(10);
expect(covered.min).toBe(10);
assert.strictEqual(covered.min, 10);
});
});

View file

@ -0,0 +1,66 @@
import { describe, test } from "node:test";
import assert from "node:assert";
import { positionToLineAndColumn } from "./position-to-line-and-column";
describe("positionToLineAndColumn", () => {
test("converts position to line and column in multi-line text", () => {
const text = "ab\ncd\n";
assert.deepStrictEqual(positionToLineAndColumn(text, 0), {
line: 0,
column: 0
});
assert.deepStrictEqual(positionToLineAndColumn(text, 1), {
line: 0,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn(text, 2), {
line: 0,
column: 2
});
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 1,
column: 0
});
assert.deepStrictEqual(positionToLineAndColumn(text, 4), {
line: 1,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn(text, 6), {
line: 2,
column: 0
});
});
test("with carrige returns", () => {
assert.deepStrictEqual(positionToLineAndColumn("a\nb", 3), {
line: 1,
column: 1
});
assert.deepStrictEqual(positionToLineAndColumn("a\r\nb", 3), {
line: 1,
column: 1
});
});
test("handles empty input", () => {
assert.deepStrictEqual(positionToLineAndColumn("", 0), {
line: 0,
column: 0
});
});
test("handles positions at the end of text", () => {
const text = "End";
assert.deepStrictEqual(positionToLineAndColumn(text, 3), {
line: 0,
column: 3
});
});
test("throws error for position out of range", () => {
const text = "Short text";
assert.throws(() => positionToLineAndColumn(text, 15));
assert.throws(() => positionToLineAndColumn(text, -1));
});
});

View file

@ -1,66 +1,64 @@
import { rateLimit } from "./rate-limit";
import { jest } from "@jest/globals";
import { describe, it, beforeEach, afterEach, mock } from "node:test";
import assert from "node:assert";
describe("rateLimit", () => {
beforeEach(() => {
jest.useFakeTimers();
mock.timers.enable({ apis: ["setTimeout"] });
});
afterEach(() => {
jest.useRealTimers();
mock.timers.reset();
});
it("should call the function immediately on first invocation", async () => {
const mockFn = jest
.fn<() => Promise<string>>()
.mockResolvedValue("result");
const mockFn = mock.fn<() => Promise<string>>();
mockFn.mock.mockImplementation(async () => "result");
const rateLimited = rateLimit(mockFn, 100);
const promise = rateLimited();
expect(mockFn).toHaveBeenCalledTimes(1);
assert.strictEqual(mockFn.mock.callCount(), 1);
await promise;
});
it("should call the function again after the interval has passed", async () => {
const mockFn = jest
.fn<(value: number) => Promise<string>>()
.mockResolvedValue("result");
const mockFn = mock.fn<(value: number) => Promise<string>>();
mockFn.mock.mockImplementation(async () => "result");
const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited(1);
await promise1;
jest.advanceTimersByTime(200);
mock.timers.tick(200);
const promise2 = rateLimited(2);
await promise2;
expect(mockFn).toHaveBeenCalledTimes(2);
expect(mockFn).toHaveBeenCalledWith(2);
assert.strictEqual(mockFn.mock.callCount(), 2);
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, [2]);
});
it("should use the most recent arguments if multiple calls are made within interval", async () => {
const mockFn = jest
.fn<(value: string) => Promise<string>>()
.mockImplementation(async (val) => `${val}-result`);
const mockFn = mock.fn<(value: string) => Promise<string>>();
mockFn.mock.mockImplementation(async (val: string) => `${val}-result`);
const rateLimited = rateLimit(mockFn, 100);
const promise1 = rateLimited("first");
jest.advanceTimersByTime(10);
mock.timers.tick(10);
const promise2 = rateLimited("second");
jest.advanceTimersByTime(10);
mock.timers.tick(10);
const promise3 = rateLimited("third");
jest.advanceTimersByTime(1000);
mock.timers.tick(1000);
expect(await promise1).toEqual("first-result");
expect(await promise2).toEqual("third-result");
expect(await promise3).toBeUndefined();
assert.strictEqual(await promise1, "first-result");
assert.strictEqual(await promise2, "third-result");
assert.strictEqual(await promise3, undefined);
expect(mockFn).toHaveBeenCalledTimes(2);
expect(mockFn).toHaveBeenNthCalledWith(1, "first");
expect(mockFn).toHaveBeenNthCalledWith(2, "third");
assert.strictEqual(mockFn.mock.callCount(), 2);
assert.deepStrictEqual(mockFn.mock.calls[0].arguments, ["first"]);
assert.deepStrictEqual(mockFn.mock.calls[1].arguments, ["third"]);
});
});

View file

@ -6,7 +6,8 @@
"allowSyntheticDefaultImports": true,
"moduleResolution": "bundler",
"lib": [
"DOM" // to get `fetch` & `WebSocket`
"DOM", // to get `fetch` & `WebSocket`
"ES2024"
],
"declaration": true,
"declarationDir": "./dist/types"

View file

@ -1,3 +0,0 @@
module.exports = {
preset: "ts-jest"
};

View file

@ -1,6 +1,6 @@
{
"name": "test-client",
"version": "0.6.1",
"version": "0.6.3",
"private": true,
"bin": {
"test-client": "./dist/cli.js"
@ -8,17 +8,18 @@
"scripts": {
"dev": "webpack watch --mode development",
"build": "webpack --mode production",
"test": "jest"
"test": "tsx --test src/**/*.test.ts"
},
"devDependencies": {
"@types/node": "^22.15.30",
"bufferutil": "^4.0.9",
"sync-client": "file:../sync-client",
"ts-loader": "^9.5.2",
"tslib": "2.8.1",
"tsx": "^4.20.5",
"typescript": "5.8.3",
"uuid": "^11.1.0",
"webpack": "^5.99.9",
"webpack-cli": "^6.0.1",
"bufferutil": "^4.0.9"
"webpack-cli": "^6.0.1"
}
}

View file

@ -2,12 +2,10 @@ import { choose } from "../utils/choose";
import { v4 as uuidv4 } from "uuid";
import { assert } from "../utils/assert";
import type { RelativePath, SyncSettings } from "sync-client";
import { Logger, LogLevel } from "sync-client";
import { debugging, Logger, LogLevel } from "sync-client";
import { MockClient } from "./mock-client";
import { sleep } from "../utils/sleep";
import type { LogLine } from "sync-client/dist/types/tracing/logger";
import { flakyFetchFactory } from "../utils/flaky-fetch";
import { flakyWebSocketFactory } from "../utils/flaky-websocket-factory";
export class MockAgent extends MockClient {
private readonly writtenContents: string[] = [];
@ -28,8 +26,8 @@ export class MockAgent extends MockClient {
public async init(): Promise<void> {
await super.init(
flakyFetchFactory(this.jitterScaleInSeconds),
flakyWebSocketFactory(
debugging.slowFetchFactory(this.jitterScaleInSeconds),
debugging.slowWebSocketFactory(
this.jitterScaleInSeconds,
new Logger() // this logger isn't wired anywhere, so messages to it will be ignored
)

View file

@ -1,20 +0,0 @@
import { sleep } from "./sleep";
export const flakyFetchFactory =
(jitterScaleInSeconds: number) =>
async (
input: string | URL | globalThis.Request,
init?: RequestInit
): Promise<Response> => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
const response = await fetch(input, init);
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
return response;
};

View file

@ -1,82 +0,0 @@
import type { Logger } from "sync-client";
import { helpers } from "sync-client";
import { sleep } from "./sleep";
export function flakyWebSocketFactory(
jitterScaleInSeconds: number,
logger: Logger
): typeof WebSocket {
// eslint-disable-next-line
return class FlakyWebSocket extends WebSocket {
private static readonly RECEIVE_KEY = "websocket-receive";
private static readonly SEND_KEY = "websocket-send";
private readonly locks = new helpers.Locks(logger);
public set onopen(callback: (event: Event) => void) {
super.onopen = async (event: Event): Promise<void> => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
callback(event);
};
}
public set onmessage(callback: (event: MessageEvent) => void) {
super.onmessage = async (event: MessageEvent): Promise<void> => {
return this.locks.withLock(
FlakyWebSocket.RECEIVE_KEY,
async () => {
if (jitterScaleInSeconds > 0) {
await sleep(
Math.random() * jitterScaleInSeconds * 1000
);
}
callback(event);
}
);
};
}
public set onclose(callback: (event: CloseEvent) => void) {
super.onclose = async (event: CloseEvent): Promise<void> => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
callback(event);
};
}
public set onerror(callback: (event: Event) => void) {
super.onerror = async (event: Event): Promise<void> => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
callback(event);
};
}
public send(
data: string | ArrayBufferLike | Blob | ArrayBufferView
): void {
this.waitingSend(data).catch((error: unknown) => {
logger.error(`Error sending WebSocket message: ${error}`);
});
}
private async waitingSend(
data: string | ArrayBufferLike | Blob | ArrayBufferView
): Promise<void> {
// maintain message order
return this.locks.withLock(FlakyWebSocket.SEND_KEY, async () => {
if (jitterScaleInSeconds > 0) {
await sleep(Math.random() * jitterScaleInSeconds * 1000);
}
super.send(data);
});
}
} as unknown as typeof WebSocket;
}

View file

@ -1,3 +1,5 @@
import { describe, it } from "node:test";
import assert from "node:assert";
import { randomCasing } from "./random-casing";
describe("randomCasing", () => {
@ -5,7 +7,7 @@ describe("randomCasing", () => {
const input =
"hello, this is a really long string with a lot of characters";
const result = randomCasing(input);
expect(result.toLowerCase()).toBe(input.toLowerCase());
expect(result).not.toBe(input);
assert.strictEqual(result.toLowerCase(), input.toLowerCase());
assert.notStrictEqual(result, input);
});
});

View file

@ -7,7 +7,7 @@
"esModuleInterop": true,
"lib": [
"DOM",
"ESNext"
"ES2024",
],
"moduleResolution": "node"
},

View file

@ -1,7 +1,7 @@
{
"id": "vault-link",
"name": "VaultLink",
"version": "0.6.1",
"version": "0.6.3",
"minAppVersion": "0.0.0",
"description": "Self-hosted synchronization and collaboration for your Vault.",
"author": "Andras Schmelczer",

View file

@ -4,17 +4,17 @@ set -e
echo "Running checks in sync-server"
cd sync-server
cargo test --verbose
cargo clippy --all-targets --all-features
cargo fmt --all -- --check
cargo machete
cargo test --verbose
echo "Running checks in frontend"
cd ../frontend
npm ci
npm run build
npm run lint
npm run test
npm run lint
if [[ $(git status --porcelain) ]]; then
git status --porcelain

View file

@ -2205,7 +2205,7 @@ dependencies = [
[[package]]
name = "sync_server"
version = "0.6.1"
version = "0.6.3"
dependencies = [
"anyhow",
"axum",

View file

@ -5,7 +5,7 @@ authors = ["Andras Schmelczer <andras@schmelczer.dev>"]
edition = "2024"
license = "MIT"
repository = "https://github.com/schmelczer/vault-link"
version = "0.6.1"
version = "0.6.3"
[dependencies]
serde = { version = "1.0.219", default-features = false, features = ["derive"] }

View file

@ -30,7 +30,7 @@ RUN . /tmp/rust_env && \
cargo build --release --target $RUST_TARGET
# Runtime image
FROM alpine:3.22.0
FROM alpine:3.22.1
LABEL org.opencontainers.image.authors="andras@schmelczer.dev"