Initial Commit

This commit is contained in:
2026-05-27 23:29:53 +01:00
commit fbc70f96e6
10000 changed files with 1102152 additions and 0 deletions

View File

@@ -0,0 +1,108 @@
# OpenSpec Change: enterprise-server-network-gundb-server
- **Change ID**: enterprise-server-network-gundb-server
- **Package**: `@suaveplan/network-gundb-server`
- **Category**: server/network
- **Target path**: `server/network/network-gundb-server`
- **Status**: SUPERSEDED-BY-GENESIS-WAVE
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/network-gundb-server.md`
---
## Summary
Deliver `@suaveplan/network-gundb-server` on the genesis branch from scratch at enterprise quality. The package runs a Gun.js relay server over WebSockets, enabling decentralised real-time data sync for browser and server peers. It wraps Gun's `GUN({web: server})` pattern into a typed `GunServerAdapter` with `start` / `stop`, `get` / `put` / `on` / `off` for direct data operations on the server side, and connection-count observability. Genesis adds Zod-validated options, `@suaveplan/error` codes, OTel gauges, structured logging, and full integration tests.
---
## Current-state audit (genesis)
Does not exist on genesis — fresh build.
---
## Available workspace deps on genesis
| Package | Role |
|---|---|
| `@suaveplan/error` (`workspace:^`) | Typed `NETWORK_GUNDB.*` error codes |
| `@suaveplan/types` (`workspace:^`) | `LoggerLike` |
| `@suaveplan/logger` (`workspace:^`) | Structured logs |
| `zod` (`catalog:forms`) | Options validation |
| `@opentelemetry/api` (`catalog:observability`) | Gauges + counters |
| `gun` (`catalog:network`) | Gun.js database/relay |
| `tslib` (`catalog:build`) | Runtime helpers |
---
## Public API surface
```ts
class GunServerAdapter {
constructor(options: GunServerOptions)
start(): Promise<void>
stop(): Promise<void>
get<T = unknown>(path: string): Promise<T | undefined>
put(path: string, data: unknown): Promise<void>
on<T = unknown>(path: string, handler: (data: T, key: string) => void): () => void
peerCount(): number
isRunning(): boolean
address(): { host: string; port: number } | undefined
}
function createGunServerAdapter(options: GunServerOptions): GunServerAdapter
interface GunServerOptions {
port?: number // default: 8765
host?: string // default: '0.0.0.0'
peers?: string[] // bootstrap relay URLs
radisk?: boolean // persist to disk, default: false
logger?: LoggerLike
}
const GunServerOptionsSchema: z.ZodSchema<GunServerOptions>
type GunServerErrorCode =
| 'NETWORK_GUNDB_START_FAILED'
| 'NETWORK_GUNDB_STOP_FAILED'
| 'NETWORK_GUNDB_PUT_FAILED'
| 'NETWORK_GUNDB_GET_TIMEOUT'
function registerGunServerErrorCodes(): void
```
---
## File layout
```text
server/network/network-gundb-server/
package.json
tsconfig.json
vitest.config.ts
src/
index.ts
error-codes.ts
constants/index.ts
types/
gundb-server.types.ts
gundb-server.types.md
adapter/
gun-server-adapter/
gun-server-adapter.ts
gun-server-adapter.md
gun-server-adapter.types.ts
gun-server-adapter.test.ts
index.ts
tests/
integration.test.ts
```
---
## Impact
Downstream: real-time sync features, collaborative editing, offline-first client sync. Server-only.
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@suaveplan/logger`.

View File

@@ -0,0 +1,55 @@
# Tasks: enterprise-server-network-gundb-server
**Change ID**: enterprise-server-network-gundb-server
**Package**: `@suaveplan/network-gundb-server`
**Target path**: `server/network/network-gundb-server`
**Prerequisites on Verdaccio**: `@suaveplan/error`, `@suaveplan/types`, `@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/logger workspace:^`, `zod catalog:forms`, `@opentelemetry/api catalog:observability`, `gun 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 `pool: 'forks'`.
- [x] Scripts: `lint`, `typecheck`, `test`, `test:coverage`, `build`, `docs:check`.
## 2. Types and errors
- [x] Define `GunServerOptions`, `GunServerOptionsSchema` in `src/types/gundb-server.types.ts`.
- [x] Write `gundb-server.types.md` (≥200 words).
- [x] `src/error-codes.ts`: four codes; `registerGunServerErrorCodes()`; eager call.
## 3. Adapter implementation
- [x] Implement `GunServerAdapter` in `src/adapter/gun-server-adapter/gun-server-adapter.ts`:
- `start()`: create Bun HTTP server on `port`; pass it to `Gun({ web: server, peers, file: radisk })`. Wait for server to be listening; throw `NETWORK_GUNDB_START_FAILED` on error. OTel span.
- `stop()`: stop Bun server; Gun instance cleanup. OTel span.
- `get(path)`: `gun.get(path).once(...)` wrapped in a `Promise` with a 5-second timeout; throw `NETWORK_GUNDB_GET_TIMEOUT` on timeout.
- `put(path, data)`: `gun.get(path).put(data, ack => ...)` wrapped in a Promise; throw `NETWORK_GUNDB_PUT_FAILED` on ack error.
- `on(path, handler)`: `gun.get(path).on(handler)`; return unsubscribe fn.
- `peerCount()` / `isRunning()` / `address()`: internal state getters.
- OTel gauge `network_gundb.peer_count`; counter `network_gundb.ops_total`.
- [x] Write `gun-server-adapter.types.ts`.
- [x] Write `gun-server-adapter.test.ts` (≤500 lines): start/stop lifecycle; put + get round-trip; on subscription receives updates; timeout on missing key; `{ timeout: 30000 }`.
- [x] Write `gun-server-adapter.md` (≥200 words, simple + advanced, API reference).
## 4. Integration tests
- [x] `tests/integration.test.ts`: start server on random port; second Gun instance connects as peer; put from server, verify peer receives via `on`; stop server, peer disconnects gracefully; `{ timeout: 30000 }`.
## 5. OTel + docs
- [x] All OTel instrumentation per proposal.
- [x] Root `README.md`: quick-start server + browser client example, API table.
## 6. Verify and publish
- [x] `bunx turbo lint --filter=@suaveplan/network-gundb-server` — zero errors.
- [x] `bunx turbo typecheck --filter=@suaveplan/network-gundb-server` — zero errors.
- [x] `bunx turbo test --filter=@suaveplan/network-gundb-server` — all pass.
- [x] `bunx turbo test:coverage --filter=@suaveplan/network-gundb-server` — 100%.
- [x] `bun scripts/publish.ts --package @suaveplan/network-gundb-server`.
- [x] Update `CLAUDE.md`; archive to `openspec/archive/2026-04-24-enterprise-server-network-gundb-server/`.