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

Commit 8585d65

Browse files
mikealvmx
authored andcommitted
feat: serialize and de-serialize CID instances
BREAKING CHANGE: return values from de-serializer are now CID instances. Serializer still supports old link objects.
1 parent 0a49705 commit 8585d65

5 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/resolver.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const util = require('./util')
44
const traverse = require('traverse')
5+
const CID = require('cids')
56

67
exports = module.exports
78

@@ -80,7 +81,7 @@ function flattenObject (obj, delimiter) {
8081
}
8182

8283
return traverse(obj).reduce(function (acc, x) {
83-
if (typeof x === 'object' && x['/']) {
84+
if (CID.isCID(x)) {
8485
this.update(undefined)
8586
}
8687
const path = this.path.join(delimiter)
@@ -125,7 +126,7 @@ exports.isLink = (binaryBlob, path, callback) => {
125126
return callback(new Error('path out of scope'))
126127
}
127128

128-
if (typeof result.value === 'object' && result.value['/']) {
129+
if (CID.isCID(result.value)) {
129130
callback(null, result.value)
130131
} else {
131132
callback(null, false)

src/util.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ const CID_CBOR_TAG = 42
1515
function tagCID (cid) {
1616
if (typeof cid === 'string') {
1717
cid = new CID(cid).buffer
18+
} else if (CID.isCID(cid)) {
19+
cid = cid.buffer
1820
}
1921

2022
return new cbor.Tagged(CID_CBOR_TAG, Buffer.concat([
@@ -28,7 +30,7 @@ const decoder = new cbor.Decoder({
2830
[CID_CBOR_TAG]: (val) => {
2931
// remove that 0
3032
val = val.slice(1)
31-
return {'/': val}
33+
return new CID(val)
3234
}
3335
}
3436
})
@@ -53,9 +55,12 @@ function replaceCIDbyTAG (dagNode) {
5355
return obj.map(transform)
5456
}
5557

58+
if (CID.isCID(obj)) {
59+
return tagCID(obj)
60+
}
61+
5662
const keys = Object.keys(obj)
5763

58-
// only `{'/': 'link'}` are valid
5964
if (keys.length === 1 && keys[0] === '/') {
6065
// Multiaddr encoding
6166
// if (typeof link === 'string' && isMultiaddr(link)) {

test/interop.spec.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ const dagCBOR = require('../src')
1111
const loadFixture = require('aegir/fixtures')
1212
const bs58 = require('bs58')
1313
const isNode = require('detect-node')
14+
const CID = require('cids')
1415

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

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

46-
node[0]['/'] = bs58Str
47-
expect(node).to.eql(arrayLinkJSON)
48-
49-
// put it back to bytes
50-
node[0]['/'] = bs58.decode(arrayLinkJSON[0]['/'])
48+
expect(bs58Str).to.eql(arrayLink[0].toBaseEncodedString())
5149

5250
dagCBOR.util.cid(node, (err, cid) => {
5351
expect(err).to.not.exist()
@@ -94,7 +92,7 @@ describe('dag-cbor interop tests', () => {
9492
dagCBOR.util.cid(node, (err, cid) => {
9593
expect(err).to.not.exist()
9694
const cidStr = cid.toBaseEncodedString()
97-
expect(cidStr).to.eql(expectedCIDs['foo']['/'])
95+
expect(cidStr).to.eql(expectedCIDs.foo['/'])
9896
done()
9997
})
10098
})

test/resolver.spec.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ describe('IPLD format resolver (local)', () => {
102102
it('resolver.isLink with valid Link', (done) => {
103103
resolver.isLink(nodeBlob, 'someLink', (err, link) => {
104104
expect(err).to.not.exist()
105-
const linkCID = new CID(link['/'])
106-
expect(CID.isCID(linkCID)).to.equal(true)
105+
expect(CID.isCID(link)).to.equal(true)
107106
done()
108107
})
109108
})
@@ -157,9 +156,7 @@ describe('IPLD format resolver (local)', () => {
157156
it('path out of scope', (done) => {
158157
resolver.resolve(nodeBlob, 'someLink/a/b/c', (err, result) => {
159158
expect(err).to.not.exist()
160-
expect(result.value).to.eql({
161-
'/': new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE').buffer
162-
})
159+
expect(result.value).to.eql(new CID('QmaNh5d3hFiqJAGjHmvxihSnWDGqYZCn7H2XHpbttYjCNE'))
163160
expect(result.remainderPath).to.equal('a/b/c')
164161
done()
165162
})

test/util.spec.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ const garbage = require('garbage')
99
const map = require('async/map')
1010
const dagCBOR = require('../src')
1111
const multihash = require('multihashes')
12+
const CID = require('cids')
1213

1314
describe('util', () => {
1415
const obj = {
1516
someKey: 'someValue',
16-
link: { '/': Buffer.from('aaaaa') },
17+
link: new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
1718
links: [
18-
{ '/': Buffer.from('1a') },
19-
{ '/': Buffer.from('2b') }
19+
new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL'),
20+
new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
2021
],
2122
nested: {
2223
hello: 'world',
23-
link: { '/': Buffer.from('mylink') }
24+
link: new CID('QmRgutAxd8t7oGkSm4wmeuByG6M51wcTso6cubDdQtuEfL')
2425
}
2526
}
2627

0 commit comments

Comments
 (0)