0http-bun 1.1.3 → 1.2.0
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/README.md +72 -3
- package/common.d.ts +37 -2
- package/lib/middleware/README.md +758 -0
- package/lib/middleware/body-parser.js +783 -0
- package/lib/middleware/cors.js +225 -0
- package/lib/middleware/index.d.ts +236 -0
- package/lib/middleware/index.js +45 -0
- package/lib/middleware/jwt-auth.js +406 -0
- package/lib/middleware/logger.js +310 -0
- package/lib/middleware/rate-limit.js +306 -0
- package/lib/router/sequential.js +10 -2
- package/package.json +6 -3
package/README.md
CHANGED
|
@@ -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,11 +292,14 @@ 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,
|
|
@@ -268,6 +307,36 @@ import {
|
|
|
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
|
package/common.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Pattern, Methods} from 'trouter'
|
|
2
|
+
import {Logger} from 'pino'
|
|
2
3
|
|
|
3
4
|
export interface IRouterConfig {
|
|
4
5
|
defaultRoute?: RequestHandler
|
|
@@ -8,10 +9,44 @@ export interface IRouterConfig {
|
|
|
8
9
|
|
|
9
10
|
export type StepFunction = (error?: unknown) => Response | Promise<Response>
|
|
10
11
|
|
|
11
|
-
|
|
12
|
+
export interface ParsedFile {
|
|
13
|
+
name: string
|
|
14
|
+
size: number
|
|
15
|
+
type: string
|
|
16
|
+
data: File
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export type ZeroRequest = Request & {
|
|
12
20
|
params: Record<string, string>
|
|
13
21
|
query: Record<string, string>
|
|
14
|
-
|
|
22
|
+
// Legacy compatibility properties (mirrored from ctx)
|
|
23
|
+
user?: any
|
|
24
|
+
jwt?: {
|
|
25
|
+
payload: any
|
|
26
|
+
header: any
|
|
27
|
+
token: string
|
|
28
|
+
}
|
|
29
|
+
apiKey?: string
|
|
30
|
+
// Context object for middleware data
|
|
31
|
+
ctx?: {
|
|
32
|
+
log?: Logger
|
|
33
|
+
user?: any
|
|
34
|
+
jwt?: {
|
|
35
|
+
payload: any
|
|
36
|
+
header: any
|
|
37
|
+
token: string
|
|
38
|
+
}
|
|
39
|
+
apiKey?: string
|
|
40
|
+
rateLimit?: {
|
|
41
|
+
limit: number
|
|
42
|
+
used: number
|
|
43
|
+
remaining: number
|
|
44
|
+
resetTime: Date
|
|
45
|
+
}
|
|
46
|
+
body?: any
|
|
47
|
+
files?: Record<string, ParsedFile | ParsedFile[]>
|
|
48
|
+
[key: string]: any
|
|
49
|
+
}
|
|
15
50
|
}
|
|
16
51
|
|
|
17
52
|
export type RequestHandler = (
|