@xnetjs/sync 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Chris Smothers
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # @xnetjs/sync
2
+
3
+ Unified sync primitives for xNet -- Change\<T\>, Lamport clocks, hash chains, and a comprehensive Yjs security layer.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm add @xnetjs/sync
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Change\<T\>** -- Universal immutable sync unit for structured data
14
+ - **Lamport clocks** -- Logical timestamps for causal ordering (tick, receive, compare)
15
+ - **Hash chains** -- Tamper-evident linked update logs with fork detection
16
+ - **SyncProvider interface** -- Pluggable sync transport abstraction
17
+ - **Yjs security layer**:
18
+ - Signed envelopes (Ed25519) for Yjs updates
19
+ - Rate and size limits per peer
20
+ - Hash-at-rest integrity verification
21
+ - Peer scoring (reputation tracking)
22
+ - ClientID-DID binding (attestation)
23
+ - Yjs hash chain integration
24
+ - Update batching for efficiency
25
+
26
+ ## Usage
27
+
28
+ ```typescript
29
+ import {
30
+ createUnsignedChange,
31
+ signChange,
32
+ createLamportClock,
33
+ tick,
34
+ receive,
35
+ compareLamportTimestamps
36
+ } from '@xnetjs/sync'
37
+
38
+ let clock = createLamportClock(did)
39
+ const [nextClock, lamport] = tick(clock)
40
+ clock = nextClock
41
+
42
+ const unsigned = createUnsignedChange({
43
+ id: crypto.randomUUID(),
44
+ type: 'task/update',
45
+ payload: { title: 'Updated title' },
46
+ parentHash: null,
47
+ authorDID: did,
48
+ lamport
49
+ })
50
+
51
+ const change = signChange(unsigned, signingKey)
52
+
53
+ // Merge remote Lamport time into local clock
54
+ clock = receive(clock, change.lamport.time)
55
+
56
+ // Deterministic ordering
57
+ const [, later] = tick(clock)
58
+ const cmp = compareLamportTimestamps(change.lamport, later)
59
+ ```
60
+
61
+ ```typescript
62
+ import { validateChain, detectFork, topologicalSort } from '@xnetjs/sync'
63
+
64
+ // Hash chain verification
65
+ const valid = validateChain(changes)
66
+ const fork = detectFork(chain1, chain2)
67
+ const sorted = topologicalSort(changes)
68
+ ```
69
+
70
+ ```typescript
71
+ import { signYjsUpdate, verifyYjsEnvelope } from '@xnetjs/sync'
72
+
73
+ // Signed Yjs envelopes
74
+ const envelope = signYjsUpdate(update, did, signingKey, clientId)
75
+ const { valid, update } = verifyYjsEnvelope(envelope)
76
+ ```
77
+
78
+ ## Architecture
79
+
80
+ ```mermaid
81
+ flowchart TD
82
+ subgraph Structured["Structured Data Sync"]
83
+ Change["Change&lt;T&gt;<br/><small>Immutable sync unit</small>"]
84
+ Clock["Lamport utilities<br/><small>tick, receive, compare</small>"]
85
+ Chain["Hash Chain<br/><small>Tamper-evident log</small>"]
86
+ end
87
+
88
+ subgraph Yjs["Yjs Security Layer"]
89
+ Envelope["Signed Envelopes<br/><small>Ed25519 signatures</small>"]
90
+ Limits["Rate/Size Limits<br/><small>Per-peer throttling</small>"]
91
+ Integrity["Hash-at-Rest<br/><small>Integrity checks</small>"]
92
+ Scoring["Peer Scoring<br/><small>Reputation tracking</small>"]
93
+ Attestation["ClientID-DID<br/><small>Binding attestation</small>"]
94
+ Batcher["Update Batcher<br/><small>Efficiency</small>"]
95
+ end
96
+
97
+ subgraph Transport["Transport"]
98
+ Provider["SyncProvider<br/><small>Pluggable interface</small>"]
99
+ end
100
+
101
+ Change --> Clock
102
+ Change --> Chain
103
+ Envelope --> Limits
104
+ Envelope --> Integrity
105
+ Limits --> Scoring
106
+ Attestation --> Envelope
107
+ Batcher --> Envelope
108
+ Structured --> Provider
109
+ Yjs --> Provider
110
+ ```
111
+
112
+ ## Modules
113
+
114
+ | Module | Description |
115
+ | ------------------------- | ------------------------------------------------------- |
116
+ | `change.ts` | Change\<T\> creation and types |
117
+ | `clock.ts` | Lamport clock implementation |
118
+ | `chain.ts` | Hash chain validation, fork detection, topological sort |
119
+ | `provider.ts` | SyncProvider interface |
120
+ | `yjs-envelope.ts` | Ed25519-signed Yjs update envelopes |
121
+ | `yjs-limits.ts` | Rate and size limits for Yjs updates |
122
+ | `yjs-integrity.ts` | Hash-at-rest integrity verification |
123
+ | `yjs-peer-scoring.ts` | Peer reputation scoring |
124
+ | `clientid-attestation.ts` | ClientID-DID binding |
125
+ | `yjs-change.ts` | Yjs hash chain integration |
126
+ | `yjs-batcher.ts` | Update batching for efficiency |
127
+
128
+ ## Dependencies
129
+
130
+ - `@xnetjs/core` -- Core types
131
+ - `@xnetjs/crypto` -- Signing, hashing
132
+ - `@xnetjs/identity` -- DID operations
133
+
134
+ ## Testing
135
+
136
+ ```bash
137
+ pnpm --filter @xnetjs/sync test
138
+ ```
139
+
140
+ 10 test files covering all modules.