libp2p 0.39.5 → 0.40.0-030dbc8
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 +14 -2
- package/dist/src/address-manager/index.d.ts +18 -2
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +19 -6
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts +2 -2
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +12 -12
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/index.d.ts +13 -3
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +29 -34
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +22 -7
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +26 -17
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/components.d.ts +110 -0
- package/dist/src/components.d.ts.map +1 -0
- package/dist/src/components.js +199 -0
- package/dist/src/components.js.map +1 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -11
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +9 -2
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +6 -6
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +12 -6
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +12 -17
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +41 -17
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -81
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +7 -2
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js +4 -4
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/content-routing/utils.d.ts +1 -1
- package/dist/src/content-routing/utils.d.ts.map +1 -1
- package/dist/src/errors.d.ts +2 -1
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +1 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts +7 -3
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +18 -6
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +24 -35
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +13 -3
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +41 -41
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js +23 -34
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +13 -12
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts +2 -8
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -3
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +24 -29
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +6 -2
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +19 -19
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +1 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +49 -73
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/nat-manager.d.ts +9 -2
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +3 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +11 -2
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-record-updater.js +10 -10
- package/dist/src/peer-record-updater.js.map +1 -1
- package/dist/src/peer-routing.d.ts +6 -2
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +4 -4
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +7 -3
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +3 -3
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +2 -17
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +4 -1
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +2 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +4 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +9 -3
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +6 -6
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +9 -3
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +5 -6
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +17 -5
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +55 -75
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +39 -40
- package/src/address-manager/index.ts +34 -10
- package/src/circuit/README.md +6 -6
- package/src/circuit/auto-relay.ts +15 -15
- package/src/circuit/index.ts +16 -5
- package/src/circuit/pb/index.ts +29 -35
- package/src/circuit/transport.ts +49 -20
- package/src/components.ts +332 -0
- package/src/config.ts +0 -11
- package/src/connection-manager/auto-dialler.ts +17 -9
- package/src/connection-manager/dialer/index.ts +26 -25
- package/src/connection-manager/index.ts +136 -93
- package/src/content-routing/index.ts +13 -7
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +28 -10
- package/src/fetch/pb/proto.ts +24 -35
- package/src/identify/index.ts +57 -53
- package/src/identify/pb/message.ts +23 -35
- package/src/index.ts +12 -11
- package/src/insecure/index.ts +6 -3
- package/src/insecure/pb/proto.ts +24 -29
- package/src/keychain/index.ts +27 -22
- package/src/libp2p.ts +50 -79
- package/src/nat-manager.ts +14 -6
- package/src/peer-record-updater.ts +23 -13
- package/src/peer-routing.ts +12 -7
- package/src/ping/index.ts +12 -7
- package/src/pnet/index.ts +5 -1
- package/src/pubsub/dummy-pubsub.ts +3 -1
- package/src/registrar.ts +17 -10
- package/src/transport-manager.ts +16 -10
- package/src/upgrader.ts +78 -87
- package/src/version.ts +1 -1
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts +0 -16
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/connection-manager/dialer/auto-dialer.js +0 -43
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +0 -1
- package/dist/src/metrics/index.d.ts +0 -93
- package/dist/src/metrics/index.d.ts.map +0 -1
- package/dist/src/metrics/index.js +0 -234
- package/dist/src/metrics/index.js.map +0 -1
- package/dist/src/metrics/moving-average.d.ts +0 -14
- package/dist/src/metrics/moving-average.d.ts.map +0 -1
- package/dist/src/metrics/moving-average.js +0 -40
- package/dist/src/metrics/moving-average.js.map +0 -1
- package/dist/src/metrics/stats.d.ts +0 -87
- package/dist/src/metrics/stats.d.ts.map +0 -1
- package/dist/src/metrics/stats.js +0 -183
- package/dist/src/metrics/stats.js.map +0 -1
- package/src/connection-manager/dialer/auto-dialer.ts +0 -65
- package/src/metrics/index.ts +0 -311
- package/src/metrics/moving-average.ts +0 -53
- package/src/metrics/stats.ts +0 -238
|
@@ -1,16 +1,26 @@
|
|
|
1
1
|
import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
|
|
2
|
-
import type { Components } from '@libp2p/components'
|
|
3
2
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
4
3
|
import { logger } from '@libp2p/logger'
|
|
5
4
|
import { protocols } from '@multiformats/multiaddr'
|
|
5
|
+
import type { TransportManager } from '@libp2p/interface-transport'
|
|
6
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
7
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
6
9
|
|
|
7
10
|
const log = logger('libp2p:peer-record-updater')
|
|
8
11
|
|
|
12
|
+
export interface PeerRecordUpdaterComponents {
|
|
13
|
+
peerId: PeerId
|
|
14
|
+
peerStore: PeerStore
|
|
15
|
+
transportManager: TransportManager
|
|
16
|
+
addressManager: AddressManager
|
|
17
|
+
}
|
|
18
|
+
|
|
9
19
|
export class PeerRecordUpdater implements Startable {
|
|
10
|
-
private readonly components:
|
|
20
|
+
private readonly components: PeerRecordUpdaterComponents
|
|
11
21
|
private started: boolean
|
|
12
22
|
|
|
13
|
-
constructor (components:
|
|
23
|
+
constructor (components: PeerRecordUpdaterComponents) {
|
|
14
24
|
this.components = components
|
|
15
25
|
this.started = false
|
|
16
26
|
this.update = this.update.bind(this)
|
|
@@ -22,16 +32,16 @@ export class PeerRecordUpdater implements Startable {
|
|
|
22
32
|
|
|
23
33
|
async start () {
|
|
24
34
|
this.started = true
|
|
25
|
-
this.components.
|
|
26
|
-
this.components.
|
|
27
|
-
this.components.
|
|
35
|
+
this.components.transportManager.addEventListener('listener:listening', this.update)
|
|
36
|
+
this.components.transportManager.addEventListener('listener:close', this.update)
|
|
37
|
+
this.components.addressManager.addEventListener('change:addresses', this.update)
|
|
28
38
|
}
|
|
29
39
|
|
|
30
40
|
async stop () {
|
|
31
41
|
this.started = false
|
|
32
|
-
this.components.
|
|
33
|
-
this.components.
|
|
34
|
-
this.components.
|
|
42
|
+
this.components.transportManager.removeEventListener('listener:listening', this.update)
|
|
43
|
+
this.components.transportManager.removeEventListener('listener:close', this.update)
|
|
44
|
+
this.components.addressManager.removeEventListener('change:addresses', this.update)
|
|
35
45
|
}
|
|
36
46
|
|
|
37
47
|
/**
|
|
@@ -41,12 +51,12 @@ export class PeerRecordUpdater implements Startable {
|
|
|
41
51
|
Promise.resolve()
|
|
42
52
|
.then(async () => {
|
|
43
53
|
const peerRecord = new PeerRecord({
|
|
44
|
-
peerId: this.components.
|
|
45
|
-
multiaddrs: this.components.
|
|
54
|
+
peerId: this.components.peerId,
|
|
55
|
+
multiaddrs: this.components.addressManager.getAddresses().map(ma => ma.decapsulateCode(protocols('p2p').code))
|
|
46
56
|
})
|
|
47
57
|
|
|
48
|
-
const envelope = await RecordEnvelope.seal(peerRecord, this.components.
|
|
49
|
-
await this.components.
|
|
58
|
+
const envelope = await RecordEnvelope.seal(peerRecord, this.components.peerId)
|
|
59
|
+
await this.components.peerStore.addressBook.consumePeerRecord(envelope)
|
|
50
60
|
})
|
|
51
61
|
.catch(err => {
|
|
52
62
|
log.error('Could not update self peer record: %o', err)
|
package/src/peer-routing.ts
CHANGED
|
@@ -23,7 +23,7 @@ import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
|
23
23
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
24
24
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
25
25
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
26
|
-
import type {
|
|
26
|
+
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
27
27
|
|
|
28
28
|
const log = logger('libp2p:peer-routing')
|
|
29
29
|
|
|
@@ -54,15 +54,20 @@ export interface PeerRoutingInit {
|
|
|
54
54
|
refreshManager?: RefreshManagerInit
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
export interface DefaultPeerRoutingComponents {
|
|
58
|
+
peerId: PeerId
|
|
59
|
+
peerStore: PeerStore
|
|
60
|
+
}
|
|
61
|
+
|
|
57
62
|
export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
58
|
-
private readonly components:
|
|
63
|
+
private readonly components: DefaultPeerRoutingComponents
|
|
59
64
|
private readonly routers: PeerRouting[]
|
|
60
65
|
private readonly refreshManagerInit: RefreshManagerInit
|
|
61
66
|
private timeoutId?: ReturnType<typeof setTimeout>
|
|
62
67
|
private started: boolean
|
|
63
68
|
private abortController?: TimeoutController
|
|
64
69
|
|
|
65
|
-
constructor (components:
|
|
70
|
+
constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
|
|
66
71
|
this.components = components
|
|
67
72
|
this.routers = init.routers
|
|
68
73
|
this.refreshManagerInit = init.refreshManager ?? {}
|
|
@@ -110,7 +115,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
110
115
|
} catch {}
|
|
111
116
|
|
|
112
117
|
// nb getClosestPeers adds the addresses to the address book
|
|
113
|
-
await drain(this.getClosestPeers(this.components.
|
|
118
|
+
await drain(this.getClosestPeers(this.components.peerId.toBytes(), { signal: this.abortController.signal }))
|
|
114
119
|
} catch (err: any) {
|
|
115
120
|
log.error(err)
|
|
116
121
|
} finally {
|
|
@@ -139,7 +144,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
139
144
|
throw errCode(new Error('No peer routers available'), codes.ERR_NO_ROUTERS_AVAILABLE)
|
|
140
145
|
}
|
|
141
146
|
|
|
142
|
-
if (id.toString() === this.components.
|
|
147
|
+
if (id.toString() === this.components.peerId.toString()) {
|
|
143
148
|
throw errCode(new Error('Should not try to find self'), codes.ERR_FIND_SELF)
|
|
144
149
|
}
|
|
145
150
|
|
|
@@ -154,7 +159,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
154
159
|
})())
|
|
155
160
|
),
|
|
156
161
|
(source) => filter(source, Boolean),
|
|
157
|
-
(source) => storeAddresses(source, this.components.
|
|
162
|
+
(source) => storeAddresses(source, this.components.peerStore),
|
|
158
163
|
async (source) => await first(source)
|
|
159
164
|
)
|
|
160
165
|
|
|
@@ -177,7 +182,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
|
|
|
177
182
|
merge(
|
|
178
183
|
...this.routers.map(router => router.getClosestPeers(key, options))
|
|
179
184
|
),
|
|
180
|
-
(source) => storeAddresses(source, this.components.
|
|
185
|
+
(source) => storeAddresses(source, this.components.peerStore),
|
|
181
186
|
(source) => uniquePeers(source),
|
|
182
187
|
(source) => requirePeers(source)
|
|
183
188
|
)
|
package/src/ping/index.ts
CHANGED
|
@@ -6,15 +6,15 @@ import { pipe } from 'it-pipe'
|
|
|
6
6
|
import first from 'it-first'
|
|
7
7
|
import { equals as uint8ArrayEquals } from 'uint8arrays/equals'
|
|
8
8
|
import { PROTOCOL_NAME, PING_LENGTH, PROTOCOL_VERSION } from './constants.js'
|
|
9
|
-
import type { IncomingStreamData } from '@libp2p/interface-registrar'
|
|
9
|
+
import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
10
10
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
11
11
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
12
|
-
import type { Components } from '@libp2p/components'
|
|
13
12
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
14
13
|
import { abortableDuplex } from 'abortable-iterator'
|
|
15
14
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
16
15
|
import type { Stream } from '@libp2p/interface-connection'
|
|
17
16
|
import { setMaxListeners } from 'events'
|
|
17
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
18
18
|
|
|
19
19
|
const log = logger('libp2p:ping')
|
|
20
20
|
|
|
@@ -29,13 +29,18 @@ export interface PingServiceInit {
|
|
|
29
29
|
timeout: number
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
export interface PingServiceComponents {
|
|
33
|
+
registrar: Registrar
|
|
34
|
+
connectionManager: ConnectionManager
|
|
35
|
+
}
|
|
36
|
+
|
|
32
37
|
export class PingService implements Startable {
|
|
33
38
|
public readonly protocol: string
|
|
34
|
-
private readonly components:
|
|
39
|
+
private readonly components: PingServiceComponents
|
|
35
40
|
private started: boolean
|
|
36
41
|
private readonly init: PingServiceInit
|
|
37
42
|
|
|
38
|
-
constructor (components:
|
|
43
|
+
constructor (components: PingServiceComponents, init: PingServiceInit) {
|
|
39
44
|
this.components = components
|
|
40
45
|
this.started = false
|
|
41
46
|
this.protocol = `/${init.protocolPrefix}/${PROTOCOL_NAME}/${PROTOCOL_VERSION}`
|
|
@@ -43,7 +48,7 @@ export class PingService implements Startable {
|
|
|
43
48
|
}
|
|
44
49
|
|
|
45
50
|
async start () {
|
|
46
|
-
await this.components.
|
|
51
|
+
await this.components.registrar.handle(this.protocol, this.handleMessage, {
|
|
47
52
|
maxInboundStreams: this.init.maxInboundStreams,
|
|
48
53
|
maxOutboundStreams: this.init.maxOutboundStreams
|
|
49
54
|
})
|
|
@@ -51,7 +56,7 @@ export class PingService implements Startable {
|
|
|
51
56
|
}
|
|
52
57
|
|
|
53
58
|
async stop () {
|
|
54
|
-
await this.components.
|
|
59
|
+
await this.components.registrar.unhandle(this.protocol)
|
|
55
60
|
this.started = false
|
|
56
61
|
}
|
|
57
62
|
|
|
@@ -82,7 +87,7 @@ export class PingService implements Startable {
|
|
|
82
87
|
|
|
83
88
|
const start = Date.now()
|
|
84
89
|
const data = randomBytes(PING_LENGTH)
|
|
85
|
-
const connection = await this.components.
|
|
90
|
+
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
86
91
|
let timeoutController
|
|
87
92
|
let signal = options.signal
|
|
88
93
|
let stream: Stream | undefined
|
package/src/pnet/index.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface ProtectorInit {
|
|
|
24
24
|
psk: Uint8Array
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
|
|
27
|
+
class PreSharedKeyConnectionProtector implements ConnectionProtector {
|
|
28
28
|
public tag: string
|
|
29
29
|
private readonly psk: Uint8Array
|
|
30
30
|
private readonly enabled: boolean
|
|
@@ -96,3 +96,7 @@ export class PreSharedKeyConnectionProtector implements ConnectionProtector {
|
|
|
96
96
|
}
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
|
+
|
|
100
|
+
export function preSharedKey (init: ProtectorInit): () => ConnectionProtector {
|
|
101
|
+
return () => new PreSharedKeyConnectionProtector(init)
|
|
102
|
+
}
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { EventEmitter } from '@libp2p/interfaces/events'
|
|
2
2
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
3
|
-
import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign } from '@libp2p/interface-pubsub'
|
|
3
|
+
import type { PublishResult, PubSub, PubSubEvents, StrictNoSign, StrictSign, TopicValidatorFn } from '@libp2p/interface-pubsub'
|
|
4
4
|
import errCode from 'err-code'
|
|
5
5
|
import { messages, codes } from '../errors.js'
|
|
6
6
|
|
|
7
7
|
export class DummyPubSub extends EventEmitter<PubSubEvents> implements PubSub {
|
|
8
|
+
public topicValidators = new Map<string, TopicValidatorFn>()
|
|
9
|
+
|
|
8
10
|
isStarted (): boolean {
|
|
9
11
|
return false
|
|
10
12
|
}
|
package/src/registrar.ts
CHANGED
|
@@ -4,24 +4,31 @@ import { codes } from './errors.js'
|
|
|
4
4
|
import { isTopology, StreamHandlerOptions, StreamHandlerRecord } from '@libp2p/interface-registrar'
|
|
5
5
|
import merge from 'merge-options'
|
|
6
6
|
import type { Registrar, StreamHandler, Topology } from '@libp2p/interface-registrar'
|
|
7
|
-
import type { PeerProtocolsChangeData } from '@libp2p/interface-peer-store'
|
|
7
|
+
import type { PeerProtocolsChangeData, PeerStore } from '@libp2p/interface-peer-store'
|
|
8
8
|
import type { Connection } from '@libp2p/interface-connection'
|
|
9
|
-
import type {
|
|
9
|
+
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
10
|
+
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
10
11
|
|
|
11
12
|
const log = logger('libp2p:registrar')
|
|
12
13
|
|
|
13
14
|
export const DEFAULT_MAX_INBOUND_STREAMS = 32
|
|
14
15
|
export const DEFAULT_MAX_OUTBOUND_STREAMS = 64
|
|
15
16
|
|
|
17
|
+
export interface RegistrarComponents {
|
|
18
|
+
peerId: PeerId
|
|
19
|
+
connectionManager: ConnectionManager
|
|
20
|
+
peerStore: PeerStore
|
|
21
|
+
}
|
|
22
|
+
|
|
16
23
|
/**
|
|
17
24
|
* Responsible for notifying registered protocols of events in the network.
|
|
18
25
|
*/
|
|
19
26
|
export class DefaultRegistrar implements Registrar {
|
|
20
27
|
private readonly topologies: Map<string, Map<string, Topology>>
|
|
21
28
|
private readonly handlers: Map<string, StreamHandlerRecord>
|
|
22
|
-
private readonly components:
|
|
29
|
+
private readonly components: RegistrarComponents
|
|
23
30
|
|
|
24
|
-
constructor (components:
|
|
31
|
+
constructor (components: RegistrarComponents) {
|
|
25
32
|
this.topologies = new Map()
|
|
26
33
|
this.handlers = new Map()
|
|
27
34
|
this.components = components
|
|
@@ -29,10 +36,10 @@ export class DefaultRegistrar implements Registrar {
|
|
|
29
36
|
this._onDisconnect = this._onDisconnect.bind(this)
|
|
30
37
|
this._onProtocolChange = this._onProtocolChange.bind(this)
|
|
31
38
|
|
|
32
|
-
this.components.
|
|
39
|
+
this.components.connectionManager.addEventListener('peer:disconnect', this._onDisconnect)
|
|
33
40
|
|
|
34
41
|
// happens after identify
|
|
35
|
-
this.components.
|
|
42
|
+
this.components.peerStore.addEventListener('change:protocols', this._onProtocolChange)
|
|
36
43
|
}
|
|
37
44
|
|
|
38
45
|
getProtocols () {
|
|
@@ -83,7 +90,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
83
90
|
})
|
|
84
91
|
|
|
85
92
|
// Add new protocols to self protocols in the Protobook
|
|
86
|
-
await this.components.
|
|
93
|
+
await this.components.peerStore.protoBook.add(this.components.peerId, [protocol])
|
|
87
94
|
}
|
|
88
95
|
|
|
89
96
|
/**
|
|
@@ -98,7 +105,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
98
105
|
})
|
|
99
106
|
|
|
100
107
|
// Remove protocols from self protocols in the Protobook
|
|
101
|
-
await this.components.
|
|
108
|
+
await this.components.peerStore.protoBook.remove(this.components.peerId, protocolList)
|
|
102
109
|
}
|
|
103
110
|
|
|
104
111
|
/**
|
|
@@ -149,7 +156,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
149
156
|
_onDisconnect (evt: CustomEvent<Connection>) {
|
|
150
157
|
const connection = evt.detail
|
|
151
158
|
|
|
152
|
-
void this.components.
|
|
159
|
+
void this.components.peerStore.protoBook.get(connection.remotePeer)
|
|
153
160
|
.then(peerProtocols => {
|
|
154
161
|
for (const protocol of peerProtocols) {
|
|
155
162
|
const topologies = this.topologies.get(protocol)
|
|
@@ -200,7 +207,7 @@ export class DefaultRegistrar implements Registrar {
|
|
|
200
207
|
}
|
|
201
208
|
|
|
202
209
|
for (const topology of topologies.values()) {
|
|
203
|
-
const connection = this.components.
|
|
210
|
+
const connection = this.components.connectionManager.getConnections(peerId)[0]
|
|
204
211
|
|
|
205
212
|
if (connection == null) {
|
|
206
213
|
continue
|
package/src/transport-manager.ts
CHANGED
|
@@ -2,14 +2,15 @@ import { logger } from '@libp2p/logger'
|
|
|
2
2
|
import pSettle from 'p-settle'
|
|
3
3
|
import { codes } from './errors.js'
|
|
4
4
|
import errCode from 'err-code'
|
|
5
|
-
import type { Listener, Transport, TransportManager, TransportManagerEvents } from '@libp2p/interface-transport'
|
|
5
|
+
import type { Listener, Transport, TransportManager, TransportManagerEvents, Upgrader } from '@libp2p/interface-transport'
|
|
6
6
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
7
7
|
import type { Connection } from '@libp2p/interface-connection'
|
|
8
8
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
9
9
|
import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
|
|
10
10
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
11
|
-
import type { Components } from '@libp2p/components'
|
|
12
11
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
12
|
+
import type { Metrics } from '@libp2p/interface-metrics'
|
|
13
|
+
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
13
14
|
|
|
14
15
|
const log = logger('libp2p:transports')
|
|
15
16
|
|
|
@@ -17,23 +18,28 @@ export interface TransportManagerInit {
|
|
|
17
18
|
faultTolerance?: FaultTolerance
|
|
18
19
|
}
|
|
19
20
|
|
|
21
|
+
export interface DefaultTransportManagerComponents {
|
|
22
|
+
metrics?: Metrics
|
|
23
|
+
addressManager: AddressManager
|
|
24
|
+
upgrader: Upgrader
|
|
25
|
+
}
|
|
26
|
+
|
|
20
27
|
export class DefaultTransportManager extends EventEmitter<TransportManagerEvents> implements TransportManager, Startable {
|
|
21
|
-
private readonly components:
|
|
28
|
+
private readonly components: DefaultTransportManagerComponents
|
|
22
29
|
private readonly transports: Map<string, Transport>
|
|
23
30
|
private readonly listeners: Map<string, Listener[]>
|
|
24
31
|
private readonly faultTolerance: FaultTolerance
|
|
25
32
|
private started: boolean
|
|
26
33
|
|
|
27
|
-
constructor (components:
|
|
34
|
+
constructor (components: DefaultTransportManagerComponents, init: TransportManagerInit = {}) {
|
|
28
35
|
super()
|
|
29
36
|
|
|
30
37
|
this.components = components
|
|
31
38
|
this.started = false
|
|
32
39
|
this.transports = new Map<string, Transport>()
|
|
33
40
|
this.listeners = trackedMap({
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
metrics: this.components.getMetrics()
|
|
41
|
+
name: 'libp2p_transport_manager_listeners',
|
|
42
|
+
metrics: this.components.metrics
|
|
37
43
|
})
|
|
38
44
|
this.faultTolerance = init.faultTolerance ?? FaultTolerance.FATAL_ALL
|
|
39
45
|
}
|
|
@@ -67,7 +73,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|
|
67
73
|
|
|
68
74
|
async start () {
|
|
69
75
|
// Listen on the provided transports for the provided addresses
|
|
70
|
-
const addrs = this.components.
|
|
76
|
+
const addrs = this.components.addressManager.getListenAddrs()
|
|
71
77
|
|
|
72
78
|
await this.listen(addrs)
|
|
73
79
|
|
|
@@ -114,7 +120,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|
|
114
120
|
try {
|
|
115
121
|
return await transport.dial(ma, {
|
|
116
122
|
...options,
|
|
117
|
-
upgrader: this.components.
|
|
123
|
+
upgrader: this.components.upgrader
|
|
118
124
|
})
|
|
119
125
|
} catch (err: any) {
|
|
120
126
|
if (err.code == null) {
|
|
@@ -177,7 +183,7 @@ export class DefaultTransportManager extends EventEmitter<TransportManagerEvents
|
|
|
177
183
|
for (const addr of supportedAddrs) {
|
|
178
184
|
log('creating listener for %s on %s', key, addr)
|
|
179
185
|
const listener = transport.createListener({
|
|
180
|
-
upgrader: this.components.
|
|
186
|
+
upgrader: this.components.upgrader
|
|
181
187
|
})
|
|
182
188
|
|
|
183
189
|
let listeners = this.listeners.get(key)
|