const e=require(`./paths-Bcdg3ma0.cjs`),t=require(`./paths-a2-BgYPx.cjs`),n=require(`./logger-ElHF9OXJ.cjs`),r=require(`./fetch-guard-CVK_RDgX.cjs`);let i=require(`node:path`);i=e.p(i);async function a(e,t){let n,r=!1;return await new Promise((i,a)=>{let o=()=>{n!==void 0&&(clearTimeout(n),n=void 0)};n=setTimeout(()=>{r=!0,o(),e.cancel().catch(()=>void 0),a(Error(`Media download stalled: no data received for ${t}ms`))},t),e.read().then(e=>{o(),r||i(e)},e=>{o(),r||a(e)})})}async function o(e,t,n){let r=n?.onOverflow??(e=>Error(`Content too large: ${e.size} bytes (limit: ${e.maxBytes} bytes)`)),i=n?.chunkTimeoutMs,o=e.body;if(!o||typeof o.getReader!=`function`){let n=Buffer.from(await e.arrayBuffer());if(n.length>t)throw r({size:n.length,maxBytes:t,res:e});return n}let s=o.getReader(),c=[],l=0;try{for(;;){let{done:n,value:o}=i?await a(s,i):await s.read();if(n)break;if(o?.length){if(l+=o.length,l>t){try{await s.cancel()}catch{}throw r({size:l,maxBytes:t,res:e})}c.push(o)}}}finally{try{s.releaseLock()}catch{}}return Buffer.concat(c.map(e=>Buffer.from(e)),l)}var s=class extends Error{constructor(e,t){super(t),this.code=e,this.name=`MediaFetchError`}};function c(e){return e.replace(/^["']|["']$/g,``)}function l(e){if(!e)return;let t=/filename\*\s*=\s*([^;]+)/i.exec(e);if(t?.[1]){let e=c(t[1].trim()),n=e.split(`''`).slice(1).join(`''`)||e;try{return i.default.basename(decodeURIComponent(n))}catch{return i.default.basename(n)}}let n=/filename\s*=\s*([^;]+)/i.exec(e);if(n?.[1])return i.default.basename(c(n[1].trim()))}async function u(e,t=200){try{let n=await e.text();if(!n)return;let r=n.replace(/\s+/g,` `).trim();return r?r.length<=t?r:`${r.slice(0,t)}…`:void 0}catch{return}}async function d(e){let{url:n,fetchImpl:a,requestInit:c,filePathHint:d,maxBytes:f,maxRedirects:p,readIdleTimeoutMs:m,ssrfPolicy:h,lookupFn:g}=e,_,v=n,y=null;try{let e=await r.t(r.n({url:n,fetchImpl:a,init:c,maxRedirects:p,policy:h,lookupFn:g}));_=e.response,v=e.finalUrl,y=e.release}catch(e){throw new s(`fetch_failed`,`Failed to fetch media from ${n}: ${String(e)}`)}try{if(!_.ok){let e=_.statusText?` ${_.statusText}`:``,t=v===n?``:` (redirected to ${v})`,r=`HTTP ${_.status}${e}`;if(!_.body)r=`HTTP ${_.status}${e}; empty response body`;else{let e=await u(_);e&&(r+=`; body: ${e}`)}throw new s(`http_error`,`Failed to fetch media from ${n}${t}: ${r}`)}let e=_.headers.get(`content-length`);if(f&&e){let t=Number(e);if(Number.isFinite(t)&&t>f)throw new s(`max_bytes`,`Failed to fetch media from ${n}: content length ${t} exceeds maxBytes ${f}`)}let r;try{r=f?await o(_,f,{onOverflow:({maxBytes:e,res:t})=>new s(`max_bytes`,`Failed to fetch media from ${t.url||n}: payload exceeds maxBytes ${e}`),chunkTimeoutMs:m}):Buffer.from(await _.arrayBuffer())}catch(e){throw e instanceof s?e:new s(`fetch_failed`,`Failed to fetch media from ${_.url||n}: ${String(e)}`)}let a;try{let e=new URL(v);a=i.default.basename(e.pathname)||void 0}catch{}let c=l(_.headers.get(`content-disposition`)),p=c||a||(d?i.default.basename(d):void 0),h=c&&i.default.extname(c)?c:d??v,g=await t.u({buffer:r,headerMime:_.headers.get(`content-type`),filePath:h});if(p&&!i.default.extname(p)&&g){let e=t.d(g);e&&(p=`${p}${e}`)}return{buffer:r,contentType:g??void 0,fileName:p}}finally{y&&await y()}}let f;function p(){return f||=n.Z(),f}function m(e,t={}){let n=i.default.resolve(e);return[t.preferredTmpDir??p(),i.default.join(n,`media`),i.default.join(n,`agents`),i.default.join(n,`workspace`),i.default.join(n,`sandboxes`)]}function h(){return m(e.c())}function g(n,r){let a=m(e.c());if(!r?.trim())return a;let o=t.W(n,r);if(!o)return a;let s=i.default.resolve(o);return a.includes(s)||a.push(s),a}Object.defineProperty(exports,`a`,{enumerable:!0,get:function(){return o}}),Object.defineProperty(exports,`i`,{enumerable:!0,get:function(){return d}}),Object.defineProperty(exports,`n`,{enumerable:!0,get:function(){return h}}),Object.defineProperty(exports,`r`,{enumerable:!0,get:function(){return s}}),Object.defineProperty(exports,`t`,{enumerable:!0,get:function(){return g}});
