Skip to content

Commit 074ccdf

Browse files
Initial release: browser git sync via Nostr
- NostrGitClient: core sync with isomorphic-git + IndexedDB - StateManager: JSON state tracking with Bitcoin anchoring - SelfDeploy: auto-reloading pages on repo updates
0 parents  commit 074ccdf

2,512 files changed

Lines changed: 350055 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

README.md

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
# nostr-git-client
2+
3+
Browser-based git sync via Nostr with optional Bitcoin anchoring.
4+
5+
Subscribe to NIP-34 repo state events (kind 30618) and sync git repositories to browser IndexedDB using isomorphic-git.
6+
7+
## Installation
8+
9+
```bash
10+
npm install nostr-git-client
11+
```
12+
13+
## Usage
14+
15+
### Basic Git Sync
16+
17+
```javascript
18+
import { NostrGitClient } from 'nostr-git-client';
19+
20+
const client = new NostrGitClient({
21+
repo: 'https://solid.social/mel/public/myapp',
22+
repoId: 'myapp',
23+
branch: 'main',
24+
relays: ['wss://relay.damus.io', 'wss://nos.lol'],
25+
trusted: ['pubkey-hex...'] // Only sync from trusted publishers
26+
});
27+
28+
client.on('sync', (event) => {
29+
if (event.status === 'complete') {
30+
console.log('Synced to:', event.commit);
31+
}
32+
});
33+
34+
client.connect();
35+
await client.sync();
36+
37+
// Read files from synced repo
38+
const content = await client.readFile('index.html');
39+
const files = await client.listFiles('src');
40+
```
41+
42+
### State Management with Bitcoin Anchoring
43+
44+
```javascript
45+
import { NostrGitClient, StateManager } from 'nostr-git-client';
46+
47+
const client = new NostrGitClient({
48+
repo: 'https://solid.social/mel/public/game',
49+
trusted: ['pubkey...']
50+
});
51+
52+
const state = new StateManager({
53+
sync: client,
54+
file: 'state.json',
55+
anchor: {
56+
privkey: 'your-bitcoin-privkey-hex',
57+
network: 'testnet4'
58+
},
59+
onChange: (newState) => {
60+
console.log('State updated:', newState);
61+
}
62+
});
63+
64+
await state.init();
65+
client.connect();
66+
await client.sync();
67+
68+
// Anchor current state to Bitcoin
69+
const anchor = await state.anchor();
70+
console.log('Anchored:', anchor.txid);
71+
```
72+
73+
### Self-Deploying Pages
74+
75+
```javascript
76+
import { SelfDeploy } from 'nostr-git-client';
77+
78+
// Auto-reload when repo is updated
79+
SelfDeploy.init({
80+
repo: 'https://solid.social/mel/public/myapp',
81+
trusted: ['pubkey...'],
82+
autoReload: true,
83+
onUpdate: (event) => {
84+
console.log('Update detected:', event.commit);
85+
}
86+
});
87+
```
88+
89+
## API
90+
91+
### NostrGitClient
92+
93+
Main client for Nostr-based git sync.
94+
95+
**Constructor options:**
96+
- `repo` - Git repository URL
97+
- `repoId` - Repository identifier (default: derived from repo URL)
98+
- `branch` - Branch to track (default: 'main')
99+
- `relays` - Nostr relay URLs
100+
- `trusted` - Trusted publisher pubkeys
101+
- `corsProxy` - CORS proxy URL (optional)
102+
103+
**Methods:**
104+
- `connect()` - Connect to relays and start listening
105+
- `disconnect()` - Disconnect from relays
106+
- `sync(commit?)` - Sync to specified commit or latest
107+
- `readFile(path)` - Read file content as string
108+
- `readFileBytes(path)` - Read file as Uint8Array
109+
- `listFiles(path?)` - List directory contents
110+
- `getAllFiles()` - Get all files recursively
111+
- `getCommit()` - Get current commit info
112+
- `clear()` - Delete local repository
113+
114+
**Events:**
115+
- `sync` - Sync status changes
116+
- `event` - Nostr events received
117+
- `connect` - Connected to relay
118+
- `disconnect` - Disconnected from relay
119+
- `error` - Errors
120+
121+
### StateManager
122+
123+
Manages JSON state with optional Bitcoin anchoring.
124+
125+
**Constructor options:**
126+
- `sync` - NostrGitClient instance
127+
- `file` - Path to JSON file (default: 'state.json')
128+
- `anchor` - Bitcoin config: `{ privkey, network }`
129+
- `onChange` - Callback on state changes
130+
131+
**Methods:**
132+
- `init()` - Initialize (loads blocktrails if anchoring)
133+
- `loadState()` - Load state from file
134+
- `get()` - Get current state
135+
- `anchor()` - Anchor state to Bitcoin
136+
- `getAnchors()` - Get anchor history
137+
- `verifyChain()` - Verify anchor chain integrity
138+
139+
### SelfDeploy
140+
141+
Auto-updating pages.
142+
143+
**Static methods:**
144+
- `init(options)` - Initialize self-deploy
145+
- `loaderScript(options)` - Generate embed script
146+
- `serviceWorkerCode()` - Generate SW code
147+
148+
**Options:**
149+
- `repo` - Repository URL (required)
150+
- `trusted` - Trusted pubkeys (required)
151+
- `autoReload` - Auto reload on update (default: true)
152+
- `reloadDelay` - Delay before reload in ms (default: 1000)
153+
- `onUpdate` - Callback on update
154+
- `onSync` - Callback after sync
155+
156+
## Dependencies
157+
158+
- [isomorphic-git](https://isomorphic-git.org/) - Git in JavaScript
159+
- [@isomorphic-git/lightning-fs](https://github.com/isomorphic-git/lightning-fs) - IndexedDB filesystem
160+
- [nostr-tools](https://github.com/nbd-wtf/nostr-tools) - Nostr protocol
161+
162+
**Optional:**
163+
- [blocktrails](https://www.npmjs.com/package/blocktrails) - Bitcoin state anchoring
164+
165+
## License
166+
167+
MIT

node_modules/.bin/blocktrails

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/crc32

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/isogit

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/sha.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/superblocktxt

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)