This commit is contained in:
Raven Scott
2026-05-20 21:02:45 -04:00
parent 1f3f4b24a2
commit 14d0980b4f
734 changed files with 682 additions and 746 deletions
@@ -0,0 +1,91 @@
# API: hyper-p2p-overlay-topology
**Protocol:** `overlay-topology/v1`
**Export:** `HyperP2POverlayTopology`
## Overview
Production network stack module: Hyperswarm discovery + Protomux when `topic` is set.
## Constructor
```js
const mod = new HyperP2POverlayTopology(opts)
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `topic` | varies | null | topic |
| `keyPair` | KeyPair | random Ed25519 | keyPair |
## Methods
### `addNeighbor(peerId, weight = 1)`
- **Returns:** `value`
- **Throws:**
- `Error: maxDegree exceeded`
- `Error: peerId required`
- `Error: weight must be non-negative`
### `removeNeighbor(peerId)`
- **Returns:** `value`
- **Throws:**
- `Error: peerId required`
### `getNeighbors(—)`
- **Returns:** `value`
- **Throws:** — (none documented in method body)
### `suggestReplacement(failedPeer)`
- **Returns:** `value`
- **Throws:**
- `Error: failedPeer required`
### `getStats(—)`
- **Returns:** `object`
- **Throws:** — (none documented in method body)
### `ready(—)`
- **Returns:** `Promise`
- **Throws:** — (none documented in method body)
### `close(—)`
- **Returns:** `Promise<void>`
- **Throws:** — (none documented in method body)
## Events
| Event | Payload |
|-------|---------|
| `closed` | no payload |
| `neighbor-added` | n |
| `neighbor-removed` | payload object |
## 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 `overlay-topology/v1`.
## Testing
```bash
npm install && npm test
```
Integration: [`../../real_tests/integration/overlay-topology-two-node.js`](../../../real_tests/integration/overlay-topology-two-node.js)
@@ -0,0 +1,46 @@
# Architecture: hyper-p2p-overlay-topology
**Category:** Network stack
```mermaid
flowchart LR
App[Application] --> Mod[HyperP2POverlayTopology]
Mod --> Mux[Protomux overlay-topology/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 |
|------|--------|-----------|----------|
| `topology-remove` | peerId | gossip | Handled in onmessage / gossipSend |
| `topology-update` | peerId, neighbors[] | 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-protocol-handshake`, `hyper-p2p-connection-pool`.
See [`../_shared/WAVE6_NETWORK_STACK.md`](../../_shared/WAVE6_NETWORK_STACK.md) for layer ordering.