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

Commit

Permalink
feat: serialize and de-serialize CID instances
Browse files Browse the repository at this point in the history
BREAKING CHANGE: return values from de-serializer are now CID instances.
Serializer still supports old link objects.
  • Loading branch information
mikeal authored and vmx committed Sep 24, 2018
1 parent 0a49705 commit 8585d65
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 20 deletions.
5 changes: 3 additions & 2 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const util = require('./util')
const traverse = require('traverse')
const CID = require('cids')

exports = module.exports

Expand Down Expand Up @@ -80,7 +81,7 @@ function flattenObject (obj, delimiter) {
}

return traverse(obj).reduce(function (acc, x) {
if (typeof x === 'object' && x['/']) {
if (CID.isCID(x)) {
this.update(undefined)
}
const path = this.path.join(delimiter)
Expand Down Expand Up @@ -125,7 +126,7 @@ exports.isLink = (binaryBlob, path, callback) => {
return callback(new Error('path out of scope'))
}

if (typeof result.value === 'object' && result.value['/']) {
if (CID.isCID(result.value)) {
callback(null, result.value)
} else {
callback(null, false)
Expand Down
9 changes: 7 additions & 2 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const CID_CBOR_TAG = 42
function tagCID (cid) {
if (typeof cid === 'string') {
cid = new CID(cid).buffer
} else if (CID.isCID(cid)) {
cid = cid.buffer
}

return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
Expand All @@ -28,7 +30,7 @@ const decoder = new cbor.Decoder({
[CID_CBOR_TAG]: (val) => {
// remove that 0
val = val.slice(1)
return {'/': val}
return new CID(val)
}
}
})
Expand All @@ -53,9 +55,12 @@ function replaceCIDbyTAG (dagNode) {
return obj.map(transform)
}

if (CID.isCID(obj)) {
return tagCID(obj)
}

const keys = Object.keys(obj)

// only `{'/': 'link'}` are valid
if (keys.length === 1 && keys[0] === '/') {
// Multiaddr encoding
// if (typeof link === 'string' && isMultiaddr(link)) {
Expand Down
12 changes: 5 additions & 7 deletions test/interop.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ const dagCBOR = require('../src')
const loadFixture = require('aegir/fixtures')
const bs58 = require('bs58')
const isNode = require('detect-node')
const CID = require('cids')

const arrayLinkCBOR = loadFixture('test/fixtures/array-link.cbor')
const arrayLinkJSON = require('./fixtures/array-link.json')
const arrayLink = arrayLinkJSON.map(x => new CID(x['/']))

const emptyArrayCBOR = loadFixture('test/fixtures/empty-array.cbor')
const emptyArrayJSON = require('./fixtures/empty-array.json')
Expand Down Expand Up @@ -41,13 +43,9 @@ describe('dag-cbor interop tests', () => {
dagCBOR.util.deserialize(arrayLinkCBOR, (err, node) => {
expect(err).to.not.exist()
// the JSON version that gets out of go-ipfs stringifies the CID
const bs58Str = bs58.encode(node[0]['/'])
const bs58Str = node[0].toBaseEncodedString()

node[0]['/'] = bs58Str
expect(node).to.eql(arrayLinkJSON)

// put it back to bytes
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])
expect(bs58Str).to.eql(arrayLink[0].toBaseEncodedString())

dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
Expand Down Expand Up @@ -94,7 +92,7 @@ describe('dag-cbor interop tests', () => {
dagCBOR.util.cid(node, (err, cid) => {
expect(err).to.not.exist()
const cidStr = cid.toBaseEncodedString()
expect(cidStr).to.eql(expectedCIDs['foo']['/'])
expect(cidStr).to.eql(expectedCIDs.foo['/'])
done()
})
})
Expand Down
7 changes: 2 additions & 5 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,7 @@ describe('IPLD format resolver (local)', () => {
it('resolver.isLink with valid Link', (done) => {
resolver.isLink(nodeBlob, 'someLink', (err, link) => {
expect(err).to.not.exist()
const linkCID = new CID(link['/'])
expect(CID.isCID(linkCID)).to.equal(true)
expect(CID.isCID(link)).to.equal(true)
done()
})
})
Expand Down Expand Up @@ -157,9 +156,7 @@ describe('IPLD format resolver (local)', () => {
it('path out of scope', (done) => {
resolver.resolve(nodeBlob, 'someLink/a/b/c', (err, result) => {
expect(err).to.not.exist()
expect(result.value).to.eql({
'/': new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE').buffer
})
expect(result.value).to.eql(new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE'))
expect(result.remainderPath).to.equal('a/b/c')
done()
})
Expand Down
9 changes: 5 additions & 4 deletions test/util.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@ const garbage = require('garbage')
const map = require('async/map')
const dagCBOR = require('../src')
const multihash = require('multihashes')
const CID = require('cids')

describe('util', () => {
const obj = {
someKey: 'someValue',
link: { '/': Buffer.from('aaaaa') },
link: new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
links: [
{ '/': Buffer.from('1a') },
{ '/': Buffer.from('2b') }
new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
],
nested: {
hello: 'world',
link: { '/': Buffer.from('mylink') }
link: new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
}
}

Expand Down

0 comments on commit 8585d65

Please sign in to comment.