libp2p 0.46.16-d5ef1c91 → 0.46.17

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.
Files changed (75) hide show
  1. package/README.md +18 -61
  2. package/dist/index.min.js +29 -29
  3. package/dist/src/autonat/index.d.ts.map +1 -1
  4. package/dist/src/autonat/index.js +3 -11
  5. package/dist/src/autonat/index.js.map +1 -1
  6. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  7. package/dist/src/circuit-relay/server/index.js +2 -7
  8. package/dist/src/circuit-relay/server/index.js.map +1 -1
  9. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  10. package/dist/src/circuit-relay/transport/discovery.js +1 -0
  11. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  12. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  13. package/dist/src/circuit-relay/transport/listener.js +3 -17
  14. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  15. package/dist/src/connection/index.d.ts.map +1 -1
  16. package/dist/src/connection/index.js +5 -6
  17. package/dist/src/connection/index.js.map +1 -1
  18. package/dist/src/connection-manager/dial-queue.d.ts +4 -0
  19. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  20. package/dist/src/connection-manager/dial-queue.js +37 -7
  21. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  22. package/dist/src/connection-manager/index.d.ts.map +1 -1
  23. package/dist/src/connection-manager/index.js +7 -5
  24. package/dist/src/connection-manager/index.js.map +1 -1
  25. package/dist/src/connection-manager/utils.d.ts.map +1 -1
  26. package/dist/src/connection-manager/utils.js +3 -11
  27. package/dist/src/connection-manager/utils.js.map +1 -1
  28. package/dist/src/dcutr/dcutr.d.ts +0 -5
  29. package/dist/src/dcutr/dcutr.d.ts.map +1 -1
  30. package/dist/src/dcutr/dcutr.js +4 -28
  31. package/dist/src/dcutr/dcutr.js.map +1 -1
  32. package/dist/src/dcutr/utils.d.ts +8 -0
  33. package/dist/src/dcutr/utils.d.ts.map +1 -0
  34. package/dist/src/dcutr/utils.js +27 -0
  35. package/dist/src/dcutr/utils.js.map +1 -0
  36. package/dist/src/fetch/index.d.ts.map +1 -1
  37. package/dist/src/fetch/index.js +2 -6
  38. package/dist/src/fetch/index.js.map +1 -1
  39. package/dist/src/identify/consts.d.ts +1 -1
  40. package/dist/src/identify/consts.d.ts.map +1 -1
  41. package/dist/src/identify/identify.d.ts.map +1 -1
  42. package/dist/src/identify/identify.js +55 -47
  43. package/dist/src/identify/identify.js.map +1 -1
  44. package/dist/src/libp2p.d.ts.map +1 -1
  45. package/dist/src/libp2p.js +3 -7
  46. package/dist/src/libp2p.js.map +1 -1
  47. package/dist/src/registrar.d.ts +7 -6
  48. package/dist/src/registrar.d.ts.map +1 -1
  49. package/dist/src/registrar.js +16 -41
  50. package/dist/src/registrar.js.map +1 -1
  51. package/dist/src/upgrader.d.ts.map +1 -1
  52. package/dist/src/upgrader.js +3 -11
  53. package/dist/src/upgrader.js.map +1 -1
  54. package/dist/src/version.d.ts +1 -1
  55. package/dist/src/version.d.ts.map +1 -1
  56. package/dist/src/version.js +1 -1
  57. package/dist/src/version.js.map +1 -1
  58. package/dist/typedoc-urls.json +71 -0
  59. package/package.json +23 -24
  60. package/src/autonat/index.ts +3 -9
  61. package/src/circuit-relay/server/index.ts +2 -6
  62. package/src/circuit-relay/transport/discovery.ts +1 -0
  63. package/src/circuit-relay/transport/listener.ts +3 -21
  64. package/src/connection/index.ts +6 -5
  65. package/src/connection-manager/dial-queue.ts +46 -6
  66. package/src/connection-manager/index.ts +7 -5
  67. package/src/connection-manager/utils.ts +3 -9
  68. package/src/dcutr/dcutr.ts +4 -35
  69. package/src/dcutr/utils.ts +33 -0
  70. package/src/fetch/index.ts +2 -5
  71. package/src/identify/identify.ts +63 -47
  72. package/src/libp2p.ts +3 -6
  73. package/src/registrar.ts +19 -50
  74. package/src/upgrader.ts +3 -9
  75. package/src/version.ts +1 -1
@@ -1,5 +1,5 @@
1
- import { setMaxListeners } from 'events'
2
1
  import { CodeError } from '@libp2p/interface/errors'
2
+ import { setMaxListeners } from '@libp2p/interface/events'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import { peerIdFromKeys } from '@libp2p/peer-id'
5
5
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
@@ -23,7 +23,7 @@ import type { Libp2pEvents, IdentifyResult, SignedPeerRecord, AbortOptions } fro
23
23
  import type { Connection, Stream } from '@libp2p/interface/connection'
24
24
  import type { TypedEventTarget } from '@libp2p/interface/events'
25
25
  import type { PeerId } from '@libp2p/interface/peer-id'
26
- import type { Peer, PeerStore } from '@libp2p/interface/peer-store'
26
+ import type { Peer, PeerData, PeerStore } from '@libp2p/interface/peer-store'
27
27
  import type { Startable } from '@libp2p/interface/startable'
28
28
  import type { AddressManager } from '@libp2p/interface-internal/address-manager'
29
29
  import type { ConnectionManager } from '@libp2p/interface-internal/connection-manager'
@@ -187,10 +187,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
187
187
 
188
188
  const signal = AbortSignal.timeout(this.timeout)
189
189
 
190
- try {
191
- // fails on node < 15.4
192
- setMaxListeners?.(Infinity, signal)
193
- } catch {}
190
+ setMaxListeners(Infinity, signal)
194
191
 
195
192
  try {
196
193
  stream = await connection.newStream([this.identifyPushProtocolStr], {
@@ -318,22 +315,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
318
315
  this.addressManager.addObservedAddr(cleanObservedAddr)
319
316
  }
320
317
 
321
- const signedPeerRecord = await this.#consumeIdentifyMessage(connection.remotePeer, message)
322
-
323
- const result: IdentifyResult = {
324
- peerId: id,
325
- protocolVersion: message.protocolVersion,
326
- agentVersion: message.agentVersion,
327
- publicKey: message.publicKey,
328
- listenAddrs: message.listenAddrs.map(buf => multiaddr(buf)),
329
- observedAddr: message.observedAddr == null ? undefined : multiaddr(message.observedAddr),
330
- protocols: message.protocols,
331
- signedPeerRecord
332
- }
333
-
334
- this.events.safeDispatchEvent('peer:identify', { detail: result })
335
-
336
- return result
318
+ return this.#consumeIdentifyMessage(connection, message)
337
319
  }
338
320
 
339
321
  /**
@@ -345,10 +327,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
345
327
 
346
328
  const signal = AbortSignal.timeout(this.timeout)
347
329
 
348
- try {
349
- // fails on node < 15.4
350
- setMaxListeners?.(Infinity, signal)
351
- } catch {}
330
+ setMaxListeners(Infinity, signal)
352
331
 
353
332
  try {
354
333
  const publicKey = this.peerId.publicKey ?? new Uint8Array(0)
@@ -411,7 +390,7 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
411
390
  const message = await pb.read(options)
412
391
  await stream.close(options)
413
392
 
414
- await this.#consumeIdentifyMessage(connection.remotePeer, message)
393
+ await this.#consumeIdentifyMessage(connection, message)
415
394
  } catch (err: any) {
416
395
  log.error('received invalid message', err)
417
396
  stream.abort(err)
@@ -421,28 +400,41 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
421
400
  log('handled push from %p', connection.remotePeer)
422
401
  }
423
402
 
424
- async #consumeIdentifyMessage (remotePeer: PeerId, message: Identify): Promise<SignedPeerRecord | undefined> {
425
- log('received identify from %p', remotePeer)
403
+ async #consumeIdentifyMessage (connection: Connection, message: Identify): Promise<IdentifyResult> {
404
+ log('received identify from %p', connection.remotePeer)
426
405
 
427
406
  if (message == null) {
428
- throw new Error('Message was null or undefined')
407
+ throw new CodeError('message was null or undefined', 'ERR_INVALID_MESSAGE')
429
408
  }
430
409
 
431
- const peer = {
432
- addresses: message.listenAddrs.map(buf => ({
410
+ const peer: PeerData = {}
411
+
412
+ if (message.listenAddrs.length > 0) {
413
+ peer.addresses = message.listenAddrs.map(buf => ({
433
414
  isCertified: false,
434
415
  multiaddr: multiaddr(buf)
435
- })),
436
- protocols: message.protocols,
437
- metadata: new Map(),
438
- peerRecordEnvelope: message.signedPeerRecord
416
+ }))
417
+ }
418
+
419
+ if (message.protocols.length > 0) {
420
+ peer.protocols = message.protocols
421
+ }
422
+
423
+ if (message.publicKey != null) {
424
+ peer.publicKey = message.publicKey
425
+
426
+ const peerId = await peerIdFromKeys(message.publicKey)
427
+
428
+ if (!peerId.equals(connection.remotePeer)) {
429
+ throw new CodeError('public key did not match remote PeerId', 'ERR_INVALID_PUBLIC_KEY')
430
+ }
439
431
  }
440
432
 
441
433
  let output: SignedPeerRecord | undefined
442
434
 
443
435
  // if the peer record has been sent, prefer the addresses in the record as they are signed by the remote peer
444
436
  if (message.signedPeerRecord != null) {
445
- log('received signedPeerRecord in push from %p', remotePeer)
437
+ log('received signedPeerRecord in push from %p', connection.remotePeer)
446
438
 
447
439
  let peerRecordEnvelope = message.signedPeerRecord
448
440
  const envelope = await RecordEnvelope.openAndCertify(peerRecordEnvelope, PeerRecord.DOMAIN)
@@ -450,12 +442,12 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
450
442
 
451
443
  // Verify peerId
452
444
  if (!peerRecord.peerId.equals(envelope.peerId)) {
453
- throw new Error('signing key does not match PeerId in the PeerRecord')
445
+ throw new CodeError('signing key does not match PeerId in the PeerRecord', 'ERR_INVALID_SIGNING_KEY')
454
446
  }
455
447
 
456
448
  // Make sure remote peer is the one sending the record
457
- if (!remotePeer.equals(peerRecord.peerId)) {
458
- throw new Error('signing key does not match remote PeerId')
449
+ if (!connection.remotePeer.equals(peerRecord.peerId)) {
450
+ throw new CodeError('signing key does not match remote PeerId', 'ERR_INVALID_PEER_RECORD_KEY')
459
451
  }
460
452
 
461
453
  let existingPeer: Peer | undefined
@@ -500,20 +492,44 @@ export class DefaultIdentifyService implements Startable, IdentifyService {
500
492
  addresses: peerRecord.multiaddrs
501
493
  }
502
494
  } else {
503
- log('%p did not send a signed peer record', remotePeer)
495
+ log('%p did not send a signed peer record', connection.remotePeer)
504
496
  }
505
497
 
506
- if (message.agentVersion != null) {
507
- peer.metadata.set('AgentVersion', uint8ArrayFromString(message.agentVersion))
498
+ log('patching %p with', peer)
499
+ await this.peerStore.patch(connection.remotePeer, peer)
500
+
501
+ if (message.agentVersion != null || message.protocolVersion != null) {
502
+ const metadata: Record<string, Uint8Array> = {}
503
+
504
+ if (message.agentVersion != null) {
505
+ metadata.AgentVersion = uint8ArrayFromString(message.agentVersion)
506
+ }
507
+
508
+ if (message.protocolVersion != null) {
509
+ metadata.ProtocolVersion = uint8ArrayFromString(message.protocolVersion)
510
+ }
511
+
512
+ log('updating %p metadata', peer)
513
+ await this.peerStore.merge(connection.remotePeer, {
514
+ metadata
515
+ })
508
516
  }
509
517
 
510
- if (message.protocolVersion != null) {
511
- peer.metadata.set('ProtocolVersion', uint8ArrayFromString(message.protocolVersion))
518
+ const result: IdentifyResult = {
519
+ peerId: connection.remotePeer,
520
+ protocolVersion: message.protocolVersion,
521
+ agentVersion: message.agentVersion,
522
+ publicKey: message.publicKey,
523
+ listenAddrs: message.listenAddrs.map(buf => multiaddr(buf)),
524
+ observedAddr: message.observedAddr == null ? undefined : multiaddr(message.observedAddr),
525
+ protocols: message.protocols,
526
+ signedPeerRecord: output,
527
+ connection
512
528
  }
513
529
 
514
- await this.peerStore.patch(remotePeer, peer)
530
+ this.events.safeDispatchEvent('peer:identify', { detail: result })
515
531
 
516
- return output
532
+ return result
517
533
  }
518
534
  }
519
535
 
package/src/libp2p.ts CHANGED
@@ -1,8 +1,7 @@
1
- import { setMaxListeners } from 'events'
2
1
  import { unmarshalPublicKey } from '@libp2p/crypto/keys'
3
2
  import { type ContentRouting, contentRouting } from '@libp2p/interface/content-routing'
4
3
  import { CodeError } from '@libp2p/interface/errors'
5
- import { TypedEventEmitter, CustomEvent } from '@libp2p/interface/events'
4
+ import { TypedEventEmitter, CustomEvent, setMaxListeners } from '@libp2p/interface/events'
6
5
  import { peerDiscovery } from '@libp2p/interface/peer-discovery'
7
6
  import { type PeerRouting, peerRouting } from '@libp2p/interface/peer-routing'
8
7
  import { DefaultKeyChain } from '@libp2p/keychain'
@@ -70,10 +69,8 @@ export class Libp2pNode<T extends ServiceMap = Record<string, unknown>> extends
70
69
  return internalResult || externalResult
71
70
  }
72
71
 
73
- try {
74
- // This emitter gets listened to a lot
75
- setMaxListeners?.(Infinity, events)
76
- } catch {}
72
+ // This emitter gets listened to a lot
73
+ setMaxListeners(Infinity, events)
77
74
 
78
75
  this.#started = false
79
76
  this.peerId = init.peerId
package/src/registrar.ts CHANGED
@@ -2,7 +2,7 @@ import { CodeError } from '@libp2p/interface/errors'
2
2
  import { logger } from '@libp2p/logger'
3
3
  import merge from 'merge-options'
4
4
  import { codes } from './errors.js'
5
- import type { Libp2pEvents, PeerUpdate } from '@libp2p/interface'
5
+ import type { IdentifyResult, Libp2pEvents, PeerUpdate } from '@libp2p/interface'
6
6
  import type { TypedEventTarget } from '@libp2p/interface/events'
7
7
  import type { PeerId } from '@libp2p/interface/peer-id'
8
8
  import type { PeerStore } from '@libp2p/interface/peer-store'
@@ -37,11 +37,11 @@ export class DefaultRegistrar implements Registrar {
37
37
 
38
38
  this._onDisconnect = this._onDisconnect.bind(this)
39
39
  this._onPeerUpdate = this._onPeerUpdate.bind(this)
40
- this._onConnect = this._onConnect.bind(this)
40
+ this._onPeerIdentify = this._onPeerIdentify.bind(this)
41
41
 
42
42
  this.components.events.addEventListener('peer:disconnect', this._onDisconnect)
43
- this.components.events.addEventListener('peer:connect', this._onConnect)
44
43
  this.components.events.addEventListener('peer:update', this._onPeerUpdate)
44
+ this.components.events.addEventListener('peer:identify', this._onPeerIdentify)
45
45
  }
46
46
 
47
47
  getProtocols (): string[] {
@@ -183,52 +183,12 @@ export class DefaultRegistrar implements Registrar {
183
183
  }
184
184
 
185
185
  /**
186
- * On peer connected if we already have their protocols. Usually used for reconnects
187
- * as change:protocols event won't be emitted due to identical protocols.
188
- */
189
- _onConnect (evt: CustomEvent<PeerId>): void {
190
- const remotePeer = evt.detail
191
-
192
- void this.components.peerStore.get(remotePeer)
193
- .then(peer => {
194
- const connection = this.components.connectionManager.getConnections(peer.id)[0]
195
-
196
- if (connection == null) {
197
- log('peer %p connected but the connection manager did not have a connection', peer)
198
- // peer disconnected while we were loading their details from the peer store
199
- return
200
- }
201
-
202
- for (const protocol of peer.protocols) {
203
- const topologies = this.topologies.get(protocol)
204
-
205
- if (topologies == null) {
206
- // no topologies are interested in this protocol
207
- continue
208
- }
209
-
210
- for (const topology of topologies.values()) {
211
- topology.onConnect?.(remotePeer, connection)
212
- }
213
- }
214
- })
215
- .catch(err => {
216
- if (err.code === codes.ERR_NOT_FOUND) {
217
- // peer has not completed identify so they are not in the peer store
218
- return
219
- }
220
-
221
- log.error('could not inform topologies of connecting peer %p', remotePeer, err)
222
- })
223
- }
224
-
225
- /**
226
- * Check if a new peer support the multicodecs for this topology
186
+ * When a peer is updated, if they have removed supported protocols notify any
187
+ * topologies interested in the removed protocols.
227
188
  */
228
189
  _onPeerUpdate (evt: CustomEvent<PeerUpdate>): void {
229
190
  const { peer, previous } = evt.detail
230
191
  const removed = (previous?.protocols ?? []).filter(protocol => !peer.protocols.includes(protocol))
231
- const added = peer.protocols.filter(protocol => !(previous?.protocols ?? []).includes(protocol))
232
192
 
233
193
  for (const protocol of removed) {
234
194
  const topologies = this.topologies.get(protocol)
@@ -242,8 +202,18 @@ export class DefaultRegistrar implements Registrar {
242
202
  topology.onDisconnect?.(peer.id)
243
203
  }
244
204
  }
205
+ }
245
206
 
246
- for (const protocol of added) {
207
+ /**
208
+ * After identify has completed and we have received the list of supported
209
+ * protocols, notify any topologies interested in those protocols.
210
+ */
211
+ _onPeerIdentify (evt: CustomEvent<IdentifyResult>): void {
212
+ const protocols = evt.detail.protocols
213
+ const connection = evt.detail.connection
214
+ const peerId = evt.detail.peerId
215
+
216
+ for (const protocol of protocols) {
247
217
  const topologies = this.topologies.get(protocol)
248
218
 
249
219
  if (topologies == null) {
@@ -252,12 +222,11 @@ export class DefaultRegistrar implements Registrar {
252
222
  }
253
223
 
254
224
  for (const topology of topologies.values()) {
255
- const connection = this.components.connectionManager.getConnections(peer.id)[0]
256
-
257
- if (connection == null) {
225
+ if (connection.transient && topology.notifyOnTransient !== true) {
258
226
  continue
259
227
  }
260
- topology.onConnect?.(peer.id, connection)
228
+
229
+ topology.onConnect?.(peerId, connection)
261
230
  }
262
231
  }
263
232
  }
package/src/upgrader.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { setMaxListeners } from 'events'
2
1
  import { CodeError } from '@libp2p/interface/errors'
2
+ import { setMaxListeners } from '@libp2p/interface/events'
3
3
  import { logger } from '@libp2p/logger'
4
4
  import * as mss from '@libp2p/multistream-select'
5
5
  import { peerIdFromString } from '@libp2p/peer-id'
@@ -170,10 +170,7 @@ export class DefaultUpgrader implements Upgrader {
170
170
 
171
171
  signal.addEventListener('abort', onAbort, { once: true })
172
172
 
173
- try {
174
- // fails on node < 15.4
175
- setMaxListeners?.(Infinity, signal)
176
- } catch { }
173
+ setMaxListeners(Infinity, signal)
177
174
 
178
175
  try {
179
176
  if ((await this.components.connectionGater.denyInboundConnection?.(maConn)) === true) {
@@ -444,10 +441,7 @@ export class DefaultUpgrader implements Upgrader {
444
441
 
445
442
  options.signal = AbortSignal.timeout(30000)
446
443
 
447
- try {
448
- // fails on node < 15.4
449
- setMaxListeners?.(Infinity, options.signal)
450
- } catch { }
444
+ setMaxListeners(Infinity, options.signal)
451
445
  }
452
446
 
453
447
  const { stream, protocol } = await mss.select(muxedStream, protocols, options)
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.46.16-d5ef1c91'
1
+ export const version = '0.46.17'
2
2
  export const name = 'libp2p'