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

refactor: convert bitswap API to async/await #2659

Merged
merged 1 commit into from
Dec 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions src/core/components/bitswap.js

This file was deleted.

25 changes: 25 additions & 0 deletions src/core/components/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'use strict'

const Big = require('bignumber.js')

function formatWantlist (list) {
return Array.from(list).map((e) => ({ '/': e[1].cid.toString() }))
}

module.exports = ({ bitswap }) => {
return async function stat () { // eslint-disable-line require-await
const snapshot = bitswap.stat().snapshot

return {
provideBufLen: parseInt(snapshot.providesBufferLength.toString()),
blocksReceived: new Big(snapshot.blocksReceived),
wantlist: formatWantlist(bitswap.getWantlist()),
peers: bitswap.peers().map((id) => id.toB58String()),
dupBlksReceived: new Big(snapshot.dupBlksReceived),
dupDataReceived: new Big(snapshot.dupDataReceived),
dataReceived: new Big(snapshot.dataReceived),
blocksSent: new Big(snapshot.blocksSent),
dataSent: new Big(snapshot.dataSent)
}
}
}
20 changes: 20 additions & 0 deletions src/core/components/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict'

const CID = require('cids')
const errCode = require('err-code')

module.exports = ({ bitswap }) => {
return async function unwant (keys) { // eslint-disable-line require-await
if (!Array.isArray(keys)) {
keys = [keys]
}

try {
keys = keys.map((key) => new CID(key))
} catch (err) {
throw errCode(err, 'ERR_INVALID_CID')
}

return bitswap.unwant(keys)
}
}
17 changes: 17 additions & 0 deletions src/core/components/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

const PeerId = require('peer-id')

function formatWantlist (list) {
return Array.from(list).map((e) => ({ '/': e[1].cid.toString() }))
}

module.exports = ({ bitswap }) => {
return async function wantlist (peerId) { // eslint-disable-line require-await
const list = peerId
? bitswap.wantlistForPeer(PeerId.createFromCID(peerId))
: bitswap.getWantlist()

return { Keys: formatWantlist(list) }
}
}
5 changes: 5 additions & 0 deletions src/core/components/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
'use strict'

exports.add = require('./add')
exports.bitswap = {
stat: require('./bitswap/stat'),
unwant: require('./bitswap/unwant'),
wantlist: require('./bitswap/wantlist')
}
exports.config = require('./config')
exports.init = require('./init')
exports.start = require('./start')
Expand Down
5 changes: 5 additions & 0 deletions src/core/components/start.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ function createApi ({

const api = {
add,
bitswap: {
stat: Commands.bitswap.stat({ bitswap }),
unwant: Commands.bitswap.unwant({ bitswap }),
wantlist: Commands.bitswap.wantlist({ bitswap })
},
config: Commands.config({ repo }),
init: () => { throw new AlreadyInitializedError() },
start: () => apiManager.api,
Expand Down