libp2p 0.36.2-fab4f13 → 0.37.0-35f9c0c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/address-manager/index.d.ts +2 -1
- package/dist/src/address-manager/index.d.ts.map +1 -1
- package/dist/src/address-manager/index.js +1 -1
- package/dist/src/address-manager/index.js.map +1 -1
- package/dist/src/circuit/auto-relay.d.ts.map +1 -1
- package/dist/src/circuit/auto-relay.js +7 -6
- package/dist/src/circuit/auto-relay.js.map +1 -1
- package/dist/src/circuit/circuit/hop.d.ts +1 -1
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +4 -4
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/index.d.ts +1 -1
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/listener.d.ts +3 -3
- package/dist/src/circuit/listener.d.ts.map +1 -1
- package/dist/src/circuit/listener.js +10 -2
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +5 -4
- package/dist/src/circuit/pb/index.d.ts.map +1 -1
- package/dist/src/circuit/pb/index.js +28 -2
- package/dist/src/circuit/pb/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +7 -5
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +5 -7
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts +1 -1
- package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dialler.js +4 -4
- package/dist/src/connection-manager/auto-dialler.js.map +1 -1
- package/dist/src/{dialer → connection-manager/dialer}/auto-dialer.d.ts +2 -0
- package/dist/src/connection-manager/dialer/auto-dialer.d.ts.map +1 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js +37 -0
- package/dist/src/connection-manager/dialer/auto-dialer.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.d.ts +2 -2
- package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/dial-request.js +29 -4
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +37 -8
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -23
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +57 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +80 -42
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts +1 -1
- package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
- package/dist/src/connection-manager/latency-monitor.js +1 -1
- package/dist/src/connection-manager/latency-monitor.js.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js +1 -1
- package/dist/src/connection-manager/visibility-change-emitter.js.map +1 -1
- package/dist/src/content-routing/index.d.ts +2 -1
- package/dist/src/content-routing/index.d.ts.map +1 -1
- package/dist/src/content-routing/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +4 -1
- package/dist/src/dht/dummy-dht.d.ts.map +1 -1
- package/dist/src/dht/dummy-dht.js +8 -1
- package/dist/src/dht/dummy-dht.js.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 +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +1 -1
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +4 -3
- package/dist/src/fetch/pb/proto.d.ts.map +1 -1
- package/dist/src/fetch/pb/proto.js +7 -1
- package/dist/src/fetch/pb/proto.js.map +1 -1
- package/dist/src/identify/index.d.ts +1 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +4 -4
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +2 -1
- package/dist/src/identify/pb/message.d.ts.map +1 -1
- package/dist/src/identify/pb/message.js.map +1 -1
- package/dist/src/index.d.ts +7 -24
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +4 -3
- package/dist/src/insecure/pb/proto.d.ts.map +1 -1
- package/dist/src/insecure/pb/proto.js +8 -1
- package/dist/src/insecure/pb/proto.js.map +1 -1
- package/dist/src/keychain/index.d.ts +0 -1
- package/dist/src/keychain/index.d.ts.map +1 -1
- package/dist/src/keychain/index.js +0 -1
- package/dist/src/keychain/index.js.map +1 -1
- package/dist/src/libp2p.d.ts +4 -2
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +57 -43
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/index.d.ts +1 -1
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -1
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js +1 -1
- package/dist/src/metrics/stats.js.map +1 -1
- package/dist/src/nat-manager.d.ts +6 -3
- package/dist/src/nat-manager.d.ts.map +1 -1
- package/dist/src/nat-manager.js +6 -3
- package/dist/src/nat-manager.js.map +1 -1
- package/dist/src/peer-record-updater.d.ts +1 -1
- package/dist/src/peer-record-updater.d.ts.map +1 -1
- package/dist/src/peer-routing.d.ts +2 -1
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +2 -1
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/pnet/index.d.ts +1 -0
- package/dist/src/pnet/index.d.ts.map +1 -1
- package/dist/src/pnet/index.js +1 -0
- package/dist/src/pnet/index.js.map +1 -1
- package/dist/src/pnet/key-generator.d.ts +1 -1
- package/dist/src/pnet/key-generator.d.ts.map +1 -1
- package/dist/src/pnet/key-generator.js +1 -1
- package/dist/src/pnet/key-generator.js.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts +1 -1
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -1
- package/dist/src/pubsub/dummy-pubsub.js +1 -1
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -1
- package/dist/src/registrar.d.ts +0 -1
- package/dist/src/registrar.d.ts.map +1 -1
- package/dist/src/registrar.js +2 -17
- package/dist/src/registrar.js.map +1 -1
- package/dist/src/transport-manager.d.ts +3 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +1 -1
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +1 -1
- package/dist/src/upgrader.js.map +1 -1
- package/package.json +16 -25
- package/src/address-manager/index.ts +2 -1
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +5 -5
- package/src/circuit/index.ts +1 -1
- package/src/circuit/listener.ts +18 -5
- package/src/circuit/pb/index.ts +33 -4
- package/src/circuit/transport.ts +8 -5
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +5 -5
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +34 -8
- package/src/{dialer → connection-manager/dialer}/index.ts +48 -37
- package/src/connection-manager/index.ts +146 -60
- package/src/connection-manager/latency-monitor.ts +1 -1
- package/src/connection-manager/visibility-change-emitter.ts +1 -1
- package/src/content-routing/index.ts +2 -1
- package/src/dht/dummy-dht.ts +10 -1
- package/src/errors.ts +2 -1
- package/src/fetch/index.ts +2 -2
- package/src/fetch/pb/proto.ts +10 -3
- package/src/identify/index.ts +5 -5
- package/src/identify/pb/message.ts +2 -1
- package/src/index.ts +7 -28
- package/src/insecure/pb/proto.ts +11 -4
- package/src/keychain/index.ts +0 -1
- package/src/libp2p.ts +68 -49
- package/src/metrics/index.ts +1 -1
- package/src/metrics/stats.ts +1 -1
- package/src/nat-manager.ts +8 -4
- package/src/peer-record-updater.ts +1 -1
- package/src/peer-routing.ts +2 -1
- package/src/ping/index.ts +3 -2
- package/src/pnet/index.ts +2 -0
- package/src/pnet/key-generator.ts +1 -1
- package/src/pubsub/dummy-pubsub.ts +1 -1
- package/src/registrar.ts +3 -19
- package/src/transport-manager.ts +3 -1
- package/src/upgrader.ts +1 -1
- package/dist/src/dialer/auto-dialer.d.ts.map +0 -1
- package/dist/src/dialer/auto-dialer.js +0 -24
- package/dist/src/dialer/auto-dialer.js.map +0 -1
- package/dist/src/dialer/dial-request.d.ts.map +0 -1
- package/dist/src/dialer/dial-request.js.map +0 -1
- package/dist/src/dialer/index.d.ts.map +0 -1
- package/dist/src/dialer/index.js.map +0 -1
package/src/config.ts
CHANGED
|
@@ -21,14 +21,8 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
21
21
|
connectionManager: {
|
|
22
22
|
maxConnections: 300,
|
|
23
23
|
minConnections: 50,
|
|
24
|
+
autoDial: true,
|
|
24
25
|
autoDialInterval: 10000,
|
|
25
|
-
autoDial: true
|
|
26
|
-
},
|
|
27
|
-
connectionGater: {},
|
|
28
|
-
transportManager: {
|
|
29
|
-
faultTolerance: FaultTolerance.FATAL_ALL
|
|
30
|
-
},
|
|
31
|
-
dialer: {
|
|
32
26
|
maxParallelDials: Constants.MAX_PARALLEL_DIALS,
|
|
33
27
|
maxDialsPerPeer: Constants.MAX_PER_PEER_DIALS,
|
|
34
28
|
dialTimeout: Constants.DIAL_TIMEOUT,
|
|
@@ -37,6 +31,10 @@ const DefaultConfig: Partial<Libp2pInit> = {
|
|
|
37
31
|
},
|
|
38
32
|
addressSorter: publicAddressesFirst
|
|
39
33
|
},
|
|
34
|
+
connectionGater: {},
|
|
35
|
+
transportManager: {
|
|
36
|
+
faultTolerance: FaultTolerance.FATAL_ALL
|
|
37
|
+
},
|
|
40
38
|
host: {
|
|
41
39
|
agentVersion: AGENT_VERSION
|
|
42
40
|
},
|
|
@@ -6,7 +6,7 @@ import all from 'it-all'
|
|
|
6
6
|
import { pipe } from 'it-pipe'
|
|
7
7
|
import filter from 'it-filter'
|
|
8
8
|
import sort from 'it-sort'
|
|
9
|
-
import type { Startable } from '@libp2p/interfaces'
|
|
9
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
10
10
|
import type { Components } from '@libp2p/interfaces/components'
|
|
11
11
|
|
|
12
12
|
const log = logger('libp2p:connection-manager:auto-dialler')
|
|
@@ -102,7 +102,7 @@ export class AutoDialler implements Startable {
|
|
|
102
102
|
const minConnections = this.options.minConnections
|
|
103
103
|
|
|
104
104
|
// Already has enough connections
|
|
105
|
-
if (this.components.getConnectionManager().
|
|
105
|
+
if (this.components.getConnectionManager().getConnections().length >= minConnections) {
|
|
106
106
|
this.autoDialTimeout = retimer(this._autoDial, this.options.autoDialInterval)
|
|
107
107
|
|
|
108
108
|
return
|
|
@@ -126,7 +126,7 @@ export class AutoDialler implements Startable {
|
|
|
126
126
|
async (source) => await all(source)
|
|
127
127
|
)
|
|
128
128
|
|
|
129
|
-
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().
|
|
129
|
+
for (let i = 0; this.running && i < peers.length && this.components.getConnectionManager().getConnections().length < minConnections; i++) {
|
|
130
130
|
// Connection Manager was stopped during async dial
|
|
131
131
|
if (!this.running) {
|
|
132
132
|
return
|
|
@@ -134,10 +134,10 @@ export class AutoDialler implements Startable {
|
|
|
134
134
|
|
|
135
135
|
const peer = peers[i]
|
|
136
136
|
|
|
137
|
-
if (this.components.getConnectionManager().
|
|
137
|
+
if (this.components.getConnectionManager().getConnections(peer.id).length === 0) {
|
|
138
138
|
log('connecting to a peerStore stored peer %p', peer.id)
|
|
139
139
|
try {
|
|
140
|
-
await this.components.
|
|
140
|
+
await this.components.getConnectionManager().openConnection(peer.id)
|
|
141
141
|
} catch (err: any) {
|
|
142
142
|
log.error('could not connect to peerStore stored peer', err)
|
|
143
143
|
}
|
|
@@ -1,39 +1,58 @@
|
|
|
1
1
|
import type { PeerInfo } from '@libp2p/interfaces/peer-info'
|
|
2
2
|
import { logger } from '@libp2p/logger'
|
|
3
3
|
import type { Components } from '@libp2p/interfaces/components'
|
|
4
|
+
import { TimeoutController } from 'timeout-abort-controller'
|
|
4
5
|
|
|
5
6
|
const log = logger('libp2p:dialer:auto-dialer')
|
|
6
7
|
|
|
7
8
|
export interface AutoDialerInit {
|
|
8
9
|
enabled: boolean
|
|
9
10
|
minConnections: number
|
|
11
|
+
dialTimeout: number
|
|
10
12
|
}
|
|
11
13
|
|
|
12
14
|
export class AutoDialer {
|
|
13
15
|
private readonly components: Components
|
|
14
16
|
private readonly enabled: boolean
|
|
15
17
|
private readonly minConnections: number
|
|
18
|
+
private readonly dialTimeout: number
|
|
16
19
|
|
|
17
20
|
constructor (components: Components, init: AutoDialerInit) {
|
|
18
21
|
this.components = components
|
|
19
22
|
this.enabled = init.enabled
|
|
20
23
|
this.minConnections = init.minConnections
|
|
24
|
+
this.dialTimeout = init.dialTimeout
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
public handle (evt: CustomEvent<PeerInfo>) {
|
|
24
28
|
const { detail: peer } = evt
|
|
25
29
|
|
|
30
|
+
if (!this.enabled) {
|
|
31
|
+
return
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const connections = this.components.getConnectionManager().getConnections(peer.id)
|
|
35
|
+
|
|
26
36
|
// If auto dialing is on and we have no connection to the peer, check if we should dial
|
|
27
|
-
if (
|
|
37
|
+
if (connections.length === 0) {
|
|
28
38
|
const minConnections = this.minConnections ?? 0
|
|
29
39
|
|
|
30
|
-
|
|
31
|
-
log('auto-dialing discovered peer %p', peer.id)
|
|
40
|
+
const allConnections = this.components.getConnectionManager().getConnections()
|
|
32
41
|
|
|
33
|
-
|
|
42
|
+
if (minConnections > allConnections.length) {
|
|
43
|
+
log('auto-dialing discovered peer %p with timeout %d', peer.id, this.dialTimeout)
|
|
44
|
+
|
|
45
|
+
const controller = new TimeoutController(this.dialTimeout)
|
|
46
|
+
|
|
47
|
+
void this.components.getConnectionManager().openConnection(peer.id, {
|
|
48
|
+
signal: controller.signal
|
|
49
|
+
})
|
|
34
50
|
.catch(err => {
|
|
35
51
|
log.error('could not connect to discovered peer %p with %o', peer.id, err)
|
|
36
52
|
})
|
|
53
|
+
.finally(() => {
|
|
54
|
+
controller.clear()
|
|
55
|
+
})
|
|
37
56
|
}
|
|
38
57
|
}
|
|
39
58
|
}
|
|
@@ -3,12 +3,12 @@ import { anySignal } from 'any-signal'
|
|
|
3
3
|
import FIFO from 'p-fifo'
|
|
4
4
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
5
5
|
import { setMaxListeners } from 'events'
|
|
6
|
-
import { codes } from '
|
|
6
|
+
import { codes } from '../../errors.js'
|
|
7
7
|
import { logger } from '@libp2p/logger'
|
|
8
8
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
9
9
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
10
10
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
11
|
-
import type {
|
|
11
|
+
import type { Dialer } from './index.js'
|
|
12
12
|
|
|
13
13
|
const log = logger('libp2p:dialer:dial-request')
|
|
14
14
|
|
|
@@ -19,12 +19,12 @@ export interface DialAction {
|
|
|
19
19
|
export interface DialRequestOptions {
|
|
20
20
|
addrs: Multiaddr[]
|
|
21
21
|
dialAction: DialAction
|
|
22
|
-
dialer:
|
|
22
|
+
dialer: Dialer
|
|
23
23
|
}
|
|
24
24
|
|
|
25
25
|
export class DialRequest {
|
|
26
26
|
private readonly addrs: Multiaddr[]
|
|
27
|
-
private readonly dialer:
|
|
27
|
+
private readonly dialer: Dialer
|
|
28
28
|
private readonly dialAction: DialAction
|
|
29
29
|
|
|
30
30
|
/**
|
|
@@ -62,7 +62,7 @@ export class DialRequest {
|
|
|
62
62
|
})
|
|
63
63
|
}
|
|
64
64
|
|
|
65
|
-
const dialAbortControllers = this.addrs.map(() => {
|
|
65
|
+
const dialAbortControllers: Array<(AbortController | undefined)> = this.addrs.map(() => {
|
|
66
66
|
const controller = new AbortController()
|
|
67
67
|
try {
|
|
68
68
|
// fails on node < 15.4
|
|
@@ -80,16 +80,27 @@ export class DialRequest {
|
|
|
80
80
|
}
|
|
81
81
|
|
|
82
82
|
let completedDials = 0
|
|
83
|
+
let done = false
|
|
83
84
|
|
|
84
85
|
try {
|
|
85
86
|
return await Promise.any(this.addrs.map(async (addr, i) => {
|
|
86
87
|
const token = await tokenHolder.shift() // get token
|
|
88
|
+
// End attempt once another attempt succeeded
|
|
89
|
+
if (done) {
|
|
90
|
+
this.dialer.releaseToken(tokens.splice(tokens.indexOf(token), 1)[0])
|
|
91
|
+
throw errCode(new Error('dialAction already succeeded'), codes.ERR_ALREADY_SUCCEEDED)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const controller = dialAbortControllers[i]
|
|
95
|
+
if (controller == null) {
|
|
96
|
+
throw errCode(new Error('dialAction did not come with an AbortController'), codes.ERR_INVALID_PARAMETERS)
|
|
97
|
+
}
|
|
87
98
|
let conn
|
|
88
99
|
try {
|
|
89
|
-
const signal =
|
|
100
|
+
const signal = controller.signal
|
|
90
101
|
conn = await this.dialAction(addr, { ...options, signal: (options.signal != null) ? anySignal([signal, options.signal]) : signal })
|
|
91
102
|
// Remove the successful AbortController so it is not aborted
|
|
92
|
-
dialAbortControllers
|
|
103
|
+
dialAbortControllers[i] = undefined
|
|
93
104
|
} finally {
|
|
94
105
|
completedDials++
|
|
95
106
|
// If we have more or equal dials remaining than tokens, recycle the token, otherwise release it
|
|
@@ -102,10 +113,25 @@ export class DialRequest {
|
|
|
102
113
|
}
|
|
103
114
|
}
|
|
104
115
|
|
|
116
|
+
if (conn == null) {
|
|
117
|
+
// Notify Promise.any that attempt was not successful
|
|
118
|
+
// to prevent from returning undefined despite there
|
|
119
|
+
// were successful dial attempts
|
|
120
|
+
throw errCode(new Error('dialAction led to empty object'), codes.ERR_TRANSPORT_DIAL_FAILED)
|
|
121
|
+
} else {
|
|
122
|
+
// This dial succeeded, don't attempt anything else
|
|
123
|
+
done = true
|
|
124
|
+
}
|
|
125
|
+
|
|
105
126
|
return conn
|
|
106
127
|
}))
|
|
107
128
|
} finally {
|
|
108
|
-
|
|
129
|
+
// success/failure happened, abort everything else
|
|
130
|
+
dialAbortControllers.forEach(c => {
|
|
131
|
+
if (c !== undefined) {
|
|
132
|
+
c.abort()
|
|
133
|
+
}
|
|
134
|
+
})
|
|
109
135
|
tokens.forEach(token => this.dialer.releaseToken(token)) // release tokens back to the dialer
|
|
110
136
|
}
|
|
111
137
|
}
|
|
@@ -3,7 +3,7 @@ import all from 'it-all'
|
|
|
3
3
|
import filter from 'it-filter'
|
|
4
4
|
import { pipe } from 'it-pipe'
|
|
5
5
|
import errCode from 'err-code'
|
|
6
|
-
import { Multiaddr } from '@multiformats/multiaddr'
|
|
6
|
+
import { Multiaddr, Resolver } from '@multiformats/multiaddr'
|
|
7
7
|
import { TimeoutController } from 'timeout-abort-controller'
|
|
8
8
|
import { AbortError } from '@libp2p/interfaces/errors'
|
|
9
9
|
import { anySignal } from 'any-signal'
|
|
@@ -12,22 +12,23 @@ import { setMaxListeners } from 'events'
|
|
|
12
12
|
import { DialAction, DialRequest } from './dial-request.js'
|
|
13
13
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
14
14
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
15
|
-
import { codes } from '
|
|
15
|
+
import { codes } from '../../errors.js'
|
|
16
16
|
import {
|
|
17
17
|
DIAL_TIMEOUT,
|
|
18
18
|
MAX_PARALLEL_DIALS,
|
|
19
19
|
MAX_PER_PEER_DIALS,
|
|
20
20
|
MAX_ADDRS_TO_DIAL
|
|
21
|
-
} from '
|
|
21
|
+
} from '../../constants.js'
|
|
22
22
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
23
|
-
import type { AbortOptions
|
|
23
|
+
import type { AbortOptions } from '@libp2p/interfaces'
|
|
24
|
+
import type { Startable } from '@libp2p/interfaces/startable'
|
|
24
25
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
25
|
-
import { getPeer } from '
|
|
26
|
+
import { getPeer } from '../../get-peer.js'
|
|
26
27
|
import sort from 'it-sort'
|
|
27
|
-
import
|
|
28
|
-
import type { Dialer, DialerInit } from '@libp2p/interfaces/dialer'
|
|
28
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
29
29
|
import map from 'it-map'
|
|
30
30
|
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
31
|
+
import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics'
|
|
31
32
|
|
|
32
33
|
const log = logger('libp2p:dialer')
|
|
33
34
|
|
|
@@ -52,8 +53,41 @@ export interface PendingDialTarget {
|
|
|
52
53
|
reject: (err: Error) => void
|
|
53
54
|
}
|
|
54
55
|
|
|
55
|
-
export
|
|
56
|
-
|
|
56
|
+
export interface DialerInit {
|
|
57
|
+
/**
|
|
58
|
+
* Sort the known addresses of a peer before trying to dial
|
|
59
|
+
*/
|
|
60
|
+
addressSorter?: AddressSorter
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Number of max concurrent dials
|
|
64
|
+
*/
|
|
65
|
+
maxParallelDials?: number
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Number of max addresses to dial for a given peer
|
|
69
|
+
*/
|
|
70
|
+
maxAddrsToDial?: number
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* How long a dial attempt is allowed to take
|
|
74
|
+
*/
|
|
75
|
+
dialTimeout?: number
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Number of max concurrent dials per peer
|
|
79
|
+
*/
|
|
80
|
+
maxDialsPerPeer?: number
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Multiaddr resolvers to use when dialing
|
|
84
|
+
*/
|
|
85
|
+
resolvers?: Record<string, Resolver>
|
|
86
|
+
metrics?: ComponentMetricsTracker
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export class Dialer implements Startable, Initializable {
|
|
90
|
+
private components: Components = new Components()
|
|
57
91
|
private readonly addressSorter: AddressSorter
|
|
58
92
|
private readonly maxAddrsToDial: number
|
|
59
93
|
private readonly timeout: number
|
|
@@ -63,8 +97,7 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
63
97
|
public pendingDialTargets: Map<string, PendingDialTarget>
|
|
64
98
|
private started: boolean
|
|
65
99
|
|
|
66
|
-
constructor (
|
|
67
|
-
this.components = components
|
|
100
|
+
constructor (init: DialerInit = {}) {
|
|
68
101
|
this.started = false
|
|
69
102
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
70
103
|
this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL
|
|
@@ -87,6 +120,10 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
87
120
|
}
|
|
88
121
|
}
|
|
89
122
|
|
|
123
|
+
init (components: Components): void {
|
|
124
|
+
this.components = components
|
|
125
|
+
}
|
|
126
|
+
|
|
90
127
|
isStarted () {
|
|
91
128
|
return this.started
|
|
92
129
|
}
|
|
@@ -139,16 +176,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
139
176
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
140
177
|
}
|
|
141
178
|
|
|
142
|
-
log('dial to %p', id)
|
|
143
|
-
|
|
144
|
-
const existingConnection = this.components.getConnectionManager().getConnection(id)
|
|
145
|
-
|
|
146
|
-
if (existingConnection != null) {
|
|
147
|
-
log('had an existing connection to %p', id)
|
|
148
|
-
|
|
149
|
-
return existingConnection
|
|
150
|
-
}
|
|
151
|
-
|
|
152
179
|
log('creating dial target for %p', id)
|
|
153
180
|
|
|
154
181
|
const dialTarget = await this._createCancellableDialTarget(id)
|
|
@@ -176,22 +203,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
176
203
|
}
|
|
177
204
|
}
|
|
178
205
|
|
|
179
|
-
async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
|
|
180
|
-
if (protocols == null) {
|
|
181
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
182
|
-
}
|
|
183
|
-
|
|
184
|
-
protocols = Array.isArray(protocols) ? protocols : [protocols]
|
|
185
|
-
|
|
186
|
-
if (protocols.length === 0) {
|
|
187
|
-
throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
const connection = await this.dial(peer, options)
|
|
191
|
-
|
|
192
|
-
return await connection.newStream(protocols)
|
|
193
|
-
}
|
|
194
|
-
|
|
195
206
|
/**
|
|
196
207
|
* Connects to a given `peer` by dialing all of its known addresses.
|
|
197
208
|
* The dial to the first address that is successfully able to upgrade a connection
|