libp2p 0.46.21 → 1.0.0-7682861f9
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 +51 -47
- package/dist/index.min.js +40 -28
- package/dist/src/address-manager/index.d.ts +4 -5
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +3 -3
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/components.d.ts +4 -15
- package/dist/src/components.d.ts.map +1 -1
- package/dist/src/components.js +2 -2
- package/dist/src/components.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +1 -1
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -1
- package/dist/src/config/connection-gater.browser.js.map +1 -1
- package/dist/src/config/connection-gater.d.ts +1 -1
- package/dist/src/config/connection-gater.d.ts.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +1 -2
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection/index.d.ts +4 -6
- package/dist/src/connection/index.d.ts.map +1 -1
- package/dist/src/connection/index.js +11 -13
- package/dist/src/connection/index.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts +3 -6
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +21 -21
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/connection-pruner.d.ts +3 -5
- package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
- package/dist/src/connection-manager/connection-pruner.js +8 -8
- package/dist/src/connection-manager/connection-pruner.js.map +1 -1
- package/dist/src/connection-manager/constants.defaults.d.ts +0 -4
- package/dist/src/connection-manager/constants.defaults.d.ts.map +1 -1
- package/dist/src/connection-manager/constants.defaults.js +0 -4
- package/dist/src/connection-manager/constants.defaults.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts +3 -10
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +30 -38
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +3 -18
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +17 -18
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/utils.js +1 -1
- package/dist/src/connection-manager/utils.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +1 -5
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +2 -2
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -2
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/content-routing/utils.js +1 -1
- package/dist/src/content-routing/utils.js.map +1 -1
- package/dist/src/errors.d.ts +0 -2
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +0 -2
- package/dist/src/errors.js.map +1 -1
- package/dist/src/get-peer.d.ts +1 -1
- package/dist/src/get-peer.d.ts.map +1 -1
- package/dist/src/get-peer.js +1 -5
- package/dist/src/get-peer.js.map +1 -1
- package/dist/src/index.d.ts +5 -16
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -12
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +31 -65
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -6
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +12 -9
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/registrar.d.ts +5 -7
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +4 -4
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +5 -9
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +12 -13
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +8 -19
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +75 -38
- package/dist/src/upgrader.js.map +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/package.json +37 -106
- package/src/address-manager/index.ts +6 -9
- package/src/components.ts +5 -16
- package/src/config/connection-gater.browser.ts +1 -1
- package/src/config/connection-gater.ts +1 -1
- package/src/config.ts +1 -2
- package/src/connection/index.ts +12 -17
- package/src/connection-manager/auto-dial.ts +23 -26
- package/src/connection-manager/connection-pruner.ts +10 -12
- package/src/connection-manager/constants.defaults.ts +0 -5
- package/src/connection-manager/dial-queue.ts +31 -46
- package/src/connection-manager/index.ts +19 -36
- package/src/connection-manager/utils.ts +1 -1
- package/src/content-routing/index.ts +3 -7
- package/src/content-routing/utils.ts +2 -3
- package/src/errors.ts +0 -2
- package/src/get-peer.ts +2 -7
- package/src/index.ts +6 -17
- package/src/libp2p.ts +32 -80
- package/src/peer-routing.ts +16 -15
- package/src/registrar.ts +8 -12
- package/src/transport-manager.ts +15 -22
- package/src/upgrader.ts +110 -64
- package/src/version.ts +1 -1
- package/dist/src/autonat/constants.d.ts +0 -18
- package/dist/src/autonat/constants.d.ts.map +0 -1
- package/dist/src/autonat/constants.js +0 -18
- package/dist/src/autonat/constants.js.map +0 -1
- package/dist/src/autonat/index.d.ts +0 -62
- package/dist/src/autonat/index.d.ts.map +0 -1
- package/dist/src/autonat/index.js +0 -447
- package/dist/src/autonat/index.js.map +0 -1
- package/dist/src/autonat/pb/index.d.ts +0 -57
- package/dist/src/autonat/pb/index.d.ts.map +0 -1
- package/dist/src/autonat/pb/index.js +0 -250
- package/dist/src/autonat/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/constants.d.ts +0 -50
- package/dist/src/circuit-relay/constants.d.ts.map +0 -1
- package/dist/src/circuit-relay/constants.js +0 -56
- package/dist/src/circuit-relay/constants.js.map +0 -1
- package/dist/src/circuit-relay/index.d.ts +0 -55
- package/dist/src/circuit-relay/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/index.js +0 -38
- package/dist/src/circuit-relay/index.js.map +0 -1
- package/dist/src/circuit-relay/pb/index.d.ts +0 -93
- package/dist/src/circuit-relay/pb/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/pb/index.js +0 -425
- package/dist/src/circuit-relay/pb/index.js.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.d.ts +0 -43
- package/dist/src/circuit-relay/server/advert-service.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/advert-service.js +0 -73
- package/dist/src/circuit-relay/server/advert-service.js.map +0 -1
- package/dist/src/circuit-relay/server/index.d.ts +0 -65
- package/dist/src/circuit-relay/server/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/index.js +0 -312
- package/dist/src/circuit-relay/server/index.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.d.ts +0 -49
- package/dist/src/circuit-relay/server/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-store.js +0 -65
- package/dist/src/circuit-relay/server/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts +0 -18
- package/dist/src/circuit-relay/server/reservation-voucher.d.ts.map +0 -1
- package/dist/src/circuit-relay/server/reservation-voucher.js +0 -36
- package/dist/src/circuit-relay/server/reservation-voucher.js.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.d.ts +0 -45
- package/dist/src/circuit-relay/transport/discovery.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/discovery.js +0 -97
- package/dist/src/circuit-relay/transport/discovery.js.map +0 -1
- package/dist/src/circuit-relay/transport/index.d.ts +0 -57
- package/dist/src/circuit-relay/transport/index.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/index.js +0 -276
- package/dist/src/circuit-relay/transport/index.js.map +0 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +0 -9
- package/dist/src/circuit-relay/transport/listener.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/listener.js +0 -66
- package/dist/src/circuit-relay/transport/listener.js.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +0 -72
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +0 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +0 -209
- package/dist/src/circuit-relay/transport/reservation-store.js.map +0 -1
- package/dist/src/circuit-relay/utils.d.ts +0 -13
- package/dist/src/circuit-relay/utils.d.ts.map +0 -1
- package/dist/src/circuit-relay/utils.js +0 -108
- package/dist/src/circuit-relay/utils.js.map +0 -1
- package/dist/src/dcutr/dcutr.d.ts +0 -43
- package/dist/src/dcutr/dcutr.d.ts.map +0 -1
- package/dist/src/dcutr/dcutr.js +0 -313
- package/dist/src/dcutr/dcutr.js.map +0 -1
- package/dist/src/dcutr/index.d.ts +0 -92
- package/dist/src/dcutr/index.d.ts.map +0 -1
- package/dist/src/dcutr/index.js +0 -63
- package/dist/src/dcutr/index.js.map +0 -1
- package/dist/src/dcutr/pb/message.d.ts +0 -20
- package/dist/src/dcutr/pb/message.d.ts.map +0 -1
- package/dist/src/dcutr/pb/message.js +0 -77
- package/dist/src/dcutr/pb/message.js.map +0 -1
- package/dist/src/dcutr/utils.d.ts +0 -8
- package/dist/src/dcutr/utils.d.ts.map +0 -1
- package/dist/src/dcutr/utils.js +0 -27
- package/dist/src/dcutr/utils.js.map +0 -1
- package/dist/src/fetch/constants.d.ts +0 -3
- package/dist/src/fetch/constants.d.ts.map +0 -1
- package/dist/src/fetch/constants.js +0 -4
- package/dist/src/fetch/constants.js.map +0 -1
- package/dist/src/fetch/index.d.ts +0 -61
- package/dist/src/fetch/index.d.ts.map +0 -1
- package/dist/src/fetch/index.js +0 -205
- package/dist/src/fetch/index.js.map +0 -1
- package/dist/src/fetch/pb/proto.d.ts +0 -28
- package/dist/src/fetch/pb/proto.d.ts.map +0 -1
- package/dist/src/fetch/pb/proto.js +0 -120
- package/dist/src/fetch/pb/proto.js.map +0 -1
- package/dist/src/identify/consts.d.ts +0 -10
- package/dist/src/identify/consts.d.ts.map +0 -1
- package/dist/src/identify/consts.js +0 -11
- package/dist/src/identify/consts.js.map +0 -1
- package/dist/src/identify/identify.d.ts +0 -54
- package/dist/src/identify/identify.d.ts.map +0 -1
- package/dist/src/identify/identify.js +0 -447
- package/dist/src/identify/identify.js.map +0 -1
- package/dist/src/identify/index.d.ts +0 -72
- package/dist/src/identify/index.d.ts.map +0 -1
- package/dist/src/identify/index.js +0 -15
- package/dist/src/identify/index.js.map +0 -1
- package/dist/src/identify/pb/message.d.ts +0 -17
- package/dist/src/identify/pb/message.d.ts.map +0 -1
- package/dist/src/identify/pb/message.js +0 -98
- package/dist/src/identify/pb/message.js.map +0 -1
- package/dist/src/insecure/index.d.ts +0 -26
- package/dist/src/insecure/index.d.ts.map +0 -1
- package/dist/src/insecure/index.js +0 -110
- package/dist/src/insecure/index.js.map +0 -1
- package/dist/src/insecure/pb/proto.d.ts +0 -30
- package/dist/src/insecure/pb/proto.d.ts.map +0 -1
- package/dist/src/insecure/pb/proto.js +0 -127
- package/dist/src/insecure/pb/proto.js.map +0 -1
- package/dist/src/ping/constants.d.ts +0 -9
- package/dist/src/ping/constants.d.ts.map +0 -1
- package/dist/src/ping/constants.js +0 -15
- package/dist/src/ping/constants.js.map +0 -1
- package/dist/src/ping/index.d.ts +0 -24
- package/dist/src/ping/index.d.ts.map +0 -1
- package/dist/src/ping/index.js +0 -115
- package/dist/src/ping/index.js.map +0 -1
- package/dist/src/pnet/crypto.d.ts +0 -18
- package/dist/src/pnet/crypto.d.ts.map +0 -1
- package/dist/src/pnet/crypto.js +0 -60
- package/dist/src/pnet/crypto.js.map +0 -1
- package/dist/src/pnet/errors.d.ts +0 -6
- package/dist/src/pnet/errors.d.ts.map +0 -1
- package/dist/src/pnet/errors.js +0 -6
- package/dist/src/pnet/errors.js.map +0 -1
- package/dist/src/pnet/index.d.ts +0 -31
- package/dist/src/pnet/index.d.ts.map +0 -1
- package/dist/src/pnet/index.js +0 -97
- package/dist/src/pnet/index.js.map +0 -1
- package/dist/src/pnet/key-generator.d.ts +0 -11
- package/dist/src/pnet/key-generator.d.ts.map +0 -1
- package/dist/src/pnet/key-generator.js +0 -22
- package/dist/src/pnet/key-generator.js.map +0 -1
- package/dist/src/upnp-nat/index.d.ts +0 -71
- package/dist/src/upnp-nat/index.d.ts.map +0 -1
- package/dist/src/upnp-nat/index.js +0 -136
- package/dist/src/upnp-nat/index.js.map +0 -1
- package/dist/src/utils/peer-job-queue.d.ts +0 -33
- package/dist/src/utils/peer-job-queue.d.ts.map +0 -1
- package/dist/src/utils/peer-job-queue.js +0 -82
- package/dist/src/utils/peer-job-queue.js.map +0 -1
- package/dist/typedoc-urls.json +0 -71
- package/src/autonat/constants.ts +0 -19
- package/src/autonat/index.ts +0 -597
- package/src/autonat/pb/index.proto +0 -35
- package/src/autonat/pb/index.ts +0 -320
- package/src/circuit-relay/constants.ts +0 -72
- package/src/circuit-relay/index.ts +0 -59
- package/src/circuit-relay/pb/index.proto +0 -67
- package/src/circuit-relay/pb/index.ts +0 -539
- package/src/circuit-relay/server/advert-service.ts +0 -108
- package/src/circuit-relay/server/index.ts +0 -443
- package/src/circuit-relay/server/reservation-store.ts +0 -116
- package/src/circuit-relay/server/reservation-voucher.ts +0 -51
- package/src/circuit-relay/transport/discovery.ts +0 -136
- package/src/circuit-relay/transport/index.ts +0 -396
- package/src/circuit-relay/transport/listener.ts +0 -97
- package/src/circuit-relay/transport/reservation-store.ts +0 -312
- package/src/circuit-relay/utils.ts +0 -136
- package/src/dcutr/dcutr.ts +0 -378
- package/src/dcutr/index.ts +0 -102
- package/src/dcutr/pb/message.proto +0 -12
- package/src/dcutr/pb/message.ts +0 -96
- package/src/dcutr/utils.ts +0 -33
- package/src/fetch/README.md +0 -41
- package/src/fetch/constants.ts +0 -3
- package/src/fetch/index.ts +0 -315
- package/src/fetch/pb/proto.proto +0 -15
- package/src/fetch/pb/proto.ts +0 -153
- package/src/identify/README.md +0 -13
- package/src/identify/consts.ts +0 -12
- package/src/identify/identify.ts +0 -561
- package/src/identify/index.ts +0 -90
- package/src/identify/pb/message.proto +0 -30
- package/src/identify/pb/message.ts +0 -126
- package/src/insecure/index.ts +0 -138
- package/src/insecure/pb/proto.proto +0 -18
- package/src/insecure/pb/proto.ts +0 -161
- package/src/ping/constants.ts +0 -15
- package/src/ping/index.ts +0 -170
- package/src/pnet/README.md +0 -68
- package/src/pnet/crypto.ts +0 -67
- package/src/pnet/errors.ts +0 -5
- package/src/pnet/index.ts +0 -126
- package/src/pnet/key-generator.ts +0 -23
- package/src/upnp-nat/index.ts +0 -212
- package/src/utils/peer-job-queue.ts +0 -119
package/src/libp2p.ts
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { unmarshalPublicKey } from '@libp2p/crypto/keys'
|
|
2
|
-
import { type ContentRouting,
|
|
3
|
-
import { CodeError } from '@libp2p/interface
|
|
4
|
-
import { TypedEventEmitter, CustomEvent, setMaxListeners } from '@libp2p/interface/events'
|
|
5
|
-
import { peerDiscovery } from '@libp2p/interface/peer-discovery'
|
|
6
|
-
import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing'
|
|
7
|
-
import { DefaultKeyChain } from '@libp2p/keychain'
|
|
2
|
+
import { type ContentRouting, contentRoutingSymbol, type PeerRouting, peerRoutingSymbol, type Libp2pEvents, type PendingDial, type ServiceMap, type AbortOptions, type ComponentLogger, type Logger, type Connection, type NewStreamOptions, type Stream, type Metrics, type PeerId, type PeerInfo, type PeerStore, type Topology } from '@libp2p/interface'
|
|
3
|
+
import { CodeError, TypedEventEmitter, CustomEvent, setMaxListeners, peerDiscoverySymbol } from '@libp2p/interface'
|
|
8
4
|
import { defaultLogger } from '@libp2p/logger'
|
|
9
5
|
import { PeerSet } from '@libp2p/peer-collections'
|
|
10
6
|
import { peerIdFromString } from '@libp2p/peer-id'
|
|
@@ -12,7 +8,6 @@ import { createEd25519PeerId } from '@libp2p/peer-id-factory'
|
|
|
12
8
|
import { PersistentPeerStore } from '@libp2p/peer-store'
|
|
13
9
|
import { isMultiaddr, type Multiaddr } from '@multiformats/multiaddr'
|
|
14
10
|
import { MemoryDatastore } from 'datastore-core/memory'
|
|
15
|
-
import mergeOptions from 'merge-options'
|
|
16
11
|
import { concat as uint8ArrayConcat } from 'uint8arrays/concat'
|
|
17
12
|
import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
|
|
18
13
|
import { DefaultAddressManager } from './address-manager/index.js'
|
|
@@ -26,32 +21,23 @@ import { DefaultPeerRouting } from './peer-routing.js'
|
|
|
26
21
|
import { DefaultRegistrar } from './registrar.js'
|
|
27
22
|
import { DefaultTransportManager } from './transport-manager.js'
|
|
28
23
|
import { DefaultUpgrader } from './upgrader.js'
|
|
24
|
+
import * as pkg from './version.js'
|
|
29
25
|
import type { Components } from './components.js'
|
|
30
26
|
import type { Libp2p, Libp2pInit, Libp2pOptions } from './index.js'
|
|
31
|
-
import type {
|
|
32
|
-
import type { Connection, NewStreamOptions, Stream } from '@libp2p/interface/connection'
|
|
33
|
-
import type { KeyChain } from '@libp2p/interface/keychain'
|
|
34
|
-
import type { Metrics } from '@libp2p/interface/metrics'
|
|
35
|
-
import type { PeerId } from '@libp2p/interface/peer-id'
|
|
36
|
-
import type { PeerInfo } from '@libp2p/interface/peer-info'
|
|
37
|
-
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
38
|
-
import type { Topology } from '@libp2p/interface/topology'
|
|
39
|
-
import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal/registrar'
|
|
40
|
-
import type { Datastore } from 'interface-datastore'
|
|
27
|
+
import type { StreamHandler, StreamHandlerOptions } from '@libp2p/interface-internal'
|
|
41
28
|
|
|
42
29
|
export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends TypedEventEmitter<Libp2pEvents> implements Libp2p<T> {
|
|
43
30
|
public peerId: PeerId
|
|
44
31
|
public peerStore: PeerStore
|
|
45
32
|
public contentRouting: ContentRouting
|
|
46
33
|
public peerRouting: PeerRouting
|
|
47
|
-
public keychain: KeyChain
|
|
48
34
|
public metrics?: Metrics
|
|
49
35
|
public services: T
|
|
50
36
|
public logger: ComponentLogger
|
|
51
37
|
|
|
52
38
|
public components: Components
|
|
53
39
|
#started: boolean
|
|
54
|
-
readonly
|
|
40
|
+
private readonly log: Logger
|
|
55
41
|
|
|
56
42
|
constructor (init: Libp2pInit<T>) {
|
|
57
43
|
super()
|
|
@@ -75,11 +61,15 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
75
61
|
this.#started = false
|
|
76
62
|
this.peerId = init.peerId
|
|
77
63
|
this.logger = init.logger ?? defaultLogger()
|
|
78
|
-
this
|
|
64
|
+
this.log = this.logger.forComponent('libp2p')
|
|
79
65
|
// @ts-expect-error {} may not be of type T
|
|
80
66
|
this.services = {}
|
|
81
67
|
const components = this.components = defaultComponents({
|
|
82
68
|
peerId: init.peerId,
|
|
69
|
+
nodeInfo: init.nodeInfo ?? {
|
|
70
|
+
name: pkg.name,
|
|
71
|
+
version: pkg.version
|
|
72
|
+
},
|
|
83
73
|
logger: this.logger,
|
|
84
74
|
events,
|
|
85
75
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
@@ -101,8 +91,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
101
91
|
if (evt.detail.previous == null) {
|
|
102
92
|
const peerInfo: PeerInfo = {
|
|
103
93
|
id: evt.detail.peer.id,
|
|
104
|
-
multiaddrs: evt.detail.peer.addresses.map(a => a.multiaddr)
|
|
105
|
-
protocols: evt.detail.peer.protocols
|
|
94
|
+
multiaddrs: evt.detail.peer.addresses.map(a => a.multiaddr)
|
|
106
95
|
}
|
|
107
96
|
|
|
108
97
|
components.events.safeDispatchEvent('peer:discovery', { detail: peerInfo })
|
|
@@ -133,13 +122,6 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
133
122
|
// Addresses {listen, announce, noAnnounce}
|
|
134
123
|
this.configureComponent('addressManager', new DefaultAddressManager(this.components, init.addresses))
|
|
135
124
|
|
|
136
|
-
// Create keychain
|
|
137
|
-
const keychainOpts = DefaultKeyChain.generateOptions()
|
|
138
|
-
this.keychain = this.configureComponent('keyChain', new DefaultKeyChain(this.components, {
|
|
139
|
-
...keychainOpts,
|
|
140
|
-
...init.keychain
|
|
141
|
-
}))
|
|
142
|
-
|
|
143
125
|
// Peer routers
|
|
144
126
|
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map((fn, index) => this.configureComponent(`peer-router-${index}`, fn(this.components)))
|
|
145
127
|
this.peerRouting = this.components.peerRouting = this.configureComponent('peerRouting', new DefaultPeerRouting(this.components, {
|
|
@@ -173,26 +155,26 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
173
155
|
const service: any = createService(this.components)
|
|
174
156
|
|
|
175
157
|
if (service == null) {
|
|
176
|
-
this
|
|
158
|
+
this.log.error('service factory %s returned null or undefined instance', name)
|
|
177
159
|
continue
|
|
178
160
|
}
|
|
179
161
|
|
|
180
162
|
this.services[name as keyof T] = service
|
|
181
163
|
this.configureComponent(name, service)
|
|
182
164
|
|
|
183
|
-
if (service[
|
|
184
|
-
this
|
|
185
|
-
contentRouters.push(service[
|
|
165
|
+
if (service[contentRoutingSymbol] != null) {
|
|
166
|
+
this.log('registering service %s for content routing', name)
|
|
167
|
+
contentRouters.push(service[contentRoutingSymbol])
|
|
186
168
|
}
|
|
187
169
|
|
|
188
|
-
if (service[
|
|
189
|
-
this
|
|
190
|
-
peerRouters.push(service[
|
|
170
|
+
if (service[peerRoutingSymbol] != null) {
|
|
171
|
+
this.log('registering service %s for peer routing', name)
|
|
172
|
+
peerRouters.push(service[peerRoutingSymbol])
|
|
191
173
|
}
|
|
192
174
|
|
|
193
|
-
if (service[
|
|
194
|
-
this
|
|
195
|
-
service[
|
|
175
|
+
if (service[peerDiscoverySymbol] != null) {
|
|
176
|
+
this.log('registering service %s for peer discovery', name)
|
|
177
|
+
service[peerDiscoverySymbol].addEventListener('peer', (evt: CustomEvent<PeerInfo>) => {
|
|
196
178
|
this.#onDiscoveryPeer(evt)
|
|
197
179
|
})
|
|
198
180
|
}
|
|
@@ -202,7 +184,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
202
184
|
|
|
203
185
|
private configureComponent <T> (name: string, component: T): T {
|
|
204
186
|
if (component == null) {
|
|
205
|
-
this
|
|
187
|
+
this.log.error('component %s was null or undefined', name)
|
|
206
188
|
}
|
|
207
189
|
|
|
208
190
|
this.components[name] = component
|
|
@@ -220,14 +202,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
220
202
|
|
|
221
203
|
this.#started = true
|
|
222
204
|
|
|
223
|
-
this
|
|
224
|
-
|
|
225
|
-
const keys = await this.keychain.listKeys()
|
|
226
|
-
|
|
227
|
-
if (keys.find(key => key.name === 'self') == null) {
|
|
228
|
-
this.#log('importing self key into keychain')
|
|
229
|
-
await this.keychain.importPeer('self', this.components.peerId)
|
|
230
|
-
}
|
|
205
|
+
this.log('libp2p is starting')
|
|
231
206
|
|
|
232
207
|
try {
|
|
233
208
|
await this.components.beforeStart?.()
|
|
@@ -235,9 +210,9 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
235
210
|
await this.components.afterStart?.()
|
|
236
211
|
|
|
237
212
|
this.safeDispatchEvent('start', { detail: this })
|
|
238
|
-
this
|
|
213
|
+
this.log('libp2p has started')
|
|
239
214
|
} catch (err: any) {
|
|
240
|
-
this
|
|
215
|
+
this.log.error('An error occurred starting libp2p', err)
|
|
241
216
|
await this.stop()
|
|
242
217
|
throw err
|
|
243
218
|
}
|
|
@@ -251,7 +226,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
251
226
|
return
|
|
252
227
|
}
|
|
253
228
|
|
|
254
|
-
this
|
|
229
|
+
this.log('libp2p is stopping')
|
|
255
230
|
|
|
256
231
|
this.#started = false
|
|
257
232
|
|
|
@@ -260,7 +235,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
260
235
|
await this.components.afterStop?.()
|
|
261
236
|
|
|
262
237
|
this.safeDispatchEvent('stop', { detail: this })
|
|
263
|
-
this
|
|
238
|
+
this.log('libp2p has stopped')
|
|
264
239
|
}
|
|
265
240
|
|
|
266
241
|
isStarted (): boolean {
|
|
@@ -325,7 +300,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
325
300
|
* Get the public key for the given peer id
|
|
326
301
|
*/
|
|
327
302
|
async getPublicKey (peer: PeerId, options: AbortOptions = {}): Promise<Uint8Array> {
|
|
328
|
-
this
|
|
303
|
+
this.log('getPublicKey %p', peer)
|
|
329
304
|
|
|
330
305
|
if (peer.publicKey != null) {
|
|
331
306
|
return peer.publicKey
|
|
@@ -394,15 +369,14 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
394
369
|
const { detail: peer } = evt
|
|
395
370
|
|
|
396
371
|
if (peer.id.toString() === this.peerId.toString()) {
|
|
397
|
-
this
|
|
372
|
+
this.log.error(new Error(codes.ERR_DISCOVERED_SELF))
|
|
398
373
|
return
|
|
399
374
|
}
|
|
400
375
|
|
|
401
376
|
void this.components.peerStore.merge(peer.id, {
|
|
402
|
-
multiaddrs: peer.multiaddrs
|
|
403
|
-
protocols: peer.protocols
|
|
377
|
+
multiaddrs: peer.multiaddrs
|
|
404
378
|
})
|
|
405
|
-
.catch(err => { this
|
|
379
|
+
.catch(err => { this.log.error(err) })
|
|
406
380
|
}
|
|
407
381
|
}
|
|
408
382
|
|
|
@@ -411,29 +385,7 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
|
|
|
411
385
|
* libp2p interface and is useful for testing and debugging.
|
|
412
386
|
*/
|
|
413
387
|
export async function createLibp2pNode <T extends ServiceMap = Record<string, unknown>> (options: Libp2pOptions<T>): Promise<Libp2pNode<T>> {
|
|
414
|
-
|
|
415
|
-
const datastore = options.datastore as Datastore | undefined
|
|
416
|
-
|
|
417
|
-
if (datastore != null) {
|
|
418
|
-
try {
|
|
419
|
-
// try load the peer id from the keychain
|
|
420
|
-
const keyChain = new DefaultKeyChain({
|
|
421
|
-
datastore
|
|
422
|
-
}, mergeOptions(DefaultKeyChain.generateOptions(), options.keychain))
|
|
423
|
-
|
|
424
|
-
options.peerId = await keyChain.exportPeerId('self')
|
|
425
|
-
} catch (err: any) {
|
|
426
|
-
if (err.code !== 'ERR_NOT_FOUND') {
|
|
427
|
-
throw err
|
|
428
|
-
}
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
if (options.peerId == null) {
|
|
434
|
-
// no peer id in the keychain, create a new peer id
|
|
435
|
-
options.peerId = await createEd25519PeerId()
|
|
436
|
-
}
|
|
388
|
+
options.peerId ??= await createEd25519PeerId()
|
|
437
389
|
|
|
438
390
|
return new Libp2pNode(validateConfig(options))
|
|
439
391
|
}
|
package/src/peer-routing.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
3
2
|
import filter from 'it-filter'
|
|
4
3
|
import first from 'it-first'
|
|
5
4
|
import merge from 'it-merge'
|
|
@@ -10,13 +9,8 @@ import {
|
|
|
10
9
|
requirePeers
|
|
11
10
|
} from './content-routing/utils.js'
|
|
12
11
|
import { codes, messages } from './errors.js'
|
|
13
|
-
import type { AbortOptions } from '@libp2p/interface'
|
|
14
|
-
import type {
|
|
15
|
-
import type { PeerInfo } from '@libp2p/interface/peer-info'
|
|
16
|
-
import type { PeerRouting } from '@libp2p/interface/peer-routing'
|
|
17
|
-
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
18
|
-
|
|
19
|
-
const log = logger('libp2p:peer-routing')
|
|
12
|
+
import type { AbortOptions, Logger, PeerId, PeerInfo, PeerRouting, PeerStore } from '@libp2p/interface'
|
|
13
|
+
import type { ComponentLogger } from '@libp2p/logger'
|
|
20
14
|
|
|
21
15
|
export interface PeerRoutingInit {
|
|
22
16
|
routers?: PeerRouting[]
|
|
@@ -25,14 +19,19 @@ export interface PeerRoutingInit {
|
|
|
25
19
|
export interface DefaultPeerRoutingComponents {
|
|
26
20
|
peerId: PeerId
|
|
27
21
|
peerStore: PeerStore
|
|
22
|
+
logger: ComponentLogger
|
|
28
23
|
}
|
|
29
24
|
|
|
30
25
|
export class DefaultPeerRouting implements PeerRouting {
|
|
31
|
-
private readonly
|
|
26
|
+
private readonly log: Logger
|
|
27
|
+
private readonly peerId: PeerId
|
|
28
|
+
private readonly peerStore: PeerStore
|
|
32
29
|
private readonly routers: PeerRouting[]
|
|
33
30
|
|
|
34
31
|
constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
|
|
35
|
-
this.
|
|
32
|
+
this.log = components.logger.forComponent('libp2p:peer-routing')
|
|
33
|
+
this.peerId = components.peerId
|
|
34
|
+
this.peerStore = components.peerStore
|
|
36
35
|
this.routers = init.routers ?? []
|
|
37
36
|
}
|
|
38
37
|
|
|
@@ -44,22 +43,24 @@ export class DefaultPeerRouting implements PeerRouting {
|
|
|
44
43
|
throw new CodeError('No peer routers available', codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
45
44
|
}
|
|
46
45
|
|
|
47
|
-
if (id.toString() === this.
|
|
46
|
+
if (id.toString() === this.peerId.toString()) {
|
|
48
47
|
throw new CodeError('Should not try to find self', codes.ERR_FIND_SELF)
|
|
49
48
|
}
|
|
50
49
|
|
|
50
|
+
const self = this
|
|
51
|
+
|
|
51
52
|
const output = await pipe(
|
|
52
53
|
merge(
|
|
53
54
|
...this.routers.map(router => (async function * () {
|
|
54
55
|
try {
|
|
55
56
|
yield await router.findPeer(id, options)
|
|
56
57
|
} catch (err) {
|
|
57
|
-
log.error(err)
|
|
58
|
+
self.log.error(err)
|
|
58
59
|
}
|
|
59
60
|
})())
|
|
60
61
|
),
|
|
61
62
|
(source) => filter(source, Boolean),
|
|
62
|
-
(source) => storeAddresses(source, this.
|
|
63
|
+
(source) => storeAddresses(source, this.peerStore),
|
|
63
64
|
async (source) => first(source)
|
|
64
65
|
)
|
|
65
66
|
|
|
@@ -82,7 +83,7 @@ export class DefaultPeerRouting implements PeerRouting {
|
|
|
82
83
|
merge(
|
|
83
84
|
...this.routers.map(router => router.getClosestPeers(key, options))
|
|
84
85
|
),
|
|
85
|
-
(source) => storeAddresses(source, this.
|
|
86
|
+
(source) => storeAddresses(source, this.peerStore),
|
|
86
87
|
(source) => uniquePeers(source),
|
|
87
88
|
(source) => requirePeers(source)
|
|
88
89
|
)
|
package/src/registrar.ts
CHANGED
|
@@ -1,16 +1,9 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError } from '@libp2p/interface'
|
|
3
2
|
import merge from 'merge-options'
|
|
4
3
|
import { codes } from './errors.js'
|
|
5
|
-
import type { IdentifyResult, Libp2pEvents, PeerUpdate } from '@libp2p/interface'
|
|
6
|
-
import type {
|
|
7
|
-
import type {
|
|
8
|
-
import type { PeerStore } from '@libp2p/interface/peer-store'
|
|
9
|
-
import type { Topology } from '@libp2p/interface/topology'
|
|
10
|
-
import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
|
|
11
|
-
import type { StreamHandlerOptions, StreamHandlerRecord, Registrar, StreamHandler } from '@libp2p/interface-internal/registrar'
|
|
12
|
-
|
|
13
|
-
const log = logger('libp2p:registrar')
|
|
4
|
+
import type { IdentifyResult, Libp2pEvents, Logger, PeerUpdate, TypedEventTarget, PeerId, PeerStore, Topology } from '@libp2p/interface'
|
|
5
|
+
import type { ConnectionManager, StreamHandlerOptions, StreamHandlerRecord, Registrar, StreamHandler } from '@libp2p/interface-internal'
|
|
6
|
+
import type { ComponentLogger } from '@libp2p/logger'
|
|
14
7
|
|
|
15
8
|
export const DEFAULT_MAX_INBOUND_STREAMS = 32
|
|
16
9
|
export const DEFAULT_MAX_OUTBOUND_STREAMS = 64
|
|
@@ -20,17 +13,20 @@ export interface RegistrarComponents {
|
|
|
20
13
|
connectionManager: ConnectionManager
|
|
21
14
|
peerStore: PeerStore
|
|
22
15
|
events: TypedEventTarget<Libp2pEvents>
|
|
16
|
+
logger: ComponentLogger
|
|
23
17
|
}
|
|
24
18
|
|
|
25
19
|
/**
|
|
26
20
|
* Responsible for notifying registered protocols of events in the network.
|
|
27
21
|
*/
|
|
28
22
|
export class DefaultRegistrar implements Registrar {
|
|
23
|
+
private readonly log: Logger
|
|
29
24
|
private readonly topologies: Map<string, Map<string, Topology>>
|
|
30
25
|
private readonly handlers: Map<string, StreamHandlerRecord>
|
|
31
26
|
private readonly components: RegistrarComponents
|
|
32
27
|
|
|
33
28
|
constructor (components: RegistrarComponents) {
|
|
29
|
+
this.log = components.logger.forComponent('libp2p:registrar')
|
|
34
30
|
this.topologies = new Map()
|
|
35
31
|
this.handlers = new Map()
|
|
36
32
|
this.components = components
|
|
@@ -178,7 +174,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
178
174
|
return
|
|
179
175
|
}
|
|
180
176
|
|
|
181
|
-
log.error('could not inform topologies of disconnecting peer %p', remotePeer, err)
|
|
177
|
+
this.log.error('could not inform topologies of disconnecting peer %p', remotePeer, err)
|
|
182
178
|
})
|
|
183
179
|
}
|
|
184
180
|
|
package/src/transport-manager.ts
CHANGED
|
@@ -1,20 +1,10 @@
|
|
|
1
|
-
import { CodeError } from '@libp2p/interface
|
|
2
|
-
import { trackedMap } from '@libp2p/
|
|
3
|
-
import { FaultTolerance } from '@libp2p/interface/transport'
|
|
4
|
-
import { logger } from '@libp2p/logger'
|
|
1
|
+
import { CodeError, FaultTolerance } from '@libp2p/interface'
|
|
2
|
+
import { trackedMap } from '@libp2p/utils/tracked-map'
|
|
5
3
|
import { codes } from './errors.js'
|
|
6
|
-
import type { Libp2pEvents, AbortOptions } from '@libp2p/interface'
|
|
7
|
-
import type {
|
|
8
|
-
import type { TypedEventTarget } from '@libp2p/interface/events'
|
|
9
|
-
import type { Metrics } from '@libp2p/interface/metrics'
|
|
10
|
-
import type { Startable } from '@libp2p/interface/startable'
|
|
11
|
-
import type { Listener, Transport, Upgrader } from '@libp2p/interface/transport'
|
|
12
|
-
import type { AddressManager } from '@libp2p/interface-internal/address-manager'
|
|
13
|
-
import type { TransportManager } from '@libp2p/interface-internal/transport-manager'
|
|
4
|
+
import type { Libp2pEvents, AbortOptions, ComponentLogger, Logger, Connection, TypedEventTarget, Metrics, Startable, Listener, Transport, Upgrader } from '@libp2p/interface'
|
|
5
|
+
import type { AddressManager, TransportManager } from '@libp2p/interface-internal'
|
|
14
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
15
7
|
|
|
16
|
-
const log = logger('libp2p:transports')
|
|
17
|
-
|
|
18
8
|
export interface TransportManagerInit {
|
|
19
9
|
faultTolerance?: FaultTolerance
|
|
20
10
|
}
|
|
@@ -24,9 +14,11 @@ export interface DefaultTransportManagerComponents {
|
|
|
24
14
|
addressManager: AddressManager
|
|
25
15
|
upgrader: Upgrader
|
|
26
16
|
events: TypedEventTarget<Libp2pEvents>
|
|
17
|
+
logger: ComponentLogger
|
|
27
18
|
}
|
|
28
19
|
|
|
29
20
|
export class DefaultTransportManager implements TransportManager, Startable {
|
|
21
|
+
private readonly log: Logger
|
|
30
22
|
private readonly components: DefaultTransportManagerComponents
|
|
31
23
|
private readonly transports: Map<string, Transport>
|
|
32
24
|
private readonly listeners: Map<string, Listener[]>
|
|
@@ -34,6 +26,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
34
26
|
private started: boolean
|
|
35
27
|
|
|
36
28
|
constructor (components: DefaultTransportManagerComponents, init: TransportManagerInit = {}) {
|
|
29
|
+
this.log = components.logger.forComponent('libp2p:transports')
|
|
37
30
|
this.components = components
|
|
38
31
|
this.started = false
|
|
39
32
|
this.transports = new Map<string, Transport>()
|
|
@@ -58,7 +51,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
58
51
|
throw new CodeError(`There is already a transport with the tag ${tag}`, codes.ERR_DUPLICATE_TRANSPORT)
|
|
59
52
|
}
|
|
60
53
|
|
|
61
|
-
log('adding transport %s', tag)
|
|
54
|
+
this.log('adding transport %s', tag)
|
|
62
55
|
|
|
63
56
|
this.transports.set(tag, transport)
|
|
64
57
|
|
|
@@ -88,7 +81,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
88
81
|
async stop (): Promise<void> {
|
|
89
82
|
const tasks = []
|
|
90
83
|
for (const [key, listeners] of this.listeners) {
|
|
91
|
-
log('closing listeners for %s', key)
|
|
84
|
+
this.log('closing listeners for %s', key)
|
|
92
85
|
while (listeners.length > 0) {
|
|
93
86
|
const listener = listeners.pop()
|
|
94
87
|
|
|
@@ -101,7 +94,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
101
94
|
}
|
|
102
95
|
|
|
103
96
|
await Promise.all(tasks)
|
|
104
|
-
log('all listeners closed')
|
|
97
|
+
this.log('all listeners closed')
|
|
105
98
|
for (const key of this.listeners.keys()) {
|
|
106
99
|
this.listeners.set(key, [])
|
|
107
100
|
}
|
|
@@ -182,7 +175,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
182
175
|
}
|
|
183
176
|
|
|
184
177
|
if (addrs == null || addrs.length === 0) {
|
|
185
|
-
log('no addresses were provided for listening, this node is dial only')
|
|
178
|
+
this.log('no addresses were provided for listening, this node is dial only')
|
|
186
179
|
return
|
|
187
180
|
}
|
|
188
181
|
|
|
@@ -194,7 +187,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
194
187
|
|
|
195
188
|
// For each supported multiaddr, create a listener
|
|
196
189
|
for (const addr of supportedAddrs) {
|
|
197
|
-
log('creating listener for %s on %a', key, addr)
|
|
190
|
+
this.log('creating listener for %s on %a', key, addr)
|
|
198
191
|
const listener = transport.createListener({
|
|
199
192
|
upgrader: this.components.upgrader
|
|
200
193
|
})
|
|
@@ -253,7 +246,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
253
246
|
if (this.faultTolerance === FaultTolerance.FATAL_ALL) {
|
|
254
247
|
throw new CodeError(message, codes.ERR_NO_VALID_ADDRESSES)
|
|
255
248
|
}
|
|
256
|
-
log(`libp2p in dial mode only: ${message}`)
|
|
249
|
+
this.log(`libp2p in dial mode only: ${message}`)
|
|
257
250
|
}
|
|
258
251
|
}
|
|
259
252
|
|
|
@@ -263,11 +256,11 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
263
256
|
*/
|
|
264
257
|
async remove (key: string): Promise<void> {
|
|
265
258
|
const listeners = this.listeners.get(key) ?? []
|
|
266
|
-
log.trace('removing transport %s', key)
|
|
259
|
+
this.log.trace('removing transport %s', key)
|
|
267
260
|
|
|
268
261
|
// Close any running listeners
|
|
269
262
|
const tasks = []
|
|
270
|
-
log.trace('closing listeners for %s', key)
|
|
263
|
+
this.log.trace('closing listeners for %s', key)
|
|
271
264
|
while (listeners.length > 0) {
|
|
272
265
|
const listener = listeners.pop()
|
|
273
266
|
|