UNPKG

webtorrent

Version:

Streaming torrent client

1 lines 218 kB
var e,t,s={61:()=>{},405:e=>{e.exports=async function*(e){for(let t of e)"function"==typeof t&&(t=t()),yield*t}},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<i.length;o++){switch(i.charCodeAt(o)){case 34:s="&quot;";break;case 38:s="&amp;";break;case 39:s="&#39;";break;case 60:s="&lt;";break;case 62:s="&gt;";break;default:continue}a!==o&&(r+=i.substring(a,o)),a=o+1,r+=s}return a!==o?r+i.substring(a,o):r}},717:(e,t,s)=>{s.r(t),s.d(t,{addListener:()=>u,argv:()=>a,binding:()=>S,browser:()=>r,chdir:()=>E,cwd:()=>k,emit:()=>g,env:()=>o,listeners:()=>v,nextTick:()=>b,off:()=>f,on:()=>d,once:()=>p,prependListener:()=>y,prependOnceListener:()=>w,removeAllListeners:()=>_,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,_=c,g=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")}},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,t){let s=0,i=0,n=-1,r=0;for(;s<e.length;)if(i<t.length&&(t[i]===e[s]||"*"===t[i]))"*"===t[i]?(n=i,r=s,i++):(s++,i++);else{if(-1===n)return!1;i=n+1,r++,s=r}for(;i<t.length&&"*"===t[i];)i++;return i===t.length}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,...t.skips.map((e=>"-"+e))].join(",");return t.enable(""),e},t.enable=function(e){t.save(e),t.namespaces=e,t.names=[],t.skips=[];const s=("string"==typeof e?e:"").trim().replace(" ",",").split(",").filter(Boolean);for(const e of s)"-"===e[0]?t.skips.push(e.slice(1)):t.names.push(e)},t.enabled=function(e){for(const s of t.skips)if(n(e,s))return!1;for(const s of t.names)if(n(e,s))return!0;return!1},t.humanize=s(6585),t.destroy=function(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")},Object.keys(e).forEach((s=>{t[s]=e[s]})),t.names=[],t.skips=[],t.formatters={},t.selectColor=function(e){let s=0;for(let t=0;t<e.length;t++)s=(s<<5)-s+e.charCodeAt(t),s|=0;return t.colors[Math.abs(s)%t.colors.length]},t.enable(t.load()),t}},815:(e,t,s)=>{var i,n=s(717),r=s(3519),o=s(6611);try{i=s(2376)}catch(e){}var a=function(){},h=/^v?\.0/.test(n.version),l=function(e){return"function"==typeof e},c=function(e,t,s,n){n=r(n);var c=!1;e.on("close",(function(){c=!0})),o(e,{readable:t,writable:s},(function(e){if(e)return n(e);c=!0,n()}));var d=!1;return function(t){if(!c&&!d)return d=!0,function(e){return!!h&&!!i&&(e instanceof(i.ReadStream||a)||e instanceof(i.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 n(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=r<t.length-1;return c(n,o,r>0,(function(t){e||(e=t),t&&i.forEach(d),o||(i.forEach(d),s(e))}))}));return t.reduce(u)}},826:()=>{},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<r;i++)s=n[i],null!=t[s]&&(o.string(e,s),o._encode(e,t[s]));e.push(a)},o.dictMap=function(e,t){e.push(h);const s=Array.from(t.keys()).sort();for(const i of s)null!=t.get(i)&&(ArrayBuffer.isView(i)?o._encode(e,i):o.string(e,String(i)),o._encode(e,t.get(i)));e.push(a)},o.list=function(e,t){let s=0;const i=t.length;for(e.push(l);s<i;s++)null!=t[s]&&o._encode(e,t[s]);e.push(a)},o.listSet=function(e,t){e.push(l);for(const s of t)null!=s&&o._encode(e,s);e.push(a)};const c=o;function d(e,t,s){let i=0,n=1;for(let r=t;r<s;r++){const s=e[r];if(s<58&&s>=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<s;){if(i[t]===e)return t;t++}throw new Error('Invalid data: Missing delimiter "'+String.fromCharCode(e)+'" [0x'+e.toString(16)+"]")},u.dictionary=function(){u.position++;const e={};for(;101!==u.data[u.position];){const t=u.buffer();let s=(0,i.dU)(t);s.includes("�")&&(s=(0,i.V5)(t)),e[s]=u.next()}return u.position++,e},u.list=function(){u.position++;const e=[];for(;101!==u.data[u.position];)e.push(u.next());return u.position++,e},u.integer=function(){const e=u.find(101),t=d(u.data,u.position+1,e);return u.position+=e+1-u.position,t},u.buffer=function(){let e=u.find(58);const t=d(u.data,u.position,e),s=++e+t;return u.position=s,u.encoding?(0,i.dU)(u.data.slice(e,s)):u.data.slice(e,s)};function p(e){if(null==e)return 0;const t=r(e);switch(t){case"arraybufferview":return function(e){const t=e.byteLength-e.byteOffset;return n(t)+1+t}(e);case"string":return function(e){const t=(0,i.L0)(e).byteLength;return n(t)+1+t}(e);case"array":case"set":return function(e){let t=2;for(const s of e)t+=p(s);return t}(e);case"number":return 1+n(Math.floor(e))+1;case"bigint":return 1+e.toString().length+1;case"object":return function(e){let t=2;const s=Object.keys(e);for(let r=0;r<s.length;r++){const o=(0,i.L0)(s[r]).byteLength;t+=n(o)+1+o,t+=p(e[s[r]])}return t}(e);case"map":return function(e){let t=2;for(const[s,r]of e){const e=(0,i.L0)(s).byteLength;t+=n(e)+1+e,t+=p(r)}return t}(e);default:throw new TypeError(`Unsupported value of type "${t}"`)}}const f={encode:c,decode:u,byteLength:p,encodingLength:p}},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<i?0:n[r===i?0:r];return n.length<10?l:10*(l-c)/n.length}}},1133:(e,t,s)=>{let i=s(8673);e.exports=new i(s(8446))},1314:e=>{e.exports=function(e,t){if(t>=e.length||t<0)return;var s=e.pop();if(t<e.length){var i=e[t];return e[t]=s,i}return 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<n;t++){const n=e[t];s.set(n,i),i+=n.byteLength||n.length}return s}e.exports=async function*(e,s=512,i={}){"object"==typeof s&&(s=(i=s).size);let{nopad:n,zeroPadding:r=!0}=i;n&&(r=!1);let o=[],a=0;for await(const i of e)if(a+=i.byteLength||i.length||1,o.push(i),a>=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))}},1551:()=>{},1827:()=>{},2072:e=>{e.exports={wait:function(e){return new Promise((t=>setTimeout(t,e)))}}},2123:()=>{},2376:()=>{},2532:()=>{},2701:()=>{},2799:()=>{},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;c<h.length;c++){var d=h[c].split("-"),u=parseInt(d[0],10),p=parseInt(d[1],10);isNaN(u)?(u=e-p,p=e-1):isNaN(p)&&(p=e-1),p>e-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;a<r.length;a++){var h=r[a],l=r[o];h.start>l.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}},3033:(e,t,s)=>{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<this.buffer.length&&!!(this.buffer[t]&128>>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<this.buffer.length&&(this.buffer[s]&=~(128>>e%8))}setAll(e,t=0){const s=Math.min(i(t+e.length),this.grow);if(this.buffer.length<s){const e=new Uint8Array(s);e.set(this.buffer),this.buffer=e}let n=t>>3,r=128>>t%8;for(let t=0;t<e.length;t++)if(e[t]?this.buffer[n]|=r:this.buffer[n]&=~r,1===r){if(n+=1,n>=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<s;r++)e(!!(this.buffer[i]&n),r),1===n?(i+=1,n=128):n>>=1}isEmpty(){for(let e=0;e<this.buffer.length;e++)if(0!==this.buffer[e])return!1;return!0}}},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.curIntervalStart||n-this.curIntervalStart>=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.curIntervalStart||t-this.curIntervalStart>=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},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()}},3198:e=>{e.exports=class{constructor(e){this.decoder=new TextDecoder("utf16le"===e?"utf16-le":e)}get remaining(){return-1}decode(e){return this.decoder.decode(e,{stream:!0})}flush(){return this.decoder.decode(new Uint8Array(0))}}},3208:()=>{},3278:()=>{},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})}))},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})}))},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)}}},3970:()=>{},4018:e=>{e.exports=function(){for(var e=navigator.hardwareConcurrency||1,t=[],s=0;s<e;s++)t.push({model:"",speed:0,times:{user:0,nice:0,sys:0,idle:0,irq:0}});return t}},4043:()=>{},4343:()=>{},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)}}},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)))}},5335:(e,t,s)=>{const i=s(3198),n=s(3198);e.exports=class{constructor(e="utf8"){switch(this.encoding=function(e){switch(e=e.toLowerCase()){case"utf8":case"utf-8":return"utf8";case"ucs2":case"ucs-2":case"utf16le":case"utf-16le":return"utf16le";case"latin1":case"binary":return"latin1";case"base64":case"ascii":case"hex":return e;default:throw new Error("Unknown encoding: "+e)}}(e),this.encoding){case"utf8":this.decoder=new n;break;case"utf16le":case"base64":throw new Error("Unsupported encoding: "+this.encoding);default:this.decoder=new i(this.encoding)}}get remaining(){return this.decoder.remaining}push(e){return"string"==typeof e?e:this.decoder.decode(e)}write(e){return this.push(e)}end(e){let t="";return e&&(t=this.push(e)),t+=this.decoder.flush(),t}}},5372:(e,t,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<r){let t;a?(t=a[l],l+=1,e[t]((function(e,s){u(t,e,s)}))):(t=l,l+=1,e[t]((function(e,s){u(t,e,s)})))}}l=t,o?a?a.some((function(s,i){return e[s]((function(e,t){u(s,e,t)})),i===t-1})):e.some((function(e,s){return e((function(e,t){u(s,e,t)})),s===t-1})):d(null);c=!1};const i=s(9596)},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},5658:(e,t,s)=>{s.r(t),s.d(t,{default:()=>_,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 _ 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)}}_.TYPE_TCP_INCOMING=h,_.TYPE_TCP_OUTGOING=l,_.TYPE_UTP_INCOMING=c,_.TYPE_UTP_OUTGOING=d,_.TYPE_WEBRTC="webrtc",_.TYPE_WEBSEED=u,_.SOURCE_MANUAL="manual",_.SOURCE_TRACKER="tracker",_.SOURCE_DHT="dht",_.SOURCE_LSD="lsd",_.SOURCE_UT_PEX="ut_pex",_.createWebRTCPeer=(e,t,s,i=null)=>{const n=new _(e.id,"webrtc");if(n.conn=e,n.swarm=t,n.throttleGroups=s,n.source=i,n.conn.connected)n.onConnect();else{const e=()=>{n.conn.removeListener("connect",t),n.conn.removeListener("error",s)},t=()=>{e(),n.onConnect()},s=t=>{e(),n.destroy(t)};n.conn.once("connect",t),n.conn.once("error",s),n.startConnectTimeout()}return n},_.createTCPIncomingPeer=(e,t)=>_._createIncomingPeer(e,h,t),_.createUTPIncomingPeer=(e,t)=>_._createIncomingPeer(e,c,t),_.createTCPOutgoingPeer=(e,t,s,i)=>_._createOutgoingPeer(e,t,l,s,i),_.createUTPOutgoingPeer=(e,t,s,i)=>_._createOutgoingPeer(e,t,d,s,i),_._createIncomingPeer=(e,t,s)=>{const i=`${e.remoteAddress}:${e.remotePort}`,n=new _(i,t);return n.conn=e,n.addr=i,n.throttleGroups=s,n.onConnect(),n},_._createOutgoingPeer=(e,t,s,i,n=null)=>{const r=new _(e,s);return r.addr=e,r.swarm=t,r.throttleGroups=i,r.source=n,r},_.createWebSeedPeer=(e,t,s,i)=>{const n=new _(t,u);return n.swarm=s,n.conn=e,n.throttleGroups=i,n.onConnect(),n}},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}}},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)}}},6458:(e,t,s)=>{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]),_=new Uint8Array([0,0,0,1,1]),g=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(_))}interested(){this.amInterested||(this.amInterested=!0,this._debug("interested"),this._push(g))}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<t.length;e++)r.setUint32(5+4*e,t[e]);this._push(n),s&&this._push(s)}_push(e){if(!this._finished)return 2===this._encryptionMethod&&this._cryptoHandshakeDone&&(e=this._encrypt(e)),this.push(e)}_onKeepAlive(){this._debug("got keep-alive"),this.emit("keep-alive")}_onPe1(e){this._peerPubKey=(0,l.V5)(e),this._sharedSecret=this._dh.computeSecret(this._peerPubKey,"hex","hex"),this.emit("pe1")}_onPe2(e){this._peerPubKey=(0,l.V5)(e),this._sharedSecret=this._dh.computeSecret(this._peerPubKey,"hex","hex"),this.emit("pe2")}async _onPe3(e){const t=await(0,l.V5)(this._utfToHex("req3")+this._sharedSecret),s=(0,l.V5)(C(t,e));this.emit("pe3",s)}_onPe3Encrypted(e,t){if(!(0,l.LC)(e,x))return this._debug("Error: verification constant did not match"),void this.destroy();for(const e of t.values())0!==e&&this._peerCryptoProvide.push(e);this._peerCryptoProvide.includes(2)?this._encryptionMethod=2:(this._debug("Error: RC4 encryption method not provided by peer"),this.destroy())}_onPe4(e){this._encryptionMethod=e[3],S.includes(this._encryptionMethod)||(this._debug("Error: peer selected invalid crypto method"),this.destroy()),this._cryptoHandshakeDone=!0,this._debug("crypto handshake done"),this.emit("pe4")}_onHandshake(e,t,s){const i=(0,l.V5)(e),n=(0,l.V5)(t);this._debug("got handshake i=%s p=%s exts=%o",i,n,s),this.peerId=n,this.peerIdBuffer=t,this.peerExtensions=s,this.extensions.fast&&this.peerExtensions.fast&&(this._debug("fast extension is enabled"),this.hasFast=!0),this.emit("handshake",i,n,s);for(const e in this._ext)this._ext[e].onHandshake(i,n,s);s.extended&&this._handshakeSent&&!this._extendedHandshakeSent&&this._sendExtendedHandshake()}_onChoke(){if(this.peerChoking=!0,this._debug("got choke"),this.emit("choke"),!this.hasFast)for(;this.requests.length;)this._callback(this.requests.pop(),new Error("peer is choking"),null)}_onUnchoke(){this.peerChoking=!1,this._debug("got unchoke"),this.emit("unchoke")}_onInterested(){this.peerInterested=!0,this._debug("got interested"),this.emit("interested")}_onUninterested(){this.peerInterested=!1,this._debug("got uninterested"),this.emit("uninterested")}_onHave(e){this.peerPieces.get(e)||(this._debug("got have %d",e),this.peerPieces.set(e,!0),this.emit("have",e))}_onBitField(e){this.peerPieces=new n.A(e),this._debug("got bitfield"),this.emit("bitfield",this.peerPieces)}_onRequest(e,t,s){if(this.amChoking&&(!this.hasFast||!this.allowedFastSet.includes(e)))return void(this.hasFast&&this.reject(e,t,s));this._debug("got request index=%d offset=%d length=%d",e,t,s);const i=(i,r)=>{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<e.length;n++){const r=e[n];if(r.piece===t&&r.offset===s&&r.length===i)return d(e,n),r}return null}_encryptHandshake(e){const t=new Uint8Array(e);if(!this._encryptGenerator)return this._debug("Warning: Encrypting without any generator"),t;for(let s=0;s<e.length;s++){const e=this._encryptGenerator.randomByte();t[s]=t[s]^e}return t}_encrypt(e){const t=new Uint8Array(e);if(!this._encryptGenerator||2!==this._encryptionMethod)return t;for(let s=0;s<e.length;s++){const e=this._encryptGenerator.randomByte();t[s]=t[s]^e}return t}_decryptHandshake(e){const t=new Uint8Array(e);if(!this._decryptGenerator)return this._debug("Warning: Decrypting without any generator"),t;for(let s=0;s<e.length;s++){const e=this._decryptGenerator.randomByte();t[s]=t[s]^e}return t}_decrypt(e){const t=new Uint8Array(e);if(!this._decryptGenerator||2!==this._encryptionMethod)return t;for(let s=0;s<e.length;s++){const e=this._decryptGenerator.randomByte();t[s]=t[s]^e}return t}_utfToHex(e){return(0,l.V5)((0,l.L0)(e))}}const I=T},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";i