UNPKG

webtorrent

Version:

Streaming torrent client

1 lines 218 kB
var e,t,i={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 i,s=""+e,n=t.exec(s);if(!n)return s;var r="",o=0,a=0;for(o=n.index;o<s.length;o++){switch(s.charCodeAt(o)){case 34:i="&quot;";break;case 38:i="&amp;";break;case 39:i="&#39;";break;case 60:i="&lt;";break;case 62:i="&gt;";break;default:continue}a!==o&&(r+=s.substring(a,o)),a=o+1,r+=i}return a!==o?r+s.substring(a,o):r}},717:(e,t,i)=>{i.r(t),i.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 s=i(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)=>s(()=>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,i)=>{e.exports=function(e){function t(e){let i,n,r,o=null;function a(...e){if(!a.enabled)return;const s=a,n=Number(new Date),r=n-(i||n);s.diff=r,s.prev=i,s.curr=n,i=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,(i,n)=>{if("%%"===i)return"%";o++;const r=t.formatters[n];if("function"==typeof r){const t=e[o];i=r.call(s,t),e.splice(o,1),o--}return i}),t.formatArgs.call(s,e);(s.log||t.log).apply(s,e)}return a.namespace=e,a.useColors=t.useColors(),a.color=t.selectColor(e),a.extend=s,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 s(e,i){const s=t(this.namespace+(void 0===i?":":i)+e);return s.log=this.log,s}function n(e,t){let i=0,s=0,n=-1,r=0;for(;i<e.length;)if(s<t.length&&(t[s]===e[i]||"*"===t[s]))"*"===t[s]?(n=s,r=i,s++):(i++,s++);else{if(-1===n)return!1;s=n+1,r++,i=r}for(;s<t.length&&"*"===t[s];)s++;return s===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 i=("string"==typeof e?e:"").trim().replace(/\s+/g,",").split(",").filter(Boolean);for(const e of i)"-"===e[0]?t.skips.push(e.slice(1)):t.names.push(e)},t.enabled=function(e){for(const i of t.skips)if(n(e,i))return!1;for(const i of t.names)if(n(e,i))return!0;return!1},t.humanize=i(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(i=>{t[i]=e[i]}),t.names=[],t.skips=[],t.formatters={},t.selectColor=function(e){let i=0;for(let t=0;t<e.length;t++)i=(i<<5)-i+e.charCodeAt(t),i|=0;return t.colors[Math.abs(i)%t.colors.length]},t.enable(t.load()),t}},815:(e,t,i)=>{var s,n=i(717),r=i(3519),o=i(6611);try{s=i(2376)}catch(e){}var a=function(){},h=void 0!==n&&/^v?\.0/.test(n.version),l=function(e){return"function"==typeof e},c=function(e,t,i,n){n=r(n);var c=!1;e.on("close",function(){c=!0}),o(e,{readable:t,writable:i},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&&!!s&&(e instanceof(s.ReadStream||a)||e instanceof(s.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),i=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 s=t.map(function(n,r){var o=r<t.length-1;return c(n,o,r>0,function(t){e||(e=t),t&&s.forEach(d),o||(s.forEach(d),i(e))})});return t.reduce(u)}},988:(e,t,i)=>{i.d(t,{A:()=>f});var s=i(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,i){const n=[];let r=null;return o._encode(n,e),r=(0,s.xW)(n),o.bytes=r.length,ArrayBuffer.isView(t)?(t.set(r,i),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,s.L0)(t.length+":"),t)},o.string=function(e,t){e.push((0,s.L0)((0,s.L0)(t).byteLength+":"+t))},o.number=function(e,t){if(Number.isInteger(t))return e.push((0,s.L0)("i"+BigInt(t)+"e"));const i=2147483648,n=(t/i|0)*i+(t%i|0);e.push((0,s.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 i,s=0;const n=Object.keys(t).sort(),r=n.length;for(;s<r;s++)i=n[s],null!=t[i]&&(o.string(e,i),o._encode(e,t[i]));e.push(a)},o.dictMap=function(e,t){e.push(h);const i=Array.from(t.keys()).sort();for(const s of i)null!=t.get(s)&&(ArrayBuffer.isView(s)?o._encode(e,s):o.string(e,String(s)),o._encode(e,t.get(s)));e.push(a)},o.list=function(e,t){let i=0;const s=t.length;for(e.push(l);i<s;i++)null!=t[i]&&o._encode(e,t[i]);e.push(a)},o.listSet=function(e,t){e.push(l);for(const i of t)null!=i&&o._encode(e,i);e.push(a)};const c=o;function d(e,t,i){let s=0,n=1;for(let r=t;r<i;r++){const i=e[r];if(i<58&&i>=48)s=10*s+(i-48);else if(r!==t||43!==i){if(r!==t||45!==i){if(46===i)break;throw new Error("not a number: buffer["+r+"] = "+i)}n=-1}}return s*n}function u(e,t,i,n){return null==e||0===e.length?null:("number"!=typeof t&&null==n&&(n=t,t=void 0),"number"!=typeof i&&null==n&&(n=i,i=void 0),u.position=0,u.encoding=n||null,u.data=ArrayBuffer.isView(e)?new Uint8Array(e.slice(t,i)):(0,s.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 i=u.data.length,s=u.data;for(;t<i;){if(s[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 i=(0,s.dU)(t);i.includes("�")&&(i=(0,s.V5)(t)),e[i]=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),i=++e+t;return u.position=i,u.encoding?(0,s.dU)(u.data.slice(e,i)):u.data.slice(e,i)};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,s.L0)(e).byteLength;return n(t)+1+t}(e);case"array":case"set":return function(e){let t=2;for(const i of e)t+=p(i);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 i=Object.keys(e);for(let r=0;r<i.length;r++){const o=(0,s.L0)(i[r]).byteLength;t+=n(o)+1+o,t+=p(e[i[r]])}return t}(e);case"map":return function(e){let t=2;for(const[i,r]of e){const e=(0,s.L0)(i).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,t,i)=>{var s=i(717);const n=void 0!==s&&!!s.hrtime,r=65535,o=n?1e8:100,a=n?()=>{const[e,t]=s.hrtime();return 1e9*e+t}:()=>performance.now();function h(e){return(a()-e)/o&r}e.exports=function(e){const t=a(),i=10*(e||5),s=[0];let n=1,o=h(t)-1&r;return function(e){const a=h(t);let l=a-o&r;for(l>i&&(l=i),o=a;l--;)n===i&&(n=0),s[n]=s[0===n?i-1:n-1],n++;e&&(s[n-1]+=e);const c=s[n-1],d=s.length<i?0:s[n===i?0:n];return s.length<10?c:10*(c-d)/s.length}}},1048:()=>{},1133:(e,t,i)=>{let s=i(8673);e.exports=new s(i(8446))},1314:e=>{e.exports=function(e,t){if(t>=e.length||t<0)return;var i=e.pop();if(t<e.length){var s=e[t];return e[t]=i,s}return i}},1467:e=>{function t(e,t){if("string"==typeof e[0])return e.join("");if("number"==typeof e[0])return new Uint8Array(e);const i=new Uint8Array(t);let s=0;for(let t=0,n=e.length;t<n;t++){const n=e[t];i.set(n,s),s+=n.byteLength||n.length}return i}e.exports=async function*(e,i=512,s={}){"object"==typeof i&&(i=(s=i).size);let{nopad:n,zeroPadding:r=!0}=s;n&&(r=!1);let o=[],a=0;for await(const s of e)if(a+=s.byteLength||s.length||1,o.push(s),a>=i){const e=t(o,a);let s=0;for(;a>=i;)yield e.slice(s,s+i),a-=i,s+=i;o=[e.slice(s,e.length)]}a&&(yield t(o,r?i:a))}},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 i(e){return{start:e.start,end:e.end}}function s(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(s).map(i);return c.type=e.type,c}(l):l}},3033:(e,t,i)=>{function s(e){return(e>>3)+Number(e%8!=0)}i.d(t,{A:()=>n});class n{get length(){return this.buffer.length<<3}constructor(e=0,t){const i=null==t?void 0:t.grow;this.grow=i?Number.isFinite(i)?s(i):i:0,this.buffer="number"==typeof e?new Uint8Array(s(e)):e}get(e){const t=e>>3;return t<this.buffer.length&&!!(this.buffer[t]&128>>e%8)}set(e,t=!0){const i=e>>3;if(t){if(i>=this.buffer.length){const e=Math.max(i+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[i]|=128>>e%8}else i<this.buffer.length&&(this.buffer[i]&=~(128>>e%8))}setAll(e,t=0){const i=Math.min(s(t+e.length),this.grow);if(this.buffer.length<i){const e=new Uint8Array(i);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,i=8*this.buffer.length){let s=t>>3,n=128>>t%8;for(let r=t;r<i;r++)e(!!(this.buffer[s]&n),r),1===n?(s+=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,i)=>{var s=i(717),n=i(5472),r=i(3085),o=function(e,t,i){this.tokenBucket=new n(e,e,t,null),this.tokenBucket.content=e,this.curIntervalStart=r(),this.tokensThisInterval=0,this.fireImmediately=i};o.prototype={tokenBucket:null,curIntervalStart:0,tokensThisInterval:0,fireImmediately:!1,removeTokens:function(e,t){if(e>this.tokenBucket.bucketSize)return s.nextTick(t.bind(null,"Requested tokens "+e+" exceeds maximum tokens per interval "+this.tokenBucket.bucketSize,null)),!1;var i=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)s.nextTick(t.bind(null,null,-1));else{var o=Math.ceil(this.curIntervalStart+this.tokenBucket.interval-n);setTimeout(function(){i.tokenBucket.removeTokens(e,a)},o)}return!1}return this.tokenBucket.removeTokens(e,a);function a(s,n){if(s)return t(s,null);i.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 i=this.tokenBucket.tryRemoveTokens(e);return i&&(this.tokensThisInterval+=e),i},getTokensRemaining:function(){return this.tokenBucket.drip(),this.tokenBucket.content}},e.exports=o},3085:(e,t,i)=>{var s=i(717);e.exports=function(){if(void 0!==s&&s.hrtime){var e=s.hrtime(),t=e[0],i=e[1];return 1e3*t+Math.floor(i/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(),i=this;let s=t.read();return{next(){const e=s;return s=t.read(),e},return:async n=>(await s,t.releaseLock(),e||i.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,i)=>{var s=i(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)},i=e.name||"Function wrapped with `once`";return t.onceError=i+" shouldn't be called more than once",t.called=!1,t}e.exports=s(n),e.exports.strict=s(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,i)=>{const s=i(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,i=()=>{}){this.mem[e]=t,this.store.put(e,t,t=>{this.mem[e]=null,i(t)})}get(e,t,i=()=>{}){if("function"==typeof t)return this.get(e,null,t);let n=this.mem[e];if(!n)return this.store.get(e,t,i);t||(t={});const r=t.offset||0,o=t.length||n.length-r;0===r&&o===n.length||(n=n.slice(r,o+r)),s(()=>i(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=[],i=0;i<e;i++)t.push({model:"",speed:0,times:{user:0,nice:0,sys:0,idle:0,irq:0}});return t}},4043:()=>{},4343:()=>{},4497:(e,t,i)=>{const s=i(8454),n=i(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 s(t)}put(e,t,i=()=>{}){if(!this.cache)return n(()=>i(new Error("CacheStore closed")));this.cache.remove(e),this.store.put(e,t,i)}get(e,t,i=()=>{}){if("function"==typeof t)return this.get(e,null,t);if(!this.cache)return n(()=>i(new Error("CacheStore closed")));t||(t={});let s=this.cache.get(e);if(s){const e=t.offset||0,r=t.length||s.length-e;return 0===e&&r===s.length||(s=s.slice(e,r+e)),n(()=>i(null,s))}let r=this.inProgressGets.get(e);const o=!!r;r||(r=[],this.inProgressGets.set(e,r)),r.push({opts:t,cb:i}),o||this.store.get(e,(t,i)=>{t||null==this.cache||this.cache.set(e,i);const s=this.inProgressGets.get(e);this.inProgressGets.delete(e);for(const{opts:e,cb:n}of s)if(t)n(t);else{const t=e.offset||0,s=e.length||i.length-t;let r=i;0===t&&s===i.length||(r=i.slice(t,s+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,i)=>{e.exports=n;const s=i(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,i=()=>{}){if(this.closed)return s(()=>i(new Error("Storage is closed")));const n=e===this.lastChunkIndex;return n&&t.length!==this.lastChunkLength?s(()=>i(new Error("Last chunk length must be "+this.lastChunkLength))):n||t.length===this.chunkLength?(this.chunks[e]=t,void s(()=>i(null))):s(()=>i(new Error("Chunk length must be "+this.chunkLength)))},n.prototype.get=function(e,t,i=()=>{}){if("function"==typeof t)return this.get(e,null,t);if(this.closed)return s(()=>i(new Error("Storage is closed")));let n=this.chunks[e];if(!n){const e=new Error("Chunk not found");return e.notFound=!0,s(()=>i(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)),s(()=>i(null,n))},n.prototype.close=n.prototype.destroy=function(e=()=>{}){if(this.closed)return s(()=>e(new Error("Storage is closed")));this.closed=!0,this.chunks=null,s(()=>e(null))}},5335:(e,t,i)=>{const s=i(3198),n=i(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 s(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,i)=>{e.exports=function(e,t,i){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(){i&&i(e,n),i=null}c?s(t):t()}function u(t,i,s){if(n[t]=s,i&&(h=!0),0===--o||i)d(i);else if(!h&&l<r){let t;a?(t=a[l],l+=1,e[t](function(e,i){u(t,e,i)})):(t=l,l+=1,e[t](function(e,i){u(t,e,i)}))}}l=t,o?a?a.some(function(i,s){return e[i](function(e,t){u(i,e,t)}),s===t-1}):e.some(function(e,i){return e(function(e,t){u(i,e,t)}),i===t-1}):d(null);c=!1};const s=i(9596)},5472:(e,t,i)=>{var s=i(717),n=function(e,t,i,s){if(this.bucketSize=e,this.tokensPerInterval=t,"string"==typeof i)switch(i){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 "+i)}else this.interval=i;this.parentBucket=s,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 i=this;return this.bucketSize?e>this.bucketSize?(s.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(s,r){return s?t(s,null):e>i.content?n():(i.content-=e,void t(null,Math.min(r,i.content)))}):(this.content-=e,s.nextTick(t.bind(null,null,this.content)),!0)):(s.nextTick(t.bind(null,null,e,Number.POSITIVE_INFINITY)),!0);function n(){var s=Math.ceil((e-i.content)*(i.interval/i.tokensPerInterval));return setTimeout(function(){i.removeTokens(e,t)},s),!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 i=t*(this.tokensPerInterval/this.interval);this.content=Math.min(this.content+i,this.bucketSize)}else this.content=this.bucketSize}},e.exports=n},5658:(e,t,i)=>{i.d(t,{A:()=>_,enableSecure:()=>m});var s=i(7007),n=i(8179),r=i(1314),o=i(7833),a=i(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 s{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,this.emit("connect"),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,i){e.emit("download",t.length),e.destroyed||i(null,t)}}),this.wire,this.throttleGroups.up.throttle(),new n.Transform({transform(t,i){e.emit("upload",t.length),e.destroyed||i(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 i=this.addr;!i&&this.conn.remoteAddress&&this.conn.remotePort&&(i=`${this.conn.remoteAddress}:${this.conn.remotePort}`),this.swarm._onWire(this.wire,i),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&&this.emit("disconnect",e),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,i=this.conn,s=this.wire;this.swarm=null,this.conn=null,this.wire=null,t&&s&&r(t.wires,t.wires.indexOf(s)),i&&(i.on("error",()=>{}),i.destroy()),s&&s.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,i,s=null)=>{const n=new _(e.id,"webrtc");if(n.conn=e,n.swarm=t,n.throttleGroups=i,n.source=s,n.conn.connected)n.onConnect();else{const e=()=>{n.conn.removeListener("connect",t),n.conn.removeListener("error",i)},t=()=>{e(),n.onConnect()},i=t=>{e(),n.destroy(t)};n.conn.once("connect",t),n.conn.once("error",i),n.startConnectTimeout()}return n},_.createTCPIncomingPeer=(e,t)=>_._createIncomingPeer(e,h,t),_.createUTPIncomingPeer=(e,t)=>_._createIncomingPeer(e,c,t),_.createTCPOutgoingPeer=(e,t,i,s)=>_._createOutgoingPeer(e,t,l,i,s),_.createUTPOutgoingPeer=(e,t,i,s)=>_._createOutgoingPeer(e,t,d,i,s),_._createIncomingPeer=(e,t,i)=>{const s=`${e.remoteAddress}:${e.remotePort}`,n=new _(s,t);return n.conn=e,n.addr=s,n.throttleGroups=i,n.onConnect(),n},_._createOutgoingPeer=(e,t,i,s,n=null)=>{const r=new _(e,i);return r.addr=e,r.swarm=t,r.throttleGroups=s,r.source=n,r},_.createWebSeedPeer=(e,t,i,s)=>{const n=new _(t,u);return n.swarm=i,n.conn=e,n.throttleGroups=s,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,i)=>{const s=i(5809);e.exports=class{constructor(e){this.hwm=e||16,this.head=new s(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 s(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 i in t)Object.defineProperty(e,i,{value:t[i],enumerable:!0,configurable:!0});return e}e.exports=function(e,i,s){if(!e||"string"==typeof e)throw new TypeError("Please pass an Error to err-code");s||(s={}),"object"==typeof i&&(s=i,i=""),i&&(s.code=i);try{return t(e,s)}catch(i){s.message=e.message,s.stack=e.stack;const n=function(){};n.prototype=Object.create(Object.getPrototypeOf(e));return t(new n,s)}}},6458:(e,t,i)=>{i.d(t,{A:()=>O});var s=i(988),n=i(3033),r=i(7264),o=i(7833),a=i(8727),h=i(8179),l=i(9639),c=i(1035),d=i(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 i=e.length;i--;)e[i]^=t[i];return e}function A(e,t=0){return e[t]<<24|e[t+1]<<16|e[t+2]<<8|e[t+3]}function P(e,t,i){e[t]=i>>>24&255,e[t+1]=i>>>16&255,e[t+2]=i>>>8&255,e[t+3]=255&i}class T{constructor(e,t,i,s){this.piece=e,this.offset=t,this.length=i,this.callback=s}}class I{constructor(){this.buffer=new Uint8Array}get(e){return!0}set(e){}}class L extends h.Duplex{constructor(e=null,t=0,i=!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=i,i?(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 i=this._nextExt,s=new e(this);function n(){}"function"!=typeof s.onHandshake&&(s.onHandshake=n),"function"!=typeof s.onExtendedHandshake&&(s.onExtendedHandshake=n),"function"!=typeof s.onMessage&&(s.onMessage=n),this.extendedMapping[i]=t,this._ext[t]=s,this[t]=s,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)),i=C(await(0,l.tW)((0,l.fk)(this._utfToHex("req2")+e)),await(0,l.tW)((0,l.fk)(this._utfToHex("req3")+this._sharedSecret))),s=new DataView((0,l.po)(2).buffer).getUint16(0)%512,n=(0,l.po)(s);let r=new Uint8Array(14+s+2);r.set(x),r.set(S,8);const o=new DataView(r.buffer);o.setInt16(12,s),n.copy(r,14),o.setInt16(14+s,0),r=this._encryptHandshake(r),this._push((0,l.xW)([t,i,r]))}async sendPe4(e){await this.setEncrypt(this._sharedSecret,e);const t=new DataView((0,l.po)(2).buffer).getUint16(0)%512,i=(0,l.po)(t);let s=new Uint8Array(14+t);const n=new DataView(s.buffer);s.set(x),s.set(E,8),n.setInt16(12,t),s.set(i,14),s=this._encryptHandshake(s),this._push(s),this._cryptoHandshakeDone=!0,this._debug("completed crypto handshake")}handshake(e,t,i){let s,n;if("string"==typeof e?(e=e.toLowerCase(),s=(0,l.fk)(e)):(s=e,e=(0,l.V5)(s)),"string"==typeof t?n=(0,l.fk)(t):(n=t,t=(0,l.V5)(n)),this._infoHash=s,20!==s.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,i);const r=new Uint8Array(w);this.extensions={extended:!0,dht:!(!i||!i.dht),fast:!(!i||!i.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,s,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 i=this.extendedMapping[t];e.m[i]=Number(t)}this.extended(0,s.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,i,s){return s||(s=()=>{}),this._finished?s(new Error("wire is closed")):!this.peerChoking||this.hasFast&&this.peerAllowedFastSet.includes(e)?(this._debug("request index=%d offset=%d length=%d",e,t,i),this.requests.push(new T(e,t,i,s)),this._timeout||this._resetTimeout(!0),void this._message(6,[e,t,i],null)):s(new Error("peer is choking"))}piece(e,t,i){this._debug("piece index=%d offset=%d",e,t),this._message(7,[e,t],i),this.uploaded+=i.length,this.uploadSpeed(i.length),this.emit("upload",i.length)}cancel(e,t,i){this._debug("cancel index=%d offset=%d length=%d",e,t,i),this._callback(this._pull(this.requests,e,t,i),new Error("request was cancelled"),null),this._message(8,[e,t,i],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,i){if(!this.hasFast)throw Error("fast extension is disabled");this._debug("reject index=%d offset=%d length=%d",e,t,i),this._pull(this.peerRequests,e,t,i),this._message(16,[e,t,i],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 i=new Uint8Array([e]),n=ArrayBuffer.isView(t)?t:s.A.encode(t);this._message(20,[],(0,l.xW)([i,n]))}}async setEncrypt(e,t){if(!this.type.startsWith("tcp"))return!1;const i="tcpOutgoing"===this.type,s=new a([...await(0,l.tW)((0,l.fk)(this._utfToHex("keyA")+e+t))]),n=new a([...await(0,l.tW)((0,l.fk)(this._utfToHex("keyB")+e+t))]);this._encryptGenerator=i?s:n,this._decryptGenerator=i?n:s;for(let e=0;e<1024;e++)this._encryptGenerator.randomByte(),this._decryptGenerator.randomByte();return this._setGenerators=!0,this.emit("_generators"),!0}_message(e,t,i){const s=i?i.length:0,n=new Uint8Array(5+4*t.length);P(n,0,n.length+s-4),n[4]=e;for(let e=0;e<t.length;e++)P(n,5+4*e,t[e]);this._push(n),i&&this._push(i)}_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.tW)((0,l.fk)(this._utfToHex("req3")+this._sharedSecret)),i=(0,l.V5)(C(t,e));this.emit("pe3",i)}_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,i){const s=(0,l.V5)(e),n=(0,l.V5)(t);this._debug("got handshake i=%s p=%s exts=%o",s,n,i),this.peerId=n,this.peerIdBuffer=t,this.peerExtensions=i,this.extensions.fast&&this.peerExtensions.fast&&(this._debug("fast extension is enabled"),this.hasFast=!0),this.emit("handshake",s,n,i);for(const e in this._ext)this._ext[e].onHandshake(s,n,i);i.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,i){if(this.amChoking&&(!this.hasFast||!this.allowedFastSet.includes(e)))return void(this.hasFast&&this.reject(e,t,i));this._debug("got request index=%d offset=%d length=%d",e,t,i);const s=(s,r)=>{if(n===this._pull(this.peerRequests,e,t,i))return s?(this._debug("error satisfying request index=%d offset=%d length=%d (%s)",e,t,i,s.message),void(this.hasFast&&this.reject(e,t,i))):void this.piece(e,t,r)},n=new T(e,t,i,s);this.peerRequests.push(n),this.emit("request",e,t,i,s)}_onPiece(e,t,i){this._debug("got piece index=%d offset=%d",e,t),this._callback(this._pull(this.requests,e,t,i.length),null,i),this.downloaded+=i.length,this.downloadSpeed(i.length),this.emit("download",i.length),this.emit("piece",e,t,i)}_onCancel(e,t,i){this._debug("got cancel index=%d offset=%d length=%d",e,t,i),this._pull(this.peerRequests,e,t,i),this.emit("cancel",e,t,i)}_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 I,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,i){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,i),this._callback(this._pull(this.requests,e,t,i),new Error("request was rejected"),null),this.emit("reject",e,t,i)}_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=s.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.subarray(this._parserSize)]:[],this._parser(e.subarray(0,this._parserSize))}t(null)}_callback(e,t,i){e&&(this._resetTimeout(!this.peerChoking&&!this._finished),e.callback(t,i))}_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=A(e);t>0?this._parse(t,this._onMessage):(this._onKeepAlive(),this._parse(4,this._onMessageLength))}_onMessage(e){switch(this._parse(4,this._onMessageLength),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(A(e,1));case 5:return this._onBitField(e.subarray(1));case 6:return this._onRequest(A(e,1),A(e,5),A(e,9));case 7:return this._onPiece(A(e,1),A(e,5),e.subarray(9));case 8:return this._onCancel(A(e,1),A(e,5),A(e,9));case 9:return this._onPort(e[1]<<8|e[2]);case 13:return this._onSuggest(A(e,1));case 14:return this._onHaveAll();case 15:return this._onHaveNone();case 16:return this._onReject(A(e,1),A(e,5),A(e,9));case 17:return this._onAllowedFast(A(e,1));case 20:return this._onExtended(e[1],e.subarray(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,async e=>{this._onPe2(e),this._setGenerators||await new Promise(e=>this.once("_generators",e)),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,async e=>{this._onPe3(e),this._setGenerators||await new Promise(e=>this.once("_generators",e)),this._parsePe3Encrypted()})}_parsePe3Encrypted(){this._parse(14,e=>{const t=this._decryptHandshake(e.slice(0,8)),i=this._decryptHandshake(e.slice(8,12)),s=new DataView(this._decryptHandshake(e.slice(12,14)).buffer).getUint16(0);this._parse(s,e=>{e=this._decryptHandshake(e),this._parse(2,s=>{const n=new DataView(this._decryptHandshake(s).buffer).getUint16(0);this._parse(n,s=>{s=this._decryptHandshake(s),this._onPe3Encrypted(t,i,e,s);const r=n?s[0]:null,o=n?s.slice(1,20):null;19===r&&"BitTorrent protocol"===(0,l.dU)(o)?this._onHandshakeBuffer(s.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)),i=new DataView(this._decryptHandshake(e.slice(4,6)).buffer).getUint16(0);this._parse(i,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,i,s){for(let n=0;n<e.length;n++){const r=e[n];if(r.piece===t&&r.offset===i&&r.length===s)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 i=0;i<e.length;i++){const e=this._encryptGenerator.randomByte();t[i]=t[i]^e}return t}_encrypt(e){const t=new Uint8Array(e);if(!this._encryptGenerator||2!==this._encryptionMethod)return t;for(let i=0;i<e.length;i++){const e=this._encryptGenerator.randomByte();t[i]=t[i]^e}return t}_decryptHandshake(e){const t=new Uint8Array(e);if(!this._decryptGenerator)return this._debug("Warning: Decrypting without any generator"),t;for(let i=0;i<e.length;i++){const e=this._decryptGenerator.randomByte();t[i]=t[i]^e}return t}_decrypt(e){const t=new Uint8Array(e);if(!this._decryptGenerator||2!==this._encryptionMethod)return t;for(let i=0;i<e.length;i++){const e=this._decryptGenerator.randomByte();t[i]=t[i]^e}return t}_utfToHex(e){return(0,l.V5)((0,l.L0)(e))}}const O=L},6585:e=>{var t=1e3,i=60*t,s=60*i,n=24*s,r=7*n,o=365.25*n;function a(e,t,i,s){var n=t>=1.5*i;return Math.round(e/i)+" "+s+(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*s;case"minutes":case"minute":case"mins":case"min":case"m":return h*i;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>=s)return a(e,r,s,"hour");if(r>=i)return a(e,r,i,"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>=s)return Math.round(e/s)+"h";if(r>=i)return Math.round(e/i)+"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))}},6587:e=>{e.exports=function e(t,i){if(t&&i)return e(t)(i);if("function"!=typeof t)throw new TypeError("need wrapper function");return Object.keys(t).f