-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathproxy.ts
More file actions
27 lines (21 loc) · 710 Bytes
/
Copy pathproxy.ts
File metadata and controls
27 lines (21 loc) · 710 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
const DEFAULT_MAX_BYTES = 750 * 1024;
const MAX_BYTES = Number(process.env.API_BODY_LIMIT_BYTES) || DEFAULT_MAX_BYTES;
export function proxy(req: NextRequest) {
if (!["POST", "PUT", "PATCH"].includes(req.method))
return NextResponse.next();
const declared = req.headers.get("content-length");
if (!declared) return;
const size = Number(declared);
if (!Number.isFinite(size)) return;
if (size > MAX_BYTES) {
return NextResponse.json(
{ error: `Body payload too large (max ${MAX_BYTES} bytes)` },
{ status: 413 },
);
}
}
export const config = {
matcher: ["/api/:path*"],
};