@helia/bitswap 3.2.3-070b7528 → 3.2.3-1361bfa5
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/dist/index.min.js +22 -1
- package/dist/index.min.js.map +4 -4
- package/dist/src/block-broker.d.ts +27 -0
- package/dist/src/block-broker.d.ts.map +1 -0
- package/dist/src/block-broker.js +91 -0
- package/dist/src/block-broker.js.map +1 -0
- package/dist/src/index.d.ts +8 -2
- package/dist/src/index.d.ts.map +1 -1
- package/dist/src/index.js +18 -4
- package/dist/src/index.js.map +1 -1
- package/dist/src/network.d.ts +2 -2
- package/dist/src/network.d.ts.map +1 -1
- package/dist/src/network.js +19 -21
- package/dist/src/network.js.map +1 -1
- package/dist/src/pb/message.d.ts +5 -5
- package/dist/src/pb/message.d.ts.map +1 -1
- package/dist/src/session.d.ts +4 -4
- package/dist/src/session.d.ts.map +1 -1
- package/dist/src/session.js +9 -7
- package/dist/src/session.js.map +1 -1
- package/package.json +25 -24
- package/src/block-broker.ts +119 -0
- package/src/index.ts +25 -5
- package/src/network.ts +27 -29
- package/src/pb/message.ts +5 -5
- package/src/session.ts +13 -11
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@helia/bitswap",
|
|
3
|
-
"version": "3.2.3-
|
|
3
|
+
"version": "3.2.3-1361bfa5",
|
|
4
4
|
"description": "JavaScript implementation of the Bitswap data exchange protocol used by Helia",
|
|
5
5
|
"license": "Apache-2.0 OR MIT",
|
|
6
6
|
"homepage": "https://github.com/ipfs/helia/tree/main/packages/bitswap#readme",
|
|
@@ -52,42 +52,43 @@
|
|
|
52
52
|
"docs": "aegir docs"
|
|
53
53
|
},
|
|
54
54
|
"dependencies": {
|
|
55
|
-
"@helia/interface": "6.2.1-
|
|
56
|
-
"@helia/
|
|
57
|
-
"@
|
|
58
|
-
"@libp2p/
|
|
59
|
-
"@libp2p/
|
|
60
|
-
"@libp2p/
|
|
61
|
-
"@
|
|
55
|
+
"@helia/interface": "6.2.1-1361bfa5",
|
|
56
|
+
"@helia/libp2p": "0.0.0-1361bfa5",
|
|
57
|
+
"@helia/utils": "2.5.2-1361bfa5",
|
|
58
|
+
"@libp2p/interface": "^3.2.3",
|
|
59
|
+
"@libp2p/logger": "^6.2.8",
|
|
60
|
+
"@libp2p/peer-collections": "^7.0.21",
|
|
61
|
+
"@libp2p/peer-id": "^6.0.10",
|
|
62
|
+
"@libp2p/utils": "^7.2.2",
|
|
63
|
+
"@multiformats/multiaddr": "^13.0.3",
|
|
62
64
|
"any-signal": "^4.2.0",
|
|
63
|
-
"interface-blockstore": "^
|
|
65
|
+
"interface-blockstore": "^7.0.1",
|
|
64
66
|
"it-drain": "^3.0.12",
|
|
65
|
-
"it-length-prefixed": "^
|
|
66
|
-
"it-map": "^3.1.
|
|
67
|
-
"it-pushable": "^3.2.
|
|
68
|
-
"it-take": "^3.0.
|
|
69
|
-
"it-to-buffer": "^
|
|
70
|
-
"multiformats": "^
|
|
67
|
+
"it-length-prefixed": "^11.0.1",
|
|
68
|
+
"it-map": "^3.1.6",
|
|
69
|
+
"it-pushable": "^3.2.4",
|
|
70
|
+
"it-take": "^3.0.11",
|
|
71
|
+
"it-to-buffer": "^5.0.0",
|
|
72
|
+
"multiformats": "^14.0.0",
|
|
71
73
|
"p-defer": "^4.0.1",
|
|
72
74
|
"progress-events": "^1.1.0",
|
|
73
|
-
"protons-runtime": "^
|
|
75
|
+
"protons-runtime": "^7.0.0",
|
|
74
76
|
"race-event": "^1.6.1",
|
|
75
|
-
"uint8-varint": "^
|
|
76
|
-
"uint8arraylist": "^
|
|
77
|
-
"uint8arrays": "^
|
|
77
|
+
"uint8-varint": "^3.0.0",
|
|
78
|
+
"uint8arraylist": "^3.0.2",
|
|
79
|
+
"uint8arrays": "^6.1.1"
|
|
78
80
|
},
|
|
79
81
|
"devDependencies": {
|
|
80
|
-
"@libp2p/crypto": "^5.1.
|
|
81
|
-
"@libp2p/peer-id": "^6.0.6",
|
|
82
|
+
"@libp2p/crypto": "^5.1.19",
|
|
82
83
|
"@types/sinon": "^21.0.1",
|
|
83
|
-
"aegir": "^48.0.
|
|
84
|
-
"blockstore-core": "^
|
|
84
|
+
"aegir": "^48.0.11",
|
|
85
|
+
"blockstore-core": "^7.0.1",
|
|
85
86
|
"delay": "^7.0.0",
|
|
86
87
|
"it-all": "^3.0.11",
|
|
87
88
|
"p-event": "^7.1.0",
|
|
88
89
|
"p-retry": "^8.0.0",
|
|
89
90
|
"p-wait-for": "^6.0.0",
|
|
90
|
-
"protons": "^
|
|
91
|
+
"protons": "^9.0.1",
|
|
91
92
|
"sinon": "^22.0.0",
|
|
92
93
|
"sinon-ts": "^2.0.0"
|
|
93
94
|
},
|
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
import { isPeerId } from '@libp2p/interface'
|
|
2
|
+
import { CustomProgressEvent } from 'progress-events'
|
|
3
|
+
import { Bitswap } from './bitswap.ts'
|
|
4
|
+
import type { BitswapOptions, BitswapWantBlockProgressEvents, BitswapNotifyProgressEvents } from './index.ts'
|
|
5
|
+
import type { BlockAnnounceOptions, BlockBroker, BlockRetrievalOptions, CreateSessionOptions, Routing, HasherLoader, SessionBlockBroker, BlockBrokerConnectProgressEvent, BlockBrokerConnectedProgressEvent, BlockBrokerRequestBlockProgressEvent, BlockBrokerReceiveBlockProgressEvent } from '@helia/interface'
|
|
6
|
+
import type { Libp2p, Startable, ComponentLogger } from '@libp2p/interface'
|
|
7
|
+
import type { Blockstore } from 'interface-blockstore'
|
|
8
|
+
import type { CID } from 'multiformats/cid'
|
|
9
|
+
|
|
10
|
+
export interface BitswapBlockBrokerComponents {
|
|
11
|
+
libp2p: Libp2p
|
|
12
|
+
blockstore: Blockstore
|
|
13
|
+
routing: Routing
|
|
14
|
+
logger: ComponentLogger
|
|
15
|
+
getHasher: HasherLoader
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export interface BitswapBlockBrokerInit extends BitswapOptions {
|
|
19
|
+
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export class BitswapBlockBroker implements BlockBroker<BitswapWantBlockProgressEvents, BitswapNotifyProgressEvents>, Startable {
|
|
23
|
+
public readonly name = 'bitswap'
|
|
24
|
+
private readonly bitswap: Bitswap
|
|
25
|
+
private started: boolean
|
|
26
|
+
|
|
27
|
+
constructor (components: BitswapBlockBrokerComponents, init: BitswapBlockBrokerInit = {}) {
|
|
28
|
+
this.bitswap = new Bitswap(components, init)
|
|
29
|
+
this.started = false
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
isStarted (): boolean {
|
|
33
|
+
return this.started
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async start (): Promise<void> {
|
|
37
|
+
await this.bitswap.start()
|
|
38
|
+
this.started = true
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
async stop (): Promise<void> {
|
|
42
|
+
await this.bitswap.stop()
|
|
43
|
+
this.started = false
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async announce (cid: CID, options?: BlockAnnounceOptions<BitswapNotifyProgressEvents>): Promise<void> {
|
|
47
|
+
await this.bitswap.notify(cid, options)
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async retrieve (cid: CID, options: BlockRetrievalOptions<BitswapWantBlockProgressEvents> = {}): Promise<Uint8Array> {
|
|
51
|
+
return this.bitswap.want(cid, {
|
|
52
|
+
...options,
|
|
53
|
+
onProgress: function bitswapBlockBrokerRetrieveCallback (evt) {
|
|
54
|
+
if (options?.onProgress == null) {
|
|
55
|
+
return
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
options.onProgress(evt)
|
|
59
|
+
|
|
60
|
+
if (evt.type === 'connection:open') {
|
|
61
|
+
if (!isPeerId(evt.detail)) {
|
|
62
|
+
// should not happen as bitswap impl only sends wantlist to
|
|
63
|
+
// connected peers so we always have a peer id
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
options.onProgress(new CustomProgressEvent<BlockBrokerConnectProgressEvent>('helia:block-broker:connect', {
|
|
68
|
+
broker: 'bitswap',
|
|
69
|
+
type: 'connect',
|
|
70
|
+
provider: evt.detail.toCID(),
|
|
71
|
+
cid
|
|
72
|
+
}))
|
|
73
|
+
} else if (evt.type === 'connection:opened') {
|
|
74
|
+
options.onProgress(new CustomProgressEvent<BlockBrokerConnectedProgressEvent>('helia:block-broker:connected', {
|
|
75
|
+
broker: 'bitswap',
|
|
76
|
+
type: 'connected',
|
|
77
|
+
provider: evt.detail.remotePeer.toCID(),
|
|
78
|
+
address: evt.detail.remoteAddr,
|
|
79
|
+
cid
|
|
80
|
+
}))
|
|
81
|
+
} else if (evt.type === 'bitswap:send-wantlist') {
|
|
82
|
+
options.onProgress(new CustomProgressEvent<BlockBrokerRequestBlockProgressEvent>('helia:block-broker:request-block', {
|
|
83
|
+
broker: 'bitswap',
|
|
84
|
+
type: 'request-block',
|
|
85
|
+
provider: evt.detail.toCID(),
|
|
86
|
+
cid
|
|
87
|
+
}))
|
|
88
|
+
} else if (evt.type === 'bitswap:block') {
|
|
89
|
+
options.onProgress(new CustomProgressEvent<BlockBrokerReceiveBlockProgressEvent>('helia:block-broker:receive-block', {
|
|
90
|
+
broker: 'bitswap',
|
|
91
|
+
type: 'receive-block',
|
|
92
|
+
provider: evt.detail.sender.toCID(),
|
|
93
|
+
cid
|
|
94
|
+
}))
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
createSession (options?: CreateSessionOptions<BitswapWantBlockProgressEvents>): SessionBlockBroker<BitswapWantBlockProgressEvents, BitswapNotifyProgressEvents> {
|
|
101
|
+
const session = this.bitswap.createSession(options)
|
|
102
|
+
|
|
103
|
+
return {
|
|
104
|
+
name: 'bitswap-session',
|
|
105
|
+
|
|
106
|
+
addPeer: async (peer, options) => {
|
|
107
|
+
await session.addPeer(peer, options)
|
|
108
|
+
},
|
|
109
|
+
|
|
110
|
+
announce: async (cid, options) => {
|
|
111
|
+
await this.bitswap.notify(cid, options)
|
|
112
|
+
},
|
|
113
|
+
|
|
114
|
+
retrieve: async (cid, options) => {
|
|
115
|
+
return session.retrieve(cid, options)
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -6,11 +6,14 @@
|
|
|
6
6
|
* It supersedes the older [ipfs-bitswap](https://www.npmjs.com/package/ipfs-bitswap) module with the aim of being smaller, faster, better integrated with libp2p/helia, having fewer dependencies and using standard JavaScript instead of Node.js APIs.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import {
|
|
9
|
+
import { start } from '@libp2p/interface'
|
|
10
|
+
import { BitswapBlockBroker } from './block-broker.ts'
|
|
11
|
+
import type { BitswapBlockBrokerInit } from './block-broker.ts'
|
|
10
12
|
import type { BitswapNetworkNotifyProgressEvents, BitswapNetworkWantProgressEvents, BitswapNetworkProgressEvents } from './network.ts'
|
|
11
13
|
import type { WantType } from './pb/message.ts'
|
|
12
|
-
import type { BlockBrokerGetBlockProgressEvents, CreateSessionOptions, HasherLoader, ProviderOptions, SessionBlockBroker } from '@helia/interface'
|
|
13
|
-
import type { Routing } from '@helia/interface
|
|
14
|
+
import type { BlockBrokerGetBlockProgressEvents, CreateSessionOptions, HasherLoader, HeliaMixin, ProviderOptions, SessionBlockBroker } from '@helia/interface'
|
|
15
|
+
import type { Routing } from '@helia/interface'
|
|
16
|
+
import type { HeliaWithLibp2p } from '@helia/libp2p'
|
|
14
17
|
import type { Libp2p, AbortOptions, Startable, ComponentLogger, Metrics, PeerId } from '@libp2p/interface'
|
|
15
18
|
import type { Blockstore } from 'interface-blockstore'
|
|
16
19
|
import type { CID } from 'multiformats/cid'
|
|
@@ -34,6 +37,7 @@ export type { BitswapNetworkWantProgressEvents }
|
|
|
34
37
|
export type { BitswapNetworkProgressEvents }
|
|
35
38
|
export type { WantType }
|
|
36
39
|
export type { BitswapProvider } from './network.ts'
|
|
40
|
+
export type { BitswapBlockBrokerInit } from './block-broker.ts'
|
|
37
41
|
|
|
38
42
|
export type WantStatus = 'want' | 'sending' | 'sent'
|
|
39
43
|
|
|
@@ -227,6 +231,22 @@ export interface BitswapOptions {
|
|
|
227
231
|
maxWantlistSize?: number
|
|
228
232
|
}
|
|
229
233
|
|
|
230
|
-
|
|
231
|
-
|
|
234
|
+
/**
|
|
235
|
+
* Return a Helia node augmented with a libp2p instance
|
|
236
|
+
*/
|
|
237
|
+
export function withBitswap (helia: HeliaWithLibp2p, options: BitswapBlockBrokerInit = {}): HeliaWithLibp2p {
|
|
238
|
+
const mixin: HeliaMixin<HeliaWithLibp2p> = {
|
|
239
|
+
start: async (helia) => {
|
|
240
|
+
if (helia.status === 'starting' && !helia.hasBlockBroker('bitswap')) {
|
|
241
|
+
const broker = new BitswapBlockBroker(helia, options)
|
|
242
|
+
await start(broker)
|
|
243
|
+
|
|
244
|
+
helia.addBlockBroker(broker)
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
helia.addMixin(mixin)
|
|
250
|
+
|
|
251
|
+
return helia
|
|
232
252
|
}
|
package/src/network.ts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { isCID } from '@helia/utils'
|
|
2
|
+
import { InvalidParametersError, NotStartedError, TimeoutError, TypedEventEmitter, isPeerId, setMaxListeners } from '@libp2p/interface'
|
|
3
|
+
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
2
4
|
import { PeerQueue } from '@libp2p/utils'
|
|
5
|
+
import { isMultiaddr } from '@multiformats/multiaddr'
|
|
3
6
|
import drain from 'it-drain'
|
|
4
7
|
import * as lp from 'it-length-prefixed'
|
|
5
8
|
import map from 'it-map'
|
|
@@ -7,7 +10,6 @@ import { pushable } from 'it-pushable'
|
|
|
7
10
|
import take from 'it-take'
|
|
8
11
|
import { CID } from 'multiformats/cid'
|
|
9
12
|
import { CustomProgressEvent } from 'progress-events'
|
|
10
|
-
import { raceEvent } from 'race-event'
|
|
11
13
|
import { BITSWAP_120, DEFAULT_MAX_INBOUND_STREAMS, DEFAULT_MAX_INCOMING_MESSAGE_SIZE, DEFAULT_MAX_OUTBOUND_STREAMS, DEFAULT_MAX_OUTGOING_MESSAGE_SIZE, DEFAULT_MAX_PROVIDERS_PER_REQUEST, DEFAULT_MESSAGE_RECEIVE_TIMEOUT, DEFAULT_MESSAGE_SEND_CONCURRENCY, DEFAULT_MESSAGE_SEND_TIMEOUT, DEFAULT_RUN_ON_TRANSIENT_CONNECTIONS } from './constants.ts'
|
|
12
14
|
import { BitswapMessage } from './pb/message.ts'
|
|
13
15
|
import { mergeMessages } from './utils/merge-messages.ts'
|
|
@@ -16,8 +18,8 @@ import type { WantOptions } from './bitswap.ts'
|
|
|
16
18
|
import type { Block } from './pb/message.ts'
|
|
17
19
|
import type { QueuedBitswapMessage } from './utils/bitswap-message.ts'
|
|
18
20
|
import type { BlockBrokerGetBlockProgressEvents } from '@helia/interface'
|
|
19
|
-
import type { Provider, Routing, RoutingFindProvidersProgressEvents } from '@helia/interface
|
|
20
|
-
import type { Libp2p, AbortOptions, Connection, PeerId, Topology, ComponentLogger,
|
|
21
|
+
import type { Provider, Routing, RoutingFindProvidersProgressEvents } from '@helia/interface'
|
|
22
|
+
import type { Libp2p, AbortOptions, Connection, PeerId, Topology, ComponentLogger, Counter, Metrics, Stream, OpenConnectionProgressEvents, NewStreamProgressEvents } from '@libp2p/interface'
|
|
21
23
|
import type { Logger } from '@libp2p/logger'
|
|
22
24
|
import type { PeerQueueJobOptions } from '@libp2p/utils'
|
|
23
25
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
@@ -102,6 +104,22 @@ interface SendMessageJobOptions extends AbortOptions, ProgressOptions<BitswapNet
|
|
|
102
104
|
message: QueuedBitswapMessage
|
|
103
105
|
}
|
|
104
106
|
|
|
107
|
+
function toDialable (peer: CID | PeerId | Multiaddr | Multiaddr[]): PeerId | Multiaddr | Multiaddr[] {
|
|
108
|
+
if (isPeerId(peer)) {
|
|
109
|
+
return peer
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
if (isCID(peer)) {
|
|
113
|
+
return peerIdFromCID(peer)
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (isMultiaddr(peer) || Array.isArray(peer)) {
|
|
117
|
+
return peer
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
throw new InvalidParametersError(`Peer ${peer} was not a CID, a Multiaddr or a Multiaddr[]`)
|
|
121
|
+
}
|
|
122
|
+
|
|
105
123
|
export class Network extends TypedEventEmitter<NetworkEvents> {
|
|
106
124
|
private readonly log: Logger
|
|
107
125
|
private readonly libp2p: Libp2p
|
|
@@ -315,7 +333,7 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
|
|
|
315
333
|
// connect to initial session providers if supplied
|
|
316
334
|
if (options?.providers != null) {
|
|
317
335
|
await Promise.all(
|
|
318
|
-
options.providers.map(async prov => this.connectTo(prov)
|
|
336
|
+
options.providers.map(async prov => this.connectTo(prov, options)
|
|
319
337
|
.catch(err => {
|
|
320
338
|
this.log.error('could not connect to supplied provider - %e', err)
|
|
321
339
|
}))
|
|
@@ -397,36 +415,16 @@ export class Network extends TypedEventEmitter<NetworkEvents> {
|
|
|
397
415
|
/**
|
|
398
416
|
* Connects to another peer
|
|
399
417
|
*/
|
|
400
|
-
async connectTo (peer: PeerId | Multiaddr | Multiaddr[], options?: AbortOptions & ProgressOptions<BitswapNetworkProgressEvents>): Promise<Connection> {
|
|
418
|
+
async connectTo (peer: CID | PeerId | Multiaddr | Multiaddr[], options?: AbortOptions & ProgressOptions<BitswapNetworkProgressEvents>): Promise<Connection> {
|
|
401
419
|
if (!this.running) {
|
|
402
420
|
throw new NotStartedError('Network isn\'t running')
|
|
403
421
|
}
|
|
404
422
|
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
// dial and wait for identify - this is to avoid opening a protocol stream
|
|
408
|
-
// that we are not going to use but depends on the remote node running the
|
|
409
|
-
// identify protocol
|
|
410
|
-
const [
|
|
411
|
-
connection
|
|
412
|
-
] = await Promise.all([
|
|
413
|
-
this.libp2p.dial(peer, options),
|
|
414
|
-
raceEvent(this.libp2p, 'peer:identify', options?.signal, {
|
|
415
|
-
filter: (evt: CustomEvent<IdentifyResult>): boolean => {
|
|
416
|
-
if (!evt.detail.peerId.equals(peer)) {
|
|
417
|
-
return false
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
if (evt.detail.protocols.includes(BITSWAP_120)) {
|
|
421
|
-
return true
|
|
422
|
-
}
|
|
423
|
+
peer = toDialable(peer)
|
|
423
424
|
|
|
424
|
-
|
|
425
|
-
}
|
|
426
|
-
})
|
|
427
|
-
])
|
|
425
|
+
options?.onProgress?.(new CustomProgressEvent<PeerId | Multiaddr | Multiaddr[]>('bitswap:dial', peer))
|
|
428
426
|
|
|
429
|
-
return
|
|
427
|
+
return this.libp2p.dial(peer, options)
|
|
430
428
|
}
|
|
431
429
|
|
|
432
430
|
_updateSentStats (blocks: Map<string, Block>): void {
|
package/src/pb/message.ts
CHANGED
|
@@ -184,7 +184,7 @@ export namespace WantlistEntry {
|
|
|
184
184
|
value: boolean
|
|
185
185
|
}
|
|
186
186
|
|
|
187
|
-
export function encode (obj: Partial<WantlistEntry>): Uint8Array {
|
|
187
|
+
export function encode (obj: Partial<WantlistEntry>): Uint8Array<ArrayBuffer> {
|
|
188
188
|
return encodeMessage(obj, WantlistEntry.codec())
|
|
189
189
|
}
|
|
190
190
|
|
|
@@ -343,7 +343,7 @@ export namespace Wantlist {
|
|
|
343
343
|
value: boolean
|
|
344
344
|
}
|
|
345
345
|
|
|
346
|
-
export function encode (obj: Partial<Wantlist>): Uint8Array {
|
|
346
|
+
export function encode (obj: Partial<Wantlist>): Uint8Array<ArrayBuffer> {
|
|
347
347
|
return encodeMessage(obj, Wantlist.codec())
|
|
348
348
|
}
|
|
349
349
|
|
|
@@ -455,7 +455,7 @@ export namespace Block {
|
|
|
455
455
|
value: Uint8Array
|
|
456
456
|
}
|
|
457
457
|
|
|
458
|
-
export function encode (obj: Partial<Block>): Uint8Array {
|
|
458
|
+
export function encode (obj: Partial<Block>): Uint8Array<ArrayBuffer> {
|
|
459
459
|
return encodeMessage(obj, Block.codec())
|
|
460
460
|
}
|
|
461
461
|
|
|
@@ -583,7 +583,7 @@ export namespace BlockPresence {
|
|
|
583
583
|
value: BlockPresenceType
|
|
584
584
|
}
|
|
585
585
|
|
|
586
|
-
export function encode (obj: Partial<BlockPresence>): Uint8Array {
|
|
586
|
+
export function encode (obj: Partial<BlockPresence>): Uint8Array<ArrayBuffer> {
|
|
587
587
|
return encodeMessage(obj, BlockPresence.codec())
|
|
588
588
|
}
|
|
589
589
|
|
|
@@ -829,7 +829,7 @@ export namespace BitswapMessage {
|
|
|
829
829
|
value: number
|
|
830
830
|
}
|
|
831
831
|
|
|
832
|
-
export function encode (obj: Partial<BitswapMessage>): Uint8Array {
|
|
832
|
+
export function encode (obj: Partial<BitswapMessage>): Uint8Array<ArrayBuffer> {
|
|
833
833
|
return encodeMessage(obj, BitswapMessage.codec())
|
|
834
834
|
}
|
|
835
835
|
|
package/src/session.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
import { AbstractSession } from '@helia/utils'
|
|
2
|
-
import {
|
|
1
|
+
import { AbstractSession, isCID } from '@helia/utils'
|
|
2
|
+
import { peerIdFromCID } from '@libp2p/peer-id'
|
|
3
|
+
import { CID } from 'multiformats/cid'
|
|
3
4
|
import { CustomProgressEvent } from 'progress-events'
|
|
4
5
|
import type { BitswapProvider, BitswapWantProgressEvents } from './index.ts'
|
|
5
6
|
import type { Network } from './network.ts'
|
|
6
7
|
import type { WantList } from './want-list.ts'
|
|
7
8
|
import type { BlockRetrievalOptions, CreateSessionOptions } from '@helia/interface'
|
|
8
|
-
import type { ComponentLogger, Libp2p,
|
|
9
|
+
import type { ComponentLogger, Libp2p, AbortOptions } from '@libp2p/interface'
|
|
9
10
|
import type { Multiaddr } from '@multiformats/multiaddr'
|
|
10
|
-
import type { CID } from 'multiformats/cid'
|
|
11
11
|
|
|
12
12
|
export interface BitswapSessionComponents {
|
|
13
13
|
network: Network
|
|
@@ -17,7 +17,7 @@ export interface BitswapSessionComponents {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
interface ProviderPeer {
|
|
20
|
-
peerId:
|
|
20
|
+
peerId: CID
|
|
21
21
|
routing: string
|
|
22
22
|
toString(): string
|
|
23
23
|
}
|
|
@@ -40,9 +40,11 @@ class BitswapSession extends AbstractSession<ProviderPeer, BitswapWantProgressEv
|
|
|
40
40
|
}
|
|
41
41
|
|
|
42
42
|
async queryProvider (cid: CID, provider: ProviderPeer, options: AbortOptions): Promise<Uint8Array> {
|
|
43
|
-
|
|
43
|
+
const peerId = peerIdFromCID(provider.peerId)
|
|
44
44
|
|
|
45
|
-
|
|
45
|
+
this.log('sending WANT-BLOCK for %c to %p', cid, peerId)
|
|
46
|
+
|
|
47
|
+
const result = await this.wantList.wantSessionBlock(cid, peerId, options)
|
|
46
48
|
|
|
47
49
|
this.log('%p %s %c', provider, result.has ? 'has' : 'does not have', cid)
|
|
48
50
|
|
|
@@ -68,15 +70,15 @@ class BitswapSession extends AbstractSession<ProviderPeer, BitswapWantProgressEv
|
|
|
68
70
|
}
|
|
69
71
|
|
|
70
72
|
toFilterKey (provider: ProviderPeer): Uint8Array | string {
|
|
71
|
-
return provider.peerId.
|
|
73
|
+
return provider.peerId.multihash.bytes
|
|
72
74
|
}
|
|
73
75
|
|
|
74
76
|
equals (providerA: ProviderPeer, providerB: ProviderPeer): boolean {
|
|
75
77
|
return providerA.peerId.equals(providerB.peerId)
|
|
76
78
|
}
|
|
77
79
|
|
|
78
|
-
async convertToProvider (provider:
|
|
79
|
-
if (
|
|
80
|
+
async convertToProvider (provider: CID | Multiaddr | Multiaddr[], routing: string, options?: AbortOptions): Promise<ProviderPeer | undefined> {
|
|
81
|
+
if (isCID(provider)) {
|
|
80
82
|
return {
|
|
81
83
|
peerId: provider,
|
|
82
84
|
routing,
|
|
@@ -92,7 +94,7 @@ class BitswapSession extends AbstractSession<ProviderPeer, BitswapWantProgressEv
|
|
|
92
94
|
const connection = await this.libp2p.dial(provider, options)
|
|
93
95
|
|
|
94
96
|
return {
|
|
95
|
-
peerId: connection.remotePeer,
|
|
97
|
+
peerId: connection.remotePeer.toCID(),
|
|
96
98
|
routing,
|
|
97
99
|
toString: () => `Bitswap(${connection.remotePeer})`
|
|
98
100
|
}
|