This commit is contained in:
Raven Scott
2026-05-20 19:51:22 -04:00
parent 0f2ab198a6
commit 040f1d05ea
324 changed files with 2715 additions and 5466 deletions
+2
View File
@@ -3,4 +3,6 @@
## v0.1.0
- Initial release.
## v0.2.0
- Production-grade docs, validation, and expanded tests.
+11 -3
View File
@@ -1,18 +1,26 @@
# hyper-p2p-mycelium-pool
Bare/Pear P2P primitive — **mycelium-pool/v1**.
Bare/Pear P2P **symbiotic bandwidth credit pool**
**Protocol:** `mycelium-pool/v1`
## Quick start
```js
const { HyperP2PMyceliumPool } = require('hyper-p2p-mycelium-pool')
const mod = new HyperP2PMyceliumPool()
// await mod.ready() when using topic
await mod.close()
```
See `examples/basic.js` and `docs/api.md`.
## Docs
- [docs/api.md](docs/api.md)
- [docs/architecture.md](docs/architecture.md)
- [../_shared/PRODUCTION.md](../_shared/PRODUCTION.md)
## Test
```bash
npm test
```
+31 -1
View File
@@ -4,5 +4,35 @@
**Export:** `HyperP2PMyceliumPool`
Integration: `../../real_tests/integration/` smoke tests.
## Constructor
```js
const mod = new HyperP2PMyceliumPool(opts)
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `topic` | `string` \| `Buffer` | `null` | Hyperswarm topic; enables P2P when set |
| `keyPair` | `KeyPair` | random | Ed25519 key pair (`hypercore-crypto`) |
| `enableBackgroundTimers` | `boolean` | `false` | Periodic timers (keep false in unit tests) |
## Methods
See [`index.js`](../index.js) for the full method list. Core operations implement **symbiotic bandwidth credit pool**.
## Events
The instance extends `EventEmitter`. Common events: `closed`, plus module-specific events documented in source.
## P2P
When `topic` is set, `ready()` joins Hyperswarm and opens Protomux channel `mycelium-pool/v1` via [`_shared/p2p-bare.js`](../_shared/p2p-bare.js).
## Testing
```bash
npm install
npm test
```
Integration: [`../../real_tests/integration/mycelium-pool-two-node.js`](../../real_tests/integration/mycelium-pool-two-node.js)
+7 -1
View File
@@ -1,4 +1,10 @@
# Architecture: hyper-p2p-mycelium-pool
Hyperswarm + Protomux (`mycelium-pool/v1`) when `topic` is set via `p2p-bare.js`.
```mermaid
flowchart LR
App[Application] --> Mod[HyperP2PMyceliumPool]
Mod --> P2P[Protomux mycelium-pool/v1]
P2P --> Swarm[Hyperswarm]
```
Local state lives in memory maps/arrays; gossip merges remote updates when `topic` is configured.
+2
View File
@@ -26,6 +26,7 @@ class HyperP2PMyceliumPool extends EventEmitter {
}
donate (peerId, credits) {
if (credits == null || credits < 0) throw new Error('credits must be non-negative')
const id = this._pid(peerId)
const next = (this._balances.get(id) || 0) + Math.max(0, credits)
this._balances.set(id, next)
@@ -35,6 +36,7 @@ class HyperP2PMyceliumPool extends EventEmitter {
}
borrow (peerId, n) {
if (n == null || n < 0) throw new Error('borrow amount must be non-negative')
const id = this._pid(peerId)
const cur = this._balances.get(id) || 0
if (cur < n) return false
+2 -2
View File
@@ -1,12 +1,12 @@
{
"name": "hyper-p2p-mycelium-pool",
"version": "0.1.0",
"version": "0.2.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "hyper-p2p-mycelium-pool",
"version": "0.1.0",
"version": "0.2.0",
"license": "Apache-2.0",
"dependencies": {
"b4a": "^1.6.7",
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "hyper-p2p-mycelium-pool",
"version": "0.1.0",
"version": "0.2.0",
"description": "Symbiotic bandwidth credit pool for Bare/Pear P2P.",
"main": "index.js",
"type": "commonjs",
+16
View File
@@ -10,6 +10,17 @@ test('mycelium-pool: donate borrow', async (t) => {
await p.close()
})
test('mycelium-pool: reject negative credits', async (t) => {
const p = new HyperP2PMyceliumPool()
try {
p.donate('peer-a', -1)
t.fail('expected error')
} catch (err) {
t.ok(/non-negative/i.test(err.message))
}
await p.close()
})
test('mycelium-pool: merge balances', async (t) => {
const a = new HyperP2PMyceliumPool()
const b = new HyperP2PMyceliumPool()
@@ -19,3 +30,8 @@ test('mycelium-pool: merge balances', async (t) => {
await a.close()
await b.close()
})
test('hyper-p2p-mycelium-pool: close without leak', async (t) => {
const m = new HyperP2PMyceliumPool()
await m.close()
t.pass()
})