UNPKG

@multiformats/multiaddr-to-uri

Version:
5 lines (4 loc) 34.6 kB
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.MultiformatsMultiaddrToUri = factory()}(typeof self !== 'undefined' ? self : this, function () { "use strict";var MultiformatsMultiaddrToUri=(()=>{var le=Object.defineProperty;var Gt=Object.getOwnPropertyDescriptor;var Xt=Object.getOwnPropertyNames;var Jt=Object.prototype.hasOwnProperty;var g=(e,r)=>{for(var t in r)le(e,t,{get:r[t],enumerable:!0})},Qt=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of Xt(r))!Jt.call(e,o)&&o!==t&&le(e,o,{get:()=>r[o],enumerable:!(n=Gt(r,o))||n.enumerable});return e};var Kt=e=>Qt(le({},"__esModule",{value:!0}),e);var so={};g(so,{multiaddrToUri:()=>io});function tt(e,r){if(e===r)return!0;if(e.byteLength!==r.byteLength)return!1;for(let t=0;t<e.byteLength;t++)if(e[t]!==r[t])return!1;return!0}function G(e=0){return new Uint8Array(e)}var Zt=Math.pow(2,7),Yt=Math.pow(2,14),er=Math.pow(2,21),fe=Math.pow(2,28),he=Math.pow(2,35),me=Math.pow(2,42),xe=Math.pow(2,49),u=128,x=127;function R(e){if(e<Zt)return 1;if(e<Yt)return 2;if(e<er)return 3;if(e<fe)return 4;if(e<he)return 5;if(e<me)return 6;if(e<xe)return 7;if(Number.MAX_SAFE_INTEGER!=null&&e>Number.MAX_SAFE_INTEGER)throw new RangeError("Could not encode varint");return 8}function we(e,r,t=0){switch(R(e)){case 8:r[t++]=e&255|u,e/=128;case 7:r[t++]=e&255|u,e/=128;case 6:r[t++]=e&255|u,e/=128;case 5:r[t++]=e&255|u,e/=128;case 4:r[t++]=e&255|u,e>>>=7;case 3:r[t++]=e&255|u,e>>>=7;case 2:r[t++]=e&255|u,e>>>=7;case 1:{r[t++]=e&255,e>>>=7;break}default:throw new Error("unreachable")}return r}function tr(e,r){let t=e[r],n=0;if(n+=t&x,t<u||(t=e[r+1],n+=(t&x)<<7,t<u)||(t=e[r+2],n+=(t&x)<<14,t<u)||(t=e[r+3],n+=(t&x)<<21,t<u)||(t=e[r+4],n+=(t&x)*fe,t<u)||(t=e[r+5],n+=(t&x)*he,t<u)||(t=e[r+6],n+=(t&x)*me,t<u)||(t=e[r+7],n+=(t&x)*xe,t<u))return n;throw new RangeError("Could not decode varint")}function rr(e,r){let t=e.get(r),n=0;if(n+=t&x,t<u||(t=e.get(r+1),n+=(t&x)<<7,t<u)||(t=e.get(r+2),n+=(t&x)<<14,t<u)||(t=e.get(r+3),n+=(t&x)<<21,t<u)||(t=e.get(r+4),n+=(t&x)*fe,t<u)||(t=e.get(r+5),n+=(t&x)*he,t<u)||(t=e.get(r+6),n+=(t&x)*me,t<u)||(t=e.get(r+7),n+=(t&x)*xe,t<u))return n;throw new RangeError("Could not decode varint")}function ge(e,r=0){return e instanceof Uint8Array?tr(e,r):rr(e,r)}function X(e,r){r==null&&(r=e.reduce((o,i)=>o+i.length,0));let t=G(r),n=0;for(let o of e)t.set(o,n),n+=o.length;return t}var ve={};g(ve,{base10:()=>ur});var wo=new Uint8Array(0);function rt(e,r){if(e===r)return!0;if(e.byteLength!==r.byteLength)return!1;for(let t=0;t<e.byteLength;t++)if(e[t]!==r[t])return!1;return!0}function O(e){if(e instanceof Uint8Array&&e.constructor.name==="Uint8Array")return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Unknown type, must be binary type")}function nt(e){return new TextEncoder().encode(e)}function ot(e){return new TextDecoder().decode(e)}function or(e,r){if(e.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),n=0;n<t.length;n++)t[n]=255;for(var o=0;o<e.length;o++){var i=e.charAt(o),s=i.charCodeAt(0);if(t[s]!==255)throw new TypeError(i+" is ambiguous");t[s]=o}var a=e.length,c=e.charAt(0),l=Math.log(a)/Math.log(256),f=Math.log(256)/Math.log(a);function I(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 w=0,z=0,y=0,v=d.length;y!==v&&d[y]===0;)y++,w++;for(var D=(v-y)*f+1>>>0,b=new Uint8Array(D);y!==v;){for(var S=d[y],_=0,E=D-1;(S!==0||_<z)&&E!==-1;E--,_++)S+=256*b[E]>>>0,b[E]=S%a>>>0,S=S/a>>>0;if(S!==0)throw new Error("Non-zero carry");z=_,y++}for(var A=D-z;A!==D&&b[A]===0;)A++;for(var ne=c.repeat(w);A<D;++A)ne+=e.charAt(b[A]);return ne}function L(d){if(typeof d!="string")throw new TypeError("Expected String");if(d.length===0)return new Uint8Array;var w=0;if(d[w]!==" "){for(var z=0,y=0;d[w]===c;)z++,w++;for(var v=(d.length-w)*l+1>>>0,D=new Uint8Array(v);d[w];){var b=t[d.charCodeAt(w)];if(b===255)return;for(var S=0,_=v-1;(b!==0||S<y)&&_!==-1;_--,S++)b+=a*D[_]>>>0,D[_]=b%256>>>0,b=b/256>>>0;if(b!==0)throw new Error("Non-zero carry");y=S,w++}if(d[w]!==" "){for(var E=v-y;E!==v&&D[E]===0;)E++;for(var A=new Uint8Array(z+(v-E)),ne=z;E!==v;)A[ne++]=D[E++];return A}}}function jt(d){var w=L(d);if(w)return w;throw new Error(`Non-${r} character`)}return{encode:I,decodeUnsafe:L,decode:jt}}var ir=or,sr=ir,st=sr;var be=class{name;prefix;baseEncode;constructor(r,t,n){this.name=r,this.prefix=t,this.baseEncode=n}encode(r){if(r instanceof Uint8Array)return`${this.prefix}${this.baseEncode(r)}`;throw Error("Unknown type, must be binary type")}},ye=class{name;prefix;baseDecode;prefixCodePoint;constructor(r,t,n){this.name=r,this.prefix=t;let o=t.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,this.baseDecode=n}decode(r){if(typeof r=="string"){if(r.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(r)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(r.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(r){return at(this,r)}},Ee=class{decoders;constructor(r){this.decoders=r}or(r){return at(this,r)}decode(r){let t=r[0],n=this.decoders[t];if(n!=null)return n.decode(r);throw RangeError(`Unable to decode multibase string ${JSON.stringify(r)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function at(e,r){return new Ee({...e.decoders??{[e.prefix]:e},...r.decoders??{[r.prefix]:r}})}var Ce=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(r,t,n,o){this.name=r,this.prefix=t,this.baseEncode=n,this.baseDecode=o,this.encoder=new be(r,t,n),this.decoder=new ye(r,t,o)}encode(r){return this.encoder.encode(r)}decode(r){return this.decoder.decode(r)}};function M({name:e,prefix:r,encode:t,decode:n}){return new Ce(e,r,t,n)}function N({name:e,prefix:r,alphabet:t}){let{encode:n,decode:o}=st(t,e);return M({prefix:r,name:e,encode:n,decode:i=>O(o(i))})}function ar(e,r,t,n){let o=e.length;for(;e[o-1]==="=";)--o;let i=new Uint8Array(o*t/8|0),s=0,a=0,c=0;for(let l=0;l<o;++l){let f=r[e[l]];if(f===void 0)throw new SyntaxError(`Non-${n} character`);a=a<<t|f,s+=t,s>=8&&(s-=8,i[c++]=255&a>>s)}if(s>=t||(255&a<<8-s)!==0)throw new SyntaxError("Unexpected end of data");return i}function cr(e,r,t){let n=r[r.length-1]==="=",o=(1<<t)-1,i="",s=0,a=0;for(let c=0;c<e.length;++c)for(a=a<<8|e[c],s+=8;s>t;)s-=t,i+=r[o&a>>s];if(s!==0&&(i+=r[o&a<<t-s]),n)for(;(i.length*t&7)!==0;)i+="=";return i}function dr(e){let r={};for(let t=0;t<e.length;++t)r[e[t]]=t;return r}function p({name:e,prefix:r,bitsPerChar:t,alphabet:n}){let o=dr(n);return M({prefix:r,name:e,encode(i){return cr(i,n,t)},decode(i){return ar(i,o,t,e)}})}var ur=N({prefix:"9",name:"base10",alphabet:"0123456789"});var De={};g(De,{base16:()=>pr,base16upper:()=>lr});var pr=p({prefix:"f",name:"base16",alphabet:"0123456789abcdef",bitsPerChar:4}),lr=p({prefix:"F",name:"base16upper",alphabet:"0123456789ABCDEF",bitsPerChar:4});var Se={};g(Se,{base2:()=>fr});var fr=p({prefix:"0",name:"base2",alphabet:"01",bitsPerChar:1});var Te={};g(Te,{base256emoji:()=>gr});var ct=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}"),hr=ct.reduce((e,r,t)=>(e[t]=r,e),[]),mr=ct.reduce((e,r,t)=>{let n=r.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${r}`);return e[n]=t,e},[]);function xr(e){return e.reduce((r,t)=>(r+=hr[t],r),"")}function wr(e){let r=[];for(let t of e){let n=t.codePointAt(0);if(n==null)throw new Error(`Invalid character: ${t}`);let o=mr[n];if(o==null)throw new Error(`Non-base256emoji character: ${t}`);r.push(o)}return new Uint8Array(r)}var gr=M({prefix:"\u{1F680}",name:"base256emoji",encode:xr,decode:wr});var Ie={};g(Ie,{base32:()=>B,base32hex:()=>Cr,base32hexpad:()=>Dr,base32hexpadupper:()=>Sr,base32hexupper:()=>vr,base32pad:()=>yr,base32padupper:()=>Er,base32upper:()=>br,base32z:()=>Tr});var B=p({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),br=p({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),yr=p({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Er=p({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Cr=p({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),vr=p({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Dr=p({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Sr=p({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Tr=p({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Ae={};g(Ae,{base36:()=>J,base36upper:()=>Ir});var J=N({prefix:"k",name:"base36",alphabet:"0123456789abcdefghijklmnopqrstuvwxyz"}),Ir=N({prefix:"K",name:"base36upper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"});var Oe={};g(Oe,{base58btc:()=>T,base58flickr:()=>Ar});var T=N({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Ar=N({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ue={};g(Ue,{base64:()=>Or,base64pad:()=>Pr,base64url:()=>Pe,base64urlpad:()=>Ur});var Or=p({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Pr=p({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),Pe=p({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Ur=p({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var _e={};g(_e,{base8:()=>_r});var _r=p({prefix:"7",name:"base8",alphabet:"01234567",bitsPerChar:3});var Ne={};g(Ne,{identity:()=>Nr});var Nr=M({prefix:"\0",name:"identity",encode:e=>ot(e),decode:e=>nt(e)});var No=new TextEncoder,Bo=new TextDecoder;var Le={};g(Le,{identity:()=>rn});var zr=pt,dt=128,$r=127,Vr=~$r,Rr=Math.pow(2,31);function pt(e,r,t){r=r||[],t=t||0;for(var n=t;e>=Rr;)r[t++]=e&255|dt,e/=128;for(;e&Vr;)r[t++]=e&255|dt,e>>>=7;return r[t]=e|0,pt.bytes=t-n+1,r}var Mr=Be,Fr=128,ut=127;function Be(e,n){var t=0,n=n||0,o=0,i=n,s,a=e.length;do{if(i>=a)throw Be.bytes=0,new RangeError("Could not decode varint");s=e[i++],t+=o<28?(s&ut)<<o:(s&ut)*Math.pow(2,o),o+=7}while(s>=Fr);return Be.bytes=i-n,t}var Wr=Math.pow(2,7),qr=Math.pow(2,14),kr=Math.pow(2,21),Hr=Math.pow(2,28),jr=Math.pow(2,35),Gr=Math.pow(2,42),Xr=Math.pow(2,49),Jr=Math.pow(2,56),Qr=Math.pow(2,63),Kr=function(e){return e<Wr?1:e<qr?2:e<kr?3:e<Hr?4:e<jr?5:e<Gr?6:e<Xr?7:e<Jr?8:e<Qr?9:10},Zr={encode:zr,decode:Mr,encodingLength:Kr},Yr=Zr,Q=Yr;function K(e,r=0){return[Q.decode(e,r),Q.decode.bytes]}function F(e,r,t=0){return Q.encode(e,r,t),r}function W(e){return Q.encodingLength(e)}function k(e,r){let t=r.byteLength,n=W(e),o=n+W(t),i=new Uint8Array(o+t);return F(e,i,0),F(t,i,n),i.set(r,o),new q(e,t,r,i)}function lt(e){let r=O(e),[t,n]=K(r),[o,i]=K(r.subarray(n)),s=r.subarray(n+i);if(s.byteLength!==o)throw new Error("Incorrect length");return new q(t,o,s,r)}function ft(e,r){if(e===r)return!0;{let t=r;return e.code===t.code&&e.size===t.size&&t.bytes instanceof Uint8Array&&rt(e.bytes,t.bytes)}}var q=class{code;size;digest;bytes;constructor(r,t,n,o){this.code=r,this.size=t,this.digest=n,this.bytes=o}};var ht=0,en="identity",mt=O;function tn(e,r){if(r?.truncate!=null&&r.truncate!==e.byteLength){if(r.truncate<0||r.truncate>e.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${e.byteLength}`);e=e.subarray(0,r.truncate)}return k(ht,mt(e))}var rn={code:ht,name:en,encode:mt,digest:tn};var Ve={};g(Ve,{sha256:()=>on,sha512:()=>sn});var nn=20;function $e({name:e,code:r,encode:t,minDigestLength:n,maxDigestLength:o}){return new ze(e,r,t,n,o)}var ze=class{name;code;encode;minDigestLength;maxDigestLength;constructor(r,t,n,o,i){this.name=r,this.code=t,this.encode=n,this.minDigestLength=o??nn,this.maxDigestLength=i}digest(r,t){if(t?.truncate!=null){if(t.truncate<this.minDigestLength)throw new Error(`Invalid truncate option, must be greater than or equal to ${this.minDigestLength}`);if(this.maxDigestLength!=null&&t.truncate>this.maxDigestLength)throw new Error(`Invalid truncate option, must be less than or equal to ${this.maxDigestLength}`)}if(r instanceof Uint8Array){let n=this.encode(r);return n instanceof Uint8Array?xt(n,this.code,t?.truncate):n.then(o=>xt(o,this.code,t?.truncate))}else throw Error("Unknown type, must be binary type")}};function xt(e,r,t){if(t!=null&&t!==e.byteLength){if(t>e.byteLength)throw new Error(`Invalid truncate option, must be less than or equal to ${e.byteLength}`);e=e.subarray(0,t)}return k(r,e)}function gt(e){return async r=>new Uint8Array(await crypto.subtle.digest(e,r))}var on=$e({name:"sha2-256",code:18,encode:gt("SHA-256")}),sn=$e({name:"sha2-512",code:19,encode:gt("SHA-512")});function bt(e,r){let{bytes:t,version:n}=e;switch(n){case 0:return cn(t,Re(e),r??T.encoder);default:return dn(t,Re(e),r??B.encoder)}}var yt=new WeakMap;function Re(e){let r=yt.get(e);if(r==null){let t=new Map;return yt.set(e,t),t}return r}var H=class e{code;version;multihash;bytes;"/";constructor(r,t,n,o){this.code=t,this.version=r,this.multihash=n,this.bytes=o,this["/"]=o}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:r,multihash:t}=this;if(r!==Y)throw new Error("Cannot convert a non dag-pb CID to CIDv0");if(t.code!==un)throw new Error("Cannot convert non sha2-256 multihash CID to CIDv0");return e.createV0(t)}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:r,digest:t}=this.multihash,n=k(r,t);return e.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(r){return e.equals(this,r)}static equals(r,t){let n=t;return n!=null&&r.code===n.code&&r.version===n.version&&ft(r.multihash,n.multihash)}toString(r){return bt(this,r)}toJSON(){return{"/":bt(this)}}link(){return this}[Symbol.toStringTag]="CID";[Symbol.for("nodejs.util.inspect.custom")](){return`CID(${this.toString()})`}static asCID(r){if(r==null)return null;let t=r;if(t instanceof e)return t;if(t["/"]!=null&&t["/"]===t.bytes||t.asCID===t){let{version:n,code:o,multihash:i,bytes:s}=t;return new e(n,o,i,s??Et(n,o,i.bytes))}else if(t[pn]===!0){let{version:n,multihash:o,code:i}=t,s=lt(o);return e.create(n,i,s)}else return null}static create(r,t,n){if(typeof t!="number")throw new Error("String codecs are no longer supported");if(!(n.bytes instanceof Uint8Array))throw new Error("Invalid digest");switch(r){case 0:{if(t!==Y)throw new Error(`Version 0 CID must use dag-pb (code: ${Y}) block encoding`);return new e(r,t,n,n.bytes)}case 1:{let o=Et(r,t,n.bytes);return new e(r,t,n,o)}default:throw new Error("Invalid version")}}static createV0(r){return e.create(0,Y,r)}static createV1(r,t){return e.create(1,r,t)}static decode(r){let[t,n]=e.decodeFirst(r);if(n.length!==0)throw new Error("Incorrect length");return t}static decodeFirst(r){let t=e.inspectBytes(r),n=t.size-t.multihashSize,o=O(r.subarray(n,n+t.multihashSize));if(o.byteLength!==t.multihashSize)throw new Error("Incorrect length");let i=o.subarray(t.multihashSize-t.digestSize),s=new q(t.multihashCode,t.digestSize,i,o);return[t.version===0?e.createV0(s):e.createV1(t.codec,s),r.subarray(t.size)]}static inspectBytes(r){let t=0,n=()=>{let[I,L]=K(r.subarray(t));return t+=L,I},o=n(),i=Y;if(o===18?(o=0,t=0):i=n(),o!==0&&o!==1)throw new RangeError(`Invalid CID version ${o}`);let s=t,a=n(),c=n(),l=t+c,f=l-s;return{version:o,codec:i,multihashCode:a,digestSize:c,multihashSize:f,size:l}}static parse(r,t){let[n,o]=an(r,t),i=e.decode(o);if(i.version===0&&r[0]!=="Q")throw Error("Version 0 CID string must not include multibase prefix");return Re(i).set(n,r),i}};function an(e,r){switch(e[0]){case"Q":{let t=r??T;return[T.prefix,t.decode(`${T.prefix}${e}`)]}case T.prefix:{let t=r??T;return[T.prefix,t.decode(e)]}case B.prefix:{let t=r??B;return[B.prefix,t.decode(e)]}case J.prefix:{let t=r??J;return[J.prefix,t.decode(e)]}default:{if(r==null)throw Error("To parse non base32, base36 or base58btc encoded CID multibase decoder must be provided");return[e[0],r.decode(e)]}}}function cn(e,r,t){let{prefix:n}=t;if(n!==T.prefix)throw Error(`Cannot string encode V0 in ${t.name} encoding`);let o=r.get(n);if(o==null){let i=t.encode(e).slice(1);return r.set(n,i),i}else return o}function dn(e,r,t){let{prefix:n}=t,o=r.get(n);if(o==null){let i=t.encode(e);return r.set(n,i),i}else return o}var Y=112,un=18;function Et(e,r,t){let n=W(e),o=n+W(r),i=new Uint8Array(o+t.byteLength);return F(e,i,0),F(r,i,n),i.set(t,o),i}var pn=Symbol.for("@ipld/js-cid/CID");var ee={...Ne,...Se,..._e,...ve,...De,...Ie,...Ae,...Oe,...Ue,...Te},ri={...Ve,...Le};function vt(e,r,t,n){return{name:e,prefix:r,encoder:{name:e,prefix:r,encode:t},decoder:{decode:n}}}var Ct=vt("utf8","u",e=>"u"+new TextDecoder("utf8").decode(e),e=>new TextEncoder().encode(e.substring(1))),Me=vt("ascii","a",e=>{let r="a";for(let t=0;t<e.length;t++)r+=String.fromCharCode(e[t]);return r},e=>{e=e.substring(1);let r=G(e.length);for(let t=0;t<e.length;t++)r[t]=e.charCodeAt(t);return r}),ln={utf8:Ct,"utf-8":Ct,hex:ee.base16,latin1:Me,ascii:Me,binary:Me,...ee},ie=ln;function te(e,r="utf8"){let t=ie[r];if(t==null)throw new Error(`Unsupported encoding "${r}"`);return t.decoder.decode(`${t.prefix}${e}`)}function $(e,r="utf8"){let t=ie[r];if(t==null)throw new Error(`Unsupported encoding "${r}"`);return t.encoder.encode(e).substring(1)}var m=class extends Error{static name="InvalidMultiaddrError";name="InvalidMultiaddrError"},P=class extends Error{static name="ValidationError";name="ValidationError"},se=class extends Error{static name="InvalidParametersError";name="InvalidParametersError"},ae=class extends Error{static name="UnknownProtocolError";name="UnknownProtocolError"};var ce=class{index=0;input="";new(r){return this.index=0,this.input=r,this}readAtomically(r){let t=this.index,n=r();return n===void 0&&(this.index=t),n}parseWith(r){let t=r();if(this.index===this.input.length)return t}peekChar(){if(!(this.index>=this.input.length))return this.input[this.index]}readChar(){if(!(this.index>=this.input.length))return this.input[this.index++]}readGivenChar(r){return this.readAtomically(()=>{let t=this.readChar();if(t===r)return t})}readSeparator(r,t,n){return this.readAtomically(()=>{if(!(t>0&&this.readGivenChar(r)===void 0))return n()})}readNumber(r,t,n,o){return this.readAtomically(()=>{let i=0,s=0,a=this.peekChar();if(a===void 0)return;let c=a==="0",l=2**(8*o)-1;for(;;){let f=this.readAtomically(()=>{let I=this.readChar();if(I===void 0)return;let L=Number.parseInt(I,r);if(!Number.isNaN(L))return L});if(f===void 0)break;if(i*=r,i+=f,i>l||(s+=1,t!==void 0&&s>t))return}if(s!==0)return!n&&c&&s>1?void 0:i})}readIPv4Addr(){return this.readAtomically(()=>{let r=new Uint8Array(4);for(let t=0;t<r.length;t++){let n=this.readSeparator(".",t,()=>this.readNumber(10,3,!1,1));if(n===void 0)return;r[t]=n}return r})}readIPv6Addr(){let r=t=>{for(let n=0;n<t.length/2;n++){let o=n*2;if(n<t.length-3){let s=this.readSeparator(":",n,()=>this.readIPv4Addr());if(s!==void 0)return t[o]=s[0],t[o+1]=s[1],t[o+2]=s[2],t[o+3]=s[3],[o+4,!0]}let i=this.readSeparator(":",n,()=>this.readNumber(16,4,!0,2));if(i===void 0)return[o,!1];t[o]=i>>8,t[o+1]=i&255}return[t.length,!1]};return this.readAtomically(()=>{let t=new Uint8Array(16),[n,o]=r(t);if(n===16)return t;if(o||this.readGivenChar(":")===void 0||this.readGivenChar(":")===void 0)return;let i=new Uint8Array(14),s=16-(n+2),[a]=r(i.subarray(0,s));return t.set(i.subarray(0,a),16-a),t})}readIPAddr(){return this.readIPv4Addr()??this.readIPv6Addr()}};var fn=45,hn=15,de=new ce;function Dt(e){if(!(e.length>hn))return de.new(e).parseWith(()=>de.readIPv4Addr())}function St(e){if(e.includes("%")&&(e=e.split("%")[0]),!(e.length>fn))return de.new(e).parseWith(()=>de.readIPv6Addr())}function ue(e){return!!Dt(e)}function Tt(e){return!!St(e)}function We(e){return r=>$(r,e)}function qe(e){return r=>te(r,e)}function j(e){return new DataView(e.buffer).getUint16(e.byteOffset).toString()}function V(e){let r=new ArrayBuffer(2);return new DataView(r).setUint16(0,typeof e=="string"?parseInt(e):e),new Uint8Array(r)}function It(e){let r=e.split(":");if(r.length!==2)throw new Error(`failed to parse onion addr: ["'${r.join('", "')}'"]' does not contain a port number`);if(r[0].length!==16)throw new Error(`failed to parse onion addr: ${r[0]} not a Tor onion address.`);let t=te(r[0],"base32"),n=parseInt(r[1],10);if(n<1||n>65536)throw new Error("Port number is not in range(1, 65536)");let o=V(n);return X([t,o],t.length+o.length)}function At(e){let r=e.split(":");if(r.length!==2)throw new Error(`failed to parse onion addr: ["'${r.join('", "')}'"]' does not contain a port number`);if(r[0].length!==56)throw new Error(`failed to parse onion addr: ${r[0]} not a Tor onion3 address.`);let t=B.decode(`b${r[0]}`),n=parseInt(r[1],10);if(n<1||n>65536)throw new Error("Port number is not in range(1, 65536)");let o=V(n);return X([t,o],t.length+o.length)}function ke(e){let r=e.subarray(0,e.length-2),t=e.subarray(e.length-2),n=$(r,"base32"),o=j(t);return`${n}:${o}`}var He=function(e){e=e.toString().trim();let r=new Uint8Array(4);return e.split(/\./g).forEach((t,n)=>{let o=parseInt(t,10);if(isNaN(o)||o<0||o>255)throw new m("Invalid byte value in IP address");r[n]=o}),r},Ot=function(e){let r=0;e=e.toString().trim();let t=e.split(":",8),n;for(n=0;n<t.length;n++){let i=ue(t[n]),s;i&&(s=He(t[n]),t[n]=$(s.subarray(0,2),"base16")),s!=null&&++n<8&&t.splice(n,0,$(s.subarray(2,4),"base16"))}if(t[0]==="")for(;t.length<8;)t.unshift("0");else if(t[t.length-1]==="")for(;t.length<8;)t.push("0");else if(t.length<8){for(n=0;n<t.length&&t[n]!=="";n++);let i=[n,1];for(n=9-t.length;n>0;n--)i.push("0");t.splice.apply(t,i)}let o=new Uint8Array(r+16);for(n=0;n<t.length;n++){t[n]===""&&(t[n]="0");let i=parseInt(t[n],16);if(isNaN(i)||i<0||i>65535)throw new m("Invalid byte value in IP address");o[r++]=i>>8&255,o[r++]=i&255}return o},Pt=function(e){if(e.byteLength!==4)throw new m("IPv4 address was incorrect length");let r=[];for(let t=0;t<e.byteLength;t++)r.push(e[t]);return r.join(".")},Ut=function(e){if(e.byteLength!==16)throw new m("IPv6 address was incorrect length");let r=[];for(let n=0;n<e.byteLength;n+=2){let o=e[n],i=e[n+1],s=`${o.toString(16).padStart(2,"0")}${i.toString(16).padStart(2,"0")}`;r.push(s)}let t=r.join(":");try{let n=new URL(`http://[${t}]`);return n.hostname.substring(1,n.hostname.length-1)}catch{throw new m(`Invalid IPv6 address "${t}"`)}};function _t(e){try{let r=new URL(`http://[${e}]`);return r.hostname.substring(1,r.hostname.length-1)}catch{throw new m(`Invalid IPv6 address "${e}"`)}}var Fe=Object.values(ee).map(e=>e.decoder),mn=(function(){let e=Fe[0].or(Fe[1]);return Fe.slice(2).forEach(r=>e=e.or(r)),e})();function Nt(e){return mn.decode(e)}function Bt(e){return r=>e.encoder.encode(r)}function xn(e){if(parseInt(e).toString()!==e)throw new P("Value must be an integer")}function wn(e){if(e<0)throw new P("Value must be a positive integer, or zero")}function gn(e){return r=>{if(r>e)throw new P(`Value must be smaller than or equal to ${e}`)}}function bn(...e){return r=>{for(let t of e)t(r)}}var re=bn(xn,wn,gn(65535));var h=-1,je=class{protocolsByCode=new Map;protocolsByName=new Map;getProtocol(r){let t;if(typeof r=="string"?t=this.protocolsByName.get(r):t=this.protocolsByCode.get(r),t==null)throw new ae(`Protocol ${r} was unknown`);return t}addProtocol(r){this.protocolsByCode.set(r.code,r),this.protocolsByName.set(r.name,r),r.aliases?.forEach(t=>{this.protocolsByName.set(t,r)})}removeProtocol(r){let t=this.protocolsByCode.get(r);t!=null&&(this.protocolsByCode.delete(t.code),this.protocolsByName.delete(t.name),t.aliases?.forEach(n=>{this.protocolsByName.delete(n)}))}},U=new je,Yn=[{code:4,name:"ip4",size:32,valueToBytes:He,bytesToValue:Pt,validate:e=>{if(!ue(e))throw new P(`Invalid IPv4 address "${e}"`)}},{code:6,name:"tcp",size:16,valueToBytes:V,bytesToValue:j,validate:re},{code:273,name:"udp",size:16,valueToBytes:V,bytesToValue:j,validate:re},{code:33,name:"dccp",size:16,valueToBytes:V,bytesToValue:j,validate:re},{code:41,name:"ip6",size:128,valueToBytes:Ot,bytesToValue:Ut,stringToValue:_t,validate:e=>{if(!Tt(e))throw new P(`Invalid IPv6 address "${e}"`)}},{code:42,name:"ip6zone",size:h},{code:43,name:"ipcidr",size:8,bytesToValue:We("base10"),valueToBytes:qe("base10")},{code:53,name:"dns",size:h},{code:54,name:"dns4",size:h},{code:55,name:"dns6",size:h},{code:56,name:"dnsaddr",size:h},{code:132,name:"sctp",size:16,valueToBytes:V,bytesToValue:j,validate:re},{code:301,name:"udt"},{code:302,name:"utp"},{code:400,name:"unix",size:h,stringToValue:e=>decodeURIComponent(e),valueToString:e=>encodeURIComponent(e)},{code:421,name:"p2p",aliases:["ipfs"],size:h,bytesToValue:We("base58btc"),valueToBytes:e=>e.startsWith("Q")||e.startsWith("1")?qe("base58btc")(e):H.parse(e).multihash.bytes},{code:444,name:"onion",size:96,bytesToValue:ke,valueToBytes:It},{code:445,name:"onion3",size:296,bytesToValue:ke,valueToBytes:At},{code:446,name:"garlic64",size:h},{code:447,name:"garlic32",size:h},{code:448,name:"tls"},{code:449,name:"sni",size:h},{code:454,name:"noise"},{code:460,name:"quic"},{code:461,name:"quic-v1"},{code:465,name:"webtransport"},{code:466,name:"certhash",size:h,bytesToValue:Bt(Pe),valueToBytes:Nt},{code:480,name:"http"},{code:481,name:"http-path",size:h,stringToValue:e=>`/${decodeURIComponent(e)}`,valueToString:e=>encodeURIComponent(e.substring(1))},{code:443,name:"https"},{code:477,name:"ws"},{code:478,name:"wss"},{code:479,name:"p2p-websocket-star"},{code:277,name:"p2p-stardust"},{code:275,name:"p2p-webrtc-star"},{code:276,name:"p2p-webrtc-direct"},{code:280,name:"webrtc-direct"},{code:281,name:"webrtc"},{code:290,name:"p2p-circuit"},{code:777,name:"memory",size:h}];Yn.forEach(e=>{U.addProtocol(e)});function Lt(e){let r=[],t=0;for(;t<e.length;){let n=ge(e,t),o=U.getProtocol(n),i=R(n),s=eo(o,e,t+i),a=0;s>0&&o.size===h&&(a=R(s));let c=i+a+s,l={code:n,name:o.name,bytes:e.subarray(t,t+c)};if(s>0){let f=t+i+a,I=e.subarray(f,f+s);l.value=o.bytesToValue?.(I)??$(I)}r.push(l),t+=c}return r}function zt(e){let r=0,t=[];for(let n of e){if(n.bytes==null){let o=U.getProtocol(n.code),i=R(n.code),s,a=0,c=0;n.value!=null&&(s=o.valueToBytes?.(n.value)??te(n.value),a=s.byteLength,o.size===h&&(c=R(a)));let l=new Uint8Array(i+c+a),f=0;we(n.code,l,f),f+=i,s!=null&&(o.size===h&&(we(a,l,f),f+=c),l.set(s,f)),n.bytes=l}t.push(n.bytes),r+=n.bytes.byteLength}return X(t,r)}function $t(e){if(e.charAt(0)!=="/")throw new m('String multiaddr must start with "/"');let r=[],t="protocol",n="",o="";for(let i=1;i<e.length;i++){let s=e.charAt(i);s!=="/"&&(t==="protocol"?o+=e.charAt(i):n+=e.charAt(i));let a=i===e.length-1;if(s==="/"||a){let c=U.getProtocol(o);if(t==="protocol"){if(c.size==null||c.size===0){r.push({code:c.code,name:c.name}),n="",o="",t="protocol";continue}else if(a)throw new m(`Component ${o} was missing value`);t="value"}else if(t==="value"){let l={code:c.code,name:c.name};if(c.size!=null&&c.size!==0){if(n==="")throw new m(`Component ${o} was missing value`);l.value=c.stringToValue?.(n)??n}r.push(l),n="",o="",t="protocol"}}}if(o!==""&&n!=="")throw new m("Incomplete multiaddr");return r}function Vt(e){return`/${e.flatMap(r=>{if(r.value==null)return r.name;let t=U.getProtocol(r.code);if(t==null)throw new m(`Unknown protocol code ${r.code}`);return[r.name,t.valueToString?.(r.value)??r.value]}).join("/")}`}function eo(e,r,t){return e.size==null||e.size===0?0:e.size>0?e.size/8:ge(r,t)}var to=Symbol.for("nodejs.util.inspect.custom"),et=Symbol.for("@multiformats/multiaddr");function ro(e){if(e==null&&(e="/"),Rt(e))return e.getComponents();if(e instanceof Uint8Array)return Lt(e);if(typeof e=="string")return e=e.replace(/\/(\/)+/,"/").replace(/(\/)+$/,""),e===""&&(e="/"),$t(e);if(Array.isArray(e))return e;throw new m("Must be a string, Uint8Array, Component[], or another Multiaddr")}var pe=class e{[et]=!0;#e;#t;#r;constructor(r="/",t={}){this.#e=ro(r),t.validate!==!1&&no(this)}get bytes(){return this.#r==null&&(this.#r=zt(this.#e)),this.#r}toString(){return this.#t==null&&(this.#t=Vt(this.#e)),this.#t}toJSON(){return this.toString()}getComponents(){return[...this.#e]}encapsulate(r){let t=new e(r);return new e([...this.#e,...t.getComponents()],{validate:!1})}decapsulate(r){let t=r.toString(),n=this.toString(),o=n.lastIndexOf(t);if(o<0)throw new se(`Address ${this.toString()} does not contain subaddress: ${t}`);return new e(n.slice(0,o),{validate:!1})}decapsulateCode(r){let t;for(let n=this.#e.length-1;n>-1;n--)if(this.#e[n].code===r){t=n;break}return new e(this.#e.slice(0,t),{validate:!1})}equals(r){return tt(this.bytes,r.bytes)}[to](){return`Multiaddr(${this.toString()})`}};function no(e){e.getComponents().forEach(r=>{let t=U.getProtocol(r.code);r.value!=null&&t.validate?.(r.value)})}function Rt(e){return!!e?.[et]}function Mt(e){return new pe(e)}var oo=[6,53,56,54,55];function Ft(e){return kt("sni",e)?.value}function Wt(e){let r=kt("tcp",e)?.value;return r==null?"":`:${r}`}function kt(e,r){return r.find(t=>t.name===e)}function qt(e){return e.some(({code:r})=>r===448)}function C(e,r){let t=Ht[e.name];if(t==null)throw new Error(`Can't interpret protocol ${e.name}`);let n=t(e,r);return e.code===41?`[${n}]`:n}var Ht={ip4:(e,r)=>e.value,ip6:(e,r)=>r.length===0?e.value:`[${e.value}]`,tcp:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return`tcp://${C(t,r)}:${e.value}`},udp:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return`udp://${C(t,r)}:${e.value}`},dnsaddr:(e,r)=>e.value,dns4:(e,r)=>e.value,dns6:(e,r)=>e.value,dns:(e,r)=>e.value,ipfs:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return`${C(t,r)}`},p2p:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return`${C(t,r)}`},http:(e,r)=>{let t=qt(r),n=Ft(r),o=Wt(r);if(t&&n!=null)return`https://${n}${o}`;let i=t?"https://":"http://",s=r.pop();if(s==null)throw new Error("Unexpected end of multiaddr");let a=C(s,r);return a=a?.replace("tcp://",""),`${i}${a}`},"http-path":(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");let n=C(t,r),o=decodeURIComponent(e.value??"");return`${n}${o}`},tls:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return C(t,r)},sni:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");return C(t,r)},https:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");let n=C(t,r);return n=n?.replace("tcp://",""),`https://${n}`},ws:(e,r)=>{let t=qt(r),n=Ft(r),o=Wt(r);if(t&&n!=null)return`wss://${n}${o}`;let i=t?"wss://":"ws://",s=r.pop();if(s==null)throw new Error("Unexpected end of multiaddr");let a=C(s,r);return a=a?.replace("tcp://",""),`${i}${a}`},wss:(e,r)=>{let t=r.pop();if(t==null)throw new Error("Unexpected end of multiaddr");let n=C(t,r);return n=n?.replace("tcp://",""),`wss://${n}`}};function io(e,r){let n=Mt(e).getComponents(),o=n.pop();if(o==null)throw new Error("Unexpected end of multiaddr");let i=Ht[o.name];if(i==null)throw new Error(`No interpreter found for ${o.name}`);let s=i(o,n)??"";return r?.assumeHttp!==!1&&oo.includes(o.code)&&(s=s.replace(/^.*:\/\//,""),o.value==="443"?s=`https://${s}`:s=`http://${s}`),(s.startsWith("http://")||s.startsWith("https://")||s.startsWith("ws://")||s.startsWith("wss://"))&&(s=new URL(s).toString(),s.endsWith("/")&&(s=s.substring(0,s.length-1))),s}return Kt(so);})(); return MultiformatsMultiaddrToUri})); //# sourceMappingURL=index.min.js.map