torlnq 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -10,5 +10,5 @@ usage
|
|
|
10
10
|
once open: type to search every source at once, enter to run, arrows to move,
|
|
11
11
|
d to download, ? for keys
|
|
12
12
|
tip: quote magnet links (they contain & characters)
|
|
13
|
-
`;var Cr="1.0.0";import{useCallback as Ie,useEffect as pt,useMemo as zi,useRef as Gi,useState as ce}from"react";import{Box as Me,Text as Qi,useApp as Wi,useInput as ji,useStdout as Vi,useStdin as qi}from"ink";import{promises as wr}from"fs";import{promises as vn}from"fs";import yn from"os";import Ce from"path";import wn from"env-paths";var Rr="torlink",Er=wn(Rr,{suffix:""}),bt=process.env.TORLINK_STATE_DIR,Tt=bt?Ce.join(bt,"data"):Er.data,bn=bt?Ce.join(bt,"config"):Er.config,Qt=Ce.join(yn.homedir(),"Downloads",Rr),Wt=Ce.join(bn,"config.json"),Oe=Ce.join(Tt,"queue.json"),_e=Ce.join(Tt,"history.json"),Le=Ce.join(Tt,"seeds.json"),jt=Ce.join(Tt,"torrents");import{promises as Vt}from"fs";import Tn from"path";function Ne(){let t=Promise.resolve();return e=>(t=t.then(e).catch(()=>{}),t)}async function ke(t,e){await Vt.mkdir(Tn.dirname(t),{recursive:!0});let r=`${t}.tmp`;await Vt.writeFile(r,JSON.stringify(e,null,2),"utf8"),await Vt.rename(r,t)}var qt={downloadDir:Qt};async function Br(){let t;try{t=await vn.readFile(Wt,"utf8")}catch{return{...qt}}try{let e=JSON.parse(t),r={...qt,...e};return(!r.downloadDir||typeof r.downloadDir!="string")&&(r.downloadDir=Qt),r}catch{return{...qt}}}var Cn=Ne();function Ir(t){return Cn(()=>ke(Wt,t))}import Mr from"os";import kr from"path";function Rn(t,e=Mr.homedir()){let r=t.trim();return r==="~"?e:r.startsWith("~/")||r.startsWith("~\\")?kr.join(e,r.slice(2)):r}function Ar(t,e=Mr.homedir()){let r=Rn(t,e);return r?kr.normalize(r):""}import{EventEmitter as _n}from"events";import En from"webtorrent";function Pr(t){return t instanceof Error?t.message:String(t)}var vt=class{client=null;torrents=new Map;ensureClient(){return this.client||(this.client=new En,this.client.on("error",()=>{})),this.client}add(e,r,o,n){let s=this.ensureClient(),i=this.torrents.get(e);if(i){this.torrents.delete(e);try{i.destroy()}catch{}}let a;try{a=s.add(r,{path:o})}catch(l){n.onError?.(Pr(l));return}this.torrents.set(e,a),a.on("metadata",()=>{n.onMetadata?.({name:a.name,total:a.length,files:a.files?.length??0,torrentFile:a.torrentFile})}),a.on("done",()=>{n.onDone?.()}),a.on("error",l=>{n.onError?.(Pr(l)),this.torrents.delete(e);try{a.destroy()}catch{}})}listenPort(){return this.client?.torrentPort??null}stats(e){let r=this.torrents.get(e);return r?{progress:r.progress,downloaded:r.downloaded,total:r.length,speed:r.downloadSpeed,uploadSpeed:r.uploadSpeed,uploaded:r.uploaded,peers:r.numPeers,timeRemaining:r.timeRemaining,name:r.name}:null}remove(e){let r=this.torrents.get(e);if(this.torrents.delete(e),r)try{r.destroy()}catch{}}destroy(){this.torrents.clear();let e=this.client;this.client=null,e&&setImmediate(()=>{try{e.destroy()}catch{}})}};import{promises as et,mkdirSync as Hr,writeFileSync as $r,renameSync as Dr,existsSync as Bn,rmSync as In}from"fs";import Yt from"path";var Or=Ne();function _r(t){return Or(()=>ke(Oe,t))}function Lr(t){try{Hr(Yt.dirname(Oe),{recursive:!0});let e=`${Oe}.sync.tmp`;$r(e,JSON.stringify(t,null,2),"utf8"),Dr(e,Oe)}catch{}}function Mn(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.id=="string"&&typeof e.magnet=="string"}async function Nr(){let t;try{t=await et.readFile(Oe,"utf8")}catch{return[]}try{let e=JSON.parse(t);return Array.isArray(e)?e.filter(Mn):[]}catch{return[]}}function Fr(t){return Or(()=>ke(Le,t))}function Ur(t){try{Hr(Yt.dirname(Le),{recursive:!0});let e=`${Le}.sync.tmp`;$r(e,JSON.stringify(t,null,2),"utf8"),Dr(e,Le)}catch{}}function tt(t){return Yt.join(jt,`${t}.torrent`)}function zr(t){return Bn(tt(t))}async function Gr(t,e){try{await et.mkdir(jt,{recursive:!0});let r=tt(t),o=`${r}.tmp`;await et.writeFile(o,e),await et.rename(o,r)}catch{}}function Ct(t){try{In(tt(t),{force:!0})}catch{}}async function Qr(){let t;try{t=await et.readFile(Le,"utf8")}catch{return[]}try{let e=JSON.parse(t);if(!Array.isArray(e))return[];let r=[];for(let o of e)if(typeof o=="string")r.push({id:o,status:"seeding"});else if(o&&typeof o=="object"){let n=o;typeof n.id=="string"&&(n.status==="seeding"||n.status==="paused")&&r.push({id:n.id,status:n.status})}return r}catch{return[]}}import{promises as kn,mkdirSync as An,writeFileSync as Pn,renameSync as Hn}from"fs";import $n from"path";var Kt=500,Dn=Ne();function Rt(t){return Dn(()=>ke(_e,t.slice(0,Kt)))}function Wr(t){try{An($n.dirname(_e),{recursive:!0});let e=`${_e}.sync.tmp`;Pn(e,JSON.stringify(t.slice(0,Kt),null,2),"utf8"),Hn(e,_e)}catch{}}function On(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.id=="string"&&typeof e.name=="string"&&typeof e.magnet=="string"}async function jr(){let t;try{t=await kn.readFile(_e,"utf8")}catch{return[]}try{let e=JSON.parse(t);return Array.isArray(e)?e.filter(On).slice(0,Kt):[]}catch{return[]}}function Ln(t){return t.total>0&&t.progress<1&&t.speed>0}var Nn=2,Fn=1e4,Un=500,Vr=500,Et=class extends _n{items=new Map;engine=new vt;poll=null;history=[];seeds=new Map;strayHits=new Map;seedStartedAt=new Map;getItems(){return[...this.items.values()].sort((e,r)=>r.addedAt-e.addedAt)}get activeCount(){let e=0;for(let r of this.items.values())r.status==="downloading"&&e++;return e}has(e){return this.items.has(e)}add(e,r){this.seeds.has(e.id)&&(this.engine.remove(e.id),this.seeds.delete(e.id),this.strayHits.delete(e.id),this.seedStartedAt.delete(e.id),this.persistSeeds());let o=this.items.get(e.id);if(o&&o.status!=="failed")return;let n=o?{...o,status:"downloading",error:void 0,speed:0}:{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:r,status:"downloading",progress:0,totalBytes:e.sizeBytes??0,downloadedBytes:0,speed:0,peers:0,addedAt:Date.now()};this.items.set(n.id,n),this.startEngine(n),this.ensurePoll(),this.changed(),this.persist()}startEngine(e){this.engine.add(e.id,e.magnet,e.dir,this.engineHandlers(e.id))}engineHandlers(e){return{onMetadata:r=>{r.torrentFile&&Gr(e,r.torrentFile);let o=this.items.get(e);o&&(r.name&&(o.name=r.name),r.total&&(o.totalBytes=r.total),o.files=r.files,this.changed(),this.persist())},onDone:()=>{let r=this.items.get(e);if(r){r.totalBytes&&(r.downloadedBytes=r.totalBytes),this.complete(r);return}this.seeds.has(e)&&(this.strayHits.set(e,0),this.seedStartedAt.delete(e))},onError:r=>{let o=this.items.get(e);if(o){o.status="failed",o.error=r,o.speed=0,o.peers=0,this.changed(),this.persist(),this.maybeStopPoll();return}let n=this.seeds.get(e);n&&(n.status="missing",n.uploadSpeed=0,n.peers=0,this.seedStartedAt.delete(e),this.changed(),this.persistSeeds(),this.maybeStopPoll())}}}complete(e){this.recordHistory(e),this.items.delete(e.id),this.beginSeed(e),this.emit("completed",e.name),this.changed(),this.persist(),this.maybeStopPoll()}beginSeed(e){e.magnet&&(this.seeds.set(e.id,{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.totalBytes,status:"seeding",uploadSpeed:0,uploaded:0,peers:0}),this.strayHits.set(e.id,0),this.seedStartedAt.set(e.id,Date.now()),this.ensurePoll(),this.persistSeeds())}tick(){let e=!1;for(let o of this.items.values()){if(o.status!=="downloading")continue;let n=this.engine.stats(o.id);n&&(o.progress=Math.min(100,Math.round(n.progress*100)),o.downloadedBytes=n.downloaded,n.total&&(o.totalBytes=n.total),o.speed=n.speed,o.peers=n.peers,o.eta=n.timeRemaining>0&&Number.isFinite(n.timeRemaining)?n.timeRemaining/1e3:void 0,n.name&&(o.name=n.name),e=!0)}let r=Date.now();for(let o of this.seeds.values()){if(o.status!=="seeding")continue;let n=this.engine.stats(o.id);if(!n)continue;if(r-(this.seedStartedAt.get(o.id)??0)>Fn&&Ln(n)){let i=(this.strayHits.get(o.id)??0)+1;this.strayHits.set(o.id,i),i>=Nn&&(this.engine.remove(o.id),this.strayHits.delete(o.id),this.seedStartedAt.delete(o.id),o.status="missing",o.uploadSpeed=0,o.peers=0,this.persistSeeds()),e=!0;continue}this.strayHits.set(o.id,0),o.uploadSpeed=n.uploadSpeed,o.uploaded=n.uploaded,o.peers=n.peers,e=!0}e&&this.changed()}ensurePoll(){this.poll||(this.poll=setInterval(()=>this.tick(),Un),this.poll.unref())}maybeStopPoll(){this.activeCount===0&&this.seedingCount===0&&this.poll&&(clearInterval(this.poll),this.poll=null)}pause(e){let r=this.items.get(e);!r||r.status!=="downloading"||(r.status="paused",r.speed=0,r.peers=0,r.eta=void 0,this.engine.remove(e),this.changed(),this.persist(),this.maybeStopPoll())}resume(e){let r=this.items.get(e);!r||r.status!=="paused"||(r.status="downloading",this.startEngine(r),this.ensurePoll(),this.changed(),this.persist())}togglePause(e){let r=this.items.get(e);r&&(r.status==="downloading"?this.pause(e):r.status==="paused"&&this.resume(e))}cancel(e){this.items.has(e)&&(this.engine.remove(e),this.items.delete(e),Ct(e),this.changed(),this.persist(),this.maybeStopPoll())}retry(e){let r=this.items.get(e);!r||r.status!=="failed"||(r.status="downloading",r.error=void 0,this.startEngine(r),this.ensurePoll(),this.changed(),this.persist())}retryFailed(){for(let e of[...this.items.values()])e.status==="failed"&&this.retry(e.id)}getSeed(e){return this.seeds.get(e)}getSeeds(){return[...this.seeds.values()]}get seedingCount(){let e=0;for(let r of this.seeds.values())r.status==="seeding"&&e++;return e}startSeeding(e){if(this.seeds.get(e.id)?.status==="seeding"||this.items.has(e.id))return;let r={id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.sizeBytes,status:"seeding",uploadSpeed:0,uploaded:0,peers:0};if(!e.magnet){this.seeds.set(e.id,{...r,status:"missing"}),this.changed(),this.persistSeeds();return}this.seeds.set(e.id,r),this.strayHits.set(e.id,0),this.seedStartedAt.set(e.id,Date.now());let o=zr(e.id)?tt(e.id):e.magnet;this.engine.add(e.id,o,e.dir,this.engineHandlers(e.id)),this.ensurePoll(),this.changed(),this.persistSeeds()}stopSeeding(e){let r=this.seeds.get(e);r&&(this.engine.remove(e),this.strayHits.delete(e),this.seedStartedAt.delete(e),r.status==="seeding"&&(r.status="paused",r.uploadSpeed=0,r.peers=0),this.changed(),this.persistSeeds(),this.maybeStopPoll())}toggleSeeding(e){this.seeds.get(e.id)?.status==="seeding"?this.stopSeeding(e.id):this.startSeeding(e)}restoreSeeds(e){for(let r of e){let o=this.history.find(n=>n.id===r.id);o&&(r.status==="seeding"?this.startSeeding(o):this.restorePaused(o))}}restorePaused(e){this.seeds.has(e.id)||(this.seeds.set(e.id,{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.sizeBytes,status:"paused",uploadSpeed:0,uploaded:0,peers:0}),this.changed())}seedRecords(){let e=[];for(let r of this.seeds.values())r.status==="seeding"?e.push({id:r.id,status:"seeding"}):e.push({id:r.id,status:"paused"});return e}persistSeeds(){return Fr(this.seedRecords()).catch(()=>{})}restore(e){for(let r of e)this.items.set(r.id,r),r.status==="downloading"&&this.startEngine(r);this.activeCount>0&&this.ensurePoll(),this.changed()}restoreHistory(e){this.history=e.slice(0,Vr)}getHistory(){return this.history}recordHistory(e){let r={id:e.id,name:e.name,source:e.source,sizeBytes:e.totalBytes,magnet:e.magnet,dir:e.dir,completedAt:Date.now()};this.history=[r,...this.history.filter(o=>o.id!==e.id)].slice(0,Vr),Rt(this.history).catch(()=>{})}removeHistory(e){let r=this.history.filter(o=>o.id!==e);r.length!==this.history.length&&(this.history=r,this.seeds.has(e)&&(this.engine.remove(e),this.seeds.delete(e),this.strayHits.delete(e),this.seedStartedAt.delete(e),this.persistSeeds(),this.maybeStopPoll()),Ct(e),Rt(this.history).catch(()=>{}),this.changed())}clearHistory(){if(this.history.length!==0){for(let e of this.history)Ct(e.id);if(this.history=[],this.seeds.size>0){for(let e of this.seeds.keys())this.engine.remove(e);this.seeds.clear(),this.strayHits.clear(),this.seedStartedAt.clear(),this.persistSeeds(),this.maybeStopPoll()}Rt(this.history).catch(()=>{}),this.changed()}}changed(){this.emit("update")}async persist(){await _r(this.getItems()).catch(()=>{})}persistSync(){Lr(this.getItems()),Wr(this.history),Ur(this.seedRecords())}suspend(){for(let e of this.items.values())e.status==="downloading"&&(e.speed=0,e.peers=0,e.eta=void 0);this.persistSync(),this.poll&&(clearInterval(this.poll),this.poll=null),this.engine.destroy()}};function qr(t){let e=new Set,r=[];for(let o of t){if(!o?.id||e.has(o.id)||(e.add(o.id),o.status==="completed"))continue;let n=o.status==="failed"?"failed":o.status==="paused"?"paused":"downloading";r.push({...o,status:n,speed:0,peers:0,eta:void 0})}return r}var zn=["udp://tracker.opentrackr.org:1337/announce","udp://open.demonii.com:1337/announce","udp://tracker.openbittorrent.com:6969/announce","udp://tracker.torrent.eu.org:451/announce","udp://exodus.desync.com:6969/announce","udp://open.stealth.si:80/announce","udp://tracker.dler.org:6969/announce"];function de(t,e){let r=encodeURIComponent(e),o=zn.map(n=>`&tr=${encodeURIComponent(n)}`).join("");return`magnet:?xt=urn:btih:${t}&dn=${r}${o}`}var Gn=/xt=urn:btih:([a-f0-9]{40}|[a-z2-7]{32})/i,Qn="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";function Wn(t){let e=0,r=0,o="";for(let n of t.toUpperCase()){let s=Qn.indexOf(n);if(s===-1)return null;r=r<<5|s,e+=5,e>=8&&(e-=8,o+=(r>>>e&255).toString(16).padStart(2,"0"),r&=(1<<e)-1)}return o.length===40?o:null}function jn(t){return t.length===32?Wn(t)??t.toLowerCase():t.toLowerCase()}function Fe(t){let e=t.trim();if(!/^magnet:\?/i.test(e))return null;let r=Gn.exec(e);if(!r)return null;let o=jn(r[1]),n=o;try{let s=new URL(e).searchParams.get("dn");s&&(n=s)}catch{}return{infoHash:o,name:n,magnet:e}}import{promises as Vn}from"fs";import qn from"parse-torrent";async function Yr(t){try{let e=await Vn.readFile(t),r=await qn(new Uint8Array(e)),o=r?.infoHash?.toLowerCase();if(!o)return null;let n=r.name||o;return{infoHash:o,name:n,magnet:de(o,n)}}catch{return null}}import{spawn as Kr}from"child_process";function Jt(t,e){return new Promise(r=>{let o="";try{let n=Kr(t,e,{windowsHide:!0}),s=setTimeout(()=>{try{n.kill()}catch{}r("")},4e3);s.unref?.(),n.stdout.on("data",i=>o+=i.toString("utf8")),n.on("error",()=>{clearTimeout(s),r("")}),n.on("close",()=>{clearTimeout(s),r(o)})}catch{r("")}})}function Xt(t,e,r){return new Promise(o=>{try{let n=Kr(t,e,{windowsHide:!0}),s=!1,i,a=l=>{s||(s=!0,clearTimeout(i),o(l))};i=setTimeout(()=>{try{n.kill()}catch{}a(!1)},4e3),i.unref?.(),n.on("error",()=>a(!1)),n.on("close",l=>a(l===0)),n.stdin?.end(r)}catch{o(!1)}})}var Yn=[["wl-paste",["--no-newline"]],["xclip",["-selection","clipboard","-o"]],["xsel",["-b"]]],Kn=[["wl-copy",[]],["xclip",["-selection","clipboard"]],["xsel",["-b","-i"]]];async function Jr(){if(process.platform==="win32")return(await Jt("powershell",["-NoProfile","-Command","Get-Clipboard"])).trim();if(process.platform==="darwin")return(await Jt("pbpaste",[])).trim();for(let[t,e]of Yn){let r=(await Jt(t,e)).trim();if(r)return r}return""}async function Xr(t){if(process.platform==="win32")return Xt("powershell",["-NoProfile","-Command","Set-Clipboard -Value ([Console]::In.ReadToEnd())"],t);if(process.platform==="darwin")return Xt("pbcopy",[],t);for(let[e,r]of Kn)if(await Xt(e,r,t))return!0;return!1}function be(t){if(t===void 0||!Number.isFinite(t)||t<=0)return"0 B";let e=["B","KB","MB","GB","TB"],r=t,o=0;for(;r>=1024&&o<e.length-1;)r/=1024,o++;return`${r.toFixed(o===0?0:2)} ${e[o]}`}var Jn={B:1,KIB:1024,MIB:1024**2,GIB:1024**3,TIB:1024**4,KB:1e3,MB:1e6,GB:1e9,TB:1e12};function Bt(t){let e=t.match(/([\d.]+)\s*([KMGT]?I?B)/i);return e?Math.round(parseFloat(e[1])*(Jn[e[2].toUpperCase()]??1)):0}function rt(t){if(t===void 0||!Number.isFinite(t)||t<=0)return"";let e=["B/s","KB/s","MB/s","GB/s"],r=t,o=0;for(;r>=1024&&o<e.length-1;)r/=1024,o++;return`${r.toFixed(r<10&&o>0?1:0)} ${e[o]}`}function ot(t){if(!t||!Number.isFinite(t)||t<=0)return"";let e=Date.now()/1e3-t;if(e<60)return"now";let r=Math.floor(e/60);if(r<60)return`${r}m ago`;let o=Math.floor(r/60);if(o<24){let i=r%60;return i>0?`${o}hr ${i}m ago`:`${o}hr ago`}let n=Math.floor(o/24);if(n<30){let i=o%24;return i>0?`${n}d ${i}hr ago`:`${n}d ago`}let s=Math.floor(n/30);return s<12?`${s}mo ago`:`${Math.floor(s/12)}y ago`}function Zr(t){if(t===void 0||!Number.isFinite(t)||t<0)return"";let e=Math.round(t),r=Math.floor(e/86400),o=Math.floor(e%86400/3600),n=Math.floor(e%3600/60),s=e%60;return r>0?[`${r}d`,o>0?`${o}hr`:"",n>0?`${n}m`:""].filter(Boolean).join(" "):o>0?n>0?`${o}hr ${n}m`:`${o}hr`:n>0?s>0?`${n}m ${s}s`:`${n}m`:`${s}s`}function Xn(t){return t<32||t===127||t===65533||t>=8203&&t<=8207||t>=8232&&t<=8238||t===8288||t===65279||t===8205||t===65039||t===8419||t>=9728&&t<=10175||t>=11008&&t<=11263||t>=126976&&t<=131071}function ie(t){let e="";for(let r of t.normalize("NFC"))Xn(r.codePointAt(0))||(e+=r);return e.replace(/\s+/g," ").trim()||"Untitled"}function J(t,e){return e<=1?t.slice(0,Math.max(0,e)):t.length<=e?t:t.slice(0,e-1)+"\u2026"}import{createContext as Zn,useContext as es,useEffect as Zt,useState as er}from"react";var nt=[{key:"all",label:"All"},{key:"games",label:"Games",group:"Games"},{key:"movies",label:"Movies",group:"Movies"},{key:"tv",label:"TV",group:"TV"},{key:"anime",label:"Anime",group:"Anime"}],It=Zn(null);function X(){let t=es(It);if(!t)throw new Error("Store not available");return t}function Ue(t){let[e,r]=er(()=>t.getItems());return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(t.getItems())},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}function Mt(t){let[e,r]=er(()=>t.getHistory());return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(t.getHistory())},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}function eo(t){let[e,r]=er(()=>new Map(t.getSeeds().map(o=>[o.id,o])));return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(new Map(t.getSeeds().map(s=>[s.id,s])))},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}import{Box as so,Text as tr}from"ink";var kt=[" \u{104CF} "," \u2580\u2588\u2580 \u2588\u2580\u2588 \u2588\u2580\u2588 \u2588 \u2588 \u2588\u2584 \u2588 \u2588\u2584\u2580"," \u2588 \u2588\u2584\u2588 \u2588\u2580\u2584 \u2588\u2584\u2584 \u2588 \u2588 \u2580\u2588 \u2588 \u2588"],to=Math.max(...kt.map(t=>[...t].length)),ro=new Set(["0,6"]);var d={accent:"#a78bfa",text:"#e9e4f5",alt:"#b9a7e6",good:"#86d6a2",warn:"#f0c560",bad:"#ee7d92",bright:"#d8b4fe"},b={done:"\u2713",error:"\u2717",pending:"\xB7",pointer:"\u276F",dot:"\xB7",warn:"\u26A0",bar:"\u258C",down:"\u2193",up:"\u2191",peer:"\u2022",pause:"\u23F8"},we="#6b6577",Z=2,Re={fitgirl:{tag:"FG",color:d.accent},yts:{tag:"YTS",color:d.good},eztv:{tag:"EZTV",color:d.warn},nyaa:{tag:"NYAA",color:d.bright},subsplease:{tag:"SUB",color:"#b9a7e6"},solid:{tag:"SLD",color:"#60a5fa"},"tpb-movies":{tag:"TPB",color:"#5fd0c5"},"tpb-tv":{tag:"TPB",color:"#5fd0c5"},"x1337-movies":{tag:"1337",color:"#f6a55c"},"x1337-tv":{tag:"1337",color:"#f6a55c"}};function oo(t){let e=parseInt(t.slice(1),16);return[e>>16&255,e>>8&255,e&255]}function ae(t,e,r){let[o,n,s]=oo(t),[i,a,l]=oo(e),c=(u,p)=>Math.round(u+(p-u)*r).toString(16).padStart(2,"0");return`#${c(o,i)}${c(n,a)}${c(s,l)}`}var no=[d.accent,d.bright];import{jsx as st}from"react/jsx-runtime";var ts="#ffffff",io=d.bright,ao="#7c5cd6",rs="#4c3a8a",os="#5ae87a";function ns(t){return t<.15?ae(ts,io,t/.15):t<.4?ae(io,d.accent,(t-.15)/.25):t<.7?ae(d.accent,ao,(t-.4)/.3):ae(ao,rs,(t-.7)/.3)}function At(){let t=kt.length;return st(so,{flexDirection:"column",children:kt.map((e,r)=>{let o=Math.max(0,r-1),n=Math.max(1,t-1),s=o/(n-1||1),i=[...e],a=Math.max(1,i.length-1);return st(so,{children:i.map((l,c)=>{if(l===" ")return st(tr,{children:" "},c);if(ro.has(`${r},${c}`))return st(tr,{bold:!0,color:os,children:l},c);let p=(c/a+s)/2;return st(tr,{bold:!0,color:ns(p),children:l},c)})},r)})})}import{Box as it,Text as rr,useInput as ss}from"ink";function he(t,e,r){return r<=0?0:((t+e)%r+r)%r}function Ae(t,e,r){if(e<=r)return 0;let o=Math.floor(r/2);return Math.max(0,Math.min(t-o,e-r))}import{jsx as Pe,jsxs as us}from"react/jsx-runtime";var is=nt.map(t=>({key:t.key,label:t.label})),as=[{key:"downloads",label:"Downloads"},{key:"seeding",label:"Seeding"}],ls=t=>t==="downloads"||t==="seeding",lo=[is,as],ze=lo.flat(),cs=5,or=Z+Math.max(...ze.map(t=>t.label.length+(ls(t.key)?cs:0)));function co(){let{section:t,setSection:e,region:r,setRegion:o,queue:n}=X(),s=r==="sidebar",i=Math.max(0,ze.findIndex(c=>c.key===t));Ue(n);let a=n.activeCount,l=n.seedingCount;return ss((c,u)=>{u.upArrow?e(ze[he(i,-1,ze.length)].key):u.downArrow?e(ze[he(i,1,ze.length)].key):u.return&&o("content")},{isActive:s}),Pe(it,{flexDirection:"column",width:or,marginRight:1,children:lo.map((c,u)=>Pe(it,{flexDirection:"column",marginTop:u>0?1:0,children:c.map(p=>{let f=p.key===t;return us(it,{children:[Pe(it,{width:Z,flexShrink:0,children:f?Pe(rr,{color:s?no[1]:we,bold:s,children:b.bar}):null}),Pe(rr,{color:f?s?d.accent:d.alt:void 0,dimColor:!f,bold:f&&s,children:p.label}),(()=>{let x=p.key==="downloads"?a:p.key==="seeding"?l:0;return x>0?Pe(it,{flexShrink:0,children:Pe(rr,{dimColor:!0,children:` (${x})`})}):null})()]},p.key)})},u))})}import{Text as ds}from"ink";import{jsx as ms}from"react/jsx-runtime";function Pt({width:t}){return ms(ds,{color:we,children:"\u2500".repeat(Math.max(1,t))})}import{Box as fs,Text as at}from"ink";import{jsx as lt,jsxs as ps}from"react/jsx-runtime";function uo({hints:t}){return lt(fs,{children:lt(at,{children:t.map((e,r)=>ps(at,{children:[r>0?lt(at,{dimColor:!0,children:" "}):null,lt(at,{color:d.alt,children:e.keys}),lt(at,{dimColor:!0,children:` ${e.label}`})]},e.keys+e.label))})})}import{Box as Qe,Text as We}from"ink";var Ge=[{title:"Navigate",hints:[{keys:"\u2191 \u2193 \u2190 \u2192",label:"Navigate content and panes"},{keys:"\u21B5",label:"Open"},{keys:"tab",label:"Switch pane"},{keys:"esc",label:"Back"},{keys:"o",label:"Download folder"},{keys:"q",label:"Quit"}]},{title:"Search",hints:[{keys:"/",label:"Edit search"},{keys:"\u21B5",label:"Run search"},{keys:"s",label:"Sort results"},{keys:"y",label:"Copy magnet"},{keys:"m",label:"Paste magnet"}]},{title:"Downloads",hints:[{keys:"p",label:"Pause/resume"},{keys:"c",label:"Cancel or remove from list"},{keys:"f",label:"Retry failed"},{keys:"d",label:"Download again"},{keys:"x",label:"Clear recent"}]},{title:"Seeding",hints:[{keys:"p",label:"Pause/resume"},{keys:"c",label:"Remove from list"}]}],nr={keys:"\u2191\u2193\u2190\u2192",label:"Move"},He={keys:"?",label:"Keys"},$e={keys:"tab",label:"Switch"};function mo(t,e,r,o){return t==="sidebar"?[nr,{keys:"\u21B5",label:"Open"},$e,He,{keys:"q",label:"Quit"}]:e==="seeding"?[{keys:"p",label:o==="seeding"?"Pause":o==="missing"?"Retry":"Resume"},{keys:"c",label:"Remove"},$e,He]:e==="downloads"?r==="paused"?[{keys:"p",label:"Resume"},{keys:"c",label:"Cancel"},$e,He]:r==="failed"?[{keys:"f",label:"Retry"},{keys:"c",label:"Remove"},$e,He]:r==="recent"?[nr,{keys:"d",label:"Download again"},{keys:"c",label:"Remove"},{keys:"x",label:"Clear"},$e,He]:[{keys:"p",label:"Pause"},{keys:"c",label:"Cancel"},$e,He]:[nr,{keys:"d",label:"Download"},{keys:"y",label:"Copy"},{keys:"s",label:"Sort"},{keys:"/",label:"Search"},$e,He]}import{jsx as Ee,jsxs as Ht}from"react/jsx-runtime";var hs=ae(d.accent,we,.55),gs=2,fo=2,sr=Ge.map(t=>Math.max(...t.hints.map(e=>e.keys.length))+gs),po=Ge.map((t,e)=>sr[e]+Math.max(...t.hints.map(r=>r.label.length))),xs=po.reduce((t,e)=>t+e,0)+(Ge.length-1)*fo+4,Ss=Math.max(...sr);function ho(){let{cols:t}=X(),e=t>=xs;return Ht(Qe,{flexDirection:"column",alignSelf:"flex-start",borderStyle:"round",borderColor:hs,paddingX:e?1:2,paddingY:1,children:[Ee(We,{bold:!0,color:d.accent,children:"Keyboard"}),Ee(Qe,{marginTop:1,flexDirection:e?"row":"column",children:Ge.map((r,o)=>Ht(Qe,{flexDirection:"column",width:e?po[o]:void 0,marginRight:e&&o<Ge.length-1?fo:0,marginTop:!e&&o>0?1:0,children:[Ee(We,{bold:!0,children:r.title}),r.hints.map(n=>Ht(Qe,{children:[Ee(Qe,{width:e?sr[o]:Ss,flexShrink:0,children:Ee(We,{color:d.alt,children:n.keys})}),Ee(We,{dimColor:!0,children:n.label})]},n.keys+n.label))]},r.title))}),Ht(Qe,{marginTop:1,flexDirection:"column",children:[Ee(We,{dimColor:!0,children:"Your downloaded files always stay on disk."}),Ee(We,{dimColor:!0,children:"Press ? or esc to close"})]})]})}import{useEffect as fr,useMemo as pr,useState as Lt}from"react";import{Box as M,Text as w,useInput as hr}from"ink";import{useEffect as ys,useState as ws}from"react";import{Text as ir}from"ink";import{jsx as xo,jsxs as bs}from"react/jsx-runtime";var go=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"];function $t({label:t}){let[e,r]=ws(0);return ys(()=>{let o=setInterval(()=>r(n=>(n+1)%go.length),80);return o.unref?.(),()=>clearInterval(o)},[]),bs(ir,{children:[xo(ir,{color:d.accent,children:go[e]}),t?xo(ir,{dimColor:!0,children:` ${t}`}):null]})}import{Box as To,Text as cr}from"ink";import{useState as So}from"react";import{Text as Be,useInput as Ts}from"ink";import{jsx as je,jsxs as wo}from"react/jsx-runtime";function vs(t,e){return e===0?{value:t,cursor:e}:{value:t.slice(0,e-1)+t.slice(e),cursor:e-1}}function Cs(t,e){let r=e;for(;r>0&&t[r-1]===" ";)r--;for(;r>0&&t[r-1]!==" ";)r--;return{value:t.slice(0,r)+t.slice(e),cursor:r}}function Rs(t,e){return{value:t.slice(0,e),cursor:e}}function Es(t,e,r){return{value:t.slice(0,e)+r+t.slice(e),cursor:e+r.length}}var yo=" ";function Dt({isDisabled:t=!1,defaultValue:e="",placeholder:r="",onChange:o,onSubmit:n,onExitDown:s,onExitLeft:i}){let[a,l]=So(e),[c,u]=So(e.length);function p(S){l(S.value),u(Math.max(0,Math.min(S.value.length,S.cursor))),S.value!==a&&o?.(S.value)}if(Ts((S,y)=>{if(y.downArrow){s?.();return}if(y.upArrow||y.tab||y.ctrl&&S==="c")return;if(y.return){n?.(a);return}if(y.ctrl)switch(S){case"u":p({value:"",cursor:0});return;case"w":p(Cs(a,c));return;case"k":p(Rs(a,c));return;case"a":u(0);return;case"e":u(a.length);return;default:return}if(y.leftArrow){if(c===0){i?.();return}u(c-1);return}if(y.rightArrow){u(Math.min(a.length,c+1));return}if(y.backspace||y.delete){p(vs(a,c));return}if(y.meta||!S)return;let R=S.replace(/\x1b?\[<\d+;\d+;\d+[Mm]/g,"");R&&p(Es(a,c,R))},{isActive:!t}),t)return a?je(Be,{children:a}):je(Be,{dimColor:!0,children:r});if(a.length===0)return r?wo(Be,{children:[je(Be,{inverse:!0,children:r[0]}),je(Be,{dimColor:!0,children:r.slice(1)})]}):je(Be,{inverse:!0,children:yo});let f=a.slice(0,c),x=a[c]??yo,v=c<a.length?a.slice(c+1):"";return wo(Be,{children:[f,je(Be,{inverse:!0,children:x}),v]})}import{Box as ar,Text as lr}from"ink";import{jsx as Ot,jsxs as bo}from"react/jsx-runtime";function me({title:t,width:e,focused:r,count:o,height:n,children:s}){let i=r?d.accent:we,a=Math.max(10,e),l=t.charAt(0).toUpperCase()+t.slice(1),c=o?`${l} ${o}`:l,u=Math.max(0,a-5-c.length);return bo(ar,{flexDirection:"column",width:a,children:[bo(ar,{children:[Ot(lr,{color:i,children:"\u256D\u2500 "}),Ot(lr,{bold:!0,color:i,children:c}),Ot(lr,{color:i,children:` ${"\u2500".repeat(u)}\u256E`})]}),Ot(ar,{width:a,height:n,flexGrow:n?0:1,flexDirection:"column",borderStyle:"round",borderTop:!1,borderColor:i,paddingX:1,overflow:"hidden",children:s})]})}import{jsx as Ve,jsxs as Bs}from"react/jsx-runtime";function _t({width:t,value:e,placeholder:r="Search torrents\u2026",editing:o,onSubmit:n,onChange:s,onExitDown:i,onExitLeft:a}){return Ve(me,{title:"search",width:t,focused:o,height:2,children:Bs(To,{children:[Ve(cr,{color:d.accent,children:`${b.pointer} `}),Ve(To,{flexGrow:1,minWidth:0,children:o?Ve(Dt,{defaultValue:e,placeholder:r,onSubmit:n,onChange:s,onExitDown:i,onExitLeft:a}):e?Ve(cr,{wrap:"truncate-end",children:e}):Ve(cr,{dimColor:!0,children:r})})]})})}import{useEffect as ci,useState as ui}from"react";var G="torlink (+https://www.npmjs.com/package/torlnq)",H=class extends Error{status;constructor(e,r){super(r??`HTTP ${e}`),this.name="HttpError",this.status=e}},Is=new Set([408,425,429,500,502,503,504]),Ms=5,ks=500,As=2e4;function Ps(t){return new Promise(e=>setTimeout(e,t))}function Hs(t){return t instanceof Error&&(t.name==="AbortError"||/aborted/i.test(t.message))}function $s(t,e=Date.now()){if(!t)return;let r=t.trim();if(/^\d+$/.test(r))return Number(r)*1e3;let o=Date.parse(r);if(!Number.isNaN(o))return Math.max(0,o-e)}function vo(t,e,r,o,n=Math.random){let s=Math.min(r,e*2**t),i=Math.floor(n()*s);return o!==void 0?Math.max(i,o):i}async function Q(t,e={}){let{retries:r=Ms,baseMs:o=ks,capMs:n=As,fetchImpl:s=fetch,sleepImpl:i=Ps,signal:a,...l}=e,c=a?{...l,signal:a}:l,u;for(let p=0;p<=r;p++){if(a?.aborted)throw new H(0,"aborted");let f;try{f=await s(t,c)}catch(S){if(Hs(S)||a?.aborted)throw S;if(u=S,p<r){await i(vo(p,o,n));continue}throw S}if(!Is.has(f.status))return f;let x=f.headers.get("server")?.toLowerCase()||"";if(f.status===503&&(x.includes("ddos-guard")||x.includes("cloudflare")))throw new H(f.status,`Request to ${t} blocked by ${x} (HTTP ${f.status}).`);if(p>=r)throw new H(f.status,`Request to ${t} failed after ${r} retries (HTTP ${f.status}).`);let v=$s(f.headers.get("retry-after"));await i(vo(p,o,n,v))}throw u instanceof Error?u:new H(0,"fetchResilient exhausted without a response")}var Ds="https://eztvx.to/api/get-torrents";async function Os(t,e={}){if(t.trim())return[];let r=await Q(`${Ds}?limit=100&page=1`,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!r.ok)throw new H(r.status,`EZTV returned ${r.status}`);let o=await r.json(),n=[];for(let s of o.torrents??[]){let i=(s.hash??"").toLowerCase(),a=s.title||s.filename||i,l=s.magnet_url||(i?de(i,a):"");!l||!i||n.push({infoHash:i,name:a,sizeBytes:Number(s.size_bytes??0)||0,seeders:s.seeds??0,leechers:s.peers??0,source:"eztv",magnet:l,added:s.date_released_unix})}return n}var Co={id:"eztv",label:"EZTV",group:"TV",homepage:"https://eztvx.to",search:Os};function De(t){return t.replace(/�?38;|&/g,"&").replace(/–|—/g,"-").replace(/’|�?39;|'/g,"'").replace(/“|”|"/g,'"').replace(/</g,"<").replace(/>/g,">")}function _s(t,e){let r=t.split("<item>").slice(1),o=[];for(let n of r){let s=n.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/i);if(!s)continue;let i=De(s[1]),a=i.match(/urn:btih:([a-zA-Z0-9]+)/)?.[1]?.toLowerCase()??"";if(!a)continue;let l=De(n.match(/<title>(.*?)<\/title>/)?.[1]??"Unknown Title"),c=n.match(/<pubDate>(.*?)<\/pubDate>/)?.[1]??"",u=c?new Date(c).getTime()/1e3:0;o.push({infoHash:a,name:l,sizeBytes:0,seeders:0,leechers:0,source:e,magnet:i,added:u})}return o}async function Ro(t,e,r,o={}){let n=r.trim(),s=n?`${t}/?s=${encodeURIComponent(n)}&feed=rss2`:`${t}/feed/`,i=await Q(s,{headers:{"User-Agent":G},signal:o.signal});if(!i.ok)throw new H(i.status,`${e} feed returned ${i.status}`);return _s(await i.text(),e)}var Eo="https://fitgirl-repacks.site",Bo={id:"fitgirl",label:"FitGirl",group:"Games",homepage:Eo,search:(t,e)=>Ro(Eo,"fitgirl",t,e)};var Ls="https://nyaa.si/";function qe(t,e){return t.match(new RegExp(`<${e}>(?:<!\\[CDATA\\[)?(.*?)(?:\\]\\]>)?</${e}>`,"s"))?.[1]?.trim()??""}async function Ns(t,e={}){let r=new URLSearchParams({page:"rss",q:t.trim(),c:"0_0",f:"0"}),o=await Q(`${Ls}?${r.toString()}`,{headers:{"User-Agent":G},signal:e.signal});if(!o.ok)throw new H(o.status,`Nyaa returned ${o.status}`);let n=await o.text(),s=[];for(let i of n.split("<item>").slice(1)){let a=qe(i,"nyaa:infoHash").toLowerCase(),l=De(qe(i,"title"));if(!a||!l)continue;let c=Number(qe(i,"nyaa:seeders")),u=Number(qe(i,"nyaa:leechers")),p=qe(i,"pubDate");s.push({infoHash:a,name:l,sizeBytes:Bt(qe(i,"nyaa:size")),seeders:Number.isFinite(c)?c:0,leechers:Number.isFinite(u)?u:0,source:"nyaa",magnet:de(a,l),added:p?new Date(p).getTime()/1e3:void 0})}return s}var Io={id:"nyaa",label:"Nyaa",group:"Anime",homepage:"https://nyaa.si",search:Ns};var Fs="https://subsplease.org/api/",Us=["1080","720","480"];function zs(t){for(let e of Us){let r=t.find(o=>o.res===e&&o.magnet);if(r)return r}return t.find(e=>e.magnet)}async function Gs(t,e={}){let r=t.trim(),o=new URLSearchParams({tz:"UTC"});r?(o.set("f","search"),o.set("s",r)):o.set("f","latest");let n=await Q(`${Fs}?${o.toString()}`,{headers:{"User-Agent":G},signal:e.signal});if(!n.ok)throw new H(n.status,`SubsPlease returned ${n.status}`);let s=await n.json();if(!s||Array.isArray(s))return[];let i=[];for(let a of Object.values(s)){let l=zs(a.downloads??[]);if(!l?.magnet)continue;let c=Fe(l.magnet);if(!c)continue;let u=a.show??"Unknown",p=a.episode?` - ${a.episode}`:"",f=l.magnet.match(/[?&]xl=(\d+)/);i.push({infoHash:c.infoHash,name:`${u}${p} [${l.res??"?"}p]`,sizeBytes:f?Number(f[1]):0,seeders:0,leechers:0,source:"subsplease",magnet:c.magnet,added:a.release_date?new Date(a.release_date).getTime()/1e3:void 0})}return i}var Mo={id:"subsplease",label:"SubsPlease",group:"Anime",homepage:"https://subsplease.org",search:Gs};async function Qs(t,e={}){let r=t.trim()||"tv show",n=`https://solidtorrents.net/api/v1/search?${new URLSearchParams({q:r}).toString()}`,s=await Q(n,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!s.ok)throw new H(s.status,`SolidTorrents returned ${s.status}`);let i=await s.json(),a=[];for(let l of i.results??[]){if(!l.infohash)continue;let c=l.infohash.toLowerCase(),u=l.title||"Unknown",p=l.updatedAt?Math.floor(new Date(l.updatedAt).getTime()/1e3):void 0;a.push({infoHash:c,name:u,sizeBytes:l.size??0,seeders:l.seeders??0,leechers:l.leechers??0,source:"solid",magnet:de(c,u),added:p})}return a}var ko={id:"solid",label:"Solid",group:"TV",homepage:"https://solidtorrents.net",search:Qs};var ur="https://apibay.org",Ws=new Set([201,202,207,209]),js=new Set([205,208]),Vs=`${ur}/precompiled/data_top100_207.json`,qs=`${ur}/precompiled/data_top100_208.json`,Ys="0000000000000000000000000000000000000000";function Ks(t,e){let r=(t.info_hash??"").toLowerCase();if(!r||r===Ys||t.id==="0")return null;let o=t.name||"Unknown",n=Number(t.num_files);return{infoHash:r,name:o,sizeBytes:Number(t.size)||0,seeders:Number(t.seeders)||0,leechers:Number(t.leechers)||0,numFiles:Number.isFinite(n)&&n>0?n:void 0,source:e,magnet:de(r,o),added:Number(t.added)||void 0}}async function Js(t,e){let r=await Q(t,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!r.ok)throw new H(r.status,`Pirate Bay returned ${r.status}`);let o=await r.json();return Array.isArray(o)?o:[]}async function Ao(t,e,r,o,n){let s=t.trim(),i=await Js(s?`${ur}/q.php?q=${encodeURIComponent(s)}`:r,n),a=[];for(let l of i){if(s&&!e.has(Number(l.category)))continue;let c=Ks(l,o);c&&a.push(c)}return a}var Po={id:"tpb-movies",label:"TPB",group:"Movies",homepage:"https://thepiratebay.org",search:(t,e={})=>Ao(t,Ws,Vs,"tpb-movies",e)},Ho={id:"tpb-tv",label:"TPB",group:"TV",homepage:"https://thepiratebay.org",search:(t,e={})=>Ao(t,js,qs,"tpb-tv",e)};var Xs=["1337x.to","1337x.st","x1337x.ws","1337xx.to"],Zs=8,ei=new Set(["the","a","an","of","and","or","to"]);function ti(t){let e=t.indexOf("table-list");if(e<0)return[];let r=[];for(let o of t.slice(e).split(/<tr[\s>]/i).slice(1)){let n=o.match(/href="(\/torrent\/[^"]+)"[^>]*>([^<]+)<\/a>/i);if(!n)continue;let s=o.match(/class="coll-4 size[^"]*">\s*([\d.]+\s*[KMGT]i?B)/i)?.[1]??"";r.push({name:De(n[2].trim()),path:n[1],seeders:Number(o.match(/class="coll-2 seeds[^"]*">\s*(\d+)/i)?.[1]??0),leechers:Number(o.match(/class="coll-3 leeches[^"]*">\s*(\d+)/i)?.[1]??0),sizeBytes:Bt(s)})}return r}async function $o(t,e,r){let o=await Q(t,{headers:{"User-Agent":G},signal:e.signal,retries:r});if(!o.ok)throw new H(o.status,`1337x returned ${o.status}`);return o.text()}async function ri(t,e,r){try{let n=(await $o(`${t}${e}`,r,1)).match(/magnet:\?xt=urn:btih:[^"'<>\s]+/i)?.[0];return n?De(n):null}catch{return null}}async function Do(t,e,r,o={}){let n=t.trim(),s=n?`/category-search/${encodeURIComponent(n).replace(/%20/g,"+")}/${e}/1/`:`/popular-${e==="Movies"?"movies":"tv"}`,i="",a="",l;for(let y of Xs)try{let R=`https://${y}`;a=await $o(`${R}${s}`,o,2),i=R;break}catch(R){if(o.signal?.aborted)throw R;l=R}if(!i)throw l instanceof Error?l:new H(0,"1337x unreachable");let c=ti(a),u=n.toLowerCase().split(/\s+/).filter(Boolean),p=u.filter(y=>!ei.has(y)),f=p.length?p:u,x=f.length?c.filter(y=>{let R=y.name.toLowerCase();return f.every(k=>R.includes(k))}):c;x.sort((y,R)=>R.seeders-y.seeders);let v=x.slice(0,Zs);return(await Promise.all(v.map(async y=>{let R=await ri(i,y.path,o),k=R?.match(/urn:btih:([a-zA-Z0-9]+)/i)?.[1]?.toLowerCase();return!R||!k?null:{infoHash:k,name:y.name,sizeBytes:y.sizeBytes,seeders:y.seeders,leechers:y.leechers,source:r,magnet:R}}))).filter(y=>y!==null)}var Oo={id:"x1337-movies",label:"1337x",group:"Movies",homepage:"https://1337x.to",search:(t,e={})=>Do(t,"Movies","x1337-movies",e)},_o={id:"x1337-tv",label:"1337x",group:"TV",homepage:"https://1337x.to",search:(t,e={})=>Do(t,"TV","x1337-tv",e)};var oi=["yts.mx","yts.am","yts.rs"];async function ni(t,e){let r;for(let o of oi)try{let n=await Q(`https://${o}/api/v2/list_movies.json?${t.toString()}`,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(n.ok)return await n.json();r=new H(n.status,`YTS returned ${n.status}`)}catch(n){if(e.signal?.aborted)throw n;r=n}throw r instanceof Error?r:new H(0,"YTS unreachable")}async function si(t,e={}){let r=t.trim(),o=new URLSearchParams({limit:"50"});r?o.set("query_term",r):o.set("sort_by","date_added");let n=await ni(o,e),s=[];for(let i of n.data?.movies??[]){let a=i.title_long||i.title||"Unknown";for(let l of i.torrents??[]){if(!l.hash)continue;let c=l.hash.toLowerCase(),u=[l.quality,l.type].filter(Boolean).join(" "),p=u?`${a} [${u}]`:a;s.push({infoHash:c,name:p,sizeBytes:l.size_bytes??0,seeders:l.seeds??0,leechers:l.peers??0,source:"yts",magnet:de(c,p),added:i.date_uploaded_unix})}}return s}var Lo={id:"yts",label:"YTS",group:"Movies",homepage:"https://yts.mx",search:si};var ee=[Bo,Lo,Po,Oo,Co,ko,Ho,_o,Io,Mo],ii=ee[0];function No(t){return ee.find(e=>e.id===t)??ii}var ai=["Games","Movies","TV","Anime"];function Fo(){return ai.map(t=>({group:t,sources:ee.filter(e=>e.group===t)})).filter(t=>t.sources.length>0)}var Uo=new Map;function li(t,e){return`${t}::${e.trim().toLowerCase()}`}async function zo(t,e,r={}){let o=li(t.id,e),n=Uo.get(o);if(n&&Date.now()-n.at<3e5)return n.results;let s=await t.search(e,r);return Uo.set(o,{at:Date.now(),results:s}),s}function di(t,e){return e?"timed out":t instanceof H&&t.status>0?`HTTP ${t.status}`:"no response"}var mi=25e3;function Go(t){let e={};for(let r of ee)e[r.id]={loading:t,error:null,code:null,count:0};return e}function fi(t){let e=new Map;for(let r of t){let o=e.get(r.infoHash);(!o||r.seeders>o.seeders)&&e.set(r.infoHash,r)}return[...e.values()]}function pi(t){return t.sort((e,r)=>r.seeders!==e.seeders?r.seeders-e.seeders:(r.added??0)-(e.added??0))}function hi(){return{results:[],perSource:Go(!1),loading:!1,done:0,total:ee.length}}function Qo(t){let[e,r]=ui(hi);return ci(()=>{let o=new AbortController,n=!0,s=[],i=Go(!0),a=0;r({results:[],perSource:{...i},loading:!0,done:0,total:ee.length});for(let l of ee){let c=new AbortController,u=()=>c.abort();o.signal.addEventListener("abort",u);let p=setTimeout(()=>c.abort(),mi);zo(l,t,{signal:c.signal}).then(f=>{n&&(s.push(...f),i[l.id]={loading:!1,error:null,code:null,count:f.length})}).catch(f=>{if(!n||o.signal.aborted)return;let x=c.signal.aborted;i[l.id]={loading:!1,error:x?"timed out":f instanceof Error?f.message:String(f),code:di(f,x),count:0}}).finally(()=>{clearTimeout(p),o.signal.removeEventListener("abort",u),n&&(a+=1,r({results:pi(fi(s.slice())),perSource:{...i},loading:a<ee.length,done:a,total:ee.length}))})}return()=>{n=!1,o.abort()}},[t]),e}var dr=["none",{field:"size",dir:"asc"},{field:"size",dir:"desc"},{field:"seeders",dir:"asc"},{field:"seeders",dir:"desc"},{field:"source",dir:"asc"},{field:"source",dir:"desc"}];function gi(t,e){return t==="none"||e==="none"?t===e:t.field===e.field&&t.dir===e.dir}function Wo(t){let e=dr.findIndex(r=>gi(r,t));return dr[(e+1)%dr.length]}function mr(t){return t==="asc"?"\u25B4":"\u25BE"}function jo(t){return t==="none"?"default":`${t.field} ${mr(t.dir)}`}function Vo(t,e){let r=t.slice();if(e==="none")return r;let o=e.dir==="asc"?1:-1;switch(e.field){case"size":r.sort((n,s)=>o*(n.sizeBytes-s.sizeBytes)||s.seeders-n.seeders);break;case"seeders":r.sort((n,s)=>o*(n.seeders-s.seeders)||(s.added??0)-(n.added??0));break;case"source":r.sort((n,s)=>o*n.source.localeCompare(s.source)||s.seeders-n.seeders);break}return r}import{Fragment as Nt,jsx as m,jsxs as te}from"react/jsx-runtime";var xi="Search or paste a magnet link\u2026";function Ye({label:t,value:e}){return te(M,{children:[m(M,{width:9,flexShrink:0,children:m(w,{dimColor:!0,children:t})}),m(M,{flexGrow:1,minWidth:0,children:e})]})}function Si({r:t,width:e}){let r=Re[t.source],o=ot(t.added),n=t.seeders||t.leechers?te(w,{children:[m(w,{color:t.seeders>0?d.good:void 0,bold:t.seeders>0,children:t.seeders}),m(w,{dimColor:!0,children:` seeders ${b.dot} ${t.leechers} leechers`})]}):m(w,{dimColor:!0,children:"unknown"});return te(M,{flexDirection:"column",children:[te(M,{children:[m(M,{flexGrow:1,minWidth:0,children:m(w,{bold:!0,color:d.text,wrap:"truncate-end",children:ie(t.name)})}),m(M,{flexShrink:0,marginLeft:2,children:m(w,{color:r.color,bold:!0,children:r.tag})})]}),m(Pt,{width:e}),te(M,{marginTop:1,flexDirection:"column",children:[m(Ye,{label:"Size",value:t.sizeBytes>0?m(w,{color:d.text,children:be(t.sizeBytes)}):m(w,{dimColor:!0,children:"unknown"})}),m(Ye,{label:"Health",value:n}),t.numFiles?m(Ye,{label:"Files",value:m(w,{dimColor:!0,children:String(t.numFiles)})}):null,o?m(Ye,{label:"Added",value:m(w,{dimColor:!0,children:o})}):null,m(Ye,{label:"Hash",value:m(w,{color:d.alt,dimColor:!0,wrap:"truncate-end",children:t.infoHash})}),m(Ye,{label:"Magnet",value:m(w,{color:d.alt,dimColor:!0,wrap:"truncate-end",children:t.magnet})})]}),te(M,{marginTop:1,children:[m(w,{color:d.accent,bold:!0,children:"d"}),m(w,{color:d.text,children:" Download"}),m(w,{dimColor:!0,children:` ${b.dot} `}),m(w,{color:d.accent,bold:!0,children:"y"}),m(w,{color:d.text,children:" Copy magnet"}),m(w,{dimColor:!0,children:` ${b.dot} `}),m(w,{color:d.alt,children:"esc"}),m(w,{dimColor:!0,children:" back"})]})]})}function qo(){let{query:t,submitQuery:e,section:r,region:o,setRegion:n,setCaptureMode:s,startDownload:i,copyMagnet:a,contentWidth:l,listRows:c}=X(),u=Qo(t),[p,f]=Lt("none"),x=pr(()=>{let g=nt.find(O=>O.key===r),A=g?.group?u.results.filter(O=>No(O.source).group===g.group):u.results;return Vo(A,p)},[u.results,r,p]),v=o==="content",[S,y]=Lt("list"),[R,k]=Lt(0),[F,L]=Lt(null);fr(()=>{k(0)},[x]),fr(()=>{if(v)return s(S==="search"?"text":S==="detail"?"esc":"none"),()=>s("none")},[S,v,s]),fr(()=>{v||y("list")},[v]);let $=Math.min(R,Math.max(0,x.length-1)),V=Math.max(5,c-3-1),D=Math.max(3,V-4),ge=Math.max(1,D-1),ne=g=>i({id:g.infoHash,name:g.name,magnet:g.magnet,source:g.source,sizeBytes:g.sizeBytes}),xe=g=>a({name:g.name,magnet:g.magnet});hr((g,A)=>{if(g==="/"){y("search");return}if(A.upArrow){x.length>0&&$>0?k($-1):y("search");return}if(x.length!==0)if(A.downArrow)k(he($,1,x.length));else if(A.pageUp)k(Math.max(0,$-ge));else if(A.pageDown)k(Math.min(x.length-1,$+ge));else if(A.return){let O=x[$];O&&(L(O),y("detail"))}else if(g==="d"){let O=x[$];O&&ne(O)}else if(g==="y"){let O=x[$];O&&xe(O)}else g==="s"&&f(O=>Wo(O))},{isActive:v&&S==="list"}),hr((g,A)=>{A.escape?(y("list"),L(null)):g==="d"&&F?ne(F):g==="y"&&F&&xe(F)},{isActive:v&&S==="detail"}),hr((g,A)=>{A.escape&&y("list")},{isActive:v&&S==="search"});let q=g=>{y("list"),e(g)},fe=t.trim()==="",Y=pr(()=>Object.values(u.perSource).filter(g=>g.error).length,[u.perSource]),pe=nt.find(g=>g.key===r),ue=pe?.group?ee.filter(g=>g.group===pe.group):ee,h=ue.length>0&&ue.every(g=>u.perSource[g.id]?.error),j=pr(()=>x.some(g=>g.sizeBytes>0||g.seeders>0),[x]),C=Math.max(2,String(x.length).length),se=g=>{let A=[...new Set(g.map(O=>u.perSource[O.id]?.code).filter(Boolean))];return A.length?` (${A.join(", ")})`:""},Se=()=>{if(u.loading)return x.length>0?m(w,{dimColor:!0,children:`searching\u2026 ${u.done}/${u.total} sources`}):m($t,{label:`${fe?"Loading":"Searching"} ${u.done}/${u.total} sources`});if(x.length===0){if(Y>=u.total){let K=ee.filter(ve=>u.perSource[ve.id]?.error);return m(w,{color:d.warn,children:`Couldn't reach any source. They may be down${se(K)}.`})}if(h&&pe){let K=ue.filter(yt=>u.perSource[yt.id]?.error),ve=K.length===1?"The source":`All ${K.length} sources`;return m(w,{color:d.warn,children:`Couldn't reach ${pe.label}. ${ve} may be down${se(K)}.`})}return u.results.length>0&&pe?.group?m(w,{dimColor:!0,children:`No ${pe.label.toLowerCase()} results yet. Try another tab or a search.`}):m(w,{dimColor:!0,children:fe?"Nothing new right now.":`No results for "${J(t,28)}".`})}let g=Y>0?` (${Y} source${Y===1?"":"s"} down)`:"",A=fe?"newest across all sources":`${x.length} result${x.length===1?"":"s"}`,O=p==="none"?"":` ${b.dot} sort: ${jo(p)}`;return m(w,{dimColor:!0,children:`${A}${g}${O}`})},Xe=(g,A)=>p==="none"||p.field!==g?A:te(Nt,{children:[m(w,{color:d.accent,bold:!0,children:mr(p.dir)}),A]}),ye=Ae($,x.length,D),xt=x.slice(ye,ye+D),St=x.length>0?`(${x.length})`:void 0;return te(M,{flexDirection:"column",children:[m(_t,{width:l,value:t,editing:S==="search",placeholder:xi,onSubmit:q,onExitDown:()=>y("list"),onExitLeft:()=>n("sidebar")}),m(M,{marginTop:1,children:m(me,{title:S==="detail"?"details":fe?"latest":"results",width:l,focused:v&&S!=="search",count:S==="detail"?void 0:St,height:V,children:S==="detail"&&F?m(Si,{r:F,width:Math.max(10,l-4)}):te(Nt,{children:[m(M,{children:Se()}),te(M,{flexDirection:"column",marginTop:x.length>0?1:0,children:[x.length>0?te(M,{children:[m(M,{width:Z,flexShrink:0}),m(M,{width:C,flexShrink:0,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:"#"})}),m(M,{flexGrow:1,minWidth:0,marginLeft:1,children:m(w,{bold:!0,dimColor:!0,children:"Name"})}),j?te(Nt,{children:[m(M,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("size","Size")})}),m(M,{width:9,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("seeders","Seed:Lch")})})]}):m(M,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:"Added"})}),m(M,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("source","Src")})})]}):null,xt.map((g,A)=>{let O=ye+A,K=O===$&&v&&S==="list",ve=Re[g.source];return te(M,{children:[m(M,{width:Z,flexShrink:0,children:m(w,{color:d.accent,children:K?b.pointer:""})}),m(M,{width:C,flexShrink:0,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:O+1})}),m(M,{flexGrow:1,minWidth:0,marginLeft:1,children:m(w,{wrap:"truncate-end",color:K?d.accent:void 0,dimColor:!K,bold:K,children:ie(g.name)})}),j?te(Nt,{children:[m(M,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:g.sizeBytes>0?be(g.sizeBytes):"-"})}),m(M,{width:9,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{color:g.seeders>0?d.good:void 0,dimColor:g.seeders===0,children:g.seeders||g.leechers?`${g.seeders}:${g.leechers}`:"-"})})]}):m(M,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:ot(g.added)||"-"})}),m(M,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{color:ve.color,dimColor:!K,children:ve.tag})})]},g.infoHash)})]})]})})})]})}import{useEffect as vi,useState as Ci}from"react";import{Box as U,Text as re,useInput as Ri}from"ink";import{useEffect as wi,useState as bi}from"react";import{Text as ct}from"ink";var Yo="#f4efff";function Ko(t){return Math.ceil(t+4.5*2)+8}function Jo(t,e){return t*.45%e-4.5}function Xo(t,e){let r=Math.abs(t-e);return r>=4.5?0:.5*(1+Math.cos(Math.PI*r/4.5))*.9}import{jsx as gr,jsxs as rn}from"react/jsx-runtime";var Ti="#7c5cd6";function Zo(t,e,r,o){return t<=.5?ae(e,r,t/.5):ae(r,o,(t-.5)/.5)}function en(t){let e=[];for(let r of t){let o=e[e.length-1];o&&o.color===r?o.len++:e.push({color:r,len:1})}return e}function tn(t){return t.map((e,r)=>gr(ct,{color:e.color,children:"\u2588".repeat(e.len)},r))}function on({pct:t,width:e,color:r=d.accent,animate:o=!1}){let n=Math.max(0,Math.min(100,t)),s=Math.round(n/100*e),i=Math.max(0,e-s),a=Math.max(1,e-1),[l,c]=bi(0);wi(()=>{if(!o)return;let v=setInterval(()=>c(S=>S+1),40);return v.unref?.(),()=>clearInterval(v)},[o]);let u=i>0?gr(ct,{color:we,children:"\u2591".repeat(i)}):null;if(s===0)return gr(ct,{children:u});if(!o){let v=ae(r,"#000000",.3),S=ae(r,d.text,.35),y=Array.from({length:s},(R,k)=>Zo(k/a,v,r,S));return rn(ct,{children:[tn(en(y)),u]})}let p=Ko(e),f=Jo(l,p),x=Array.from({length:s},(v,S)=>{let y=Zo(S/a,Ti,d.accent,d.bright),R=Xo(S,f);return R>0&&(y=ae(y,Yo,R)),y});return rn(ct,{children:[tn(en(x)),u]})}import{jsx as B,jsxs as dt}from"react/jsx-runtime";var ut=2,Ft=2,Ei="#7c7785";function Bi(t){return t==="failed"?d.bad:t==="paused"?Ei:d.accent}function Ii(t){return t==="failed"?b.error:t==="paused"?b.pause:b.down}function Mi(t){if(t.status==="downloading"){let e=rt(t.speed)||"\u2026",r=t.eta?` ${Zr(t.eta)}`:"";return`${t.progress}% ${e} ${b.peer}${t.peers}${r}`}return t.status==="paused"?`paused ${t.progress}%`:J(t.error||"failed",28)}function nn(){let{queue:t,region:e,contentWidth:r,listRows:o,startDownload:n,setDownloadFocus:s}=X(),i=Ue(t),a=Mt(t),l=e==="content",c=i.length+a.length,[u,p]=Ci(0),f=Math.min(u,Math.max(0,c-1)),x=f<i.length,v=f-i.length;Ri((h,j)=>{if(j.upArrow)p(he(f,-1,c));else if(j.downArrow)p(he(f,1,c));else if(h==="f")t.retryFailed();else if(h==="x")t.clearHistory();else if(x){let C=i[f];if(!C)return;h==="c"?t.cancel(C.id):h==="p"&&t.togglePause(C.id)}else{let C=a[v];if(!C)return;j.return||h==="d"?n({id:C.id,name:C.name,magnet:C.magnet,source:C.source,sizeBytes:C.sizeBytes}):h==="c"&&t.removeHistory(C.id)}},{isActive:l&&c>0});let S=null;if(l&&c>0)if(!x)S="recent";else{let h=i[f]?.status;(h==="downloading"||h==="paused"||h==="failed")&&(S=h)}vi(()=>(s(S),()=>s(null)),[S,s]);let y=Math.max(5,o-1);if(c===0)return B(me,{title:"downloads",width:r,focused:l,height:y,children:B(re,{dimColor:!0,children:"No downloads yet. Find something and press d to grab it."})});let R=i.length>0,k=a.length>0,F=k?1:0,L=Math.max(1,y-1),$=R&&k?1:0,I=0,V=0;if(!k)I=Math.max(1,Math.floor(L/ut));else if(!R)V=Math.max(1,L-F);else{let h=L-F-$;h<ut+1&&($=0,h=L-F);let j=Math.max(ut,Math.floor(h*.55));I=Math.min(i.length,Math.max(1,Math.floor(j/ut))),V=Math.max(1,h-I*ut)}let D=Ae(x?f:0,i.length,I),ge=i.slice(D,D+I),ne=Ae(x?0:v,a.length,V),xe=a.slice(ne,ne+V),q=r-4,fe=2,Y=Math.max(8,Math.min(28,Math.floor(q*.4))),pe=Math.max(6,q-Ft-Z-Y-fe),ue=R?`(${i.length})`:void 0;return dt(me,{title:"downloads",width:r,focused:l,count:ue,height:y,children:[ge.map((h,j)=>{let C=D+j===f&&l&&x,se=Bi(h.status),Se=Re[h.source??"fitgirl"];return dt(U,{flexDirection:"column",children:[dt(U,{children:[B(U,{width:Ft,flexShrink:0,children:B(re,{color:d.accent,bold:!0,children:C?b.pointer:""})}),B(U,{width:Z,flexShrink:0,children:B(re,{color:se,children:Ii(h.status)})}),B(U,{flexGrow:1,minWidth:0,children:B(re,{wrap:"truncate-end",bold:C,color:C?d.accent:void 0,dimColor:!C,children:ie(h.name)})}),B(U,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:h.totalBytes>0?be(h.totalBytes):"-"})}),B(U,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{color:h.source?Se.color:void 0,dimColor:!h.source||!C,children:h.source?Se.tag:"mag"})})]}),dt(U,{children:[B(U,{width:Ft+Z,flexShrink:0}),B(on,{pct:h.progress,width:Y,color:se,animate:h.status==="downloading"}),B(U,{marginLeft:fe,flexShrink:0,children:B(re,{dimColor:!0,children:J(Mi(h),pe)})})]})]},h.id)}),k?B(U,{marginTop:$?1:0,children:B(re,{dimColor:!0,children:`Recently downloaded${a.length>1?` (${a.length})`:""}`})}):null,xe.map((h,j)=>{let C=ne+j===v&&l&&!x,se=Re[h.source??"fitgirl"],Se=ot(h.completedAt/1e3);return dt(U,{children:[B(U,{width:Ft,flexShrink:0,children:B(re,{color:d.accent,bold:!0,children:C?b.pointer:""})}),B(U,{width:Z,flexShrink:0,children:B(re,{color:d.good,dimColor:!C,children:b.done})}),B(U,{flexGrow:1,minWidth:0,children:B(re,{wrap:"truncate-end",bold:C,color:C?d.accent:void 0,dimColor:!C,children:ie(h.name)})}),B(U,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:h.sizeBytes>0?be(h.sizeBytes):"-"})}),B(U,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:Se||"-"})}),B(U,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{color:h.source?se.color:void 0,dimColor:!h.source||!C,children:h.source?se.tag:"mag"})})]},h.id)})]})}import{useEffect as ki,useState as Ai}from"react";import{Box as W,Text as oe,useInput as Pi}from"ink";import{jsx as P,jsxs as mt}from"react/jsx-runtime";var sn=2,an=10,xr=14,ln=4,Hi="#7c7785";function $i(t){return t?t.status==="seeding"?{icon:b.up,color:d.good}:t.status==="paused"?{icon:b.pause,color:Hi}:{icon:b.warn,color:d.warn}:{icon:b.done,color:d.good}}function Di(t){return t?t.status==="seeding"?{text:`${b.up}${rt(t.uploadSpeed)||"0 B/s"} ${b.peer}${t.peers}`,color:d.good,dim:!1}:t.status==="paused"?{text:"paused",dim:!0}:{text:"file gone",color:d.warn,dim:!1}:{text:"ready",dim:!0}}function cn(){let{queue:t,region:e,contentWidth:r,listRows:o,setNotice:n,setSeedFocus:s}=X(),i=Mt(t),a=eo(t),l=e==="content",c=i.length,[u,p]=Ai(0),f=Math.min(u,Math.max(0,c-1)),x=l&&c>0?a.get(i[f]?.id??"")?.status??"idle":null;ki(()=>(s(x),()=>s(null)),[x,s]),Pi((I,V)=>{if(V.upArrow)p(he(f,-1,c));else if(V.downArrow)p(he(f,1,c));else if(I==="p"){let D=i[f];if(!D)return;t.toggleSeeding(D),t.getSeed(D.id)?.status==="missing"&&n(`${b.warn} That file isn't on disk anymore.`)}else if(I==="c"){let D=i[f];D&&t.removeHistory(D.id)}},{isActive:l&&c>0});let v=Math.max(5,o-1),S=t.seedingCount;if(c===0)return P(me,{title:"seeding",width:r,focused:l,height:v,children:P(oe,{dimColor:!0,children:"Nothing here yet. Downloads start seeding automatically when they finish, and show up here."})});let y=0,R=0,k=0;for(let I of a.values())k+=I.uploaded,I.status==="seeding"&&(y+=I.uploadSpeed,R+=I.peers);let F=Math.max(1,v-2),L=Ae(f,c,F),$=i.slice(L,L+F);return mt(me,{title:"seeding",width:r,focused:l,count:S>0?`(${S})`:void 0,height:v,children:[P(W,{children:S>0?mt(oe,{color:d.good,children:[b.up," ",rt(y)||"0 B/s",P(oe,{dimColor:!0,children:` ${b.dot} ${R} peers ${b.dot} ${be(k)} shared back`})]}):P(oe,{dimColor:!0,children:"Downloads seed automatically when they finish. Press p to pause or resume any of them."})}),mt(W,{flexDirection:"column",marginTop:1,children:[mt(W,{children:[P(W,{width:sn,flexShrink:0}),P(W,{width:Z,flexShrink:0}),P(W,{flexGrow:1,minWidth:0,marginLeft:1,children:P(oe,{bold:!0,dimColor:!0,children:"Name"})}),P(W,{width:an,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Size"})}),P(W,{width:xr,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Status"})}),P(W,{width:ln,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Src"})})]}),$.map((I,V)=>{let D=L+V===f&&l,ge=a.get(I.id),ne=$i(ge),xe=Di(ge),q=Re[I.source??"fitgirl"];return mt(W,{children:[P(W,{width:sn,flexShrink:0,children:P(oe,{color:d.accent,bold:!0,children:D?b.pointer:""})}),P(W,{width:Z,flexShrink:0,children:P(oe,{color:ne.color,dimColor:!ge&&!D,children:ne.icon})}),P(W,{flexGrow:1,minWidth:0,marginLeft:1,children:P(oe,{wrap:"truncate-end",bold:D,color:D?d.accent:void 0,dimColor:!D,children:ie(I.name)})}),P(W,{width:an,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{dimColor:!0,children:I.sizeBytes>0?be(I.sizeBytes):"-"})}),P(W,{width:xr,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{color:xe.color,dimColor:xe.dim,children:J(xe.text,xr)})}),P(W,{width:ln,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{color:I.source?q.color:void 0,dimColor:!I.source||!D,children:I.source?q.tag:"mag"})})]},I.id)})]})]})}import{useEffect as Oi}from"react";function Sr(){let{queue:t}=X();Ue(t);let e=t.activeCount;return Oi(()=>{let r=e>0?`\u2193${e} \xB7 torlink`:"torlink";process.stdout.write(`\x1B]0;${r}\x07`),process.platform==="win32"&&(process.title=r)},[e]),null}import{Box as ft,Text as le,useInput as _i,useStdin as Li}from"ink";import{jsx as z,jsxs as un}from"react/jsx-runtime";var Ni=Fo().map(t=>t.group.toLowerCase()).join(` ${b.dot} `);function dn(){let{submitQuery:t,quitAll:e,cols:r,rows:o}=X(),{isRawModeSupported:n}=Li();_i((a,l)=>{(l.escape||l.ctrl&&a==="c")&&e()},{isActive:n});let s=r>=to+2,i=Math.max(24,Math.min(r-6,62));return un(ft,{height:Math.max(1,o-1),flexDirection:"column",justifyContent:"center",alignItems:"center",children:[s?z(At,{}):z(le,{bold:!0,color:d.accent,children:"torlink"}),z(ft,{marginTop:2,children:z(le,{color:d.text,children:"A curated, terminal-native torrent downloader."})}),z(ft,{children:z(le,{dimColor:!0,children:Ni})}),z(ft,{marginTop:1,width:i,children:z(_t,{width:i,value:"",editing:!0,placeholder:"Search or paste a magnet link\u2026",onSubmit:t})}),z(ft,{marginTop:1,children:un(le,{children:[z(le,{color:d.alt,children:"\u21B5"}),z(le,{dimColor:!0,children:" search"}),z(le,{dimColor:!0,children:` ${b.dot} `}),z(le,{dimColor:!0,children:"empty "}),z(le,{color:d.alt,children:"\u21B5"}),z(le,{dimColor:!0,children:" browse"}),z(le,{dimColor:!0,children:` ${b.dot} `}),z(le,{color:d.alt,children:"^c"}),z(le,{dimColor:!0,children:" quit"})]})})]})}import{Box as Ut,Text as Ke,useInput as Fi}from"ink";import{jsx as Te,jsxs as yr}from"react/jsx-runtime";function mn({width:t,value:e,onSubmit:r,onCancel:o}){return Fi((n,s)=>{s.escape&&o()}),yr(Ut,{flexDirection:"column",width:t,children:[Te(me,{title:"download folder",width:t,focused:!0,height:2,children:yr(Ut,{children:[Te(Ke,{color:d.accent,children:`${b.pointer} `}),Te(Ut,{flexGrow:1,minWidth:0,children:Te(Dt,{defaultValue:e,placeholder:"~/Downloads/torlink",onSubmit:r})})]})}),yr(Ut,{marginTop:1,children:[Te(Ke,{color:d.alt,children:"\u21B5"}),Te(Ke,{dimColor:!0,children:" save"}),Te(Ke,{dimColor:!0,children:` ${b.dot} `}),Te(Ke,{color:d.alt,children:"esc"}),Te(Ke,{dimColor:!0,children:" cancel"})]})]})}import{useEffect as Ui}from"react";function fn(){Ui(()=>{let{stdout:t,stdin:e}=process;t.write("\x1B[?1000h\x1B[?1006h");let r=o=>{let n=o.toString("utf8"),s=/\x1b\[<(64|65);\d+;\d+[Mm]/g,i;for(;(i=s.exec(n))!==null;){let a=i[1]==="64"?"\x1B[A":"\x1B[B";process.nextTick(()=>e.emit("data",Buffer.from(a)))}};return e.prependListener("data",r),()=>{t.write("\x1B[?1000l\x1B[?1006l"),e.removeListener("data",r)}},[])}import{jsx as N,jsxs as ht}from"react/jsx-runtime";function pn({initialMagnet:t,initialTorrent:e,onQuit:r}={}){fn();let{exit:o}=Wi(),{isRawModeSupported:n}=qi(),{stdout:s}=Vi(),[i,a]=ce({rows:s?.rows??24,cols:s?.columns??80});pt(()=>{if(!s)return;let T={rows:s.rows??24,cols:s.columns??80},E=()=>{let _={rows:s.rows??24,cols:s.columns??80};_.rows===T.rows&&_.cols===T.cols||((_.rows<T.rows||_.cols<T.cols)&&s.write("\x1B[2J\x1B[H"),T=_,a(_))};return s.on("resize",E),()=>{s.off("resize",E)}},[s]);let l=i.rows,c=i.cols,[u,p]=ce(null),[f,x]=ce(null),[v,S]=ce("splash"),[y,R]=ce(""),[k,F]=ce("all"),[L,$]=ce("content"),[I,V]=ce("none"),[D,ge]=ce(null),[ne,xe]=ce(null),[q,fe]=ce(!1),[Y,pe]=ce(!1),[ue,h]=ce(null),j=Gi(!1);pt(()=>{if(j.current)return;j.current=!0;let T=!0;return(async()=>{let E=await Br(),_=new Et;if(_.restore(qr(await Nr())),_.restoreHistory(await jr()),_.restoreSeeds(await Qr()),!T){_.suspend();return}x(E),p(_);let wt=t?Fe(t):e?await Yr(e):null;wt&&(await wr.mkdir(E.downloadDir,{recursive:!0}).catch(()=>{}),_.add({id:wt.infoHash,name:wt.name,magnet:wt.magnet},E.downloadDir),S("browser"),F("downloads"),$("content"))})(),()=>{T=!1}},[t,e]),pt(()=>{if(!u)return;let T=E=>h(`${b.done} ${J(ie(E),40)}`);return u.on("completed",T),()=>{u.off("completed",T)}},[u]),pt(()=>()=>{u?.suspend()},[u]);let C=Ie(()=>{u?.persistSync(),r?r():o()},[u,r,o]),se=Ie(T=>{x(T),Ir(T)},[]),Se=Ie(()=>{pe(!1)},[]),Xe=Ie(T=>{Se();let E=Ar(T);if(!f||!E||E===f.downloadDir){f&&E&&E===f.downloadDir&&h("Download folder unchanged.");return}(async()=>{try{await wr.mkdir(E,{recursive:!0})}catch{h(`Couldn't use folder: ${J(E,48)}`);return}se({...f,downloadDir:E}),h(`Download folder: ${J(E,48)}`)})()},[f,se,Se]),ye=Ie(T=>{!f||!u||(wr.mkdir(f.downloadDir,{recursive:!0}).catch(()=>{}),u.add(T,f.downloadDir),h(`Added: ${J(ie(T.name),40)}`),F("downloads"),$("content"))},[f,u]),xt=Ie(T=>{(async()=>{if(await Xr(T.magnet)){h(`Copied magnet: ${J(ie(T.magnet),60)}`);return}h(`Couldn't copy magnet for ${J(ie(T.name),32)}.`)})()},[]),St=Ie(T=>{let E=T.trim();if(E){let _=Fe(E);if(_){ye({id:_.infoHash,name:_.name,magnet:_.magnet}),S("browser");return}}R(E),S("browser"),k==="downloads"&&F("all"),$("content")},[k,ye]),g=Ie(async()=>{let T=(await Jr()).trim();if(!T){h("Clipboard is empty.");return}let E=T.match(/magnet:\?xt=urn:btih:[^\s"'<>]+/i)?.[0],_=E?Fe(E):null;if(_){ye({id:_.infoHash,name:_.name,magnet:_.magnet}),S("browser");return}h("No magnet link on the clipboard.")},[ye]);pt(()=>{if(!ue)return;let T=setTimeout(()=>h(null),4e3);return()=>clearTimeout(T)},[ue]);let A=l<18,O=!A,K=l>=12,ve=3+(O?1:0)+(A?0:1)+(K?1:0),yt=Math.max(6,l-1-ve),br=Math.max(4,yt),Tr=Math.max(24,c-or-3),Sn=Math.max(10,c-2),Ze=zi(()=>!u||!f?null:{config:f,setConfig:se,queue:u,view:v,setView:S,query:y,submitQuery:St,section:k,setSection:F,region:q||Y?"help":L,setRegion:$,captureMode:I,setCaptureMode:V,downloadFocus:D,setDownloadFocus:ge,seedFocus:ne,setSeedFocus:xe,startDownload:ye,copyMagnet:xt,notice:ue,setNotice:h,quitAll:C,listRows:br,compact:A,contentWidth:Tr,cols:c,rows:l},[u,f,v,y,St,k,L,q,Y,I,D,ne,ye,xt,ue,br,A,Tr,c,l,se,C]);return ji((T,E)=>{if(E.ctrl&&T==="c"){C();return}if(!Y&&I!=="text"){if(q){fe(!1);return}if(T==="?"){fe(!0);return}if(T==="o"){fe(!1),pe(!0);return}if(T==="m"){g();return}if(E.tab){$(L==="sidebar"?"content":"sidebar");return}if(E.rightArrow){L==="sidebar"&&$("content");return}if(E.leftArrow){L==="content"&&$("sidebar");return}if(E.escape){if(I==="esc")return;if(L==="content"){$("sidebar");return}S("splash");return}if(T==="q"){C();return}}},{isActive:n&&v==="browser"&&!!Ze}),Ze?v==="splash"?ht(It.Provider,{value:Ze,children:[N(Sr,{}),N(dn,{})]}):ht(It.Provider,{value:Ze,children:[N(Sr,{}),ht(Me,{flexDirection:"column",paddingX:1,children:[ht(Me,{justifyContent:"space-between",children:[N(At,{}),ue?N(Qi,{color:d.good,children:ue}):null]}),O?N(Pt,{width:Sn}):null,q?N(Me,{marginTop:1,children:N(ho,{})}):null,Y?N(Me,{marginTop:1,children:N(mn,{width:Math.max(24,Math.min(c-4,62)),value:Ze.config.downloadDir,onSubmit:Xe,onCancel:Se})}):null,ht(Me,{height:yt,marginTop:A?0:1,display:q||Y?"none":"flex",overflow:"hidden",children:[N(co,{}),N(Me,{flexGrow:1,flexDirection:"column",children:k==="downloads"?N(nn,{}):k==="seeding"?N(cn,{}):N(qo,{})})]}),K?N(Me,{display:q||Y?"none":"flex",children:N(uo,{hints:mo(L,k,D,ne)})}):null]})]}):N(Me,{height:l,justifyContent:"center",alignItems:"center",children:N($t,{label:"Starting torlink"})})}import{jsx as Ki}from"react/jsx-runtime";var Je=vr(process.argv.slice(2));Je.kind==="help"&&(console.log(Gt),process.exit(0));Je.kind==="version"&&(console.log(`torlink v${Cr}`),process.exit(0));Je.kind==="invalid"&&(console.error(`error: unknown argument '${Je.arg}'
|
|
13
|
+
`;var Cr="0.0.1";import{useCallback as Ie,useEffect as pt,useMemo as zi,useRef as Gi,useState as ce}from"react";import{Box as Me,Text as Qi,useApp as Wi,useInput as ji,useStdout as Vi,useStdin as qi}from"ink";import{promises as wr}from"fs";import{promises as vn}from"fs";import yn from"os";import Ce from"path";import wn from"env-paths";var Rr="torlink",Er=wn(Rr,{suffix:""}),bt=process.env.TORLINK_STATE_DIR,Tt=bt?Ce.join(bt,"data"):Er.data,bn=bt?Ce.join(bt,"config"):Er.config,Qt=Ce.join(yn.homedir(),"Downloads",Rr),Wt=Ce.join(bn,"config.json"),Oe=Ce.join(Tt,"queue.json"),_e=Ce.join(Tt,"history.json"),Le=Ce.join(Tt,"seeds.json"),jt=Ce.join(Tt,"torrents");import{promises as Vt}from"fs";import Tn from"path";function Ne(){let t=Promise.resolve();return e=>(t=t.then(e).catch(()=>{}),t)}async function ke(t,e){await Vt.mkdir(Tn.dirname(t),{recursive:!0});let r=`${t}.tmp`;await Vt.writeFile(r,JSON.stringify(e,null,2),"utf8"),await Vt.rename(r,t)}var qt={downloadDir:Qt};async function Br(){let t;try{t=await vn.readFile(Wt,"utf8")}catch{return{...qt}}try{let e=JSON.parse(t),r={...qt,...e};return(!r.downloadDir||typeof r.downloadDir!="string")&&(r.downloadDir=Qt),r}catch{return{...qt}}}var Cn=Ne();function Ir(t){return Cn(()=>ke(Wt,t))}import Mr from"os";import kr from"path";function Rn(t,e=Mr.homedir()){let r=t.trim();return r==="~"?e:r.startsWith("~/")||r.startsWith("~\\")?kr.join(e,r.slice(2)):r}function Ar(t,e=Mr.homedir()){let r=Rn(t,e);return r?kr.normalize(r):""}import{EventEmitter as _n}from"events";import En from"webtorrent";function Pr(t){return t instanceof Error?t.message:String(t)}var vt=class{client=null;torrents=new Map;ensureClient(){return this.client||(this.client=new En,this.client.on("error",()=>{})),this.client}add(e,r,o,n){let s=this.ensureClient(),i=this.torrents.get(e);if(i){this.torrents.delete(e);try{i.destroy()}catch{}}let a;try{a=s.add(r,{path:o})}catch(l){n.onError?.(Pr(l));return}this.torrents.set(e,a),a.on("metadata",()=>{n.onMetadata?.({name:a.name,total:a.length,files:a.files?.length??0,torrentFile:a.torrentFile})}),a.on("done",()=>{n.onDone?.()}),a.on("error",l=>{n.onError?.(Pr(l)),this.torrents.delete(e);try{a.destroy()}catch{}})}listenPort(){return this.client?.torrentPort??null}stats(e){let r=this.torrents.get(e);return r?{progress:r.progress,downloaded:r.downloaded,total:r.length,speed:r.downloadSpeed,uploadSpeed:r.uploadSpeed,uploaded:r.uploaded,peers:r.numPeers,timeRemaining:r.timeRemaining,name:r.name}:null}remove(e){let r=this.torrents.get(e);if(this.torrents.delete(e),r)try{r.destroy()}catch{}}destroy(){this.torrents.clear();let e=this.client;this.client=null,e&&setImmediate(()=>{try{e.destroy()}catch{}})}};import{promises as et,mkdirSync as Hr,writeFileSync as $r,renameSync as Dr,existsSync as Bn,rmSync as In}from"fs";import Yt from"path";var Or=Ne();function _r(t){return Or(()=>ke(Oe,t))}function Lr(t){try{Hr(Yt.dirname(Oe),{recursive:!0});let e=`${Oe}.sync.tmp`;$r(e,JSON.stringify(t,null,2),"utf8"),Dr(e,Oe)}catch{}}function Mn(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.id=="string"&&typeof e.magnet=="string"}async function Nr(){let t;try{t=await et.readFile(Oe,"utf8")}catch{return[]}try{let e=JSON.parse(t);return Array.isArray(e)?e.filter(Mn):[]}catch{return[]}}function Fr(t){return Or(()=>ke(Le,t))}function Ur(t){try{Hr(Yt.dirname(Le),{recursive:!0});let e=`${Le}.sync.tmp`;$r(e,JSON.stringify(t,null,2),"utf8"),Dr(e,Le)}catch{}}function tt(t){return Yt.join(jt,`${t}.torrent`)}function zr(t){return Bn(tt(t))}async function Gr(t,e){try{await et.mkdir(jt,{recursive:!0});let r=tt(t),o=`${r}.tmp`;await et.writeFile(o,e),await et.rename(o,r)}catch{}}function Ct(t){try{In(tt(t),{force:!0})}catch{}}async function Qr(){let t;try{t=await et.readFile(Le,"utf8")}catch{return[]}try{let e=JSON.parse(t);if(!Array.isArray(e))return[];let r=[];for(let o of e)if(typeof o=="string")r.push({id:o,status:"seeding"});else if(o&&typeof o=="object"){let n=o;typeof n.id=="string"&&(n.status==="seeding"||n.status==="paused")&&r.push({id:n.id,status:n.status})}return r}catch{return[]}}import{promises as kn,mkdirSync as An,writeFileSync as Pn,renameSync as Hn}from"fs";import $n from"path";var Kt=500,Dn=Ne();function Rt(t){return Dn(()=>ke(_e,t.slice(0,Kt)))}function Wr(t){try{An($n.dirname(_e),{recursive:!0});let e=`${_e}.sync.tmp`;Pn(e,JSON.stringify(t.slice(0,Kt),null,2),"utf8"),Hn(e,_e)}catch{}}function On(t){if(!t||typeof t!="object")return!1;let e=t;return typeof e.id=="string"&&typeof e.name=="string"&&typeof e.magnet=="string"}async function jr(){let t;try{t=await kn.readFile(_e,"utf8")}catch{return[]}try{let e=JSON.parse(t);return Array.isArray(e)?e.filter(On).slice(0,Kt):[]}catch{return[]}}function Ln(t){return t.total>0&&t.progress<1&&t.speed>0}var Nn=2,Fn=1e4,Un=500,Vr=500,Et=class extends _n{items=new Map;engine=new vt;poll=null;history=[];seeds=new Map;strayHits=new Map;seedStartedAt=new Map;getItems(){return[...this.items.values()].sort((e,r)=>r.addedAt-e.addedAt)}get activeCount(){let e=0;for(let r of this.items.values())r.status==="downloading"&&e++;return e}has(e){return this.items.has(e)}add(e,r){this.seeds.has(e.id)&&(this.engine.remove(e.id),this.seeds.delete(e.id),this.strayHits.delete(e.id),this.seedStartedAt.delete(e.id),this.persistSeeds());let o=this.items.get(e.id);if(o&&o.status!=="failed")return;let n=o?{...o,status:"downloading",error:void 0,speed:0}:{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:r,status:"downloading",progress:0,totalBytes:e.sizeBytes??0,downloadedBytes:0,speed:0,peers:0,addedAt:Date.now()};this.items.set(n.id,n),this.startEngine(n),this.ensurePoll(),this.changed(),this.persist()}startEngine(e){this.engine.add(e.id,e.magnet,e.dir,this.engineHandlers(e.id))}engineHandlers(e){return{onMetadata:r=>{r.torrentFile&&Gr(e,r.torrentFile);let o=this.items.get(e);o&&(r.name&&(o.name=r.name),r.total&&(o.totalBytes=r.total),o.files=r.files,this.changed(),this.persist())},onDone:()=>{let r=this.items.get(e);if(r){r.totalBytes&&(r.downloadedBytes=r.totalBytes),this.complete(r);return}this.seeds.has(e)&&(this.strayHits.set(e,0),this.seedStartedAt.delete(e))},onError:r=>{let o=this.items.get(e);if(o){o.status="failed",o.error=r,o.speed=0,o.peers=0,this.changed(),this.persist(),this.maybeStopPoll();return}let n=this.seeds.get(e);n&&(n.status="missing",n.uploadSpeed=0,n.peers=0,this.seedStartedAt.delete(e),this.changed(),this.persistSeeds(),this.maybeStopPoll())}}}complete(e){this.recordHistory(e),this.items.delete(e.id),this.beginSeed(e),this.emit("completed",e.name),this.changed(),this.persist(),this.maybeStopPoll()}beginSeed(e){e.magnet&&(this.seeds.set(e.id,{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.totalBytes,status:"seeding",uploadSpeed:0,uploaded:0,peers:0}),this.strayHits.set(e.id,0),this.seedStartedAt.set(e.id,Date.now()),this.ensurePoll(),this.persistSeeds())}tick(){let e=!1;for(let o of this.items.values()){if(o.status!=="downloading")continue;let n=this.engine.stats(o.id);n&&(o.progress=Math.min(100,Math.round(n.progress*100)),o.downloadedBytes=n.downloaded,n.total&&(o.totalBytes=n.total),o.speed=n.speed,o.peers=n.peers,o.eta=n.timeRemaining>0&&Number.isFinite(n.timeRemaining)?n.timeRemaining/1e3:void 0,n.name&&(o.name=n.name),e=!0)}let r=Date.now();for(let o of this.seeds.values()){if(o.status!=="seeding")continue;let n=this.engine.stats(o.id);if(!n)continue;if(r-(this.seedStartedAt.get(o.id)??0)>Fn&&Ln(n)){let i=(this.strayHits.get(o.id)??0)+1;this.strayHits.set(o.id,i),i>=Nn&&(this.engine.remove(o.id),this.strayHits.delete(o.id),this.seedStartedAt.delete(o.id),o.status="missing",o.uploadSpeed=0,o.peers=0,this.persistSeeds()),e=!0;continue}this.strayHits.set(o.id,0),o.uploadSpeed=n.uploadSpeed,o.uploaded=n.uploaded,o.peers=n.peers,e=!0}e&&this.changed()}ensurePoll(){this.poll||(this.poll=setInterval(()=>this.tick(),Un),this.poll.unref())}maybeStopPoll(){this.activeCount===0&&this.seedingCount===0&&this.poll&&(clearInterval(this.poll),this.poll=null)}pause(e){let r=this.items.get(e);!r||r.status!=="downloading"||(r.status="paused",r.speed=0,r.peers=0,r.eta=void 0,this.engine.remove(e),this.changed(),this.persist(),this.maybeStopPoll())}resume(e){let r=this.items.get(e);!r||r.status!=="paused"||(r.status="downloading",this.startEngine(r),this.ensurePoll(),this.changed(),this.persist())}togglePause(e){let r=this.items.get(e);r&&(r.status==="downloading"?this.pause(e):r.status==="paused"&&this.resume(e))}cancel(e){this.items.has(e)&&(this.engine.remove(e),this.items.delete(e),Ct(e),this.changed(),this.persist(),this.maybeStopPoll())}retry(e){let r=this.items.get(e);!r||r.status!=="failed"||(r.status="downloading",r.error=void 0,this.startEngine(r),this.ensurePoll(),this.changed(),this.persist())}retryFailed(){for(let e of[...this.items.values()])e.status==="failed"&&this.retry(e.id)}getSeed(e){return this.seeds.get(e)}getSeeds(){return[...this.seeds.values()]}get seedingCount(){let e=0;for(let r of this.seeds.values())r.status==="seeding"&&e++;return e}startSeeding(e){if(this.seeds.get(e.id)?.status==="seeding"||this.items.has(e.id))return;let r={id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.sizeBytes,status:"seeding",uploadSpeed:0,uploaded:0,peers:0};if(!e.magnet){this.seeds.set(e.id,{...r,status:"missing"}),this.changed(),this.persistSeeds();return}this.seeds.set(e.id,r),this.strayHits.set(e.id,0),this.seedStartedAt.set(e.id,Date.now());let o=zr(e.id)?tt(e.id):e.magnet;this.engine.add(e.id,o,e.dir,this.engineHandlers(e.id)),this.ensurePoll(),this.changed(),this.persistSeeds()}stopSeeding(e){let r=this.seeds.get(e);r&&(this.engine.remove(e),this.strayHits.delete(e),this.seedStartedAt.delete(e),r.status==="seeding"&&(r.status="paused",r.uploadSpeed=0,r.peers=0),this.changed(),this.persistSeeds(),this.maybeStopPoll())}toggleSeeding(e){this.seeds.get(e.id)?.status==="seeding"?this.stopSeeding(e.id):this.startSeeding(e)}restoreSeeds(e){for(let r of e){let o=this.history.find(n=>n.id===r.id);o&&(r.status==="seeding"?this.startSeeding(o):this.restorePaused(o))}}restorePaused(e){this.seeds.has(e.id)||(this.seeds.set(e.id,{id:e.id,name:e.name,source:e.source,magnet:e.magnet,dir:e.dir,sizeBytes:e.sizeBytes,status:"paused",uploadSpeed:0,uploaded:0,peers:0}),this.changed())}seedRecords(){let e=[];for(let r of this.seeds.values())r.status==="seeding"?e.push({id:r.id,status:"seeding"}):e.push({id:r.id,status:"paused"});return e}persistSeeds(){return Fr(this.seedRecords()).catch(()=>{})}restore(e){for(let r of e)this.items.set(r.id,r),r.status==="downloading"&&this.startEngine(r);this.activeCount>0&&this.ensurePoll(),this.changed()}restoreHistory(e){this.history=e.slice(0,Vr)}getHistory(){return this.history}recordHistory(e){let r={id:e.id,name:e.name,source:e.source,sizeBytes:e.totalBytes,magnet:e.magnet,dir:e.dir,completedAt:Date.now()};this.history=[r,...this.history.filter(o=>o.id!==e.id)].slice(0,Vr),Rt(this.history).catch(()=>{})}removeHistory(e){let r=this.history.filter(o=>o.id!==e);r.length!==this.history.length&&(this.history=r,this.seeds.has(e)&&(this.engine.remove(e),this.seeds.delete(e),this.strayHits.delete(e),this.seedStartedAt.delete(e),this.persistSeeds(),this.maybeStopPoll()),Ct(e),Rt(this.history).catch(()=>{}),this.changed())}clearHistory(){if(this.history.length!==0){for(let e of this.history)Ct(e.id);if(this.history=[],this.seeds.size>0){for(let e of this.seeds.keys())this.engine.remove(e);this.seeds.clear(),this.strayHits.clear(),this.seedStartedAt.clear(),this.persistSeeds(),this.maybeStopPoll()}Rt(this.history).catch(()=>{}),this.changed()}}changed(){this.emit("update")}async persist(){await _r(this.getItems()).catch(()=>{})}persistSync(){Lr(this.getItems()),Wr(this.history),Ur(this.seedRecords())}suspend(){for(let e of this.items.values())e.status==="downloading"&&(e.speed=0,e.peers=0,e.eta=void 0);this.persistSync(),this.poll&&(clearInterval(this.poll),this.poll=null),this.engine.destroy()}};function qr(t){let e=new Set,r=[];for(let o of t){if(!o?.id||e.has(o.id)||(e.add(o.id),o.status==="completed"))continue;let n=o.status==="failed"?"failed":o.status==="paused"?"paused":"downloading";r.push({...o,status:n,speed:0,peers:0,eta:void 0})}return r}var zn=["udp://tracker.opentrackr.org:1337/announce","udp://open.demonii.com:1337/announce","udp://tracker.openbittorrent.com:6969/announce","udp://tracker.torrent.eu.org:451/announce","udp://exodus.desync.com:6969/announce","udp://open.stealth.si:80/announce","udp://tracker.dler.org:6969/announce"];function de(t,e){let r=encodeURIComponent(e),o=zn.map(n=>`&tr=${encodeURIComponent(n)}`).join("");return`magnet:?xt=urn:btih:${t}&dn=${r}${o}`}var Gn=/xt=urn:btih:([a-f0-9]{40}|[a-z2-7]{32})/i,Qn="ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";function Wn(t){let e=0,r=0,o="";for(let n of t.toUpperCase()){let s=Qn.indexOf(n);if(s===-1)return null;r=r<<5|s,e+=5,e>=8&&(e-=8,o+=(r>>>e&255).toString(16).padStart(2,"0"),r&=(1<<e)-1)}return o.length===40?o:null}function jn(t){return t.length===32?Wn(t)??t.toLowerCase():t.toLowerCase()}function Fe(t){let e=t.trim();if(!/^magnet:\?/i.test(e))return null;let r=Gn.exec(e);if(!r)return null;let o=jn(r[1]),n=o;try{let s=new URL(e).searchParams.get("dn");s&&(n=s)}catch{}return{infoHash:o,name:n,magnet:e}}import{promises as Vn}from"fs";import qn from"parse-torrent";async function Yr(t){try{let e=await Vn.readFile(t),r=await qn(new Uint8Array(e)),o=r?.infoHash?.toLowerCase();if(!o)return null;let n=r.name||o;return{infoHash:o,name:n,magnet:de(o,n)}}catch{return null}}import{spawn as Kr}from"child_process";function Jt(t,e){return new Promise(r=>{let o="";try{let n=Kr(t,e,{windowsHide:!0}),s=setTimeout(()=>{try{n.kill()}catch{}r("")},4e3);s.unref?.(),n.stdout.on("data",i=>o+=i.toString("utf8")),n.on("error",()=>{clearTimeout(s),r("")}),n.on("close",()=>{clearTimeout(s),r(o)})}catch{r("")}})}function Xt(t,e,r){return new Promise(o=>{try{let n=Kr(t,e,{windowsHide:!0}),s=!1,i,a=l=>{s||(s=!0,clearTimeout(i),o(l))};i=setTimeout(()=>{try{n.kill()}catch{}a(!1)},4e3),i.unref?.(),n.on("error",()=>a(!1)),n.on("close",l=>a(l===0)),n.stdin?.end(r)}catch{o(!1)}})}var Yn=[["wl-paste",["--no-newline"]],["xclip",["-selection","clipboard","-o"]],["xsel",["-b"]]],Kn=[["wl-copy",[]],["xclip",["-selection","clipboard"]],["xsel",["-b","-i"]]];async function Jr(){if(process.platform==="win32")return(await Jt("powershell",["-NoProfile","-Command","Get-Clipboard"])).trim();if(process.platform==="darwin")return(await Jt("pbpaste",[])).trim();for(let[t,e]of Yn){let r=(await Jt(t,e)).trim();if(r)return r}return""}async function Xr(t){if(process.platform==="win32")return Xt("powershell",["-NoProfile","-Command","Set-Clipboard -Value ([Console]::In.ReadToEnd())"],t);if(process.platform==="darwin")return Xt("pbcopy",[],t);for(let[e,r]of Kn)if(await Xt(e,r,t))return!0;return!1}function be(t){if(t===void 0||!Number.isFinite(t)||t<=0)return"0 B";let e=["B","KB","MB","GB","TB"],r=t,o=0;for(;r>=1024&&o<e.length-1;)r/=1024,o++;return`${r.toFixed(o===0?0:2)} ${e[o]}`}var Jn={B:1,KIB:1024,MIB:1024**2,GIB:1024**3,TIB:1024**4,KB:1e3,MB:1e6,GB:1e9,TB:1e12};function Bt(t){let e=t.match(/([\d.]+)\s*([KMGT]?I?B)/i);return e?Math.round(parseFloat(e[1])*(Jn[e[2].toUpperCase()]??1)):0}function rt(t){if(t===void 0||!Number.isFinite(t)||t<=0)return"";let e=["B/s","KB/s","MB/s","GB/s"],r=t,o=0;for(;r>=1024&&o<e.length-1;)r/=1024,o++;return`${r.toFixed(r<10&&o>0?1:0)} ${e[o]}`}function ot(t){if(!t||!Number.isFinite(t)||t<=0)return"";let e=Date.now()/1e3-t;if(e<60)return"now";let r=Math.floor(e/60);if(r<60)return`${r}m ago`;let o=Math.floor(r/60);if(o<24){let i=r%60;return i>0?`${o}hr ${i}m ago`:`${o}hr ago`}let n=Math.floor(o/24);if(n<30){let i=o%24;return i>0?`${n}d ${i}hr ago`:`${n}d ago`}let s=Math.floor(n/30);return s<12?`${s}mo ago`:`${Math.floor(s/12)}y ago`}function Zr(t){if(t===void 0||!Number.isFinite(t)||t<0)return"";let e=Math.round(t),r=Math.floor(e/86400),o=Math.floor(e%86400/3600),n=Math.floor(e%3600/60),s=e%60;return r>0?[`${r}d`,o>0?`${o}hr`:"",n>0?`${n}m`:""].filter(Boolean).join(" "):o>0?n>0?`${o}hr ${n}m`:`${o}hr`:n>0?s>0?`${n}m ${s}s`:`${n}m`:`${s}s`}function Xn(t){return t<32||t===127||t===65533||t>=8203&&t<=8207||t>=8232&&t<=8238||t===8288||t===65279||t===8205||t===65039||t===8419||t>=9728&&t<=10175||t>=11008&&t<=11263||t>=126976&&t<=131071}function ie(t){let e="";for(let r of t.normalize("NFC"))Xn(r.codePointAt(0))||(e+=r);return e.replace(/\s+/g," ").trim()||"Untitled"}function J(t,e){return e<=1?t.slice(0,Math.max(0,e)):t.length<=e?t:t.slice(0,e-1)+"\u2026"}import{createContext as Zn,useContext as es,useEffect as Zt,useState as er}from"react";var nt=[{key:"all",label:"All"},{key:"games",label:"Games",group:"Games"},{key:"movies",label:"Movies",group:"Movies"},{key:"tv",label:"TV",group:"TV"},{key:"anime",label:"Anime",group:"Anime"}],It=Zn(null);function X(){let t=es(It);if(!t)throw new Error("Store not available");return t}function Ue(t){let[e,r]=er(()=>t.getItems());return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(t.getItems())},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}function Mt(t){let[e,r]=er(()=>t.getHistory());return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(t.getHistory())},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}function eo(t){let[e,r]=er(()=>new Map(t.getSeeds().map(o=>[o.id,o])));return Zt(()=>{let o=null,n=()=>{o||(o=setTimeout(()=>{o=null,r(new Map(t.getSeeds().map(s=>[s.id,s])))},200))};return t.on("update",n),n(),()=>{t.off("update",n),o&&clearTimeout(o)}},[t]),e}import{Box as so,Text as tr}from"ink";var kt=[" \u{104CF} "," \u2580\u2588\u2580 \u2588\u2580\u2588 \u2588\u2580\u2588 \u2588 \u2588 \u2588\u2584 \u2588 \u2588\u2584\u2580"," \u2588 \u2588\u2584\u2588 \u2588\u2580\u2584 \u2588\u2584\u2584 \u2588 \u2588 \u2580\u2588 \u2588 \u2588"],to=Math.max(...kt.map(t=>[...t].length)),ro=new Set(["0,6"]);var d={accent:"#a78bfa",text:"#e9e4f5",alt:"#b9a7e6",good:"#86d6a2",warn:"#f0c560",bad:"#ee7d92",bright:"#d8b4fe"},b={done:"\u2713",error:"\u2717",pending:"\xB7",pointer:"\u276F",dot:"\xB7",warn:"\u26A0",bar:"\u258C",down:"\u2193",up:"\u2191",peer:"\u2022",pause:"\u23F8"},we="#6b6577",Z=2,Re={fitgirl:{tag:"FG",color:d.accent},yts:{tag:"YTS",color:d.good},eztv:{tag:"EZTV",color:d.warn},nyaa:{tag:"NYAA",color:d.bright},subsplease:{tag:"SUB",color:"#b9a7e6"},solid:{tag:"SLD",color:"#60a5fa"},"tpb-movies":{tag:"TPB",color:"#5fd0c5"},"tpb-tv":{tag:"TPB",color:"#5fd0c5"},"x1337-movies":{tag:"1337",color:"#f6a55c"},"x1337-tv":{tag:"1337",color:"#f6a55c"}};function oo(t){let e=parseInt(t.slice(1),16);return[e>>16&255,e>>8&255,e&255]}function ae(t,e,r){let[o,n,s]=oo(t),[i,a,l]=oo(e),c=(u,p)=>Math.round(u+(p-u)*r).toString(16).padStart(2,"0");return`#${c(o,i)}${c(n,a)}${c(s,l)}`}var no=[d.accent,d.bright];import{jsx as st}from"react/jsx-runtime";var ts="#ffffff",io=d.bright,ao="#7c5cd6",rs="#4c3a8a",os="#5ae87a";function ns(t){return t<.15?ae(ts,io,t/.15):t<.4?ae(io,d.accent,(t-.15)/.25):t<.7?ae(d.accent,ao,(t-.4)/.3):ae(ao,rs,(t-.7)/.3)}function At(){let t=kt.length;return st(so,{flexDirection:"column",children:kt.map((e,r)=>{let o=Math.max(0,r-1),n=Math.max(1,t-1),s=o/(n-1||1),i=[...e],a=Math.max(1,i.length-1);return st(so,{children:i.map((l,c)=>{if(l===" ")return st(tr,{children:" "},c);if(ro.has(`${r},${c}`))return st(tr,{bold:!0,color:os,children:l},c);let p=(c/a+s)/2;return st(tr,{bold:!0,color:ns(p),children:l},c)})},r)})})}import{Box as it,Text as rr,useInput as ss}from"ink";function he(t,e,r){return r<=0?0:((t+e)%r+r)%r}function Ae(t,e,r){if(e<=r)return 0;let o=Math.floor(r/2);return Math.max(0,Math.min(t-o,e-r))}import{jsx as Pe,jsxs as us}from"react/jsx-runtime";var is=nt.map(t=>({key:t.key,label:t.label})),as=[{key:"downloads",label:"Downloads"},{key:"seeding",label:"Seeding"}],ls=t=>t==="downloads"||t==="seeding",lo=[is,as],ze=lo.flat(),cs=5,or=Z+Math.max(...ze.map(t=>t.label.length+(ls(t.key)?cs:0)));function co(){let{section:t,setSection:e,region:r,setRegion:o,queue:n}=X(),s=r==="sidebar",i=Math.max(0,ze.findIndex(c=>c.key===t));Ue(n);let a=n.activeCount,l=n.seedingCount;return ss((c,u)=>{u.upArrow?e(ze[he(i,-1,ze.length)].key):u.downArrow?e(ze[he(i,1,ze.length)].key):u.return&&o("content")},{isActive:s}),Pe(it,{flexDirection:"column",width:or,marginRight:1,children:lo.map((c,u)=>Pe(it,{flexDirection:"column",marginTop:u>0?1:0,children:c.map(p=>{let f=p.key===t;return us(it,{children:[Pe(it,{width:Z,flexShrink:0,children:f?Pe(rr,{color:s?no[1]:we,bold:s,children:b.bar}):null}),Pe(rr,{color:f?s?d.accent:d.alt:void 0,dimColor:!f,bold:f&&s,children:p.label}),(()=>{let x=p.key==="downloads"?a:p.key==="seeding"?l:0;return x>0?Pe(it,{flexShrink:0,children:Pe(rr,{dimColor:!0,children:` (${x})`})}):null})()]},p.key)})},u))})}import{Text as ds}from"ink";import{jsx as ms}from"react/jsx-runtime";function Pt({width:t}){return ms(ds,{color:we,children:"\u2500".repeat(Math.max(1,t))})}import{Box as fs,Text as at}from"ink";import{jsx as lt,jsxs as ps}from"react/jsx-runtime";function uo({hints:t}){return lt(fs,{children:lt(at,{children:t.map((e,r)=>ps(at,{children:[r>0?lt(at,{dimColor:!0,children:" "}):null,lt(at,{color:d.alt,children:e.keys}),lt(at,{dimColor:!0,children:` ${e.label}`})]},e.keys+e.label))})})}import{Box as Qe,Text as We}from"ink";var Ge=[{title:"Navigate",hints:[{keys:"\u2191 \u2193 \u2190 \u2192",label:"Navigate content and panes"},{keys:"\u21B5",label:"Open"},{keys:"tab",label:"Switch pane"},{keys:"esc",label:"Back"},{keys:"o",label:"Download folder"},{keys:"q",label:"Quit"}]},{title:"Search",hints:[{keys:"/",label:"Edit search"},{keys:"\u21B5",label:"Run search"},{keys:"s",label:"Sort results"},{keys:"y",label:"Copy magnet"},{keys:"m",label:"Paste magnet"}]},{title:"Downloads",hints:[{keys:"p",label:"Pause/resume"},{keys:"c",label:"Cancel or remove from list"},{keys:"f",label:"Retry failed"},{keys:"d",label:"Download again"},{keys:"x",label:"Clear recent"}]},{title:"Seeding",hints:[{keys:"p",label:"Pause/resume"},{keys:"c",label:"Remove from list"}]}],nr={keys:"\u2191\u2193\u2190\u2192",label:"Move"},He={keys:"?",label:"Keys"},$e={keys:"tab",label:"Switch"};function mo(t,e,r,o){return t==="sidebar"?[nr,{keys:"\u21B5",label:"Open"},$e,He,{keys:"q",label:"Quit"}]:e==="seeding"?[{keys:"p",label:o==="seeding"?"Pause":o==="missing"?"Retry":"Resume"},{keys:"c",label:"Remove"},$e,He]:e==="downloads"?r==="paused"?[{keys:"p",label:"Resume"},{keys:"c",label:"Cancel"},$e,He]:r==="failed"?[{keys:"f",label:"Retry"},{keys:"c",label:"Remove"},$e,He]:r==="recent"?[nr,{keys:"d",label:"Download again"},{keys:"c",label:"Remove"},{keys:"x",label:"Clear"},$e,He]:[{keys:"p",label:"Pause"},{keys:"c",label:"Cancel"},$e,He]:[nr,{keys:"d",label:"Download"},{keys:"y",label:"Copy"},{keys:"s",label:"Sort"},{keys:"/",label:"Search"},$e,He]}import{jsx as Ee,jsxs as Ht}from"react/jsx-runtime";var hs=ae(d.accent,we,.55),gs=2,fo=2,sr=Ge.map(t=>Math.max(...t.hints.map(e=>e.keys.length))+gs),po=Ge.map((t,e)=>sr[e]+Math.max(...t.hints.map(r=>r.label.length))),xs=po.reduce((t,e)=>t+e,0)+(Ge.length-1)*fo+4,Ss=Math.max(...sr);function ho(){let{cols:t}=X(),e=t>=xs;return Ht(Qe,{flexDirection:"column",alignSelf:"flex-start",borderStyle:"round",borderColor:hs,paddingX:e?1:2,paddingY:1,children:[Ee(We,{bold:!0,color:d.accent,children:"Keyboard"}),Ee(Qe,{marginTop:1,flexDirection:e?"row":"column",children:Ge.map((r,o)=>Ht(Qe,{flexDirection:"column",width:e?po[o]:void 0,marginRight:e&&o<Ge.length-1?fo:0,marginTop:!e&&o>0?1:0,children:[Ee(We,{bold:!0,children:r.title}),r.hints.map(n=>Ht(Qe,{children:[Ee(Qe,{width:e?sr[o]:Ss,flexShrink:0,children:Ee(We,{color:d.alt,children:n.keys})}),Ee(We,{dimColor:!0,children:n.label})]},n.keys+n.label))]},r.title))}),Ht(Qe,{marginTop:1,flexDirection:"column",children:[Ee(We,{dimColor:!0,children:"Your downloaded files always stay on disk."}),Ee(We,{dimColor:!0,children:"Press ? or esc to close"})]})]})}import{useEffect as fr,useMemo as pr,useState as Lt}from"react";import{Box as M,Text as w,useInput as hr}from"ink";import{useEffect as ys,useState as ws}from"react";import{Text as ir}from"ink";import{jsx as xo,jsxs as bs}from"react/jsx-runtime";var go=["\u280B","\u2819","\u2839","\u2838","\u283C","\u2834","\u2826","\u2827","\u2807","\u280F"];function $t({label:t}){let[e,r]=ws(0);return ys(()=>{let o=setInterval(()=>r(n=>(n+1)%go.length),80);return o.unref?.(),()=>clearInterval(o)},[]),bs(ir,{children:[xo(ir,{color:d.accent,children:go[e]}),t?xo(ir,{dimColor:!0,children:` ${t}`}):null]})}import{Box as To,Text as cr}from"ink";import{useState as So}from"react";import{Text as Be,useInput as Ts}from"ink";import{jsx as je,jsxs as wo}from"react/jsx-runtime";function vs(t,e){return e===0?{value:t,cursor:e}:{value:t.slice(0,e-1)+t.slice(e),cursor:e-1}}function Cs(t,e){let r=e;for(;r>0&&t[r-1]===" ";)r--;for(;r>0&&t[r-1]!==" ";)r--;return{value:t.slice(0,r)+t.slice(e),cursor:r}}function Rs(t,e){return{value:t.slice(0,e),cursor:e}}function Es(t,e,r){return{value:t.slice(0,e)+r+t.slice(e),cursor:e+r.length}}var yo=" ";function Dt({isDisabled:t=!1,defaultValue:e="",placeholder:r="",onChange:o,onSubmit:n,onExitDown:s,onExitLeft:i}){let[a,l]=So(e),[c,u]=So(e.length);function p(S){l(S.value),u(Math.max(0,Math.min(S.value.length,S.cursor))),S.value!==a&&o?.(S.value)}if(Ts((S,y)=>{if(y.downArrow){s?.();return}if(y.upArrow||y.tab||y.ctrl&&S==="c")return;if(y.return){n?.(a);return}if(y.ctrl)switch(S){case"u":p({value:"",cursor:0});return;case"w":p(Cs(a,c));return;case"k":p(Rs(a,c));return;case"a":u(0);return;case"e":u(a.length);return;default:return}if(y.leftArrow){if(c===0){i?.();return}u(c-1);return}if(y.rightArrow){u(Math.min(a.length,c+1));return}if(y.backspace||y.delete){p(vs(a,c));return}if(y.meta||!S)return;let R=S.replace(/\x1b?\[<\d+;\d+;\d+[Mm]/g,"");R&&p(Es(a,c,R))},{isActive:!t}),t)return a?je(Be,{children:a}):je(Be,{dimColor:!0,children:r});if(a.length===0)return r?wo(Be,{children:[je(Be,{inverse:!0,children:r[0]}),je(Be,{dimColor:!0,children:r.slice(1)})]}):je(Be,{inverse:!0,children:yo});let f=a.slice(0,c),x=a[c]??yo,v=c<a.length?a.slice(c+1):"";return wo(Be,{children:[f,je(Be,{inverse:!0,children:x}),v]})}import{Box as ar,Text as lr}from"ink";import{jsx as Ot,jsxs as bo}from"react/jsx-runtime";function me({title:t,width:e,focused:r,count:o,height:n,children:s}){let i=r?d.accent:we,a=Math.max(10,e),l=t.charAt(0).toUpperCase()+t.slice(1),c=o?`${l} ${o}`:l,u=Math.max(0,a-5-c.length);return bo(ar,{flexDirection:"column",width:a,children:[bo(ar,{children:[Ot(lr,{color:i,children:"\u256D\u2500 "}),Ot(lr,{bold:!0,color:i,children:c}),Ot(lr,{color:i,children:` ${"\u2500".repeat(u)}\u256E`})]}),Ot(ar,{width:a,height:n,flexGrow:n?0:1,flexDirection:"column",borderStyle:"round",borderTop:!1,borderColor:i,paddingX:1,overflow:"hidden",children:s})]})}import{jsx as Ve,jsxs as Bs}from"react/jsx-runtime";function _t({width:t,value:e,placeholder:r="Search torrents\u2026",editing:o,onSubmit:n,onChange:s,onExitDown:i,onExitLeft:a}){return Ve(me,{title:"search",width:t,focused:o,height:2,children:Bs(To,{children:[Ve(cr,{color:d.accent,children:`${b.pointer} `}),Ve(To,{flexGrow:1,minWidth:0,children:o?Ve(Dt,{defaultValue:e,placeholder:r,onSubmit:n,onChange:s,onExitDown:i,onExitLeft:a}):e?Ve(cr,{wrap:"truncate-end",children:e}):Ve(cr,{dimColor:!0,children:r})})]})})}import{useEffect as ci,useState as ui}from"react";var G="torlink (+https://www.npmjs.com/package/torlnq)",H=class extends Error{status;constructor(e,r){super(r??`HTTP ${e}`),this.name="HttpError",this.status=e}},Is=new Set([408,425,429,500,502,503,504]),Ms=5,ks=500,As=2e4;function Ps(t){return new Promise(e=>setTimeout(e,t))}function Hs(t){return t instanceof Error&&(t.name==="AbortError"||/aborted/i.test(t.message))}function $s(t,e=Date.now()){if(!t)return;let r=t.trim();if(/^\d+$/.test(r))return Number(r)*1e3;let o=Date.parse(r);if(!Number.isNaN(o))return Math.max(0,o-e)}function vo(t,e,r,o,n=Math.random){let s=Math.min(r,e*2**t),i=Math.floor(n()*s);return o!==void 0?Math.max(i,o):i}async function Q(t,e={}){let{retries:r=Ms,baseMs:o=ks,capMs:n=As,fetchImpl:s=fetch,sleepImpl:i=Ps,signal:a,...l}=e,c=a?{...l,signal:a}:l,u;for(let p=0;p<=r;p++){if(a?.aborted)throw new H(0,"aborted");let f;try{f=await s(t,c)}catch(S){if(Hs(S)||a?.aborted)throw S;if(u=S,p<r){await i(vo(p,o,n));continue}throw S}if(!Is.has(f.status))return f;let x=f.headers.get("server")?.toLowerCase()||"";if(f.status===503&&(x.includes("ddos-guard")||x.includes("cloudflare")))throw new H(f.status,`Request to ${t} blocked by ${x} (HTTP ${f.status}).`);if(p>=r)throw new H(f.status,`Request to ${t} failed after ${r} retries (HTTP ${f.status}).`);let v=$s(f.headers.get("retry-after"));await i(vo(p,o,n,v))}throw u instanceof Error?u:new H(0,"fetchResilient exhausted without a response")}var Ds="https://eztvx.to/api/get-torrents";async function Os(t,e={}){if(t.trim())return[];let r=await Q(`${Ds}?limit=100&page=1`,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!r.ok)throw new H(r.status,`EZTV returned ${r.status}`);let o=await r.json(),n=[];for(let s of o.torrents??[]){let i=(s.hash??"").toLowerCase(),a=s.title||s.filename||i,l=s.magnet_url||(i?de(i,a):"");!l||!i||n.push({infoHash:i,name:a,sizeBytes:Number(s.size_bytes??0)||0,seeders:s.seeds??0,leechers:s.peers??0,source:"eztv",magnet:l,added:s.date_released_unix})}return n}var Co={id:"eztv",label:"EZTV",group:"TV",homepage:"https://eztvx.to",search:Os};function De(t){return t.replace(/�?38;|&/g,"&").replace(/–|—/g,"-").replace(/’|�?39;|'/g,"'").replace(/“|”|"/g,'"').replace(/</g,"<").replace(/>/g,">")}function _s(t,e){let r=t.split("<item>").slice(1),o=[];for(let n of r){let s=n.match(/href="(magnet:\?xt=urn:btih:[^"]+)"/i);if(!s)continue;let i=De(s[1]),a=i.match(/urn:btih:([a-zA-Z0-9]+)/)?.[1]?.toLowerCase()??"";if(!a)continue;let l=De(n.match(/<title>(.*?)<\/title>/)?.[1]??"Unknown Title"),c=n.match(/<pubDate>(.*?)<\/pubDate>/)?.[1]??"",u=c?new Date(c).getTime()/1e3:0;o.push({infoHash:a,name:l,sizeBytes:0,seeders:0,leechers:0,source:e,magnet:i,added:u})}return o}async function Ro(t,e,r,o={}){let n=r.trim(),s=n?`${t}/?s=${encodeURIComponent(n)}&feed=rss2`:`${t}/feed/`,i=await Q(s,{headers:{"User-Agent":G},signal:o.signal});if(!i.ok)throw new H(i.status,`${e} feed returned ${i.status}`);return _s(await i.text(),e)}var Eo="https://fitgirl-repacks.site",Bo={id:"fitgirl",label:"FitGirl",group:"Games",homepage:Eo,search:(t,e)=>Ro(Eo,"fitgirl",t,e)};var Ls="https://nyaa.si/";function qe(t,e){return t.match(new RegExp(`<${e}>(?:<!\\[CDATA\\[)?(.*?)(?:\\]\\]>)?</${e}>`,"s"))?.[1]?.trim()??""}async function Ns(t,e={}){let r=new URLSearchParams({page:"rss",q:t.trim(),c:"0_0",f:"0"}),o=await Q(`${Ls}?${r.toString()}`,{headers:{"User-Agent":G},signal:e.signal});if(!o.ok)throw new H(o.status,`Nyaa returned ${o.status}`);let n=await o.text(),s=[];for(let i of n.split("<item>").slice(1)){let a=qe(i,"nyaa:infoHash").toLowerCase(),l=De(qe(i,"title"));if(!a||!l)continue;let c=Number(qe(i,"nyaa:seeders")),u=Number(qe(i,"nyaa:leechers")),p=qe(i,"pubDate");s.push({infoHash:a,name:l,sizeBytes:Bt(qe(i,"nyaa:size")),seeders:Number.isFinite(c)?c:0,leechers:Number.isFinite(u)?u:0,source:"nyaa",magnet:de(a,l),added:p?new Date(p).getTime()/1e3:void 0})}return s}var Io={id:"nyaa",label:"Nyaa",group:"Anime",homepage:"https://nyaa.si",search:Ns};var Fs="https://subsplease.org/api/",Us=["1080","720","480"];function zs(t){for(let e of Us){let r=t.find(o=>o.res===e&&o.magnet);if(r)return r}return t.find(e=>e.magnet)}async function Gs(t,e={}){let r=t.trim(),o=new URLSearchParams({tz:"UTC"});r?(o.set("f","search"),o.set("s",r)):o.set("f","latest");let n=await Q(`${Fs}?${o.toString()}`,{headers:{"User-Agent":G},signal:e.signal});if(!n.ok)throw new H(n.status,`SubsPlease returned ${n.status}`);let s=await n.json();if(!s||Array.isArray(s))return[];let i=[];for(let a of Object.values(s)){let l=zs(a.downloads??[]);if(!l?.magnet)continue;let c=Fe(l.magnet);if(!c)continue;let u=a.show??"Unknown",p=a.episode?` - ${a.episode}`:"",f=l.magnet.match(/[?&]xl=(\d+)/);i.push({infoHash:c.infoHash,name:`${u}${p} [${l.res??"?"}p]`,sizeBytes:f?Number(f[1]):0,seeders:0,leechers:0,source:"subsplease",magnet:c.magnet,added:a.release_date?new Date(a.release_date).getTime()/1e3:void 0})}return i}var Mo={id:"subsplease",label:"SubsPlease",group:"Anime",homepage:"https://subsplease.org",search:Gs};async function Qs(t,e={}){let r=t.trim()||"tv show",n=`https://solidtorrents.net/api/v1/search?${new URLSearchParams({q:r}).toString()}`,s=await Q(n,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!s.ok)throw new H(s.status,`SolidTorrents returned ${s.status}`);let i=await s.json(),a=[];for(let l of i.results??[]){if(!l.infohash)continue;let c=l.infohash.toLowerCase(),u=l.title||"Unknown",p=l.updatedAt?Math.floor(new Date(l.updatedAt).getTime()/1e3):void 0;a.push({infoHash:c,name:u,sizeBytes:l.size??0,seeders:l.seeders??0,leechers:l.leechers??0,source:"solid",magnet:de(c,u),added:p})}return a}var ko={id:"solid",label:"Solid",group:"TV",homepage:"https://solidtorrents.net",search:Qs};var ur="https://apibay.org",Ws=new Set([201,202,207,209]),js=new Set([205,208]),Vs=`${ur}/precompiled/data_top100_207.json`,qs=`${ur}/precompiled/data_top100_208.json`,Ys="0000000000000000000000000000000000000000";function Ks(t,e){let r=(t.info_hash??"").toLowerCase();if(!r||r===Ys||t.id==="0")return null;let o=t.name||"Unknown",n=Number(t.num_files);return{infoHash:r,name:o,sizeBytes:Number(t.size)||0,seeders:Number(t.seeders)||0,leechers:Number(t.leechers)||0,numFiles:Number.isFinite(n)&&n>0?n:void 0,source:e,magnet:de(r,o),added:Number(t.added)||void 0}}async function Js(t,e){let r=await Q(t,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(!r.ok)throw new H(r.status,`Pirate Bay returned ${r.status}`);let o=await r.json();return Array.isArray(o)?o:[]}async function Ao(t,e,r,o,n){let s=t.trim(),i=await Js(s?`${ur}/q.php?q=${encodeURIComponent(s)}`:r,n),a=[];for(let l of i){if(s&&!e.has(Number(l.category)))continue;let c=Ks(l,o);c&&a.push(c)}return a}var Po={id:"tpb-movies",label:"TPB",group:"Movies",homepage:"https://thepiratebay.org",search:(t,e={})=>Ao(t,Ws,Vs,"tpb-movies",e)},Ho={id:"tpb-tv",label:"TPB",group:"TV",homepage:"https://thepiratebay.org",search:(t,e={})=>Ao(t,js,qs,"tpb-tv",e)};var Xs=["1337x.to","1337x.st","x1337x.ws","1337xx.to"],Zs=8,ei=new Set(["the","a","an","of","and","or","to"]);function ti(t){let e=t.indexOf("table-list");if(e<0)return[];let r=[];for(let o of t.slice(e).split(/<tr[\s>]/i).slice(1)){let n=o.match(/href="(\/torrent\/[^"]+)"[^>]*>([^<]+)<\/a>/i);if(!n)continue;let s=o.match(/class="coll-4 size[^"]*">\s*([\d.]+\s*[KMGT]i?B)/i)?.[1]??"";r.push({name:De(n[2].trim()),path:n[1],seeders:Number(o.match(/class="coll-2 seeds[^"]*">\s*(\d+)/i)?.[1]??0),leechers:Number(o.match(/class="coll-3 leeches[^"]*">\s*(\d+)/i)?.[1]??0),sizeBytes:Bt(s)})}return r}async function $o(t,e,r){let o=await Q(t,{headers:{"User-Agent":G},signal:e.signal,retries:r});if(!o.ok)throw new H(o.status,`1337x returned ${o.status}`);return o.text()}async function ri(t,e,r){try{let n=(await $o(`${t}${e}`,r,1)).match(/magnet:\?xt=urn:btih:[^"'<>\s]+/i)?.[0];return n?De(n):null}catch{return null}}async function Do(t,e,r,o={}){let n=t.trim(),s=n?`/category-search/${encodeURIComponent(n).replace(/%20/g,"+")}/${e}/1/`:`/popular-${e==="Movies"?"movies":"tv"}`,i="",a="",l;for(let y of Xs)try{let R=`https://${y}`;a=await $o(`${R}${s}`,o,2),i=R;break}catch(R){if(o.signal?.aborted)throw R;l=R}if(!i)throw l instanceof Error?l:new H(0,"1337x unreachable");let c=ti(a),u=n.toLowerCase().split(/\s+/).filter(Boolean),p=u.filter(y=>!ei.has(y)),f=p.length?p:u,x=f.length?c.filter(y=>{let R=y.name.toLowerCase();return f.every(k=>R.includes(k))}):c;x.sort((y,R)=>R.seeders-y.seeders);let v=x.slice(0,Zs);return(await Promise.all(v.map(async y=>{let R=await ri(i,y.path,o),k=R?.match(/urn:btih:([a-zA-Z0-9]+)/i)?.[1]?.toLowerCase();return!R||!k?null:{infoHash:k,name:y.name,sizeBytes:y.sizeBytes,seeders:y.seeders,leechers:y.leechers,source:r,magnet:R}}))).filter(y=>y!==null)}var Oo={id:"x1337-movies",label:"1337x",group:"Movies",homepage:"https://1337x.to",search:(t,e={})=>Do(t,"Movies","x1337-movies",e)},_o={id:"x1337-tv",label:"1337x",group:"TV",homepage:"https://1337x.to",search:(t,e={})=>Do(t,"TV","x1337-tv",e)};var oi=["yts.mx","yts.am","yts.rs"];async function ni(t,e){let r;for(let o of oi)try{let n=await Q(`https://${o}/api/v2/list_movies.json?${t.toString()}`,{headers:{"User-Agent":G},signal:e.signal,retries:1});if(n.ok)return await n.json();r=new H(n.status,`YTS returned ${n.status}`)}catch(n){if(e.signal?.aborted)throw n;r=n}throw r instanceof Error?r:new H(0,"YTS unreachable")}async function si(t,e={}){let r=t.trim(),o=new URLSearchParams({limit:"50"});r?o.set("query_term",r):o.set("sort_by","date_added");let n=await ni(o,e),s=[];for(let i of n.data?.movies??[]){let a=i.title_long||i.title||"Unknown";for(let l of i.torrents??[]){if(!l.hash)continue;let c=l.hash.toLowerCase(),u=[l.quality,l.type].filter(Boolean).join(" "),p=u?`${a} [${u}]`:a;s.push({infoHash:c,name:p,sizeBytes:l.size_bytes??0,seeders:l.seeds??0,leechers:l.peers??0,source:"yts",magnet:de(c,p),added:i.date_uploaded_unix})}}return s}var Lo={id:"yts",label:"YTS",group:"Movies",homepage:"https://yts.mx",search:si};var ee=[Bo,Lo,Po,Oo,Co,ko,Ho,_o,Io,Mo],ii=ee[0];function No(t){return ee.find(e=>e.id===t)??ii}var ai=["Games","Movies","TV","Anime"];function Fo(){return ai.map(t=>({group:t,sources:ee.filter(e=>e.group===t)})).filter(t=>t.sources.length>0)}var Uo=new Map;function li(t,e){return`${t}::${e.trim().toLowerCase()}`}async function zo(t,e,r={}){let o=li(t.id,e),n=Uo.get(o);if(n&&Date.now()-n.at<3e5)return n.results;let s=await t.search(e,r);return Uo.set(o,{at:Date.now(),results:s}),s}function di(t,e){return e?"timed out":t instanceof H&&t.status>0?`HTTP ${t.status}`:"no response"}var mi=25e3;function Go(t){let e={};for(let r of ee)e[r.id]={loading:t,error:null,code:null,count:0};return e}function fi(t){let e=new Map;for(let r of t){let o=e.get(r.infoHash);(!o||r.seeders>o.seeders)&&e.set(r.infoHash,r)}return[...e.values()]}function pi(t){return t.sort((e,r)=>r.seeders!==e.seeders?r.seeders-e.seeders:(r.added??0)-(e.added??0))}function hi(){return{results:[],perSource:Go(!1),loading:!1,done:0,total:ee.length}}function Qo(t){let[e,r]=ui(hi);return ci(()=>{let o=new AbortController,n=!0,s=[],i=Go(!0),a=0;r({results:[],perSource:{...i},loading:!0,done:0,total:ee.length});for(let l of ee){let c=new AbortController,u=()=>c.abort();o.signal.addEventListener("abort",u);let p=setTimeout(()=>c.abort(),mi);zo(l,t,{signal:c.signal}).then(f=>{n&&(s.push(...f),i[l.id]={loading:!1,error:null,code:null,count:f.length})}).catch(f=>{if(!n||o.signal.aborted)return;let x=c.signal.aborted;i[l.id]={loading:!1,error:x?"timed out":f instanceof Error?f.message:String(f),code:di(f,x),count:0}}).finally(()=>{clearTimeout(p),o.signal.removeEventListener("abort",u),n&&(a+=1,r({results:pi(fi(s.slice())),perSource:{...i},loading:a<ee.length,done:a,total:ee.length}))})}return()=>{n=!1,o.abort()}},[t]),e}var dr=["none",{field:"size",dir:"asc"},{field:"size",dir:"desc"},{field:"seeders",dir:"asc"},{field:"seeders",dir:"desc"},{field:"source",dir:"asc"},{field:"source",dir:"desc"}];function gi(t,e){return t==="none"||e==="none"?t===e:t.field===e.field&&t.dir===e.dir}function Wo(t){let e=dr.findIndex(r=>gi(r,t));return dr[(e+1)%dr.length]}function mr(t){return t==="asc"?"\u25B4":"\u25BE"}function jo(t){return t==="none"?"default":`${t.field} ${mr(t.dir)}`}function Vo(t,e){let r=t.slice();if(e==="none")return r;let o=e.dir==="asc"?1:-1;switch(e.field){case"size":r.sort((n,s)=>o*(n.sizeBytes-s.sizeBytes)||s.seeders-n.seeders);break;case"seeders":r.sort((n,s)=>o*(n.seeders-s.seeders)||(s.added??0)-(n.added??0));break;case"source":r.sort((n,s)=>o*n.source.localeCompare(s.source)||s.seeders-n.seeders);break}return r}import{Fragment as Nt,jsx as m,jsxs as te}from"react/jsx-runtime";var xi="Search or paste a magnet link\u2026";function Ye({label:t,value:e}){return te(M,{children:[m(M,{width:9,flexShrink:0,children:m(w,{dimColor:!0,children:t})}),m(M,{flexGrow:1,minWidth:0,children:e})]})}function Si({r:t,width:e}){let r=Re[t.source],o=ot(t.added),n=t.seeders||t.leechers?te(w,{children:[m(w,{color:t.seeders>0?d.good:void 0,bold:t.seeders>0,children:t.seeders}),m(w,{dimColor:!0,children:` seeders ${b.dot} ${t.leechers} leechers`})]}):m(w,{dimColor:!0,children:"unknown"});return te(M,{flexDirection:"column",children:[te(M,{children:[m(M,{flexGrow:1,minWidth:0,children:m(w,{bold:!0,color:d.text,wrap:"truncate-end",children:ie(t.name)})}),m(M,{flexShrink:0,marginLeft:2,children:m(w,{color:r.color,bold:!0,children:r.tag})})]}),m(Pt,{width:e}),te(M,{marginTop:1,flexDirection:"column",children:[m(Ye,{label:"Size",value:t.sizeBytes>0?m(w,{color:d.text,children:be(t.sizeBytes)}):m(w,{dimColor:!0,children:"unknown"})}),m(Ye,{label:"Health",value:n}),t.numFiles?m(Ye,{label:"Files",value:m(w,{dimColor:!0,children:String(t.numFiles)})}):null,o?m(Ye,{label:"Added",value:m(w,{dimColor:!0,children:o})}):null,m(Ye,{label:"Hash",value:m(w,{color:d.alt,dimColor:!0,wrap:"truncate-end",children:t.infoHash})}),m(Ye,{label:"Magnet",value:m(w,{color:d.alt,dimColor:!0,wrap:"truncate-end",children:t.magnet})})]}),te(M,{marginTop:1,children:[m(w,{color:d.accent,bold:!0,children:"d"}),m(w,{color:d.text,children:" Download"}),m(w,{dimColor:!0,children:` ${b.dot} `}),m(w,{color:d.accent,bold:!0,children:"y"}),m(w,{color:d.text,children:" Copy magnet"}),m(w,{dimColor:!0,children:` ${b.dot} `}),m(w,{color:d.alt,children:"esc"}),m(w,{dimColor:!0,children:" back"})]})]})}function qo(){let{query:t,submitQuery:e,section:r,region:o,setRegion:n,setCaptureMode:s,startDownload:i,copyMagnet:a,contentWidth:l,listRows:c}=X(),u=Qo(t),[p,f]=Lt("none"),x=pr(()=>{let g=nt.find(O=>O.key===r),A=g?.group?u.results.filter(O=>No(O.source).group===g.group):u.results;return Vo(A,p)},[u.results,r,p]),v=o==="content",[S,y]=Lt("list"),[R,k]=Lt(0),[F,L]=Lt(null);fr(()=>{k(0)},[x]),fr(()=>{if(v)return s(S==="search"?"text":S==="detail"?"esc":"none"),()=>s("none")},[S,v,s]),fr(()=>{v||y("list")},[v]);let $=Math.min(R,Math.max(0,x.length-1)),V=Math.max(5,c-3-1),D=Math.max(3,V-4),ge=Math.max(1,D-1),ne=g=>i({id:g.infoHash,name:g.name,magnet:g.magnet,source:g.source,sizeBytes:g.sizeBytes}),xe=g=>a({name:g.name,magnet:g.magnet});hr((g,A)=>{if(g==="/"){y("search");return}if(A.upArrow){x.length>0&&$>0?k($-1):y("search");return}if(x.length!==0)if(A.downArrow)k(he($,1,x.length));else if(A.pageUp)k(Math.max(0,$-ge));else if(A.pageDown)k(Math.min(x.length-1,$+ge));else if(A.return){let O=x[$];O&&(L(O),y("detail"))}else if(g==="d"){let O=x[$];O&&ne(O)}else if(g==="y"){let O=x[$];O&&xe(O)}else g==="s"&&f(O=>Wo(O))},{isActive:v&&S==="list"}),hr((g,A)=>{A.escape?(y("list"),L(null)):g==="d"&&F?ne(F):g==="y"&&F&&xe(F)},{isActive:v&&S==="detail"}),hr((g,A)=>{A.escape&&y("list")},{isActive:v&&S==="search"});let q=g=>{y("list"),e(g)},fe=t.trim()==="",Y=pr(()=>Object.values(u.perSource).filter(g=>g.error).length,[u.perSource]),pe=nt.find(g=>g.key===r),ue=pe?.group?ee.filter(g=>g.group===pe.group):ee,h=ue.length>0&&ue.every(g=>u.perSource[g.id]?.error),j=pr(()=>x.some(g=>g.sizeBytes>0||g.seeders>0),[x]),C=Math.max(2,String(x.length).length),se=g=>{let A=[...new Set(g.map(O=>u.perSource[O.id]?.code).filter(Boolean))];return A.length?` (${A.join(", ")})`:""},Se=()=>{if(u.loading)return x.length>0?m(w,{dimColor:!0,children:`searching\u2026 ${u.done}/${u.total} sources`}):m($t,{label:`${fe?"Loading":"Searching"} ${u.done}/${u.total} sources`});if(x.length===0){if(Y>=u.total){let K=ee.filter(ve=>u.perSource[ve.id]?.error);return m(w,{color:d.warn,children:`Couldn't reach any source. They may be down${se(K)}.`})}if(h&&pe){let K=ue.filter(yt=>u.perSource[yt.id]?.error),ve=K.length===1?"The source":`All ${K.length} sources`;return m(w,{color:d.warn,children:`Couldn't reach ${pe.label}. ${ve} may be down${se(K)}.`})}return u.results.length>0&&pe?.group?m(w,{dimColor:!0,children:`No ${pe.label.toLowerCase()} results yet. Try another tab or a search.`}):m(w,{dimColor:!0,children:fe?"Nothing new right now.":`No results for "${J(t,28)}".`})}let g=Y>0?` (${Y} source${Y===1?"":"s"} down)`:"",A=fe?"newest across all sources":`${x.length} result${x.length===1?"":"s"}`,O=p==="none"?"":` ${b.dot} sort: ${jo(p)}`;return m(w,{dimColor:!0,children:`${A}${g}${O}`})},Xe=(g,A)=>p==="none"||p.field!==g?A:te(Nt,{children:[m(w,{color:d.accent,bold:!0,children:mr(p.dir)}),A]}),ye=Ae($,x.length,D),xt=x.slice(ye,ye+D),St=x.length>0?`(${x.length})`:void 0;return te(M,{flexDirection:"column",children:[m(_t,{width:l,value:t,editing:S==="search",placeholder:xi,onSubmit:q,onExitDown:()=>y("list"),onExitLeft:()=>n("sidebar")}),m(M,{marginTop:1,children:m(me,{title:S==="detail"?"details":fe?"latest":"results",width:l,focused:v&&S!=="search",count:S==="detail"?void 0:St,height:V,children:S==="detail"&&F?m(Si,{r:F,width:Math.max(10,l-4)}):te(Nt,{children:[m(M,{children:Se()}),te(M,{flexDirection:"column",marginTop:x.length>0?1:0,children:[x.length>0?te(M,{children:[m(M,{width:Z,flexShrink:0}),m(M,{width:C,flexShrink:0,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:"#"})}),m(M,{flexGrow:1,minWidth:0,marginLeft:1,children:m(w,{bold:!0,dimColor:!0,children:"Name"})}),j?te(Nt,{children:[m(M,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("size","Size")})}),m(M,{width:9,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("seeders","Seed:Lch")})})]}):m(M,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:"Added"})}),m(M,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{bold:!0,dimColor:!0,children:Xe("source","Src")})})]}):null,xt.map((g,A)=>{let O=ye+A,K=O===$&&v&&S==="list",ve=Re[g.source];return te(M,{children:[m(M,{width:Z,flexShrink:0,children:m(w,{color:d.accent,children:K?b.pointer:""})}),m(M,{width:C,flexShrink:0,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:O+1})}),m(M,{flexGrow:1,minWidth:0,marginLeft:1,children:m(w,{wrap:"truncate-end",color:K?d.accent:void 0,dimColor:!K,bold:K,children:ie(g.name)})}),j?te(Nt,{children:[m(M,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:g.sizeBytes>0?be(g.sizeBytes):"-"})}),m(M,{width:9,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{color:g.seeders>0?d.good:void 0,dimColor:g.seeders===0,children:g.seeders||g.leechers?`${g.seeders}:${g.leechers}`:"-"})})]}):m(M,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{dimColor:!0,children:ot(g.added)||"-"})}),m(M,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:m(w,{color:ve.color,dimColor:!K,children:ve.tag})})]},g.infoHash)})]})]})})})]})}import{useEffect as vi,useState as Ci}from"react";import{Box as U,Text as re,useInput as Ri}from"ink";import{useEffect as wi,useState as bi}from"react";import{Text as ct}from"ink";var Yo="#f4efff";function Ko(t){return Math.ceil(t+4.5*2)+8}function Jo(t,e){return t*.45%e-4.5}function Xo(t,e){let r=Math.abs(t-e);return r>=4.5?0:.5*(1+Math.cos(Math.PI*r/4.5))*.9}import{jsx as gr,jsxs as rn}from"react/jsx-runtime";var Ti="#7c5cd6";function Zo(t,e,r,o){return t<=.5?ae(e,r,t/.5):ae(r,o,(t-.5)/.5)}function en(t){let e=[];for(let r of t){let o=e[e.length-1];o&&o.color===r?o.len++:e.push({color:r,len:1})}return e}function tn(t){return t.map((e,r)=>gr(ct,{color:e.color,children:"\u2588".repeat(e.len)},r))}function on({pct:t,width:e,color:r=d.accent,animate:o=!1}){let n=Math.max(0,Math.min(100,t)),s=Math.round(n/100*e),i=Math.max(0,e-s),a=Math.max(1,e-1),[l,c]=bi(0);wi(()=>{if(!o)return;let v=setInterval(()=>c(S=>S+1),40);return v.unref?.(),()=>clearInterval(v)},[o]);let u=i>0?gr(ct,{color:we,children:"\u2591".repeat(i)}):null;if(s===0)return gr(ct,{children:u});if(!o){let v=ae(r,"#000000",.3),S=ae(r,d.text,.35),y=Array.from({length:s},(R,k)=>Zo(k/a,v,r,S));return rn(ct,{children:[tn(en(y)),u]})}let p=Ko(e),f=Jo(l,p),x=Array.from({length:s},(v,S)=>{let y=Zo(S/a,Ti,d.accent,d.bright),R=Xo(S,f);return R>0&&(y=ae(y,Yo,R)),y});return rn(ct,{children:[tn(en(x)),u]})}import{jsx as B,jsxs as dt}from"react/jsx-runtime";var ut=2,Ft=2,Ei="#7c7785";function Bi(t){return t==="failed"?d.bad:t==="paused"?Ei:d.accent}function Ii(t){return t==="failed"?b.error:t==="paused"?b.pause:b.down}function Mi(t){if(t.status==="downloading"){let e=rt(t.speed)||"\u2026",r=t.eta?` ${Zr(t.eta)}`:"";return`${t.progress}% ${e} ${b.peer}${t.peers}${r}`}return t.status==="paused"?`paused ${t.progress}%`:J(t.error||"failed",28)}function nn(){let{queue:t,region:e,contentWidth:r,listRows:o,startDownload:n,setDownloadFocus:s}=X(),i=Ue(t),a=Mt(t),l=e==="content",c=i.length+a.length,[u,p]=Ci(0),f=Math.min(u,Math.max(0,c-1)),x=f<i.length,v=f-i.length;Ri((h,j)=>{if(j.upArrow)p(he(f,-1,c));else if(j.downArrow)p(he(f,1,c));else if(h==="f")t.retryFailed();else if(h==="x")t.clearHistory();else if(x){let C=i[f];if(!C)return;h==="c"?t.cancel(C.id):h==="p"&&t.togglePause(C.id)}else{let C=a[v];if(!C)return;j.return||h==="d"?n({id:C.id,name:C.name,magnet:C.magnet,source:C.source,sizeBytes:C.sizeBytes}):h==="c"&&t.removeHistory(C.id)}},{isActive:l&&c>0});let S=null;if(l&&c>0)if(!x)S="recent";else{let h=i[f]?.status;(h==="downloading"||h==="paused"||h==="failed")&&(S=h)}vi(()=>(s(S),()=>s(null)),[S,s]);let y=Math.max(5,o-1);if(c===0)return B(me,{title:"downloads",width:r,focused:l,height:y,children:B(re,{dimColor:!0,children:"No downloads yet. Find something and press d to grab it."})});let R=i.length>0,k=a.length>0,F=k?1:0,L=Math.max(1,y-1),$=R&&k?1:0,I=0,V=0;if(!k)I=Math.max(1,Math.floor(L/ut));else if(!R)V=Math.max(1,L-F);else{let h=L-F-$;h<ut+1&&($=0,h=L-F);let j=Math.max(ut,Math.floor(h*.55));I=Math.min(i.length,Math.max(1,Math.floor(j/ut))),V=Math.max(1,h-I*ut)}let D=Ae(x?f:0,i.length,I),ge=i.slice(D,D+I),ne=Ae(x?0:v,a.length,V),xe=a.slice(ne,ne+V),q=r-4,fe=2,Y=Math.max(8,Math.min(28,Math.floor(q*.4))),pe=Math.max(6,q-Ft-Z-Y-fe),ue=R?`(${i.length})`:void 0;return dt(me,{title:"downloads",width:r,focused:l,count:ue,height:y,children:[ge.map((h,j)=>{let C=D+j===f&&l&&x,se=Bi(h.status),Se=Re[h.source??"fitgirl"];return dt(U,{flexDirection:"column",children:[dt(U,{children:[B(U,{width:Ft,flexShrink:0,children:B(re,{color:d.accent,bold:!0,children:C?b.pointer:""})}),B(U,{width:Z,flexShrink:0,children:B(re,{color:se,children:Ii(h.status)})}),B(U,{flexGrow:1,minWidth:0,children:B(re,{wrap:"truncate-end",bold:C,color:C?d.accent:void 0,dimColor:!C,children:ie(h.name)})}),B(U,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:h.totalBytes>0?be(h.totalBytes):"-"})}),B(U,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{color:h.source?Se.color:void 0,dimColor:!h.source||!C,children:h.source?Se.tag:"mag"})})]}),dt(U,{children:[B(U,{width:Ft+Z,flexShrink:0}),B(on,{pct:h.progress,width:Y,color:se,animate:h.status==="downloading"}),B(U,{marginLeft:fe,flexShrink:0,children:B(re,{dimColor:!0,children:J(Mi(h),pe)})})]})]},h.id)}),k?B(U,{marginTop:$?1:0,children:B(re,{dimColor:!0,children:`Recently downloaded${a.length>1?` (${a.length})`:""}`})}):null,xe.map((h,j)=>{let C=ne+j===v&&l&&!x,se=Re[h.source??"fitgirl"],Se=ot(h.completedAt/1e3);return dt(U,{children:[B(U,{width:Ft,flexShrink:0,children:B(re,{color:d.accent,bold:!0,children:C?b.pointer:""})}),B(U,{width:Z,flexShrink:0,children:B(re,{color:d.good,dimColor:!C,children:b.done})}),B(U,{flexGrow:1,minWidth:0,children:B(re,{wrap:"truncate-end",bold:C,color:C?d.accent:void 0,dimColor:!C,children:ie(h.name)})}),B(U,{width:10,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:h.sizeBytes>0?be(h.sizeBytes):"-"})}),B(U,{width:12,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{dimColor:!0,children:Se||"-"})}),B(U,{width:4,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:B(re,{color:h.source?se.color:void 0,dimColor:!h.source||!C,children:h.source?se.tag:"mag"})})]},h.id)})]})}import{useEffect as ki,useState as Ai}from"react";import{Box as W,Text as oe,useInput as Pi}from"ink";import{jsx as P,jsxs as mt}from"react/jsx-runtime";var sn=2,an=10,xr=14,ln=4,Hi="#7c7785";function $i(t){return t?t.status==="seeding"?{icon:b.up,color:d.good}:t.status==="paused"?{icon:b.pause,color:Hi}:{icon:b.warn,color:d.warn}:{icon:b.done,color:d.good}}function Di(t){return t?t.status==="seeding"?{text:`${b.up}${rt(t.uploadSpeed)||"0 B/s"} ${b.peer}${t.peers}`,color:d.good,dim:!1}:t.status==="paused"?{text:"paused",dim:!0}:{text:"file gone",color:d.warn,dim:!1}:{text:"ready",dim:!0}}function cn(){let{queue:t,region:e,contentWidth:r,listRows:o,setNotice:n,setSeedFocus:s}=X(),i=Mt(t),a=eo(t),l=e==="content",c=i.length,[u,p]=Ai(0),f=Math.min(u,Math.max(0,c-1)),x=l&&c>0?a.get(i[f]?.id??"")?.status??"idle":null;ki(()=>(s(x),()=>s(null)),[x,s]),Pi((I,V)=>{if(V.upArrow)p(he(f,-1,c));else if(V.downArrow)p(he(f,1,c));else if(I==="p"){let D=i[f];if(!D)return;t.toggleSeeding(D),t.getSeed(D.id)?.status==="missing"&&n(`${b.warn} That file isn't on disk anymore.`)}else if(I==="c"){let D=i[f];D&&t.removeHistory(D.id)}},{isActive:l&&c>0});let v=Math.max(5,o-1),S=t.seedingCount;if(c===0)return P(me,{title:"seeding",width:r,focused:l,height:v,children:P(oe,{dimColor:!0,children:"Nothing here yet. Downloads start seeding automatically when they finish, and show up here."})});let y=0,R=0,k=0;for(let I of a.values())k+=I.uploaded,I.status==="seeding"&&(y+=I.uploadSpeed,R+=I.peers);let F=Math.max(1,v-2),L=Ae(f,c,F),$=i.slice(L,L+F);return mt(me,{title:"seeding",width:r,focused:l,count:S>0?`(${S})`:void 0,height:v,children:[P(W,{children:S>0?mt(oe,{color:d.good,children:[b.up," ",rt(y)||"0 B/s",P(oe,{dimColor:!0,children:` ${b.dot} ${R} peers ${b.dot} ${be(k)} shared back`})]}):P(oe,{dimColor:!0,children:"Downloads seed automatically when they finish. Press p to pause or resume any of them."})}),mt(W,{flexDirection:"column",marginTop:1,children:[mt(W,{children:[P(W,{width:sn,flexShrink:0}),P(W,{width:Z,flexShrink:0}),P(W,{flexGrow:1,minWidth:0,marginLeft:1,children:P(oe,{bold:!0,dimColor:!0,children:"Name"})}),P(W,{width:an,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Size"})}),P(W,{width:xr,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Status"})}),P(W,{width:ln,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{bold:!0,dimColor:!0,children:"Src"})})]}),$.map((I,V)=>{let D=L+V===f&&l,ge=a.get(I.id),ne=$i(ge),xe=Di(ge),q=Re[I.source??"fitgirl"];return mt(W,{children:[P(W,{width:sn,flexShrink:0,children:P(oe,{color:d.accent,bold:!0,children:D?b.pointer:""})}),P(W,{width:Z,flexShrink:0,children:P(oe,{color:ne.color,dimColor:!ge&&!D,children:ne.icon})}),P(W,{flexGrow:1,minWidth:0,marginLeft:1,children:P(oe,{wrap:"truncate-end",bold:D,color:D?d.accent:void 0,dimColor:!D,children:ie(I.name)})}),P(W,{width:an,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{dimColor:!0,children:I.sizeBytes>0?be(I.sizeBytes):"-"})}),P(W,{width:xr,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{color:xe.color,dimColor:xe.dim,children:J(xe.text,xr)})}),P(W,{width:ln,flexShrink:0,marginLeft:1,justifyContent:"flex-end",children:P(oe,{color:I.source?q.color:void 0,dimColor:!I.source||!D,children:I.source?q.tag:"mag"})})]},I.id)})]})]})}import{useEffect as Oi}from"react";function Sr(){let{queue:t}=X();Ue(t);let e=t.activeCount;return Oi(()=>{let r=e>0?`\u2193${e} \xB7 torlink`:"torlink";process.stdout.write(`\x1B]0;${r}\x07`),process.platform==="win32"&&(process.title=r)},[e]),null}import{Box as ft,Text as le,useInput as _i,useStdin as Li}from"ink";import{jsx as z,jsxs as un}from"react/jsx-runtime";var Ni=Fo().map(t=>t.group.toLowerCase()).join(` ${b.dot} `);function dn(){let{submitQuery:t,quitAll:e,cols:r,rows:o}=X(),{isRawModeSupported:n}=Li();_i((a,l)=>{(l.escape||l.ctrl&&a==="c")&&e()},{isActive:n});let s=r>=to+2,i=Math.max(24,Math.min(r-6,62));return un(ft,{height:Math.max(1,o-1),flexDirection:"column",justifyContent:"center",alignItems:"center",children:[s?z(At,{}):z(le,{bold:!0,color:d.accent,children:"torlink"}),z(ft,{marginTop:2,children:z(le,{color:d.text,children:"A curated, terminal-native torrent downloader."})}),z(ft,{children:z(le,{dimColor:!0,children:Ni})}),z(ft,{marginTop:1,width:i,children:z(_t,{width:i,value:"",editing:!0,placeholder:"Search or paste a magnet link\u2026",onSubmit:t})}),z(ft,{marginTop:1,children:un(le,{children:[z(le,{color:d.alt,children:"\u21B5"}),z(le,{dimColor:!0,children:" search"}),z(le,{dimColor:!0,children:` ${b.dot} `}),z(le,{dimColor:!0,children:"empty "}),z(le,{color:d.alt,children:"\u21B5"}),z(le,{dimColor:!0,children:" browse"}),z(le,{dimColor:!0,children:` ${b.dot} `}),z(le,{color:d.alt,children:"^c"}),z(le,{dimColor:!0,children:" quit"})]})})]})}import{Box as Ut,Text as Ke,useInput as Fi}from"ink";import{jsx as Te,jsxs as yr}from"react/jsx-runtime";function mn({width:t,value:e,onSubmit:r,onCancel:o}){return Fi((n,s)=>{s.escape&&o()}),yr(Ut,{flexDirection:"column",width:t,children:[Te(me,{title:"download folder",width:t,focused:!0,height:2,children:yr(Ut,{children:[Te(Ke,{color:d.accent,children:`${b.pointer} `}),Te(Ut,{flexGrow:1,minWidth:0,children:Te(Dt,{defaultValue:e,placeholder:"~/Downloads/torlink",onSubmit:r})})]})}),yr(Ut,{marginTop:1,children:[Te(Ke,{color:d.alt,children:"\u21B5"}),Te(Ke,{dimColor:!0,children:" save"}),Te(Ke,{dimColor:!0,children:` ${b.dot} `}),Te(Ke,{color:d.alt,children:"esc"}),Te(Ke,{dimColor:!0,children:" cancel"})]})]})}import{useEffect as Ui}from"react";function fn(){Ui(()=>{let{stdout:t,stdin:e}=process;t.write("\x1B[?1000h\x1B[?1006h");let r=o=>{let n=o.toString("utf8"),s=/\x1b\[<(64|65);\d+;\d+[Mm]/g,i;for(;(i=s.exec(n))!==null;){let a=i[1]==="64"?"\x1B[A":"\x1B[B";process.nextTick(()=>e.emit("data",Buffer.from(a)))}};return e.prependListener("data",r),()=>{t.write("\x1B[?1000l\x1B[?1006l"),e.removeListener("data",r)}},[])}import{jsx as N,jsxs as ht}from"react/jsx-runtime";function pn({initialMagnet:t,initialTorrent:e,onQuit:r}={}){fn();let{exit:o}=Wi(),{isRawModeSupported:n}=qi(),{stdout:s}=Vi(),[i,a]=ce({rows:s?.rows??24,cols:s?.columns??80});pt(()=>{if(!s)return;let T={rows:s.rows??24,cols:s.columns??80},E=()=>{let _={rows:s.rows??24,cols:s.columns??80};_.rows===T.rows&&_.cols===T.cols||((_.rows<T.rows||_.cols<T.cols)&&s.write("\x1B[2J\x1B[H"),T=_,a(_))};return s.on("resize",E),()=>{s.off("resize",E)}},[s]);let l=i.rows,c=i.cols,[u,p]=ce(null),[f,x]=ce(null),[v,S]=ce("splash"),[y,R]=ce(""),[k,F]=ce("all"),[L,$]=ce("content"),[I,V]=ce("none"),[D,ge]=ce(null),[ne,xe]=ce(null),[q,fe]=ce(!1),[Y,pe]=ce(!1),[ue,h]=ce(null),j=Gi(!1);pt(()=>{if(j.current)return;j.current=!0;let T=!0;return(async()=>{let E=await Br(),_=new Et;if(_.restore(qr(await Nr())),_.restoreHistory(await jr()),_.restoreSeeds(await Qr()),!T){_.suspend();return}x(E),p(_);let wt=t?Fe(t):e?await Yr(e):null;wt&&(await wr.mkdir(E.downloadDir,{recursive:!0}).catch(()=>{}),_.add({id:wt.infoHash,name:wt.name,magnet:wt.magnet},E.downloadDir),S("browser"),F("downloads"),$("content"))})(),()=>{T=!1}},[t,e]),pt(()=>{if(!u)return;let T=E=>h(`${b.done} ${J(ie(E),40)}`);return u.on("completed",T),()=>{u.off("completed",T)}},[u]),pt(()=>()=>{u?.suspend()},[u]);let C=Ie(()=>{u?.persistSync(),r?r():o()},[u,r,o]),se=Ie(T=>{x(T),Ir(T)},[]),Se=Ie(()=>{pe(!1)},[]),Xe=Ie(T=>{Se();let E=Ar(T);if(!f||!E||E===f.downloadDir){f&&E&&E===f.downloadDir&&h("Download folder unchanged.");return}(async()=>{try{await wr.mkdir(E,{recursive:!0})}catch{h(`Couldn't use folder: ${J(E,48)}`);return}se({...f,downloadDir:E}),h(`Download folder: ${J(E,48)}`)})()},[f,se,Se]),ye=Ie(T=>{!f||!u||(wr.mkdir(f.downloadDir,{recursive:!0}).catch(()=>{}),u.add(T,f.downloadDir),h(`Added: ${J(ie(T.name),40)}`),F("downloads"),$("content"))},[f,u]),xt=Ie(T=>{(async()=>{if(await Xr(T.magnet)){h(`Copied magnet: ${J(ie(T.magnet),60)}`);return}h(`Couldn't copy magnet for ${J(ie(T.name),32)}.`)})()},[]),St=Ie(T=>{let E=T.trim();if(E){let _=Fe(E);if(_){ye({id:_.infoHash,name:_.name,magnet:_.magnet}),S("browser");return}}R(E),S("browser"),k==="downloads"&&F("all"),$("content")},[k,ye]),g=Ie(async()=>{let T=(await Jr()).trim();if(!T){h("Clipboard is empty.");return}let E=T.match(/magnet:\?xt=urn:btih:[^\s"'<>]+/i)?.[0],_=E?Fe(E):null;if(_){ye({id:_.infoHash,name:_.name,magnet:_.magnet}),S("browser");return}h("No magnet link on the clipboard.")},[ye]);pt(()=>{if(!ue)return;let T=setTimeout(()=>h(null),4e3);return()=>clearTimeout(T)},[ue]);let A=l<18,O=!A,K=l>=12,ve=3+(O?1:0)+(A?0:1)+(K?1:0),yt=Math.max(6,l-1-ve),br=Math.max(4,yt),Tr=Math.max(24,c-or-3),Sn=Math.max(10,c-2),Ze=zi(()=>!u||!f?null:{config:f,setConfig:se,queue:u,view:v,setView:S,query:y,submitQuery:St,section:k,setSection:F,region:q||Y?"help":L,setRegion:$,captureMode:I,setCaptureMode:V,downloadFocus:D,setDownloadFocus:ge,seedFocus:ne,setSeedFocus:xe,startDownload:ye,copyMagnet:xt,notice:ue,setNotice:h,quitAll:C,listRows:br,compact:A,contentWidth:Tr,cols:c,rows:l},[u,f,v,y,St,k,L,q,Y,I,D,ne,ye,xt,ue,br,A,Tr,c,l,se,C]);return ji((T,E)=>{if(E.ctrl&&T==="c"){C();return}if(!Y&&I!=="text"){if(q){fe(!1);return}if(T==="?"){fe(!0);return}if(T==="o"){fe(!1),pe(!0);return}if(T==="m"){g();return}if(E.tab){$(L==="sidebar"?"content":"sidebar");return}if(E.rightArrow){L==="sidebar"&&$("content");return}if(E.leftArrow){L==="content"&&$("sidebar");return}if(E.escape){if(I==="esc")return;if(L==="content"){$("sidebar");return}S("splash");return}if(T==="q"){C();return}}},{isActive:n&&v==="browser"&&!!Ze}),Ze?v==="splash"?ht(It.Provider,{value:Ze,children:[N(Sr,{}),N(dn,{})]}):ht(It.Provider,{value:Ze,children:[N(Sr,{}),ht(Me,{flexDirection:"column",paddingX:1,children:[ht(Me,{justifyContent:"space-between",children:[N(At,{}),ue?N(Qi,{color:d.good,children:ue}):null]}),O?N(Pt,{width:Sn}):null,q?N(Me,{marginTop:1,children:N(ho,{})}):null,Y?N(Me,{marginTop:1,children:N(mn,{width:Math.max(24,Math.min(c-4,62)),value:Ze.config.downloadDir,onSubmit:Xe,onCancel:Se})}):null,ht(Me,{height:yt,marginTop:A?0:1,display:q||Y?"none":"flex",overflow:"hidden",children:[N(co,{}),N(Me,{flexGrow:1,flexDirection:"column",children:k==="downloads"?N(nn,{}):k==="seeding"?N(cn,{}):N(qo,{})})]}),K?N(Me,{display:q||Y?"none":"flex",children:N(uo,{hints:mo(L,k,D,ne)})}):null]})]}):N(Me,{height:l,justifyContent:"center",alignItems:"center",children:N($t,{label:"Starting torlink"})})}import{jsx as Ki}from"react/jsx-runtime";var Je=vr(process.argv.slice(2));Je.kind==="help"&&(console.log(Gt),process.exit(0));Je.kind==="version"&&(console.log(`torlnq v${Cr}`),process.exit(0));Je.kind==="invalid"&&(console.error(`error: unknown argument '${Je.arg}'
|
|
14
14
|
`),console.error(Gt),process.exit(1));process.stdout.write("\x1B[?1049h\x1B[?25l\x1B[22;0t\x1B]0;torlink\x07");process.platform==="win32"&&(process.title="torlink");var hn=!1;function gt(){hn||(hn=!0,process.stdout.write("\x1B[?1000l\x1B[?1006l\x1B[?25h\x1B[23;0t\x1B[?1049l"))}var gn=!1;function zt(t=0){gn&&(gt(),process.exit(t)),gn=!0;try{xn?.unmount()}catch{}gt(),process.exit(t)}var xn=Yi(Ki(pn,{initialMagnet:Je.initialMagnet,initialTorrent:Je.initialTorrent,onQuit:()=>zt(0)}),{exitOnCtrlC:!1});xn.waitUntilExit().then(()=>zt(0)).catch(t=>{gt(),console.error(t),process.exit(1)});process.on("SIGINT",()=>zt(0));process.on("SIGTERM",()=>zt(0));process.on("exit",gt);process.on("uncaughtException",t=>{gt(),console.error(t),process.exit(1)});
|