libp2p 1.0.11-cd8cafcd5 → 1.0.12
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/dist/index.min.js +1 -1
- package/dist/src/version.d.ts +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js +1 -1
- package/dist/src/version.js.map +1 -1
- package/dist/typedoc-urls.json +10 -0
- package/package.json +18 -18
- package/src/version.ts +1 -1
package/dist/index.min.js
CHANGED
|
@@ -58,7 +58,7 @@
|
|
|
58
58
|
`),r),r.log.trace('handle: responded with "%s" for "%s"',i,i),{stream:n.unwrap(),protocol:i};if(i==="ls"){let s=new Fe(...e.map(o=>wn.single(rt(`${o}
|
|
59
59
|
`))),rt(`
|
|
60
60
|
`));r.log.trace('handle: respond with "%s" for %s',e,i),await vn(n,s,r),r.log.trace('handle: responded with "%s" for %s',e,i);continue}r.log('handle: respond with "na" for "%s"',i),await vn(n,rt(`na
|
|
61
|
-
`),r),r.log('handle: responded with "na" for "%s"',i)}}var Vv=500,zl=class{id;remoteAddr;remotePeer;direction;timeline;multiplexer;encryption;status;transient;log;tags;_newStream;_close;_abort;_getStreams;constructor(e){let{remoteAddr:r,remotePeer:n,newStream:i,close:s,abort:o,getStreams:a}=e;this.id=`${parseInt(String(Math.random()*1e9)).toString(36)}${Date.now()}`,this.remoteAddr=r,this.remotePeer=n,this.direction=e.direction,this.status="open",this.timeline=e.timeline,this.multiplexer=e.multiplexer,this.encryption=e.encryption,this.transient=e.transient??!1,this.log=e.logger.forComponent(`libp2p:connection:${this.direction}:${this.id}`),this.remoteAddr.getPeerId()==null&&(this.remoteAddr=this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)),this._newStream=i,this._close=s,this._abort=o,this._getStreams=a,this.tags=[]}[Symbol.toStringTag]="Connection";[xh]=!0;get streams(){return this._getStreams()}async newStream(e,r){if(this.status==="closing")throw new S("the connection is being closed","ERR_CONNECTION_BEING_CLOSED");if(this.status==="closed")throw new S("the connection is closed","ERR_CONNECTION_CLOSED");if(Array.isArray(e)||(e=[e]),this.transient&&r?.runOnTransientConnection!==!0)throw new S("Cannot open protocol stream on transient connection","ERR_TRANSIENT_CONNECTION");let n=await this._newStream(e,r);return n.direction="outbound",n}async close(e={}){if(!(this.status==="closed"||this.status==="closing")){if(this.log("closing connection to %a",this.remoteAddr),this.status="closing",e.signal==null){let r=AbortSignal.timeout(Vv);ht(1/0,r),e={...e,signal:r}}try{this.log.trace("closing all streams"),await Promise.all(this.streams.map(async r=>r.close(e))),this.log.trace("closing underlying transport"),await this._close(e),this.log.trace("updating timeline with close time"),this.status="closed",this.timeline.close=Date.now()}catch(r){this.log.error("error encountered during graceful close of connection to %a",this.remoteAddr,r),this.abort(r)}}}abort(e){this.log.error("aborting connection to %a due to error",this.remoteAddr,e),this.status="closing",this.streams.forEach(r=>{r.abort(e)}),this.log.error("all streams aborted",this.streams.length),this._abort(e),this.timeline.close=Date.now(),this.status="closed"}};function og(t){return new zl(t)}var zv=3e4;function Hv(t,e){try{let{options:r}=e.getHandler(t);return r.maxInboundStreams}catch(r){if(r.code!==F.ERR_NO_HANDLER_FOR_PROTOCOL)throw r}return Ol}function Gv(t,e,r={}){try{let{options:n}=e.getHandler(t);if(n.maxOutboundStreams!=null)return n.maxOutboundStreams}catch(n){if(n.code!==F.ERR_NO_HANDLER_FOR_PROTOCOL)throw n}return r.maxOutboundStreams??Ml}function ag(t,e,r){let n=0;return r.streams.forEach(i=>{i.direction===e&&i.protocol===t&&n++}),n}var da=class{components;connectionEncryption;muxers;inboundUpgradeTimeout;events;constructor(e,r){this.components=e,this.connectionEncryption=new Map,r.connectionEncryption.forEach(n=>{this.connectionEncryption.set(n.protocol,n)}),this.muxers=new Map,r.muxers.forEach(n=>{this.muxers.set(n.protocol,n)}),this.inboundUpgradeTimeout=r.inboundUpgradeTimeout??3e4,this.events=e.events}async shouldBlockConnection(e,r,n){let i=this.components.connectionGater[n];if(i!==void 0&&await i(e,r))throw new S(`The multiaddr connection is blocked by gater.${n}`,F.ERR_CONNECTION_INTERCEPTED)}async upgradeInbound(e,r){if(!await this.components.connectionManager.acceptIncomingConnection(e))throw new S("connection denied",F.ERR_CONNECTION_DENIED);let i,s,o,a,c,u=AbortSignal.timeout(this.inboundUpgradeTimeout),l=()=>{e.abort(new S("inbound upgrade timeout",_s))};u.addEventListener("abort",l,{once:!0}),ht(1/0,u);try{if(await this.components.connectionGater.denyInboundConnection?.(e)===!0)throw new S("The multiaddr connection is blocked by gater.acceptConnection",F.ERR_CONNECTION_INTERCEPTED);this.components.metrics?.trackMultiaddrConnection(e),e.log("starting the inbound connection upgrade");let f=e;if(r?.skipProtection!==!0){let d=this.components.connectionProtector;d!=null&&(e.log("protecting the inbound connection"),f=await d.protect(e))}try{if(i=f,r?.skipEncryption!==!0){({conn:i,remotePeer:s,protocol:c}=await this._encryptInbound(f));let d={...f,...i};await this.shouldBlockConnection(s,d,"denyInboundEncryptedConnection")}else{let d=e.remoteAddr.getPeerId();if(d==null)throw new S("inbound connection that skipped encryption must have a peer id",F.ERR_INVALID_MULTIADDR);let p=se(d);c="native",s=p}if(o=i,r?.muxerFactory!=null)a=r.muxerFactory;else if(this.muxers.size>0){let d=await this._multiplexInbound({...f,...i},this.muxers);a=d.muxerFactory,o=d.stream}}catch(d){throw e.log.error("failed to upgrade inbound connection",d),d}return await this.shouldBlockConnection(s,e,"denyInboundUpgradedConnection"),e.log("successfully upgraded inbound connection"),this._createConnection({cryptoProtocol:c,direction:"inbound",maConn:e,upgradedConn:o,muxerFactory:a,remotePeer:s,transient:r?.transient})}finally{u.removeEventListener("abort",l),this.components.connectionManager.afterUpgradeInbound()}}async upgradeOutbound(e,r){let n=e.remoteAddr.getPeerId(),i;n!=null&&(i=se(n),await this.shouldBlockConnection(i,e,"denyOutboundConnection"));let s,o,a,c,u;this.components.metrics?.trackMultiaddrConnection(e),e.log("starting the outbound connection upgrade");let l=e;if(r?.skipProtection!==!0){let f=this.components.connectionProtector;f!=null&&(l=await f.protect(e))}try{if(s=l,r?.skipEncryption!==!0){({conn:s,remotePeer:o,protocol:c}=await this._encryptOutbound(l,i));let f={...l,...s};await this.shouldBlockConnection(o,f,"denyOutboundEncryptedConnection")}else{if(i==null)throw new S("Encryption was skipped but no peer id was passed",F.ERR_INVALID_PEER);c="native",o=i}if(a=s,r?.muxerFactory!=null)u=r.muxerFactory;else if(this.muxers.size>0){let f=await this._multiplexOutbound({...l,...s},this.muxers);u=f.muxerFactory,a=f.stream}}catch(f){throw e.log.error("failed to upgrade outbound connection",f),await e.close(f),f}return await this.shouldBlockConnection(o,e,"denyOutboundUpgradedConnection"),e.log("successfully upgraded outbound connection"),this._createConnection({cryptoProtocol:c,direction:"outbound",maConn:e,upgradedConn:a,muxerFactory:u,remotePeer:o,transient:r?.transient})}_createConnection(e){let{cryptoProtocol:r,direction:n,maConn:i,upgradedConn:s,remotePeer:o,muxerFactory:a,transient:c}=e,u,l,f;a!=null&&(u=a.createStreamMuxer({direction:n,onIncomingStream:h=>{f!=null&&Promise.resolve().then(async()=>{let m=this.components.registrar.getProtocols(),{stream:g,protocol:y}=await ns(h,m,{log:h.log,yieldBytes:!1});if(f==null)return;f.log("incoming stream opened on %s",y);let x=Hv(y,this.components.registrar);if(ag(y,"inbound",f)===x){let I=new S(`Too many inbound protocol streams for protocol "${y}" - limit ${x}`,F.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS);throw h.abort(I),I}h.source=g.source,h.sink=g.sink,h.protocol=y,g.closeWrite!=null&&(h.closeWrite=g.closeWrite),g.closeRead!=null&&(h.closeRead=g.closeRead),g.close!=null&&(h.close=g.close),await this.components.peerStore.merge(o,{protocols:[y]}),this.components.metrics?.trackProtocolStream(h,f),this._onStream({connection:f,stream:h,protocol:y})}).catch(async m=>{f.log.error("error handling incoming stream id %s",h.id,m.message,m.code,m.stack),h.timeline.close==null&&await h.close()})}}),l=async(h,m={})=>{if(u==null)throw new S("Stream is not multiplexed",F.ERR_MUXER_UNAVAILABLE);f.log("starting new stream for protocols %s",h);let g=await u.newStream();f.log.trace("started new stream %s for protocols %s",g.id,h);try{if(m.signal==null){g.log("no abort signal was passed while trying to negotiate protocols %s falling back to default timeout",h);let v=AbortSignal.timeout(zv);ht(1/0,v),m={...m,signal:v}}g.log.trace("selecting protocol from protocols %s",h);let{stream:y,protocol:x}=await rs(g,h,{...m,log:g.log,yieldBytes:!0});g.log("selected protocol %s",x);let b=Gv(x,this.components.registrar,m);if(ag(x,"outbound",f)>=b){let v=new S(`Too many outbound protocol streams for protocol "${x}" - limit ${b}`,F.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS);throw g.abort(v),v}return await this.components.peerStore.merge(o,{protocols:[x]}),g.source=y.source,g.sink=y.sink,g.protocol=x,y.closeWrite!=null&&(g.closeWrite=y.closeWrite),y.closeRead!=null&&(g.closeRead=y.closeRead),y.close!=null&&(g.close=y.close),this.components.metrics?.trackProtocolStream(g,f),g}catch(y){throw f.log.error("could not create new stream for protocols %s",h,y),g.timeline.close==null&&g.abort(y),y.code!=null?y:new S(String(y),F.ERR_UNSUPPORTED_PROTOCOL)}},Promise.all([u.sink(s.source),s.sink(u.source)]).catch(h=>{f.log.error("error piping data through muxer",h)}));let d=i.timeline;i.timeline=new Proxy(d,{set:(...h)=>(f!=null&&h[1]==="close"&&h[2]!=null&&d.close==null&&(async()=>{try{f.status==="open"&&await f.close()}catch(m){f.log.error("error closing connection after timeline close",m)}finally{this.events.safeDispatchEvent("connection:close",{detail:f})}})().catch(m=>{f.log.error("error thrown while dispatching connection:close event",m)}),Reflect.set(...h))}),i.timeline.upgraded=Date.now();let p=()=>{throw new S("connection is not multiplexed",F.ERR_CONNECTION_NOT_MULTIPLEXED)};return f=og({remoteAddr:i.remoteAddr,remotePeer:o,status:"open",direction:n,timeline:i.timeline,multiplexer:u?.protocol,encryption:r,transient:c,logger:this.components.logger,newStream:l??p,getStreams:()=>u!=null?u.streams:[],close:async h=>{u!=null&&(f.log.trace("close muxer"),await u.close(h)),f.log.trace("close maconn"),await i.close(h),f.log.trace("closed maconn")},abort:h=>{i.abort(h),u?.abort(h)}}),this.events.safeDispatchEvent("connection:open",{detail:f}),f}_onStream(e){let{connection:r,stream:n,protocol:i}=e,{handler:s,options:o}=this.components.registrar.getHandler(i);if(r.transient&&o.runOnTransientConnection!==!0)throw new S("Cannot open protocol stream on transient connection","ERR_TRANSIENT_CONNECTION");s({connection:r,stream:n})}async _encryptInbound(e){let r=Array.from(this.connectionEncryption.keys());e.log("handling inbound crypto protocol selection",r);try{let{stream:n,protocol:i}=await ns(e,r,{log:e.log}),s=this.connectionEncryption.get(i);if(s==null)throw new Error(`no crypto module found for ${i}`);return e.log("encrypting inbound connection using",i),{...await s.secureInbound(this.components.peerId,n),protocol:i}}catch(n){throw e.log.error("encrypting inbound connection to %p failed",n),new S(n.message,F.ERR_ENCRYPTION_FAILED)}}async _encryptOutbound(e,r){let n=Array.from(this.connectionEncryption.keys());e.log("selecting outbound crypto protocol",n);try{e.log.trace("selecting encrypter from %s",n);let{stream:i,protocol:s}=await rs(e,n,{log:e.log,yieldBytes:!0}),o=this.connectionEncryption.get(s);if(o==null)throw new Error(`no crypto module found for ${s}`);return e.log("encrypting outbound connection to %p using %p",r),{...await o.secureOutbound(this.components.peerId,i,r),protocol:s}}catch(i){throw e.log.error("encrypting outbound connection to %p failed",i),new S(i.message,F.ERR_ENCRYPTION_FAILED)}}async _multiplexOutbound(e,r){let n=Array.from(r.keys());e.log("outbound selecting muxer %s",n);try{e.log.trace("selecting stream muxer from %s",n);let{stream:i,protocol:s}=await rs(e,n,{log:e.log,yieldBytes:!0});e.log("selected %s as muxer protocol",s);let o=r.get(s);return{stream:i,muxerFactory:o}}catch(i){throw e.log.error("error multiplexing outbound connection",i),new S(String(i),F.ERR_MUXER_UNAVAILABLE)}}async _multiplexInbound(e,r){let n=Array.from(r.keys());e.log("inbound handling muxers %s",n);try{let{stream:i,protocol:s}=await ns(e,n,{log:e.log}),o=r.get(s);return{stream:i,muxerFactory:o}}catch(i){throw e.log.error("error multiplexing inbound connection",i),new S(String(i),F.ERR_MUXER_UNAVAILABLE)}}};var cg="1.0.11-cd8cafcd5",ug="libp2p";var Hl=class extends wi{peerId;peerStore;contentRouting;peerRouting;metrics;services;logger;status;components;log;constructor(e){super(),this.status="stopped";let r=new wi,n=r.dispatchEvent.bind(r);r.dispatchEvent=a=>{let c=n(a),u=this.dispatchEvent(new $a(a.type,{detail:a.detail}));return c||u},ht(1/0,r),this.peerId=e.peerId,this.logger=e.logger??Qs(),this.log=this.logger.forComponent("libp2p"),this.services={};let i=this.components=hp({peerId:e.peerId,nodeInfo:e.nodeInfo??{name:ug,version:cg},logger:this.logger,events:r,datastore:e.datastore??new Eo,connectionGater:vp(e.connectionGater)});this.peerStore=this.configureComponent("peerStore",new bo(i,{addressFilter:this.components.connectionGater.filterMultiaddrForPeer,...e.peerStore})),e.metrics!=null&&(this.metrics=this.configureComponent("metrics",e.metrics(this.components))),i.events.addEventListener("peer:update",a=>{if(a.detail.previous==null){let c={id:a.detail.peer.id,multiaddrs:a.detail.peer.addresses.map(u=>u.multiaddr)};i.events.safeDispatchEvent("peer:discovery",{detail:c})}}),e.connectionProtector!=null&&this.configureComponent("connectionProtector",e.connectionProtector(i)),this.components.upgrader=new da(this.components,{connectionEncryption:(e.connectionEncryption??[]).map((a,c)=>this.configureComponent(`connection-encryption-${c}`,a(this.components))),muxers:(e.streamMuxers??[]).map((a,c)=>this.configureComponent(`stream-muxers-${c}`,a(this.components))),inboundUpgradeTimeout:e.connectionManager.inboundUpgradeTimeout}),this.configureComponent("transportManager",new na(this.components,e.transportManager)),this.configureComponent("connectionManager",new Zo(this.components,e.connectionManager)),this.configureComponent("registrar",new ra(this.components)),this.configureComponent("addressManager",new Ao(this.components,e.addresses));let s=(e.peerRouters??[]).map((a,c)=>this.configureComponent(`peer-router-${c}`,a(this.components)));this.peerRouting=this.components.peerRouting=this.configureComponent("peerRouting",new ta(this.components,{routers:s}));let o=(e.contentRouters??[]).map((a,c)=>this.configureComponent(`content-router-${c}`,a(this.components)));if(this.contentRouting=this.components.contentRouting=this.configureComponent("contentRouting",new ea(this.components,{routers:o})),(e.peerDiscovery??[]).forEach((a,c)=>{this.configureComponent(`peer-discovery-${c}`,a(this.components)).addEventListener("peer",l=>{this.#e(l)})}),e.transports?.forEach((a,c)=>{this.components.transportManager.add(this.configureComponent(`transport-${c}`,a(this.components)))}),e.services!=null)for(let a of Object.keys(e.services)){let c=e.services[a],u=c(this.components);if(u==null){this.log.error("service factory %s returned null or undefined instance",a);continue}this.services[a]=u,this.configureComponent(a,u),u[Ma]!=null&&(this.log("registering service %s for content routing",a),o.push(u[Ma])),u[Ka]!=null&&(this.log("registering service %s for peer routing",a),s.push(u[Ka])),u[Ua]!=null&&(this.log("registering service %s for peer discovery",a),u[Ua].addEventListener?.("peer",l=>{this.#e(l)}))}}configureComponent(e,r){return r==null&&this.log.error("component %s was null or undefined",e),this.components[e]=r,r}async start(){if(this.status==="stopped"){this.status="starting",this.log("libp2p is starting");try{await this.components.beforeStart?.(),await this.components.start(),await this.components.afterStart?.(),this.status="started",this.safeDispatchEvent("start",{detail:this}),this.log("libp2p has started")}catch(e){throw this.log.error("An error occurred starting libp2p",e),this.status="started",await this.stop(),e}}}async stop(){this.status==="started"&&(this.log("libp2p is stopping"),this.status="stopping",await this.components.beforeStop?.(),await this.components.stop(),await this.components.afterStop?.(),this.status="stopped",this.safeDispatchEvent("stop",{detail:this}),this.log("libp2p has stopped"))}getConnections(e){return this.components.connectionManager.getConnections(e)}getDialQueue(){return this.components.connectionManager.getDialQueue()}getPeers(){let e=new St;for(let r of this.components.connectionManager.getConnections())e.add(r.remotePeer);return Array.from(e)}async dial(e,r={}){return this.components.connectionManager.openConnection(e,{priority:75,...r})}async dialProtocol(e,r,n={}){if(r==null)throw new S("no protocols were provided to open a stream",F.ERR_INVALID_PROTOCOLS_FOR_STREAM);if(r=Array.isArray(r)?r:[r],r.length===0)throw new S("no protocols were provided to open a stream",F.ERR_INVALID_PROTOCOLS_FOR_STREAM);return(await this.dial(e,n)).newStream(r,n)}getMultiaddrs(){return this.components.addressManager.getAddresses()}getProtocols(){return this.components.registrar.getProtocols()}async hangUp(e,r={}){Pr(e)&&(e=se(e.getPeerId()??"")),await this.components.connectionManager.closeConnections(e,r)}async getPublicKey(e,r={}){if(this.log("getPublicKey %p",e),e.publicKey!=null)return e.publicKey;let n=await this.peerStore.get(e);if(n.id.publicKey!=null)return n.id.publicKey;let i=cp([So("/pk/"),e.multihash.digest]),s=await this.contentRouting.get(i,r);return Ui(s),await this.peerStore.patch(e,{publicKey:s}),s}async handle(e,r,n){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async i=>{await this.components.registrar.handle(i,r,n)}))}async unhandle(e){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async r=>{await this.components.registrar.unhandle(r)}))}async register(e,r){return this.components.registrar.register(e,r)}unregister(e){this.components.registrar.unregister(e)}#e(e){let{detail:r}=e;if(r.id.toString()===this.peerId.toString()){this.log.error(new Error(F.ERR_DISCOVERED_SELF));return}this.components.peerStore.merge(r.id,{multiaddrs:r.multiaddrs}).catch(n=>{this.log.error(n)})}};async function lg(t={}){return t.peerId??=await hd(),new Hl(Wp(t))}async function Yv(t={}){let e=await lg(t);return t.start!==!1&&await e.start(),e}return Sg(Qv);})();
|
|
61
|
+
`),r),r.log('handle: responded with "na" for "%s"',i)}}var Vv=500,zl=class{id;remoteAddr;remotePeer;direction;timeline;multiplexer;encryption;status;transient;log;tags;_newStream;_close;_abort;_getStreams;constructor(e){let{remoteAddr:r,remotePeer:n,newStream:i,close:s,abort:o,getStreams:a}=e;this.id=`${parseInt(String(Math.random()*1e9)).toString(36)}${Date.now()}`,this.remoteAddr=r,this.remotePeer=n,this.direction=e.direction,this.status="open",this.timeline=e.timeline,this.multiplexer=e.multiplexer,this.encryption=e.encryption,this.transient=e.transient??!1,this.log=e.logger.forComponent(`libp2p:connection:${this.direction}:${this.id}`),this.remoteAddr.getPeerId()==null&&(this.remoteAddr=this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)),this._newStream=i,this._close=s,this._abort=o,this._getStreams=a,this.tags=[]}[Symbol.toStringTag]="Connection";[xh]=!0;get streams(){return this._getStreams()}async newStream(e,r){if(this.status==="closing")throw new S("the connection is being closed","ERR_CONNECTION_BEING_CLOSED");if(this.status==="closed")throw new S("the connection is closed","ERR_CONNECTION_CLOSED");if(Array.isArray(e)||(e=[e]),this.transient&&r?.runOnTransientConnection!==!0)throw new S("Cannot open protocol stream on transient connection","ERR_TRANSIENT_CONNECTION");let n=await this._newStream(e,r);return n.direction="outbound",n}async close(e={}){if(!(this.status==="closed"||this.status==="closing")){if(this.log("closing connection to %a",this.remoteAddr),this.status="closing",e.signal==null){let r=AbortSignal.timeout(Vv);ht(1/0,r),e={...e,signal:r}}try{this.log.trace("closing all streams"),await Promise.all(this.streams.map(async r=>r.close(e))),this.log.trace("closing underlying transport"),await this._close(e),this.log.trace("updating timeline with close time"),this.status="closed",this.timeline.close=Date.now()}catch(r){this.log.error("error encountered during graceful close of connection to %a",this.remoteAddr,r),this.abort(r)}}}abort(e){this.log.error("aborting connection to %a due to error",this.remoteAddr,e),this.status="closing",this.streams.forEach(r=>{r.abort(e)}),this.log.error("all streams aborted",this.streams.length),this._abort(e),this.timeline.close=Date.now(),this.status="closed"}};function og(t){return new zl(t)}var zv=3e4;function Hv(t,e){try{let{options:r}=e.getHandler(t);return r.maxInboundStreams}catch(r){if(r.code!==F.ERR_NO_HANDLER_FOR_PROTOCOL)throw r}return Ol}function Gv(t,e,r={}){try{let{options:n}=e.getHandler(t);if(n.maxOutboundStreams!=null)return n.maxOutboundStreams}catch(n){if(n.code!==F.ERR_NO_HANDLER_FOR_PROTOCOL)throw n}return r.maxOutboundStreams??Ml}function ag(t,e,r){let n=0;return r.streams.forEach(i=>{i.direction===e&&i.protocol===t&&n++}),n}var da=class{components;connectionEncryption;muxers;inboundUpgradeTimeout;events;constructor(e,r){this.components=e,this.connectionEncryption=new Map,r.connectionEncryption.forEach(n=>{this.connectionEncryption.set(n.protocol,n)}),this.muxers=new Map,r.muxers.forEach(n=>{this.muxers.set(n.protocol,n)}),this.inboundUpgradeTimeout=r.inboundUpgradeTimeout??3e4,this.events=e.events}async shouldBlockConnection(e,r,n){let i=this.components.connectionGater[n];if(i!==void 0&&await i(e,r))throw new S(`The multiaddr connection is blocked by gater.${n}`,F.ERR_CONNECTION_INTERCEPTED)}async upgradeInbound(e,r){if(!await this.components.connectionManager.acceptIncomingConnection(e))throw new S("connection denied",F.ERR_CONNECTION_DENIED);let i,s,o,a,c,u=AbortSignal.timeout(this.inboundUpgradeTimeout),l=()=>{e.abort(new S("inbound upgrade timeout",_s))};u.addEventListener("abort",l,{once:!0}),ht(1/0,u);try{if(await this.components.connectionGater.denyInboundConnection?.(e)===!0)throw new S("The multiaddr connection is blocked by gater.acceptConnection",F.ERR_CONNECTION_INTERCEPTED);this.components.metrics?.trackMultiaddrConnection(e),e.log("starting the inbound connection upgrade");let f=e;if(r?.skipProtection!==!0){let d=this.components.connectionProtector;d!=null&&(e.log("protecting the inbound connection"),f=await d.protect(e))}try{if(i=f,r?.skipEncryption!==!0){({conn:i,remotePeer:s,protocol:c}=await this._encryptInbound(f));let d={...f,...i};await this.shouldBlockConnection(s,d,"denyInboundEncryptedConnection")}else{let d=e.remoteAddr.getPeerId();if(d==null)throw new S("inbound connection that skipped encryption must have a peer id",F.ERR_INVALID_MULTIADDR);let p=se(d);c="native",s=p}if(o=i,r?.muxerFactory!=null)a=r.muxerFactory;else if(this.muxers.size>0){let d=await this._multiplexInbound({...f,...i},this.muxers);a=d.muxerFactory,o=d.stream}}catch(d){throw e.log.error("failed to upgrade inbound connection",d),d}return await this.shouldBlockConnection(s,e,"denyInboundUpgradedConnection"),e.log("successfully upgraded inbound connection"),this._createConnection({cryptoProtocol:c,direction:"inbound",maConn:e,upgradedConn:o,muxerFactory:a,remotePeer:s,transient:r?.transient})}finally{u.removeEventListener("abort",l),this.components.connectionManager.afterUpgradeInbound()}}async upgradeOutbound(e,r){let n=e.remoteAddr.getPeerId(),i;n!=null&&(i=se(n),await this.shouldBlockConnection(i,e,"denyOutboundConnection"));let s,o,a,c,u;this.components.metrics?.trackMultiaddrConnection(e),e.log("starting the outbound connection upgrade");let l=e;if(r?.skipProtection!==!0){let f=this.components.connectionProtector;f!=null&&(l=await f.protect(e))}try{if(s=l,r?.skipEncryption!==!0){({conn:s,remotePeer:o,protocol:c}=await this._encryptOutbound(l,i));let f={...l,...s};await this.shouldBlockConnection(o,f,"denyOutboundEncryptedConnection")}else{if(i==null)throw new S("Encryption was skipped but no peer id was passed",F.ERR_INVALID_PEER);c="native",o=i}if(a=s,r?.muxerFactory!=null)u=r.muxerFactory;else if(this.muxers.size>0){let f=await this._multiplexOutbound({...l,...s},this.muxers);u=f.muxerFactory,a=f.stream}}catch(f){throw e.log.error("failed to upgrade outbound connection",f),await e.close(f),f}return await this.shouldBlockConnection(o,e,"denyOutboundUpgradedConnection"),e.log("successfully upgraded outbound connection"),this._createConnection({cryptoProtocol:c,direction:"outbound",maConn:e,upgradedConn:a,muxerFactory:u,remotePeer:o,transient:r?.transient})}_createConnection(e){let{cryptoProtocol:r,direction:n,maConn:i,upgradedConn:s,remotePeer:o,muxerFactory:a,transient:c}=e,u,l,f;a!=null&&(u=a.createStreamMuxer({direction:n,onIncomingStream:h=>{f!=null&&Promise.resolve().then(async()=>{let m=this.components.registrar.getProtocols(),{stream:g,protocol:y}=await ns(h,m,{log:h.log,yieldBytes:!1});if(f==null)return;f.log("incoming stream opened on %s",y);let x=Hv(y,this.components.registrar);if(ag(y,"inbound",f)===x){let I=new S(`Too many inbound protocol streams for protocol "${y}" - limit ${x}`,F.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS);throw h.abort(I),I}h.source=g.source,h.sink=g.sink,h.protocol=y,g.closeWrite!=null&&(h.closeWrite=g.closeWrite),g.closeRead!=null&&(h.closeRead=g.closeRead),g.close!=null&&(h.close=g.close),await this.components.peerStore.merge(o,{protocols:[y]}),this.components.metrics?.trackProtocolStream(h,f),this._onStream({connection:f,stream:h,protocol:y})}).catch(async m=>{f.log.error("error handling incoming stream id %s",h.id,m.message,m.code,m.stack),h.timeline.close==null&&await h.close()})}}),l=async(h,m={})=>{if(u==null)throw new S("Stream is not multiplexed",F.ERR_MUXER_UNAVAILABLE);f.log("starting new stream for protocols %s",h);let g=await u.newStream();f.log.trace("started new stream %s for protocols %s",g.id,h);try{if(m.signal==null){g.log("no abort signal was passed while trying to negotiate protocols %s falling back to default timeout",h);let v=AbortSignal.timeout(zv);ht(1/0,v),m={...m,signal:v}}g.log.trace("selecting protocol from protocols %s",h);let{stream:y,protocol:x}=await rs(g,h,{...m,log:g.log,yieldBytes:!0});g.log("selected protocol %s",x);let b=Gv(x,this.components.registrar,m);if(ag(x,"outbound",f)>=b){let v=new S(`Too many outbound protocol streams for protocol "${x}" - limit ${b}`,F.ERR_TOO_MANY_OUTBOUND_PROTOCOL_STREAMS);throw g.abort(v),v}return await this.components.peerStore.merge(o,{protocols:[x]}),g.source=y.source,g.sink=y.sink,g.protocol=x,y.closeWrite!=null&&(g.closeWrite=y.closeWrite),y.closeRead!=null&&(g.closeRead=y.closeRead),y.close!=null&&(g.close=y.close),this.components.metrics?.trackProtocolStream(g,f),g}catch(y){throw f.log.error("could not create new stream for protocols %s",h,y),g.timeline.close==null&&g.abort(y),y.code!=null?y:new S(String(y),F.ERR_UNSUPPORTED_PROTOCOL)}},Promise.all([u.sink(s.source),s.sink(u.source)]).catch(h=>{f.log.error("error piping data through muxer",h)}));let d=i.timeline;i.timeline=new Proxy(d,{set:(...h)=>(f!=null&&h[1]==="close"&&h[2]!=null&&d.close==null&&(async()=>{try{f.status==="open"&&await f.close()}catch(m){f.log.error("error closing connection after timeline close",m)}finally{this.events.safeDispatchEvent("connection:close",{detail:f})}})().catch(m=>{f.log.error("error thrown while dispatching connection:close event",m)}),Reflect.set(...h))}),i.timeline.upgraded=Date.now();let p=()=>{throw new S("connection is not multiplexed",F.ERR_CONNECTION_NOT_MULTIPLEXED)};return f=og({remoteAddr:i.remoteAddr,remotePeer:o,status:"open",direction:n,timeline:i.timeline,multiplexer:u?.protocol,encryption:r,transient:c,logger:this.components.logger,newStream:l??p,getStreams:()=>u!=null?u.streams:[],close:async h=>{u!=null&&(f.log.trace("close muxer"),await u.close(h)),f.log.trace("close maconn"),await i.close(h),f.log.trace("closed maconn")},abort:h=>{i.abort(h),u?.abort(h)}}),this.events.safeDispatchEvent("connection:open",{detail:f}),f}_onStream(e){let{connection:r,stream:n,protocol:i}=e,{handler:s,options:o}=this.components.registrar.getHandler(i);if(r.transient&&o.runOnTransientConnection!==!0)throw new S("Cannot open protocol stream on transient connection","ERR_TRANSIENT_CONNECTION");s({connection:r,stream:n})}async _encryptInbound(e){let r=Array.from(this.connectionEncryption.keys());e.log("handling inbound crypto protocol selection",r);try{let{stream:n,protocol:i}=await ns(e,r,{log:e.log}),s=this.connectionEncryption.get(i);if(s==null)throw new Error(`no crypto module found for ${i}`);return e.log("encrypting inbound connection using",i),{...await s.secureInbound(this.components.peerId,n),protocol:i}}catch(n){throw e.log.error("encrypting inbound connection to %p failed",n),new S(n.message,F.ERR_ENCRYPTION_FAILED)}}async _encryptOutbound(e,r){let n=Array.from(this.connectionEncryption.keys());e.log("selecting outbound crypto protocol",n);try{e.log.trace("selecting encrypter from %s",n);let{stream:i,protocol:s}=await rs(e,n,{log:e.log,yieldBytes:!0}),o=this.connectionEncryption.get(s);if(o==null)throw new Error(`no crypto module found for ${s}`);return e.log("encrypting outbound connection to %p using %p",r),{...await o.secureOutbound(this.components.peerId,i,r),protocol:s}}catch(i){throw e.log.error("encrypting outbound connection to %p failed",i),new S(i.message,F.ERR_ENCRYPTION_FAILED)}}async _multiplexOutbound(e,r){let n=Array.from(r.keys());e.log("outbound selecting muxer %s",n);try{e.log.trace("selecting stream muxer from %s",n);let{stream:i,protocol:s}=await rs(e,n,{log:e.log,yieldBytes:!0});e.log("selected %s as muxer protocol",s);let o=r.get(s);return{stream:i,muxerFactory:o}}catch(i){throw e.log.error("error multiplexing outbound connection",i),new S(String(i),F.ERR_MUXER_UNAVAILABLE)}}async _multiplexInbound(e,r){let n=Array.from(r.keys());e.log("inbound handling muxers %s",n);try{let{stream:i,protocol:s}=await ns(e,n,{log:e.log}),o=r.get(s);return{stream:i,muxerFactory:o}}catch(i){throw e.log.error("error multiplexing inbound connection",i),new S(String(i),F.ERR_MUXER_UNAVAILABLE)}}};var cg="1.0.12",ug="libp2p";var Hl=class extends wi{peerId;peerStore;contentRouting;peerRouting;metrics;services;logger;status;components;log;constructor(e){super(),this.status="stopped";let r=new wi,n=r.dispatchEvent.bind(r);r.dispatchEvent=a=>{let c=n(a),u=this.dispatchEvent(new $a(a.type,{detail:a.detail}));return c||u},ht(1/0,r),this.peerId=e.peerId,this.logger=e.logger??Qs(),this.log=this.logger.forComponent("libp2p"),this.services={};let i=this.components=hp({peerId:e.peerId,nodeInfo:e.nodeInfo??{name:ug,version:cg},logger:this.logger,events:r,datastore:e.datastore??new Eo,connectionGater:vp(e.connectionGater)});this.peerStore=this.configureComponent("peerStore",new bo(i,{addressFilter:this.components.connectionGater.filterMultiaddrForPeer,...e.peerStore})),e.metrics!=null&&(this.metrics=this.configureComponent("metrics",e.metrics(this.components))),i.events.addEventListener("peer:update",a=>{if(a.detail.previous==null){let c={id:a.detail.peer.id,multiaddrs:a.detail.peer.addresses.map(u=>u.multiaddr)};i.events.safeDispatchEvent("peer:discovery",{detail:c})}}),e.connectionProtector!=null&&this.configureComponent("connectionProtector",e.connectionProtector(i)),this.components.upgrader=new da(this.components,{connectionEncryption:(e.connectionEncryption??[]).map((a,c)=>this.configureComponent(`connection-encryption-${c}`,a(this.components))),muxers:(e.streamMuxers??[]).map((a,c)=>this.configureComponent(`stream-muxers-${c}`,a(this.components))),inboundUpgradeTimeout:e.connectionManager.inboundUpgradeTimeout}),this.configureComponent("transportManager",new na(this.components,e.transportManager)),this.configureComponent("connectionManager",new Zo(this.components,e.connectionManager)),this.configureComponent("registrar",new ra(this.components)),this.configureComponent("addressManager",new Ao(this.components,e.addresses));let s=(e.peerRouters??[]).map((a,c)=>this.configureComponent(`peer-router-${c}`,a(this.components)));this.peerRouting=this.components.peerRouting=this.configureComponent("peerRouting",new ta(this.components,{routers:s}));let o=(e.contentRouters??[]).map((a,c)=>this.configureComponent(`content-router-${c}`,a(this.components)));if(this.contentRouting=this.components.contentRouting=this.configureComponent("contentRouting",new ea(this.components,{routers:o})),(e.peerDiscovery??[]).forEach((a,c)=>{this.configureComponent(`peer-discovery-${c}`,a(this.components)).addEventListener("peer",l=>{this.#e(l)})}),e.transports?.forEach((a,c)=>{this.components.transportManager.add(this.configureComponent(`transport-${c}`,a(this.components)))}),e.services!=null)for(let a of Object.keys(e.services)){let c=e.services[a],u=c(this.components);if(u==null){this.log.error("service factory %s returned null or undefined instance",a);continue}this.services[a]=u,this.configureComponent(a,u),u[Ma]!=null&&(this.log("registering service %s for content routing",a),o.push(u[Ma])),u[Ka]!=null&&(this.log("registering service %s for peer routing",a),s.push(u[Ka])),u[Ua]!=null&&(this.log("registering service %s for peer discovery",a),u[Ua].addEventListener?.("peer",l=>{this.#e(l)}))}}configureComponent(e,r){return r==null&&this.log.error("component %s was null or undefined",e),this.components[e]=r,r}async start(){if(this.status==="stopped"){this.status="starting",this.log("libp2p is starting");try{await this.components.beforeStart?.(),await this.components.start(),await this.components.afterStart?.(),this.status="started",this.safeDispatchEvent("start",{detail:this}),this.log("libp2p has started")}catch(e){throw this.log.error("An error occurred starting libp2p",e),this.status="started",await this.stop(),e}}}async stop(){this.status==="started"&&(this.log("libp2p is stopping"),this.status="stopping",await this.components.beforeStop?.(),await this.components.stop(),await this.components.afterStop?.(),this.status="stopped",this.safeDispatchEvent("stop",{detail:this}),this.log("libp2p has stopped"))}getConnections(e){return this.components.connectionManager.getConnections(e)}getDialQueue(){return this.components.connectionManager.getDialQueue()}getPeers(){let e=new St;for(let r of this.components.connectionManager.getConnections())e.add(r.remotePeer);return Array.from(e)}async dial(e,r={}){return this.components.connectionManager.openConnection(e,{priority:75,...r})}async dialProtocol(e,r,n={}){if(r==null)throw new S("no protocols were provided to open a stream",F.ERR_INVALID_PROTOCOLS_FOR_STREAM);if(r=Array.isArray(r)?r:[r],r.length===0)throw new S("no protocols were provided to open a stream",F.ERR_INVALID_PROTOCOLS_FOR_STREAM);return(await this.dial(e,n)).newStream(r,n)}getMultiaddrs(){return this.components.addressManager.getAddresses()}getProtocols(){return this.components.registrar.getProtocols()}async hangUp(e,r={}){Pr(e)&&(e=se(e.getPeerId()??"")),await this.components.connectionManager.closeConnections(e,r)}async getPublicKey(e,r={}){if(this.log("getPublicKey %p",e),e.publicKey!=null)return e.publicKey;let n=await this.peerStore.get(e);if(n.id.publicKey!=null)return n.id.publicKey;let i=cp([So("/pk/"),e.multihash.digest]),s=await this.contentRouting.get(i,r);return Ui(s),await this.peerStore.patch(e,{publicKey:s}),s}async handle(e,r,n){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async i=>{await this.components.registrar.handle(i,r,n)}))}async unhandle(e){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async r=>{await this.components.registrar.unhandle(r)}))}async register(e,r){return this.components.registrar.register(e,r)}unregister(e){this.components.registrar.unregister(e)}#e(e){let{detail:r}=e;if(r.id.toString()===this.peerId.toString()){this.log.error(new Error(F.ERR_DISCOVERED_SELF));return}this.components.peerStore.merge(r.id,{multiaddrs:r.multiaddrs}).catch(n=>{this.log.error(n)})}};async function lg(t={}){return t.peerId??=await hd(),new Hl(Wp(t))}async function Yv(t={}){let e=await lg(t);return t.start!==!1&&await e.start(),e}return Sg(Qv);})();
|
|
62
62
|
/*! Bundled license information:
|
|
63
63
|
|
|
64
64
|
@noble/hashes/esm/utils.js:
|
package/dist/src/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,
|
|
1
|
+
{"version":3,"file":"version.d.ts","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,OAAO,WAAW,CAAA;AAC/B,eAAO,MAAM,IAAI,WAAW,CAAA"}
|
package/dist/src/version.js
CHANGED
package/dist/src/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,QAAQ,CAAA;AAC/B,MAAM,CAAC,MAAM,IAAI,GAAG,QAAQ,CAAA"}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
{
|
|
2
|
+
"Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.Libp2pInit.html",
|
|
3
|
+
".:Libp2pInit": "https://libp2p.github.io/js-libp2p/interfaces/libp2p.Libp2pInit.html",
|
|
4
|
+
"Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.Libp2pOptions.html",
|
|
5
|
+
".:Libp2pOptions": "https://libp2p.github.io/js-libp2p/types/libp2p.Libp2pOptions.html",
|
|
6
|
+
"ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.ServiceFactoryMap.html",
|
|
7
|
+
".:ServiceFactoryMap": "https://libp2p.github.io/js-libp2p/types/libp2p.ServiceFactoryMap.html",
|
|
8
|
+
"createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.createLibp2p.html",
|
|
9
|
+
".:createLibp2p": "https://libp2p.github.io/js-libp2p/functions/libp2p.createLibp2p.html"
|
|
10
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "libp2p",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.12",
|
|
4
4
|
"description": "JavaScript implementation of libp2p, a modular peer to peer network stack",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/libp2p/js-libp2p/tree/main/packages/libp2p#readme",
|
|
@@ -84,16 +84,16 @@
|
|
|
84
84
|
"test:webkit": "aegir test -t browser -f \"./dist/test/**/*.spec.js\" -- --browser webkit"
|
|
85
85
|
},
|
|
86
86
|
"dependencies": {
|
|
87
|
-
"@libp2p/crypto": "3.0.2
|
|
88
|
-
"@libp2p/interface": "1.0.2
|
|
89
|
-
"@libp2p/interface-internal": "1.0.3
|
|
90
|
-
"@libp2p/logger": "4.0.2
|
|
91
|
-
"@libp2p/multistream-select": "5.0.3
|
|
92
|
-
"@libp2p/peer-collections": "5.1.1
|
|
93
|
-
"@libp2p/peer-id": "4.0.2
|
|
94
|
-
"@libp2p/peer-id-factory": "4.0.1
|
|
95
|
-
"@libp2p/peer-store": "10.0.3
|
|
96
|
-
"@libp2p/utils": "5.1.0
|
|
87
|
+
"@libp2p/crypto": "^3.0.2",
|
|
88
|
+
"@libp2p/interface": "^1.0.2",
|
|
89
|
+
"@libp2p/interface-internal": "^1.0.3",
|
|
90
|
+
"@libp2p/logger": "^4.0.2",
|
|
91
|
+
"@libp2p/multistream-select": "^5.0.3",
|
|
92
|
+
"@libp2p/peer-collections": "^5.1.1",
|
|
93
|
+
"@libp2p/peer-id": "^4.0.2",
|
|
94
|
+
"@libp2p/peer-id-factory": "^4.0.1",
|
|
95
|
+
"@libp2p/peer-store": "^10.0.3",
|
|
96
|
+
"@libp2p/utils": "^5.1.0",
|
|
97
97
|
"@multiformats/mafmt": "^12.1.6",
|
|
98
98
|
"@multiformats/multiaddr": "^12.1.10",
|
|
99
99
|
"@multiformats/multiaddr-matcher": "^1.1.0",
|
|
@@ -119,13 +119,13 @@
|
|
|
119
119
|
},
|
|
120
120
|
"devDependencies": {
|
|
121
121
|
"@chainsafe/libp2p-yamux": "^6.0.1",
|
|
122
|
-
"@libp2p/circuit-relay-v2": "1.0.8
|
|
123
|
-
"@libp2p/identify": "1.0.7
|
|
124
|
-
"@libp2p/interface-compliance-tests": "5.0.8
|
|
125
|
-
"@libp2p/mplex": "10.0.8
|
|
126
|
-
"@libp2p/plaintext": "1.0.8
|
|
127
|
-
"@libp2p/tcp": "9.0.8
|
|
128
|
-
"@libp2p/websockets": "8.0.8
|
|
122
|
+
"@libp2p/circuit-relay-v2": "^1.0.8",
|
|
123
|
+
"@libp2p/identify": "^1.0.7",
|
|
124
|
+
"@libp2p/interface-compliance-tests": "^5.0.8",
|
|
125
|
+
"@libp2p/mplex": "^10.0.8",
|
|
126
|
+
"@libp2p/plaintext": "^1.0.8",
|
|
127
|
+
"@libp2p/tcp": "^9.0.8",
|
|
128
|
+
"@libp2p/websockets": "^8.0.8",
|
|
129
129
|
"aegir": "^41.0.2",
|
|
130
130
|
"it-pushable": "^3.2.1",
|
|
131
131
|
"p-event": "^6.0.0",
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '1.0.
|
|
1
|
+
export const version = '1.0.12'
|
|
2
2
|
export const name = 'libp2p'
|