libp2p 3.1.0-e2bdc7e63 → 3.1.1
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 +38 -38
- package/dist/index.min.js +1 -1
- package/dist/index.min.js.map +2 -2
- 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 +24 -0
- package/package.json +10 -10
- package/src/version.ts +1 -1
package/README.md
CHANGED
|
@@ -85,44 +85,44 @@ List of packages currently in existence for libp2p
|
|
|
85
85
|
|
|
86
86
|
> This table is generated using the module `package-table` with `package-table --data=package-list.json`.
|
|
87
87
|
|
|
88
|
-
| Package
|
|
89
|
-
|
|
|
90
|
-
| **libp2p**
|
|
91
|
-
| [`libp2p`](//github.com/libp2p/js-libp2p)
|
|
92
|
-
| [`@libp2p/interface`](//github.com/libp2p/js-libp2p/tree/main/packages/interface)
|
|
93
|
-
| **transports**
|
|
94
|
-
| [`@libp2p/memory`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-memory)
|
|
95
|
-
| [`@libp2p/tcp`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-tcp)
|
|
96
|
-
| [`@libp2p/webrtc`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-webrtc)
|
|
97
|
-
| [`@libp2p/websockets`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-websockets)
|
|
98
|
-
| [`@libp2p/webtransport`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-webtransport)
|
|
99
|
-
| **secure channels**
|
|
100
|
-
| [`@chainsafe/libp2p-noise`](//github.com/ChainSafe/js-libp2p-noise)
|
|
101
|
-
| [`@libp2p/plaintext`](//github.com/libp2p/js-libp2p/tree/main/packages/connection-encrypter-plaintext) | [
|
|
103
|
-
| **stream multiplexers**
|
|
104
|
-
| [`@chainsafe/libp2p-yamux`](//github.com/ChainSafe/js-libp2p-yamux)
|
|
105
|
-
| [`@libp2p/mplex`](//github.com/libp2p/js-libp2p/tree/main/packages/stream-multiplexer-mplex)
|
|
106
|
-
| **peer discovery**
|
|
107
|
-
| [`@libp2p/bootstrap`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap)
|
|
108
|
-
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht)
|
|
109
|
-
| [`@libp2p/mdns`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns)
|
|
110
|
-
| [`@chainsafe/discv5`](//github.com/ChainSafe/discv5)
|
|
111
|
-
| **content routing**
|
|
112
|
-
| [`@libp2p/http-v1-content-routing`](//github.com/libp2p/js-http-v1-content-routing)
|
|
113
|
-
| [`@libp2p/delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing)
|
|
114
|
-
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht)
|
|
115
|
-
| **peer routing**
|
|
116
|
-
| [`@libp2p/delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing)
|
|
117
|
-
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht)
|
|
118
|
-
| **utilities**
|
|
119
|
-
| [`@libp2p/crypto`](//github.com/libp2p/js-libp2p/tree/main/packages/crypto)
|
|
120
|
-
| **data types**
|
|
121
|
-
| [`@libp2p/peer-id`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-id)
|
|
122
|
-
| [`@libp2p/peer-record`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-record)
|
|
123
|
-
| **pubsub**
|
|
124
|
-
| [`@chainsafe/libp2p-gossipsub`](//github.com/ChainSafe/js-libp2p-gossipsub)
|
|
125
|
-
| [`@libp2p/floodsub`](//github.com/libp2p/js-libp2p/tree/main/packages/floodsub)
|
|
88
|
+
| Package | Version | Deps | CI | Coverage |
|
|
89
|
+
| ------------------------------------------------------------------------------------------------------ | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
90
|
+
| **libp2p** | | | | |
|
|
91
|
+
| [`libp2p`](//github.com/libp2p/js-libp2p) | [](//npmjs.com/package/libp2p) | [](//libraries.io/npm/libp2p) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p) |
|
|
92
|
+
| [`@libp2p/interface`](//github.com/libp2p/js-libp2p/tree/main/packages/interface) | [](//npmjs.com/package/@libp2p/interface) | [](//libraries.io/npm/%40libp2p%2Finterface) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/interface) |
|
|
93
|
+
| **transports** | | | | |
|
|
94
|
+
| [`@libp2p/memory`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-memory) | [](//npmjs.com/package/@libp2p/memory) | [](//libraries.io/npm/%40libp2p%2Fmemory) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/transport-memory) |
|
|
95
|
+
| [`@libp2p/tcp`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-tcp) | [](//npmjs.com/package/@libp2p/tcp) | [](//libraries.io/npm/%40libp2p%2Ftcp) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/transport-tcp) |
|
|
96
|
+
| [`@libp2p/webrtc`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-webrtc) | [](//npmjs.com/package/@libp2p/webrtc) | [](//libraries.io/npm/%40libp2p%2Fwebrtc) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/transport-webrtc) |
|
|
97
|
+
| [`@libp2p/websockets`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-websockets) | [](//npmjs.com/package/@libp2p/websockets) | [](//libraries.io/npm/%40libp2p%2Fwebsockets) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/transport-websockets) |
|
|
98
|
+
| [`@libp2p/webtransport`](//github.com/libp2p/js-libp2p/tree/main/packages/transport-webtransport) | [](//npmjs.com/package/@libp2p/webtransport) | [](//libraries.io/npm/%40libp2p%2Fwebtransport) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/transport-webtransport) |
|
|
99
|
+
| **secure channels** | | | | |
|
|
100
|
+
| [`@chainsafe/libp2p-noise`](//github.com/ChainSafe/js-libp2p-noise) | [](//npmjs.com/package/@chainsafe/libp2p-noise) | [](//libraries.io/npm/%40chainsafe%2Flibp2p-noise) | [](//github.com/ChainSafe/js-libp2p-noise/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/ChainSafe/js-libp2p-noise) |
|
|
101
|
+
| [`@libp2p/plaintext`](//github.com/libp2p/js-libp2p/tree/main/packages/connection-encrypter-plaintext) | [](//npmjs.com/package/@libp2p/plaintext) | [](//libraries.io/npm/%40libp2p%2Fplaintext) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/connection-encrypter-plaintext) |
|
|
102
|
+
| [`@libp2p/tls`](//github.com/libp2p/js-libp2p/tree/main/packages/connection-encrypter-tls) | [](//npmjs.com/package/@libp2p/tls) | [](//libraries.io/npm/%40libp2p%2Ftls) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/connection-encrypter-tls) |
|
|
103
|
+
| **stream multiplexers** | | | | |
|
|
104
|
+
| [`@chainsafe/libp2p-yamux`](//github.com/ChainSafe/js-libp2p-yamux) | [](//npmjs.com/package/@chainsafe/libp2p-yamux) | [](//libraries.io/npm/%40chainsafe%2Flibp2p-yamux) | [](//github.com/ChainSafe/js-libp2p-yamux/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/ChainSafe/js-libp2p-yamux) |
|
|
105
|
+
| [`@libp2p/mplex`](//github.com/libp2p/js-libp2p/tree/main/packages/stream-multiplexer-mplex) | [](//npmjs.com/package/@libp2p/mplex) | [](//libraries.io/npm/%40libp2p%2Fmplex) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/stream-multiplexer-mplex) |
|
|
106
|
+
| **peer discovery** | | | | |
|
|
107
|
+
| [`@libp2p/bootstrap`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap) | [](//npmjs.com/package/@libp2p/bootstrap) | [](//libraries.io/npm/%40libp2p%2Fbootstrap) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/peer-discovery-bootstrap) |
|
|
108
|
+
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht) | [](//npmjs.com/package/@libp2p/kad-dht) | [](//libraries.io/npm/%40libp2p%2Fkad-dht) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/kad-dht) |
|
|
109
|
+
| [`@libp2p/mdns`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns) | [](//npmjs.com/package/@libp2p/mdns) | [](//libraries.io/npm/%40libp2p%2Fmdns) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/peer-discovery-mdns) |
|
|
110
|
+
| [`@chainsafe/discv5`](//github.com/ChainSafe/discv5) | [](//npmjs.com/package/@chainsafe/discv5) | [](//libraries.io/npm/%40chainsafe%2Fdiscv5) | [](//github.com/ChainSafe/discv5/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/ChainSafe/discv5) |
|
|
111
|
+
| **content routing** | | | | |
|
|
112
|
+
| [`@libp2p/http-v1-content-routing`](//github.com/libp2p/js-http-v1-content-routing) | [](//npmjs.com/package/@libp2p/http-v1-content-routing) | [](//libraries.io/npm/%40libp2p%2Fhttp-v1-content-routing) | [](//github.com/libp2p/js-http-v1-content-routing/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-http-v1-content-routing) |
|
|
113
|
+
| [`@libp2p/delegated-content-routing`](//github.com/libp2p/js-libp2p-delegated-content-routing) | [](//npmjs.com/package/@libp2p/delegated-content-routing) | [](//libraries.io/npm/%40libp2p%2Fdelegated-content-routing) | [](//github.com/libp2p/js-libp2p-delegated-content-routing/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-content-routing) |
|
|
114
|
+
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht) | [](//npmjs.com/package/@libp2p/kad-dht) | [](//libraries.io/npm/%40libp2p%2Fkad-dht) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/kad-dht) |
|
|
115
|
+
| **peer routing** | | | | |
|
|
116
|
+
| [`@libp2p/delegated-peer-routing`](//github.com/libp2p/js-libp2p-delegated-peer-routing) | [](//npmjs.com/package/@libp2p/delegated-peer-routing) | [](//libraries.io/npm/%40libp2p%2Fdelegated-peer-routing) | [](//github.com/libp2p/js-libp2p-delegated-peer-routing/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p-delegated-peer-routing) |
|
|
117
|
+
| [`@libp2p/kad-dht`](//github.com/libp2p/js-libp2p/tree/main/packages/kad-dht) | [](//npmjs.com/package/@libp2p/kad-dht) | [](//libraries.io/npm/%40libp2p%2Fkad-dht) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/kad-dht) |
|
|
118
|
+
| **utilities** | | | | |
|
|
119
|
+
| [`@libp2p/crypto`](//github.com/libp2p/js-libp2p/tree/main/packages/crypto) | [](//npmjs.com/package/@libp2p/crypto) | [](//libraries.io/npm/%40libp2p%2Fcrypto) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/crypto) |
|
|
120
|
+
| **data types** | | | | |
|
|
121
|
+
| [`@libp2p/peer-id`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-id) | [](//npmjs.com/package/@libp2p/peer-id) | [](//libraries.io/npm/%40libp2p%2Fpeer-id) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/peer-id) |
|
|
122
|
+
| [`@libp2p/peer-record`](//github.com/libp2p/js-libp2p/tree/main/packages/peer-record) | [](//npmjs.com/package/@libp2p/peer-record) | [](//libraries.io/npm/%40libp2p%2Fpeer-record) | [](//github.com/libp2p/js-libp2p/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/peer-record) |
|
|
123
|
+
| **pubsub** | | | | |
|
|
124
|
+
| [`@chainsafe/libp2p-gossipsub`](//github.com/ChainSafe/js-libp2p-gossipsub) | [](//npmjs.com/package/@chainsafe/libp2p-gossipsub) | [](//libraries.io/npm/%40chainsafe%2Flibp2p-gossipsub) | [](//github.com/ChainSafe/js-libp2p-gossipsub/actions?query=branch%3Amaster+workflow%3Aci+) | [](https://codecov.io/gh/ChainSafe/js-libp2p-gossipsub) |
|
|
125
|
+
| [`@libp2p/floodsub`](//github.com/libp2p/js-libp2p/tree/main/packages/floodsub) | [](//github.com/libp2p/js-libp2p/tree/main/packages/floodsub/releases) | [](//libraries.io/npm/%40libp2p%2Ffloodsub) | [](//github.com/libp2p/js-libp2p/tree/main/packages/floodsub/actions?query=branch%3Amain+workflow%3Aci+) | [](https://codecov.io/gh/libp2p/js-libp2p/tree/main/packages/floodsub) |
|
|
126
126
|
|
|
127
127
|
# Used by
|
|
128
128
|
|
package/dist/index.min.js
CHANGED
|
@@ -23,7 +23,7 @@ ${[...t.errors.entries()].map(([s,i])=>`
|
|
|
23
23
|
`),t),n.trace('responded with "%s" for "%s"',s,s),o.unwrap(),s;if(s==="ls"){let i=new Q(...e.map(a=>Ks.single(L(`${a}
|
|
24
24
|
`))),L(`
|
|
25
25
|
`));n.trace('respond with "%s" for %s',e,s),await o.write(i,t),n.trace('responded with "%s" for %s',e,s);continue}n.trace('respond with "na" for "%s"',s),await o.write(L(`na
|
|
26
|
-
`),t),n('responded with "na" for "%s"',s)}}var ku=class extends Le{id;remoteAddr;remotePeer;direction;timeline;direct;multiplexer;encryption;limits;log;maConn;muxer;components;outboundStreamProtocolNegotiationTimeout;inboundStreamProtocolNegotiationTimeout;closeTimeout;constructor(e,t){super(),this.components=e,this.id=t.id,this.remoteAddr=t.maConn.remoteAddr,this.remotePeer=t.remotePeer,this.direction=t.direction??"outbound",this.timeline=t.maConn.timeline,this.encryption=t.cryptoProtocol,this.limits=t.limits,this.maConn=t.maConn,this.log=t.maConn.log,this.outboundStreamProtocolNegotiationTimeout=t.outboundStreamProtocolNegotiationTimeout??1e4,this.inboundStreamProtocolNegotiationTimeout=t.inboundStreamProtocolNegotiationTimeout??1e4,this.closeTimeout=t.closeTimeout??1e3,this.direct=uu(t.maConn.remoteAddr),this.onIncomingStream=this.onIncomingStream.bind(this),this.remoteAddr.getComponents().find(n=>n.code===421)==null&&(this.remoteAddr=this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)),t.muxer!=null&&(this.multiplexer=t.muxer.protocol,this.muxer=t.muxer,this.muxer.addEventListener("stream",this.onIncomingStream)),this.maConn.addEventListener("close",n=>{this.dispatchEvent(new Fo(n.local,n.error))})}[Symbol.toStringTag]="Connection";[Bu]=!0;get streams(){return this.muxer?.streams??[]}get status(){return this.maConn.status}newStream=async(e,t={})=>{if(this.muxer==null)throw new Vt("Connection is not multiplexed");if(this.muxer.status!=="open")throw new zt(`The connection muxer is "${this.muxer.status}" and not "open"`);if(this.maConn.status!=="open")throw new zt(`The connection is "${this.status}" and not "open"`);if(this.limits!=null&&t?.runOnLimitedConnection!==!0)throw new bn("Cannot open protocol stream on limited connection");Array.isArray(e)||(e=[e]),this.log.trace("starting new stream for protocols %s",e);let n=await this.muxer.createStream({...t,protocol:e.length===1?e[0]:void 0});this.log.trace("started new stream %s for protocols %s",n.id,e);try{if(t.signal==null){n.log("no abort signal was passed while trying to negotiate protocols %s falling back to default timeout",e);let a=AbortSignal.timeout(this.outboundStreamProtocolNegotiationTimeout);t={...t,signal:a}}n.protocol===""?(n.log.trace("selecting protocol from protocols %s",e),n.protocol=await mn(n,e,t),n.log("negotiated protocol %s",n.protocol)):n.log("pre-negotiated protocol %s",n.protocol);let o=Jb(n.protocol,this.components.registrar,t),s=Op(n.protocol,"outbound",this);if(s>o){let a=new Bo(`Too many outbound protocol streams for protocol "${n.protocol}" - ${s}/${o}`);throw n.abort(a),a}await this.components.peerStore.merge(this.remotePeer,{protocols:[n.protocol]}),this.components.metrics?.trackProtocolStream(n);let i=this.components.registrar.getMiddleware(n.protocol);return await this.runMiddlewareChain(n,this,i)}catch(o){throw n.status==="open"?n.abort(o):this.log.error("could not create new outbound stream on connection %s %a for protocols %s - %e",this.direction==="inbound"?"from":"to",this.remoteAddr,e,o),o}};async onIncomingStream(e){let t=e.detail,n=AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout);t.log("start protocol negotiation, timing out after %dms",this.inboundStreamProtocolNegotiationTimeout);try{if(t.protocol===""){let l=this.components.registrar.getProtocols();t.log.trace("selecting protocol from protocols %s",l),t.protocol=await gn(t,l,{signal:n}),t.log("negotiated protocol %s",t.protocol)}else t.log("pre-negotiated protocol %s",t.protocol);let o=Qb(t.protocol,this.components.registrar);if(Op(t.protocol,"inbound",this)>o)throw new Mo(`Too many inbound protocol streams for protocol "${t.protocol}" - limit ${o}`);await this.components.peerStore.merge(this.remotePeer,{protocols:[t.protocol]},{signal:n}),this.components.metrics?.trackProtocolStream(t);let{handler:i,options:a}=this.components.registrar.getHandler(t.protocol);if(this.limits!=null&&a.runOnLimitedConnection!==!0)throw new bn("Cannot open protocol stream on limited connection");let c=this.components.registrar.getMiddleware(t.protocol);c.push(async(l,f,u)=>{await i(l,f),u(l,f)}),await this.runMiddlewareChain(t,this,c)}catch(o){t.abort(o)}}async runMiddlewareChain(e,t,n){for(let o=0;o<n.length;o++){let s=n[o];e.log.trace("running middleware",o,s),await new Promise((i,a)=>{try{let c=s(e,t,(l,f)=>{e=l,t=f,i()});c instanceof Promise&&c.catch(a)}catch(c){a(c)}}),e.log.trace("ran middleware",o,s)}return e}async close(e={}){if(this.log("closing connection to %a",this.remoteAddr),e.signal==null){let t=AbortSignal.timeout(this.closeTimeout);e={...e,signal:t}}await this.muxer?.close(e),await this.maConn.close(e)}abort(e){this.muxer?.abort(e),this.maConn.abort(e)}};function Np(r,e){return new ku(r,e)}function Qb(r,e){try{let{options:t}=e.getHandler(r);if(t.maxInboundStreams!=null)return t.maxInboundStreams}catch(t){if(t.name!=="UnhandledProtocolError")throw t}return Pu}function Jb(r,e,t={}){try{let{options:n}=e.getHandler(r);if(n.maxOutboundStreams!=null)return n.maxOutboundStreams}catch(n){if(n.name!=="UnhandledProtocolError")throw n}return t.maxOutboundStreams??Du}function Op(r,e,t){let n=0;return t.streams.forEach(o=>{o.direction===e&&o.protocol===r&&n++}),n}var sa=class{components;connectionEncrypters;streamMuxers;inboundUpgradeTimeout;inboundStreamProtocolNegotiationTimeout;outboundStreamProtocolNegotiationTimeout;events;metrics;connectionCloseTimeout;constructor(e,t){this.components=e,this.connectionEncrypters=Ie({name:"libp2p_upgrader_connection_encrypters",metrics:this.components.metrics}),t.connectionEncrypters.forEach(n=>{this.connectionEncrypters.set(n.protocol,n)}),this.streamMuxers=Ie({name:"libp2p_upgrader_stream_multiplexers",metrics:this.components.metrics}),t.streamMuxers.forEach(n=>{this.streamMuxers.set(n.protocol,n)}),this.inboundUpgradeTimeout=t.inboundUpgradeTimeout??1e4,this.inboundStreamProtocolNegotiationTimeout=t.inboundStreamProtocolNegotiationTimeout??1e4,this.outboundStreamProtocolNegotiationTimeout=t.outboundStreamProtocolNegotiationTimeout??1e4,this.connectionCloseTimeout=t.connectionCloseTimeout??1e3,this.events=e.events,this.metrics={dials:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_total"),errors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dial_errors_total"),inboundErrors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_inbound_errors_total"),outboundErrors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_outbound_errors_total")}}[Symbol.toStringTag]="@libp2p/upgrader";async shouldBlockConnection(e,...t){let n=this.components.connectionGater[e];if(n==null)return;if(await n.apply(this.components.connectionGater,t)===!0)throw new Mi(`The multiaddr connection is blocked by gater.${e}`)}createInboundAbortSignal(e){let t=vt([AbortSignal.timeout(this.inboundUpgradeTimeout),e]);return t}async upgradeInbound(e,t){let n=!1,o=this.createInboundAbortSignal(t.signal);try{if(this.metrics.dials?.increment({inbound:!0}),n=this.components.connectionManager.acceptIncomingConnection(e),!n)throw new Bi("Connection denied");await Et(this.shouldBlockConnection("denyInboundConnection",e),o),await this._performUpgrade(e,"inbound",{...t,signal:o})}catch(s){throw this.metrics.errors?.increment({inbound:!0}),this.metrics.inboundErrors?.increment({[s.name??"Error"]:!0}),s}finally{o.clear(),n&&this.components.connectionManager.afterUpgradeInbound()}}async upgradeOutbound(e,t){try{this.metrics.dials?.increment({outbound:!0});let n=e.remoteAddr.getComponents().findLast(i=>i.code===421)?.value,o;n!=null&&(o=at(n),await Et(this.shouldBlockConnection("denyOutboundConnection",o,e),t.signal));let s="outbound";return t.initiator===!1&&(s="inbound"),await this._performUpgrade(e,s,t)}catch(n){throw this.metrics.errors?.increment({outbound:!0}),this.metrics.outboundErrors?.increment({[n.name??"Error"]:!0}),n}}async _performUpgrade(e,t,n){let o=e,s,i,a,c,l=`${parseInt(String(Math.random()*1e9)).toString(36)}${Date.now()}`;if(e.log=e.log.newScope(`${t}:${l}`),this.components.metrics?.trackMultiaddrConnection(e),e.log.trace("starting the %s connection upgrade",t),n?.skipProtection!==!0){let u=this.components.connectionProtector;u!=null&&(e.log("protecting the %s connection",t),o=await u.protect(o,n))}try{if(ew(n)){if(n.remotePeer==null)throw new Ct(`${t} connection that skipped encryption must have a peer id`);c="native",s=n.remotePeer}else{let u=e.remoteAddr.getComponents().findLast(h=>h.code===421)?.value,d;u!=null&&(d=at(u)),n?.onProgress?.(new de(`upgrader:encrypt-${t}-connection`)),{connection:o,remotePeer:s,protocol:c,streamMuxer:i}=await(t==="inbound"?this._encryptInbound(o,{...n,remotePeer:d}):this._encryptOutbound(o,{...n,remotePeer:d}))}if(s.equals(this.components.peerId)){let u=new Ir("Can not dial self");throw e.abort(u),u}await this.shouldBlockConnection(t==="inbound"?"denyInboundEncryptedConnection":"denyOutboundEncryptedConnection",s,e),n?.muxerFactory!=null?i=n.muxerFactory:i==null&&this.streamMuxers.size>0&&(n?.onProgress?.(new de(`upgrader:multiplex-${t}-connection`)),i=await(t==="inbound"?this._multiplexInbound(o,this.streamMuxers,n):this._multiplexOutbound(o,this.streamMuxers,n)))}catch(u){throw e.log.error("failed to upgrade %s connection %s %a - %e",t,t==="inbound"?"from":"to",e.remoteAddr,u),u}i!=null&&(e.log("create muxer %s",i.protocol),a=i.createStreamMuxer(o)),await this.shouldBlockConnection(t==="inbound"?"denyInboundUpgradedConnection":"denyOutboundUpgradedConnection",s,e);let f=this._createConnection({id:l,cryptoProtocol:c,direction:t,maConn:e,stream:o,muxer:a,remotePeer:s,limits:n?.limits,closeTimeout:this.connectionCloseTimeout});return f.log("successfully upgraded connection"),f}_createConnection(e){let t=Np(this.components,{...e,outboundStreamProtocolNegotiationTimeout:this.outboundStreamProtocolNegotiationTimeout,inboundStreamProtocolNegotiationTimeout:this.inboundStreamProtocolNegotiationTimeout});return t.addEventListener("close",()=>{this.events.safeDispatchEvent("connection:close",{detail:t})}),this.events.safeDispatchEvent("connection:open",{detail:t}),t}async _encryptInbound(e,t){let n=Array.from(this.connectionEncrypters.keys());try{let o=await gn(e,n,t),s=this.connectionEncrypters.get(o);if(s==null)throw new Sr(`no crypto module found for ${o}`);return e.log("encrypting inbound connection using %s",o),{...await s.secureInbound(e,t),protocol:o}}catch(o){throw new Sr(o.message)}}async _encryptOutbound(e,t){let n=Array.from(this.connectionEncrypters.keys());try{e.log.trace("selecting encrypter from %s",n);let o=await mn(e,n,t),s=this.connectionEncrypters.get(o);if(s==null)throw new Sr(`no crypto module found for ${o}`);return e.log("encrypting outbound connection using %s",o),{...await s.secureOutbound(e,t),protocol:o}}catch(o){throw new Sr(o.message)}}async _multiplexOutbound(e,t,n){let o=Array.from(t.keys());e.log("outbound selecting muxer %s",o);try{e.log.trace("selecting stream muxer from %s",o);let s=await mn(e,o,n),i=t.get(s);if(i==null)throw new Vt(`No muxer configured for protocol "${s}"`);return e.log("selected %s as muxer protocol",s),i}catch(s){throw e.log.error("error multiplexing outbound connection - %e",s),new Vt(String(s))}}async _multiplexInbound(e,t,n){let o=Array.from(t.keys());e.log("inbound handling muxers %s",o);try{e.log.trace("selecting stream muxer from %s",o);let s=await gn(e,o,n),i=t.get(s);if(i==null)throw new Vt(`No muxer configured for protocol "${s}"`);return e.log("selected %s as muxer protocol",s),i}catch(s){throw e.log.error("error multiplexing inbound connection - %e",s),s}}getConnectionEncrypters(){return this.connectionEncrypters}getStreamMuxers(){return this.streamMuxers}};function ew(r){return r.skipEncryption===!0}var ia="3.1.0-e2bdc7e63",aa="js-libp2p";function Bp(r,e){return`${r??aa}/${e??ia} browser/${globalThis.navigator.userAgent}`}var To=class extends Le{peerId;peerStore;contentRouting;peerRouting;metrics;services;logger;status;components;log;constructor(e){super(),this.status="stopped";let t=new Le,n=t.dispatchEvent.bind(t);t.dispatchEvent=l=>{let f=n(l),u=this.dispatchEvent(new CustomEvent(l.type,{detail:l.detail}));return f||u},this.peerId=e.peerId,this.logger=e.logger??ds(),this.log=this.logger.forComponent("libp2p"),this.services={};let o=e.nodeInfo?.name??aa,s=e.nodeInfo?.version??ia,i=this.components=fp({peerId:e.peerId,privateKey:e.privateKey,nodeInfo:{name:o,version:s,userAgent:e.nodeInfo?.userAgent??Bp(o,s)},logger:this.logger,events:t,datastore:e.datastore??new yi,connectionGater:hp(e.connectionGater),dns:e.dns});e.metrics!=null&&(this.metrics=this.configureComponent("metrics",e.metrics(this.components))),this.peerStore=this.configureComponent("peerStore",Zh(i,{addressFilter:this.components.connectionGater.filterMultiaddrForPeer,...e.peerStore})),i.events.addEventListener("peer:update",l=>{if(l.detail.previous==null){let f={id:l.detail.peer.id,multiaddrs:l.detail.peer.addresses.map(u=>u.multiaddr)};i.events.safeDispatchEvent("peer:discovery",{detail:f})}}),e.connectionProtector!=null&&this.configureComponent("connectionProtector",e.connectionProtector(i)),this.components.upgrader=new sa(this.components,{connectionEncrypters:(e.connectionEncrypters??[]).map((l,f)=>this.configureComponent(`connection-encryption-${f}`,l(this.components))),streamMuxers:(e.streamMuxers??[]).map((l,f)=>this.configureComponent(`stream-muxers-${f}`,l(this.components))),inboundUpgradeTimeout:e.connectionManager?.inboundUpgradeTimeout,inboundStreamProtocolNegotiationTimeout:e.connectionManager?.inboundStreamProtocolNegotiationTimeout,outboundStreamProtocolNegotiationTimeout:e.connectionManager?.outboundStreamProtocolNegotiationTimeout,connectionCloseTimeout:e.connectionManager?.connectionCloseTimeout}),this.configureComponent("transportManager",new oa(this.components,e.transportManager)),this.configureComponent("connectionManager",new ji(this.components,e.connectionManager)),e.connectionMonitor?.enabled!==!1&&this.configureComponent("connectionMonitor",new Qi(this.components,e.connectionMonitor)),this.configureComponent("registrar",new na(this.components)),this.configureComponent("addressManager",new Ci(this.components,e.addresses));let a=(e.peerRouters??[]).map((l,f)=>this.configureComponent(`peer-router-${f}`,l(this.components)));this.peerRouting=this.components.peerRouting=this.configureComponent("peerRouting",new ta(this.components,{routers:a}));let c=(e.contentRouters??[]).map((l,f)=>this.configureComponent(`content-router-${f}`,l(this.components)));if(this.contentRouting=this.components.contentRouting=this.configureComponent("contentRouting",new Ji(this.components,{routers:c})),this.configureComponent("randomWalk",new ra(this.components)),(e.peerDiscovery??[]).forEach((l,f)=>{this.configureComponent(`peer-discovery-${f}`,l(this.components)).addEventListener("peer",d=>{this.#e(d)})}),e.transports?.forEach((l,f)=>{this.components.transportManager.add(this.configureComponent(`transport-${f}`,l(this.components)))}),e.services!=null)for(let l of Object.keys(e.services)){let f=e.services[l],u=f(this.components);if(u==null){this.log.error("service factory %s returned null or undefined instance",l);continue}this.services[l]=u,this.configureComponent(l,u),u[ua]!=null&&(this.log("registering service %s for content routing",l),c.push(u[ua])),u[da]!=null&&(this.log("registering service %s for peer routing",l),a.push(u[da])),u[fa]!=null&&(this.log("registering service %s for peer discovery",l),u[fa].addEventListener?.("peer",d=>{this.#e(d)}))}dp(i)}configureComponent(e,t){return t==null&&this.log.error("component %s was null or undefined",e),this.components[e]=t,t}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 with peer id %p",this.peerId)}catch(e){throw this.log.error("an error occurred starting libp2p - %e",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 ir;for(let t of this.components.connectionManager.getConnections())e.add(t.remotePeer);return Array.from(e)}async dial(e,t={}){return this.components.connectionManager.openConnection(e,{priority:75,...t})}async dialProtocol(e,t,n={}){if(t==null)throw new C("no protocols were provided to open a stream");if(t=Array.isArray(t)?t:[t],t.length===0)throw new C("no protocols were provided to open a stream");return this.components.connectionManager.openStream(e,t,n)}getMultiaddrs(){return this.components.addressManager.getAddresses()}getProtocols(){return this.components.registrar.getProtocols()}async hangUp(e,t={}){$t(e)&&(e=at(e.getComponents().findLast(n=>n.code===421)?.value??"")),await this.components.connectionManager.closeConnections(e,t)}async getPublicKey(e,t={}){if(this.log("getPublicKey %p",e),e.publicKey!=null)return e.publicKey;try{let i=await this.peerStore.get(e,t);if(i.id.publicKey!=null)return i.id.publicKey}catch(i){if(i.name!=="NotFoundError")throw i}let n=Xe([L("/pk/"),e.toMultihash().bytes]),o=await this.contentRouting.get(n,t),s=Gr(o);return await this.peerStore.patch(e,{publicKey:s},t),s}async handle(e,t,n){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async o=>{await this.components.registrar.handle(o,t,n)}))}async unhandle(e,t){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async n=>{await this.components.registrar.unhandle(n,t)}))}async register(e,t,n){return this.components.registrar.register(e,t,n)}unregister(e){this.components.registrar.unregister(e)}use(e,t){this.components.registrar.use(e,Array.isArray(t)?t:[t])}unuse(e){this.components.registrar.unuse(e)}async isDialable(e,t={}){return this.components.connectionManager.isDialable(e,t)}#e(e){let{detail:t}=e;if(t.id.toString()===this.peerId.toString()){this.log.error("peer discovery mechanism discovered self");return}this.components.peerStore.merge(t.id,{multiaddrs:t.multiaddrs}).catch(n=>{this.log.error("could not update multiaddrs of discovered peer - %e",n)})}};async function tw(r={}){r.privateKey??=await qd("Ed25519");let e=new To({...await Wd(r),peerId:Hd(r.privateKey)});return r.start!==!1&&await e.start(),e}var rw=["dial","dialProtocol","hangUp","handle","unhandle","getMultiaddrs","getProtocols"];function nw(r){return r==null?!1:r instanceof To?!0:rw.every(e=>typeof r[e]=="function")}return Vp(ow);})();
|
|
26
|
+
`),t),n('responded with "na" for "%s"',s)}}var ku=class extends Le{id;remoteAddr;remotePeer;direction;timeline;direct;multiplexer;encryption;limits;log;maConn;muxer;components;outboundStreamProtocolNegotiationTimeout;inboundStreamProtocolNegotiationTimeout;closeTimeout;constructor(e,t){super(),this.components=e,this.id=t.id,this.remoteAddr=t.maConn.remoteAddr,this.remotePeer=t.remotePeer,this.direction=t.direction??"outbound",this.timeline=t.maConn.timeline,this.encryption=t.cryptoProtocol,this.limits=t.limits,this.maConn=t.maConn,this.log=t.maConn.log,this.outboundStreamProtocolNegotiationTimeout=t.outboundStreamProtocolNegotiationTimeout??1e4,this.inboundStreamProtocolNegotiationTimeout=t.inboundStreamProtocolNegotiationTimeout??1e4,this.closeTimeout=t.closeTimeout??1e3,this.direct=uu(t.maConn.remoteAddr),this.onIncomingStream=this.onIncomingStream.bind(this),this.remoteAddr.getComponents().find(n=>n.code===421)==null&&(this.remoteAddr=this.remoteAddr.encapsulate(`/p2p/${this.remotePeer}`)),t.muxer!=null&&(this.multiplexer=t.muxer.protocol,this.muxer=t.muxer,this.muxer.addEventListener("stream",this.onIncomingStream)),this.maConn.addEventListener("close",n=>{this.dispatchEvent(new Fo(n.local,n.error))})}[Symbol.toStringTag]="Connection";[Bu]=!0;get streams(){return this.muxer?.streams??[]}get status(){return this.maConn.status}newStream=async(e,t={})=>{if(this.muxer==null)throw new Vt("Connection is not multiplexed");if(this.muxer.status!=="open")throw new zt(`The connection muxer is "${this.muxer.status}" and not "open"`);if(this.maConn.status!=="open")throw new zt(`The connection is "${this.status}" and not "open"`);if(this.limits!=null&&t?.runOnLimitedConnection!==!0)throw new bn("Cannot open protocol stream on limited connection");Array.isArray(e)||(e=[e]),this.log.trace("starting new stream for protocols %s",e);let n=await this.muxer.createStream({...t,protocol:e.length===1?e[0]:void 0});this.log.trace("started new stream %s for protocols %s",n.id,e);try{if(t.signal==null){n.log("no abort signal was passed while trying to negotiate protocols %s falling back to default timeout",e);let a=AbortSignal.timeout(this.outboundStreamProtocolNegotiationTimeout);t={...t,signal:a}}n.protocol===""?(n.log.trace("selecting protocol from protocols %s",e),n.protocol=await mn(n,e,t),n.log("negotiated protocol %s",n.protocol)):n.log("pre-negotiated protocol %s",n.protocol);let o=Jb(n.protocol,this.components.registrar,t),s=Op(n.protocol,"outbound",this);if(s>o){let a=new Bo(`Too many outbound protocol streams for protocol "${n.protocol}" - ${s}/${o}`);throw n.abort(a),a}await this.components.peerStore.merge(this.remotePeer,{protocols:[n.protocol]}),this.components.metrics?.trackProtocolStream(n);let i=this.components.registrar.getMiddleware(n.protocol);return await this.runMiddlewareChain(n,this,i)}catch(o){throw n.status==="open"?n.abort(o):this.log.error("could not create new outbound stream on connection %s %a for protocols %s - %e",this.direction==="inbound"?"from":"to",this.remoteAddr,e,o),o}};async onIncomingStream(e){let t=e.detail,n=AbortSignal.timeout(this.inboundStreamProtocolNegotiationTimeout);t.log("start protocol negotiation, timing out after %dms",this.inboundStreamProtocolNegotiationTimeout);try{if(t.protocol===""){let l=this.components.registrar.getProtocols();t.log.trace("selecting protocol from protocols %s",l),t.protocol=await gn(t,l,{signal:n}),t.log("negotiated protocol %s",t.protocol)}else t.log("pre-negotiated protocol %s",t.protocol);let o=Qb(t.protocol,this.components.registrar);if(Op(t.protocol,"inbound",this)>o)throw new Mo(`Too many inbound protocol streams for protocol "${t.protocol}" - limit ${o}`);await this.components.peerStore.merge(this.remotePeer,{protocols:[t.protocol]},{signal:n}),this.components.metrics?.trackProtocolStream(t);let{handler:i,options:a}=this.components.registrar.getHandler(t.protocol);if(this.limits!=null&&a.runOnLimitedConnection!==!0)throw new bn("Cannot open protocol stream on limited connection");let c=this.components.registrar.getMiddleware(t.protocol);c.push(async(l,f,u)=>{await i(l,f),u(l,f)}),await this.runMiddlewareChain(t,this,c)}catch(o){t.abort(o)}}async runMiddlewareChain(e,t,n){for(let o=0;o<n.length;o++){let s=n[o];e.log.trace("running middleware",o,s),await new Promise((i,a)=>{try{let c=s(e,t,(l,f)=>{e=l,t=f,i()});c instanceof Promise&&c.catch(a)}catch(c){a(c)}}),e.log.trace("ran middleware",o,s)}return e}async close(e={}){if(this.log("closing connection to %a",this.remoteAddr),e.signal==null){let t=AbortSignal.timeout(this.closeTimeout);e={...e,signal:t}}await this.muxer?.close(e),await this.maConn.close(e)}abort(e){this.muxer?.abort(e),this.maConn.abort(e)}};function Np(r,e){return new ku(r,e)}function Qb(r,e){try{let{options:t}=e.getHandler(r);if(t.maxInboundStreams!=null)return t.maxInboundStreams}catch(t){if(t.name!=="UnhandledProtocolError")throw t}return Pu}function Jb(r,e,t={}){try{let{options:n}=e.getHandler(r);if(n.maxOutboundStreams!=null)return n.maxOutboundStreams}catch(n){if(n.name!=="UnhandledProtocolError")throw n}return t.maxOutboundStreams??Du}function Op(r,e,t){let n=0;return t.streams.forEach(o=>{o.direction===e&&o.protocol===r&&n++}),n}var sa=class{components;connectionEncrypters;streamMuxers;inboundUpgradeTimeout;inboundStreamProtocolNegotiationTimeout;outboundStreamProtocolNegotiationTimeout;events;metrics;connectionCloseTimeout;constructor(e,t){this.components=e,this.connectionEncrypters=Ie({name:"libp2p_upgrader_connection_encrypters",metrics:this.components.metrics}),t.connectionEncrypters.forEach(n=>{this.connectionEncrypters.set(n.protocol,n)}),this.streamMuxers=Ie({name:"libp2p_upgrader_stream_multiplexers",metrics:this.components.metrics}),t.streamMuxers.forEach(n=>{this.streamMuxers.set(n.protocol,n)}),this.inboundUpgradeTimeout=t.inboundUpgradeTimeout??1e4,this.inboundStreamProtocolNegotiationTimeout=t.inboundStreamProtocolNegotiationTimeout??1e4,this.outboundStreamProtocolNegotiationTimeout=t.outboundStreamProtocolNegotiationTimeout??1e4,this.connectionCloseTimeout=t.connectionCloseTimeout??1e3,this.events=e.events,this.metrics={dials:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_total"),errors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dial_errors_total"),inboundErrors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_inbound_errors_total"),outboundErrors:e.metrics?.registerCounterGroup("libp2p_connection_manager_dials_outbound_errors_total")}}[Symbol.toStringTag]="@libp2p/upgrader";async shouldBlockConnection(e,...t){let n=this.components.connectionGater[e];if(n==null)return;if(await n.apply(this.components.connectionGater,t)===!0)throw new Mi(`The multiaddr connection is blocked by gater.${e}`)}createInboundAbortSignal(e){let t=vt([AbortSignal.timeout(this.inboundUpgradeTimeout),e]);return t}async upgradeInbound(e,t){let n=!1,o=this.createInboundAbortSignal(t.signal);try{if(this.metrics.dials?.increment({inbound:!0}),n=this.components.connectionManager.acceptIncomingConnection(e),!n)throw new Bi("Connection denied");await Et(this.shouldBlockConnection("denyInboundConnection",e),o),await this._performUpgrade(e,"inbound",{...t,signal:o})}catch(s){throw this.metrics.errors?.increment({inbound:!0}),this.metrics.inboundErrors?.increment({[s.name??"Error"]:!0}),s}finally{o.clear(),n&&this.components.connectionManager.afterUpgradeInbound()}}async upgradeOutbound(e,t){try{this.metrics.dials?.increment({outbound:!0});let n=e.remoteAddr.getComponents().findLast(i=>i.code===421)?.value,o;n!=null&&(o=at(n),await Et(this.shouldBlockConnection("denyOutboundConnection",o,e),t.signal));let s="outbound";return t.initiator===!1&&(s="inbound"),await this._performUpgrade(e,s,t)}catch(n){throw this.metrics.errors?.increment({outbound:!0}),this.metrics.outboundErrors?.increment({[n.name??"Error"]:!0}),n}}async _performUpgrade(e,t,n){let o=e,s,i,a,c,l=`${parseInt(String(Math.random()*1e9)).toString(36)}${Date.now()}`;if(e.log=e.log.newScope(`${t}:${l}`),this.components.metrics?.trackMultiaddrConnection(e),e.log.trace("starting the %s connection upgrade",t),n?.skipProtection!==!0){let u=this.components.connectionProtector;u!=null&&(e.log("protecting the %s connection",t),o=await u.protect(o,n))}try{if(ew(n)){if(n.remotePeer==null)throw new Ct(`${t} connection that skipped encryption must have a peer id`);c="native",s=n.remotePeer}else{let u=e.remoteAddr.getComponents().findLast(h=>h.code===421)?.value,d;u!=null&&(d=at(u)),n?.onProgress?.(new de(`upgrader:encrypt-${t}-connection`)),{connection:o,remotePeer:s,protocol:c,streamMuxer:i}=await(t==="inbound"?this._encryptInbound(o,{...n,remotePeer:d}):this._encryptOutbound(o,{...n,remotePeer:d}))}if(s.equals(this.components.peerId)){let u=new Ir("Can not dial self");throw e.abort(u),u}await this.shouldBlockConnection(t==="inbound"?"denyInboundEncryptedConnection":"denyOutboundEncryptedConnection",s,e),n?.muxerFactory!=null?i=n.muxerFactory:i==null&&this.streamMuxers.size>0&&(n?.onProgress?.(new de(`upgrader:multiplex-${t}-connection`)),i=await(t==="inbound"?this._multiplexInbound(o,this.streamMuxers,n):this._multiplexOutbound(o,this.streamMuxers,n)))}catch(u){throw e.log.error("failed to upgrade %s connection %s %a - %e",t,t==="inbound"?"from":"to",e.remoteAddr,u),u}i!=null&&(e.log("create muxer %s",i.protocol),a=i.createStreamMuxer(o)),await this.shouldBlockConnection(t==="inbound"?"denyInboundUpgradedConnection":"denyOutboundUpgradedConnection",s,e);let f=this._createConnection({id:l,cryptoProtocol:c,direction:t,maConn:e,stream:o,muxer:a,remotePeer:s,limits:n?.limits,closeTimeout:this.connectionCloseTimeout});return f.log("successfully upgraded connection"),f}_createConnection(e){let t=Np(this.components,{...e,outboundStreamProtocolNegotiationTimeout:this.outboundStreamProtocolNegotiationTimeout,inboundStreamProtocolNegotiationTimeout:this.inboundStreamProtocolNegotiationTimeout});return t.addEventListener("close",()=>{this.events.safeDispatchEvent("connection:close",{detail:t})}),this.events.safeDispatchEvent("connection:open",{detail:t}),t}async _encryptInbound(e,t){let n=Array.from(this.connectionEncrypters.keys());try{let o=await gn(e,n,t),s=this.connectionEncrypters.get(o);if(s==null)throw new Sr(`no crypto module found for ${o}`);return e.log("encrypting inbound connection using %s",o),{...await s.secureInbound(e,t),protocol:o}}catch(o){throw new Sr(o.message)}}async _encryptOutbound(e,t){let n=Array.from(this.connectionEncrypters.keys());try{e.log.trace("selecting encrypter from %s",n);let o=await mn(e,n,t),s=this.connectionEncrypters.get(o);if(s==null)throw new Sr(`no crypto module found for ${o}`);return e.log("encrypting outbound connection using %s",o),{...await s.secureOutbound(e,t),protocol:o}}catch(o){throw new Sr(o.message)}}async _multiplexOutbound(e,t,n){let o=Array.from(t.keys());e.log("outbound selecting muxer %s",o);try{e.log.trace("selecting stream muxer from %s",o);let s=await mn(e,o,n),i=t.get(s);if(i==null)throw new Vt(`No muxer configured for protocol "${s}"`);return e.log("selected %s as muxer protocol",s),i}catch(s){throw e.log.error("error multiplexing outbound connection - %e",s),new Vt(String(s))}}async _multiplexInbound(e,t,n){let o=Array.from(t.keys());e.log("inbound handling muxers %s",o);try{e.log.trace("selecting stream muxer from %s",o);let s=await gn(e,o,n),i=t.get(s);if(i==null)throw new Vt(`No muxer configured for protocol "${s}"`);return e.log("selected %s as muxer protocol",s),i}catch(s){throw e.log.error("error multiplexing inbound connection - %e",s),s}}getConnectionEncrypters(){return this.connectionEncrypters}getStreamMuxers(){return this.streamMuxers}};function ew(r){return r.skipEncryption===!0}var ia="3.1.1",aa="js-libp2p";function Bp(r,e){return`${r??aa}/${e??ia} browser/${globalThis.navigator.userAgent}`}var To=class extends Le{peerId;peerStore;contentRouting;peerRouting;metrics;services;logger;status;components;log;constructor(e){super(),this.status="stopped";let t=new Le,n=t.dispatchEvent.bind(t);t.dispatchEvent=l=>{let f=n(l),u=this.dispatchEvent(new CustomEvent(l.type,{detail:l.detail}));return f||u},this.peerId=e.peerId,this.logger=e.logger??ds(),this.log=this.logger.forComponent("libp2p"),this.services={};let o=e.nodeInfo?.name??aa,s=e.nodeInfo?.version??ia,i=this.components=fp({peerId:e.peerId,privateKey:e.privateKey,nodeInfo:{name:o,version:s,userAgent:e.nodeInfo?.userAgent??Bp(o,s)},logger:this.logger,events:t,datastore:e.datastore??new yi,connectionGater:hp(e.connectionGater),dns:e.dns});e.metrics!=null&&(this.metrics=this.configureComponent("metrics",e.metrics(this.components))),this.peerStore=this.configureComponent("peerStore",Zh(i,{addressFilter:this.components.connectionGater.filterMultiaddrForPeer,...e.peerStore})),i.events.addEventListener("peer:update",l=>{if(l.detail.previous==null){let f={id:l.detail.peer.id,multiaddrs:l.detail.peer.addresses.map(u=>u.multiaddr)};i.events.safeDispatchEvent("peer:discovery",{detail:f})}}),e.connectionProtector!=null&&this.configureComponent("connectionProtector",e.connectionProtector(i)),this.components.upgrader=new sa(this.components,{connectionEncrypters:(e.connectionEncrypters??[]).map((l,f)=>this.configureComponent(`connection-encryption-${f}`,l(this.components))),streamMuxers:(e.streamMuxers??[]).map((l,f)=>this.configureComponent(`stream-muxers-${f}`,l(this.components))),inboundUpgradeTimeout:e.connectionManager?.inboundUpgradeTimeout,inboundStreamProtocolNegotiationTimeout:e.connectionManager?.inboundStreamProtocolNegotiationTimeout,outboundStreamProtocolNegotiationTimeout:e.connectionManager?.outboundStreamProtocolNegotiationTimeout,connectionCloseTimeout:e.connectionManager?.connectionCloseTimeout}),this.configureComponent("transportManager",new oa(this.components,e.transportManager)),this.configureComponent("connectionManager",new ji(this.components,e.connectionManager)),e.connectionMonitor?.enabled!==!1&&this.configureComponent("connectionMonitor",new Qi(this.components,e.connectionMonitor)),this.configureComponent("registrar",new na(this.components)),this.configureComponent("addressManager",new Ci(this.components,e.addresses));let a=(e.peerRouters??[]).map((l,f)=>this.configureComponent(`peer-router-${f}`,l(this.components)));this.peerRouting=this.components.peerRouting=this.configureComponent("peerRouting",new ta(this.components,{routers:a}));let c=(e.contentRouters??[]).map((l,f)=>this.configureComponent(`content-router-${f}`,l(this.components)));if(this.contentRouting=this.components.contentRouting=this.configureComponent("contentRouting",new Ji(this.components,{routers:c})),this.configureComponent("randomWalk",new ra(this.components)),(e.peerDiscovery??[]).forEach((l,f)=>{this.configureComponent(`peer-discovery-${f}`,l(this.components)).addEventListener("peer",d=>{this.#e(d)})}),e.transports?.forEach((l,f)=>{this.components.transportManager.add(this.configureComponent(`transport-${f}`,l(this.components)))}),e.services!=null)for(let l of Object.keys(e.services)){let f=e.services[l],u=f(this.components);if(u==null){this.log.error("service factory %s returned null or undefined instance",l);continue}this.services[l]=u,this.configureComponent(l,u),u[ua]!=null&&(this.log("registering service %s for content routing",l),c.push(u[ua])),u[da]!=null&&(this.log("registering service %s for peer routing",l),a.push(u[da])),u[fa]!=null&&(this.log("registering service %s for peer discovery",l),u[fa].addEventListener?.("peer",d=>{this.#e(d)}))}dp(i)}configureComponent(e,t){return t==null&&this.log.error("component %s was null or undefined",e),this.components[e]=t,t}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 with peer id %p",this.peerId)}catch(e){throw this.log.error("an error occurred starting libp2p - %e",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 ir;for(let t of this.components.connectionManager.getConnections())e.add(t.remotePeer);return Array.from(e)}async dial(e,t={}){return this.components.connectionManager.openConnection(e,{priority:75,...t})}async dialProtocol(e,t,n={}){if(t==null)throw new C("no protocols were provided to open a stream");if(t=Array.isArray(t)?t:[t],t.length===0)throw new C("no protocols were provided to open a stream");return this.components.connectionManager.openStream(e,t,n)}getMultiaddrs(){return this.components.addressManager.getAddresses()}getProtocols(){return this.components.registrar.getProtocols()}async hangUp(e,t={}){$t(e)&&(e=at(e.getComponents().findLast(n=>n.code===421)?.value??"")),await this.components.connectionManager.closeConnections(e,t)}async getPublicKey(e,t={}){if(this.log("getPublicKey %p",e),e.publicKey!=null)return e.publicKey;try{let i=await this.peerStore.get(e,t);if(i.id.publicKey!=null)return i.id.publicKey}catch(i){if(i.name!=="NotFoundError")throw i}let n=Xe([L("/pk/"),e.toMultihash().bytes]),o=await this.contentRouting.get(n,t),s=Gr(o);return await this.peerStore.patch(e,{publicKey:s},t),s}async handle(e,t,n){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async o=>{await this.components.registrar.handle(o,t,n)}))}async unhandle(e,t){Array.isArray(e)||(e=[e]),await Promise.all(e.map(async n=>{await this.components.registrar.unhandle(n,t)}))}async register(e,t,n){return this.components.registrar.register(e,t,n)}unregister(e){this.components.registrar.unregister(e)}use(e,t){this.components.registrar.use(e,Array.isArray(t)?t:[t])}unuse(e){this.components.registrar.unuse(e)}async isDialable(e,t={}){return this.components.connectionManager.isDialable(e,t)}#e(e){let{detail:t}=e;if(t.id.toString()===this.peerId.toString()){this.log.error("peer discovery mechanism discovered self");return}this.components.peerStore.merge(t.id,{multiaddrs:t.multiaddrs}).catch(n=>{this.log.error("could not update multiaddrs of discovered peer - %e",n)})}};async function tw(r={}){r.privateKey??=await qd("Ed25519");let e=new To({...await Wd(r),peerId:Hd(r.privateKey)});return r.start!==!1&&await e.start(),e}var rw=["dial","dialProtocol","hangUp","handle","unhandle","getMultiaddrs","getProtocols"];function nw(r){return r==null?!1:r instanceof To?!0:rw.every(e=>typeof r[e]=="function")}return Vp(ow);})();
|
|
27
27
|
/*! Bundled license information:
|
|
28
28
|
|
|
29
29
|
@noble/hashes/utils.js:
|