MCPcopy
hub / github.com/simstudioai/sim / readJsonBodyWithLimit

Function readJsonBodyWithLimit

apps/sim/lib/api/server/validation.ts:106–119  ·  view source on GitHub ↗

* Reads the JSON body while enforcing a byte cap. The body is read through a * size-limited stream so chunked/streamed bodies are bounded even when the * `content-length` header is absent or lies about the true size. When no * readable stream is available (e.g. a mocked request) the content-lengt

(request: Request, maxBytes: number)

Source from the content-addressed store, hash-verified

104 * "UTF-8 decode" behavior of `request.json()`.
105 */
106async function readJsonBodyWithLimit(request: Request, maxBytes: number): Promise<unknown> {
107 assertContentLengthWithinLimit(request.headers, maxBytes, REQUEST_BODY_LABEL)
108
109 const stream = request.body
110 if (!stream) {
111 return request.json()
112 }
113
114 const buffer = await readStreamToBufferWithLimit(stream, {
115 maxBytes,
116 label: REQUEST_BODY_LABEL,
117 })
118 return JSON.parse(new TextDecoder().decode(buffer))
119}
120
121export async function parseJsonBody(
122 request: Request,

Callers 1

parseJsonBodyFunction · 0.85

Calls 3

parseMethod · 0.80

Tested by

no test coverage detected