From 3928ccc696784272ab0dabf66d3b70a64ccf5c24 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Fri, 8 Nov 2019 15:05:36 +0000 Subject: [PATCH] refactor: convert bitswap to async/await --- src/bitswap/index.js | 16 ++++++--------- src/bitswap/stat.js | 31 +++++++++++++++++----------- src/bitswap/unwant.js | 38 ++++++++++++++++++----------------- src/bitswap/wantlist.js | 41 +++++++++++++++++++------------------- src/utils/load-commands.js | 4 ++-- 5 files changed, 67 insertions(+), 63 deletions(-) diff --git a/src/bitswap/index.js b/src/bitswap/index.js index 62f9fb58a..fb47d4da0 100644 --- a/src/bitswap/index.js +++ b/src/bitswap/index.js @@ -1,13 +1,9 @@ 'use strict' -const moduleConfig = require('../utils/module-config') +const callbackify = require('callbackify') -module.exports = (arg) => { - const send = moduleConfig(arg) - - return { - wantlist: require('./wantlist')(send), - stat: require('./stat')(send), - unwant: require('./unwant')(send) - } -} +module.exports = (config) => ({ + wantlist: callbackify.variadic(require('./wantlist')(config)), + stat: callbackify.variadic(require('./stat')(config)), + unwant: callbackify.variadic(require('./unwant')(config)) +}) diff --git a/src/bitswap/stat.js b/src/bitswap/stat.js index f0770d47d..3795327bb 100644 --- a/src/bitswap/stat.js +++ b/src/bitswap/stat.js @@ -1,10 +1,25 @@ 'use strict' -const promisify = require('promisify-es6') +const configure = require('../lib/configure') const Big = require('bignumber.js') -const transform = function (res, callback) { - callback(null, { +module.exports = configure(({ ky }) => { + return async (options) => { + options = options || {} + + const res = await ky.get('bitswap/stat', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams: options.searchParams + }).json() + + return toCoreInterface(res) + } +}) + +function toCoreInterface (res) { + return { provideBufLen: res.ProvideBufLen, wantlist: res.Wantlist || [], peers: res.Peers || [], @@ -14,13 +29,5 @@ const transform = function (res, callback) { dataSent: new Big(res.DataSent), dupBlksReceived: new Big(res.DupBlksReceived), dupDataReceived: new Big(res.DupDataReceived) - }) -} - -module.exports = (send) => { - return promisify((callback) => { - send.andTransform({ - path: 'bitswap/stat' - }, transform, callback) - }) + } } diff --git a/src/bitswap/unwant.js b/src/bitswap/unwant.js index 865fbc27b..9b2044d00 100644 --- a/src/bitswap/unwant.js +++ b/src/bitswap/unwant.js @@ -1,25 +1,27 @@ 'use strict' -const promisify = require('promisify-es6') const CID = require('cids') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((cid, opts, callback) => { - if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async (cid, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) - try { - cid = new CID(cid) - } catch (err) { - return callback(err) + if (typeof cid === 'string') { + searchParams.set('arg', cid) + } else { + searchParams.set('arg', new CID(cid).toString()) } - send({ - path: 'bitswap/unwant', - args: cid.toBaseEncodedString(), - qs: opts - }, callback) - }) -} + const res = await ky.get('bitswap/unwant', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return res + } +}) diff --git a/src/bitswap/wantlist.js b/src/bitswap/wantlist.js index 6cd1e3868..3235de8a9 100644 --- a/src/bitswap/wantlist.js +++ b/src/bitswap/wantlist.js @@ -1,30 +1,29 @@ 'use strict' -const promisify = require('promisify-es6') const CID = require('cids') +const configure = require('../lib/configure') -module.exports = (send) => { - return promisify((peerId, opts, callback) => { - if (typeof (peerId) === 'function') { - callback = peerId - opts = {} - peerId = null - } else if (typeof (opts) === 'function') { - callback = opts - opts = {} - } +module.exports = configure(({ ky }) => { + return async (peerId, options) => { + options = options || {} + + const searchParams = new URLSearchParams(options.searchParams) if (peerId) { - try { - opts.peer = new CID(peerId).toBaseEncodedString() - } catch (err) { - return callback(err) + if (typeof peerId === 'string') { + searchParams.set('peer', peerId) + } else { + searchParams.set('peer', new CID(peerId).toString()) } } - send({ - path: 'bitswap/wantlist', - qs: opts - }, callback) - }) -} + const res = await ky.get('bitswap/wantlist', { + timeout: options.timeout, + signal: options.signal, + headers: options.headers, + searchParams + }).json() + + return res + } +}) diff --git a/src/utils/load-commands.js b/src/utils/load-commands.js index 710b8396f..81948a54e 100644 --- a/src/utils/load-commands.js +++ b/src/utils/load-commands.js @@ -3,7 +3,8 @@ function requireCommands (send, config) { const cmds = { ...require('../files-regular')(config), - getEndpointConfig: require('../get-endpoint-config')(config) + getEndpointConfig: require('../get-endpoint-config')(config), + bitswap: require('../bitswap')(config) } const subCmds = { @@ -12,7 +13,6 @@ function requireCommands (send, config) { // Block block: require('../block'), - bitswap: require('../bitswap'), // Graph dag: require('../dag'),