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

Commit c05c5ec

Browse files
author
Alan Shaw
authored
refactor: convert bitswap to async/await (#1149)
1 parent 2d9afc8 commit c05c5ec

File tree

5 files changed

+67
-63
lines changed

5 files changed

+67
-63
lines changed

src/bitswap/index.js

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

3-
const moduleConfig = require('../utils/module-config')
3+
const callbackify = require('callbackify')
44

5-
module.exports = (arg) => {
6-
const send = moduleConfig(arg)
7-
8-
return {
9-
wantlist: require('./wantlist')(send),
10-
stat: require('./stat')(send),
11-
unwant: require('./unwant')(send)
12-
}
13-
}
5+
module.exports = (config) => ({
6+
wantlist: callbackify.variadic(require('./wantlist')(config)),
7+
stat: callbackify.variadic(require('./stat')(config)),
8+
unwant: callbackify.variadic(require('./unwant')(config))
9+
})

src/bitswap/stat.js

+19-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
3+
const configure = require('../lib/configure')
44
const Big = require('bignumber.js')
55

6-
const transform = function (res, callback) {
7-
callback(null, {
6+
module.exports = configure(({ ky }) => {
7+
return async (options) => {
8+
options = options || {}
9+
10+
const res = await ky.get('bitswap/stat', {
11+
timeout: options.timeout,
12+
signal: options.signal,
13+
headers: options.headers,
14+
searchParams: options.searchParams
15+
}).json()
16+
17+
return toCoreInterface(res)
18+
}
19+
})
20+
21+
function toCoreInterface (res) {
22+
return {
823
provideBufLen: res.ProvideBufLen,
924
wantlist: res.Wantlist || [],
1025
peers: res.Peers || [],
@@ -14,13 +29,5 @@ const transform = function (res, callback) {
1429
dataSent: new Big(res.DataSent),
1530
dupBlksReceived: new Big(res.DupBlksReceived),
1631
dupDataReceived: new Big(res.DupDataReceived)
17-
})
18-
}
19-
20-
module.exports = (send) => {
21-
return promisify((callback) => {
22-
send.andTransform({
23-
path: 'bitswap/stat'
24-
}, transform, callback)
25-
})
32+
}
2633
}

src/bitswap/unwant.js

+20-18
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,27 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
43
const CID = require('cids')
4+
const configure = require('../lib/configure')
55

6-
module.exports = (send) => {
7-
return promisify((cid, opts, callback) => {
8-
if (typeof (opts) === 'function') {
9-
callback = opts
10-
opts = {}
11-
}
6+
module.exports = configure(({ ky }) => {
7+
return async (cid, options) => {
8+
options = options || {}
9+
10+
const searchParams = new URLSearchParams(options.searchParams)
1211

13-
try {
14-
cid = new CID(cid)
15-
} catch (err) {
16-
return callback(err)
12+
if (typeof cid === 'string') {
13+
searchParams.set('arg', cid)
14+
} else {
15+
searchParams.set('arg', new CID(cid).toString())
1716
}
1817

19-
send({
20-
path: 'bitswap/unwant',
21-
args: cid.toBaseEncodedString(),
22-
qs: opts
23-
}, callback)
24-
})
25-
}
18+
const res = await ky.get('bitswap/unwant', {
19+
timeout: options.timeout,
20+
signal: options.signal,
21+
headers: options.headers,
22+
searchParams
23+
}).json()
24+
25+
return res
26+
}
27+
})

src/bitswap/wantlist.js

+20-21
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,29 @@
11
'use strict'
22

3-
const promisify = require('promisify-es6')
43
const CID = require('cids')
4+
const configure = require('../lib/configure')
55

6-
module.exports = (send) => {
7-
return promisify((peerId, opts, callback) => {
8-
if (typeof (peerId) === 'function') {
9-
callback = peerId
10-
opts = {}
11-
peerId = null
12-
} else if (typeof (opts) === 'function') {
13-
callback = opts
14-
opts = {}
15-
}
6+
module.exports = configure(({ ky }) => {
7+
return async (peerId, options) => {
8+
options = options || {}
9+
10+
const searchParams = new URLSearchParams(options.searchParams)
1611

1712
if (peerId) {
18-
try {
19-
opts.peer = new CID(peerId).toBaseEncodedString()
20-
} catch (err) {
21-
return callback(err)
13+
if (typeof peerId === 'string') {
14+
searchParams.set('peer', peerId)
15+
} else {
16+
searchParams.set('peer', new CID(peerId).toString())
2217
}
2318
}
2419

25-
send({
26-
path: 'bitswap/wantlist',
27-
qs: opts
28-
}, callback)
29-
})
30-
}
20+
const res = await ky.get('bitswap/wantlist', {
21+
timeout: options.timeout,
22+
signal: options.signal,
23+
headers: options.headers,
24+
searchParams
25+
}).json()
26+
27+
return res
28+
}
29+
})

src/utils/load-commands.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
function requireCommands (send, config) {
44
const cmds = {
55
...require('../files-regular')(config),
6-
getEndpointConfig: require('../get-endpoint-config')(config)
6+
getEndpointConfig: require('../get-endpoint-config')(config),
7+
bitswap: require('../bitswap')(config)
78
}
89

910
const subCmds = {
@@ -12,7 +13,6 @@ function requireCommands (send, config) {
1213

1314
// Block
1415
block: require('../block'),
15-
bitswap: require('../bitswap'),
1616

1717
// Graph
1818
dag: require('../dag'),

0 commit comments

Comments
 (0)