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,83 @@
# OpenSpec Change: enterprise-api-plugin-rate-limit
- **Change ID**: enterprise-api-plugin-rate-limit
- **Package**: `@suaveplan/api-plugin-rate-limit`
- **Category**: api/data
- **Target path**: `api/data/api-plugin-rate-limit`
- **Status**: PROPOSED
- **Date**: 2026-04-24
- **Reference spec**: `/mnt/s/web/suaveplan/enterprise/api-plugin-rate-limit.md`
---
## Summary
Deliver `@suaveplan/api-plugin-rate-limit` on the genesis branch from scratch at enterprise quality. Rate-limiting plugin that wraps `@suaveplan/rate-limiter` and exposes it as a `@suaveplan/api` plugin service. Registers a `RateLimiterService` accessible by other plugins, provides per-route Hono middleware factory, configurable strategies (fixed-window, sliding-window, token-bucket, leaky-bucket) per endpoint, key extraction (IP, user-id, API-key, custom header), per-tenant override support, admin endpoints for viewing/resetting rate-limit state, and OTel metrics. Routes under `/admin/rate-limits`. Events: `rate-limit:exceeded`, `rate-limit:reset`.
---
## Available workspace deps on genesis
| Package | Role |
|---|---|
| `@suaveplan/api` (`workspace:^`) | `definePlugin` |
| `@suaveplan/api-shared` (`workspace:^`) | Helpers |
| `@suaveplan/api-plugin-auth` (`workspace:^`) | `requireAuth`, `requireRole` |
| `@suaveplan/rate-limiter` (`workspace:^`) | Rate-limiter core |
| `@suaveplan/events` (`workspace:^`) | Event bus |
| `@suaveplan/error` (`workspace:^`) | `RateLimitPluginError` |
| `hono` + `@hono/zod-openapi` + `zod` | Routes |
| `@opentelemetry/api` | OTel |
---
## Public API surface
```ts
function createRateLimitPlugin(config?: RateLimitPluginConfig): PluginDescriptor
// Registered service:
interface RateLimiterService {
middleware(options: RouteRateLimitOptions): MiddlewareHandler
check(key: string, limit: number, windowMs: number): Promise<RateLimitResult>
reset(key: string): Promise<void>
getState(key: string): Promise<RateLimitState>
}
interface RateLimitPluginConfig {
defaultWindowMs?: number
defaultLimit?: number
strategy?: 'fixed-window' | 'sliding-window' | 'token-bucket' | 'leaky-bucket'
keyExtractor?: (c: Context) => string
}
```
### Error codes
```ts
type RateLimitPluginErrorCode =
| "RATE_LIMIT_EXCEEDED"
| "RATE_LIMIT_KEY_MISSING"
| "RATE_LIMIT_CONFIG_INVALID"
```
---
## File layout
```text
api/data/api-plugin-rate-limit/
package.json tsconfig.json vitest.config.ts
src/
index.ts events.ts types/ errors/
services/rate-limiter/
middleware/rate-limit/
routes/admin/
tests/integration.test.ts
```
---
## Impact
Prerequisites: `@suaveplan/api`, `@suaveplan/api-plugin-auth`, `@suaveplan/rate-limiter`, `@suaveplan/events`.

View File

@@ -0,0 +1,59 @@
# Tasks: enterprise-api-plugin-rate-limit
**Change ID**: enterprise-api-plugin-rate-limit
**Package**: `@suaveplan/api-plugin-rate-limit`
**Target path**: `api/data/api-plugin-rate-limit`
**Prerequisite packages on Verdaccio**: `@suaveplan/api`, `@suaveplan/api-shared`, `@suaveplan/api-plugin-auth`, `@suaveplan/rate-limiter`, `@suaveplan/events`, `@suaveplan/error`
---
## 0. Pre-flight (non-negotiable)
- [ ] 0.1 Confirm clean working tree (`git status` reports nothing) and no in-flight worktrees with uncommitted work (`git worktree list` then `git status` in each)
- [ ] 0.2 If any worktrees exist under `.claude/worktrees/`, account for each one: still-active agent, work-pending-merge, or stale-and-prune. Stale worktrees MUST be removed (`git worktree remove`) before opening a new one — code that has been lost twice on main is the failure mode this guards against.
- [ ] 0.3 Confirm the package set in `MANIFEST.md` matches `find packages -maxdepth 3 -name package.json` count. Drift here means a prior change archived without regenerating the manifest.
## 1. Scaffold + types + errors + events
- [ ] Create directory; `package.json` v0.0.0; standard tooling.
- [ ] Types: `RouteRateLimitOptions`, `RateLimitResult`, `RateLimitState`, `RateLimitPluginConfig` (Zod).
- [ ] Errors: 3-code union + registration. Test + doc.
- [ ] Events: `RateLimitEventMap` (rate-limit:exceeded, rate-limit:reset).
---
## 2. RateLimiterService
- [ ] `middleware(options)` — factory returning Hono `MiddlewareHandler`; extracts key via `keyExtractor`; calls `check`; on limit exceeded emits `rate-limit:exceeded`, returns 429 with `Retry-After` header.
- [ ] `check(key, limit, windowMs)` — delegates to `@suaveplan/rate-limiter`.
- [ ] `reset(key)` — admin; emits `rate-limit:reset`.
- [ ] `getState(key)` — returns current window count + remaining.
- [ ] OTel: `rate_limit.checks_total`, `rate_limit.exceeded_total` counters.
- [ ] `.test.ts` + `.md`.
---
## 3. Middleware factory
- [ ] `createRateLimitMiddleware(service, options)` — convenience wrapper.
- [ ] Per-tenant override: if `c.get('tenantId')` present, look up tenant-specific limit override from config.
- [ ] `.test.ts` + `.md`.
---
## 4. Admin routes
- [ ] `GET /admin/rate-limits/:key` — `requireAuth` + `requireRole("ADMIN")`.
- [ ] `DELETE /admin/rate-limits/:key` — reset; `requireAuth` + `requireRole("ADMIN")`.
- [ ] Route `.test.ts` + `.md`.
---
## 5. Plugin factory + integration tests + publish
- [ ] `createRateLimitPlugin`: `definePlugin`, `dependencies: ["auth"]`.
- [ ] `onInit`: instantiate `RateLimiterService` with config; register as `"rate-limiter"` service.
- [ ] Integration: middleware applied → 5 requests pass → 6th returns 429 → reset → pass again.
- [ ] `bunx turbo lint typecheck test --filter=@suaveplan/api-plugin-rate-limit` — green; 100% coverage.
- [ ] `bun scripts/publish.ts --package @suaveplan/api-plugin-rate-limit`.
- [ ] Archive → `openspec/archive/2026-04-24-enterprise-api-plugin-rate-limit/`.