mirror of
https://github.com/PR0M3TH3AN/bitvid.git
synced 2026-10-28 08:22:24 +00:00
2 lines
25 KiB
JavaScript
2 lines
25 KiB
JavaScript
import{IS_DEV_MODE as ve,IS_VERBOSE_DEV_MODE as Ie,IS_LOCKDOWN_MODE as Oe,LOCKDOWN_TITLE as _t,LOCKDOWN_MESSAGE as St,ADMIN_SUPER_NPUB as wt,ADMIN_DM_IMAGE_URL as Lt,BITVID_WEBSITE_URL as At,BLOG_URL as Ct,COMMUNITY_URL as xt,NOSTR_URL as vt,GITHUB_URL as It,BETA_URL as Ot,DNS_URL as Bt,TIP_JAR_URL as Nt,MAX_WALLET_DEFAULT_ZAP as Mt,PLATFORM_FEE_PERCENT as Dt,PLATFORM_LUD16_OVERRIDE as Rt,DEFAULT_RELAY_URLS_OVERRIDE as Ut,ADMIN_WHITELIST_MODE_STORAGE_KEY as Ht,DEFAULT_WHITELIST_MODE_ENABLED as kt,ALLOW_NSFW_CONTENT as Ft,DEFAULT_TRUST_SEED_NPUBS as Vt,DEFAULT_BLUR_THRESHOLD as Pt,DEFAULT_AUTOPLAY_BLOCK_THRESHOLD as Wt,DEFAULT_TRUSTED_MUTE_HIDE_THRESHOLD as qt,DEFAULT_TRUSTED_SPAM_HIDE_THRESHOLD as jt,WATCH_HISTORY_KIND as $t,WATCH_HISTORY_LIST_IDENTIFIER as Yt,WATCH_HISTORY_MAX_ITEMS as Kt,WATCH_HISTORY_BATCH_RESOLVE as zt,WATCH_HISTORY_BATCH_PAGE_SIZE as Gt,WATCH_HISTORY_PAYLOAD_MAX_BYTES as Xt,WATCH_HISTORY_FETCH_EVENT_LIMIT as Zt,WATCH_HISTORY_CACHE_TTL_MS as Jt,VIEW_COUNT_DEDUPE_WINDOW_SECONDS as Qt,VIEW_COUNT_BACKFILL_MAX_DAYS as er,VIEW_COUNT_CACHE_TTL_MS as tr,ENSURE_PRESENCE_REBROADCAST_COOLDOWN_SECONDS as rr,DEFAULT_PLAYBACK_SOURCE as nr,DEFAULT_PLAYBACK_START_TIMEOUT as or}from"../config/instance-config.js";var re=typeof globalThis>"u"?null:globalThis,ne=e=>{if(typeof e=="boolean")return e;if(typeof e=="string"){let t=e.trim().toLowerCase();if(t==="true")return!0;if(t==="false")return!1}return null},ee=ne(re?.__BITVID_DEV_MODE_OVERRIDE__),te=ne(re?.__BITVID_VERBOSE_DEV_MODE_OVERRIDE__),U=ee===null?!!ve:ee;var Be=te===null?!!Ie:te;var Ne=!!Oe;typeof window<"u"&&(window.__BITVID_DEV_MODE__=U,window.__BITVID_VERBOSE_DEV_MODE__=Be,window.__BITVID_LOCKDOWN_MODE__=Ne);var k=()=>{};function Me(){return typeof console>"u"||console===null?null:console}function De(){let e=Me();if(!e)return{log:k,info:k,debug:k,warn:k,error:k};let t=typeof e.log=="function"?e.log.bind(e):k;return{log:typeof e.log=="function"?e.log.bind(e):t,info:typeof e.info=="function"?e.info.bind(e):t,debug:typeof e.debug=="function"?e.debug.bind(e):t,warn:typeof e.warn=="function"?e.warn.bind(e):t,error:typeof e.error=="function"?e.error.bind(e):t}}var _=De(),M="[bitvid]";function oe(e){if(e.length===0)return{args:e,force:!1};let t=e[e.length-1];if(t&&typeof t=="object"&&!Array.isArray(t)&&Object.prototype.hasOwnProperty.call(t,"force")){let r=[...e],o=r.pop();return{args:r,force:!!o.force}}return{args:e,force:!1}}var S=Object.freeze({log:(...e)=>{U&&_.log(...e)},info:(...e)=>{U&&_.info(...e)},debug:(...e)=>{U&&_.debug(...e)},warn:(...e)=>{U&&_.warn(...e)},error:(...e)=>{U&&_.error(...e)}}),b=Object.freeze({log:(...e)=>{let{args:t,force:r}=oe(e);if(!r&&t.length===0)return;if(r){_.log(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){_.log(`${M} ${o}`);return}o!==void 0&&_.log(M,o)},info:(...e)=>{let{args:t,force:r}=oe(e);if(!r&&t.length===0)return;if(r){_.info(M,...t);return}let[o,...n]=t;if(typeof o=="string"&&n.length===0){_.info(`${M} ${o}`);return}o!==void 0&&_.info(M,o)},warn:(...e)=>{_.warn(M,...e)},error:(...e)=>{_.error(M,...e)}}),ae=Object.freeze({dev:S,user:b});function Re(e){return!e||typeof e!="object"?!1:Object.entries({dev:["log","info","debug","warn","error"],user:["log","info","warn","error"]}).every(([r,o])=>{let n=e[r];return!n||typeof n!="object"?!1:o.every(c=>typeof n[c]=="function")})}typeof window<"u"&&(Re(window.bitvidLogger)||(window.bitvidLogger=ae));Object.freeze(ae);import{THEME_ACCENT_OVERRIDES as se}from"../config/instance-config.js";var G="bitvid:theme",v="dark",Ue=new Set(["light","dark"]),ie={dark:"rgb(15, 23, 42)",light:"rgb(248, 250, 252)"},ce=["[data-theme-toggle]",'[data-action="toggle-theme"]',".js-theme-toggle","#themeToggle"],ue=Object.freeze({accent:"--color-accent",accentStrong:"--color-accent-strong",accentPressed:"--color-accent-pressed"}),le=new WeakSet,W=new Set,D=null,de=!1,F=null,I=()=>typeof window<"u"&&typeof document<"u",q=e=>{if(typeof e!="string")return null;let t=e.trim().toLowerCase();return t==="default"||t===""?v:Ue.has(t)?t:null},He=()=>{if(!I())return null;try{let e=window.localStorage.getItem(G);return q(e)}catch(e){return b.warn("Unable to read stored theme preference:",e),null}},ke=e=>{if(I())try{window.localStorage.setItem(G,e)}catch(t){b.warn("Unable to persist theme preference:",t)}},K=()=>{if(!I())return null;let{documentElement:e}=document;return e instanceof HTMLElement?e:null},Fe=e=>typeof e=="string"?e.trim():"",Ve=e=>{if(!e||typeof e!="object")return null;let t=Object.entries(e).reduce((r,[o,n])=>{if(!n||typeof n!="object")return r;let c=Object.entries(n).reduce((a,[u,d])=>(Object.prototype.hasOwnProperty.call(ue,u)&&(a[u]=d),a),{});return Object.keys(c).length>0&&(r[o]=c),r},{});return Object.keys(t).length>0?t:null},fe=e=>{F=Ve(e)};fe(typeof se<"u"?se:null);var pe=(e,t)=>{if(!e)return;let r=F&&typeof F=="object"&&F[t]&&typeof F[t]=="object"?F[t]:null,o=!1;Object.entries(ue).forEach(([n,c])=>{let a=r?r[n]:null,u=Fe(a);u?(e.style.setProperty(c,u),o=!0):e.style.removeProperty(c)}),o?e.dataset.themeAccent="override":e.removeAttribute("data-theme-accent")},Pe=e=>{if(!I())return;let t=document.querySelector('meta[name="theme-color"]');if(!t)return;let r=K();if(!r)return;let o="";try{typeof window.getComputedStyle=="function"&&(o=getComputedStyle(r).getPropertyValue("--color-page").trim())}catch(a){b.warn("Unable to compute theme color token:",a)}let n=ie[e]||ie[v],c=o||n;c&&t.setAttribute("content",c)},me=(e,t)=>{if(!(e instanceof HTMLElement))return;e.dataset.themeState=t,e.setAttribute("aria-pressed",t==="dark"?"true":"false");let r=t==="dark"?"Switch to light theme":"Switch to dark theme";e.setAttribute("aria-label",r),e.setAttribute("title",r);let o=e.querySelector("[data-theme-toggle-label]")||e.querySelector(".sr-only");o&&(o.textContent=r);let n=e.querySelector("[data-theme-toggle-icon]")||e.querySelector('[aria-hidden="true"]');if(n){let c=typeof SVGElement<"u"?SVGElement:null;if(c&&n instanceof c||n instanceof Element&&!!n.querySelector("svg"))n instanceof Element&&n.setAttribute("data-theme-icon-state",t);else{let u=e.dataset.iconLight||"\u2600\uFE0F",d=e.dataset.iconDark||"\u{1F319}";n.textContent=t==="dark"?u:d}}},We=()=>{if(!I()){W.clear();return}W.forEach(e=>{(!(e instanceof HTMLElement)||!document.contains(e))&&W.delete(e)})},qe=e=>{We(),W.forEach(t=>{me(t,e)})},je=e=>{!(e instanceof HTMLElement)||le.has(e)||(e.addEventListener("click",t=>{t.preventDefault(),Ye(D==="dark"?"light":"dark")}),e.dataset.themeToggleBound="true",le.add(e))},$e=(e=document)=>{if(!I())return[];let t=new Set,r=e&&typeof e.querySelectorAll=="function"?e:document;return ce.forEach(o=>{try{r.querySelectorAll(o).forEach(n=>{n instanceof HTMLElement&&t.add(n)})}catch(n){b.warn(`Invalid selector for theme toggle: ${o}`,n)}}),e instanceof HTMLElement&&ce.forEach(o=>{e.matches(o)&&t.add(e)}),Array.from(t)},X=(e,{persist:t=!0}={})=>{let r=q(e)||v;D=r;let o=K();return o&&(o.dataset.theme=r,pe(o,r)),t&&ke(r),Pe(r),qe(r),r},he=e=>{fe(e);let t=K();if(!t)return;pe(t,D||v)},Ye=e=>I()?X(e,{persist:!0}):(D=q(e)||v,D),Ke=(e=document)=>{if(!I())return;$e(e).forEach(r=>{W.add(r),je(r),me(r,D||v)})},ze=()=>{if(!I())return v;let e=K();return(e?q(e.dataset.theme):null)||He()||v},Ge=e=>{if(!e||e.key!==G)return;let t=q(e.newValue)||v;t!==D&&X(t,{persist:!1})},be=()=>{if(!I()){D=v;return}de||(window.addEventListener("storage",Ge),de=!0);let e=ze();X(e,{persist:!0}),Ke(document)};var ye={info:"torrent-toast torrent-toast--info",success:"torrent-toast torrent-toast--success",error:"torrent-toast torrent-toast--error",warn:"torrent-toast torrent-toast--warn"},Ee="transition duration-200 ease-out",Xe="beacon-toast-motion",Z="data-beacon-motion";function Ze(e,t){let r=e?.defaultView;if(r&&typeof r.requestAnimationFrame=="function"){r.requestAnimationFrame(t);return}setTimeout(t,16)}function Je(e){let t=e||(typeof document<"u"?document:null);if(!t)return null;let r=t.getElementById("beacon-toast-container");if(!r){r=t.createElement("div"),r.id="beacon-toast-container",r.setAttribute("role","region"),r.setAttribute("aria-live","polite"),r.className=["pointer-events-none","fixed","inset-x-0","top-0","z-overlay-toast","flex","w-full","flex-col","items-center","gap-3","px-4","pt-6","sm:items-end","sm:px-6","sm:pt-8"].join(" ");let o=t.body||t.getElementsByTagName?.("body")?.[0];o&&o.appendChild(r)}return r}function Qe(e,t,r){let o=e.createElement("div");o.className=[Ee,r].join(" "),o.setAttribute("role","status"),o.setAttribute("tabindex","0"),o.textContent=t;let n=e.createElement("button");n.type="button",n.className=["notification-dismiss","focus-ring","absolute","right-3","top-3"].join(" "),n.setAttribute("aria-label","Dismiss notification"),n.innerHTML='<svg class="h-4 w-4" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3.75 3.75l8.5 8.5"></path><path d="M12.25 3.75l-8.5 8.5"></path></svg>';let c=e.createElement("div");return c.className=["relative","w-full",Ee,Xe].join(" "),c.setAttribute(Z,"exit"),c.appendChild(o),o.appendChild(n),{wrapper:c,toast:o,closeButton:n}}function et(e,t=setTimeout,r=clearTimeout){return{show(o,n={}){if(!o)return;let c=n.type&&ye[n.type]?n.type:"info",a=Je(e);if(!a)return;let{wrapper:u,toast:d,closeButton:L}=Qe(e,o,ye[c]);a.appendChild(u),Ze(e,()=>{u.setAttribute(Z,"enter")});let w=typeof n.duration=="number"?n.duration:3500,m=()=>{u.setAttribute(Z,"exit");let N=t(()=>{u.parentNode&&u.parentNode.removeChild(u)},200);return L.removeEventListener("click",A),d.removeEventListener("keydown",y),L.removeEventListener("keydown",y),N},A=()=>m(),y=N=>{N.key==="Escape"&&(N.stopPropagation(),m())};L.addEventListener("click",A),d.addEventListener("keydown",y),L.addEventListener("keydown",y);let B=null;return n.sticky||(B=t(()=>{m()},w)),{dismiss(){B&&r(B),m()}}},set(o,n){return typeof n=="string"?this.show(o,{type:n}):this.show(o,n)},info(o,n={}){return this.show(o,{...n,type:"info"})},success(o,n={}){return this.show(o,{...n,type:"success"})},error(o,n={}){return this.show(o,{...n,type:"error"})},warn(o,n={}){return this.show(o,{...n,type:"warn"})}}}function Te(e,t=setTimeout,r=clearTimeout){return et(e,t,r)}var J=[{key:"name",label:"Name",headerClass:"w-full sm:w-1/3 text-left",cellClass:"font-medium text-text truncate"},{key:"progress",label:"Progress",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"progress"},{key:"downloadSpeed",label:"\u2193 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"uploadSpeed",label:"\u2191 Speed",headerClass:"w-24 text-right",cellClass:"tabular-nums text-right",formatter:"bytesPerSecond"},{key:"numPeers",label:"Peers",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"integer"},{key:"ratio",label:"Ratio",headerClass:"w-20 text-right",cellClass:"tabular-nums text-right",formatter:"ratio"}];function tt(e){return e?e.infoHash||e.magnetURI||e.name||String(Math.random()):""}function rt(e,t){return!e||!t?!1:e===t||e.magnetURI&&e.magnetURI===t.magnetURI||e.infoHash&&e.infoHash===t.infoHash}function nt(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function ot(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function at(e){return typeof e!="number"||!Number.isFinite(e)?"0":Math.max(0,Math.floor(e)).toString()}function st(e,t){if(typeof t=="function")return t(e,!0)||"";if(typeof e!="number"||Number.isNaN(e)||e<1)return"";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}/s`}function it(e,t){let r=e.formatter;if(typeof r=="function")return r;let o=t?.[r];if(typeof o=="function")return o;switch(r){case"progress":return nt;case"ratio":return ot;case"integer":return at;case"bytesPerSecond":return n=>st(n,t?.bytes);default:return n=>n}}function ge({documentRef:e=typeof document<"u"?document:null,root:t,onSelect:r,formatters:o={}}={}){if(!e)throw new Error("createTorrentTable requires a document reference");if(!(t instanceof e.defaultView.HTMLElement))throw new Error("createTorrentTable requires a valid root element");let n=e;t.innerHTML="";let c=n.createElement("div");c.className="overflow-hidden rounded-xl border border-border-translucent bg-surface shadow-sm";let a=n.createElement("div");a.className="overflow-x-auto",c.appendChild(a);let u=n.createElement("table");u.className="min-w-full divide-y divide-border-translucent text-sm",a.appendChild(u);let d=n.createElement("thead");d.className="bg-surface-alt text-muted-strong";let L=n.createElement("tr");J.forEach(E=>{let x=n.createElement("th");x.setAttribute("scope","col"),x.className="px-4 py-3 text-left text-xs font-semibold uppercase tracking-wide",E.headerClass&&(x.className+=` ${E.headerClass}`),x.textContent=E.label,L.appendChild(x)}),d.appendChild(L),u.appendChild(d);let w=n.createElement("tbody");w.className="divide-y divide-border-translucent",u.appendChild(w);let m=n.createElement("tbody"),A=n.createElement("tr"),y=n.createElement("td");y.className="px-4 py-8 text-center text-sm text-muted",y.colSpan=J.length,y.textContent="No active torrents yet. Add a magnet link to get started.",A.appendChild(y),m.appendChild(A),u.appendChild(m),t.appendChild(c);function B(E=[],x=null){let V=Array.isArray(E)?E:[];if(w.innerHTML="",!V.length){w.hidden=!0,m.hidden=!1;return}w.hidden=!1,m.hidden=!0;for(let O of V){let g=n.createElement("tr");g.className=["group","align-middle","cursor-pointer","transition","hover:bg-panel-hover","focus-visible:outline","focus-visible:outline-2","focus-visible:outline-offset-2","focus-visible:outline-info"].join(" "),g.setAttribute("role","button"),g.setAttribute("tabindex","0"),g.dataset.torrentId=tt(O),rt(x,O)?(g.classList.add("bg-info/10","text-text"),g.setAttribute("aria-pressed","true")):g.setAttribute("aria-pressed","false"),g.addEventListener("click",()=>{typeof r=="function"&&r(O)}),g.addEventListener("keydown",C=>{(C.key==="Enter"||C.key===" ")&&(C.preventDefault(),typeof r=="function"&&r(O))}),J.forEach(C=>{let H=n.createElement("td");H.className="px-4 py-3",C.cellClass&&(H.className+=` ${C.cellClass}`);let z=O?O[C.key]:void 0,s=it(C,o)(z,O,C),i=n.createElement("span");i.textContent=s==null?"":String(s),H.appendChild(i),g.appendChild(H)}),w.appendChild(g)}}function N(){t.innerHTML=""}return{render:B,destroy:N}}var ct="2.0",Le=["wss://tracker.btorrent.xyz","wss://tracker.openwebtorrent.com"],_e={announce:Le},lt={announce:Le};function j(e,t=!1){if(typeof e!="number"||Number.isNaN(e))return"";if(e<1)return t?"":"0 B";let r=["B","kB","MB","GB","TB"],o=Math.min(Math.floor(Math.log(e)/Math.log(1e3)),r.length-1);return`${(e/Math.pow(1e3,o)).toFixed(1)} ${r[o]}${t?"/s":""}`}function dt(e){return typeof e!="number"||Number.isNaN(e)?"":`${(e*100).toFixed(1)}%`}function Se(e){return typeof e!="number"||!Number.isFinite(e)?"0.00":e.toFixed(2)}function ut(e,t){return!e||!t?!1:!!(e===t||e.infoHash&&t.infoHash&&e.infoHash===t.infoHash||e.magnetURI&&t.magnetURI&&e.magnetURI===t.magnetURI)}function ft(e){if(!(!e||!Array.isArray(e.files))&&(e.files.forEach(t=>{typeof t.priority!="string"&&(t.priority="0"),typeof t.getBlobURL=="function"&&t.getBlobURL((r,o)=>{r||!o||(t.url=o)})}),e.torrentFile&&typeof Blob=="function"&&typeof URL<"u"))try{let t=new Blob([e.torrentFile],{type:"application/x-bittorrent"});if(e.torrentFileBlobURL=URL.createObjectURL(t),!e.fileName){let r=e.name||"download";e.fileName=`${r}.torrent`}}catch(t){S.warn("[beacon] Failed to create torrent blob URL",t)}}function pt(e,t){if(!e)return;let r=typeof t=="string"?t:String(t??"0");if(e.priority=r,r==="-1"){typeof e.deselect=="function"&&e.deselect();return}let o=Number.parseInt(r,10);Number.isFinite(o)&&typeof e.select=="function"&&e.select(o)}function we(e,t){if(!Array.isArray(t)||t.length===0)return null;if(!e)return t[0];let r=t.find(o=>ut(o,e));return r||t[0]}function Ae({documentRef:e=typeof document<"u"?document:null,WebTorrentCtor:t}={}){if(!e)throw new Error("createBeaconApp requires a document reference");let r=e.defaultView||globalThis,o=typeof t=="function"?t:typeof r?.WebTorrent=="function"?r.WebTorrent:null;if(!o)throw new Error("createBeaconApp requires a WebTorrent constructor");let n=Te(e),c=new o({tracker:lt}),a={form:e.querySelector('[data-beacon="magnet-form"]'),magnetInput:e.querySelector('[data-beacon="magnet-input"]'),seedButton:e.querySelector('[data-beacon="seed-button"]'),seedInput:e.querySelector('[data-beacon="seed-input"]'),tableRoot:e.querySelector('[data-beacon="torrent-table"]'),selectedSection:e.querySelector('[data-beacon="selected"]'),selectedName:e.querySelector('[data-beacon="selected-name"]'),pauseButton:e.querySelector('[data-beacon-action="pause"]'),resumeButton:e.querySelector('[data-beacon-action="resume"]'),downloadAllButton:e.querySelector('[data-beacon-action="download-all"]'),removeButton:e.querySelector('[data-beacon-action="remove"]'),copyMagnetButton:e.querySelector('[data-beacon-action="copy-magnet"]'),infoHashValue:e.querySelector('[data-beacon="info-hash"]'),fileList:e.querySelector('[data-beacon="file-list"]'),stats:e.querySelector('[data-beacon="client-stats"]'),overlay:e.querySelector('[data-beacon="processing-overlay"]')},u=a.tableRoot?ge({documentRef:e,root:a.tableRoot,formatters:{bytes:(s,i=!1)=>j(s,i),progress:s=>dt(s),ratio:s=>Se(s),integer:s=>typeof s=="number"&&Number.isFinite(s)?Math.floor(s).toString():"0",bytesPerSecond:s=>j(s,!0)},onSelect(s){d.selectedTorrent=s,E()}}):null,d={selectedTorrent:null,processing:!1},L=[],w=[];function m(s){L.push(s)}function A(s){if(d.processing=!!s,!a.overlay)return;let i=d.processing;a.overlay.hidden=!i,a.overlay.setAttribute("aria-hidden",i?"false":"true"),i?a.overlay.style.removeProperty("display"):a.overlay.style.display="none"}function y(){return Array.isArray(c.torrents)?[...c.torrents]:[]}function B(){if(!a.stats)return;let s=j(c.downloadSpeed,!0)||"0 B/s",i=j(c.uploadSpeed,!0)||"0 B/s",p=Se(c.ratio);a.stats.textContent=`Client stats: \u2193 ${s} \xB7 \u2191 ${i} \xB7 Ratio: ${p}`}function N(s){a.fileList&&(a.fileList.innerHTML="",!(!s||!Array.isArray(s.files)||!s.files.length)&&s.files.forEach((i,p)=>{let f=e.createElement("tr");f.className="align-top";let l=e.createElement("td");if(l.className="px-3 py-2",i.done&&i.url){let T=e.createElement("a");T.className="text-info hover:text-info-strong",T.href=i.url,T.download=i.name||`file-${p+1}`,T.textContent=i.name||`File ${p+1}`,l.appendChild(T)}else{let T=e.createElement("span");T.textContent=i.name||`File ${p+1}`,l.appendChild(T)}f.appendChild(l);let h=e.createElement("td");h.className="px-3 py-2 text-muted-strong",h.textContent=j(i.length)||"0 B",f.appendChild(h);let P=e.createElement("td");P.className="px-3 py-2";let R=e.createElement("select");R.className="select",R.name=`${i.name||`file-${p+1}`}-priority`,R.setAttribute("aria-label",`Set priority for ${i.name||`file ${p+1}`}`),[{value:"1",label:"High priority"},{value:"0",label:"Low priority"},{value:"-1",label:"Don't download"}].forEach(T=>{let Y=e.createElement("option");Y.value=T.value,Y.textContent=T.label,T.value===i.priority&&(Y.selected=!0),R.appendChild(Y)}),R.value=i.priority??"0",R.addEventListener("change",T=>{pt(i,T.target.value)}),P.appendChild(R),f.appendChild(P),a.fileList.appendChild(f)}))}function E(){if(!a.selectedSection)return;let s=y();d.selectedTorrent=we(d.selectedTorrent,s);let i=d.selectedTorrent;a.selectedSection.hidden=!i,i&&(a.selectedName&&(a.selectedName.textContent=i.name||"Untitled torrent"),a.pauseButton&&(a.pauseButton.hidden=i.paused===!0,a.pauseButton.disabled=i.paused===!0),a.resumeButton&&(a.resumeButton.hidden=i.paused!==!0,a.resumeButton.disabled=i.paused!==!0),a.downloadAllButton&&(a.downloadAllButton.disabled=!(i.progress>=1)),a.infoHashValue&&(a.infoHashValue.textContent=i.infoHash||""),N(i))}function x(s,{isSeed:i=!1}={}){A(!1),ft(s),i||n.info(`Received ${s.name||"torrent"} metadata`),d.selectedTorrent||(d.selectedTorrent=s),E(),u?.render(y(),d.selectedTorrent)}function V(s){let i=typeof s=="string"?s.trim():"";i&&(A(!0),c.add(i,_e,p=>{x(p,{isSeed:!1})}))}function O(s){!Array.isArray(s)||s.length===0||(A(!0),c.seed(s,_e,i=>{x(i,{isSeed:!0}),n.success(`Seeding ${i.files.length} file(s)`)}))}function g(s){if(s){if(s.progress<1){n.warn("Torrent is not finished downloading yet.");return}s.files.forEach(i=>{typeof i.getBlob=="function"&&i.getBlob((p,f)=>{if(p||!f){b.error("[beacon] Failed to get blob for file",i?.name,p),n.error("Failed to prepare file for download.");return}let l=URL.createObjectURL(f),h=e.createElement("a");h.className="beacon-hidden-download",h.setAttribute("data-beacon-hidden-download","true"),h.href=l,h.download=i.name||"download",e.body.appendChild(h),h.click(),e.body.removeChild(h),URL.revokeObjectURL(l)})})}}function C(s){if(!s?.magnetURI)return;let i=s.magnetURI,p=r?.navigator?.clipboard;if(p&&typeof p.writeText=="function"){p.writeText(i).then(()=>{n.success("Magnet URI copied to clipboard!")}).catch(f=>{b.error("[beacon] Clipboard error",f),n.error("Failed to copy magnet URI")});return}try{let f=e.createElement("textarea");f.value=i,f.className="beacon-clipboard-offscreen",f.setAttribute("data-beacon-clipboard-state","offscreen"),e.body.appendChild(f),f.select(),e.execCommand?.("copy"),e.body.removeChild(f),n.success("Magnet URI copied (fallback)!")}catch(f){b.error("[beacon] Clipboard fallback error",f),n.error("Failed to copy magnet URI")}}function H(s){s&&s.destroy(i=>{i&&(b.error("[beacon] Failed to destroy torrent",i),n.error("Failed to remove torrent"));let p=y();d.selectedTorrent=we(null,p),u?.render(p,d.selectedTorrent),E()})}function z(){if(S.info(`[beacon] Starting beacon runtime v${ct}`),n||S.warn("[beacon] Toast service unavailable"),o.WEBRTC_SUPPORT||n?.error("Please use a browser with WebRTC support.",{sticky:!0}),a.form){let l=h=>{h.preventDefault(),V(a.magnetInput?.value||""),a.magnetInput&&(a.magnetInput.value="")};a.form.addEventListener("submit",l),m(()=>a.form.removeEventListener("submit",l))}if(a.seedButton&&a.seedInput){let l=()=>{a.seedInput.click()},h=()=>{let P=Array.from(a.seedInput.files||[]);a.seedInput.value="",O(P)};a.seedButton.addEventListener("click",l),a.seedInput.addEventListener("change",h),m(()=>{a.seedButton.removeEventListener("click",l),a.seedInput.removeEventListener("change",h)})}if(a.pauseButton){let l=()=>{d.selectedTorrent&&typeof d.selectedTorrent.pause=="function"&&(d.selectedTorrent.pause(),E())};a.pauseButton.addEventListener("click",l),m(()=>a.pauseButton.removeEventListener("click",l))}if(a.resumeButton){let l=()=>{d.selectedTorrent&&typeof d.selectedTorrent.resume=="function"&&(d.selectedTorrent.resume(),E())};a.resumeButton.addEventListener("click",l),m(()=>a.resumeButton.removeEventListener("click",l))}if(a.downloadAllButton){let l=()=>g(d.selectedTorrent);a.downloadAllButton.addEventListener("click",l),m(()=>a.downloadAllButton.removeEventListener("click",l))}if(a.removeButton){let l=()=>H(d.selectedTorrent);a.removeButton.addEventListener("click",l),m(()=>a.removeButton.removeEventListener("click",l))}if(a.copyMagnetButton){let l=()=>C(d.selectedTorrent);a.copyMagnetButton.addEventListener("click",l),m(()=>a.copyMagnetButton.removeEventListener("click",l))}let s=l=>{b.error("[beacon] Torrent client error",l),n?.error(l?.message||String(l)),A(!1)};c.on("error",s),m(()=>c.removeListener("error",s));let i=l=>{if(c.torrents&&c.torrents.length>0)return l.preventDefault(),l.returnValue="Transfers are in progress. Are you sure you want to leave or refresh?",l.returnValue};r&&typeof r.addEventListener=="function"&&(r.addEventListener("beforeunload",i),m(()=>r.removeEventListener("beforeunload",i))),u?.render(y(),d.selectedTorrent),E(),B(),A(!1);let p=r.setInterval(()=>{u?.render(y(),d.selectedTorrent),E(),B()},1e3);w.push(p);let f=r?.location?.hash;typeof f=="string"&&f.length>1&&V(f.slice(1))}function Q(){w.forEach(s=>r.clearInterval(s)),w.length=0,L.forEach(s=>{try{s()}catch(i){S.warn("[beacon] Cleanup task failed",i)}}),L.length=0,u?.destroy();try{c.destroy()}catch(s){S.warn("[beacon] Failed to destroy client",s)}}return{mount:z,destroy:Q}}var Ce="../../config/instance-config.js";function mt(){return typeof window<"u"&&window||typeof globalThis<"u"&&globalThis||null}function ht(){let e=mt();if(e&&typeof e.WebTorrent=="function")return e.WebTorrent;throw new Error("WebTorrent runtime is not available on the global scope")}var $=null,bt=async()=>{try{let e=await import(Ce);if(e&&typeof e.THEME_ACCENT_OVERRIDES<"u")return e.THEME_ACCENT_OVERRIDES}catch(e){S.warn(`[beacon] Failed to load theme accent overrides from ${Ce}`,e)}return null},yt=async()=>{try{let e=await bt();e&&he(e)}catch(e){S.warn("[beacon] Unable to hydrate theme accent overrides",e)}try{be()}catch(e){S.warn("[beacon] Failed to initialize theme controller",e)}};yt();function xe(){if($)return S.info("[beacon] Reusing existing beacon app instance"),$;if(typeof document>"u")throw new Error("Beacon runtime requires a document environment");let e=ht();return S.info("[beacon] Creating beacon app instance"),$=Ae({documentRef:document,WebTorrentCtor:e}),$.mount(),$}if(typeof document<"u")if(document.readyState==="loading")document.addEventListener("DOMContentLoaded",()=>{try{xe()}catch(e){b.error("[beacon] Failed to mount app",e)}});else try{xe()}catch(e){b.error("[beacon] Failed to mount app",e)}export{Ae as createBeaconApp,xe as mountBeaconApp};
|