libp2p 0.37.3-b1b2b21 → 0.37.3-ba56c64
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/circuit/hop.d.ts +3 -2
- package/dist/src/circuit/circuit/hop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/hop.js +8 -4
- package/dist/src/circuit/circuit/hop.js.map +1 -1
- package/dist/src/circuit/circuit/stop.d.ts +2 -1
- package/dist/src/circuit/circuit/stop.d.ts.map +1 -1
- package/dist/src/circuit/circuit/stop.js +4 -2
- package/dist/src/circuit/circuit/stop.js.map +1 -1
- package/dist/src/circuit/index.d.ts +2 -10
- package/dist/src/circuit/index.d.ts.map +1 -1
- package/dist/src/circuit/index.js.map +1 -1
- package/dist/src/circuit/transport.d.ts +3 -0
- package/dist/src/circuit/transport.d.ts.map +1 -1
- package/dist/src/circuit/transport.js +73 -57
- package/dist/src/circuit/transport.js.map +1 -1
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +19 -4
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/dialer/index.d.ts +4 -4
- package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
- package/dist/src/connection-manager/dialer/index.js +10 -10
- package/dist/src/connection-manager/dialer/index.js.map +1 -1
- package/dist/src/connection-manager/index.d.ts +15 -1
- package/dist/src/connection-manager/index.d.ts.map +1 -1
- package/dist/src/connection-manager/index.js +135 -23
- package/dist/src/connection-manager/index.js.map +1 -1
- package/dist/src/constants.d.ts +4 -0
- package/dist/src/constants.d.ts.map +1 -1
- package/dist/src/constants.js +4 -0
- package/dist/src/constants.js.map +1 -1
- package/dist/src/fetch/index.d.ts +4 -0
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +19 -6
- package/dist/src/fetch/index.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 +13 -11
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +1 -0
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +3 -13
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/metrics/index.d.ts +2 -2
- package/dist/src/metrics/index.d.ts.map +1 -1
- package/dist/src/metrics/index.js +6 -2
- package/dist/src/metrics/index.js.map +1 -1
- package/dist/src/ping/index.d.ts +4 -0
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +20 -7
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/upgrader.d.ts +6 -0
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +83 -59
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/version.d.ts.map +1 -1
- package/dist/src/version.js.map +1 -1
- package/package.json +13 -12
- package/src/circuit/circuit/hop.ts +13 -6
- package/src/circuit/circuit/stop.ts +7 -3
- package/src/circuit/index.ts +2 -11
- package/src/circuit/transport.ts +79 -58
- package/src/config.ts +19 -4
- package/src/connection-manager/dialer/index.ts +10 -10
- package/src/connection-manager/index.ts +178 -26
- package/src/constants.ts +5 -0
- package/src/fetch/index.ts +28 -8
- package/src/identify/index.ts +16 -14
- package/src/index.ts +1 -0
- package/src/libp2p.ts +3 -14
- package/src/metrics/index.ts +8 -4
- package/src/ping/index.ts +30 -10
- package/src/upgrader.ts +95 -61
- package/src/version.ts +1 -2
package/src/metrics/index.ts
CHANGED
|
@@ -3,7 +3,7 @@ import each from 'it-foreach'
|
|
|
3
3
|
import LRU from 'hashlru'
|
|
4
4
|
import { METRICS as defaultOptions } from '../constants.js'
|
|
5
5
|
import { DefaultStats, StatsInit } from './stats.js'
|
|
6
|
-
import type { ComponentMetricsUpdate, Metrics, Stats, TrackStreamOptions } from '@libp2p/interface-metrics'
|
|
6
|
+
import type { ComponentMetricsUpdate, Metrics, Stats, TrackedMetric, TrackStreamOptions } from '@libp2p/interface-metrics'
|
|
7
7
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
8
8
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
9
9
|
import type { Duplex } from 'it-stream-types'
|
|
@@ -41,7 +41,7 @@ export class DefaultMetrics implements Metrics, Startable {
|
|
|
41
41
|
private readonly protocolStats: Map<string, DefaultStats>
|
|
42
42
|
private readonly oldPeers: ReturnType<typeof LRU>
|
|
43
43
|
private running: boolean
|
|
44
|
-
private readonly systems: Map<string, Map<string, Map<string,
|
|
44
|
+
private readonly systems: Map<string, Map<string, Map<string, TrackedMetric>>>
|
|
45
45
|
private readonly statsInit: StatsInit
|
|
46
46
|
|
|
47
47
|
constructor (init: MetricsInit) {
|
|
@@ -115,7 +115,7 @@ export class DefaultMetrics implements Metrics, Startable {
|
|
|
115
115
|
}
|
|
116
116
|
|
|
117
117
|
updateComponentMetric (update: ComponentMetricsUpdate) {
|
|
118
|
-
const { system = 'libp2p', component, metric, value } = update
|
|
118
|
+
const { system = 'libp2p', component, metric, value, label, help } = update
|
|
119
119
|
|
|
120
120
|
if (!this.systems.has(system)) {
|
|
121
121
|
this.systems.set(system, new Map())
|
|
@@ -137,7 +137,11 @@ export class DefaultMetrics implements Metrics, Startable {
|
|
|
137
137
|
throw new Error('Unknown metric component')
|
|
138
138
|
}
|
|
139
139
|
|
|
140
|
-
componentMetrics.set(metric,
|
|
140
|
+
componentMetrics.set(metric, {
|
|
141
|
+
label,
|
|
142
|
+
help,
|
|
143
|
+
calculate: typeof value !== 'function' ? () => value : value
|
|
144
|
+
})
|
|
141
145
|
}
|
|
142
146
|
|
|
143
147
|
/**
|
package/src/ping/index.ts
CHANGED
|
@@ -11,8 +11,9 @@ import type { PeerId } from '@libp2p/interface-peer-id'
|
|
|
11
11
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
12
12
|
import type { Components } from '@libp2p/components'
|
|
13
13
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
14
|
-
import type { Duplex } from 'it-stream-types'
|
|
15
14
|
import { abortableDuplex } from 'abortable-iterator'
|
|
15
|
+
import { TimeoutController } from 'timeout-abort-controller'
|
|
16
|
+
import type { Stream } from '@libp2p/interface-connection'
|
|
16
17
|
|
|
17
18
|
const log = logger('libp2p:ping')
|
|
18
19
|
|
|
@@ -20,6 +21,11 @@ export interface PingServiceInit {
|
|
|
20
21
|
protocolPrefix: string
|
|
21
22
|
maxInboundStreams: number
|
|
22
23
|
maxOutboundStreams: number
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* How long we should wait for a ping response
|
|
27
|
+
*/
|
|
28
|
+
timeout: number
|
|
23
29
|
}
|
|
24
30
|
|
|
25
31
|
export class PingService implements Startable {
|
|
@@ -73,19 +79,27 @@ export class PingService implements Startable {
|
|
|
73
79
|
async ping (peer: PeerId, options: AbortOptions = {}): Promise<number> {
|
|
74
80
|
log('dialing %s to %p', this.protocol, peer)
|
|
75
81
|
|
|
76
|
-
const connection = await this.components.getConnectionManager().openConnection(peer, options)
|
|
77
|
-
const stream = await connection.newStream([this.protocol], options)
|
|
78
82
|
const start = Date.now()
|
|
79
83
|
const data = randomBytes(PING_LENGTH)
|
|
80
|
-
|
|
81
|
-
let
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
const connection = await this.components.getConnectionManager().openConnection(peer, options)
|
|
85
|
+
let timeoutController
|
|
86
|
+
let signal = options.signal
|
|
87
|
+
let stream: Stream | undefined
|
|
88
|
+
|
|
89
|
+
// create a timeout if no abort signal passed
|
|
90
|
+
if (signal == null) {
|
|
91
|
+
timeoutController = new TimeoutController(this.init.timeout)
|
|
92
|
+
signal = timeoutController.signal
|
|
86
93
|
}
|
|
87
94
|
|
|
88
95
|
try {
|
|
96
|
+
stream = await connection.newStream([this.protocol], {
|
|
97
|
+
signal
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
// make stream abortable
|
|
101
|
+
const source = abortableDuplex(stream, signal)
|
|
102
|
+
|
|
89
103
|
const result = await pipe(
|
|
90
104
|
[data],
|
|
91
105
|
source,
|
|
@@ -99,7 +113,13 @@ export class PingService implements Startable {
|
|
|
99
113
|
|
|
100
114
|
return end - start
|
|
101
115
|
} finally {
|
|
102
|
-
|
|
116
|
+
if (timeoutController != null) {
|
|
117
|
+
timeoutController.clear()
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
if (stream != null) {
|
|
121
|
+
stream.close()
|
|
122
|
+
}
|
|
103
123
|
}
|
|
104
124
|
}
|
|
105
125
|
}
|
package/src/upgrader.ts
CHANGED
|
@@ -18,6 +18,8 @@ import { Components, isInitializable } from '@libp2p/components'
|
|
|
18
18
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
19
19
|
import type { Registrar } from '@libp2p/interface-registrar'
|
|
20
20
|
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
|
|
21
|
+
import { TimeoutController } from 'timeout-abort-controller'
|
|
22
|
+
import { abortableDuplex } from 'abortable-iterator'
|
|
21
23
|
|
|
22
24
|
const log = logger('libp2p:upgrader')
|
|
23
25
|
|
|
@@ -43,6 +45,12 @@ export interface CryptoResult extends SecuredConnection {
|
|
|
43
45
|
export interface UpgraderInit {
|
|
44
46
|
connectionEncryption: ConnectionEncrypter[]
|
|
45
47
|
muxers: StreamMuxerFactory[]
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* An amount of ms by which an inbound connection upgrade
|
|
51
|
+
* must complete
|
|
52
|
+
*/
|
|
53
|
+
inboundUpgradeTimeout: number
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
function findIncomingStreamLimit (protocol: string, registrar: Registrar) {
|
|
@@ -89,6 +97,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
89
97
|
private readonly components: Components
|
|
90
98
|
private readonly connectionEncryption: Map<string, ConnectionEncrypter>
|
|
91
99
|
private readonly muxers: Map<string, StreamMuxerFactory>
|
|
100
|
+
private readonly inboundUpgradeTimeout: number
|
|
92
101
|
|
|
93
102
|
constructor (components: Components, init: UpgraderInit) {
|
|
94
103
|
super()
|
|
@@ -105,6 +114,8 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
105
114
|
init.muxers.forEach(muxer => {
|
|
106
115
|
this.muxers.set(muxer.protocol, muxer)
|
|
107
116
|
})
|
|
117
|
+
|
|
118
|
+
this.inboundUpgradeTimeout = init.inboundUpgradeTimeout
|
|
108
119
|
}
|
|
109
120
|
|
|
110
121
|
/**
|
|
@@ -120,82 +131,92 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
120
131
|
let proxyPeer
|
|
121
132
|
const metrics = this.components.getMetrics()
|
|
122
133
|
|
|
123
|
-
|
|
124
|
-
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
125
|
-
}
|
|
134
|
+
const timeoutController = new TimeoutController(this.inboundUpgradeTimeout)
|
|
126
135
|
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
|
|
132
|
-
}
|
|
136
|
+
try {
|
|
137
|
+
const abortableStream = abortableDuplex(maConn, timeoutController.signal)
|
|
138
|
+
maConn.source = abortableStream.source
|
|
139
|
+
maConn.sink = abortableStream.sink
|
|
133
140
|
|
|
134
|
-
|
|
141
|
+
if (await this.components.getConnectionGater().denyInboundConnection(maConn)) {
|
|
142
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
143
|
+
}
|
|
135
144
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
145
|
+
if (metrics != null) {
|
|
146
|
+
({ setTarget: setPeer, proxy: proxyPeer } = mutableProxy())
|
|
147
|
+
const idString = `${(Math.random() * 1e9).toString(36)}${Date.now()}`
|
|
148
|
+
setPeer({ toString: () => idString })
|
|
149
|
+
maConn = metrics.trackStream({ stream: maConn, remotePeer: proxyPeer })
|
|
150
|
+
}
|
|
139
151
|
|
|
140
|
-
|
|
141
|
-
log('protecting the inbound connection')
|
|
142
|
-
protectedConn = await protector.protect(maConn)
|
|
143
|
-
}
|
|
152
|
+
log('starting the inbound connection upgrade')
|
|
144
153
|
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
(
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
154
|
+
// Protect
|
|
155
|
+
let protectedConn = maConn
|
|
156
|
+
const protector = this.components.getConnectionProtector()
|
|
157
|
+
|
|
158
|
+
if (protector != null) {
|
|
159
|
+
log('protecting the inbound connection')
|
|
160
|
+
protectedConn = await protector.protect(maConn)
|
|
161
|
+
}
|
|
152
162
|
|
|
153
|
-
|
|
163
|
+
try {
|
|
164
|
+
// Encrypt the connection
|
|
165
|
+
({
|
|
166
|
+
conn: encryptedConn,
|
|
167
|
+
remotePeer,
|
|
168
|
+
protocol: cryptoProtocol
|
|
169
|
+
} = await this._encryptInbound(protectedConn))
|
|
170
|
+
|
|
171
|
+
if (await this.components.getConnectionGater().denyInboundEncryptedConnection(remotePeer, {
|
|
172
|
+
...protectedConn,
|
|
173
|
+
...encryptedConn
|
|
174
|
+
})) {
|
|
175
|
+
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
// Multiplex the connection
|
|
179
|
+
if (this.muxers.size > 0) {
|
|
180
|
+
const multiplexed = await this._multiplexInbound({
|
|
181
|
+
...protectedConn,
|
|
182
|
+
...encryptedConn
|
|
183
|
+
}, this.muxers)
|
|
184
|
+
muxerFactory = multiplexed.muxerFactory
|
|
185
|
+
upgradedConn = multiplexed.stream
|
|
186
|
+
} else {
|
|
187
|
+
upgradedConn = encryptedConn
|
|
188
|
+
}
|
|
189
|
+
} catch (err: any) {
|
|
190
|
+
log.error('Failed to upgrade inbound connection', err)
|
|
191
|
+
await maConn.close(err)
|
|
192
|
+
throw err
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (await this.components.getConnectionGater().denyInboundUpgradedConnection(remotePeer, {
|
|
154
196
|
...protectedConn,
|
|
155
197
|
...encryptedConn
|
|
156
198
|
})) {
|
|
157
199
|
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
158
200
|
}
|
|
159
201
|
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
...protectedConn,
|
|
164
|
-
...encryptedConn
|
|
165
|
-
}, this.muxers)
|
|
166
|
-
muxerFactory = multiplexed.muxerFactory
|
|
167
|
-
upgradedConn = multiplexed.stream
|
|
168
|
-
} else {
|
|
169
|
-
upgradedConn = encryptedConn
|
|
202
|
+
if (metrics != null) {
|
|
203
|
+
metrics.updatePlaceholder(proxyPeer, remotePeer)
|
|
204
|
+
setPeer(remotePeer)
|
|
170
205
|
}
|
|
171
|
-
} catch (err: any) {
|
|
172
|
-
log.error('Failed to upgrade inbound connection', err)
|
|
173
|
-
await maConn.close(err)
|
|
174
|
-
throw err
|
|
175
|
-
}
|
|
176
206
|
|
|
177
|
-
|
|
178
|
-
...protectedConn,
|
|
179
|
-
...encryptedConn
|
|
180
|
-
})) {
|
|
181
|
-
throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
|
|
182
|
-
}
|
|
207
|
+
log('Successfully upgraded inbound connection')
|
|
183
208
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
209
|
+
return this._createConnection({
|
|
210
|
+
cryptoProtocol,
|
|
211
|
+
direction: 'inbound',
|
|
212
|
+
maConn,
|
|
213
|
+
upgradedConn,
|
|
214
|
+
muxerFactory,
|
|
215
|
+
remotePeer
|
|
216
|
+
})
|
|
217
|
+
} finally {
|
|
218
|
+
timeoutController.clear()
|
|
187
219
|
}
|
|
188
|
-
|
|
189
|
-
log('Successfully upgraded inbound connection')
|
|
190
|
-
|
|
191
|
-
return this._createConnection({
|
|
192
|
-
cryptoProtocol,
|
|
193
|
-
direction: 'inbound',
|
|
194
|
-
maConn,
|
|
195
|
-
upgradedConn,
|
|
196
|
-
muxerFactory,
|
|
197
|
-
remotePeer
|
|
198
|
-
})
|
|
199
220
|
}
|
|
200
221
|
|
|
201
222
|
/**
|
|
@@ -315,6 +336,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
315
336
|
if (muxerFactory != null) {
|
|
316
337
|
// Create the muxer
|
|
317
338
|
muxer = muxerFactory.createStreamMuxer({
|
|
339
|
+
direction,
|
|
318
340
|
// Run anytime a remote stream is created
|
|
319
341
|
onIncomingStream: muxedStream => {
|
|
320
342
|
if (connection == null) {
|
|
@@ -377,8 +399,16 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
377
399
|
const muxedStream = muxer.newStream()
|
|
378
400
|
const mss = new Dialer(muxedStream)
|
|
379
401
|
const metrics = this.components.getMetrics()
|
|
402
|
+
let controller: TimeoutController | undefined
|
|
380
403
|
|
|
381
404
|
try {
|
|
405
|
+
if (options.signal == null) {
|
|
406
|
+
log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
|
|
407
|
+
|
|
408
|
+
controller = new TimeoutController(30000)
|
|
409
|
+
options.signal = controller.signal
|
|
410
|
+
}
|
|
411
|
+
|
|
382
412
|
let { stream, protocol } = await mss.select(protocols, options)
|
|
383
413
|
|
|
384
414
|
if (metrics != null) {
|
|
@@ -414,6 +444,10 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
414
444
|
}
|
|
415
445
|
|
|
416
446
|
throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
|
|
447
|
+
} finally {
|
|
448
|
+
if (controller != null) {
|
|
449
|
+
controller.clear()
|
|
450
|
+
}
|
|
417
451
|
}
|
|
418
452
|
}
|
|
419
453
|
|
|
@@ -469,7 +503,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
|
|
|
469
503
|
await maConn.close()
|
|
470
504
|
// Ensure remaining streams are closed
|
|
471
505
|
if (muxer != null) {
|
|
472
|
-
muxer.
|
|
506
|
+
muxer.close()
|
|
473
507
|
}
|
|
474
508
|
}
|
|
475
509
|
})
|
package/src/version.ts
CHANGED