Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
fix(put): pass CID options to resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored and vmx committed Jul 17, 2018
1 parent 3845a93 commit a419048
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ const Resolver = new Resolver(<ipfs-block-service instance>)
`options` is an object that must contain one of the following combinations:
- `cid` - the CID of the node
- `hashAlg` and `format` - the hashAlg and the format that should be used to create the CID of the node
- `[hashAlg]`, `[version]` and `format` - the hashAlg, version and the format that should be used to create the CID of the node. The
`hashAlg` and `version` defaults to the default values for the `format`.

`callback` is a function that should have the signature as following: `function (err, cid) {}`, where `err` is an Error object in case of error and `cid` is the cid of the stored object.

Expand Down
6 changes: 1 addition & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,15 +228,11 @@ class IPLDResolver {
return this._put(options.cid, node, callback)
}

options.hashAlg = options.hashAlg || 'sha2-256'
const r = this.resolvers[options.format]
if (!r) {
return callback(new Error('No resolver found for codec "' + options.format + '"'))
}
// TODO add support for different hash funcs in the utils of
// each format (just really needed for CBOR for now, really
// r.util.cid(node1, hashAlg, (err, cid) => {
r.util.cid(node, (err, cid) => {
r.util.cid(node, options, (err, cid) => {
if (err) {
return callback(err)
}
Expand Down
30 changes: 25 additions & 5 deletions test/ipld-dag-cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const dagCBOR = require('ipld-dag-cbor')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')
const multihash = require('multihashes')

const IPLDResolver = require('../src')

Expand Down Expand Up @@ -107,11 +108,30 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with hashAlg + format', (done) => {
resolver.put(node1, {
format: 'dag-cbor',
hashAlg: 'sha2-256'
}, done)
it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'dag-cbor' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'dag-cbor', hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-cbor')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})

it('resolver.get just CID', (done) => {
Expand Down
31 changes: 25 additions & 6 deletions test/ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ const dagPB = require('ipld-dag-pb')
const series = require('async/series')
const each = require('async/each')
const pull = require('pull-stream')

const multihash = require('multihashes')
const IPLDResolver = require('../src')

module.exports = (repo) => {
Expand Down Expand Up @@ -151,11 +151,30 @@ module.exports = (repo) => {
resolver.put(node1, { cid: cid1 }, done)
})

it('resolver.put with hashAlg + format', (done) => {
resolver.put(node1, {
format: 'dag-pb',
hashAlg: 'sha2-256'
}, done)
it('resolver.put with format', (done) => {
resolver.put(node1, { format: 'dag-pb' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(0)
expect(cid.codec).to.equal('dag-pb')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-256')
done()
})
})

it('resolver.put with format + hashAlg', (done) => {
resolver.put(node1, { format: 'dag-pb', hashAlg: 'sha2-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('dag-pb')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha2-512')
done()
})
})

it('resolver.get just CID', (done) => {
Expand Down

0 comments on commit a419048

Please sign in to comment.