Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 5417b46

Browse files
committed
feat(bitswap.unwatch) expose bitswap.unwatch to cli and http api
1 parent e189b72 commit 5417b46

File tree

4 files changed

+37
-9
lines changed

4 files changed

+37
-9
lines changed

src/cli/commands/bitswap/unwant.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
'use strict'
22

3+
const print = require('../../utils').print
4+
35
module.exports = {
46
command: 'unwant <key>',
57

6-
describe: 'Remove a given block from your wantlist.',
8+
describe: 'Removes a given block from your wantlist.',
79

10+
builder: {
11+
key: {
12+
alias: 'k',
13+
describe: 'Key to remove from your wantlist',
14+
type: 'string'
15+
}
16+
},
817
handler (argv) {
9-
throw new Error('Not implemented yet')
18+
argv.ipfs.bitswap.unwant(argv.key, (err, res) => {
19+
if (err) {
20+
throw err
21+
}
22+
print(`Key ${argv.key} removed from wantlist`)
23+
})
1024
}
1125
}

src/core/components/bitswap.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,12 @@ module.exports = function bitswap (self) {
4040
})
4141
}),
4242

43-
unwant: (key) => {
43+
unwant: promisify((key, callback) => {
4444
if (!self.isOnline()) {
4545
throw new Error(OFFLINE_ERROR)
4646
}
4747

48-
// TODO: implement when https://github.com/ipfs/js-ipfs-bitswap/pull/10 is merged
49-
}
48+
return self._bitswap.unwant(key)
49+
})
5050
}
5151
}

src/http/api/resources/bitswap.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,19 @@ exports.stat = (request, reply) => {
4646
}
4747

4848
exports.unwant = {
49-
// uses common parseKey method that returns a `key`
49+
// uses common parseKey method that assigns a `key` to request.pre.args
5050
parseArgs: parseKey,
5151

52+
// main route handler which is called after the above `parseArgs`, but only if the args were valid
5253
handler: (request, reply) => {
53-
reply(boom.badRequest(new Error('Not implemented yet')))
54+
const key = request.pre.args.key
55+
const ipfs = request.server.app.ipfs
56+
try {
57+
ipfs.bitswap.unwant(key)
58+
} catch (err) {
59+
return reply(boom.badRequest(err))
60+
}
61+
62+
reply({ Key: key })
5463
}
5564
}

test/cli/bitswap.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,7 @@ describe('bitswap', () => runOn((thing) => {
2323
})
2424
})
2525

26-
// TODO @hacdias fix this with https://github.com/ipfs/js-ipfs/pull/1198
27-
it.skip('stat', function () {
26+
it('stat', function () {
2827
this.timeout(20 * 1000)
2928

3029
return ipfs('bitswap stat').then((out) => {
@@ -40,4 +39,10 @@ describe('bitswap', () => runOn((thing) => {
4039
].join('\n') + '\n')
4140
})
4241
})
42+
43+
it('unwant', function () {
44+
return ipfs('bitswap unwant ' + key).then((out) => {
45+
expect(out).to.eql(`Key ${key} removed from wantlist\n`)
46+
})
47+
})
4348
}))

0 commit comments

Comments
 (0)