|
| 1 | +# Body Limit Middleware |
| 2 | + |
| 3 | +> [!WARNING] This feature is available in the development version but not yet released. |
| 4 | +
|
| 5 | +> **Reference**: [RFC 7230 HTTP/1.1 Message Syntax and Routing](https://datatracker.ietf.org/doc/html/rfc7230#section-3.3.1) |
| 6 | +
|
| 7 | +Body Limit middleware enforces maximum request body size by checking the `Content-Length` header. Prevents large payloads from overwhelming your server. |
| 8 | + |
| 9 | +## Basic Usage |
| 10 | + |
| 11 | +Apply body limit middleware using Deserve's built-in middleware: |
| 12 | + |
| 13 | +```typescript |
| 14 | +import { Router, Mware } from '@neabyte/deserve' |
| 15 | + |
| 16 | +const router = new Router() |
| 17 | + |
| 18 | +router.use(Mware.bodyLimit({ |
| 19 | + limit: 1024 * 1024 // 1MB limit |
| 20 | +})) |
| 21 | + |
| 22 | +await router.serve(8000) |
| 23 | +``` |
| 24 | + |
| 25 | +## Route-Specific Limits |
| 26 | + |
| 27 | +Apply different body limits to specific routes: |
| 28 | + |
| 29 | +```typescript |
| 30 | +// 1MB limit for general routes |
| 31 | +router.use(Mware.bodyLimit({ limit: 1024 * 1024 })) |
| 32 | + |
| 33 | +// 5MB limit for upload routes |
| 34 | +router.use('/uploads', Mware.bodyLimit({ limit: 5 * 1024 * 1024 })) |
| 35 | + |
| 36 | +// 10MB limit for API routes |
| 37 | +router.use('/api', Mware.bodyLimit({ limit: 10 * 1024 * 1024 })) |
| 38 | +``` |
| 39 | + |
| 40 | +## Configuration Options |
| 41 | + |
| 42 | +### `limit` |
| 43 | + |
| 44 | +Maximum body size in bytes: |
| 45 | + |
| 46 | +```typescript |
| 47 | +// 1MB (1,048,576 bytes) |
| 48 | +limit: 1024 * 1024 |
| 49 | + |
| 50 | +// 5MB (5,242,880 bytes) |
| 51 | +limit: 5 * 1024 * 1024 |
| 52 | + |
| 53 | +// 10MB (10,485,760 bytes) |
| 54 | +limit: 10 * 1024 * 1024 |
| 55 | +``` |
| 56 | + |
| 57 | +## How It Works |
| 58 | + |
| 59 | +The middleware checks the `Content-Length` header before the body is read: |
| 60 | + |
| 61 | +1. **GET/HEAD requests** - Automatically skipped (no body) |
| 62 | +2. **Content-Length present** - Validates against limit |
| 63 | +3. **Transfer-Encoding present** - Passes through (chunked encoding) |
| 64 | +4. **No headers** - Passes through (size unknown) |
| 65 | + |
| 66 | +### RFC 7230 Compliance |
| 67 | + |
| 68 | +The middleware follows RFC 7230: |
| 69 | +- If both `Transfer-Encoding` and `Content-Length` are present, `Transfer-Encoding` takes precedence and body size is not validated |
| 70 | +- Only validates `Content-Length` when `Transfer-Encoding` is absent |
| 71 | +- Handles chunked encoding by passing through (can't check size upfront) |
| 72 | + |
| 73 | +## Complete Example |
| 74 | + |
| 75 | +```typescript |
| 76 | +import { Router, Mware } from '@neabyte/deserve' |
| 77 | + |
| 78 | +const router = new Router({ routesDir: './routes' }) |
| 79 | + |
| 80 | +// Global 1MB limit |
| 81 | +router.use(Mware.bodyLimit({ limit: 1024 * 1024 })) |
| 82 | + |
| 83 | +// 5MB for file uploads |
| 84 | +router.use('/uploads', Mware.bodyLimit({ limit: 5 * 1024 * 1024 })) |
| 85 | + |
| 86 | +// 10MB for API routes |
| 87 | +router.use('/api', Mware.bodyLimit({ limit: 10 * 1024 * 1024 })) |
| 88 | + |
| 89 | +await router.serve(8000) |
| 90 | +``` |
| 91 | + |
| 92 | +## Error Handling |
| 93 | + |
| 94 | +Body Limit automatically uses `router.catch()` if defined: |
| 95 | + |
| 96 | +```typescript |
| 97 | +router.catch((ctx, { statusCode, error }) => { |
| 98 | + if (statusCode === 413) { |
| 99 | + return ctx.send.json( |
| 100 | + { error: 'Request entity too large', message: error?.message }, |
| 101 | + { status: 413 } |
| 102 | + ) |
| 103 | + } |
| 104 | + return ctx.send.json({ |
| 105 | + error: error?.message ?? 'Unknown error' |
| 106 | + }, { status: statusCode }) |
| 107 | +}) |
| 108 | + |
| 109 | +router.use(Mware.bodyLimit({ limit: 1024 * 1024 })) |
| 110 | +``` |
| 111 | + |
| 112 | +When the limit is exceeded, the middleware returns message `Request entity too large` with `status code: 413` before the request body is read. |
0 commit comments