import{RequestBodySizeLimitError as m}from"../errors/request-body-size-limit.js";import{ResponseSizeLimitError as d}from"../errors/response-size-limit.js";async function h({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:r,value:e}=await t.read();if(r)break;e&&(a+=e.byteLength,a>s&&(await t.cancel("Stream size exceeded"),n()),o.push(e))}return Buffer.concat(o)}catch(r){if(r instanceof m||r instanceof d)throw r;try{await t?.cancel("Stream reading error occurred")}catch(e){throw new Error("Stream reading error occurred: "+e.message)}throw new Error("Stream reading error occurred: "+r.message)}}export{h as readStreamWithSizeLimit};
