* Parse JSON body with size limit
(request: Request)
| 81 | * Parse JSON body with size limit |
| 82 | */ |
| 83 | async function parseJSON(request: Request): Promise<unknown> { |
| 84 | const text = await request.text(); |
| 85 | |
| 86 | // Check size limit (1MB) |
| 87 | if (text.length > 1024 * 1024) { |
| 88 | throw new SecurityError( |
| 89 | SecurityErrorType.INVALID_INPUT, |
| 90 | 'Request body too large', |
| 91 | 413 |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | try { |
| 96 | return JSON.parse(text); |
| 97 | } catch { |
| 98 | throw new SecurityError( |
| 99 | SecurityErrorType.INVALID_INPUT, |
| 100 | 'Invalid JSON', |
| 101 | 400 |
| 102 | ); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Parse URL-encoded form data |