Updates
This commit is contained in:
@@ -0,0 +1,2 @@
|
||||
node_modules/
|
||||
*-storage/
|
||||
@@ -0,0 +1,23 @@
|
||||
# Changelog
|
||||
|
||||
<!-- legacy: v0.2.0 -->
|
||||
|
||||
- `handoff()` returns signed capability token verifiable via hyper-p2p-capabilities.
|
||||
|
||||
<!-- legacy: v0.1.0 -->
|
||||
|
||||
- Initial release.
|
||||
<!-- legacy: v0.2.1 -->
|
||||
|
||||
- Production docs, input validation, third test, integration notes.
|
||||
<!-- 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-session-bridge
|
||||
|
||||
Production core infrastructure module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
**Category:** Core infrastructure
|
||||
|
||||
**Composes with:** `hyper-p2p-presence`, `hyper-p2p-rpc`, `hyper-p2p-capabilities`
|
||||
|
||||
**Protocol:** `session-bridge/v1`
|
||||
|
||||
## When to use
|
||||
|
||||
Multi-peer apps that need core infrastructure 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 { HyperP2PSessionBridge } = require('hyper-p2p-session-bridge')
|
||||
const topic = process.argv[2] // 64-char hex or string
|
||||
const mod = new HyperP2PSessionBridge({ 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/) — `session-bridge-two-node.js`
|
||||
|
||||
## Test
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
@@ -0,0 +1,83 @@
|
||||
# API: hyper-p2p-session-bridge
|
||||
|
||||
**Protocol:** `session-bridge/v1`
|
||||
|
||||
**Export:** `HyperP2PSessionBridge`
|
||||
|
||||
## Overview
|
||||
|
||||
Production core infrastructure module: Hyperswarm discovery + Protomux when `topic` is set.
|
||||
|
||||
## Constructor
|
||||
|
||||
```js
|
||||
const mod = new HyperP2PSessionBridge(opts)
|
||||
```
|
||||
|
||||
| Option | Type | Default | Description |
|
||||
|--------|------|---------|-------------|
|
||||
| `topic` | varies | null | topic |
|
||||
| `keyPair` | KeyPair | random Ed25519 | keyPair |
|
||||
| `subjectPubKey` | varies | this.keyPair.publicKey | Capability subject public key |
|
||||
|
||||
## Methods
|
||||
|
||||
### `createPair(—)`
|
||||
|
||||
- **Returns:** `Promise`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `handoff(token, opts = {})`
|
||||
|
||||
- **Returns:** `value`
|
||||
- **Throws:** — (none documented in method body)
|
||||
|
||||
### `getPair(token)`
|
||||
|
||||
- **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 |
|
||||
| `handoff` | handoff |
|
||||
| `pair` | payload object |
|
||||
| `remote-handoff` | data.handoff |
|
||||
|
||||
## 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 `session-bridge/v1`.
|
||||
|
||||
## Testing
|
||||
|
||||
```bash
|
||||
npm install && npm test
|
||||
```
|
||||
|
||||
Integration: [`../../real_tests/integration/session-bridge-two-node.js`](../../../real_tests/integration/session-bridge-two-node.js)
|
||||
@@ -0,0 +1,43 @@
|
||||
# Architecture: hyper-p2p-session-bridge
|
||||
|
||||
**Category:** Core infrastructure
|
||||
|
||||
```mermaid
|
||||
flowchart LR
|
||||
App[Application] --> Mod[HyperP2PSessionBridge]
|
||||
Mod --> Mux[Protomux session-bridge/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 |
|
||||
|------|--------|-----------|----------|
|
||||
| `handoff` | token, capability, publicKey, at | gossip | Session capability handoff broadcast |
|
||||
|
||||
## 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-presence`, `hyper-p2p-rpc`, `hyper-p2p-capabilities`.
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
require('bare-process/global')
|
||||
const { HyperP2PSessionBridge } = require('../index.js')
|
||||
|
||||
async function main () {
|
||||
const b = new HyperP2PSessionBridge()
|
||||
const { pairId } = await b.createPair()
|
||||
console.log('pair', pairId)
|
||||
console.log('handoff', b.handoff(pairId))
|
||||
await b.close()
|
||||
console.log('done')
|
||||
}
|
||||
main().catch(console.error)
|
||||
@@ -0,0 +1,93 @@
|
||||
require('bare-process/global')
|
||||
const EventEmitter = require('bare-events')
|
||||
const b4a = require('b4a')
|
||||
const SecretStream = require('@hyperswarm/secret-stream')
|
||||
const { pairSecretStreams, waitSecretStreamsConnected } = require('../hyper-p2p-rpc/lib/pair.js')
|
||||
const { createCapability } = require('../hyper-p2p-capabilities/index.js')
|
||||
const { initModuleSwarm, gossipSend } = require('../../_shared/p2p-bare.js')
|
||||
|
||||
const PROTOCOL = 'session-bridge/v1'
|
||||
|
||||
class HyperP2PSessionBridge 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.subjectPubKey = opts.subjectPubKey || this.keyPair.publicKey
|
||||
this._tokens = new Map()
|
||||
this.swarm = null
|
||||
this._peerMsgs = null
|
||||
}
|
||||
|
||||
async createPair () {
|
||||
const initiator = new SecretStream(true)
|
||||
const responder = new SecretStream(false)
|
||||
pairSecretStreams(initiator, responder)
|
||||
await waitSecretStreamsConnected(initiator, responder)
|
||||
const pairId = b4a.toString(require('hypercore-crypto').hash(initiator.publicKey), 'hex').slice(0, 16)
|
||||
this._tokens.set(pairId, { initiator, responder, createdAt: Date.now() })
|
||||
this.emit('pair', { pairId })
|
||||
return { pairId, initiator, responder }
|
||||
}
|
||||
|
||||
handoff (token, opts = {}) {
|
||||
const entry = this._tokens.get(token)
|
||||
if (!entry) return null
|
||||
const resource = opts.resource || `session-bridge://${token}`
|
||||
const actions = opts.actions || ['relay', 'read']
|
||||
const cap = createCapability(
|
||||
this.keyPair,
|
||||
opts.subjectPubKey || this.subjectPubKey,
|
||||
resource,
|
||||
actions,
|
||||
opts.ttlMs || 3600000
|
||||
)
|
||||
const handoff = {
|
||||
token,
|
||||
pairId: token,
|
||||
capability: cap,
|
||||
publicKey: b4a.toString(entry.initiator.publicKey, 'hex'),
|
||||
at: Date.now()
|
||||
}
|
||||
if (this._peerMsgs) gossipSend(this, { type: 'handoff', handoff })
|
||||
this.emit('handoff', handoff)
|
||||
return handoff
|
||||
}
|
||||
|
||||
getPair (token) {
|
||||
return this._tokens.get(token) || null
|
||||
}
|
||||
|
||||
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 === 'handoff') this.emit('remote-handoff', data.handoff)
|
||||
}
|
||||
})
|
||||
return this
|
||||
}
|
||||
|
||||
|
||||
getStats () {
|
||||
return { ...this._stats }
|
||||
}
|
||||
|
||||
async close () {
|
||||
for (const entry of this._tokens.values()) {
|
||||
try { await entry.initiator.destroy() } catch (_) {}
|
||||
try { await entry.responder.destroy() } catch (_) {}
|
||||
}
|
||||
this._tokens.clear()
|
||||
if (this.swarm) await this.swarm.destroy().catch(() => {})
|
||||
this.swarm = null
|
||||
this.emit('closed')
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = { HyperP2PSessionBridge, PROTOCOL }
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"name": "hyper-p2p-session-bridge",
|
||||
"version": "0.3.1",
|
||||
"description": "SecretStream session bridge and handoff for Bare/Pear P2P.",
|
||||
"main": "index.js",
|
||||
"type": "commonjs",
|
||||
"license": "Apache-2.0",
|
||||
"scripts": { "test": "brittle-bare test/test.js" },
|
||||
"dependencies": {
|
||||
"@hyperswarm/secret-stream": "^6.9.0",
|
||||
"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 { HyperP2PSessionBridge } = require('../index.js')
|
||||
|
||||
test('session-bridge: createPair', async (t) => {
|
||||
const b = new HyperP2PSessionBridge()
|
||||
const { pairId, initiator, responder } = await b.createPair()
|
||||
t.ok(pairId)
|
||||
t.ok(initiator)
|
||||
t.ok(responder)
|
||||
await b.close()
|
||||
})
|
||||
|
||||
test('session-bridge: handoff token', async (t) => {
|
||||
const b = new HyperP2PSessionBridge()
|
||||
const { pairId } = await b.createPair()
|
||||
const h = b.handoff(pairId)
|
||||
t.ok(h)
|
||||
t.is(h.token, pairId)
|
||||
await b.close()
|
||||
})
|
||||
test('hyper-p2p-session-bridge: close without leak', async (t) => {
|
||||
const m = new HyperP2PSessionBridge()
|
||||
await m.close()
|
||||
t.pass()
|
||||
})
|
||||
test('hyper-p2p-session-bridge: validation rejects invalid input', async (t) => {
|
||||
const m = new HyperP2PSessionBridge()
|
||||
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