From fd0ea710395904e1b4cf5f22fd17af13db43ce5f Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 16 Jul 2024 13:12:33 +0100 Subject: webtorrent and teaser support --- index.html | 3 ++- posts/2015-10-07-rosa-rosa-rosam.md | 1 + posts/2015-12-07-tu-quoque.md | 2 +- scripts/webtorrent.min.js | 2 ++ site.hs | 16 +++++++++++++- style.css | 6 ++++- sw.min.js | 2 ++ templates/article.html | 44 +++++++++++++++++++++++++++++++++++++ templates/base.html | 2 +- templates/default.html | 33 ---------------------------- templates/load-webtorrent.html | 9 ++++++++ templates/post.html | 30 ++----------------------- templates/thumbs.html | 18 ++++++++++++++- todo.md | 3 ++- 14 files changed, 103 insertions(+), 68 deletions(-) create mode 100644 scripts/webtorrent.min.js create mode 100644 sw.min.js create mode 100644 templates/article.html delete mode 100644 templates/default.html create mode 100644 templates/load-webtorrent.html diff --git a/index.html b/index.html index cc90d6e..7836e19 100644 --- a/index.html +++ b/index.html @@ -1 +1,2 @@ -$partial("templates/thumbs.html")$ \ No newline at end of file +$partial("templates/load-webtorrent.html")$ +$partial("templates/thumbs.html")$ diff --git a/posts/2015-10-07-rosa-rosa-rosam.md b/posts/2015-10-07-rosa-rosa-rosam.md index 594442e..235160c 100644 --- a/posts/2015-10-07-rosa-rosa-rosam.md +++ b/posts/2015-10-07-rosa-rosa-rosam.md @@ -1,5 +1,6 @@ --- title: Rosa Rosa Rosam +torrent: magnet:?xt=urn:btih:dd8255ecdc7ca55fb0bbf81323d87062db1f6d1c&dn=Big+Buck+Bunny&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fbig-buck-bunny.torrent author: Ovidius --- diff --git a/posts/2015-12-07-tu-quoque.md b/posts/2015-12-07-tu-quoque.md index e9b32a0..89488c6 100644 --- a/posts/2015-12-07-tu-quoque.md +++ b/posts/2015-12-07-tu-quoque.md @@ -1,6 +1,6 @@ --- title: Tu Quoque -thumbnail: '' +torrent: magnet:?xt=urn:btih:08ada5a7a6183aae1e09d831df6748d566095a10&dn=Sintel&tr=udp%3A%2F%2Fexplodie.org%3A6969&tr=udp%3A%2F%2Ftracker.coppersurfer.tk%3A6969&tr=udp%3A%2F%2Ftracker.empire-js.us%3A1337&tr=udp%3A%2F%2Ftracker.leechers-paradise.org%3A6969&tr=udp%3A%2F%2Ftracker.opentrackr.org%3A1337&tr=wss%3A%2F%2Ftracker.btorrent.xyz&tr=wss%3A%2F%2Ftracker.fastcast.nz&tr=wss%3A%2F%2Ftracker.openwebtorrent.com&ws=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2F&xs=https%3A%2F%2Fwebtorrent.io%2Ftorrents%2Fsintel.torrent subtitle: this is the subtitle omg tags: lol, omg, slay author: celeste_🌸 diff --git a/scripts/webtorrent.min.js b/scripts/webtorrent.min.js new file mode 100644 index 0000000..7be3be2 --- /dev/null +++ b/scripts/webtorrent.min.js @@ -0,0 +1,2 @@ +var e,t,s={1467:e=>{function t(e,t){if("string"==typeof e[0])return e.join("");if("number"==typeof e[0])return new Uint8Array(e);const s=new Uint8Array(t);let i=0;for(let t=0,n=e.length;t=s){const e=t(o,a);let i=0;for(;a>=s;)yield e.slice(i,i+s),a-=s,i+=s;o=[e.slice(i,e.length)]}a&&(yield t(o,r?s:a))}},4497:(e,t,s)=>{const i=s(8454),n=s(9596);e.exports=class{constructor(e,t){if(this.store=e,this.chunkLength=e.chunkLength,this.inProgressGets=new Map,!this.store||!this.store.get||!this.store.put)throw new Error("First argument must be abstract-chunk-store compliant");this.cache=new i(t)}put(e,t,s=(()=>{})){if(!this.cache)return n((()=>s(new Error("CacheStore closed"))));this.cache.remove(e),this.store.put(e,t,s)}get(e,t,s=(()=>{})){if("function"==typeof t)return this.get(e,null,t);if(!this.cache)return n((()=>s(new Error("CacheStore closed"))));t||(t={});let i=this.cache.get(e);if(i){const e=t.offset||0,r=t.length||i.length-e;return 0===e&&r===i.length||(i=i.slice(e,r+e)),n((()=>s(null,i)))}let r=this.inProgressGets.get(e);const o=!!r;r||(r=[],this.inProgressGets.set(e,r)),r.push({opts:t,cb:s}),o||this.store.get(e,((t,s)=>{t||null==this.cache||this.cache.set(e,s);const i=this.inProgressGets.get(e);this.inProgressGets.delete(e);for(const{opts:e,cb:n}of i)if(t)n(t);else{const t=e.offset||0,i=e.length||s.length-t;let r=s;0===t&&i===s.length||(r=s.slice(t,i+t)),n(null,r)}}))}close(e=(()=>{})){if(!this.cache)return n((()=>e(new Error("CacheStore closed"))));this.cache=null,this.store.close(e)}destroy(e=(()=>{})){if(!this.cache)return n((()=>e(new Error("CacheStore closed"))));this.cache=null,this.store.destroy(e)}}},4018:e=>{e.exports=function(){for(var e=navigator.hardwareConcurrency||1,t=[],s=0;s{var i=s(717);t.formatArgs=function(t){if(t[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+t[0]+(this.useColors?"%c ":" ")+"+"+e.exports.humanize(this.diff),!this.useColors)return;const s="color: "+this.color;t.splice(1,0,s,"color: inherit");let i=0,n=0;t[0].replace(/%[a-zA-Z%]/g,(e=>{"%%"!==e&&(i++,"%c"===e&&(n=i))})),t.splice(n,0,s)},t.save=function(e){try{e?t.storage.setItem("debug",e):t.storage.removeItem("debug")}catch(e){}},t.load=function(){let e;try{e=t.storage.getItem("debug")}catch(e){}!e&&void 0!==i&&"env"in i&&(e=i.env.DEBUG);return e},t.useColors=function(){if("undefined"!=typeof window&&window.process&&("renderer"===window.process.type||window.process.__nwjs))return!0;if("undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/))return!1;return"undefined"!=typeof document&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||"undefined"!=typeof window&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||"undefined"!=typeof navigator&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)},t.storage=function(){try{return localStorage}catch(e){}}(),t.destroy=(()=>{let e=!1;return()=>{e||(e=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})(),t.colors=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"],t.log=console.debug||console.log||(()=>{}),e.exports=s(736)(t);const{formatters:n}=e.exports;n.j=function(e){try{return JSON.stringify(e)}catch(e){return"[UnexpectedJSONParseError]: "+e.message}}},736:(e,t,s)=>{e.exports=function(e){function t(e){let s,n,r,o=null;function a(...e){if(!a.enabled)return;const i=a,n=Number(new Date),r=n-(s||n);i.diff=r,i.prev=s,i.curr=n,s=n,e[0]=t.coerce(e[0]),"string"!=typeof e[0]&&e.unshift("%O");let o=0;e[0]=e[0].replace(/%([a-zA-Z%])/g,((s,n)=>{if("%%"===s)return"%";o++;const r=t.formatters[n];if("function"==typeof r){const t=e[o];s=r.call(i,t),e.splice(o,1),o--}return s})),t.formatArgs.call(i,e);(i.log||t.log).apply(i,e)}return a.namespace=e,a.useColors=t.useColors(),a.color=t.selectColor(e),a.extend=i,a.destroy=t.destroy,Object.defineProperty(a,"enabled",{enumerable:!0,configurable:!1,get:()=>null!==o?o:(n!==t.namespaces&&(n=t.namespaces,r=t.enabled(e)),r),set:e=>{o=e}}),"function"==typeof t.init&&t.init(a),a}function i(e,s){const i=t(this.namespace+(void 0===s?":":s)+e);return i.log=this.log,i}function n(e){return e.toString().substring(2,e.toString().length-2).replace(/\.\*\?$/,"*")}return t.debug=t,t.default=t,t.coerce=function(e){if(e instanceof Error)return e.stack||e.message;return e},t.disable=function(){const e=[...t.names.map(n),...t.skips.map(n).map((e=>"-"+e))].join(",");return t.enable(""),e},t.enable=function(e){let s;t.save(e),t.namespaces=e,t.names=[],t.skips=[];const i=("string"==typeof e?e:"").split(/[\s,]+/),n=i.length;for(s=0;s{t[s]=e[s]})),t.names=[],t.skips=[],t.formatters={},t.selectColor=function(e){let s=0;for(let t=0;t{var i=s(717),n=s(3519),r=function(){},o=function(e,t,s){if("function"==typeof t)return o(e,null,t);t||(t={}),s=n(s||r);var a=e._writableState,h=e._readableState,l=t.readable||!1!==t.readable&&e.readable,c=t.writable||!1!==t.writable&&e.writable,d=!1,u=function(){e.writable||p()},p=function(){c=!1,l||s.call(e)},f=function(){l=!1,c||s.call(e)},m=function(t){s.call(e,t?new Error("exited with error code: "+t):null)},g=function(t){s.call(e,t)},_=function(){i.nextTick(y)},y=function(){if(!d)return(!l||h&&h.ended&&!h.destroyed)&&(!c||a&&a.ended&&!a.destroyed)?void 0:s.call(e,new Error("premature close"))},w=function(){e.req.on("finish",p)};return!function(e){return e.setHeader&&"function"==typeof e.abort}(e)?c&&!a&&(e.on("end",u),e.on("close",u)):(e.on("complete",p),e.on("abort",_),e.req?w():e.on("request",w)),function(e){return e.stdio&&Array.isArray(e.stdio)&&3===e.stdio.length}(e)&&e.on("exit",m),e.on("end",f),e.on("finish",p),!1!==t.error&&e.on("error",g),e.on("close",_),function(){d=!0,e.removeListener("complete",p),e.removeListener("abort",_),e.removeListener("request",w),e.req&&e.req.removeListener("finish",p),e.removeListener("end",u),e.removeListener("close",u),e.removeListener("finish",p),e.removeListener("exit",m),e.removeListener("end",f),e.removeListener("error",g),e.removeListener("close",_)}};e.exports=o},6310:e=>{function t(e,t){for(const s in t)Object.defineProperty(e,s,{value:t[s],enumerable:!0,configurable:!0});return e}e.exports=function(e,s,i){if(!e||"string"==typeof e)throw new TypeError("Please pass an Error to err-code");i||(i={}),"object"==typeof s&&(i=s,s=""),s&&(i.code=s);try{return t(e,i)}catch(s){i.message=e.message,i.stack=e.stack;const n=function(){};n.prototype=Object.create(Object.getPrototypeOf(e));return t(new n,i)}}},580:e=>{var t=/["'&<>]/;e.exports=function(e){var s,i=""+e,n=t.exec(i);if(!n)return i;var r="",o=0,a=0;for(o=n.index;o{var t=Object.create||function(e){var t=function(){};return t.prototype=e,new t},s=Object.keys||function(e){var t=[];for(var s in e)Object.prototype.hasOwnProperty.call(e,s)&&t.push(s);return s},i=Function.prototype.bind||function(e){var t=this;return function(){return t.apply(e,arguments)}};function n(){this._events&&Object.prototype.hasOwnProperty.call(this,"_events")||(this._events=t(null),this._eventsCount=0),this._maxListeners=this._maxListeners||void 0}e.exports=n,n.EventEmitter=n,n.prototype._events=void 0,n.prototype._maxListeners=void 0;var r,o=10;try{var a={};Object.defineProperty&&Object.defineProperty(a,"x",{value:0}),r=0===a.x}catch(e){r=!1}function h(e){return void 0===e._maxListeners?n.defaultMaxListeners:e._maxListeners}function l(e,s,i,n){var r,o,a;if("function"!=typeof i)throw new TypeError('"listener" argument must be a function');if((o=e._events)?(o.newListener&&(e.emit("newListener",s,i.listener?i.listener:i),o=e._events),a=o[s]):(o=e._events=t(null),e._eventsCount=0),a){if("function"==typeof a?a=o[s]=n?[i,a]:[a,i]:n?a.unshift(i):a.push(i),!a.warned&&(r=h(e))&&r>0&&a.length>r){a.warned=!0;var l=new Error("Possible EventEmitter memory leak detected. "+a.length+' "'+String(s)+'" listeners added. Use emitter.setMaxListeners() to increase limit.');l.name="MaxListenersExceededWarning",l.emitter=e,l.type=s,l.count=a.length,"object"==typeof console&&console.warn&&console.warn("%s: %s",l.name,l.message)}}else a=o[s]=i,++e._eventsCount;return e}function c(){if(!this.fired)switch(this.target.removeListener(this.type,this.wrapFn),this.fired=!0,arguments.length){case 0:return this.listener.call(this.target);case 1:return this.listener.call(this.target,arguments[0]);case 2:return this.listener.call(this.target,arguments[0],arguments[1]);case 3:return this.listener.call(this.target,arguments[0],arguments[1],arguments[2]);default:for(var e=new Array(arguments.length),t=0;t1&&(t=arguments[1]),t instanceof Error)throw t;var h=new Error('Unhandled "error" event. ('+t+")");throw h.context=t,h}if(!(s=o[e]))return!1;var l="function"==typeof s;switch(i=arguments.length){case 1:!function(e,t,s){if(t)e.call(s);else for(var i=e.length,n=f(e,i),r=0;r=0;o--)if(i[o]===s||i[o].listener===s){a=i[o].listener,r=o;break}if(r<0)return this;0===r?i.shift():function(e,t){for(var s=t,i=s+1,n=e.length;i=0;r--)this.removeListener(e,i[r]);return this},n.prototype.listeners=function(e){return u(this,e,!0)},n.prototype.rawListeners=function(e){return u(this,e,!1)},n.listenerCount=function(e,t){return"function"==typeof e.listenerCount?e.listenerCount(t):p.call(e,t)},n.prototype.listenerCount=p,n.prototype.eventNames=function(){return this._eventsCount>0?Reflect.ownKeys(this._events):[]}},5809:e=>{e.exports=class{constructor(e){if(!(e>0)||e-1&e)throw new Error("Max size for a FixedFIFO should be a power of two");this.buffer=new Array(e),this.mask=e-1,this.top=0,this.btm=0,this.next=null}clear(){this.top=this.btm=0,this.next=null,this.buffer.fill(void 0)}push(e){return void 0===this.buffer[this.top]&&(this.buffer[this.top]=e,this.top=this.top+1&this.mask,!0)}shift(){const e=this.buffer[this.btm];if(void 0!==e)return this.buffer[this.btm]=void 0,this.btm=this.btm+1&this.mask,e}peek(){return this.buffer[this.btm]}isEmpty(){return void 0===this.buffer[this.btm]}}},6080:(e,t,s)=>{const i=s(5809);e.exports=class{constructor(e){this.hwm=e||16,this.head=new i(this.hwm),this.tail=this.head,this.length=0}clear(){this.head=this.tail,this.head.clear(),this.length=0}push(e){if(this.length++,!this.head.push(e)){const t=this.head;this.head=t.next=new i(2*this.head.buffer.length),this.head.push(e)}}shift(){0!==this.length&&this.length--;const e=this.tail.shift();if(void 0===e&&this.tail.next){const e=this.tail.next;return this.tail.next=null,this.tail=e,this.tail.shift()}return e}peek(){const e=this.tail.peek();return void 0===e&&this.tail.next?this.tail.next.peek():e}isEmpty(){return 0===this.length}}},3468:()=>{"undefined"!=typeof ReadableStream&&(ReadableStream.prototype[Symbol.asyncIterator]||(ReadableStream.prototype[Symbol.asyncIterator]=function({preventCancel:e}={}){const t=this.getReader(),s=this;let i=t.read();return{next(){const e=i;return i=t.read(),e},return:async n=>(await i,t.releaseLock(),e||s.cancel(),{done:!0,value:n}),async throw(e){throw await this.return(),e},[Symbol.asyncIterator](){return this}}}),ReadableStream.prototype.getIterator||(ReadableStream.prototype.getIterator=function({preventCancel:e}={}){return this[Symbol.asyncIterator]({preventCancel:e})}))},3714:(e,t,s)=>{const i=s(9596);e.exports=class{constructor(e){if(this.store=e,this.chunkLength=e.chunkLength,!this.store||!this.store.get||!this.store.put)throw new Error("First argument must be abstract-chunk-store compliant");this.mem=[]}put(e,t,s=(()=>{})){this.mem[e]=t,this.store.put(e,t,(t=>{this.mem[e]=null,s(t)}))}get(e,t,s=(()=>{})){if("function"==typeof t)return this.get(e,null,t);let n=this.mem[e];if(!n)return this.store.get(e,t,s);t||(t={});const r=t.offset||0,o=t.length||n.length-r;0===r&&o===n.length||(n=n.slice(r,o+r)),i((()=>s(null,n)))}close(e=(()=>{})){this.store.close(e)}destroy(e=(()=>{})){this.store.destroy(e)}}},6698:e=>{"function"==typeof Object.create?e.exports=function(e,t){t&&(e.super_=t,e.prototype=Object.create(t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}))}:e.exports=function(e,t){if(t){e.super_=t;var s=function(){};s.prototype=t.prototype,e.prototype=new s,e.prototype.constructor=e}}},405:e=>{e.exports=async function*(e){for(let t of e)"function"==typeof t&&(t=t()),yield*t}},9439:(e,t,s)=>{t.RateLimiter=s(3065),t.TokenBucket=s(5472)},3085:(e,t,s)=>{var i=s(717);e.exports=function(){if(void 0!==i&&i.hrtime){var e=i.hrtime(),t=e[0],s=e[1];return 1e3*t+Math.floor(s/1e6)}return(new Date).getTime()}},3065:(e,t,s)=>{var i=s(717),n=s(5472),r=s(3085),o=function(e,t,s){this.tokenBucket=new n(e,e,t,null),this.tokenBucket.content=e,this.curIntervalStart=r(),this.tokensThisInterval=0,this.fireImmediately=s};o.prototype={tokenBucket:null,curIntervalStart:0,tokensThisInterval:0,fireImmediately:!1,removeTokens:function(e,t){if(e>this.tokenBucket.bucketSize)return i.nextTick(t.bind(null,"Requested tokens "+e+" exceeds maximum tokens per interval "+this.tokenBucket.bucketSize,null)),!1;var s=this,n=r();if((n=this.tokenBucket.interval)&&(this.curIntervalStart=n,this.tokensThisInterval=0),e>this.tokenBucket.tokensPerInterval-this.tokensThisInterval){if(this.fireImmediately)i.nextTick(t.bind(null,null,-1));else{var o=Math.ceil(this.curIntervalStart+this.tokenBucket.interval-n);setTimeout((function(){s.tokenBucket.removeTokens(e,a)}),o)}return!1}return this.tokenBucket.removeTokens(e,a);function a(i,n){if(i)return t(i,null);s.tokensThisInterval+=e,t(null,n)}},tryRemoveTokens:function(e){if(e>this.tokenBucket.bucketSize)return!1;var t=r();if((t=this.tokenBucket.interval)&&(this.curIntervalStart=t,this.tokensThisInterval=0),e>this.tokenBucket.tokensPerInterval-this.tokensThisInterval)return!1;var s=this.tokenBucket.tryRemoveTokens(e);return s&&(this.tokensThisInterval+=e),s},getTokensRemaining:function(){return this.tokenBucket.drip(),this.tokenBucket.content}},e.exports=o},5472:(e,t,s)=>{var i=s(717),n=function(e,t,s,i){if(this.bucketSize=e,this.tokensPerInterval=t,"string"==typeof s)switch(s){case"sec":case"second":this.interval=1e3;break;case"min":case"minute":this.interval=6e4;break;case"hr":case"hour":this.interval=36e5;break;case"day":this.interval=864e5;break;default:throw new Error("Invaid interval "+s)}else this.interval=s;this.parentBucket=i,this.content=0,this.lastDrip=+new Date};n.prototype={bucketSize:1,tokensPerInterval:1,interval:1e3,parentBucket:null,content:0,lastDrip:0,removeTokens:function(e,t){var s=this;return this.bucketSize?e>this.bucketSize?(i.nextTick(t.bind(null,"Requested tokens "+e+" exceeds bucket size "+this.bucketSize,null)),!1):(this.drip(),e>this.content?n():this.parentBucket?this.parentBucket.removeTokens(e,(function(i,r){return i?t(i,null):e>s.content?n():(s.content-=e,void t(null,Math.min(r,s.content)))})):(this.content-=e,i.nextTick(t.bind(null,null,this.content)),!0)):(i.nextTick(t.bind(null,null,e,Number.POSITIVE_INFINITY)),!0);function n(){var i=Math.ceil((e-s.content)*(s.interval/s.tokensPerInterval));return setTimeout((function(){s.removeTokens(e,t)}),i),!1}},tryRemoveTokens:function(e){return!this.bucketSize||!(e>this.bucketSize)&&(this.drip(),!(e>this.content)&&(!(this.parentBucket&&!this.parentBucket.tryRemoveTokens(e))&&(this.content-=e,!0)))},drip:function(){if(this.tokensPerInterval){var e=+new Date,t=Math.max(e-this.lastDrip,0);this.lastDrip=e;var s=t*(this.tokensPerInterval/this.interval);this.content=Math.min(this.content+s,this.bucketSize)}else this.content=this.bucketSize}},e.exports=n},8454:(e,t,s)=>{var i=s(7007),n=s(6698);function r(e){if(!(this instanceof r))return new r(e);"number"==typeof e&&(e={max:e}),e||(e={}),i.EventEmitter.call(this),this.cache={},this.head=this.tail=null,this.length=0,this.max=e.max||1e3,this.maxAge=e.maxAge||0}e.exports=r,n(r,i.EventEmitter),Object.defineProperty(r.prototype,"keys",{get:function(){return Object.keys(this.cache)}}),r.prototype.clear=function(){this.cache={},this.head=this.tail=null,this.length=0},r.prototype.remove=function(e){if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){var t=this.cache[e];return delete this.cache[e],this._unlink(e,t.prev,t.next),t.value}},r.prototype._unlink=function(e,t,s){this.length--,0===this.length?this.head=this.tail=null:this.head===e?(this.head=t,this.cache[this.head].next=null):this.tail===e?(this.tail=s,this.cache[this.tail].prev=null):(this.cache[t].next=s,this.cache[s].prev=t)},r.prototype.peek=function(e){if(this.cache.hasOwnProperty(e)){var t=this.cache[e];if(this._checkAge(e,t))return t.value}},r.prototype.set=function(e,t){var s;if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){if((s=this.cache[e]).value=t,this.maxAge&&(s.modified=Date.now()),e===this.head)return t;this._unlink(e,s.prev,s.next)}else s={value:t,modified:0,next:null,prev:null},this.maxAge&&(s.modified=Date.now()),this.cache[e]=s,this.length===this.max&&this.evict();return this.length++,s.next=null,s.prev=this.head,this.head&&(this.cache[this.head].next=e),this.head=e,this.tail||(this.tail=e),t},r.prototype._checkAge=function(e,t){return!(this.maxAge&&Date.now()-t.modified>this.maxAge)||(this.remove(e),this.emit("evict",{key:e,value:t.value}),!1)},r.prototype.get=function(e){if("string"!=typeof e&&(e=""+e),this.cache.hasOwnProperty(e)){var t=this.cache[e];if(this._checkAge(e,t))return this.head!==e&&(e===this.tail?(this.tail=t.next,this.cache[this.tail].prev=null):this.cache[t.prev].next=t.next,this.cache[t.next].prev=t.prev,this.cache[this.head].next=e,t.prev=this.head,t.next=null,this.head=e),t.value}},r.prototype.evict=function(){if(this.tail){var e=this.tail,t=this.remove(this.tail);this.emit("evict",{key:e,value:t})}}},4862:(e,t,s)=>{e.exports=n;const i=s(9596);function n(e,t){if(!(this instanceof n))return new n(e,t);if(t||(t={}),this.chunkLength=Number(e),!this.chunkLength)throw new Error("First argument must be a chunk length");this.chunks=[],this.closed=!1,this.length=Number(t.length)||1/0,this.length!==1/0&&(this.lastChunkLength=this.length%this.chunkLength||this.chunkLength,this.lastChunkIndex=Math.ceil(this.length/this.chunkLength)-1)}n.prototype.put=function(e,t,s=(()=>{})){if(this.closed)return i((()=>s(new Error("Storage is closed"))));const n=e===this.lastChunkIndex;return n&&t.length!==this.lastChunkLength?i((()=>s(new Error("Last chunk length must be "+this.lastChunkLength)))):n||t.length===this.chunkLength?(this.chunks[e]=t,void i((()=>s(null)))):i((()=>s(new Error("Chunk length must be "+this.chunkLength))))},n.prototype.get=function(e,t,s=(()=>{})){if("function"==typeof t)return this.get(e,null,t);if(this.closed)return i((()=>s(new Error("Storage is closed"))));let n=this.chunks[e];if(!n){const e=new Error("Chunk not found");return e.notFound=!0,i((()=>s(e)))}t||(t={});const r=t.offset||0,o=t.length||n.length-r;0===r&&o===n.length||(n=n.slice(r,o+r)),i((()=>s(null,n)))},n.prototype.close=n.prototype.destroy=function(e=(()=>{})){if(this.closed)return i((()=>e(new Error("Storage is closed"))));this.closed=!0,this.chunks=null,i((()=>e(null)))}},8673:e=>{function t(){this._types=Object.create(null),this._extensions=Object.create(null);for(let e=0;e{let i=s(8673);e.exports=new i(s(8446))},8446:e=>{e.exports={"application/andrew-inset":["ez"],"application/applixware":["aw"],"application/atom+xml":["atom"],"application/atomcat+xml":["atomcat"],"application/atomdeleted+xml":["atomdeleted"],"application/atomsvc+xml":["atomsvc"],"application/atsc-dwd+xml":["dwd"],"application/atsc-held+xml":["held"],"application/atsc-rsat+xml":["rsat"],"application/bdoc":["bdoc"],"application/calendar+xml":["xcs"],"application/ccxml+xml":["ccxml"],"application/cdfx+xml":["cdfx"],"application/cdmi-capability":["cdmia"],"application/cdmi-container":["cdmic"],"application/cdmi-domain":["cdmid"],"application/cdmi-object":["cdmio"],"application/cdmi-queue":["cdmiq"],"application/cu-seeme":["cu"],"application/dash+xml":["mpd"],"application/davmount+xml":["davmount"],"application/docbook+xml":["dbk"],"application/dssc+der":["dssc"],"application/dssc+xml":["xdssc"],"application/ecmascript":["es","ecma"],"application/emma+xml":["emma"],"application/emotionml+xml":["emotionml"],"application/epub+zip":["epub"],"application/exi":["exi"],"application/express":["exp"],"application/fdt+xml":["fdt"],"application/font-tdpfr":["pfr"],"application/geo+json":["geojson"],"application/gml+xml":["gml"],"application/gpx+xml":["gpx"],"application/gxf":["gxf"],"application/gzip":["gz"],"application/hjson":["hjson"],"application/hyperstudio":["stk"],"application/inkml+xml":["ink","inkml"],"application/ipfix":["ipfix"],"application/its+xml":["its"],"application/java-archive":["jar","war","ear"],"application/java-serialized-object":["ser"],"application/java-vm":["class"],"application/javascript":["js","mjs"],"application/json":["json","map"],"application/json5":["json5"],"application/jsonml+json":["jsonml"],"application/ld+json":["jsonld"],"application/lgr+xml":["lgr"],"application/lost+xml":["lostxml"],"application/mac-binhex40":["hqx"],"application/mac-compactpro":["cpt"],"application/mads+xml":["mads"],"application/manifest+json":["webmanifest"],"application/marc":["mrc"],"application/marcxml+xml":["mrcx"],"application/mathematica":["ma","nb","mb"],"application/mathml+xml":["mathml"],"application/mbox":["mbox"],"application/mediaservercontrol+xml":["mscml"],"application/metalink+xml":["metalink"],"application/metalink4+xml":["meta4"],"application/mets+xml":["mets"],"application/mmt-aei+xml":["maei"],"application/mmt-usd+xml":["musd"],"application/mods+xml":["mods"],"application/mp21":["m21","mp21"],"application/mp4":["mp4s","m4p"],"application/msword":["doc","dot"],"application/mxf":["mxf"],"application/n-quads":["nq"],"application/n-triples":["nt"],"application/node":["cjs"],"application/octet-stream":["bin","dms","lrf","mar","so","dist","distz","pkg","bpk","dump","elc","deploy","exe","dll","deb","dmg","iso","img","msi","msp","msm","buffer"],"application/oda":["oda"],"application/oebps-package+xml":["opf"],"application/ogg":["ogx"],"application/omdoc+xml":["omdoc"],"application/onenote":["onetoc","onetoc2","onetmp","onepkg"],"application/oxps":["oxps"],"application/p2p-overlay+xml":["relo"],"application/patch-ops-error+xml":["xer"],"application/pdf":["pdf"],"application/pgp-encrypted":["pgp"],"application/pgp-signature":["asc","sig"],"application/pics-rules":["prf"],"application/pkcs10":["p10"],"application/pkcs7-mime":["p7m","p7c"],"application/pkcs7-signature":["p7s"],"application/pkcs8":["p8"],"application/pkix-attr-cert":["ac"],"application/pkix-cert":["cer"],"application/pkix-crl":["crl"],"application/pkix-pkipath":["pkipath"],"application/pkixcmp":["pki"],"application/pls+xml":["pls"],"application/postscript":["ai","eps","ps"],"application/provenance+xml":["provx"],"application/pskc+xml":["pskcxml"],"application/raml+yaml":["raml"],"application/rdf+xml":["rdf","owl"],"application/reginfo+xml":["rif"],"application/relax-ng-compact-syntax":["rnc"],"application/resource-lists+xml":["rl"],"application/resource-lists-diff+xml":["rld"],"application/rls-services+xml":["rs"],"application/route-apd+xml":["rapd"],"application/route-s-tsid+xml":["sls"],"application/route-usd+xml":["rusd"],"application/rpki-ghostbusters":["gbr"],"application/rpki-manifest":["mft"],"application/rpki-roa":["roa"],"application/rsd+xml":["rsd"],"application/rss+xml":["rss"],"application/rtf":["rtf"],"application/sbml+xml":["sbml"],"application/scvp-cv-request":["scq"],"application/scvp-cv-response":["scs"],"application/scvp-vp-request":["spq"],"application/scvp-vp-response":["spp"],"application/sdp":["sdp"],"application/senml+xml":["senmlx"],"application/sensml+xml":["sensmlx"],"application/set-payment-initiation":["setpay"],"application/set-registration-initiation":["setreg"],"application/shf+xml":["shf"],"application/sieve":["siv","sieve"],"application/smil+xml":["smi","smil"],"application/sparql-query":["rq"],"application/sparql-results+xml":["srx"],"application/srgs":["gram"],"application/srgs+xml":["grxml"],"application/sru+xml":["sru"],"application/ssdl+xml":["ssdl"],"application/ssml+xml":["ssml"],"application/swid+xml":["swidtag"],"application/tei+xml":["tei","teicorpus"],"application/thraud+xml":["tfi"],"application/timestamped-data":["tsd"],"application/toml":["toml"],"application/trig":["trig"],"application/ttml+xml":["ttml"],"application/ubjson":["ubj"],"application/urc-ressheet+xml":["rsheet"],"application/urc-targetdesc+xml":["td"],"application/voicexml+xml":["vxml"],"application/wasm":["wasm"],"application/widget":["wgt"],"application/winhlp":["hlp"],"application/wsdl+xml":["wsdl"],"application/wspolicy+xml":["wspolicy"],"application/xaml+xml":["xaml"],"application/xcap-att+xml":["xav"],"application/xcap-caps+xml":["xca"],"application/xcap-diff+xml":["xdf"],"application/xcap-el+xml":["xel"],"application/xcap-ns+xml":["xns"],"application/xenc+xml":["xenc"],"application/xhtml+xml":["xhtml","xht"],"application/xliff+xml":["xlf"],"application/xml":["xml","xsl","xsd","rng"],"application/xml-dtd":["dtd"],"application/xop+xml":["xop"],"application/xproc+xml":["xpl"],"application/xslt+xml":["*xsl","xslt"],"application/xspf+xml":["xspf"],"application/xv+xml":["mxml","xhvml","xvml","xvm"],"application/yang":["yang"],"application/yin+xml":["yin"],"application/zip":["zip"],"audio/3gpp":["*3gpp"],"audio/adpcm":["adp"],"audio/amr":["amr"],"audio/basic":["au","snd"],"audio/midi":["mid","midi","kar","rmi"],"audio/mobile-xmf":["mxmf"],"audio/mp3":["*mp3"],"audio/mp4":["m4a","mp4a"],"audio/mpeg":["mpga","mp2","mp2a","mp3","m2a","m3a"],"audio/ogg":["oga","ogg","spx","opus"],"audio/s3m":["s3m"],"audio/silk":["sil"],"audio/wav":["wav"],"audio/wave":["*wav"],"audio/webm":["weba"],"audio/xm":["xm"],"font/collection":["ttc"],"font/otf":["otf"],"font/ttf":["ttf"],"font/woff":["woff"],"font/woff2":["woff2"],"image/aces":["exr"],"image/apng":["apng"],"image/avif":["avif"],"image/bmp":["bmp"],"image/cgm":["cgm"],"image/dicom-rle":["drle"],"image/emf":["emf"],"image/fits":["fits"],"image/g3fax":["g3"],"image/gif":["gif"],"image/heic":["heic"],"image/heic-sequence":["heics"],"image/heif":["heif"],"image/heif-sequence":["heifs"],"image/hej2k":["hej2"],"image/hsj2":["hsj2"],"image/ief":["ief"],"image/jls":["jls"],"image/jp2":["jp2","jpg2"],"image/jpeg":["jpeg","jpg","jpe"],"image/jph":["jph"],"image/jphc":["jhc"],"image/jpm":["jpm"],"image/jpx":["jpx","jpf"],"image/jxr":["jxr"],"image/jxra":["jxra"],"image/jxrs":["jxrs"],"image/jxs":["jxs"],"image/jxsc":["jxsc"],"image/jxsi":["jxsi"],"image/jxss":["jxss"],"image/ktx":["ktx"],"image/ktx2":["ktx2"],"image/png":["png"],"image/sgi":["sgi"],"image/svg+xml":["svg","svgz"],"image/t38":["t38"],"image/tiff":["tif","tiff"],"image/tiff-fx":["tfx"],"image/webp":["webp"],"image/wmf":["wmf"],"message/disposition-notification":["disposition-notification"],"message/global":["u8msg"],"message/global-delivery-status":["u8dsn"],"message/global-disposition-notification":["u8mdn"],"message/global-headers":["u8hdr"],"message/rfc822":["eml","mime"],"model/3mf":["3mf"],"model/gltf+json":["gltf"],"model/gltf-binary":["glb"],"model/iges":["igs","iges"],"model/mesh":["msh","mesh","silo"],"model/mtl":["mtl"],"model/obj":["obj"],"model/step+xml":["stpx"],"model/step+zip":["stpz"],"model/step-xml+zip":["stpxz"],"model/stl":["stl"],"model/vrml":["wrl","vrml"],"model/x3d+binary":["*x3db","x3dbz"],"model/x3d+fastinfoset":["x3db"],"model/x3d+vrml":["*x3dv","x3dvz"],"model/x3d+xml":["x3d","x3dz"],"model/x3d-vrml":["x3dv"],"text/cache-manifest":["appcache","manifest"],"text/calendar":["ics","ifb"],"text/coffeescript":["coffee","litcoffee"],"text/css":["css"],"text/csv":["csv"],"text/html":["html","htm","shtml"],"text/jade":["jade"],"text/jsx":["jsx"],"text/less":["less"],"text/markdown":["markdown","md"],"text/mathml":["mml"],"text/mdx":["mdx"],"text/n3":["n3"],"text/plain":["txt","text","conf","def","list","log","in","ini"],"text/richtext":["rtx"],"text/rtf":["*rtf"],"text/sgml":["sgml","sgm"],"text/shex":["shex"],"text/slim":["slim","slm"],"text/spdx":["spdx"],"text/stylus":["stylus","styl"],"text/tab-separated-values":["tsv"],"text/troff":["t","tr","roff","man","me","ms"],"text/turtle":["ttl"],"text/uri-list":["uri","uris","urls"],"text/vcard":["vcard"],"text/vtt":["vtt"],"text/xml":["*xml"],"text/yaml":["yaml","yml"],"video/3gpp":["3gp","3gpp"],"video/3gpp2":["3g2"],"video/h261":["h261"],"video/h263":["h263"],"video/h264":["h264"],"video/iso.segment":["m4s"],"video/jpeg":["jpgv"],"video/jpm":["*jpm","jpgm"],"video/mj2":["mj2","mjp2"],"video/mp2t":["ts"],"video/mp4":["mp4","mp4v","mpg4"],"video/mpeg":["mpeg","mpg","mpe","m1v","m2v"],"video/ogg":["ogv"],"video/quicktime":["qt","mov"],"video/webm":["webm"]}},6585:e=>{var t=1e3,s=60*t,i=60*s,n=24*i,r=7*n,o=365.25*n;function a(e,t,s,i){var n=t>=1.5*s;return Math.round(e/s)+" "+i+(n?"s":"")}e.exports=function(e,h){h=h||{};var l=typeof e;if("string"===l&&e.length>0)return function(e){if((e=String(e)).length>100)return;var a=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(e);if(!a)return;var h=parseFloat(a[1]);switch((a[2]||"ms").toLowerCase()){case"years":case"year":case"yrs":case"yr":case"y":return h*o;case"weeks":case"week":case"w":return h*r;case"days":case"day":case"d":return h*n;case"hours":case"hour":case"hrs":case"hr":case"h":return h*i;case"minutes":case"minute":case"mins":case"min":case"m":return h*s;case"seconds":case"second":case"secs":case"sec":case"s":return h*t;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return h;default:return}}(e);if("number"===l&&isFinite(e))return h.long?function(e){var r=Math.abs(e);if(r>=n)return a(e,r,n,"day");if(r>=i)return a(e,r,i,"hour");if(r>=s)return a(e,r,s,"minute");if(r>=t)return a(e,r,t,"second");return e+" ms"}(e):function(e){var r=Math.abs(e);if(r>=n)return Math.round(e/n)+"d";if(r>=i)return Math.round(e/i)+"h";if(r>=s)return Math.round(e/s)+"m";if(r>=t)return Math.round(e/t)+"s";return e+"ms"}(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))}},3519:(e,t,s)=>{var i=s(6587);function n(e){var t=function(){return t.called?t.value:(t.called=!0,t.value=e.apply(this,arguments))};return t.called=!1,t}function r(e){var t=function(){if(t.called)throw new Error(t.onceError);return t.called=!0,t.value=e.apply(this,arguments)},s=e.name||"Function wrapped with `once`";return t.onceError=s+" shouldn't be called more than once",t.called=!1,t}e.exports=i(n),e.exports.strict=i(r),n.proto=n((function(){Object.defineProperty(Function.prototype,"once",{value:function(){return n(this)},configurable:!0}),Object.defineProperty(Function.prototype,"onceStrict",{value:function(){return r(this)},configurable:!0})}))},8444:e=>{e.exports=function(e){return Math.max(16384,1<{var i=s(717),n=s(3519),r=s(6611),o=s(2376),a=function(){},h=/^v?\.0/.test(i.version),l=function(e){return"function"==typeof e},c=function(e,t,s,i){i=n(i);var c=!1;e.on("close",(function(){c=!0})),r(e,{readable:t,writable:s},(function(e){if(e)return i(e);c=!0,i()}));var d=!1;return function(t){if(!c&&!d)return d=!0,function(e){return!!h&&!!o&&(e instanceof(o.ReadStream||a)||e instanceof(o.WriteStream||a))&&l(e.close)}(e)?e.close(a):function(e){return e.setHeader&&l(e.abort)}(e)?e.abort():l(e.destroy)?e.destroy():void i(t||new Error("stream was destroyed"))}},d=function(e){e()},u=function(e,t){return e.pipe(t)};e.exports=function(){var e,t=Array.prototype.slice.call(arguments),s=l(t[t.length-1]||a)&&t.pop()||a;if(Array.isArray(t[0])&&(t=t[0]),t.length<2)throw new Error("pump requires two streams per minimum");var i=t.map((function(n,r){var o=r0,(function(t){e||(e=t),t&&i.forEach(d),o||(i.forEach(d),s(e))}))}));return t.reduce(u)}},9596:e=>{let t;e.exports="function"==typeof queueMicrotask?queueMicrotask.bind("undefined"!=typeof window?window:globalThis):e=>(t||(t=Promise.resolve())).then(e).catch((e=>setTimeout((()=>{throw e}),0)))},2133:e=>{e.exports="function"==typeof queueMicrotask?queueMicrotask:e=>Promise.resolve().then(e)},7373:e=>{e.exports=function(e){var t=0;return function(){if(t===e.length)return null;var s=e.length-t,i=Math.random()*s|0,n=e[t+i],r=e[t];return e[t]=n,e[t+i]=r,t++,n}}},2878:e=>{function t(e,t){return{start:e.start,end:e.end,index:t}}function s(e){return{start:e.start,end:e.end}}function i(e,t){return e.index-t.index}function n(e,t){return e.start-t.start}e.exports=function(e,r,o){if("string"!=typeof r)throw new TypeError("argument str must be a string");var a=r.indexOf("=");if(-1===a)return-2;var h=r.slice(a+1).split(","),l=[];l.type=r.slice(0,a);for(var c=0;ce-1&&(p=e-1),isNaN(u)||isNaN(p)||u>p||u<0||l.push({start:u,end:p})}if(l.length<1)return-1;return o&&o.combine?function(e){for(var r=e.map(t).sort(n),o=0,a=1;al.end+1?r[++o]=h:h.end>l.end&&(l.end=h.end,l.index=Math.min(l.index,h.index))}r.length=o+1;var c=r.sort(i).map(s);return c.type=e.type,c}(l):l}},8727:e=>{function t(e){return parseInt(e,10)===e}function s(e){function s(t){if(void 0===t){t=new Array(e);for(var s=0;s{e.exports=function(e,t,s){if("number"!=typeof t)throw new Error("second argument must be a Number");let n,r,o,a,h,l,c=!0;Array.isArray(e)?(n=[],o=r=e.length):(a=Object.keys(e),n={},o=r=a.length);function d(e){function t(){s&&s(e,n),s=null}c?i(t):t()}function u(t,s,i){if(n[t]=i,s&&(h=!0),0==--o||s)d(s);else if(!h&&l{e.exports=function(e,t){let s,n,r,o=!0;Array.isArray(e)?(s=[],n=e.length):(r=Object.keys(e),s={},n=r.length);function a(e){function n(){t&&t(e,s),t=null}o?i(n):n()}function h(e,t,i){s[e]=i,(0==--n||t)&&a(t)}n?r?r.forEach((function(t){e[t]((function(e,s){h(t,e,s)}))})):e.forEach((function(e,t){e((function(e,s){h(t,e,s)}))})):a(null);o=!1};const i=s(9596)},7541:(e,t,s)=>{const i=s(8765),n=s(8499);e.exports={Throttle:i,ThrottleGroup:n}},8499:(e,t,s)=>{const{TokenBucket:i}=s(9439),n=s(8765);e.exports=class{constructor(e={}){if("object"!=typeof e)throw new Error("Options must be an object");this.throttles=[],this.setEnabled(e.enabled),this.setRate(e.rate,e.chunksize)}getEnabled(){return this._enabled}getRate(){return this.bucket.tokensPerInterval}getChunksize(){return this.chunksize}setEnabled(e=!0){if("boolean"!=typeof e)throw new Error("Enabled must be a boolean");this._enabled=e;for(const t of this.throttles)t.setEnabled(e)}setRate(e,t=null){if(!Number.isInteger(e)||e<0)throw new Error("Rate must be an integer bigger than zero");if(e=parseInt(e),t&&("number"!=typeof t||t<=0))throw new Error("Chunksize must be bigger than zero");if(t=t||Math.max(parseInt(e/10),1),t=parseInt(t),e>0&&t>e)throw new Error("Chunk size must be smaller than rate");this.bucket||(this.bucket=new i(e,e,"second",null)),this.bucket.bucketSize=e,this.bucket.tokensPerInterval=e,this.chunksize=t}setChunksize(e){if(!Number.isInteger(e)||e<=0)throw new Error("Chunk size must be an integer bigger than zero");const t=this.getRate();if(e=parseInt(e),t>0&&e>t)throw new Error("Chunk size must be smaller than rate");this.chunksize=e}throttle(e={}){if("object"!=typeof e)throw new Error("Options must be an object");return new n({...e,group:this})}destroy(){for(const e of this.throttles)e.destroy();this.throttles=[]}_addThrottle(e){if(!(e instanceof n))throw new Error("Throttle must be an instance of Throttle");this.throttles.push(e)}_removeThrottle(e){const t=this.throttles.indexOf(e);t>-1&&this.throttles.splice(t,1)}}},8765:(e,t,s)=>{const{EventEmitter:i}=s(7007),{Transform:n}=s(8179),{wait:r}=s(2072);e.exports=class extends n{constructor(e={}){if(super(),"object"!=typeof e)throw new Error("Options must be an object");const t=Object.assign({},e);if(t.group&&!(t.group instanceof o))throw new Error("Group must be an instanece of ThrottleGroup");t.group||(t.group=new o(t)),this._setEnabled(t.enabled||t.group.enabled),this._group=t.group,this._emitter=new i,this._destroyed=!1,this._group._addThrottle(this)}getEnabled(){return this._enabled}getGroup(){return this._group}_setEnabled(e=!0){if("boolean"!=typeof e)throw new Error("Enabled must be a boolean");this._enabled=e}setEnabled(e){this._setEnabled(e),this._enabled?this._emitter.emit("enabled"):this._emitter.emit("disabled")}_transform(e,t){this._processChunk(e,t)}async _waitForTokens(e){return new Promise(((t,s)=>{let i=!1;const n=this;function r(e){if(n._emitter.removeListener("disabled",r),n._emitter.removeListener("destroyed",r),!i){if(i=!0,e)return s(e);t()}}this._emitter.once("disabled",r),this._emitter.once("destroyed",r),this._group.bucket.removeTokens(e,r)}))}_areBothEnabled(){return this._enabled&&this._group.getEnabled()}async _processChunk(e,t){if(!this._areBothEnabled())return t(null,e);let s=0,i=this._group.getChunksize(),n=e.slice(s,s+i);for(;n.length>0;){if(this._areBothEnabled())try{for(;0===this._group.getRate()&&!this._destroyed&&this._areBothEnabled();)if(await r(1e3),this._destroyed)return;if(this._areBothEnabled()&&!this._group.bucket.tryRemoveTokens(n.length)&&(await this._waitForTokens(n.length),this._destroyed))return}catch(e){return t(e)}this.push(n),s+=i,i=this._areBothEnabled()?this._group.getChunksize():e.length-s,n=e.slice(s,s+i)}return t()}destroy(...e){this._group._removeThrottle(this),this._destroyed=!0,this._emitter.emit("destroyed"),super.destroy(...e)}};const o=s(8499)},2072:e=>{e.exports={wait:function(e){return new Promise((t=>setTimeout(t,e)))}}},8179:(e,t,s)=>{const{EventEmitter:i}=s(7007),n=new Error("Stream was destroyed"),r=new Error("Premature close"),o=s(2133),a=s(6080),h=536870911,l=1^h,c=2^h,d=64,u=128,p=256,f=1024,m=2048,g=4096,_=8192,y=16384,w=32768,b=131072,v=131328,k=16^h,x=768^h,S=536838143,E=32^h,C=536739839,P=2<<18,A=4<<18,T=8<<18,I=16<<18,L=32<<18,R=64<<18,O=128<<18,H=256<<18,N=512<<18,M=1024<<18,U=469499903,j=535822335,B=503316479,F=262160,D=536608751,q=8404992,W=14,$=15,z=8405006,V=33587200,G=33587215,K=2359296,Y=270794767,J=Symbol.asyncIterator||Symbol("asyncIterator");class X{constructor(e,{highWaterMark:t=16384,map:s=null,mapWritable:i,byteLength:n,byteLengthWritable:r}={}){this.stream=e,this.queue=new a,this.highWaterMark=t,this.buffered=0,this.error=null,this.pipeline=null,this.drains=null,this.byteLength=r||n||be,this.map=i||s,this.afterWrite=ne.bind(this),this.afterUpdateNextTick=ae.bind(this)}get ended(){return!!(this.stream._duplexState&L)}push(e){return null!==this.map&&(e=this.map(e)),this.buffered+=this.byteLength(e),this.queue.push(e),this.buffered0,this.error=null,this.pipeline=null,this.byteLength=r||n||be,this.map=i||s,this.pipeTo=null,this.afterRead=re.bind(this),this.afterUpdateNextTick=oe.bind(this)}get ended(){return!!(this.stream._duplexState&y)}pipe(e,t){if(null!==this.pipeTo)throw new Error("Can only pipe to one destination");if("function"!=typeof t&&(t=null),this.stream._duplexState|=512,this.pipeTo=e,this.pipeline=new ee(this.stream,e,t),t&&this.stream.on("error",ve),we(e))e._writableState.pipeline=this.pipeline,t&&e.on("error",ve),e.on("finish",this.pipeline.finished.bind(this.pipeline));else{const t=this.pipeline.done.bind(this.pipeline,e),s=this.pipeline.done.bind(this.pipeline,e,null);e.on("error",t),e.on("close",s),e.on("finish",this.pipeline.finished.bind(this.pipeline))}e.on("drain",te.bind(this)),this.stream.emit("piping",e),e.emit("pipe",this.stream)}push(e){const t=this.stream;return null===e?(this.highWaterMark=0,t._duplexState=536805311&t._duplexState|1024,!1):(null!==this.map&&(e=this.map(e)),this.buffered+=this.byteLength(e),this.queue.push(e),t._duplexState=536805375&t._duplexState|128,this.buffered0;)t.push(this.shift());for(let e=0;e0;)i.drains.shift().resolve(!1);null!==i.pipeline&&i.pipeline.done(t,e)}}function ne(e){const t=this.stream;e&&t.destroy(e),t._duplexState&=U,null!==this.drains&&function(e){for(let t=0;t=e._readableState.highWaterMark}static isPaused(e){return!(e._duplexState&p)}[J](){const e=this;let t=null,s=null,i=null;return this.on("error",(e=>{t=e})),this.on("readable",(function(){null!==s&&r(e.read())})),this.on("close",(function(){null!==s&&r(null)})),{[J](){return this},next:()=>new Promise((function(t,n){s=t,i=n;const o=e.read();null!==o?r(o):8&e._duplexState&&r(null)})),return:()=>o(null),throw:e=>o(e)};function r(r){null!==i&&(t?i(t):null!==r||e._duplexState&y?s({value:r,done:null===r}):i(n),i=s=null)}function o(t){return e.destroy(t),new Promise(((s,i)=>{if(8&e._duplexState)return s({value:void 0,done:!0});e.once("close",(function(){t?i(t):s({value:void 0,done:!0})}))}))}}}class pe extends de{constructor(e){super(e),this._duplexState|=16385,this._writableState=new X(this,e),e&&(e.writev&&(this._writev=e.writev),e.write&&(this._write=e.write),e.final&&(this._final=e.final),e.eagerOpen&&this._writableState.updateNextTick())}cork(){this._duplexState|=M}uncork(){this._duplexState&=268435455,this._writableState.updateNextTick()}_writev(e,t){t(null)}_write(e,t){this._writableState.autoBatch(e,t)}_final(e){e(null)}static isBackpressured(e){return!!(146800654&e._duplexState)}static drained(e){if(e.destroyed)return Promise.resolve(!1);const t=e._writableState;var s;const i=((s=e)._writev!==pe.prototype._writev&&s._writev!==fe.prototype._writev?Math.min(1,t.queue.length):t.queue.length)+(e._duplexState&H?1:0);return 0===i?Promise.resolve(!0):(null===t.drains&&(t.drains=[]),new Promise((e=>{t.drains.push({writes:i,resolve:e})})))}write(e){return this._writableState.updateNextTick(),this._writableState.push(e)}end(e){return this._writableState.updateNextTick(),this._writableState.end(e),this}}class fe extends ue{constructor(e){super(e),this._duplexState=1|this._duplexState&b,this._writableState=new X(this,e),e&&(e.writev&&(this._writev=e.writev),e.write&&(this._write=e.write),e.final&&(this._final=e.final))}_writev(e,t){t(null)}_write(e,t){this._writableState.autoBatch(e,t)}_final(e){e(null)}write(e){return this._writableState.updateNextTick(),this._writableState.push(e)}end(e){return this._writableState.updateNextTick(),this._writableState.end(e),this}}class me extends fe{constructor(e){super(e),this._transformState=new Z(this),e&&(e.transform&&(this._transform=e.transform),e.flush&&(this._flush=e.flush))}_write(e,t){this._readableState.buffered>=this._readableState.highWaterMark?this._transformState.data=e:this._transform(e,this._transformState.afterTransform)}_read(e){if(null!==this._transformState.data){const t=this._transformState.data;this._transformState.data=null,e(null),this._transform(t,this._transformState.afterTransform)}else e(null)}destroy(e){super.destroy(e),null!==this._transformState.data&&(this._transformState.data=null,this._transformState.afterTransform())}_transform(e,t){t(null,e)}_flush(e){e(null)}_final(e){this._transformState.afterFinal=e,this._flush(ge.bind(this))}}function ge(e,t){const s=this._transformState.afterFinal;if(e)return s(e);null!=t&&this.push(t),this.push(null),s(null)}function _e(e,...t){const s=Array.isArray(e)?[...e,...t]:[e,...t],i=s.length&&"function"==typeof s[s.length-1]?s.pop():null;if(s.length<2)throw new Error("Pipeline requires at least 2 streams");let n=s[0],o=null,a=null;for(let e=1;e1,l),n.pipe(o)),n=o;if(i){let e=!1;const t=we(o)||!(!o._writableState||!o._writableState.autoDestroy);o.on("error",(e=>{null===a&&(a=e)})),o.on("finish",(()=>{e=!0,t||i(a)})),t&&o.on("close",(()=>i(a||(e?null:r))))}return o;function h(e,t,s,i){e.on("error",i),e.on("close",(function(){if(t&&e._readableState&&!e._readableState.ended)return i(r);if(s&&e._writableState&&!e._writableState.ended)return i(r)}))}function l(e){if(e&&!a){a=e;for(const t of s)t.destroy(e)}}}function ye(e){return!!e._readableState||!!e._writableState}function we(e){return"number"==typeof e._duplexState&&ye(e)}function be(e){return function(e){return"object"==typeof e&&null!==e&&"number"==typeof e.byteLength}(e)?e.byteLength:1024}function ve(){}function ke(){this.destroy(new Error("Stream aborted."))}e.exports={pipeline:_e,pipelinePromise:function(...e){return new Promise(((t,s)=>_e(...e,(e=>{if(e)return s(e);t()}))))},isStream:ye,isStreamx:we,getStreamError:function(e){const t=e._readableState&&e._readableState.error||e._writableState&&e._writableState.error;return t===n?null:t},Stream:de,Writable:pe,Readable:ue,Duplex:fe,Transform:me,PassThrough:class extends me{}}},1035:e=>{function t(e){return(+Date.now()-e)/100&65535}e.exports=function(e){const s=+Date.now(),i=10*(e||5),n=[0];let r=1,o=t(s)-1&65535;return function(e){const a=t(s);let h=a-o&65535;for(h>i&&(h=i),o=a;h--;)r===i&&(r=0),n[r]=n[0===r?i-1:r-1],r++;e&&(n[r-1]+=e);const l=n[r-1],c=n.length{e.exports=function(e,t){if(t>=e.length||t<0)return;var s=e.pop();if(t{e.exports=function e(t,s){if(t&&s)return e(t)(s);if("function"!=typeof t)throw new TypeError("need wrapper function");return Object.keys(t).forEach((function(e){i[e]=t[e]})),i;function i(){for(var e=new Array(arguments.length),s=0;s{},6686:()=>{},4043:()=>{},7828:()=>{},6889:()=>{},8878:()=>{},2701:()=>{},7264:()=>{},1551:()=>{},826:()=>{},61:()=>{},7936:()=>{},2532:()=>{},2123:()=>{},3208:()=>{},2376:()=>{},3970:()=>{},2799:()=>{},8271:()=>{},3278:()=>{},6664:()=>{},1827:()=>{},6819:(e,t,s)=>{e.exports=s(8330).version},5658:(e,t,s)=>{s.r(t),s.d(t,{default:()=>g,enableSecure:()=>m});var i=s(7007),n=s(8179),r=s(1314),o=s(7833),a=s(6458);const h="tcpIncoming",l="tcpOutgoing",c="utpIncoming",d="utpOutgoing",u="webSeed",p=o("webtorrent:peer");let f=!1;const m=()=>{f=!0};class g extends i{constructor(e,t){super(),this.id=e,this.type=t,p("new %s Peer %s",t,e),this.addr=null,this.conn=null,this.swarm=null,this.wire=null,this.source=null,this.connected=!1,this.destroyed=!1,this.timeout=null,this.retries=0,this.sentPe1=!1,this.sentPe2=!1,this.sentPe3=!1,this.sentPe4=!1,this.sentHandshake=!1}onConnect(){if(this.destroyed)return;this.connected=!0,p("Peer %s connected",this.id),clearTimeout(this.connectTimeout);const e=this.conn;e.once("end",(()=>{this.destroy()})),e.once("close",(()=>{this.destroy()})),e.once("finish",(()=>{this.destroy()})),e.once("error",(e=>{this.destroy(e)}));const t=this.wire=new a.A(this.type,this.retries,f);t.once("end",(()=>{this.destroy()})),t.once("close",(()=>{this.destroy()})),t.once("finish",(()=>{this.destroy()})),t.once("error",(e=>{this.destroy(e)})),t.once("pe1",(()=>{this.onPe1()})),t.once("pe2",(()=>{this.onPe2()})),t.once("pe3",(()=>{this.onPe3()})),t.once("pe4",(()=>{this.onPe4()})),t.once("handshake",((e,t)=>{this.onHandshake(e,t)})),this.startHandshakeTimeout(),this.setThrottlePipes(),this.swarm&&("tcpOutgoing"===this.type?f&&0===this.retries&&!this.sentPe1?this.sendPe1():this.sentHandshake||this.handshake():"tcpIncoming"===this.type||this.sentHandshake||this.handshake())}sendPe1(){this.wire.sendPe1(),this.sentPe1=!0}onPe1(){this.sendPe2()}sendPe2(){this.wire.sendPe2(),this.sentPe2=!0}onPe2(){this.sendPe3()}sendPe3(){this.wire.sendPe3(this.swarm.infoHash),this.sentPe3=!0}onPe3(e){this.swarm&&(this.swarm.infoHashHash!==e&&this.destroy(new Error("unexpected crypto handshake info hash for this swarm")),this.sendPe4())}sendPe4(){this.wire.sendPe4(this.swarm.infoHash),this.sentPe4=!0}onPe4(){this.sentHandshake||this.handshake()}clearPipes(){this.conn.unpipe(),this.wire.unpipe()}setThrottlePipes(){const e=this;(0,n.pipeline)(this.conn,this.throttleGroups.down.throttle(),new n.Transform({transform(t,s){e.emit("download",t.length),e.destroyed||s(null,t)}}),this.wire,this.throttleGroups.up.throttle(),new n.Transform({transform(t,s){e.emit("upload",t.length),e.destroyed||s(null,t)}}),this.conn)}onHandshake(e,t){if(!this.swarm)return;if(this.destroyed)return;if(this.swarm.destroyed)return this.destroy(new Error("swarm already destroyed"));if(e!==this.swarm.infoHash)return this.destroy(new Error("unexpected handshake info hash for this swarm"));if(t===this.swarm.peerId)return this.destroy(new Error("refusing to connect to ourselves"));p("Peer %s got handshake %s",this.id,e),clearTimeout(this.handshakeTimeout),this.retries=0;let s=this.addr;!s&&this.conn.remoteAddress&&this.conn.remotePort&&(s=`${this.conn.remoteAddress}:${this.conn.remotePort}`),this.swarm._onWire(this.wire,s),this.swarm&&!this.swarm.destroyed&&(this.sentHandshake||this.handshake())}handshake(){const e={dht:!this.swarm.private&&!!this.swarm.client.dht,fast:!0};this.wire.handshake(this.swarm.infoHash,this.swarm.client.peerId,e),this.sentHandshake=!0}startConnectTimeout(){clearTimeout(this.connectTimeout);const e={webrtc:25e3,tcpOutgoing:5e3,utpOutgoing:5e3};this.connectTimeout=setTimeout((()=>{this.destroy(new Error("connect timeout"))}),e[this.type]),this.connectTimeout.unref&&this.connectTimeout.unref()}startHandshakeTimeout(){clearTimeout(this.handshakeTimeout),this.handshakeTimeout=setTimeout((()=>{this.destroy(new Error("handshake timeout"))}),25e3),this.handshakeTimeout.unref&&this.handshakeTimeout.unref()}destroy(e){if(this.destroyed)return;this.destroyed=!0,this.connected=!1,p("destroy %s %s (error: %s)",this.type,this.id,e&&(e.message||e)),clearTimeout(this.connectTimeout),clearTimeout(this.handshakeTimeout);const t=this.swarm,s=this.conn,i=this.wire;this.swarm=null,this.conn=null,this.wire=null,t&&i&&r(t.wires,t.wires.indexOf(i)),s&&(s.on("error",(()=>{})),s.destroy()),i&&i.destroy(),t&&t.removePeer(this.id)}}g.TYPE_TCP_INCOMING=h,g.TYPE_TCP_OUTGOING=l,g.TYPE_UTP_INCOMING=c,g.TYPE_UTP_OUTGOING=d,g.TYPE_WEBRTC="webrtc",g.TYPE_WEBSEED=u,g.SOURCE_MANUAL="manual",g.SOURCE_TRACKER="tracker",g.SOURCE_DHT="dht",g.SOURCE_LSD="lsd",g.SOURCE_UT_PEX="ut_pex",g.createWebRTCPeer=(e,t,s)=>{const i=new g(e.id,"webrtc");if(i.conn=e,i.swarm=t,i.throttleGroups=s,i.conn.connected)i.onConnect();else{const e=()=>{i.conn.removeListener("connect",t),i.conn.removeListener("error",s)},t=()=>{e(),i.onConnect()},s=t=>{e(),i.destroy(t)};i.conn.once("connect",t),i.conn.once("error",s),i.startConnectTimeout()}return i},g.createTCPIncomingPeer=(e,t)=>g._createIncomingPeer(e,h,t),g.createUTPIncomingPeer=(e,t)=>g._createIncomingPeer(e,c,t),g.createTCPOutgoingPeer=(e,t,s)=>g._createOutgoingPeer(e,t,l,s),g.createUTPOutgoingPeer=(e,t,s)=>g._createOutgoingPeer(e,t,d,s),g._createIncomingPeer=(e,t,s)=>{const i=`${e.remoteAddress}:${e.remotePort}`,n=new g(i,t);return n.conn=e,n.addr=i,n.throttleGroups=s,n.onConnect(),n},g._createOutgoingPeer=(e,t,s,i)=>{const n=new g(e,s);return n.addr=e,n.swarm=t,n.throttleGroups=i,n},g.createWebSeedPeer=(e,t,s,i)=>{const n=new g(t,u);return n.swarm=s,n.conn=e,n.throttleGroups=i,n.onConnect(),n}},988:(e,t,s)=>{s.d(t,{A:()=>f});var i=s(9639);function n(e){const t=e<0?1:0;return e=Math.abs(Number(e||1)),Math.floor(Math.log10(e))+1+t}function r(e){return ArrayBuffer.isView(e)?"arraybufferview":Array.isArray(e)?"array":e instanceof Number?"number":e instanceof Boolean?"boolean":e instanceof Set?"set":e instanceof Map?"map":e instanceof String?"string":e instanceof ArrayBuffer?"arraybuffer":typeof e}function o(e,t,s){const n=[];let r=null;return o._encode(n,e),r=(0,i.xW)(n),o.bytes=r.length,ArrayBuffer.isView(t)?(t.set(r,s),t):r}o.bytes=-1,o._floatConversionDetected=!1,o._encode=function(e,t){if(null!=t)switch(r(t)){case"object":o.dict(e,t);break;case"map":o.dictMap(e,t);break;case"array":o.list(e,t);break;case"set":o.listSet(e,t);break;case"string":o.string(e,t);break;case"number":case"boolean":o.number(e,t);break;case"arraybufferview":o.buffer(e,new Uint8Array(t.buffer,t.byteOffset,t.byteLength));break;case"arraybuffer":o.buffer(e,new Uint8Array(t))}};const a=new Uint8Array([101]),h=new Uint8Array([100]),l=new Uint8Array([108]);o.buffer=function(e,t){e.push((0,i.L0)(t.length+":"),t)},o.string=function(e,t){e.push((0,i.L0)((0,i.L0)(t).byteLength+":"+t))},o.number=function(e,t){if(Number.isInteger(t))return e.push((0,i.L0)("i"+BigInt(t)+"e"));const s=2147483648,n=(t/s|0)*s+(t%s|0);e.push((0,i.L0)("i"+n+"e")),n===t||o._floatConversionDetected||(o._floatConversionDetected=!0,console.warn('WARNING: Possible data corruption detected with value "'+t+'":','Bencoding only defines support for integers, value was converted to "'+n+'"'),console.trace())},o.dict=function(e,t){e.push(h);let s,i=0;const n=Object.keys(t).sort(),r=n.length;for(;i=48)i=10*i+(s-48);else if(r!==t||43!==s){if(r!==t||45!==s){if(46===s)break;throw new Error("not a number: buffer["+r+"] = "+s)}n=-1}}return i*n}function u(e,t,s,n){return null==e||0===e.length?null:("number"!=typeof t&&null==n&&(n=t,t=void 0),"number"!=typeof s&&null==n&&(n=s,s=void 0),u.position=0,u.encoding=n||null,u.data=ArrayBuffer.isView(e)?new Uint8Array(e.slice(t,s)):(0,i.L0)(e),u.bytes=u.data.length,u.next())}u.bytes=0,u.position=0,u.data=null,u.encoding=null,u.next=function(){switch(u.data[u.position]){case 100:return u.dictionary();case 108:return u.list();case 105:return u.integer();default:return u.buffer()}},u.find=function(e){let t=u.position;const s=u.data.length,i=u.data;for(;t{function i(e){return(e>>3)+Number(e%8!=0)}s.d(t,{A:()=>n});class n{get length(){return this.buffer.length<<3}constructor(e=0,t){const s=null==t?void 0:t.grow;this.grow=s?Number.isFinite(s)?i(s):s:0,this.buffer="number"==typeof e?new Uint8Array(i(e)):e}get(e){const t=e>>3;return t>e%8)}set(e,t=!0){const s=e>>3;if(t){if(s>=this.buffer.length){const e=Math.max(s+1,Math.min(2*this.buffer.length,this.grow));if(e<=this.grow){const t=new Uint8Array(e);t.set(this.buffer),this.buffer=t}}this.buffer[s]|=128>>e%8}else s>e%8))}setAll(e,t=0){const s=Math.min(i(t+e.length),this.grow);if(this.buffer.length>3,r=128>>t%8;for(let t=0;t=this.buffer.length)break;r=128}else r>>=1}forEach(e,t=0,s=8*this.buffer.length){let i=t>>3,n=128>>t%8;for(let r=t;r>=1}isEmpty(){for(let e=0;e{s.d(t,{A:()=>I});var i=s(988),n=s(3033),r=s(7264),o=s(7833),a=s(8727),h=s(8179),l=s(9639),c=s(1035),d=s(1314);const u=o("bittorrent-protocol"),p=(0,l.L0)("BitTorrent protocol"),f=new Uint8Array([0,0,0,0]),m=new Uint8Array([0,0,0,1,0]),g=new Uint8Array([0,0,0,1,1]),_=new Uint8Array([0,0,0,1,2]),y=new Uint8Array([0,0,0,1,3]),w=[0,0,0,0,0,0,0,0],b=[0,0,0,3,9,0,0],v=new Uint8Array([0,0,0,1,14]),k=new Uint8Array([0,0,0,1,15]),x=new Uint8Array([0,0,0,0,0,0,0,0]),S=new Uint8Array([0,0,1,2]),E=new Uint8Array([0,0,0,2]);function C(e,t){for(let s=e.length;s--;)e[s]^=t[s];return e}class P{constructor(e,t,s,i){this.piece=e,this.offset=t,this.length=s,this.callback=i}}class A{constructor(){this.buffer=new Uint8Array}get(e){return!0}set(e){}}class T extends h.Duplex{constructor(e=null,t=0,s=!1){super(),this._debugId=(0,l.V5)((0,l.po)(4)),this._debug("new wire"),this.peerId=null,this.peerIdBuffer=null,this.type=e,this.amChoking=!0,this.amInterested=!1,this.peerChoking=!0,this.peerInterested=!1,this.peerPieces=new n.A(0,{grow:4e5}),this.extensions={},this.peerExtensions={},this.requests=[],this.peerRequests=[],this.extendedMapping={},this.peerExtendedMapping={},this.extendedHandshake={},this.peerExtendedHandshake={},this.hasFast=!1,this.allowedFastSet=[],this.peerAllowedFastSet=[],this._ext={},this._nextExt=1,this.uploaded=0,this.downloaded=0,this.uploadSpeed=c(),this.downloadSpeed=c(),this._keepAliveInterval=null,this._timeout=null,this._timeoutMs=0,this._timeoutExpiresAt=null,this._finished=!1,this._parserSize=0,this._parser=null,this._buffer=[],this._bufferSize=0,this._peEnabled=s,s?(this._dh=r.createDiffieHellman("ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a36210000000000090563","hex",2),this._myPubKey=this._dh.generateKeys("hex")):this._myPubKey=null,this._peerPubKey=null,this._sharedSecret=null,this._peerCryptoProvide=[],this._cryptoHandshakeDone=!1,this._cryptoSyncPattern=null,this._waitMaxBytes=null,this._encryptionMethod=null,this._encryptGenerator=null,this._decryptGenerator=null,this._setGenerators=!1,this.once("finish",(()=>this._onFinish())),this.on("finish",this._onFinish),this._debug("type:",this.type),"tcpIncoming"===this.type&&this._peEnabled?this._determineHandshakeType():"tcpOutgoing"===this.type&&this._peEnabled&&0===t?this._parsePe2():this._parseHandshake(null)}setKeepAlive(e){this._debug("setKeepAlive %s",e),clearInterval(this._keepAliveInterval),!1!==e&&(this._keepAliveInterval=setInterval((()=>{this.keepAlive()}),55e3))}setTimeout(e,t){this._debug("setTimeout ms=%d unref=%s",e,t),this._timeoutMs=e,this._timeoutUnref=!!t,this._resetTimeout(!0)}destroy(){if(!this.destroyed)return this._debug("destroy"),this.end(),this}end(e){if(!this.destroyed&&!this.destroying)return this._debug("end"),this._onUninterested(),this._onChoke(),super.end(e)}use(e){const t=e.prototype.name;if(!t)throw new Error('Extension class requires a "name" property on the prototype');this._debug("use extension.name=%s",t);const s=this._nextExt,i=new e(this);function n(){}"function"!=typeof i.onHandshake&&(i.onHandshake=n),"function"!=typeof i.onExtendedHandshake&&(i.onExtendedHandshake=n),"function"!=typeof i.onMessage&&(i.onMessage=n),this.extendedMapping[s]=t,this._ext[t]=i,this[t]=i,this._nextExt+=1}keepAlive(){this._debug("keep-alive"),this._push(f)}sendPe1(){if(this._peEnabled){const e=Math.floor(513*Math.random()),t=(0,l.po)(e);this._push((0,l.xW)([(0,l.fk)(this._myPubKey),t]))}}sendPe2(){const e=Math.floor(513*Math.random()),t=(0,l.po)(e);this._push((0,l.xW)([(0,l.fk)(this._myPubKey),t]))}async sendPe3(e){await this.setEncrypt(this._sharedSecret,e);const t=await(0,l.tW)((0,l.fk)(this._utfToHex("req1")+this._sharedSecret)),s=C(await(0,l.tW)((0,l.fk)(this._utfToHex("req2")+e)),await(0,l.tW)((0,l.fk)(this._utfToHex("req3")+this._sharedSecret))),i=new DataView((0,l.po)(2).buffer).getUint16(0)%512,n=(0,l.po)(i);let r=new Uint8Array(14+i+2);r.set(x),r.set(S,8);const o=new DataView(r.buffer);o.setInt16(12,i),n.copy(r,14),o.setInt16(14+i,0),r=this._encryptHandshake(r),this._push((0,l.xW)([t,s,r]))}async sendPe4(e){await this.setEncrypt(this._sharedSecret,e);const t=new DataView((0,l.po)(2).buffer).getUint16(0)%512,s=(0,l.po)(t);let i=new Uint8Array(14+t);const n=new DataView(i.buffer);i.set(x),i.set(E,8),n.setInt16(12,t),i.set(s,14),i=this._encryptHandshake(i),this._push(i),this._cryptoHandshakeDone=!0,this._debug("completed crypto handshake")}handshake(e,t,s){let i,n;if("string"==typeof e?(e=e.toLowerCase(),i=(0,l.fk)(e)):(i=e,e=(0,l.V5)(i)),"string"==typeof t?n=(0,l.fk)(t):(n=t,t=(0,l.V5)(n)),this._infoHash=i,20!==i.length||20!==n.length)throw new Error("infoHash and peerId MUST have length 20");this._debug("handshake i=%s p=%s exts=%o",e,t,s);const r=new Uint8Array(w);this.extensions={extended:!0,dht:!(!s||!s.dht),fast:!(!s||!s.fast)},r[5]|=16,this.extensions.dht&&(r[7]|=1),this.extensions.fast&&(r[7]|=4),this.extensions.fast&&this.peerExtensions.fast&&(this._debug("fast extension is enabled"),this.hasFast=!0),this._push((0,l.xW)([p,r,i,n])),this._handshakeSent=!0,this.peerExtensions.extended&&!this._extendedHandshakeSent&&this._sendExtendedHandshake()}_sendExtendedHandshake(){const e=Object.assign({},this.extendedHandshake);e.m={};for(const t in this.extendedMapping){const s=this.extendedMapping[t];e.m[s]=Number(t)}this.extended(0,i.A.encode(e)),this._extendedHandshakeSent=!0}choke(){if(!this.amChoking)if(this.amChoking=!0,this._debug("choke"),this._push(m),this.hasFast){let e=0;for(;this.peerRequests.length>e;){const t=this.peerRequests[e];this.allowedFastSet.includes(t.piece)?++e:this.reject(t.piece,t.offset,t.length)}}else for(;this.peerRequests.length;)this.peerRequests.pop()}unchoke(){this.amChoking&&(this.amChoking=!1,this._debug("unchoke"),this._push(g))}interested(){this.amInterested||(this.amInterested=!0,this._debug("interested"),this._push(_))}uninterested(){this.amInterested&&(this.amInterested=!1,this._debug("uninterested"),this._push(y))}have(e){this._debug("have %d",e),this._message(4,[e],null)}bitfield(e){this._debug("bitfield"),ArrayBuffer.isView(e)||(e=e.buffer),this._message(5,[],e)}request(e,t,s,i){return i||(i=()=>{}),this._finished?i(new Error("wire is closed")):!this.peerChoking||this.hasFast&&this.peerAllowedFastSet.includes(e)?(this._debug("request index=%d offset=%d length=%d",e,t,s),this.requests.push(new P(e,t,s,i)),this._timeout||this._resetTimeout(!0),void this._message(6,[e,t,s],null)):i(new Error("peer is choking"))}piece(e,t,s){this._debug("piece index=%d offset=%d",e,t),this._message(7,[e,t],s),this.uploaded+=s.length,this.uploadSpeed(s.length),this.emit("upload",s.length)}cancel(e,t,s){this._debug("cancel index=%d offset=%d length=%d",e,t,s),this._callback(this._pull(this.requests,e,t,s),new Error("request was cancelled"),null),this._message(8,[e,t,s],null)}port(e){this._debug("port %d",e);const t=new Uint8Array(b);new DataView(t.buffer).setUint16(5,e),this._push(t)}suggest(e){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("suggest %d",e),this._message(13,[e],null)}haveAll(){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("have-all"),this._push(v)}haveNone(){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("have-none"),this._push(k)}reject(e,t,s){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("reject index=%d offset=%d length=%d",e,t,s),this._pull(this.peerRequests,e,t,s),this._message(16,[e,t,s],null)}allowedFast(e){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("allowed-fast %d",e),this.allowedFastSet.includes(e)||this.allowedFastSet.push(e),this._message(17,[e],null)}extended(e,t){if(this._debug("extended ext=%s",e),"string"==typeof e&&this.peerExtendedMapping[e]&&(e=this.peerExtendedMapping[e]),"number"!=typeof e)throw new Error(`Unrecognized extension: ${e}`);{const s=new Uint8Array([e]),n=ArrayBuffer.isView(t)?t:i.A.encode(t);this._message(20,[],(0,l.xW)([s,n]))}}async setEncrypt(e,t){let s,i,n,r;switch(this.type){case"tcpIncoming":s=await(0,l.tW)((0,l.fk)(this._utfToHex("keyB")+e+t)),n=await(0,l.tW)((0,l.fk)(this._utfToHex("keyA")+e+t)),i=[];for(const e of s.values())i.push(e);r=[];for(const e of n.values())r.push(e);this._encryptGenerator=new a(i),this._decryptGenerator=new a(r);break;case"tcpOutgoing":s=await(0,l.tW)((0,l.fk)(this._utfToHex("keyA")+e+t)),n=await(0,l.tW)((0,l.fk)(this._utfToHex("keyB")+e+t)),i=[];for(const e of s.values())i.push(e);r=[];for(const e of n.values())r.push(e);this._encryptGenerator=new a(i),this._decryptGenerator=new a(r);break;default:return!1}for(let e=0;e<1024;e++)this._encryptGenerator.randomByte(),this._decryptGenerator.randomByte();return this._setGenerators=!0,!0}_message(e,t,s){const i=s?s.length:0,n=new Uint8Array(5+4*t.length),r=new DataView(n.buffer);r.setUint32(0,n.length+i-4),n[4]=e;for(let e=0;e{if(n===this._pull(this.peerRequests,e,t,s))return i?(this._debug("error satisfying request index=%d offset=%d length=%d (%s)",e,t,s,i.message),void(this.hasFast&&this.reject(e,t,s))):void this.piece(e,t,r)},n=new P(e,t,s,i);this.peerRequests.push(n),this.emit("request",e,t,s,i)}_onPiece(e,t,s){this._debug("got piece index=%d offset=%d",e,t),this._callback(this._pull(this.requests,e,t,s.length),null,s),this.downloaded+=s.length,this.downloadSpeed(s.length),this.emit("download",s.length),this.emit("piece",e,t,s)}_onCancel(e,t,s){this._debug("got cancel index=%d offset=%d length=%d",e,t,s),this._pull(this.peerRequests,e,t,s),this.emit("cancel",e,t,s)}_onPort(e){this._debug("got port %d",e),this.emit("port",e)}_onSuggest(e){if(!this.hasFast)return this._debug("Error: got suggest whereas fast extension is disabled"),void this.destroy();this._debug("got suggest %d",e),this.emit("suggest",e)}_onHaveAll(){if(!this.hasFast)return this._debug("Error: got have-all whereas fast extension is disabled"),void this.destroy();this._debug("got have-all"),this.peerPieces=new A,this.emit("have-all")}_onHaveNone(){if(!this.hasFast)return this._debug("Error: got have-none whereas fast extension is disabled"),void this.destroy();this._debug("got have-none"),this.emit("have-none")}_onReject(e,t,s){if(!this.hasFast)return this._debug("Error: got reject whereas fast extension is disabled"),void this.destroy();this._debug("got reject index=%d offset=%d length=%d",e,t,s),this._callback(this._pull(this.requests,e,t,s),new Error("request was rejected"),null),this.emit("reject",e,t,s)}_onAllowedFast(e){if(!this.hasFast)return this._debug("Error: got allowed-fast whereas fast extension is disabled"),void this.destroy();this._debug("got allowed-fast %d",e),this.peerAllowedFastSet.includes(e)||this.peerAllowedFastSet.push(e),this.peerAllowedFastSet.length>100&&this.peerAllowedFastSet.shift(),this.emit("allowed-fast",e)}_onExtended(e,t){if(0===e){let e;try{e=i.A.decode(t)}catch(e){this._debug("ignoring invalid extended handshake: %s",e.message||e)}if(!e)return;if(this.peerExtendedHandshake=e,"object"==typeof e.m)for(const t in e.m)this.peerExtendedMapping[t]=Number(e.m[t].toString());for(const e in this._ext)this.peerExtendedMapping[e]&&this._ext[e].onExtendedHandshake(this.peerExtendedHandshake);this._debug("got extended handshake"),this.emit("extended","handshake",this.peerExtendedHandshake)}else this.extendedMapping[e]&&(e=this.extendedMapping[e],this._ext[e]&&this._ext[e].onMessage(t)),this._debug("got extended message ext=%s",e),this.emit("extended",e,t)}_onTimeout(){this._debug("request timed out"),this._callback(this.requests.shift(),new Error("request has timed out"),null),this.emit("timeout")}_write(e,t){if(2===this._encryptionMethod&&this._cryptoHandshakeDone&&(e=this._decrypt(e)),this._bufferSize+=e.length,this._buffer.push(e),this._buffer.length>1&&(this._buffer=[(0,l.xW)(this._buffer,this._bufferSize)]),this._cryptoSyncPattern){const t=this._buffer[0].indexOf(this._cryptoSyncPattern);if(-1!==t)this._buffer[0]=this._buffer[0].slice(t+this._cryptoSyncPattern.length),this._bufferSize-=t+this._cryptoSyncPattern.length,this._cryptoSyncPattern=null;else if(this._bufferSize+e.length>this._waitMaxBytes+this._cryptoSyncPattern.length)return this._debug("Error: could not resynchronize"),void this.destroy()}for(;this._bufferSize>=this._parserSize&&!this._cryptoSyncPattern;)if(0===this._parserSize)this._parser(new Uint8Array);else{const e=this._buffer[0];this._bufferSize-=this._parserSize,this._buffer=this._bufferSize?[e.slice(this._parserSize)]:[],this._parser(e.slice(0,this._parserSize))}t(null)}_callback(e,t,s){e&&(this._resetTimeout(!this.peerChoking&&!this._finished),e.callback(t,s))}_resetTimeout(e){if(!e||!this._timeoutMs||!this.requests.length)return clearTimeout(this._timeout),this._timeout=null,void(this._timeoutExpiresAt=null);const t=Date.now()+this._timeoutMs;if(this._timeout){if(t-this._timeoutExpiresAt<.05*this._timeoutMs)return;clearTimeout(this._timeout)}this._timeoutExpiresAt=t,this._timeout=setTimeout((()=>this._onTimeout()),this._timeoutMs),this._timeoutUnref&&this._timeout.unref&&this._timeout.unref()}_parse(e,t){this._parserSize=e,this._parser=t}_parseUntil(e,t){this._cryptoSyncPattern=e,this._waitMaxBytes=t}_onMessageLength(e){const t=new DataView(e.buffer,e.byteOffset,e.byteLength).getUint32(0);t>0?this._parse(t,this._onMessage):(this._onKeepAlive(),this._parse(4,this._onMessageLength))}_onMessage(e){this._parse(4,this._onMessageLength);const t=new DataView(e.buffer,e.byteOffset,e.byteLength);switch(e[0]){case 0:return this._onChoke();case 1:return this._onUnchoke();case 2:return this._onInterested();case 3:return this._onUninterested();case 4:return this._onHave(t.getUint32(1));case 5:return this._onBitField(e.slice(1));case 6:return this._onRequest(t.getUint32(1),t.getUint32(5),t.getUint32(9));case 7:return this._onPiece(t.getUint32(1),t.getUint32(5),e.slice(9));case 8:return this._onCancel(t.getUint32(1),t.getUint32(5),t.getUint32(9));case 9:return this._onPort(t.getUint16(1));case 13:return this._onSuggest(t.getUint32(1));case 14:return this._onHaveAll();case 15:return this._onHaveNone();case 16:return this._onReject(t.getUint32(1),t.getUint32(5),t.getUint32(9));case 17:return this._onAllowedFast(t.getUint32(1));case 20:return this._onExtended(e[1],e.slice(2));default:return this._debug("got unknown message"),this.emit("unknownmessage",e)}}_determineHandshakeType(){this._parse(1,(e=>{const t=e[0];19===t?this._parse(t+48,this._onHandshakeBuffer):this._parsePe1(e)}))}_parsePe1(e){this._parse(95,(t=>{this._onPe1((0,l.xW)([e,t])),this._parsePe3()}))}_parsePe2(){this._parse(96,(e=>{for(this._onPe2(e);!this._setGenerators;);this._parsePe4()}))}async _parsePe3(){const e=await(0,l.tW)((0,l.fk)(this._utfToHex("req1")+this._sharedSecret));this._parseUntil(e,512),this._parse(20,(e=>{for(this._onPe3(e);!this._setGenerators;);this._parsePe3Encrypted()}))}_parsePe3Encrypted(){this._parse(14,(e=>{const t=this._decryptHandshake(e.slice(0,8)),s=this._decryptHandshake(e.slice(8,12)),i=new DataView(this._decryptHandshake(e.slice(12,14)).buffer).getUint16(0);this._parse(i,(e=>{e=this._decryptHandshake(e),this._parse(2,(i=>{const n=new DataView(this._decryptHandshake(i).buffer).getUint16(0);this._parse(n,(i=>{i=this._decryptHandshake(i),this._onPe3Encrypted(t,s,e,i);const r=n?i[0]:null,o=n?i.slice(1,20):null;19===r&&"BitTorrent protocol"===(0,l.dU)(o)?this._onHandshakeBuffer(i.slice(1)):this._parseHandshake()}))}))}))}))}_parsePe4(){const e=this._decryptHandshake(x);this._parseUntil(e,512),this._parse(6,(e=>{const t=this._decryptHandshake(e.slice(0,4)),s=new DataView(this._decryptHandshake(e.slice(4,6)).buffer).getUint16(0);this._parse(s,(e=>{this._decryptHandshake(e),this._onPe4(t),this._parseHandshake(null)}))}))}_parseHandshake(){this._parse(1,(e=>{const t=e[0];if(19!==t)return this._debug("Error: wire not speaking BitTorrent protocol (%s)",t.toString()),void this.end();this._parse(t+48,this._onHandshakeBuffer)}))}_onHandshakeBuffer(e){const t=e.slice(0,19);if("BitTorrent protocol"!==(0,l.dU)(t))return this._debug("Error: wire not speaking BitTorrent protocol (%s)",(0,l.dU)(t)),void this.end();e=e.slice(19),this._onHandshake(e.slice(8,28),e.slice(28,48),{dht:!!(1&e[7]),fast:!!(4&e[7]),extended:!!(16&e[5])}),this._parse(4,this._onMessageLength)}_onFinish(){for(this._finished=!0,this.push(null);this.read(););for(clearInterval(this._keepAliveInterval),this._parse(Number.MAX_VALUE,(()=>{}));this.peerRequests.length;)this.peerRequests.pop();for(;this.requests.length;)this._callback(this.requests.pop(),new Error("wire was closed"),null)}_debug(...e){e[0]=`[${this._debugId}] ${e[0]}`,u(...e)}_pull(e,t,s,i){for(let n=0;n{s.d(t,{NJ:()=>_,V5:()=>o,dU:()=>f,HB:()=>y,xW:()=>h,LC:()=>l,tW:()=>S,fk:()=>a,Ht:()=>w,po:()=>E,L0:()=>g});const i="0123456789abcdef",n=[],r=[];for(let e=0;e<256;e++)n[e]=i[e>>4&15]+i[15&e],e<16&&(e<10?r[48+e]=e:r[87+e]=e);const o=e=>{const t=e.length;let s="",i=0;for(;i{const t=e.length>>1,s=t<<1,i=new Uint8Array(t);let n=0,o=0;for(;o{const s=e.length||0;if(!t){let i=s;for(;i--;)t+=e[i].length}const i=new Uint8Array(t);let n=t,r=s;for(;r--;)n-=e[r].length,i.set(e[r],n);return i},l=(e,t)=>{if(e.length!==t.length)return!1;for(let s=e.length;s>-1;s-=1)if(e[s]!==t[s])return!1;return!0};for(var c="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",d="undefined"==typeof Uint8Array?[]:new Uint8Array(256),u=0;u<64;u++)d[c.charCodeAt(u)]=u;const p=new TextDecoder,f=(e,t)=>{if(!t)return p.decode(e);return new TextDecoder(t).decode(e)},m=new TextEncoder,g=e=>m.encode(e),_=e=>function(e){var t,s=new Uint8Array(e),i=s.length,n="";for(t=0;t>2],n+=c[(3&s[t])<<4|s[t+1]>>4],n+=c[(15&s[t+1])<<2|s[t+2]>>6],n+=c[63&s[t+2]];return i%3==2?n=n.substring(0,n.length-1)+"=":i%3==1&&(n=n.substring(0,n.length-2)+"=="),n}(e),y=e=>{let t,s="",n=0;const r=e.length;for(;n>4]+i[15&t];return s},w=e=>{const t=a(e);if(t.length<=65536)return String.fromCharCode(...t);let s="",i=0;for(;i{if(!k)throw new Error("no web crypto support");"string"==typeof e&&(e=g(e));const i=new Uint8Array(await k.digest(s,e));return t?x[t](i):i},E=e=>{const t=new Uint8Array(e);return v.getRandomValues(t)}},717:(e,t,s)=>{s.r(t),s.d(t,{addListener:()=>u,argv:()=>a,binding:()=>S,browser:()=>r,chdir:()=>E,cwd:()=>k,emit:()=>_,env:()=>o,listeners:()=>v,nextTick:()=>b,off:()=>f,on:()=>d,once:()=>p,prependListener:()=>y,prependOnceListener:()=>w,removeAllListeners:()=>g,removeListener:()=>m,title:()=>n,umask:()=>x,version:()=>h,versions:()=>l});var i=s(9596);const n="browser",r=!0,o={},a=[],h="",l={};function c(){}const d=c,u=c,p=c,f=c,m=c,g=c,_=c,y=c,w=c,b=(e,...t)=>i((()=>e(...t))),v=e=>[],k=()=>"/",x=()=>0,S=e=>{throw new Error("process.binding is not supported")},E=e=>{throw new Error("process.chdir is not supported")}},8330:e=>{e.exports={version:"2.4.10"}}},i={};function n(e){var t=i[e];if(void 0!==t)return t.exports;var r=i[e]={exports:{}};return s[e](r,r.exports,n),r.exports}t=Object.getPrototypeOf?e=>Object.getPrototypeOf(e):e=>e.__proto__,n.t=function(s,i){if(1&i&&(s=this(s)),8&i)return s;if("object"==typeof s&&s){if(4&i&&s.__esModule)return s;if(16&i&&"function"==typeof s.then)return s}var r=Object.create(null);n.r(r);var o={};e=e||[null,t({}),t([]),t(t)];for(var a=2&i&&s;"object"==typeof a&&!~e.indexOf(a);a=t(a))Object.getOwnPropertyNames(a).forEach((e=>o[e]=()=>s[e]));return o.default=()=>s,n.d(r,o),r},n.d=(e,t)=>{for(var s in t)n.o(t,s)&&!n.o(e,s)&&Object.defineProperty(e,s,{enumerable:!0,get:t[s]})},n.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t),n.r=e=>{"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})};var r={};n.d(r,{A:()=>ps});var o=n(7007);function a(e){if("string"!=typeof e)throw new TypeError("Path must be a string. Received "+JSON.stringify(e))}function h(e,t){let s,i="",n=0,r=-1,o=0;for(let a=0;a<=e.length;++a){if(a2){const e=i.lastIndexOf("/");if(e!==i.length-1){-1===e?(i="",n=0):(i=i.slice(0,e),n=i.length-1-i.lastIndexOf("/")),r=a,o=0;continue}}else if(2===i.length||1===i.length){i="",n=0,r=a,o=0;continue}t&&(i.length>0?i+="/..":i="..",n=2)}else i.length>0?i+="/"+e.slice(r+1,a):i=e.slice(r+1,a),n=a-r-1;r=a,o=0}else 46===s&&-1!==o?++o:o=-1}return i}function l(){if(0===arguments.length)return".";let e;for(let t=0;t0&&(void 0===e?e=s:e+="/"+s)}return void 0===e?".":function(e){if(a(e),0===e.length)return".";const t=47===e.charCodeAt(0),s=47===e.charCodeAt(e.length-1);return 0!==(e=h(e,!t)).length||t||(e="."),e.length>0&&s&&(e+="/"),t?"/"+e:e}(e)}n(717);const c="/";var d=n(988),u=n(1467),p=n(8444),f=n(2123);const m=new RegExp(["^npm-debug\\.log$","^\\..*\\.swp$","^\\.DS_Store$","^\\.AppleDouble$","^\\.LSOverride$","^Icon\\r$","^\\._.*","^\\.Spotlight-V100(?:$|\\/)","\\.Trashes","^__MACOSX$","~$","^Thumbs\\.db$","^ehthumbs\\.db$","^[Dd]esktop\\.ini$","@eaDir$"].join("|"));function g(e){return m.test(e)}var _=n(405),y=n(8190),w=n(9596),b=n(9639),v=(n(3468),n(2532));const k=[["udp://tracker.leechers-paradise.org:6969"],["udp://tracker.coppersurfer.tk:6969"],["udp://tracker.opentrackr.org:1337"],["udp://explodie.org:6969"],["udp://tracker.empire-js.us:1337"],["wss://tracker.btorrent.xyz"],["wss://tracker.openwebtorrent.com"],["wss://tracker.webtorrent.dev"]];const x=Symbol("itemPath");function S(e,t,s){var i;if(i=e,"undefined"!=typeof FileList&&i instanceof FileList&&(e=Array.from(e)),Array.isArray(e)||(e=[e]),0===e.length)throw new Error("invalid input type");e.forEach((e=>{if(null==e)throw new Error(`invalid input type: ${e}`)})),1!==(e=e.map((e=>P(e)&&"string"==typeof e.path&&"function"==typeof v?e.path:e))).length||"string"==typeof e[0]||e[0].name||(e[0].name=t.name);let n=null;e.forEach(((t,s)=>{if("string"==typeof t)return;let i=t.fullPath||t.name;i||(i=`Unknown File ${s+1}`,t.unknownName=!0),t[x]=i.split("/"),t[x][0]||t[x].shift(),t[x].length<2?n=null:0===s&&e.length>1?n=t[x][0]:t[x][0]!==n&&(n=null)}));(void 0===t.filterJunkFiles||t.filterJunkFiles)&&(e=e.filter((e=>"string"==typeof e||!function(e){const t=e[e.length-1];return"."===t[0]&&g(t)}(e[x])))),n&&e.forEach((e=>{const t=(ArrayBuffer.isView(e)||A(e))&&!e[x];"string"==typeof e||t||e[x].shift()})),!t.name&&n&&(t.name=n),t.name||e.some((e=>"string"==typeof e?(t.name=function(e,t){if(void 0!==t&&"string"!=typeof t)throw new TypeError('"ext" argument must be a string');a(e);let s,i=0,n=-1,r=!0;if(void 0!==t&&t.length>0&&t.length<=e.length){if(t.length===e.length&&t===e)return"";let o=t.length-1,a=-1;for(s=e.length-1;s>=0;--s){const h=e.charCodeAt(s);if(47===h){if(!r){i=s+1;break}}else-1===a&&(r=!1,a=s+1),o>=0&&(h===t.charCodeAt(o)?-1==--o&&(n=s):(o=-1,n=a))}return i===n?n=a:-1===n&&(n=e.length),e.slice(i,n)}for(s=e.length-1;s>=0;--s)if(47===e.charCodeAt(s)){if(!r){i=s+1;break}}else-1===n&&(r=!1,n=s+1);return-1===n?"":e.slice(i,n)}(e),!0):!e.unknownName&&(t.name=e[x][e[x].length-1],!0))),t.name||(t.name=`Unnamed Torrent ${Date.now()}`);const r=e.reduce(((e,t)=>e+Number("string"==typeof t)),0);let o=1===e.length;if(1===e.length&&"string"==typeof e[0]){if("function"!=typeof v)throw new Error("filesystem paths do not work in the browser");f(e[0],((e,t)=>{if(e)return s(e);o=t,h()}))}else w(h);function h(){y(e.map((e=>t=>{const s={};if(P(e))s.getStream=e.stream(),s.length=e.size;else if(ArrayBuffer.isView(e))s.getStream=[e],s.length=e.length;else{if(!A(e)){if("string"==typeof e){if("function"!=typeof v)throw new Error("filesystem paths do not work in the browser");return void v(e,r>1||o,t)}throw new Error("invalid input type")}s.getStream=async function*(e,t){for await(const s of e)t.length+=s.length,yield s}(e,s),s.length=0}s.path=e[x],t(null,s)})),((e,t)=>{if(e)return s(e);t=t.flat(),s(null,t,o)}))}}const E=5;function C(e,t){return e+t.length}function P(e){return"undefined"!=typeof Blob&&e instanceof Blob}function A(e){return"object"==typeof e&&null!=e&&"function"==typeof e.pipe}const T=function(e,t,s){"function"==typeof t&&([t,s]=[s,t]),S(e,t=t?Object.assign({},t):{},((e,i,n)=>{if(e)return s(e);t.singleFileTorrent=n,function(e,t,s){let i=t.announceList;i||("string"==typeof t.announce?i=[[t.announce]]:Array.isArray(t.announce)&&(i=t.announce.map((e=>[e]))));i||(i=[]);globalThis.WEBTORRENT_ANNOUNCE&&("string"==typeof globalThis.WEBTORRENT_ANNOUNCE?i.push([[globalThis.WEBTORRENT_ANNOUNCE]]):Array.isArray(globalThis.WEBTORRENT_ANNOUNCE)&&(i=i.concat(globalThis.WEBTORRENT_ANNOUNCE.map((e=>[e])))));void 0===t.announce&&void 0===t.announceList&&(i=i.concat(k));"string"==typeof t.urlList&&(t.urlList=[t.urlList]);const n={info:{name:t.name},"creation date":Math.ceil((Number(t.creationDate)||Date.now())/1e3),encoding:"UTF-8"};0!==i.length&&(n.announce=i[0][0],n["announce-list"]=i);void 0!==t.comment&&(n.comment=t.comment);void 0!==t.createdBy&&(n["created by"]=t.createdBy);void 0!==t.private&&(n.info.private=Number(t.private));void 0!==t.info&&Object.assign(n.info,t.info);void 0!==t.sslCert&&(n.info["ssl-cert"]=t.sslCert);void 0!==t.urlList&&(n["url-list"]=t.urlList);const r=e.reduce(C,0),o=t.pieceLength||p(r);n.info["piece length"]=o,async function(e,t,s,i,n){const r=[];let o=0,a=0;const h=e.map((e=>e.getStream)),l=i.onProgress;let c=0,d=0,p=!1;const f=u(_(h),t,{zeroPadding:!1});try{for await(const e of f)await new Promise((t=>{o+=e.length;const i=d;++d,++c{r[i]=h,--c,a+=e.length,l&&l(a,s),t(),p&&0===c&&n(null,(0,b.fk)(r.join("")),o)}))}));if(0===c)return n(null,(0,b.fk)(r.join("")),o);p=!0}catch(e){n(e)}}(e,o,r,t,((i,r,o)=>{if(i)return s(i);n.info.pieces=r,e.forEach((e=>{delete e.getStream})),t.singleFileTorrent?n.info.length=o:n.info.files=e,s(null,d.A.encode(n))}))}(i,t,s)}))};var I=n(7833),L=n(6664),R=n(1827),O=n(3208);self.Blob,self.File,self.FormData,self.Headers,self.Request,self.Response,self.AbortController,self.AbortSignal;const H=self.fetch||(()=>{throw new Error("global fetch is not available!")}),N=[255,255,26,27,28,29,30,31,255,255,255,255,255,255,255,255,255,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255,255,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,255,255,255,255,255];const M=function(e){if(!ArrayBuffer.isView(e)&&"string"!=typeof e)throw new TypeError("base32.decode only takes Buffer or string as parameter");let t,s=0,i=0,n=0;ArrayBuffer.isView(e)||(e=(0,b.L0)(e));const r=new Uint8Array(Math.ceil(5*e.length/8));for(let o=0;o>>s,r[n]=t,n++,t=255&i<<8-s)}return r.subarray(0,n)};function U(e){return e.reduce(((e,t,s,i)=>{const n=t.split("-").map((e=>parseInt(e)));return e.concat(((e,t=e)=>Array.from({length:t-e+1},((t,s)=>s+e)))(...n))}),[])}const j=function(e){const t={},s=e.split("magnet:?")[1];let i;if((s&&s.length>=0?s.split("&"):[]).forEach((e=>{const s=e.split("=");if(2!==s.length)return;const i=s[0];let n=s[1];"dn"===i&&(n=decodeURIComponent(n).replace(/\+/g," ")),"tr"!==i&&"xs"!==i&&"as"!==i&&"ws"!==i||(n=decodeURIComponent(n)),"kt"===i&&(n=decodeURIComponent(n).split("+")),"ix"===i&&(n=Number(n)),"so"===i&&(n=U(decodeURIComponent(n).split(","))),t[i]?(Array.isArray(t[i])||(t[i]=[t[i]]),t[i].push(n)):t[i]=n})),t.xt){(Array.isArray(t.xt)?t.xt:[t.xt]).forEach((e=>{(i=e.match(/^urn:btih:(.{40})/))?t.infoHash=i[1].toLowerCase():(i=e.match(/^urn:btih:(.{32})/))?t.infoHash=(0,b.V5)(M(i[1])):(i=e.match(/^urn:btmh:1220(.{64})/))&&(t.infoHashV2=i[1].toLowerCase())}))}if(t.xs){(Array.isArray(t.xs)?t.xs:[t.xs]).forEach((e=>{(i=e.match(/^urn:btpk:(.{64})/))&&(t.publicKey=i[1].toLowerCase())}))}return t.infoHash&&(t.infoHashBuffer=(0,b.fk)(t.infoHash)),t.infoHashV2&&(t.infoHashV2Buffer=(0,b.fk)(t.infoHashV2)),t.publicKey&&(t.publicKeyBuffer=(0,b.fk)(t.publicKey)),t.dn&&(t.name=t.dn),t.kt&&(t.keywords=t.kt),t.announce=[],("string"==typeof t.tr||Array.isArray(t.tr))&&(t.announce=t.announce.concat(t.tr)),t.urlList=[],("string"==typeof t.as||Array.isArray(t.as))&&(t.urlList=t.urlList.concat(t.as)),("string"==typeof t.ws||Array.isArray(t.ws))&&(t.urlList=t.urlList.concat(t.ws)),t.peerAddresses=[],("string"==typeof t["x.pe"]||Array.isArray(t["x.pe"]))&&(t.peerAddresses=t.peerAddresses.concat(t["x.pe"])),t.announce=Array.from(new Set(t.announce)),t.urlList=Array.from(new Set(t.urlList)),t.peerAddresses=Array.from(new Set(t.peerAddresses)),t};async function B(e){if("string"==typeof e&&/^(stream-)?magnet:/.test(e)){const t=j(e);if(!t.infoHash)throw new Error("Invalid torrent identifier");return t}if("string"==typeof e&&(/^[a-f0-9]{40}$/i.test(e)||/^[a-z2-7]{32}$/i.test(e)))return j(`magnet:?xt=urn:btih:${e}`);if(ArrayBuffer.isView(e)&&20===e.length)return j(`magnet:?xt=urn:btih:${(0,b.V5)(e)}`);if(ArrayBuffer.isView(e))return await async function(e){ArrayBuffer.isView(e)&&(e=d.A.decode(e));q(e.info,"info"),q(e.info["name.utf-8"]||e.info.name,"info.name"),q(e.info["piece length"],"info['piece length']"),q(e.info.pieces,"info.pieces"),e.info.files?e.info.files.forEach((e=>{q("number"==typeof e.length,"info.files[0].length"),q(e["path.utf-8"]||e.path,"info.files[0].path")})):q("number"==typeof e.info.length,"info.length");const t={info:e.info,infoBuffer:d.A.encode(e.info),name:(0,b.dU)(e.info["name.utf-8"]||e.info.name),announce:[]};t.infoHashBuffer=await(0,b.tW)(t.infoBuffer),t.infoHash=(0,b.V5)(t.infoHashBuffer),void 0!==e.info.private&&(t.private=!!e.info.private);e["creation date"]&&(t.created=new Date(1e3*e["creation date"]));e["created by"]&&(t.createdBy=(0,b.dU)(e["created by"]));ArrayBuffer.isView(e.comment)&&(t.comment=(0,b.dU)(e.comment));Array.isArray(e["announce-list"])&&e["announce-list"].length>0?e["announce-list"].forEach((e=>{e.forEach((e=>{t.announce.push((0,b.dU)(e))}))})):e.announce&&t.announce.push((0,b.dU)(e.announce));ArrayBuffer.isView(e["url-list"])&&(e["url-list"]=e["url-list"].length>0?[e["url-list"]]:[]);t.urlList=(e["url-list"]||[]).map((e=>(0,b.dU)(e))),t.announce=Array.from(new Set(t.announce)),t.urlList=Array.from(new Set(t.urlList));const s=e.info.files||[e.info];t.files=s.map(((e,i)=>{const n=[].concat(t.name,e["path.utf-8"]||e.path||[]).map((e=>ArrayBuffer.isView(e)?(0,b.dU)(e):e));return{path:l.apply(null,[c].concat(n)).slice(1),name:n[n.length-1],length:e.length,offset:s.slice(0,i).reduce(D,0)}})),t.length=s.reduce(D,0);const i=t.files[t.files.length-1];return t.pieceLength=e.info["piece length"],t.lastPieceLength=(i.offset+i.length)%t.pieceLength||t.pieceLength,t.pieces=function(e){const t=[];for(let s=0;s{s(null,i)}));else if(n=e,"undefined"!=typeof Blob&&n instanceof Blob)try{r(new Uint8Array(await e.arrayBuffer()))}catch(e){return s(new Error(`Error converting Blob: ${e.message}`))}else if(/^https?:/.test(e))try{const s=await H(e,{headers:{"user-agent":"WebTorrent (https://webtorrent.io)"},signal:AbortSignal.timeout(3e4),...t});r(new Uint8Array(await s.arrayBuffer()))}catch(e){return s(new Error(`Error downloading torrent: ${e.message}`))}else"function"==typeof O.readFile&&"string"==typeof e?O.readFile(e,((e,t)=>{if(e)return s(new Error("Invalid torrent identifier"));r(t)})):w((()=>{s(new Error("Invalid torrent identifier"))}));var n;async function r(e){try{i=await B(e)}catch(e){return s(e)}i&&i.infoHash?s(null,i):s(new Error("Invalid torrent identifier"))}}function D(e,t){return e+t.length}function q(e,t){if(!e)throw new Error(`Torrent is missing required field: ${t}`)}const W=B,$=function(e){e=Object.assign({},e);let t=new Set;e.xt&&"string"==typeof e.xt&&t.add(e.xt),e.xt&&Array.isArray(e.xt)&&(t=new Set(e.xt)),e.infoHashBuffer&&t.add(`urn:btih:${(0,b.V5)(e.infoHashBuffer)}`),e.infoHash&&t.add(`urn:btih:${e.infoHash}`),e.infoHashV2Buffer&&t.add(e.xt=`urn:btmh:1220${(0,b.V5)(e.infoHashV2Buffer)}`),e.infoHashV2&&t.add(`urn:btmh:1220${e.infoHashV2}`);const s=Array.from(t);1===s.length&&(e.xt=s[0]),s.length>1&&(e.xt=s),e.publicKeyBuffer&&(e.xs=`urn:btpk:${(0,b.V5)(e.publicKeyBuffer)}`),e.publicKey&&(e.xs=`urn:btpk:${e.publicKey}`),e.name&&(e.dn=e.name),e.keywords&&(e.kt=e.keywords),e.announce&&(e.tr=e.announce),e.urlList&&(e.ws=e.urlList,delete e.as),e.peerAddresses&&(e["x.pe"]=e.peerAddresses);let i="magnet:?";return Object.keys(e).filter((e=>2===e.length||"x.pe"===e)).forEach(((t,s)=>{const n=Array.isArray(e[t])?e[t]:[e[t]];var r;n.forEach(((e,n)=>{(s>0||n>0)&&("kt"!==t&&"so"!==t||0===n)&&(i+="&"),"dn"===t&&(e=encodeURIComponent(e).replace(/%20/g,"+")),"tr"!==t&&"as"!==t&&"ws"!==t||(e=encodeURIComponent(e)),"xs"!==t||e.startsWith("urn:btpk:")||(e=encodeURIComponent(e)),"kt"===t&&(e=encodeURIComponent(e)),"so"!==t&&(i+="kt"===t&&n>0?`+${e}`:`${t}=${e}`)})),"so"===t&&(i+=`${t}=${r=n,r.reduce(((e,t,s,i)=>(0!==s&&t===i[s-1]+1||e.push([]),e[e.length-1].push(t),e)),[]).map((e=>e.length>1?`${e[0]}-${e[e.length-1]}`:`${e[0]}`))}`)})),i},z="undefined"!=typeof window?window:self,V=z.RTCPeerConnection||z.mozRTCPeerConnection||z.webkitRTCPeerConnection,G=z.RTCSessionDescription||z.mozRTCSessionDescription||z.webkitRTCSessionDescription,K=z.RTCIceCandidate||z.mozRTCIceCandidate||z.webkitRTCIceCandidate;z.RTCIceTransport,z.RTCDataChannel,z.RTCSctpTransport,z.RTCDtlsTransport,z.RTCCertificate,z.MediaStream,z.MediaStreamTrack,z.MediaStreamTrackEvent,z.RTCPeerConnectionIceEvent,z.RTCDataChannelEvent,z.RTCTrackEvent,z.RTCError,z.RTCErrorEvent,z.RTCRtpTransceiver,z.RTCRtpReceiver,z.RTCRtpSender;var Y=n(8179),J=n(6310);const X=I("simple-peer"),Q=65536;function Z(e){return e.replace(/a=ice-options:trickle\s\n/g,"")}class ee extends Y.Duplex{_pc;constructor(e){if(super(e=Object.assign({allowHalfOpen:!1},e)),this.__objectMode=!!e.objectMode,this._id=(0,b.V5)((0,b.po)(4)).slice(0,7),this._debug("new peer %o",e),this.channelName=e.initiator?e.channelName||(0,b.V5)((0,b.po)(20)):null,this.initiator=e.initiator||!1,this.channelConfig=e.channelConfig||ee.channelConfig,this.channelNegotiated=this.channelConfig.negotiated,this.config=Object.assign({},ee.config,e.config),this.offerOptions=e.offerOptions||{},this.answerOptions=e.answerOptions||{},this.sdpTransform=e.sdpTransform||(e=>e),this.trickle=void 0===e.trickle||e.trickle,this.allowHalfTrickle=void 0!==e.allowHalfTrickle&&e.allowHalfTrickle,this.iceCompleteTimeout=e.iceCompleteTimeout||5e3,this._destroying=!1,this._connected=!1,this.remoteAddress=void 0,this.remoteFamily=void 0,this.remotePort=void 0,this.localAddress=void 0,this.localFamily=void 0,this.localPort=void 0,!V)throw"undefined"==typeof window?J(new Error("No WebRTC support: Specify `opts.wrtc` option in this environment"),"ERR_WEBRTC_SUPPORT"):J(new Error("No WebRTC support: Not a supported browser"),"ERR_WEBRTC_SUPPORT");this._pcReady=!1,this._channelReady=!1,this._iceComplete=!1,this._iceCompleteTimer=null,this._channel=null,this._pendingCandidates=[],this._isNegotiating=!1,this._firstNegotiation=!0,this._batchedNegotiation=!1,this._queuedNegotiation=!1,this._sendersAwaitingStable=[],this._closingInterval=null,this._remoteTracks=[],this._remoteStreams=[],this._chunk=null,this._cb=null,this._interval=null;try{this._pc=new V(this.config)}catch(e){return void this.__destroy(J(e,"ERR_PC_CONSTRUCTOR"))}this._isReactNativeWebrtc="number"==typeof this._pc._peerConnectionId,this._pc.oniceconnectionstatechange=()=>{this._onIceStateChange()},this._pc.onicegatheringstatechange=()=>{this._onIceStateChange()},this._pc.onconnectionstatechange=()=>{this._onConnectionStateChange()},this._pc.onsignalingstatechange=()=>{this._onSignalingStateChange()},this._pc.onicecandidate=e=>{this._onIceCandidate(e)},"object"==typeof this._pc.peerIdentity&&this._pc.peerIdentity.catch((e=>{this.__destroy(J(e,"ERR_PC_PEER_IDENTITY"))})),this.initiator||this.channelNegotiated?this._setupData({channel:this._pc.createDataChannel(this.channelName,this.channelConfig)}):this._pc.ondatachannel=e=>{this._setupData(e)},this._debug("initial negotiation"),this._needsNegotiation(),this._onFinishBound=()=>{this._onFinish()},this.once("finish",this._onFinishBound)}get bufferSize(){return this._channel&&this._channel.bufferedAmount||0}get connected(){return this._connected&&"open"===this._channel.readyState}address(){return{port:this.localPort,family:this.localFamily,address:this.localAddress}}signal(e){if(!this._destroying){if(this.destroyed)throw J(new Error("cannot signal after peer is destroyed"),"ERR_DESTROYED");if("string"==typeof e)try{e=JSON.parse(e)}catch(t){e={}}this._debug("signal()"),e.renegotiate&&this.initiator&&(this._debug("got request to renegotiate"),this._needsNegotiation()),e.transceiverRequest&&this.initiator&&(this._debug("got request for transceiver"),this.addTransceiver(e.transceiverRequest.kind,e.transceiverRequest.init)),e.candidate&&(this._pc.remoteDescription&&this._pc.remoteDescription.type?this._addIceCandidate(e.candidate):this._pendingCandidates.push(e.candidate)),e.sdp&&this._pc.setRemoteDescription(new G(e)).then((()=>{this.destroyed||(this._pendingCandidates.forEach((e=>{this._addIceCandidate(e)})),this._pendingCandidates=[],"offer"===this._pc.remoteDescription.type&&this._createAnswer())})).catch((e=>{this.__destroy(J(e,"ERR_SET_REMOTE_DESCRIPTION"))})),e.sdp||e.candidate||e.renegotiate||e.transceiverRequest||this.__destroy(J(new Error("signal() called with invalid signal data"),"ERR_SIGNALING"))}}_addIceCandidate(e){const t=new K(e);this._pc.addIceCandidate(t).catch((e=>{var s;!t.address||t.address.endsWith(".local")?(s="Ignoring unsupported ICE candidate.",console.warn(s)):this.__destroy(J(e,"ERR_ADD_ICE_CANDIDATE"))}))}send(e){if(!this._destroying){if(this.destroyed)throw J(new Error("cannot send after peer is destroyed"),"ERR_DESTROYED");this._channel.send(e)}}_needsNegotiation(){this._debug("_needsNegotiation"),this._batchedNegotiation||(this._batchedNegotiation=!0,queueMicrotask((()=>{this._batchedNegotiation=!1,this.initiator||!this._firstNegotiation?(this._debug("starting batched negotiation"),this.negotiate()):this._debug("non-initiator initial negotiation request discarded"),this._firstNegotiation=!1})))}negotiate(){if(!this._destroying){if(this.destroyed)throw J(new Error("cannot negotiate after peer is destroyed"),"ERR_DESTROYED");this.initiator?this._isNegotiating?(this._queuedNegotiation=!0,this._debug("already negotiating, queueing")):(this._debug("start negotiation"),setTimeout((()=>{this._createOffer()}),0)):this._isNegotiating?(this._queuedNegotiation=!0,this._debug("already negotiating, queueing")):(this._debug("requesting negotiation from initiator"),this.emit("signal",{type:"renegotiate",renegotiate:!0})),this._isNegotiating=!0}}_final(e){this._readableState.ended||this.push(null),e(null)}__destroy(e){this.end(),this._destroy((()=>{}),e)}_destroy(e,t){this.destroyed||this._destroying||(this._destroying=!0,this._debug("destroying (error: %s)",t&&(t.message||t)),setTimeout((()=>{if(this._connected=!1,this._pcReady=!1,this._channelReady=!1,this._remoteTracks=null,this._remoteStreams=null,this._senderMap=null,clearInterval(this._closingInterval),this._closingInterval=null,clearInterval(this._interval),this._interval=null,this._chunk=null,this._cb=null,this._onFinishBound&&this.removeListener("finish",this._onFinishBound),this._onFinishBound=null,this._channel){try{this._channel.close()}catch(e){}this._channel.onmessage=null,this._channel.onopen=null,this._channel.onclose=null,this._channel.onerror=null}if(this._pc){try{this._pc.close()}catch(e){}this._pc.oniceconnectionstatechange=null,this._pc.onicegatheringstatechange=null,this._pc.onsignalingstatechange=null,this._pc.onicecandidate=null,this._pc.ontrack=null,this._pc.ondatachannel=null}this._pc=null,this._channel=null,t&&this.emit("error",t),e()}),0))}_setupData(e){if(!e.channel)return this.__destroy(J(new Error("Data channel event is missing `channel` property"),"ERR_DATA_CHANNEL"));this._channel=e.channel,this._channel.binaryType="arraybuffer","number"==typeof this._channel.bufferedAmountLowThreshold&&(this._channel.bufferedAmountLowThreshold=Q),this.channelName=this._channel.label,this._channel.onmessage=e=>{this._onChannelMessage(e)},this._channel.onbufferedamountlow=()=>{this._onChannelBufferedAmountLow()},this._channel.onopen=()=>{this._onChannelOpen()},this._channel.onclose=()=>{this._onChannelClose()},this._channel.onerror=e=>{const t=e.error instanceof Error?e.error:new Error(`Datachannel error: ${e.message} ${e.filename}:${e.lineno}:${e.colno}`);this.__destroy(J(t,"ERR_DATA_CHANNEL"))};let t=!1;this._closingInterval=setInterval((()=>{this._channel&&"closing"===this._channel.readyState?(t&&this._onChannelClose(),t=!0):t=!1}),5e3)}_write(e,t){if(this.destroyed)return t(J(new Error("cannot write after peer is destroyed"),"ERR_DATA_CHANNEL"));if(this._connected){try{this.send(e)}catch(e){return this.__destroy(J(e,"ERR_DATA_CHANNEL"))}this._channel.bufferedAmount>Q?(this._debug("start backpressure: bufferedAmount %d",this._channel.bufferedAmount),this._cb=t):t(null)}else this._debug("write before connect"),this._chunk=e,this._cb=t}_onFinish(){if(this.destroyed)return;const e=()=>{setTimeout((()=>this.__destroy()),1e3)};this._connected?e():this.once("connect",e)}_startIceCompleteTimeout(){this.destroyed||this._iceCompleteTimer||(this._debug("started iceComplete timeout"),this._iceCompleteTimer=setTimeout((()=>{this._iceComplete||(this._iceComplete=!0,this._debug("iceComplete timeout completed"),this.emit("iceTimeout"),this.emit("_iceComplete"))}),this.iceCompleteTimeout))}_createOffer(){this.destroyed||this._pc.createOffer(this.offerOptions).then((e=>{if(this.destroyed)return;this.trickle||this.allowHalfTrickle||(e.sdp=Z(e.sdp)),e.sdp=this.sdpTransform(e.sdp);const t=()=>{if(this.destroyed)return;const t=this._pc.localDescription||e;this._debug("signal"),this.emit("signal",{type:t.type,sdp:t.sdp})};this._pc.setLocalDescription(e).then((()=>{this._debug("createOffer success"),this.destroyed||(this.trickle||this._iceComplete?t():this.once("_iceComplete",t))})).catch((e=>{this.__destroy(J(e,"ERR_SET_LOCAL_DESCRIPTION"))}))})).catch((e=>{this.__destroy(J(e,"ERR_CREATE_OFFER"))}))}_createAnswer(){this.destroyed||this._pc.createAnswer(this.answerOptions).then((e=>{if(this.destroyed)return;this.trickle||this.allowHalfTrickle||(e.sdp=Z(e.sdp)),e.sdp=this.sdpTransform(e.sdp);const t=()=>{if(this.destroyed)return;const t=this._pc.localDescription||e;this._debug("signal"),this.emit("signal",{type:t.type,sdp:t.sdp}),this.initiator||this._requestMissingTransceivers?.()};this._pc.setLocalDescription(e).then((()=>{this.destroyed||(this.trickle||this._iceComplete?t():this.once("_iceComplete",t))})).catch((e=>{this.__destroy(J(e,"ERR_SET_LOCAL_DESCRIPTION"))}))})).catch((e=>{this.__destroy(J(e,"ERR_CREATE_ANSWER"))}))}_onConnectionStateChange(){this.destroyed||this._destroying||"failed"===this._pc.connectionState&&this.__destroy(J(new Error("Connection failed."),"ERR_CONNECTION_FAILURE"))}_onIceStateChange(){if(this.destroyed)return;const e=this._pc.iceConnectionState,t=this._pc.iceGatheringState;this._debug("iceStateChange (connection: %s) (gathering: %s)",e,t),this.emit("iceStateChange",e,t),"connected"!==e&&"completed"!==e||(this._pcReady=!0,this._maybeReady()),"failed"===e&&this.__destroy(J(new Error("Ice connection failed."),"ERR_ICE_CONNECTION_FAILURE")),"closed"===e&&this.__destroy(J(new Error("Ice connection closed."),"ERR_ICE_CONNECTION_CLOSED"))}getStats(e){const t=e=>("[object Array]"===Object.prototype.toString.call(e.values)&&e.values.forEach((t=>{Object.assign(e,t)})),e);0===this._pc.getStats.length||this._isReactNativeWebrtc?this._pc.getStats().then((s=>{const i=[];s.forEach((e=>{i.push(t(e))})),e(null,i)}),(t=>e(t))):this._pc.getStats.length>0?this._pc.getStats((s=>{if(this.destroyed)return;const i=[];s.result().forEach((e=>{const s={};e.names().forEach((t=>{s[t]=e.stat(t)})),s.id=e.id,s.type=e.type,s.timestamp=e.timestamp,i.push(t(s))})),e(null,i)}),(t=>e(t))):e(null,[])}_maybeReady(){if(this._debug("maybeReady pc %s channel %s",this._pcReady,this._channelReady),this._connected||this._connecting||!this._pcReady||!this._channelReady)return;this._connecting=!0;const e=()=>{this.destroyed||this._destroying||this.getStats(((t,s)=>{if(this.destroyed||this._destroying)return;t&&(s=[]);const i={},n={},r={};let o=!1;s.forEach((e=>{"remotecandidate"!==e.type&&"remote-candidate"!==e.type||(i[e.id]=e),"localcandidate"!==e.type&&"local-candidate"!==e.type||(n[e.id]=e),"candidatepair"!==e.type&&"candidate-pair"!==e.type||(r[e.id]=e)}));const a=e=>{o=!0;let t=n[e.localCandidateId];t&&(t.ip||t.address)?(this.localAddress=t.ip||t.address,this.localPort=Number(t.port)):t&&t.ipAddress?(this.localAddress=t.ipAddress,this.localPort=Number(t.portNumber)):"string"==typeof e.googLocalAddress&&(t=e.googLocalAddress.split(":"),this.localAddress=t[0],this.localPort=Number(t[1])),this.localAddress&&(this.localFamily=this.localAddress.includes(":")?"IPv6":"IPv4");let s=i[e.remoteCandidateId];s&&(s.ip||s.address)?(this.remoteAddress=s.ip||s.address,this.remotePort=Number(s.port)):s&&s.ipAddress?(this.remoteAddress=s.ipAddress,this.remotePort=Number(s.portNumber)):"string"==typeof e.googRemoteAddress&&(s=e.googRemoteAddress.split(":"),this.remoteAddress=s[0],this.remotePort=Number(s[1])),this.remoteAddress&&(this.remoteFamily=this.remoteAddress.includes(":")?"IPv6":"IPv4"),this._debug("connect local: %s:%s remote: %s:%s",this.localAddress,this.localPort,this.remoteAddress,this.remotePort)};if(s.forEach((e=>{"transport"===e.type&&e.selectedCandidatePairId&&a(r[e.selectedCandidatePairId]),("googCandidatePair"===e.type&&"true"===e.googActiveConnection||("candidatepair"===e.type||"candidate-pair"===e.type)&&e.selected)&&a(e)})),o||Object.keys(r).length&&!Object.keys(n).length){if(this._connecting=!1,this._connected=!0,this._chunk){try{this.send(this._chunk)}catch(t){return this.__destroy(J(t,"ERR_DATA_CHANNEL"))}this._chunk=null,this._debug('sent chunk from "write before connect"');const e=this._cb;this._cb=null,e(null)}"number"!=typeof this._channel.bufferedAmountLowThreshold&&(this._interval=setInterval((()=>this._onInterval()),150),this._interval.unref&&this._interval.unref()),this._debug("connect"),this.emit("connect")}else setTimeout(e,100)}))};e()}_onInterval(){!this._cb||!this._channel||this._channel.bufferedAmount>Q||this._onChannelBufferedAmountLow()}_onSignalingStateChange(){this.destroyed||("stable"===this._pc.signalingState&&(this._isNegotiating=!1,this._debug("flushing sender queue",this._sendersAwaitingStable),this._sendersAwaitingStable.forEach((e=>{this._pc.removeTrack(e),this._queuedNegotiation=!0})),this._sendersAwaitingStable=[],this._queuedNegotiation?(this._debug("flushing negotiation queue"),this._queuedNegotiation=!1,this._needsNegotiation()):(this._debug("negotiated"),this.emit("negotiated"))),this._debug("signalingStateChange %s",this._pc.signalingState),this.emit("signalingStateChange",this._pc.signalingState))}_onIceCandidate(e){this.destroyed||(e.candidate&&this.trickle?this.emit("signal",{type:"candidate",candidate:{candidate:e.candidate.candidate,sdpMLineIndex:e.candidate.sdpMLineIndex,sdpMid:e.candidate.sdpMid}}):e.candidate||this._iceComplete||(this._iceComplete=!0,this.emit("_iceComplete")),e.candidate&&this._startIceCompleteTimeout())}_onChannelMessage(e){if(this.destroyed)return;let t=e.data;t instanceof ArrayBuffer?t=new Uint8Array(t):!1===this.__objectMode&&(t=(0,b.L0)(t)),this.push(t)}_onChannelBufferedAmountLow(){if(this.destroyed||!this._cb)return;this._debug("ending backpressure: bufferedAmount %d",this._channel.bufferedAmount);const e=this._cb;this._cb=null,e(null)}_onChannelOpen(){this._connected||this.destroyed||(this._debug("on channel open"),this._channelReady=!0,this._maybeReady())}_onChannelClose(){this.destroyed||(this._debug("on channel close"),this.__destroy())}_debug(){const e=[].slice.call(arguments);e[0]="["+this._id+"] "+e[0],X.apply(null,e)}}ee.WEBRTC_SUPPORT=!!V,ee.config={iceServers:[{urls:["stun:stun.l.google.com:19302","stun:global.stun.twilio.com:3478"]}],sdpSemantics:"unified-plan"},ee.channelConfig={};const te=ee;var se=n(1035),ie=n(7541),ne=n(3278),re=n(8271),oe=n(6686),ae=n(7828),he=n(6889);const le=/^\[?([^\]]+)]?:(\d+)$/;let ce=new Map;function de(e){if(1e5===ce.size&&ce.clear(),!ce.has(e)){const t=le.exec(e);if(!t)throw new Error(`invalid addr: ${e}`);ce.set(e,[t[1],Number(t[2])])}return ce.get(e)}var ue=n(3033),pe=n(4497);async function*fe(e,t={}){if(e?.[Symbol.asyncIterator])return void(yield*e[Symbol.asyncIterator](t.offset));if(!e?.get)throw new Error("First argument must be an abstract-chunk-store compliant store");const s=t.chunkLength||e.chunkLength;if(!s)throw new Error("missing required `chunkLength` property");let i=t.length||e.length;if(!Number.isFinite(i))throw new Error("missing required `length` property");const n=t.offset||0,r=(t,s,i)=>new Promise(((n,r)=>{e.get(t,{offset:i,length:s},((e,t)=>{e&&r(e),n(t)}))}));let o=Math.floor(n/s);const a=n%s;if(n){const e=Math.min(i,s-a);i-=e,yield r(o++,e,a)}for(let e=i;e>0;++o,e-=s)yield r(o,Math.min(e,s))}async function me(e,t,s={}){if(!e?.put)throw new Error("First argument must be an abstract-chunk-store compliant store");const i=s.chunkLength||e.chunkLength;if(!i)throw new Error("missing required `chunkLength` property");const n=s.storeMaxOutstandingPuts||16;let r=0,o=0,a=()=>{},h=!1;for await(const l of u(t,i,{zeroPadding:s.zeroPadding||!1}))await new Promise((t=>{r++<=n&&t(),e.put(o++,l,(e=>{if(e)throw e;--r,t(),h&&0===r&&a()}))}));0!==r&&(h=new Promise((e=>{a=e})),await h)}var ge=n(4018),_e=n(3970),ye=n(3519),we=n(1551);const be={DEFAULT_ANNOUNCE_PEERS:50,MAX_ANNOUNCE_PEERS:82,parseUrl:e=>{const t=new URL(e.replace(/^udp:/,"http:"));return e.match(/^udp:/)&&Object.defineProperties(t,{href:{value:t.href.replace(/^http/,"udp")},protocol:{value:t.protocol.replace(/^http/,"udp")},origin:{value:t.origin.replace(/^http/,"udp")}}),t},...n.t(we,2)};var ve=n(826),ke=n(61),xe=n(2701);const Se=I("simple-websocket"),Ee="function"!=typeof xe?WebSocket:xe;class Ce extends Y.Duplex{constructor(e={}){if("string"==typeof e&&(e={url:e}),super(e=Object.assign({allowHalfOpen:!1},e)),this.__objectMode=!!e.objectMode,null!=e.objectMode&&delete e.objectMode,null==e.url&&null==e.socket)throw new Error("Missing required `url` or `socket` option");if(null!=e.url&&null!=e.socket)throw new Error("Must specify either `url` or `socket` option, not both");if(this._id=(0,b.V5)((0,b.po)(4)).slice(0,7),this._debug("new websocket: %o",e),this.connected=!1,this._chunk=null,this._cb=null,this._interval=null,e.socket)this.url=e.socket.url,this._ws=e.socket,this.connected=e.socket.readyState===Ee.OPEN;else{this.url=e.url;try{this._ws="function"==typeof xe?new Ee(e.url,{...e,encoding:void 0}):new Ee(e.url)}catch(e){return void w((()=>this.destroy(e)))}}this._ws.binaryType="arraybuffer",e.socket&&this.connected?w((()=>this._handleOpen())):this._ws.onopen=()=>this._handleOpen(),this._ws.onmessage=e=>this._handleMessage(e),this._ws.onclose=()=>this._handleClose(),this._ws.onerror=e=>this._handleError(e),this._handleFinishBound=()=>this._handleFinish(),this.once("finish",this._handleFinishBound)}send(e){this._ws.send(e)}_final(e){this._readableState.ended||this.push(null),e(null)}_destroy(e){if(!this.destroyed){if(this._writableState.ended||this.end(),this.connected=!1,clearInterval(this._interval),this._interval=null,this._chunk=null,this._cb=null,this._handleFinishBound&&this.removeListener("finish",this._handleFinishBound),this._handleFinishBound=null,this._ws){const e=this._ws,t=()=>{e.onclose=null};if(e.readyState===Ee.CLOSED)t();else try{e.onclose=t,e.close()}catch(e){t()}e.onopen=null,e.onmessage=null,e.onerror=()=>{}}this._ws=null,e()}}_write(e,t){if(this.destroyed)return t(new Error("cannot write after socket is destroyed"));if(this.connected){try{this.send(e)}catch(e){return this.destroy(e)}"function"!=typeof xe&&this._ws.bufferedAmount>65536?(this._debug("start backpressure: bufferedAmount %d",this._ws.bufferedAmount),this._cb=t):t(null)}else this._debug("write before connect"),this._chunk=e,this._cb=t}_handleOpen(){if(!this.connected&&!this.destroyed){if(this.connected=!0,this._chunk){try{this.send(this._chunk)}catch(e){return this.destroy(e)}this._chunk=null,this._debug('sent chunk from "write before connect"');const e=this._cb;this._cb=null,e(null)}"function"!=typeof xe&&(this._interval=setInterval((()=>this._onInterval()),150),this._interval.unref&&this._interval.unref()),this._debug("connect"),this.emit("connect")}}_handleMessage(e){if(this.destroyed)return;let t=e.data;t instanceof ArrayBuffer&&(t=new Uint8Array(t)),!1===this.__objectMode&&(t=(0,b.L0)(t)),this.push(t)}_handleClose(){this.destroyed||(this._debug("on close"),this.destroy())}_handleError(e){this.destroy(new Error(`Error connecting to ${this.url}`))}_handleFinish(){if(this.destroyed)return;const e=()=>{setTimeout((()=>this.destroy()),1e3)};this.connected?e():this.once("connect",e)}_onInterval(){if(!this._cb||!this._ws||this._ws.bufferedAmount>65536)return;this._debug("ending backpressure: bufferedAmount %d",this._ws.bufferedAmount);const e=this._cb;this._cb=null,e(null)}_debug(){const e=[].slice.call(arguments);e[0]="["+this._id+"] "+e[0],Se.apply(null,e)}}Ce.WEBSOCKET_SUPPORT=!!Ee;const Pe=class extends o{constructor(e,t){super(),this.client=e,this.announceUrl=t,this.interval=null,this.destroyed=!1}setInterval(e){null==e&&(e=this.DEFAULT_ANNOUNCE_INTERVAL),clearInterval(this.interval),e&&(this.interval=setInterval((()=>{this.announce(this.client._defaultAnnounceOpts())}),e),this.interval.unref&&this.interval.unref())}},Ae=I("bittorrent-tracker:websocket-tracker"),Te={};class Ie extends Pe{constructor(e,t){super(e,t),Ae("new websocket tracker %s",t),this.peers={},this.socket=null,this.reconnecting=!1,this.retries=0,this.reconnectTimer=null,this.expectingResponse=!1,this._openSocket()}announce(e){if(this.destroyed||this.reconnecting)return;if(!this.socket.connected)return void this.socket.once("connect",(()=>{this.announce(e)}));const t=Object.assign({},e,{action:"announce",info_hash:this.client._infoHashBinary,peer_id:this.client._peerIdBinary});if(this._trackerId&&(t.trackerid=this._trackerId),"stopped"===e.event||"completed"===e.event)this._send(t);else{const s=Math.min(e.numwant,5);this._generateOffers(s,(e=>{t.numwant=s,t.offers=e,this._send(t)}))}}scrape(e){if(this.destroyed||this.reconnecting)return;if(!this.socket.connected)return void this.socket.once("connect",(()=>{this.scrape(e)}));const t={action:"scrape",info_hash:Array.isArray(e.infoHash)&&e.infoHash.length>0?e.infoHash.map((e=>(0,b.Ht)(e))):e.infoHash&&(0,b.Ht)(e.infoHash)||this.client._infoHashBinary};this._send(t)}destroy(e=Le){if(this.destroyed)return e(null);this.destroyed=!0,clearInterval(this.interval),clearTimeout(this.reconnectTimer);for(const e in this.peers){const t=this.peers[e];clearTimeout(t.trackerTimeout),t.destroy()}if(this.peers=null,this.socket&&(this.socket.removeListener("connect",this._onSocketConnectBound),this.socket.removeListener("data",this._onSocketDataBound),this.socket.removeListener("close",this._onSocketCloseBound),this.socket.removeListener("error",this._onSocketErrorBound),this.socket=null),this._onSocketConnectBound=null,this._onSocketErrorBound=null,this._onSocketDataBound=null,this._onSocketCloseBound=null,Te[this.announceUrl]&&(Te[this.announceUrl].consumers-=1),Te[this.announceUrl].consumers>0)return e();let t,s=Te[this.announceUrl];if(delete Te[this.announceUrl],s.on("error",Le),s.once("close",e),!this.expectingResponse)return i();function i(){t&&(clearTimeout(t),t=null),s.removeListener("data",i),s.destroy(),s=null}t=setTimeout(i,be.DESTROY_TIMEOUT),s.once("data",i)}_openSocket(){if(this.destroyed=!1,this.peers||(this.peers={}),this._onSocketConnectBound=()=>{this._onSocketConnect()},this._onSocketErrorBound=e=>{this._onSocketError(e)},this._onSocketDataBound=e=>{this._onSocketData(e)},this._onSocketCloseBound=()=>{this._onSocketClose()},this.socket=Te[this.announceUrl],this.socket)Te[this.announceUrl].consumers+=1,this.socket.connected&&this._onSocketConnectBound();else{const e=new URL(this.announceUrl);let t;this.client._proxyOpts&&(t="wss:"===e.protocol?this.client._proxyOpts.httpsAgent:this.client._proxyOpts.httpAgent,!t&&this.client._proxyOpts.socksProxy&&(t=this.client._proxyOpts.socksProxy)),this.socket=Te[this.announceUrl]=new Ce({url:this.announceUrl,agent:t}),this.socket.consumers=1,this.socket.once("connect",this._onSocketConnectBound)}this.socket.on("data",this._onSocketDataBound),this.socket.once("close",this._onSocketCloseBound),this.socket.once("error",this._onSocketErrorBound)}_onSocketConnect(){this.destroyed||this.reconnecting&&(this.reconnecting=!1,this.retries=0,this.announce(this.client._defaultAnnounceOpts()))}_onSocketData(e){if(!this.destroyed){this.expectingResponse=!1;try{e=JSON.parse((0,b.dU)(e))}catch(e){return void this.client.emit("warning",new Error("Invalid tracker response"))}"announce"===e.action?this._onAnnounceResponse(e):"scrape"===e.action?this._onScrapeResponse(e):this._onSocketError(new Error(`invalid action in WS response: ${e.action}`))}}_onAnnounceResponse(e){if(e.info_hash!==this.client._infoHashBinary)return void Ae("ignoring websocket data from %s for %s (looking for %s: reused socket)",this.announceUrl,(0,b.HB)(e.info_hash),this.client.infoHash);if(e.peer_id&&e.peer_id===this.client._peerIdBinary)return;Ae("received %s from %s for %s",JSON.stringify(e),this.announceUrl,this.client.infoHash);const t=e["failure reason"];if(t)return this.client.emit("warning",new Error(t));const s=e["warning message"];s&&this.client.emit("warning",new Error(s));const i=e.interval||e["min interval"];i&&this.setInterval(1e3*i);const n=e["tracker id"];if(n&&(this._trackerId=n),null!=e.complete){const t=Object.assign({},e,{announce:this.announceUrl,infoHash:(0,b.HB)(e.info_hash)});this.client.emit("update",t)}let r;if(e.offer&&e.peer_id&&(Ae("creating peer (from remote offer)"),r=this._createPeer(),r.id=(0,b.HB)(e.peer_id),r.once("signal",(t=>{const s={action:"announce",info_hash:this.client._infoHashBinary,peer_id:this.client._peerIdBinary,to_peer_id:e.peer_id,answer:t,offer_id:e.offer_id};this._trackerId&&(s.trackerid=this._trackerId),this._send(s)})),this.client.emit("peer",r),r.signal(e.offer)),e.answer&&e.peer_id){const t=(0,b.HB)(e.offer_id);r=this.peers[t],r?(r.id=(0,b.HB)(e.peer_id),this.client.emit("peer",r),r.signal(e.answer),clearTimeout(r.trackerTimeout),r.trackerTimeout=null,delete this.peers[t]):Ae(`got unexpected answer: ${JSON.stringify(e.answer)}`)}}_onScrapeResponse(e){e=e.files||{};const t=Object.keys(e);0!==t.length?t.forEach((t=>{const s=Object.assign(e[t],{announce:this.announceUrl,infoHash:(0,b.HB)(t)});this.client.emit("scrape",s)})):this.client.emit("warning",new Error("invalid scrape response"))}_onSocketClose(){this.destroyed||(this.destroy(),this._startReconnectTimer())}_onSocketError(e){this.destroyed||(this.destroy(),this.client.emit("warning",e),this._startReconnectTimer())}_startReconnectTimer(){const e=Math.floor(3e5*Math.random())+Math.min(1e4*Math.pow(2,this.retries),36e5);this.reconnecting=!0,clearTimeout(this.reconnectTimer),this.reconnectTimer=setTimeout((()=>{this.retries++,this._openSocket()}),e),this.reconnectTimer.unref&&this.reconnectTimer.unref(),Ae("reconnecting socket in %s ms",e)}_send(e){if(this.destroyed)return;this.expectingResponse=!0;const t=JSON.stringify(e);Ae("send %s",t),this.socket.send(t)}_generateOffers(e,t){const s=this,i=[];Ae("generating %s offers",e);for(let t=0;t{i.push({offer:t,offer_id:(0,b.Ht)(e)}),r()})),t.trackerTimeout=setTimeout((()=>{Ae("tracker timeout: destroying peer"),t.trackerTimeout=null,delete s.peers[e],t.destroy()}),5e4),t.trackerTimeout.unref&&t.trackerTimeout.unref()}function r(){i.length===e&&(Ae("generated %s offers",e),t(i))}r()}_createPeer(e){const t=this;e=Object.assign({trickle:!1,config:t.client._rtcConfig,wrtc:t.client._wrtc},e);const s=new te(e);return s.once("error",i),s.once("connect",(function e(){s.removeListener("error",i),s.removeListener("connect",e)})),s;function i(e){t.client.emit("warning",new Error(`Connection error: ${e.message}`)),s.destroy()}}}function Le(){}Ie.prototype.DEFAULT_ANNOUNCE_INTERVAL=3e4,Ie._socketPool=Te;const Re=Ie;var Oe=n(717);const He=I("bittorrent-tracker:client");class Ne extends o{constructor(e={}){if(super(),!e.peerId)throw new Error("Option `peerId` is required");if(!e.infoHash)throw new Error("Option `infoHash` is required");if(!e.announce)throw new Error("Option `announce` is required");if(!Oe.browser&&!e.port)throw new Error("Option `port` is required");this.peerId="string"==typeof e.peerId?e.peerId:(0,b.V5)(e.peerId),this._peerIdBuffer=(0,b.fk)(this.peerId),this._peerIdBinary=(0,b.Ht)(this.peerId),this.infoHash="string"==typeof e.infoHash?e.infoHash.toLowerCase():(0,b.V5)(e.infoHash),this._infoHashBuffer=(0,b.fk)(this.infoHash),this._infoHashBinary=(0,b.Ht)(this.infoHash),He("new client %s",this.infoHash),this.destroyed=!1,this._port=e.port,this._getAnnounceOpts=e.getAnnounceOpts,this._rtcConfig=e.rtcConfig,this._userAgent=e.userAgent,this._proxyOpts=e.proxyOpts,this._wrtc="function"==typeof e.wrtc?e.wrtc():e.wrtc;let t="string"==typeof e.announce?[e.announce]:null==e.announce?[]:e.announce;t=t.map((e=>(ArrayBuffer.isView(e)&&(e=(0,b.dU)(e)),"/"===e[e.length-1]&&(e=e.substring(0,e.length-1)),e))),t=Array.from(new Set(t));const s=!1!==this._wrtc&&(!!this._wrtc||te.WEBRTC_SUPPORT),i=e=>{w((()=>{this.emit("warning",e)}))};this._trackers=t.map((e=>{let t;try{t=be.parseUrl(e)}catch(t){return i(new Error(`Invalid tracker URL: ${e}`)),null}const n=t.port;if(n<0||n>65535)return i(new Error(`Invalid tracker port: ${e}`)),null;const r=t.protocol;return"http:"!==r&&"https:"!==r||"function"!=typeof ve?"udp:"===r&&"function"==typeof ke?new ke(this,e):"ws:"!==r&&"wss:"!==r||!s||"ws:"===r&&"undefined"!=typeof window&&"https:"===window.location.protocol?(i(new Error(`Unsupported tracker protocol: ${e}`)),null):new Re(this,e):new ve(this,e)})).filter(Boolean)}start(e){(e=this._defaultAnnounceOpts(e)).event="started",He("send `start` %o",e),this._announce(e),this._trackers.forEach((e=>{e.setInterval()}))}stop(e){(e=this._defaultAnnounceOpts(e)).event="stopped",He("send `stop` %o",e),this._announce(e)}complete(e){e||(e={}),(e=this._defaultAnnounceOpts(e)).event="completed",He("send `complete` %o",e),this._announce(e)}update(e){(e=this._defaultAnnounceOpts(e)).event&&delete e.event,He("send `update` %o",e),this._announce(e)}_announce(e){this._trackers.forEach((t=>{t.announce(e)}))}scrape(e){He("send `scrape`"),e||(e={}),this._trackers.forEach((t=>{t.scrape(e)}))}setInterval(e){He("setInterval %d",e),this._trackers.forEach((t=>{t.setInterval(e)}))}destroy(e){if(this.destroyed)return;this.destroyed=!0,He("destroy");const t=this._trackers.map((e=>t=>{e.destroy(t)}));y(t,e),this._trackers=[],this._getAnnounceOpts=null}_defaultAnnounceOpts(e={}){return null==e.numwant&&(e.numwant=be.DEFAULT_ANNOUNCE_PEERS),null==e.uploaded&&(e.uploaded=0),null==e.downloaded&&(e.downloaded=0),this._getAnnounceOpts&&(e=Object.assign({},e,this._getAnnounceOpts())),e}}Ne.scrape=(e,t)=>{if(t=ye(t),!e.infoHash)throw new Error("Option `infoHash` is required");if(!e.announce)throw new Error("Option `announce` is required");const s=Object.assign({},e,{infoHash:Array.isArray(e.infoHash)?e.infoHash[0]:e.infoHash,peerId:(0,b.L0)("01234567890123456789"),port:6881}),i=new Ne(s);i.once("error",t),i.once("warning",t);let n=Array.isArray(e.infoHash)?e.infoHash.length:1;const r={};return i.on("scrape",(e=>{if(n-=1,r[e.infoHash]=e,0===n){i.destroy();const e=Object.keys(r);1===e.length?t(null,r[e[0]]):t(null,r)}})),i.scrape({infoHash:e.infoHash}),i};const Me=Ne;n(7936);var Ue=n(2799),je=n(717);const Be=I("torrent-discovery");class Fe extends o.EventEmitter{constructor(e){if(super(),!e.peerId)throw new Error("Option `peerId` is required");if(!e.infoHash)throw new Error("Option `infoHash` is required");if(!je.browser&&!e.port)throw new Error("Option `port` is required");this.peerId="string"==typeof e.peerId?e.peerId:e.peerId.toString("hex"),this.infoHash="string"==typeof e.infoHash?e.infoHash.toLowerCase():e.infoHash.toString("hex"),this._port=e.port,this._userAgent=e.userAgent,this.destroyed=!1,this._announce=e.announce||[],this._intervalMs=e.intervalMs||9e5,this._trackerOpts=null,this._dhtAnnouncing=!1,this._dhtTimeout=!1,this._internalDHT=!1,this._onWarning=e=>{this.emit("warning",e)},this._onError=e=>{this.emit("error",e)},this._onDHTPeer=(e,t)=>{t.toString("hex")===this.infoHash&&this.emit("peer",`${e.host}:${e.port}`,"dht")},this._onTrackerPeer=e=>{this.emit("peer",e,"tracker")},this._onTrackerAnnounce=()=>{this.emit("trackerAnnounce")},this._onLSDPeer=(e,t)=>{this.emit("peer",e,"lsd")};const t=(e,t)=>{const s=new _e.Client(t);return s.on("warning",this._onWarning),s.on("error",this._onError),s.listen(e),this._internalDHT=!0,s};!1===e.tracker?this.tracker=null:e.tracker&&"object"==typeof e.tracker?(this._trackerOpts=Object.assign({},e.tracker),this.tracker=this._createTracker()):this.tracker=this._createTracker(),!1===e.dht||"function"!=typeof _e.Client?this.dht=null:e.dht&&"function"==typeof e.dht.addNode?this.dht=e.dht:e.dht&&"object"==typeof e.dht?this.dht=t(e.dhtPort,e.dht):this.dht=t(e.dhtPort),this.dht&&(this.dht.on("peer",this._onDHTPeer),this._dhtAnnounce()),!1===e.lsd||"function"!=typeof Ue?this.lsd=null:this.lsd=this._createLSD()}updatePort(e){e!==this._port&&(this._port=e,this.dht&&this._dhtAnnounce(),this.tracker&&(this.tracker.stop(),this.tracker.destroy((()=>{this.tracker=this._createTracker()}))))}complete(e){this.tracker&&this.tracker.complete(e)}destroy(e){if(this.destroyed)return;this.destroyed=!0,clearTimeout(this._dhtTimeout);const t=[];this.tracker&&(this.tracker.stop(),this.tracker.removeListener("warning",this._onWarning),this.tracker.removeListener("error",this._onError),this.tracker.removeListener("peer",this._onTrackerPeer),this.tracker.removeListener("update",this._onTrackerAnnounce),t.push((e=>{this.tracker.destroy(e)}))),this.dht&&this.dht.removeListener("peer",this._onDHTPeer),this._internalDHT&&(this.dht.removeListener("warning",this._onWarning),this.dht.removeListener("error",this._onError),t.push((e=>{this.dht.destroy(e)}))),this.lsd&&(this.lsd.removeListener("warning",this._onWarning),this.lsd.removeListener("error",this._onError),this.lsd.removeListener("peer",this._onLSDPeer),t.push((e=>{this.lsd.destroy(e)}))),y(t,e),this.dht=null,this.tracker=null,this.lsd=null,this._announce=null}_createTracker(){const e=Object.assign({},this._trackerOpts,{infoHash:this.infoHash,announce:this._announce,peerId:this.peerId,port:this._port,userAgent:this._userAgent}),t=new Me(e);return t.on("warning",this._onWarning),t.on("error",this._onError),t.on("peer",this._onTrackerPeer),t.on("update",this._onTrackerAnnounce),t.setInterval(this._intervalMs),t.start(),t}_dhtAnnounce(){this._dhtAnnouncing||(Be("dht announce"),this._dhtAnnouncing=!0,clearTimeout(this._dhtTimeout),this.dht.announce(this.infoHash,this._port,(e=>{this._dhtAnnouncing=!1,Be("dht announce complete"),e&&this.emit("warning",e),this.emit("dhtAnnounce"),this.destroyed||(this._dhtTimeout=setTimeout((()=>{this._dhtAnnounce()}),this._intervalMs+Math.floor(Math.random()*this._intervalMs/5)),this._dhtTimeout.unref&&this._dhtTimeout.unref())})))}_createLSD(){const e=Object.assign({},{infoHash:this.infoHash,peerId:this.peerId,port:this._port}),t=new Ue(e);return t.on("warning",this._onWarning),t.on("error",this._onError),t.on("peer",this._onLSDPeer),t.start(),t}}const De=Fe;const qe=/[<>:"/\\|?*\u0000-\u001F]/g;"undefined"!=typeof navigator&&navigator.storage?.getDirectory&&navigator.storage.getDirectory().then((e=>{e.removeEntry("chunks",{recursive:!0}).catch((()=>{}))}));const We=()=>{};class $e{constructor(e,t={}){if(this.chunkLength=Number(e),!this.chunkLength)throw new Error("First argument must be a chunk length");if("undefined"==typeof navigator||!navigator.storage?.getDirectory)throw new Error("FSA API is not supported");if(this.closed=!1,this.name=t.name||crypto.randomUUID(),this.chunks=[],this.rootDirPromise=t.rootDir||navigator.storage.getDirectory(),this.storageDirPromise=(async()=>(await this.rootDirPromise).getDirectoryHandle(this.name,{create:!0}))(),this.chunksDirPromise=this.storageDirPromise,t.files&&t.rootDir){if(this.chunkMap=[],this.directoryMap={},this.chunksDirPromise=this._getChunksDirHandle(),this.files=t.files.map(((e,t,s)=>{if(null==e.path)throw new Error("File is missing `path` property");if(null==e.length)throw new Error("File is missing `length` property");if(null==e.offset)if(0===t)e.offset=0;else{const i=s[t-1];e.offset=i.offset+i.length}null==e.handle&&(e.handle=this._createFileHandle({path:e.path})),e.blob=this._createBlobReference(e.handle);const i=e.offset,n=e.offset+e.length,r=Math.floor(i/this.chunkLength),o=Math.floor((n-1)/this.chunkLength);for(let t=r;t<=o;++t){const s=t*this.chunkLength,r=is+this.chunkLength?this.chunkLength:n-s,a=i>s?0:s-i;this.chunkMap[t]||(this.chunkMap[t]=[]),this.chunkMap[t].push({from:r,to:o,offset:a,file:e})}return e})),window.addEventListener("pagehide",(()=>this.cleanup())),this.length=this.files.reduce(((e,t)=>e+t.length),0),null!=t.length&&t.length!==this.length)throw new Error("total `files` length is not equal to explicit `length` option")}else this.length=Number(t.length)||1/0;this.length!==1/0&&(this.lastChunkLength=this.length%this.chunkLength||this.chunkLength,this.lastChunkIndex=Math.ceil(this.length/this.chunkLength)-1)}async _getChunkHandle(e){let t=this.chunks[e];if(!t){const s=await this.chunksDirPromise;this.chunks[e]=t=await s.getFileHandle(e,{create:!0})}return t}async _createFileHandle(e){const t=e.path.slice(e.path.lastIndexOf("/")+1);return(await this._getDirectoryHandle(e)).getFileHandle(t.replace(qe,""),{create:!0})}async _createBlobReference(e){return(await e).getFile()}async _getDirectoryHandle(e){const t=e.path.lastIndexOf("/");if(-1===t||0===t)return this.storageDirPromise;const s=e.path=e.path.slice(0,t);return this.directoryMap[s]||(this.directoryMap[s]=(async()=>(await this._getDirectoryHandle(e)).getDirectoryHandle(s.slice(s.lastIndexOf("/")+1),{create:!0}))()),this.directoryMap[s]}async _getChunksDirHandle(){const e=await navigator.storage.getDirectory();return(await e.getDirectoryHandle("chunks",{create:!0})).getDirectoryHandle(this.name,{create:!0})}async put(e,t,s=We){try{return await this._put(e,t),s(null),null}catch(e){return queueMicrotask((()=>s(e))),e}}async _put(e,t){if(this.closed)throw new Error("Storage is closed");const s=e===this.lastChunkIndex;if(s&&t.length!==this.lastChunkLength)throw new Error(`Last chunk length must be ${this.lastChunkLength}`);if(!s&&t.length!==this.chunkLength)throw new Error(`Chunk length must be ${this.chunkLength}`);const i=(async()=>{const s=await this._getChunkHandle(e),i=await s.createWritable({keepExistingData:!1});await i.write(t),await i.close()})();if(!this.files)return i;const n=this.chunkMap[e];if(!n)throw new Error("No files matching the request range");const r=n.map((async({file:e,offset:s,from:i,to:n})=>{e.stream||(e.stream=await(await e.handle).createWritable({keepExistingData:!0})),await e.stream.write({type:"write",position:s,data:t.slice(i,n)})}));r.push(i),await Promise.all(r)}async get(e,t,s=We){null==t&&(t={});try{const i=await this._get(e,t);return s(null,i),i}catch(e){return s(e),e}}async _get(e,t){if("function"==typeof t)return this.get(e,void 0,t);if(this.closed)throw new Error("Storage is closed");const s=e===this.lastChunkIndex?this.lastChunkLength:this.chunkLength,i=t.offset||0,n=t.length?i+t.length:s,r=t.length||s-i;if(i<0||i<0||n>s)throw new Error("Invalid offset and/or length");if(i===n)return new Uint8Array(0);if(!this.files||this.chunks[e]){const t=await this._getChunkHandle(e);let n=await t.getFile();0===i&&r===s||(n=n.slice(i,r+i));const o=await n.arrayBuffer();if(0===o.byteLength)throw new Error(`Index ${e} does not exist`);return new Uint8Array(o)}let o=this.chunkMap[e];if(!o)throw new Error("No files matching the request range");if(t&&(o=o.filter((({from:e,to:t})=>t>i&&e{t&&(s>n&&(s=n),equeueMicrotask((()=>e(new Error(t)))))(e,"Storage is closed");this.closed=!0,this.chunkMap=null,this.directoryMap=null,this.files&&await this.cleanup(),queueMicrotask((()=>e(null)))}async cleanup(){const e=[];for(const t of this.files)t.stream&&(e.push(t.stream.close()),t.stream=null);const t=(async()=>{const e=await this.chunksDirPromise;this.chunks=[];for await(const t of e.keys())await e.removeEntry(t,{recursive:!0});this.chunksDirPromise=await this._getChunksDirHandle()})();await Promise.all(e);for(const e of this.files)e.blob=this._createBlobReference(e.handle);await t}async destroy(e=We){this.close((async t=>{if(t)e(t);else{try{const e=await this.rootDirPromise;await e.removeEntry(this.name,{recursive:!0})}catch(t){return void e(t)}e(null)}}))}}const ze=(e,t)=>t.some((t=>e instanceof t));let Ve,Ge;const Ke=new WeakMap,Ye=new WeakMap,Je=new WeakMap,Xe=new WeakMap,Qe=new WeakMap;let Ze={get(e,t,s){if(e instanceof IDBTransaction){if("done"===t)return Ye.get(e);if("objectStoreNames"===t)return e.objectStoreNames||Je.get(e);if("store"===t)return s.objectStoreNames[1]?void 0:s.objectStore(s.objectStoreNames[0])}return st(e[t])},set:(e,t,s)=>(e[t]=s,!0),has:(e,t)=>e instanceof IDBTransaction&&("done"===t||"store"===t)||t in e};function et(e){return e!==IDBDatabase.prototype.transaction||"objectStoreNames"in IDBTransaction.prototype?(Ge||(Ge=[IDBCursor.prototype.advance,IDBCursor.prototype.continue,IDBCursor.prototype.continuePrimaryKey])).includes(e)?function(...t){return e.apply(it(this),t),st(Ke.get(this))}:function(...t){return st(e.apply(it(this),t))}:function(t,...s){const i=e.call(it(this),t,...s);return Je.set(i,t.sort?t.sort():[t]),st(i)}}function tt(e){return"function"==typeof e?et(e):(e instanceof IDBTransaction&&function(e){if(Ye.has(e))return;const t=new Promise(((t,s)=>{const i=()=>{e.removeEventListener("complete",n),e.removeEventListener("error",r),e.removeEventListener("abort",r)},n=()=>{t(),i()},r=()=>{s(e.error||new DOMException("AbortError","AbortError")),i()};e.addEventListener("complete",n),e.addEventListener("error",r),e.addEventListener("abort",r)}));Ye.set(e,t)}(e),ze(e,Ve||(Ve=[IDBDatabase,IDBObjectStore,IDBIndex,IDBCursor,IDBTransaction]))?new Proxy(e,Ze):e)}function st(e){if(e instanceof IDBRequest)return function(e){const t=new Promise(((t,s)=>{const i=()=>{e.removeEventListener("success",n),e.removeEventListener("error",r)},n=()=>{t(st(e.result)),i()},r=()=>{s(e.error),i()};e.addEventListener("success",n),e.addEventListener("error",r)}));return t.then((t=>{t instanceof IDBCursor&&Ke.set(t,e)})).catch((()=>{})),Qe.set(t,e),t}(e);if(Xe.has(e))return Xe.get(e);const t=tt(e);return t!==e&&(Xe.set(e,t),Qe.set(t,e)),t}const it=e=>Qe.get(e);const nt=["get","getKey","getAll","getAllKeys","count"],rt=["put","add","delete","clear"],ot=new Map;function at(e,t){if(!(e instanceof IDBDatabase)||t in e||"string"!=typeof t)return;if(ot.get(t))return ot.get(t);const s=t.replace(/FromIndex$/,""),i=t!==s,n=rt.includes(s);if(!(s in(i?IDBIndex:IDBObjectStore).prototype)||!n&&!nt.includes(s))return;const r=async function(e,...t){const r=this.transaction(e,n?"readwrite":"readonly");let o=r.store;return i&&(o=o.index(t.shift())),(await Promise.all([o[s](...t),n&&r.done]))[0]};return ot.set(t,r),r}Ze=(e=>({...e,get:(t,s,i)=>at(t,s)||e.get(t,s,i),has:(t,s)=>!!at(t,s)||e.has(t,s)}))(Ze);class ht extends o{constructor(e,t){if(t||(t={}),super(),this.chunkLength=Number(e),!this.chunkLength)throw new Error("First argument must be a chunk length");this.closed=!1,this.destroyed=!1,this.length=Number(t.length)||1/0,this.name=t.name||"idb-chunk-store",this.length!==1/0&&(this.lastChunkLength=this.length%this.chunkLength||this.chunkLength,this.lastChunkIndex=Math.ceil(this.length/this.chunkLength)-1),this.dbPromise=function(e,t,{blocked:s,upgrade:i,blocking:n,terminated:r}={}){const o=indexedDB.open(e,t),a=st(o);return i&&o.addEventListener("upgradeneeded",(e=>{i(st(o.result),e.oldVersion,e.newVersion,st(o.transaction),e)})),s&&o.addEventListener("blocked",(e=>s(e.oldVersion,e.newVersion,e))),a.then((e=>{r&&e.addEventListener("close",(()=>r())),n&&e.addEventListener("versionchange",(e=>n(e.oldVersion,e.newVersion,e)))})).catch((()=>{})),a}(this.name,void 0,{upgrade:e=>e.createObjectStore("chunks"),blocking:()=>this.close(),terminated:()=>{this.closed=!0,this.emit("error",new Error("Database unexpectedly closed"))}})}put(e,t,s=(e=>{})){if(this.closed)return w((()=>s(new Error("Storage is closed"))));const i=e===this.lastChunkIndex;return i&&t.length!==this.lastChunkLength?w((()=>s(new Error("Last chunk length must be "+this.lastChunkLength)))):i||t.length===this.chunkLength?(0===t.byteOffset&&t.byteLength===t.buffer.byteLength||(t=t.slice()),void(async()=>{try{const s=await this.dbPromise;await s.put("chunks",t,e)}catch(e){return void s(e)}s(null)})()):w((()=>s(new Error("Chunk length must be "+this.chunkLength))))}get(e,t,s=((e,t)=>{})){return"function"==typeof t?this.get(e,{},t):(t||(t={}),this.closed?w((()=>s(new Error("Storage is closed")))):void(async()=>{let i;try{const t=await this.dbPromise;i=await t.get("chunks",e)}catch(e){return void s(e)}if(null==i){const e=new Error("Chunk not found");return e.notFound=!0,void s(e)}const n=t.offset||0,r=t.length||i.length-n;0===n&&r===i.length||(i=i.slice(n,r+n)),s(null,i)})())}close(e=(e=>{})){if(this.closed)return w((()=>e(new Error("Storage is closed"))));this.closed=!0,(async()=>{try{(await this.dbPromise).close()}catch(t){return void e(t)}e(null)})()}destroy(e=(e=>{})){return this.closed?w((()=>e(new Error("Storage is closed")))):this.destroyed?w((()=>e(new Error("Storage is destroyed")))):(this.destroyed=!0,void this.close((async t=>{if(t)e(t);else{try{await function(e,{blocked:t}={}){const s=indexedDB.deleteDatabase(e);return t&&s.addEventListener("blocked",(e=>t(e.oldVersion,e))),st(s).then((()=>{}))}(this.name)}catch(t){return void e(t)}e(null)}})))}}var lt=n(4862);const ct="undefined"!=typeof chrome&&!!chrome.loadTimes?1/0:2130706432,dt="undefined"!=typeof navigator&&navigator.storage?.getDirectory&&"undefined"!=typeof FileSystemFileHandle&&FileSystemFileHandle?.prototype?.createWritable,ut=()=>{};class pt{constructor(e,t={}){if(this.chunkLength=Number(e),!this.chunkLength)throw new Error("First argument must be a chunk length");this.length=Number(t.length)||1/0,this.limit=t.limit||ct,this.fallbackStore=null,this.dataStore=null,this.chunkCount=0,this.stores=[],this._init(t),this.dataStore&&(t.max>0&&(this.dataStore=new pe(this.dataStore,{max:t.max})),this.stores.push(this.dataStore)),this.stores.push(this.fallbackStore)}_init(e){if(e.onlyMem||this.limit=this.length)return void(this.fallbackStore=new t(this.chunkLength,e));this.chunkCount=Math.floor(Math.min(this.length,this.limit)/this.chunkLength);const s=this.chunkCount*this.chunkLength,i=this.length-s;this.dataStore=new t(this.chunkLength,{...e,length:s}),this.fallbackStore=new lt(this.chunkLength,{...e,length:i})}get(e,t,s){e>=this.chunkCount?this.fallbackStore.get(e-this.chunkCount,t,s):this.dataStore.get(e,t,s)}put(e,t,s){e>=this.chunkCount?this.fallbackStore.put(e-this.chunkCount,t,s):this.dataStore.put(e,t,s)}close(e=ut){Promise.all(this.stores.map((e=>new Promise((t=>e.close(t)))))).then((t=>{const s=t.find((e=>e));e(s)}))}destroy(e=ut){Promise.all(this.stores.map((e=>new Promise((t=>e.destroy(t)))))).then((t=>{const s=t.find((e=>e));e(s)}))}}var ft=n(3714),mt=n(1314);const gt=I("lt_donthave"),_t=()=>{class e extends o.EventEmitter{constructor(e){super(),this._peerSupports=!1,this._wire=e}onExtendedHandshake(){this._peerSupports=!0}onMessage(e){let t;try{t=new DataView(e.buffer).getUint32(0)}catch(e){return}this._wire.peerPieces.get(t)&&(gt("got donthave %d",t),this._wire.peerPieces.set(t,!1),this.emit("donthave",t),this._failRequests(t))}donthave(e){if(!this._peerSupports)return;gt("donthave %d",e);const t=new Uint8Array(4);new DataView(t.buffer).setUint32(0,e),this._wire.extended("lt_donthave",t)}_failRequests(e){const t=this._wire.requests;for(let s=0;s{class t extends o.EventEmitter{constructor(t){super(),this._wire=t,this._fetching=!1,this._metadataComplete=!1,this._metadataSize=null,this._remainingRejects=null,this._bitfield=new ue.A(0,{grow:1e3}),ArrayBuffer.isView(e)&&this.setMetadata(e)}onHandshake(e,t,s){this._infoHash=e}onExtendedHandshake(e){return e.m&&e.m.ut_metadata?e.metadata_size?"number"!=typeof e.metadata_size||1e7this._metadataSize&&(s=this._metadataSize);const i=this.metadata.slice(t,s);this._data(e,i,this._metadataSize)}_onData(e,t,s){t.length>xt||!this._fetching||(this.metadata.set(t,e*xt),this._bitfield.set(e),this._checkDone())}_onReject(e){this._remainingRejects>0&&this._fetching?(this._request(e),this._remainingRejects-=1):this.emit("warning",new Error('Peer sent "reject" too much'))}_requestPieces(){if(this._fetching){this.metadata=new Uint8Array(this._metadataSize);for(let e=0;e0?this._requestPieces():this.emit("warning",new Error("Peer sent invalid metadata"))}}return t.prototype.name="ut_metadata",t};var Et=n(8878),Ct=n(1133);const Pt=I("webtorrent:file-iterator");class At extends o{constructor(e,{start:t,end:s}){super(),this._torrent=e._torrent,this._pieceLength=e._torrent.pieceLength,this._startPiece=(t+e.offset)/this._pieceLength|0,this._endPiece=(s+e.offset)/this._pieceLength|0,this._piece=this._startPiece,this._offset=t+e.offset-this._startPiece*this._pieceLength,this._missing=s-t+1,this._criticalLength=Math.min(1048576/this._pieceLength|0,2),this._torrent._select(this._startPiece,this._endPiece,1,null,!0),this.destroyed=!1}[Symbol.asyncIterator](){return this}next(){return new Promise(((e,t)=>{if(0===this._missing||this.destroyed)return e({done:!0}),this.destroy();const s=(i,n)=>{if(!this._torrent.bitfield.get(i)){const e=t=>{(t===i||this.destroyed)&&(this._torrent.removeListener("verified",e),s(i,n))};return this._torrent.on("verified",e),this._torrent.critical(i,i+this._criticalLength)}if(this._torrent.destroyed)return t(new Error("Torrent removed"));this._torrent.store.get(i,n,((s,n)=>this.destroyed?e({done:!0}):(Pt("read %s and yielding (length %s) (err %s)",i,n?.length,s?.message),s?t(s):void e({value:n,done:!1}))))},i=Math.min(this._missing,this._pieceLength-this._offset);s(this._piece++,{length:i,offset:this._offset}),this._missing-=i,this._offset=0}))}async return(){return this.destroy(),{done:!0}}async throw(e){throw e}destroy(e=(()=>{}),t){this.destroyed||(this.destroyed=!0,this._torrent.destroyed||this._torrent._deselect(this._startPiece,this._endPiece,!0),this.emit("return"),e(t))}}class Tt extends o{constructor(e,t){super(),this._torrent=e,this._destroyed=!1,this._fileStreams=new Set,this._iterators=new Set,this.name=t.name,this.path=t.path,this.length=t.length,this.size=t.length,this.type=Ct.getType(this.name)||"application/octet-stream",this.offset=t.offset,this.done=!1;const s=t.offset,i=s+t.length-1;this._startPiece=s/this._torrent.pieceLength|0,this._endPiece=i/this._torrent.pieceLength|0,0===this.length&&(this.done=!0,this.emit("done")),this._client=e.client}get downloaded(){if(this._destroyed||!this._torrent.bitfield)return 0;const{pieces:e,bitfield:t,pieceLength:s,lastPieceLength:i}=this._torrent,{_startPiece:n,_endPiece:r}=this,o=n=>{const r=n===e.length-1?i:s;return t.get(n)?r:r-e[n].missing};let a=0;for(let t=n;t<=r;t+=1){const h=o(t);if(a+=h,t===n){const e=this.offset%s;a-=Math.min(e,h)}if(t===r){const t=(r===e.length-1?i:s)-(this.offset+this.length)%s;a-=Math.min(t,h)}}return a}get progress(){return this.length?this.downloaded/this.length:0}select(e){0!==this.length&&this._torrent.select(this._startPiece,this._endPiece,e)}deselect(){0!==this.length&&this._torrent.deselect(this._startPiece,this._endPiece)}[Symbol.asyncIterator](e={}){if(0===this.length)return async function*(){}();const{start:t=0}=e??{},s=e?.end&&e.end{this._iterators.delete(i)})),i}createReadStream(e){const t=this[Symbol.asyncIterator](e),s=Y.Readable.from(t);return this._fileStreams.add(s),s.once("close",(()=>{this._fileStreams.delete(s)})),s}async arrayBuffer(e){const t=new Uint8Array(this.length);let s=0;for await(const i of this[Symbol.asyncIterator](e))t.set(i,s),s+=i.length;return t.buffer}async blob(e){return new Blob([await this.arrayBuffer(e)],{type:this.type})}stream(e){let t;return new ReadableStream({start:()=>{t=this[Symbol.asyncIterator](e)},async pull(e){const{value:s,done:i}=await t.next();i?e.close():e.enqueue(s)},cancel(){t.return()}})}get streamURL(){if(!this._client._server)throw new Error("No server created");return`${this._client._server.pathname}/${this._torrent.infoHash}/${this.path}`}streamTo(e){return e.src=this.streamURL,e}includes(e){return this._startPiece<=e&&this._endPiece>=e}_destroy(){this._destroyed=!0,this._torrent=null;for(const e of this._fileStreams)e.destroy();this._fileStreams.clear();for(const e of this._iterators)e.destroy();this._iterators.clear()}}var It=n(5658);class Lt{constructor(e){this._torrent=e,this._numPieces=e.pieces.length,this._pieces=new Array(this._numPieces),this._onWire=e=>{this.recalculate(),this._initWire(e)},this._onWireHave=e=>{this._pieces[e]+=1},this._onWireBitfield=()=>{this.recalculate()},this._torrent.wires.forEach((e=>{this._initWire(e)})),this._torrent.on("wire",this._onWire),this.recalculate()}getRarestPiece(e){let t=[],s=1/0;for(let i=0;i{this._cleanupWireEvents(e)})),this._torrent=null,this._pieces=null,this._onWire=null,this._onWireHave=null,this._onWireBitfield=null}_initWire(e){e._onClose=()=>{this._cleanupWireEvents(e);for(let t=0;t{const i=await(0,b.tW)(e,"hex");if(this.destroyed)return;this.handshake(t,i);const n=this._torrent.pieces.length,r=new ue.A(n);for(let e=0;e<=n;e++)r.set(e,!0);this.bitfield(r)})),this.once("interested",(()=>{Nt("interested"),this.unchoke()})),this.on("uninterested",(()=>{Nt("uninterested")})),this.on("choke",(()=>{Nt("choke")})),this.on("unchoke",(()=>{Nt("unchoke")})),this.on("bitfield",(()=>{Nt("bitfield")})),this.lt_donthave.on("donthave",(()=>{Nt("donthave")})),this.on("request",((e,t,s,i)=>{Nt("request pieceIndex=%d offset=%d length=%d",e,t,s),this.httpRequest(e,t,s,((t,s)=>{if(t){this.lt_donthave.donthave(e);const t=setTimeout((()=>{this.destroyed||this.have(e)}),1e4);t.unref&&t.unref()}i(t,s)}))}))}async httpRequest(e,t,s,i){i=ye(i);const n=e*this._torrent.pieceLength+t,r=n+s-1,o=this._torrent.files;let a,h;if(o.length<=1)a=[{url:this.url,start:n,end:r}];else{const e=o.filter((e=>e.offset<=r&&e.offset+e.length>n));if(e.length<1)return i(new Error("Could not find file corresponding to web seed range request"));a=e.map((e=>{const t=e.offset+e.length-1;return{url:this.url+("/"===this.url[this.url.length-1]?"":"/")+e.path.replace(this._torrent.path,""),fileOffsetInRange:Math.max(e.offset-n,0),start:Math.max(n-e.offset,0),end:Math.min(t,r-e.offset)}}))}try{h=await Promise.all(a.map((async({start:i,end:n,url:r})=>{Nt("Requesting url=%s pieceIndex=%d offset=%d length=%d start=%d end=%d",r,e,t,s,i,n);const o=await H(r,{cache:"no-store",method:"GET",headers:{"Cache-Control":"no-store","user-agent":`WebTorrent/${Ht} (https://webtorrent.io)`,range:`bytes=${i}-${n}`},signal:AbortSignal.timeout(6e4)});if(!o.ok)throw new Error(`Unexpected HTTP status code ${o.status}`);const a=new Uint8Array(await o.arrayBuffer());return Nt("Got data of length %d",a.length),a})))}catch(e){return i(e)}i(null,(0,b.xW)(h))}destroy(){super.destroy(),this._torrent=null}}class Ut{_items=[];remove(e){for(let t=0;t=0&&0!==e.from&&i.push(n);const r={...s,from:e.to+1};r.to-r.from>=0&&i.push(r),this._items.splice(t,1,...i),t=t-1+i.length}else Dt(e,s)&&(this._items.splice(t,1),t--)}}insert(e){if(e.from>e.to)throw new Error("Invalid interval");if(!e.isStreamSelection){const{notify:t}=e,s=[];for(const t of this._items)t.notify&&qt(e,t)&&s.push(t.notify);s.length>0&&(e.notify=()=>{s.forEach((e=>e())),t?.()}),this.remove(e)}this._items.push(e)}sort(e=((e,t)=>e.from-t.from)){this._items.sort(e)}get length(){return this._items.length}get(e){return this._items[e]}swap(e,t){const s=this._items[e];this._items[e]=this._items[t],this._items[t]=s}clear(){this._items.length=0}*[Symbol.iterator](){for(let e=0;e{this._items.splice(e,1),e--},yield t,delete t.remove}}}function jt(e,t){return e.from<=t.to&&e.from>t.from&&e.to>t.to}function Bt(e,t){return e.to>=t.from&&e.to=t.from&&e.to<=t.to&&i=t.to}const qt=(e,t)=>()=>jt(e,t)||Bt(e,t)||Ft(e,t)||Dt(e,t);var Wt=n(717);const $t=I("webtorrent:torrent"),zt=5e3,Vt=3*bt.BLOCK_LENGTH,Gt=Wt.browser?ge().length:2,Kt=[1e3,5e3,15e3],Yt=`WebTorrent/${Ht} (https://webtorrent.io)`;let Jt;try{Jt=l(oe.statSync("/tmp")&&"/tmp","webtorrent")}catch(e){Jt=l("function"==typeof he.tmpdir?he.tmpdir():"/","webtorrent")}const Xt="undefined"!=typeof window&&"function"==typeof window.requestIdleCallback&&window.requestIdleCallback;class Qt extends o{constructor(e,t,s){super(),this._debugId="unknown infohash",this.client=t,this.announce=s.announce,this.urlList=s.urlList,this.path=s.path||Jt,this.addUID=s.addUID||!1,this.rootDir=s.rootDir||null,this.skipVerify=!!s.skipVerify,this._store=s.store||pt,this._preloadedStore=s.preloadedStore||null,this._storeCacheSlots=void 0!==s.storeCacheSlots?s.storeCacheSlots:20,this._destroyStoreOnDestroy=s.destroyStoreOnDestroy||!1,this.store=null,this.storeOpts=s.storeOpts,this.alwaysChokeSeeders=s.alwaysChokeSeeders??!0,this._getAnnounceOpts=s.getAnnounceOpts,"boolean"==typeof s.private&&(this.private=s.private),this.strategy=s.strategy||"sequential",this.maxWebConns=s.maxWebConns||4,this._rechokeNumSlots=!1===s.uploads||0===s.uploads?0:+s.uploads||10,this._rechokeOptimisticWire=null,this._rechokeOptimisticTime=0,this._rechokeIntervalId=null,this._noPeersIntervalId=null,this._noPeersIntervalTime=s.noPeersIntervalTime?1e3*s.noPeersIntervalTime:3e4,this._startAsDeselected=s.deselect||!1,this.ready=!1,this.destroyed=!1,this.paused=s.paused||!1,this.done=!1,this.metadata=null,this.files=[],this.pieces=[],this._amInterested=!1,this._selections=new Ut,this._critical=[],this.wires=[],this._queue=[],this._peers={},this._peersLength=0,this.received=0,this.uploaded=0,this._downloadSpeed=se(),this._uploadSpeed=se(),this._servers=[],this._xsRequests=[],this._fileModtimes=s.fileModtimes,null!==e&&this._onTorrentId(e),this._debug("new torrent")}get timeRemaining(){return this.done?0:0===this.downloadSpeed?1/0:(this.length-this.downloaded)/this.downloadSpeed*1e3}get downloaded(){if(!this.bitfield)return 0;let e=0;for(let t=0,s=this.pieces.length;t{this.destroyed||this._onParsedTorrent(t)}))):F(e,((e,t)=>{if(!this.destroyed)return e?this._destroy(e):void this._onParsedTorrent(t)}))}_onParsedTorrent(e){if(!this.destroyed){if(this._processParsedTorrent(e),!this.infoHash)return this._destroy(new Error("Malformed torrent data: No info hash"));this._rechokeIntervalId=setInterval((()=>{this._rechoke()}),1e4),this._rechokeIntervalId.unref&&this._rechokeIntervalId.unref(),this.emit("_infoHash",this.infoHash),this.destroyed||(this.emit("infoHash",this.infoHash),this.destroyed||(this.client.listening?this._onListening():this.client.once("listening",(()=>{this._onListening()}))))}}_processParsedTorrent(e){this._debugId=(0,b.V5)(e.infoHash).substring(0,7),void 0!==this.private&&(e.private=this.private),Array.isArray(this.announce)&&(e.announce=e.announce.concat(this.announce)),this.client.tracker&&Array.isArray(this.client.tracker.announce)&&!e.private&&(e.announce=e.announce.concat(this.client.tracker.announce)),this.client.tracker&&globalThis.WEBTORRENT_ANNOUNCE&&!e.private&&(e.announce=e.announce.concat(globalThis.WEBTORRENT_ANNOUNCE)),this.urlList&&(e.urlList=e.urlList.concat(this.urlList)),e.announce=Array.from(new Set(e.announce)),e.urlList=Array.from(new Set(e.urlList)),Object.assign(this,e),this.magnetURI=$(e),this.torrentFile=function(e){const t={info:e.info};return t["announce-list"]=(e.announce||[]).map((e=>(t.announce||(t.announce=e),[e=(0,b.L0)(e)]))),t["url-list"]=e.urlList||[],void 0!==e.private&&(t.private=Number(e.private)),e.created&&(t["creation date"]=e.created.getTime()/1e3|0),e.createdBy&&(t["created by"]=e.createdBy),e.comment&&(t.comment=e.comment),d.A.encode(t)}(e)}_onListening(){this.destroyed||(this.info?this._onMetadata(this):(this.xs&&this._getMetadataFromServer(),this._startDiscovery()))}_startDiscovery(){if(this.discovery||this.destroyed)return;let e=this.client.tracker;e&&(e=Object.assign({},this.client.tracker,{getAnnounceOpts:()=>{if(this.destroyed)return;const e={uploaded:this.uploaded,downloaded:this.downloaded,left:Math.max(this.length-this.downloaded,0)};return this.client.tracker.getAnnounceOpts&&Object.assign(e,this.client.tracker.getAnnounceOpts()),this._getAnnounceOpts&&Object.assign(e,this._getAnnounceOpts()),e}})),this.peerAddresses&&this.peerAddresses.forEach((e=>this.addPeer(e,It.default.SOURCE_MANUAL))),this.discovery=new De({infoHash:this.infoHash,announce:this.announce,peerId:this.client.peerId,dht:!this.private&&this.client.dht,tracker:e,port:this.client.torrentPort,userAgent:Yt,lsd:this.client.lsd}),this.discovery.on("error",(e=>{this._destroy(e)})),this.discovery.on("peer",((e,t)=>{this._debug("peer %s discovered via %s",e,t),this.client.seedOutgoingConnections||!this.done?this.addPeer(e,t):this._debug("ignoring peer %s: torrent is done and seedOutgoingConnections is false",e)})),this.discovery.on("trackerAnnounce",(()=>{this.emit("trackerAnnounce")})),this.discovery.on("dhtAnnounce",(()=>{this.emit("dhtAnnounce")})),this.discovery.on("warning",(e=>{this.emit("warning",e)})),this._noPeersIntervalId=setInterval((()=>{if(this.destroyed)return;const e={[It.default.SOURCE_TRACKER]:{enabled:!!this.client.tracker,numPeers:0},[It.default.SOURCE_DHT]:{enabled:!!this.client.dht,numPeers:0},[It.default.SOURCE_LSD]:{enabled:!!this.client.lsd,numPeers:0},[It.default.SOURCE_UT_PEX]:{enabled:this.client.utPex&&"function"==typeof Et,numPeers:0}};for(const t of Object.values(this._peers)){const s=e[t.source];void 0!==s&&s.numPeers++}for(const t of Object.keys(e)){const s=e[t];s.enabled&&0===s.numPeers&&this.emit("noPeers",t)}}),this._noPeersIntervalTime),this._noPeersIntervalId.unref&&this._noPeersIntervalId.unref()}_getMetadataFromServer(){const e=this,t=Array.isArray(this.xs)?this.xs:[this.xs];e._xsRequestsController=new AbortController;const s=e._xsRequestsController.signal,i=t.map((t=>i=>{!async function(t,i){if(0!==t.indexOf("http://")&&0!==t.indexOf("https://"))return e.emit("warning",new Error(`skipping non-http xs param: ${t}`)),i(null);const n={method:"GET",headers:{"user-agent":Yt},signal:s};let r,o,a;try{r=await H(t,n)}catch(s){return e.emit("warning",new Error(`http error from xs param: ${t}`)),i(null)}if(e.destroyed)return i(null);if(e.metadata)return i(null);if(200!==r.status)return e.emit("warning",new Error(`non-200 status code ${r.status} from xs param: ${t}`)),i(null);try{o=new Uint8Array(await r.arrayBuffer())}catch(t){return e.emit("warning",t),i(null)}try{a=await W(o)}catch(e){}if(!a)return e.emit("warning",new Error(`got invalid torrent file from xs param: ${t}`)),i(null);if(a.infoHash!==e.infoHash)return e.emit("warning",new Error(`got torrent file with incorrect info hash from xs param: ${t}`)),i(null);e._onMetadata(a),i(null)}(t,i)}));y(i)}async _onMetadata(e){if(this.metadata||this.destroyed)return;let t;if(this._debug("got metadata"),this._xsRequestsController?.abort(),this._xsRequestsController=null,e&&e.infoHash)t=e;else try{t=await W(e)}catch(e){return this._destroy(e)}this._processParsedTorrent(t),this.metadata=this.torrentFile,this.client.enableWebSeeds&&this.urlList.forEach((e=>{this.addWebSeed(e)})),this._rarityMap=new Lt(this),this.files=this.files.map((e=>new Tt(this,e)));let s=this._preloadedStore;if(s||(s=new this._store(this.pieceLength,{...this.storeOpts,torrent:this,path:this.path,files:this.files,length:this.length,name:this.name+" - "+this.infoHash.slice(0,8),addUID:this.addUID,rootDir:this.rootDir,max:this._storeCacheSlots})),this._storeCacheSlots>0&&!(s instanceof lt||s instanceof pt)&&(s=new pe(s,{max:this._storeCacheSlots})),this.store=new ft(s),this.so&&!this._startAsDeselected?this.files.forEach(((e,t)=>{this.so.includes(t)&&this.files[t].select()})):0===this.pieces.length||this._startAsDeselected||this.select(0,this.pieces.length-1,0),this._hashes=this.pieces,this.pieces=this.pieces.map(((e,t)=>{const s=t===this.pieces.length-1?this.lastPieceLength:this.pieceLength;return new bt(s)})),this._reservations=this.pieces.map((()=>[])),this.bitfield=new ue.A(this.pieces.length),this.emit("metadata"),!this.destroyed)if(this.skipVerify)this._markAllVerified(),this._onStore();else{const e=e=>{if(e)return this._destroy(e);this._debug("done verifying"),this._onStore()};this._debug("verifying existing torrent data"),this._fileModtimes&&this._store===pt?this.getFileModtimes(((t,s)=>{if(t)return this._destroy(t);this.files.map(((e,t)=>s[t]===this._fileModtimes[t])).every((e=>e))?(this._markAllVerified(),this._onStore()):this._verifyPieces(e)})):this._verifyPieces(e)}}getFileModtimes(e){const t=[];yt(this.files.map(((e,s)=>i=>{const n=this.addUID?l(this.name+" - "+this.infoHash.slice(0,8)):l(this.path,e.path);oe.stat(n,((e,n)=>{if(e&&"ENOENT"!==e.code)return i(e);t[s]=n&&n.mtime.getTime(),i(null)}))})),Gt,(s=>{this._debug("done getting file modtimes"),e(s,t)}))}_verifyPieces(e){yt(this.pieces.map(((e,t)=>e=>{if(this.destroyed)return e(new Error("torrent is destroyed"));const s={};t===this.pieces.length-1&&(s.length=this.lastPieceLength),this.store.get(t,s,(async(s,i)=>{if(this.destroyed)return e(new Error("torrent is destroyed"));if(s)return w((()=>e(null)));const n=await(0,b.tW)(i,"hex");if(this.destroyed)return e(new Error("torrent is destroyed"));n===this._hashes[t]?(this._debug("piece verified %s",t),this._markVerified(t)):(this._markUnverified(t),this._debug("piece invalid %s",t)),e(null)}))})),Gt,e)}rescanFiles(e){if(this.destroyed)throw new Error("torrent is destroyed");e||(e=es),this._verifyPieces((t=>{if(t)return this._destroy(t),e(t);this._checkDone(),e(null)}))}_markAllVerified(){for(let e=0;e{t.done&&t.includes(e)&&(t.done=!1)}))}_hasAllPieces(){for(let e=0;ee))return!0;return!1}_onStore(){this.destroyed||(this._debug("on store"),this._startDiscovery(),this.ready=!0,this.emit("ready"),this._checkDone(),this._updateSelections(),this.wires.forEach((e=>{e.ut_metadata&&e.ut_metadata.setMetadata(this.metadata),this._onWireWithMetadata(e)})))}destroy(e,t){if("function"==typeof e)return this.destroy(null,e);this._destroy(null,e,t)}_destroy(e,t,s){if("function"==typeof t)return this._destroy(e,null,t);if(this.destroyed)return;this.destroyed=!0,this._debug("destroy"),this.client._remove(this),this._selections.clear(),clearInterval(this._rechokeIntervalId),clearInterval(this._noPeersIntervalId),this._xsRequestsController?.abort(),this._rarityMap&&this._rarityMap.destroy();for(const e in this._peers)this.removePeer(e);this.files.forEach((e=>{e instanceof Tt&&e._destroy()}));const i=this._servers.map((e=>t=>{e.destroy(t)}));if(this.discovery&&i.push((e=>{this.discovery.destroy(e)})),this.store){let e=this._destroyStoreOnDestroy;t&&void 0!==t.destroyStore&&(e=t.destroyStore),i.push((t=>{e?this.store.destroy(t):this.store.close(t)}))}y(i,s),e&&(0===this.listenerCount("error")?this.client.emit("error",e):this.emit("error",e)),this.emit("close"),this.client=null,this.files=[],this.discovery=null,this.store=null,this._rarityMap=null,this._peers=null,this._servers=null,this._xsRequests=null}addPeer(e,t){if(this.destroyed)throw new Error("torrent is destroyed");if(!this.infoHash)throw new Error("addPeer() must not be called before the `infoHash` event");let s;if(this.client.blocked){if("string"==typeof e){let t;try{t=de(e)}catch(t){return this._debug("ignoring peer: invalid %s",e),this.emit("invalidPeer",e),!1}s=t[0]}else"string"==typeof e.remoteAddress&&(s=e.remoteAddress);if(s&&this.client.blocked.contains(s))return this._debug("ignoring peer: blocked %s",e),"string"!=typeof e&&e.destroy(),this.emit("blockedPeer",e),!1}const i=this.client.utp&&this._isIPv4(s)?"utp":"tcp",n=!!this._addPeer(e,i,t);return n?this.emit("peer",e):this.emit("invalidPeer",e),n}_addPeer(e,t,s){if(this.destroyed)return"string"!=typeof e&&e.destroy(),null;if("string"==typeof e&&!this._validAddr(e))return this._debug("ignoring peer: invalid %s",e),null;const i=e&&e.id||e;if(this._peers[i])return this._debug("ignoring peer: duplicate (%s)",i),"string"!=typeof e&&e.destroy(),null;if(this.paused)return this._debug("ignoring peer: torrent is paused"),"string"!=typeof e&&e.destroy(),null;let n;return this._debug("add peer %s",i),n="string"==typeof e?"utp"===t?It.default.createUTPOutgoingPeer(e,this,this.client.throttleGroups):It.default.createTCPOutgoingPeer(e,this,this.client.throttleGroups):It.default.createWebRTCPeer(e,this,this.client.throttleGroups),this._registerPeer(n),"string"==typeof e&&(this._queue.push(n),this._drain()),n}addWebSeed(e){if(this.destroyed)throw new Error("torrent is destroyed");let t,s;if("string"==typeof e){if(t=e,!/^https?:\/\/.+/.test(t))return this.emit("warning",new Error(`ignoring invalid web seed: ${t}`)),void this.emit("invalidPeer",t);if(this._peers[t])return this.emit("warning",new Error(`ignoring duplicate web seed: ${t}`)),void this.emit("invalidPeer",t);s=new Mt(t,this)}else{if(!e||"string"!=typeof e.connId)return void this.emit("warning",new Error("addWebSeed must be passed a string or connection object with id property"));if(s=e,t=s.connId,this._peers[t])return this.emit("warning",new Error(`ignoring duplicate web seed: ${t}`)),void this.emit("invalidPeer",t)}this._debug("add web seed %s",t);const i=It.default.createWebSeedPeer(s,t,this,this.client.throttleGroups);this._registerPeer(i),this.emit("peer",t)}_addIncomingPeer(e){return this.destroyed?e.destroy(new Error("torrent is destroyed")):this.paused?e.destroy(new Error("torrent is paused")):(this._debug("add incoming peer %s",e.id),void this._registerPeer(e))}_registerPeer(e){e.on("download",(e=>{this.destroyed||(this.received+=e,this._downloadSpeed(e),this.client._downloadSpeed(e),this.emit("download",e),this.destroyed||this.client.emit("download",e))})),e.on("upload",(e=>{this.destroyed||(this.uploaded+=e,this._uploadSpeed(e),this.client._uploadSpeed(e),this.emit("upload",e),this.destroyed||this.client.emit("upload",e))})),this._peers[e.id]=e,this._peersLength+=1}removePeer(e){const t=e?.id||e;e&&!e.id&&(e=this._peers?.[t]),e&&(e.destroy(),this.destroyed||(this._debug("removePeer %s",t),delete this._peers[t],this._peersLength-=1,this._drain()))}_select(e,t,s,i,n=!1){if(this.destroyed)throw new Error("torrent is destroyed");if(e<0||tt.priority-e.priority)),this._updateSelections()}select(e,t,s,i){this._select(e,t,s,i,!1)}_deselect(e,t,s=!1){if(this.destroyed)throw new Error("torrent is destroyed");this._debug("deselect %s-%s",e,t),this._selections.remove({from:e,to:t,isStreamSelection:s}),this._updateSelections()}deselect(e,t){this._deselect(e,t,!1)}critical(e,t){if(this.destroyed)throw new Error("torrent is destroyed");this._debug("critical %s-%s",e,t);for(let s=e;s<=t;++s)this._critical[s]=!0;this._updateSelections()}_onWire(e,t){if(this._debug("got wire %s (%s)",e._debugId,t||"Unknown"),this.wires.push(e),t){const s=de(t);e.remoteAddress=s[0],e.remotePort=s[1]}this.client.dht&&this.client.dht.listening&&e.on("port",(s=>{if(!this.destroyed&&!this.client.dht.destroyed){if(!e.remoteAddress)return this._debug("ignoring PORT from peer with no address");if(0===s||s>65536)return this._debug("ignoring invalid PORT from peer");this._debug("port: %s (from %s)",s,t),this.client.dht.addNode({host:e.remoteAddress,port:s})}})),e.on("timeout",(()=>{this._debug("wire timeout (%s)",t),e.destroy()})),"webSeed"!==e.type&&e.setTimeout(3e4,!0),e.setKeepAlive(!0),e.use(St(this.metadata)),e.ut_metadata.on("warning",(e=>{this._debug("ut_metadata warning: %s",e.message)})),this.metadata||(e.ut_metadata.on("metadata",(e=>{this._debug("got metadata via ut_metadata"),this._onMetadata(e)})),e.ut_metadata.fetch()),this.client.utPex&&"function"==typeof Et&&!this.private&&(e.use(Et()),e.ut_pex.on("peer",(e=>{this.done||(this._debug("ut_pex: got peer: %s (from %s)",e,t),this.addPeer(e,It.default.SOURCE_UT_PEX))})),e.ut_pex.on("dropped",(e=>{const s=this._peers[e];s&&!s.connected&&(this._debug("ut_pex: dropped peer: %s (from %s)",e,t),this.removePeer(e))})),e.once("close",(()=>{e.ut_pex.reset()}))),e.use(_t()),this.emit("wire",e,t),this.ready&&w((()=>{this._onWireWithMetadata(e)}))}_onWireWithMetadata(e){let t=null;const s=()=>{this.destroyed||e.destroyed||(this._numQueued>2*(this._numConns-this.numPeers)&&e.amInterested?e.destroy():(t=setTimeout(s,zt),t.unref&&t.unref()))};let i;const n=()=>{if(e.peerPieces.buffer.length===this.bitfield.buffer.length){for(i=0;i{n(),this._update(),this._updateWireInterest(e)})),e.on("have",(()=>{n(),this._update(),this._updateWireInterest(e)})),e.lt_donthave.on("donthave",(()=>{n(),this._update(),this._updateWireInterest(e)})),e.on("have-all",(()=>{e.isSeeder=!0,this.alwaysChokeSeeders&&e.choke(),this._update(),this._updateWireInterest(e)})),e.on("have-none",(()=>{e.isSeeder=!1,this._update(),this._updateWireInterest(e)})),e.on("allowed-fast",(e=>{this._update()})),e.once("interested",(()=>{e.unchoke()})),e.once("close",(()=>{clearTimeout(t)})),e.on("choke",(()=>{clearTimeout(t),t=setTimeout(s,zt),t.unref&&t.unref()})),e.on("unchoke",(()=>{clearTimeout(t),this._update()})),e.on("request",((t,s,i,n)=>{if(i>131072)return e.destroy();this.pieces[t]||this.store.get(t,{offset:s,length:i},n)})),e.hasFast&&this._hasAllPieces()?e.haveAll():e.hasFast&&this._hasNoPieces()?e.haveNone():e.bitfield(this.bitfield),this._updateWireInterest(e),e.peerExtensions.dht&&this.client.dht&&this.client.dht.listening&&e.port(this.client.dht.address().port),"webSeed"!==e.type&&(t=setTimeout(s,zt),t.unref&&t.unref()),e.isSeeder=!1,n()}_updateSelections(){this.ready&&!this.destroyed&&(w((()=>{this._gcSelections()})),this._updateInterest(),this._update())}_gcSelections(){for(const e of this._selections){const t=e.offset;for(;this.bitfield.get(e.from+e.offset)&&e.from+e.offsetthis._updateWireInterest(e))),e!==this._amInterested&&(this._amInterested?this.emit("interested"):this.emit("uninterested"))}_updateWireInterest(e){let t=!1;for(let s=0;sthis._updateWireWrapper()),{timeout:250}):this._updateWireWrapper()}_updateWireWrapper(){if(this.destroyed)return;const e=vt(this.wires);let t;for(;t=e();)this._updateWire(t)}_updateWire(e){if(e.destroyed)return!1;const t=this,s=Zt(e,.5);if(e.requests.length>=s)return;const i=Zt(e,1);if(e.peerChoking)e.hasFast&&e.peerAllowedFastSet.length>0&&!this._hasMorePieces(e.peerAllowedFastSet.length-1)&&function(){if(e.requests.length>=i)return!1;for(const s of e.peerAllowedFastSet){if(e.peerPieces.get(s)&&!t.bitfield.get(s))for(;t._request(e,s,!1)&&e.requests.length=i.from+i.offset;--r)if(e.peerPieces.get(r)&&t._request(e,r,!1))return}}();o(!1)||o(!0)}function n(t,s,i,n){return r=>r>=t&&r<=s&&!(r in i)&&e.peerPieces.get(r)&&(!n||n(r))}function r(e){let s=e;for(let i=e;i=i)return!0;const o=function(){const s=e.downloadSpeed()||1;if(s>Vt)return()=>!0;const i=Math.max(1,e.requests.length)*bt.BLOCK_LENGTH/s;let n=10,r=0;return e=>{if(!n||t.bitfield.get(e))return!0;let o=t.pieces[e].missing;for(;r0))return n--,!1}return!0}}();for(let a=0;a({wire:e,random:Math.random()}))).sort(((e,t)=>{const s=e.wire,i=t.wire;return s.downloadSpeed()!==i.downloadSpeed()?s.downloadSpeed()-i.downloadSpeed():s.uploadSpeed()!==i.uploadSpeed()?s.uploadSpeed()-i.uploadSpeed():s.amChoking!==i.amChoking?s.amChoking?-1:1:e.random-t.random})).map((e=>e.wire));this._rechokeOptimisticTime<=0?this._rechokeOptimisticWire=null:this._rechokeOptimisticTime-=1;let t=0;for(;e.length>0&&t0){const t=e.filter((e=>e.peerInterested));if(t.length>0){const e=t[(s=t.length,Math.random()*s|0)];e.unchoke(),this._rechokeOptimisticWire=e,this._rechokeOptimisticTime=2}}var s;e.filter((e=>e!==this._rechokeOptimisticWire)).forEach((e=>e.choke()))}_hotswap(e,t){const s=e.downloadSpeed();if(s=Vt||(2*a>s||a>o||(n=t,o=a))}if(!n)return!1;for(r=0;r=o)return!1;const a=i.pieces[t];let h=r?a.reserveRemaining():a.reserve();if(-1===h&&s&&i._hotswap(e,t)&&(h=r?a.reserveRemaining():a.reserve()),-1===h)return!1;let l=i._reservations[t];l||(l=i._reservations[t]=[]);let c=l.indexOf(null);-1===c&&(c=l.length),l[c]=e;const d=a.chunkOffset(h),u=r?a.chunkLengthRemaining(h):a.chunkLength(h);function p(){w((()=>{i._update()}))}return e.request(t,d,u,(async function s(n,o){if(i.destroyed)return;if(!i.ready)return i.once("ready",(()=>{s(n,o)}));if(l[c]===e&&(l[c]=null),a!==i.pieces[t])return p();if(n)return i._debug("error getting piece %s (offset: %s length: %s) from %s: %s",t,d,u,`${e.remoteAddress}:${e.remotePort}`,n.message),r?a.cancelRemaining(h):a.cancel(h),void p();if(i._debug("got piece %s (offset: %s length: %s) from %s",t,d,u,`${e.remoteAddress}:${e.remotePort}`),!a.set(h,o,e))return p();const f=a.flush(),m=await(0,b.tW)(f,"hex");i.destroyed||(m===i._hashes[t]?(i._debug("piece verified %s",t),i.store.put(t,f,(e=>{e?i._destroy(e):(i.pieces[t]=null,i._markVerified(t),i.wires.forEach((e=>{e.have(t)})),i._checkDone()&&!i.destroyed&&i.discovery.complete(),p())}))):(i.pieces[t]=new bt(a.length),i.emit("warning",new Error(`Piece ${t} failed verification`)),p()))})),!0}_checkDone(){if(this.destroyed)return;this.files.forEach((e=>{if(!e.done){for(let t=e._startPiece;t<=e._endPiece;++t)if(!this.bitfield.get(t))return;e.done=!0,e.emit("done"),this._debug(`file done: ${e.name}`)}}));let e=!0;for(const t of this._selections){for(let s=t.from;s<=t.to;s++)if(!this.bitfield.get(s)){e=!1;break}if(!e)break}return!this.done&&e?(this.done=!0,this._debug(`torrent done: ${this.infoHash}`),this.emit("done")):this.done=!1,this._gcSelections(),e}async load(e,t){if(this.destroyed)throw new Error("torrent is destroyed");if(!this.ready)return this.once("ready",(()=>{this.load(e,t)}));Array.isArray(e)||(e=[e]),t||(t=es);try{await me(this.store,_(e),{chunkLength:this.pieceLength}),this._markAllVerified(),this._checkDone(),t(null)}catch(e){return t(e),e}}pause(){this.destroyed||(this._debug("pause"),this.paused=!0)}resume(){this.destroyed||(this._debug("resume"),this.paused=!1,this._drain())}_debug(){const e=[].slice.call(arguments);e[0]=`[${this.client?this.client._debugId:"No Client"}] [${this._debugId}] ${e[0]}`,$t(...e)}_drain(){if(this._debug("_drain numConns %s maxConns %s",this._numConns,this.client.maxConns),"function"!=typeof ae.connect||this.destroyed||this.paused||this._numConns>=this.client.maxConns)return;this._debug("drain (%s queued, %s/%s peers)",this._numQueued,this.numPeers,this.client.maxConns);const e=this._queue.shift();if(!e)return;this._debug("%s connect attempt to %s",e.type,e.addr);const t=de(e.addr),s={host:t[0],port:t[1]};this.client.utp&&e.type===It.default.TYPE_UTP_OUTGOING?e.conn=Rt.connect(s.port,s.host):e.conn=ae.connect(s);const i=e.conn;i.once("connect",(()=>{this.destroyed||e.onConnect()})),i.once("error",(t=>{e.destroy(t)})),e.startConnectTimeout(),i.on("close",(()=>{if(this.destroyed)return;if(e.retries>=Kt.length){if(this.client.utp){const t=this._addPeer(e.addr,"tcp",e.source);t&&(t.retries=0)}else this._debug("conn %s closed: will not re-add (max %s attempts)",e.addr,Kt.length);return}const t=Kt[e.retries];this._debug("conn %s closed: will re-add to queue in %sms (attempt %s)",e.addr,t,e.retries+1);const s=setTimeout((()=>{if(this.destroyed)return;const t=de(e.addr)[0],s=this.client.utp&&this._isIPv4(t)?"utp":"tcp",i=this._addPeer(e.addr,s,e.source);i&&(i.retries=e.retries+1)}),t);s.unref&&s.unref()}))}_validAddr(e){let t;try{t=de(e)}catch(e){return!1}const s=t[0],i=t[1];return i>0&&i<65535&&!("127.0.0.1"===s&&i===this.client.torrentPort)}_isIPv4(e){return/^((?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])[.]){3}(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])$/.test(e)}}function Zt(e,t){let s=2+Math.ceil(t*e.downloadSpeed()/bt.BLOCK_LENGTH);if(e.peerExtendedHandshake){const t=e.peerExtendedHandshake.reqq;"number"==typeof t&&t>0&&(s=Math.min(s,t))}return s}function es(){}var ts=n(4043),ss=n(580),is=n(815),ns=n(2878);class rs{constructor(e,t={}){this.client=e,t.origin||(t.origin="*"),this.opts=t,this.pendingReady=new Set}static serveIndexPage(e,t,s){const i=t.map((e=>`
  • \n \n ${ss(e.name)}\n \n (${ss(e.length)} bytes)\n
  • `)).join("
    ");return e.status=200,e.headers["Content-Type"]="text/html",e.body=hs("WebTorrent",`

    WebTorrent

    \n
      ${i}
    `),e}isOriginAllowed(e){return!1!==this.opts.origin&&("*"===this.opts.origin||e.headers.origin===this.opts.origin)}static serveMethodNotAllowed(e){return e.status=405,e.headers["Content-Type"]="text/html",e.body=hs("405 - Method Not Allowed","

    405 - Method Not Allowed

    "),e}static serve404Page(e){return e.status=404,e.headers["Content-Type"]="text/html",e.body=hs("404 - Not Found","

    404 - Not Found

    "),e}static serveTorrentPage(e,t,s){const i=e.files.map((t=>`
  • \n \n ${ss(t.path)}\n \n (${ss(t.length)} bytes)\n
  • `)).join("
    ");return t.status=200,t.headers["Content-Type"]="text/html",t.body=hs(`${ss(e.name)} - WebTorrent`,`

    ${ss(e.name)}

    \n
      ${i}
    `),t}static serveOptionsRequest(e,t){return t.status=204,t.headers["Access-Control-Max-Age"]="600",t.headers["Access-Control-Allow-Methods"]="GET,HEAD",e.headers["access-control-request-headers"]&&(t.headers["Access-Control-Allow-Headers"]=e.headers["access-control-request-headers"]),t}static serveFile(e,t,s){s.status=200,s.headers.Expires="0",s.headers["Cache-Control"]="no-cache, no-store, must-revalidate, max-age=0",s.headers["Accept-Ranges"]="bytes",s.headers["Content-Type"]=e.type,s.headers["transferMode.dlna.org"]="Streaming",s.headers["contentFeatures.dlna.org"]="DLNA.ORG_OP=01;DLNA.ORG_CI=0;DLNA.ORG_FLAGS=01700000000000000000000000000000","document"===t.destination?(s.headers["Content-Type"]="application/octet-stream",s.headers["Content-Disposition"]=`attachment; filename*=UTF-8''${ls(e.name)}`,s.body="DOWNLOAD"):s.headers["Content-Disposition"]=`inline; filename*=UTF-8''${ls(e.name)}`;let i=ns(e.length,t.headers.range||"");if(Array.isArray(i)?(s.status=206,i=i[0],s.headers["Content-Range"]=`bytes ${i.start}-${i.end}/${e.length}`,s.headers["Content-Length"]=i.end-i.start+1):(s.statusCode=200,i=null,s.headers["Content-Length"]=e.length),"GET"===t.method){const n=e[Symbol.asyncIterator](i);let r=null;e.emit("iterator",{iterator:n,req:t,file:e},(e=>{r=e}));const o=Y.Readable.from(r||n);let a=null;e.emit("stream",{stream:o,req:t,file:e},(e=>{a=is(o,e)})),s.body=a||o}else s.body=!1;return s}async onRequest(e,t){let s=new URL(e.url,"http://example.com").pathname;s=s.slice(s.indexOf(this.pathname)+this.pathname.length+1);const i={headers:{"X-Content-Type-Options":"nosniff","Content-Security-Policy":"base-uri 'none'; frame-ancestors 'none'; form-action 'none';"}};if(this.isOriginAllowed(e)&&(i.headers["Access-Control-Allow-Origin"]="*"===this.opts.origin?"*":e.headers.origin),"favicon.ico"===s)return t(rs.serve404Page(i));if("OPTIONS"===e.method)return this.isOriginAllowed(e)?t(rs.serveOptionsRequest(e,i)):t(rs.serveMethodNotAllowed(i));const n=async()=>{this.pendingReady.delete(n);const e=await r();t(e)},r=async()=>{if(""===s)return rs.serveIndexPage(i,this.client.torrents,this.pathname);let[t,...n]=s.split("/");n=decodeURI(n.join("/"));const r=await this.client.get(t);if(!t||!r)return rs.serve404Page(i);if(!n)return rs.serveTorrentPage(r,i,this.pathname);const o=r.files.find((e=>e.path.replace(/\\/g,"/")===n));return o?rs.serveFile(o,e,i):rs.serve404Page(i)};if("GET"===e.method||"HEAD"===e.method){if(this.client.ready){const e=await r();return t(e)}return this.pendingReady.add(n),void this.client.once("ready",n)}return t(rs.serveMethodNotAllowed(i))}close(e=(()=>{})){this.closed=!0,this.pendingReady.forEach((e=>{this.client.removeListener("ready",e)})),this.pendingReady.clear(),w(e)}destroy(e=(()=>{})){this.closed?w(e):this.close(e),this.client=null}}class os extends rs{constructor(e,t){super(e,t),this.server=ts.createServer(),this._listen=this.server.listen,this.server.listen=this.listen.bind(this),this._close=this.server.close,this.server.close=this.close.bind(this),this.sockets=new Set,this.closed=!1,this.pathname=t?.pathname||"/webtorrent"}wrapRequest(e,t){return this.opts.hostname&&e.headers.host!==`${this.opts.hostname}:${this.server.address().port}`?e.destroy():new URL(e.url,"http://example.com").pathname.startsWith(this.pathname)?void this.onRequest(e,(({status:e,headers:s,body:i})=>{t.writeHead(e,s),i._readableState||i._writableState?is(i,t):t.end(i)})):e.destroy()}onConnection(e){e.setTimeout(36e6),this.sockets.add(e),e.once("close",(()=>{this.sockets.delete(e)}))}address(){return this.server.address()}listen(...e){return this.closed=!1,this.server.on("connection",this.onConnection.bind(this)),this.server.on("request",this.wrapRequest.bind(this)),this._listen.apply(this.server,e)}close(e=(()=>{})){this.server.removeAllListeners("connection"),this.server.removeAllListeners("request"),this.server.removeAllListeners("listening"),super.close(),this._close.call(this.server,e)}destroy(e){this.sockets.forEach((e=>{e.destroy()})),super.destroy(e)}}class as extends rs{constructor(e,t){super(e,t),this.registration=t.controller,this.workerKeepAliveInterval=null,this.workerPortCount=0;const s=new URL(t.controller.scope);this.pathname=s.pathname+"webtorrent",this._address={port:s.port,family:"IPv4",address:s.hostname},this.boundHandler=this.wrapRequest.bind(this),navigator.serviceWorker.addEventListener("message",this.boundHandler),fetch(`${this.pathname}/cancel/`).then((e=>{e.body.cancel()}))}wrapRequest(e){const t=e.data;if("webtorrent"===!t?.type||!t.url)return null;const[s]=e.ports;this.onRequest(t,(({status:e,headers:t,body:i})=>{const n=i[Symbol.asyncIterator]?.(),r=()=>{s.onmessage=null,i?.destroy&&i.destroy(),this.workerPortCount--,this.workerPortCount||(clearInterval(this.workerKeepAliveInterval),this.workerKeepAliveInterval=null)};s.onmessage=async e=>{if(e.data){let e;try{e=(await n.next()).value}catch(e){}s.postMessage(e),e||r(),this.workerKeepAliveInterval||(this.workerKeepAliveInterval=setInterval((()=>fetch(`${this.pathname}/keepalive/`)),2e4))}else r()},this.workerPortCount++,s.postMessage({status:e,headers:t,body:n?"STREAM":i})}))}listen(e,t){t()}address(){return this._address}close(e){navigator.serviceWorker.removeEventListener("message",this.boundHandler),super.close(e)}destroy(e){super.destroy(e)}}function hs(e,t){return`\n \n \n \n \n ${e}\n \n \n ${t}\n \n \n `}function ls(e){return encodeURIComponent(e).replace(/['()]/g,escape).replace(/\*/g,"%2A").replace(/%(?:7C|60|5E)/g,unescape)}const cs=I("webtorrent"),ds=Ht.replace(/\d*./g,(e=>("0"+e%100).slice(-2))).slice(0,4),us=`-WW${ds}-`;class ps extends o{constructor(e={}){super(),"string"==typeof e.peerId?this.peerId=e.peerId:ArrayBuffer.isView(e.peerId)?this.peerId=(0,b.V5)(e.peerId):this.peerId=(0,b.V5)((0,b.L0)(us+(0,b.NJ)((0,b.po)(9)))),this.peerIdBuffer=(0,b.fk)(this.peerId),"string"==typeof e.nodeId?this.nodeId=e.nodeId:ArrayBuffer.isView(e.nodeId)?this.nodeId=(0,b.V5)(e.nodeId):this.nodeId=(0,b.V5)((0,b.po)(20)),this.nodeIdBuffer=(0,b.fk)(this.nodeId),this._debugId=this.peerId.substring(0,7),this.destroyed=!1,this.listening=!1,this.torrentPort=e.torrentPort||0,this.dhtPort=e.dhtPort||0,this.tracker=void 0!==e.tracker?e.tracker:{},this.lsd=!1!==e.lsd,this.utPex=!1!==e.utPex,this.natUpnp=e.natUpnp??!0,this.natPmp=e.natPmp??!0,this.torrents=[],this.maxConns=Number(e.maxConns)||55,this.utp=ps.UTP_SUPPORT&&!1!==e.utp,this.seedOutgoingConnections=e.seedOutgoingConnections??!0,this._downloadLimit=Math.max("number"==typeof e.downloadLimit?e.downloadLimit:-1,-1),this._uploadLimit=Math.max("number"==typeof e.uploadLimit?e.uploadLimit:-1,-1),(this.natUpnp||this.natPmp)&&"function"==typeof ne&&(this.natTraversal=new ne({enableUPNP:this.natUpnp,enablePMP:this.natPmp,upnpPermanentFallback:"permanent"===e.natUpnp})),!0===e.secure&&Promise.resolve().then(n.bind(n,5658)).then((({enableSecure:e})=>e())),this._debug("new webtorrent (peerId %s, nodeId %s, port %s)",this.peerId,this.nodeId,this.torrentPort),this.throttleGroups={down:new ie.ThrottleGroup({rate:Math.max(this._downloadLimit,0),enabled:this._downloadLimit>=0}),up:new ie.ThrottleGroup({rate:Math.max(this._uploadLimit,0),enabled:this._uploadLimit>=0})},this.tracker&&("object"!=typeof this.tracker&&(this.tracker={}),globalThis.WRTC&&!this.tracker.wrtc&&(this.tracker.wrtc=globalThis.WRTC)),"function"==typeof re?this._connPool=new re(this):w((()=>{this._onListening()})),this._downloadSpeed=se(),this._uploadSpeed=se(),!1!==e.dht&&"function"==typeof L.Client?(this.dht=new L.Client(Object.assign({},{nodeId:this.nodeId},e.dht)),this.dht.once("error",(e=>{this._destroy(e)})),this.dht.once("listening",(()=>{const e=this.dht.address();e&&(this.dhtPort=e.port,this.natTraversal&&this.natTraversal.map({publicPort:this.dhtPort,privatePort:this.dhtPort,protocol:"udp",description:"WebTorrent DHT"}).catch((e=>{cs("error mapping DHT port via UPnP/PMP: %o",e)})))})),this.dht.setMaxListeners(0),this.dht.listen(this.dhtPort)):this.dht=!1,this.enableWebSeeds=!1!==e.webSeeds;const t=()=>{this.destroyed||(this.ready=!0,this.emit("ready"))};"function"==typeof R&&null!=e.blocklist?R(e.blocklist,{headers:{"user-agent":`WebTorrent/${Ht} (https://webtorrent.io)`}},((e,s)=>{if(e)return console.error(`Failed to load blocklist: ${e.message}`);this.blocked=s,t()})):w(t)}createServer(e,t){if(this.destroyed)throw new Error("torrent is destroyed");if(this._server)throw new Error("server already created");if("undefined"!=typeof window&&"node"!==t||"browser"===t){if(!(e?.controller instanceof ServiceWorkerRegistration))throw new Error("Invalid worker registration");if("activated"!==e.controller.active.state)throw new Error("Worker isn't activated");return this._server=new as(this,e),this._server}return this._server=new os(this,e),this._server}get downloadSpeed(){return this._downloadSpeed()}get uploadSpeed(){return this._uploadSpeed()}get progress(){const e=this.torrents.filter((e=>1!==e.progress));return e.reduce(((e,t)=>e+t.downloaded),0)/(e.reduce(((e,t)=>e+(t.length||0)),0)||1)}get ratio(){return this.torrents.reduce(((e,t)=>e+t.uploaded),0)/(this.torrents.reduce(((e,t)=>e+t.received),0)||1)}async get(e){if(e instanceof Qt){if(this.torrents.includes(e))return e}else{const t=this.torrents;let s;try{s=await W(e)}catch(e){}if(!s)return null;if(!s.infoHash)throw new Error("Invalid torrent identifier");for(const e of t)if(e.infoHash===s.infoHash)return e}return null}add(e,t={},s=(()=>{})){if(this.destroyed)throw new Error("client is destroyed");"function"==typeof t&&([t,s]=[{},t]);const i=()=>{if(!this.destroyed)for(const e of this.torrents)if(e.infoHash===r.infoHash&&e!==r)return r._destroy(new Error(`Cannot add duplicate torrent ${r.infoHash}`)),void s(e)},n=()=>{this.destroyed||(s(r),this.emit("torrent",r))};this._debug("add"),t=t?Object.assign({},t):{};const r=new Qt(e,this,t);return this.torrents.push(r),r.once("_infoHash",i),r.once("ready",n),r.once("close",(function e(){r.removeListener("_infoHash",i),r.removeListener("ready",n),r.removeListener("close",e)})),this.emit("add",r),r}seed(e,t,s){if(this.destroyed)throw new Error("client is destroyed");"function"==typeof t&&([t,s]=[{},t]),this._debug("seed"),(t=t?Object.assign({},t):{}).skipVerify=!0;const i="string"==typeof e;i&&(t.path=function(e){if(a(e),0===e.length)return".";let t=e.charCodeAt(0);const s=47===t;let i=-1,n=!0;for(let s=e.length-1;s>=1;--s)if(t=e.charCodeAt(s),47===t){if(!n){i=s;break}}else n=!1;return-1===i?s?"/":".":s&&1===i?"//":e.slice(0,i)}(e)),t.createdBy||(t.createdBy=`WebTorrent/${ds}`);const n=e=>{this._debug("on seed"),"function"==typeof s&&s(e),e.emit("seed"),this.emit("seed",e)},r=this.add(null,t,(e=>{const s=[s=>{if(i||t.preloadedStore)return s();e.load(o,s)}];this.dht&&s.push((t=>{e.once("dhtAnnounce",t)})),y(s,(t=>{if(!this.destroyed)return t?e._destroy(t):void n(e)}))}));let o;var h;return h=e,"undefined"!=typeof FileList&&h instanceof FileList?e=Array.from(e):Array.isArray(e)||(e=[e]),y(e.map((e=>async s=>{if(!t.preloadedStore&&function(e){return"object"==typeof e&&null!=e&&"function"==typeof e.pipe}(e)){const t=[];try{for await(const s of e)t.push(s)}catch(e){return s(e)}const i=(0,b.xW)(t);i.name=e.name,s(null,i)}else s(null,e)})),((e,i)=>{if(!this.destroyed)return e?r._destroy(e):void function(e,t,s){"function"==typeof t&&([t,s]=[s,t]),S(e,t=t?Object.assign({},t):{},s)}(i,t,((e,n)=>{if(!this.destroyed){if(e)return r._destroy(e);o=n.map((e=>e.getStream)),T(i,t,(async(e,t)=>{if(this.destroyed)return;if(e)return r._destroy(e);const i=await this.get(t);i?(console.warn("A torrent with the same id is already being seeded"),r._destroy(),"function"==typeof s&&s(i)):r._onTorrentId(t)}))}}))})),r}async remove(e,t,s){if("function"==typeof t)return this.remove(e,null,t);this._debug("remove");const i=await this.get(e);if(!i)throw new Error(`No torrent with id ${e}`);this._remove(i,t,s)}_remove(e,t,s){if(!e)return;if("function"==typeof t)return this._remove(e,null,t);const i=this.torrents.indexOf(e);-1!==i&&(this.torrents.splice(i,1),e.destroy(t,s),this.dht&&this.dht._tables.remove(e.infoHash),this.emit("remove",e))}address(){return this.listening?this._connPool?this._connPool.tcpServer.address():{address:"0.0.0.0",family:"IPv4",port:0}:null}throttleDownload(e){return e=Number(e),!(isNaN(e)||!isFinite(e)||e<-1)&&(this._downloadLimit=e,this._downloadLimit<0?this.throttleGroups.down.setEnabled(!1):(this.throttleGroups.down.setEnabled(!0),void this.throttleGroups.down.setRate(this._downloadLimit)))}throttleUpload(e){return e=Number(e),!(isNaN(e)||!isFinite(e)||e<-1)&&(this._uploadLimit=e,this._uploadLimit<0?this.throttleGroups.up.setEnabled(!1):(this.throttleGroups.up.setEnabled(!0),void this.throttleGroups.up.setRate(this._uploadLimit)))}destroy(e){if(this.destroyed)throw new Error("client already destroyed");this._destroy(null,e)}_destroy(e,t){this._debug("client destroy"),this.destroyed=!0;const s=this.torrents.map((e=>t=>{e.destroy(t)}));this._connPool&&s.push((e=>{this._connPool.destroy(e)})),this.dht&&s.push((e=>{this.dht.destroy(e)})),this._server&&s.push((e=>{this._server.destroy(e)})),this.natTraversal&&s.push((e=>{this.natTraversal.destroy().then((()=>e()))})),y(s,t),e&&this.emit("error",e),this.torrents=[],this._connPool=null,this.dht=null,this.throttleGroups.down.destroy(),this.throttleGroups.up.destroy()}_onListening(){if(this._debug("listening"),this.listening=!0,this._connPool){const e=this._connPool.tcpServer.address();e&&(this.torrentPort=e.port,this.natTraversal&&this.natTraversal.map({publicPort:this.torrentPort,privatePort:this.torrentPort,protocol:this.utp?null:"tcp",description:"WebTorrent Torrent"}).catch((e=>{cs("error mapping WebTorrent port via UPnP/PMP: %o",e)})))}this.emit("listening")}_debug(){const e=[].slice.call(arguments);e[0]=`[${this._debugId}] ${e[0]}`,cs(...e)}async _getByHash(e){for(const t of this.torrents)if(t.infoHashHash||(t.infoHashHash=await(0,b.tW)((0,b.fk)("72657132"+t.infoHash),"hex")),e===t.infoHashHash)return t;return null}}ps.WEBRTC_SUPPORT=te.WEBRTC_SUPPORT,ps.UTP_SUPPORT=re.UTP_SUPPORT,ps.VERSION=Ht;var fs=r.A;export{fs as default}; +//# sourceMappingURL=webtorrent.min.js.map diff --git a/site.hs b/site.hs index b8ad4b3..8284c2f 100644 --- a/site.hs +++ b/site.hs @@ -1,7 +1,7 @@ -------------------------------------------------------------------------------- {-# LANGUAGE OverloadedStrings #-} import Data.Monoid (mappend) -import System.FilePath ((), (<.>), splitExtension, splitFileName, takeDirectory) +import System.FilePath ((), (<.>), splitExtension, splitFileName, takeDirectory, stripExtension, takeFileName, takeBaseName) import Hakyll @@ -20,6 +20,13 @@ main = hakyll $ do route idRoute compile copyFileCompiler + match "scripts/*" $ do + route idRoute + compile copyFileCompiler + + match "sw.min.js" $ do + route idRoute + compile copyFileCompiler match "style.css" $ do route idRoute @@ -128,6 +135,8 @@ postCtx :: Context String postCtx = constField "root" root `mappend` dateField "date" "%Y-%m-%d" `mappend` + idFieldFromPath "id" `mappend` + teaserField "teaser" "article-text" `mappend` dropIndexHtml "url" `mappend` defaultContext @@ -138,6 +147,11 @@ appendIndex :: Routes appendIndex = customRoute $ (\(p, e) -> p "index" <.> e) . splitExtension . toFilePath + +idFieldFromPath :: String -> Context a +idFieldFromPath key = mapContext transform (pathField key) where + transform path = takeBaseName path + dropIndexHtml :: String -> Context a dropIndexHtml key = mapContext transform (urlField key) where transform url = case splitFileName url of diff --git a/style.css b/style.css index 342071c..9a83e04 100644 --- a/style.css +++ b/style.css @@ -193,6 +193,7 @@ nav ul { article { display: flex; + align-items: center; flex-direction: column; /* align-items: center; */ gap: 2vw; @@ -206,6 +207,9 @@ article { .thumbnail { filter: drop-shadow(0px 0px 4px #e3b69b); border: 4px groove #800080; + background-color: #06360f; + /* aspect-ratio: 16 / 10; */ + width: auto; padding: 0vw; } @@ -670,4 +674,4 @@ code span.wa { font-weight: bold; font-style: normal; font-display: block; -} \ No newline at end of file +} diff --git a/sw.min.js b/sw.min.js new file mode 100644 index 0000000..1ccc2af --- /dev/null +++ b/sw.min.js @@ -0,0 +1,2 @@ +(()=>{"use strict";let e=!1;self.addEventListener("install",(()=>{self.skipWaiting()})),self.addEventListener("fetch",(s=>{const t=(s=>{const{url:t}=s.request;return t.includes(self.registration.scope+"webtorrent/")?t.includes(self.registration.scope+"webtorrent/keepalive/")?new Response:t.includes(self.registration.scope+"webtorrent/cancel/")?new Response(new ReadableStream({cancel(){e=!0}})):async function({request:s}){const{url:t,method:n,headers:o,destination:a}=s,l=await clients.matchAll({type:"window",includeUncontrolled:!0}),[r,i]=await new Promise((e=>{for(const s of l){const l=new MessageChannel,{port1:r,port2:i}=l;r.onmessage=({data:s})=>{e([s,r])},s.postMessage({url:t,method:n,headers:Object.fromEntries(o.entries()),scope:self.registration.scope,destination:a,type:"webtorrent"},[i])}}));let c=null;const d=()=>{i.postMessage(!1),clearTimeout(c),i.onmessage=null};return"STREAM"!==r.body?(d(),new Response(r.body,r)):new Response(new ReadableStream({pull:s=>new Promise((t=>{i.onmessage=({data:e})=>{e?s.enqueue(e):(d(),s.close()),t()},e||(clearTimeout(c),"document"!==a&&(c=setTimeout((()=>{d(),t()}),5e3))),i.postMessage(!0)})),cancel(){d()}}),r)}(s):null})(s);t&&s.respondWith(t)})),self.addEventListener("activate",(()=>{self.clients.claim()}))})(); +//# sourceMappingURL=sw.min.js.map diff --git a/templates/article.html b/templates/article.html new file mode 100644 index 0000000..e5ed720 --- /dev/null +++ b/templates/article.html @@ -0,0 +1,44 @@ +
    + $if(thumbnail)$ +
    $thumbnail$
    + $endif$ + $if(torrent)$ +
    + + +
    + $endif$ +
    +
    +
    +

    $title$

    + permalink + raw + $if(torrent)$ + magnet link + $endif$ +
    +
    + $if(author)$ +

    $author$

    + $endif$ +

    $date$

    + $if(tags)$ +

    $tags$

    + $endif$ +
    +
    +
    +
    +
    + $body$ +
    +
    +
    diff --git a/templates/base.html b/templates/base.html index 321ab9d..1875345 100644 --- a/templates/base.html +++ b/templates/base.html @@ -38,4 +38,4 @@ - + \ No newline at end of file diff --git a/templates/default.html b/templates/default.html deleted file mode 100644 index e91badc..0000000 --- a/templates/default.html +++ /dev/null @@ -1,33 +0,0 @@ - - - - - - - My Hakyll Blog - $title$ - - - -
    - - -
    - -
    -

    $title$

    - $body$ -
    - -
    - Site proudly generated by - Hakyll -
    - - diff --git a/templates/load-webtorrent.html b/templates/load-webtorrent.html new file mode 100644 index 0000000..0d7347f --- /dev/null +++ b/templates/load-webtorrent.html @@ -0,0 +1,9 @@ + diff --git a/templates/post.html b/templates/post.html index 77986ae..ddbd384 100644 --- a/templates/post.html +++ b/templates/post.html @@ -1,30 +1,4 @@
    -
    - $if(thumbnail)$ -
    $thumbnail$
    - $endif$ -
    -
    -
    -

    $title$

    - permalink - raw -
    -
    - $if(author)$ -

    $author$

    - $endif$ -

    $date$

    - $if(tags)$ -

    $tags$

    - $endif$ -
    -
    -
    -
    -
    - $body$ -
    -
    -
    + $partial("templates/load-webtorrent.html")$ + $partial("templates/article.html")$
    \ No newline at end of file diff --git a/templates/thumbs.html b/templates/thumbs.html index 91cfd74..3fcc6f7 100644 --- a/templates/thumbs.html +++ b/templates/thumbs.html @@ -5,6 +5,17 @@
    $thumbnail$

    $endif$ + $if(torrent)$ + + + $endif$ $endif$ + $if(teaser)$ + + $endif$ $endfor$ - \ No newline at end of file + diff --git a/todo.md b/todo.md index 13203eb..56340e9 100644 --- a/todo.md +++ b/todo.md @@ -8,10 +8,11 @@ [x] flowerfont [x] text-wrap issues [x] sitemap.xml +[x] teasers +[x] webtorrent [ ] remove placeholders later: -[ ] teasers [ ] fun homepage design [ ] author pages [ ] tags filter -- cgit