libp2p 0.36.2-64bfcee → 0.36.2-a15254f
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/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 +4 -4
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +5 -5
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.d.ts +3 -3
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.d.ts +4 -4
- package/dist/src/circuit/circuit/stream-handler.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stream-handler.js +2 -3
- package/dist/src/circuit/circuit/stream-handler.js.map +1 -1
- package/dist/src/circuit/circuit/utils.d.ts +2 -2
- package/dist/src/circuit/circuit/utils.d.ts.map +1 -1
- package/dist/src/circuit/circuit/utils.js.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 +9 -1
- package/dist/src/circuit/listener.js.map +1 -1
- package/dist/src/circuit/pb/index.d.ts +42 -164
- package/dist/src/circuit/pb/index.d.ts.map +1 -0
- package/dist/src/circuit/pb/index.js +67 -523
- package/dist/src/circuit/pb/index.js.map +1 -0
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +6 -4
- 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.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 +1 -1
- package/dist/src/connection-manager/dialer/dial-request.js.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.d.ts +35 -7
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -0
- package/dist/src/{dialer → connection-manager/dialer}/index.js +10 -24
- package/dist/src/connection-manager/dialer/index.js.map +1 -0
- package/dist/src/connection-manager/index.d.ts +55 -26
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +78 -40
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/dht/dummy-dht.d.ts +17 -0
- package/dist/src/dht/dummy-dht.d.ts.map +1 -0
- package/dist/src/dht/dummy-dht.js +39 -0
- package/dist/src/dht/dummy-dht.js.map +1 -0
- package/dist/src/errors.d.ts +2 -0
- package/dist/src/errors.d.ts.map +1 -1
- package/dist/src/errors.js +2 -0
- package/dist/src/errors.js.map +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +6 -7
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/fetch/pb/proto.d.ts +20 -128
- package/dist/src/fetch/pb/proto.d.ts.map +1 -0
- package/dist/src/fetch/pb/proto.js +44 -331
- package/dist/src/fetch/pb/proto.js.map +1 -0
- package/dist/src/identify/index.d.ts +4 -2
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +12 -12
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/identify/pb/message.d.ts +13 -108
- package/dist/src/identify/pb/message.d.ts.map +1 -0
- package/dist/src/identify/pb/message.js +21 -366
- package/dist/src/identify/pb/message.js.map +1 -0
- package/dist/src/index.d.ts +11 -25
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/insecure/index.d.ts.map +1 -1
- package/dist/src/insecure/index.js +5 -2
- package/dist/src/insecure/index.js.map +1 -1
- package/dist/src/insecure/pb/proto.d.ts +24 -130
- package/dist/src/insecure/pb/proto.d.ts.map +1 -0
- package/dist/src/insecure/pb/proto.js +46 -388
- package/dist/src/insecure/pb/proto.js.map +1 -0
- package/dist/src/keychain/index.d.ts +3 -3
- package/dist/src/keychain/index.js +2 -2
- package/dist/src/keychain/util.d.ts +1 -1
- package/dist/src/keychain/util.d.ts.map +1 -1
- package/dist/src/libp2p.d.ts +5 -4
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +69 -45
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/stats.d.ts +1 -5
- package/dist/src/metrics/stats.d.ts.map +1 -1
- package/dist/src/metrics/stats.js.map +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/pubsub/dummy-pubsub.d.ts +17 -0
- package/dist/src/pubsub/dummy-pubsub.d.ts.map +1 -0
- package/dist/src/pubsub/dummy-pubsub.js +37 -0
- package/dist/src/pubsub/dummy-pubsub.js.map +1 -0
- package/dist/src/registrar.js +1 -1
- package/dist/src/registrar.js.map +1 -1
- package/package.json +46 -44
- package/src/circuit/auto-relay.ts +8 -6
- package/src/circuit/circuit/hop.ts +9 -9
- package/src/circuit/circuit/stop.ts +3 -3
- package/src/circuit/circuit/stream-handler.ts +5 -6
- package/src/circuit/circuit/utils.ts +2 -2
- package/src/circuit/listener.ts +17 -4
- package/src/circuit/pb/index.proto +1 -1
- package/src/circuit/pb/index.ts +88 -0
- package/src/circuit/transport.ts +7 -4
- package/src/config.ts +5 -7
- package/src/connection-manager/auto-dialler.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/auto-dialer.ts +23 -4
- package/src/{dialer → connection-manager/dialer}/dial-request.ts +4 -4
- package/src/{dialer → connection-manager/dialer}/index.ts +46 -39
- package/src/connection-manager/index.ts +143 -59
- package/src/dht/dummy-dht.ts +51 -0
- package/src/errors.ts +2 -0
- package/src/fetch/index.ts +7 -8
- package/src/fetch/pb/proto.ts +58 -0
- package/src/identify/index.ts +14 -14
- package/src/identify/pb/message.ts +36 -0
- package/src/index.ts +12 -30
- package/src/insecure/index.ts +8 -4
- package/src/insecure/pb/proto.ts +61 -0
- package/src/keychain/index.ts +2 -2
- package/src/libp2p.ts +81 -54
- package/src/metrics/stats.ts +1 -6
- package/src/ping/index.ts +2 -1
- package/src/pubsub/dummy-pubsub.ts +51 -0
- package/src/registrar.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/circuit/pb/index.d.ts +0 -173
- package/src/circuit/pb/index.js +0 -528
- package/src/fetch/pb/proto.d.ts +0 -134
- package/src/fetch/pb/proto.js +0 -331
- package/src/identify/pb/message.d.ts +0 -110
- package/src/identify/pb/message.js +0 -369
- package/src/insecure/pb/proto.d.ts +0 -134
- package/src/insecure/pb/proto.js +0 -388
|
@@ -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,22 @@ 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
23
|
import type { AbortOptions, Startable } from '@libp2p/interfaces'
|
|
24
24
|
import type { PeerId } from '@libp2p/interfaces/peer-id'
|
|
25
|
-
import { getPeer } from '
|
|
25
|
+
import { getPeer } from '../../get-peer.js'
|
|
26
26
|
import sort from 'it-sort'
|
|
27
|
-
import
|
|
28
|
-
import type { Dialer, DialerInit } from '@libp2p/interfaces/dialer'
|
|
27
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
29
28
|
import map from 'it-map'
|
|
30
29
|
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
30
|
+
import type { ComponentMetricsTracker } from '@libp2p/interfaces/metrics'
|
|
31
31
|
|
|
32
32
|
const log = logger('libp2p:dialer')
|
|
33
33
|
|
|
@@ -52,8 +52,41 @@ export interface PendingDialTarget {
|
|
|
52
52
|
reject: (err: Error) => void
|
|
53
53
|
}
|
|
54
54
|
|
|
55
|
-
export
|
|
56
|
-
|
|
55
|
+
export interface DialerInit {
|
|
56
|
+
/**
|
|
57
|
+
* Sort the known addresses of a peer before trying to dial
|
|
58
|
+
*/
|
|
59
|
+
addressSorter?: AddressSorter
|
|
60
|
+
|
|
61
|
+
/**
|
|
62
|
+
* Number of max concurrent dials
|
|
63
|
+
*/
|
|
64
|
+
maxParallelDials?: number
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Number of max addresses to dial for a given peer
|
|
68
|
+
*/
|
|
69
|
+
maxAddrsToDial?: number
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* How long a dial attempt is allowed to take
|
|
73
|
+
*/
|
|
74
|
+
dialTimeout?: number
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Number of max concurrent dials per peer
|
|
78
|
+
*/
|
|
79
|
+
maxDialsPerPeer?: number
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Multiaddr resolvers to use when dialing
|
|
83
|
+
*/
|
|
84
|
+
resolvers?: Record<string, Resolver>
|
|
85
|
+
metrics?: ComponentMetricsTracker
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export class Dialer implements Startable, Initializable {
|
|
89
|
+
private components: Components = new Components()
|
|
57
90
|
private readonly addressSorter: AddressSorter
|
|
58
91
|
private readonly maxAddrsToDial: number
|
|
59
92
|
private readonly timeout: number
|
|
@@ -63,8 +96,7 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
63
96
|
public pendingDialTargets: Map<string, PendingDialTarget>
|
|
64
97
|
private started: boolean
|
|
65
98
|
|
|
66
|
-
constructor (
|
|
67
|
-
this.components = components
|
|
99
|
+
constructor (init: DialerInit = {}) {
|
|
68
100
|
this.started = false
|
|
69
101
|
this.addressSorter = init.addressSorter ?? publicAddressesFirst
|
|
70
102
|
this.maxAddrsToDial = init.maxAddrsToDial ?? MAX_ADDRS_TO_DIAL
|
|
@@ -87,6 +119,10 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
87
119
|
}
|
|
88
120
|
}
|
|
89
121
|
|
|
122
|
+
init (components: Components): void {
|
|
123
|
+
this.components = components
|
|
124
|
+
}
|
|
125
|
+
|
|
90
126
|
isStarted () {
|
|
91
127
|
return this.started
|
|
92
128
|
}
|
|
@@ -139,16 +175,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
139
175
|
throw errCode(new Error('The dial request is blocked by gater.allowDialPeer'), codes.ERR_PEER_DIAL_INTERCEPTED)
|
|
140
176
|
}
|
|
141
177
|
|
|
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
178
|
log('creating dial target for %p', id)
|
|
153
179
|
|
|
154
180
|
const dialTarget = await this._createCancellableDialTarget(id)
|
|
@@ -176,22 +202,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
176
202
|
}
|
|
177
203
|
}
|
|
178
204
|
|
|
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
205
|
/**
|
|
196
206
|
* Connects to a given `peer` by dialing all of its known addresses.
|
|
197
207
|
* The dial to the first address that is successfully able to upgrade a connection
|
|
@@ -244,9 +254,6 @@ export class DefaultDialer implements Dialer, Startable {
|
|
|
244
254
|
const addrs: Multiaddr[] = []
|
|
245
255
|
for (const a of knownAddrs) {
|
|
246
256
|
const resolvedAddrs = await this._resolve(a)
|
|
247
|
-
|
|
248
|
-
log('resolved %s to %s', a, resolvedAddrs)
|
|
249
|
-
|
|
250
257
|
resolvedAddrs.forEach(ra => addrs.push(ra))
|
|
251
258
|
}
|
|
252
259
|
|
|
@@ -4,16 +4,19 @@ import mergeOptions from 'merge-options'
|
|
|
4
4
|
import { LatencyMonitor, SummaryObject } from './latency-monitor.js'
|
|
5
5
|
// @ts-expect-error retimer does not have types
|
|
6
6
|
import retimer from 'retimer'
|
|
7
|
-
import { CustomEvent, EventEmitter, Startable } from '@libp2p/interfaces'
|
|
7
|
+
import { AbortOptions, CustomEvent, EventEmitter, Startable } from '@libp2p/interfaces'
|
|
8
8
|
import { trackedMap } from '@libp2p/tracked-map'
|
|
9
9
|
import { codes } from '../errors.js'
|
|
10
10
|
import { isPeerId, PeerId } from '@libp2p/interfaces/peer-id'
|
|
11
11
|
// @ts-expect-error setMaxListeners is missing from the node 16 types
|
|
12
12
|
import { setMaxListeners } from 'events'
|
|
13
13
|
import type { Connection } from '@libp2p/interfaces/connection'
|
|
14
|
-
import type { ConnectionManager } from '@libp2p/interfaces/
|
|
15
|
-
import
|
|
14
|
+
import type { ConnectionManager } from '@libp2p/interfaces/connection-manager'
|
|
15
|
+
import { Components, Initializable } from '@libp2p/interfaces/components'
|
|
16
16
|
import * as STATUS from '@libp2p/interfaces/connection/status'
|
|
17
|
+
import { Dialer } from './dialer/index.js'
|
|
18
|
+
import type { AddressSorter } from '@libp2p/interfaces/peer-store'
|
|
19
|
+
import type { Resolver } from '@multiformats/multiaddr'
|
|
17
20
|
|
|
18
21
|
const log = logger('libp2p:connection-manager')
|
|
19
22
|
|
|
@@ -34,21 +37,16 @@ const METRICS_COMPONENT = 'connection-manager'
|
|
|
34
37
|
const METRICS_PEER_CONNECTIONS = 'peer-connections'
|
|
35
38
|
const METRICS_PEER_VALUES = 'peer-values'
|
|
36
39
|
|
|
37
|
-
export interface ConnectionManagerEvents {
|
|
38
|
-
'peer:connect': CustomEvent<PeerId>
|
|
39
|
-
'peer:disconnect': CustomEvent<PeerId>
|
|
40
|
-
}
|
|
41
|
-
|
|
42
40
|
export interface ConnectionManagerInit {
|
|
43
41
|
/**
|
|
44
|
-
* The maximum number of connections
|
|
42
|
+
* The maximum number of connections to keep open
|
|
45
43
|
*/
|
|
46
|
-
maxConnections
|
|
44
|
+
maxConnections: number
|
|
47
45
|
|
|
48
46
|
/**
|
|
49
|
-
* The minimum number of connections to
|
|
47
|
+
* The minimum number of connections to keep open
|
|
50
48
|
*/
|
|
51
|
-
minConnections
|
|
49
|
+
minConnections: number
|
|
52
50
|
|
|
53
51
|
/**
|
|
54
52
|
* The max data (in and out), per average interval to allow
|
|
@@ -86,39 +84,75 @@ export interface ConnectionManagerInit {
|
|
|
86
84
|
defaultPeerValue?: number
|
|
87
85
|
|
|
88
86
|
/**
|
|
89
|
-
*
|
|
87
|
+
* If true, try to connect to all discovered peers up to the connection manager limit
|
|
90
88
|
*/
|
|
91
89
|
autoDial?: boolean
|
|
92
90
|
|
|
93
91
|
/**
|
|
94
|
-
* How
|
|
92
|
+
* How long to wait between attempting to keep our number of concurrent connections
|
|
93
|
+
* above minConnections
|
|
94
|
+
*/
|
|
95
|
+
autoDialInterval: number
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Sort the known addresses of a peer before trying to dial
|
|
99
|
+
*/
|
|
100
|
+
addressSorter?: AddressSorter
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Number of max concurrent dials
|
|
104
|
+
*/
|
|
105
|
+
maxParallelDials?: number
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Number of max addresses to dial for a given peer
|
|
109
|
+
*/
|
|
110
|
+
maxAddrsToDial?: number
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* How long a dial attempt is allowed to take
|
|
95
114
|
*/
|
|
96
|
-
|
|
115
|
+
dialTimeout?: number
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Number of max concurrent dials per peer
|
|
119
|
+
*/
|
|
120
|
+
maxDialsPerPeer?: number
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Multiaddr resolvers to use when dialing
|
|
124
|
+
*/
|
|
125
|
+
resolvers?: Record<string, Resolver>
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
export interface ConnectionManagerEvents {
|
|
129
|
+
'peer:connect': CustomEvent<PeerId>
|
|
130
|
+
'peer:disconnect': CustomEvent<PeerId>
|
|
97
131
|
}
|
|
98
132
|
|
|
99
133
|
/**
|
|
100
134
|
* Responsible for managing known connections.
|
|
101
135
|
*/
|
|
102
|
-
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable {
|
|
103
|
-
|
|
104
|
-
private
|
|
136
|
+
export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEvents> implements ConnectionManager, Startable, Initializable {
|
|
137
|
+
public readonly dialer: Dialer
|
|
138
|
+
private components = new Components()
|
|
139
|
+
private readonly opts: Required<ConnectionManagerInit>
|
|
105
140
|
private readonly peerValues: Map<string, number>
|
|
106
141
|
private readonly connections: Map<string, Connection[]>
|
|
107
142
|
private started: boolean
|
|
108
143
|
private timer?: ReturnType<retimer>
|
|
109
144
|
private readonly latencyMonitor: LatencyMonitor
|
|
110
145
|
|
|
111
|
-
constructor (
|
|
146
|
+
constructor (init: ConnectionManagerInit) {
|
|
112
147
|
super()
|
|
113
148
|
|
|
114
|
-
this.
|
|
115
|
-
this.init = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
149
|
+
this.opts = mergeOptions.call({ ignoreUndefined: true }, defaultOptions, init)
|
|
116
150
|
|
|
117
|
-
if (this.
|
|
151
|
+
if (this.opts.maxConnections < this.opts.minConnections) {
|
|
118
152
|
throw errCode(new Error('Connection Manager maxConnections must be greater than minConnections'), codes.ERR_INVALID_PARAMETERS)
|
|
119
153
|
}
|
|
120
154
|
|
|
121
|
-
log('options: %o', this.
|
|
155
|
+
log('options: %o', this.opts)
|
|
122
156
|
|
|
123
157
|
/**
|
|
124
158
|
* Map of peer identifiers to their peer value for pruning connections.
|
|
@@ -153,12 +187,16 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
153
187
|
setMaxListeners?.(Infinity, this)
|
|
154
188
|
} catch {}
|
|
155
189
|
|
|
156
|
-
this.
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
190
|
+
this.dialer = new Dialer(this.opts)
|
|
191
|
+
|
|
192
|
+
this.onConnect = this.onConnect.bind(this)
|
|
193
|
+
this.onDisconnect = this.onDisconnect.bind(this)
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
init (components: Components): void {
|
|
197
|
+
this.components = components
|
|
198
|
+
|
|
199
|
+
this.dialer.init(components)
|
|
162
200
|
}
|
|
163
201
|
|
|
164
202
|
isStarted () {
|
|
@@ -171,18 +209,29 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
171
209
|
*/
|
|
172
210
|
async start () {
|
|
173
211
|
if (this.components.getMetrics() != null) {
|
|
174
|
-
this.timer = this.timer ?? retimer(this._checkMetrics, this.
|
|
212
|
+
this.timer = this.timer ?? retimer(this._checkMetrics, this.opts.pollInterval)
|
|
175
213
|
}
|
|
176
214
|
|
|
177
215
|
// latency monitor
|
|
178
216
|
this.latencyMonitor.start()
|
|
179
217
|
this._onLatencyMeasure = this._onLatencyMeasure.bind(this)
|
|
180
218
|
this.latencyMonitor.addEventListener('data', this._onLatencyMeasure)
|
|
219
|
+
await this.dialer.start()
|
|
181
220
|
|
|
182
221
|
this.started = true
|
|
183
222
|
log('started')
|
|
184
223
|
}
|
|
185
224
|
|
|
225
|
+
async afterStart () {
|
|
226
|
+
this.components.getUpgrader().addEventListener('connection', this.onConnect)
|
|
227
|
+
this.components.getUpgrader().addEventListener('connectionEnd', this.onDisconnect)
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
async beforeStop () {
|
|
231
|
+
this.components.getUpgrader().removeEventListener('connection', this.onConnect)
|
|
232
|
+
this.components.getUpgrader().removeEventListener('connectionEnd', this.onDisconnect)
|
|
233
|
+
}
|
|
234
|
+
|
|
186
235
|
/**
|
|
187
236
|
* Stops the Connection Manager
|
|
188
237
|
*/
|
|
@@ -191,6 +240,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
191
240
|
|
|
192
241
|
this.latencyMonitor.removeEventListener('data', this._onLatencyMeasure)
|
|
193
242
|
this.latencyMonitor.stop()
|
|
243
|
+
await this.dialer.stop()
|
|
194
244
|
|
|
195
245
|
this.started = false
|
|
196
246
|
await this._close()
|
|
@@ -238,23 +288,29 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
238
288
|
if (metrics != null) {
|
|
239
289
|
try {
|
|
240
290
|
const movingAverages = metrics.getGlobal().getMovingAverages()
|
|
241
|
-
const received = movingAverages.dataReceived[this.
|
|
291
|
+
const received = movingAverages.dataReceived[this.opts.movingAverageInterval].movingAverage
|
|
242
292
|
await this._checkMaxLimit('maxReceivedData', received)
|
|
243
|
-
const sent = movingAverages.dataSent[this.
|
|
293
|
+
const sent = movingAverages.dataSent[this.opts.movingAverageInterval].movingAverage
|
|
244
294
|
await this._checkMaxLimit('maxSentData', sent)
|
|
245
295
|
const total = received + sent
|
|
246
296
|
await this._checkMaxLimit('maxData', total)
|
|
247
297
|
log('metrics update', total)
|
|
248
298
|
} finally {
|
|
249
|
-
this.timer = retimer(this._checkMetrics, this.
|
|
299
|
+
this.timer = retimer(this._checkMetrics, this.opts.pollInterval)
|
|
250
300
|
}
|
|
251
301
|
}
|
|
252
302
|
}
|
|
253
303
|
|
|
304
|
+
onConnect (evt: CustomEvent<Connection>) {
|
|
305
|
+
void this._onConnect(evt).catch(err => {
|
|
306
|
+
log.error(err)
|
|
307
|
+
})
|
|
308
|
+
}
|
|
309
|
+
|
|
254
310
|
/**
|
|
255
311
|
* Tracks the incoming connection and check the connection limit
|
|
256
312
|
*/
|
|
257
|
-
async
|
|
313
|
+
async _onConnect (evt: CustomEvent<Connection>) {
|
|
258
314
|
const { detail: connection } = evt
|
|
259
315
|
|
|
260
316
|
if (!this.started) {
|
|
@@ -267,8 +323,6 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
267
323
|
const peerIdStr = peerId.toString()
|
|
268
324
|
const storedConns = this.connections.get(peerIdStr)
|
|
269
325
|
|
|
270
|
-
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
|
|
271
|
-
|
|
272
326
|
if (storedConns != null) {
|
|
273
327
|
storedConns.push(connection)
|
|
274
328
|
} else {
|
|
@@ -280,10 +334,11 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
280
334
|
}
|
|
281
335
|
|
|
282
336
|
if (!this.peerValues.has(peerIdStr)) {
|
|
283
|
-
this.peerValues.set(peerIdStr, this.
|
|
337
|
+
this.peerValues.set(peerIdStr, this.opts.defaultPeerValue)
|
|
284
338
|
}
|
|
285
339
|
|
|
286
|
-
await this._checkMaxLimit('maxConnections', this.
|
|
340
|
+
await this._checkMaxLimit('maxConnections', this.getConnections().length)
|
|
341
|
+
this.dispatchEvent(new CustomEvent<Connection>('peer:connect', { detail: connection }))
|
|
287
342
|
}
|
|
288
343
|
|
|
289
344
|
/**
|
|
@@ -312,35 +367,64 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
312
367
|
}
|
|
313
368
|
}
|
|
314
369
|
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
370
|
+
getConnections (peerId?: PeerId): Connection[] {
|
|
371
|
+
if (peerId != null) {
|
|
372
|
+
return this.connections.get(peerId.toString()) ?? []
|
|
373
|
+
}
|
|
318
374
|
|
|
319
|
-
|
|
320
|
-
let output: Connection[] = []
|
|
375
|
+
let conns: Connection[] = []
|
|
321
376
|
|
|
322
|
-
for (const
|
|
323
|
-
|
|
377
|
+
for (const c of this.connections.values()) {
|
|
378
|
+
conns = conns.concat(c)
|
|
324
379
|
}
|
|
325
380
|
|
|
326
|
-
return
|
|
381
|
+
return conns
|
|
327
382
|
}
|
|
328
383
|
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
384
|
+
async openConnection (peerId: PeerId, options?: AbortOptions): Promise<Connection> {
|
|
385
|
+
log('dial to %p', peerId)
|
|
386
|
+
const existingConnections = this.getConnections(peerId)
|
|
332
387
|
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
388
|
+
if (existingConnections.length > 0) {
|
|
389
|
+
log('had an existing connection to %p', peerId)
|
|
390
|
+
|
|
391
|
+
return existingConnections[0]
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
const connection = await this.dialer.dial(peerId, options)
|
|
395
|
+
let peerConnections = this.connections.get(peerId.toString())
|
|
396
|
+
|
|
397
|
+
if (peerConnections == null) {
|
|
398
|
+
peerConnections = []
|
|
399
|
+
this.connections.set(peerId.toString(), peerConnections)
|
|
400
|
+
}
|
|
338
401
|
|
|
339
|
-
|
|
340
|
-
|
|
402
|
+
// we get notified of connections via the Upgrader emitting "connection"
|
|
403
|
+
// events, double check we aren't already tracking this connection before
|
|
404
|
+
// storing it
|
|
405
|
+
let trackedConnection = false
|
|
406
|
+
|
|
407
|
+
for (const conn of peerConnections) {
|
|
408
|
+
if (conn.id === connection.id) {
|
|
409
|
+
trackedConnection = true
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
if (!trackedConnection) {
|
|
414
|
+
peerConnections.push(connection)
|
|
341
415
|
}
|
|
342
416
|
|
|
343
|
-
return
|
|
417
|
+
return connection
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
async closeConnections (peerId: PeerId): Promise<void> {
|
|
421
|
+
const connections = this.connections.get(peerId.toString()) ?? []
|
|
422
|
+
|
|
423
|
+
await Promise.all(
|
|
424
|
+
connections.map(async connection => {
|
|
425
|
+
return await connection.close()
|
|
426
|
+
})
|
|
427
|
+
)
|
|
344
428
|
}
|
|
345
429
|
|
|
346
430
|
/**
|
|
@@ -378,7 +462,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
378
462
|
* If the `value` of `name` has exceeded its limit, maybe close a connection
|
|
379
463
|
*/
|
|
380
464
|
async _checkMaxLimit (name: keyof ConnectionManagerInit, value: number) {
|
|
381
|
-
const limit = this.
|
|
465
|
+
const limit = this.opts[name]
|
|
382
466
|
log.trace('checking limit of %s. current value: %d of %d', name, value, limit)
|
|
383
467
|
if (value > limit) {
|
|
384
468
|
log('%s: limit exceeded: %p, %d', this.components.getPeerId(), name, value)
|
|
@@ -391,7 +475,7 @@ export class DefaultConnectionManager extends EventEmitter<ConnectionManagerEven
|
|
|
391
475
|
* to the lowest valued peer.
|
|
392
476
|
*/
|
|
393
477
|
async _maybeDisconnectOne () {
|
|
394
|
-
if (this.
|
|
478
|
+
if (this.opts.minConnections < this.connections.size) {
|
|
395
479
|
const peerValues = Array.from(new Map([...this.peerValues.entries()].sort((a, b) => a[1] - b[1])))
|
|
396
480
|
|
|
397
481
|
log('%p: sorted peer values: %j', this.components.getPeerId(), peerValues)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type { DualDHT, QueryEvent, SingleDHT } from '@libp2p/interfaces/dht'
|
|
2
|
+
import type { PeerDiscoveryEvents } from '@libp2p/interfaces/peer-discovery'
|
|
3
|
+
import errCode from 'err-code'
|
|
4
|
+
import { messages, codes } from '../errors.js'
|
|
5
|
+
import { EventEmitter } from '@libp2p/interfaces'
|
|
6
|
+
|
|
7
|
+
export class DummyDHT extends EventEmitter<PeerDiscoveryEvents> implements DualDHT {
|
|
8
|
+
get wan (): SingleDHT {
|
|
9
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
get lan (): SingleDHT {
|
|
13
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
get (): AsyncIterable<QueryEvent> {
|
|
17
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
findProviders (): AsyncIterable<QueryEvent> {
|
|
21
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
findPeer (): AsyncIterable<QueryEvent> {
|
|
25
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
getClosestPeers (): AsyncIterable<QueryEvent> {
|
|
29
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
provide (): AsyncIterable<QueryEvent> {
|
|
33
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
put (): AsyncIterable<QueryEvent> {
|
|
37
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async getMode (): Promise<'client' | 'server'> {
|
|
41
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async setMode (): Promise<void> {
|
|
45
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async refreshRoutingTable (): Promise<void> {
|
|
49
|
+
throw errCode(new Error(messages.DHT_DISABLED), codes.DHT_DISABLED)
|
|
50
|
+
}
|
|
51
|
+
}
|
package/src/errors.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
export enum messages {
|
|
2
2
|
NOT_STARTED_YET = 'The libp2p node is not started yet',
|
|
3
3
|
DHT_DISABLED = 'DHT is not available',
|
|
4
|
+
PUBSUB_DISABLED = 'PubSub is not available',
|
|
4
5
|
CONN_ENCRYPTION_REQUIRED = 'At least one connection encryption module is required',
|
|
5
6
|
ERR_TRANSPORTS_REQUIRED = 'At least one transport module is required',
|
|
6
7
|
ERR_PROTECTOR_REQUIRED = 'Private network is enforced, but no protector was provided',
|
|
@@ -9,6 +10,7 @@ export enum messages {
|
|
|
9
10
|
|
|
10
11
|
export enum codes {
|
|
11
12
|
DHT_DISABLED = 'ERR_DHT_DISABLED',
|
|
13
|
+
ERR_PUBSUB_DISABLED = 'ERR_PUBSUB_DISABLED',
|
|
12
14
|
PUBSUB_NOT_STARTED = 'ERR_PUBSUB_NOT_STARTED',
|
|
13
15
|
DHT_NOT_STARTED = 'ERR_DHT_NOT_STARTED',
|
|
14
16
|
CONN_ENCRYPTION_REQUIRED = 'ERR_CONN_ENCRYPTION_REQUIRED',
|
package/src/fetch/index.ts
CHANGED
|
@@ -70,13 +70,12 @@ export class FetchService implements Startable {
|
|
|
70
70
|
async fetch (peer: PeerId, key: string): Promise<Uint8Array | null> {
|
|
71
71
|
log('dialing %s to %p', this.protocol, peer)
|
|
72
72
|
|
|
73
|
-
const connection = await this.components.
|
|
73
|
+
const connection = await this.components.getConnectionManager().openConnection(peer)
|
|
74
74
|
const { stream } = await connection.newStream([this.protocol])
|
|
75
75
|
const shake = handshake(stream)
|
|
76
76
|
|
|
77
77
|
// send message
|
|
78
|
-
|
|
79
|
-
shake.write(lp.encode.single(FetchRequest.encode(request).finish()).slice())
|
|
78
|
+
shake.write(lp.encode.single(FetchRequest.encode({ identifier: key })).slice())
|
|
80
79
|
|
|
81
80
|
// read response
|
|
82
81
|
// @ts-expect-error fromReader returns a Source which has no .next method
|
|
@@ -109,21 +108,21 @@ export class FetchService implements Startable {
|
|
|
109
108
|
// @ts-expect-error fromReader returns a Source which has no .next method
|
|
110
109
|
const request = FetchRequest.decode((await lp.decode.fromReader(shake.reader).next()).value.slice())
|
|
111
110
|
|
|
112
|
-
let response
|
|
111
|
+
let response: FetchResponse
|
|
113
112
|
const lookup = this._getLookupFunction(request.identifier)
|
|
114
113
|
if (lookup != null) {
|
|
115
114
|
const data = await lookup(request.identifier)
|
|
116
115
|
if (data != null) {
|
|
117
|
-
response =
|
|
116
|
+
response = { status: FetchResponse.StatusCode.OK, data }
|
|
118
117
|
} else {
|
|
119
|
-
response =
|
|
118
|
+
response = { status: FetchResponse.StatusCode.NOT_FOUND, data: new Uint8Array(0) }
|
|
120
119
|
}
|
|
121
120
|
} else {
|
|
122
121
|
const errmsg = (new TextEncoder()).encode('No lookup function registered for key: ' + request.identifier)
|
|
123
|
-
response =
|
|
122
|
+
response = { status: FetchResponse.StatusCode.ERROR, data: errmsg }
|
|
124
123
|
}
|
|
125
124
|
|
|
126
|
-
shake.write(lp.encode.single(FetchResponse.encode(response)
|
|
125
|
+
shake.write(lp.encode.single(FetchResponse.encode(response)).slice())
|
|
127
126
|
}
|
|
128
127
|
|
|
129
128
|
/**
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/* eslint-disable import/export */
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-namespace */
|
|
3
|
+
|
|
4
|
+
import { encodeMessage, decodeMessage, message, string, enumeration, bytes } from 'protons-runtime'
|
|
5
|
+
|
|
6
|
+
export interface FetchRequest {
|
|
7
|
+
identifier: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export namespace FetchRequest {
|
|
11
|
+
export const codec = () => {
|
|
12
|
+
return message<FetchRequest>({
|
|
13
|
+
1: { name: 'identifier', codec: string }
|
|
14
|
+
})
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export const encode = (obj: FetchRequest): Uint8Array => {
|
|
18
|
+
return encodeMessage(obj, FetchRequest.codec())
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const decode = (buf: Uint8Array): FetchRequest => {
|
|
22
|
+
return decodeMessage(buf, FetchRequest.codec())
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface FetchResponse {
|
|
27
|
+
status: FetchResponse.StatusCode
|
|
28
|
+
data: Uint8Array
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export namespace FetchResponse {
|
|
32
|
+
export enum StatusCode {
|
|
33
|
+
OK = 'OK',
|
|
34
|
+
NOT_FOUND = 'NOT_FOUND',
|
|
35
|
+
ERROR = 'ERROR'
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export namespace StatusCode {
|
|
39
|
+
export const codec = () => {
|
|
40
|
+
return enumeration<typeof StatusCode>(StatusCode)
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export const codec = () => {
|
|
45
|
+
return message<FetchResponse>({
|
|
46
|
+
1: { name: 'status', codec: FetchResponse.StatusCode.codec() },
|
|
47
|
+
2: { name: 'data', codec: bytes }
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export const encode = (obj: FetchResponse): Uint8Array => {
|
|
52
|
+
return encodeMessage(obj, FetchResponse.codec())
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export const decode = (buf: Uint8Array): FetchResponse => {
|
|
56
|
+
return decodeMessage(buf, FetchResponse.codec())
|
|
57
|
+
}
|
|
58
|
+
}
|