@zero-server/sdk 0.9.0 → 0.9.2

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.
Files changed (126) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +460 -437
  3. package/index.js +414 -412
  4. package/lib/app.js +1172 -1172
  5. package/lib/auth/authorize.js +399 -399
  6. package/lib/auth/enrollment.js +367 -367
  7. package/lib/auth/index.js +57 -57
  8. package/lib/auth/jwt.js +731 -731
  9. package/lib/auth/oauth.js +362 -362
  10. package/lib/auth/session.js +588 -588
  11. package/lib/auth/trustedDevice.js +409 -409
  12. package/lib/auth/twoFactor.js +1150 -1150
  13. package/lib/auth/webauthn.js +946 -946
  14. package/lib/body/index.js +14 -14
  15. package/lib/body/json.js +109 -109
  16. package/lib/body/multipart.js +440 -440
  17. package/lib/body/raw.js +71 -71
  18. package/lib/body/rawBuffer.js +160 -160
  19. package/lib/body/sendError.js +25 -25
  20. package/lib/body/text.js +75 -75
  21. package/lib/body/typeMatch.js +41 -41
  22. package/lib/body/urlencoded.js +235 -235
  23. package/lib/cli.js +845 -845
  24. package/lib/cluster.js +666 -666
  25. package/lib/debug.js +372 -372
  26. package/lib/env/index.js +460 -460
  27. package/lib/errors.js +683 -683
  28. package/lib/fetch/index.js +256 -256
  29. package/lib/grpc/balancer.js +378 -378
  30. package/lib/grpc/call.js +708 -708
  31. package/lib/grpc/client.js +764 -764
  32. package/lib/grpc/codec.js +1221 -1221
  33. package/lib/grpc/credentials.js +398 -398
  34. package/lib/grpc/frame.js +262 -262
  35. package/lib/grpc/health.js +287 -287
  36. package/lib/grpc/index.js +121 -121
  37. package/lib/grpc/metadata.js +461 -461
  38. package/lib/grpc/proto.js +821 -821
  39. package/lib/grpc/reflection.js +590 -590
  40. package/lib/grpc/server.js +445 -445
  41. package/lib/grpc/status.js +118 -118
  42. package/lib/grpc/watch.js +173 -173
  43. package/lib/http/index.js +10 -10
  44. package/lib/http/request.js +727 -727
  45. package/lib/http/response.js +799 -799
  46. package/lib/lifecycle.js +557 -557
  47. package/lib/middleware/compress.js +230 -230
  48. package/lib/middleware/cookieParser.js +237 -237
  49. package/lib/middleware/cors.js +93 -93
  50. package/lib/middleware/csrf.js +136 -136
  51. package/lib/middleware/errorHandler.js +101 -101
  52. package/lib/middleware/helmet.js +175 -175
  53. package/lib/middleware/index.js +19 -17
  54. package/lib/middleware/logger.js +74 -74
  55. package/lib/middleware/rateLimit.js +88 -88
  56. package/lib/middleware/requestId.js +53 -53
  57. package/lib/middleware/static.js +326 -326
  58. package/lib/middleware/timeout.js +71 -71
  59. package/lib/middleware/validator.js +254 -254
  60. package/lib/observe/health.js +326 -326
  61. package/lib/observe/index.js +50 -50
  62. package/lib/observe/logger.js +359 -359
  63. package/lib/observe/metrics.js +805 -805
  64. package/lib/observe/tracing.js +592 -592
  65. package/lib/orm/adapters/json.js +290 -290
  66. package/lib/orm/adapters/memory.js +764 -764
  67. package/lib/orm/adapters/mongo.js +764 -764
  68. package/lib/orm/adapters/mysql.js +933 -933
  69. package/lib/orm/adapters/postgres.js +1144 -1144
  70. package/lib/orm/adapters/redis.js +1534 -1534
  71. package/lib/orm/adapters/sql-base.js +212 -212
  72. package/lib/orm/adapters/sqlite.js +858 -858
  73. package/lib/orm/audit.js +649 -649
  74. package/lib/orm/cache.js +394 -394
  75. package/lib/orm/geo.js +387 -387
  76. package/lib/orm/index.js +784 -784
  77. package/lib/orm/migrate.js +432 -432
  78. package/lib/orm/model.js +1706 -1706
  79. package/lib/orm/plugin.js +375 -375
  80. package/lib/orm/procedures.js +836 -836
  81. package/lib/orm/profiler.js +233 -233
  82. package/lib/orm/query.js +1772 -1772
  83. package/lib/orm/replicas.js +241 -241
  84. package/lib/orm/schema.js +307 -307
  85. package/lib/orm/search.js +380 -380
  86. package/lib/orm/seed/data/commerce.js +136 -136
  87. package/lib/orm/seed/data/internet.js +111 -111
  88. package/lib/orm/seed/data/locations.js +204 -204
  89. package/lib/orm/seed/data/names.js +338 -338
  90. package/lib/orm/seed/data/person.js +128 -128
  91. package/lib/orm/seed/data/phone.js +211 -211
  92. package/lib/orm/seed/data/words.js +134 -134
  93. package/lib/orm/seed/factory.js +178 -178
  94. package/lib/orm/seed/fake.js +1186 -1186
  95. package/lib/orm/seed/index.js +18 -18
  96. package/lib/orm/seed/rng.js +70 -70
  97. package/lib/orm/seed/seeder.js +124 -124
  98. package/lib/orm/seed/unique.js +68 -68
  99. package/lib/orm/snapshot.js +366 -366
  100. package/lib/orm/tenancy.js +605 -605
  101. package/lib/orm/views.js +350 -350
  102. package/lib/router/index.js +436 -436
  103. package/lib/sse/index.js +8 -8
  104. package/lib/sse/stream.js +349 -349
  105. package/lib/ws/connection.js +451 -451
  106. package/lib/ws/handshake.js +125 -125
  107. package/lib/ws/index.js +14 -14
  108. package/lib/ws/room.js +223 -223
  109. package/package.json +73 -73
  110. package/types/app.d.ts +223 -223
  111. package/types/auth.d.ts +520 -520
  112. package/types/cluster.d.ts +75 -75
  113. package/types/env.d.ts +80 -80
  114. package/types/errors.d.ts +316 -316
  115. package/types/fetch.d.ts +43 -43
  116. package/types/grpc.d.ts +432 -432
  117. package/types/index.d.ts +384 -384
  118. package/types/lifecycle.d.ts +60 -60
  119. package/types/middleware.d.ts +320 -320
  120. package/types/observe.d.ts +304 -304
  121. package/types/orm.d.ts +1887 -1887
  122. package/types/request.d.ts +109 -109
  123. package/types/response.d.ts +157 -157
  124. package/types/router.d.ts +78 -78
  125. package/types/sse.d.ts +78 -78
  126. package/types/websocket.d.ts +126 -126
package/README.md CHANGED
@@ -1,437 +1,460 @@
1
- <p align="center">
2
- <img src="documentation/public/icons/logo-animated.svg" alt="zero-server logo" width="300" height="300">
3
- </p>
4
-
5
- <h1 align="center">zero-server</h1>
6
-
7
- [![npm version](https://img.shields.io/npm/v/%40zero-server%2Fsdk.svg)](https://www.npmjs.com/package/@zero-server/sdk)
8
- [![npm downloads](https://img.shields.io/npm/dm/%40zero-server%2Fsdk.svg)](https://www.npmjs.com/package/@zero-server/sdk)
9
- [![GitHub](https://img.shields.io/badge/GitHub-zero--server-blue.svg)](https://github.com/tonywied17/zero-server)
10
- [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](https://opensource.org/licenses/MIT)
11
- [![Node.js](https://img.shields.io/badge/node-%3E%3D18-brightgreen.svg)](https://nodejs.org)
12
- [![Dependencies](https://img.shields.io/badge/dependencies-0-success.svg)](package.json)
13
- [![Tests](https://img.shields.io/badge/tests-7385%20passed-brightgreen.svg)](https://github.com/tonywied17/zero-server/actions)
14
- [![Coverage](https://img.shields.io/badge/coverage-97.01%25-brightgreen.svg)](https://github.com/tonywied17/zero-server)
15
-
16
- > **Zero-dependency backend framework for Node.js — routing, ORM, auth, WebSocket, SSE, observability, and 20+ middleware from a single `require`.**
17
-
18
- <p align="center">
19
- <strong>
20
- <a href="https://z-server.com">📖 Full Documentation &amp; Live Playground →</a>
21
- </strong>
22
- </p>
23
-
24
- ---
25
-
26
- ## Install
27
-
28
- ```bash
29
- npm install @zero-server/sdk
30
- ```
31
-
32
- Requires Node.js 18+. No external dependencies — everything is built on Node.js core APIs.
33
-
34
- ### Or install only what you need (scoped packages)
35
-
36
- `@zero-server/sdk` is the meta-package that re-exports every module. If you want a smaller install footprint, every section of the SDK is also published as its own scoped package and re-exports just that surface from the SDK:
37
-
38
- | Package | Surface |
39
- |---|---|
40
- | `@zero-server/core` | `createApp`, `Router`, `Request`, `Response` |
41
- | `@zero-server/body` | `json`, `urlencoded`, `text`, `raw`, `multipart` |
42
- | `@zero-server/middleware` | `cors`, `helmet`, `compress`, `rateLimit`, `logger`, `timeout`, `requestId`, `cookieParser`, `csrf`, `validate`, `errorHandler`, `static` |
43
- | `@zero-server/auth` | `jwt`, `session`, `oauth`, `authorize`, `twoFactor`, `webauthn`, `trustedDevice`, `enrollment` |
44
- | `@zero-server/orm` | `Database`, `Model`, `Query`, `TYPES`, migrations, seeders, replicas, search, geo, tenancy, audit |
45
- | `@zero-server/realtime` | `WebSocketConnection`, `WebSocketPool`, `SSEStream` |
46
- | `@zero-server/grpc` | gRPC server, client, codec, status, metadata, framing, health, reflection, balancer |
47
- | `@zero-server/observe` | `MetricsRegistry`, `Tracer`, structured `Logger`, health checks |
48
- | `@zero-server/lifecycle` | `LifecycleManager`, `ClusterManager`, `clusterize` |
49
- | `@zero-server/env` | typed `.env` loader |
50
- | `@zero-server/fetch` | server-side `fetch` client |
51
- | `@zero-server/errors` | every typed `HttpError` class plus ORM/framework errors |
52
-
53
- ```bash
54
- npm install @zero-server/core @zero-server/body @zero-server/middleware
55
- ```
56
-
57
- > All scoped packages depend on `@zero-server/sdk` and pin to the same version, so mixing-and-matching is safe.
58
-
59
- ---
60
-
61
- ## Quick Start
62
-
63
- ```js
64
- const { createApp, json } = require('@zero-server/sdk')
65
- const app = createApp()
66
-
67
- app.use(json())
68
- app.post('/echo', (req, res) => res.json({ received: req.body }))
69
- app.listen(3000, () => console.log('Listening on :3000'))
70
- ```
71
-
72
- ---
73
-
74
- ## Features
75
-
76
- ### Routing
77
-
78
- `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `all`, plus `Router()` sub-apps with `use()` mounting. Route chaining via `chain(path)`, route grouping via `group(prefix, ...mw, cb)`, wildcard & parameter patterns, and `param()` pre-processing. Full route introspection with `routes()`.
79
-
80
- ### Body Parsers
81
-
82
- `json()`, `urlencoded()`, `text()`, `raw()`, and `multipart()` with streaming file uploads, size limits, and progress tracking.
83
-
84
- ### Middleware
85
-
86
- 20+ built-in middleware — all zero-dependency:
87
-
88
- | Middleware | Purpose |
89
- |---|---|
90
- | `cors()` | Cross-origin resource sharing |
91
- | `helmet()` | Security headers |
92
- | `compress()` | Gzip, Brotli, and deflate compression |
93
- | `rateLimit()` | Per-IP request throttling |
94
- | `logger()` | Request logging with timing and colors |
95
- | `timeout()` | Request timeout enforcement |
96
- | `requestId()` | Unique request IDs |
97
- | `cookieParser()` | Cookie parsing with signed cookie support |
98
- | `csrf()` | CSRF token protection |
99
- | `validate()` | Schema-based request validation |
100
- | `errorHandler()` | Centralized error handling |
101
- | `static()` | Static file serving with ETags and HTTP/2 push |
102
-
103
- ### Authentication & Authorization
104
-
105
- Full auth stack with no external libraries:
106
-
107
- - **JWT** — `jwt()` middleware, `jwtSign()`, `jwtVerify()`, `jwtDecode()`, JWKS key sets, access/refresh token pairs
108
- - **Sessions** — `session()` middleware with in-memory store (pluggable)
109
- - **OAuth 2.0** — `oauth()` middleware with PKCE, pre-configured providers (Google, GitHub, Microsoft, etc.)
110
- - **Authorization** `authorize()` policies, `can()` / `canAny()` permission checks, `gate()` middleware
111
-
112
- ### ORM & Database
113
-
114
- Full-featured ORM with 7 adapters memory, JSON file, SQLite, MySQL, PostgreSQL, MongoDB, and Redis:
115
-
116
- ```js
117
- const { Database, Model, TYPES } = require('@zero-server/sdk')
118
-
119
- const db = Database.connect('sqlite', { filename: 'app.db' })
120
-
121
- class User extends Model {
122
- static table = 'users'
123
- static schema = {
124
- name: { type: TYPES.STRING, required: true },
125
- email: { type: TYPES.STRING, unique: true },
126
- }
127
- }
128
-
129
- db.register(User)
130
- await db.sync()
131
-
132
- await User.create({ name: 'Alice', email: 'alice@example.com' })
133
- const users = await User.find({ name: 'Alice' })
134
- ```
135
-
136
- **Query builder** — `where()`, `select()`, `orderBy()`, `limit()`, `offset()`, `join()`, `groupBy()`, `having()`, `paginate()`, `findOrCreate()`
137
-
138
- **Advanced ORM features:**
139
-
140
- | Feature | Description |
141
- |---|---|
142
- | Migrations | `Migrator` with up/down, rollback, and status tracking |
143
- | Seeding | `Seeder`, `Factory`, and `Fake` for test data generation |
144
- | Query caching | In-memory LRU cache with TTL and write-through invalidation |
145
- | Read replicas | `ReplicaManager` with automatic primary/replica routing |
146
- | Full-text search | `FullTextSearch` with indexing and ranked results |
147
- | Geo queries | `GeoQuery` with distance, bounding box, and nearest-neighbor |
148
- | Multi-tenancy | `TenantManager` with isolated per-tenant scoping |
149
- | Audit logging | `AuditLog` for change tracking with diffs and user attribution |
150
- | Schema snapshots | EF Core-style snapshot diffing with auto-generated migrations |
151
- | Query profiler | N+1 detection, slow query tracking, and execution analysis |
152
- | Views & procedures | `DatabaseView`, `StoredProcedure`, `StoredFunction`, `TriggerManager` |
153
- | Plugins | `PluginManager` for extending ORM behavior |
154
-
155
- ### Real-Time
156
-
157
- - **WebSocket** `app.ws(path, handler)` with RFC 6455, `WebSocketPool` for rooms, broadcasting, and sub-protocols
158
- - **Server-Sent Events** `res.sse()` with auto-IDs, named events, and keep-alive
159
-
160
- ### Observability
161
-
162
- Built-in Prometheus metrics, health checks, distributed tracing, and structured logging — zero dependencies.
163
-
164
- ```js
165
- const { createApp, metricsMiddleware } = require('@zero-server/sdk')
166
- const app = createApp()
167
-
168
- // Auto-instrument all HTTP requests (counters, histograms, active connections)
169
- app.use(metricsMiddleware({ registry: app.metrics() }))
170
-
171
- // Expose endpoints
172
- app.metricsEndpoint() // GET /metrics (Prometheus scrape target)
173
- app.health() // GET /healthz (liveness probe)
174
- app.ready() // GET /readyz (readiness probe)
175
-
176
- // Custom metrics
177
- const logins = app.metrics().counter({
178
- name: 'user_logins_total',
179
- help: 'Total login attempts',
180
- labels: ['provider'],
181
- })
182
- logins.inc({ provider: 'github' })
183
-
184
- app.listen(3000)
185
- ```
186
-
187
- **Scrape with Prometheus** — create a `prometheus.yml`:
188
-
189
- ```yaml
190
- scrape_configs:
191
- - job_name: 'my-app'
192
- scrape_interval: 5s
193
- static_configs:
194
- - targets: ['localhost:3000']
195
- ```
196
-
197
- ```bash
198
- docker run -d -p 9090:9090 -v ./prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
199
- # Open http://localhost:9090 → Graph → query http_requests_total
200
- ```
201
-
202
- **Also includes:**
203
- - **Distributed tracing** — `Tracer` and `Span` with W3C Trace Context (`traceparent` propagation), `instrumentFetch()` for outgoing requests
204
- - **Structured logging** — `Logger` with levels, JSON output, and namespaced `debug()` logger
205
-
206
- ### Lifecycle & Clustering
207
-
208
- - **Graceful shutdown** — signal handlers (SIGTERM/SIGINT), in-flight request draining, automatic WebSocket/SSE/database cleanup
209
- - **Clustering** — `clusterize()` for multi-worker processes with auto-respawn and exponential backoff
210
-
211
- ### CLI
212
-
213
- Scaffolding and database management via `npx zh`:
214
-
215
- ```bash
216
- npx zh migrate # run pending migrations
217
- npx zh migrate:rollback # rollback last migration
218
- npx zh migrate:status # show migration status
219
- npx zh seed # run seeders
220
- npx zh make:model User # scaffold a model
221
- npx zh make:migration name # create migration file
222
- npx zh make:seeder User # create seeder file
223
- ```
224
-
225
- ### Environment Config
226
-
227
- Typed `.env` loader with schema validation, multi-file support (`.env`, `.env.local`, `.env.{NODE_ENV}`), variable interpolation, and type coercion (string, number, boolean, integer, array, json, url, port, enum).
228
-
229
- ### HTTP Client
230
-
231
- Built-in `fetch()` with HTTPS/mTLS support, timeouts, `AbortSignal`, progress callbacks, and JSON/form/stream bodies.
232
-
233
- ### HTTPS & HTTP/2
234
-
235
- ```js
236
- app.listen(443, {
237
- key: fs.readFileSync('key.pem'),
238
- cert: fs.readFileSync('cert.pem'),
239
- }, () => console.log('HTTPS on 443'))
240
- ```
241
-
242
- Native HTTP/2 with automatic HTTP/1.1 fallback. `req.secure`, `req.protocol`, `ws.secure`, and `sse.secure` everywhere.
243
-
244
- ### Error Handling
245
-
246
- 20+ typed error classes (`NotFoundError`, `ValidationError`, `ForbiddenError`, `PayloadTooLargeError`, `DatabaseError`, `MigrationError`, etc.) plus `createError(status, message)` factory and `isHttpError()` check. Built-in CRLF injection prevention, prototype pollution filtering, path traversal guards, and filename sanitization.
247
-
248
- ### TypeScript
249
-
250
- Full type definitions for every API, middleware option, ORM model, auth flow, and plugin interface.
251
-
252
- ---
253
-
254
- ## Production Example
255
-
256
- ```js
257
- const path = require('path')
258
- const {
259
- createApp, Router, cors, json, urlencoded, compress,
260
- helmet, timeout, requestId, cookieParser, logger,
261
- static: serveStatic, rateLimit, jwt, session,
262
- Database, Model, TYPES, env, clusterize,
263
- WebSocketPool,
264
- } = require('@zero-server/sdk')
265
-
266
- env.load({
267
- PORT: { type: 'port', default: 3000 },
268
- JWT_SECRET: { type: 'string', required: true },
269
- DB_PATH: { type: 'string', default: './data.db' },
270
- })
271
-
272
- clusterize(() => {
273
- const app = createApp()
274
- const db = Database.connect('sqlite', { filename: env.DB_PATH })
275
-
276
- // Middleware stack
277
- app.use(helmet())
278
- app.use(logger())
279
- app.use(cors())
280
- app.use(compress())
281
- app.use(timeout(30_000))
282
- app.use(rateLimit())
283
- app.use(cookieParser())
284
- app.use(session({ secret: env.JWT_SECRET }))
285
- app.use(json())
286
- app.use(urlencoded())
287
- app.use(serveStatic(path.join(__dirname, 'public')))
288
-
289
- // Observability
290
- app.health()
291
- app.ready()
292
- app.metricsEndpoint()
293
-
294
- // API routes
295
- const api = Router()
296
- api.get('/health', (req, res) => res.json({ status: 'ok' }))
297
- api.get('/users/:id', jwt({ secret: env.JWT_SECRET }), (req, res) => {
298
- res.json({ id: req.params.id, user: req.user })
299
- })
300
- app.use('/api', api)
301
-
302
- // WebSocket
303
- const pool = new WebSocketPool()
304
- app.ws('/chat', (ws) => {
305
- pool.add(ws)
306
- pool.join(ws, 'lobby')
307
- ws.on('message', msg => pool.toRoom('lobby', msg, ws))
308
- })
309
-
310
- // SSE
311
- app.get('/events', (req, res) => {
312
- const sse = res.sse({ retry: 3000, autoId: true })
313
- sse.send('connected')
314
- })
315
-
316
- app.listen(env.PORT, () => console.log(`Worker ${process.pid} on :${env.PORT}`))
317
- })
318
- ```
319
-
320
- ---
321
-
322
- ## Exports
323
-
324
- All exports from the package root:
325
-
326
- ```js
327
- const {
328
- // Core
329
- createApp, Router, version,
330
-
331
- // Body parsers
332
- json, urlencoded, text, raw, multipart,
333
-
334
- // Middleware
335
- cors, helmet, compress, rateLimit, logger,
336
- timeout, requestId, cookieParser, csrf,
337
- validate, errorHandler, static: serveStatic,
338
-
339
- // Auth
340
- jwt, jwtSign, jwtVerify, jwtDecode, jwks, tokenPair,
341
- session, Session, MemoryStore,
342
- oauth, generatePKCE, generateState, OAUTH_PROVIDERS,
343
- authorize, can, canAny, Policy, gate,
344
-
345
- // ORM
346
- Database, Model, TYPES, Query,
347
- Migrator, defineMigration,
348
- Seeder, SeederRunner, Factory, Fake,
349
- QueryCache, QueryProfiler, ReplicaManager,
350
- FullTextSearch, GeoQuery, TenantManager, AuditLog,
351
- DatabaseView, StoredProcedure, StoredFunction, TriggerManager,
352
- PluginManager, buildSnapshot, diffSnapshots,
353
-
354
- // Observability
355
- Logger, structuredLogger,
356
- Counter, Gauge, Histogram, MetricsRegistry,
357
- metricsMiddleware, metricsEndpoint,
358
- Span, Tracer, tracingMiddleware, instrumentFetch,
359
- healthCheck, memoryCheck, eventLoopCheck, diskSpaceCheck,
360
-
361
- // Real-time
362
- WebSocketConnection, WebSocketPool, SSEStream,
363
-
364
- // Utilities
365
- fetch, env, debug,
366
- ClusterManager, clusterize,
367
- LifecycleManager, LIFECYCLE_STATE,
368
-
369
- // Errors
370
- HttpError, BadRequestError, UnauthorizedError,
371
- ForbiddenError, NotFoundError, ValidationError,
372
- ConflictError, PayloadTooLargeError, TooManyRequestsError,
373
- TimeoutError, DatabaseError, MigrationError,
374
- createError, isHttpError,
375
-
376
- // CLI
377
- CLI, runCLI,
378
- } = require('@zero-server/sdk')
379
- ```
380
-
381
- ---
382
-
383
- ## Documentation
384
-
385
- | Resource | Description |
386
- |---|---|
387
- | **[z-server.com](https://z-server.com)** | Interactive documentation with live playground, search, and examples |
388
- | **[API.md](API.md)** | Full API reference with tables, examples, and options for every export |
389
-
390
- ### Run docs locally
391
-
392
- ```bash
393
- cp documentation/.env.example documentation/.env
394
- npm run docs
395
- # open http://localhost:7273
396
- ```
397
-
398
- ---
399
-
400
- ## File Layout
401
-
402
- ```
403
- lib/
404
- app.js — App class (middleware, routing, listen, ws upgrade, lifecycle)
405
- auth/ — JWT, OAuth 2.0, sessions, and authorization policies
406
- body/ — body parsers (json, urlencoded, text, raw, multipart)
407
- cli.js — CLI runner (migrate, seed, scaffold commands)
408
- cluster.js — multi-worker clustering with auto-respawn
409
- debug.js — namespaced debug logger
410
- env/ — typed .env loader with schema validation
411
- errors.js — 20+ HttpError classes and factory
412
- fetch/ — HTTP/HTTPS client
413
- http/ — Request & Response wrappers
414
- lifecycle.js — graceful shutdown and lifecycle management
415
- middleware/ — cors, helmet, logger, rateLimit, compress, static, timeout,
416
- requestId, cookieParser, csrf, validate, errorHandler
417
- observe/ — Prometheus metrics, W3C tracing, health checks, structured logging
418
- orm/ — Database, Model, Query, adapters, migrations, seeds, cache,
419
- replicas, search, geo, tenancy, audit, views, procedures, plugins
420
- router/ Router with sub-app mounting and pattern matching
421
- sse/ SSE stream controller
422
- ws/ WebSocket connection, handshake, and room management
423
- types/ full TypeScript definitions
424
- documentation/ live demo server, controllers, and playground UI
425
- test/ vitest test suite (6000+ tests, 95%+ coverage)
426
- ```
427
-
428
- ## Testing
429
-
430
- ```bash
431
- npm test # vitest run (single pass)
432
- npm run test:watch # vitest (watch mode)
433
- ```
434
-
435
- ## License
436
-
437
- MIT
1
+ <p align="center">
2
+ <img src="website-docs/public/icons/logo-animated.svg" alt="zero-server logo" width="300" height="300">
3
+ </p>
4
+
5
+ <h1 align="center">zero-server</h1>
6
+
7
+ <p align="center">
8
+ <a href="https://www.npmjs.com/package/@zero-server/sdk"><img src="https://img.shields.io/badge/%40zero--server%2Fsdk-000?style=flat-square&logo=npm&logoColor=white" alt="@zero-server/sdk"></a>
9
+ <a href="https://www.npmjs.com/package/@zero-server/sdk"><img src="https://img.shields.io/npm/v/%40zero-server%2Fsdk?style=flat-square&logo=npm&logoColor=white&label=&color=00d8e0" alt="npm version"></a>
10
+ <a href="https://www.npmjs.com/package/@zero-server/sdk"><img src="https://img.shields.io/npm/dm/%40zero-server%2Fsdk?style=flat-square&logo=npm&logoColor=white&label=downloads&color=ff6b35" alt="npm downloads"></a>
11
+ </p>
12
+
13
+ <p align="center">
14
+ <a href="https://github.com/tonywied17/zero-server/actions"><img src="https://img.shields.io/github/actions/workflow/status/tonywied17/zero-server/ci.yml?branch=main&style=flat-square&logo=githubactions&logoColor=white&label=CI" alt="CI"></a>
15
+ <a href="https://github.com/tonywied17/zero-server/actions"><img src="https://img.shields.io/badge/tests-7443%20passing-brightgreen?style=flat-square&logo=vitest&logoColor=white" alt="tests"></a>
16
+ <a href="https://github.com/tonywied17/zero-server"><img src="https://img.shields.io/badge/coverage-55.35%25-yellow?style=flat-square&logo=vitest&logoColor=white" alt="coverage"></a>
17
+ <a href="https://z-server.dev"><img src="https://img.shields.io/badge/docs-z--server.dev-00d8e0?style=flat-square&logo=readthedocs&logoColor=white" alt="docs"></a>
18
+ <a href="https://opensource.org/licenses/MIT"><img src="https://img.shields.io/badge/license-MIT-00d8e0?style=flat-square&logo=opensourceinitiative&logoColor=white" alt="MIT"></a>
19
+ <a href="https://nodejs.org"><img src="https://img.shields.io/badge/node-%3E%3D18-brightgreen?style=flat-square&logo=nodedotjs&logoColor=white" alt="node >=18"></a>
20
+ </p>
21
+
22
+ > **Zero-dependency backend framework for Node.js — routing, ORM, auth, WebSocket, SSE, observability, and 20+ middleware from a single `require`.**
23
+
24
+ <p align="center">
25
+ <strong>
26
+ <a href="https://z-server.dev">📖 Full Documentation &amp; Live Playground →</a>
27
+ </strong>
28
+ </p>
29
+
30
+ ---
31
+
32
+ ## Install
33
+
34
+ ```bash
35
+ npm install @zero-server/sdk
36
+ ```
37
+
38
+ Requires Node.js 18+. No external dependencies — everything is built on Node.js core APIs.
39
+
40
+ ### Or install only what you need (scoped packages)
41
+
42
+ `@zero-server/sdk` is the meta-package that re-exports every module. If you want a smaller install footprint, every section of the SDK is also published as its own scoped package and re-exports just that surface from the SDK:
43
+
44
+ | Package | Surface |
45
+ |---|---|
46
+ | `@zero-server/core` | `createApp`, `Router`, `Request`, `Response` |
47
+ | `@zero-server/body` | `json`, `urlencoded`, `text`, `raw`, `multipart` |
48
+ | `@zero-server/middleware` | `cors`, `helmet`, `compress`, `rateLimit`, `logger`, `timeout`, `requestId`, `cookieParser`, `csrf`, `validate`, `errorHandler`, `static` |
49
+ | `@zero-server/auth` | `jwt`, `session`, `oauth`, `authorize`, `twoFactor`, `webauthn`, `trustedDevice`, `enrollment` |
50
+ | `@zero-server/orm` | `Database`, `Model`, `Query`, `TYPES`, migrations, seeders, replicas, search, geo, tenancy, audit |
51
+ | `@zero-server/realtime` | `WebSocketConnection`, `WebSocketPool`, `SSEStream` |
52
+ | `@zero-server/grpc` | gRPC server, client, codec, status, metadata, framing, health, reflection, balancer |
53
+ | `@zero-server/observe` | `MetricsRegistry`, `Tracer`, structured `Logger`, health checks |
54
+ | `@zero-server/lifecycle` | `LifecycleManager`, `ClusterManager`, `clusterize` |
55
+ | `@zero-server/env` | typed `.env` loader |
56
+ | `@zero-server/fetch` | server-side `fetch` client |
57
+ | `@zero-server/errors` | every typed `HttpError` class plus ORM/framework errors |
58
+ | `@zero-server/cli` | programmatic `CLI` / `runCLI` entry points for `zh` / `zs` |
59
+
60
+ ```bash
61
+ npm install @zero-server/core @zero-server/body @zero-server/middleware
62
+ ```
63
+
64
+ > All scoped packages depend on `@zero-server/sdk` and pin to the same version, so mixing-and-matching is safe.
65
+
66
+ ---
67
+
68
+ ## Quick Start
69
+
70
+ ```js
71
+ const { createApp, json } = require('@zero-server/sdk')
72
+ const app = createApp()
73
+
74
+ app.use(json())
75
+ app.post('/echo', (req, res) => res.json({ received: req.body }))
76
+ app.listen(3000, () => console.log('Listening on :3000'))
77
+ ```
78
+
79
+ ---
80
+
81
+ ## Features
82
+
83
+ ### Routing
84
+
85
+ `get`, `post`, `put`, `delete`, `patch`, `head`, `options`, `all`, plus `Router()` sub-apps with `use()` mounting. Route chaining via `chain(path)`, route grouping via `group(prefix, ...mw, cb)`, wildcard & parameter patterns, and `param()` pre-processing. Full route introspection with `routes()`.
86
+
87
+ ### Body Parsers
88
+
89
+ `json()`, `urlencoded()`, `text()`, `raw()`, and `multipart()` with streaming file uploads, size limits, and progress tracking.
90
+
91
+ ### Middleware
92
+
93
+ 20+ built-in middleware all zero-dependency:
94
+
95
+ | Middleware | Purpose |
96
+ |---|---|
97
+ | `cors()` | Cross-origin resource sharing |
98
+ | `helmet()` | Security headers |
99
+ | `compress()` | Gzip, Brotli, and deflate compression |
100
+ | `rateLimit()` | Per-IP request throttling |
101
+ | `logger()` | Request logging with timing and colors |
102
+ | `timeout()` | Request timeout enforcement |
103
+ | `requestId()` | Unique request IDs |
104
+ | `cookieParser()` | Cookie parsing with signed cookie support |
105
+ | `csrf()` | CSRF token protection |
106
+ | `validate()` | Schema-based request validation |
107
+ | `errorHandler()` | Centralized error handling |
108
+ | `static()` | Static file serving with ETags and HTTP/2 push |
109
+
110
+ ### Authentication & Authorization
111
+
112
+ Full auth stack with no external libraries:
113
+
114
+ - **JWT** `jwt()` middleware, `jwtSign()`, `jwtVerify()`, `jwtDecode()`, JWKS key sets, access/refresh token pairs
115
+ - **Sessions** — `session()` middleware with in-memory store (pluggable)
116
+ - **OAuth 2.0** — `oauth()` middleware with PKCE, pre-configured providers (Google, GitHub, Microsoft, etc.)
117
+ - **Authorization** `authorize()` policies, `can()` / `canAny()` permission checks, `gate()` middleware
118
+
119
+ ### ORM & Database
120
+
121
+ Full-featured ORM with 7 adapters — memory, JSON file, SQLite, MySQL, PostgreSQL, MongoDB, and Redis:
122
+
123
+ ```js
124
+ const { Database, Model, TYPES } = require('@zero-server/sdk')
125
+
126
+ const db = Database.connect('sqlite', { filename: 'app.db' })
127
+
128
+ class User extends Model {
129
+ static table = 'users'
130
+ static schema = {
131
+ name: { type: TYPES.STRING, required: true },
132
+ email: { type: TYPES.STRING, unique: true },
133
+ }
134
+ }
135
+
136
+ db.register(User)
137
+ await db.sync()
138
+
139
+ await User.create({ name: 'Alice', email: 'alice@example.com' })
140
+ const users = await User.find({ name: 'Alice' })
141
+ ```
142
+
143
+ **Query builder** `where()`, `select()`, `orderBy()`, `limit()`, `offset()`, `join()`, `groupBy()`, `having()`, `paginate()`, `findOrCreate()`
144
+
145
+ **Advanced ORM features:**
146
+
147
+ | Feature | Description |
148
+ |---|---|
149
+ | Migrations | `Migrator` with up/down, rollback, and status tracking |
150
+ | Seeding | `Seeder`, `Factory`, and `Fake` for test data generation |
151
+ | Query caching | In-memory LRU cache with TTL and write-through invalidation |
152
+ | Read replicas | `ReplicaManager` with automatic primary/replica routing |
153
+ | Full-text search | `FullTextSearch` with indexing and ranked results |
154
+ | Geo queries | `GeoQuery` with distance, bounding box, and nearest-neighbor |
155
+ | Multi-tenancy | `TenantManager` with isolated per-tenant scoping |
156
+ | Audit logging | `AuditLog` for change tracking with diffs and user attribution |
157
+ | Schema snapshots | EF Core-style snapshot diffing with auto-generated migrations |
158
+ | Query profiler | N+1 detection, slow query tracking, and execution analysis |
159
+ | Views & procedures | `DatabaseView`, `StoredProcedure`, `StoredFunction`, `TriggerManager` |
160
+ | Plugins | `PluginManager` for extending ORM behavior |
161
+
162
+ ### Real-Time
163
+
164
+ - **WebSocket** — `app.ws(path, handler)` with RFC 6455, `WebSocketPool` for rooms, broadcasting, and sub-protocols
165
+ - **Server-Sent Events** — `res.sse()` with auto-IDs, named events, and keep-alive
166
+
167
+ ### Observability
168
+
169
+ Built-in Prometheus metrics, health checks, distributed tracing, and structured logging — zero dependencies.
170
+
171
+ ```js
172
+ const { createApp, metricsMiddleware } = require('@zero-server/sdk')
173
+ const app = createApp()
174
+
175
+ // Auto-instrument all HTTP requests (counters, histograms, active connections)
176
+ app.use(metricsMiddleware({ registry: app.metrics() }))
177
+
178
+ // Expose endpoints
179
+ app.metricsEndpoint() // GET /metrics (Prometheus scrape target)
180
+ app.health() // GET /healthz (liveness probe)
181
+ app.ready() // GET /readyz (readiness probe)
182
+
183
+ // Custom metrics
184
+ const logins = app.metrics().counter({
185
+ name: 'user_logins_total',
186
+ help: 'Total login attempts',
187
+ labels: ['provider'],
188
+ })
189
+ logins.inc({ provider: 'github' })
190
+
191
+ app.listen(3000)
192
+ ```
193
+
194
+ **Scrape with Prometheus** — create a `prometheus.yml`:
195
+
196
+ ```yaml
197
+ scrape_configs:
198
+ - job_name: 'my-app'
199
+ scrape_interval: 5s
200
+ static_configs:
201
+ - targets: ['localhost:3000']
202
+ ```
203
+
204
+ ```bash
205
+ docker run -d -p 9090:9090 -v ./prometheus.yml:/etc/prometheus/prometheus.yml prom/prometheus
206
+ # Open http://localhost:9090 → Graph → query http_requests_total
207
+ ```
208
+
209
+ **Also includes:**
210
+ - **Distributed tracing** — `Tracer` and `Span` with W3C Trace Context (`traceparent` propagation), `instrumentFetch()` for outgoing requests
211
+ - **Structured logging** — `Logger` with levels, JSON output, and namespaced `debug()` logger
212
+
213
+ ### Lifecycle & Clustering
214
+
215
+ - **Graceful shutdown** — signal handlers (SIGTERM/SIGINT), in-flight request draining, automatic WebSocket/SSE/database cleanup
216
+ - **Clustering** `clusterize()` for multi-worker processes with auto-respawn and exponential backoff
217
+
218
+ ### CLI
219
+
220
+ Scaffolding and database management via `npx zh`:
221
+
222
+ ```bash
223
+ npx zh migrate # run pending migrations
224
+ npx zh migrate:rollback # rollback last migration
225
+ npx zh migrate:status # show migration status
226
+ npx zh seed # run seeders
227
+ npx zh make:model User # scaffold a model
228
+ npx zh make:migration name # create migration file
229
+ npx zh make:seeder User # create seeder file
230
+ ```
231
+
232
+ ### Environment Config
233
+
234
+ Typed `.env` loader with schema validation, multi-file support (`.env`, `.env.local`, `.env.{NODE_ENV}`), variable interpolation, and type coercion (string, number, boolean, integer, array, json, url, port, enum).
235
+
236
+ ### HTTP Client
237
+
238
+ Built-in `fetch()` with HTTPS/mTLS support, timeouts, `AbortSignal`, progress callbacks, and JSON/form/stream bodies.
239
+
240
+ ### HTTPS & HTTP/2
241
+
242
+ ```js
243
+ app.listen(443, {
244
+ key: fs.readFileSync('key.pem'),
245
+ cert: fs.readFileSync('cert.pem'),
246
+ }, () => console.log('HTTPS on 443'))
247
+ ```
248
+
249
+ Native HTTP/2 with automatic HTTP/1.1 fallback. `req.secure`, `req.protocol`, `ws.secure`, and `sse.secure` everywhere.
250
+
251
+ ### Error Handling
252
+
253
+ 20+ typed error classes (`NotFoundError`, `ValidationError`, `ForbiddenError`, `PayloadTooLargeError`, `DatabaseError`, `MigrationError`, etc.) plus `createError(status, message)` factory and `isHttpError()` check. Built-in CRLF injection prevention, prototype pollution filtering, path traversal guards, and filename sanitization.
254
+
255
+ ### TypeScript
256
+
257
+ Full type definitions for every API, middleware option, ORM model, auth flow, and plugin interface.
258
+
259
+ ---
260
+
261
+ ## Production Example
262
+
263
+ ```js
264
+ const path = require('path')
265
+ const {
266
+ createApp, Router, cors, json, urlencoded, compress,
267
+ helmet, timeout, requestId, cookieParser, logger,
268
+ static: serveStatic, rateLimit, jwt, session,
269
+ Database, Model, TYPES, env, clusterize,
270
+ WebSocketPool,
271
+ } = require('@zero-server/sdk')
272
+
273
+ env.load({
274
+ PORT: { type: 'port', default: 3000 },
275
+ JWT_SECRET: { type: 'string', required: true },
276
+ DB_PATH: { type: 'string', default: './data.db' },
277
+ })
278
+
279
+ clusterize(() => {
280
+ const app = createApp()
281
+ const db = Database.connect('sqlite', { filename: env.DB_PATH })
282
+
283
+ // Middleware stack
284
+ app.use(helmet())
285
+ app.use(logger())
286
+ app.use(cors())
287
+ app.use(compress())
288
+ app.use(timeout(30_000))
289
+ app.use(rateLimit())
290
+ app.use(cookieParser())
291
+ app.use(session({ secret: env.JWT_SECRET }))
292
+ app.use(json())
293
+ app.use(urlencoded())
294
+ app.use(serveStatic(path.join(__dirname, 'public')))
295
+
296
+ // Observability
297
+ app.health()
298
+ app.ready()
299
+ app.metricsEndpoint()
300
+
301
+ // API routes
302
+ const api = Router()
303
+ api.get('/health', (req, res) => res.json({ status: 'ok' }))
304
+ api.get('/users/:id', jwt({ secret: env.JWT_SECRET }), (req, res) => {
305
+ res.json({ id: req.params.id, user: req.user })
306
+ })
307
+ app.use('/api', api)
308
+
309
+ // WebSocket
310
+ const pool = new WebSocketPool()
311
+ app.ws('/chat', (ws) => {
312
+ pool.add(ws)
313
+ pool.join(ws, 'lobby')
314
+ ws.on('message', msg => pool.toRoom('lobby', msg, ws))
315
+ })
316
+
317
+ // SSE
318
+ app.get('/events', (req, res) => {
319
+ const sse = res.sse({ retry: 3000, autoId: true })
320
+ sse.send('connected')
321
+ })
322
+
323
+ app.listen(env.PORT, () => console.log(`Worker ${process.pid} on :${env.PORT}`))
324
+ })
325
+ ```
326
+
327
+ ---
328
+
329
+ ## Exports
330
+
331
+ All exports from the package root:
332
+
333
+ ```js
334
+ const {
335
+ // Core
336
+ createApp, Router, version,
337
+
338
+ // Body parsers
339
+ json, urlencoded, text, raw, multipart,
340
+
341
+ // Middleware
342
+ cors, helmet, compress, rateLimit, logger,
343
+ timeout, requestId, cookieParser, csrf,
344
+ validate, errorHandler, static: serveStatic,
345
+
346
+ // Auth
347
+ jwt, jwtSign, jwtVerify, jwtDecode, jwks, tokenPair,
348
+ session, Session, MemoryStore,
349
+ oauth, generatePKCE, generateState, OAUTH_PROVIDERS,
350
+ authorize, can, canAny, Policy, gate,
351
+
352
+ // ORM
353
+ Database, Model, TYPES, Query,
354
+ Migrator, defineMigration,
355
+ Seeder, SeederRunner, Factory, Fake,
356
+ QueryCache, QueryProfiler, ReplicaManager,
357
+ FullTextSearch, GeoQuery, TenantManager, AuditLog,
358
+ DatabaseView, StoredProcedure, StoredFunction, TriggerManager,
359
+ PluginManager, buildSnapshot, diffSnapshots,
360
+
361
+ // Observability
362
+ Logger, structuredLogger,
363
+ Counter, Gauge, Histogram, MetricsRegistry,
364
+ metricsMiddleware, metricsEndpoint,
365
+ Span, Tracer, tracingMiddleware, instrumentFetch,
366
+ healthCheck, memoryCheck, eventLoopCheck, diskSpaceCheck,
367
+
368
+ // Real-time
369
+ WebSocketConnection, WebSocketPool, SSEStream,
370
+
371
+ // gRPC
372
+ GrpcStatus, grpcToHttp, grpcStatusName, GRPC_STATUS_NAMES,
373
+ GrpcMetadata, ProtoWriter, ProtoReader, protoEncode, protoDecode,
374
+ parseProto, parseProtoFile, frameEncode, FrameParser,
375
+ GrpcServiceRegistry, GrpcClient,
376
+ GrpcHealthService, GrpcServingStatus, GrpcReflectionService,
377
+ GrpcLoadBalancer, GrpcSubchannel, GrpcSubchannelState,
378
+ ChannelCredentials, createRotatingCredentials, watchProto,
379
+
380
+ // Utilities
381
+ fetch, env, debug,
382
+ ClusterManager, clusterize,
383
+ LifecycleManager, LIFECYCLE_STATE,
384
+
385
+ // Errors
386
+ HttpError, BadRequestError, UnauthorizedError,
387
+ ForbiddenError, NotFoundError, ValidationError,
388
+ ConflictError, PayloadTooLargeError, TooManyRequestsError,
389
+ TimeoutError, DatabaseError, MigrationError,
390
+ createError, isHttpError,
391
+
392
+ // CLI
393
+ CLI, runCLI,
394
+ } = require('@zero-server/sdk')
395
+ ```
396
+
397
+ ---
398
+
399
+ ## Documentation
400
+
401
+ | Resource | Description |
402
+ |---|---|
403
+ | **[z-server.dev](https://z-server.dev)** | Interactive documentation with live playground, search, and examples |
404
+ | **[API.md](API.md)** | Full API reference with tables, examples, and options for every export |
405
+
406
+ ### Run docs locally
407
+
408
+ ```bash
409
+ cp website-docs/.env.example website-docs/.env
410
+ npm run docs
411
+ # open http://localhost:7273
412
+ ```
413
+
414
+ ---
415
+
416
+ ## File Layout
417
+
418
+ ```
419
+ lib/
420
+ app.js App class (middleware, routing, listen, ws upgrade, lifecycle)
421
+ auth/ JWT, OAuth 2.0, sessions, MFA (TOTP/WebAuthn), authorization
422
+ body/ body parsers (json, urlencoded, text, raw, multipart)
423
+ cli.js CLI runner (migrate, seed, scaffold commands)
424
+ cluster.js multi-worker clustering with auto-respawn
425
+ debug.js namespaced debug logger
426
+ env/ — typed .env loader with schema validation
427
+ errors.js — 25+ HttpError / framework / ORM error classes
428
+ fetch/ — HTTP/HTTPS client (mTLS, AbortSignal, retries)
429
+ grpc/ — HTTP/2 gRPC stack: server, client, codec, framing,
430
+ status, metadata, health, reflection, balancer, watch
431
+ http/ — Request & Response wrappers
432
+ lifecycle.js — graceful shutdown and lifecycle management
433
+ middleware/ — cors, helmet, logger, rateLimit, compress, static, timeout,
434
+ requestId, cookieParser, csrf, validate, errorHandler
435
+ observe/ — Prometheus metrics, W3C tracing, health checks, structured logging
436
+ orm/ — Database, Model, Query, adapters, migrations, seeds, cache,
437
+ replicas, search, geo, tenancy, audit, views, procedures, plugins
438
+ router/ — Router with sub-app mounting and pattern matching
439
+ sse/ — SSE stream controller
440
+ ws/ — WebSocket connection, handshake, and room management
441
+ packages/ — generated scoped @zero-server/* re-exports (one dir per scope)
442
+ .tools/
443
+ scope-manifest.js — single source of truth for scoped packages & their surface
444
+ generate-package-stubs.js
445
+ generate-scope-docs.js
446
+ types/ — full TypeScript definitions
447
+ website-docs/ — live demo server, controllers, and playground UI
448
+ test/ — vitest test suite (7000+ tests, 95%+ coverage)
449
+ ```
450
+
451
+ ## Testing
452
+
453
+ ```bash
454
+ npm test # vitest run (single pass)
455
+ npm run test:watch # vitest (watch mode)
456
+ ```
457
+
458
+ ## License
459
+
460
+ MIT