Skip to content

Commit

Permalink
Merge pull request ipfs#532 from ipfs/update-dag-pb
Browse files Browse the repository at this point in the history
chore: upgrade dag-pb to the new, new api
  • Loading branch information
achingbrain committed Sep 25, 2019
2 parents 1a08a28 + 00b577e commit 22b97bc
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 84 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"async": "^2.6.2",
"bl": "^3.0.0",
"bs58": "^4.0.1",
"callbackify": "^1.1.0",
"chai": "^4.2.0",
"cids": "~0.7.1",
"concat-stream": "^2.0.0",
Expand All @@ -50,7 +51,7 @@
"ipfs-unixfs": "~0.1.16",
"ipfs-utils": "~0.1.0",
"ipld-dag-cbor": "~0.15.0",
"ipld-dag-pb": "~0.17.3",
"ipld-dag-pb": "^0.18.1",
"is-ipfs": "~0.6.1",
"is-plain-object": "^3.0.0",
"it-pushable": "^1.2.1",
Expand Down
10 changes: 5 additions & 5 deletions src/dag/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ module.exports = (createCommon, options) => {
const someData = Buffer.from('some other data')

try {
pbNode = DAGNode.create(someData)
pbNode = new DAGNode(someData)
} catch (err) {
return cb(err)
}
Expand All @@ -62,15 +62,15 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
nodePb = DAGNode.create(Buffer.from('I am inside a Protobuf'))
nodePb = new DAGNode(Buffer.from('I am inside a Protobuf'))
} catch (err) {
return cb(err)
}

cb()
},
(cb) => {
dagPB.util.cid(dagPB.util.serialize(nodePb))
dagPB.util.cid(nodePb.serialize())
.then(cid => {
cidPb = cid
cb()
Expand Down Expand Up @@ -139,7 +139,7 @@ module.exports = (createCommon, options) => {

const node = result.value

dagPB.util.cid(dagPB.util.serialize(node))
dagPB.util.cid(node.serialize())
.then(cid => {
expect(cid).to.eql(cidPb)
done()
Expand Down Expand Up @@ -232,7 +232,7 @@ module.exports = (createCommon, options) => {
it('should get a node added as CIDv0 with a CIDv1', done => {
const input = Buffer.from(`TEST${Date.now()}`)

const node = dagPB.DAGNode.create(input)
const node = new DAGNode(input)

ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => {
expect(err).to.not.exist()
Expand Down
2 changes: 1 addition & 1 deletion src/dag/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ module.exports = (createCommon, options) => {
const someData = Buffer.from('some data')

try {
pbNode = DAGNode.create(someData)
pbNode = new DAGNode(someData)
} catch (err) {
return done(err)
}
Expand Down
5 changes: 3 additions & 2 deletions src/dag/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
const series = require('async/series')
const eachSeries = require('async/eachSeries')
const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const dagCBOR = require('ipld-dag-cbor')
const { spawnNodeWithId } = require('../utils/spawn')
const { getDescribe, getIt, expect } = require('../utils/mocha')
Expand Down Expand Up @@ -43,15 +44,15 @@ module.exports = (createCommon, options) => {
series([
(cb) => {
try {
nodePb = dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'))
nodePb = new DAGNode(Buffer.from('I am inside a Protobuf'))
} catch (err) {
return cb(err)
}

cb()
},
(cb) => {
dagPB.util.cid(dagPB.util.serialize(nodePb))
dagPB.util.cid(nodePb.serialize())
.then(cid => {
cidPb = cid
cb()
Expand Down
30 changes: 15 additions & 15 deletions src/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node.Data === 'string') {
node = DAGNode.create(Buffer.from(node.Data), node.Links, node.size)
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
}

node2 = node
Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node2.Data === 'string') {
node2 = DAGNode.create(Buffer.from(node2.Data), node2.Links, node2.size)
node2 = new DAGNode(Buffer.from(node2.Data), node2.Links, node2.size)
}

expect(node1.Data).to.deep.equal(node2.Data)
Expand Down Expand Up @@ -133,7 +133,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node.Data === 'string') {
node = DAGNode.create(Buffer.from(node.Data), node.Links, node.size)
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
}

node2 = node
Expand Down Expand Up @@ -161,7 +161,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node2.Data === 'string') {
node2 = DAGNode.create(Buffer.from(node2.Data), node2.Links, node2.size)
node2 = new DAGNode(Buffer.from(node2.Data), node2.Links, node2.size)
}

expect(node1.Data).to.deep.equal(node2.Data)
Expand All @@ -178,7 +178,7 @@ module.exports = (createCommon, options) => {
series([
(cb) => {
try {
node1a = DAGNode.create(Buffer.from('Some data 1'))
node1a = new DAGNode(Buffer.from('Some data 1'))
} catch (err) {
return cb(err)
}
Expand All @@ -187,7 +187,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
node2 = DAGNode.create(Buffer.from('Some data 2'))
node2 = new DAGNode(Buffer.from('Some data 2'))
} catch (err) {
return cb(err)
}
Expand All @@ -196,13 +196,13 @@ module.exports = (createCommon, options) => {
},
(cb) => {
asDAGLink(node2, 'some-link', (err, link) => {
expect(err).to.not.exist()
if (err) {
return cb(err)
}

DAGNode.addLink(node1a, link)
.then(node => {
node1b = node
cb()
}, cb)
node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))

cb()
})
},
(cb) => {
Expand All @@ -219,7 +219,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node.Data === 'string') {
node = DAGNode.create(Buffer.from(node.Data), node.Links, node.size)
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
}

node1c = node
Expand Down Expand Up @@ -262,7 +262,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node.Data === 'string') {
node = DAGNode.create(Buffer.from(node.Data), node.Links, node.size)
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
}
node1b = node
cb()
Expand Down Expand Up @@ -305,7 +305,7 @@ module.exports = (createCommon, options) => {
// because js-ipfs-api can't infer if the
// returned Data is Buffer or String
if (typeof node.Data === 'string') {
node = DAGNode.create(Buffer.from(node.Data), node.Links, node.size)
node = new DAGNode(Buffer.from(node.Data), node.Links, node.size)
}
node1b = node
cb()
Expand Down
25 changes: 11 additions & 14 deletions src/object/links.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = (createCommon, options) => {
series([
(cb) => {
try {
node1a = DAGNode.create(Buffer.from('Some data 1'))
node1a = new DAGNode(Buffer.from('Some data 1'))
} catch (err) {
return cb(err)
}
Expand All @@ -89,7 +89,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
node2 = DAGNode.create(Buffer.from('Some data 2'))
node2 = new DAGNode(Buffer.from('Some data 2'))
} catch (err) {
return cb(err)
}
Expand All @@ -100,22 +100,19 @@ module.exports = (createCommon, options) => {
asDAGLink(node2, 'some-link', (err, link) => {
expect(err).to.not.exist()

DAGNode.addLink(node1a, link)
.then(node => {
node1b = node
node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))

return dagPB.util.cid(dagPB.util.serialize(node1b))
})
.then(cid => {
node1bCid = cid

cb()
})
.catch(cb)
cb()
})
},
(cb) => {
ipfs.object.put(node1b, cb)
ipfs.object.put(node1b, (err, cid) => {
expect(err).to.not.exist()

node1bCid = cid

cb()
})
},
(cb) => {
ipfs.object.links(node1bCid, (err, links) => {
Expand Down
42 changes: 20 additions & 22 deletions src/object/patch/add-link.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const { getDescribe, getIt, expect } = require('../../utils/mocha')
const {
calculateCid,
createDAGNode,
addLinkToDAGNode
addLinkToDAGNode,
asDAGLink
} = require('../utils')

module.exports = (createCommon, options) => {
Expand Down Expand Up @@ -44,7 +45,6 @@ module.exports = (createCommon, options) => {
let node1a
let node1b
let node2
let node2Cid

const obj = {
Data: Buffer.from('patch test object'),
Expand All @@ -61,7 +61,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
node1a = DAGNode.create(obj.Data, obj.Links)
node1a = new DAGNode(obj.Data, obj.Links)
} catch (err) {
return cb(err)
}
Expand All @@ -70,7 +70,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
node2 = DAGNode.create(Buffer.from('some other node'))
node2 = new DAGNode(Buffer.from('some other node'))
} catch (err) {
return cb(err)
}
Expand All @@ -82,29 +82,27 @@ module.exports = (createCommon, options) => {
// timeout. Reason: it needs the node to get its size
ipfs.object.put(node2, (err, cid) => {
expect(err).to.not.exist()
node2Cid = cid

cb()
})
},
(cb) => {
asDAGLink(node2, 'link-to-node', (err, link) => {
expect(err).to.not.exist()

node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))

cb()
})
},
(cb) => {
DAGNode.addLink(node1a, {
name: 'link-to-node',
size: node2.toJSON().size,
cid: node2Cid
ipfs.object.put(node1b, (err, cid) => {
expect(err).to.not.exist()

node1bCid = cid

cb()
})
.then(node => {
node1b = node

return dagPB.util.cid(dagPB.util.serialize(node), {
cidVersion: 0
})
})
.then(cid => {
node1bCid = cid

cb()
})
.catch(cb)
},
(cb) => {
ipfs.object.patch.addLink(testNodeCid, node1b.Links[0], (err, cid) => {
Expand Down
18 changes: 8 additions & 10 deletions src/object/put.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ module.exports = (createCommon, options) => {
series([
(cb) => {
try {
node = DAGNode.create(Buffer.from(hat()))
node = new DAGNode(Buffer.from(hat()))
} catch (err) {
return cb(err)
}
Expand All @@ -109,7 +109,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
serialized = dagPB.util.serialize(node)
serialized = node.serialize()
} catch (err) {
return cb(err)
}
Expand Down Expand Up @@ -146,7 +146,7 @@ module.exports = (createCommon, options) => {
})

it('should put a Protobuf DAGNode', (done) => {
const dNode = DAGNode.create(Buffer.from(hat()))
const dNode = new DAGNode(Buffer.from(hat()))

ipfs.object.put(dNode, (err, cid) => {
expect(err).to.not.exist()
Expand Down Expand Up @@ -175,7 +175,7 @@ module.exports = (createCommon, options) => {
series([
(cb) => {
try {
node1a = DAGNode.create(Buffer.from(hat()))
node1a = new DAGNode(Buffer.from(hat()))
} catch (err) {
return cb(err)
}
Expand All @@ -184,7 +184,7 @@ module.exports = (createCommon, options) => {
},
(cb) => {
try {
node2 = DAGNode.create(Buffer.from(hat()))
node2 = new DAGNode(Buffer.from(hat()))
} catch (err) {
return cb(err)
}
Expand All @@ -195,11 +195,9 @@ module.exports = (createCommon, options) => {
asDAGLink(node2, 'some-link', (err, link) => {
expect(err).to.not.exist()

DAGNode.addLink(node1a, link)
.then(node => {
node1b = node
cb()
}, cb)
node1b = new DAGNode(node1a.Data, node1a.Links.concat(link))

cb()
})
},
(cb) => {
Expand Down
Loading

0 comments on commit 22b97bc

Please sign in to comment.