libp2p 0.44.0-53b1645c → 0.44.0-59f34a21
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/index.min.js +14 -14
- package/dist/src/autonat/constants.d.ts +16 -2
- package/dist/src/autonat/constants.d.ts.map +1 -1
- package/dist/src/autonat/constants.js +16 -2
- package/dist/src/autonat/constants.js.map +1 -1
- package/dist/src/autonat/index.d.ts +9 -30
- package/dist/src/autonat/index.d.ts.map +1 -1
- package/dist/src/autonat/index.js +35 -29
- package/dist/src/autonat/index.js.map +1 -1
- package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/server/index.js +3 -0
- package/dist/src/circuit-relay/server/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/discovery.js +2 -2
- package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
- package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/index.js +4 -6
- package/dist/src/circuit-relay/transport/index.js.map +1 -1
- package/dist/src/circuit-relay/transport/listener.d.ts +0 -3
- package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/listener.js +27 -24
- package/dist/src/circuit-relay/transport/listener.js.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.d.ts +2 -4
- package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
- package/dist/src/circuit-relay/transport/reservation-store.js +28 -26
- package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
- package/dist/src/config/connection-gater.browser.d.ts +8 -0
- package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
- package/dist/src/config/connection-gater.browser.js +27 -0
- package/dist/src/config/connection-gater.browser.js.map +1 -0
- package/dist/src/config/connection-gater.d.ts +6 -0
- package/dist/src/config/connection-gater.d.ts.map +1 -0
- package/dist/src/config/connection-gater.js +18 -0
- package/dist/src/config/connection-gater.js.map +1 -0
- package/dist/src/config.d.ts.map +1 -1
- package/dist/src/config.js +0 -55
- package/dist/src/config.js.map +1 -1
- package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
- package/dist/src/connection-manager/auto-dial.js +7 -2
- package/dist/src/connection-manager/auto-dial.js.map +1 -1
- package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
- package/dist/src/connection-manager/dial-queue.js +8 -10
- package/dist/src/connection-manager/dial-queue.js.map +1 -1
- package/dist/src/fetch/index.d.ts.map +1 -1
- package/dist/src/fetch/index.js +2 -8
- package/dist/src/fetch/index.js.map +1 -1
- package/dist/src/identify/index.d.ts.map +1 -1
- package/dist/src/identify/index.js +14 -24
- package/dist/src/identify/index.js.map +1 -1
- package/dist/src/index.d.ts +2 -6
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +2 -1
- package/dist/src/index.js.map +1 -1
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +2 -13
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/peer-routing.d.ts +1 -38
- package/dist/src/peer-routing.d.ts.map +1 -1
- package/dist/src/peer-routing.js +0 -59
- package/dist/src/peer-routing.js.map +1 -1
- package/dist/src/ping/index.d.ts.map +1 -1
- package/dist/src/ping/index.js +7 -9
- package/dist/src/ping/index.js.map +1 -1
- package/dist/src/transport-manager.d.ts +5 -1
- package/dist/src/transport-manager.d.ts.map +1 -1
- package/dist/src/transport-manager.js +10 -1
- package/dist/src/transport-manager.js.map +1 -1
- package/dist/src/upgrader.d.ts.map +1 -1
- package/dist/src/upgrader.js +10 -16
- package/dist/src/upgrader.js.map +1 -1
- package/dist/src/upnp-nat/index.d.ts +2 -2
- package/dist/src/upnp-nat/index.d.ts.map +1 -1
- package/dist/src/upnp-nat/index.js +1 -1
- package/dist/src/upnp-nat/index.js.map +1 -1
- package/package.json +8 -9
- package/src/autonat/constants.ts +17 -2
- package/src/autonat/index.ts +49 -41
- package/src/circuit-relay/server/index.ts +4 -0
- package/src/circuit-relay/transport/discovery.ts +2 -2
- package/src/circuit-relay/transport/index.ts +4 -7
- package/src/circuit-relay/transport/listener.ts +35 -29
- package/src/circuit-relay/transport/reservation-store.ts +21 -15
- package/src/config/connection-gater.browser.ts +31 -0
- package/src/config/connection-gater.ts +19 -0
- package/src/config.ts +0 -55
- package/src/connection-manager/auto-dial.ts +8 -2
- package/src/connection-manager/dial-queue.ts +10 -16
- package/src/fetch/index.ts +2 -9
- package/src/identify/index.ts +16 -26
- package/src/index.ts +2 -7
- package/src/libp2p.ts +2 -13
- package/src/peer-routing.ts +1 -101
- package/src/ping/index.ts +7 -9
- package/src/transport-manager.ts +13 -1
- package/src/upgrader.ts +10 -16
- package/src/upnp-nat/index.ts +2 -2
- package/src/version.ts +1 -1
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { logger } from '@libp2p/logger'
|
|
2
2
|
import { AbortError, CodeError } from '@libp2p/interfaces/errors'
|
|
3
3
|
import { Multiaddr, Resolver, resolvers } from '@multiformats/multiaddr'
|
|
4
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
5
4
|
import { publicAddressesFirst } from '@libp2p/utils/address-sort'
|
|
6
5
|
import { codes } from '../errors.js'
|
|
7
6
|
import {
|
|
@@ -22,7 +21,7 @@ import PQueue from 'p-queue'
|
|
|
22
21
|
import { dnsaddrResolver } from '@multiformats/multiaddr/resolvers'
|
|
23
22
|
import { combineSignals, resolveMultiaddrs } from './utils.js'
|
|
24
23
|
import pDefer from 'p-defer'
|
|
25
|
-
import
|
|
24
|
+
import { ClearableSignal, anySignal } from 'any-signal'
|
|
26
25
|
import type { AddressSorter } from '@libp2p/interface-libp2p'
|
|
27
26
|
|
|
28
27
|
const log = logger('libp2p:connection-manager:dial-queue')
|
|
@@ -176,7 +175,7 @@ export class DialQueue {
|
|
|
176
175
|
|
|
177
176
|
// create abort conditions - need to do this before `calculateMultiaddrs` as we may be about to
|
|
178
177
|
// resolve a dns addr which can time out
|
|
179
|
-
const
|
|
178
|
+
const signal = this.createDialAbortControllers(options.signal)
|
|
180
179
|
let addrsToDial: Address[]
|
|
181
180
|
|
|
182
181
|
try {
|
|
@@ -187,7 +186,6 @@ export class DialQueue {
|
|
|
187
186
|
})
|
|
188
187
|
} catch (err) {
|
|
189
188
|
signal.clear()
|
|
190
|
-
timeoutController.clear()
|
|
191
189
|
throw err
|
|
192
190
|
}
|
|
193
191
|
|
|
@@ -210,7 +208,6 @@ export class DialQueue {
|
|
|
210
208
|
if (existingDial != null) {
|
|
211
209
|
log('joining existing dial target for %p', peerId)
|
|
212
210
|
signal.clear()
|
|
213
|
-
timeoutController.clear()
|
|
214
211
|
return await existingDial.promise
|
|
215
212
|
}
|
|
216
213
|
|
|
@@ -233,13 +230,12 @@ export class DialQueue {
|
|
|
233
230
|
|
|
234
231
|
// clean up abort signals/controllers
|
|
235
232
|
signal.clear()
|
|
236
|
-
timeoutController.clear()
|
|
237
233
|
})
|
|
238
234
|
.catch(err => {
|
|
239
235
|
log.error('dial failed to %s', addrsToDial.map(({ multiaddr }) => multiaddr.toString()).join(', '), err)
|
|
240
236
|
|
|
241
237
|
// Error is a timeout
|
|
242
|
-
if (
|
|
238
|
+
if (signal.aborted) {
|
|
243
239
|
const error = new CodeError(err.message, codes.ERR_TIMEOUT)
|
|
244
240
|
throw error
|
|
245
241
|
}
|
|
@@ -253,18 +249,16 @@ export class DialQueue {
|
|
|
253
249
|
return await pendingDial.promise
|
|
254
250
|
}
|
|
255
251
|
|
|
256
|
-
private createDialAbortControllers (userSignal?: AbortSignal):
|
|
257
|
-
// ensure we throw if the dial takes longer than the dial timeout
|
|
258
|
-
const timeoutController = new TimeoutController(this.dialTimeout)
|
|
259
|
-
|
|
252
|
+
private createDialAbortControllers (userSignal?: AbortSignal): ClearableSignal {
|
|
260
253
|
// let any signal abort the dial
|
|
261
|
-
const signal =
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
254
|
+
const signal = anySignal(
|
|
255
|
+
[AbortSignal.timeout(this.dialTimeout),
|
|
256
|
+
this.shutDownController.signal,
|
|
257
|
+
userSignal
|
|
258
|
+
]
|
|
265
259
|
)
|
|
266
260
|
|
|
267
|
-
return
|
|
261
|
+
return signal
|
|
268
262
|
}
|
|
269
263
|
|
|
270
264
|
private async calculateMultiaddrs (peerId?: PeerId, addrs: Address[] = [], options: DialOptions = {}): Promise<Address[]> {
|
package/src/fetch/index.ts
CHANGED
|
@@ -12,7 +12,6 @@ import type { AbortOptions } from '@libp2p/interfaces'
|
|
|
12
12
|
import { abortableDuplex } from 'abortable-iterator'
|
|
13
13
|
import { pipe } from 'it-pipe'
|
|
14
14
|
import first from 'it-first'
|
|
15
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
16
15
|
import { setMaxListeners } from 'events'
|
|
17
16
|
import { fromString as uint8arrayFromString } from 'uint8arrays/from-string'
|
|
18
17
|
import { toString as uint8arrayToString } from 'uint8arrays/to-string'
|
|
@@ -139,19 +138,17 @@ class DefaultFetchService implements Startable, FetchService {
|
|
|
139
138
|
log('dialing %s to %p', this.protocol, peer)
|
|
140
139
|
|
|
141
140
|
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
142
|
-
let timeoutController
|
|
143
141
|
let signal = options.signal
|
|
144
142
|
let stream: Stream | undefined
|
|
145
143
|
|
|
146
144
|
// create a timeout if no abort signal passed
|
|
147
145
|
if (signal == null) {
|
|
148
146
|
log('using default timeout of %d ms', this.init.timeout)
|
|
149
|
-
|
|
150
|
-
signal = timeoutController.signal
|
|
147
|
+
signal = AbortSignal.timeout(this.init.timeout ?? DEFAULT_TIMEOUT)
|
|
151
148
|
|
|
152
149
|
try {
|
|
153
150
|
// fails on node < 15.4
|
|
154
|
-
setMaxListeners?.(Infinity,
|
|
151
|
+
setMaxListeners?.(Infinity, signal)
|
|
155
152
|
} catch {}
|
|
156
153
|
}
|
|
157
154
|
|
|
@@ -203,10 +200,6 @@ class DefaultFetchService implements Startable, FetchService {
|
|
|
203
200
|
|
|
204
201
|
return result ?? null
|
|
205
202
|
} finally {
|
|
206
|
-
if (timeoutController != null) {
|
|
207
|
-
timeoutController.clear()
|
|
208
|
-
}
|
|
209
|
-
|
|
210
203
|
if (stream != null) {
|
|
211
204
|
stream.close()
|
|
212
205
|
}
|
package/src/identify/index.ts
CHANGED
|
@@ -22,7 +22,6 @@ import type { IncomingStreamData, Registrar } from '@libp2p/interface-registrar'
|
|
|
22
22
|
import type { Connection, Stream } from '@libp2p/interface-connection'
|
|
23
23
|
import type { Startable } from '@libp2p/interfaces/startable'
|
|
24
24
|
import { peerIdFromKeys } from '@libp2p/peer-id'
|
|
25
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
26
25
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
27
26
|
import { abortableDuplex } from 'abortable-iterator'
|
|
28
27
|
import { setMaxListeners } from 'events'
|
|
@@ -30,6 +29,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
|
30
29
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
31
30
|
import type { Peer, PeerStore } from '@libp2p/interface-peer-store'
|
|
32
31
|
import type { AddressManager } from '@libp2p/interface-address-manager'
|
|
32
|
+
import { anySignal } from 'any-signal'
|
|
33
33
|
import type { EventEmitter } from '@libp2p/interfaces/events'
|
|
34
34
|
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
35
35
|
import { toString as uint8ArrayToString } from 'uint8arrays/to-string'
|
|
@@ -233,20 +233,21 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
233
233
|
|
|
234
234
|
const pushes = connections.map(async connection => {
|
|
235
235
|
let stream: Stream | undefined
|
|
236
|
-
|
|
236
|
+
|
|
237
|
+
const signal = AbortSignal.timeout(this.timeout)
|
|
237
238
|
|
|
238
239
|
try {
|
|
239
240
|
// fails on node < 15.4
|
|
240
|
-
setMaxListeners?.(Infinity,
|
|
241
|
+
setMaxListeners?.(Infinity, signal)
|
|
241
242
|
} catch {}
|
|
242
243
|
|
|
243
244
|
try {
|
|
244
245
|
stream = await connection.newStream([this.identifyPushProtocolStr], {
|
|
245
|
-
signal
|
|
246
|
+
signal
|
|
246
247
|
})
|
|
247
248
|
|
|
248
249
|
// make stream abortable
|
|
249
|
-
const source = abortableDuplex(stream,
|
|
250
|
+
const source = abortableDuplex(stream, signal)
|
|
250
251
|
|
|
251
252
|
await source.sink(pipe(
|
|
252
253
|
[Identify.encode({
|
|
@@ -265,8 +266,6 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
265
266
|
if (stream != null) {
|
|
266
267
|
stream.close()
|
|
267
268
|
}
|
|
268
|
-
|
|
269
|
-
timeoutController.clear()
|
|
270
269
|
}
|
|
271
270
|
})
|
|
272
271
|
|
|
@@ -306,20 +305,14 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
306
305
|
}
|
|
307
306
|
|
|
308
307
|
async _identify (connection: Connection, options: AbortOptions = {}): Promise<Identify> {
|
|
309
|
-
let timeoutController
|
|
310
|
-
let signal = options.signal
|
|
311
308
|
let stream: Stream | undefined
|
|
312
309
|
|
|
313
|
-
|
|
314
|
-
if (signal == null) {
|
|
315
|
-
timeoutController = new TimeoutController(this.timeout)
|
|
316
|
-
signal = timeoutController.signal
|
|
310
|
+
const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])
|
|
317
311
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
312
|
+
try {
|
|
313
|
+
// fails on node < 15.4
|
|
314
|
+
setMaxListeners?.(Infinity, signal)
|
|
315
|
+
} catch {}
|
|
323
316
|
|
|
324
317
|
try {
|
|
325
318
|
stream = await connection.newStream([this.identifyProtocolStr], {
|
|
@@ -348,13 +341,10 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
348
341
|
throw new CodeError(String(err), codes.ERR_INVALID_MESSAGE)
|
|
349
342
|
}
|
|
350
343
|
} finally {
|
|
351
|
-
if (timeoutController != null) {
|
|
352
|
-
timeoutController.clear()
|
|
353
|
-
}
|
|
354
|
-
|
|
355
344
|
if (stream != null) {
|
|
356
345
|
stream.close()
|
|
357
346
|
}
|
|
347
|
+
signal.clear()
|
|
358
348
|
}
|
|
359
349
|
}
|
|
360
350
|
|
|
@@ -401,11 +391,12 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
401
391
|
*/
|
|
402
392
|
async _handleIdentify (data: IncomingStreamData): Promise<void> {
|
|
403
393
|
const { connection, stream } = data
|
|
404
|
-
|
|
394
|
+
|
|
395
|
+
const signal = AbortSignal.timeout(this.timeout)
|
|
405
396
|
|
|
406
397
|
try {
|
|
407
398
|
// fails on node < 15.4
|
|
408
|
-
setMaxListeners?.(Infinity,
|
|
399
|
+
setMaxListeners?.(Infinity, signal)
|
|
409
400
|
} catch {}
|
|
410
401
|
|
|
411
402
|
try {
|
|
@@ -435,7 +426,7 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
435
426
|
})
|
|
436
427
|
|
|
437
428
|
// make stream abortable
|
|
438
|
-
const source = abortableDuplex(stream,
|
|
429
|
+
const source = abortableDuplex(stream, signal)
|
|
439
430
|
|
|
440
431
|
const msgWithLenPrefix = pipe([message], (source) => lp.encode(source))
|
|
441
432
|
await source.sink(msgWithLenPrefix)
|
|
@@ -443,7 +434,6 @@ class DefaultIdentifyService implements Startable, IdentifyService {
|
|
|
443
434
|
log.error('could not respond to identify request', err)
|
|
444
435
|
} finally {
|
|
445
436
|
stream.close()
|
|
446
|
-
timeoutController.clear()
|
|
447
437
|
}
|
|
448
438
|
}
|
|
449
439
|
|
package/src/index.ts
CHANGED
|
@@ -32,7 +32,6 @@ import type { Components } from './components.js'
|
|
|
32
32
|
import type { Libp2p, ServiceMap } from '@libp2p/interface-libp2p'
|
|
33
33
|
import type { KeyChainInit } from '@libp2p/keychain'
|
|
34
34
|
import type { AddressManagerInit } from './address-manager/index.js'
|
|
35
|
-
import type { PeerRoutingInit } from './peer-routing.js'
|
|
36
35
|
import type { ConnectionManagerInit } from './connection-manager/index.js'
|
|
37
36
|
import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
|
|
38
37
|
|
|
@@ -81,11 +80,6 @@ export interface Libp2pInit<T extends ServiceMap = {}> {
|
|
|
81
80
|
*/
|
|
82
81
|
peerStore: PersistentPeerStoreInit
|
|
83
82
|
|
|
84
|
-
/**
|
|
85
|
-
* libp2p Peer routing service configuration
|
|
86
|
-
*/
|
|
87
|
-
peerRouting: PeerRoutingInit
|
|
88
|
-
|
|
89
83
|
/**
|
|
90
84
|
* keychain configuration
|
|
91
85
|
*/
|
|
@@ -134,11 +128,12 @@ export type Libp2pOptions<T extends ServiceMap = {}> = RecursivePartial<Libp2pIn
|
|
|
134
128
|
* import { tcp } from '@libp2p/tcp'
|
|
135
129
|
* import { mplex } from '@libp2p/mplex'
|
|
136
130
|
* import { noise } from '@chainsafe/libp2p-noise'
|
|
131
|
+
* import { yamux } from '@chainsafe/libp2p-yamux'
|
|
137
132
|
*
|
|
138
133
|
* // specify options
|
|
139
134
|
* const options = {
|
|
140
135
|
* transports: [tcp()],
|
|
141
|
-
* streamMuxers: [mplex()],
|
|
136
|
+
* streamMuxers: [yamux(), mplex()],
|
|
142
137
|
* connectionEncryption: [noise()]
|
|
143
138
|
* }
|
|
144
139
|
*
|
package/src/libp2p.ts
CHANGED
|
@@ -38,6 +38,7 @@ import { validateConfig } from './config.js'
|
|
|
38
38
|
import { ContentRouting, contentRouting } from '@libp2p/interface-content-routing'
|
|
39
39
|
import { PeerRouting, peerRouting } from '@libp2p/interface-peer-routing'
|
|
40
40
|
import { peerDiscovery } from '@libp2p/interface-peer-discovery'
|
|
41
|
+
import { connectionGater } from './config/connection-gater.js'
|
|
41
42
|
|
|
42
43
|
const log = logger('libp2p')
|
|
43
44
|
|
|
@@ -82,18 +83,7 @@ export class Libp2pNode<T extends ServiceMap = {}> extends EventEmitter<Libp2pEv
|
|
|
82
83
|
peerId: init.peerId,
|
|
83
84
|
events,
|
|
84
85
|
datastore: init.datastore ?? new MemoryDatastore(),
|
|
85
|
-
connectionGater:
|
|
86
|
-
denyDialPeer: async () => await Promise.resolve(false),
|
|
87
|
-
denyDialMultiaddr: async () => await Promise.resolve(false),
|
|
88
|
-
denyInboundConnection: async () => await Promise.resolve(false),
|
|
89
|
-
denyOutboundConnection: async () => await Promise.resolve(false),
|
|
90
|
-
denyInboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
91
|
-
denyOutboundEncryptedConnection: async () => await Promise.resolve(false),
|
|
92
|
-
denyInboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
93
|
-
denyOutboundUpgradedConnection: async () => await Promise.resolve(false),
|
|
94
|
-
filterMultiaddrForPeer: async () => await Promise.resolve(true),
|
|
95
|
-
...init.connectionGater
|
|
96
|
-
}
|
|
86
|
+
connectionGater: connectionGater(init.connectionGater)
|
|
97
87
|
})
|
|
98
88
|
|
|
99
89
|
this.peerStore = this.configureComponent('peerStore', new PersistentPeerStore(components, {
|
|
@@ -147,7 +137,6 @@ export class Libp2pNode<T extends ServiceMap = {}> extends EventEmitter<Libp2pEv
|
|
|
147
137
|
// Peer routers
|
|
148
138
|
const peerRouters: PeerRouting[] = (init.peerRouters ?? []).map((fn, index) => this.configureComponent(`peer-router-${index}`, fn(this.components)))
|
|
149
139
|
this.peerRouting = this.components.peerRouting = this.configureComponent('peerRouting', new DefaultPeerRouting(this.components, {
|
|
150
|
-
...init.peerRouting,
|
|
151
140
|
routers: peerRouters
|
|
152
141
|
}))
|
|
153
142
|
|
package/src/peer-routing.ts
CHANGED
|
@@ -6,52 +6,20 @@ import {
|
|
|
6
6
|
uniquePeers,
|
|
7
7
|
requirePeers
|
|
8
8
|
} from './content-routing/utils.js'
|
|
9
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
10
9
|
import merge from 'it-merge'
|
|
11
10
|
import { pipe } from 'it-pipe'
|
|
12
11
|
import first from 'it-first'
|
|
13
|
-
import drain from 'it-drain'
|
|
14
12
|
import filter from 'it-filter'
|
|
15
|
-
import {
|
|
16
|
-
setDelayedInterval,
|
|
17
|
-
clearDelayedInterval
|
|
18
|
-
// @ts-expect-error module with no types
|
|
19
|
-
} from 'set-delayed-interval'
|
|
20
|
-
import { setMaxListeners } from 'events'
|
|
21
13
|
import type { PeerId } from '@libp2p/interface-peer-id'
|
|
22
14
|
import type { PeerRouting } from '@libp2p/interface-peer-routing'
|
|
23
15
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
24
|
-
import type { Startable } from '@libp2p/interfaces/startable'
|
|
25
16
|
import type { PeerInfo } from '@libp2p/interface-peer-info'
|
|
26
17
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
27
18
|
|
|
28
19
|
const log = logger('libp2p:peer-routing')
|
|
29
20
|
|
|
30
|
-
export interface RefreshManagerInit {
|
|
31
|
-
/**
|
|
32
|
-
* Whether to enable the Refresh manager
|
|
33
|
-
*/
|
|
34
|
-
enabled?: boolean
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Boot delay to start the Refresh Manager (in ms)
|
|
38
|
-
*/
|
|
39
|
-
bootDelay?: number
|
|
40
|
-
|
|
41
|
-
/**
|
|
42
|
-
* Interval between each Refresh Manager run (in ms)
|
|
43
|
-
*/
|
|
44
|
-
interval?: number
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* How long to let each refresh run (in ms)
|
|
48
|
-
*/
|
|
49
|
-
timeout?: number
|
|
50
|
-
}
|
|
51
|
-
|
|
52
21
|
export interface PeerRoutingInit {
|
|
53
22
|
routers?: PeerRouting[]
|
|
54
|
-
refreshManager?: RefreshManagerInit
|
|
55
23
|
}
|
|
56
24
|
|
|
57
25
|
export interface DefaultPeerRoutingComponents {
|
|
@@ -59,81 +27,13 @@ export interface DefaultPeerRoutingComponents {
|
|
|
59
27
|
peerStore: PeerStore
|
|
60
28
|
}
|
|
61
29
|
|
|
62
|
-
export class DefaultPeerRouting implements PeerRouting
|
|
30
|
+
export class DefaultPeerRouting implements PeerRouting {
|
|
63
31
|
private readonly components: DefaultPeerRoutingComponents
|
|
64
32
|
private readonly routers: PeerRouting[]
|
|
65
|
-
private readonly refreshManagerInit: RefreshManagerInit
|
|
66
|
-
private timeoutId?: ReturnType<typeof setTimeout>
|
|
67
|
-
private started: boolean
|
|
68
|
-
private abortController?: TimeoutController
|
|
69
33
|
|
|
70
34
|
constructor (components: DefaultPeerRoutingComponents, init: PeerRoutingInit) {
|
|
71
35
|
this.components = components
|
|
72
36
|
this.routers = init.routers ?? []
|
|
73
|
-
this.refreshManagerInit = init.refreshManager ?? {}
|
|
74
|
-
this.started = false
|
|
75
|
-
|
|
76
|
-
this._findClosestPeersTask = this._findClosestPeersTask.bind(this)
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
isStarted (): boolean {
|
|
80
|
-
return this.started
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
/**
|
|
84
|
-
* Start peer routing service.
|
|
85
|
-
*/
|
|
86
|
-
async start (): Promise<void> {
|
|
87
|
-
if (this.started || this.routers.length === 0 || this.timeoutId != null || this.refreshManagerInit.enabled === false) {
|
|
88
|
-
return
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
this.timeoutId = setDelayedInterval(
|
|
92
|
-
this._findClosestPeersTask, this.refreshManagerInit.interval, this.refreshManagerInit.bootDelay
|
|
93
|
-
)
|
|
94
|
-
|
|
95
|
-
this.started = true
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* Recurrent task to find closest peers and add their addresses to the Address Book.
|
|
100
|
-
*/
|
|
101
|
-
async _findClosestPeersTask (): Promise<void> {
|
|
102
|
-
if (this.abortController != null) {
|
|
103
|
-
// we are already running the query
|
|
104
|
-
return
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
try {
|
|
108
|
-
this.abortController = new TimeoutController(this.refreshManagerInit.timeout ?? 10e3)
|
|
109
|
-
|
|
110
|
-
// this controller may be used while dialing lots of peers so prevent MaxListenersExceededWarning
|
|
111
|
-
// appearing in the console
|
|
112
|
-
try {
|
|
113
|
-
// fails on node < 15.4
|
|
114
|
-
setMaxListeners?.(Infinity, this.abortController.signal)
|
|
115
|
-
} catch {}
|
|
116
|
-
|
|
117
|
-
// nb getClosestPeers adds the addresses to the address book
|
|
118
|
-
await drain(this.getClosestPeers(this.components.peerId.toBytes(), { signal: this.abortController.signal }))
|
|
119
|
-
} catch (err: any) {
|
|
120
|
-
log.error(err)
|
|
121
|
-
} finally {
|
|
122
|
-
this.abortController?.clear()
|
|
123
|
-
this.abortController = undefined
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Stop peer routing service.
|
|
129
|
-
*/
|
|
130
|
-
async stop (): Promise<void> {
|
|
131
|
-
clearDelayedInterval(this.timeoutId)
|
|
132
|
-
|
|
133
|
-
// abort query if it is in-flight
|
|
134
|
-
this.abortController?.abort()
|
|
135
|
-
|
|
136
|
-
this.started = false
|
|
137
37
|
}
|
|
138
38
|
|
|
139
39
|
/**
|
package/src/ping/index.ts
CHANGED
|
@@ -14,6 +14,7 @@ import { abortableDuplex } from 'abortable-iterator'
|
|
|
14
14
|
import type { Stream } from '@libp2p/interface-connection'
|
|
15
15
|
import { setMaxListeners } from 'events'
|
|
16
16
|
import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
17
|
+
import { anySignal } from 'any-signal'
|
|
17
18
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
18
19
|
|
|
19
20
|
const log = logger('libp2p:ping')
|
|
@@ -96,18 +97,14 @@ class DefaultPingService implements Startable, PingService {
|
|
|
96
97
|
const start = Date.now()
|
|
97
98
|
const data = randomBytes(PING_LENGTH)
|
|
98
99
|
const connection = await this.components.connectionManager.openConnection(peer, options)
|
|
99
|
-
let signal = options.signal
|
|
100
100
|
let stream: Stream | undefined
|
|
101
101
|
|
|
102
|
-
|
|
103
|
-
if (signal == null) {
|
|
104
|
-
signal = AbortSignal.timeout(this.timeout)
|
|
102
|
+
const signal = anySignal([AbortSignal.timeout(this.timeout), options?.signal])
|
|
105
103
|
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
}
|
|
104
|
+
try {
|
|
105
|
+
// fails on node < 15.4
|
|
106
|
+
setMaxListeners?.(Infinity, signal)
|
|
107
|
+
} catch {}
|
|
111
108
|
|
|
112
109
|
try {
|
|
113
110
|
stream = await connection.newStream([this.protocol], {
|
|
@@ -133,6 +130,7 @@ class DefaultPingService implements Startable, PingService {
|
|
|
133
130
|
if (stream != null) {
|
|
134
131
|
stream.close()
|
|
135
132
|
}
|
|
133
|
+
signal.clear()
|
|
136
134
|
}
|
|
137
135
|
}
|
|
138
136
|
}
|
package/src/transport-manager.ts
CHANGED
|
@@ -157,6 +157,13 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
157
157
|
return Array.of(...this.transports.values())
|
|
158
158
|
}
|
|
159
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Returns all the listener instances
|
|
162
|
+
*/
|
|
163
|
+
getListeners (): Listener[] {
|
|
164
|
+
return Array.of(...this.listeners.values()).flat()
|
|
165
|
+
}
|
|
166
|
+
|
|
160
167
|
/**
|
|
161
168
|
* Finds a transport that matches the given Multiaddr
|
|
162
169
|
*/
|
|
@@ -192,7 +199,7 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
192
199
|
upgrader: this.components.upgrader
|
|
193
200
|
})
|
|
194
201
|
|
|
195
|
-
let listeners = this.listeners.get(key)
|
|
202
|
+
let listeners: Listener[] = this.listeners.get(key) ?? []
|
|
196
203
|
|
|
197
204
|
if (listeners == null) {
|
|
198
205
|
listeners = []
|
|
@@ -216,6 +223,11 @@ export class DefaultTransportManager implements TransportManager, Startable {
|
|
|
216
223
|
})
|
|
217
224
|
})
|
|
218
225
|
listener.addEventListener('close', () => {
|
|
226
|
+
const index = listeners.findIndex(l => l === listener)
|
|
227
|
+
|
|
228
|
+
// remove the listener
|
|
229
|
+
listeners.splice(index, 1)
|
|
230
|
+
|
|
219
231
|
this.components.peerStore.patch(this.components.peerId, {
|
|
220
232
|
multiaddrs: this.getAddrs()
|
|
221
233
|
})
|
package/src/upgrader.ts
CHANGED
|
@@ -14,7 +14,6 @@ import type { Duplex, Source } from 'it-stream-types'
|
|
|
14
14
|
import type { AbortOptions } from '@libp2p/interfaces'
|
|
15
15
|
import type { Registrar } from '@libp2p/interface-registrar'
|
|
16
16
|
import { DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_OUTBOUND_STREAMS } from './registrar.js'
|
|
17
|
-
import { TimeoutController } from 'timeout-abort-controller'
|
|
18
17
|
import { abortableDuplex } from 'abortable-iterator'
|
|
19
18
|
import { setMaxListeners } from 'events'
|
|
20
19
|
import type { Metrics } from '@libp2p/interface-metrics'
|
|
@@ -22,6 +21,7 @@ import type { ConnectionManager } from '@libp2p/interface-connection-manager'
|
|
|
22
21
|
import type { PeerStore } from '@libp2p/interface-peer-store'
|
|
23
22
|
import type { ConnectionGater } from '@libp2p/interface-connection-gater'
|
|
24
23
|
import { INBOUND_UPGRADE_TIMEOUT } from './connection-manager/constants.js'
|
|
24
|
+
import { anySignal } from 'any-signal'
|
|
25
25
|
import type { Libp2pEvents } from '@libp2p/interface-libp2p'
|
|
26
26
|
|
|
27
27
|
const log = logger('libp2p:upgrader')
|
|
@@ -73,7 +73,6 @@ function findIncomingStreamLimit (protocol: string, registrar: Registrar): numbe
|
|
|
73
73
|
function findOutgoingStreamLimit (protocol: string, registrar: Registrar): number | undefined {
|
|
74
74
|
try {
|
|
75
75
|
const { options } = registrar.getHandler(protocol)
|
|
76
|
-
|
|
77
76
|
return options.maxOutboundStreams
|
|
78
77
|
} catch (err: any) {
|
|
79
78
|
if (err.code !== codes.ERR_NO_HANDLER_FOR_PROTOCOL) {
|
|
@@ -162,15 +161,15 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
162
161
|
let muxerFactory: StreamMuxerFactory | undefined
|
|
163
162
|
let cryptoProtocol
|
|
164
163
|
|
|
165
|
-
const
|
|
164
|
+
const signal = anySignal([AbortSignal.timeout(this.inboundUpgradeTimeout)])
|
|
166
165
|
|
|
167
166
|
try {
|
|
168
167
|
// fails on node < 15.4
|
|
169
|
-
setMaxListeners?.(Infinity,
|
|
168
|
+
setMaxListeners?.(Infinity, signal)
|
|
170
169
|
} catch { }
|
|
171
170
|
|
|
172
171
|
try {
|
|
173
|
-
const abortableStream = abortableDuplex(maConn,
|
|
172
|
+
const abortableStream = abortableDuplex(maConn, signal)
|
|
174
173
|
maConn.source = abortableStream.source
|
|
175
174
|
maConn.sink = abortableStream.sink
|
|
176
175
|
|
|
@@ -254,7 +253,7 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
254
253
|
})
|
|
255
254
|
} finally {
|
|
256
255
|
this.components.connectionManager.afterUpgradeInbound()
|
|
257
|
-
|
|
256
|
+
signal.clear()
|
|
258
257
|
}
|
|
259
258
|
}
|
|
260
259
|
|
|
@@ -392,9 +391,10 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
392
391
|
const streamCount = countStreams(protocol, 'inbound', connection)
|
|
393
392
|
|
|
394
393
|
if (streamCount === incomingLimit) {
|
|
395
|
-
|
|
394
|
+
const err = new CodeError(`Too many inbound protocol streams for protocol "${protocol}" - limit ${incomingLimit}`, codes.ERR_TOO_MANY_INBOUND_PROTOCOL_STREAMS)
|
|
395
|
+
muxedStream.abort(err)
|
|
396
396
|
|
|
397
|
-
|
|
397
|
+
throw err
|
|
398
398
|
}
|
|
399
399
|
|
|
400
400
|
// after the handshake the returned stream can have early data so override
|
|
@@ -435,18 +435,16 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
435
435
|
|
|
436
436
|
log('%s: starting new stream on %s', direction, protocols)
|
|
437
437
|
const muxedStream = await muxer.newStream()
|
|
438
|
-
let controller: TimeoutController | undefined
|
|
439
438
|
|
|
440
439
|
try {
|
|
441
440
|
if (options.signal == null) {
|
|
442
441
|
log('No abort signal was passed while trying to negotiate protocols %s falling back to default timeout', protocols)
|
|
443
442
|
|
|
444
|
-
|
|
445
|
-
options.signal = controller.signal
|
|
443
|
+
options.signal = AbortSignal.timeout(30000)
|
|
446
444
|
|
|
447
445
|
try {
|
|
448
446
|
// fails on node < 15.4
|
|
449
|
-
setMaxListeners?.(Infinity,
|
|
447
|
+
setMaxListeners?.(Infinity, options.signal)
|
|
450
448
|
} catch { }
|
|
451
449
|
}
|
|
452
450
|
|
|
@@ -489,10 +487,6 @@ export class DefaultUpgrader implements Upgrader {
|
|
|
489
487
|
}
|
|
490
488
|
|
|
491
489
|
throw new CodeError(String(err), codes.ERR_UNSUPPORTED_PROTOCOL)
|
|
492
|
-
} finally {
|
|
493
|
-
if (controller != null) {
|
|
494
|
-
controller.clear()
|
|
495
|
-
}
|
|
496
490
|
}
|
|
497
491
|
}
|
|
498
492
|
|
package/src/upnp-nat/index.ts
CHANGED
|
@@ -50,7 +50,7 @@ export interface UPnPNATInit {
|
|
|
50
50
|
/**
|
|
51
51
|
* Whether to automatically refresh UPnP port mappings when their TTL is reached
|
|
52
52
|
*/
|
|
53
|
-
keepAlive
|
|
53
|
+
keepAlive?: boolean
|
|
54
54
|
|
|
55
55
|
/**
|
|
56
56
|
* Pass a value to use instead of auto-detection
|
|
@@ -205,7 +205,7 @@ class UPnPNAT implements Startable {
|
|
|
205
205
|
}
|
|
206
206
|
}
|
|
207
207
|
|
|
208
|
-
export function
|
|
208
|
+
export function uPnPNATService (init: UPnPNATInit = {}): (components: UPnPNATComponents) => {} {
|
|
209
209
|
return (components: UPnPNATComponents) => {
|
|
210
210
|
return new UPnPNAT(components, init)
|
|
211
211
|
}
|
package/src/version.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export const version = '0.44.0-
|
|
1
|
+
export const version = '0.44.0-59f34a21'
|
|
2
2
|
export const name = 'libp2p'
|