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

fix: added rm method for blocks #910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ const ipfs = ipfsClient({
- [block](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md)
- [`ipfs.block.get(cid, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockget)
- [`ipfs.block.put(block, [options], [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockput)
- [`ipfs.block.rm(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockrm)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method takes optional options.

- [`ipfs.block.stat(cid, [callback])`](https://github.com/ipfs/interface-ipfs-core/blob/master/SPEC/BLOCK.md#blockstat)

#### Graph
Expand Down
3 changes: 2 additions & 1 deletion src/block/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = (arg) => {
return {
get: require('./get')(send),
stat: require('./stat')(send),
put: require('./put')(send)
put: require('./put')(send),
rm: require('./rm')(send)
}
}
31 changes: 31 additions & 0 deletions src/block/rm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict'

const promisify = require('promisify-es6')
const CID = require('cids')
const multihash = require('multihashes')

module.exports = (send) => {
return promisify((args, opts, callback) => {
if (args && CID.isCID(args)) {
args = multihash.toB58String(args.multihash)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just call toString on the CID instance:

Suggested change
args = multihash.toB58String(args.multihash)
args = args.toString()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to check if args is passed as a CID in a buffer. The CID constructor will take a string/buffer/instance so it's usually best to just pass args to the constructor to nomalise the input to a CID that you can serialize. Something like this:

try {
  args = new CID(args).toString()
} catch (err) {
  return callback(err)
}
// args is now a valid, serialized (string) CID

}

if (typeof (opts) === 'function') {
callback = opts
opts = {}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also add a opts = opts || {} after here in case people call ipfs.block.rm(hash, null, (err) => {})


const request = {
path: 'block/rm',
args: args,
qs: opts
}

// Transform the response from { Key, Size } objects to { key, size } objects
const transform = (stats, callback) => {
callback(null)
}

send.andTransform(request, transform, callback)
})
}