Skip to content
This repository has been archived by the owner on Mar 10, 2020. It is now read-only.

Commit

Permalink
refactor: convert bitswap to async/await (#1149)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alan Shaw authored Nov 8, 2019
1 parent 2d9afc8 commit c05c5ec
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 63 deletions.
16 changes: 6 additions & 10 deletions src/bitswap/index.js
Original file line number Diff line number Diff line change
@@ -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))
})
31 changes: 19 additions & 12 deletions src/bitswap/stat.js
Original file line number Diff line number Diff line change
@@ -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 || [],
Expand All @@ -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)
})
}
}
38 changes: 20 additions & 18 deletions src/bitswap/unwant.js
Original file line number Diff line number Diff line change
@@ -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
}
})
41 changes: 20 additions & 21 deletions src/bitswap/wantlist.js
Original file line number Diff line number Diff line change
@@ -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
}
})
4 changes: 2 additions & 2 deletions src/utils/load-commands.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -12,7 +13,6 @@ function requireCommands (send, config) {

// Block
block: require('../block'),
bitswap: require('../bitswap'),

// Graph
dag: require('../dag'),
Expand Down

0 comments on commit c05c5ec

Please sign in to comment.