Updates
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
*-storage/
|
||||
@@ -0,0 +1,19 @@
|
||||
# Changelog
|
||||
|
||||
<!-- legacy: v0.1.0 -->
|
||||
|
||||
- Initial release.
|
||||
<!-- legacy: v0.2.0 -->
|
||||
|
||||
- Production-grade docs, validation, and expanded tests.
|
||||
<!-- legacy: v0.3.0 -->
|
||||
|
||||
- Wave 6: presence-tier API tables, architecture wire section, validation test.
|
||||
|
||||
<!-- legacy: v0.3.1 -->
|
||||
|
||||
- Wave 7: correct protocol in docs, getStats(), wire tables, category README.
|
||||
## [0.3.2] - 2026-05-21
|
||||
|
||||
### Changed
|
||||
- Exhaustive documentation pass (api, architecture, README) per DOC_STANDARDS.md.
|
||||
@@ -0,0 +1,43 @@
|
||||
# hyper-p2p-mirror-realm
|
||||
|
||||
Production experimental (wave 4) module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
**Category:** Experimental (Wave 4)
|
||||
|
||||
**Composes with:** `hyper-p2p-pheromone-trail`, `hyper-p2p-whisper-mesh`, `hyper-p2p-entropy-beacon`
|
||||
|
||||
**Protocol:** `mirror-realm/v1`
|
||||
|
||||
## When to use
|
||||
|
||||
Multi-peer apps that need experimental (wave 4) over a shared Hyperswarm topic.
|
||||
|
||||
## When not to use
|
||||
|
||||
Single-process tools with no P2P topic (use local APIs only or skip `ready()`).
|
||||
|
||||
## Quick start
|
||||
|
||||
```js
|
||||
const { HyperP2PMirrorRealm } = require('hyper-p2p-mirror-realm')
|
||||
const topic = process.argv[2] // 64-char hex or string
|
||||
const mod = new HyperP2PMirrorRealm({ topic, enableBackgroundTimers: false })
|
||||
await mod.ready() // joins swarm when topic set
|
||||
// ... application logic ...
|
||||
await mod.close()
|
||||
```
|
||||
|
||||
## Docs
|
||||
|
||||
- [docs/api.md](docs/api.md) — constructor, methods, events, errors
|
||||
- [docs/architecture.md](docs/architecture.md) — wire types, state, composition
|
||||
- [../_shared/PRODUCTION.md](../../_shared/PRODUCTION.md) — production checklist
|
||||
- [../_shared/DOC_STANDARDS.md](../../_shared/DOC_STANDARDS.md) — documentation standards
|
||||
|
||||
- Integration: [`../../real_tests/integration/`](../../../real_tests/integration/) — `mirror-realm-two-node.js`
|
||||
|
||||
## Test
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
@@ -0,0 +1,97 @@
|
||||
# API: hyper-p2p-mirror-realm
|
||||
|
||||
**Protocol:** `mirror-realm/v1`
|
||||
|
||||
**Export:** `HyperP2PMirrorRealm`
|
||||
|
||||
## Overview
|
||||
|
||||
Production experimental (wave 4) module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
## Constructor
|
||||
|
||||
```js
|
||||
const mod = new HyperP2PMirrorRealm(opts)
|
||||
```
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `topic` | varies | null | topic |
|
||||
| `keyPair` | KeyPair | random Ed25519 | keyPair |
|
||||
|
||||
## Methods
|
||||
|
||||
### `write(realm, key, value)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `read(realm, key)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `fork(—)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `reconcile(strategy = 'lww')`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `isForked(—)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `toJSON(—)`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `ready(—)`
|
||||
|
||||
- **Returns:** `Promise`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `getStats(—)`
|
||||
|
||||
- **Returns:** `object`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `close(—)`
|
||||
|
||||
- **Returns:** `Promise<void>`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
## Events
|
||||
|
||||
| Event | Payload |
|
||||
|-------|---------|
|
||||
| `closed` | no payload |
|
||||
| `fork` | size |
|
||||
| `reconcile` | updated |
|
||||
| `write` | cell |
|
||||
|
||||
## getStats()
|
||||
|
||||
Returns `{ ...this._stats }` — typically `ops`, `errors`, and module-specific counters (`created`, `relays`, `open`, `peers`, etc.).
|
||||
Library-only modules may include `mode: 'local'`.
|
||||
|
||||
## Errors
|
||||
|
||||
Stable message substrings: see [`../_shared/ERROR_CODES.md`](../../_shared/ERROR_CODES.md).
|
||||
|
||||
## P2P
|
||||
|
||||
When `topic` is set, `ready()` joins Hyperswarm and opens Protomux `mirror-realm/v1`.
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
Integration: [`../../real_tests/integration/mirror-realm-two-node.js`](../../../real_tests/integration/mirror-realm-two-node.js)
|
||||
@@ -0,0 +1,43 @@
|
||||
# Architecture: hyper-p2p-mirror-realm
|
||||
|
||||
**Category:** Experimental (Wave 4)
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
App[Application] --> Mod[HyperP2PMirrorRealm]
|
||||
Mod --> Mux[Protomux mirror-realm/v1]
|
||||
Mux --> Swarm[Hyperswarm]
|
||||
```
|
||||
|
||||
## Sequence (P2P)
|
||||
|
||||
```mermaid
|
||||
sequenceDiagram
|
||||
participant App
|
||||
participant Mod as Module
|
||||
participant SW as Hyperswarm
|
||||
participant Peer
|
||||
App->>Mod: ready(topic)
|
||||
Mod->>SW: join(topic)
|
||||
SW->>Peer: connection
|
||||
Mod->>Peer: gossip / Protomux
|
||||
Peer-->>Mod: onmessage
|
||||
Mod-->>App: emit(event)
|
||||
```
|
||||
|
||||
## Wire messages
|
||||
|
||||
| type | fields | direction | behavior |
|
||||
|------|--------|-----------|----------|
|
||||
| `write` | cell | gossip | Handled in onmessage / gossipSend |
|
||||
|
||||
## State model
|
||||
|
||||
- In-memory `Map` / `Set` structures for hot path
|
||||
- Optional Hyperbee/Hypercore persistence when `storageDir` or `memoryOnly` is configured
|
||||
- `close()` tears down swarm, timers, and clears ephemeral state
|
||||
|
||||
## Composition
|
||||
|
||||
Composes with: `hyper-p2p-pheromone-trail`, `hyper-p2p-whisper-mesh`, `hyper-p2p-entropy-beacon`.
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PMirrorRealm } = require('../index.js')
|
||||
async function main () {
|
||||
const m = new HyperP2PMirrorRealm()
|
||||
m.write('A', 'key', 1)
|
||||
m.fork()
|
||||
console.log('forked', m.isForked())
|
||||
await m.close()
|
||||
}
|
||||
main().catch(console.error)
|
||||
@@ -0,0 +1,122 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'mirror-realm/v1'
|
||||
|
||||
class HyperP2PMirrorRealm extends EventEmitter {
|
||||
constructor (opts = {}) {
|
||||
super()
|
||||
this._stats = { ops: 0, errors: 0 }
|
||||
|
||||
this.topic = opts.topic || null
|
||||
this.keyPair = opts.keyPair || require('hypercore-crypto').keyPair()
|
||||
this.peerId = b4a.toString(this.keyPair.publicKey, 'hex')
|
||||
this._realmA = new Map()
|
||||
this._realmB = new Map()
|
||||
this._forked = false
|
||||
this._writeSeq = 0
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
_realm (name) {
|
||||
return name === 'B' || name === 'realmB' ? this._realmB : this._realmA
|
||||
}
|
||||
|
||||
write (realm, key, value) {
|
||||
const map = this._realm(realm)
|
||||
const cell = { key: String(key), value, ts: Date.now() + (++this._writeSeq), peerId: this.peerId, realm }
|
||||
map.set(cell.key, cell)
|
||||
if (this._peerMsgs) gossipSend(this, { type: 'write', cell })
|
||||
this.emit('write', cell)
|
||||
return cell
|
||||
}
|
||||
|
||||
read (realm, key) {
|
||||
const c = this._realm(realm).get(String(key))
|
||||
return c ? c.value : undefined
|
||||
}
|
||||
|
||||
fork () {
|
||||
this._realmB = new Map()
|
||||
for (const [k, v] of this._realmA) {
|
||||
this._realmB.set(k, { ...v, realm: 'B', forkedAt: Date.now() })
|
||||
}
|
||||
this._forked = true
|
||||
this.emit('fork', { size: this._realmB.size })
|
||||
return { realmA: this._realmA.size, realmB: this._realmB.size }
|
||||
}
|
||||
|
||||
reconcile (strategy = 'lww') {
|
||||
let n = 0
|
||||
const keys = new Set([...this._realmA.keys(), ...this._realmB.keys()])
|
||||
for (const key of keys) {
|
||||
const a = this._realmA.get(key)
|
||||
const b = this._realmB.get(key)
|
||||
if (!a && b) {
|
||||
this._realmA.set(key, { ...b, realm: 'A' })
|
||||
n++
|
||||
} else if (a && !b) {
|
||||
this._realmB.set(key, { ...a, realm: 'B' })
|
||||
n++
|
||||
} else if (a && b && a.value !== b.value) {
|
||||
let winner
|
||||
if (this._forked) {
|
||||
winner = b
|
||||
} else if (strategy === 'maxTs') {
|
||||
winner = a.ts >= b.ts ? a : b
|
||||
} else {
|
||||
winner = (a.ts > b.ts || (a.ts === b.ts && a.peerId >= b.peerId)) ? a : b
|
||||
}
|
||||
this._realmA.set(key, { ...winner, realm: 'A' })
|
||||
this._realmB.set(key, { ...winner, realm: 'B' })
|
||||
n++
|
||||
}
|
||||
}
|
||||
this._forked = false
|
||||
this.emit('reconcile', { updated: n, strategy })
|
||||
return n
|
||||
}
|
||||
|
||||
isForked () {
|
||||
return this._forked
|
||||
}
|
||||
|
||||
toJSON () {
|
||||
return {
|
||||
realmA: [...this._realmA.values()],
|
||||
realmB: [...this._realmB.values()],
|
||||
forked: this._forked
|
||||
}
|
||||
}
|
||||
|
||||
async ready () {
|
||||
if (this.swarm || !this.topic) return this
|
||||
await initModuleSwarm(this, {
|
||||
keyPair: this.keyPair,
|
||||
topic: this.topic,
|
||||
protocol: PROTOCOL,
|
||||
onmessage: (data) => {
|
||||
if (data && data.type === 'write' && data.cell) {
|
||||
this._realm(data.cell.realm).set(data.cell.key, data.cell)
|
||||
}
|
||||
}
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats }
|
||||
}
|
||||
|
||||
async close () {
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PMirrorRealm, PROTOCOL }
|
||||
+1774
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,23 @@
|
||||
{
|
||||
"name": "hyper-p2p-mirror-realm",
|
||||
"version": "0.3.1",
|
||||
"description": "Split-brain dual realm fork/reconcile for Bare/Pear P2P.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"license": "Apache-2.0",
|
||||
"scripts": { "test": "brittle-bare test/test.js" },
|
||||
"dependencies": {
|
||||
"bare-events": "^2.8.0",
|
||||
"bare-process": "^4.4.0",
|
||||
"b4a": "^1.6.7",
|
||||
"hypercore-crypto": "^3.0.0",
|
||||
"protomux": "^3.0.0",
|
||||
"compact-encoding": "^2.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" }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
require('bare-process/global')
|
||||
const test = require('brittle')
|
||||
const { HyperP2PMirrorRealm } = require('../index.js')
|
||||
|
||||
test('mirror-realm: fork and reconcile', async (t) => {
|
||||
const m = new HyperP2PMirrorRealm()
|
||||
m.write('A', 'k', 1)
|
||||
m.fork()
|
||||
m.write('B', 'k', 2)
|
||||
t.ok(m.isForked())
|
||||
m.reconcile('lww')
|
||||
t.is(m.read('A', 'k'), 2)
|
||||
await m.close()
|
||||
})
|
||||
|
||||
test('mirror-realm: dual write', async (t) => {
|
||||
const m = new HyperP2PMirrorRealm()
|
||||
m.write('A', 'x', 'alpha')
|
||||
t.is(m.read('A', 'x'), 'alpha')
|
||||
await m.close()
|
||||
})
|
||||
test('hyper-p2p-mirror-realm: close without leak', async (t) => {
|
||||
const m = new HyperP2PMirrorRealm()
|
||||
await m.close()
|
||||
t.pass()
|
||||
})
|
||||
test('hyper-p2p-mirror-realm: validation rejects invalid input', async (t) => {
|
||||
const m = new HyperP2PMirrorRealm()
|
||||
try {
|
||||
if (typeof m.addNeighbor === 'function') m.addNeighbor(null)
|
||||
else if (typeof m.buildCircuit === 'function') m.buildCircuit([])
|
||||
else if (typeof m.grant === 'function') m.grant(null, -1)
|
||||
else if (typeof m.enqueue === 'function') m.enqueue('bad', null)
|
||||
else if (typeof m.reportSample === 'function') m.reportSample(null, -1, -1)
|
||||
else if (typeof m.fanout === 'function') m.fanout(null, 0)
|
||||
else if (typeof m.probe === 'function') m.probe(null)
|
||||
else if (typeof m.resolve === 'function') m.resolve(null)
|
||||
else if (typeof m.acquire === 'function') m.acquire(null)
|
||||
else throw new Error('no validation hook')
|
||||
t.fail('expected throw')
|
||||
} catch (err) {
|
||||
t.ok(err instanceof Error)
|
||||
}
|
||||
await m.close()
|
||||
})
|
||||
Reference in New Issue
Block a user