libp2p 0.40.0-fed012d → 0.41.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 (49) hide show
  1. package/README.md +12 -0
  2. package/dist/src/address-manager/index.d.ts.map +1 -1
  3. package/dist/src/address-manager/index.js +1 -1
  4. package/dist/src/address-manager/index.js.map +1 -1
  5. package/dist/src/connection/index.d.ts +83 -0
  6. package/dist/src/connection/index.d.ts.map +1 -0
  7. package/dist/src/connection/index.js +96 -0
  8. package/dist/src/connection/index.js.map +1 -0
  9. package/dist/src/connection-manager/dialer/index.d.ts +12 -12
  10. package/dist/src/connection-manager/dialer/index.d.ts.map +1 -1
  11. package/dist/src/connection-manager/dialer/index.js +79 -54
  12. package/dist/src/connection-manager/dialer/index.js.map +1 -1
  13. package/dist/src/connection-manager/index.d.ts +2 -2
  14. package/dist/src/connection-manager/index.d.ts.map +1 -1
  15. package/dist/src/connection-manager/index.js +15 -3
  16. package/dist/src/connection-manager/index.js.map +1 -1
  17. package/dist/src/get-peer.d.ts +2 -2
  18. package/dist/src/get-peer.d.ts.map +1 -1
  19. package/dist/src/get-peer.js +2 -5
  20. package/dist/src/get-peer.js.map +1 -1
  21. package/dist/src/index.d.ts +8 -4
  22. package/dist/src/index.d.ts.map +1 -1
  23. package/dist/src/index.js +7 -1
  24. package/dist/src/index.js.map +1 -1
  25. package/dist/src/keychain/index.d.ts +1 -1
  26. package/dist/src/keychain/index.d.ts.map +1 -1
  27. package/dist/src/libp2p.d.ts +3 -3
  28. package/dist/src/libp2p.d.ts.map +1 -1
  29. package/dist/src/libp2p.js.map +1 -1
  30. package/dist/src/registrar.d.ts +5 -0
  31. package/dist/src/registrar.d.ts.map +1 -1
  32. package/dist/src/registrar.js +25 -0
  33. package/dist/src/registrar.js.map +1 -1
  34. package/dist/src/upgrader.d.ts +1 -1
  35. package/dist/src/upgrader.d.ts.map +1 -1
  36. package/dist/src/upgrader.js +42 -23
  37. package/dist/src/upgrader.js.map +1 -1
  38. package/package.json +2 -2
  39. package/src/address-manager/index.ts +3 -2
  40. package/src/connection/index.ts +171 -0
  41. package/src/connection-manager/dialer/index.ts +96 -64
  42. package/src/connection-manager/index.ts +15 -3
  43. package/src/get-peer.ts +3 -7
  44. package/src/index.ts +13 -5
  45. package/src/keychain/index.ts +1 -1
  46. package/src/libp2p.ts +3 -3
  47. package/src/registrar.ts +29 -2
  48. package/src/upgrader.ts +44 -22
  49. package/src/version.ts +1 -1
package/src/libp2p.ts CHANGED
@@ -373,7 +373,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
373
373
  return this.components.addressManager.getAddresses()
374
374
  }
375
375
 
376
- async hangUp (peer: PeerId | Multiaddr | string): Promise<void> {
376
+ async hangUp (peer: PeerId | Multiaddr): Promise<void> {
377
377
  const { id } = getPeer(peer)
378
378
 
379
379
  await this.components.connectionManager.closeConnections(id)
@@ -418,7 +418,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
418
418
  throw errCode(new Error(`Node not responding with its public key: ${peer.toString()}`), codes.ERR_INVALID_RECORD)
419
419
  }
420
420
 
421
- async fetch (peer: PeerId | Multiaddr | string, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
421
+ async fetch (peer: PeerId | Multiaddr, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
422
422
  const { id, multiaddrs } = getPeer(peer)
423
423
 
424
424
  if (multiaddrs != null) {
@@ -428,7 +428,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
428
428
  return await this.fetchService.fetch(id, key, options)
429
429
  }
430
430
 
431
- async ping (peer: PeerId | Multiaddr | string, options: AbortOptions = {}): Promise<number> {
431
+ async ping (peer: PeerId | Multiaddr, options: AbortOptions = {}): Promise<number> {
432
432
  const { id, multiaddrs } = getPeer(peer)
433
433
 
434
434
  if (multiaddrs.length > 0) {
package/src/registrar.ts CHANGED
@@ -35,8 +35,10 @@ export class DefaultRegistrar implements Registrar {
35
35
 
36
36
  this._onDisconnect = this._onDisconnect.bind(this)
37
37
  this._onProtocolChange = this._onProtocolChange.bind(this)
38
+ this._onConnect = this._onConnect.bind(this)
38
39
 
39
40
  this.components.connectionManager.addEventListener('peer:disconnect', this._onDisconnect)
41
+ this.components.connectionManager.addEventListener('peer:connect', this._onConnect)
40
42
 
41
43
  // happens after identify
42
44
  this.components.peerStore.addEventListener('change:protocols', this._onProtocolChange)
@@ -176,12 +178,38 @@ export class DefaultRegistrar implements Registrar {
176
178
  })
177
179
  }
178
180
 
181
+ /**
182
+ * On peer connected if we already have their protocols. Usually used for reconnects
183
+ * as change:protocols event won't be emitted due to identical protocols.
184
+ */
185
+ _onConnect (evt: CustomEvent<Connection>) {
186
+ const connection = evt.detail
187
+
188
+ void this.components.peerStore.protoBook.get(connection.remotePeer)
189
+ .then(peerProtocols => {
190
+ for (const protocol of peerProtocols) {
191
+ const topologies = this.topologies.get(protocol)
192
+
193
+ if (topologies == null) {
194
+ // no topologies are interested in this protocol
195
+ continue
196
+ }
197
+
198
+ for (const topology of topologies.values()) {
199
+ topology.onConnect(connection.remotePeer, connection)
200
+ }
201
+ }
202
+ })
203
+ .catch(err => {
204
+ log.error(err)
205
+ })
206
+ }
207
+
179
208
  /**
180
209
  * Check if a new peer support the multicodecs for this topology
181
210
  */
182
211
  _onProtocolChange (evt: CustomEvent<PeerProtocolsChangeData>) {
183
212
  const { peerId, protocols, oldProtocols } = evt.detail
184
-
185
213
  const removed = oldProtocols.filter(protocol => !protocols.includes(protocol))
186
214
  const added = protocols.filter(protocol => !oldProtocols.includes(protocol))
187
215
 
@@ -212,7 +240,6 @@ export class DefaultRegistrar implements Registrar {
212
240
  if (connection == null) {
213
241
  continue
214
242
  }
215
-
216
243
  topology.onConnect(peerId, connection)
217
244
  }
218
245
  }
package/src/upgrader.ts CHANGED
@@ -1,9 +1,8 @@
1
1
  import { logger } from '@libp2p/logger'
2
2
  import errCode from 'err-code'
3
3
  import * as mss from '@libp2p/multistream-select'
4
- import { pipe } from 'it-pipe'
5
4
  import { codes } from './errors.js'
6
- import { createConnection } from '@libp2p/connection'
5
+ import { createConnection } from './connection/index.js'
7
6
  import { CustomEvent, EventEmitter } from '@libp2p/interfaces/events'
8
7
  import { peerIdFromString } from '@libp2p/peer-id'
9
8
  import type { MultiaddrConnection, Connection, Stream, ConnectionGater, ConnectionProtector } from '@libp2p/interface-connection'
@@ -132,7 +131,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
132
131
  /**
133
132
  * Upgrades an inbound connection
134
133
  */
135
- async upgradeInbound (maConn: MultiaddrConnection): Promise<Connection> {
134
+ async upgradeInbound (maConn: MultiaddrConnection, opts?: UpgraderOptions): Promise<Connection> {
136
135
  const accept = await this.components.connectionManager.acceptIncomingConnection(maConn)
137
136
 
138
137
  if (!accept) {
@@ -167,38 +166,56 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
167
166
 
168
167
  // Protect
169
168
  let protectedConn = maConn
170
- const protector = this.components.connectionProtector
171
169
 
172
- if (protector != null) {
173
- log('protecting the inbound connection')
174
- protectedConn = await protector.protect(maConn)
170
+ if (opts?.skipProtection !== true) {
171
+ const protector = this.components.connectionProtector
172
+
173
+ if (protector != null) {
174
+ log('protecting the inbound connection')
175
+ protectedConn = await protector.protect(maConn)
176
+ }
175
177
  }
176
178
 
177
179
  try {
178
180
  // Encrypt the connection
179
- ({
180
- conn: encryptedConn,
181
- remotePeer,
182
- protocol: cryptoProtocol
183
- } = await this._encryptInbound(protectedConn))
181
+ encryptedConn = protectedConn
182
+ if (opts?.skipEncryption !== true) {
183
+ ({
184
+ conn: encryptedConn,
185
+ remotePeer,
186
+ protocol: cryptoProtocol
187
+ } = await this._encryptInbound(protectedConn))
188
+
189
+ if (await this.components.connectionGater.denyInboundEncryptedConnection(remotePeer, {
190
+ ...protectedConn,
191
+ ...encryptedConn
192
+ })) {
193
+ throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
194
+ }
195
+ } else {
196
+ const idStr = maConn.remoteAddr.getPeerId()
184
197
 
185
- if (await this.components.connectionGater.denyInboundEncryptedConnection(remotePeer, {
186
- ...protectedConn,
187
- ...encryptedConn
188
- })) {
189
- throw errCode(new Error('The multiaddr connection is blocked by gater.acceptEncryptedConnection'), codes.ERR_CONNECTION_INTERCEPTED)
198
+ if (idStr == null) {
199
+ throw errCode(new Error('inbound connection that skipped encryption must have a peer id'), codes.ERR_INVALID_MULTIADDR)
200
+ }
201
+
202
+ const remotePeerId = peerIdFromString(idStr)
203
+
204
+ cryptoProtocol = 'native'
205
+ remotePeer = remotePeerId
190
206
  }
191
207
 
192
- // Multiplex the connection
193
- if (this.muxers.size > 0) {
208
+ upgradedConn = encryptedConn
209
+ if (opts?.muxerFactory != null) {
210
+ muxerFactory = opts.muxerFactory
211
+ } else if (this.muxers.size > 0) {
212
+ // Multiplex the connection
194
213
  const multiplexed = await this._multiplexInbound({
195
214
  ...protectedConn,
196
215
  ...encryptedConn
197
216
  }, this.muxers)
198
217
  muxerFactory = multiplexed.muxerFactory
199
218
  upgradedConn = multiplexed.stream
200
- } else {
201
- upgradedConn = encryptedConn
202
219
  }
203
220
  } catch (err: any) {
204
221
  log.error('Failed to upgrade inbound connection', err)
@@ -466,7 +483,12 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
466
483
  }
467
484
 
468
485
  // Pipe all data through the muxer
469
- pipe(upgradedConn, muxer, upgradedConn).catch(log.error)
486
+ void Promise.all([
487
+ muxer.sink(upgradedConn.source),
488
+ upgradedConn.sink(muxer.source)
489
+ ]).catch(err => {
490
+ log.error(err)
491
+ })
470
492
  }
471
493
 
472
494
  const _timeline = maConn.timeline
package/src/version.ts CHANGED
@@ -1,2 +1,2 @@
1
- export const version = '0.40.0-fed012d'
1
+ export const version = '0.41.0'
2
2
  export const name = 'libp2p'