diff --git a/package.json b/package.json index 286558f47..7a1e87591 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,7 @@ "bl": "^3.0.0", "bs58": "^4.0.1", "chai": "^4.2.0", - "cids": "~0.5.8", + "cids": "~0.7.1", "concat-stream": "^2.0.0", "dirty-chai": "^2.0.1", "es6-promisify": "^6.0.1", @@ -49,8 +49,8 @@ "ipfs-block": "~0.8.0", "ipfs-unixfs": "~0.1.16", "ipfs-utils": "~0.0.3", - "ipld-dag-cbor": "~0.13.1", - "ipld-dag-pb": "~0.15.3", + "ipld-dag-cbor": "~0.15.0", + "ipld-dag-pb": "~0.17.1", "is-ipfs": "~0.6.0", "is-plain-object": "^3.0.0", "libp2p-crypto": "~0.16.0", diff --git a/src/dag/get.js b/src/dag/get.js index 03855b784..bb9e186ca 100644 --- a/src/dag/get.js +++ b/src/dag/get.js @@ -3,7 +3,7 @@ const { series, eachSeries } = require('async') const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = dagPB const dagCBOR = require('ipld-dag-cbor') const Unixfs = require('ipfs-unixfs') const CID = require('cids') @@ -48,29 +48,21 @@ module.exports = (createCommon, options) => { (cb) => { const someData = Buffer.from('some other data') - DAGNode.create(someData, (err, node) => { - expect(err).to.not.exist() - pbNode = node - cb() - }) + pbNode = DAGNode.create(someData) cborNode = { data: someData } + + nodePb = DAGNode.create(Buffer.from('I am inside a Protobuf')) + + cb() }, (cb) => { - dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'), (err, node) => { - expect(err).to.not.exist() - nodePb = node - cb() - }) - }, - (cb) => { - dagPB.util.cid(nodePb, (err, cid) => { - expect(err).to.not.exist() - cidPb = cid - cb() - }) + dagPB.util.cid(dagPB.util.serialize(nodePb)) + .then(cid => { cidPb = cid }) + .then(cb) + .catch(cb) }, (cb) => { nodeCbor = { @@ -78,11 +70,10 @@ module.exports = (createCommon, options) => { pb: cidPb } - dagCBOR.util.cid(nodeCbor, (err, cid) => { - expect(err).to.not.exist() - cidCbor = cid - cb() - }) + dagCBOR.util.cid(dagCBOR.util.serialize(nodeCbor)) + .then(cid => { cidCbor = cid }) + .then(cb) + .catch(cb) }, (cb) => { eachSeries([ @@ -135,11 +126,10 @@ module.exports = (createCommon, options) => { const node = result.value - dagPB.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.eql(cidPb) - done() - }) + dagPB.util.cid(dagPB.util.serialize(node)) + .then(cid => { expect(cid).to.eql(cidPb) }) + .then(done) + .catch(done) }) }) @@ -160,11 +150,10 @@ module.exports = (createCommon, options) => { const node = result.value - dagCBOR.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.eql(cidCbor) - done() - }) + dagCBOR.util.cid(dagCBOR.util.serialize(node)) + .then(cid => { expect(cid).to.eql(cidCbor) }) + .then(done) + .catch(done) }) }) @@ -196,11 +185,10 @@ module.exports = (createCommon, options) => { const node = result.value - dagCBOR.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid).to.eql(cidCbor) - done() - }) + dagCBOR.util.cid(dagCBOR.util.serialize(node)) + .then(cid => { expect(cid).to.eql(cidCbor) }) + .then(done) + .catch(done) }) }) @@ -224,21 +212,18 @@ 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 = DAGNode.create(input) - dagPB.DAGNode.create(input, (err, node) => { + ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => { expect(err).to.not.exist() + expect(cid.version).to.equal(0) - ipfs.dag.put(node, { format: 'dag-pb', hashAlg: 'sha2-256' }, (err, cid) => { - expect(err).to.not.exist() - expect(cid.version).to.equal(0) - - const cidv1 = cid.toV1() + const cidv1 = cid.toV1() - ipfs.dag.get(cidv1, (err, output) => { - expect(err).to.not.exist() - expect(output.value.data).to.eql(input) - done() - }) + ipfs.dag.get(cidv1, (err, output) => { + expect(err).to.not.exist() + expect(output.value.Data).to.eql(input) + done() }) }) }) @@ -256,7 +241,7 @@ module.exports = (createCommon, options) => { ipfs.dag.get(cidv0, (err, output) => { expect(err).to.not.exist() - expect(Unixfs.unmarshal(output.value.data).data).to.eql(input) + expect(Unixfs.unmarshal(output.value.Data).data).to.eql(input) done() }) }) diff --git a/src/dag/put.js b/src/dag/put.js index 1e4a561c7..15579f72a 100644 --- a/src/dag/put.js +++ b/src/dag/put.js @@ -1,8 +1,7 @@ /* eslint-env mocha */ 'use strict' -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = require('ipld-dag-pb') const dagCBOR = require('ipld-dag-cbor') const CID = require('cids') const multihash = require('multihashes') @@ -38,14 +37,10 @@ module.exports = (createCommon, options) => { let pbNode let cborNode - before((done) => { + before(() => { const someData = Buffer.from('some data') - DAGNode.create(someData, (err, node) => { - expect(err).to.not.exist() - pbNode = node - done() - }) + pbNode = DAGNode.create(someData) cborNode = { data: someData @@ -88,11 +83,10 @@ module.exports = (createCommon, options) => { expect(err).to.not.exist() expect(cid).to.exist() expect(CID.isCID(cid)).to.equal(true) - dagCBOR.util.cid(cborNode, (err, _cid) => { - expect(err).to.not.exist() - expect(cid.buffer).to.eql(_cid.buffer) - done() - }) + dagCBOR.util.cid(dagCBOR.util.serialize(cborNode)) + .then(_cid => { expect(cid.buffer).to.eql(_cid.buffer) }) + .then(done) + .catch(done) }) }) diff --git a/src/dag/tree.js b/src/dag/tree.js index d4444dfdb..8ef48e342 100644 --- a/src/dag/tree.js +++ b/src/dag/tree.js @@ -42,18 +42,12 @@ module.exports = (createCommon, options) => { before(function (done) { series([ (cb) => { - dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf'), (err, node) => { - expect(err).to.not.exist() - nodePb = node - cb() - }) - }, - (cb) => { - dagPB.util.cid(nodePb, (err, cid) => { - expect(err).to.not.exist() - cidPb = cid - cb() - }) + nodePb = dagPB.DAGNode.create(Buffer.from('I am inside a Protobuf')) + + dagPB.util.cid(dagPB.util.serialize(nodePb)) + .then(cid => { cidPb = cid }) + .then(cb) + .catch(cb) }, (cb) => { nodeCbor = { @@ -61,11 +55,10 @@ module.exports = (createCommon, options) => { pb: cidPb } - dagCBOR.util.cid(nodeCbor, (err, cid) => { - expect(err).to.not.exist() - cidCbor = cid - cb() - }) + dagCBOR.util.cid(dagPB.util.serialize(nodeCbor)) + .then(cid => { cidCbor = cid }) + .then(cb) + .catch(cb) }, (cb) => { eachSeries([ diff --git a/src/files-regular/refs-tests.js b/src/files-regular/refs-tests.js index f3f8dbbf1..a79ef24da 100644 --- a/src/files-regular/refs-tests.js +++ b/src/files-regular/refs-tests.js @@ -4,6 +4,7 @@ const mapSeries = require('async/mapSeries') const { getDescribe, getIt, expect } = require('../utils/mocha') const loadFixture = require('aegir/fixtures') +const CID = require('cids') module.exports = (createCommon, suiteName, ipfsRefs, options) => { const describe = getDescribe(options) @@ -365,7 +366,7 @@ function loadDagContent (ipfs, node, callback) { putLinks: (links, cb) => { const obj = {} for (const { name, cid } of links) { - obj[name] = { '/': cid } + obj[name] = new CID(cid) } ipfs.dag.put(obj, cb) } diff --git a/src/object/get.js b/src/object/get.js index 438459ae2..9bac48ec4 100644 --- a/src/object/get.js +++ b/src/object/get.js @@ -1,8 +1,7 @@ /* eslint-env mocha */ 'use strict' -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = require('ipld-dag-pb') const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') @@ -67,15 +66,15 @@ 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.data = Buffer.from(node2.data) + if (typeof node2.Data === 'string') { + node2.Data = Buffer.from(node2.Data) } cb() }) }, (cb) => { - expect(node1.data).to.eql(node2.data) - expect(node1.links).to.eql(node2.links) + expect(node1.Data).to.deep.equal(node2.Data) + expect(node1.Links).to.deep.equal(node2.Links) cb() } ], done) @@ -93,12 +92,12 @@ 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.data = Buffer.from(node2.data) + if (typeof node2.Data === 'string') { + node2.Data = Buffer.from(node2.Data) } - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) + expect(node1.Data).to.deep.equal(node2.Data) + expect(node1.Links).to.deep.equal(node2.Links) }) it('should get object by multihash string', (done) => { @@ -130,16 +129,16 @@ module.exports = (createCommon, options) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the // returned Data is Buffer or String - if (typeof node.data === 'string') { - node.data = Buffer.from(node.data) + if (typeof node.Data === 'string') { + node.Data = Buffer.from(node.Data) } node2 = node cb() }) }, (cb) => { - expect(node1.data).to.eql(node2.data) - expect(node1.links).to.eql(node2.links) + expect(node1.Data).to.deep.equal(node2.Data) + expect(node1.Links).to.deep.equal(node2.Links) cb() } ], done) @@ -157,12 +156,12 @@ 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.data = Buffer.from(node2.data) + if (typeof node2.Data === 'string') { + node2.Data = Buffer.from(node2.Data) } - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) + expect(node1.Data).to.deep.equal(node2.Data) + expect(node1.Links).to.deep.equal(node2.Links) }) it('should get object with links by multihash string', (done) => { @@ -174,31 +173,16 @@ module.exports = (createCommon, options) => { series([ (cb) => { - DAGNode.create(Buffer.from('Some data 1'), (err, node) => { - expect(err).to.not.exist() - node1a = node - - cb() - }) - }, - (cb) => { - DAGNode.create(Buffer.from('Some data 2'), (err, node) => { - expect(err).to.not.exist() - node2 = node - - cb() - }) + node1a = DAGNode.create(Buffer.from('Some data 1')) + node2 = DAGNode.create(Buffer.from('Some data 2')) + cb() }, (cb) => { - asDAGLink(node2, 'some-link', (err, link) => { - expect(err).to.not.exist() - - DAGNode.addLink(node1a, link, (err, node) => { - expect(err).to.not.exist() - node1b = node - cb() - }) - }) + asDAGLink(node2, 'some-link') + .then(link => DAGNode.addLink(node1a, link)) + .then(node => { node1b = node }) + .then(cb) + .catch(cb) }, (cb) => { ipfs.object.put(node1b, (err, cid) => { @@ -213,8 +197,8 @@ 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.data = Buffer.from(node.data) + if (typeof node.Data === 'string') { + node.Data = Buffer.from(node.Data) } node1c = node @@ -222,7 +206,7 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - expect(node1a.data).to.eql(node1c.data) + expect(node1a.Data).to.eql(node1c.Data) cb() } ], done) @@ -257,15 +241,15 @@ 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.data = Buffer.from(node.data) + node.Data = Buffer.from(node.Data) } node1b = node cb() }) }, (cb) => { - expect(node1a.data).to.eql(node1b.data) - expect(node1a.links).to.eql(node1b.links) + expect(node1a.Data).to.eql(node1b.Data) + expect(node1a.Links).to.eql(node1b.Links) cb() } ], done) @@ -299,16 +283,16 @@ module.exports = (createCommon, options) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the // returned Data is Buffer or String - if (typeof node.data === 'string') { - node.data = Buffer.from(node.data) + if (typeof node.Data === 'string') { + node.Data = Buffer.from(node.Data) } node1b = node cb() }) }, (cb) => { - expect(node1a.data).to.eql(node1b.data) - expect(node1a.links).to.eql(node1b.links) + expect(node1a.Data).to.eql(node1b.Data) + expect(node1a.Links).to.eql(node1b.Links) cb() } ], done) @@ -342,7 +326,7 @@ module.exports = (createCommon, options) => { return ipfs.object.get(result[0].hash) }) .then((node) => { - const meta = UnixFs.unmarshal(node.data) + const meta = UnixFs.unmarshal(node.Data) expect(meta.fileSize()).to.equal(data.length) }) diff --git a/src/object/links.js b/src/object/links.js index 04b757597..70038128c 100644 --- a/src/object/links.js +++ b/src/object/links.js @@ -50,7 +50,7 @@ module.exports = (createCommon, options) => { ipfs.object.links(cid, (err, links) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) + expect(node.Links).to.deep.equal(links) done() }) }) @@ -67,46 +67,24 @@ module.exports = (createCommon, options) => { const node = await ipfs.object.get(cid) const links = await ipfs.object.links(cid) - expect(node.links).to.eql(links) + expect(node.Links).to.eql(links) }) it('should get links by multihash', (done) => { - let node1a + const node1a = DAGNode.create(Buffer.from('Some data 1')) let node1b let node1bCid - let node2 + const node2 = DAGNode.create(Buffer.from('Some data 2')) series([ (cb) => { - DAGNode.create(Buffer.from('Some data 1'), (err, node) => { - expect(err).to.not.exist() - node1a = node - - cb() - }) - }, - (cb) => { - DAGNode.create(Buffer.from('Some data 2'), (err, node) => { - expect(err).to.not.exist() - node2 = node - cb() - }) - }, - (cb) => { - asDAGLink(node2, 'some-link', (err, link) => { - expect(err).to.not.exist() - - DAGNode.addLink(node1a, link, (err, node) => { - expect(err).to.not.exist() - node1b = node - - dagPB.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - node1bCid = cid - cb() - }) - }) - }) + asDAGLink(node2, 'some-link') + .then(link => DAGNode.addLink(node1a, link)) + .then(node => { node1b = node }) + .then(() => dagPB.util.cid(dagPB.util.serialize(node1b))) + .then(cid => { node1bCid = cid }) + .then(cb) + .catch(cb) }, (cb) => { ipfs.object.put(node1b, (cb)) @@ -114,7 +92,11 @@ module.exports = (createCommon, options) => { (cb) => { ipfs.object.links(node1bCid, (err, links) => { expect(err).to.not.exist() - expect(node1b.links[0].toJSON()).to.eql(links[0].toJSON()) + expect({ + cid: node1b.Links[0].Hash.toString(), + name: node1b.Links[0].Name, + size: node1b.Links[0].Tsize + }).to.eql(links[0].toJSON()) cb() }) } @@ -135,7 +117,7 @@ module.exports = (createCommon, options) => { ipfs.object.links(cid.buffer, { enc: 'base58' }, (err, links) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) + expect(node.Links).to.deep.equal(links) done() }) }) @@ -156,7 +138,7 @@ module.exports = (createCommon, options) => { ipfs.object.links(cid.toBaseEncodedString(), { enc: 'base58' }, (err, links) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) + expect(node.Links).to.deep.equal(links) done() }) }) diff --git a/src/object/patch/add-link.js b/src/object/patch/add-link.js index 5d8fc86ac..766a1ffb4 100644 --- a/src/object/patch/add-link.js +++ b/src/object/patch/add-link.js @@ -2,14 +2,9 @@ 'use strict' const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = dagPB const series = require('async/series') const { getDescribe, getIt, expect } = require('../../utils/mocha') -const { - calculateCid, - createDAGNode, - addLinkToDAGNode -} = require('../utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -39,18 +34,18 @@ module.exports = (createCommon, options) => { after((done) => common.teardown(done)) it('should add a link to an existing node', (done) => { - let testNodeCid - let node1bCid - let node1a - let node1b - let node2 - let node2Cid - const obj = { Data: Buffer.from('patch test object'), Links: [] } + let testNodeCid + let node1bCid + const node1a = DAGNode.create(obj.Data, obj.Links) + let node1b + const node2 = DAGNode.create(Buffer.from('some other node')) + let node2Cid + series([ (cb) => { ipfs.object.put(obj, (err, cid) => { @@ -59,20 +54,6 @@ module.exports = (createCommon, options) => { cb() }) }, - (cb) => { - DAGNode.create(obj.Data, obj.Links, (err, node) => { - expect(err).to.not.exist() - node1a = node - cb() - }) - }, - (cb) => { - DAGNode.create(Buffer.from('some other node'), (err, node) => { - expect(err).to.not.exist() - node2 = node - cb() - }) - }, (cb) => { // note: we need to put the linked obj, otherwise IPFS won't // timeout. Reason: it needs the node to get its size @@ -83,23 +64,20 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - DAGNode.addLink(node1a, { - name: 'link-to-node', - size: node2.toJSON().size, - cid: node2Cid - }, (err, node) => { - expect(err).to.not.exist() - node1b = node - - dagPB.util.cid(node, (err, cid) => { - expect(err).to.not.exist() - node1bCid = cid - cb() + DAGNode + .addLink(node1a, { + name: 'link-to-node', + size: node2.toJSON().size, + cid: node2Cid }) - }) + .then(node => { node1b = node }) + .then(() => dagPB.util.cid(dagPB.util.serialize(node1b), { cidVersion: 0 })) + .then(cid => { node1bCid = cid }) + .then(cb) + .catch(cb) }, (cb) => { - ipfs.object.patch.addLink(testNodeCid, node1b.links[0], (err, cid) => { + ipfs.object.patch.addLink(testNodeCid, node1b.Links[0], (err, cid) => { expect(err).to.not.exist() expect(node1bCid).to.eql(cid) cb() @@ -145,15 +123,15 @@ module.exports = (createCommon, options) => { const parentCid = await ipfs.object.put(obj) const parent = await ipfs.object.get(parentCid) - const childCid = await ipfs.object.put(await createDAGNode(Buffer.from('some other node'), [])) + const childCid = await ipfs.object.put(DAGNode.create(Buffer.from('some other node'))) const child = await ipfs.object.get(childCid) - const newParent = await addLinkToDAGNode(parent, { + const newParent = await DAGNode.addLink(parent, { name: 'link-to-node', size: child.size, cid: childCid }) - const newParentCid = await calculateCid(newParent) - const nodeFromObjectPatchCid = await ipfs.object.patch.addLink(parentCid, newParent.links[0]) + const newParentCid = await dagPB.util.cid(dagPB.util.serialize(newParent), { cidVersion: 0 }) + const nodeFromObjectPatchCid = await ipfs.object.patch.addLink(parentCid, newParent.Links[0]) expect(newParentCid).to.eql(nodeFromObjectPatchCid) }) diff --git a/src/object/patch/set-data.js b/src/object/patch/set-data.js index 6f7c58e82..621fc43e4 100644 --- a/src/object/patch/set-data.js +++ b/src/object/patch/set-data.js @@ -47,7 +47,7 @@ module.exports = (createCommon, options) => { ipfs.object.get(patchedNodeCid, (err, patchedNode) => { expect(err).to.not.exist() - expect(patchedNode.data).to.eql(patchData) + expect(patchedNode.Data).to.eql(patchData) done() }) }) @@ -66,7 +66,7 @@ module.exports = (createCommon, options) => { const patchedNode = await ipfs.object.get(patchedNodeCid) expect(nodeCid).to.not.deep.equal(patchedNodeCid) - expect(patchedNode.data).to.eql(patchData) + expect(patchedNode.Data).to.eql(patchData) }) }) } diff --git a/src/object/put.js b/src/object/put.js index b297e0cc3..0c2cf47b5 100644 --- a/src/object/put.js +++ b/src/object/put.js @@ -2,7 +2,7 @@ 'use strict' const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = dagPB const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') @@ -87,43 +87,25 @@ module.exports = (createCommon, options) => { ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() const nodeJSON = node.toJSON() - expect(nodeJSON.data).to.eql(node.data) + expect(nodeJSON.data).to.eql(node.Data) done() }) }) }) it('should put a Protobuf encoded Buffer', (done) => { - let node - let serialized + let node = DAGNode.create(Buffer.from(hat())) + let serialized = dagPB.util.serialize(node) - series([ - (cb) => { - DAGNode.create(Buffer.from(hat()), (err, _node) => { - expect(err).to.not.exist() - node = _node - cb() - }) - }, - (cb) => { - dagPB.util.serialize(node, (err, _serialized) => { - expect(err).to.not.exist() - serialized = _serialized - cb() - }) - }, - (cb) => { - ipfs.object.put(serialized, { enc: 'protobuf' }, (err, cid) => { - expect(err).to.not.exist() - ipfs.object.get(cid, (err, node) => { - expect(err).to.not.exist() - expect(node.data).to.deep.equal(node.data) - expect(node.links).to.deep.equal(node.links) - cb() - }) - }) - } - ], done) + ipfs.object.put(serialized, { enc: 'protobuf' }, (err, cid) => { + expect(err).to.not.exist() + ipfs.object.get(cid, (err, node) => { + expect(err).to.not.exist() + expect(node.Data).to.deep.equal(node.Data) + expect(node.Links).to.deep.equal(node.Links) + done() + }) + }) }) it('should put a Buffer as data', (done) => { @@ -142,17 +124,16 @@ module.exports = (createCommon, options) => { }) it('should put a Protobuf DAGNode', (done) => { - DAGNode.create(Buffer.from(hat()), (err, dNode) => { + const dNode = DAGNode.create(Buffer.from(hat())) + + ipfs.object.put(dNode, (err, cid) => { expect(err).to.not.exist() - ipfs.object.put(dNode, (err, cid) => { - expect(err).to.not.exist() - ipfs.object.get(cid, (err, node) => { - expect(err).to.not.exist() - expect(dNode.data).to.deep.equal(node.data) - expect(dNode.links).to.deep.equal(node.links) - done() - }) + ipfs.object.get(cid, (err, node) => { + expect(err).to.not.exist() + expect(dNode.Data).to.deep.equal(node.Data) + expect(dNode.Links).to.deep.equal(node.Links) + done() }) }) }) @@ -165,35 +146,17 @@ module.exports = (createCommon, options) => { }) it('should put a Protobuf DAGNode with a link', (done) => { - let node1a + const node1a = DAGNode.create(Buffer.from(hat())) let node1b - let node2 + const node2 = DAGNode.create(Buffer.from(hat())) series([ (cb) => { - DAGNode.create(Buffer.from(hat()), (err, node) => { - expect(err).to.not.exist() - node1a = node - cb() - }) - }, - (cb) => { - DAGNode.create(Buffer.from(hat()), (err, node) => { - expect(err).to.not.exist() - node2 = node - cb() - }) - }, - (cb) => { - asDAGLink(node2, 'some-link', (err, link) => { - expect(err).to.not.exist() - - DAGNode.addLink(node1a, link, (err, node) => { - expect(err).to.not.exist() - node1b = node - cb() - }) - }) + asDAGLink(node2, 'some-link') + .then(link => DAGNode.addLink(node1a, link)) + .then(node => { node1b = node }) + .then(cb) + .catch(cb) }, (cb) => { ipfs.object.put(node1b, (err, cid) => { @@ -201,9 +164,8 @@ module.exports = (createCommon, options) => { ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - expect(node1b.data).to.deep.equal(node.data) - expect(node1b.links.map((l) => l.toJSON())) - .to.deep.equal(node.links.map((l) => l.toJSON())) + expect(node1b.Data).to.deep.equal(node.Data) + expect(node1b.Links).to.deep.equal(node.Links) cb() }) }) diff --git a/src/object/stat.js b/src/object/stat.js index 6e1e867a2..7d74e57f6 100644 --- a/src/object/stat.js +++ b/src/object/stat.js @@ -2,8 +2,7 @@ /* eslint-disable max-nested-callbacks */ 'use strict' -const dagPB = require('ipld-dag-pb') -const DAGNode = dagPB.DAGNode +const { DAGNode } = require('ipld-dag-pb') const series = require('async/series') const { getDescribe, getIt, expect } = require('../utils/mocha') const { asDAGLink } = require('./utils') @@ -82,38 +81,18 @@ module.exports = (createCommon, options) => { }) it('should get stats for object with links by multihash', (done) => { - let node1a + const node1a = DAGNode.create(Buffer.from('Some data 1')) let node1b let node1bCid - let node2 + let node2 = DAGNode.create(Buffer.from('Some data 2')) series([ (cb) => { - DAGNode.create(Buffer.from('Some data 1'), (err, node) => { - expect(err).to.not.exist() - node1a = node - cb() - }) - }, - (cb) => { - DAGNode.create(Buffer.from('Some data 2'), (err, node) => { - expect(err).to.not.exist() - node2 = node - cb() - }) - }, - (cb) => { - asDAGLink(node2, 'some-link', (err, link) => { - expect(err).to.not.exist() - - DAGNode.addLink(node1a, link, (err, node) => { - expect(err).to.not.exist() - - node1b = node - - cb() - }) - }) + asDAGLink(node2, 'some-link') + .then(link => DAGNode.addLink(node1a, link)) + .then(node => { node1b = node }) + .then(cb) + .catch(cb) }, (cb) => { ipfs.object.put(node1b, (err, cid) => { diff --git a/src/object/utils.js b/src/object/utils.js index fb122a7d4..3b99cbdcf 100644 --- a/src/object/utils.js +++ b/src/object/utils.js @@ -1,32 +1,11 @@ 'use strict' -const { promisify } = require('es6-promisify') const dagPB = require('ipld-dag-pb') -const { DAGNode, DAGLink } = dagPB +const { DAGLink } = dagPB -module.exports.calculateCid = promisify((node, cb) => { - dagPB.util.cid(node, cb) -}) +module.exports.asDAGLink = async (node, name) => { + name = name || '' -module.exports.createDAGNode = promisify((data, links, cb) => { - DAGNode.create(data, links, cb) -}) - -module.exports.addLinkToDAGNode = promisify((parent, link, cb) => { - DAGNode.addLink(parent, link, cb) -}) - -module.exports.asDAGLink = promisify((node, name, cb) => { - if (typeof name === 'function') { - cb = name - name = '' - } - - dagPB.util.cid(node, (err, nodeCid) => { - if (err) { - return cb(err) - } - - DAGLink.create(name, node.size, nodeCid, cb) - }) -}) + const nodeCid = await dagPB.util.cid(dagPB.util.serialize(node), { cidVersion: 0 }) + return new DAGLink(name, node.size, nodeCid) +}