UNPKG

@multiformats/multiaddr-matcher

Version:
4 lines (3 loc) 10.7 kB
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.MultiformatsMultiaddrMatcher = factory()}(typeof self !== 'undefined' ? self : this, function () { "use strict";var MultiformatsMultiaddrMatcher=(()=>{var L=Object.defineProperty;var ye=Object.getOwnPropertyDescriptor;var Ae=Object.getOwnPropertyNames;var ge=Object.prototype.hasOwnProperty;var ve=(t,e)=>{for(var r in e)L(t,r,{get:e[r],enumerable:!0})},Ie=(t,e,r,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of Ae(e))!ge.call(t,i)&&i!==r&&L(t,i,{get:()=>e[i],enumerable:!(n=ye(e,i))||n.enumerable});return t};var Pe=t=>Ie(L({},"__esModule",{value:!0}),t);var ct={};ve(ct,{Circuit:()=>et,DNS:()=>Ve,DNS4:()=>Me,DNS6:()=>ke,DNSADDR:()=>ze,HTTP:()=>it,HTTPS:()=>st,IP:()=>$e,IP4:()=>Be,IP6:()=>Ge,IP_OR_DOMAIN:()=>Oe,Memory:()=>ft,P2P:()=>Ke,PEER_ID:()=>Re,QUIC:()=>qe,QUICV1:()=>He,TCP:()=>Qe,UDP:()=>Xe,WebRTC:()=>rt,WebRTCDirect:()=>Je,WebSockets:()=>je,WebSocketsSecure:()=>Fe,WebTransport:()=>Ze});var V=class{index=0;input="";new(e){return this.index=0,this.input=e,this}readAtomically(e){let r=this.index,n=e();return n===void 0&&(this.index=r),n}parseWith(e){let r=e();if(this.index===this.input.length)return r}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(e){return this.readAtomically(()=>{let r=this.readChar();if(r===e)return r})}readSeparator(e,r,n){return this.readAtomically(()=>{if(!(r>0&&this.readGivenChar(e)===void 0))return n()})}readNumber(e,r,n,i){return this.readAtomically(()=>{let o=0,d=0,u=this.peekChar();if(u===void 0)return;let m=u==="0",D=2**(8*i)-1;for(;;){let w=this.readAtomically(()=>{let S=this.readChar();if(S===void 0)return;let T=Number.parseInt(S,e);if(!Number.isNaN(T))return T});if(w===void 0)break;if(o*=e,o+=w,o>D||(d+=1,r!==void 0&&d>r))return}if(d!==0)return!n&&m&&d>1?void 0:o})}readIPv4Addr(){return this.readAtomically(()=>{let e=new Uint8Array(4);for(let r=0;r<e.length;r++){let n=this.readSeparator(".",r,()=>this.readNumber(10,3,!1,1));if(n===void 0)return;e[r]=n}return e})}readIPv6Addr(){let e=r=>{for(let n=0;n<r.length/2;n++){let i=n*2;if(n<r.length-3){let d=this.readSeparator(":",n,()=>this.readIPv4Addr());if(d!==void 0)return r[i]=d[0],r[i+1]=d[1],r[i+2]=d[2],r[i+3]=d[3],[i+4,!0]}let o=this.readSeparator(":",n,()=>this.readNumber(16,4,!0,2));if(o===void 0)return[i,!1];r[i]=o>>8,r[i+1]=o&255}return[r.length,!1]};return this.readAtomically(()=>{let r=new Uint8Array(16),[n,i]=e(r);if(n===16)return r;if(i||this.readGivenChar(":")===void 0||this.readGivenChar(":")===void 0)return;let o=new Uint8Array(14),d=16-(n+2),[u]=e(o.subarray(0,d));return r.set(o.subarray(0,u),16-u),r})}readIPAddr(){return this.readIPv4Addr()??this.readIPv6Addr()}};var _e=45,Ue=15,O=new V;function re(t){if(!(t.length>Ue))return O.new(t).parseWith(()=>O.readIPv4Addr())}function ne(t){if(t.includes("%")&&(t=t.split("%")[0]),!(t.length>_e))return O.new(t).parseWith(()=>O.readIPv6Addr())}function ie(t){return!!re(t)}function oe(t){return!!ne(t)}var wt=new Uint8Array(0);function se(t){if(t instanceof Uint8Array&&t.constructor.name==="Uint8Array")return t;if(t instanceof ArrayBuffer)return new Uint8Array(t);if(ArrayBuffer.isView(t))return new Uint8Array(t.buffer,t.byteOffset,t.byteLength);throw new Error("Unknown type, must be binary type")}function Se(t,e){if(t.length>=255)throw new TypeError("Alphabet too long");for(var r=new Uint8Array(256),n=0;n<r.length;n++)r[n]=255;for(var i=0;i<t.length;i++){var o=t.charAt(i),d=o.charCodeAt(0);if(r[d]!==255)throw new TypeError(o+" is ambiguous");r[d]=i}var u=t.length,m=t.charAt(0),D=Math.log(u)/Math.log(256),w=Math.log(256)/Math.log(u);function S(h){if(h instanceof Uint8Array||(ArrayBuffer.isView(h)?h=new Uint8Array(h.buffer,h.byteOffset,h.byteLength):Array.isArray(h)&&(h=Uint8Array.from(h))),!(h instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(h.length===0)return"";for(var x=0,C=0,y=0,g=h.length;y!==g&&h[y]===0;)y++,x++;for(var v=(g-y)*w+1>>>0,b=new Uint8Array(v);y!==g;){for(var I=h[y],U=0,A=v-1;(I!==0||U<C)&&A!==-1;A--,U++)I+=256*b[A]>>>0,b[A]=I%u>>>0,I=I/u>>>0;if(I!==0)throw new Error("Non-zero carry");C=U,y++}for(var P=v-C;P!==v&&b[P]===0;)P++;for(var z=m.repeat(x);P<v;++P)z+=t.charAt(b[P]);return z}function T(h){if(typeof h!="string")throw new TypeError("Expected String");if(h.length===0)return new Uint8Array;var x=0;if(h[x]!==" "){for(var C=0,y=0;h[x]===m;)C++,x++;for(var g=(h.length-x)*D+1>>>0,v=new Uint8Array(g);h[x];){var b=r[h.charCodeAt(x)];if(b===255)return;for(var I=0,U=g-1;(b!==0||I<y)&&U!==-1;U--,I++)b+=u*v[U]>>>0,v[U]=b%256>>>0,b=b/256>>>0;if(b!==0)throw new Error("Non-zero carry");y=I,x++}if(h[x]!==" "){for(var A=g-y;A!==g&&v[A]===0;)A++;for(var P=new Uint8Array(C+(g-A)),z=C;A!==g;)P[z++]=v[A++];return P}}}function be(h){var x=T(h);if(x)return x;throw new Error(`Non-${e} character`)}return{encode:S,decodeUnsafe:T,decode:be}}var Ce=Se,Ee=Ce,ae=Ee;var q=class{name;prefix;baseEncode;constructor(e,r,n){this.name=e,this.prefix=r,this.baseEncode=n}encode(e){if(e instanceof Uint8Array)return`${this.prefix}${this.baseEncode(e)}`;throw Error("Unknown type, must be binary type")}},H=class{name;prefix;baseDecode;prefixCodePoint;constructor(e,r,n){this.name=e,this.prefix=r;let i=r.codePointAt(0);if(i===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=i,this.baseDecode=n}decode(e){if(typeof e=="string"){if(e.codePointAt(0)!==this.prefixCodePoint)throw Error(`Unable to decode multibase string ${JSON.stringify(e)}, ${this.name} decoder only supports inputs prefixed with ${this.prefix}`);return this.baseDecode(e.slice(this.prefix.length))}else throw Error("Can only multibase decode strings")}or(e){return fe(this,e)}},j=class{decoders;constructor(e){this.decoders=e}or(e){return fe(this,e)}decode(e){let r=e[0],n=this.decoders[r];if(n!=null)return n.decode(e);throw RangeError(`Unable to decode multibase string ${JSON.stringify(e)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}};function fe(t,e){return new j({...t.decoders??{[t.prefix]:t},...e.decoders??{[e.prefix]:e}})}var F=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(e,r,n,i){this.name=e,this.prefix=r,this.baseEncode=n,this.baseDecode=i,this.encoder=new q(e,r,n),this.decoder=new H(e,r,i)}encode(e){return this.encoder.encode(e)}decode(e){return this.decoder.decode(e)}};function ce({name:t,prefix:e,encode:r,decode:n}){return new F(t,e,r,n)}function J({name:t,prefix:e,alphabet:r}){let{encode:n,decode:i}=ae(r,t);return ce({prefix:e,name:t,encode:n,decode:o=>se(i(o))})}function Ne(t,e,r,n){let i={};for(let w=0;w<e.length;++w)i[e[w]]=w;let o=t.length;for(;t[o-1]==="=";)--o;let d=new Uint8Array(o*r/8|0),u=0,m=0,D=0;for(let w=0;w<o;++w){let S=i[t[w]];if(S===void 0)throw new SyntaxError(`Non-${n} character`);m=m<<r|S,u+=r,u>=8&&(u-=8,d[D++]=255&m>>u)}if(u>=r||(255&m<<8-u)!==0)throw new SyntaxError("Unexpected end of data");return d}function De(t,e,r){let n=e[e.length-1]==="=",i=(1<<r)-1,o="",d=0,u=0;for(let m=0;m<t.length;++m)for(u=u<<8|t[m],d+=8;d>r;)d-=r,o+=e[i&u>>d];if(d!==0&&(o+=e[i&u<<r-d]),n)for(;(o.length*r&7)!==0;)o+="=";return o}function W({name:t,prefix:e,bitsPerChar:r,alphabet:n}){return ce({prefix:e,name:t,encode(i){return De(i,n,r)},decode(i){return Ne(i,n,r,t)}})}var de=J({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Pt=J({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var St=W({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Ct=W({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),ue=W({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),Et=W({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});var Te=t=>t.toString().split("/").slice(1),N=t=>({match:e=>e.length<1?!1:t(e[0])?e.slice(1):!1,pattern:"fn"}),s=t=>({match:e=>N(r=>r===t).match(e),pattern:t}),E=()=>({match:t=>N(e=>typeof e=="string").match(t),pattern:"{string}"}),R=()=>({match:t=>N(e=>!isNaN(parseInt(e))).match(t),pattern:"{number}"}),c=()=>({match:t=>{if(t.length<2||t[0]!=="p2p"&&t[0]!=="ipfs")return!1;if(t[1].startsWith("Q")||t[1].startsWith("1"))try{de.decode(`z${t[1]}`)}catch{return!1}else return!1;return t.slice(2)},pattern:"/p2p/{peerid}"}),M=()=>({match:t=>{if(t.length<2||t[0]!=="certhash")return!1;try{ue.decode(t[1])}catch{return!1}return t.slice(2)},pattern:"/certhash/{certhash}"}),f=t=>({match:e=>{let r=t.match(e);return r===!1?e:r},pattern:`optional(${t.pattern})`}),l=(...t)=>({match:e=>{let r;for(let n of t){let i=n.match(e);i!==!1&&(r==null||i.length<r.length)&&(r=i)}return r??!1},pattern:`or(${t.map(e=>e.pattern).join(", ")})`}),a=(...t)=>({match:e=>{for(let r of t){let n=r.match(e);if(n===!1)return!1;e=n}return e},pattern:`and(${t.map(e=>e.pattern).join(", ")})`});function p(...t){function e(i){let o=Te(i);for(let d of t){let u=d.match(o);if(u===!1)return!1;o=u}return o}function r(i){return e(i)!==!1}function n(i){let o=e(i);return o===!1?!1:o.length===0}return{matchers:t,matches:r,exactMatch:n}}var We=c(),Re=p(We),G=a(s("dns4"),E()),$=a(s("dns6"),E()),Q=a(s("dnsaddr"),E()),K=a(s("dns"),E()),Me=p(G,f(c())),ke=p($,f(c())),ze=p(Q,f(c())),Ve=p(l(K,Q,G,$),f(c())),he=a(s("ip4"),N(ie)),pe=a(s("ip6"),N(oe)),Y=l(he,pe),_=l(Y,K,G,$,Q),Oe=p(l(Y,a(l(K,Q,G,$),f(c())))),Be=p(he),Ge=p(pe),$e=p(Y),ee=a(_,s("tcp"),R()),k=a(_,s("udp"),R()),Qe=p(a(ee,f(c()))),Xe=p(k),te=a(k,s("quic"),f(c())),X=a(k,s("quic-v1"),f(c())),Le=l(te,X),qe=p(te),He=p(X),Z=l(_,ee,k,te,X),le=l(a(Z,s("ws"),f(c()))),je=p(le),xe=l(a(Z,s("wss"),f(c())),a(Z,s("tls"),f(a(s("sni"),E())),s("ws"),f(c()))),Fe=p(xe),me=a(k,s("webrtc-direct"),f(M()),f(M()),f(c())),Je=p(me),we=a(X,s("webtransport"),f(M()),f(M()),f(c())),Ze=p(we),B=l(le,xe,a(ee,f(c())),a(Le,f(c())),a(_,f(c())),me,we,c()),Ke=p(B),Ye=a(B,s("p2p-circuit"),c()),et=p(Ye),tt=l(a(B,s("p2p-circuit"),s("webrtc"),f(c())),a(B,s("webrtc"),f(c())),a(s("webrtc"),f(c()))),rt=p(tt),nt=l(a(_,s("tcp"),R(),s("http"),f(c())),a(_,s("http"),f(c()))),it=p(nt),ot=l(a(_,s("tcp"),l(a(s("443"),s("http")),a(R(),s("https"))),f(c())),a(_,s("tls"),s("http"),f(c())),a(_,s("https"),f(c()))),st=p(ot),at=l(a(s("memory"),E(),f(c()))),ft=p(at);return Pe(ct);})(); return MultiformatsMultiaddrMatcher}));