@libp2p/logger 3.0.5 → 3.1.0-05b52d69c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -3
- package/dist/index.min.js +1 -1
- package/dist/src/index.d.ts +75 -3
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +82 -3
- package/dist/src/index.js.map +1 -1
- package/dist/src/utils.d.ts +4 -0
- package/dist/src/utils.d.ts.map +1 -0
- package/dist/src/utils.js +7 -0
- package/dist/src/utils.js.map +1 -0
- package/package.json +6 -6
- package/src/index.ts +94 -3
- package/src/utils.ts +10 -0
- package/dist/typedoc-urls.json +0 -12
package/README.md
CHANGED
|
@@ -7,9 +7,7 @@
|
|
|
7
7
|
|
|
8
8
|
# About
|
|
9
9
|
|
|
10
|
-
A
|
|
11
|
-
|
|
12
|
-
If metrics are disabled a regular map is used.
|
|
10
|
+
A logger for libp2p based on the venerable [debug](https://www.npmjs.com/package/debug) module.
|
|
13
11
|
|
|
14
12
|
## Example
|
|
15
13
|
|
package/dist/index.min.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
(function (root, factory) {(typeof module === 'object' && module.exports) ? module.exports = factory() : root.Libp2PLogger = factory()}(typeof self !== 'undefined' ? self : this, function () {
|
|
2
|
-
"use strict";var Libp2PLogger=(()=>{var re=Object.create;var P=Object.defineProperty;var te=Object.getOwnPropertyDescriptor;var ne=Object.getOwnPropertyNames;var oe=Object.getPrototypeOf,se=Object.prototype.hasOwnProperty;var $=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports),ae=(e,r)=>{for(var t in r)P(e,t,{get:r[t],enumerable:!0})},T=(e,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let a of ne(r))!se.call(e,a)&&a!==t&&P(e,a,{get:()=>r[a],enumerable:!(o=te(r,a))||o.enumerable});return e};var ie=(e,r,t)=>(t=e!=null?re(oe(e)):{},T(r||!e||!e.__esModule?P(t,"default",{value:e,enumerable:!0}):t,e)),ce=e=>T(P({},"__esModule",{value:!0}),e);var V=$((Pe,q)=>{var M=1e3,z=M*60,O=z*60,U=O*24,fe=U*7,ue=U*365.25;q.exports=function(e,r){r=r||{};var t=typeof e;if(t==="string"&&e.length>0)return de(e);if(t==="number"&&isFinite(e))return r.long?pe(e):le(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))};function de(e){if(e=String(e),!(e.length>100)){var r=/^(-?(?:\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(r){var t=parseFloat(r[1]),o=(r[2]||"ms").toLowerCase();switch(o){case"years":case"year":case"yrs":case"yr":case"y":return t*ue;case"weeks":case"week":case"w":return t*fe;case"days":case"day":case"d":return t*U;case"hours":case"hour":case"hrs":case"hr":case"h":return t*O;case"minutes":case"minute":case"mins":case"min":case"m":return t*z;case"seconds":case"second":case"secs":case"sec":case"s":return t*M;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function le(e){var r=Math.abs(e);return r>=U?Math.round(e/U)+"d":r>=O?Math.round(e/O)+"h":r>=z?Math.round(e/z)+"m":r>=M?Math.round(e/M)+"s":e+"ms"}function pe(e){var r=Math.abs(e);return r>=U?j(e,r,U,"day"):r>=O?j(e,r,O,"hour"):r>=z?j(e,r,z,"minute"):r>=M?j(e,r,M,"second"):e+" ms"}function j(e,r,t,o){var a=r>=t*1.5;return Math.round(e/t)+" "+o+(a?"s":"")}});var X=$((je,G)=>{function he(e){t.debug=t,t.default=t,t.coerce=w,t.disable=p,t.enable=a,t.enabled=m,t.humanize=V(),t.destroy=N,Object.keys(e).forEach(n=>{t[n]=e[n]}),t.names=[],t.skips=[],t.formatters={};function r(n){let s=0;for(let f=0;f<n.length;f++)s=(s<<5)-s+n.charCodeAt(f),s|=0;return t.colors[Math.abs(s)%t.colors.length]}t.selectColor=r;function t(n){let s,f=null,S,i;function c(...d){if(!c.enabled)return;let l=c,C=Number(new Date),F=C-(s||C);l.diff=F,l.prev=s,l.curr=C,s=C,d[0]=t.coerce(d[0]),typeof d[0]!="string"&&d.unshift("%O");let h=0;d[0]=d[0].replace(/%([a-zA-Z%])/g,(v,y)=>{if(v==="%%")return"%";h++;let A=t.formatters[y];if(typeof A=="function"){let k=d[h];v=A.call(l,k),d.splice(h,1),h--}return v}),t.formatArgs.call(l,d),(l.log||t.log).apply(l,d)}return c.namespace=n,c.useColors=t.useColors(),c.color=t.selectColor(n),c.extend=o,c.destroy=t.destroy,Object.defineProperty(c,"enabled",{enumerable:!0,configurable:!1,get:()=>f!==null?f:(S!==t.namespaces&&(S=t.namespaces,i=t.enabled(n)),i),set:d=>{f=d}}),typeof t.init=="function"&&t.init(c),c}function o(n,s){let f=t(this.namespace+(typeof s>"u"?":":s)+n);return f.log=this.log,f}function a(n){t.save(n),t.namespaces=n,t.names=[],t.skips=[];let s,f=(typeof n=="string"?n:"").split(/[\s,]+/),S=f.length;for(s=0;s<S;s++)f[s]&&(n=f[s].replace(/\*/g,".*?"),n[0]==="-"?t.skips.push(new RegExp("^"+n.slice(1)+"$")):t.names.push(new RegExp("^"+n+"$")))}function p(){let n=[...t.names.map(u),...t.skips.map(u).map(s=>"-"+s)].join(",");return t.enable(""),n}function m(n){if(n[n.length-1]==="*")return!0;let s,f;for(s=0,f=t.skips.length;s<f;s++)if(t.skips[s].test(n))return!1;for(s=0,f=t.names.length;s<f;s++)if(t.names[s].test(n))return!0;return!1}function u(n){return n.toString().substring(2,n.toString().length-2).replace(/\.\*\?$/,"*")}function w(n){return n instanceof Error?n.stack||n.message:n}function N(){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.enable(t.load()),t}G.exports=he});var Z=$((x,I)=>{x.formatArgs=Ce;x.save=ge;x.load=me;x.useColors=be;x.storage=we();x.destroy=(()=>{let e=!1;return()=>{e||(e=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})();x.colors=["#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 be(){return typeof window<"u"&&window.process&&(window.process.type==="renderer"||window.process.__nwjs)?!0:typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)?!1:typeof document<"u"&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)}function Ce(e){if(e[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+e[0]+(this.useColors?"%c ":" ")+"+"+I.exports.humanize(this.diff),!this.useColors)return;let r="color: "+this.color;e.splice(1,0,r,"color: inherit");let t=0,o=0;e[0].replace(/%[a-zA-Z%]/g,a=>{a!=="%%"&&(t++,a==="%c"&&(o=t))}),e.splice(o,0,r)}x.log=console.debug||console.log||(()=>{});function ge(e){try{e?x.storage.setItem("debug",e):x.storage.removeItem("debug")}catch{}}function me(){let e;try{e=x.storage.getItem("debug")}catch{}return!e&&typeof process<"u"&&"env"in process&&(e=process.env.DEBUG),e}function we(){try{return localStorage}catch{}}I.exports=X()(x);var{formatters:ye}=I.exports;ye.j=function(e){try{return JSON.stringify(e)}catch(r){return"[UnexpectedJSONParseError]: "+r.message}}});var Oe={};ae(Oe,{disable:()=>ke,enable:()=>Me,enabled:()=>ze,logger:()=>Se});var b=ie(Z(),1);function xe(e,r){if(e.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),o=0;o<t.length;o++)t[o]=255;for(var a=0;a<e.length;a++){var p=e.charAt(a),m=p.charCodeAt(0);if(t[m]!==255)throw new TypeError(p+" is ambiguous");t[m]=a}var u=e.length,w=e.charAt(0),N=Math.log(u)/Math.log(256),n=Math.log(256)/Math.log(u);function s(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,d=0,l=0,C=i.length;l!==C&&i[l]===0;)l++,c++;for(var F=(C-l)*n+1>>>0,h=new Uint8Array(F);l!==C;){for(var E=i[l],v=0,y=F-1;(E!==0||v<d)&&y!==-1;y--,v++)E+=256*h[y]>>>0,h[y]=E%u>>>0,E=E/u>>>0;if(E!==0)throw new Error("Non-zero carry");d=v,l++}for(var A=F-d;A!==F&&h[A]===0;)A++;for(var k=w.repeat(c);A<F;++A)k+=e.charAt(h[A]);return k}function f(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 d=0,l=0;i[c]===w;)d++,c++;for(var C=(i.length-c)*N+1>>>0,F=new Uint8Array(C);i[c];){var h=t[i.charCodeAt(c)];if(h===255)return;for(var E=0,v=C-1;(h!==0||E<l)&&v!==-1;v--,E++)h+=u*F[v]>>>0,F[v]=h%256>>>0,h=h/256>>>0;if(h!==0)throw new Error("Non-zero carry");l=E,c++}if(i[c]!==" "){for(var y=C-l;y!==C&&F[y]===0;)y++;for(var A=new Uint8Array(d+(C-y)),k=d;y!==C;)A[k++]=F[y++];return A}}}function S(i){var c=f(i);if(c)return c;throw new Error(`Non-${r} character`)}return{encode:s,decodeUnsafe:f,decode:S}}var Fe=xe,ve=Fe,K=ve;var $e=new Uint8Array(0);var Q=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")};var D=class{constructor(r,t,o){this.name=r,this.prefix=t,this.baseEncode=o}encode(r){if(r instanceof Uint8Array)return`${this.prefix}${this.baseEncode(r)}`;throw Error("Unknown type, must be binary type")}},L=class{constructor(r,t,o){if(this.name=r,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=o}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 _(this,r)}},R=class{constructor(r){this.decoders=r}or(r){return _(this,r)}decode(r){let t=r[0],o=this.decoders[t];if(o)return o.decode(r);throw RangeError(`Unable to decode multibase string ${JSON.stringify(r)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},_=(e,r)=>new R({...e.decoders||{[e.prefix]:e},...r.decoders||{[r.prefix]:r}}),B=class{constructor(r,t,o,a){this.name=r,this.prefix=t,this.baseEncode=o,this.baseDecode=a,this.encoder=new D(r,t,o),this.decoder=new L(r,t,a)}encode(r){return this.encoder.encode(r)}decode(r){return this.decoder.decode(r)}},W=({name:e,prefix:r,encode:t,decode:o})=>new B(e,r,t,o),J=({prefix:e,name:r,alphabet:t})=>{let{encode:o,decode:a}=K(t,r);return W({prefix:e,name:r,encode:o,decode:p=>Q(a(p))})},Ae=(e,r,t,o)=>{let a={};for(let n=0;n<r.length;++n)a[r[n]]=n;let p=e.length;for(;e[p-1]==="=";)--p;let m=new Uint8Array(p*t/8|0),u=0,w=0,N=0;for(let n=0;n<p;++n){let s=a[e[n]];if(s===void 0)throw new SyntaxError(`Non-${o} character`);w=w<<t|s,u+=t,u>=8&&(u-=8,m[N++]=255&w>>u)}if(u>=t||255&w<<8-u)throw new SyntaxError("Unexpected end of data");return m},Ee=(e,r,t)=>{let o=r[r.length-1]==="=",a=(1<<t)-1,p="",m=0,u=0;for(let w=0;w<e.length;++w)for(u=u<<8|e[w],m+=8;m>t;)m-=t,p+=r[a&u>>m];if(m&&(p+=r[a&u<<t-m]),o)for(;p.length*t&7;)p+="=";return p},g=({name:e,prefix:r,bitsPerChar:t,alphabet:o})=>W({prefix:r,name:e,encode(a){return Ee(a,o,t)},decode(a){return Ae(a,o,t,e)}});var Y=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Te=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),qe=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ve=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ge=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Xe=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ze=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ke=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Qe=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var H=J({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),Ye=J({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var ee=g({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),rr=g({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),tr=g({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),nr=g({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});b.default.formatters.b=e=>e==null?"undefined":H.baseEncode(e);b.default.formatters.t=e=>e==null?"undefined":Y.baseEncode(e);b.default.formatters.m=e=>e==null?"undefined":ee.baseEncode(e);b.default.formatters.p=e=>e==null?"undefined":e.toString();b.default.formatters.c=e=>e==null?"undefined":e.toString();b.default.formatters.k=e=>e==null?"undefined":e.toString();b.default.formatters.a=e=>e==null?"undefined":e.toString();function Ue(e){let r=()=>{};return r.enabled=!1,r.color="",r.diff=0,r.log=()=>{},r.namespace=e,r.destroy=()=>!0,r.extend=()=>r,r}function Se(e){let r=Ue(`${e}:trace`);return b.default.enabled(`${e}:trace`)&&b.default.names.map(t=>t.toString()).find(t=>t.includes(":trace"))!=null&&(r=(0,b.default)(`${e}:trace`)),Object.assign((0,b.default)(e),{error:(0,b.default)(`${e}:error`),trace:r})}function ke(){b.default.disable()}function Me(e){b.default.enable(e)}function ze(e){return b.default.enabled(e)}return ce(Oe);})();
|
|
2
|
+
"use strict";var Libp2PLogger=(()=>{var oe=Object.create;var I=Object.defineProperty;var se=Object.getOwnPropertyDescriptor;var ae=Object.getOwnPropertyNames;var ie=Object.getPrototypeOf,ce=Object.prototype.hasOwnProperty;var N=(e,r)=>()=>(r||e((r={exports:{}}).exports,r),r.exports),fe=(e,r)=>{for(var t in r)I(e,t,{get:r[t],enumerable:!0})},q=(e,r,t,o)=>{if(r&&typeof r=="object"||typeof r=="function")for(let s of ae(r))!ce.call(e,s)&&s!==t&&I(e,s,{get:()=>r[s],enumerable:!(o=se(r,s))||o.enumerable});return e};var ue=(e,r,t)=>(t=e!=null?oe(ie(e)):{},q(r||!e||!e.__esModule?I(t,"default",{value:e,enumerable:!0}):t,e)),de=e=>q(I({},"__esModule",{value:!0}),e);var G=N((je,V)=>{var L=1e3,M=L*60,z=M*60,U=z*24,le=U*7,pe=U*365.25;V.exports=function(e,r){r=r||{};var t=typeof e;if(t==="string"&&e.length>0)return he(e);if(t==="number"&&isFinite(e))return r.long?Ce(e):be(e);throw new Error("val is not a non-empty string or a valid number. val="+JSON.stringify(e))};function he(e){if(e=String(e),!(e.length>100)){var r=/^(-?(?:\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(r){var t=parseFloat(r[1]),o=(r[2]||"ms").toLowerCase();switch(o){case"years":case"year":case"yrs":case"yr":case"y":return t*pe;case"weeks":case"week":case"w":return t*le;case"days":case"day":case"d":return t*U;case"hours":case"hour":case"hrs":case"hr":case"h":return t*z;case"minutes":case"minute":case"mins":case"min":case"m":return t*M;case"seconds":case"second":case"secs":case"sec":case"s":return t*L;case"milliseconds":case"millisecond":case"msecs":case"msec":case"ms":return t;default:return}}}}function be(e){var r=Math.abs(e);return r>=U?Math.round(e/U)+"d":r>=z?Math.round(e/z)+"h":r>=M?Math.round(e/M)+"m":r>=L?Math.round(e/L)+"s":e+"ms"}function Ce(e){var r=Math.abs(e);return r>=U?P(e,r,U,"day"):r>=z?P(e,r,z,"hour"):r>=M?P(e,r,M,"minute"):r>=L?P(e,r,L,"second"):e+" ms"}function P(e,r,t,o){var s=r>=t*1.5;return Math.round(e/t)+" "+o+(s?"s":"")}});var Z=N((De,X)=>{function ge(e){t.debug=t,t.default=t,t.coerce=w,t.disable=p,t.enable=s,t.enabled=m,t.humanize=G(),t.destroy=O,Object.keys(e).forEach(n=>{t[n]=e[n]}),t.names=[],t.skips=[],t.formatters={};function r(n){let a=0;for(let f=0;f<n.length;f++)a=(a<<5)-a+n.charCodeAt(f),a|=0;return t.colors[Math.abs(a)%t.colors.length]}t.selectColor=r;function t(n){let a,f=null,S,i;function c(...d){if(!c.enabled)return;let l=c,C=Number(new Date),F=C-(a||C);l.diff=F,l.prev=a,l.curr=C,a=C,d[0]=t.coerce(d[0]),typeof d[0]!="string"&&d.unshift("%O");let h=0;d[0]=d[0].replace(/%([a-zA-Z%])/g,(v,x)=>{if(v==="%%")return"%";h++;let A=t.formatters[x];if(typeof A=="function"){let k=d[h];v=A.call(l,k),d.splice(h,1),h--}return v}),t.formatArgs.call(l,d),(l.log||t.log).apply(l,d)}return c.namespace=n,c.useColors=t.useColors(),c.color=t.selectColor(n),c.extend=o,c.destroy=t.destroy,Object.defineProperty(c,"enabled",{enumerable:!0,configurable:!1,get:()=>f!==null?f:(S!==t.namespaces&&(S=t.namespaces,i=t.enabled(n)),i),set:d=>{f=d}}),typeof t.init=="function"&&t.init(c),c}function o(n,a){let f=t(this.namespace+(typeof a>"u"?":":a)+n);return f.log=this.log,f}function s(n){t.save(n),t.namespaces=n,t.names=[],t.skips=[];let a,f=(typeof n=="string"?n:"").split(/[\s,]+/),S=f.length;for(a=0;a<S;a++)f[a]&&(n=f[a].replace(/\*/g,".*?"),n[0]==="-"?t.skips.push(new RegExp("^"+n.slice(1)+"$")):t.names.push(new RegExp("^"+n+"$")))}function p(){let n=[...t.names.map(u),...t.skips.map(u).map(a=>"-"+a)].join(",");return t.enable(""),n}function m(n){if(n[n.length-1]==="*")return!0;let a,f;for(a=0,f=t.skips.length;a<f;a++)if(t.skips[a].test(n))return!1;for(a=0,f=t.names.length;a<f;a++)if(t.names[a].test(n))return!0;return!1}function u(n){return n.toString().substring(2,n.toString().length-2).replace(/\.\*\?$/,"*")}function w(n){return n instanceof Error?n.stack||n.message:n}function O(){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.enable(t.load()),t}X.exports=ge});var K=N((y,$)=>{y.formatArgs=we;y.save=xe;y.load=ye;y.useColors=me;y.storage=Fe();y.destroy=(()=>{let e=!1;return()=>{e||(e=!0,console.warn("Instance method `debug.destroy()` is deprecated and no longer does anything. It will be removed in the next major version of `debug`."))}})();y.colors=["#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 me(){return typeof window<"u"&&window.process&&(window.process.type==="renderer"||window.process.__nwjs)?!0:typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/(edge|trident)\/(\d+)/)?!1:typeof document<"u"&&document.documentElement&&document.documentElement.style&&document.documentElement.style.WebkitAppearance||typeof window<"u"&&window.console&&(window.console.firebug||window.console.exception&&window.console.table)||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)&&parseInt(RegExp.$1,10)>=31||typeof navigator<"u"&&navigator.userAgent&&navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/)}function we(e){if(e[0]=(this.useColors?"%c":"")+this.namespace+(this.useColors?" %c":" ")+e[0]+(this.useColors?"%c ":" ")+"+"+$.exports.humanize(this.diff),!this.useColors)return;let r="color: "+this.color;e.splice(1,0,r,"color: inherit");let t=0,o=0;e[0].replace(/%[a-zA-Z%]/g,s=>{s!=="%%"&&(t++,s==="%c"&&(o=t))}),e.splice(o,0,r)}y.log=console.debug||console.log||(()=>{});function xe(e){try{e?y.storage.setItem("debug",e):y.storage.removeItem("debug")}catch{}}function ye(){let e;try{e=y.storage.getItem("debug")}catch{}return!e&&typeof process<"u"&&"env"in process&&(e=process.env.DEBUG),e}function Fe(){try{return localStorage}catch{}}$.exports=Z()(y);var{formatters:ve}=$.exports;ve.j=function(e){try{return JSON.stringify(e)}catch(r){return"[UnexpectedJSONParseError]: "+r.message}}});var $e={};fe($e,{defaultLogger:()=>ze,disable:()=>Oe,enable:()=>Ie,enabled:()=>Pe,logger:()=>T,peerLogger:()=>Me,prefixLogger:()=>ne});var b=ue(K(),1);function Ae(e,r){if(e.length>=255)throw new TypeError("Alphabet too long");for(var t=new Uint8Array(256),o=0;o<t.length;o++)t[o]=255;for(var s=0;s<e.length;s++){var p=e.charAt(s),m=p.charCodeAt(0);if(t[m]!==255)throw new TypeError(p+" is ambiguous");t[m]=s}var u=e.length,w=e.charAt(0),O=Math.log(u)/Math.log(256),n=Math.log(256)/Math.log(u);function a(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,d=0,l=0,C=i.length;l!==C&&i[l]===0;)l++,c++;for(var F=(C-l)*n+1>>>0,h=new Uint8Array(F);l!==C;){for(var E=i[l],v=0,x=F-1;(E!==0||v<d)&&x!==-1;x--,v++)E+=256*h[x]>>>0,h[x]=E%u>>>0,E=E/u>>>0;if(E!==0)throw new Error("Non-zero carry");d=v,l++}for(var A=F-d;A!==F&&h[A]===0;)A++;for(var k=w.repeat(c);A<F;++A)k+=e.charAt(h[A]);return k}function f(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 d=0,l=0;i[c]===w;)d++,c++;for(var C=(i.length-c)*O+1>>>0,F=new Uint8Array(C);i[c];){var h=t[i.charCodeAt(c)];if(h===255)return;for(var E=0,v=C-1;(h!==0||E<l)&&v!==-1;v--,E++)h+=u*F[v]>>>0,F[v]=h%256>>>0,h=h/256>>>0;if(h!==0)throw new Error("Non-zero carry");l=E,c++}if(i[c]!==" "){for(var x=C-l;x!==C&&F[x]===0;)x++;for(var A=new Uint8Array(d+(C-x)),k=d;x!==C;)A[k++]=F[x++];return A}}}function S(i){var c=f(i);if(c)return c;throw new Error(`Non-${r} character`)}return{encode:a,decodeUnsafe:f,decode:S}}var Ee=Ae,Ue=Ee,Q=Ue;var Be=new Uint8Array(0);var _=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")};var j=class{constructor(r,t,o){this.name=r,this.prefix=t,this.baseEncode=o}encode(r){if(r instanceof Uint8Array)return`${this.prefix}${this.baseEncode(r)}`;throw Error("Unknown type, must be binary type")}},D=class{constructor(r,t,o){if(this.name=r,this.prefix=t,t.codePointAt(0)===void 0)throw new Error("Invalid prefix character");this.prefixCodePoint=t.codePointAt(0),this.baseDecode=o}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 W(this,r)}},R=class{constructor(r){this.decoders=r}or(r){return W(this,r)}decode(r){let t=r[0],o=this.decoders[t];if(o)return o.decode(r);throw RangeError(`Unable to decode multibase string ${JSON.stringify(r)}, only inputs prefixed with ${Object.keys(this.decoders)} are supported`)}},W=(e,r)=>new R({...e.decoders||{[e.prefix]:e},...r.decoders||{[r.prefix]:r}}),B=class{constructor(r,t,o,s){this.name=r,this.prefix=t,this.baseEncode=o,this.baseDecode=s,this.encoder=new j(r,t,o),this.decoder=new D(r,t,s)}encode(r){return this.encoder.encode(r)}decode(r){return this.decoder.decode(r)}},Y=({name:e,prefix:r,encode:t,decode:o})=>new B(e,r,t,o),J=({prefix:e,name:r,alphabet:t})=>{let{encode:o,decode:s}=Q(t,r);return Y({prefix:e,name:r,encode:o,decode:p=>_(s(p))})},Se=(e,r,t,o)=>{let s={};for(let n=0;n<r.length;++n)s[r[n]]=n;let p=e.length;for(;e[p-1]==="=";)--p;let m=new Uint8Array(p*t/8|0),u=0,w=0,O=0;for(let n=0;n<p;++n){let a=s[e[n]];if(a===void 0)throw new SyntaxError(`Non-${o} character`);w=w<<t|a,u+=t,u>=8&&(u-=8,m[O++]=255&w>>u)}if(u>=t||255&w<<8-u)throw new SyntaxError("Unexpected end of data");return m},ke=(e,r,t)=>{let o=r[r.length-1]==="=",s=(1<<t)-1,p="",m=0,u=0;for(let w=0;w<e.length;++w)for(u=u<<8|e[w],m+=8;m>t;)m-=t,p+=r[s&u>>m];if(m&&(p+=r[s&u<<t-m]),o)for(;p.length*t&7;)p+="=";return p},g=({name:e,prefix:r,bitsPerChar:t,alphabet:o})=>Y({prefix:r,name:e,encode(s){return ke(s,o,t)},decode(s){return Se(s,o,t,e)}});var H=g({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5}),Xe=g({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ze=g({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ke=g({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Qe=g({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),_e=g({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),We=g({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ye=g({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),He=g({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});var ee=J({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"}),tr=J({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var re=g({prefix:"m",name:"base64",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",bitsPerChar:6}),sr=g({prefix:"M",name:"base64pad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",bitsPerChar:6}),ar=g({prefix:"u",name:"base64url",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",bitsPerChar:6}),ir=g({prefix:"U",name:"base64urlpad",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_=",bitsPerChar:6});function te(e,r={}){let t=r.prefixLength??2,o=r.suffixLength??4,s=e.toString();return`${s.substring(0,t)}\u2026${s.substring(s.length,s.length-o)}`}b.default.formatters.b=e=>e==null?"undefined":ee.baseEncode(e);b.default.formatters.t=e=>e==null?"undefined":H.baseEncode(e);b.default.formatters.m=e=>e==null?"undefined":re.baseEncode(e);b.default.formatters.p=e=>e==null?"undefined":e.toString();b.default.formatters.c=e=>e==null?"undefined":e.toString();b.default.formatters.k=e=>e==null?"undefined":e.toString();b.default.formatters.a=e=>e==null?"undefined":e.toString();function Le(e){let r=()=>{};return r.enabled=!1,r.color="",r.diff=0,r.log=()=>{},r.namespace=e,r.destroy=()=>!0,r.extend=()=>r,r}function Me(e,r={}){return ne(te(e,r))}function ne(e){return{forComponent(r){return T(`${e}:${r}`)}}}function ze(){return{forComponent(e){return T(e)}}}function T(e){let r=Le(`${e}:trace`);return b.default.enabled(`${e}:trace`)&&b.default.names.map(t=>t.toString()).find(t=>t.includes(":trace"))!=null&&(r=(0,b.default)(`${e}:trace`)),Object.assign((0,b.default)(e),{error:(0,b.default)(`${e}:error`),trace:r})}function Oe(){b.default.disable()}function Ie(e){b.default.enable(e)}function Pe(e){return b.default.enabled(e)}return de($e);})();
|
|
3
3
|
return Libp2PLogger}));
|
package/dist/src/index.d.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
6
|
-
* If metrics are disabled a regular map is used.
|
|
4
|
+
* A logger for libp2p based on the venerable [debug](https://www.npmjs.com/package/debug) module.
|
|
7
5
|
*
|
|
8
6
|
* @example
|
|
9
7
|
*
|
|
@@ -28,12 +26,86 @@
|
|
|
28
26
|
* with this base32: bafyfoo
|
|
29
27
|
* ```
|
|
30
28
|
*/
|
|
29
|
+
import type { PeerId } from '@libp2p/interface/peer-id';
|
|
31
30
|
export interface Logger {
|
|
32
31
|
(formatter: any, ...args: any[]): void;
|
|
33
32
|
error(formatter: any, ...args: any[]): void;
|
|
34
33
|
trace(formatter: any, ...args: any[]): void;
|
|
35
34
|
enabled: boolean;
|
|
36
35
|
}
|
|
36
|
+
export interface ComponentLogger {
|
|
37
|
+
forComponent(name: string): Logger;
|
|
38
|
+
}
|
|
39
|
+
export interface PeerLoggerOptions {
|
|
40
|
+
prefixLength: number;
|
|
41
|
+
suffixLength: number;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Create a component logger that will prefix any log messages with a truncated
|
|
45
|
+
* peer id.
|
|
46
|
+
*
|
|
47
|
+
* @example
|
|
48
|
+
*
|
|
49
|
+
* ```TypeScript
|
|
50
|
+
* import { peerLogger } from '@libp2p/logger'
|
|
51
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
52
|
+
*
|
|
53
|
+
* const peerId = peerIdFromString('12D3FooBar')
|
|
54
|
+
* const logger = peerLogger(peerId)
|
|
55
|
+
*
|
|
56
|
+
* const log = logger.forComponent('my-component')
|
|
57
|
+
* log.info('hello world')
|
|
58
|
+
* // logs "12…oBar:my-component hello world"
|
|
59
|
+
* ```
|
|
60
|
+
*/
|
|
61
|
+
export declare function peerLogger(peerId: PeerId, options?: Partial<PeerLoggerOptions>): ComponentLogger;
|
|
62
|
+
/**
|
|
63
|
+
* Create a component logger that will prefix any log messages with the passed
|
|
64
|
+
* string.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
*
|
|
68
|
+
* ```TypeScript
|
|
69
|
+
* import { prefixLogger } from '@libp2p/logger'
|
|
70
|
+
*
|
|
71
|
+
* const logger = prefixLogger('my-node')
|
|
72
|
+
*
|
|
73
|
+
* const log = logger.forComponent('my-component')
|
|
74
|
+
* log.info('hello world')
|
|
75
|
+
* // logs "my-node:my-component hello world"
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare function prefixLogger(prefix: string): ComponentLogger;
|
|
79
|
+
/**
|
|
80
|
+
* Create a component logger
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
*
|
|
84
|
+
* ```TypeScript
|
|
85
|
+
* import { defaultLogger } from '@libp2p/logger'
|
|
86
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
87
|
+
*
|
|
88
|
+
* const logger = defaultLogger()
|
|
89
|
+
*
|
|
90
|
+
* const log = logger.forComponent('my-component')
|
|
91
|
+
* log.info('hello world')
|
|
92
|
+
* // logs "my-component hello world"
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
95
|
+
export declare function defaultLogger(): ComponentLogger;
|
|
96
|
+
/**
|
|
97
|
+
* Creates a logger for the passed component name.
|
|
98
|
+
*
|
|
99
|
+
* @example
|
|
100
|
+
*
|
|
101
|
+
* ```TypeScript
|
|
102
|
+
* import { logger } from '@libp2p/logger'
|
|
103
|
+
*
|
|
104
|
+
* const log = logger('my-component')
|
|
105
|
+
* log.info('hello world')
|
|
106
|
+
* // logs "my-component hello world"
|
|
107
|
+
* ```
|
|
108
|
+
*/
|
|
37
109
|
export declare function logger(name: string): Logger;
|
|
38
110
|
export declare function disable(): void;
|
|
39
111
|
export declare function enable(namespaces: string): void;
|
package/dist/src/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;GA2BG;AAOH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2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|
4
|
+
* A logger for libp2p based on the venerable [debug](https://www.npmjs.com/package/debug) module.
|
|
7
5
|
*
|
|
8
6
|
* @example
|
|
9
7
|
*
|
|
@@ -32,6 +30,7 @@ import debug from 'debug';
|
|
|
32
30
|
import { base32 } from 'multiformats/bases/base32';
|
|
33
31
|
import { base58btc } from 'multiformats/bases/base58';
|
|
34
32
|
import { base64 } from 'multiformats/bases/base64';
|
|
33
|
+
import { truncatePeerId } from './utils.js';
|
|
35
34
|
// Add a formatter for converting to a base58 string
|
|
36
35
|
debug.formatters.b = (v) => {
|
|
37
36
|
return v == null ? 'undefined' : base58btc.baseEncode(v);
|
|
@@ -71,6 +70,86 @@ function createDisabledLogger(namespace) {
|
|
|
71
70
|
logger.extend = () => logger;
|
|
72
71
|
return logger;
|
|
73
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* Create a component logger that will prefix any log messages with a truncated
|
|
75
|
+
* peer id.
|
|
76
|
+
*
|
|
77
|
+
* @example
|
|
78
|
+
*
|
|
79
|
+
* ```TypeScript
|
|
80
|
+
* import { peerLogger } from '@libp2p/logger'
|
|
81
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
82
|
+
*
|
|
83
|
+
* const peerId = peerIdFromString('12D3FooBar')
|
|
84
|
+
* const logger = peerLogger(peerId)
|
|
85
|
+
*
|
|
86
|
+
* const log = logger.forComponent('my-component')
|
|
87
|
+
* log.info('hello world')
|
|
88
|
+
* // logs "12…oBar:my-component hello world"
|
|
89
|
+
* ```
|
|
90
|
+
*/
|
|
91
|
+
export function peerLogger(peerId, options = {}) {
|
|
92
|
+
return prefixLogger(truncatePeerId(peerId, options));
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Create a component logger that will prefix any log messages with the passed
|
|
96
|
+
* string.
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
*
|
|
100
|
+
* ```TypeScript
|
|
101
|
+
* import { prefixLogger } from '@libp2p/logger'
|
|
102
|
+
*
|
|
103
|
+
* const logger = prefixLogger('my-node')
|
|
104
|
+
*
|
|
105
|
+
* const log = logger.forComponent('my-component')
|
|
106
|
+
* log.info('hello world')
|
|
107
|
+
* // logs "my-node:my-component hello world"
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
export function prefixLogger(prefix) {
|
|
111
|
+
return {
|
|
112
|
+
forComponent(name) {
|
|
113
|
+
return logger(`${prefix}:${name}`);
|
|
114
|
+
}
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Create a component logger
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
*
|
|
122
|
+
* ```TypeScript
|
|
123
|
+
* import { defaultLogger } from '@libp2p/logger'
|
|
124
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
125
|
+
*
|
|
126
|
+
* const logger = defaultLogger()
|
|
127
|
+
*
|
|
128
|
+
* const log = logger.forComponent('my-component')
|
|
129
|
+
* log.info('hello world')
|
|
130
|
+
* // logs "my-component hello world"
|
|
131
|
+
* ```
|
|
132
|
+
*/
|
|
133
|
+
export function defaultLogger() {
|
|
134
|
+
return {
|
|
135
|
+
forComponent(name) {
|
|
136
|
+
return logger(name);
|
|
137
|
+
}
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Creates a logger for the passed component name.
|
|
142
|
+
*
|
|
143
|
+
* @example
|
|
144
|
+
*
|
|
145
|
+
* ```TypeScript
|
|
146
|
+
* import { logger } from '@libp2p/logger'
|
|
147
|
+
*
|
|
148
|
+
* const log = logger('my-component')
|
|
149
|
+
* log.info('hello world')
|
|
150
|
+
* // logs "my-component hello world"
|
|
151
|
+
* ```
|
|
152
|
+
*/
|
|
74
153
|
export function logger(name) {
|
|
75
154
|
// trace logging is a no-op by default
|
|
76
155
|
let trace = createDisabledLogger(`${name}:trace`);
|
package/dist/src/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","source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U,OAAO,CAAE,UAAkB;IACzC,OAAO,KAAK,CAAC,OAAO,CAAC,UAAU,CAAC,CAAA;AAClC,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAA;AACnD,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,2BAA2B,CAAA;AAEvD,wBAAgB,cAAc,CAAE,MAAM,EAAE,MAAM,EAAE,OAAO,GAAE,OAAO,CAAC,iBAAiB,CAAM,GAAG,MAAM,CAMhG"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export function truncatePeerId(peerId, options = {}) {
|
|
2
|
+
const prefixLength = options.prefixLength ?? 2;
|
|
3
|
+
const suffixLength = options.suffixLength ?? 4;
|
|
4
|
+
const peerIdString = peerId.toString();
|
|
5
|
+
return `${peerIdString.substring(0, prefixLength)}…${peerIdString.substring(peerIdString.length, peerIdString.length - suffixLength)}`;
|
|
6
|
+
}
|
|
7
|
+
//# sourceMappingURL=utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils.ts"],"names":[],"mappings":"AAGA,MAAM,UAAU,cAAc,CAAE,MAAc,EAAE,UAAsC,EAAE;IACtF,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,CAAC,CAAA;IAC9C,MAAM,YAAY,GAAG,OAAO,CAAC,YAAY,IAAI,CAAC,CAAA;IAE9C,MAAM,YAAY,GAAG,MAAM,CAAC,QAAQ,EAAE,CAAA;IACtC,OAAO,GAAG,YAAY,CAAC,SAAS,CAAC,CAAC,EAAE,YAAY,CAAC,IAAI,YAAY,CAAC,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,YAAY,CAAC,MAAM,GAAG,YAAY,CAAC,EAAE,CAAA;AACxI,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@libp2p/logger",
|
|
3
|
-
"version": "3.0
|
|
3
|
+
"version": "3.1.0-05b52d69c",
|
|
4
4
|
"description": "A logging component for use in js-libp2p modules",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/master/packages/logger#readme",
|
|
@@ -49,17 +49,17 @@
|
|
|
49
49
|
"test:electron-main": "aegir test -t electron-main"
|
|
50
50
|
},
|
|
51
51
|
"dependencies": {
|
|
52
|
-
"@libp2p/interface": "
|
|
53
|
-
"@multiformats/multiaddr": "^12.1.
|
|
52
|
+
"@libp2p/interface": "0.1.6-05b52d69c",
|
|
53
|
+
"@multiformats/multiaddr": "^12.1.10",
|
|
54
54
|
"debug": "^4.3.4",
|
|
55
55
|
"interface-datastore": "^8.2.0",
|
|
56
|
-
"multiformats": "^12.
|
|
56
|
+
"multiformats": "^12.1.3"
|
|
57
57
|
},
|
|
58
58
|
"devDependencies": {
|
|
59
|
-
"@libp2p/peer-id": "
|
|
59
|
+
"@libp2p/peer-id": "3.0.6-05b52d69c",
|
|
60
60
|
"@types/debug": "^4.1.7",
|
|
61
61
|
"aegir": "^41.0.2",
|
|
62
62
|
"sinon": "^17.0.0",
|
|
63
|
-
"uint8arrays": "^4.0.
|
|
63
|
+
"uint8arrays": "^4.0.6"
|
|
64
64
|
}
|
|
65
65
|
}
|
package/src/index.ts
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* @packageDocumentation
|
|
3
3
|
*
|
|
4
|
-
* A
|
|
5
|
-
*
|
|
6
|
-
* If metrics are disabled a regular map is used.
|
|
4
|
+
* A logger for libp2p based on the venerable [debug](https://www.npmjs.com/package/debug) module.
|
|
7
5
|
*
|
|
8
6
|
* @example
|
|
9
7
|
*
|
|
@@ -33,6 +31,7 @@ import debug from 'debug'
|
|
|
33
31
|
import { base32 } from 'multiformats/bases/base32'
|
|
34
32
|
import { base58btc } from 'multiformats/bases/base58'
|
|
35
33
|
import { base64 } from 'multiformats/bases/base64'
|
|
34
|
+
import { truncatePeerId } from './utils.js'
|
|
36
35
|
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
37
36
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
38
37
|
import type { Key } from 'interface-datastore'
|
|
@@ -80,6 +79,10 @@ export interface Logger {
|
|
|
80
79
|
enabled: boolean
|
|
81
80
|
}
|
|
82
81
|
|
|
82
|
+
export interface ComponentLogger {
|
|
83
|
+
forComponent(name: string): Logger
|
|
84
|
+
}
|
|
85
|
+
|
|
83
86
|
function createDisabledLogger (namespace: string): debug.Debugger {
|
|
84
87
|
const logger = (): void => {}
|
|
85
88
|
logger.enabled = false
|
|
@@ -93,6 +96,94 @@ function createDisabledLogger (namespace: string): debug.Debugger {
|
|
|
93
96
|
return logger
|
|
94
97
|
}
|
|
95
98
|
|
|
99
|
+
export interface PeerLoggerOptions {
|
|
100
|
+
prefixLength: number
|
|
101
|
+
suffixLength: number
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Create a component logger that will prefix any log messages with a truncated
|
|
106
|
+
* peer id.
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
*
|
|
110
|
+
* ```TypeScript
|
|
111
|
+
* import { peerLogger } from '@libp2p/logger'
|
|
112
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
113
|
+
*
|
|
114
|
+
* const peerId = peerIdFromString('12D3FooBar')
|
|
115
|
+
* const logger = peerLogger(peerId)
|
|
116
|
+
*
|
|
117
|
+
* const log = logger.forComponent('my-component')
|
|
118
|
+
* log.info('hello world')
|
|
119
|
+
* // logs "12…oBar:my-component hello world"
|
|
120
|
+
* ```
|
|
121
|
+
*/
|
|
122
|
+
export function peerLogger (peerId: PeerId, options: Partial<PeerLoggerOptions> = {}): ComponentLogger {
|
|
123
|
+
return prefixLogger(truncatePeerId(peerId, options))
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Create a component logger that will prefix any log messages with the passed
|
|
128
|
+
* string.
|
|
129
|
+
*
|
|
130
|
+
* @example
|
|
131
|
+
*
|
|
132
|
+
* ```TypeScript
|
|
133
|
+
* import { prefixLogger } from '@libp2p/logger'
|
|
134
|
+
*
|
|
135
|
+
* const logger = prefixLogger('my-node')
|
|
136
|
+
*
|
|
137
|
+
* const log = logger.forComponent('my-component')
|
|
138
|
+
* log.info('hello world')
|
|
139
|
+
* // logs "my-node:my-component hello world"
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
export function prefixLogger (prefix: string): ComponentLogger {
|
|
143
|
+
return {
|
|
144
|
+
forComponent (name: string) {
|
|
145
|
+
return logger(`${prefix}:${name}`)
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Create a component logger
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
*
|
|
155
|
+
* ```TypeScript
|
|
156
|
+
* import { defaultLogger } from '@libp2p/logger'
|
|
157
|
+
* import { peerIdFromString } from '@libp2p/peer-id'
|
|
158
|
+
*
|
|
159
|
+
* const logger = defaultLogger()
|
|
160
|
+
*
|
|
161
|
+
* const log = logger.forComponent('my-component')
|
|
162
|
+
* log.info('hello world')
|
|
163
|
+
* // logs "my-component hello world"
|
|
164
|
+
* ```
|
|
165
|
+
*/
|
|
166
|
+
export function defaultLogger (): ComponentLogger {
|
|
167
|
+
return {
|
|
168
|
+
forComponent (name: string) {
|
|
169
|
+
return logger(name)
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Creates a logger for the passed component name.
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
*
|
|
179
|
+
* ```TypeScript
|
|
180
|
+
* import { logger } from '@libp2p/logger'
|
|
181
|
+
*
|
|
182
|
+
* const log = logger('my-component')
|
|
183
|
+
* log.info('hello world')
|
|
184
|
+
* // logs "my-component hello world"
|
|
185
|
+
* ```
|
|
186
|
+
*/
|
|
96
187
|
export function logger (name: string): Logger {
|
|
97
188
|
// trace logging is a no-op by default
|
|
98
189
|
let trace: debug.Debugger = createDisabledLogger(`${name}:trace`)
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { PeerLoggerOptions } from './index.js'
|
|
2
|
+
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
3
|
+
|
|
4
|
+
export function truncatePeerId (peerId: PeerId, options: Partial<PeerLoggerOptions> = {}): string {
|
|
5
|
+
const prefixLength = options.prefixLength ?? 2
|
|
6
|
+
const suffixLength = options.suffixLength ?? 4
|
|
7
|
+
|
|
8
|
+
const peerIdString = peerId.toString()
|
|
9
|
+
return `${peerIdString.substring(0, prefixLength)}…${peerIdString.substring(peerIdString.length, peerIdString.length - suffixLength)}`
|
|
10
|
+
}
|
package/dist/typedoc-urls.json
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"Logger": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_logger.Logger.html",
|
|
3
|
-
".:Logger": "https://libp2p.github.io/js-libp2p/interfaces/_libp2p_logger.Logger.html",
|
|
4
|
-
"disable": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.disable.html",
|
|
5
|
-
".:disable": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.disable.html",
|
|
6
|
-
"enable": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.enable.html",
|
|
7
|
-
".:enable": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.enable.html",
|
|
8
|
-
"enabled": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.enabled.html",
|
|
9
|
-
".:enabled": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.enabled.html",
|
|
10
|
-
"logger": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.logger-1.html",
|
|
11
|
-
".:logger": "https://libp2p.github.io/js-libp2p/functions/_libp2p_logger.logger-1.html"
|
|
12
|
-
}
|