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 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
11 changes: 6 additions & 5 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 @@ -75,22 +76,22 @@ exports.deserialize = (data, callback) => {
* @param {?CID} cid - CID if call was successful
*/
/**
* Get the CID of the DAG-Node.
* Get the CID of the serialized Git node.
*
* @param {Object} dagNode - Internal representation
* @param {Buffer} blob - Serialized Git node
* @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