From 572aa82dd64b1334c44589694f3b8c5b69c50aef Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Wed, 11 Dec 2019 12:13:46 +0000 Subject: [PATCH] refactor: convert bitswap API to async/await --- src/core/components/bitswap.js | 72 ------------------------- src/core/components/bitswap/stat.js | 25 +++++++++ src/core/components/bitswap/unwant.js | 20 +++++++ src/core/components/bitswap/wantlist.js | 17 ++++++ src/core/components/index.js | 5 ++ src/core/components/start.js | 5 ++ 6 files changed, 72 insertions(+), 72 deletions(-) delete mode 100644 src/core/components/bitswap.js create mode 100644 src/core/components/bitswap/stat.js create mode 100644 src/core/components/bitswap/unwant.js create mode 100644 src/core/components/bitswap/wantlist.js diff --git a/src/core/components/bitswap.js b/src/core/components/bitswap.js deleted file mode 100644 index 654f9f045b..0000000000 --- a/src/core/components/bitswap.js +++ /dev/null @@ -1,72 +0,0 @@ -'use strict' - -const OFFLINE_ERROR = require('../utils').OFFLINE_ERROR -const callbackify = require('callbackify') -const Big = require('bignumber.js') -const CID = require('cids') -const PeerId = require('peer-id') -const errCode = require('err-code') - -function formatWantlist (list, cidBase) { - return Array.from(list).map((e) => ({ '/': e[1].cid.toBaseEncodedString(cidBase) })) -} - -module.exports = function bitswap (self) { - return { - wantlist: callbackify.variadic(async (peerId) => { // eslint-disable-line require-await - if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) - } - - let list - - if (peerId) { - peerId = PeerId.createFromB58String(peerId) - - list = self._bitswap.wantlistForPeer(peerId) - } else { - list = self._bitswap.getWantlist() - } - - return { Keys: formatWantlist(list) } - }), - - stat: callbackify(async () => { // eslint-disable-line require-await - if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) - } - - const snapshot = self._bitswap.stat().snapshot - - return { - provideBufLen: parseInt(snapshot.providesBufferLength.toString()), - blocksReceived: new Big(snapshot.blocksReceived), - wantlist: formatWantlist(self._bitswap.getWantlist()), - peers: self._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) - } - }), - - unwant: callbackify(async (keys) => { // eslint-disable-line require-await - if (!self.isOnline()) { - throw new Error(OFFLINE_ERROR) - } - - if (!Array.isArray(keys)) { - keys = [keys] - } - - try { - keys = keys.map((key) => new CID(key)) - } catch (err) { - throw errCode(err, 'ERR_INVALID_CID') - } - - return self._bitswap.unwant(keys) - }) - } -} diff --git a/src/core/components/bitswap/stat.js b/src/core/components/bitswap/stat.js new file mode 100644 index 0000000000..b2b609d44d --- /dev/null +++ b/src/core/components/bitswap/stat.js @@ -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) + } + } +} diff --git a/src/core/components/bitswap/unwant.js b/src/core/components/bitswap/unwant.js new file mode 100644 index 0000000000..9f71172ef4 --- /dev/null +++ b/src/core/components/bitswap/unwant.js @@ -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) + } +} diff --git a/src/core/components/bitswap/wantlist.js b/src/core/components/bitswap/wantlist.js new file mode 100644 index 0000000000..02c882abc6 --- /dev/null +++ b/src/core/components/bitswap/wantlist.js @@ -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) } + } +} diff --git a/src/core/components/index.js b/src/core/components/index.js index 44d922712a..6c217e946f 100644 --- a/src/core/components/index.js +++ b/src/core/components/index.js @@ -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') diff --git a/src/core/components/start.js b/src/core/components/start.js index f9f41c7458..e13f7e8cd1 100644 --- a/src/core/components/start.js +++ b/src/core/components/start.js @@ -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,