@@ -5,7 +5,6 @@ A high-performance, minimalist HTTP framework for [Bun](https://bun.sh/), inspir
5
5
## Key Benefits
6
6
7
7
- ** 🚀 Bun-Native Performance** : Optimized for Bun's runtime with minimal overhead
8
- - ** ⚡ Zero Dependencies** : Core framework uses only essential, lightweight dependencies
9
8
- ** 🔧 TypeScript First** : Full TypeScript support with comprehensive type definitions
10
9
- ** 🎯 Minimalist API** : Clean, intuitive API that's easy to learn and use
11
10
- ** 🔄 Middleware Support** : Flexible middleware system with async/await support
@@ -203,6 +202,42 @@ Bun.serve({
203
202
})
204
203
```
205
204
205
+ ## Middleware Support
206
+
207
+ 0http-bun includes a comprehensive middleware system with built-in middlewares for common use cases:
208
+
209
+ - ** [ Body Parser] ( ./lib/middleware/README.md#body-parser ) ** - Automatic request body parsing (JSON, form data, text)
210
+ - ** [ CORS] ( ./lib/middleware/README.md#cors ) ** - Cross-Origin Resource Sharing with flexible configuration
211
+ - ** [ JWT Authentication] ( ./lib/middleware/README.md#jwt-authentication ) ** - JSON Web Token authentication and authorization
212
+ - ** [ Logger] ( ./lib/middleware/README.md#logger ) ** - Request logging with multiple output formats
213
+ - ** [ Rate Limiting] ( ./lib/middleware/README.md#rate-limiting ) ** - Flexible rate limiting with sliding window support
214
+
215
+ ### Quick Example
216
+
217
+ ``` javascript
218
+ // Import middleware functions from the middleware module
219
+ const {
220
+ createCORS ,
221
+ createLogger ,
222
+ createBodyParser ,
223
+ createJWTAuth ,
224
+ createRateLimit ,
225
+ } = require (' 0http-bun/lib/middleware' )
226
+
227
+ const {router } = http ()
228
+
229
+ // Apply middleware stack
230
+ router .use (createCORS ()) // Enable CORS
231
+ router .use (createLogger ()) // Request logging
232
+ router .use (createBodyParser ()) // Parse request bodies
233
+ router .use (createRateLimit ({max: 100 })) // Rate limiting
234
+
235
+ // Protected routes
236
+ router .use (' /api/*' , createJWTAuth ({secret: process .env .JWT_SECRET }))
237
+ ```
238
+
239
+ 📖 ** [ Complete Middleware Documentation] ( ./lib/middleware/README.md ) **
240
+
206
241
### Error Handling
207
242
208
243
``` typescript
@@ -245,8 +280,9 @@ router.get('/api/risky', (req: ZeroRequest) => {
245
280
246
281
- ** Minimal overhead** : Direct use of Web APIs
247
282
- ** Efficient routing** : Based on the proven ` trouter ` library
248
- - ** Fast parameter parsing** : Optimized URL parameter extraction
249
- - ** Query string parsing** : Uses ` fast-querystring ` for performance
283
+ - ** Fast parameter parsing** : Optimized URL parameter extraction with caching
284
+ - ** Query string parsing** : Uses ` fast-querystring ` for optimal performance
285
+ - ** Memory efficient** : Route caching and object reuse to minimize allocations
250
286
251
287
### Benchmark Results
252
288
@@ -256,18 +292,51 @@ Run benchmarks with:
256
292
bun run bench
257
293
```
258
294
295
+ _ Performance characteristics will vary based on your specific use case and middleware stack._
296
+
259
297
## TypeScript Support
260
298
261
299
Full TypeScript support is included with comprehensive type definitions:
262
300
263
301
``` typescript
302
+ // Main framework types
264
303
import {
265
304
ZeroRequest ,
266
305
StepFunction ,
267
306
RequestHandler ,
268
307
IRouter ,
269
308
IRouterConfig ,
270
309
} from ' 0http-bun'
310
+
311
+ // Middleware-specific types
312
+ import {
313
+ LoggerOptions ,
314
+ JWTAuthOptions ,
315
+ APIKeyAuthOptions ,
316
+ RateLimitOptions ,
317
+ CORSOptions ,
318
+ BodyParserOptions ,
319
+ MemoryStore ,
320
+ } from ' 0http-bun/lib/middleware'
321
+
322
+ // Example typed middleware
323
+ const customMiddleware: RequestHandler = (
324
+ req : ZeroRequest ,
325
+ next : StepFunction ,
326
+ ) => {
327
+ req .ctx = req .ctx || {}
328
+ req .ctx .timestamp = Date .now ()
329
+ return next ()
330
+ }
331
+
332
+ // Example typed route handler
333
+ const typedHandler = (req : ZeroRequest ): Response => {
334
+ return Response .json ({
335
+ params: req .params ,
336
+ query: req .query ,
337
+ context: req .ctx ,
338
+ })
339
+ }
271
340
```
272
341
273
342
## License
0 commit comments