How 0xdevansh/lapis works
Purpose and entry points
Lapis is a self-hosted, Cloudflare-native Obsidian-vault system: a browser workspace, an Obsidian plugin, and a REST/sync backend. Main execution boundaries are:
• Worker API: worker/src/index.ts
• React SPA bootstrap and application shell: web/src/main.tsx, web/src/App.tsx
• Obsidian plugin bootstrap: plugin/src/main.ts
Cloudflare backend and persistence
The Worker is the system of record and API gateway. It uses Cloudflare D1/SQLite for identities, vault metadata, search, device and remote records; R2 for vault file content; KV for session-oriented state; Durable Objects for serialized vault coordination; and Cloudflare Artifacts for sealed Git history. Schema evolution is in worker/migrations/; deployment bindings are declared in worker/wrangler.jsonc.
• worker/src/index.ts
• worker/src/db/schema.sql
• worker/src/vault/coordinator.ts
Vault file lifecycle
Vault routes implement authenticated file, manifest, export, membership, and lifecycle operations. File writes are normalized by vault path/identity rules, patched or stored as text/content, recorded in the manifest, then made available to clients. ZIP export is generated from vault state. Conflict handling preserves colliding edits as explicit conflict notes rather than overwriting.
• worker/src/vault/routes.ts
• worker/src/vault/manifest.ts
• worker/src/vault/conflict.ts
Synchronization and concurrency
The plugin and web clients use device-scoped identity and authenticated APIs. The plugin detects local changes, computes diffs/hashes, persists an offline journal, and replays/reconciles it on reconnect. Server-side sync routes and the per-vault coordinator form the concurrency boundary for mutations, notifications, and conflict resolution.
• plugin/src/sync/engine.ts
• plugin/src/sync/journal.ts
• worker/src/sync/routes.ts
Authentication, devices, and access control
User sessions are handled by the auth subsystem and Worker middleware. Obsidian devices connect through device-code authentication, then use device Bearer credentials for sync. Vault access, invitations, membership, and device ownership are enforced server-side before content or synchronization operations.
• worker/src/auth/index.ts
• worker/src/middleware/auth.ts
• worker/src/device/routes.ts
Browser workspace
The SPA provides authentication, vault selection, workspace editing, device administration, and MCP consent. Its central workspace state invokes the Worker API and responds to vault notifications. Markdown is rendered client-side with Obsidian-oriented constructs, while editing uses a dedicated Markdown editor.
• web/src/pages/VaultWorkspace.tsx
• web/src/store/workspace.tsx
• web/src/api.ts
Derived services and external remotes
After vault mutations, search indexing supports SQLite FTS-backed search, backlinks, and tags. Notification routes distribute live vault changes/presence to connected clients. Git remote support reconciles vault state with GitHub using configured remote/device credentials; Artifacts-based storage and LFS-pointer sealing support managed historical content. MCP routes expose controlled programmatic access for agents/integrations.
• worker/src/search/indexer.ts
• worker/src/notify/routes.ts
• worker/src/git/routes.ts
Components 21
Client runtimes
- React SPA shell
web/src/main.tsx - Vault workspace state · Edits vaults and responds to vault notifications.
web/src/store/workspace.tsx - Obsidian plugin
plugin/src/main.ts - Offline sync engine · Diffs local changes and reconciles its journal after reconnect.
plugin/src/sync/engine.ts - Offline journal
plugin/src/sync/journal.ts
Cloudflare Worker
- Worker API gateway · System of record and authenticated API entry point.
worker/src/index.ts - Auth & access middleware · Enforces sessions, device credentials, ownership, and vault access.
worker/src/middleware/auth.ts - Device authentication · Issues device-scoped credentials through device-code authentication.
worker/src/device/routes.ts - Vault lifecycle API · Serves files, manifests, exports, membership, and lifecycle operations.
worker/src/vault/routes.ts - Vault manifest · Records normalized vault file state.
worker/src/vault/manifest.ts - Server sync API
worker/src/sync/routes.ts - Per-vault coordinator · Serializes mutations, notifications, and conflict resolution.
worker/src/vault/coordinator.ts - Conflict preservation · Stores colliding edits as explicit conflict notes.
worker/src/vault/conflict.ts
Cloudflare persistence
- D1 / SQLite · Stores identities, vault metadata, search, devices, and remotes.
worker/src/db/schema.sql - R2 vault content
- KV session state
- Artifacts history · Stores managed historical content and LFS-pointer seals.
Derived & external services
- Search indexer · Builds FTS search, backlinks, and tags after mutations.
worker/src/search/indexer.ts - Vault notifications · Distributes live vault changes and presence.
worker/src/notify/routes.ts - Git remote reconciliation · Reconciles vault state with GitHub remotes.
worker/src/git/routes.ts - MCP access · Provides controlled agent and integration access.
Connections 27
- React SPA shell → Vault workspace state: mounts
- Vault workspace state → Worker API gateway: authenticated API
- Obsidian plugin → Offline sync engine: runs
- Offline sync engine → Offline journal: persists offline changes
- Offline sync engine → Worker API gateway: sync API
- Worker API gateway → Auth & access middleware: authenticates requests
- Auth & access middleware → D1 / SQLite: identities and access
- Auth & access middleware → KV session state: session state
- Worker API gateway → Device authentication: device-code flow
- Device authentication → D1 / SQLite: device ownership
- Worker API gateway → Vault lifecycle API: vault operations
- Worker API gateway → Server sync API: sync operations
- Vault lifecycle API → Per-vault coordinator: serialized mutations
- Server sync API → Per-vault coordinator: serialized sync
- Vault lifecycle API → Vault manifest: updates
- Vault manifest → D1 / SQLite: file metadata
- Vault lifecycle API → R2 vault content: file content
- Per-vault coordinator → Conflict preservation: colliding edits
- Per-vault coordinator → Search indexer: mutation events
- Search indexer → D1 / SQLite: FTS and graph data
- Per-vault coordinator → Vault notifications: vault events
- Vault notifications → Vault workspace state: live changes
- Vault notifications → Offline sync engine: sync signals
- Per-vault coordinator → Git remote reconciliation: reconcile state
- Git remote reconciliation → Artifacts history: sealed history
- Worker API gateway → MCP access: controlled programmatic access
- MCP access → Vault lifecycle API: vault actions