export declare const requestBodyTemplate = "export class RequestTooLargeError extends Error {}\n\nexport async function readJsonBody(\n req: Request,\n maxBytes: number,\n): Promise {\n const contentLength = req.headers.get(\"content-length\");\n if (contentLength !== null && Number(contentLength) > maxBytes) {\n throw new RequestTooLargeError();\n }\n\n req.signal.throwIfAborted();\n if (!req.body) throw new SyntaxError(\"Missing body\");\n\n const reader = req.body.getReader();\n const chunks: Uint8Array[] = [];\n let size = 0;\n const abortRead = () => {\n void reader.cancel(req.signal.reason).catch(() => {});\n };\n req.signal.addEventListener(\"abort\", abortRead, { once: true });\n\n try {\n while (true) {\n req.signal.throwIfAborted();\n const { done, value } = await reader.read();\n req.signal.throwIfAborted();\n if (done) break;\n\n size += value.byteLength;\n if (size > maxBytes) {\n try {\n await reader.cancel();\n } catch {\n // The request may have been aborted while the oversized body arrived.\n }\n throw new RequestTooLargeError();\n }\n chunks.push(value);\n }\n\n const bytes = new Uint8Array(size);\n let offset = 0;\n for (const chunk of chunks) {\n bytes.set(chunk, offset);\n offset += chunk.byteLength;\n }\n\n const text = new TextDecoder(\"utf-8\", { fatal: true }).decode(bytes);\n return JSON.parse(text);\n } finally {\n req.signal.removeEventListener(\"abort\", abortRead);\n reader.releaseLock();\n }\n}\n";