@helia/bitswap 2.0.5-daaa511 → 2.1.0-d43efc7

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/network.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { InvalidParametersError, NotStartedError, TimeoutError, TypedEventEmitter, UnsupportedProtocolError, setMaxListeners } from '@libp2p/interface'
2
- import { PeerQueue, type PeerQueueJobOptions } from '@libp2p/utils/peer-queue'
2
+ import { PeerQueue } from '@libp2p/utils/peer-queue'
3
3
  import drain from 'it-drain'
4
4
  import * as lp from 'it-length-prefixed'
5
5
  import map from 'it-map'
@@ -18,11 +18,13 @@ import type { QueuedBitswapMessage } from './utils/bitswap-message.js'
18
18
  import type { Provider, Routing } from '@helia/interface/routing'
19
19
  import type { Libp2p, AbortOptions, Connection, PeerId, IncomingStreamData, Topology, ComponentLogger, IdentifyResult, Counter, Metrics } from '@libp2p/interface'
20
20
  import type { Logger } from '@libp2p/logger'
21
+ import type { PeerQueueJobOptions } from '@libp2p/utils/peer-queue'
22
+ import type { Multiaddr } from '@multiformats/multiaddr'
21
23
  import type { CID } from 'multiformats/cid'
22
24
  import type { ProgressEvent, ProgressOptions } from 'progress-events'
23
25
 
24
26
  export type BitswapNetworkProgressEvents =
25
- ProgressEvent<'bitswap:network:dial', PeerId>
27
+ ProgressEvent<'bitswap:network:dial', PeerId | Multiaddr | Multiaddr[]>
26
28
 
27
29
  export type BitswapNetworkWantProgressEvents =
28
30
  ProgressEvent<'bitswap:network:send-wantlist', PeerId> |
@@ -262,6 +264,17 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
262
264
  * Find the providers of a given `cid` and connect to them.
263
265
  */
264
266
  async findAndConnect (cid: CID, options?: WantOptions): Promise<void> {
267
+ // connect to initial session providers if supplied
268
+ if (options?.providers != null) {
269
+ await Promise.all(
270
+ options.providers.map(async prov => this.connectTo(prov)
271
+ .catch(err => {
272
+ this.log.error('could not connect to supplied provider - %e', err)
273
+ }))
274
+ )
275
+ }
276
+
277
+ // make a routing query to find additional providers
265
278
  await drain(
266
279
  map(
267
280
  take(this.findProviders(cid, options), options?.maxProviders ?? DEFAULT_MAX_PROVIDERS_PER_REQUEST),
@@ -335,12 +348,12 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
335
348
  /**
336
349
  * Connects to another peer
337
350
  */
338
- async connectTo (peer: PeerId, options?: AbortOptions & ProgressOptions<BitswapNetworkProgressEvents>): Promise<Connection> { // eslint-disable-line require-await
351
+ async connectTo (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions & ProgressOptions<BitswapNetworkProgressEvents>): Promise<Connection> { // eslint-disable-line require-await
339
352
  if (!this.running) {
340
353
  throw new NotStartedError('Network isn\'t running')
341
354
  }
342
355
 
343
- options?.onProgress?.(new CustomProgressEvent<PeerId>('bitswap:network:dial', peer))
356
+ options?.onProgress?.(new CustomProgressEvent<PeerId | Multiaddr | Multiaddr[]>('bitswap:network:dial', peer))
344
357
 
345
358
  // dial and wait for identify - this is to avoid opening a protocol stream
346
359
  // that we are not going to use but depends on the remote node running the
package/src/pb/message.ts CHANGED
@@ -4,8 +4,9 @@
4
4
  /* eslint-disable @typescript-eslint/no-unnecessary-boolean-literal-compare */
5
5
  /* eslint-disable @typescript-eslint/no-empty-interface */
6
6
 
7
- import { type Codec, decodeMessage, type DecodeOptions, encodeMessage, enumeration, MaxLengthError, message } from 'protons-runtime'
7
+ import { decodeMessage, encodeMessage, enumeration, MaxLengthError, message } from 'protons-runtime'
8
8
  import { alloc as uint8ArrayAlloc } from 'uint8arrays/alloc'
9
+ import type { Codec, DecodeOptions } from 'protons-runtime'
9
10
  import type { Uint8ArrayList } from 'uint8arraylist'
10
11
 
11
12
  export enum WantType {
@@ -6,10 +6,9 @@ import { Ledger } from './ledger.js'
6
6
  import type { BitswapNotifyProgressEvents, WantListEntry } from '../index.js'
7
7
  import type { Network } from '../network.js'
8
8
  import type { BitswapMessage } from '../pb/message.js'
9
- import type { ComponentLogger, Libp2p, Logger, Metrics, PeerId } from '@libp2p/interface'
9
+ import type { AbortOptions, ComponentLogger, Libp2p, Logger, Metrics, PeerId } from '@libp2p/interface'
10
10
  import type { PeerMap } from '@libp2p/peer-collections'
11
11
  import type { Blockstore } from 'interface-blockstore'
12
- import type { AbortOptions } from 'it-length-prefixed-stream'
13
12
  import type { ProgressOptions } from 'progress-events'
14
13
 
15
14
  export interface PeerWantListsInit {
@@ -4,9 +4,8 @@ import { BlockPresenceType, WantType } from '../pb/message.js'
4
4
  import { QueuedBitswapMessage } from '../utils/bitswap-message.js'
5
5
  import { cidToPrefix } from '../utils/cid-prefix.js'
6
6
  import type { Network } from '../network.js'
7
- import type { ComponentLogger, Logger, PeerId } from '@libp2p/interface'
7
+ import type { AbortOptions, ComponentLogger, Logger, PeerId } from '@libp2p/interface'
8
8
  import type { Blockstore } from 'interface-blockstore'
9
- import type { AbortOptions } from 'it-length-prefixed-stream'
10
9
  import type { CID } from 'multiformats/cid'
11
10
 
12
11
  export interface LedgerComponents {
package/src/session.ts CHANGED
@@ -1,9 +1,11 @@
1
1
  import { AbstractSession } from '@helia/utils'
2
+ import { isPeerId } from '@libp2p/interface'
2
3
  import type { BitswapWantProgressEvents } from './index.js'
3
4
  import type { Network } from './network.js'
4
5
  import type { WantList } from './want-list.js'
5
6
  import type { CreateSessionOptions } from '@helia/interface'
6
- import type { ComponentLogger, PeerId } from '@libp2p/interface'
7
+ import type { ComponentLogger, Libp2p, PeerId } from '@libp2p/interface'
8
+ import type { Multiaddr } from '@multiformats/multiaddr'
7
9
  import type { AbortOptions } from 'interface-store'
8
10
  import type { CID } from 'multiformats/cid'
9
11
 
@@ -11,11 +13,13 @@ export interface BitswapSessionComponents {
11
13
  network: Network
12
14
  wantList: WantList
13
15
  logger: ComponentLogger
16
+ libp2p: Libp2p
14
17
  }
15
18
 
16
19
  class BitswapSession extends AbstractSession<PeerId, BitswapWantProgressEvents> {
17
20
  private readonly wantList: WantList
18
21
  private readonly network: Network
22
+ private readonly libp2p: Libp2p
19
23
 
20
24
  constructor (components: BitswapSessionComponents, init: CreateSessionOptions) {
21
25
  super(components, {
@@ -25,6 +29,7 @@ class BitswapSession extends AbstractSession<PeerId, BitswapWantProgressEvents>
25
29
 
26
30
  this.wantList = components.wantList
27
31
  this.network = components.network
32
+ this.libp2p = components.libp2p
28
33
  }
29
34
 
30
35
  async queryProvider (cid: CID, provider: PeerId, options: AbortOptions): Promise<Uint8Array> {
@@ -54,6 +59,16 @@ class BitswapSession extends AbstractSession<PeerId, BitswapWantProgressEvents>
54
59
  equals (providerA: PeerId, providerB: PeerId): boolean {
55
60
  return providerA.equals(providerB)
56
61
  }
62
+
63
+ async convertToProvider (provider: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions): Promise<PeerId | undefined> {
64
+ if (isPeerId(provider)) {
65
+ return provider
66
+ }
67
+
68
+ const connection = await this.libp2p.dial(provider, options)
69
+
70
+ return connection.remotePeer
71
+ }
57
72
  }
58
73
 
59
74
  export function createBitswapSession (components: BitswapSessionComponents, init: CreateSessionOptions): BitswapSession {