UNPKG

@libp2p/logger

Version:

A logging component for use in js-libp2p modules

5 lines (4 loc) • 13.9 kB
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PLogger = factory()}(typeof self !== 'undefined' ? self : this, function () { "use strict";var Libp2PLogger=(()=>{var L=Object.defineProperty;var _=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var Y=Object.prototype.hasOwnProperty;var H=(e,t)=>{for(var r in t)L(e,r,{get:t[r],enumerable:!0})},ee=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let o of W(t))!Y.call(e,o)&&o!==r&&L(e,o,{get:()=>t[o],enumerable:!(n=_(t,o))||n.enumerable});return e};var re=e=>ee(L({},"__esModule",{value:!0}),e);var Se={};H(Se,{defaultLogger:()=>Ae,disable:()=>Ee,enable:()=>ve,enabled:()=>Ue,logger:()=>D,peerLogger:()=>Fe,prefixLogger:()=>Q});var ke=new Uint8Array(0);function T(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 te(e,t){if(e.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 o=0;o<e.length;o++){var l=e.charAt(o),p=l.charCodeAt(0);if(r[p]!==255)throw new TypeError(l+" is ambiguous");r[p]=o}var h=e.length,E=e.charAt(0),v=Math.log(h)/Math.log(256),s=Math.log(256)/Math.log(h);function i(a){if(a instanceof Uint8Array||(ArrayBuffer.isView(a)?a=new Uint8Array(a.buffer,a.byteOffset,a.byteLength):Array.isArray(a)&&(a=Uint8Array.from(a))),!(a instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(a.length===0)return"";for(var c=0,u=0,d=0,m=a.length;d!==m&&a[d]===0;)d++,c++;for(var w=(m-d)*s+1>>>0,b=new Uint8Array(w);d!==m;){for(var A=a[d],y=0,x=w-1;(A!==0||y<u)&&x!==-1;x--,y++)A+=256*b[x]>>>0,b[x]=A%h>>>0,A=A/h>>>0;if(A!==0)throw new Error("Non-zero carry");u=y,d++}for(var F=w-u;F!==w&&b[F]===0;)F++;for(var S=E.repeat(c);F<w;++F)S+=e.charAt(b[F]);return S}function f(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;var c=0;if(a[c]!==" "){for(var u=0,d=0;a[c]===E;)u++,c++;for(var m=(a.length-c)*v+1>>>0,w=new Uint8Array(m);a[c];){var b=r[a.charCodeAt(c)];if(b===255)return;for(var A=0,y=m-1;(b!==0||A<d)&&y!==-1;y--,A++)b+=h*w[y]>>>0,w[y]=b%256>>>0,b=b/256>>>0;if(b!==0)throw new Error("Non-zero carry");d=A,c++}if(a[c]!==" "){for(var x=m-d;x!==m&&w[x]===0;)x++;for(var F=new Uint8Array(u+(m-x)),S=u;x!==m;)F[S++]=w[x++];return F}}}function U(a){var c=f(a);if(c)return c;throw new Error(`Non-${t} character`)}return{encode:i,decodeUnsafe:f,decode:U}}var ne=te,oe=ne,R=oe;var M=class{name;prefix;baseEncode;constructor(t,r,n){this.name=t,this.prefix=r,this.baseEncode=n}encode(t){if(t instanceof Uint8Array)return`${this.prefix}${this.baseEncode(t)}`;throw Error("Unknown type, must be binary type")}},N=class{name;prefix;baseDecode;prefixCodePoint;constructor(t,r,n){this.name=t,this.prefix=r;let o=r.codePointAt(0);if(o===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=o,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 V(this,t)}},O=class{decoders;constructor(t){this.decoders=t}or(t){return V(this,t)}decode(t){let r=t[0],n=this.decoders[r];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 V(e,t){return new O({...e.decoders??{[e.prefix]:e},...t.decoders??{[t.prefix]:t}})}var I=class{name;prefix;baseEncode;baseDecode;encoder;decoder;constructor(t,r,n,o){this.name=t,this.prefix=r,this.baseEncode=n,this.baseDecode=o,this.encoder=new M(t,r,n),this.decoder=new N(t,r,o)}encode(t){return this.encoder.encode(t)}decode(t){return this.decoder.decode(t)}};function B({name:e,prefix:t,encode:r,decode:n}){return new I(e,t,r,n)}function P({name:e,prefix:t,alphabet:r}){let{encode:n,decode:o}=R(r,e);return B({prefix:t,name:e,encode:n,decode:l=>T(o(l))})}function se(e,t,r,n){let o=e.length;for(;e[o-1]==="=";)--o;let l=new Uint8Array(o*r/8|0),p=0,h=0,E=0;for(let v=0;v<o;++v){let s=t[e[v]];if(s===void 0)throw new SyntaxError(`Non-${n} character`);h=h<<r|s,p+=r,p>=8&&(p-=8,l[E++]=255&h>>p)}if(p>=r||(255&h<<8-p)!==0)throw new SyntaxError("Unexpected end of data");return l}function ie(e,t,r){let n=t[t.length-1]==="=",o=(1<<r)-1,l="",p=0,h=0;for(let E=0;E<e.length;++E)for(h=h<<8|e[E],p+=8;p>r;)p-=r,l+=t[o&h>>p];if(p!==0&&(l+=t[o&h<<r-p]),n)for(;(l.length*r&7)!==0;)l+="=";return l}function ae(e){let t={};for(let r=0;r<e.length;++r)t[e[r]]=r;return t}function g({name:e,prefix:t,bitsPerChar:r,alphabet:n}){let o=ae(n);return B({prefix:t,name:e,encode(l){return ie(l,n,r)},decode(l){return se(l,o,r,e)}})}var J=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Pe=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),je=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),De=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Te=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Re=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ve=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Be=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Je=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var q=P({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Xe=P({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var G=g({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),Qe=g({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),_e=g({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),We=g({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});function ce(e,t){try{if(typeof e=="string"&&e.length>0)return fe(e);if(typeof e=="number"&&isFinite(e))return t?.long?le(e):ue(e);throw new Error("Value is not a string or number.")}catch(r){let n=de(r)?`${r.message}. value=${JSON.stringify(e)}`:"An unknown error has occured.";throw new Error(n)}}function fe(e){if(e=String(e),e.length>100)throw new Error("Value exceeds the maximum length of 100 characters.");let t=/^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(e);if(!t)return NaN;let r=parseFloat(t[1]),n=(t[2]||"ms").toLowerCase();switch(n){case"years":case"year":case"yrs":case"yr":case"y":return r*315576e5;case"weeks":case"week":case"w":return r*6048e5;case"days":case"day":case"d":return r*864e5;case"hours":case"hour":case"hrs":case"hr":case"h":return r*36e5;case"minutes":case"minute":case"mins":case"min":case"m":return r*6e4;case"seconds":case"second":case"secs":case"sec":case"s":return r*1e3;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return r;default:throw new Error(`The unit ${n} was matched, but no matching case exists.`)}}var k=ce;function ue(e){let t=Math.abs(e);return t>=864e5?`${Math.round(e/864e5)}d`:t>=36e5?`${Math.round(e/36e5)}h`:t>=6e4?`${Math.round(e/6e4)}m`:t>=1e3?`${Math.round(e/1e3)}s`:`${e}ms`}function le(e){let t=Math.abs(e);return t>=864e5?$(e,t,864e5,"day"):t>=36e5?$(e,t,36e5,"hour"):t>=6e4?$(e,t,6e4,"minute"):t>=1e3?$(e,t,1e3,"second"):`${e} ms`}function $(e,t,r,n){let o=t>=r*1.5;return`${Math.round(e/r)} ${n}${o?"s":""}`}function de(e){return typeof e=="object"&&e!==null&&"message"in e}function j(e){r.debug=r,r.default=r,r.coerce=E,r.disable=l,r.enable=o,r.enabled=p,r.humanize=k,r.destroy=v,Object.keys(e).forEach(s=>{r[s]=e[s]}),r.names=[],r.skips=[],r.formatters={};function t(s){let i=0;for(let f=0;f<s.length;f++)i=(i<<5)-i+s.charCodeAt(f),i|=0;return r.colors[Math.abs(i)%r.colors.length]}r.selectColor=t;function r(s){let i,f=null,U,a;function c(...u){if(!c.enabled)return;let d=c,m=Number(new Date),w=m-(i||m);d.diff=w,d.prev=i,d.curr=m,i=m,u[0]=r.coerce(u[0]),typeof u[0]!="string"&&u.unshift("%O");let b=0;u[0]=u[0].replace(/%([a-zA-Z%])/g,(y,x)=>{if(y==="%%")return"%";b++;let F=r.formatters[x];if(typeof F=="function"){let S=u[b];y=F.call(d,S),u.splice(b,1),b--}return y}),r.formatArgs.call(d,u),(d.log||r.log).apply(d,u)}return c.namespace=s,c.useColors=r.useColors(),c.color=r.selectColor(s),c.extend=n,c.destroy=r.destroy,Object.defineProperty(c,"enabled",{enumerable:!0,configurable:!1,get:()=>f!==null?f:(U!==r.namespaces&&(U=r.namespaces,a=r.enabled(s)),a),set:u=>{f=u}}),typeof r.init=="function"&&r.init(c),c}function n(s,i){let f=r(this.namespace+(typeof i>"u"?":":i)+s);return f.log=this.log,f}function o(s){r.save(s),r.namespaces=s,r.names=[],r.skips=[];let i,f=(typeof s=="string"?s:"").split(/[\s,]+/),U=f.length;for(i=0;i<U;i++)f[i]&&(s=f[i].replace(/\*/g,".*?"),s[0]==="-"?r.skips.push(new RegExp("^"+s.substr(1)+"$")):r.names.push(new RegExp("^"+s+"$")))}function l(){let s=[...r.names.map(h),...r.skips.map(h).map(i=>"-"+i)].join(",");return r.enable(""),s}function p(s){if(s[s.length-1]==="*")return!0;let i,f;for(i=0,f=r.skips.length;i<f;i++)if(r.skips[i].test(s))return!1;for(i=0,f=r.names.length;i<f;i++)if(r.names[i].test(s))return!0;return!1}function h(s){return s.toString().substring(2,s.toString().length-2).replace(/\.\*\?$/,"*")}function E(s){return s instanceof Error?s.stack??s.message:s}function v(){console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`.")}return r.setupFormatters(r.formatters),r.enable(r.load()),r}var z=xe(),pe=["#0000CC","#0000FF","#0033CC","#0033FF","#0066CC","#0066FF","#0099CC","#0099FF","#00CC00","#00CC33","#00CC66","#00CC99","#00CCCC","#00CCFF","#3300CC","#3300FF","#3333CC","#3333FF","#3366CC","#3366FF","#3399CC","#3399FF","#33CC00","#33CC33","#33CC66","#33CC99","#33CCCC","#33CCFF","#6600CC","#6600FF","#6633CC","#6633FF","#66CC00","#66CC33","#9900CC","#9900FF","#9933CC","#9933FF","#99CC00","#99CC33","#CC0000","#CC0033","#CC0066","#CC0099","#CC00CC","#CC00FF","#CC3300","#CC3333","#CC3366","#CC3399","#CC33CC","#CC33FF","#CC6600","#CC6633","#CC9900","#CC9933","#CCCC00","#CCCC33","#FF0000","#FF0033","#FF0066","#FF0099","#FF00CC","#FF00FF","#FF3300","#FF3333","#FF3366","#FF3399","#FF33CC","#FF33FF","#FF6600","#FF6633","#FF9900","#FF9933","#FFCC00","#FFCC33"];function he(){return typeof window<"u"&&window.process&&(window.process.type==="renderer"||window.process.__nwjs)?!0:typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/(edge|trident)\/(\d+)/)!=null?!1:typeof document<"u"&&document.documentElement?.style?.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/firefox\/(\d+)/)!=null&&parseInt(RegExp.$1,10)>=31||typeof navigator<"u"&&navigator.userAgent?.toLowerCase().match(/applewebkit\/(\d+)/)}function be(e){if(e[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+e[0]+(this.useColors?"%c ":" ")+"+"+k(this.diff),!this.useColors)return;let t="color: "+this.color;e.splice(1,0,t,"color: inherit");let r=0,n=0;e[0].replace(/%[a-zA-Z%]/g,o=>{o!=="%%"&&(r++,o==="%c"&&(n=r))}),e.splice(n,0,t)}var Ce=console.debug??console.log??(()=>{});function me(e){try{e?z?.setItem("debug",e):z?.removeItem("debug")}catch{}}function ge(){let e;try{e=z?.getItem("debug")}catch{}return!e&&typeof globalThis.process<"u"&&"env"in globalThis.process&&(e=globalThis.process.env.DEBUG),e}function xe(){try{return localStorage}catch{}}function we(e){e.j=function(t){try{return JSON.stringify(t)}catch(r){return"[UnexpectedJSONParseError]: "+r.message}}}var X=j({formatArgs:be,save:me,load:ge,useColors:he,setupFormatters:we,colors:pe,storage:z,log:Ce});var C=X;function Z(e,t={}){let r=t.prefixLength??2,n=t.suffixLength??4,o=e.toString();return`${o.substring(0,r)}\u2026${o.substring(o.length,o.length-n)}`}C.formatters.b=e=>e==null?"undefined":q.baseEncode(e);C.formatters.t=e=>e==null?"undefined":J.baseEncode(e);C.formatters.m=e=>e==null?"undefined":G.baseEncode(e);C.formatters.p=e=>e==null?"undefined":e.toString();C.formatters.c=e=>e==null?"undefined":e.toString();C.formatters.k=e=>e==null?"undefined":e.toString();C.formatters.a=e=>e==null?"undefined":e.toString();C.formatters.e=e=>e==null?"undefined":K(e.stack)??K(e.message)??e.toString();function ye(e){let t=()=>{};return t.enabled=!1,t.color="",t.diff=0,t.log=()=>{},t.namespace=e,t.destroy=()=>!0,t.extend=()=>t,t}function Fe(e,t={}){return Q(Z(e,t))}function Q(e){return{forComponent(t){return D(`${e}:${t}`)}}}function Ae(){return{forComponent(e){return D(e)}}}function D(e){let t=ye(`${e}:trace`);return C.enabled(`${e}:trace`)&&C.names.map(r=>r.toString()).find(r=>r.includes(":trace"))!=null&&(t=C(`${e}:trace`)),Object.assign(C(e),{error:C(`${e}:error`),trace:t})}function Ee(){C.disable()}function ve(e){C.enable(e)}function Ue(e){return C.enabled(e)}function K(e){if(e!=null&&(e=e.trim(),e.length!==0))return e}return re(Se);})(); return Libp2PLogger})); //# sourceMappingURL=index.min.js.map