@lastshotlabs/bunshot 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CLAUDE.md +102 -0
- package/README.md +1458 -0
- package/bun.lock +170 -0
- package/package.json +47 -0
- package/src/adapters/memoryAuth.ts +240 -0
- package/src/adapters/mongoAuth.ts +91 -0
- package/src/adapters/sqliteAuth.ts +320 -0
- package/src/app.ts +368 -0
- package/src/cli.ts +265 -0
- package/src/index.ts +52 -0
- package/src/lib/HttpError.ts +5 -0
- package/src/lib/appConfig.ts +29 -0
- package/src/lib/authAdapter.ts +46 -0
- package/src/lib/authRateLimit.ts +104 -0
- package/src/lib/constants.ts +2 -0
- package/src/lib/context.ts +17 -0
- package/src/lib/emailVerification.ts +105 -0
- package/src/lib/fingerprint.ts +43 -0
- package/src/lib/jwt.ts +17 -0
- package/src/lib/logger.ts +9 -0
- package/src/lib/mongo.ts +70 -0
- package/src/lib/oauth.ts +114 -0
- package/src/lib/queue.ts +18 -0
- package/src/lib/redis.ts +45 -0
- package/src/lib/roles.ts +23 -0
- package/src/lib/session.ts +91 -0
- package/src/lib/validate.ts +14 -0
- package/src/lib/ws.ts +82 -0
- package/src/middleware/bearerAuth.ts +15 -0
- package/src/middleware/botProtection.ts +73 -0
- package/src/middleware/cacheResponse.ts +189 -0
- package/src/middleware/cors.ts +19 -0
- package/src/middleware/errorHandler.ts +14 -0
- package/src/middleware/identify.ts +36 -0
- package/src/middleware/index.ts +8 -0
- package/src/middleware/logger.ts +9 -0
- package/src/middleware/rateLimit.ts +37 -0
- package/src/middleware/requireRole.ts +42 -0
- package/src/middleware/requireVerifiedEmail.ts +31 -0
- package/src/middleware/userAuth.ts +9 -0
- package/src/models/AuthUser.ts +17 -0
- package/src/routes/auth.ts +245 -0
- package/src/routes/health.ts +27 -0
- package/src/routes/home.ts +21 -0
- package/src/routes/oauth.ts +174 -0
- package/src/schemas/auth.ts +14 -0
- package/src/server.ts +91 -0
- package/src/services/auth.ts +59 -0
- package/src/ws/index.ts +42 -0
- package/tsconfig.json +43 -0
package/CLAUDE.md
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
# CLAUDE.md
|
|
2
|
+
|
|
3
|
+
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
|
4
|
+
|
|
5
|
+
See @README for project overview
|
|
6
|
+
|
|
7
|
+
Update ReadMe and this file (CLAUDE.md) when adding, modifying, or deleting features to ensure proper documentation
|
|
8
|
+
|
|
9
|
+
## Commands
|
|
10
|
+
|
|
11
|
+
```bash
|
|
12
|
+
bun run dev # Watch mode (hot reload)
|
|
13
|
+
bun run start # Run without watch
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
There are no build, test, or lint scripts — this is a library package published to npm as `@lastshotlabs/bunshot`.
|
|
17
|
+
|
|
18
|
+
## Architecture
|
|
19
|
+
|
|
20
|
+
This is a **Bun + Hono API framework library** that consuming projects install as a dependency. It provides batteries-included scaffolding: auto-discovery of routes/workers, built-in auth, OpenAPI docs, WebSocket rooms, caching, and rate limiting.
|
|
21
|
+
|
|
22
|
+
### Entry Points
|
|
23
|
+
|
|
24
|
+
- **[src/app.ts](src/app.ts)** — `createApp(config)` factory: sets up Hono with CORS, middleware stack, auto-discovered routes, and OpenAPI/Scalar docs
|
|
25
|
+
- **[src/server.ts](src/server.ts)** — `createServer(config)` factory: wraps app with `Bun.serve()`, adds WebSocket upgrade handling, and auto-discovers BullMQ workers
|
|
26
|
+
- **[src/index.ts](src/index.ts)** — Barrel export for all public APIs
|
|
27
|
+
- **[src/cli.ts](src/cli.ts)** — CLI entry point
|
|
28
|
+
|
|
29
|
+
### Key Patterns
|
|
30
|
+
|
|
31
|
+
**Route auto-discovery:** Consuming projects place routes in a `routes/` directory. The framework scans and registers them automatically. Routes use `@hono/zod-openapi` for type-safe OpenAPI definitions. Load order can be controlled by exporting `export const priority = <number>` from a route file (lower = loaded first; files without it load last).
|
|
32
|
+
|
|
33
|
+
**Worker auto-discovery:** BullMQ workers are placed in a `workers/` directory and auto-started by `createServer`.
|
|
34
|
+
|
|
35
|
+
**Auth flow:** `src/lib/auth.ts` orchestrates login/register/logout/email-verification. The auth store is pluggable via `AuthAdapter` interface (default: `src/adapters/mongoAuth.ts`). Sessions can be stored in Redis, MongoDB, SQLite, or memory — configured via `db.sessions` in `CreateAppConfig`. Login identifier is configurable via `auth.primaryField` (`"email"` | `"username"` | `"phone"`). Email verification is opt-in via `auth.emailVerification` (supports `required: true` to block login until verified).
|
|
36
|
+
|
|
37
|
+
**Context extension:** The framework exposes a typed `AppContext` (Hono `Context`) that consuming apps extend with their own variables.
|
|
38
|
+
|
|
39
|
+
### Lib Layer (`src/lib/`)
|
|
40
|
+
|
|
41
|
+
| File | Purpose |
|
|
42
|
+
|------|---------|
|
|
43
|
+
| `mongo.ts` | Mongoose connection management |
|
|
44
|
+
| `redis.ts` | ioredis client |
|
|
45
|
+
| `jwt.ts` | `jose`-based JWT sign/verify |
|
|
46
|
+
| `session.ts` | Session CRUD — store set via `db.sessions` ("redis" \| "mongo" \| "sqlite" \| "memory") |
|
|
47
|
+
| `auth.ts` | Register/login/logout/password logic |
|
|
48
|
+
| `oauth.ts` | OAuth provider coordination via `arctic` — state store set via `db.oauthState` |
|
|
49
|
+
| `cache.ts` | Response cache — default store set via `db.cache`, overridable per-route; exports `bustCache` (all stores) and `bustCachePattern` (wildcard invalidation) |
|
|
50
|
+
| `rateLimit.ts` | Per-key rate limiting; exports `trackAttempt`, `isLimited`, `bustAuthLimit` for use in custom routes |
|
|
51
|
+
| `ws.ts` | WebSocket room registry, pub/sub helpers (`publish`, `subscribe`, `unsubscribe`, `getSubscriptions`, `handleRoomActions`, `getRooms`, `getRoomSubscribers`) — in-memory, no DB dependency |
|
|
52
|
+
|
|
53
|
+
### Middleware (`src/middleware/`)
|
|
54
|
+
|
|
55
|
+
- `bearerAuth` — API key validation via `Authorization: Bearer` header
|
|
56
|
+
- `identify` — Reads session and attaches user to context (non-blocking)
|
|
57
|
+
- `userAuth` — Requires authenticated user (blocks if not logged in)
|
|
58
|
+
- `requireRole` — RBAC role enforcement
|
|
59
|
+
- `requireVerifiedEmail` — Blocks access for users whose email has not been verified (requires `getEmailVerified` on adapter)
|
|
60
|
+
- `rateLimit` — Request rate limiting
|
|
61
|
+
- `cacheResponse` — Response caching with TTL
|
|
62
|
+
|
|
63
|
+
### Adapters (`src/adapters/`)
|
|
64
|
+
|
|
65
|
+
| File | Purpose |
|
|
66
|
+
|------|---------|
|
|
67
|
+
| `mongoAuth.ts` | Default `AuthAdapter` backed by Mongoose / MongoDB |
|
|
68
|
+
| `sqliteAuth.ts` | `AuthAdapter` + session/OAuth/cache helpers backed by `bun:sqlite`. Exports `setSqliteDb(path)` and `startSqliteCleanup()`. |
|
|
69
|
+
| `memoryAuth.ts` | `AuthAdapter` + session/OAuth/cache helpers backed by in-memory Maps. Exports `clearMemoryStore()` for test isolation. |
|
|
70
|
+
|
|
71
|
+
### Built-in Routes (`src/routes/`)
|
|
72
|
+
|
|
73
|
+
- `auth.ts` — `/auth/register`, `/auth/login`, `/auth/logout`, `/auth/set-password`, `/auth/me`, `/auth/verify-email`, `/auth/resend-verification`
|
|
74
|
+
- `oauth.ts` — OAuth initiation (`GET /auth/{provider}`), callbacks, link (`GET /auth/{provider}/link`), and unlink (`DELETE /auth/{provider}/link`) handlers
|
|
75
|
+
- `health.ts` — Health check
|
|
76
|
+
- `home.ts` — Root endpoint
|
|
77
|
+
|
|
78
|
+
### TypeScript Path Aliases
|
|
79
|
+
|
|
80
|
+
Defined in `tsconfig.json`, used throughout the codebase:
|
|
81
|
+
|
|
82
|
+
```
|
|
83
|
+
@lib/* → src/lib/*
|
|
84
|
+
@middleware/* → src/middleware/*
|
|
85
|
+
@models/* → src/models/*
|
|
86
|
+
@routes/* → src/routes/*
|
|
87
|
+
@workers/* → src/workers/*
|
|
88
|
+
@ws/* → src/ws/*
|
|
89
|
+
@schemas/* → src/schemas/*
|
|
90
|
+
@services/* → src/services/*
|
|
91
|
+
@queues/* → src/queues/*
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### WebSocket
|
|
95
|
+
|
|
96
|
+
`src/ws/index.ts` handles connection upgrades and authenticates via `getSession()` (supports all 4 store backends). Room management lives in `src/lib/ws.ts` using in-memory Maps + Bun's native pub/sub (`ws.subscribe`/`server.publish`).
|
|
97
|
+
|
|
98
|
+
### Environment Variables
|
|
99
|
+
|
|
100
|
+
See README.md for the full reference. All DB/auth vars are split by environment: `*_DEV` / `*_PROD` (selected by `NODE_ENV`). Key groups: `MONGO_*`, `REDIS_*`, `JWT_SECRET_*`, `BEARER_TOKEN_*`, `PORT`.
|
|
101
|
+
|
|
102
|
+
|