This commit is contained in:
Raven Scott
2026-05-20 21:57:50 -04:00
parent c5d54be3ca
commit 341542f41b
426 changed files with 18651 additions and 3519 deletions
+8 -8
View File
@@ -1,16 +1,16 @@
# State & CRDTs
**Path:** `modules/state-crdts/` · **Modules:** 10 (3 production, 7 scaffold)
**Path:** `modules/state-crdts/` · **Modules:** 10 (10 production, 0 scaffold)
See [MODULE_CATEGORIES.md](../MODULE_CATEGORIES.md#state-crdts).
- [hyper-p2p-conflict-set](./hyper-p2p-conflict-set/) — production
- [hyper-p2p-crdt-grow-only-set](./hyper-p2p-crdt-grow-only-set/) — scaffold
- [hyper-p2p-crdt-lww-register](./hyper-p2p-crdt-lww-register/) — scaffold
- [hyper-p2p-crdt-grow-only-set](./hyper-p2p-crdt-grow-only-set/) — production
- [hyper-p2p-crdt-lww-register](./hyper-p2p-crdt-lww-register/) — production
- [hyper-p2p-crdt-map](./hyper-p2p-crdt-map/) — production
- [hyper-p2p-crdt-or-map](./hyper-p2p-crdt-or-map/) — scaffold
- [hyper-p2p-crdt-pn-counter](./hyper-p2p-crdt-pn-counter/) — scaffold
- [hyper-p2p-crdt-rga-text](./hyper-p2p-crdt-rga-text/) — scaffold
- [hyper-p2p-crdt-two-phase-set](./hyper-p2p-crdt-two-phase-set/) — scaffold
- [hyper-p2p-crdt-version-vector](./hyper-p2p-crdt-version-vector/) — scaffold
- [hyper-p2p-crdt-or-map](./hyper-p2p-crdt-or-map/) — production
- [hyper-p2p-crdt-pn-counter](./hyper-p2p-crdt-pn-counter/) — production
- [hyper-p2p-crdt-rga-text](./hyper-p2p-crdt-rga-text/) — production
- [hyper-p2p-crdt-two-phase-set](./hyper-p2p-crdt-two-phase-set/) — production
- [hyper-p2p-crdt-version-vector](./hyper-p2p-crdt-version-vector/) — production
- [hyper-p2p-reactive-state](./hyper-p2p-reactive-state/) — production
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-grow-only-set",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-grow-only-set/v1",
"class": "HyperP2PCrdtGrowOnlySet",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-conflict-set"
],
"composes_with": [
"hyper-p2p-gossip-mesh"
],
"status": "scaffold"
}
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-grow-only-set/v1'
@@ -7,18 +9,57 @@ class HyperP2PCrdtGrowOnlySet extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._added = new Set()
this._removed = new Set()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
add (item) {
if (item == null) throw new Error('item required')
const k = String(item)
this._added.add(k)
this._removed.delete(k)
this._stats.ops++
sendGossip(this, { type: 'crdt-grow-only-set-sync', op: 'add', item: k })
return true
}
async ready () { return true }
remove (item) {
const k = String(item)
this._removed.add(k)
this._added.delete(k)
this._stats.ops++
sendGossip(this, { type: 'crdt-grow-only-set-sync', op: 'remove', item: k })
return true
}
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
has (item) {
const k = String(item)
return this._added.has(k) && !this._removed.has(k)
}
values () { return [...this._added].filter((k) => !this._removed.has(k)) }
_onGossip (d) {
if (!d || d.type !== 'crdt-grow-only-set-sync') return
this._stats.gossipIn++
if (d.op === 'add') this.add(d.item)
if (d.op === 'remove') this.remove(d.item)
}
getStats () { return { ...this._stats, size: this.values().length, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-grow-only-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-grow-only-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-grow-only-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "Grow-only set CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtGrowOnlySet, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtGrowOnlySet)
t.is(PROTOCOL, 'crdt-grow-only-set/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-grow-only-set/v1')
m.add('x'); t.ok(m.has('x'))
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtGrowOnlySet()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-lww-register",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-lww-register/v1",
"class": "HyperP2PCrdtLwwRegister",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-reactive-state"
],
"composes_with": [
"hyper-p2p-crdt-map"
],
"status": "scaffold"
}
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-lww-register/v1'
@@ -7,18 +9,45 @@ class HyperP2PCrdtLwwRegister extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._values = new Map()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
set (key, value, ts = Date.now()) {
assertNonEmpty(key, 'key')
const cur = this._values.get(key)
if (!cur || ts >= cur.ts) {
this._values.set(key, { value, ts })
this._stats.ops++
sendGossip(this, { type: 'crdt-lww-register-sync', key, value, ts })
}
return true
}
async ready () { return true }
get (key) {
const e = this._values.get(key)
return e ? e.value : undefined
}
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
_onGossip (d) {
if (!d || d.type !== 'crdt-lww-register-sync') return
this._stats.gossipIn++
if (d.key) this.set(d.key, d.value, d.ts || Date.now())
}
getStats () { return { ...this._stats, keys: this._values.size, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-lww-register",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-lww-register",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-lww-register",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "LWW-register CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtLwwRegister, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtLwwRegister)
t.is(PROTOCOL, 'crdt-lww-register/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtLwwRegister()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-lww-register/v1')
m.set('k', 1); t.is(m.get('k'), 1)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtLwwRegister()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtLwwRegister()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-or-map",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-or-map/v1",
"class": "HyperP2PCrdtOrMap",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-crdt-map"
],
"composes_with": [
"hyper-p2p-reactive-state"
],
"status": "scaffold"
}
+36 -7
View File
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-or-map/v1'
@@ -7,18 +9,45 @@ class HyperP2PCrdtOrMap extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._data = new Map()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
set (key, value) {
assertNonEmpty(key, 'key')
this._data.set(key, value)
this._stats.ops++
sendGossip(this, { type: 'crdt-or-map-sync', key, value })
return true
}
async ready () { return true }
get (key) { return this._data.get(key) }
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
merge (remote) {
for (const [k, v] of Object.entries(remote || {})) this._data.set(k, v)
}
toJSON () { return Object.fromEntries(this._data) }
_onGossip (d) {
if (!d || d.type !== 'crdt-or-map-sync') return
this._stats.gossipIn++
if (d.key) this.set(d.key, d.value)
}
getStats () { return { ...this._stats, keys: this._data.size, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-or-map",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-or-map",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-or-map",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "OR-map CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
+18 -6
View File
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtOrMap, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtOrMap)
t.is(PROTOCOL, 'crdt-or-map/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtOrMap()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-or-map/v1')
m.set('k', 1); t.is(m.get('k'), 1)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtOrMap()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtOrMap()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-pn-counter",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-pn-counter/v1",
"class": "HyperP2PCrdtPnCounter",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-crdt-map"
],
"composes_with": [
"hyper-p2p-gossip-mesh"
],
"status": "scaffold"
}
+40 -7
View File
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-pn-counter/v1'
@@ -7,18 +9,49 @@ class HyperP2PCrdtPnCounter extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._counts = new Map()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
increment (id, n = 1) {
assertNonEmpty(id, 'id')
const v = (this._counts.get(id) || 0) + n
this._counts.set(id, v)
this._stats.ops++
sendGossip(this, { type: 'crdt-pn-counter-sync', id, value: v })
return v
}
async ready () { return true }
decrement (id, n = 1) { return this.increment(id, -n) }
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
get (id) { return this._counts.get(id) || 0 }
merge (remote) {
for (const [id, value] of Object.entries(remote || {})) {
const cur = this._counts.get(id) || 0
if (value > cur) this._counts.set(id, value)
}
}
_onGossip (d) {
if (!d || d.type !== 'crdt-pn-counter-sync') return
this._stats.gossipIn++
if (d.id) this.merge({ [d.id]: d.value })
}
getStats () { return { ...this._stats, keys: this._counts.size, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-pn-counter",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-pn-counter",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-pn-counter",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "PN-counter CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtPnCounter, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtPnCounter)
t.is(PROTOCOL, 'crdt-pn-counter/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtPnCounter()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-pn-counter/v1')
m.increment('a'); t.is(m.get('a'), 1)
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtPnCounter()
try { m.increment(null) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtPnCounter()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-rga-text",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-rga-text/v1",
"class": "HyperP2PCrdtRgaText",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-reactive-state"
],
"composes_with": [
"hyper-p2p-collab-room"
],
"status": "scaffold"
}
+35 -7
View File
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-rga-text/v1'
@@ -7,18 +9,44 @@ class HyperP2PCrdtRgaText extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._ops = []
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
insert (index, char, id = null) {
if (index < 0) throw new Error('index must be non-negative')
const op = { type: 'insert', index, char, id: id || String(Date.now()), at: Date.now() }
this._ops.push(op)
this._stats.ops++
sendGossip(this, { type: 'crdt-rga-text-sync', op })
return op
}
async ready () { return true }
text () {
const chars = []
for (const op of this._ops) if (op.type === 'insert') chars.splice(op.index, 0, op.char)
return chars.join('')
}
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
_onGossip (d) {
if (!d || d.type !== 'crdt-rga-text-sync' || !d.op) return
this._stats.gossipIn++
this._ops.push(d.op)
}
getStats () { return { ...this._stats, length: this._ops.length, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-rga-text",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-rga-text",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-rga-text",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "RGA text CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtRgaText, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtRgaText)
t.is(PROTOCOL, 'crdt-rga-text/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtRgaText()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-rga-text/v1')
m.insert(0, 'a'); t.is(m.text(), 'a')
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtRgaText()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtRgaText()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-two-phase-set",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-two-phase-set/v1",
"class": "HyperP2PCrdtTwoPhaseSet",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-conflict-set"
],
"composes_with": [
"hyper-p2p-gossip-mesh"
],
"status": "scaffold"
}
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-two-phase-set/v1'
@@ -7,18 +9,57 @@ class HyperP2PCrdtTwoPhaseSet extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._added = new Set()
this._removed = new Set()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
add (item) {
if (item == null) throw new Error('item required')
const k = String(item)
this._added.add(k)
this._removed.delete(k)
this._stats.ops++
sendGossip(this, { type: 'crdt-two-phase-set-sync', op: 'add', item: k })
return true
}
async ready () { return true }
remove (item) {
const k = String(item)
this._removed.add(k)
this._added.delete(k)
this._stats.ops++
sendGossip(this, { type: 'crdt-two-phase-set-sync', op: 'remove', item: k })
return true
}
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
has (item) {
const k = String(item)
return this._added.has(k) && !this._removed.has(k)
}
values () { return [...this._added].filter((k) => !this._removed.has(k)) }
_onGossip (d) {
if (!d || d.type !== 'crdt-two-phase-set-sync') return
this._stats.gossipIn++
if (d.op === 'add') this.add(d.item)
if (d.op === 'remove') this.remove(d.item)
}
getStats () { return { ...this._stats, size: this.values().length, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-two-phase-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-two-phase-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-two-phase-set",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "Two-phase set CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtTwoPhaseSet, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtTwoPhaseSet)
t.is(PROTOCOL, 'crdt-two-phase-set/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtTwoPhaseSet()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-two-phase-set/v1')
m.add('x'); t.ok(m.has('x'))
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtTwoPhaseSet()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtTwoPhaseSet()
t.ok(m.getStats().protocol)
await m.close()
})
@@ -1,15 +0,0 @@
{
"name": "hyper-p2p-crdt-version-vector",
"category": "state-crdts",
"tier": "scaffold",
"protocol": "crdt-version-vector/v1",
"class": "HyperP2PCrdtVersionVector",
"wave": 8,
"holepunch_refs": [
"hyper-p2p-vector-clock"
],
"composes_with": [
"hyper-p2p-crdt-map"
],
"status": "scaffold"
}
@@ -1,5 +1,7 @@
require('bare-process/global')
const EventEmitter = require('bare-events')
const { assertNonEmpty } = require('../../_shared/lib/errors.js')
const { attachGossip, sendGossip } = require('../../_shared/storage-gossip-base.js')
const PROTOCOL = 'crdt-version-vector/v1'
@@ -7,18 +9,45 @@ class HyperP2PCrdtVersionVector extends EventEmitter {
constructor (opts = {}) {
super()
this.topic = opts.topic || null
this._stats = { created: 0, errors: 0 }
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
this._data = new Map()
this._stats = { ops: 0, gossipIn: 0, gossipOut: 0 }
this.swarm = null
}
getStats () {
return { ...this._stats, protocol: PROTOCOL, tier: 'scaffold' }
set (key, value) {
assertNonEmpty(key, 'key')
this._data.set(key, value)
this._stats.ops++
sendGossip(this, { type: 'crdt-version-vector-sync', key, value })
return true
}
async ready () { return true }
get (key) { return this._data.get(key) }
_notImplemented (method) {
this._stats.errors++
throw new Error(`not implemented: scaffold (${method})`)
merge (remote) {
for (const [k, v] of Object.entries(remote || {})) this._data.set(k, v)
}
toJSON () { return Object.fromEntries(this._data) }
_onGossip (d) {
if (!d || d.type !== 'crdt-version-vector-sync') return
this._stats.gossipIn++
if (d.key) this.set(d.key, d.value)
}
getStats () { return { ...this._stats, keys: this._data.size, protocol: PROTOCOL } }
async ready () {
if (this.swarm || !this.topic) return this
await attachGossip(this, { keyPair: this.keyPair, topic: this.topic, protocol: PROTOCOL, onmessage: (d) => this._onGossip(d) })
return this
}
async close () {
if (this.swarm) await this.swarm.destroy().catch(() => {})
this.swarm = null
}
}
+8 -5
View File
@@ -1,19 +1,18 @@
{
"name": "hyper-p2p-crdt-version-vector",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-crdt-version-vector",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"compact-encoding": "^2.0.0",
"compact-encoding": "^2.16.0",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0"
},
@@ -955,6 +954,8 @@
"resolved": "https://registry.npmjs.org/bare-timers/-/bare-timers-2.2.0.tgz",
"integrity": "sha512-Ft68HFT4d8oTLOGqHKBoLAA3h32BRyQLa7qzX1Z31QAPSrfBodfMIwBSo52xUtnPc/UIIP93kInezZ5cgWrZKg==",
"license": "Apache-2.0",
"optional": true,
"peer": true,
"dependencies": {
"tiny-binary-heap": "^1.1.0"
}
@@ -1704,7 +1705,9 @@
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/tiny-binary-heap/-/tiny-binary-heap-1.1.0.tgz",
"integrity": "sha512-IaM+/bh71URhhzlH1qNKNylamK4nxaby2q0qxolWL4Typ3+sIcZHxLe/Mj35b7GOHat/RKpp2vbRepjV6sD+Rw==",
"license": "MIT"
"license": "MIT",
"optional": true,
"peer": true
},
"node_modules/tmatch": {
"version": "5.0.0",
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-crdt-version-vector",
"version": "0.0.0-scaffold",
"version": "0.3.1",
"description": "Version vector CRDT.",
"main": "index.js",
"type": "commonjs",
@@ -9,17 +9,18 @@
"dependencies": {
"bare-events": "^2.8.0",
"bare-process": "^4.4.0",
"bare-timers": "^2.0.0",
"b4a": "^1.6.7",
"hypercore-crypto": "^3.0.0",
"protomux": "^3.0.0",
"compact-encoding": "^2.0.0"
"compact-encoding": "^2.16.0"
},
"peerDependencies": {
"hyperswarm": "^4.0.0",
"bare": ">=1.0.0"
},
"peerDependencies": { "hyperswarm": "^4.0.0", "bare": ">=1.0.0" },
"devDependencies": { "brittle": "^3.0.0" },
"imports": {
"process": { "bare": "bare-process", "default": "process" },
"events": { "bare": "bare-events", "default": "events" },
"timers": { "bare": "bare-timers", "default": "timers" }
"events": { "bare": "bare-events", "default": "events" }
}
}
}
@@ -1,14 +1,26 @@
require('bare-process/global')
const test = require('brittle')
const { HyperP2PCrdtVersionVector, PROTOCOL } = require('../index.js')
test('exports load', (t) => {
test('exports', (t) => {
t.ok(HyperP2PCrdtVersionVector)
t.is(PROTOCOL, 'crdt-version-vector/v1')
t.ok(PROTOCOL)
})
test('getStats shape', async (t) => {
test('basic operation', async (t) => {
const m = new HyperP2PCrdtVersionVector()
const s = m.getStats()
t.is(s.tier, 'scaffold')
t.is(s.protocol, 'crdt-version-vector/v1')
m.set('k', 1); t.ok(m.toJSON())
await m.close()
})
test('validation', async (t) => {
const m = new HyperP2PCrdtVersionVector()
try { m.put(null, 1) } catch (e) { t.ok(e) }
await m.close()
})
test('getStats', async (t) => {
const m = new HyperP2PCrdtVersionVector()
t.ok(m.getStats().protocol)
await m.close()
})