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

feat: add util.cid options #15

Merged
merged 2 commits into from
Jun 25, 2018
Merged
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
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)
Copy link
Member

Choose a reason for hiding this comment

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

Nit-pick: This can be written as expect(Buffer.isBuffer(serialized)).to.be.true(). There's no need to change it, it's just a note for future cases.

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()
})
})
})