@libp2p/logger
Version:
A logging component for use in js-libp2p modules
15 lines (14 loc) • 15.4 kB
JavaScript
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PLogger = factory()}(typeof self !== 'undefined' ? self : this, function () {
;var Libp2PLogger=(()=>{var z=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.prototype.hasOwnProperty;var se=(e,r)=>{for(var t in r)z(e,t,{get:r[t],enumerable:!0})},ie=(e,r,t,n)=>{if(r&&typeof r=="object"||typeof r=="function")for(let o of ne(r))!oe.call(e,o)&&o!==t&&z(e,o,{get:()=>r[o],enumerable:!(n=te(r,o))||n.enumerable});return e};var ae=e=>ie(z({},"__esModule",{value:!0}),e);var Oe={};se(Oe,{defaultLogger:()=>Ie,disable:()=>Le,enable:()=>Me,enabled:()=>Ne,logger:()=>O,peerLogger:()=>ke,prefixLogger:()=>re});var je=new Uint8Array(0);function V(e){if(e instanceof Uint8Array&&e.constructor.name==="Uint8Array")return J(e);if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return J(new Uint8Array(e.buffer,e.byteOffset,e.byteLength));throw new Error("Unknown type, must be binary type")}function fe(e){return e?.buffer instanceof ArrayBuffer}function J(e){return fe(e)?e:e.slice()}function ue(e,r,t){if(e.length>=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),o=0;o<n.length;o++)n[o]=255;for(var f=0;f<e.length;f++){var u=e.charAt(f),d=u.charCodeAt(0);if(n[d]!==255)throw new TypeError(u+" is ambiguous");if(n[d]=f,t){var v=u.toLowerCase().charCodeAt(0),S=u.toUpperCase().charCodeAt(0);v!==d&&(n[v]=f),S!==d&&(n[S]=f)}}var s=e.length,a=e.charAt(0),l=Math.log(s)/Math.log(256),U=Math.log(256)/Math.log(s);function I(i){if(i instanceof Uint8Array||(ArrayBuffer.isView(i)?i=new Uint8Array(i.buffer,i.byteOffset,i.byteLength):Array.isArray(i)&&(i=Uint8Array.from(i))),!(i instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(i.length===0)return"";for(var c=0,y=0,C=0,m=i.length;C!==m&&i[C]===0;)C++,c++;for(var F=(m-C)*U+1>>>0,h=new Uint8Array(F);C!==m;){for(var x=i[C],A=0,g=F-1;(x!==0||A<y)&&g!==-1;g--,A++)x+=256*h[g]>>>0,h[g]=x%s>>>0,x=x/s>>>0;if(x!==0)throw new Error("Non-zero carry");y=A,C++}for(var E=F-y;E!==F&&h[E]===0;)E++;for(var L=a.repeat(c);E<F;++E)L+=e.charAt(h[E]);return L}function k(i){if(typeof i!="string")throw new TypeError("Expected String");if(i.length===0)return new Uint8Array;var c=0;if(i[c]!==" "){for(var y=0,C=0;i[c]===a;)y++,c++;for(var m=(i.length-c)*l+1>>>0,F=new Uint8Array(m);i[c];){var h=n[i.charCodeAt(c)];if(h===255)return;for(var x=0,A=m-1;(h!==0||x<C)&&A!==-1;A--,x++)h+=s*F[A]>>>0,F[A]=h%256>>>0,h=h/256>>>0;if(h!==0)throw new Error("Non-zero carry");C=x,c++}if(i[c]!==" "){for(var g=m-C;g!==m&&F[g]===0;)g++;for(var E=new Uint8Array(y+(m-g)),L=y;g!==m;)E[L++]=F[g++];return E}}}function w(i){var c=k(i);if(c)return c;throw new Error(`Non-${r} character`)}return{encode:I,decodeUnsafe:k,decode:w}}var ce=ue,le=ce,G=le;var j=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")}},P=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 q(this,r)}},B=class{decoders;constructor(r){this.decoders=r}or(r){return q(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 q(e,r){return new B({...e.decoders??{[e.prefix]:e},...r.decoders??{[r.prefix]:r}})}var D=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 j(r,t,n),this.decoder=new P(r,t,o)}encode(r){return this.encoder.encode(r)}decode(r){return this.decoder.decode(r)}};function X({name:e,prefix:r,encode:t,decode:n}){return new D(e,r,t,n)}function R({name:e,prefix:r,alphabet:t,caseInsensitive:n=!1}){let{encode:o,decode:f}=G(t,e,n);return X({prefix:r,name:e,encode:o,decode:u=>V(f(u))})}function de(e,r,t,n){let o=e.length;for(;e[o-1]==="=";)--o;let f=new Uint8Array(o*t/8|0),u=0,d=0,v=0;for(let S=0;S<o;++S){let s=r[e[S]];if(s===void 0)throw new SyntaxError(`Non-${n} character`);d=d<<t|s,u+=t,u>=8&&(u-=8,f[v++]=255&d>>u)}if(u>=t||(255&d<<8-u)!==0)throw new SyntaxError("Unexpected end of data");return f}function pe(e,r,t){let n=r[r.length-1]==="=",o=(1<<t)-1,f="",u=0,d=0;for(let v=0;v<e.length;++v)for(d=d<<8|e[v],u+=8;u>t;)u-=t,f+=r[o&d>>u];if(u!==0&&(f+=r[o&d<<t-u]),n)for(;(f.length*t&7)!==0;)f+="=";return f}function he(e,r){let t={};for(let n=0;n<e.length;++n)if(t[e[n]]=n,r){let o=e[n].toLowerCase(),f=e[n].toUpperCase();o!==e[n]&&(t[o]=n),f!==e[n]&&(t[f]=n)}return t}function b({name:e,prefix:r,bitsPerChar:t,alphabet:n,caseInsensitive:o=!1}){let f=he(n,o);return X({prefix:r,name:e,encode(u){return pe(u,n,t)},decode(u){return de(u,f,t,e)}})}var _=b({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5,caseInsensitive:!0}),Ve=b({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5,caseInsensitive:!0}),Ge=b({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5,caseInsensitive:!0}),qe=b({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5,caseInsensitive:!0}),Xe=b({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5,caseInsensitive:!0}),_e=b({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5,caseInsensitive:!0}),Ze=b({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5,caseInsensitive:!0}),Ke=b({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5,caseInsensitive:!0}),Qe=b({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var Z=R({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Ye=R({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var K=b({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),tr=b({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),nr=b({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),or=b({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});function M(e,r){if(typeof e=="string")return me(e);if(typeof e=="number")return ge(e,r);throw new Error(`Value provided to ms() must be a string or number. value=${JSON.stringify(e)}`)}function me(e){if(typeof e!="string"||e.length===0||e.length>100)throw new Error(`Value provided to ms.parse() must be a string with length between 1 and 99. value=${JSON.stringify(e)}`);let r=/^(?<value>-?\d*\.?\d+) *(?<unit>milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|months?|mo|years?|yrs?|y)?$/i.exec(e);if(!r?.groups)return NaN;let{value:t,unit:n="ms"}=r.groups,o=parseFloat(t),f=n.toLowerCase();switch(f){case"years":case"year":case"yrs":case"yr":case"y":return o*315576e5;case"months":case"month":case"mo":return o*26298e5;case"weeks":case"week":case"w":return o*6048e5;case"days":case"day":case"d":return o*864e5;case"hours":case"hour":case"hrs":case"hr":case"h":return o*36e5;case"minutes":case"minute":case"mins":case"min":case"m":return o*6e4;case"seconds":case"second":case"secs":case"sec":case"s":return o*1e3;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return o;default:throw new Error(`Unknown unit "${f}" provided to ms.parse(). value=${JSON.stringify(e)}`)}}function be(e){let r=Math.abs(e);return r>=315576e5?`${Math.round(e/315576e5)}y`:r>=26298e5?`${Math.round(e/26298e5)}mo`:r>=6048e5?`${Math.round(e/6048e5)}w`:r>=864e5?`${Math.round(e/864e5)}d`:r>=36e5?`${Math.round(e/36e5)}h`:r>=6e4?`${Math.round(e/6e4)}m`:r>=1e3?`${Math.round(e/1e3)}s`:`${e}ms`}function Ce(e){let r=Math.abs(e);return r>=315576e5?$(e,r,315576e5,"year"):r>=26298e5?$(e,r,26298e5,"month"):r>=6048e5?$(e,r,6048e5,"week"):r>=864e5?$(e,r,864e5,"day"):r>=36e5?$(e,r,36e5,"hour"):r>=6e4?$(e,r,6e4,"minute"):r>=1e3?$(e,r,1e3,"second"):`${e} ms`}function ge(e,r){if(typeof e!="number"||!Number.isFinite(e))throw new Error("Value provided to ms.format() must be of type number.");return r?.long?Ce(e):be(e)}function $(e,r,t,n){let o=r>=t*1.5;return`${Math.round(e/t)} ${n}${o?"s":""}`}function T(e){t.debug=t,t.default=t,t.coerce=v,t.disable=f,t.enable=o,t.enabled=u,t.humanize=M,t.destroy=S,Object.keys(e).forEach(s=>{t[s]=e[s]}),t.names=[],t.skips=[],t.formatters={};function r(s){let a=0;for(let l=0;l<s.length;l++)a=(a<<5)-a+s.charCodeAt(l),a|=0;return t.colors[Math.abs(a)%t.colors.length]}t.selectColor=r;function t(s,a){let l,U=null,I,k;function w(...i){if(!w.enabled)return;let c=w,y=Number(new Date),C=y-(l||y);c.diff=C,c.prev=l,c.curr=y,l=y,i[0]=t.coerce(i[0]),typeof i[0]!="string"&&i.unshift("%O");let m=0;i[0]=i[0].replace(/%([a-zA-Z%])/g,(h,x)=>{if(h==="%%")return"%";m++;let A=t.formatters[x];if(typeof A=="function"){let g=i[m];h=A.call(c,g),i.splice(m,1),m--}return h}),t.formatArgs.call(c,i),a?.onLog!=null&&a.onLog(...i),(c.log||t.log).apply(c,i)}return w.namespace=s,w.useColors=t.useColors(),w.color=t.selectColor(s),w.extend=n,w.destroy=t.destroy,Object.defineProperty(w,"enabled",{enumerable:!0,configurable:!1,get:()=>U!==null?U:(I!==t.namespaces&&(I=t.namespaces,k=t.enabled(s)),k),set:i=>{U=i}}),typeof t.init=="function"&&t.init(w),w}function n(s,a){let l=t(this.namespace+(typeof a>"u"?":":a)+s);return l.log=this.log,l}function o(s){t.save(s),t.namespaces=s,t.names=[],t.skips=[];let a,l=(typeof s=="string"?s:"").split(/[\s,]+/),U=l.length;for(a=0;a<U;a++)l[a]&&(s=l[a].replace(/\*/g,".*?"),s[0]==="-"?t.skips.push(new RegExp("^"+s.substr(1)+"$")):t.names.push(new RegExp("^"+s+"$")))}function f(){let s=[...t.names.map(d),...t.skips.map(d).map(a=>"-"+a)].join(",");return t.enable(""),s}function u(s){if(s[s.length-1]==="*")return!0;let a,l;for(a=0,l=t.skips.length;a<l;a++)if(t.skips[a].test(s))return!1;for(a=0,l=t.names.length;a<l;a++)if(t.names[a].test(s))return!0;return!1}function d(s){return s.toString().substring(2,s.toString().length-2).replace(/\.\*\?$/,"*")}function v(s){return s instanceof Error?s.stack??s.message:s}function S(){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 t.setupFormatters(t.formatters),t.enable(t.load()),t}var N=Ee(),we=["#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 ye(){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 xe(e){if(e[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+e[0]+(this.useColors?"%c ":" ")+"+"+M(this.diff),!this.useColors)return;let r="color: "+this.color;e.splice(1,0,r,"color: inherit");let t=0,n=0;e[0].replace(/%[a-zA-Z%]/g,o=>{o!=="%%"&&(t++,o==="%c"&&(n=t))}),e.splice(n,0,r)}var Fe=console.debug??console.log??(()=>{});function Ae(e){try{e?N?.setItem("debug",e):N?.removeItem("debug")}catch{}}function ve(){let e;try{e=N?.getItem("debug")}catch{}return!e&&typeof globalThis.process<"u"&&"env"in globalThis.process&&(e=globalThis.process.env.DEBUG),e}function Ee(){try{return localStorage}catch{}}function Se(e){e.j=function(r){try{return JSON.stringify(r)}catch(t){return"[UnexpectedJSONParseError]: "+t.message}}}var Q=T({formatArgs:xe,save:Ae,load:ve,useColors:ye,setupFormatters:Se,colors:we,storage:N,log:Fe});var p=Q;function W(e,r={}){let t=r.prefixLength??2,n=r.suffixLength??4,o=e.toString();return`${o.substring(0,t)}\u2026${o.substring(o.length,o.length-n)}`}p.formatters.b=e=>e==null?"undefined":Z.baseEncode(e);p.formatters.t=e=>e==null?"undefined":_.baseEncode(e);p.formatters.m=e=>e==null?"undefined":K.baseEncode(e);p.formatters.p=e=>e==null?"undefined":e.toString();p.formatters.c=e=>e==null?"undefined":e.toString();p.formatters.k=e=>e==null?"undefined":e.toString();p.formatters.a=e=>e==null?"undefined":e.toString();function H(e,r=""){let t=Y(e.message),n=Y(e.stack);return t!=null&&n!=null?n.includes(t)?`${n.split(`
`).join(`
${r}`)}`:`${t}
${r}${n.split(`
`).join(`
${r}`)}`:n!=null?`${n.split(`
`).join(`
${r}`)}`:t!=null?`${t}`:`${e.toString()}`}function Ue(e){return e instanceof AggregateError||e?.name==="AggregateError"&&Array.isArray(e.errors)}function ee(e,r=""){if(Ue(e)){let t=H(e,r);return e.errors.length>0?(r=`${r} `,t+=`
${r}${e.errors.map(n=>`${ee(n,`${r}`)}`).join(`
${r}`)}`):t+=`
${r}[Error list was empty]`,t.trim()}return H(e,r)}p.formatters.e=e=>e==null?"undefined":ee(e);function $e(e){let r=()=>{};return r.enabled=!1,r.color="",r.diff=0,r.log=()=>{},r.namespace=e,r.destroy=()=>!0,r.extend=()=>r,r.useColors=()=>!1,r}function ke(e,r={}){return re(W(e,r),r)}function re(e,r){return{forComponent(t){return O(`${e}:${t}`,r)}}}function Ie(e){return{forComponent(r){return O(r,e)}}}function O(e,r){let t=$e(`${e}:trace`);return p.enabled(`${e}:trace`)&&p.names.map(n=>n.toString()).find(n=>n.includes(":trace"))!=null&&(t=p(`${e}:trace`,r)),Object.assign(p(e,r),{error:p(`${e}:error`,r),trace:t,newScope:n=>O(`${e}:${n}`,r)})}function Le(){p.disable()}function Me(e){p.enable(e)}function Ne(e){return p.enabled(e)}function Y(e){if(e!=null&&(e=e.trim(),e.length!==0))return e}return ae(Oe);})();
return Libp2PLogger}));
//# sourceMappingURL=index.min.js.map