libp2p 0.42.2-45fc415b → 0.42.2-5e63ee2d

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 (251) hide show
  1. package/dist/index.min.js +28 -28
  2. package/dist/src/address-manager/index.d.ts +1 -10
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +46 -29
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/autonat/constants.d.ts +4 -0
  7. package/dist/src/autonat/constants.d.ts.map +1 -0
  8. package/dist/src/autonat/constants.js +4 -0
  9. package/dist/src/autonat/constants.js.map +1 -0
  10. package/dist/src/autonat/index.d.ts +64 -0
  11. package/dist/src/autonat/index.d.ts.map +1 -0
  12. package/dist/src/autonat/index.js +417 -0
  13. package/dist/src/autonat/index.js.map +1 -0
  14. package/dist/src/autonat/pb/index.d.ts +57 -0
  15. package/dist/src/autonat/pb/index.d.ts.map +1 -0
  16. package/dist/src/autonat/pb/index.js +250 -0
  17. package/dist/src/autonat/pb/index.js.map +1 -0
  18. package/dist/src/circuit/constants.d.ts +31 -6
  19. package/dist/src/circuit/constants.d.ts.map +1 -1
  20. package/dist/src/circuit/constants.js +37 -7
  21. package/dist/src/circuit/constants.js.map +1 -1
  22. package/dist/src/circuit/index.d.ts +16 -61
  23. package/dist/src/circuit/index.d.ts.map +1 -1
  24. package/dist/src/circuit/index.js +2 -68
  25. package/dist/src/circuit/index.js.map +1 -1
  26. package/dist/src/circuit/pb/index.d.ts +83 -43
  27. package/dist/src/circuit/pb/index.d.ts.map +1 -1
  28. package/dist/src/circuit/pb/index.js +363 -127
  29. package/dist/src/circuit/pb/index.js.map +1 -1
  30. package/dist/src/circuit/server/advert-service.d.ts +43 -0
  31. package/dist/src/circuit/server/advert-service.d.ts.map +1 -0
  32. package/dist/src/circuit/server/advert-service.js +69 -0
  33. package/dist/src/circuit/server/advert-service.js.map +1 -0
  34. package/dist/src/circuit/server/index.d.ts +60 -0
  35. package/dist/src/circuit/server/index.d.ts.map +1 -0
  36. package/dist/src/circuit/server/index.js +287 -0
  37. package/dist/src/circuit/server/index.js.map +1 -0
  38. package/dist/src/circuit/server/reservation-store.d.ts +49 -0
  39. package/dist/src/circuit/server/reservation-store.d.ts.map +1 -0
  40. package/dist/src/circuit/server/reservation-store.js +57 -0
  41. package/dist/src/circuit/server/reservation-store.js.map +1 -0
  42. package/dist/src/circuit/server/reservation-voucher.d.ts +18 -0
  43. package/dist/src/circuit/server/reservation-voucher.d.ts.map +1 -0
  44. package/dist/src/circuit/server/reservation-voucher.js +33 -0
  45. package/dist/src/circuit/server/reservation-voucher.js.map +1 -0
  46. package/dist/src/circuit/transport/discovery.d.ts +45 -0
  47. package/dist/src/circuit/transport/discovery.d.ts.map +1 -0
  48. package/dist/src/circuit/transport/discovery.js +82 -0
  49. package/dist/src/circuit/transport/discovery.js.map +1 -0
  50. package/dist/src/circuit/transport/index.d.ts +33 -0
  51. package/dist/src/circuit/transport/index.d.ts.map +1 -0
  52. package/dist/src/circuit/transport/index.js +225 -0
  53. package/dist/src/circuit/transport/index.js.map +1 -0
  54. package/dist/src/circuit/transport/listener.d.ts +9 -0
  55. package/dist/src/circuit/transport/listener.d.ts.map +1 -0
  56. package/dist/src/circuit/transport/listener.js +76 -0
  57. package/dist/src/circuit/transport/listener.js.map +1 -0
  58. package/dist/src/circuit/transport/reservation-store.d.ts +60 -0
  59. package/dist/src/circuit/transport/reservation-store.d.ts.map +1 -0
  60. package/dist/src/circuit/transport/reservation-store.js +165 -0
  61. package/dist/src/circuit/transport/reservation-store.js.map +1 -0
  62. package/dist/src/circuit/utils.d.ts +7 -0
  63. package/dist/src/circuit/utils.d.ts.map +1 -1
  64. package/dist/src/circuit/utils.js +87 -0
  65. package/dist/src/circuit/utils.js.map +1 -1
  66. package/dist/src/components.d.ts +2 -1
  67. package/dist/src/components.d.ts.map +1 -1
  68. package/dist/src/components.js.map +1 -1
  69. package/dist/src/config.d.ts.map +1 -1
  70. package/dist/src/config.js +17 -21
  71. package/dist/src/config.js.map +1 -1
  72. package/dist/src/connection/index.d.ts +2 -2
  73. package/dist/src/connection/index.d.ts.map +1 -1
  74. package/dist/src/connection/index.js +1 -1
  75. package/dist/src/connection/index.js.map +1 -1
  76. package/dist/src/connection-manager/auto-dialler.d.ts +0 -4
  77. package/dist/src/connection-manager/auto-dialler.d.ts.map +1 -1
  78. package/dist/src/connection-manager/auto-dialler.js +0 -9
  79. package/dist/src/connection-manager/auto-dialler.js.map +1 -1
  80. package/dist/src/connection-manager/dialer/dial-request.d.ts.map +1 -1
  81. package/dist/src/connection-manager/dialer/dial-request.js +8 -1
  82. package/dist/src/connection-manager/dialer/dial-request.js.map +1 -1
  83. package/dist/src/connection-manager/dialer/index.d.ts +2 -1
  84. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  85. package/dist/src/connection-manager/dialer/index.js +3 -3
  86. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  87. package/dist/src/connection-manager/index.d.ts +0 -4
  88. package/dist/src/connection-manager/index.d.ts.map +1 -1
  89. package/dist/src/connection-manager/index.js +1 -1
  90. package/dist/src/connection-manager/index.js.map +1 -1
  91. package/dist/src/connection-manager/latency-monitor.d.ts.map +1 -1
  92. package/dist/src/connection-manager/latency-monitor.js +1 -1
  93. package/dist/src/connection-manager/latency-monitor.js.map +1 -1
  94. package/dist/src/connection-manager/visibility-change-emitter.d.ts.map +1 -1
  95. package/dist/src/content-routing/index.d.ts +6 -4
  96. package/dist/src/content-routing/index.d.ts.map +1 -1
  97. package/dist/src/content-routing/index.js +1 -1
  98. package/dist/src/content-routing/index.js.map +1 -1
  99. package/dist/src/content-routing/utils.d.ts +3 -3
  100. package/dist/src/content-routing/utils.d.ts.map +1 -1
  101. package/dist/src/dht/dht-content-routing.d.ts +2 -1
  102. package/dist/src/dht/dht-content-routing.d.ts.map +1 -1
  103. package/dist/src/dht/dht-content-routing.js.map +1 -1
  104. package/dist/src/dht/dht-peer-routing.d.ts +3 -2
  105. package/dist/src/dht/dht-peer-routing.d.ts.map +1 -1
  106. package/dist/src/dht/dht-peer-routing.js.map +1 -1
  107. package/dist/src/dht/dummy-dht.d.ts +1 -1
  108. package/dist/src/dht/dummy-dht.d.ts.map +1 -1
  109. package/dist/src/errors.d.ts +2 -1
  110. package/dist/src/errors.d.ts.map +1 -1
  111. package/dist/src/errors.js +1 -0
  112. package/dist/src/errors.js.map +1 -1
  113. package/dist/src/fetch/index.d.ts.map +1 -1
  114. package/dist/src/fetch/pb/proto.d.ts +3 -3
  115. package/dist/src/fetch/pb/proto.d.ts.map +1 -1
  116. package/dist/src/fetch/pb/proto.js +4 -3
  117. package/dist/src/fetch/pb/proto.js.map +1 -1
  118. package/dist/src/identify/index.d.ts +3 -1
  119. package/dist/src/identify/index.d.ts.map +1 -1
  120. package/dist/src/identify/index.js +12 -7
  121. package/dist/src/identify/index.js.map +1 -1
  122. package/dist/src/identify/pb/message.d.ts +2 -2
  123. package/dist/src/identify/pb/message.d.ts.map +1 -1
  124. package/dist/src/identify/pb/message.js +1 -0
  125. package/dist/src/identify/pb/message.js.map +1 -1
  126. package/dist/src/index.d.ts +13 -7
  127. package/dist/src/index.d.ts.map +1 -1
  128. package/dist/src/index.js.map +1 -1
  129. package/dist/src/insecure/index.d.ts.map +1 -1
  130. package/dist/src/insecure/index.js.map +1 -1
  131. package/dist/src/insecure/pb/proto.d.ts +3 -3
  132. package/dist/src/insecure/pb/proto.d.ts.map +1 -1
  133. package/dist/src/insecure/pb/proto.js +4 -5
  134. package/dist/src/insecure/pb/proto.js.map +1 -1
  135. package/dist/src/libp2p.d.ts +6 -2
  136. package/dist/src/libp2p.d.ts.map +1 -1
  137. package/dist/src/libp2p.js +19 -21
  138. package/dist/src/libp2p.js.map +1 -1
  139. package/dist/src/nat-manager.d.ts.map +1 -1
  140. package/dist/src/nat-manager.js +3 -1
  141. package/dist/src/nat-manager.js.map +1 -1
  142. package/dist/src/peer-record-updater.d.ts.map +1 -1
  143. package/dist/src/peer-routing.d.ts.map +1 -1
  144. package/dist/src/ping/index.d.ts.map +1 -1
  145. package/dist/src/pnet/crypto.d.ts +1 -1
  146. package/dist/src/pnet/crypto.d.ts.map +1 -1
  147. package/dist/src/pnet/crypto.js +1 -1
  148. package/dist/src/pnet/crypto.js.map +1 -1
  149. package/dist/src/pnet/key-generator.d.ts.map +1 -1
  150. package/dist/src/registrar.d.ts.map +1 -1
  151. package/dist/src/transport-manager.d.ts.map +1 -1
  152. package/dist/src/transport-manager.js +3 -4
  153. package/dist/src/transport-manager.js.map +1 -1
  154. package/dist/src/upgrader.d.ts +2 -1
  155. package/dist/src/upgrader.d.ts.map +1 -1
  156. package/dist/src/upgrader.js +19 -14
  157. package/dist/src/upgrader.js.map +1 -1
  158. package/package.json +32 -26
  159. package/src/address-manager/index.ts +62 -35
  160. package/src/autonat/constants.ts +4 -0
  161. package/src/autonat/index.ts +567 -0
  162. package/src/autonat/pb/index.proto +35 -0
  163. package/src/autonat/pb/index.ts +320 -0
  164. package/src/circuit/constants.ts +48 -7
  165. package/src/circuit/index.ts +17 -129
  166. package/src/circuit/pb/index.proto +57 -32
  167. package/src/circuit/pb/index.ts +448 -138
  168. package/src/circuit/server/advert-service.ts +108 -0
  169. package/src/circuit/server/index.ts +421 -0
  170. package/src/circuit/server/reservation-store.ts +111 -0
  171. package/src/circuit/server/reservation-voucher.ts +51 -0
  172. package/src/circuit/transport/discovery.ts +127 -0
  173. package/src/circuit/transport/index.ts +325 -0
  174. package/src/circuit/transport/listener.ts +99 -0
  175. package/src/circuit/transport/reservation-store.ts +249 -0
  176. package/src/circuit/utils.ts +108 -0
  177. package/src/components.ts +9 -8
  178. package/src/config.ts +17 -21
  179. package/src/connection/index.ts +7 -7
  180. package/src/connection-manager/auto-dialler.ts +4 -20
  181. package/src/connection-manager/dialer/dial-request.ts +8 -1
  182. package/src/connection-manager/dialer/index.ts +10 -9
  183. package/src/connection-manager/index.ts +14 -19
  184. package/src/connection-manager/latency-monitor.ts +9 -9
  185. package/src/connection-manager/visibility-change-emitter.ts +4 -4
  186. package/src/content-routing/index.ts +10 -8
  187. package/src/content-routing/utils.ts +3 -3
  188. package/src/dht/dht-content-routing.ts +3 -2
  189. package/src/dht/dht-peer-routing.ts +3 -2
  190. package/src/dht/dummy-dht.ts +1 -1
  191. package/src/errors.ts +2 -1
  192. package/src/fetch/index.ts +7 -7
  193. package/src/fetch/pb/proto.ts +8 -7
  194. package/src/identify/index.ts +22 -17
  195. package/src/identify/pb/message.ts +3 -2
  196. package/src/index.ts +14 -7
  197. package/src/insecure/index.ts +2 -1
  198. package/src/insecure/pb/proto.ts +8 -9
  199. package/src/libp2p.ts +33 -32
  200. package/src/nat-manager.ts +9 -7
  201. package/src/peer-record-updater.ts +4 -4
  202. package/src/peer-routing.ts +4 -4
  203. package/src/ping/index.ts +4 -4
  204. package/src/pnet/crypto.ts +3 -3
  205. package/src/pnet/key-generator.ts +1 -1
  206. package/src/registrar.ts +8 -8
  207. package/src/transport-manager.ts +12 -13
  208. package/src/upgrader.ts +24 -23
  209. package/src/version.ts +1 -1
  210. package/dist/src/circuit/auto-relay.d.ts +0 -46
  211. package/dist/src/circuit/auto-relay.d.ts.map +0 -1
  212. package/dist/src/circuit/auto-relay.js +0 -220
  213. package/dist/src/circuit/auto-relay.js.map +0 -1
  214. package/dist/src/circuit/circuit/hop.d.ts +0 -42
  215. package/dist/src/circuit/circuit/hop.d.ts.map +0 -1
  216. package/dist/src/circuit/circuit/hop.js +0 -142
  217. package/dist/src/circuit/circuit/hop.js.map +0 -1
  218. package/dist/src/circuit/circuit/stop.d.ts +0 -24
  219. package/dist/src/circuit/circuit/stop.d.ts.map +0 -1
  220. package/dist/src/circuit/circuit/stop.js +0 -51
  221. package/dist/src/circuit/circuit/stop.js.map +0 -1
  222. package/dist/src/circuit/circuit/stream-handler.d.ts +0 -40
  223. package/dist/src/circuit/circuit/stream-handler.d.ts.map +0 -1
  224. package/dist/src/circuit/circuit/stream-handler.js +0 -59
  225. package/dist/src/circuit/circuit/stream-handler.js.map +0 -1
  226. package/dist/src/circuit/circuit/utils.d.ts +0 -7
  227. package/dist/src/circuit/circuit/utils.d.ts.map +0 -1
  228. package/dist/src/circuit/circuit/utils.js +0 -43
  229. package/dist/src/circuit/circuit/utils.js.map +0 -1
  230. package/dist/src/circuit/listener.d.ts +0 -9
  231. package/dist/src/circuit/listener.d.ts.map +0 -1
  232. package/dist/src/circuit/listener.js +0 -62
  233. package/dist/src/circuit/listener.js.map +0 -1
  234. package/dist/src/circuit/multicodec.d.ts +0 -2
  235. package/dist/src/circuit/multicodec.d.ts.map +0 -1
  236. package/dist/src/circuit/multicodec.js +0 -2
  237. package/dist/src/circuit/multicodec.js.map +0 -1
  238. package/dist/src/circuit/transport.d.ts +0 -51
  239. package/dist/src/circuit/transport.d.ts.map +0 -1
  240. package/dist/src/circuit/transport.js +0 -223
  241. package/dist/src/circuit/transport.js.map +0 -1
  242. package/src/circuit/IMPLEMENTATION_NOTES.md +0 -128
  243. package/src/circuit/README.md +0 -111
  244. package/src/circuit/auto-relay.ts +0 -292
  245. package/src/circuit/circuit/hop.ts +0 -220
  246. package/src/circuit/circuit/stop.ts +0 -83
  247. package/src/circuit/circuit/stream-handler.ts +0 -87
  248. package/src/circuit/circuit/utils.ts +0 -44
  249. package/src/circuit/listener.ts +0 -83
  250. package/src/circuit/multicodec.ts +0 -2
  251. package/src/circuit/transport.ts +0 -278
@@ -4,7 +4,7 @@ import * as lp from 'it-length-prefixed'
4
4
  import { pipe } from 'it-pipe'
5
5
  import first from 'it-first'
6
6
  import { fromString as uint8ArrayFromString } from 'uint8arrays/from-string'
7
- import { multiaddr, protocols } from '@multiformats/multiaddr'
7
+ import { Multiaddr, multiaddr, protocols } from '@multiformats/multiaddr'
8
8
  import { Identify } from './pb/message.js'
9
9
  import { RecordEnvelope, PeerRecord } from '@libp2p/peer-record'
10
10
  import {
@@ -65,6 +65,7 @@ export interface IdentifyServiceInit {
65
65
 
66
66
  maxPushIncomingStreams: number
67
67
  maxPushOutgoingStreams: number
68
+ maxObservedAddresses?: number
68
69
  }
69
70
 
70
71
  export interface IdentifyServiceComponents {
@@ -112,7 +113,7 @@ export class IdentifyService implements Startable {
112
113
  const { peerId } = evt.detail
113
114
 
114
115
  if (this.components.peerId.equals(peerId)) {
115
- void this.pushToPeerStore().catch(err => log.error(err))
116
+ void this.pushToPeerStore().catch(err => { log.error(err) })
116
117
  }
117
118
  })
118
119
 
@@ -121,16 +122,16 @@ export class IdentifyService implements Startable {
121
122
  const { peerId } = evt.detail
122
123
 
123
124
  if (this.components.peerId.equals(peerId)) {
124
- void this.pushToPeerStore().catch(err => log.error(err))
125
+ void this.pushToPeerStore().catch(err => { log.error(err) })
125
126
  }
126
127
  })
127
128
  }
128
129
 
129
- isStarted () {
130
+ isStarted (): boolean {
130
131
  return this.started
131
132
  }
132
133
 
133
- async start () {
134
+ async start (): Promise<void> {
134
135
  if (this.started) {
135
136
  return
136
137
  }
@@ -158,7 +159,7 @@ export class IdentifyService implements Startable {
158
159
  this.started = true
159
160
  }
160
161
 
161
- async stop () {
162
+ async stop (): Promise<void> {
162
163
  await this.components.registrar.unhandle(this.identifyProtocolStr)
163
164
  await this.components.registrar.unhandle(this.identifyPushProtocolStr)
164
165
 
@@ -216,7 +217,7 @@ export class IdentifyService implements Startable {
216
217
  /**
217
218
  * Calls `push` on all peer connections
218
219
  */
219
- async pushToPeerStore () {
220
+ async pushToPeerStore (): Promise<void> {
220
221
  // Do not try to push if we are not running
221
222
  if (!this.isStarted()) {
222
223
  return
@@ -378,17 +379,20 @@ export class IdentifyService implements Startable {
378
379
  }
379
380
 
380
381
  log('identify completed for peer %p and protocols %o', id, protocols)
382
+ log('our observed address is %s', cleanObservedAddr)
381
383
 
382
- // TODO: Add and score our observed addr
383
- log('received observed address of %s', cleanObservedAddr?.toString())
384
- // this.components.addressManager.addObservedAddr(observedAddr)
384
+ if (cleanObservedAddr != null &&
385
+ this.components.addressManager.getObservedAddrs().length < (this.init.maxObservedAddresses ?? Infinity)) {
386
+ log('storing our observed address %s', cleanObservedAddr?.toString())
387
+ this.components.addressManager.addObservedAddr(cleanObservedAddr)
388
+ }
385
389
  }
386
390
 
387
391
  /**
388
392
  * Sends the `Identify` response with the Signed Peer Record
389
393
  * to the requesting peer over the given `connection`
390
394
  */
391
- async _handleIdentify (data: IncomingStreamData) {
395
+ async _handleIdentify (data: IncomingStreamData): Promise<void> {
392
396
  const { connection, stream } = data
393
397
  const timeoutController = new TimeoutController(this.init.timeout)
394
398
 
@@ -440,7 +444,7 @@ export class IdentifyService implements Startable {
440
444
  /**
441
445
  * Reads the Identify Push message from the given `connection`
442
446
  */
443
- async _handlePush (data: IncomingStreamData) {
447
+ async _handlePush (data: IncomingStreamData): Promise<void> {
444
448
  const { connection, stream } = data
445
449
  const timeoutController = new TimeoutController(this.init.timeout)
446
450
 
@@ -467,14 +471,16 @@ export class IdentifyService implements Startable {
467
471
  message = Identify.decode(data)
468
472
  }
469
473
  } catch (err: any) {
470
- return log.error('received invalid message', err)
474
+ log.error('received invalid message', err)
475
+ return
471
476
  } finally {
472
477
  stream.close()
473
478
  timeoutController.clear()
474
479
  }
475
480
 
476
481
  if (message == null) {
477
- return log.error('received invalid message')
482
+ log.error('received invalid message')
483
+ return
478
484
  }
479
485
 
480
486
  const id = connection.remotePeer
@@ -509,8 +515,7 @@ export class IdentifyService implements Startable {
509
515
 
510
516
  // LEGACY: Update peers data in PeerStore
511
517
  try {
512
- await this.components.peerStore.addressBook.set(id,
513
- message.listenAddrs.map((addr) => multiaddr(addr)))
518
+ await this.components.peerStore.addressBook.set(id, message.listenAddrs.map((addr) => multiaddr(addr)))
514
519
  } catch (err: any) {
515
520
  log.error('received invalid addrs', err)
516
521
  }
@@ -528,7 +533,7 @@ export class IdentifyService implements Startable {
528
533
  /**
529
534
  * Takes the `addr` and converts it to a Multiaddr if possible
530
535
  */
531
- static getCleanMultiaddr (addr: Uint8Array | string | null | undefined) {
536
+ static getCleanMultiaddr (addr: Uint8Array | string | null | undefined): Multiaddr | undefined {
532
537
  if (addr != null && addr.length > 0) {
533
538
  try {
534
539
  return multiaddr(addr)
@@ -2,10 +2,11 @@
2
2
  /* eslint-disable complexity */
3
3
  /* eslint-disable @typescript-eslint/no-namespace */
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
5
6
 
6
7
  import { encodeMessage, decodeMessage, message } from 'protons-runtime'
7
- import type { Uint8ArrayList } from 'uint8arraylist'
8
8
  import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
9
10
 
10
11
  export interface Identify {
11
12
  protocolVersion?: string
@@ -115,7 +116,7 @@ export namespace Identify {
115
116
  return _codec
116
117
  }
117
118
 
118
- export const encode = (obj: Identify): Uint8Array => {
119
+ export const encode = (obj: Partial<Identify>): Uint8Array => {
119
120
  return encodeMessage(obj, Identify.codec())
120
121
  }
121
122
 
package/src/index.ts CHANGED
@@ -22,9 +22,9 @@ import type { DualDHT } from '@libp2p/interface-dht'
22
22
  import type { Datastore } from 'interface-datastore'
23
23
  import type { PeerStoreInit } from '@libp2p/interface-peer-store'
24
24
  import type { PeerId } from '@libp2p/interface-peer-id'
25
- import type { RelayConfig } from './circuit/index.js'
26
25
  import type { PeerDiscovery } from '@libp2p/interface-peer-discovery'
27
- import type { ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection'
26
+ import type { ConnectionProtector } from '@libp2p/interface-connection'
27
+ import type { ConnectionGater } from '@libp2p/interface-connection-gater'
28
28
  import type { Transport } from '@libp2p/interface-transport'
29
29
  import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
30
30
  import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
@@ -35,6 +35,7 @@ import type { Metrics } from '@libp2p/interface-metrics'
35
35
  import type { PeerInfo } from '@libp2p/interface-peer-info'
36
36
  import type { PingServiceInit } from './ping/index.js'
37
37
  import type { FetchServiceInit } from './fetch/index.js'
38
+ import type { AutonatServiceInit } from './autonat/index.js'
38
39
  import type { Components } from './components.js'
39
40
  import type { Libp2p } from '@libp2p/interface-libp2p'
40
41
  import type { KeyChainInit } from '@libp2p/keychain'
@@ -42,6 +43,7 @@ import type { NatManagerInit } from './nat-manager.js'
42
43
  import type { AddressManagerInit } from './address-manager/index.js'
43
44
  import type { PeerRoutingInit } from './peer-routing.js'
44
45
  import type { ConnectionManagerInit } from './connection-manager/index.js'
46
+ import type { CircuitRelayService } from './circuit/index.js'
45
47
 
46
48
  /**
47
49
  * For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
@@ -65,7 +67,7 @@ export interface Libp2pInit {
65
67
  /**
66
68
  * A connection gater can deny new connections based on user criteria
67
69
  */
68
- connectionGater: Partial<ConnectionGater>
70
+ connectionGater: ConnectionGater
69
71
 
70
72
  /**
71
73
  * libp2p transport manager configuration
@@ -103,23 +105,28 @@ export interface Libp2pInit {
103
105
  * If configured as a relay this node will relay certain
104
106
  * types of traffic for other peers
105
107
  */
106
- relay: RelayConfig
108
+ relay: (components: Components) => CircuitRelayService
107
109
 
108
110
  /**
109
- * libp2p identify protocol options
111
+ * identify protocol options
110
112
  */
111
113
  identify: IdentifyServiceInit
112
114
 
113
115
  /**
114
- * libp2p ping protocol options
116
+ * ping protocol options
115
117
  */
116
118
  ping: PingServiceInit
117
119
 
118
120
  /**
119
- * libp2p fetch protocol options
121
+ * fetch protocol options
120
122
  */
121
123
  fetch: FetchServiceInit
122
124
 
125
+ /**
126
+ * autonat protocol options
127
+ */
128
+ autonat: AutonatServiceInit
129
+
123
130
  /**
124
131
  * An array that must include at least 1 compliant transport
125
132
  */
@@ -32,11 +32,12 @@ import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
32
32
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
33
33
  import type { Duplex } from 'it-stream-types'
34
34
  import map from 'it-map'
35
+ import type { Uint8ArrayList } from 'uint8arraylist'
35
36
 
36
37
  const log = logger('libp2p:plaintext')
37
38
  const PROTOCOL = '/plaintext/2.0.0'
38
39
 
39
- function lpEncodeExchange (exchange: Exchange) {
40
+ function lpEncodeExchange (exchange: Exchange): Uint8ArrayList {
40
41
  const pb = Exchange.encode(exchange)
41
42
 
42
43
  return lp.encode.single(pb)
@@ -2,10 +2,11 @@
2
2
  /* eslint-disable complexity */
3
3
  /* eslint-disable @typescript-eslint/no-namespace */
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
+ /* eslint-disable @typescript-eslint/no-empty-interface */
5
6
 
6
7
  import { encodeMessage, decodeMessage, message, enumeration } from 'protons-runtime'
7
- import type { Uint8ArrayList } from 'uint8arraylist'
8
8
  import type { Codec } from 'protons-runtime'
9
+ import type { Uint8ArrayList } from 'uint8arraylist'
9
10
 
10
11
  export interface Exchange {
11
12
  id?: Uint8Array
@@ -29,9 +30,7 @@ export namespace Exchange {
29
30
 
30
31
  if (obj.pubkey != null) {
31
32
  w.uint32(18)
32
- PublicKey.codec().encode(obj.pubkey, w, {
33
- writeDefaults: false
34
- })
33
+ PublicKey.codec().encode(obj.pubkey, w)
35
34
  }
36
35
 
37
36
  if (opts.lengthDelimited !== false) {
@@ -65,7 +64,7 @@ export namespace Exchange {
65
64
  return _codec
66
65
  }
67
66
 
68
- export const encode = (obj: Exchange): Uint8Array => {
67
+ export const encode = (obj: Partial<Exchange>): Uint8Array => {
69
68
  return encodeMessage(obj, Exchange.codec())
70
69
  }
71
70
 
@@ -89,7 +88,7 @@ enum __KeyTypeValues {
89
88
  }
90
89
 
91
90
  export namespace KeyType {
92
- export const codec = () => {
91
+ export const codec = (): Codec<KeyType> => {
93
92
  return enumeration<KeyType>(__KeyTypeValues)
94
93
  }
95
94
  }
@@ -108,12 +107,12 @@ export namespace PublicKey {
108
107
  w.fork()
109
108
  }
110
109
 
111
- if (opts.writeDefaults === true || (obj.Type != null && __KeyTypeValues[obj.Type] !== 0)) {
110
+ if (obj.Type != null && __KeyTypeValues[obj.Type] !== 0) {
112
111
  w.uint32(8)
113
112
  KeyType.codec().encode(obj.Type, w)
114
113
  }
115
114
 
116
- if (opts.writeDefaults === true || (obj.Data != null && obj.Data.byteLength > 0)) {
115
+ if ((obj.Data != null && obj.Data.byteLength > 0)) {
117
116
  w.uint32(18)
118
117
  w.bytes(obj.Data)
119
118
  }
@@ -152,7 +151,7 @@ export namespace PublicKey {
152
151
  return _codec
153
152
  }
154
153
 
155
- export const encode = (obj: PublicKey): Uint8Array => {
154
+ export const encode = (obj: Partial<PublicKey>): Uint8Array => {
156
155
  return encodeMessage(obj, PublicKey.codec())
157
156
  }
158
157
 
package/src/libp2p.ts CHANGED
@@ -10,8 +10,6 @@ import { codes } from './errors.js'
10
10
  import { DefaultAddressManager } from './address-manager/index.js'
11
11
  import { DefaultConnectionManager } from './connection-manager/index.js'
12
12
  import { AutoDialler } from './connection-manager/auto-dialler.js'
13
- import { Circuit } from './circuit/transport.js'
14
- import { Relay } from './circuit/index.js'
15
13
  import { DefaultKeyChain } from '@libp2p/keychain'
16
14
  import { DefaultTransportManager } from './transport-manager.js'
17
15
  import { DefaultUpgrader } from './upgrader.js'
@@ -24,10 +22,11 @@ import { PeerRecordUpdater } from './peer-record-updater.js'
24
22
  import { DHTPeerRouting } from './dht/dht-peer-routing.js'
25
23
  import { PersistentPeerStore } from '@libp2p/peer-store'
26
24
  import { DHTContentRouting } from './dht/dht-content-routing.js'
25
+ import { AutonatService } from './autonat/index.js'
27
26
  import { DefaultComponents } from './components.js'
28
27
  import type { Components } from './components.js'
29
28
  import type { PeerId } from '@libp2p/interface-peer-id'
30
- import type { Connection } from '@libp2p/interface-connection'
29
+ import type { Connection, Stream } from '@libp2p/interface-connection'
31
30
  import type { PeerRouting } from '@libp2p/interface-peer-routing'
32
31
  import type { ContentRouting } from '@libp2p/interface-content-routing'
33
32
  import type { PubSub } from '@libp2p/interface-pubsub'
@@ -52,6 +51,7 @@ import { peerIdFromString } from '@libp2p/peer-id'
52
51
  import type { Datastore } from 'interface-datastore'
53
52
  import type { KeyChain } from '@libp2p/interface-keychain'
54
53
  import mergeOptions from 'merge-options'
54
+ import type { CircuitRelayService } from './circuit/index.js'
55
55
 
56
56
  const log = logger('libp2p')
57
57
 
@@ -60,8 +60,10 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
60
60
  public dht: DualDHT
61
61
  public pubsub: PubSub
62
62
  public identifyService: IdentifyService
63
+ public circuitService?: CircuitRelayService
63
64
  public fetchService: FetchService
64
65
  public pingService: PingService
66
+ public autonatService: AutonatService
65
67
  public components: Components
66
68
  public peerStore: PeerStore
67
69
  public contentRouting: ContentRouting
@@ -155,12 +157,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
155
157
  // update our peer record when addresses change
156
158
  this.configureComponent(new PeerRecordUpdater(this.components))
157
159
 
158
- this.configureComponent(new AutoDialler(this.components, {
159
- enabled: init.connectionManager.autoDial,
160
- minConnections: init.connectionManager.minConnections,
161
- autoDialInterval: init.connectionManager.autoDialInterval
162
- }))
163
-
164
160
  // Create keychain
165
161
  const keychainOpts = DefaultKeyChain.generateOptions()
166
162
  this.keychain = this.configureComponent(new DefaultKeyChain(this.components, {
@@ -171,10 +167,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
171
167
  // Create the Nat Manager
172
168
  this.services.push(new NatManager(this.components, init.nat))
173
169
 
174
- init.transports.forEach((fn) => {
175
- this.components.transportManager.add(this.configureComponent(fn(this.components)))
176
- })
177
-
178
170
  // Add the identify service
179
171
  this.identifyService = new IdentifyService(this.components, {
180
172
  ...init.identify
@@ -226,15 +218,6 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
226
218
  routers: contentRouters
227
219
  }))
228
220
 
229
- if (init.relay.enabled) {
230
- this.components.transportManager.add(this.configureComponent(new Circuit(this.components, init.relay)))
231
-
232
- this.configureComponent(new Relay(this.components, {
233
- addressSorter: init.connectionManager.addressSorter,
234
- ...init.relay
235
- }))
236
- }
237
-
238
221
  this.fetchService = this.configureComponent(new FetchService(this.components, {
239
222
  ...init.fetch
240
223
  }))
@@ -243,6 +226,19 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
243
226
  ...init.ping
244
227
  }))
245
228
 
229
+ this.autonatService = this.configureComponent(new AutonatService(this.components, {
230
+ ...init.autonat
231
+ }))
232
+
233
+ this.configureComponent(new AutoDialler(this.components, {
234
+ minConnections: init.connectionManager.minConnections,
235
+ autoDialInterval: init.connectionManager.autoDialInterval
236
+ }))
237
+
238
+ if (init.relay != null) {
239
+ this.circuitService = this.configureComponent(init.relay(this.components))
240
+ }
241
+
246
242
  // Discovery modules
247
243
  for (const fn of init.peerDiscovery ?? []) {
248
244
  const service = this.configureComponent(fn(this.components))
@@ -251,6 +247,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
251
247
  this.onDiscoveryPeer(evt)
252
248
  })
253
249
  }
250
+
251
+ // Transport modules
252
+ init.transports.forEach((fn) => {
253
+ this.components.transportManager.add(this.configureComponent(fn(this.components)))
254
+ })
254
255
  }
255
256
 
256
257
  private configureComponent <T> (component: T): T {
@@ -264,7 +265,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
264
265
  /**
265
266
  * Starts the libp2p node and all its subsystems
266
267
  */
267
- async start () {
268
+ async start (): Promise<void> {
268
269
  if (this.started) {
269
270
  return
270
271
  }
@@ -291,7 +292,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
291
292
 
292
293
  // start any startables
293
294
  await Promise.all(
294
- this.services.map(service => service.start())
295
+ this.services.map(async service => { await service.start() })
295
296
  )
296
297
 
297
298
  await Promise.all(
@@ -313,7 +314,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
313
314
  /**
314
315
  * Stop the libp2p node by closing its listeners and open connections
315
316
  */
316
- async stop () {
317
+ async stop (): Promise<void> {
317
318
  if (!this.started) {
318
319
  return
319
320
  }
@@ -331,7 +332,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
331
332
  )
332
333
 
333
334
  await Promise.all(
334
- this.services.map(service => service.stop())
335
+ this.services.map(async service => { await service.stop() })
335
336
  )
336
337
 
337
338
  await Promise.all(
@@ -345,7 +346,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
345
346
  log('libp2p has stopped')
346
347
  }
347
348
 
348
- isStarted () {
349
+ isStarted (): boolean {
349
350
  return this.started
350
351
  }
351
352
 
@@ -367,7 +368,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
367
368
  return await this.components.connectionManager.openConnection(peer, options)
368
369
  }
369
370
 
370
- async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}) {
371
+ async dialProtocol (peer: PeerId | Multiaddr, protocols: string | string[], options: AbortOptions = {}): Promise<Stream> {
371
372
  if (protocols == null) {
372
373
  throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
373
374
  }
@@ -486,7 +487,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
486
487
  return await this.registrar.register(protocol, topology)
487
488
  }
488
489
 
489
- unregister (id: string) {
490
+ unregister (id: string): void {
490
491
  this.registrar.unregister(id)
491
492
  }
492
493
 
@@ -494,7 +495,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
494
495
  * Called whenever peer discovery services emit `peer` events.
495
496
  * Known peers may be emitted.
496
497
  */
497
- onDiscoveryPeer (evt: CustomEvent<PeerInfo>) {
498
+ onDiscoveryPeer (evt: CustomEvent<PeerInfo>): void {
498
499
  const { detail: peer } = evt
499
500
 
500
501
  if (peer.id.toString() === this.peerId.toString()) {
@@ -503,11 +504,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
503
504
  }
504
505
 
505
506
  if (peer.multiaddrs.length > 0) {
506
- void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => log.error(err))
507
+ void this.components.peerStore.addressBook.add(peer.id, peer.multiaddrs).catch(err => { log.error(err) })
507
508
  }
508
509
 
509
510
  if (peer.protocols.length > 0) {
510
- void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => log.error(err))
511
+ void this.components.peerStore.protoBook.set(peer.id, peer.protocols).catch(err => { log.error(err) })
511
512
  }
512
513
 
513
514
  this.dispatchEvent(new CustomEvent<PeerInfo>('peer:discovery', { detail: peer }))
@@ -15,7 +15,7 @@ import type { AddressManager } from '@libp2p/interface-address-manager'
15
15
  const log = logger('libp2p:nat')
16
16
  const DEFAULT_TTL = 7200
17
17
 
18
- function highPort (min = 1024, max = 65535) {
18
+ function highPort (min = 1024, max = 65535): number {
19
19
  return Math.floor(Math.random() * (max - min + 1) + min)
20
20
  }
21
21
 
@@ -98,18 +98,20 @@ export class NatManager implements Startable {
98
98
  }
99
99
  }
100
100
 
101
- isStarted () {
101
+ isStarted (): boolean {
102
102
  return this.started
103
103
  }
104
104
 
105
- start () {}
105
+ start (): void {
106
+ // #TODO: is there a way to remove this? Seems like a hack
107
+ }
106
108
 
107
109
  /**
108
110
  * Attempt to use uPnP to configure port mapping using the current gateway.
109
111
  *
110
112
  * Run after start to ensure the transport manager has all addresses configured.
111
113
  */
112
- afterStart () {
114
+ afterStart (): void {
113
115
  if (isBrowser || !this.enabled || this.started) {
114
116
  return
115
117
  }
@@ -123,7 +125,7 @@ export class NatManager implements Startable {
123
125
  })
124
126
  }
125
127
 
126
- async _start () {
128
+ async _start (): Promise<void> {
127
129
  const addrs = this.components.transportManager.getAddrs()
128
130
 
129
131
  for (const addr of addrs) {
@@ -178,7 +180,7 @@ export class NatManager implements Startable {
178
180
  }
179
181
  }
180
182
 
181
- async _getClient () {
183
+ async _getClient (): Promise<NatAPI> {
182
184
  if (this.client != null) {
183
185
  return this.client
184
186
  }
@@ -196,7 +198,7 @@ export class NatManager implements Startable {
196
198
  /**
197
199
  * Stops the NAT manager
198
200
  */
199
- async stop () {
201
+ async stop (): Promise<void> {
200
202
  if (isBrowser || this.client == null) {
201
203
  return
202
204
  }
@@ -26,18 +26,18 @@ export class PeerRecordUpdater implements Startable {
26
26
  this.update = this.update.bind(this)
27
27
  }
28
28
 
29
- isStarted () {
29
+ isStarted (): boolean {
30
30
  return this.started
31
31
  }
32
32
 
33
- async start () {
33
+ async start (): Promise<void> {
34
34
  this.started = true
35
35
  this.components.transportManager.addEventListener('listener:listening', this.update)
36
36
  this.components.transportManager.addEventListener('listener:close', this.update)
37
37
  this.components.addressManager.addEventListener('change:addresses', this.update)
38
38
  }
39
39
 
40
- async stop () {
40
+ async stop (): Promise<void> {
41
41
  this.started = false
42
42
  this.components.transportManager.removeEventListener('listener:listening', this.update)
43
43
  this.components.transportManager.removeEventListener('listener:close', this.update)
@@ -47,7 +47,7 @@ export class PeerRecordUpdater implements Startable {
47
47
  /**
48
48
  * Create (or update if existing) self peer record and store it in the AddressBook.
49
49
  */
50
- update () {
50
+ update (): void {
51
51
  Promise.resolve()
52
52
  .then(async () => {
53
53
  const peerRecord = new PeerRecord({
@@ -76,14 +76,14 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
76
76
  this._findClosestPeersTask = this._findClosestPeersTask.bind(this)
77
77
  }
78
78
 
79
- isStarted () {
79
+ isStarted (): boolean {
80
80
  return this.started
81
81
  }
82
82
 
83
83
  /**
84
84
  * Start peer routing service.
85
85
  */
86
- async start () {
86
+ async start (): Promise<void> {
87
87
  if (this.started || this.routers.length === 0 || this.timeoutId != null || this.refreshManagerInit.enabled === false) {
88
88
  return
89
89
  }
@@ -98,7 +98,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
98
98
  /**
99
99
  * Recurrent task to find closest peers and add their addresses to the Address Book.
100
100
  */
101
- async _findClosestPeersTask () {
101
+ async _findClosestPeersTask (): Promise<void> {
102
102
  if (this.abortController != null) {
103
103
  // we are already running the query
104
104
  return
@@ -127,7 +127,7 @@ export class DefaultPeerRouting implements PeerRouting, Startable {
127
127
  /**
128
128
  * Stop peer routing service.
129
129
  */
130
- async stop () {
130
+ async stop (): Promise<void> {
131
131
  clearDelayedInterval(this.timeoutId)
132
132
 
133
133
  // abort query if it is in-flight
package/src/ping/index.ts CHANGED
@@ -47,7 +47,7 @@ export class PingService implements Startable {
47
47
  this.init = init
48
48
  }
49
49
 
50
- async start () {
50
+ async start (): Promise<void> {
51
51
  await this.components.registrar.handle(this.protocol, this.handleMessage, {
52
52
  maxInboundStreams: this.init.maxInboundStreams,
53
53
  maxOutboundStreams: this.init.maxOutboundStreams
@@ -55,19 +55,19 @@ export class PingService implements Startable {
55
55
  this.started = true
56
56
  }
57
57
 
58
- async stop () {
58
+ async stop (): Promise<void> {
59
59
  await this.components.registrar.unhandle(this.protocol)
60
60
  this.started = false
61
61
  }
62
62
 
63
- isStarted () {
63
+ isStarted (): boolean {
64
64
  return this.started
65
65
  }
66
66
 
67
67
  /**
68
68
  * A handler to register with Libp2p to process ping messages
69
69
  */
70
- handleMessage (data: IncomingStreamData) {
70
+ handleMessage (data: IncomingStreamData): void {
71
71
  const { stream } = data
72
72
 
73
73
  void pipe(stream, stream)
@@ -11,7 +11,7 @@ const log = logger('libp2p:pnet')
11
11
  /**
12
12
  * Creates a stream iterable to encrypt messages in a private network
13
13
  */
14
- export function createBoxStream (nonce: Uint8Array, psk: Uint8Array) {
14
+ export function createBoxStream (nonce: Uint8Array, psk: Uint8Array): (source: Source<Uint8Array>) => AsyncIterable<Uint8Array> {
15
15
  const xor = xsalsa20(nonce, psk)
16
16
 
17
17
  return (source: Source<Uint8Array>) => (async function * () {
@@ -38,7 +38,7 @@ export function createUnboxStream (nonce: Uint8Array, psk: Uint8Array) {
38
38
  /**
39
39
  * Decode the version 1 psk from the given Uint8Array
40
40
  */
41
- export function decodeV1PSK (pskBuffer: Uint8Array) {
41
+ export function decodeV1PSK (pskBuffer: Uint8Array): { tag: string | undefined, codecName: string | undefined, psk: Uint8Array } {
42
42
  try {
43
43
  // This should pull from multibase/multicodec to allow for
44
44
  // more encoding flexibility. Ideally we'd consume the codecs
@@ -58,7 +58,7 @@ export function decodeV1PSK (pskBuffer: Uint8Array) {
58
58
  return {
59
59
  tag: pskTag,
60
60
  codecName: codec,
61
- psk: psk
61
+ psk
62
62
  }
63
63
  } catch (err: any) {
64
64
  log.error(err)