@@ -30,6 +30,7 @@ const statsKeys = [
30
30
*
31
31
* @param {Libp2p} libp2p
32
32
* @param {Blockstore} blockstore
33
+ * @param {Object} options
33
34
*/
34
35
class Bitswap {
35
36
constructor (libp2p, blockstore, options) {
@@ -93,17 +94,17 @@ class Bitswap {
93
94
}))
94
95
}
95
96
96
- // _handleReceivedBlock (peerId, block, wasWanted, callback) {
97
97
async _handleReceivedBlock (peerId, block, wasWanted) {
98
98
this._log('received block')
99
99
100
100
const has = await this.blockstore.has(block.cid)
101
101
this._updateReceiveCounters(peerId.toB58String(), block, has)
102
- if (has || !wasWanted) {
102
+
103
+ if (!wasWanted) {
103
104
return
104
105
}
105
106
106
- await this._putBlock (block)
107
+ await this.put (block)
107
108
}
108
109
109
110
_updateReceiveCounters (peerId, block, exists) {
@@ -133,23 +134,16 @@ class Bitswap {
133
134
this._stats.disconnected(peerId)
134
135
}
135
136
136
- async _putBlock (block) {
137
- await this.blockstore.put(block)
138
-
139
- this.notifications.hasBlock(block)
140
-
141
- // Note: Don't wait for provide to finish before returning
142
- this.network.provide(block.cid).catch((err) => {
143
- this._log.error('Failed to provide: %s', err.message)
144
- })
145
-
146
- this.engine.receivedBlocks([block.cid])
147
- }
148
-
137
+ /**
138
+ * @returns {void}
139
+ */
149
140
enableStats () {
150
141
this._stats.enable()
151
142
}
152
143
144
+ /**
145
+ * @returns {void}
146
+ */
153
147
disableStats () {
154
148
this._stats.disable()
155
149
}
@@ -158,7 +152,7 @@ class Bitswap {
158
152
* Return the current wantlist for a given `peerId`
159
153
*
160
154
* @param {PeerId} peerId
161
- * @returns {Wantlist }
155
+ * @returns {Map }
162
156
*/
163
157
wantlistForPeer (peerId) {
164
158
return this.engine.wantlistForPeer(peerId)
@@ -168,7 +162,7 @@ class Bitswap {
168
162
* Return ledger information for a given `peerId`
169
163
*
170
164
* @param {PeerId} peerId
171
- * @returns {? Object}
165
+ * @returns {Object}
172
166
*/
173
167
ledgerForPeer (peerId) {
174
168
return this.engine.ledgerForPeer(peerId)
@@ -179,8 +173,7 @@ class Bitswap {
179
173
* blockstore it is returned, otherwise the block is added to the wantlist and returned once another node sends it to us.
180
174
*
181
175
* @param {CID} cid
182
- * @param {function(Error, Block)} callback
183
- * @returns {void}
176
+ * @returns {Promise<Block>}
184
177
*/
185
178
async get (cid) {
186
179
for await (const block of this.getMany([cid])) {
@@ -192,9 +185,8 @@ class Bitswap {
192
185
* Fetch a a list of blocks by cid. If the blocks are in the local
193
186
* blockstore they are returned, otherwise the blocks are added to the wantlist and returned once another node sends them to us.
194
187
*
195
- * @param {Array<CID>} cids
196
- * @param {function(Error, Blocks)} callback
197
- * @returns {void}
188
+ * @param {Iterable<CID>} cids
189
+ * @returns {Promise<AsyncIterator<Block>>}
198
190
*/
199
191
async * getMany (cids) {
200
192
let pendingStart = cids.length
@@ -238,7 +230,12 @@ class Bitswap {
238
230
}
239
231
}
240
232
241
- // removes the given cids from the wantlist independent of any ref counts
233
+ /**
234
+ * Removes the given CIDs from the wantlist independent of any ref counts
235
+ *
236
+ * @param {Iterable<CID>} cids
237
+ * @returns {void}
238
+ */
242
239
unwant (cids) {
243
240
if (!Array.isArray(cids)) {
244
241
cids = [cids]
@@ -248,7 +245,12 @@ class Bitswap {
248
245
cids.forEach((cid) => this.notifications.unwantBlock(cid))
249
246
}
250
247
251
- // removes the given keys from the want list
248
+ /**
249
+ * Removes the given keys from the want list
250
+ *
251
+ * @param {Iterable<CID>} cids
252
+ * @returns {void}
253
+ */
252
254
cancelWants (cids) {
253
255
if (!Array.isArray(cids)) {
254
256
cids = [cids]
@@ -261,26 +263,18 @@ class Bitswap {
261
263
* send it to nodes that have it in their wantlist.
262
264
*
263
265
* @param {Block} block
264
- * @param {function(Error)} callback
265
- * @returns {void}
266
+ * @returns {Promise<void>}
266
267
*/
267
268
async put (block) { // eslint-disable-line require-await
268
- if (!Array.isArray(block)) {
269
- block = [
270
- block
271
- ]
272
- }
273
-
274
- return this.putMany(block)
269
+ return this.putMany([block])
275
270
}
276
271
277
272
/**
278
273
* Put the given blocks to the underlying blockstore and
279
274
* send it to nodes that have it them their wantlist.
280
275
*
281
- * @param {AsyncIterable<Block>} blocks
282
- * @param {function(Error)} callback
283
- * @returns {void}
276
+ * @param {AsyncIterable<Block>|Iterable<Block>} blocks
277
+ * @returns {Promise<void>}
284
278
*/
285
279
async putMany (blocks) { // eslint-disable-line require-await
286
280
const self = this
@@ -315,7 +309,7 @@ class Bitswap {
315
309
/**
316
310
* Get the current list of partners.
317
311
*
318
- * @returns {Array <PeerId>}
312
+ * @returns {Iterator <PeerId>}
319
313
*/
320
314
peers () {
321
315
return this.engine.peers()
0 commit comments