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
+18 -19
View File
@@ -4,36 +4,35 @@
**Export:** `HyperP2PBucketRateLimit`
## Methods
## Constructor
- `tryConsume(peerId`
- `cost)`
- `configure({ rate`
- `burst })`
## P2P and runtime options
```js
const mod = new HyperP2PBucketRateLimit(opts)
```
| Option | Type | Default | Description |
|--------|------|---------|-------------|
| `topic` | `string` \| `Buffer` | `null` | Hyperswarm discovery topic. Hex (64 chars) or string (hashed via `hypercore-crypto`). P2P is active when set. |
| `keyPair` | `KeyPair` | random | Ed25519 key pair (`hypercore-crypto.keyPair()`). |
| `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) |
### Runtime flags (test exit)
## Methods
| Option | Modules | Default | Description |
|--------|---------|---------|-------------|
| `enableBackgroundTimers` | oracle, reputation | `false` | Enables periodic cleanup/decay/gossip timers. Keep `false` in unit tests so the process exits. |
| `enableGossip` | causal-consensus | `false` | Enables gossip interval + Protomux proposal fan-out when `topic` is also set. |
See [`index.js`](../index.js) for the full method list. Core operations implement **distributed token-bucket rate limiting with gossip sync**.
### Protomux
## Events
Wire format uses **Protomux v3** (`createChannel``addMessage``open`) via [`../_shared/p2p-bare.js`](../_shared/p2p-bare.js).
The instance extends `EventEmitter`. Common events: `closed`, plus module-specific events documented in source.
### Testing
## P2P
When `topic` is set, `ready()` joins Hyperswarm and opens Protomux channel `bucket-rate-limit/v1` via [`_shared/p2p-bare.js`](../_shared/p2p-bare.js).
## Testing
```bash
npm install
npx brittle-bare test/test.js
npm test
```
Integration (2-node): [`../../real_tests/integration/`](../../real_tests/integration/) — see [DEVELOPMENT.md](../../DEVELOPMENT.md).
Integration: [`../../real_tests/integration/bucket-rate-limit-two-node.js`](../../real_tests/integration/bucket-rate-limit-two-node.js)
@@ -1,10 +1,10 @@
# Architecture: hyper-p2p-bucket-rate-limit
`bucket-rate-limit/v1` over Hyperswarm + Protomux when `topic` is set.
```mermaid
flowchart LR
App[Application] --> Mod[HyperP2PBucketRateLimit]
Mod --> P2P[Protomux bucket-rate-limit/v1]
P2P --> Swarm[Hyperswarm]
```
Local state lives in memory maps/arrays; gossip merges remote updates when `topic` is configured.