diff --git a/js/src/dag/get.js b/js/src/dag/get.js index 0efdcd0f..35f211ac 100644 --- a/js/src/dag/get.js +++ b/js/src/dag/get.js @@ -46,7 +46,7 @@ module.exports = (createCommon, options) => { (cb) => { const someData = Buffer.from('some other data') - pbNode = DAGNode.create(someData, (err, node) => { + DAGNode.create(someData, (err, node) => { expect(err).to.not.exist() pbNode = node cb() diff --git a/js/src/dag/put.js b/js/src/dag/put.js index f5996fff..1e4a561c 100644 --- a/js/src/dag/put.js +++ b/js/src/dag/put.js @@ -41,7 +41,7 @@ module.exports = (createCommon, options) => { before((done) => { const someData = Buffer.from('some data') - pbNode = DAGNode.create(someData, (err, node) => { + DAGNode.create(someData, (err, node) => { expect(err).to.not.exist() pbNode = node done() diff --git a/js/src/object/data.js b/js/src/object/data.js index ddc94ee8..e0a58ba9 100644 --- a/js/src/object/data.js +++ b/js/src/object/data.js @@ -1,9 +1,13 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const bs58 = require('bs58') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') +const { + calculateCid +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -41,36 +45,40 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.data(node.multihash, (err, data) => { + calculateCid(node, (err, nodeCid) => { expect(err).to.not.exist() - // because js-ipfs-api can't infer - // if the returned Data is Buffer or String - if (typeof data === 'string') { - data = Buffer.from(data) - } - expect(node.data).to.eql(data) - done() + ipfs.object.data(nodeCid, (err, data) => { + expect(err).to.not.exist() + + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(node.data).to.eql(data) + done() + }) }) }) }) - it('should get data by multihash (promised)', () => { + it('should get data by multihash (promised)', async () => { const testObj = { Data: Buffer.from(hat()), Links: [] } - return ipfs.object.put(testObj).then((node) => { - return ipfs.object.data(node.multihash).then((data) => { - // because js-ipfs-api can't infer - // if the returned Data is Buffer or String - if (typeof data === 'string') { - data = Buffer.from(data) - } - expect(node.data).to.deep.equal(data) - }) - }) + const node = await ipfs.object.put(testObj) + const nodeCid = await calculateCid(node) + let data = await ipfs.object.data(nodeCid) + + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(node.data).to.deep.equal(data) }) it('should get data by base58 encoded multihash', (done) => { @@ -82,16 +90,20 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.data(bs58.encode(node.multihash), { enc: 'base58' }, (err, data) => { + calculateCid(node, (err, nodeCid) => { expect(err).to.not.exist() - // because js-ipfs-api can't infer - // if the returned Data is Buffer or String - if (typeof data === 'string') { - data = Buffer.from(data) - } - expect(node.data).to.eql(data) - done() + ipfs.object.data(bs58.encode(nodeCid.buffer), { enc: 'base58' }, (err, data) => { + expect(err).to.not.exist() + + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(node.data).to.eql(data) + done() + }) }) }) }) @@ -105,16 +117,20 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.data(bs58.encode(node.multihash).toString(), { enc: 'base58' }, (err, data) => { + calculateCid(node, (err, nodeCid) => { expect(err).to.not.exist() - // because js-ipfs-api can't infer if the - // returned Data is Buffer or String - if (typeof data === 'string') { - data = Buffer.from(data) - } - expect(node.data).to.eql(data) - done() + ipfs.object.data(bs58.encode(nodeCid.buffer).toString(), { enc: 'base58' }, (err, data) => { + expect(err).to.not.exist() + + // because js-ipfs-api can't infer if the + // returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(node.data).to.eql(data) + done() + }) }) }) }) diff --git a/js/src/object/get.js b/js/src/object/get.js index 4040d152..eabdd711 100644 --- a/js/src/object/get.js +++ b/js/src/object/get.js @@ -3,12 +3,15 @@ const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode -const bs58 = require('bs58') const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') const UnixFs = require('ipfs-unixfs') const crypto = require('crypto') +const { + calculateCid, + asDAGLink +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -44,6 +47,7 @@ module.exports = (createCommon, options) => { } let node1 + let node1Cid let node2 series([ @@ -51,11 +55,18 @@ module.exports = (createCommon, options) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() node1 = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1Cid = result + + cb() + }) }) }, (cb) => { - ipfs.object.get(node1.multihash, (err, node) => { + ipfs.object.get(node1Cid, (err, node) => { expect(err).to.not.exist() node2 = node @@ -70,30 +81,29 @@ module.exports = (createCommon, options) => { (cb) => { expect(node1.data).to.eql(node2.data) expect(node1.links).to.eql(node2.links) - expect(node1.multihash).to.eql(node2.multihash) cb() } ], done) }) - it('should get object by multihash (promised)', () => { + it('should get object by multihash (promised)', async () => { const testObj = { Data: Buffer.from(hat()), Links: [] } - return ipfs.object.put(testObj).then((node1) => { - return ipfs.object.get(node1.multihash).then((node2) => { - // 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) - } + const node1 = await ipfs.object.put(testObj) + const node1Cid = await calculateCid(node1) + const node2 = await ipfs.object.get(node1Cid) - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) - }) - }) + // 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) + } + + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) }) it('should get object by multihash string', (done) => { @@ -103,19 +113,28 @@ module.exports = (createCommon, options) => { } let node1 + let node1Cid let node2 + let node2Cid series([ (cb) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() node1 = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1Cid = result + + cb() + }) }) }, (cb) => { // get object from ipfs multihash string - ipfs.object.get(node1.toJSON().multihash, (err, node) => { + ipfs.object.get(node1Cid.toBaseEncodedString(), (err, node) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the // returned Data is Buffer or String @@ -123,44 +142,52 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node2 = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node2Cid = result + + cb() + }) }) }, (cb) => { expect(node1.data).to.eql(node2.data) expect(node1.links).to.eql(node2.links) - expect(node1.multihash).to.eql(node2.multihash) + expect(node1Cid).to.deep.eql(node2Cid) cb() } ], done) }) - it('should get object by multihash string (promised)', () => { + it('should get object by multihash string (promised)', async () => { const obj = { Data: Buffer.from(hat()), Links: [] } - return ipfs.object.put(obj) - .then((node1) => { - return ipfs.object.get(node1.toJSON().multihash) - .then((node2) => { - // 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) - } - - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) - }) - }) + const node1 = await ipfs.object.put(obj) + const node1Cid = await calculateCid(node1) + const node2 = await ipfs.object.get(node1Cid.toBaseEncodedString()) + const node2Cid = await calculateCid(node2) + // 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) + } + + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) + expect(node1Cid).to.deep.eql(node2Cid) }) it('should get object with links by multihash string', (done) => { let node1a let node1b + let node1bCid let node1c + let node1cCid let node2 series([ @@ -168,6 +195,7 @@ module.exports = (createCommon, options) => { DAGNode.create(Buffer.from('Some data 1'), (err, node) => { expect(err).to.not.exist() node1a = node + cb() }) }, @@ -175,23 +203,36 @@ module.exports = (createCommon, options) => { DAGNode.create(Buffer.from('Some data 2'), (err, node) => { expect(err).to.not.exist() node2 = node + cb() }) }, (cb) => { - const link = node2.toJSON() - link.name = 'some-link' - DAGNode.addLink(node1a, link, (err, node) => { + asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - node1b = node - cb() + + DAGNode.addLink(node1a, link, (err, node) => { + expect(err).to.not.exist() + node1b = node + cb() + }) }) }, (cb) => { - ipfs.object.put(node1b, cb) + ipfs.object.put(node1b, (err, node) => { + expect(err).to.not.exist() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1bCid = result + + cb() + }) + }) }, (cb) => { - ipfs.object.get(node1b.multihash, (err, node) => { + ipfs.object.get(node1bCid, (err, node) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the @@ -201,12 +242,19 @@ module.exports = (createCommon, options) => { } node1c = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1cCid = result + + cb() + }) }) }, (cb) => { expect(node1a.data).to.eql(node1c.data) - expect(node1b.multihash).to.eql(node1c.multihash) + expect(node1bCid).to.eql(node1cCid) cb() } ], done) @@ -219,18 +267,27 @@ module.exports = (createCommon, options) => { } let node1a + let node1aCid let node1b + let node1bCid series([ (cb) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() node1a = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1aCid = result + + cb() + }) }) }, (cb) => { - ipfs.object.get(node1a.multihash, { enc: 'base58' }, (err, node) => { + ipfs.object.get(node1aCid, { enc: 'base58' }, (err, node) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the // returned Data is Buffer or String @@ -238,11 +295,18 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node1b = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1bCid = result + + cb() + }) }) }, (cb) => { - expect(node1a.multihash).to.eql(node1b.multihash) + expect(node1aCid).to.deep.eql(node1bCid) expect(node1a.data).to.eql(node1b.data) expect(node1a.links).to.eql(node1b.links) cb() @@ -257,18 +321,27 @@ module.exports = (createCommon, options) => { } let node1a + let node1aCid let node1b + let node1bCid series([ (cb) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() node1a = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1aCid = result + + cb() + }) }) }, (cb) => { - ipfs.object.get(bs58.encode(node1a.multihash).toString(), { enc: 'base58' }, (err, node) => { + ipfs.object.get(node1aCid.toBaseEncodedString(), { enc: 'base58' }, (err, node) => { expect(err).to.not.exist() // because js-ipfs-api can't infer if the // returned Data is Buffer or String @@ -276,11 +349,18 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node1b = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1bCid = result + + cb() + }) }) }, (cb) => { - expect(node1a.multihash).to.eql(node1b.multihash) + expect(node1aCid).to.deep.eql(node1bCid) expect(node1a.data).to.eql(node1b.data) expect(node1a.links).to.eql(node1b.links) cb() diff --git a/js/src/object/links.js b/js/src/object/links.js index 18617b09..a86dbc5b 100644 --- a/js/src/object/links.js +++ b/js/src/object/links.js @@ -1,12 +1,16 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode -const bs58 = require('bs58') const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') +const { + calculateCid, + asDAGLink +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -44,30 +48,35 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.links(node.multihash, (err, links) => { + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) - done() + + ipfs.object.links(cid, (err, links) => { + expect(err).to.not.exist() + expect(node.links).to.deep.equal(links) + done() + }) }) }) }) - it('should get empty links by multihash (promised)', () => { + it('should get empty links by multihash (promised)', async () => { const testObj = { Data: Buffer.from(hat()), Links: [] } - return ipfs.object.put(testObj).then((node) => { - return ipfs.object.links(node.multihash).then((links) => { - expect(node.links).to.eql(links) - }) - }) + const node = await ipfs.object.put(testObj) + const cid = await calculateCid(node) + const links = await ipfs.object.links(cid) + + expect(node.links).to.eql(links) }) it('should get links by multihash', (done) => { let node1a let node1b + let node1bCid let node2 series([ @@ -75,6 +84,7 @@ module.exports = (createCommon, options) => { DAGNode.create(Buffer.from('Some data 1'), (err, node) => { expect(err).to.not.exist() node1a = node + cb() }) }, @@ -86,20 +96,28 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - const link = node2.toJSON() - link.name = 'some-link' - - DAGNode.addLink(node1a, link, (err, node) => { + asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - node1b = node - cb() + + DAGNode.addLink(node1a, link, (err, node) => { + expect(err).to.not.exist() + node1b = node + + calculateCid(node, (err, cid) => { + expect(err).to.not.exist() + + node1bCid = cid + + cb() + }) + }) }) }, (cb) => { - ipfs.object.put(node1b, cb) + ipfs.object.put(node1b, (cb)) }, (cb) => { - ipfs.object.links(node1b.multihash, (err, links) => { + ipfs.object.links(node1bCid, (err, links) => { expect(err).to.not.exist() expect(node1b.links[0].toJSON()).to.eql(links[0].toJSON()) cb() @@ -117,10 +135,14 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.links(bs58.encode(node.multihash), { enc: 'base58' }, (err, links) => { + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) - done() + + ipfs.object.links(cid.buffer, { enc: 'base58' }, (err, links) => { + expect(err).to.not.exist() + expect(node.links).to.deep.equal(links) + done() + }) }) }) }) @@ -133,10 +155,15 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.links(bs58.encode(node.multihash), { enc: 'base58' }, (err, links) => { + + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - expect(node.links).to.deep.equal(links) - done() + + ipfs.object.links(cid.toBaseEncodedString(), { enc: 'base58' }, (err, links) => { + expect(err).to.not.exist() + expect(node.links).to.deep.equal(links) + done() + }) }) }) }) diff --git a/js/src/object/new.js b/js/src/object/new.js index 8be1c790..c61b7276 100644 --- a/js/src/object/new.js +++ b/js/src/object/new.js @@ -2,6 +2,9 @@ 'use strict' const { getDescribe, getIt, expect } = require('../utils/mocha') +const { + calculateCid +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -33,27 +36,31 @@ module.exports = (createCommon, options) => { it('should create a new object with no template', (done) => { ipfs.object.new((err, node) => { expect(err).to.not.exist() - const nodeJSON = node.toJSON() - expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() + + calculateCid(node, (err, cid) => { + expect(err).to.not.exist() + expect(cid.toBaseEncodedString()).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() + }) }) }) - it('should create a new object with no template (promised)', () => { - return ipfs.object.new() - .then((node) => { - const nodeJSON = node.toJSON() - expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - }) + it('should create a new object with no template (promised)', async () => { + const node = await ipfs.object.new() + const cid = await calculateCid(node) + + expect(cid.toBaseEncodedString()).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') }) it('should create a new object with unixfs-dir template', (done) => { ipfs.object.new('unixfs-dir', (err, node) => { expect(err).to.not.exist() - const nodeJSON = node.toJSON() - expect(nodeJSON.multihash) - .to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') - done() + + calculateCid(node, (err, cid) => { + expect(err).to.not.exist() + expect(cid.toBaseEncodedString()).to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + done() + }) }) }) }) diff --git a/js/src/object/patch/add-link.js b/js/src/object/patch/add-link.js index e04387d0..8a8d3e80 100644 --- a/js/src/object/patch/add-link.js +++ b/js/src/object/patch/add-link.js @@ -5,6 +5,11 @@ const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const series = require('async/series') const { getDescribe, getIt, expect } = require('../../utils/mocha') +const { + calculateCid, + createDAGNode, + addLinkToDAGNode +} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -35,6 +40,7 @@ module.exports = (createCommon, options) => { it('should add a link to an existing node', (done) => { let testNodeMultihash + let node1bMultihash let node1a let node1b let node2 @@ -48,8 +54,12 @@ module.exports = (createCommon, options) => { (cb) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() - testNodeMultihash = node.multihash - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + testNodeMultihash = result + cb() + }) }) }, (cb) => { @@ -72,19 +82,34 @@ module.exports = (createCommon, options) => { ipfs.object.put(node2, cb) }, (cb) => { - const link = node2.toJSON() - link.name = 'link-to-node' - DAGNode.addLink(node1a, link, (err, node) => { + calculateCid(node2, (err, result) => { expect(err).to.not.exist() - node1b = node - cb() + + DAGNode.addLink(node1a, { + name: 'link-to-node', + size: node2.toJSON().size, + cid: result + }, (err, node) => { + expect(err).to.not.exist() + node1b = node + + calculateCid(node1b, (err, result) => { + expect(err).to.not.exist() + node1bMultihash = result + cb() + }) + }) }) }, (cb) => { ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0], (err, node) => { expect(err).to.not.exist() - expect(node1b.multihash).to.eql(node.multihash) - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + expect(node1bMultihash).to.eql(result) + cb() + }) }) } /* TODO: revisit this assertions. @@ -119,61 +144,27 @@ module.exports = (createCommon, options) => { ], done) }) - it('should add a link to an existing node (promised)', () => { - let testNodeMultihash - let node1a - let node1b - let node2 - + it('should add a link to an existing node (promised)', async () => { const obj = { Data: Buffer.from('patch test object (promised)'), Links: [] } - return ipfs.object.put(obj) - .then((node) => { - testNodeMultihash = node.multihash - }) - .then(() => new Promise((resolve, reject) => { - DAGNode.create(obj.Data, obj.Links, function (err, node) { - if (err) { - return reject(err) - } - return resolve(node) - }) - })) - .then((node) => { - node1a = node - return new Promise((resolve, reject) => { - DAGNode.create(Buffer.from('some other node'), function (err, node) { - if (err) { - return reject(err) - } - return resolve(node) - }) - }).then((node1) => { - node2 = node1 - return ipfs.object.put(node2) - }) - }) - .then(() => { - const link = node2.toJSON() - link.name = 'link-to-node' - return new Promise((resolve, reject) => { - DAGNode.addLink(node1a, link, function (err, node) { - if (err) { - return reject(err) - } - return resolve(node) - }) - }).then((node) => { - node1b = node - return ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0]) - }) - }) - .then((node) => { - expect(node1b.multihash).to.eql(node.multihash) - }) + const parent = await ipfs.object.put(obj) + const parentCid = await calculateCid(parent) + const child = await ipfs.object.put(await createDAGNode(Buffer.from('some other node'), [])) + const childCid = await calculateCid(child) + const newParent = await addLinkToDAGNode(parent, { + name: 'link-to-node', + size: child.size, + cid: childCid + }) + const newParentCid = await calculateCid(newParent) + + const nodeFromObjectPatch = await ipfs.object.patch.addLink(parentCid, newParent.links[0]) + const nodeFromObjectPatchCid = await calculateCid(nodeFromObjectPatch) + + expect(newParentCid).to.eql(nodeFromObjectPatchCid) }) }) } diff --git a/js/src/object/patch/append-data.js b/js/src/object/patch/append-data.js index 8687f26c..dd4eda9e 100644 --- a/js/src/object/patch/append-data.js +++ b/js/src/object/patch/append-data.js @@ -1,7 +1,11 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const { getDescribe, getIt, expect } = require('../../utils/mocha') +const { + calculateCid +} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -39,28 +43,35 @@ module.exports = (createCommon, options) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() - ipfs.object.patch.appendData(node.multihash, Buffer.from('append'), (err, patchedNode) => { + calculateCid(node, (err, nodeCid) => { expect(err).to.not.exist() - expect(patchedNode.multihash).to.not.deep.equal(node.multihash) - done() + + ipfs.object.patch.appendData(nodeCid, Buffer.from('append'), (err, patchedNode) => { + expect(err).to.not.exist() + + calculateCid(patchedNode, (err, patchedNodeCid) => { + expect(err).to.not.exist() + expect(patchedNodeCid).to.not.deep.equal(nodeCid) + + done() + }) + }) }) }) }) - it('should append data to an existing node (promised)', () => { + it('should append data to an existing node (promised)', async () => { const obj = { Data: Buffer.from('patch test object (promised)'), Links: [] } - return ipfs.object.put(obj) - .then((node) => { - return ipfs.object.patch.appendData(node.multihash, Buffer.from('append')) - .then((patchedNode) => ({ patchedNode, node })) - }) - .then(({ patchedNode, node }) => { - expect(patchedNode.multihash).to.not.deep.equal(node.multihash) - }) + const node = await ipfs.object.put(obj) + const nodeCid = await calculateCid(node) + const patchedNode = await ipfs.object.patch.appendData(nodeCid, Buffer.from('append')) + const patchedNodeCid = await calculateCid(patchedNode) + + expect(nodeCid).to.not.deep.equal(patchedNodeCid) }) }) } diff --git a/js/src/object/patch/rm-link.js b/js/src/object/patch/rm-link.js index 4b9241b7..c0f2a48f 100644 --- a/js/src/object/patch/rm-link.js +++ b/js/src/object/patch/rm-link.js @@ -5,6 +5,10 @@ const dagPB = require('ipld-dag-pb') const DAGLink = dagPB.DAGLink const series = require('async/series') const { getDescribe, getIt, expect } = require('../../utils/mocha') +const { + calculateCid, + asDAGLink +} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -34,9 +38,10 @@ module.exports = (createCommon, options) => { after((done) => common.teardown(done)) it('should remove a link from an existing node', (done) => { - let node1a - let node1b + let node1aCid + let node1bCid let node2 + let node2Cid let testLink const obj1 = { @@ -53,31 +58,57 @@ module.exports = (createCommon, options) => { (cb) => { ipfs.object.put(obj1, (err, node) => { expect(err).to.not.exist() - node1a = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1aCid = result + + cb() + }) }) }, (cb) => { ipfs.object.put(obj2, (err, node) => { expect(err).to.not.exist() node2 = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node2Cid = result + + cb() + }) }) }, (cb) => { - testLink = new DAGLink('link-to-node', node2.size, node2.multihash) + testLink = new DAGLink('link-to-node', node2.size, node2Cid) - ipfs.object.patch.addLink(node1a.multihash, testLink, (err, node) => { + ipfs.object.patch.addLink(node1aCid, testLink, (err, node) => { expect(err).to.not.exist() - node1b = node - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + node1bCid = result + + cb() + }) }) }, (cb) => { - ipfs.object.patch.rmLink(node1b.multihash, testLink, (err, node) => { + ipfs.object.patch.rmLink(node1bCid, testLink, (err, node) => { expect(err).to.not.exist() - expect(node.multihash).to.not.deep.equal(node1b.multihash) - cb() + + calculateCid(node, (err, result) => { + expect(err).to.not.exist() + + expect(result).to.not.deep.equal(node1bCid) + expect(result).to.deep.equal(node1aCid) + + cb() + }) }) } /* TODO: revisit this assertions. @@ -92,12 +123,7 @@ module.exports = (createCommon, options) => { ], done) }) - it('should remove a link from an existing node (promised)', () => { - let node1a - let node1b - let node2 - let testLink - + it('should remove a link from an existing node (promised)', async () => { const obj1 = { Data: Buffer.from('patch test object 1'), Links: [] @@ -108,17 +134,17 @@ module.exports = (createCommon, options) => { Links: [] } - return ipfs.object.put(obj1) - .then((node) => { node1a = node }) - .then(() => ipfs.object.put(obj2)) - .then((node) => { node2 = node }) - .then(() => { - testLink = new DAGLink('link-to-node', node2.size, node2.multihash) - return ipfs.object.patch.addLink(node1a.multihash, testLink) - }) - .then((node) => { node1b = node }) - .then(() => ipfs.object.patch.rmLink(node1b.multihash, testLink)) - .then((node) => expect(node.multihash).to.not.deep.equal(node1b.multihash)) + const node = await ipfs.object.put(obj1) + const nodeCid = await calculateCid(node) + const child = await ipfs.object.put(obj2) + const childAsDAGLink = await asDAGLink(child, 'my-link') + const parent = await ipfs.object.patch.addLink(nodeCid, childAsDAGLink) + const parentCid = await calculateCid(parent) + const withoutChild = await ipfs.object.patch.rmLink(parentCid, childAsDAGLink) + const withoutChildCid = await calculateCid(withoutChild) + + expect(withoutChildCid).to.not.deep.equal(parentCid) + expect(withoutChildCid).to.deep.equal(nodeCid) }) }) } diff --git a/js/src/object/patch/set-data.js b/js/src/object/patch/set-data.js index 9d2cb818..cd9723d1 100644 --- a/js/src/object/patch/set-data.js +++ b/js/src/object/patch/set-data.js @@ -1,7 +1,11 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const { getDescribe, getIt, expect } = require('../../utils/mocha') +const { + calculateCid +} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -39,28 +43,34 @@ module.exports = (createCommon, options) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist() - ipfs.object.patch.setData(node.multihash, Buffer.from('set'), (err, patchedNode) => { + calculateCid(node, (err, nodeCid) => { expect(err).to.not.exist() - expect(node.multihash).to.not.deep.equal(patchedNode.multihash) - done() + + ipfs.object.patch.setData(nodeCid, Buffer.from('set'), (err, patchedNode) => { + expect(err).to.not.exist() + + calculateCid(patchedNode, (err, patchedNodeCid) => { + expect(err).to.not.exist() + + done() + }) + }) }) }) }) - it('should set data for an existing node (promised)', () => { + it('should set data for an existing node (promised)', async () => { const obj = { Data: Buffer.from('patch test object (promised)'), Links: [] } - return ipfs.object.put(obj) - .then((node) => { - return ipfs.object.patch.setData(node.multihash, Buffer.from('set')) - .then((patchedNode) => ({ patchedNode, node })) - }) - .then(({ patchedNode, node }) => { - expect(node.multihash).to.not.deep.equal(patchedNode.multihash) - }) + const node = await ipfs.object.put(obj) + const nodeCid = await calculateCid(node) + const patchedNode = await ipfs.object.patch.setData(nodeCid, Buffer.from('set')) + const patchedNodeCid = await calculateCid(patchedNode) + + expect(nodeCid).to.not.deep.equal(patchedNodeCid) }) }) } diff --git a/js/src/object/put.js b/js/src/object/put.js index 0065008e..f5addf21 100644 --- a/js/src/object/put.js +++ b/js/src/object/put.js @@ -6,6 +6,9 @@ const DAGNode = dagPB.DAGNode const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') +const { + asDAGLink +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -45,24 +48,21 @@ module.exports = (createCommon, options) => { const nodeJSON = node.toJSON() expect(nodeJSON.data).to.eql(obj.Data) expect(nodeJSON.links).to.eql(obj.Links) - expect(nodeJSON.multihash).to.exist() done() }) }) - it('should put an object (promised)', () => { + it('should put an object (promised)', async () => { const obj = { Data: Buffer.from(hat()), Links: [] } - return ipfs.object.put(obj) - .then((node) => { - const nodeJSON = node.toJSON() - expect(obj.Data).to.deep.equal(nodeJSON.data) - expect(obj.Links).to.deep.equal(nodeJSON.links) - expect(nodeJSON.multihash).to.exist() - }) + const node = await ipfs.object.put(obj) + + const nodeJSON = node.toJSON() + expect(obj.Data).to.deep.equal(nodeJSON.data) + expect(obj.Links).to.deep.equal(nodeJSON.links) }) it('should put a JSON encoded Buffer', (done) => { @@ -83,7 +83,6 @@ module.exports = (createCommon, options) => { const nodeJSON = node.toJSON() expect(nodeJSON.data).to.eql(node.data) - expect(nodeJSON.multihash).to.exist() done() }) }) @@ -112,7 +111,6 @@ module.exports = (createCommon, options) => { expect(err).to.not.exist() expect(node.data).to.deep.equal(node.data) expect(node.links).to.deep.equal(node.links) - expect(node.multihash).to.eql(storedNode.multihash) cb() }) } @@ -126,7 +124,6 @@ module.exports = (createCommon, options) => { const nodeJSON = node.toJSON() expect(data).to.deep.equal(nodeJSON.data) expect([]).to.deep.equal(nodeJSON.links) - expect(nodeJSON.multihash).to.exist() done() }) }) @@ -171,12 +168,14 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - const link = node2.toJSON() - link.name = 'some-link' - DAGNode.addLink(node1a, link, (err, node) => { + asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - node1b = node - cb() + + DAGNode.addLink(node1a, link, (err, node) => { + expect(err).to.not.exist() + node1b = node + cb() + }) }) }, (cb) => { diff --git a/js/src/object/stat.js b/js/src/object/stat.js index 8e2a56b3..ef4942ae 100644 --- a/js/src/object/stat.js +++ b/js/src/object/stat.js @@ -1,11 +1,15 @@ /* eslint-env mocha */ +/* eslint-disable max-nested-callbacks */ 'use strict' const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode -const bs58 = require('bs58') const series = require('async/series') const { getDescribe, getIt, expect } = require('../utils/mocha') +const { + calculateCid, + asDAGLink +} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -43,33 +47,11 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.stat(node.multihash, (err, stats) => { + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - const expected = { - Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', - NumLinks: 0, - BlockSize: 17, - LinksSize: 2, - DataSize: 15, - CumulativeSize: 17 - } - expect(expected).to.deep.equal(stats) - done() - }) - }) - }) - it('should get stats for object by multihash (promised)', () => { - const testObj = { - Data: Buffer.from('get test object'), - Links: [] - } - - return ipfs.object.put(testObj, (err, node) => { - expect(err).to.not.exist() - - return ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' }) - .then((stats) => { + ipfs.object.stat(cid, (err, stats) => { + expect(err).to.not.exist() const expected = { Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', NumLinks: 0, @@ -79,13 +61,37 @@ module.exports = (createCommon, options) => { CumulativeSize: 17 } expect(expected).to.deep.equal(stats) + done() }) + }) }) }) + it('should get stats for object by multihash (promised)', async () => { + const testObj = { + Data: Buffer.from('get test object'), + Links: [] + } + + await ipfs.object.put(testObj) + const stats = await ipfs.object.stat('QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', { enc: 'base58' }) + + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + + expect(expected).to.deep.equal(stats) + }) + it('should get stats for object with links by multihash', (done) => { let node1a let node1b + let node1bCid let node2 series([ @@ -104,20 +110,33 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - const link = node2.toJSON() - link.name = 'some-link' - - DAGNode.addLink(node1a, link, (err, node) => { + asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - node1b = node - cb() + + DAGNode.addLink(node1a, link, (err, node) => { + expect(err).to.not.exist() + + node1b = node + + cb() + }) }) }, (cb) => { - ipfs.object.put(node1b, cb) + ipfs.object.put(node1b, (err, node) => { + expect(err).to.not.exist() + + calculateCid(node, (err, cid) => { + expect(err).to.not.exist() + + node1bCid = cid + + cb() + }) + }) }, (cb) => { - ipfs.object.stat(node1b.multihash, (err, stats) => { + ipfs.object.stat(node1bCid, (err, stats) => { expect(err).to.not.exist() const expected = { Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC', @@ -143,18 +162,22 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.stat(bs58.encode(node.multihash), { enc: 'base58' }, (err, stats) => { + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - const expected = { - Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', - NumLinks: 0, - BlockSize: 17, - LinksSize: 2, - DataSize: 15, - CumulativeSize: 17 - } - expect(expected).to.deep.equal(stats) - done() + + ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => { + expect(err).to.not.exist() + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() + }) }) }) }) @@ -168,18 +191,22 @@ module.exports = (createCommon, options) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist() - ipfs.object.stat(bs58.encode(node.multihash).toString(), { enc: 'base58' }, (err, stats) => { + calculateCid(node, (err, cid) => { expect(err).to.not.exist() - const expected = { - Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', - NumLinks: 0, - BlockSize: 17, - LinksSize: 2, - DataSize: 15, - CumulativeSize: 17 - } - expect(expected).to.deep.equal(stats) - done() + + ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => { + expect(err).to.not.exist() + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() + }) }) }) }) diff --git a/js/src/utils/dag-pb.js b/js/src/utils/dag-pb.js new file mode 100644 index 00000000..f199e063 --- /dev/null +++ b/js/src/utils/dag-pb.js @@ -0,0 +1,39 @@ +'use strict' + +const { + promisify +} = require('es6-promisify') +const { + util: { + cid + }, + DAGNode, + DAGLink +} = require('ipld-dag-pb') + +module.exports.calculateCid = promisify((node, cb) => { + cid(node, cb) +}) + +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 = '' + } + + cid(node, (err, nodeCid) => { + if (err) { + return cb(err) + } + + DAGLink.create(name, node.size, nodeCid, cb) + }) +}) diff --git a/package.json b/package.json index 9cf06198..f8ed1f1e 100644 --- a/package.json +++ b/package.json @@ -6,6 +6,9 @@ "main": "js/src/index.js", "scripts": { "test": "exit 0", + "test:node": "exit 0", + "test:browser": "exit 0", + "test:webworker": "exit 0", "lint": "aegir lint", "release": "aegir release -t node --no-docs --no-build --no-test", "release-minor": "aegir release -t node --type minor --no-docs --no-build --no-test", @@ -40,11 +43,12 @@ "concat-stream": "^1.6.2", "crypto": "^1.0.1", "dirty-chai": "^2.0.1", + "es6-promisify": "^6.0.1", "hat": "0.0.3", "ipfs-block": "~0.8.0", "ipfs-unixfs": "~0.1.16", "ipld-dag-cbor": "~0.13.0", - "ipld-dag-pb": "~0.14.11", + "ipld-dag-pb": "~0.15.0", "is-ipfs": "~0.4.7", "libp2p-crypto": "~0.14.0", "multiaddr": "^5.0.0",