[{"data":1,"prerenderedAt":601},["ShallowReactive",2],{"navigation":3,"-guide-body-limit":61,"-guide-body-limit-surround":596},[4],{"title":5,"path":6,"stem":7,"children":8,"icon":10},"Getting Started","/guide","1.guide/1.index",[9,11,16,21,26,31,36,41,46,51,56],{"title":5,"path":6,"stem":7,"icon":10},"ph:book-open-duotone",{"title":12,"path":13,"stem":14,"icon":15},"Using CLI","/guide/cli","1.guide/10.cli","garden:terminal-cli-stroke-12",{"title":17,"path":18,"stem":19,"icon":20},"Body Size Limiting","/guide/body-limit","1.guide/11.body-limit","ri:scales-3-line",{"title":22,"path":23,"stem":24,"icon":25},"Fetch Handler","/guide/handler","1.guide/2.handler","i-fluent:target-24-regular",{"title":27,"path":28,"stem":29,"icon":30},"Server Instance","/guide/server","1.guide/3.server","radix-icons:component-instance",{"title":32,"path":33,"stem":34,"icon":35},"Middleware","/guide/middleware","1.guide/4.middleware","clarity:plugin-line",{"title":37,"path":38,"stem":39,"icon":40},"Server Options","/guide/options","1.guide/5.options","ri:settings-3-line",{"title":42,"path":43,"stem":44,"icon":45},"TLS","/guide/tls","1.guide/6.tls","ri:lock-2-line",{"title":47,"path":48,"stem":49,"icon":50},"Bundler Usage","/guide/bundler","1.guide/7.bundler","clarity:bundle-line",{"title":52,"path":53,"stem":54,"icon":55},"Node.js Support","/guide/node","1.guide/8.node","akar-icons:node-fill",{"title":57,"path":58,"stem":59,"icon":60},"AWS Lambda","/guide/aws-lambda","1.guide/9.aws-lambda","clarity:cloud-traffic-line",{"id":62,"title":17,"body":63,"description":590,"extension":591,"meta":592,"navigation":593,"path":18,"seo":594,"stem":19,"__hash__":595},"content/1.guide/11.body-limit.md",{"type":64,"value":65,"toc":585,"icon":20},"minimark",[66,88,91,104,111,118,193,208,214,221,296,324,335,339,354,407,581],[67,68,69,70,78,79,82,83,87],"p",{},"The ",[71,72,74],"a",{"href":73},"/guide/options#maxrequestbodysize",[75,76,77],"code",{},"maxRequestBodySize"," option enforces a body size limit for the whole server. The same building blocks are exported from ",[75,80,81],{},"srvx/body-limit"," so downstream layers (for example per-route or per-handler limits) can enforce the ",[84,85,86],"strong",{},"same streaming semantics and error shape"," without re-implementing them.",[67,89,90],{},"These helpers use only web streams (no Node.js APIs), so they run on any runtime.",[92,93,94],"note",{},[67,95,96,97,99,100,103],{},"\nOn Bun the server-level ",[75,98,77],{}," is enforced natively by ",[75,101,102],{},"Bun.serve",". These helpers still work anywhere for your own limits, but the srvx Node and Deno adapters are the ones that use them internally.",[105,106,108],"h2",{"id":107},"limitbodystream",[75,109,110],{},"limitBodyStream",[67,112,113,114,117],{},"Wraps a body ",[75,115,116],{},"ReadableStream\u003CUint8Array>"," so the total number of bytes read cannot exceed the limit, returning a new stream.",[119,120,125],"pre",{"className":121,"code":122,"language":123,"meta":124,"style":124},"language-js shiki shiki-themes github-light github-dark github-dark","import { limitBodyStream } from \"srvx/body-limit\";\n\nconst limited = limitBodyStream(request.body, 1024 * 1024 /* 1 MiB */);\n","js","",[75,126,127,150,157],{"__ignoreMap":124},[128,129,132,136,140,143,147],"span",{"class":130,"line":131},"line",1,[128,133,135],{"class":134},"so5gQ","import",[128,137,139],{"class":138},"slsVL"," { limitBodyStream } ",[128,141,142],{"class":134},"from",[128,144,146],{"class":145},"sfrk1"," \"srvx/body-limit\"",[128,148,149],{"class":138},";\n",[128,151,153],{"class":130,"line":152},2,[128,154,156],{"emptyLinePlaceholder":155},true,"\n",[128,158,160,163,167,170,174,177,180,183,186,190],{"class":130,"line":159},3,[128,161,162],{"class":134},"const",[128,164,166],{"class":165},"suiK_"," limited",[128,168,169],{"class":134}," =",[128,171,173],{"class":172},"shcOC"," limitBodyStream",[128,175,176],{"class":138},"(request.body, ",[128,178,179],{"class":165},"1024",[128,181,182],{"class":134}," *",[128,184,185],{"class":165}," 1024",[128,187,189],{"class":188},"sCsY4"," /* 1 MiB */",[128,191,192],{"class":138},");\n",[67,194,195,196,199,200,207],{},"It is ",[84,197,198],{},"pull-based",": it reads from the upstream stream only when the consumer pulls, so it preserves backpressure and never buffers the whole body. As soon as the accumulated size passes the limit, the wrapped stream errors with a ",[71,201,203,206],{"href":202},"#error-shape",[75,204,205],{},"413","-style error"," and the upstream stream is cancelled with that same error, so the source can stop producing and release the socket. Cancelling the wrapped stream propagates the cancellation upstream.",[105,209,211],{"id":210},"limitrequestbody",[75,212,213],{},"limitRequestBody",[67,215,216,217,220],{},"Returns a ",[75,218,219],{},"Request"," whose body is size-limited. A request without a body is returned unchanged; otherwise it is rebuilt with a size-limited body stream (method, url, headers and signal are preserved).",[119,222,224],{"className":121,"code":223,"language":123,"meta":124,"style":124},"import { limitRequestBody } from \"srvx/body-limit\";\n\nconst safeRequest = limitRequestBody(request, 1024 * 1024 /* 1 MiB */);\n// reading the body throws the 413-style error if it exceeds the limit\nconst data = await safeRequest.json();\n",[75,225,226,239,243,268,274],{"__ignoreMap":124},[128,227,228,230,233,235,237],{"class":130,"line":131},[128,229,135],{"class":134},[128,231,232],{"class":138}," { limitRequestBody } ",[128,234,142],{"class":134},[128,236,146],{"class":145},[128,238,149],{"class":138},[128,240,241],{"class":130,"line":152},[128,242,156],{"emptyLinePlaceholder":155},[128,244,245,247,250,252,255,258,260,262,264,266],{"class":130,"line":159},[128,246,162],{"class":134},[128,248,249],{"class":165}," safeRequest",[128,251,169],{"class":134},[128,253,254],{"class":172}," limitRequestBody",[128,256,257],{"class":138},"(request, ",[128,259,179],{"class":165},[128,261,182],{"class":134},[128,263,185],{"class":165},[128,265,189],{"class":188},[128,267,192],{"class":138},[128,269,271],{"class":130,"line":270},4,[128,272,273],{"class":188},"// reading the body throws the 413-style error if it exceeds the limit\n",[128,275,277,279,282,284,287,290,293],{"class":130,"line":276},5,[128,278,162],{"class":134},[128,280,281],{"class":165}," data",[128,283,169],{"class":134},[128,285,286],{"class":134}," await",[128,288,289],{"class":138}," safeRequest.",[128,291,292],{"class":172},"json",[128,294,295],{"class":138},"();\n",[67,297,298,299,302,303,306,307,309,310,313,314,313,317,313,320,323],{},"When the request declares a ",[75,300,301],{},"Content-Length"," that already exceeds the limit, the body is ",[84,304,305],{},"rejected early",": the original body is cancelled without being read and the returned request's body errors immediately. ",[75,308,301],{}," is only a fast path — it may be absent (chunked transfer encoding) or understated, so the streaming limit is always enforced regardless. The error surfaces when the body is consumed (",[75,311,312],{},"request.text()"," / ",[75,315,316],{},".json()",[75,318,319],{},".arrayBuffer()",[75,321,322],{},".body","), matching the streamed-limit behaviour.",[92,325,326],{},[67,327,328,329,331,332,334],{},"\nA request whose ",[75,330,301],{}," overstates the actual body (e.g. it declares more bytes than it sends) is rejected on the declared length, even if the bytes that follow would fit — an overstated ",[75,333,301],{}," is a malformed request, and this matches how servers such as Bun and nginx enforce their limits.",[105,336,338],{"id":337},"error-shape","Error shape",[67,340,341,342,345,346,349,350,353],{},"Both helpers reject with the canonical error created by ",[75,343,344],{},"createBodyTooLargeError"," (typed as the exported ",[75,347,348],{},"BodyTooLargeError","). It carries a stable, documented shape so any layer can map it to an HTTP ",[75,351,352],{},"413 Payload Too Large"," response without string matching:",[355,356,357,370],"table",{},[358,359,360],"thead",{},[361,362,363,367],"tr",{},[364,365,366],"th",{},"Property",[364,368,369],{},"Value",[371,372,373,385,396],"tbody",{},[361,374,375,380],{},[376,377,378],"td",{},[75,379,75],{},[376,381,382],{},[75,383,384],{},"\"ERR_BODY_TOO_LARGE\"",[361,386,387,392],{},[376,388,389],{},[75,390,391],{},"statusCode",[376,393,394],{},[75,395,205],{},[361,397,398,403],{},[376,399,400],{},[75,401,402],{},"status",[376,404,405],{},[75,406,205],{},[119,408,410],{"className":121,"code":409,"language":123,"meta":124,"style":124},"import { createBodyTooLargeError, limitRequestBody } from \"srvx/body-limit\";\n\nconst safeRequest = limitRequestBody(request, 1024 * 1024);\ntry {\n  return Response.json(await safeRequest.json());\n} catch (error) {\n  if (error.code === \"ERR_BODY_TOO_LARGE\") {\n    return new Response(\"Payload Too Large\", { status: error.statusCode });\n  }\n  throw error;\n}\n\n// Or construct the error directly to enforce a limit elsewhere:\nthrow createBodyTooLargeError(1024 * 1024);\n",[75,411,412,425,429,449,457,480,492,510,530,536,545,551,556,562],{"__ignoreMap":124},[128,413,414,416,419,421,423],{"class":130,"line":131},[128,415,135],{"class":134},[128,417,418],{"class":138}," { createBodyTooLargeError, limitRequestBody } ",[128,420,142],{"class":134},[128,422,146],{"class":145},[128,424,149],{"class":138},[128,426,427],{"class":130,"line":152},[128,428,156],{"emptyLinePlaceholder":155},[128,430,431,433,435,437,439,441,443,445,447],{"class":130,"line":159},[128,432,162],{"class":134},[128,434,249],{"class":165},[128,436,169],{"class":134},[128,438,254],{"class":172},[128,440,257],{"class":138},[128,442,179],{"class":165},[128,444,182],{"class":134},[128,446,185],{"class":165},[128,448,192],{"class":138},[128,450,451,454],{"class":130,"line":270},[128,452,453],{"class":134},"try",[128,455,456],{"class":138}," {\n",[128,458,459,462,465,467,470,473,475,477],{"class":130,"line":276},[128,460,461],{"class":134},"  return",[128,463,464],{"class":138}," Response.",[128,466,292],{"class":172},[128,468,469],{"class":138},"(",[128,471,472],{"class":134},"await",[128,474,289],{"class":138},[128,476,292],{"class":172},[128,478,479],{"class":138},"());\n",[128,481,483,486,489],{"class":130,"line":482},6,[128,484,485],{"class":138},"} ",[128,487,488],{"class":134},"catch",[128,490,491],{"class":138}," (error) {\n",[128,493,495,498,501,504,507],{"class":130,"line":494},7,[128,496,497],{"class":134},"  if",[128,499,500],{"class":138}," (error.code ",[128,502,503],{"class":134},"===",[128,505,506],{"class":145}," \"ERR_BODY_TOO_LARGE\"",[128,508,509],{"class":138},") {\n",[128,511,513,516,519,522,524,527],{"class":130,"line":512},8,[128,514,515],{"class":134},"    return",[128,517,518],{"class":134}," new",[128,520,521],{"class":172}," Response",[128,523,469],{"class":138},[128,525,526],{"class":145},"\"Payload Too Large\"",[128,528,529],{"class":138},", { status: error.statusCode });\n",[128,531,533],{"class":130,"line":532},9,[128,534,535],{"class":138},"  }\n",[128,537,539,542],{"class":130,"line":538},10,[128,540,541],{"class":134},"  throw",[128,543,544],{"class":138}," error;\n",[128,546,548],{"class":130,"line":547},11,[128,549,550],{"class":138},"}\n",[128,552,554],{"class":130,"line":553},12,[128,555,156],{"emptyLinePlaceholder":155},[128,557,559],{"class":130,"line":558},13,[128,560,561],{"class":188},"// Or construct the error directly to enforce a limit elsewhere:\n",[128,563,565,568,571,573,575,577,579],{"class":130,"line":564},14,[128,566,567],{"class":134},"throw",[128,569,570],{"class":172}," createBodyTooLargeError",[128,572,469],{"class":138},[128,574,179],{"class":165},[128,576,182],{"class":134},[128,578,185],{"class":165},[128,580,192],{"class":138},[582,583,584],"style",{},"html pre.shiki code .so5gQ, html code.shiki .so5gQ{--shiki-light:#D73A49;--shiki-default:#F97583;--shiki-dark:#F97583}html pre.shiki code .slsVL, html code.shiki .slsVL{--shiki-light:#24292E;--shiki-default:#E1E4E8;--shiki-dark:#E1E4E8}html pre.shiki code .sfrk1, html code.shiki .sfrk1{--shiki-light:#032F62;--shiki-default:#9ECBFF;--shiki-dark:#9ECBFF}html pre.shiki code .suiK_, html code.shiki .suiK_{--shiki-light:#005CC5;--shiki-default:#79B8FF;--shiki-dark:#79B8FF}html pre.shiki code .shcOC, html code.shiki .shcOC{--shiki-light:#6F42C1;--shiki-default:#B392F0;--shiki-dark:#B392F0}html pre.shiki code .sCsY4, html code.shiki .sCsY4{--shiki-light:#6A737D;--shiki-default:#6A737D;--shiki-dark:#6A737D}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":124,"searchDepth":152,"depth":152,"links":586},[587,588,589],{"id":107,"depth":152,"text":110},{"id":210,"depth":152,"text":213},{"id":337,"depth":152,"text":338},"Reusable, runtime-agnostic helpers for enforcing request body size limits.","md",{"icon":20},{"icon":20},{"title":17,"description":590},"b28vVBiA_Dupjg_oVxJ29IJNCDx5-xc5DxpOX_k9wC4",[597,599],{"title":12,"path":13,"stem":14,"description":598,"icon":15,"children":-1},"Use the srvx CLI command to easily start a development or production server.",{"title":22,"path":23,"stem":24,"description":600,"icon":25,"children":-1},"Get familiar with srvx fetch server handler and ServerRequest.",1784719519561]