libp2p 0.44.0 → 0.45.0

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 (188) hide show
  1. package/dist/index.min.js +30 -30
  2. package/dist/src/address-manager/index.d.ts +8 -4
  3. package/dist/src/address-manager/index.d.ts.map +1 -1
  4. package/dist/src/address-manager/index.js +37 -6
  5. package/dist/src/address-manager/index.js.map +1 -1
  6. package/dist/src/address-manager/utils.d.ts +2 -0
  7. package/dist/src/address-manager/utils.d.ts.map +1 -0
  8. package/dist/src/address-manager/utils.js +12 -0
  9. package/dist/src/address-manager/utils.js.map +1 -0
  10. package/dist/src/autonat/constants.d.ts +16 -2
  11. package/dist/src/autonat/constants.d.ts.map +1 -1
  12. package/dist/src/autonat/constants.js +16 -2
  13. package/dist/src/autonat/constants.js.map +1 -1
  14. package/dist/src/autonat/index.d.ts +12 -33
  15. package/dist/src/autonat/index.d.ts.map +1 -1
  16. package/dist/src/autonat/index.js +36 -30
  17. package/dist/src/autonat/index.js.map +1 -1
  18. package/dist/src/circuit-relay/server/advert-service.d.ts.map +1 -1
  19. package/dist/src/circuit-relay/server/advert-service.js +2 -2
  20. package/dist/src/circuit-relay/server/advert-service.js.map +1 -1
  21. package/dist/src/circuit-relay/server/index.d.ts.map +1 -1
  22. package/dist/src/circuit-relay/server/index.js +10 -3
  23. package/dist/src/circuit-relay/server/index.js.map +1 -1
  24. package/dist/src/circuit-relay/transport/discovery.d.ts.map +1 -1
  25. package/dist/src/circuit-relay/transport/discovery.js +4 -2
  26. package/dist/src/circuit-relay/transport/discovery.js.map +1 -1
  27. package/dist/src/circuit-relay/transport/index.d.ts +3 -0
  28. package/dist/src/circuit-relay/transport/index.d.ts.map +1 -1
  29. package/dist/src/circuit-relay/transport/index.js +6 -4
  30. package/dist/src/circuit-relay/transport/index.js.map +1 -1
  31. package/dist/src/circuit-relay/transport/listener.d.ts.map +1 -1
  32. package/dist/src/circuit-relay/transport/listener.js +30 -26
  33. package/dist/src/circuit-relay/transport/listener.js.map +1 -1
  34. package/dist/src/circuit-relay/transport/reservation-store.d.ts +5 -4
  35. package/dist/src/circuit-relay/transport/reservation-store.d.ts.map +1 -1
  36. package/dist/src/circuit-relay/transport/reservation-store.js +37 -30
  37. package/dist/src/circuit-relay/transport/reservation-store.js.map +1 -1
  38. package/dist/src/components.d.ts +6 -63
  39. package/dist/src/components.d.ts.map +1 -1
  40. package/dist/src/components.js +60 -164
  41. package/dist/src/components.js.map +1 -1
  42. package/dist/src/config/connection-gater.browser.d.ts +8 -0
  43. package/dist/src/config/connection-gater.browser.d.ts.map +1 -0
  44. package/dist/src/config/connection-gater.browser.js +27 -0
  45. package/dist/src/config/connection-gater.browser.js.map +1 -0
  46. package/dist/src/config/connection-gater.d.ts +6 -0
  47. package/dist/src/config/connection-gater.d.ts.map +1 -0
  48. package/dist/src/config/connection-gater.js +18 -0
  49. package/dist/src/config/connection-gater.js.map +1 -0
  50. package/dist/src/config.d.ts +2 -1
  51. package/dist/src/config.d.ts.map +1 -1
  52. package/dist/src/config.js +0 -62
  53. package/dist/src/config.js.map +1 -1
  54. package/dist/src/connection-manager/auto-dial.d.ts +3 -0
  55. package/dist/src/connection-manager/auto-dial.d.ts.map +1 -1
  56. package/dist/src/connection-manager/auto-dial.js +36 -13
  57. package/dist/src/connection-manager/auto-dial.js.map +1 -1
  58. package/dist/src/connection-manager/connection-pruner.d.ts +4 -0
  59. package/dist/src/connection-manager/connection-pruner.d.ts.map +1 -1
  60. package/dist/src/connection-manager/connection-pruner.js +23 -8
  61. package/dist/src/connection-manager/connection-pruner.js.map +1 -1
  62. package/dist/src/connection-manager/dial-queue.d.ts +2 -1
  63. package/dist/src/connection-manager/dial-queue.d.ts.map +1 -1
  64. package/dist/src/connection-manager/dial-queue.js +36 -14
  65. package/dist/src/connection-manager/dial-queue.js.map +1 -1
  66. package/dist/src/connection-manager/index.d.ts +8 -7
  67. package/dist/src/connection-manager/index.d.ts.map +1 -1
  68. package/dist/src/connection-manager/index.js +23 -34
  69. package/dist/src/connection-manager/index.js.map +1 -1
  70. package/dist/src/content-routing/index.d.ts +0 -10
  71. package/dist/src/content-routing/index.d.ts.map +1 -1
  72. package/dist/src/content-routing/index.js +7 -42
  73. package/dist/src/content-routing/index.js.map +1 -1
  74. package/dist/src/content-routing/utils.d.ts.map +1 -1
  75. package/dist/src/content-routing/utils.js +3 -1
  76. package/dist/src/content-routing/utils.js.map +1 -1
  77. package/dist/src/fetch/index.d.ts +13 -35
  78. package/dist/src/fetch/index.d.ts.map +1 -1
  79. package/dist/src/fetch/index.js +7 -9
  80. package/dist/src/fetch/index.js.map +1 -1
  81. package/dist/src/identify/identify.d.ts +53 -0
  82. package/dist/src/identify/identify.d.ts.map +1 -0
  83. package/dist/src/identify/identify.js +405 -0
  84. package/dist/src/identify/identify.js.map +1 -0
  85. package/dist/src/identify/index.d.ts +12 -58
  86. package/dist/src/identify/index.d.ts.map +1 -1
  87. package/dist/src/identify/index.js +5 -388
  88. package/dist/src/identify/index.js.map +1 -1
  89. package/dist/src/index.d.ts +13 -68
  90. package/dist/src/index.d.ts.map +1 -1
  91. package/dist/src/index.js +2 -1
  92. package/dist/src/index.js.map +1 -1
  93. package/dist/src/insecure/index.js.map +1 -1
  94. package/dist/src/libp2p.d.ts +11 -42
  95. package/dist/src/libp2p.d.ts.map +1 -1
  96. package/dist/src/libp2p.js +137 -196
  97. package/dist/src/libp2p.js.map +1 -1
  98. package/dist/src/peer-routing.d.ts +1 -38
  99. package/dist/src/peer-routing.d.ts.map +1 -1
  100. package/dist/src/peer-routing.js +0 -59
  101. package/dist/src/peer-routing.js.map +1 -1
  102. package/dist/src/ping/constants.d.ts +4 -0
  103. package/dist/src/ping/constants.d.ts.map +1 -1
  104. package/dist/src/ping/constants.js +10 -0
  105. package/dist/src/ping/constants.js.map +1 -1
  106. package/dist/src/ping/index.d.ts +10 -27
  107. package/dist/src/ping/index.d.ts.map +1 -1
  108. package/dist/src/ping/index.js +18 -21
  109. package/dist/src/ping/index.js.map +1 -1
  110. package/dist/src/registrar.d.ts +5 -2
  111. package/dist/src/registrar.d.ts.map +1 -1
  112. package/dist/src/registrar.js +27 -25
  113. package/dist/src/registrar.js.map +1 -1
  114. package/dist/src/transport-manager.d.ts +11 -4
  115. package/dist/src/transport-manager.d.ts.map +1 -1
  116. package/dist/src/transport-manager.js +19 -10
  117. package/dist/src/transport-manager.js.map +1 -1
  118. package/dist/src/upgrader.d.ts +15 -10
  119. package/dist/src/upgrader.d.ts.map +1 -1
  120. package/dist/src/upgrader.js +39 -48
  121. package/dist/src/upgrader.js.map +1 -1
  122. package/dist/src/upnp-nat/index.d.ts +42 -0
  123. package/dist/src/upnp-nat/index.d.ts.map +1 -0
  124. package/dist/src/{nat-manager.js → upnp-nat/index.js} +11 -7
  125. package/dist/src/upnp-nat/index.js.map +1 -0
  126. package/dist/src/version.d.ts +1 -1
  127. package/dist/src/version.d.ts.map +1 -1
  128. package/dist/src/version.js +1 -1
  129. package/dist/src/version.js.map +1 -1
  130. package/dist/typedoc-urls.json +25 -10
  131. package/package.json +61 -45
  132. package/src/address-manager/index.ts +50 -8
  133. package/src/address-manager/utils.ts +14 -0
  134. package/src/autonat/constants.ts +17 -2
  135. package/src/autonat/index.ts +53 -45
  136. package/src/circuit-relay/server/advert-service.ts +2 -2
  137. package/src/circuit-relay/server/index.ts +11 -3
  138. package/src/circuit-relay/transport/discovery.ts +5 -3
  139. package/src/circuit-relay/transport/index.ts +9 -4
  140. package/src/circuit-relay/transport/listener.ts +38 -28
  141. package/src/circuit-relay/transport/reservation-store.ts +34 -20
  142. package/src/components.ts +71 -234
  143. package/src/config/connection-gater.browser.ts +31 -0
  144. package/src/config/connection-gater.ts +19 -0
  145. package/src/config.ts +3 -64
  146. package/src/connection-manager/auto-dial.ts +44 -15
  147. package/src/connection-manager/connection-pruner.ts +28 -8
  148. package/src/connection-manager/dial-queue.ts +41 -21
  149. package/src/connection-manager/index.ts +31 -43
  150. package/src/content-routing/index.ts +7 -55
  151. package/src/content-routing/utils.ts +3 -1
  152. package/src/fetch/index.ts +51 -14
  153. package/src/identify/identify.ts +518 -0
  154. package/src/identify/index.ts +17 -507
  155. package/src/index.ts +14 -77
  156. package/src/insecure/index.ts +4 -4
  157. package/src/libp2p.ts +139 -260
  158. package/src/peer-routing.ts +1 -101
  159. package/src/ping/constants.ts +11 -0
  160. package/src/ping/index.ts +32 -28
  161. package/src/registrar.ts +31 -27
  162. package/src/transport-manager.ts +27 -13
  163. package/src/upgrader.ts +62 -58
  164. package/src/{nat-manager.ts → upnp-nat/index.ts} +16 -17
  165. package/src/version.ts +1 -1
  166. package/dist/src/dht/dht-content-routing.d.ts +0 -17
  167. package/dist/src/dht/dht-content-routing.d.ts.map +0 -1
  168. package/dist/src/dht/dht-content-routing.js +0 -32
  169. package/dist/src/dht/dht-content-routing.js.map +0 -1
  170. package/dist/src/dht/dht-peer-routing.d.ts +0 -15
  171. package/dist/src/dht/dht-peer-routing.d.ts.map +0 -1
  172. package/dist/src/dht/dht-peer-routing.js +0 -26
  173. package/dist/src/dht/dht-peer-routing.js.map +0 -1
  174. package/dist/src/dht/dummy-dht.d.ts +0 -20
  175. package/dist/src/dht/dummy-dht.d.ts.map +0 -1
  176. package/dist/src/dht/dummy-dht.js +0 -46
  177. package/dist/src/dht/dummy-dht.js.map +0 -1
  178. package/dist/src/nat-manager.d.ts +0 -74
  179. package/dist/src/nat-manager.d.ts.map +0 -1
  180. package/dist/src/nat-manager.js.map +0 -1
  181. package/dist/src/peer-record-updater.d.ts +0 -24
  182. package/dist/src/peer-record-updater.d.ts.map +0 -1
  183. package/dist/src/peer-record-updater.js +0 -44
  184. package/dist/src/peer-record-updater.js.map +0 -1
  185. package/src/dht/dht-content-routing.ts +0 -44
  186. package/src/dht/dht-peer-routing.ts +0 -36
  187. package/src/dht/dummy-dht.ts +0 -60
  188. package/src/peer-record-updater.ts +0 -65
package/src/index.ts CHANGED
@@ -17,10 +17,7 @@
17
17
  import { createLibp2pNode } from './libp2p.js'
18
18
  import type { RecursivePartial } from '@libp2p/interfaces'
19
19
  import type { TransportManagerInit } from './transport-manager.js'
20
- import type { IdentifyServiceInit } from './identify/index.js'
21
- import type { DualDHT } from '@libp2p/interface-dht'
22
20
  import type { Datastore } from 'interface-datastore'
23
- import type { PeerStoreInit } from '@libp2p/interface-peer-store'
24
21
  import type { PeerId } from '@libp2p/interface-peer-id'
25
22
  import type { PeerDiscovery } from '@libp2p/interface-peer-discovery'
26
23
  import type { ConnectionProtector } from '@libp2p/interface-connection'
@@ -30,25 +27,22 @@ import type { StreamMuxerFactory } from '@libp2p/interface-stream-muxer'
30
27
  import type { ConnectionEncrypter } from '@libp2p/interface-connection-encrypter'
31
28
  import type { PeerRouting } from '@libp2p/interface-peer-routing'
32
29
  import type { ContentRouting } from '@libp2p/interface-content-routing'
33
- import type { PubSub } from '@libp2p/interface-pubsub'
34
30
  import type { Metrics } from '@libp2p/interface-metrics'
35
- import type { PeerInfo } from '@libp2p/interface-peer-info'
36
- import type { PingServiceInit } from './ping/index.js'
37
- import type { FetchServiceInit } from './fetch/index.js'
38
- import type { AutonatServiceInit } from './autonat/index.js'
39
31
  import type { Components } from './components.js'
40
- import type { Libp2p } from '@libp2p/interface-libp2p'
32
+ import type { Libp2p, ServiceMap } from '@libp2p/interface-libp2p'
41
33
  import type { KeyChainInit } from '@libp2p/keychain'
42
- import type { NatManagerInit } from './nat-manager.js'
43
34
  import type { AddressManagerInit } from './address-manager/index.js'
44
- import type { PeerRoutingInit } from './peer-routing.js'
45
35
  import type { ConnectionManagerInit } from './connection-manager/index.js'
46
- import type { CircuitRelayService } from './circuit-relay/index.js'
36
+ import type { PersistentPeerStoreInit } from '@libp2p/peer-store'
37
+
38
+ export type ServiceFactoryMap<T extends Record<string, unknown> = Record<string, unknown>> = {
39
+ [Property in keyof T]: (components: Components) => T[Property]
40
+ }
47
41
 
48
42
  /**
49
43
  * For Libp2p configurations and modules details read the [Configuration Document](./CONFIGURATION.md).
50
44
  */
51
- export interface Libp2pInit {
45
+ export interface Libp2pInit<T extends ServiceMap = {}> {
52
46
  /**
53
47
  * peerId instance (it will be created if not provided)
54
48
  */
@@ -84,49 +78,13 @@ export interface Libp2pInit {
84
78
  /**
85
79
  * libp2p PeerStore configuration
86
80
  */
87
- peerStore: PeerStoreInit
88
-
89
- /**
90
- * libp2p Peer routing service configuration
91
- */
92
- peerRouting: PeerRoutingInit
81
+ peerStore: PersistentPeerStoreInit
93
82
 
94
83
  /**
95
84
  * keychain configuration
96
85
  */
97
86
  keychain: KeyChainInit
98
87
 
99
- /**
100
- * The NAT manager controls uPNP hole punching
101
- */
102
- nat: NatManagerInit
103
-
104
- /**
105
- * If configured as a relay this node will relay certain
106
- * types of traffic for other peers
107
- */
108
- relay: (components: Components) => CircuitRelayService
109
-
110
- /**
111
- * identify protocol options
112
- */
113
- identify: IdentifyServiceInit
114
-
115
- /**
116
- * ping protocol options
117
- */
118
- ping: PingServiceInit
119
-
120
- /**
121
- * fetch protocol options
122
- */
123
- fetch: FetchServiceInit
124
-
125
- /**
126
- * autonat protocol options
127
- */
128
- autonat: AutonatServiceInit
129
-
130
88
  /**
131
89
  * An array that must include at least 1 compliant transport
132
90
  */
@@ -137,47 +95,25 @@ export interface Libp2pInit {
137
95
  peerRouters?: Array<(components: Components) => PeerRouting>
138
96
  contentRouters?: Array<(components: Components) => ContentRouting>
139
97
 
140
- /**
141
- * Pass a DHT implementation to enable DHT operations
142
- */
143
- dht?: (components: Components) => DualDHT
144
-
145
98
  /**
146
99
  * A Metrics implementation can be supplied to collect metrics on this node
147
100
  */
148
101
  metrics?: (components: Components) => Metrics
149
102
 
150
- /**
151
- * If a PubSub implmentation is supplied, PubSub operations will become available
152
- */
153
- pubsub?: (components: Components) => PubSub
154
-
155
103
  /**
156
104
  * A ConnectionProtector can be used to create a secure overlay on top of the network using pre-shared keys
157
105
  */
158
106
  connectionProtector?: (components: Components) => ConnectionProtector
159
- }
160
107
 
161
- /**
162
- * Once you have a libp2p instance, you can listen to several events it emits, so that you can be notified of relevant network events.
163
- */
164
- export interface Libp2pEvents {
165
108
  /**
166
- * @example
167
- *
168
- * ```js
169
- * libp2p.addEventListener('peer:discovery', (event) => {
170
- * const peerInfo = event.detail
171
- * // ...
172
- * })
173
- * ```
109
+ * Arbitrary libp2p modules
174
110
  */
175
- 'peer:discovery': CustomEvent<PeerInfo>
111
+ services: ServiceFactoryMap<T>
176
112
  }
177
113
 
178
114
  export type { Libp2p }
179
115
 
180
- export type Libp2pOptions = RecursivePartial<Libp2pInit> & { start?: boolean }
116
+ export type Libp2pOptions<T extends ServiceMap = {}> = RecursivePartial<Libp2pInit<T>> & { start?: boolean }
181
117
 
182
118
  /**
183
119
  * Returns a new instance of the Libp2p interface, generating a new PeerId
@@ -192,11 +128,12 @@ export type Libp2pOptions = RecursivePartial<Libp2pInit> & { start?: boolean }
192
128
  * import { tcp } from '@libp2p/tcp'
193
129
  * import { mplex } from '@libp2p/mplex'
194
130
  * import { noise } from '@chainsafe/libp2p-noise'
131
+ * import { yamux } from '@chainsafe/libp2p-yamux'
195
132
  *
196
133
  * // specify options
197
134
  * const options = {
198
135
  * transports: [tcp()],
199
- * streamMuxers: [mplex()],
136
+ * streamMuxers: [yamux(), mplex()],
200
137
  * connectionEncryption: [noise()]
201
138
  * }
202
139
  *
@@ -204,7 +141,7 @@ export type Libp2pOptions = RecursivePartial<Libp2pInit> & { start?: boolean }
204
141
  * const libp2p = await createLibp2p(options)
205
142
  * ```
206
143
  */
207
- export async function createLibp2p (options: Libp2pOptions): Promise<Libp2p> {
144
+ export async function createLibp2p <T extends ServiceMap = {}> (options: Libp2pOptions<T>): Promise<Libp2p<T>> {
208
145
  const node = await createLibp2pNode(options)
209
146
 
210
147
  if (options.start !== false) {
@@ -30,7 +30,7 @@ import { Exchange, KeyType } from './pb/proto.js'
30
30
  import type { PeerId } from '@libp2p/interface-peer-id'
31
31
  import { peerIdFromBytes, peerIdFromKeys } from '@libp2p/peer-id'
32
32
  import type { ConnectionEncrypter, SecuredConnection } from '@libp2p/interface-connection-encrypter'
33
- import type { Duplex } from 'it-stream-types'
33
+ import type { Duplex, Source } from 'it-stream-types'
34
34
  import map from 'it-map'
35
35
  import type { Uint8ArrayList } from 'uint8arraylist'
36
36
 
@@ -46,7 +46,7 @@ function lpEncodeExchange (exchange: Exchange): Uint8ArrayList {
46
46
  /**
47
47
  * Encrypt connection
48
48
  */
49
- async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
49
+ async function encrypt (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
50
50
  const shake = handshake(conn)
51
51
 
52
52
  let type = KeyType.RSA
@@ -124,11 +124,11 @@ async function encrypt (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: Pe
124
124
  class Plaintext implements ConnectionEncrypter {
125
125
  public protocol: string = PROTOCOL
126
126
 
127
- async secureInbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
127
+ async secureInbound (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
128
128
  return await encrypt(localId, conn, remoteId)
129
129
  }
130
130
 
131
- async secureOutbound (localId: PeerId, conn: Duplex<Uint8Array>, remoteId?: PeerId): Promise<SecuredConnection> {
131
+ async secureOutbound (localId: PeerId, conn: Duplex<AsyncGenerator<Uint8Array>, Source<Uint8Array>, Promise<void>>, remoteId?: PeerId): Promise<SecuredConnection> {
132
132
  return await encrypt(localId, conn, remoteId)
133
133
  }
134
134
  }