Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 1449103

Browse files
author
Alan Shaw
authored
refactor: convert bitswap API to async/await (#2659)
1 parent 78f361e commit 1449103

File tree

6 files changed

+72
-72
lines changed

6 files changed

+72
-72
lines changed

src/core/components/bitswap.js

-72
This file was deleted.

src/core/components/bitswap/stat.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
'use strict'
2+
3+
const Big = require('bignumber.js')
4+
5+
function formatWantlist (list) {
6+
return Array.from(list).map((e) => ({ '/': e[1].cid.toString() }))
7+
}
8+
9+
module.exports = ({ bitswap }) => {
10+
return async function stat () { // eslint-disable-line require-await
11+
const snapshot = bitswap.stat().snapshot
12+
13+
return {
14+
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
15+
blocksReceived: new Big(snapshot.blocksReceived),
16+
wantlist: formatWantlist(bitswap.getWantlist()),
17+
peers: bitswap.peers().map((id) => id.toB58String()),
18+
dupBlksReceived: new Big(snapshot.dupBlksReceived),
19+
dupDataReceived: new Big(snapshot.dupDataReceived),
20+
dataReceived: new Big(snapshot.dataReceived),
21+
blocksSent: new Big(snapshot.blocksSent),
22+
dataSent: new Big(snapshot.dataSent)
23+
}
24+
}
25+
}

src/core/components/bitswap/unwant.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict'
2+
3+
const CID = require('cids')
4+
const errCode = require('err-code')
5+
6+
module.exports = ({ bitswap }) => {
7+
return async function unwant (keys) { // eslint-disable-line require-await
8+
if (!Array.isArray(keys)) {
9+
keys = [keys]
10+
}
11+
12+
try {
13+
keys = keys.map((key) => new CID(key))
14+
} catch (err) {
15+
throw errCode(err, 'ERR_INVALID_CID')
16+
}
17+
18+
return bitswap.unwant(keys)
19+
}
20+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict'
2+
3+
const PeerId = require('peer-id')
4+
5+
function formatWantlist (list) {
6+
return Array.from(list).map((e) => ({ '/': e[1].cid.toString() }))
7+
}
8+
9+
module.exports = ({ bitswap }) => {
10+
return async function wantlist (peerId) { // eslint-disable-line require-await
11+
const list = peerId
12+
? bitswap.wantlistForPeer(PeerId.createFromCID(peerId))
13+
: bitswap.getWantlist()
14+
15+
return { Keys: formatWantlist(list) }
16+
}
17+
}

src/core/components/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
'use strict'
22

33
exports.add = require('./add')
4+
exports.bitswap = {
5+
stat: require('./bitswap/stat'),
6+
unwant: require('./bitswap/unwant'),
7+
wantlist: require('./bitswap/wantlist')
8+
}
49
exports.config = require('./config')
510
exports.init = require('./init')
611
exports.ping = require('./ping')

src/core/components/start.js

+5
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ function createApi ({
131131

132132
const api = {
133133
add,
134+
bitswap: {
135+
stat: Commands.bitswap.stat({ bitswap }),
136+
unwant: Commands.bitswap.unwant({ bitswap }),
137+
wantlist: Commands.bitswap.wantlist({ bitswap })
138+
},
134139
config: Commands.config({ repo }),
135140
init: () => { throw new AlreadyInitializedError() },
136141
ping: Commands.ping({ libp2p }),

0 commit comments

Comments
 (0)