Refactor Study Desk application structure
This commit is contained in:
parent
faaccf8a7e
commit
089439ed90
145 changed files with 8087 additions and 3412 deletions
24
src/lib/limitedJson.ts
Normal file
24
src/lib/limitedJson.ts
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import type { NextRequest } from "next/server";
|
||||
import { CONTENT_LIMITS } from "@/lib/validation/contentSchemas";
|
||||
|
||||
export class RequestTooLargeError extends Error {}
|
||||
|
||||
export async function readLimitedJson(
|
||||
request: NextRequest,
|
||||
maximumBytes = CONTENT_LIMITS.requestBytes
|
||||
): Promise<unknown> {
|
||||
const declaredLength = Number(request.headers.get("content-length"));
|
||||
if (Number.isFinite(declaredLength) && declaredLength > maximumBytes) {
|
||||
throw new RequestTooLargeError(`Request body must not exceed ${maximumBytes} bytes`);
|
||||
}
|
||||
|
||||
const body = await request.text();
|
||||
if (new TextEncoder().encode(body).byteLength > maximumBytes) {
|
||||
throw new RequestTooLargeError(`Request body must not exceed ${maximumBytes} bytes`);
|
||||
}
|
||||
try {
|
||||
return JSON.parse(body) as unknown;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue