@libp2p/fetch
Advanced tools
Comparing version 2.0.15 to 3.0.0
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PFetch = factory()}(typeof self !== 'undefined' ? self : this, function () { | ||
"use strict";var Libp2PFetch=(()=>{var Ut=Object.defineProperty;var cr=Object.getOwnPropertyDescriptor;var hr=Object.getOwnPropertyNames;var ur=Object.prototype.hasOwnProperty;var y=(r,t)=>{for(var e in t)Ut(r,e,{get:t[e],enumerable:!0})},fr=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of hr(t))!ur.call(r,i)&&i!==e&&Ut(r,i,{get:()=>t[i],enumerable:!(n=cr(t,i))||n.enumerable});return r};var lr=r=>fr(Ut({},"__esModule",{value:!0}),r);var Mn={};y(Mn,{fetch:()=>Bn});var st=class extends Error{static name="AbortError";constructor(t="The operation was aborted"){super(t),this.name="AbortError"}};var at=class extends Error{static name="InvalidParametersError";constructor(t="Invalid parameters"){super(t),this.name="InvalidParametersError"}};var ct=class extends Error{static name="InvalidMessageError";constructor(t="Invalid message"){super(t),this.name="InvalidMessageError"}},ht=class extends Error{static name="ProtocolError";constructor(t="Protocol error"){super(t),this.name="ProtocolError"}};var be=(r,...t)=>{try{[...t]}catch{}};function G(){let r={};return r.promise=new Promise((t,e)=>{r.resolve=t,r.reject=e}),r}var ut=class extends Error{type;code;constructor(t,e,n){super(t??"The operation was aborted"),this.type="aborted",this.name=n??"AbortError",this.code=e??"ABORT_ERR"}};async function ye(r,t,e){if(t==null)return r;if(t.aborted)return Promise.reject(new ut(e?.errorMessage,e?.errorCode,e?.errorName));let n,i=new ut(e?.errorMessage,e?.errorCode,e?.errorName);try{return await Promise.race([r,new Promise((o,s)=>{n=()=>{s(i)},t.addEventListener("abort",n)})])}finally{n!=null&&t.removeEventListener("abort",n)}}var Dt=class{readNext;haveNext;ended;nextResult;constructor(){this.ended=!1,this.readNext=G(),this.haveNext=G()}[Symbol.asyncIterator](){return this}async next(){if(this.nextResult==null&&await this.haveNext.promise,this.nextResult==null)throw new Error("HaveNext promise resolved but nextResult was undefined");let t=this.nextResult;return this.nextResult=void 0,this.readNext.resolve(),this.readNext=G(),t}async throw(t){return this.ended=!0,t!=null&&(this.haveNext.promise.catch(()=>{}),this.haveNext.reject(t)),{done:!0,value:void 0}}async return(){let t={done:!0,value:void 0};return await this._push(void 0),t}async push(t,e){await this._push(t,e)}async end(t,e){t!=null?await this.throw(t):await this._push(void 0,e)}async _push(t,e){if(t!=null&&this.ended)throw new Error("Cannot push value onto an ended pushable");for(;this.nextResult!=null;)await this.readNext.promise;t!=null?this.nextResult={done:!1,value:t}:(this.ended=!0,this.nextResult={done:!0,value:void 0}),this.haveNext.resolve(),this.haveNext=G(),await ye(this.readNext.promise,e?.signal,e)}};function Ee(){return new Dt}function N(r=0){return new Uint8Array(r)}function b(r=0){return new Uint8Array(r)}function Ft(r,t){t==null&&(t=r.reduce((i,o)=>i+o.length,0));let e=b(t),n=0;for(let i of r)e.set(i,n),n+=i.length;return e}function Le(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}var Ae=Symbol.for("@achingbrain/uint8arraylist");function Se(r,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let e=0;for(let n of r){let i=e+n.byteLength;if(t<i)return{buf:n,index:t-e};e=i}throw new RangeError("index is out of bounds")}function ft(r){return!!r?.[Ae]}var O=class r{bufs;length;[Ae]=!0;constructor(...t){this.bufs=[],this.length=0,t.length>0&&this.appendAll(t)}*[Symbol.iterator](){yield*this.bufs}get byteLength(){return this.length}append(...t){this.appendAll(t)}appendAll(t){let e=0;for(let n of t)if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.push(n);else if(ft(n))e+=n.byteLength,this.bufs.push(...n.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}prepend(...t){this.prependAll(t)}prependAll(t){let e=0;for(let n of t.reverse())if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.unshift(n);else if(ft(n))e+=n.byteLength,this.bufs.unshift(...n.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}get(t){let e=Se(this.bufs,t);return e.buf[e.index]}set(t,e){let n=Se(this.bufs,t);n.buf[n.index]=e}write(t,e=0){if(t instanceof Uint8Array)for(let n=0;n<t.length;n++)this.set(e+n,t[n]);else if(ft(t))for(let n=0;n<t.length;n++)this.set(e+n,t.get(n));else throw new Error("Could not write value, must be an Uint8Array or a Uint8ArrayList")}consume(t){if(t=Math.trunc(t),!(Number.isNaN(t)||t<=0)){if(t===this.byteLength){this.bufs=[],this.length=0;return}for(;this.bufs.length>0;)if(t>=this.bufs[0].byteLength)t-=this.bufs[0].byteLength,this.length-=this.bufs[0].byteLength,this.bufs.shift();else{this.bufs[0]=this.bufs[0].subarray(t),this.length-=t;break}}}slice(t,e){let{bufs:n,length:i}=this._subList(t,e);return Ft(n,i)}subarray(t,e){let{bufs:n,length:i}=this._subList(t,e);return n.length===1?n[0]:Ft(n,i)}sublist(t,e){let{bufs:n,length:i}=this._subList(t,e),o=new r;return o.length=i,o.bufs=[...n],o}_subList(t,e){if(t=t??0,e=e??this.length,t<0&&(t=this.length+t),e<0&&(e=this.length+e),t<0||e>this.length)throw new RangeError("index is out of bounds");if(t===e)return{bufs:[],length:0};if(t===0&&e===this.length)return{bufs:this.bufs,length:this.length};let n=[],i=0;for(let o=0;o<this.bufs.length;o++){let s=this.bufs[o],a=i,c=a+s.byteLength;if(i=c,t>=c)continue;let f=t>=a&&t<c,l=e>a&&e<=c;if(f&&l){if(t===a&&e===c){n.push(s);break}let u=t-a;n.push(s.subarray(u,u+(e-t)));break}if(f){if(t===0){n.push(s);continue}n.push(s.subarray(t-a));continue}if(l){if(e===c){n.push(s);break}n.push(s.subarray(0,e-a));break}n.push(s)}return{bufs:n,length:e-t}}indexOf(t,e=0){if(!ft(t)&&!(t instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let n=t instanceof Uint8Array?t:t.subarray();if(e=Number(e??0),isNaN(e)&&(e=0),e<0&&(e=this.length+e),e<0&&(e=0),t.length===0)return e>this.length?this.length:e;let i=n.byteLength;if(i===0)throw new TypeError("search must be at least 1 byte long");let o=256,s=new Int32Array(o);for(let u=0;u<o;u++)s[u]=-1;for(let u=0;u<i;u++)s[n[u]]=u;let a=s,c=this.byteLength-n.byteLength,f=n.byteLength-1,l;for(let u=e;u<=c;u+=l){l=0;for(let w=f;w>=0;w--){let it=this.get(u+w);if(n[w]!==it){l=Math.max(1,w-a[it]);break}}if(l===0)return u}return-1}getInt8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getInt8(0)}setInt8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setInt8(0,e),this.write(n,t)}getInt16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt16(0,e)}setInt16(t,e,n){let i=N(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt16(0,e,n),this.write(i,t)}getInt32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt32(0,e)}setInt32(t,e,n){let i=N(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt32(0,e,n),this.write(i,t)}getBigInt64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigInt64(0,e)}setBigInt64(t,e,n){let i=N(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigInt64(0,e,n),this.write(i,t)}getUint8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint8(0)}setUint8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setUint8(0,e),this.write(n,t)}getUint16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint16(0,e)}setUint16(t,e,n){let i=N(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint16(0,e,n),this.write(i,t)}getUint32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(0,e)}setUint32(t,e,n){let i=N(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint32(0,e,n),this.write(i,t)}getBigUint64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(0,e)}setBigUint64(t,e,n){let i=N(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigUint64(0,e,n),this.write(i,t)}getFloat32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat32(0,e)}setFloat32(t,e,n){let i=N(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat32(0,e,n),this.write(i,t)}getFloat64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat64(0,e)}setFloat64(t,e,n){let i=N(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat64(0,e,n),this.write(i,t)}equals(t){if(t==null||!(t instanceof r)||t.bufs.length!==this.bufs.length)return!1;for(let e=0;e<this.bufs.length;e++)if(!Le(this.bufs[e],t.bufs[e]))return!1;return!0}static fromUint8Arrays(t,e){let n=new r;return n.bufs=t,e==null&&(e=t.reduce((i,o)=>i+o.byteLength,0)),n.length=e,n}};var lt=class extends Error{name="UnexpectedEOFError";code="ERR_UNEXPECTED_EOF"};var vt=class extends Error{code;constructor(t,e){super(t),this.code=e}},Ct=class extends vt{type;constructor(t){super(t,"ABORT_ERR"),this.type="aborted",this.name="AbortError"}};function Ne(r,t){let e=Ee();r.sink(e).catch(async s=>{await e.end(s)}),r.sink=async s=>{for await(let a of s)await e.push(a);await e.end()};let n=r.source;r.source[Symbol.iterator]!=null?n=r.source[Symbol.iterator]():r.source[Symbol.asyncIterator]!=null&&(n=r.source[Symbol.asyncIterator]());let i=new O;return{read:async(s,a)=>{a?.signal?.throwIfAborted();let c,f=new Promise((l,u)=>{c=()=>{u(new Ct("Read aborted"))},a?.signal?.addEventListener("abort",c)});try{if(s==null){let{done:u,value:w}=await Promise.race([n.next(),f]);return u===!0?new O:w}for(;i.byteLength<s;){let{value:u,done:w}=await Promise.race([n.next(),f]);if(w===!0)throw new lt("unexpected end of input");i.append(u)}let l=i.sublist(0,s);return i.consume(s),l}finally{c!=null&&a?.signal?.removeEventListener("abort",c)}},write:async(s,a)=>{a?.signal?.throwIfAborted(),s instanceof Uint8Array?await e.push(s,a):await e.push(s.subarray(),a)},unwrap:()=>{if(i.byteLength>0){let s=r.source;r.source=async function*(){t?.yieldBytes===!1?yield i:yield*i,yield*s}()}return r}}}var dr=Math.pow(2,7),pr=Math.pow(2,14),mr=Math.pow(2,21),Rt=Math.pow(2,28),Ot=Math.pow(2,35),Tt=Math.pow(2,42),Bt=Math.pow(2,49),h=128,m=127;function T(r){if(r<dr)return 1;if(r<pr)return 2;if(r<mr)return 3;if(r<Rt)return 4;if(r<Ot)return 5;if(r<Tt)return 6;if(r<Bt)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function Mt(r,t,e=0){switch(T(r)){case 8:t[e++]=r&255|h,r/=128;case 7:t[e++]=r&255|h,r/=128;case 6:t[e++]=r&255|h,r/=128;case 5:t[e++]=r&255|h,r/=128;case 4:t[e++]=r&255|h,r>>>=7;case 3:t[e++]=r&255|h,r>>>=7;case 2:t[e++]=r&255|h,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function gr(r,t,e=0){switch(T(r)){case 8:t.set(e++,r&255|h),r/=128;case 7:t.set(e++,r&255|h),r/=128;case 6:t.set(e++,r&255|h),r/=128;case 5:t.set(e++,r&255|h),r/=128;case 4:t.set(e++,r&255|h),r>>>=7;case 3:t.set(e++,r&255|h),r>>>=7;case 2:t.set(e++,r&255|h),r>>>=7;case 1:{t.set(e++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return t}function kt(r,t){let e=r[t],n=0;if(n+=e&m,e<h||(e=r[t+1],n+=(e&m)<<7,e<h)||(e=r[t+2],n+=(e&m)<<14,e<h)||(e=r[t+3],n+=(e&m)<<21,e<h)||(e=r[t+4],n+=(e&m)*Rt,e<h)||(e=r[t+5],n+=(e&m)*Ot,e<h)||(e=r[t+6],n+=(e&m)*Tt,e<h)||(e=r[t+7],n+=(e&m)*Bt,e<h))return n;throw new RangeError("Could not decode varint")}function wr(r,t){let e=r.get(t),n=0;if(n+=e&m,e<h||(e=r.get(t+1),n+=(e&m)<<7,e<h)||(e=r.get(t+2),n+=(e&m)<<14,e<h)||(e=r.get(t+3),n+=(e&m)<<21,e<h)||(e=r.get(t+4),n+=(e&m)*Rt,e<h)||(e=r.get(t+5),n+=(e&m)*Ot,e<h)||(e=r.get(t+6),n+=(e&m)*Tt,e<h)||(e=r.get(t+7),n+=(e&m)*Bt,e<h))return n;throw new RangeError("Could not decode varint")}function Ie(r,t,e=0){return t==null&&(t=b(T(r))),t instanceof Uint8Array?Mt(r,t,e):gr(r,t,e)}function Ue(r,t=0){return r instanceof Uint8Array?kt(r,t):wr(r,t)}var dt=class extends Error{name="InvalidMessageLengthError";code="ERR_INVALID_MSG_LENGTH"},pt=class extends Error{name="InvalidDataLengthError";code="ERR_MSG_DATA_TOO_LONG"},mt=class extends Error{name="InvalidDataLengthLengthError";code="ERR_MSG_LENGTH_TOO_LONG"};function De(r,t={}){let e=Ne(r,t);t.maxDataLength!=null&&t.maxLengthLength==null&&(t.maxLengthLength=T(t.maxDataLength));let n=t?.lengthDecoder??Ue,i=t?.lengthEncoder??Ie;return{read:async s=>{let a=-1,c=new O;for(;;){c.append(await e.read(1,s));try{a=n(c)}catch(f){if(f instanceof RangeError)continue;throw f}if(a<0)throw new dt("Invalid message length");if(t?.maxLengthLength!=null&&c.byteLength>t.maxLengthLength)throw new mt("message length length too long");if(a>-1)break}if(t?.maxDataLength!=null&&a>t.maxDataLength)throw new pt("message length too long");return e.read(a,s)},write:async(s,a)=>{await e.write(new O(i(s.byteLength),s),a)},writeV:async(s,a)=>{let c=new O(...s.flatMap(f=>[i(f.byteLength),f]));await e.write(c,a)},unwrap:()=>e.unwrap()}}function Vt(r,t){let e=De(r,t),n={read:async(i,o)=>{let s=await e.read(o);return i.decode(s)},write:async(i,o,s)=>{await e.write(o.encode(i),s)},writeV:async(i,o,s)=>{await e.writeV(i.map(a=>o.encode(a)),s)},pb:i=>({read:async o=>n.read(i,o),write:async(o,s)=>n.write(o,i,s),writeV:async(o,s)=>n.writeV(o,i,s),unwrap:()=>n}),unwrap:()=>e.unwrap()};return n}var Gt={};y(Gt,{base10:()=>Ar});var mi=new Uint8Array(0);function Fe(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}function B(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}function ve(r){return new TextEncoder().encode(r)}function Ce(r){return new TextDecoder().decode(r)}function br(r,t){if(r.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),n=0;n<e.length;n++)e[n]=255;for(var i=0;i<r.length;i++){var o=r.charAt(i),s=o.charCodeAt(0);if(e[s]!==255)throw new TypeError(o+" is ambiguous");e[s]=i}var a=r.length,c=r.charAt(0),f=Math.log(a)/Math.log(256),l=Math.log(256)/Math.log(a);function u(d){if(d instanceof Uint8Array||(ArrayBuffer.isView(d)?d=new Uint8Array(d.buffer,d.byteOffset,d.byteLength):Array.isArray(d)&&(d=Uint8Array.from(d))),!(d instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(d.length===0)return"";for(var x=0,_=0,S=0,D=d.length;S!==D&&d[S]===0;)S++,x++;for(var F=(D-S)*l+1>>>0,L=new Uint8Array(F);S!==D;){for(var v=d[S],M=0,A=F-1;(v!==0||M<_)&&A!==-1;A--,M++)v+=256*L[A]>>>0,L[A]=v%a>>>0,v=v/a>>>0;if(v!==0)throw new Error("Non-zero carry");_=M,S++}for(var R=F-_;R!==F&&L[R]===0;)R++;for(var ot=c.repeat(x);R<F;++R)ot+=r.charAt(L[R]);return ot}function w(d){if(typeof d!="string")throw new TypeError("Expected String");if(d.length===0)return new Uint8Array;var x=0;if(d[x]!==" "){for(var _=0,S=0;d[x]===c;)_++,x++;for(var D=(d.length-x)*f+1>>>0,F=new Uint8Array(D);d[x];){var L=e[d.charCodeAt(x)];if(L===255)return;for(var v=0,M=D-1;(L!==0||v<S)&&M!==-1;M--,v++)L+=a*F[M]>>>0,F[M]=L%256>>>0,L=L/256>>>0;if(L!==0)throw new Error("Non-zero carry");S=v,x++}if(d[x]!==" "){for(var A=D-S;A!==D&&F[A]===0;)A++;for(var R=new Uint8Array(_+(D-A)),ot=_;A!==D;)R[ot++]=F[A++];return R}}}function it(d){var x=w(d);if(x)return x;throw new Error(`Non-${t} character`)}return{encode:u,decodeUnsafe:w,decode:it}}var yr=br,Er=yr,Oe=Er;var _t=class{name;prefix;baseEncode;constructor(t,e,n){this.name=t,this.prefix=e,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},Pt=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,e,n){this.name=t,this.prefix=e;let i=e.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return Te(this,t)}},zt=class{decoders;constructor(t){this.decoders=t}or(t){return Te(this,t)}decode(t){let e=t[0],n=this.decoders[e];if(n!=null)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function Te(r,t){return new zt({...r.decoders??{[r.prefix]:r},...t.decoders??{[t.prefix]:t}})}var $t=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,e,n,i){this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=i,this.encoder=new _t(t,e,n),this.decoder=new Pt(t,e,i)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function q({name:r,prefix:t,encode:e,decode:n}){return new $t(r,t,e,n)}function k({name:r,prefix:t,alphabet:e}){let{encode:n,decode:i}=Oe(e,r);return q({prefix:t,name:r,encode:n,decode:o=>B(i(o))})}function Lr(r,t,e,n){let i={};for(let l=0;l<t.length;++l)i[t[l]]=l;let o=r.length;for(;r[o-1]==="=";)--o;let s=new Uint8Array(o*e/8|0),a=0,c=0,f=0;for(let l=0;l<o;++l){let u=i[r[l]];if(u===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<e|u,a+=e,a>=8&&(a-=8,s[f++]=255&c>>a)}if(a>=e||255&c<<8-a)throw new SyntaxError("Unexpected end of data");return s}function Sr(r,t,e){let n=t[t.length-1]==="=",i=(1<<e)-1,o="",s=0,a=0;for(let c=0;c<r.length;++c)for(a=a<<8|r[c],s+=8;s>e;)s-=e,o+=t[i&a>>s];if(s!==0&&(o+=t[i&a<<e-s]),n)for(;o.length*e&7;)o+="=";return o}function p({name:r,prefix:t,bitsPerChar:e,alphabet:n}){return q({prefix:t,name:r,encode(i){return Sr(i,n,e)},decode(i){return Lr(i,n,e,r)}})}var Ar=k({prefix:"9",name:"base10",alphabet:"0123456789"});var qt={};y(qt,{base16:()=>Nr,base16upper:()=>Ir});var Nr=p({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Ir=p({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Kt={};y(Kt,{base2:()=>Ur});var Ur=p({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Xt={};y(Xt,{base256emoji:()=>Rr});var Be=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),Dr=Be.reduce((r,t,e)=>(r[e]=t,r),[]),Fr=Be.reduce((r,t,e)=>{let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);return r[n]=e,r},[]);function vr(r){return r.reduce((t,e)=>(t+=Dr[e],t),"")}function Cr(r){let t=[];for(let e of r){let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);let i=Fr[n];if(i==null)throw new Error(`Non-base256emoji character: ${e}`);t.push(i)}return new Uint8Array(t)}var Rr=q({prefix:"\u{1F680}",name:"base256emoji",encode:vr,decode:Cr});var jt={};y(jt,{base32:()=>K,base32hex:()=>Mr,base32hexpad:()=>Vr,base32hexpadupper:()=>_r,base32hexupper:()=>kr,base32pad:()=>Tr,base32padupper:()=>Br,base32upper:()=>Or,base32z:()=>Pr});var K=p({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Or=p({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Tr=p({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Br=p({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Mr=p({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),kr=p({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Vr=p({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),_r=p({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Pr=p({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Wt={};y(Wt,{base36:()=>H,base36upper:()=>zr});var H=k({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),zr=k({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Jt={};y(Jt,{base58btc:()=>C,base58flickr:()=>$r});var C=k({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),$r=k({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ht={};y(Ht,{base64:()=>Gr,base64pad:()=>qr,base64url:()=>Kr,base64urlpad:()=>Xr});var Gr=p({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),qr=p({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Kr=p({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Xr=p({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Qt={};y(Qt,{base8:()=>jr});var jr=p({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Zt={};y(Zt,{identity:()=>Wr});var Wr=q({prefix:"\0",name:"identity",encode:r=>Ce(r),decode:r=>ve(r)});var Ci=new TextEncoder,Ri=new TextDecoder;var te={};y(te,{identity:()=>xn});var Qr=Ve,Me=128,Zr=127,Yr=~Zr,tn=Math.pow(2,31);function Ve(r,t,e){t=t||[],e=e||0;for(var n=e;r>=tn;)t[e++]=r&255|Me,r/=128;for(;r&Yr;)t[e++]=r&255|Me,r>>>=7;return t[e]=r|0,Ve.bytes=e-n+1,t}var en=Yt,rn=128,ke=127;function Yt(r,n){var e=0,n=n||0,i=0,o=n,s,a=r.length;do{if(o>=a)throw Yt.bytes=0,new RangeError("Could not decode varint");s=r[o++],e+=i<28?(s&ke)<<i:(s&ke)*Math.pow(2,i),i+=7}while(s>=rn);return Yt.bytes=o-n,e}var nn=Math.pow(2,7),on=Math.pow(2,14),sn=Math.pow(2,21),an=Math.pow(2,28),cn=Math.pow(2,35),hn=Math.pow(2,42),un=Math.pow(2,49),fn=Math.pow(2,56),ln=Math.pow(2,63),dn=function(r){return r<nn?1:r<on?2:r<sn?3:r<an?4:r<cn?5:r<hn?6:r<un?7:r<fn?8:r<ln?9:10},pn={encode:Qr,decode:en,encodingLength:dn},mn=pn,Q=mn;function Z(r,t=0){return[Q.decode(r,t),Q.decode.bytes]}function X(r,t,e=0){return Q.encode(r,t,e),t}function j(r){return Q.encodingLength(r)}function P(r,t){let e=t.byteLength,n=j(r),i=n+j(e),o=new Uint8Array(i+e);return X(r,o,0),X(e,o,n),o.set(t,i),new W(r,e,t,o)}function _e(r){let t=B(r),[e,n]=Z(t),[i,o]=Z(t.subarray(n)),s=t.subarray(n+o);if(s.byteLength!==i)throw new Error("Incorrect length");return new W(e,i,s,t)}function Pe(r,t){if(r===t)return!0;{let e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&Fe(r.bytes,e.bytes)}}var W=class{code;size;digest;bytes;constructor(t,e,n,i){this.code=t,this.size=e,this.digest=n,this.bytes=i}};var ze=0,gn="identity",$e=B;function wn(r){return P(ze,$e(r))}var xn={code:ze,name:gn,encode:$e,digest:wn};var ne={};y(ne,{sha256:()=>bn,sha512:()=>yn});function re({name:r,code:t,encode:e}){return new ee(r,t,e)}var ee=class{name;code;encode;constructor(t,e,n){this.name=t,this.code=e,this.encode=n}digest(t){if(t instanceof Uint8Array){let e=this.encode(t);return e instanceof Uint8Array?P(this.code,e):e.then(n=>P(this.code,n))}else throw Error("Unknown type, must be binary type")}};function qe(r){return async t=>new Uint8Array(await crypto.subtle.digest(r,t))}var bn=re({name:"sha2-256",code:18,encode:qe("SHA-256")}),yn=re({name:"sha2-512",code:19,encode:qe("SHA-512")});function Ke(r,t){let{bytes:e,version:n}=r;switch(n){case 0:return Ln(e,ie(r),t??C.encoder);default:return Sn(e,ie(r),t??K.encoder)}}var Xe=new WeakMap;function ie(r){let t=Xe.get(r);if(t==null){let e=new Map;return Xe.set(r,e),e}return t}var wt=class r{code;version;multihash;bytes;"/";constructor(t,e,n,i){this.code=e,this.version=t,this.multihash=n,this.bytes=i,this["/"]=i}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:t,multihash:e}=this;if(t!==tt)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==An)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(e)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:t,digest:e}=this.multihash,n=P(t,e);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return r.equals(this,t)}static equals(t,e){let n=e;return n!=null&&t.code===n.code&&t.version===n.version&&Pe(t.multihash,n.multihash)}toString(t){return Ke(this,t)}toJSON(){return{"/":Ke(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let e=t;if(e instanceof r)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){let{version:n,code:i,multihash:o,bytes:s}=e;return new r(n,i,o,s??je(n,i,o.bytes))}else if(e[Nn]===!0){let{version:n,multihash:i,code:o}=e,s=_e(i);return r.create(n,o,s)}else return null}static create(t,e,n){if(typeof e!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(e!==tt)throw new Error(`Version 0 CID must use dag-pb (code: ${tt}) block encoding`);return new r(t,e,n,n.bytes)}case 1:{let i=je(t,e,n.bytes);return new r(t,e,n,i)}default:throw new Error("Invalid version")}}static createV0(t){return r.create(0,tt,t)}static createV1(t,e){return r.create(1,t,e)}static decode(t){let[e,n]=r.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return e}static decodeFirst(t){let e=r.inspectBytes(t),n=e.size-e.multihashSize,i=B(t.subarray(n,n+e.multihashSize));if(i.byteLength!==e.multihashSize)throw new Error("Incorrect length");let o=i.subarray(e.multihashSize-e.digestSize),s=new W(e.multihashCode,e.digestSize,o,i);return[e.version===0?r.createV0(s):r.createV1(e.codec,s),t.subarray(e.size)]}static inspectBytes(t){let e=0,n=()=>{let[u,w]=Z(t.subarray(e));return e+=w,u},i=n(),o=tt;if(i===18?(i=0,e=0):o=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let s=e,a=n(),c=n(),f=e+c,l=f-s;return{version:i,codec:o,multihashCode:a,digestSize:c,multihashSize:l,size:f}}static parse(t,e){let[n,i]=En(t,e),o=r.decode(i);if(o.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return ie(o).set(n,t),o}};function En(r,t){switch(r[0]){case"Q":{let e=t??C;return[C.prefix,e.decode(`${C.prefix}${r}`)]}case C.prefix:{let e=t??C;return[C.prefix,e.decode(r)]}case K.prefix:{let e=t??K;return[K.prefix,e.decode(r)]}case H.prefix:{let e=t??H;return[H.prefix,e.decode(r)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],t.decode(r)]}}}function Ln(r,t,e){let{prefix:n}=e;if(n!==C.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);let i=t.get(n);if(i==null){let o=e.encode(r).slice(1);return t.set(n,o),o}else return i}function Sn(r,t,e){let{prefix:n}=e,i=t.get(n);if(i==null){let o=e.encode(r);return t.set(n,o),o}else return i}var tt=112,An=18;function je(r,t,e){let n=j(r),i=n+j(t),o=new Uint8Array(i+e.byteLength);return X(r,o,0),X(t,o,n),o.set(e,i),o}var Nn=Symbol.for("@ipld/js-cid/CID");var oe={...Zt,...Kt,...Qt,...Gt,...qt,...jt,...Wt,...Jt,...Ht,...Xt},to={...ne,...te};function Je(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var We=Je("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),se=Je("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=b(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),In={utf8:We,"utf-8":We,hex:oe.base16,latin1:se,ascii:se,binary:se,...oe},xt=In;function bt(r,t="utf8"){let e=xt[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.decoder.decode(`${e.prefix}${r}`)}function He(r,t="utf8"){let e=xt[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.encoder.encode(r).substring(1)}var Qe="0.0.1",Ze="fetch";var ae=new Float32Array([-0]),V=new Uint8Array(ae.buffer);function Ye(r,t,e){ae[0]=r,t[e]=V[0],t[e+1]=V[1],t[e+2]=V[2],t[e+3]=V[3]}function tr(r,t){return V[0]=r[t],V[1]=r[t+1],V[2]=r[t+2],V[3]=r[t+3],ae[0]}var ce=new Float64Array([-0]),g=new Uint8Array(ce.buffer);function er(r,t,e){ce[0]=r,t[e]=g[0],t[e+1]=g[1],t[e+2]=g[2],t[e+3]=g[3],t[e+4]=g[4],t[e+5]=g[5],t[e+6]=g[6],t[e+7]=g[7]}function rr(r,t){return g[0]=r[t],g[1]=r[t+1],g[2]=r[t+2],g[3]=r[t+3],g[4]=r[t+4],g[5]=r[t+5],g[6]=r[t+6],g[7]=r[t+7],ce[0]}var Un=BigInt(Number.MAX_SAFE_INTEGER),Dn=BigInt(Number.MIN_SAFE_INTEGER),E=class r{lo;hi;constructor(t,e){this.lo=t|0,this.hi=e|0}toNumber(t=!1){if(!t&&this.hi>>>31>0){let e=~this.lo+1>>>0,n=~this.hi>>>0;return e===0&&(n=n+1>>>0),-(e+n*4294967296)}return this.lo+this.hi*4294967296}toBigInt(t=!1){if(t)return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n);if(this.hi>>>31){let e=~this.lo+1>>>0,n=~this.hi>>>0;return e===0&&(n=n+1>>>0),-(BigInt(e)+(BigInt(n)<<32n))}return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n)}toString(t=!1){return this.toBigInt(t).toString()}zzEncode(){let t=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^t)>>>0,this.lo=(this.lo<<1^t)>>>0,this}zzDecode(){let t=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^t)>>>0,this.hi=(this.hi>>>1^t)>>>0,this}length(){let t=this.lo,e=(this.lo>>>28|this.hi<<4)>>>0,n=this.hi>>>24;return n===0?e===0?t<16384?t<128?1:2:t<2097152?3:4:e<16384?e<128?5:6:e<2097152?7:8:n<128?9:10}static fromBigInt(t){if(t===0n)return z;if(t<Un&&t>Dn)return this.fromNumber(Number(t));let e=t<0n;e&&(t=-t);let n=t>>32n,i=t-(n<<32n);return e&&(n=~n|0n,i=~i|0n,++i>nr&&(i=0n,++n>nr&&(n=0n))),new r(Number(i),Number(n))}static fromNumber(t){if(t===0)return z;let e=t<0;e&&(t=-t);let n=t>>>0,i=(t-n)/4294967296>>>0;return e&&(i=~i>>>0,n=~n>>>0,++n>4294967295&&(n=0,++i>4294967295&&(i=0))),new r(n,i)}static from(t){return typeof t=="number"?r.fromNumber(t):typeof t=="bigint"?r.fromBigInt(t):typeof t=="string"?r.fromBigInt(BigInt(t)):t.low!=null||t.high!=null?new r(t.low>>>0,t.high>>>0):z}},z=new E(0,0);z.toBigInt=function(){return 0n};z.zzEncode=z.zzDecode=function(){return this};z.length=function(){return 1};var nr=4294967296n;function ir(r){let t=0,e=0;for(let n=0;n<r.length;++n)e=r.charCodeAt(n),e<128?t+=1:e<2048?t+=2:(e&64512)===55296&&(r.charCodeAt(n+1)&64512)===56320?(++n,t+=4):t+=3;return t}function or(r,t,e){if(e-t<1)return"";let i,o=[],s=0,a;for(;t<e;)a=r[t++],a<128?o[s++]=a:a>191&&a<224?o[s++]=(a&31)<<6|r[t++]&63:a>239&&a<365?(a=((a&7)<<18|(r[t++]&63)<<12|(r[t++]&63)<<6|r[t++]&63)-65536,o[s++]=55296+(a>>10),o[s++]=56320+(a&1023)):o[s++]=(a&15)<<12|(r[t++]&63)<<6|r[t++]&63,s>8191&&((i??(i=[])).push(String.fromCharCode.apply(String,o)),s=0);return i!=null?(s>0&&i.push(String.fromCharCode.apply(String,o.slice(0,s))),i.join("")):String.fromCharCode.apply(String,o.slice(0,s))}function he(r,t,e){let n=e,i,o;for(let s=0;s<r.length;++s)i=r.charCodeAt(s),i<128?t[e++]=i:i<2048?(t[e++]=i>>6|192,t[e++]=i&63|128):(i&64512)===55296&&((o=r.charCodeAt(s+1))&64512)===56320?(i=65536+((i&1023)<<10)+(o&1023),++s,t[e++]=i>>18|240,t[e++]=i>>12&63|128,t[e++]=i>>6&63|128,t[e++]=i&63|128):(t[e++]=i>>12|224,t[e++]=i>>6&63|128,t[e++]=i&63|128);return e-n}function I(r,t){return RangeError(`index out of range: ${r.pos} + ${t??1} > ${r.len}`)}function yt(r,t){return(r[t-4]|r[t-3]<<8|r[t-2]<<16|r[t-1]<<24)>>>0}var ue=class{buf;pos;len;_slice=Uint8Array.prototype.subarray;constructor(t){this.buf=t,this.pos=0,this.len=t.length}uint32(){let t=4294967295;if(t=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(t=(t|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return t;if((this.pos+=5)>this.len)throw this.pos=this.len,I(this,10);return t}int32(){return this.uint32()|0}sint32(){let t=this.uint32();return t>>>1^-(t&1)|0}bool(){return this.uint32()!==0}fixed32(){if(this.pos+4>this.len)throw I(this,4);return yt(this.buf,this.pos+=4)}sfixed32(){if(this.pos+4>this.len)throw I(this,4);return yt(this.buf,this.pos+=4)|0}float(){if(this.pos+4>this.len)throw I(this,4);let t=tr(this.buf,this.pos);return this.pos+=4,t}double(){if(this.pos+8>this.len)throw I(this,4);let t=rr(this.buf,this.pos);return this.pos+=8,t}bytes(){let t=this.uint32(),e=this.pos,n=this.pos+t;if(n>this.len)throw I(this,t);return this.pos+=t,e===n?new Uint8Array(0):this.buf.subarray(e,n)}string(){let t=this.bytes();return or(t,0,t.length)}skip(t){if(typeof t=="number"){if(this.pos+t>this.len)throw I(this,t);this.pos+=t}else do if(this.pos>=this.len)throw I(this);while(this.buf[this.pos++]&128);return this}skipType(t){switch(t){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(t=this.uint32()&7)!==4;)this.skipType(t);break;case 5:this.skip(4);break;default:throw Error(`invalid wire type ${t} at offset ${this.pos}`)}return this}readLongVarint(){let t=new E(0,0),e=0;if(this.len-this.pos>4){for(;e<4;++e)if(t.lo=(t.lo|(this.buf[this.pos]&127)<<e*7)>>>0,this.buf[this.pos++]<128)return t;if(t.lo=(t.lo|(this.buf[this.pos]&127)<<28)>>>0,t.hi=(t.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return t;e=0}else{for(;e<3;++e){if(this.pos>=this.len)throw I(this);if(t.lo=(t.lo|(this.buf[this.pos]&127)<<e*7)>>>0,this.buf[this.pos++]<128)return t}return t.lo=(t.lo|(this.buf[this.pos++]&127)<<e*7)>>>0,t}if(this.len-this.pos>4){for(;e<5;++e)if(t.hi=(t.hi|(this.buf[this.pos]&127)<<e*7+3)>>>0,this.buf[this.pos++]<128)return t}else for(;e<5;++e){if(this.pos>=this.len)throw I(this);if(t.hi=(t.hi|(this.buf[this.pos]&127)<<e*7+3)>>>0,this.buf[this.pos++]<128)return t}throw Error("invalid varint encoding")}readFixed64(){if(this.pos+8>this.len)throw I(this,8);let t=yt(this.buf,this.pos+=4),e=yt(this.buf,this.pos+=4);return new E(t,e)}int64(){return this.readLongVarint().toBigInt()}int64Number(){return this.readLongVarint().toNumber()}int64String(){return this.readLongVarint().toString()}uint64(){return this.readLongVarint().toBigInt(!0)}uint64Number(){let t=kt(this.buf,this.pos);return this.pos+=T(t),t}uint64String(){return this.readLongVarint().toString(!0)}sint64(){return this.readLongVarint().zzDecode().toBigInt()}sint64Number(){return this.readLongVarint().zzDecode().toNumber()}sint64String(){return this.readLongVarint().zzDecode().toString()}fixed64(){return this.readFixed64().toBigInt()}fixed64Number(){return this.readFixed64().toNumber()}fixed64String(){return this.readFixed64().toString()}sfixed64(){return this.readFixed64().toBigInt()}sfixed64Number(){return this.readFixed64().toNumber()}sfixed64String(){return this.readFixed64().toString()}};function fe(r){return new ue(r instanceof Uint8Array?r:r.subarray())}function Et(r,t,e){let n=fe(r);return t.decode(n,void 0,e)}function le(r){let t=r??8192,e=t>>>1,n,i=t;return function(s){if(s<1||s>e)return b(s);i+s>t&&(n=b(t),i=0);let a=n.subarray(i,i+=s);return i&7&&(i=(i|7)+1),a}}var $=class{fn;len;next;val;constructor(t,e,n){this.fn=t,this.len=e,this.next=void 0,this.val=n}};function de(){}var me=class{head;tail;len;next;constructor(t){this.head=t.head,this.tail=t.tail,this.len=t.len,this.next=t.states}},Fn=le();function vn(r){return globalThis.Buffer!=null?b(r):Fn(r)}var rt=class{len;head;tail;states;constructor(){this.len=0,this.head=new $(de,0,0),this.tail=this.head,this.states=null}_push(t,e,n){return this.tail=this.tail.next=new $(t,e,n),this.len+=e,this}uint32(t){return this.len+=(this.tail=this.tail.next=new ge((t=t>>>0)<128?1:t<16384?2:t<2097152?3:t<268435456?4:5,t)).len,this}int32(t){return t<0?this._push(Lt,10,E.fromNumber(t)):this.uint32(t)}sint32(t){return this.uint32((t<<1^t>>31)>>>0)}uint64(t){let e=E.fromBigInt(t);return this._push(Lt,e.length(),e)}uint64Number(t){return this._push(Mt,T(t),t)}uint64String(t){return this.uint64(BigInt(t))}int64(t){return this.uint64(t)}int64Number(t){return this.uint64Number(t)}int64String(t){return this.uint64String(t)}sint64(t){let e=E.fromBigInt(t).zzEncode();return this._push(Lt,e.length(),e)}sint64Number(t){let e=E.fromNumber(t).zzEncode();return this._push(Lt,e.length(),e)}sint64String(t){return this.sint64(BigInt(t))}bool(t){return this._push(pe,1,t?1:0)}fixed32(t){return this._push(et,4,t>>>0)}sfixed32(t){return this.fixed32(t)}fixed64(t){let e=E.fromBigInt(t);return this._push(et,4,e.lo)._push(et,4,e.hi)}fixed64Number(t){let e=E.fromNumber(t);return this._push(et,4,e.lo)._push(et,4,e.hi)}fixed64String(t){return this.fixed64(BigInt(t))}sfixed64(t){return this.fixed64(t)}sfixed64Number(t){return this.fixed64Number(t)}sfixed64String(t){return this.fixed64String(t)}float(t){return this._push(Ye,4,t)}double(t){return this._push(er,8,t)}bytes(t){let e=t.length>>>0;return e===0?this._push(pe,1,0):this.uint32(e)._push(Rn,e,t)}string(t){let e=ir(t);return e!==0?this.uint32(e)._push(he,e,t):this._push(pe,1,0)}fork(){return this.states=new me(this),this.head=this.tail=new $(de,0,0),this.len=0,this}reset(){return this.states!=null?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new $(de,0,0),this.len=0),this}ldelim(){let t=this.head,e=this.tail,n=this.len;return this.reset().uint32(n),n!==0&&(this.tail.next=t.next,this.tail=e,this.len+=n),this}finish(){let t=this.head.next,e=vn(this.len),n=0;for(;t!=null;)t.fn(t.val,e,n),n+=t.len,t=t.next;return e}};function pe(r,t,e){t[e]=r&255}function Cn(r,t,e){for(;r>127;)t[e++]=r&127|128,r>>>=7;t[e]=r}var ge=class extends ${next;constructor(t,e){super(Cn,t,e),this.next=void 0}};function Lt(r,t,e){for(;r.hi!==0;)t[e++]=r.lo&127|128,r.lo=(r.lo>>>7|r.hi<<25)>>>0,r.hi>>>=7;for(;r.lo>127;)t[e++]=r.lo&127|128,r.lo=r.lo>>>7;t[e++]=r.lo}function et(r,t,e){t[e]=r&255,t[e+1]=r>>>8&255,t[e+2]=r>>>16&255,t[e+3]=r>>>24}function Rn(r,t,e){t.set(r,e)}globalThis.Buffer!=null&&(rt.prototype.bytes=function(r){let t=r.length>>>0;return this.uint32(t),t>0&&this._push(On,t,r),this},rt.prototype.string=function(r){let t=globalThis.Buffer.byteLength(r);return this.uint32(t),t>0&&this._push(Tn,t,r),this});function On(r,t,e){t.set(r,e)}function Tn(r,t,e){r.length<40?he(r,t,e):t.utf8Write!=null?t.utf8Write(r,e):t.set(bt(r),e)}function we(){return new rt}function St(r,t){let e=we();return t.encode(r,e,{lengthDelimited:!1}),e.finish()}var J;(function(r){r[r.VARINT=0]="VARINT",r[r.BIT64=1]="BIT64",r[r.LENGTH_DELIMITED=2]="LENGTH_DELIMITED",r[r.START_GROUP=3]="START_GROUP",r[r.END_GROUP=4]="END_GROUP",r[r.BIT32=5]="BIT32"})(J||(J={}));function At(r,t,e,n){return{name:r,type:t,encode:e,decode:n}}function xe(r){function t(i){if(r[i.toString()]==null)throw new Error("Invalid enum value");return r[i]}let e=function(o,s){let a=t(o);s.int32(a)},n=function(o){let s=o.int32();return t(s)};return At("enum",J.VARINT,e,n)}function Nt(r,t){return At("message",J.LENGTH_DELIMITED,r,t)}var nt;(function(r){let t;r.codec=()=>(t==null&&(t=Nt((e,n,i={})=>{i.lengthDelimited!==!1&&n.fork(),e.identifier!=null&&e.identifier!==""&&(n.uint32(10),n.string(e.identifier)),i.lengthDelimited!==!1&&n.ldelim()},(e,n,i={})=>{let o={identifier:""},s=n==null?e.len:e.pos+n;for(;e.pos<s;){let a=e.uint32();switch(a>>>3){case 1:{o.identifier=e.string();break}default:{e.skipType(a&7);break}}}return o})),t),r.encode=e=>St(e,r.codec()),r.decode=(e,n)=>Et(e,r.codec(),n)})(nt||(nt={}));var U;(function(r){let t;(function(i){i.OK="OK",i.NOT_FOUND="NOT_FOUND",i.ERROR="ERROR"})(t=r.StatusCode||(r.StatusCode={}));let e;(function(i){i[i.OK=0]="OK",i[i.NOT_FOUND=1]="NOT_FOUND",i[i.ERROR=2]="ERROR"})(e||(e={})),function(i){i.codec=()=>xe(e)}(t=r.StatusCode||(r.StatusCode={}));let n;r.codec=()=>(n==null&&(n=Nt((i,o,s={})=>{s.lengthDelimited!==!1&&o.fork(),i.status!=null&&e[i.status]!==0&&(o.uint32(8),r.StatusCode.codec().encode(i.status,o)),i.data!=null&&i.data.byteLength>0&&(o.uint32(18),o.bytes(i.data)),s.lengthDelimited!==!1&&o.ldelim()},(i,o,s={})=>{let a={status:t.OK,data:N(0)},c=o==null?i.len:i.pos+o;for(;i.pos<c;){let f=i.uint32();switch(f>>>3){case 1:{a.status=r.StatusCode.codec().decode(i);break}case 2:{a.data=i.bytes();break}default:{i.skipType(f&7);break}}}return a})),n),r.encode=i=>St(i,r.codec()),r.decode=(i,o)=>Et(i,r.codec(),o)})(U||(U={}));var ar=1e4,It=class{protocol;components;lookupFunctions;started;init;log;constructor(t,e={}){this.log=t.logger.forComponent("libp2p:fetch"),this.started=!1,this.components=t,this.protocol=`/${e.protocolPrefix??"libp2p"}/${Ze}/${Qe}`,this.lookupFunctions=new Map,this.handleMessage=this.handleMessage.bind(this),this.init=e}[Symbol.toStringTag]="@libp2p/fetch";async start(){await this.components.registrar.handle(this.protocol,t=>{this.handleMessage(t).then(async()=>{await t.stream.close()}).catch(e=>{this.log.error(e)})},{maxInboundStreams:this.init.maxInboundStreams,maxOutboundStreams:this.init.maxOutboundStreams}),this.started=!0}async stop(){await this.components.registrar.unhandle(this.protocol),this.started=!1}isStarted(){return this.started}async fetch(t,e,n={}){this.log("dialing %s to %p",this.protocol,t);let i=await this.components.connectionManager.openConnection(t,n),o=n.signal,s,a=()=>{};if(o==null){let c=this.init.timeout??ar;this.log("using default timeout of %d ms",c),o=AbortSignal.timeout(c),be(1/0,o)}try{s=await i.newStream(this.protocol,{signal:o}),a=()=>{s?.abort(new st)},o.addEventListener("abort",a,{once:!0}),this.log("fetch %s",e);let c=Vt(s);await c.write({identifier:e},nt,n);let f=await c.read(U,n);switch(await c.unwrap().close(n),f.status){case U.StatusCode.OK:return this.log("received status for %s ok",e),f.data;case U.StatusCode.NOT_FOUND:{this.log("received status for %s not found",e);return}case U.StatusCode.ERROR:{this.log("received status for %s error",e);let l=He(f.data);throw new ht("Error in fetch protocol response: "+l)}default:throw this.log("received status for %s unknown",e),new ct("Unknown response status")}}catch(c){throw s?.abort(c),c}finally{o.removeEventListener("abort",a),s!=null&&await s.close()}}async handleMessage(t){let{stream:e}=t,n=AbortSignal.timeout(this.init.timeout??ar);try{let i=Vt(e),o=await i.read(nt,{signal:n}),s,a=this._getLookupFunction(o.identifier);if(a!=null){this.log("look up data with identifier %s",o.identifier);let c=await a(o.identifier);c!=null?(this.log("sending status for %s ok",o.identifier),s={status:U.StatusCode.OK,data:c}):(this.log("sending status for %s not found",o.identifier),s={status:U.StatusCode.NOT_FOUND,data:new Uint8Array(0)})}else{this.log("sending status for %s error",o.identifier);let c=bt(`No lookup function registered for key: ${o.identifier}`);s={status:U.StatusCode.ERROR,data:c}}await i.write(s,U,{signal:n}),await i.unwrap().close({signal:n})}catch(i){this.log("error answering fetch request",i),e.abort(i)}}_getLookupFunction(t){for(let e of this.lookupFunctions.keys())if(t.startsWith(e))return this.lookupFunctions.get(e)}registerLookupFunction(t,e){if(this.lookupFunctions.has(t))throw new at(`Fetch protocol handler for key prefix '${t}' already registered`);this.lookupFunctions.set(t,e)}unregisterLookupFunction(t,e){e!=null&&this.lookupFunctions.get(t)!==e||this.lookupFunctions.delete(t)}};function Bn(r={}){return t=>new It(t,r)}return lr(Mn);})(); | ||
"use strict";var Libp2PFetch=(()=>{var Ut=Object.defineProperty;var cr=Object.getOwnPropertyDescriptor;var hr=Object.getOwnPropertyNames;var ur=Object.prototype.hasOwnProperty;var y=(r,t)=>{for(var e in t)Ut(r,e,{get:t[e],enumerable:!0})},fr=(r,t,e,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let i of hr(t))!ur.call(r,i)&&i!==e&&Ut(r,i,{get:()=>t[i],enumerable:!(n=cr(t,i))||n.enumerable});return r};var lr=r=>fr(Ut({},"__esModule",{value:!0}),r);var Mn={};y(Mn,{fetch:()=>Bn});var at=class extends Error{static name="AbortError";constructor(t="The operation was aborted"){super(t),this.name="AbortError"}};var ct=class extends Error{static name="InvalidParametersError";constructor(t="Invalid parameters"){super(t),this.name="InvalidParametersError"}};var ht=class extends Error{static name="InvalidMessageError";constructor(t="Invalid message"){super(t),this.name="InvalidMessageError"}},ut=class extends Error{static name="ProtocolError";constructor(t="Protocol error"){super(t),this.name="ProtocolError"}};var ye=(r,...t)=>{try{[...t]}catch{}};function G(){let r={};return r.promise=new Promise((t,e)=>{r.resolve=t,r.reject=e}),r}var ft=class extends Error{type;code;constructor(t,e,n){super(t??"The operation was aborted"),this.type="aborted",this.name=n??"AbortError",this.code=e??"ABORT_ERR"}};async function Ee(r,t,e){if(t==null)return r;if(t.aborted)return Promise.reject(new ft(e?.errorMessage,e?.errorCode,e?.errorName));let n,i=new ft(e?.errorMessage,e?.errorCode,e?.errorName);try{return await Promise.race([r,new Promise((o,s)=>{n=()=>{s(i)},t.addEventListener("abort",n)})])}finally{n!=null&&t.removeEventListener("abort",n)}}var Dt=class{readNext;haveNext;ended;nextResult;constructor(){this.ended=!1,this.readNext=G(),this.haveNext=G()}[Symbol.asyncIterator](){return this}async next(){if(this.nextResult==null&&await this.haveNext.promise,this.nextResult==null)throw new Error("HaveNext promise resolved but nextResult was undefined");let t=this.nextResult;return this.nextResult=void 0,this.readNext.resolve(),this.readNext=G(),t}async throw(t){return this.ended=!0,t!=null&&(this.haveNext.promise.catch(()=>{}),this.haveNext.reject(t)),{done:!0,value:void 0}}async return(){let t={done:!0,value:void 0};return await this._push(void 0),t}async push(t,e){await this._push(t,e)}async end(t,e){t!=null?await this.throw(t):await this._push(void 0,e)}async _push(t,e){if(t!=null&&this.ended)throw new Error("Cannot push value onto an ended pushable");for(;this.nextResult!=null;)await this.readNext.promise;t!=null?this.nextResult={done:!1,value:t}:(this.ended=!0,this.nextResult={done:!0,value:void 0}),this.haveNext.resolve(),this.haveNext=G(),await Ee(this.readNext.promise,e?.signal,e)}};function Le(){return new Dt}function S(r=0){return new Uint8Array(r)}function b(r=0){return new Uint8Array(r)}function Ft(r,t){t==null&&(t=r.reduce((i,o)=>i+o.length,0));let e=b(t),n=0;for(let i of r)e.set(i,n),n+=i.length;return e}function Se(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}var Ne=Symbol.for("@achingbrain/uint8arraylist");function Ae(r,t){if(t==null||t<0)throw new RangeError("index is out of bounds");let e=0;for(let n of r){let i=e+n.byteLength;if(t<i)return{buf:n,index:t-e};e=i}throw new RangeError("index is out of bounds")}function lt(r){return!!r?.[Ne]}var C=class r{bufs;length;[Ne]=!0;constructor(...t){this.bufs=[],this.length=0,t.length>0&&this.appendAll(t)}*[Symbol.iterator](){yield*this.bufs}get byteLength(){return this.length}append(...t){this.appendAll(t)}appendAll(t){let e=0;for(let n of t)if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.push(n);else if(lt(n))e+=n.byteLength,this.bufs.push(...n.bufs);else throw new Error("Could not append value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}prepend(...t){this.prependAll(t)}prependAll(t){let e=0;for(let n of t.reverse())if(n instanceof Uint8Array)e+=n.byteLength,this.bufs.unshift(n);else if(lt(n))e+=n.byteLength,this.bufs.unshift(...n.bufs);else throw new Error("Could not prepend value, must be an Uint8Array or a Uint8ArrayList");this.length+=e}get(t){let e=Ae(this.bufs,t);return e.buf[e.index]}set(t,e){let n=Ae(this.bufs,t);n.buf[n.index]=e}write(t,e=0){if(t instanceof Uint8Array)for(let n=0;n<t.length;n++)this.set(e+n,t[n]);else if(lt(t))for(let n=0;n<t.length;n++)this.set(e+n,t.get(n));else throw new Error("Could not write value, must be an Uint8Array or a Uint8ArrayList")}consume(t){if(t=Math.trunc(t),!(Number.isNaN(t)||t<=0)){if(t===this.byteLength){this.bufs=[],this.length=0;return}for(;this.bufs.length>0;)if(t>=this.bufs[0].byteLength)t-=this.bufs[0].byteLength,this.length-=this.bufs[0].byteLength,this.bufs.shift();else{this.bufs[0]=this.bufs[0].subarray(t),this.length-=t;break}}}slice(t,e){let{bufs:n,length:i}=this._subList(t,e);return Ft(n,i)}subarray(t,e){let{bufs:n,length:i}=this._subList(t,e);return n.length===1?n[0]:Ft(n,i)}sublist(t,e){let{bufs:n,length:i}=this._subList(t,e),o=new r;return o.length=i,o.bufs=[...n],o}_subList(t,e){if(t=t??0,e=e??this.length,t<0&&(t=this.length+t),e<0&&(e=this.length+e),t<0||e>this.length)throw new RangeError("index is out of bounds");if(t===e)return{bufs:[],length:0};if(t===0&&e===this.length)return{bufs:this.bufs,length:this.length};let n=[],i=0;for(let o=0;o<this.bufs.length;o++){let s=this.bufs[o],a=i,c=a+s.byteLength;if(i=c,t>=c)continue;let h=t>=a&&t<c,l=e>a&&e<=c;if(h&&l){if(t===a&&e===c){n.push(s);break}let f=t-a;n.push(s.subarray(f,f+(e-t)));break}if(h){if(t===0){n.push(s);continue}n.push(s.subarray(t-a));continue}if(l){if(e===c){n.push(s);break}n.push(s.subarray(0,e-a));break}n.push(s)}return{bufs:n,length:e-t}}indexOf(t,e=0){if(!lt(t)&&!(t instanceof Uint8Array))throw new TypeError('The "value" argument must be a Uint8ArrayList or Uint8Array');let n=t instanceof Uint8Array?t:t.subarray();if(e=Number(e??0),isNaN(e)&&(e=0),e<0&&(e=this.length+e),e<0&&(e=0),t.length===0)return e>this.length?this.length:e;let i=n.byteLength;if(i===0)throw new TypeError("search must be at least 1 byte long");let o=256,s=new Int32Array(o);for(let f=0;f<o;f++)s[f]=-1;for(let f=0;f<i;f++)s[n[f]]=f;let a=s,c=this.byteLength-n.byteLength,h=n.byteLength-1,l;for(let f=e;f<=c;f+=l){l=0;for(let w=h;w>=0;w--){let ot=this.get(f+w);if(n[w]!==ot){l=Math.max(1,w-a[ot]);break}}if(l===0)return f}return-1}getInt8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getInt8(0)}setInt8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setInt8(0,e),this.write(n,t)}getInt16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt16(0,e)}setInt16(t,e,n){let i=S(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt16(0,e,n),this.write(i,t)}getInt32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getInt32(0,e)}setInt32(t,e,n){let i=S(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setInt32(0,e,n),this.write(i,t)}getBigInt64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigInt64(0,e)}setBigInt64(t,e,n){let i=S(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigInt64(0,e,n),this.write(i,t)}getUint8(t){let e=this.subarray(t,t+1);return new DataView(e.buffer,e.byteOffset,e.byteLength).getUint8(0)}setUint8(t,e){let n=b(1);new DataView(n.buffer,n.byteOffset,n.byteLength).setUint8(0,e),this.write(n,t)}getUint16(t,e){let n=this.subarray(t,t+2);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint16(0,e)}setUint16(t,e,n){let i=S(2);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint16(0,e,n),this.write(i,t)}getUint32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getUint32(0,e)}setUint32(t,e,n){let i=S(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setUint32(0,e,n),this.write(i,t)}getBigUint64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getBigUint64(0,e)}setBigUint64(t,e,n){let i=S(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setBigUint64(0,e,n),this.write(i,t)}getFloat32(t,e){let n=this.subarray(t,t+4);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat32(0,e)}setFloat32(t,e,n){let i=S(4);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat32(0,e,n),this.write(i,t)}getFloat64(t,e){let n=this.subarray(t,t+8);return new DataView(n.buffer,n.byteOffset,n.byteLength).getFloat64(0,e)}setFloat64(t,e,n){let i=S(8);new DataView(i.buffer,i.byteOffset,i.byteLength).setFloat64(0,e,n),this.write(i,t)}equals(t){if(t==null||!(t instanceof r)||t.bufs.length!==this.bufs.length)return!1;for(let e=0;e<this.bufs.length;e++)if(!Se(this.bufs[e],t.bufs[e]))return!1;return!0}static fromUint8Arrays(t,e){let n=new r;return n.bufs=t,e==null&&(e=t.reduce((i,o)=>i+o.byteLength,0)),n.length=e,n}};var dt=class extends Error{name="UnexpectedEOFError";code="ERR_UNEXPECTED_EOF"};var vt=class extends Error{code;constructor(t,e){super(t),this.code=e}},Rt=class extends vt{type;constructor(t){super(t,"ABORT_ERR"),this.type="aborted",this.name="AbortError"}};function Ie(r,t){let e=Le();r.sink(e).catch(async s=>{await e.end(s)}),r.sink=async s=>{for await(let a of s)await e.push(a);await e.end()};let n=r.source;r.source[Symbol.iterator]!=null?n=r.source[Symbol.iterator]():r.source[Symbol.asyncIterator]!=null&&(n=r.source[Symbol.asyncIterator]());let i=new C;return{read:async(s,a)=>{a?.signal?.throwIfAborted();let c,h=new Promise((l,f)=>{c=()=>{f(new Rt("Read aborted"))},a?.signal?.addEventListener("abort",c)});try{if(s==null){let{done:f,value:w}=await Promise.race([n.next(),h]);return f===!0?new C:w}for(;i.byteLength<s;){let{value:f,done:w}=await Promise.race([n.next(),h]);if(w===!0)throw new dt("unexpected end of input");i.append(f)}let l=i.sublist(0,s);return i.consume(s),l}finally{c!=null&&a?.signal?.removeEventListener("abort",c)}},write:async(s,a)=>{a?.signal?.throwIfAborted(),s instanceof Uint8Array?await e.push(s,a):await e.push(s.subarray(),a)},unwrap:()=>{if(i.byteLength>0){let s=r.source;r.source=async function*(){t?.yieldBytes===!1?yield i:yield*i,yield*s}()}return r}}}var dr=Math.pow(2,7),pr=Math.pow(2,14),mr=Math.pow(2,21),Ot=Math.pow(2,28),Ct=Math.pow(2,35),Tt=Math.pow(2,42),Bt=Math.pow(2,49),u=128,m=127;function T(r){if(r<dr)return 1;if(r<pr)return 2;if(r<mr)return 3;if(r<Ot)return 4;if(r<Ct)return 5;if(r<Tt)return 6;if(r<Bt)return 7;if(Number.MAX_SAFE_INTEGER!=null&&r>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function Mt(r,t,e=0){switch(T(r)){case 8:t[e++]=r&255|u,r/=128;case 7:t[e++]=r&255|u,r/=128;case 6:t[e++]=r&255|u,r/=128;case 5:t[e++]=r&255|u,r/=128;case 4:t[e++]=r&255|u,r>>>=7;case 3:t[e++]=r&255|u,r>>>=7;case 2:t[e++]=r&255|u,r>>>=7;case 1:{t[e++]=r&255,r>>>=7;break}default:throw new Error("unreachable")}return t}function gr(r,t,e=0){switch(T(r)){case 8:t.set(e++,r&255|u),r/=128;case 7:t.set(e++,r&255|u),r/=128;case 6:t.set(e++,r&255|u),r/=128;case 5:t.set(e++,r&255|u),r/=128;case 4:t.set(e++,r&255|u),r>>>=7;case 3:t.set(e++,r&255|u),r>>>=7;case 2:t.set(e++,r&255|u),r>>>=7;case 1:{t.set(e++,r&255),r>>>=7;break}default:throw new Error("unreachable")}return t}function kt(r,t){let e=r[t],n=0;if(n+=e&m,e<u||(e=r[t+1],n+=(e&m)<<7,e<u)||(e=r[t+2],n+=(e&m)<<14,e<u)||(e=r[t+3],n+=(e&m)<<21,e<u)||(e=r[t+4],n+=(e&m)*Ot,e<u)||(e=r[t+5],n+=(e&m)*Ct,e<u)||(e=r[t+6],n+=(e&m)*Tt,e<u)||(e=r[t+7],n+=(e&m)*Bt,e<u))return n;throw new RangeError("Could not decode varint")}function wr(r,t){let e=r.get(t),n=0;if(n+=e&m,e<u||(e=r.get(t+1),n+=(e&m)<<7,e<u)||(e=r.get(t+2),n+=(e&m)<<14,e<u)||(e=r.get(t+3),n+=(e&m)<<21,e<u)||(e=r.get(t+4),n+=(e&m)*Ot,e<u)||(e=r.get(t+5),n+=(e&m)*Ct,e<u)||(e=r.get(t+6),n+=(e&m)*Tt,e<u)||(e=r.get(t+7),n+=(e&m)*Bt,e<u))return n;throw new RangeError("Could not decode varint")}function Ue(r,t,e=0){return t==null&&(t=b(T(r))),t instanceof Uint8Array?Mt(r,t,e):gr(r,t,e)}function De(r,t=0){return r instanceof Uint8Array?kt(r,t):wr(r,t)}var pt=class extends Error{name="InvalidMessageLengthError";code="ERR_INVALID_MSG_LENGTH"},mt=class extends Error{name="InvalidDataLengthError";code="ERR_MSG_DATA_TOO_LONG"},gt=class extends Error{name="InvalidDataLengthLengthError";code="ERR_MSG_LENGTH_TOO_LONG"};function Fe(r,t={}){let e=Ie(r,t);t.maxDataLength!=null&&t.maxLengthLength==null&&(t.maxLengthLength=T(t.maxDataLength));let n=t?.lengthDecoder??De,i=t?.lengthEncoder??Ue;return{read:async s=>{let a=-1,c=new C;for(;;){c.append(await e.read(1,s));try{a=n(c)}catch(h){if(h instanceof RangeError)continue;throw h}if(a<0)throw new pt("Invalid message length");if(t?.maxLengthLength!=null&&c.byteLength>t.maxLengthLength)throw new gt("message length length too long");if(a>-1)break}if(t?.maxDataLength!=null&&a>t.maxDataLength)throw new mt("message length too long");return e.read(a,s)},write:async(s,a)=>{await e.write(new C(i(s.byteLength),s),a)},writeV:async(s,a)=>{let c=new C(...s.flatMap(h=>[i(h.byteLength),h]));await e.write(c,a)},unwrap:()=>e.unwrap()}}function Vt(r,t){let e=Fe(r,t),n={read:async(i,o)=>{let s=await e.read(o);return i.decode(s)},write:async(i,o,s)=>{await e.write(o.encode(i),s)},writeV:async(i,o,s)=>{await e.writeV(i.map(a=>o.encode(a)),s)},pb:i=>({read:async o=>n.read(i,o),write:async(o,s)=>n.write(o,i,s),writeV:async(o,s)=>n.writeV(o,i,s),unwrap:()=>n}),unwrap:()=>e.unwrap()};return n}var Gt={};y(Gt,{base10:()=>Ar});var mi=new Uint8Array(0);function ve(r,t){if(r===t)return!0;if(r.byteLength!==t.byteLength)return!1;for(let e=0;e<r.byteLength;e++)if(r[e]!==t[e])return!1;return!0}function B(r){if(r instanceof Uint8Array&&r.constructor.name==="Uint8Array")return r;if(r instanceof ArrayBuffer)return new Uint8Array(r);if(ArrayBuffer.isView(r))return new Uint8Array(r.buffer,r.byteOffset,r.byteLength);throw new Error("Unknown type, must be binary type")}function Re(r){return new TextEncoder().encode(r)}function Oe(r){return new TextDecoder().decode(r)}function br(r,t){if(r.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),n=0;n<e.length;n++)e[n]=255;for(var i=0;i<r.length;i++){var o=r.charAt(i),s=o.charCodeAt(0);if(e[s]!==255)throw new TypeError(o+" is ambiguous");e[s]=i}var a=r.length,c=r.charAt(0),h=Math.log(a)/Math.log(256),l=Math.log(256)/Math.log(a);function f(d){if(d instanceof Uint8Array||(ArrayBuffer.isView(d)?d=new Uint8Array(d.buffer,d.byteOffset,d.byteLength):Array.isArray(d)&&(d=Uint8Array.from(d))),!(d instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(d.length===0)return"";for(var x=0,_=0,N=0,D=d.length;N!==D&&d[N]===0;)N++,x++;for(var F=(D-N)*l+1>>>0,L=new Uint8Array(F);N!==D;){for(var v=d[N],M=0,I=F-1;(v!==0||M<_)&&I!==-1;I--,M++)v+=256*L[I]>>>0,L[I]=v%a>>>0,v=v/a>>>0;if(v!==0)throw new Error("Non-zero carry");_=M,N++}for(var O=F-_;O!==F&&L[O]===0;)O++;for(var st=c.repeat(x);O<F;++O)st+=r.charAt(L[O]);return st}function w(d){if(typeof d!="string")throw new TypeError("Expected String");if(d.length===0)return new Uint8Array;var x=0;if(d[x]!==" "){for(var _=0,N=0;d[x]===c;)_++,x++;for(var D=(d.length-x)*h+1>>>0,F=new Uint8Array(D);d[x];){var L=e[d.charCodeAt(x)];if(L===255)return;for(var v=0,M=D-1;(L!==0||v<N)&&M!==-1;M--,v++)L+=a*F[M]>>>0,F[M]=L%256>>>0,L=L/256>>>0;if(L!==0)throw new Error("Non-zero carry");N=v,x++}if(d[x]!==" "){for(var I=D-N;I!==D&&F[I]===0;)I++;for(var O=new Uint8Array(_+(D-I)),st=_;I!==D;)O[st++]=F[I++];return O}}}function ot(d){var x=w(d);if(x)return x;throw new Error(`Non-${t} character`)}return{encode:f,decodeUnsafe:w,decode:ot}}var yr=br,Er=yr,Te=Er;var _t=class{name;prefix;baseEncode;constructor(t,e,n){this.name=t,this.prefix=e,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},Pt=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,e,n){this.name=t,this.prefix=e;let i=e.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(t){if(typeof t=="string"){if(t.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(t)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(t.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(t){return Be(this,t)}},zt=class{decoders;constructor(t){this.decoders=t}or(t){return Be(this,t)}decode(t){let e=t[0],n=this.decoders[e];if(n!=null)return n.decode(t);throw RangeError(`Unable to decode multibase string ${JSON.stringify(t)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function Be(r,t){return new zt({...r.decoders??{[r.prefix]:r},...t.decoders??{[t.prefix]:t}})}var $t=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,e,n,i){this.name=t,this.prefix=e,this.baseEncode=n,this.baseDecode=i,this.encoder=new _t(t,e,n),this.decoder=new Pt(t,e,i)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function q({name:r,prefix:t,encode:e,decode:n}){return new $t(r,t,e,n)}function k({name:r,prefix:t,alphabet:e}){let{encode:n,decode:i}=Te(e,r);return q({prefix:t,name:r,encode:n,decode:o=>B(i(o))})}function Lr(r,t,e,n){let i={};for(let l=0;l<t.length;++l)i[t[l]]=l;let o=r.length;for(;r[o-1]==="=";)--o;let s=new Uint8Array(o*e/8|0),a=0,c=0,h=0;for(let l=0;l<o;++l){let f=i[r[l]];if(f===void 0)throw new SyntaxError(`Non-${n} character`);c=c<<e|f,a+=e,a>=8&&(a-=8,s[h++]=255&c>>a)}if(a>=e||255&c<<8-a)throw new SyntaxError("Unexpected end of data");return s}function Sr(r,t,e){let n=t[t.length-1]==="=",i=(1<<e)-1,o="",s=0,a=0;for(let c=0;c<r.length;++c)for(a=a<<8|r[c],s+=8;s>e;)s-=e,o+=t[i&a>>s];if(s!==0&&(o+=t[i&a<<e-s]),n)for(;o.length*e&7;)o+="=";return o}function p({name:r,prefix:t,bitsPerChar:e,alphabet:n}){return q({prefix:t,name:r,encode(i){return Sr(i,n,e)},decode(i){return Lr(i,n,e,r)}})}var Ar=k({prefix:"9",name:"base10",alphabet:"0123456789"});var qt={};y(qt,{base16:()=>Nr,base16upper:()=>Ir});var Nr=p({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),Ir=p({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Kt={};y(Kt,{base2:()=>Ur});var Ur=p({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Xt={};y(Xt,{base256emoji:()=>Or});var Me=Array.from("\u{1F680}\u{1FA90}\u2604\u{1F6F0}\u{1F30C}\u{1F311}\u{1F312}\u{1F313}\u{1F314}\u{1F315}\u{1F316}\u{1F317}\u{1F318}\u{1F30D}\u{1F30F}\u{1F30E}\u{1F409}\u2600\u{1F4BB}\u{1F5A5}\u{1F4BE}\u{1F4BF}\u{1F602}\u2764\u{1F60D}\u{1F923}\u{1F60A}\u{1F64F}\u{1F495}\u{1F62D}\u{1F618}\u{1F44D}\u{1F605}\u{1F44F}\u{1F601}\u{1F525}\u{1F970}\u{1F494}\u{1F496}\u{1F499}\u{1F622}\u{1F914}\u{1F606}\u{1F644}\u{1F4AA}\u{1F609}\u263A\u{1F44C}\u{1F917}\u{1F49C}\u{1F614}\u{1F60E}\u{1F607}\u{1F339}\u{1F926}\u{1F389}\u{1F49E}\u270C\u2728\u{1F937}\u{1F631}\u{1F60C}\u{1F338}\u{1F64C}\u{1F60B}\u{1F497}\u{1F49A}\u{1F60F}\u{1F49B}\u{1F642}\u{1F493}\u{1F929}\u{1F604}\u{1F600}\u{1F5A4}\u{1F603}\u{1F4AF}\u{1F648}\u{1F447}\u{1F3B6}\u{1F612}\u{1F92D}\u2763\u{1F61C}\u{1F48B}\u{1F440}\u{1F62A}\u{1F611}\u{1F4A5}\u{1F64B}\u{1F61E}\u{1F629}\u{1F621}\u{1F92A}\u{1F44A}\u{1F973}\u{1F625}\u{1F924}\u{1F449}\u{1F483}\u{1F633}\u270B\u{1F61A}\u{1F61D}\u{1F634}\u{1F31F}\u{1F62C}\u{1F643}\u{1F340}\u{1F337}\u{1F63B}\u{1F613}\u2B50\u2705\u{1F97A}\u{1F308}\u{1F608}\u{1F918}\u{1F4A6}\u2714\u{1F623}\u{1F3C3}\u{1F490}\u2639\u{1F38A}\u{1F498}\u{1F620}\u261D\u{1F615}\u{1F33A}\u{1F382}\u{1F33B}\u{1F610}\u{1F595}\u{1F49D}\u{1F64A}\u{1F639}\u{1F5E3}\u{1F4AB}\u{1F480}\u{1F451}\u{1F3B5}\u{1F91E}\u{1F61B}\u{1F534}\u{1F624}\u{1F33C}\u{1F62B}\u26BD\u{1F919}\u2615\u{1F3C6}\u{1F92B}\u{1F448}\u{1F62E}\u{1F646}\u{1F37B}\u{1F343}\u{1F436}\u{1F481}\u{1F632}\u{1F33F}\u{1F9E1}\u{1F381}\u26A1\u{1F31E}\u{1F388}\u274C\u270A\u{1F44B}\u{1F630}\u{1F928}\u{1F636}\u{1F91D}\u{1F6B6}\u{1F4B0}\u{1F353}\u{1F4A2}\u{1F91F}\u{1F641}\u{1F6A8}\u{1F4A8}\u{1F92C}\u2708\u{1F380}\u{1F37A}\u{1F913}\u{1F619}\u{1F49F}\u{1F331}\u{1F616}\u{1F476}\u{1F974}\u25B6\u27A1\u2753\u{1F48E}\u{1F4B8}\u2B07\u{1F628}\u{1F31A}\u{1F98B}\u{1F637}\u{1F57A}\u26A0\u{1F645}\u{1F61F}\u{1F635}\u{1F44E}\u{1F932}\u{1F920}\u{1F927}\u{1F4CC}\u{1F535}\u{1F485}\u{1F9D0}\u{1F43E}\u{1F352}\u{1F617}\u{1F911}\u{1F30A}\u{1F92F}\u{1F437}\u260E\u{1F4A7}\u{1F62F}\u{1F486}\u{1F446}\u{1F3A4}\u{1F647}\u{1F351}\u2744\u{1F334}\u{1F4A3}\u{1F438}\u{1F48C}\u{1F4CD}\u{1F940}\u{1F922}\u{1F445}\u{1F4A1}\u{1F4A9}\u{1F450}\u{1F4F8}\u{1F47B}\u{1F910}\u{1F92E}\u{1F3BC}\u{1F975}\u{1F6A9}\u{1F34E}\u{1F34A}\u{1F47C}\u{1F48D}\u{1F4E3}\u{1F942}"),Dr=Me.reduce((r,t,e)=>(r[e]=t,r),[]),Fr=Me.reduce((r,t,e)=>{let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);return r[n]=e,r},[]);function vr(r){return r.reduce((t,e)=>(t+=Dr[e],t),"")}function Rr(r){let t=[];for(let e of r){let n=e.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${e}`);let i=Fr[n];if(i==null)throw new Error(`Non-base256emoji character: ${e}`);t.push(i)}return new Uint8Array(t)}var Or=q({prefix:"\u{1F680}",name:"base256emoji",encode:vr,decode:Rr});var jt={};y(jt,{base32:()=>K,base32hex:()=>Mr,base32hexpad:()=>Vr,base32hexpadupper:()=>_r,base32hexupper:()=>kr,base32pad:()=>Tr,base32padupper:()=>Br,base32upper:()=>Cr,base32z:()=>Pr});var K=p({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Cr=p({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Tr=p({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Br=p({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Mr=p({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),kr=p({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Vr=p({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),_r=p({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Pr=p({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Wt={};y(Wt,{base36:()=>Q,base36upper:()=>zr});var Q=k({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),zr=k({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Jt={};y(Jt,{base58btc:()=>R,base58flickr:()=>$r});var R=k({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),$r=k({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ht={};y(Ht,{base64:()=>Gr,base64pad:()=>qr,base64url:()=>Kr,base64urlpad:()=>Xr});var Gr=p({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),qr=p({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Kr=p({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Xr=p({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Qt={};y(Qt,{base8:()=>jr});var jr=p({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Zt={};y(Zt,{identity:()=>Wr});var Wr=q({prefix:"\0",name:"identity",encode:r=>Oe(r),decode:r=>Re(r)});var Ri=new TextEncoder,Oi=new TextDecoder;var te={};y(te,{identity:()=>xn});var Qr=_e,ke=128,Zr=127,Yr=~Zr,tn=Math.pow(2,31);function _e(r,t,e){t=t||[],e=e||0;for(var n=e;r>=tn;)t[e++]=r&255|ke,r/=128;for(;r&Yr;)t[e++]=r&255|ke,r>>>=7;return t[e]=r|0,_e.bytes=e-n+1,t}var en=Yt,rn=128,Ve=127;function Yt(r,n){var e=0,n=n||0,i=0,o=n,s,a=r.length;do{if(o>=a)throw Yt.bytes=0,new RangeError("Could not decode varint");s=r[o++],e+=i<28?(s&Ve)<<i:(s&Ve)*Math.pow(2,i),i+=7}while(s>=rn);return Yt.bytes=o-n,e}var nn=Math.pow(2,7),on=Math.pow(2,14),sn=Math.pow(2,21),an=Math.pow(2,28),cn=Math.pow(2,35),hn=Math.pow(2,42),un=Math.pow(2,49),fn=Math.pow(2,56),ln=Math.pow(2,63),dn=function(r){return r<nn?1:r<on?2:r<sn?3:r<an?4:r<cn?5:r<hn?6:r<un?7:r<fn?8:r<ln?9:10},pn={encode:Qr,decode:en,encodingLength:dn},mn=pn,Z=mn;function Y(r,t=0){return[Z.decode(r,t),Z.decode.bytes]}function X(r,t,e=0){return Z.encode(r,t,e),t}function j(r){return Z.encodingLength(r)}function P(r,t){let e=t.byteLength,n=j(r),i=n+j(e),o=new Uint8Array(i+e);return X(r,o,0),X(e,o,n),o.set(t,i),new W(r,e,t,o)}function Pe(r){let t=B(r),[e,n]=Y(t),[i,o]=Y(t.subarray(n)),s=t.subarray(n+o);if(s.byteLength!==i)throw new Error("Incorrect length");return new W(e,i,s,t)}function ze(r,t){if(r===t)return!0;{let e=t;return r.code===e.code&&r.size===e.size&&e.bytes instanceof Uint8Array&&ve(r.bytes,e.bytes)}}var W=class{code;size;digest;bytes;constructor(t,e,n,i){this.code=t,this.size=e,this.digest=n,this.bytes=i}};var $e=0,gn="identity",Ge=B;function wn(r){return P($e,Ge(r))}var xn={code:$e,name:gn,encode:Ge,digest:wn};var ne={};y(ne,{sha256:()=>bn,sha512:()=>yn});function re({name:r,code:t,encode:e}){return new ee(r,t,e)}var ee=class{name;code;encode;constructor(t,e,n){this.name=t,this.code=e,this.encode=n}digest(t){if(t instanceof Uint8Array){let e=this.encode(t);return e instanceof Uint8Array?P(this.code,e):e.then(n=>P(this.code,n))}else throw Error("Unknown type, must be binary type")}};function Ke(r){return async t=>new Uint8Array(await crypto.subtle.digest(r,t))}var bn=re({name:"sha2-256",code:18,encode:Ke("SHA-256")}),yn=re({name:"sha2-512",code:19,encode:Ke("SHA-512")});function Xe(r,t){let{bytes:e,version:n}=r;switch(n){case 0:return Ln(e,ie(r),t??R.encoder);default:return Sn(e,ie(r),t??K.encoder)}}var je=new WeakMap;function ie(r){let t=je.get(r);if(t==null){let e=new Map;return je.set(r,e),e}return t}var xt=class r{code;version;multihash;bytes;"/";constructor(t,e,n,i){this.code=e,this.version=t,this.multihash=n,this.bytes=i,this["/"]=i}get asCID(){return this}get byteOffset(){return this.bytes.byteOffset}get byteLength(){return this.bytes.byteLength}toV0(){switch(this.version){case 0:return this;case 1:{let{code:t,multihash:e}=this;if(t!==et)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(e.code!==An)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return r.createV0(e)}default:throw Error(`Can not convert CID version ${this.version} to version 0. This is a bug please report`)}}toV1(){switch(this.version){case 0:{let{code:t,digest:e}=this.multihash,n=P(t,e);return r.createV1(this.code,n)}case 1:return this;default:throw Error(`Can not convert CID version ${this.version} to version 1. This is a bug please report`)}}equals(t){return r.equals(this,t)}static equals(t,e){let n=e;return n!=null&&t.code===n.code&&t.version===n.version&&ze(t.multihash,n.multihash)}toString(t){return Xe(this,t)}toJSON(){return{"/":Xe(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(t){if(t==null)return null;let e=t;if(e instanceof r)return e;if(e["/"]!=null&&e["/"]===e.bytes||e.asCID===e){let{version:n,code:i,multihash:o,bytes:s}=e;return new r(n,i,o,s??We(n,i,o.bytes))}else if(e[Nn]===!0){let{version:n,multihash:i,code:o}=e,s=Pe(i);return r.create(n,o,s)}else return null}static create(t,e,n){if(typeof e!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(t){case 0:{if(e!==et)throw new Error(`Version 0 CID must use dag-pb (code: ${et}) block encoding`);return new r(t,e,n,n.bytes)}case 1:{let i=We(t,e,n.bytes);return new r(t,e,n,i)}default:throw new Error("Invalid version")}}static createV0(t){return r.create(0,et,t)}static createV1(t,e){return r.create(1,t,e)}static decode(t){let[e,n]=r.decodeFirst(t);if(n.length!==0)throw new Error("Incorrect length");return e}static decodeFirst(t){let e=r.inspectBytes(t),n=e.size-e.multihashSize,i=B(t.subarray(n,n+e.multihashSize));if(i.byteLength!==e.multihashSize)throw new Error("Incorrect length");let o=i.subarray(e.multihashSize-e.digestSize),s=new W(e.multihashCode,e.digestSize,o,i);return[e.version===0?r.createV0(s):r.createV1(e.codec,s),t.subarray(e.size)]}static inspectBytes(t){let e=0,n=()=>{let[f,w]=Y(t.subarray(e));return e+=w,f},i=n(),o=et;if(i===18?(i=0,e=0):o=n(),i!==0&&i!==1)throw new RangeError(`Invalid CID version ${i}`);let s=e,a=n(),c=n(),h=e+c,l=h-s;return{version:i,codec:o,multihashCode:a,digestSize:c,multihashSize:l,size:h}}static parse(t,e){let[n,i]=En(t,e),o=r.decode(i);if(o.version===0&&t[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return ie(o).set(n,t),o}};function En(r,t){switch(r[0]){case"Q":{let e=t??R;return[R.prefix,e.decode(`${R.prefix}${r}`)]}case R.prefix:{let e=t??R;return[R.prefix,e.decode(r)]}case K.prefix:{let e=t??K;return[K.prefix,e.decode(r)]}case Q.prefix:{let e=t??Q;return[Q.prefix,e.decode(r)]}default:{if(t==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[r[0],t.decode(r)]}}}function Ln(r,t,e){let{prefix:n}=e;if(n!==R.prefix)throw Error(`Cannot string encode V0 in ${e.name} encoding`);let i=t.get(n);if(i==null){let o=e.encode(r).slice(1);return t.set(n,o),o}else return i}function Sn(r,t,e){let{prefix:n}=e,i=t.get(n);if(i==null){let o=e.encode(r);return t.set(n,o),o}else return i}var et=112,An=18;function We(r,t,e){let n=j(r),i=n+j(t),o=new Uint8Array(i+e.byteLength);return X(r,o,0),X(t,o,n),o.set(e,i),o}var Nn=Symbol.for("@ipld/js-cid/CID");var oe={...Zt,...Kt,...Qt,...Gt,...qt,...jt,...Wt,...Jt,...Ht,...Xt},to={...ne,...te};function He(r,t,e,n){return{name:r,prefix:t,encoder:{name:r,prefix:t,encode:e},decoder:{decode:n}}}var Je=He("utf8","u",r=>"u"+new TextDecoder("utf8").decode(r),r=>new TextEncoder().encode(r.substring(1))),se=He("ascii","a",r=>{let t="a";for(let e=0;e<r.length;e++)t+=String.fromCharCode(r[e]);return t},r=>{r=r.substring(1);let t=b(r.length);for(let e=0;e<r.length;e++)t[e]=r.charCodeAt(e);return t}),In={utf8:Je,"utf-8":Je,hex:oe.base16,latin1:se,ascii:se,binary:se,...oe},bt=In;function J(r,t="utf8"){let e=bt[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.decoder.decode(`${e.prefix}${r}`)}function ae(r,t="utf8"){let e=bt[t];if(e==null)throw new Error(`Unsupported encoding "${t}"`);return e.encoder.encode(r).substring(1)}var Qe="0.0.1",Ze="fetch";var ce=new Float32Array([-0]),V=new Uint8Array(ce.buffer);function Ye(r,t,e){ce[0]=r,t[e]=V[0],t[e+1]=V[1],t[e+2]=V[2],t[e+3]=V[3]}function tr(r,t){return V[0]=r[t],V[1]=r[t+1],V[2]=r[t+2],V[3]=r[t+3],ce[0]}var he=new Float64Array([-0]),g=new Uint8Array(he.buffer);function er(r,t,e){he[0]=r,t[e]=g[0],t[e+1]=g[1],t[e+2]=g[2],t[e+3]=g[3],t[e+4]=g[4],t[e+5]=g[5],t[e+6]=g[6],t[e+7]=g[7]}function rr(r,t){return g[0]=r[t],g[1]=r[t+1],g[2]=r[t+2],g[3]=r[t+3],g[4]=r[t+4],g[5]=r[t+5],g[6]=r[t+6],g[7]=r[t+7],he[0]}var Un=BigInt(Number.MAX_SAFE_INTEGER),Dn=BigInt(Number.MIN_SAFE_INTEGER),E=class r{lo;hi;constructor(t,e){this.lo=t|0,this.hi=e|0}toNumber(t=!1){if(!t&&this.hi>>>31>0){let e=~this.lo+1>>>0,n=~this.hi>>>0;return e===0&&(n=n+1>>>0),-(e+n*4294967296)}return this.lo+this.hi*4294967296}toBigInt(t=!1){if(t)return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n);if(this.hi>>>31){let e=~this.lo+1>>>0,n=~this.hi>>>0;return e===0&&(n=n+1>>>0),-(BigInt(e)+(BigInt(n)<<32n))}return BigInt(this.lo>>>0)+(BigInt(this.hi>>>0)<<32n)}toString(t=!1){return this.toBigInt(t).toString()}zzEncode(){let t=this.hi>>31;return this.hi=((this.hi<<1|this.lo>>>31)^t)>>>0,this.lo=(this.lo<<1^t)>>>0,this}zzDecode(){let t=-(this.lo&1);return this.lo=((this.lo>>>1|this.hi<<31)^t)>>>0,this.hi=(this.hi>>>1^t)>>>0,this}length(){let t=this.lo,e=(this.lo>>>28|this.hi<<4)>>>0,n=this.hi>>>24;return n===0?e===0?t<16384?t<128?1:2:t<2097152?3:4:e<16384?e<128?5:6:e<2097152?7:8:n<128?9:10}static fromBigInt(t){if(t===0n)return z;if(t<Un&&t>Dn)return this.fromNumber(Number(t));let e=t<0n;e&&(t=-t);let n=t>>32n,i=t-(n<<32n);return e&&(n=~n|0n,i=~i|0n,++i>nr&&(i=0n,++n>nr&&(n=0n))),new r(Number(i),Number(n))}static fromNumber(t){if(t===0)return z;let e=t<0;e&&(t=-t);let n=t>>>0,i=(t-n)/4294967296>>>0;return e&&(i=~i>>>0,n=~n>>>0,++n>4294967295&&(n=0,++i>4294967295&&(i=0))),new r(n,i)}static from(t){return typeof t=="number"?r.fromNumber(t):typeof t=="bigint"?r.fromBigInt(t):typeof t=="string"?r.fromBigInt(BigInt(t)):t.low!=null||t.high!=null?new r(t.low>>>0,t.high>>>0):z}},z=new E(0,0);z.toBigInt=function(){return 0n};z.zzEncode=z.zzDecode=function(){return this};z.length=function(){return 1};var nr=4294967296n;function ir(r){let t=0,e=0;for(let n=0;n<r.length;++n)e=r.charCodeAt(n),e<128?t+=1:e<2048?t+=2:(e&64512)===55296&&(r.charCodeAt(n+1)&64512)===56320?(++n,t+=4):t+=3;return t}function or(r,t,e){if(e-t<1)return"";let i,o=[],s=0,a;for(;t<e;)a=r[t++],a<128?o[s++]=a:a>191&&a<224?o[s++]=(a&31)<<6|r[t++]&63:a>239&&a<365?(a=((a&7)<<18|(r[t++]&63)<<12|(r[t++]&63)<<6|r[t++]&63)-65536,o[s++]=55296+(a>>10),o[s++]=56320+(a&1023)):o[s++]=(a&15)<<12|(r[t++]&63)<<6|r[t++]&63,s>8191&&((i??(i=[])).push(String.fromCharCode.apply(String,o)),s=0);return i!=null?(s>0&&i.push(String.fromCharCode.apply(String,o.slice(0,s))),i.join("")):String.fromCharCode.apply(String,o.slice(0,s))}function ue(r,t,e){let n=e,i,o;for(let s=0;s<r.length;++s)i=r.charCodeAt(s),i<128?t[e++]=i:i<2048?(t[e++]=i>>6|192,t[e++]=i&63|128):(i&64512)===55296&&((o=r.charCodeAt(s+1))&64512)===56320?(i=65536+((i&1023)<<10)+(o&1023),++s,t[e++]=i>>18|240,t[e++]=i>>12&63|128,t[e++]=i>>6&63|128,t[e++]=i&63|128):(t[e++]=i>>12|224,t[e++]=i>>6&63|128,t[e++]=i&63|128);return e-n}function U(r,t){return RangeError(`index out of range: ${r.pos} + ${t??1} > ${r.len}`)}function yt(r,t){return(r[t-4]|r[t-3]<<8|r[t-2]<<16|r[t-1]<<24)>>>0}var fe=class{buf;pos;len;_slice=Uint8Array.prototype.subarray;constructor(t){this.buf=t,this.pos=0,this.len=t.length}uint32(){let t=4294967295;if(t=(this.buf[this.pos]&127)>>>0,this.buf[this.pos++]<128||(t=(t|(this.buf[this.pos]&127)<<7)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&127)<<14)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&127)<<21)>>>0,this.buf[this.pos++]<128)||(t=(t|(this.buf[this.pos]&15)<<28)>>>0,this.buf[this.pos++]<128))return t;if((this.pos+=5)>this.len)throw this.pos=this.len,U(this,10);return t}int32(){return this.uint32()|0}sint32(){let t=this.uint32();return t>>>1^-(t&1)|0}bool(){return this.uint32()!==0}fixed32(){if(this.pos+4>this.len)throw U(this,4);return yt(this.buf,this.pos+=4)}sfixed32(){if(this.pos+4>this.len)throw U(this,4);return yt(this.buf,this.pos+=4)|0}float(){if(this.pos+4>this.len)throw U(this,4);let t=tr(this.buf,this.pos);return this.pos+=4,t}double(){if(this.pos+8>this.len)throw U(this,4);let t=rr(this.buf,this.pos);return this.pos+=8,t}bytes(){let t=this.uint32(),e=this.pos,n=this.pos+t;if(n>this.len)throw U(this,t);return this.pos+=t,e===n?new Uint8Array(0):this.buf.subarray(e,n)}string(){let t=this.bytes();return or(t,0,t.length)}skip(t){if(typeof t=="number"){if(this.pos+t>this.len)throw U(this,t);this.pos+=t}else do if(this.pos>=this.len)throw U(this);while(this.buf[this.pos++]&128);return this}skipType(t){switch(t){case 0:this.skip();break;case 1:this.skip(8);break;case 2:this.skip(this.uint32());break;case 3:for(;(t=this.uint32()&7)!==4;)this.skipType(t);break;case 5:this.skip(4);break;default:throw Error(`invalid wire type ${t} at offset ${this.pos}`)}return this}readLongVarint(){let t=new E(0,0),e=0;if(this.len-this.pos>4){for(;e<4;++e)if(t.lo=(t.lo|(this.buf[this.pos]&127)<<e*7)>>>0,this.buf[this.pos++]<128)return t;if(t.lo=(t.lo|(this.buf[this.pos]&127)<<28)>>>0,t.hi=(t.hi|(this.buf[this.pos]&127)>>4)>>>0,this.buf[this.pos++]<128)return t;e=0}else{for(;e<3;++e){if(this.pos>=this.len)throw U(this);if(t.lo=(t.lo|(this.buf[this.pos]&127)<<e*7)>>>0,this.buf[this.pos++]<128)return t}return t.lo=(t.lo|(this.buf[this.pos++]&127)<<e*7)>>>0,t}if(this.len-this.pos>4){for(;e<5;++e)if(t.hi=(t.hi|(this.buf[this.pos]&127)<<e*7+3)>>>0,this.buf[this.pos++]<128)return t}else for(;e<5;++e){if(this.pos>=this.len)throw U(this);if(t.hi=(t.hi|(this.buf[this.pos]&127)<<e*7+3)>>>0,this.buf[this.pos++]<128)return t}throw Error("invalid varint encoding")}readFixed64(){if(this.pos+8>this.len)throw U(this,8);let t=yt(this.buf,this.pos+=4),e=yt(this.buf,this.pos+=4);return new E(t,e)}int64(){return this.readLongVarint().toBigInt()}int64Number(){return this.readLongVarint().toNumber()}int64String(){return this.readLongVarint().toString()}uint64(){return this.readLongVarint().toBigInt(!0)}uint64Number(){let t=kt(this.buf,this.pos);return this.pos+=T(t),t}uint64String(){return this.readLongVarint().toString(!0)}sint64(){return this.readLongVarint().zzDecode().toBigInt()}sint64Number(){return this.readLongVarint().zzDecode().toNumber()}sint64String(){return this.readLongVarint().zzDecode().toString()}fixed64(){return this.readFixed64().toBigInt()}fixed64Number(){return this.readFixed64().toNumber()}fixed64String(){return this.readFixed64().toString()}sfixed64(){return this.readFixed64().toBigInt()}sfixed64Number(){return this.readFixed64().toNumber()}sfixed64String(){return this.readFixed64().toString()}};function le(r){return new fe(r instanceof Uint8Array?r:r.subarray())}function Et(r,t,e){let n=le(r);return t.decode(n,void 0,e)}function de(r){let t=r??8192,e=t>>>1,n,i=t;return function(s){if(s<1||s>e)return b(s);i+s>t&&(n=b(t),i=0);let a=n.subarray(i,i+=s);return i&7&&(i=(i|7)+1),a}}var $=class{fn;len;next;val;constructor(t,e,n){this.fn=t,this.len=e,this.next=void 0,this.val=n}};function pe(){}var ge=class{head;tail;len;next;constructor(t){this.head=t.head,this.tail=t.tail,this.len=t.len,this.next=t.states}},Fn=de();function vn(r){return globalThis.Buffer!=null?b(r):Fn(r)}var nt=class{len;head;tail;states;constructor(){this.len=0,this.head=new $(pe,0,0),this.tail=this.head,this.states=null}_push(t,e,n){return this.tail=this.tail.next=new $(t,e,n),this.len+=e,this}uint32(t){return this.len+=(this.tail=this.tail.next=new we((t=t>>>0)<128?1:t<16384?2:t<2097152?3:t<268435456?4:5,t)).len,this}int32(t){return t<0?this._push(Lt,10,E.fromNumber(t)):this.uint32(t)}sint32(t){return this.uint32((t<<1^t>>31)>>>0)}uint64(t){let e=E.fromBigInt(t);return this._push(Lt,e.length(),e)}uint64Number(t){return this._push(Mt,T(t),t)}uint64String(t){return this.uint64(BigInt(t))}int64(t){return this.uint64(t)}int64Number(t){return this.uint64Number(t)}int64String(t){return this.uint64String(t)}sint64(t){let e=E.fromBigInt(t).zzEncode();return this._push(Lt,e.length(),e)}sint64Number(t){let e=E.fromNumber(t).zzEncode();return this._push(Lt,e.length(),e)}sint64String(t){return this.sint64(BigInt(t))}bool(t){return this._push(me,1,t?1:0)}fixed32(t){return this._push(rt,4,t>>>0)}sfixed32(t){return this.fixed32(t)}fixed64(t){let e=E.fromBigInt(t);return this._push(rt,4,e.lo)._push(rt,4,e.hi)}fixed64Number(t){let e=E.fromNumber(t);return this._push(rt,4,e.lo)._push(rt,4,e.hi)}fixed64String(t){return this.fixed64(BigInt(t))}sfixed64(t){return this.fixed64(t)}sfixed64Number(t){return this.fixed64Number(t)}sfixed64String(t){return this.fixed64String(t)}float(t){return this._push(Ye,4,t)}double(t){return this._push(er,8,t)}bytes(t){let e=t.length>>>0;return e===0?this._push(me,1,0):this.uint32(e)._push(On,e,t)}string(t){let e=ir(t);return e!==0?this.uint32(e)._push(ue,e,t):this._push(me,1,0)}fork(){return this.states=new ge(this),this.head=this.tail=new $(pe,0,0),this.len=0,this}reset(){return this.states!=null?(this.head=this.states.head,this.tail=this.states.tail,this.len=this.states.len,this.states=this.states.next):(this.head=this.tail=new $(pe,0,0),this.len=0),this}ldelim(){let t=this.head,e=this.tail,n=this.len;return this.reset().uint32(n),n!==0&&(this.tail.next=t.next,this.tail=e,this.len+=n),this}finish(){let t=this.head.next,e=vn(this.len),n=0;for(;t!=null;)t.fn(t.val,e,n),n+=t.len,t=t.next;return e}};function me(r,t,e){t[e]=r&255}function Rn(r,t,e){for(;r>127;)t[e++]=r&127|128,r>>>=7;t[e]=r}var we=class extends ${next;constructor(t,e){super(Rn,t,e),this.next=void 0}};function Lt(r,t,e){for(;r.hi!==0;)t[e++]=r.lo&127|128,r.lo=(r.lo>>>7|r.hi<<25)>>>0,r.hi>>>=7;for(;r.lo>127;)t[e++]=r.lo&127|128,r.lo=r.lo>>>7;t[e++]=r.lo}function rt(r,t,e){t[e]=r&255,t[e+1]=r>>>8&255,t[e+2]=r>>>16&255,t[e+3]=r>>>24}function On(r,t,e){t.set(r,e)}globalThis.Buffer!=null&&(nt.prototype.bytes=function(r){let t=r.length>>>0;return this.uint32(t),t>0&&this._push(Cn,t,r),this},nt.prototype.string=function(r){let t=globalThis.Buffer.byteLength(r);return this.uint32(t),t>0&&this._push(Tn,t,r),this});function Cn(r,t,e){t.set(r,e)}function Tn(r,t,e){r.length<40?ue(r,t,e):t.utf8Write!=null?t.utf8Write(r,e):t.set(J(r),e)}function xe(){return new nt}function St(r,t){let e=xe();return t.encode(r,e,{lengthDelimited:!1}),e.finish()}var H;(function(r){r[r.VARINT=0]="VARINT",r[r.BIT64=1]="BIT64",r[r.LENGTH_DELIMITED=2]="LENGTH_DELIMITED",r[r.START_GROUP=3]="START_GROUP",r[r.END_GROUP=4]="END_GROUP",r[r.BIT32=5]="BIT32"})(H||(H={}));function At(r,t,e,n){return{name:r,type:t,encode:e,decode:n}}function be(r){function t(i){if(r[i.toString()]==null)throw new Error("Invalid enum value");return r[i]}let e=function(o,s){let a=t(o);s.int32(a)},n=function(o){let s=o.int32();return t(s)};return At("enum",H.VARINT,e,n)}function Nt(r,t){return At("message",H.LENGTH_DELIMITED,r,t)}var it;(function(r){let t;r.codec=()=>(t==null&&(t=Nt((e,n,i={})=>{i.lengthDelimited!==!1&&n.fork(),e.identifier!=null&&e.identifier.byteLength>0&&(n.uint32(10),n.bytes(e.identifier)),i.lengthDelimited!==!1&&n.ldelim()},(e,n,i={})=>{let o={identifier:S(0)},s=n==null?e.len:e.pos+n;for(;e.pos<s;){let a=e.uint32();switch(a>>>3){case 1:{o.identifier=e.bytes();break}default:{e.skipType(a&7);break}}}return o})),t),r.encode=e=>St(e,r.codec()),r.decode=(e,n)=>Et(e,r.codec(),n)})(it||(it={}));var A;(function(r){let t;(function(i){i.OK="OK",i.NOT_FOUND="NOT_FOUND",i.ERROR="ERROR"})(t=r.StatusCode||(r.StatusCode={}));let e;(function(i){i[i.OK=0]="OK",i[i.NOT_FOUND=1]="NOT_FOUND",i[i.ERROR=2]="ERROR"})(e||(e={})),function(i){i.codec=()=>be(e)}(t=r.StatusCode||(r.StatusCode={}));let n;r.codec=()=>(n==null&&(n=Nt((i,o,s={})=>{s.lengthDelimited!==!1&&o.fork(),i.status!=null&&e[i.status]!==0&&(o.uint32(8),r.StatusCode.codec().encode(i.status,o)),i.data!=null&&i.data.byteLength>0&&(o.uint32(18),o.bytes(i.data)),s.lengthDelimited!==!1&&o.ldelim()},(i,o,s={})=>{let a={status:t.OK,data:S(0)},c=o==null?i.len:i.pos+o;for(;i.pos<c;){let h=i.uint32();switch(h>>>3){case 1:{a.status=r.StatusCode.codec().decode(i);break}case 2:{a.data=i.bytes();break}default:{i.skipType(h&7);break}}}return a})),n),r.encode=i=>St(i,r.codec()),r.decode=(i,o)=>Et(i,r.codec(),o)})(A||(A={}));var ar=1e4,It=class{protocol;components;lookupFunctions;started;init;log;constructor(t,e={}){this.log=t.logger.forComponent("libp2p:fetch"),this.started=!1,this.components=t,this.protocol=`/${e.protocolPrefix??"libp2p"}/${Ze}/${Qe}`,this.lookupFunctions=new Map,this.handleMessage=this.handleMessage.bind(this),this.init=e}[Symbol.toStringTag]="@libp2p/fetch";async start(){await this.components.registrar.handle(this.protocol,t=>{this.handleMessage(t).then(async()=>{await t.stream.close()}).catch(e=>{this.log.error("error handling message - %e",e)})},{maxInboundStreams:this.init.maxInboundStreams,maxOutboundStreams:this.init.maxOutboundStreams}),this.started=!0}async stop(){await this.components.registrar.unhandle(this.protocol),this.started=!1}isStarted(){return this.started}async fetch(t,e,n={}){typeof e=="string"&&(e=J(e)),this.log.trace("dialing %s to %p",this.protocol,t);let i=await this.components.connectionManager.openConnection(t,n),o=n.signal,s,a=()=>{};if(o==null){let c=this.init.timeout??ar;this.log.trace("using default timeout of %d ms",c),o=AbortSignal.timeout(c),ye(1/0,o)}try{s=await i.newStream(this.protocol,{signal:o}),a=()=>{s?.abort(new at)},o.addEventListener("abort",a,{once:!0}),this.log.trace("fetch %m",e);let c=Vt(s);await c.write({identifier:e},it,n);let h=await c.read(A,n);switch(await c.unwrap().close(n),h.status){case A.StatusCode.OK:return this.log.trace("received status OK for %m",e),h.data;case A.StatusCode.NOT_FOUND:{this.log("received status NOT_FOUND for %m",e);return}case A.StatusCode.ERROR:{this.log("received status ERROR for %m",e);let l=ae(h.data);throw new ut("Error in fetch protocol response: "+l)}default:throw this.log("received status unknown for %m",e),new ht("Unknown response status")}}catch(c){throw s?.abort(c),c}finally{o.removeEventListener("abort",a),s!=null&&await s.close()}}async handleMessage(t){let{stream:e}=t,n=AbortSignal.timeout(this.init.timeout??ar);try{let i=Vt(e),o=await i.read(it,{signal:n}),s,a=ae(o.identifier),c=this._getLookupFunction(a);if(c==null){this.log.trace("sending status ERROR for %m",o.identifier);let h=J("No lookup function registered for key");s={status:A.StatusCode.ERROR,data:h}}else{this.log.trace("lookup data with identifier %s",c.prefix);try{let h=await c.fn(o.identifier);h==null?(this.log.trace("sending status NOT_FOUND for %m",o.identifier),s={status:A.StatusCode.NOT_FOUND,data:new Uint8Array(0)}):(this.log.trace("sending status OK for %m",o.identifier),s={status:A.StatusCode.OK,data:h})}catch(h){this.log.error("error during lookup of %m - %e",o.identifier,h);let l=J(h.message);s={status:A.StatusCode.ERROR,data:l}}}await i.write(s,A,{signal:n}),await i.unwrap().close({signal:n})}catch(i){this.log.error("error answering fetch request - %e",i),e.abort(i)}}_getLookupFunction(t){for(let e of this.lookupFunctions.keys())if(t.startsWith(e)){let n=this.lookupFunctions.get(e);if(n!=null)return{fn:n,prefix:e}}}registerLookupFunction(t,e){if(this.lookupFunctions.has(t))throw new ct(`Fetch protocol handler for key prefix '${t}' already registered`);this.lookupFunctions.set(t,e)}unregisterLookupFunction(t,e){e!=null&&this.lookupFunctions.get(t)!==e||this.lookupFunctions.delete(t)}};function Bn(r={}){return t=>new It(t,r)}return lr(Mn);})(); | ||
return Libp2PFetch})); |
@@ -25,3 +25,3 @@ import type { Fetch as FetchInterface, FetchComponents, FetchInit, LookupFunction } from './index.js'; | ||
*/ | ||
fetch(peer: PeerId, key: string, options?: AbortOptions): Promise<Uint8Array | undefined>; | ||
fetch(peer: PeerId, key: string | Uint8Array, options?: AbortOptions): Promise<Uint8Array | undefined>; | ||
/** | ||
@@ -37,3 +37,6 @@ * Invoked when a fetch request is received. Reads the request message off the given stream and | ||
*/ | ||
_getLookupFunction(key: string): LookupFunction | undefined; | ||
_getLookupFunction(key: string): { | ||
fn: LookupFunction; | ||
prefix: string; | ||
} | undefined; | ||
/** | ||
@@ -40,0 +43,0 @@ * Registers a new lookup callback that can map keys to values, for a given set of keys that |
@@ -38,3 +38,3 @@ import { AbortError, InvalidMessageError, InvalidParametersError, ProtocolError, setMaxListeners } from '@libp2p/interface'; | ||
.catch(err => { | ||
this.log.error(err); | ||
this.log.error('error handling message - %e', err); | ||
}); | ||
@@ -58,3 +58,6 @@ }, { | ||
async fetch(peer, key, options = {}) { | ||
this.log('dialing %s to %p', this.protocol, peer); | ||
if (typeof key === 'string') { | ||
key = uint8arrayFromString(key); | ||
} | ||
this.log.trace('dialing %s to %p', this.protocol, peer); | ||
const connection = await this.components.connectionManager.openConnection(peer, options); | ||
@@ -67,3 +70,3 @@ let signal = options.signal; | ||
const timeout = this.init.timeout ?? DEFAULT_TIMEOUT; | ||
this.log('using default timeout of %d ms', timeout); | ||
this.log.trace('using default timeout of %d ms', timeout); | ||
signal = AbortSignal.timeout(timeout); | ||
@@ -81,3 +84,3 @@ setMaxListeners(Infinity, signal); | ||
signal.addEventListener('abort', onAbort, { once: true }); | ||
this.log('fetch %s', key); | ||
this.log.trace('fetch %m', key); | ||
const pb = pbStream(stream); | ||
@@ -91,11 +94,11 @@ await pb.write({ | ||
case (FetchResponse.StatusCode.OK): { | ||
this.log('received status for %s ok', key); | ||
this.log.trace('received status OK for %m', key); | ||
return response.data; | ||
} | ||
case (FetchResponse.StatusCode.NOT_FOUND): { | ||
this.log('received status for %s not found', key); | ||
this.log('received status NOT_FOUND for %m', key); | ||
return; | ||
} | ||
case (FetchResponse.StatusCode.ERROR): { | ||
this.log('received status for %s error', key); | ||
this.log('received status ERROR for %m', key); | ||
const errmsg = uint8arrayToString(response.data); | ||
@@ -105,3 +108,3 @@ throw new ProtocolError('Error in fetch protocol response: ' + errmsg); | ||
default: { | ||
this.log('received status for %s unknown', key); | ||
this.log('received status unknown for %m', key); | ||
throw new InvalidMessageError('Unknown response status'); | ||
@@ -136,20 +139,28 @@ } | ||
let response; | ||
const lookup = this._getLookupFunction(request.identifier); | ||
if (lookup != null) { | ||
this.log('look up data with identifier %s', request.identifier); | ||
const data = await lookup(request.identifier); | ||
if (data != null) { | ||
this.log('sending status for %s ok', request.identifier); | ||
response = { status: FetchResponse.StatusCode.OK, data }; | ||
const key = uint8arrayToString(request.identifier); | ||
const lookup = this._getLookupFunction(key); | ||
if (lookup == null) { | ||
this.log.trace('sending status ERROR for %m', request.identifier); | ||
const errmsg = uint8arrayFromString('No lookup function registered for key'); | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }; | ||
} | ||
else { | ||
this.log.trace('lookup data with identifier %s', lookup.prefix); | ||
try { | ||
const data = await lookup.fn(request.identifier); | ||
if (data == null) { | ||
this.log.trace('sending status NOT_FOUND for %m', request.identifier); | ||
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }; | ||
} | ||
else { | ||
this.log.trace('sending status OK for %m', request.identifier); | ||
response = { status: FetchResponse.StatusCode.OK, data }; | ||
} | ||
} | ||
else { | ||
this.log('sending status for %s not found', request.identifier); | ||
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }; | ||
catch (err) { | ||
this.log.error('error during lookup of %m - %e', request.identifier, err); | ||
const errmsg = uint8arrayFromString(err.message); | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }; | ||
} | ||
} | ||
else { | ||
this.log('sending status for %s error', request.identifier); | ||
const errmsg = uint8arrayFromString(`No lookup function registered for key: ${request.identifier}`); | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }; | ||
} | ||
await pb.write(response, FetchResponse, { | ||
@@ -163,3 +174,3 @@ signal | ||
catch (err) { | ||
this.log('error answering fetch request', err); | ||
this.log.error('error answering fetch request - %e', err); | ||
stream.abort(err); | ||
@@ -175,3 +186,9 @@ } | ||
if (key.startsWith(prefix)) { | ||
return this.lookupFunctions.get(prefix); | ||
const fn = this.lookupFunctions.get(prefix); | ||
if (fn != null) { | ||
return { | ||
fn, | ||
prefix | ||
}; | ||
} | ||
} | ||
@@ -178,0 +195,0 @@ } |
@@ -21,7 +21,8 @@ /** | ||
* | ||
* // Given a key (as a string) returns a value (as a Uint8Array), or undefined | ||
* // if the key isn't found. | ||
* // Given a key (as a Uint8Array) returns a value (as a Uint8Array), or | ||
* // undefined if the key isn't found. | ||
* // | ||
* // All keys must be prefixed by the same prefix, which will be used to find | ||
* // the appropriate key lookup function. | ||
* async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> { | ||
* async function my_subsystem_key_lookup (key: Uint8Array): Promise<Uint8Array | undefined> { | ||
* // app specific callback to lookup key-value pairs. | ||
@@ -55,4 +56,11 @@ * return Uint8Array.from([0, 1, 2, 3, 4]) | ||
} | ||
/** | ||
* A lookup function is registered against a specific identifier prefix and is | ||
* invoked when a remote peer requests a value with that prefix | ||
*/ | ||
export interface LookupFunction { | ||
(key: string): Promise<Uint8Array | undefined>; | ||
/** | ||
* The key is the identifier requested by the remote peer | ||
*/ | ||
(key: Uint8Array): Promise<Uint8Array | undefined>; | ||
} | ||
@@ -68,3 +76,3 @@ export interface FetchComponents { | ||
*/ | ||
fetch(peer: PeerId, key: string, options?: AbortOptions): Promise<Uint8Array | undefined>; | ||
fetch(peer: PeerId, key: string | Uint8Array, options?: AbortOptions): Promise<Uint8Array | undefined>; | ||
/** | ||
@@ -71,0 +79,0 @@ * Registers a new lookup callback that can map keys to values, for a given set of keys that |
@@ -21,7 +21,8 @@ /** | ||
* | ||
* // Given a key (as a string) returns a value (as a Uint8Array), or undefined | ||
* // if the key isn't found. | ||
* // Given a key (as a Uint8Array) returns a value (as a Uint8Array), or | ||
* // undefined if the key isn't found. | ||
* // | ||
* // All keys must be prefixed by the same prefix, which will be used to find | ||
* // the appropriate key lookup function. | ||
* async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> { | ||
* async function my_subsystem_key_lookup (key: Uint8Array): Promise<Uint8Array | undefined> { | ||
* // app specific callback to lookup key-value pairs. | ||
@@ -28,0 +29,0 @@ * return Uint8Array.from([0, 1, 2, 3, 4]) |
import { type Codec, type DecodeOptions } from 'protons-runtime'; | ||
import type { Uint8ArrayList } from 'uint8arraylist'; | ||
export interface FetchRequest { | ||
identifier: string; | ||
identifier: Uint8Array; | ||
} | ||
@@ -6,0 +6,0 @@ export declare namespace FetchRequest { |
@@ -17,5 +17,5 @@ /* eslint-disable import/export */ | ||
} | ||
if ((obj.identifier != null && obj.identifier !== '')) { | ||
if ((obj.identifier != null && obj.identifier.byteLength > 0)) { | ||
w.uint32(10); | ||
w.string(obj.identifier); | ||
w.bytes(obj.identifier); | ||
} | ||
@@ -27,3 +27,3 @@ if (opts.lengthDelimited !== false) { | ||
const obj = { | ||
identifier: '' | ||
identifier: uint8ArrayAlloc(0) | ||
}; | ||
@@ -35,3 +35,3 @@ const end = length == null ? reader.len : reader.pos + length; | ||
case 1: { | ||
obj.identifier = reader.string(); | ||
obj.identifier = reader.bytes(); | ||
break; | ||
@@ -38,0 +38,0 @@ } |
{ | ||
"name": "@libp2p/fetch", | ||
"version": "2.0.15", | ||
"version": "3.0.0", | ||
"description": "Implementation of the Fetch Protocol", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0 OR MIT", |
@@ -44,7 +44,8 @@ # @libp2p/fetch | ||
// Given a key (as a string) returns a value (as a Uint8Array), or undefined | ||
// if the key isn't found. | ||
// Given a key (as a Uint8Array) returns a value (as a Uint8Array), or | ||
// undefined if the key isn't found. | ||
// | ||
// All keys must be prefixed by the same prefix, which will be used to find | ||
// the appropriate key lookup function. | ||
async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> { | ||
async function my_subsystem_key_lookup (key: Uint8Array): Promise<Uint8Array | undefined> { | ||
// app specific callback to lookup key-value pairs. | ||
@@ -51,0 +52,0 @@ return Uint8Array.from([0, 1, 2, 3, 4]) |
@@ -46,3 +46,3 @@ import { AbortError, InvalidMessageError, InvalidParametersError, ProtocolError, setMaxListeners } from '@libp2p/interface' | ||
.catch(err => { | ||
this.log.error(err) | ||
this.log.error('error handling message - %e', err) | ||
}) | ||
@@ -68,5 +68,9 @@ }, { | ||
*/ | ||
async fetch (peer: PeerId, key: string, options: AbortOptions = {}): Promise<Uint8Array | undefined> { | ||
this.log('dialing %s to %p', this.protocol, peer) | ||
async fetch (peer: PeerId, key: string | Uint8Array, options: AbortOptions = {}): Promise<Uint8Array | undefined> { | ||
if (typeof key === 'string') { | ||
key = uint8arrayFromString(key) | ||
} | ||
this.log.trace('dialing %s to %p', this.protocol, peer) | ||
const connection = await this.components.connectionManager.openConnection(peer, options) | ||
@@ -80,3 +84,3 @@ let signal = options.signal | ||
const timeout = this.init.timeout ?? DEFAULT_TIMEOUT | ||
this.log('using default timeout of %d ms', timeout) | ||
this.log.trace('using default timeout of %d ms', timeout) | ||
signal = AbortSignal.timeout(timeout) | ||
@@ -99,3 +103,3 @@ | ||
this.log('fetch %s', key) | ||
this.log.trace('fetch %m', key) | ||
@@ -112,11 +116,11 @@ const pb = pbStream(stream) | ||
case (FetchResponse.StatusCode.OK): { | ||
this.log('received status for %s ok', key) | ||
this.log.trace('received status OK for %m', key) | ||
return response.data | ||
} | ||
case (FetchResponse.StatusCode.NOT_FOUND): { | ||
this.log('received status for %s not found', key) | ||
this.log('received status NOT_FOUND for %m', key) | ||
return | ||
} | ||
case (FetchResponse.StatusCode.ERROR): { | ||
this.log('received status for %s error', key) | ||
this.log('received status ERROR for %m', key) | ||
const errmsg = uint8arrayToString(response.data) | ||
@@ -126,3 +130,3 @@ throw new ProtocolError('Error in fetch protocol response: ' + errmsg) | ||
default: { | ||
this.log('received status for %s unknown', key) | ||
this.log('received status unknown for %m', key) | ||
throw new InvalidMessageError('Unknown response status') | ||
@@ -158,17 +162,28 @@ } | ||
let response: FetchResponse | ||
const lookup = this._getLookupFunction(request.identifier) | ||
if (lookup != null) { | ||
this.log('look up data with identifier %s', request.identifier) | ||
const data = await lookup(request.identifier) | ||
if (data != null) { | ||
this.log('sending status for %s ok', request.identifier) | ||
response = { status: FetchResponse.StatusCode.OK, data } | ||
} else { | ||
this.log('sending status for %s not found', request.identifier) | ||
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) } | ||
const key = uint8arrayToString(request.identifier) | ||
const lookup = this._getLookupFunction(key) | ||
if (lookup == null) { | ||
this.log.trace('sending status ERROR for %m', request.identifier) | ||
const errmsg = uint8arrayFromString('No lookup function registered for key') | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg } | ||
} else { | ||
this.log.trace('lookup data with identifier %s', lookup.prefix) | ||
try { | ||
const data = await lookup.fn(request.identifier) | ||
if (data == null) { | ||
this.log.trace('sending status NOT_FOUND for %m', request.identifier) | ||
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) } | ||
} else { | ||
this.log.trace('sending status OK for %m', request.identifier) | ||
response = { status: FetchResponse.StatusCode.OK, data } | ||
} | ||
} catch (err: any) { | ||
this.log.error('error during lookup of %m - %e', request.identifier, err) | ||
const errmsg = uint8arrayFromString(err.message) | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg } | ||
} | ||
} else { | ||
this.log('sending status for %s error', request.identifier) | ||
const errmsg = uint8arrayFromString(`No lookup function registered for key: ${request.identifier}`) | ||
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg } | ||
} | ||
@@ -184,3 +199,3 @@ | ||
} catch (err: any) { | ||
this.log('error answering fetch request', err) | ||
this.log.error('error answering fetch request - %e', err) | ||
stream.abort(err) | ||
@@ -194,6 +209,13 @@ } | ||
*/ | ||
_getLookupFunction (key: string): LookupFunction | undefined { | ||
_getLookupFunction (key: string): { fn: LookupFunction, prefix: string } | undefined { | ||
for (const prefix of this.lookupFunctions.keys()) { | ||
if (key.startsWith(prefix)) { | ||
return this.lookupFunctions.get(prefix) | ||
const fn = this.lookupFunctions.get(prefix) | ||
if (fn != null) { | ||
return { | ||
fn, | ||
prefix | ||
} | ||
} | ||
} | ||
@@ -200,0 +222,0 @@ } |
@@ -21,7 +21,8 @@ /** | ||
* | ||
* // Given a key (as a string) returns a value (as a Uint8Array), or undefined | ||
* // if the key isn't found. | ||
* // Given a key (as a Uint8Array) returns a value (as a Uint8Array), or | ||
* // undefined if the key isn't found. | ||
* // | ||
* // All keys must be prefixed by the same prefix, which will be used to find | ||
* // the appropriate key lookup function. | ||
* async function my_subsystem_key_lookup (key: string): Promise<Uint8Array | undefined> { | ||
* async function my_subsystem_key_lookup (key: Uint8Array): Promise<Uint8Array | undefined> { | ||
* // app specific callback to lookup key-value pairs. | ||
@@ -60,4 +61,11 @@ * return Uint8Array.from([0, 1, 2, 3, 4]) | ||
/** | ||
* A lookup function is registered against a specific identifier prefix and is | ||
* invoked when a remote peer requests a value with that prefix | ||
*/ | ||
export interface LookupFunction { | ||
(key: string): Promise<Uint8Array | undefined> | ||
/** | ||
* The key is the identifier requested by the remote peer | ||
*/ | ||
(key: Uint8Array): Promise<Uint8Array | undefined> | ||
} | ||
@@ -75,3 +83,3 @@ | ||
*/ | ||
fetch(peer: PeerId, key: string, options?: AbortOptions): Promise<Uint8Array | undefined> | ||
fetch(peer: PeerId, key: string | Uint8Array, options?: AbortOptions): Promise<Uint8Array | undefined> | ||
@@ -78,0 +86,0 @@ /** |
@@ -12,3 +12,3 @@ /* eslint-disable import/export */ | ||
export interface FetchRequest { | ||
identifier: string | ||
identifier: Uint8Array | ||
} | ||
@@ -26,5 +26,5 @@ | ||
if ((obj.identifier != null && obj.identifier !== '')) { | ||
if ((obj.identifier != null && obj.identifier.byteLength > 0)) { | ||
w.uint32(10) | ||
w.string(obj.identifier) | ||
w.bytes(obj.identifier) | ||
} | ||
@@ -37,3 +37,3 @@ | ||
const obj: any = { | ||
identifier: '' | ||
identifier: uint8ArrayAlloc(0) | ||
} | ||
@@ -48,3 +48,3 @@ | ||
case 1: { | ||
obj.identifier = reader.string() | ||
obj.identifier = reader.bytes() | ||
break | ||
@@ -51,0 +51,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
106665
1238
95