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

fix: pass serialized blob to util.cid #16

Merged
merged 2 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 5 additions & 4 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict'

const assert = require('assert')
const setImmediate = require('async/setImmediate')
const waterfall = require('async/waterfall')
const multihashing = require('multihashing-async')
Expand Down Expand Up @@ -77,20 +78,20 @@ exports.deserialize = (data, callback) => {
/**
* Get the CID of the DAG-Node.
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps changing to Get the CID of the Git object?

*
* @param {Object} dagNode - Internal representation
* @param {Buffer} blob - Serialized binary data
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps: Git object.

* @param {Object} [options] - Ignored
* @param {CidCallback} callback - Callback that handles the return value
* @returns {void}
*/
exports.cid = (dagNode, options, callback) => {
exports.cid = (blob, options, callback) => {
assert(Buffer.isBuffer(blob), 'blob must be a Buffer')
if (typeof options === 'function') {
callback = options
options = {}
}
options = options || {}
waterfall([
(cb) => exports.serialize(dagNode, cb),
(serialized, cb) => multihashing(serialized, resolver.defaultHashAlg, cb),
(cb) => multihashing(blob, resolver.defaultHashAlg, cb),
(mh, cb) => cb(null, new CID(1, resolver.multicodec, mh))
], callback)
}
2 changes: 1 addition & 1 deletion test/parse.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe('git object parsing', () => {

let expCid = util.shaToCid(Buffer.from(object[0], 'hex'))

ipldGit.util.cid(node, (err, cid) => {
ipldGit.util.cid(object[1], (err, cid) => {
expect(err).to.not.exist()
expect(cid).to.exist()

Expand Down
6 changes: 4 additions & 2 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@ describe('IPLD format util', () => {
},
message: 'A message\n'
}
let tagNodeSerialized

it('.serialize and .deserialize', (done) => {
ipldGit.util.serialize(tagNode, (err, serialized) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(serialized)).to.equal(true)
tagNodeSerialized = serialized
ipldGit.util.deserialize(serialized, (err, deserialized) => {
expect(err).to.not.exist()
expect(tagNode).to.eql(deserialized)
Expand All @@ -36,7 +38,7 @@ describe('IPLD format util', () => {
})

it('.cid', (done) => {
ipldGit.util.cid(tagNode, (err, cid) => {
ipldGit.util.cid(tagNodeSerialized, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('git-raw')
Expand All @@ -48,7 +50,7 @@ describe('IPLD format util', () => {
})

it('.cid ignores options', (done) => {
ipldGit.util.cid(tagNode, { hashAlg: 'unknown' }, (err, cid) => {
ipldGit.util.cid(tagNodeSerialized, { hashAlg: 'unknown' }, (err, cid) => {
expect(err).to.not.exist()
expect(cid.version).to.equal(1)
expect(cid.codec).to.equal('git-raw')
Expand Down