import{RequestBodySizeLimitError as d,ResponseSizeLimitError as m}from"../errors.js";async function f({stream:i,maxSizeMB:c,onSizeExceededErrorHandler:n}){const s=c*1024*1024;let a=0;const o=[];let t=null;try{for(t=i.getReader();;){const{done:e,value:r}=await t.read();if(e)break;r&&(a+=r.byteLength,a>s&&(await t.cancel("Stream size exceeded"),n()),o.push(r))}return Buffer.concat(o)}catch(e){if(e instanceof d||e instanceof m)throw e;try{await t?.cancel("Stream reading error occurred")}catch(r){throw new Error("Stream reading error occurred: "+r.message)}throw new Error("Stream reading error occurred: "+e.message)}}export{f as readStreamWithSizeLimit};
