import{c as b,e as f}from"./es6-D9GNxEuI.js";import"./index-DCNLpqIv.js";const{WritableStream:y,TransformStream:_,DOMException:P,Blob:m}=b,{GONE:v}=f,M=/constructor/i.test(window.HTMLElement);class E{constructor(e="unkown"){this.name=e,this.kind="file"}async getFile(){throw new P(...v)}async isSameEntry(e){return this===e}async createWritable(e={}){const t=await navigator.serviceWorker?.getRegistration(),a=document.createElement("a"),o=new _,u=o.writable;if(a.download=this.name,M||!t){let s=[];o.readable.pipeTo(new y({write(r){s.push(new m([r]))},close(){const r=new m(s,{type:"application/octet-stream; charset=utf-8"});s=[],a.href=URL.createObjectURL(r),a.click(),setTimeout(()=>URL.revokeObjectURL(a.href),1e4)}}))}else{const{writable:s,readablePort:r}=new C(y),n=encodeURIComponent(this.name).replace(/['()]/g,escape).replace(/\*/g,"%2A"),g={"content-disposition":"attachment; filename*=UTF-8''"+n,"content-type":"application/octet-stream; charset=utf-8",...e.size?{"content-length":e.size}:{}},w=setTimeout(()=>t.active.postMessage(0),1e4);o.readable.pipeThrough(new _({transform(c,l){if(c instanceof Uint8Array)return l.enqueue(c);const R=new Response(c).body.getReader(),d=S=>R.read().then(p=>p.done?0:d(l.enqueue(p.value)));return d()}})).pipeTo(s).finally(()=>{clearInterval(w)}),t.active.postMessage({url:t.scope+n,headers:g,readablePort:r},[r]);const i=document.createElement("iframe");i.hidden=!0,i.src=t.scope+n,document.body.appendChild(i)}return u.getWriter()}}const T=0,U=0,j=1,k=1,L=2;class O{constructor(e){e.onmessage=t=>this._onMessage(t.data),this._port=e,this._resetReady()}start(e){return this._controller=e,this._readyPromise}write(e){const t={type:T,chunk:e};return this._port.postMessage(t,[e.buffer]),this._resetReady(),this._readyPromise}close(){this._port.postMessage({type:L}),this._port.close()}abort(e){this._port.postMessage({type:k,reason:e}),this._port.close()}_onMessage(e){e.type===U&&this._resolveReady(),e.type===j&&this._onError(e.reason)}_onError(e){this._controller.error(e),this._rejectReady(e),this._port.close()}_resetReady(){this._readyPromise=new Promise((e,t)=>{this._readyResolve=e,this._readyReject=t}),this._readyPending=!0}_resolveReady(){this._readyResolve(),this._readyPending=!1}_rejectReady(e){this._readyPending||this._resetReady(),this._readyPromise.catch(()=>{}),this._readyReject(e),this._readyPending=!1}}class C{constructor(e){const t=new MessageChannel;this.readablePort=t.port1,this.writable=new e(new O(t.port2))}}export{E as FileHandle};
