From 8afc6b3443d3b7977f98ad73d81664e49ea067f9 Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 26 Jun 2018 13:27:31 +1200 Subject: [PATCH 1/2] fix: pass serialized blob to util.cid BREAKING CHANGE: the first argument is now the serialized output NOT the dag node. See https://github.com/ipld/interface-ipld-format/issues/32 --- src/util.js | 9 +++++---- test/parse.spec.js | 2 +- test/util.spec.js | 6 ++++-- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/util.js b/src/util.js index dd9e8ec..b2708c0 100644 --- a/src/util.js +++ b/src/util.js @@ -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') @@ -77,20 +78,20 @@ exports.deserialize = (data, callback) => { /** * Get the CID of the DAG-Node. * - * @param {Object} dagNode - Internal representation + * @param {Buffer} blob - Serialized binary data * @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) } diff --git a/test/parse.spec.js b/test/parse.spec.js index 4f8df72..a8d590b 100644 --- a/test/parse.spec.js +++ b/test/parse.spec.js @@ -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() diff --git a/test/util.spec.js b/test/util.spec.js index 8f70fd6..1d77420 100644 --- a/test/util.spec.js +++ b/test/util.spec.js @@ -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) @@ -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') @@ -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') From de712a949b606db3665d8ba7d2cdc2bf4965d31f Mon Sep 17 00:00:00 2001 From: Richard Schneider Date: Tue, 26 Jun 2018 21:40:02 +1200 Subject: [PATCH 2/2] docs: improve --- src/util.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util.js b/src/util.js index b2708c0..7a8f6ee 100644 --- a/src/util.js +++ b/src/util.js @@ -76,9 +76,9 @@ 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 {Buffer} blob - Serialized binary data + * @param {Buffer} blob - Serialized Git node * @param {Object} [options] - Ignored * @param {CidCallback} callback - Callback that handles the return value * @returns {void}