mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-27 07:52:24 +00:00
The management primitives, self-contained + unit-tested. Not yet wired into the
S3-shaped orphan-GC / delete-flow (that reconcile system assumes bucket/key
semantics — its own browser-tested increment, same pattern as the upload).
- Vendor entry: export createDeleteAuth (+ rebuilt bundle, still ~27KB).
- blossomService.deleteFile({ server, sha256, signer }) — BUD-12 DELETE /<sha256>
with a signed kind-24242 delete auth (BUD-11).
- blossomService.listFiles({ server, pubkey, signer }) — BUD-12 GET /list/<pubkey>,
optional list auth.
Tests: blossom-service 9/9 (added delete/list guards + happy paths). Lint + build
clean.
REMAINING Phase 2: wire deleteFile into video-delete cleanup (resolve
server+sha256 from the blossom storagePointer), orphan-GC parity, and the
kind-10063 discovery UI (import/publish server list — needs relay fetch/publish
plumbing).
Co-Authored-By: Claude Opus 4.8 <[email protected]>
7 lines
26 KiB
JavaScript
7 lines
26 KiB
JavaScript
var _t=Object.defineProperty;var ue=(n,e,t)=>e in n?_t(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var $=(n,e)=>()=>(n&&(e=n(n=0)),e);var St=(n,e)=>{for(var t in e)_t(n,t,{get:e[t],enumerable:!0})};var Q=(n,e,t)=>ue(n,typeof e!="symbol"?e+"":e,t);function he(n){return n instanceof Uint8Array||ArrayBuffer.isView(n)&&n.constructor.name==="Uint8Array"}function Y(n,...e){if(!he(n))throw new Error("Uint8Array expected");if(e.length>0&&!e.includes(n.length))throw new Error("Uint8Array expected of length "+e+", got length="+n.length)}function Bt(n,e=!0){if(n.destroyed)throw new Error("Hash instance has been destroyed");if(e&&n.finished)throw new Error("Hash#digest() has already been called")}function Mt(n,e){Y(n);let t=e.outputLen;if(n.length<t)throw new Error("digestInto() expects output buffer of length at least "+t)}function V(...n){for(let e=0;e<n.length;e++)n[e].fill(0)}function st(n){return new DataView(n.buffer,n.byteOffset,n.byteLength)}function R(n,e){return n<<32-e|n>>>e}function Ft(n){if(Y(n),fe)return n.toHex();let e="";for(let t=0;t<n.length;t++)e+=le[n[t]];return e}function de(n){if(typeof n!="string")throw new Error("string expected");return new Uint8Array(new TextEncoder().encode(n))}function Ut(n){return typeof n=="string"&&(n=de(n)),Y(n),n}function N(n){let e=r=>n().update(Ut(r)).digest(),t=n();return e.outputLen=t.outputLen,e.blockLen=t.blockLen,e.create=()=>n(),e}var fe,le,it,ct=$(()=>{fe=typeof Uint8Array.from([]).toHex=="function"&&typeof Uint8Array.fromHex=="function",le=Array.from({length:256},(n,e)=>e.toString(16).padStart(2,"0"));it=class{}});function xe(n,e,t,r){if(typeof n.setBigUint64=="function")return n.setBigUint64(e,t,r);let o=BigInt(32),i=BigInt(4294967295),a=Number(t>>o&i),s=Number(t&i),u=r?4:0,h=r?0:4;n.setUint32(e+u,a,r),n.setUint32(e+h,s,r)}function kt(n,e,t){return n&e^~n&t}function Dt(n,e,t){return n&e^n&t^e&t}var Z,_,M,E,S,jt=$(()=>{ct();Z=class extends it{constructor(e,t,r,o){super(),this.finished=!1,this.length=0,this.pos=0,this.destroyed=!1,this.blockLen=e,this.outputLen=t,this.padOffset=r,this.isLE=o,this.buffer=new Uint8Array(e),this.view=st(this.buffer)}update(e){Bt(this),e=Ut(e),Y(e);let{view:t,buffer:r,blockLen:o}=this,i=e.length;for(let a=0;a<i;){let s=Math.min(o-this.pos,i-a);if(s===o){let u=st(e);for(;o<=i-a;a+=o)this.process(u,a);continue}r.set(e.subarray(a,a+s),this.pos),this.pos+=s,a+=s,this.pos===o&&(this.process(t,0),this.pos=0)}return this.length+=e.length,this.roundClean(),this}digestInto(e){Bt(this),Mt(e,this),this.finished=!0;let{buffer:t,view:r,blockLen:o,isLE:i}=this,{pos:a}=this;t[a++]=128,V(this.buffer.subarray(a)),this.padOffset>o-a&&(this.process(r,0),a=0);for(let c=a;c<o;c++)t[c]=0;xe(r,o-8,BigInt(this.length*8),i),this.process(r,0);let s=st(e),u=this.outputLen;if(u%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let h=u/4,d=this.get();if(h>d.length)throw new Error("_sha2: outputLen bigger than state");for(let c=0;c<h;c++)s.setUint32(4*c,d[c],i)}digest(){let{buffer:e,outputLen:t}=this;this.digestInto(e);let r=e.slice(0,t);return this.destroy(),r}_cloneInto(e){e||(e=new this.constructor),e.set(...this.get());let{blockLen:t,buffer:r,length:o,finished:i,destroyed:a,pos:s}=this;return e.destroyed=a,e.finished=i,e.length=o,e.pos=s,o%t&&e.buffer.set(r),e}clone(){return this._cloneInto()}},_=Uint32Array.from([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),M=Uint32Array.from([3238371032,914150663,812702999,4144912697,4290775857,1750603025,1694076839,3204075428]),E=Uint32Array.from([3418070365,3238371032,1654270250,914150663,2438529370,812702999,355462360,4144912697,1731405415,4290775857,2394180231,1750603025,3675008525,1694076839,1203062813,3204075428]),S=Uint32Array.from([1779033703,4089235720,3144134277,2227873595,1013904242,4271175723,2773480762,1595750129,1359893119,2917565137,2600822924,725511199,528734635,4215389547,1541459225,327033209])});function me(n,e=!1){return e?{h:Number(n&ut),l:Number(n>>Vt&ut)}:{h:Number(n>>Vt&ut)|0,l:Number(n&ut)|0}}function Nt(n,e=!1){let t=n.length,r=new Uint32Array(t),o=new Uint32Array(t);for(let i=0;i<t;i++){let{h:a,l:s}=me(n[i],e);[r[i],o[i]]=[a,s]}return[r,o]}function I(n,e,t,r){let o=(e>>>0)+(r>>>0);return{h:n+t+(o/2**32|0)|0,l:o|0}}var ut,Vt,Ht,Lt,G,W,tt,et,Gt,Wt,Ot,qt,Xt,Kt,$t=$(()=>{ut=BigInt(4294967295),Vt=BigInt(32);Ht=(n,e,t)=>n>>>t,Lt=(n,e,t)=>n<<32-t|e>>>t,G=(n,e,t)=>n>>>t|e<<32-t,W=(n,e,t)=>n<<32-t|e>>>t,tt=(n,e,t)=>n<<64-t|e>>>t-32,et=(n,e,t)=>n>>>t-32|e<<64-t;Gt=(n,e,t)=>(n>>>0)+(e>>>0)+(t>>>0),Wt=(n,e,t,r)=>e+t+r+(n/2**32|0)|0,Ot=(n,e,t,r)=>(n>>>0)+(e>>>0)+(t>>>0)+(r>>>0),qt=(n,e,t,r,o)=>e+t+r+o+(n/2**32|0)|0,Xt=(n,e,t,r,o)=>(n>>>0)+(e>>>0)+(t>>>0)+(r>>>0)+(o>>>0),Kt=(n,e,t,r,o,i)=>e+t+r+o+i+(n/2**32|0)|0});var Qt={};St(Qt,{SHA224:()=>ht,SHA256:()=>nt,SHA384:()=>ft,SHA512:()=>O,SHA512_224:()=>lt,SHA512_256:()=>dt,sha224:()=>Ae,sha256:()=>ye,sha384:()=>Se,sha512:()=>Ee,sha512_224:()=>Ue,sha512_256:()=>Be});var ge,k,nt,ht,Jt,we,be,D,j,O,ft,B,U,lt,dt,ye,Ae,Ee,Se,Be,Ue,Yt=$(()=>{jt();$t();ct();ge=Uint32Array.from([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),k=new Uint32Array(64),nt=class extends Z{constructor(e=32){super(64,e,8,!1),this.A=_[0]|0,this.B=_[1]|0,this.C=_[2]|0,this.D=_[3]|0,this.E=_[4]|0,this.F=_[5]|0,this.G=_[6]|0,this.H=_[7]|0}get(){let{A:e,B:t,C:r,D:o,E:i,F:a,G:s,H:u}=this;return[e,t,r,o,i,a,s,u]}set(e,t,r,o,i,a,s,u){this.A=e|0,this.B=t|0,this.C=r|0,this.D=o|0,this.E=i|0,this.F=a|0,this.G=s|0,this.H=u|0}process(e,t){for(let c=0;c<16;c++,t+=4)k[c]=e.getUint32(t,!1);for(let c=16;c<64;c++){let m=k[c-15],f=k[c-2],x=R(m,7)^R(m,18)^m>>>3,p=R(f,17)^R(f,19)^f>>>10;k[c]=p+k[c-7]+x+k[c-16]|0}let{A:r,B:o,C:i,D:a,E:s,F:u,G:h,H:d}=this;for(let c=0;c<64;c++){let m=R(s,6)^R(s,11)^R(s,25),f=d+m+kt(s,u,h)+ge[c]+k[c]|0,p=(R(r,2)^R(r,13)^R(r,22))+Dt(r,o,i)|0;d=h,h=u,u=s,s=a+f|0,a=i,i=o,o=r,r=f+p|0}r=r+this.A|0,o=o+this.B|0,i=i+this.C|0,a=a+this.D|0,s=s+this.E|0,u=u+this.F|0,h=h+this.G|0,d=d+this.H|0,this.set(r,o,i,a,s,u,h,d)}roundClean(){V(k)}destroy(){this.set(0,0,0,0,0,0,0,0),V(this.buffer)}},ht=class extends nt{constructor(){super(28),this.A=M[0]|0,this.B=M[1]|0,this.C=M[2]|0,this.D=M[3]|0,this.E=M[4]|0,this.F=M[5]|0,this.G=M[6]|0,this.H=M[7]|0}},Jt=Nt(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(n=>BigInt(n))),we=Jt[0],be=Jt[1],D=new Uint32Array(80),j=new Uint32Array(80),O=class extends Z{constructor(e=64){super(128,e,16,!1),this.Ah=S[0]|0,this.Al=S[1]|0,this.Bh=S[2]|0,this.Bl=S[3]|0,this.Ch=S[4]|0,this.Cl=S[5]|0,this.Dh=S[6]|0,this.Dl=S[7]|0,this.Eh=S[8]|0,this.El=S[9]|0,this.Fh=S[10]|0,this.Fl=S[11]|0,this.Gh=S[12]|0,this.Gl=S[13]|0,this.Hh=S[14]|0,this.Hl=S[15]|0}get(){let{Ah:e,Al:t,Bh:r,Bl:o,Ch:i,Cl:a,Dh:s,Dl:u,Eh:h,El:d,Fh:c,Fl:m,Gh:f,Gl:x,Hh:p,Hl:l}=this;return[e,t,r,o,i,a,s,u,h,d,c,m,f,x,p,l]}set(e,t,r,o,i,a,s,u,h,d,c,m,f,x,p,l){this.Ah=e|0,this.Al=t|0,this.Bh=r|0,this.Bl=o|0,this.Ch=i|0,this.Cl=a|0,this.Dh=s|0,this.Dl=u|0,this.Eh=h|0,this.El=d|0,this.Fh=c|0,this.Fl=m|0,this.Gh=f|0,this.Gl=x|0,this.Hh=p|0,this.Hl=l|0}process(e,t){for(let w=0;w<16;w++,t+=4)D[w]=e.getUint32(t),j[w]=e.getUint32(t+=4);for(let w=16;w<80;w++){let v=D[w-15]|0,F=j[w-15]|0,bt=G(v,F,1)^G(v,F,8)^Ht(v,F,7),yt=W(v,F,1)^W(v,F,8)^Lt(v,F,7),z=D[w-2]|0,C=j[w-2]|0,ot=G(z,C,19)^tt(z,C,61)^Ht(z,C,6),At=W(z,C,19)^et(z,C,61)^Lt(z,C,6),at=Ot(yt,At,j[w-7],j[w-16]),Et=qt(at,bt,ot,D[w-7],D[w-16]);D[w]=Et|0,j[w]=at|0}let{Ah:r,Al:o,Bh:i,Bl:a,Ch:s,Cl:u,Dh:h,Dl:d,Eh:c,El:m,Fh:f,Fl:x,Gh:p,Gl:l,Hh:g,Hl:b}=this;for(let w=0;w<80;w++){let v=G(c,m,14)^G(c,m,18)^tt(c,m,41),F=W(c,m,14)^W(c,m,18)^et(c,m,41),bt=c&f^~c&p,yt=m&x^~m&l,z=Xt(b,F,yt,be[w],j[w]),C=Kt(z,g,v,bt,we[w],D[w]),ot=z|0,At=G(r,o,28)^tt(r,o,34)^tt(r,o,39),at=W(r,o,28)^et(r,o,34)^et(r,o,39),Et=r&i^r&s^i&s,ce=o&a^o&u^a&u;g=p|0,b=l|0,p=f|0,l=x|0,f=c|0,x=m|0,{h:c,l:m}=I(h|0,d|0,C|0,ot|0),h=s|0,d=u|0,s=i|0,u=a|0,i=r|0,a=o|0;let Ct=Gt(ot,at,ce);r=Wt(Ct,C,At,Et),o=Ct|0}({h:r,l:o}=I(this.Ah|0,this.Al|0,r|0,o|0)),{h:i,l:a}=I(this.Bh|0,this.Bl|0,i|0,a|0),{h:s,l:u}=I(this.Ch|0,this.Cl|0,s|0,u|0),{h,l:d}=I(this.Dh|0,this.Dl|0,h|0,d|0),{h:c,l:m}=I(this.Eh|0,this.El|0,c|0,m|0),{h:f,l:x}=I(this.Fh|0,this.Fl|0,f|0,x|0),{h:p,l}=I(this.Gh|0,this.Gl|0,p|0,l|0),{h:g,l:b}=I(this.Hh|0,this.Hl|0,g|0,b|0),this.set(r,o,i,a,s,u,h,d,c,m,f,x,p,l,g,b)}roundClean(){V(D,j)}destroy(){V(this.buffer),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}},ft=class extends O{constructor(){super(48),this.Ah=E[0]|0,this.Al=E[1]|0,this.Bh=E[2]|0,this.Bl=E[3]|0,this.Ch=E[4]|0,this.Cl=E[5]|0,this.Dh=E[6]|0,this.Dl=E[7]|0,this.Eh=E[8]|0,this.El=E[9]|0,this.Fh=E[10]|0,this.Fl=E[11]|0,this.Gh=E[12]|0,this.Gl=E[13]|0,this.Hh=E[14]|0,this.Hl=E[15]|0}},B=Uint32Array.from([2352822216,424955298,1944164710,2312950998,502970286,855612546,1738396948,1479516111,258812777,2077511080,2011393907,79989058,1067287976,1780299464,286451373,2446758561]),U=Uint32Array.from([573645204,4230739756,2673172387,3360449730,596883563,1867755857,2520282905,1497426621,2519219938,2827943907,3193839141,1401305490,721525244,746961066,246885852,2177182882]),lt=class extends O{constructor(){super(28),this.Ah=B[0]|0,this.Al=B[1]|0,this.Bh=B[2]|0,this.Bl=B[3]|0,this.Ch=B[4]|0,this.Cl=B[5]|0,this.Dh=B[6]|0,this.Dl=B[7]|0,this.Eh=B[8]|0,this.El=B[9]|0,this.Fh=B[10]|0,this.Fl=B[11]|0,this.Gh=B[12]|0,this.Gl=B[13]|0,this.Hh=B[14]|0,this.Hl=B[15]|0}},dt=class extends O{constructor(){super(32),this.Ah=U[0]|0,this.Al=U[1]|0,this.Bh=U[2]|0,this.Bl=U[3]|0,this.Ch=U[4]|0,this.Cl=U[5]|0,this.Dh=U[6]|0,this.Dl=U[7]|0,this.Eh=U[8]|0,this.El=U[9]|0,this.Fh=U[10]|0,this.Fl=U[11]|0,this.Gh=U[12]|0,this.Gl=U[13]|0,this.Hh=U[14]|0,this.Hl=U[15]|0}},ye=N(()=>new nt),Ae=N(()=>new ht),Ee=N(()=>new O),Se=N(()=>new ft),Be=N(()=>new dt),Ue=N(()=>new lt)});var q={};St(q,{decodePaymentRequest:()=>Ne,default:()=>We,getDecodedToken:()=>Ge,getEncodedToken:()=>Ve});function vt(){throw new Error("Cashu payments (BUD-07) are not supported in the bitvid Blossom bundle")}var Ve,Ne,Ge,We,X=$(()=>{Ve=vt,Ne=vt,Ge=vt,We={}});var J={};St(J,{getPaymentRequestFromHeaders:()=>Oe});async function Oe(n,e=!1){let t=n.get("X-Cashu");if(!t){if(e)return;throw new Error("Missing cashu header")}let{decodePaymentRequest:r}=await Promise.resolve().then(()=>(X(),q)),o=r(t);return o.transport=[],o}var K=$(()=>{});ct();var Tt=Symbol.for("sha256");function P(n){return Reflect.has(n,Tt)?Reflect.get(n,Tt):He(n).then(e=>(Reflect.set(n,Tt,e),e))}async function He(n){let e;typeof File<"u"&&n instanceof File||n instanceof Blob?e=await n.arrayBuffer():e=n;let t;if(typeof crypto<"u"&&crypto.subtle){let r=await crypto.subtle.digest("SHA-256",e);t=new Uint8Array(r)}else{let{sha256:r}=await Promise.resolve().then(()=>(Yt(),Qt));t=r.create().update(new Uint8Array(e)).digest()}return Ft(t)}function xt(n){return typeof File<"u"&&n instanceof File||n instanceof Blob?n.size:n.length}function mt(n){if(typeof File<"u"&&n instanceof File||n instanceof Blob)return n.type}function pt(n){return n instanceof URL?n.hostname.toLowerCase():URL.canParse(n)?new URL(n).hostname.toLowerCase():n.toLowerCase()}function Zt(n,e){return pt(n)===pt(e)}var Rt=()=>Math.floor(new Date().valueOf()/1e3),Re=()=>Rt()+3600;function y(n){let e=JSON.stringify(n),t=new TextEncoder().encode(e),r="";for(let o of t)r+=String.fromCharCode(o);return"Nostr "+btoa(r).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/g,"")}function te(n,e){return n.tags.filter(t=>t[0]===e).map(t=>t[1])}function ve(n){let e=n.tags.find(r=>r[0]==="expiration")?.[1];if(!e)return;let t=Number(e);if(Number.isFinite(t))return t}function ne(n,e=Rt()){let t=ve(n);return t!==void 0&&t<=e}function Ie(n){return pt(n)}function Pe(n){return n==="upload"||n==="media"||n==="delete"}async function ze(n,e){if(ne(n)||n.tags.find(a=>a[0]==="t")?.[1]!==e.type)return!1;let r=te(n,"server");if(r.length>0&&!r.some(a=>Zt(a,e.server)))return!1;if(e.type==="list")return!0;let o=te(n,"x");if(!e.blob)return!1;let i=typeof e.blob=="string"?e.blob:await P(e.blob);return o.length===0?!Pe(e.type):o.includes(i)}async function L(n,e){for(let t of n){if(ne(t)){n.delete(t);continue}if(await ze(t,e))return t}}function T(n,e){return n.add(e),e}async function ee(n){return typeof n=="string"?n:P(n)}function Ce(n){let e=Array.isArray(n)?n:[n];return[...new Set(e.map(t=>Ie(t)))]}async function gt(n,e,t){let r={created_at:Rt(),kind:24242,content:t?.message??"",tags:[["t",e],["expiration",String(t?.expiration??Re())]]};if(t?.blobs)if(Array.isArray(t.blobs)){let o=new Set;for(let i of t.blobs){let a=await ee(i);o.has(a)||(r.tags.push(["x",a]),o.add(a))}}else r.tags.push(["x",await ee(t.blobs)]);if(t?.servers)for(let o of Ce(t.servers))r.tags.push(["server",o]);return await n(r)}async function _e(n,e,t){return await gt(n,t?.type??"upload",{message:"Upload Blob",...t,blobs:e})}async function Me(n,e,t){return await gt(n,"upload",{message:"Mirror Blob",...t,blobs:e})}async function Fe(n,e){return await gt(n,"list",{message:"List Blobs",...e})}async function ke(n,e,t){return await gt(n,"delete",{message:"Delete Blob",...t,blobs:e})}var De=10063;function je(n){let e=[];for(let t of n.tags)if(t[0]==="server"&&t[1])try{let r=new URL(t[1]);r.pathname="/",e.push(r)}catch{}return e}K();var It=class extends Error{};function re(n,e,t){let r=new AbortController;t&&t.addEventListener("abort",a=>r.abort(a),{once:!0});let o=setTimeout(()=>{r.abort(new It(e))},n);return{cancel:()=>clearTimeout(o),signal:r.signal}}function A(n,e){if(e?.timeout){let{cancel:t,signal:r}=re(e.timeout,e.timeoutMessage??"Timeout",e.signal??void 0);return fetch(n,{...e,signal:r}).finally(t)}else return fetch(n,e)}var qe={409:"conflict",413:"too_large",415:"unsupported_type",422:"unprocessable"},H=class n extends Error{constructor(t,r){super(typeof r=="string"?r:r.message);Q(this,"response");Q(this,"status");Q(this,"body");Q(this,"code");this.response=t,this.status=t.status,this.code=qe[t.status],typeof r=="object"&&(this.body=r)}static isRejection(t){return t instanceof n&&t.code!==void 0}static async handleErrorResponse(t){if(!t.ok)try{throw new n(t,t.headers.get("x-reason")||"Something went wrong")}catch(r){throw r}}};var rt=class extends Error{};async function oe(n,e,t){let r=new URL("/media",n),o=await P(e),i=t?.authEvents,a=async(m=!1)=>{let f=i?await L(i,{server:n,type:"media",blob:o}):void 0;if(f)return f;let x=await t?.onAuth?.(n,o,"media",e);if(!x)throw new Error(m?"Missing onAuth handler":"Missing auth handler");return i&&T(i,x),x},s={"X-SHA-256":o};t?.auth&&(typeof t.auth=="boolean"?s.Authorization=y(await a(!0)):s.Authorization=y(t.auth));let u={...s,"X-Content-Length":String(xt(e))},h=mt(e);h&&(u["X-Content-Type"]=h);let d=await fetch(r,{method:"HEAD",signal:t?.signal,headers:u}),c;if(d.status===404)throw new rt("/media endpoint not supported");switch(d.status){case 401:{if(t?.auth===!1)throw new Error("Authorization disabled");let m=await a();c=await fetch(r,{signal:t?.signal,method:"PUT",body:e,headers:{...s,Authorization:y(m)}});break}case 402:{if(!t?.onPayment)throw new Error("Missing payment handler");let{getEncodedToken:m}=await Promise.resolve().then(()=>(X(),q)),{getPaymentRequestFromHeaders:f}=await Promise.resolve().then(()=>(K(),J)),x=await f(d.headers),p=await t.onPayment(n,o,e,x),l=m(p);c=await fetch(r,{signal:t?.signal,method:"PUT",body:e,headers:{...s,"X-Cashu":l}});break}}if(d.status>=500)throw new Error("Server error");return c||(c=await fetch(r,{signal:t?.signal,method:"PUT",body:e,headers:{...s}})),await H.handleErrorResponse(c),c.json()}async function wt(n,e,t){let r=new URL("/mirror",n),o=async(u=!1)=>{let h=t?.authEvents?await L(t.authEvents,{server:n,type:"upload",blob:e.sha256}):void 0;if(h)return h;let d=await t?.onAuth?.(n,e.sha256,e);if(!d)throw new Error(u?"Missing onAuth handler":"Missing auth handler");return t?.authEvents&&T(t.authEvents,d),d},i={"X-SHA-256":e.sha256,"X-Content-Length":String(e.size),"Content-Type":"application/json"};e.type&&(i["X-Content-Type"]=e.type),t?.auth&&(typeof t.auth=="boolean"?i.Authorization=y(await o(!0)):i.Authorization=y(t.auth));let a=JSON.stringify({url:e.url}),s=await A(r,{method:"PUT",signal:t?.signal,headers:i,body:a,timeout:t?.timeout});switch(s.status){case 401:{if(t?.auth===!1)throw new Error("Authorization disabled");let u=await o();s=await A(r,{signal:t?.signal,method:"PUT",body:a,headers:{...i,Authorization:y(u)},timeout:t?.timeout});break}case 402:{if(!t?.onPayment)throw new Error("Missing payment handler");let{getEncodedToken:u}=await Promise.resolve().then(()=>(X(),q)),{getPaymentRequestFromHeaders:h}=await Promise.resolve().then(()=>(K(),J)),d=await h(s.headers),c=await t.onPayment(n,e.sha256,e,d),m=u(c);s=await A(r,{signal:t?.signal,method:"PUT",body:a,headers:{...i,"X-Cashu":m},timeout:t?.timeout});break}}return await H.handleErrorResponse(s),s.json()}async function Pt(n,e,t){return(await A(new URL("/"+e,n),{method:"HEAD",signal:t?.signal,timeout:t?.timeout})).status!==404}async function zt(n,e,t){let r=new URL("/upload",n),o=await P(e),i=async(c=!1)=>{let m=t?.authEvents?await L(t.authEvents,{server:n,type:"upload",blob:o}):void 0;if(m)return m;let f=await t?.onAuth?.(n,o,"upload",e);if(!f)throw new Error(c?"Missing onAuth handler":"Missing auth handler");return t?.authEvents&&T(t.authEvents,f),f},a={"X-SHA-256":o};t?.auth&&(typeof t.auth=="boolean"?a.Authorization=y(await i(!0)):a.Authorization=y(t.auth));let s={...a,"X-Content-Length":String(xt(e))},u=mt(e);u&&(s["X-Content-Type"]=u);let h=await A(r,{method:"HEAD",signal:t?.signal,headers:s,timeout:t?.timeout}),d;switch(h.status===404&&(d=h=await A(r,{body:e,method:"PUT",signal:t?.signal,timeout:t?.timeout})),h.status){case 401:{if(t?.auth===!1)throw new Error("Authorization disabled");let c=await i();d=await A(r,{method:"PUT",body:e,headers:{...a,Authorization:y(c)},signal:t?.signal,timeout:t?.timeout});break}case 402:{if(!t?.onPayment)throw new Error("Missing payment handler");let{getEncodedToken:c}=await Promise.resolve().then(()=>(X(),q)),{getPaymentRequestFromHeaders:m}=await Promise.resolve().then(()=>(K(),J)),f=await m(h.headers),x=await t.onPayment(n,o,e,f),p=c(x);d=await A(r,{method:"PUT",body:e,headers:{...a,"X-Cashu":p},signal:t?.signal,timeout:t?.timeout});break}}if(h.status>=500)throw new Error("Server error");return d||(d=await A(r,{method:"PUT",body:e,headers:{...a},signal:t?.signal,timeout:t?.timeout})),await H.handleErrorResponse(d),d.json()}var ae={mirrorTimeout:5e3,preflight:!0},Xe={...ae,mediaUploadBehavior:"first",mediaUploadFallback:!1};async function ie(n,e,t){let r={...ae,...t},o,i=new Map,a=r.authEvents??new Set,s=await P(e);typeof r.auth=="object"&&a.add(r.auth);let u=async(f,x,p)=>{let l=await L(a,{server:f,type:p,blob:x});if(l)return l;if(r.onAuth){let g=await r.onAuth(f,x,p,e);return T(a,g)}else throw new Error("Missing onAuth handler")},h=async(f,x,p,l)=>{if(!r.onPayment)throw new Error("Missing payment handler");return r.onPayment(f,x,e,l)},d=async(f,x,p)=>H.isRejection(p)?r.onRejection?await r.onRejection(f,x,e,p):(r.onError?.(f,x,e,p),"skip"):!1,c=Array.from(n),m=new Set;if(r.preflight!==!1&&c.length>0){let f=await Promise.allSettled(c.map(async x=>{let p=await Pt(x,s,{signal:r.signal,timeout:r.mirrorTimeout});return{server:x,exists:p}}));for(let x of f)x.status==="fulfilled"&&x.value.exists&&m.add(x.value.server)}for(let f of c){let x=m.has(f);try{let p;if(o||x){let l=o??{url:new URL("/"+s,f).toString(),sha256:s,size:0,uploaded:0};try{r.onStart?.(f,s,e),p=await wt(f,l,{signal:r.signal,auth:typeof r.auth=="boolean"?r.auth:void 0,onAuth:(g,b)=>u(g,b,"upload"),onPayment:h,timeout:r.mirrorTimeout})}catch{if(x)continue}}p||(r.onStart?.(f,s,e),p=await zt(f,e,{signal:r.signal,auth:typeof r.auth=="boolean"?r.auth:void 0,onAuth:u,onPayment:h,timeout:r.mirrorTimeout}),o||(o=p)),i.set(f,p),r.onUpload?.(f,p.sha256,e)}catch(p){let l=await d(f,s,p);if(l==="cancel")return i;!l&&p instanceof Error&&r.onError?.(f,s,e,p)}}return i}async function Ke(n,e,t){let r={...Xe,...t},o,i=new Map,a=r.authEvents??new Set,s=await P(e);typeof r.auth=="object"&&a.add(r.auth);let u=async(l,g,b)=>{let w=await L(a,{server:l,type:b,blob:g});if(w)return w;if(r.onAuth){let v=await r.onAuth(l,g,b,e);return T(a,v)}else throw new Error("Missing onAuth handler")},h=async(l,g,b,w)=>{if(!r.onPayment)throw new Error("Missing payment handler");return r.onPayment(l,g,e,w)},d=async(l,g,b)=>H.isRejection(b)?r.onRejection?await r.onRejection(l,g,e,b):(r.onError?.(l,g,e,b),"skip"):!1,c=Array.from(n),m=r.mediaUploadBehavior==="any"?Array.from(c):[c[0]],f;for(let l of m){try{r.onStart?.(l,s,e),o=await oe(l,e,{signal:r.signal,auth:typeof r.auth=="boolean"?r.auth:void 0,onAuth:u,onPayment:h}),i.set(l,o),f=l,r.onUpload?.(l,o.sha256,e)}catch(g){if(!(g instanceof rt)){let b=await d(l,s,g);if(b==="cancel")return i;!b&&g instanceof Error&&r.onError?.(l,s,e,g)}}if(o)break}if(!o){if(!r.mediaUploadFallback)throw new Error("Failed to find media processing endpoint");return ie(n,e,r)}let x=c.filter(l=>l!==f),p=new Set;if(r.preflight!==!1&&x.length>0){let l=await Promise.allSettled(x.map(async g=>{let b=await Pt(g,o.sha256,{signal:r.signal,timeout:r.mirrorTimeout});return{server:g,exists:b}}));for(let g of l)g.status==="fulfilled"&&g.value.exists&&p.add(g.value.server)}for(let l of x)try{r.onStart?.(l,o.sha256,e);let g=await wt(l,o,{signal:r.signal,auth:typeof r.auth=="boolean"?r.auth:void 0,onAuth:(b,w)=>u(b,w,"upload"),onPayment:h,timeout:r.mirrorTimeout});i.set(l,g),r.onUpload?.(l,g.sha256,e)}catch(g){let b=await d(l,o.sha256,g);if(b==="cancel")return i;!b&&g instanceof Error&&r.onError?.(l,o.sha256,e,g)}return i}async function se(n,e,t){let r=new URL("/list/"+e,n);t?.cursor&&r.searchParams.append("cursor",t.cursor),t?.limit&&r.searchParams.append("limit",String(t.limit)),t?.since&&r.searchParams.append("since",String(t.since)),t?.until&&r.searchParams.append("until",String(t.until));let o=async(s=!1)=>{let u=t?.authEvents?await L(t.authEvents,{server:n,type:"list"}):void 0;if(u)return u;let h=await t?.onAuth?.(n);if(!h)throw new Error(s?"Missing onAuth handler":"Missing auth handler");return t?.authEvents&&T(t.authEvents,h),h},i={};t?.auth&&(typeof t.auth=="boolean"?i.Authorization=y(await o(!0)):i.Authorization=y(t.auth));let a=await A(r,{headers:i,signal:t?.signal,timeout:t?.timeout});switch(a.status){case 401:{if(t?.auth===!1)throw new Error("Authorization disabled");let s=await o();a=await A(r,{headers:{...i,Authorization:y(s)},signal:t?.signal,timeout:t?.timeout});break}case 402:{if(!t?.onPayment)throw new Error("Missing payment handler");let{getEncodedToken:s}=await Promise.resolve().then(()=>(X(),q)),{getPaymentRequestFromHeaders:u}=await Promise.resolve().then(()=>(K(),J)),h=await u(a.headers),d=await t.onPayment(n,h),c=s(d);a=await A(r,{headers:{...i,"X-Cashu":c},signal:t?.signal,timeout:t?.timeout});break}}return await H.handleErrorResponse(a),a.json()}async function*$e(n,e,t){let r=t?.cursor;for(;;){let o=await se(n,e,{...t,cursor:r});if(o.length===0||(yield o,t?.limit&&o.length<t.limit)||(r=o[o.length-1]?.sha256,!r))return}}async function Je(n,e,t){let r=new URL("/"+e,n),o=async(s=!1)=>{let u=t?.authEvents?await L(t.authEvents,{server:n,type:"delete",blob:e}):void 0;if(u)return u;let h=await t?.onAuth?.(n,e);if(!h)throw new Error(s?"Missing onAuth handler":"Missing auth handler");return t?.authEvents&&T(t.authEvents,h),h},i={};t?.auth&&(typeof t.auth=="boolean"?i.Authorization=y(await o(!0)):i.Authorization=y(t.auth));let a=await A(r,{method:"DELETE",headers:i,signal:t?.signal,timeout:t?.timeout});switch(a.status){case 401:{if(t?.auth===!1)throw new Error("Authorization disabled");let s=await o();a=await A(r,{signal:t?.signal,method:"DELETE",headers:{...i,Authorization:y(s)},timeout:t?.timeout});break}case 402:{if(!t?.onPayment)throw new Error("Missing payment handler");let{getEncodedToken:s}=await Promise.resolve().then(()=>(X(),q)),{getPaymentRequestFromHeaders:u}=await Promise.resolve().then(()=>(K(),J)),h=await u(a.headers),d=await t.onPayment(n,e,h),c=s(d);a=await A(r,{signal:t?.signal,method:"DELETE",headers:{...i,"X-Cashu":c},timeout:t?.timeout});break}}return await H.handleErrorResponse(a),a.ok}export{De as USER_BLOSSOM_SERVER_LIST_KIND,ke as createDeleteAuth,Fe as createListAuth,Me as createMirrorAuth,_e as createUploadAuth,Je as deleteBlob,y as encodeAuthorizationHeader,je as getServersFromServerListEvent,$e as iterateBlobs,se as listBlobs,wt as mirrorBlob,Ke as multiServerMediaUpload,ie as multiServerUpload,zt as uploadBlob};
|
|
/*! Bundled license information:
|
|
|
|
@noble/hashes/esm/utils.js:
|
|
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
|
*/
|