libp2p 0.44.0-97b473ad → 0.44.0-e3deaa40

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