Initial Commit
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
# OpenSpec Change: enterprise-server-network-hyperswarm
|
||||
|
||||
- **Change ID**: enterprise-server-network-hyperswarm
|
||||
- **Package**: `@suaveplan/network-hyperswarm`
|
||||
- **Category**: server/network
|
||||
- **Target path**: `server/network/network-hyperswarm`
|
||||
- **Status**: SUPERSEDED-BY-GENESIS-WAVE
|
||||
- **Date**: 2026-04-24
|
||||
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/network-hyperswarm.md`
|
||||
|
||||
---
|
||||
|
||||
## Summary
|
||||
|
||||
Deliver `@suaveplan/network-hyperswarm` on the genesis branch from scratch at enterprise quality. The package provides a Node.js server-side transport adapter built on [Hyperswarm](https://github.com/holepunchto/hyperswarm) — a DHT-based peer discovery and encrypted connection layer. It exposes a typed `HyperswarmAdapter` with `join(topic)` / `leave(topic)` / `connect(peerId)` / `disconnect(peerId)` / `send(peerId, data)` / `onData(handler)` and full connection lifecycle events. Connections use the Noise protocol for encryption. Genesis adds `@suaveplan/error`-aligned errors, OTel spans + gauges (peer count, message throughput), retry logic, structured logging, and 100% unit + integration coverage.
|
||||
|
||||
---
|
||||
|
||||
## Current-state audit (genesis)
|
||||
|
||||
Does not exist on genesis — fresh build.
|
||||
|
||||
---
|
||||
|
||||
## Available workspace deps on genesis
|
||||
|
||||
| Package | Role |
|
||||
|---|---|
|
||||
| `@suaveplan/error` (`workspace:^`) | Typed `NETWORK_HYPERSWARM.*` error codes |
|
||||
| `@suaveplan/types` (`workspace:^`) | `LoggerLike`, `EventBusLike` |
|
||||
| `@suaveplan/events` (`workspace:^`) | Internal event bus for peer events |
|
||||
| `@suaveplan/retry` (`workspace:^`) | Reconnect backoff |
|
||||
| `@suaveplan/logger` (`workspace:^`) | Structured logs |
|
||||
| `zod` (`catalog:forms`) | Options validation |
|
||||
| `@opentelemetry/api` (`catalog:observability`) | Spans + gauges |
|
||||
| `hyperswarm` (`catalog:network`) | DHT peer discovery |
|
||||
| `tslib` (`catalog:build`) | Runtime helpers |
|
||||
|
||||
---
|
||||
|
||||
## Public API surface
|
||||
|
||||
```ts
|
||||
class HyperswarmAdapter {
|
||||
constructor(options: HyperswarmOptions)
|
||||
join(topic: Buffer | string): Promise<void>
|
||||
leave(topic: Buffer | string): Promise<void>
|
||||
send(peerId: string, data: Uint8Array): Promise<void>
|
||||
broadcast(topic: Buffer | string, data: Uint8Array): Promise<void>
|
||||
onData(handler: (peerId: string, data: Uint8Array) => void): () => void
|
||||
onPeerConnect(handler: (peerId: string) => void): () => void
|
||||
onPeerDisconnect(handler: (peerId: string) => void): () => void
|
||||
peers(topic?: Buffer | string): string[]
|
||||
peerCount(): number
|
||||
destroy(): Promise<void>
|
||||
}
|
||||
|
||||
function createHyperswarmAdapter(options: HyperswarmOptions): HyperswarmAdapter
|
||||
|
||||
interface HyperswarmOptions {
|
||||
keyPair?: { publicKey: Buffer; secretKey: Buffer }
|
||||
maxPeers?: number // default: 64
|
||||
firewall?: (remotePublicKey: Buffer) => boolean
|
||||
logger?: LoggerLike
|
||||
}
|
||||
|
||||
const HyperswarmOptionsSchema: z.ZodSchema<HyperswarmOptions>
|
||||
|
||||
type HyperswarmErrorCode =
|
||||
| 'NETWORK_HYPERSWARM_JOIN_FAILED'
|
||||
| 'NETWORK_HYPERSWARM_SEND_FAILED'
|
||||
| 'NETWORK_HYPERSWARM_PEER_NOT_FOUND'
|
||||
| 'NETWORK_HYPERSWARM_DESTROY_FAILED'
|
||||
|
||||
function registerHyperswarmErrorCodes(): void
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## File layout
|
||||
|
||||
```text
|
||||
server/network/network-hyperswarm/
|
||||
package.json
|
||||
tsconfig.json
|
||||
vitest.config.ts
|
||||
src/
|
||||
index.ts
|
||||
error-codes.ts
|
||||
constants/index.ts
|
||||
types/
|
||||
hyperswarm.types.ts
|
||||
hyperswarm.types.md
|
||||
adapter/
|
||||
hyperswarm-adapter/
|
||||
hyperswarm-adapter.ts
|
||||
hyperswarm-adapter.md
|
||||
hyperswarm-adapter.types.ts
|
||||
hyperswarm-adapter.test.ts
|
||||
index.ts
|
||||
tests/
|
||||
integration.test.ts
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Impact
|
||||
|
||||
Downstream: decentralised network packages, peer-to-peer features in the platform. Requires Node.js and native modules — not universal.
|
||||
|
||||
**Prerequisites**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/events`, `@suaveplan/retry`, `@suaveplan/logger`.
|
||||
@@ -0,0 +1,61 @@
|
||||
# Tasks: enterprise-server-network-hyperswarm
|
||||
|
||||
**Change ID**: enterprise-server-network-hyperswarm
|
||||
**Package**: `@suaveplan/network-hyperswarm`
|
||||
**Target path**: `server/network/network-hyperswarm`
|
||||
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/events`, `@suaveplan/retry`, `@suaveplan/logger`
|
||||
|
||||
---
|
||||
|
||||
## 1. Scaffold
|
||||
|
||||
- [x] Create directory tree per `proposal.md`.
|
||||
- [x] `package.json` at `0.1.0`, `"sideEffects": false`, exports `"."` + `adapter` subpath.
|
||||
- [x] Deps: `@suaveplan/error workspace:^`, `@suaveplan/types workspace:^`, `@suaveplan/events workspace:^`, `@suaveplan/retry workspace:^`, `@suaveplan/logger workspace:^`, `zod catalog:forms`, `@opentelemetry/api catalog:observability`, `hyperswarm catalog:network`, `tslib catalog:build`.
|
||||
- [x] DevDeps: `vitest catalog:testing`, `@vitest/coverage-istanbul catalog:testing`, `@suaveplan/testing workspace:^`.
|
||||
- [x] `tsconfig.json` extending `library-node.json`; `vitest.config.ts` with `createTestConfig()` and `pool: 'forks'`.
|
||||
- [x] Scripts: `lint`, `typecheck`, `test`, `test:coverage`, `build`, `docs:check`.
|
||||
|
||||
## 2. Types and errors
|
||||
|
||||
- [x] Define `HyperswarmOptions`, `HyperswarmOptionsSchema`, peer event handler types in `src/types/hyperswarm.types.ts`.
|
||||
- [x] Write `hyperswarm.types.md` (≥200 words).
|
||||
- [x] `src/error-codes.ts`: all four error codes with metadata; `registerHyperswarmErrorCodes()`; eager call.
|
||||
|
||||
## 3. Adapter implementation
|
||||
|
||||
- [x] Implement `HyperswarmAdapter` in `src/adapter/hyperswarm-adapter/hyperswarm-adapter.ts`:
|
||||
- Constructor creates `new Hyperswarm({ keyPair, maxPeers, firewall })`; validates options via `HyperswarmOptionsSchema`; registers event listeners.
|
||||
- `join(topic)`: `swarm.join(toBuffer(topic))` + `await swarm.flush()`; throws `NETWORK_HYPERSWARM_JOIN_FAILED` on error.
|
||||
- `leave(topic)`: `swarm.leave(toBuffer(topic))`.
|
||||
- `send(peerId, data)`: find connection in internal map; write `data`; throws `NETWORK_HYPERSWARM_PEER_NOT_FOUND` if not connected.
|
||||
- `broadcast(topic, data)`: iterate all peers in topic; `send` to each.
|
||||
- `onData` / `onPeerConnect` / `onPeerDisconnect`: register listeners via internal `EventEmitter`; return unsubscribe fn.
|
||||
- `peers(topic?)`: return peer IDs for topic or all.
|
||||
- `peerCount()`: total connected peers.
|
||||
- `destroy()`: `await swarm.destroy()`.
|
||||
- OTel gauge `network_hyperswarm.peer_count`; counter `network_hyperswarm.messages_total`; span per `join` / `send`.
|
||||
- [x] Export `createHyperswarmAdapter(options)`.
|
||||
- [x] Write `hyperswarm-adapter.types.ts` with internal connection-map types.
|
||||
- [x] Write `hyperswarm-adapter.test.ts` (≤500 lines): unit tests with real Hyperswarm instances on loopback (two adapters connecting to each other) — no mocks; send/receive round-trip, peer lifecycle events, destroy cleanup. Skip tests if `SKIP_NETWORK_TESTS=1`.
|
||||
- [x] Write `hyperswarm-adapter.md` (≥200 words, simple + advanced, API reference).
|
||||
|
||||
## 4. Integration tests
|
||||
|
||||
- [x] `tests/integration.test.ts`: multi-peer mesh test — three adapters join same topic, each sends to others, verify all receive; one adapter destroys, others emit disconnect; `{ timeout: 30000 }`.
|
||||
|
||||
## 5. OTel + docs
|
||||
|
||||
- [x] Gauge `network_hyperswarm.peer_count` updated on connect/disconnect.
|
||||
- [x] Counter `network_hyperswarm.messages_total` with `direction` (`sent`/`received`).
|
||||
- [x] Span `network_hyperswarm.join` and `network_hyperswarm.send`.
|
||||
- [x] Root `README.md`: quick-start, key-pair generation example, multi-peer mesh example, API table.
|
||||
|
||||
## 6. Verify and publish
|
||||
|
||||
- [x] `bunx turbo lint --filter=@suaveplan/network-hyperswarm` — zero errors.
|
||||
- [x] `bunx turbo typecheck --filter=@suaveplan/network-hyperswarm` — zero errors.
|
||||
- [x] `bunx turbo test --filter=@suaveplan/network-hyperswarm` — all pass.
|
||||
- [x] `bunx turbo test:coverage --filter=@suaveplan/network-hyperswarm` — 100%.
|
||||
- [x] `bun scripts/publish.ts --package @suaveplan/network-hyperswarm`.
|
||||
- [x] Update `CLAUDE.md`; archive to `openspec/archive/2026-04-24-enterprise-server-network-hyperswarm/`.
|
||||
Reference in New Issue
Block a user