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

Commit 8d3f52a

Browse files
committed
chore: simplify CID parsing for wantlist and unwant
License: MIT Signed-off-by: Alan Shaw <[email protected]>
1 parent ed5b651 commit 8d3f52a

File tree

2 files changed

+13
-41
lines changed

2 files changed

+13
-41
lines changed

src/bitswap/unwant.js

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,21 @@ const promisify = require('promisify-es6')
44
const CID = require('cids')
55

66
module.exports = (send) => {
7-
return promisify((args, opts, callback) => {
7+
return promisify((cid, opts, callback) => {
88
if (typeof (opts) === 'function') {
99
callback = opts
1010
opts = {}
1111
}
1212

13-
// Mirrors block.get's parsing of the CID
14-
let cid
1513
try {
16-
if (CID.isCID(args)) {
17-
cid = args
18-
args = cid.toBaseEncodedString()
19-
} else if (Buffer.isBuffer(args)) {
20-
cid = new CID(args)
21-
args = cid.toBaseEncodedString()
22-
} else if (typeof args === 'string') {
23-
cid = new CID(args)
24-
} else {
25-
return callback(new Error('invalid argument'))
26-
}
14+
cid = new CID(cid)
2715
} catch (err) {
2816
return callback(err)
2917
}
3018

3119
send({
3220
path: 'bitswap/unwant',
33-
args: args,
21+
args: cid.toBaseEncodedString(),
3422
qs: opts
3523
}, callback)
3624
})

src/bitswap/wantlist.js

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,42 +4,26 @@ const promisify = require('promisify-es6')
44
const CID = require('cids')
55

66
module.exports = (send) => {
7-
return promisify((args, opts, callback) => {
8-
9-
if (typeof (args) === 'function') {
10-
callback = args
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
1114
opts = {}
12-
args = null
13-
} else {
14-
if (typeof (opts) === 'function') {
15-
callback = opts
16-
opts = {}
17-
}
1815
}
1916

20-
// Mirrors block.get's parsing of the CID
21-
let cid
22-
if (args) {
17+
if (peerId) {
2318
try {
24-
if (CID.isCID(args)) {
25-
cid = args
26-
args = cid.toBaseEncodedString()
27-
} else if (Buffer.isBuffer(args)) {
28-
cid = new CID(args)
29-
args = cid.toBaseEncodedString()
30-
} else if (typeof args === 'string') {
31-
cid = new CID(args)
32-
} else {
33-
return callback(new Error('invalid argument'))
34-
}
19+
opts.peer = new CID(peerId).toBaseEncodedString()
3520
} catch (err) {
3621
return callback(err)
3722
}
38-
opts.peer = args
3923
}
24+
4025
send({
4126
path: 'bitswap/wantlist',
42-
args: args,
4327
qs: opts
4428
}, callback)
4529
})

0 commit comments

Comments
 (0)