libp2p 0.37.0-b09eb8f → 0.37.1

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.
package/src/index.ts CHANGED
@@ -4,7 +4,7 @@ import type { EventEmitter } from '@libp2p/interfaces/events'
4
4
  import type { Startable } from '@libp2p/interfaces/startable'
5
5
  import type { Multiaddr } from '@multiformats/multiaddr'
6
6
  import type { FaultTolerance } from './transport-manager.js'
7
- import type { HostProperties } from './identify/index.js'
7
+ import type { IdentifyServiceInit } from './identify/index.js'
8
8
  import type { DualDHT } from '@libp2p/interfaces/dht'
9
9
  import type { Datastore } from 'interface-datastore'
10
10
  import type { PeerStore, PeerStoreInit } from '@libp2p/interfaces/peer-store'
@@ -24,6 +24,8 @@ import type { Metrics, MetricsInit } from '@libp2p/interfaces/metrics'
24
24
  import type { PeerInfo } from '@libp2p/interfaces/peer-info'
25
25
  import type { KeyChain } from './keychain/index.js'
26
26
  import type { ConnectionManagerInit } from './connection-manager/index.js'
27
+ import type { PingServiceInit } from './ping/index.js'
28
+ import type { FetchServiceInit } from './fetch/index.js'
27
29
 
28
30
  export interface PersistentPeerStoreOptions {
29
31
  threshold?: number
@@ -95,7 +97,6 @@ export interface RefreshManagerConfig {
95
97
 
96
98
  export interface Libp2pInit {
97
99
  peerId: PeerId
98
- host: HostProperties
99
100
  addresses: AddressesConfig
100
101
  connectionManager: ConnectionManagerInit
101
102
  connectionGater: Partial<ConnectionGater>
@@ -105,9 +106,11 @@ export interface Libp2pInit {
105
106
  peerStore: PeerStoreInit
106
107
  peerRouting: PeerRoutingConfig
107
108
  keychain: KeychainConfig
108
- protocolPrefix: string
109
109
  nat: NatManagerConfig
110
110
  relay: RelayConfig
111
+ identify: IdentifyServiceInit
112
+ ping: PingServiceInit
113
+ fetch: FetchServiceInit
111
114
 
112
115
  transports: Transport[]
113
116
  streamMuxers?: StreamMuxerFactory[]
@@ -195,12 +198,12 @@ export interface Libp2p extends Startable, EventEmitter<Libp2pEvents> {
195
198
  /**
196
199
  * Pings the given peer in order to obtain the operation latency
197
200
  */
198
- ping: (peer: Multiaddr |PeerId) => Promise<number>
201
+ ping: (peer: Multiaddr | PeerId, options?: AbortOptions) => Promise<number>
199
202
 
200
203
  /**
201
204
  * Sends a request to fetch the value associated with the given key from the given peer.
202
205
  */
203
- fetch: (peer: PeerId | Multiaddr | string, key: string) => Promise<Uint8Array | null>
206
+ fetch: (peer: PeerId | Multiaddr | string, key: string, options?: AbortOptions) => Promise<Uint8Array | null>
204
207
 
205
208
  /**
206
209
  * Returns the public key for the passed PeerId. If the PeerId is of the 'RSA' type
package/src/libp2p.ts CHANGED
@@ -166,10 +166,7 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
166
166
  if (init.streamMuxers != null && init.streamMuxers.length > 0) {
167
167
  // Add the identify service since we can multiplex
168
168
  this.identifyService = new IdentifyService(this.components, {
169
- protocolPrefix: init.protocolPrefix,
170
- host: {
171
- agentVersion: init.host.agentVersion
172
- }
169
+ ...init.identify
173
170
  })
174
171
  this.configureComponent(this.identifyService)
175
172
  }
@@ -229,11 +226,11 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
229
226
  }
230
227
 
231
228
  this.fetchService = this.configureComponent(new FetchService(this.components, {
232
- protocolPrefix: init.protocolPrefix
229
+ ...init.fetch
233
230
  }))
234
231
 
235
232
  this.pingService = this.configureComponent(new PingService(this.components, {
236
- protocolPrefix: init.protocolPrefix
233
+ ...init.ping
237
234
  }))
238
235
 
239
236
  const autoDialer = this.configureComponent(new AutoDialer(this.components, {
@@ -419,9 +416,9 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
419
416
  throw errCode(new Error('no protocols were provided to open a stream'), codes.ERR_INVALID_PROTOCOLS_FOR_STREAM)
420
417
  }
421
418
 
422
- const connection = await this.dial(peer)
419
+ const connection = await this.dial(peer, options)
423
420
 
424
- return await connection.newStream(protocols)
421
+ return await connection.newStream(protocols, options)
425
422
  }
426
423
 
427
424
  getMultiaddrs (): Multiaddr[] {
@@ -473,24 +470,24 @@ export class Libp2pNode extends EventEmitter<Libp2pEvents> implements Libp2p {
473
470
  throw errCode(new Error(`Node not responding with its public key: ${peer.toString()}`), codes.ERR_INVALID_RECORD)
474
471
  }
475
472
 
476
- async fetch (peer: PeerId | Multiaddr | string, key: string): Promise<Uint8Array | null> {
473
+ async fetch (peer: PeerId | Multiaddr | string, key: string, options: AbortOptions = {}): Promise<Uint8Array | null> {
477
474
  const { id, multiaddrs } = getPeer(peer)
478
475
 
479
476
  if (multiaddrs != null) {
480
477
  await this.components.getPeerStore().addressBook.add(id, multiaddrs)
481
478
  }
482
479
 
483
- return await this.fetchService.fetch(id, key)
480
+ return await this.fetchService.fetch(id, key, options)
484
481
  }
485
482
 
486
- async ping (peer: PeerId | Multiaddr | string): Promise<number> {
483
+ async ping (peer: PeerId | Multiaddr | string, options: AbortOptions = {}): Promise<number> {
487
484
  const { id, multiaddrs } = getPeer(peer)
488
485
 
489
486
  if (multiaddrs.length > 0) {
490
487
  await this.components.getPeerStore().addressBook.add(id, multiaddrs)
491
488
  }
492
489
 
493
- return await this.pingService.ping(id)
490
+ return await this.pingService.ping(id, options)
494
491
  }
495
492
 
496
493
  async handle (protocols: string | string[], handler: StreamHandler): Promise<void> {
package/src/ping/index.ts CHANGED
@@ -10,6 +10,9 @@ import type { IncomingStreamData } from '@libp2p/interfaces/registrar'
10
10
  import type { PeerId } from '@libp2p/interfaces/peer-id'
11
11
  import type { Startable } from '@libp2p/interfaces/startable'
12
12
  import type { Components } from '@libp2p/interfaces/components'
13
+ import type { AbortOptions } from '@libp2p/interfaces'
14
+ import type { Duplex } from 'it-stream-types'
15
+ import { abortableDuplex } from 'abortable-iterator'
13
16
 
14
17
  const log = logger('libp2p:ping')
15
18
 
@@ -18,8 +21,8 @@ export interface PingServiceInit {
18
21
  }
19
22
 
20
23
  export class PingService implements Startable {
24
+ public readonly protocol: string
21
25
  private readonly components: Components
22
- private readonly protocol: string
23
26
  private started: boolean
24
27
 
25
28
  constructor (components: Components, init: PingServiceInit) {
@@ -60,25 +63,36 @@ export class PingService implements Startable {
60
63
  * @param {PeerId|Multiaddr} peer
61
64
  * @returns {Promise<number>}
62
65
  */
63
- async ping (peer: PeerId): Promise<number> {
66
+ async ping (peer: PeerId, options: AbortOptions = {}): Promise<number> {
64
67
  log('dialing %s to %p', this.protocol, peer)
65
68
 
66
- const connection = await this.components.getConnectionManager().openConnection(peer)
67
- const { stream } = await connection.newStream([this.protocol])
69
+ const connection = await this.components.getConnectionManager().openConnection(peer, options)
70
+ const { stream } = await connection.newStream([this.protocol], options)
68
71
  const start = Date.now()
69
72
  const data = randomBytes(PING_LENGTH)
70
73
 
71
- const result = await pipe(
72
- [data],
73
- stream,
74
- async (source) => await first(source)
75
- )
76
- const end = Date.now()
74
+ let source: Duplex<Uint8Array> = stream
77
75
 
78
- if (result == null || !uint8ArrayEquals(data, result)) {
79
- throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
76
+ // make stream abortable if AbortSignal passed
77
+ if (options.signal != null) {
78
+ source = abortableDuplex(stream, options.signal)
80
79
  }
81
80
 
82
- return end - start
81
+ try {
82
+ const result = await pipe(
83
+ [data],
84
+ source,
85
+ async (source) => await first(source)
86
+ )
87
+ const end = Date.now()
88
+
89
+ if (result == null || !uint8ArrayEquals(data, result)) {
90
+ throw errCode(new Error('Received wrong ping ack'), codes.ERR_WRONG_PING_ACK)
91
+ }
92
+
93
+ return end - start
94
+ } finally {
95
+ stream.close()
96
+ }
83
97
  }
84
98
  }
package/src/upgrader.ts CHANGED
@@ -15,6 +15,7 @@ import type { PeerId } from '@libp2p/interfaces/peer-id'
15
15
  import type { MultiaddrConnection, Upgrader, UpgraderEvents } from '@libp2p/interfaces/transport'
16
16
  import type { Duplex } from 'it-stream-types'
17
17
  import type { Components } from '@libp2p/interfaces/components'
18
+ import type { AbortOptions } from '@libp2p/interfaces'
18
19
 
19
20
  const log = logger('libp2p:upgrader')
20
21
 
@@ -266,7 +267,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
266
267
  } = opts
267
268
 
268
269
  let muxer: StreamMuxer | undefined
269
- let newStream: ((multicodecs: string[]) => Promise<ProtocolStream>) | undefined
270
+ let newStream: ((multicodecs: string[], options?: AbortOptions) => Promise<ProtocolStream>) | undefined
270
271
  let connection: Connection // eslint-disable-line prefer-const
271
272
 
272
273
  if (muxerFactory != null) {
@@ -308,7 +309,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
308
309
  }
309
310
  })
310
311
 
311
- newStream = async (protocols: string[]): Promise<ProtocolStream> => {
312
+ newStream = async (protocols: string[], options: AbortOptions = {}): Promise<ProtocolStream> => {
312
313
  if (muxer == null) {
313
314
  throw errCode(new Error('Stream is not multiplexed'), codes.ERR_MUXER_UNAVAILABLE)
314
315
  }
@@ -319,7 +320,7 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
319
320
  const metrics = this.components.getMetrics()
320
321
 
321
322
  try {
322
- let { stream, protocol } = await mss.select(protocols)
323
+ let { stream, protocol } = await mss.select(protocols, options)
323
324
 
324
325
  if (metrics != null) {
325
326
  stream = metrics.trackStream({ stream, remotePeer, protocol })
@@ -328,6 +329,11 @@ export class DefaultUpgrader extends EventEmitter<UpgraderEvents> implements Upg
328
329
  return { stream: { ...muxedStream, ...stream }, protocol }
329
330
  } catch (err: any) {
330
331
  log.error('could not create new stream', err)
332
+
333
+ if (err.code != null) {
334
+ throw err
335
+ }
336
+
331
337
  throw errCode(err, codes.ERR_UNSUPPORTED_PROTOCOL)
332
338
  }
333
339
  }