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

Commit

Permalink
fix: do not ignore cid.options (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Jun 28, 2018
1 parent ce5b547 commit 4641b63
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
10 changes: 7 additions & 3 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ exports.deserialize = (data, callback) => {
* Get the CID of the DAG-Node.
*
* @param {Object} dagNode - Internal representation
* @param {Object} [options] - Ignored
* @param {Object} [options] - Options to create the CID
* @param {number} [options.version=1] - CID version number
* @param {string} [options.hashAlg='sha1'] - Hashing algorithm
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
Expand All @@ -88,9 +90,11 @@ exports.cid = (dagNode, options, callback) => {
options = {}
}
options = options || {}
const hashAlg = options.hashAlg || resolver.defaultHashAlg
const version = typeof options.version === 'undefined' ? 1 : options.version
waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, resolver.defaultHashAlg, cb),
(mh, cb) => cb(null, new CID(1, resolver.multicodec, mh))
(serialized, cb) => multihashing(serialized, hashAlg, cb),
(mh, cb) => cb(null, new CID(version, resolver.multicodec, mh))
], callback)
}
13 changes: 10 additions & 3 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,21 @@ describe('IPLD format util', () => {
})
})

it('.cid ignores options', (done) => {
ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => {
it('.cid with options', (done) => {
ipldGit.util.cid(tagNode, { hashAlg: 'sha3-512' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('git-raw')
expect(cid.multihash).to.exist()
const mh = multihash.decode(cid.multihash)
expect(mh.name).to.equal('sha1')
expect(mh.name).to.equal('sha3-512')
done()
})
})

it('.cid errors unknown hashAlg', (done) => {
ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => {
expect(err).to.exist()
done()
})
})
Expand Down

0 comments on commit 4641b63

Please sign in to comment.