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

Commit

Permalink
feat: add util.cid options (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
richardschneider authored Jun 25, 2018
1 parent d0ccec3 commit 5ed9c74
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,25 @@ exports.deserialize = (data, callback) => {
}
}

exports.cid = (dagNode, callback) => {
/**
* @callback CidCallback
* @param {?Error} error - Error if getting the CID failed
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
*
* @param {Object} dagNode - Internal representation
* @param {Object} [options] - Ignored
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
exports.cid = (dagNode, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, resolver.defaultHashAlg, cb),
Expand Down
61 changes: 61 additions & 0 deletions test/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* eslint-env mocha */
'use strict'

const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const ipldGit = require('../src')
const multihash = require('multihashes')
const CID = require('cids')

describe('IPLD format util', () => {
const tagNode = {
gitType: 'tag',
object: {'/': new CID('z8mWaHQaEAKd5KMRNU3npB3saSZmhFh3e').buffer},
type: 'commit',
tag: 'v0.0.0',
tagger: {
name: 'John Doe',
email: 'johndoe@example.com',
date: '1497302532 +0200'
},
message: 'A message\n'
}

it('.serialize and .deserialize', (done) => {
ipldGit.util.serialize(tagNode, (err, serialized) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(serialized)).to.equal(true)
ipldGit.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist()
expect(tagNode).to.eql(deserialized)
done()
})
})
})

it('.cid', (done) => {
ipldGit.util.cid(tagNode, (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')
done()
})
})

it('.cid ignores options', (done) => {
ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (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')
done()
})
})
})

0 comments on commit 5ed9c74

Please sign in to comment.