diff --git a/src/object.js b/src/object.js index 9697cc9c..76431ee4 100644 --- a/src/object.js +++ b/src/object.js @@ -97,15 +97,14 @@ module.exports = (common) => { }) it('of protobuf encoded buffer', (done) => { - const node = new DAGNode(new Buffer('Some data')) + let node let serialized - let multihash series([ (cb) => { - node.multihash((err, _multihash) => { + DAGNode.create(new Buffer('Some data'), (err, _node) => { expect(err).to.not.exist - multihash = _multihash + node = _node cb() }) }, @@ -121,11 +120,8 @@ module.exports = (common) => { expect(err).to.not.exist expect(node.data).to.deep.equal(node.data) expect(node.links).to.deep.equal(node.links) - storedNode.multihash((err, _multihash) => { - expect(err).to.not.exist - expect(multihash).to.eql(_multihash) - cb() - }) + expect(node.multihash).to.eql(storedNode.multihash) + cb() }) } ], done) @@ -135,24 +131,23 @@ module.exports = (common) => { const data = new Buffer('Some data') ipfs.object.put(data, (err, node) => { expect(err).to.not.exist - node.toJSON((err, nodeJSON) => { - expect(err).to.not.exist - expect(data).to.deep.equal(nodeJSON.Data) - expect([]).to.deep.equal(nodeJSON.Links) - expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') - done() - }) + const nodeJSON = node.toJSON() + expect(data).to.deep.equal(nodeJSON.data) + expect([]).to.deep.equal(nodeJSON.links) + expect(nodeJSON.multihash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + done() }) }) it('of DAGNode', (done) => { - const dNode = new DAGNode(new Buffer('Some data')) - - ipfs.object.put(dNode, (err, node) => { + DAGNode.create(new Buffer('Some data'), (err, dNode) => { expect(err).to.not.exist - expect(dNode.data).to.deep.equal(node.data) - expect(dNode.links).to.deep.equal(node.links) - done() + ipfs.object.put(dNode, (err, node) => { + expect(err).to.not.exist + expect(dNode.data).to.deep.equal(node.data) + expect(dNode.links).to.deep.equal(node.links) + done() + }) }) }) @@ -163,22 +158,44 @@ module.exports = (common) => { }) }) - it('DAGNode with some DAGLinks', (done) => { - const dNode1 = new DAGNode(new Buffer('Some data 1')) - const dNode2 = new DAGNode(new Buffer('Some data 2')) - dNode1.addNodeLink('some-link', dNode2, (err) => { - expect(err).to.not.exist - ipfs.object.put(dNode1, (err, node) => { - expect(err).to.not.exist - expect(dNode1.data).to.deep.equal(node.data) - expect( - dNode1.links.map((l) => l.toJSON()) - ).to.deep.equal( - node.links.map((l) => l.toJSON()) - ) - done() - }) - }) + it('DAGNode with a link', (done) => { + let node1a + let node1b + let node2 + series([ + (cb) => { + DAGNode.create(new Buffer('Some data 1'), (err, node) => { + expect(err).to.not.exist + node1a = node + cb() + }) + }, + (cb) => { + DAGNode.create(new Buffer('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) => { + expect(err).to.not.exist + node1b = node + cb() + }) + }, + (cb) => { + ipfs.object.put(node1b, (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())) + cb() + }) + } + ], done) }) }) @@ -190,7 +207,6 @@ module.exports = (common) => { } let node1 - let node1Multihash let node2 series([ @@ -202,14 +218,7 @@ module.exports = (common) => { }) }, (cb) => { - node1.multihash((err, multihash) => { - expect(err).to.not.exist - node1Multihash = multihash - cb() - }) - }, - (cb) => { - ipfs.object.get(node1Multihash, (err, node) => { + ipfs.object.get(node1.multihash, (err, node) => { expect(err).to.not.exist node2 = node @@ -222,18 +231,16 @@ module.exports = (common) => { }) }, (cb) => { - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) - node2.multihash((err, multihash) => { - expect(err).to.not.exist - expect(node1Multihash).to.deep.equal(multihash) - 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('with multihash (+ links)', (done) => { + // TODO const node1 = new DAGNode(new Buffer('Some data 1')) const node2 = new DAGNode(new Buffer('Some data 2')) let node1Multihash @@ -286,7 +293,6 @@ module.exports = (common) => { } let node1a - let node1Multihash let node1b series([ @@ -298,14 +304,7 @@ module.exports = (common) => { }) }, (cb) => { - node1a.multihash((err, multihash) => { - expect(err).to.not.exist - node1Multihash = multihash - cb() - }) - }, - (cb) => { - ipfs.object.get(node1Multihash, { enc: 'base58' }, (err, node) => { + ipfs.object.get(node1a.multihash, { enc: 'base58' }, (err, node) => { expect(err).to.not.exist // because js-ipfs-api can't infer if the // returned Data is Buffer or String @@ -317,13 +316,10 @@ module.exports = (common) => { }) }, (cb) => { - node1b.multihash((err, multihash) => { - expect(err).to.not.exist - expect(node1Multihash).to.eql(multihash) - expect(node1a.data).to.eql(node1b.data) - expect(node1a.links).to.eql(node1b.links) - cb() - }) + expect(node1a.multihash).to.eql(node1b.multihash) + expect(node1a.data).to.eql(node1b.data) + expect(node1a.links).to.eql(node1b.links) + cb() } ], done) }) @@ -335,7 +331,6 @@ module.exports = (common) => { } let node1a - let node1Multihash let node1b series([ @@ -347,14 +342,7 @@ module.exports = (common) => { }) }, (cb) => { - node1a.multihash((err, multihash) => { - expect(err).to.not.exist - node1Multihash = multihash - cb() - }) - }, - (cb) => { - ipfs.object.get(bs58.encode(node1Multihash).toString(), { enc: 'base58' }, (err, node) => { + ipfs.object.get(bs58.encode(node1a.multihash).toString(), { enc: 'base58' }, (err, node) => { expect(err).to.not.exist // because js-ipfs-api can't infer if the // returned Data is Buffer or String @@ -366,13 +354,10 @@ module.exports = (common) => { }) }, (cb) => { - node1b.multihash((err, multihash) => { - expect(err).to.not.exist - expect(node1Multihash).to.eql(multihash) - expect(node1a.data).to.eql(node1b.data) - expect(node1a.links).to.eql(node1b.links) - cb() - }) + expect(node1a.multihash).to.eql(node1b.multihash) + expect(node1a.data).to.eql(node1b.data) + expect(node1a.links).to.eql(node1b.links) + cb() } ], done) }) @@ -388,19 +373,16 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.data(node.multihash, (err, data) => { expect(err).to.not.exist - ipfs.object.data(multihash, (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 = new Buffer(data) - } - expect(node.data).to.deep.equal(data) - done() - }) + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = new Buffer(data) + } + expect(node.data).to.eql(data) + done() }) }) }) @@ -414,20 +396,16 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.data(bs58.encode(node.multihash), { enc: 'base58' }, (err, data) => { expect(err).to.not.exist - ipfs.object.data(bs58.encode(multihash), { 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 = new Buffer(data) - } - expect(node.data).to.deep.equal(data) - done() - }) + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = new Buffer(data) + } + expect(node.data).to.eql(data) + done() }) }) }) @@ -441,20 +419,16 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.data(bs58.encode(node.multihash).toString(), { enc: 'base58' }, (err, data) => { expect(err).to.not.exist - ipfs.object.data(bs58.encode(multihash).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 = new Buffer(data) - } - expect(node.data).to.deep.equal(data) - done() - }) + // because js-ipfs-api can't infer if the + // returned Data is Buffer or String + if (typeof data === 'string') { + data = new Buffer(data) + } + expect(node.data).to.eql(data) + done() }) }) }) @@ -469,19 +443,17 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - ipfs.object.links(multihash, (err, links) => { - expect(err).to.not.exist - expect(node.links).to.deep.equal(links) - done() - }) + ipfs.object.links(node.multihash, (err, links) => { + expect(err).to.not.exist + expect(node.links).to.deep.equal(links) + done() }) }) }) it('with multihash (+ links)', (done) => { + // TODO const node1 = new DAGNode(new Buffer('Some data 1')) const node2 = new DAGNode(new Buffer('Some data 2')) @@ -511,14 +483,11 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - ipfs.object.links(bs58.encode(multihash), { enc: 'base58' }, (err, links) => { - expect(err).to.not.exist - expect(node.links).to.deep.equal(links) - done() - }) + ipfs.object.links(bs58.encode(node.multihash), { enc: 'base58' }, (err, links) => { + expect(err).to.not.exist + expect(node.links).to.deep.equal(links) + done() }) }) }) @@ -531,14 +500,10 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.links(bs58.encode(node.multihash), { enc: 'base58' }, (err, links) => { expect(err).to.not.exist - - ipfs.object.links(bs58.encode(multihash), { enc: 'base58' }, (err, links) => { - expect(err).to.not.exist - expect(node.links).to.deep.equal(links) - done() - }) + expect(node.links).to.deep.equal(links) + done() }) }) }) @@ -554,27 +519,24 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.stat(node.multihash, (err, stats) => { expect(err).to.not.exist - - ipfs.object.stat(multihash, (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() - }) + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() }) }) }) it('with multihash (+ Links)', (done) => { + // TODO const node1 = new DAGNode(new Buffer('Some data 1')) const node2 = new DAGNode(new Buffer('Some data 2')) node1.addNodeLink('some-link', node2, (err) => { @@ -611,22 +573,18 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.stat(bs58.encode(node.multihash), { enc: 'base58' }, (err, stats) => { expect(err).to.not.exist - - ipfs.object.stat(bs58.encode(multihash), { 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() - }) + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() }) }) }) @@ -640,22 +598,18 @@ module.exports = (common) => { ipfs.object.put(testObj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { + ipfs.object.stat(bs58.encode(node.multihash).toString(), { enc: 'base58' }, (err, stats) => { expect(err).to.not.exist - - ipfs.object.stat(bs58.encode(multihash).toString(), { 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() - }) + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() }) }) }) @@ -674,15 +628,13 @@ module.exports = (common) => { before((done) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - testNodeMultihash = multihash - done() - }) + testNodeMultihash = node.multihash + done() }) }) it('.addLink', (done) => { + // TODO const node1 = new DAGNode(obj.Data, obj.Links) const node2 = new DAGNode(new Buffer('some other node')) let node1Multihash @@ -721,33 +673,24 @@ module.exports = (common) => { it('.rmLink', (done) => { ipfs.object.patch.rmLink(testNodeWithLinkMultihash, testLink, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeWithLinkMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeWithLinkMultihash) + done() }) }) it('.appendData', (done) => { ipfs.object.patch.appendData(testNodeMultihash, new Buffer('append'), (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeMultihash) + done() }) }) it('.setData', (done) => { ipfs.object.patch.appendData(testNodeMultihash, new Buffer('set'), (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeMultihash) + done() }) }) }) @@ -757,11 +700,9 @@ module.exports = (common) => { it('object.new', (done) => { ipfs.object.new() .then((node) => { - node.toJSON((err, nodeJSON) => { - expect(err).to.not.exist - expect(nodeJSON.Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + const nodeJSON = node.toJSON() + expect(nodeJSON.multihash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() }) }) @@ -773,13 +714,11 @@ module.exports = (common) => { ipfs.object.put(obj) .then((node) => { - node.toJSON((err, nodeJSON) => { - expect(err).to.not.exist - expect(obj.Data).to.deep.equal(nodeJSON.Data) - expect(obj.Links).to.deep.equal(nodeJSON.Links) - expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') - done() - }) + const nodeJSON = node.toJSON() + expect(obj.Data).to.deep.equal(nodeJSON.data) + expect(obj.Links).to.deep.equal(nodeJSON.links) + expect(nodeJSON.multihash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + done() }) }) @@ -789,24 +728,19 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj) - .then((node1) => { - node1.multihash((err, multihash) => { - expect(err).to.not.exist - ipfs.object.get(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 = new Buffer(node2.data) - } + ipfs.object.put(testObj).then((node1) => { + 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 = new Buffer(node2.data) + } - expect(node1.data).to.deep.equal(node2.data) - expect(node1.links).to.deep.equal(node2.links) - done() - }) - }) + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) + done() }) + }) }) it('object.data', (done) => { @@ -815,22 +749,17 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj) - .then((node) => { - node.multihash((err, multihash) => { - expect(err).to.not.exist - ipfs.object.data(multihash) - .then((data) => { - // because js-ipfs-api can't infer - // if the returned Data is Buffer or String - if (typeof data === 'string') { - data = new Buffer(data) - } - expect(node.data).to.deep.equal(data) - done() - }) - }) + ipfs.object.put(testObj).then((node) => { + 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 = new Buffer(data) + } + expect(node.data).to.deep.equal(data) + done() }) + }) }) it('object.stat', (done) => { @@ -867,17 +796,12 @@ module.exports = (common) => { Links: [] } - ipfs.object.put(testObj) - .then((node) => { - node.multihash((err, multihash) => { - expect(err).to.not.exist - ipfs.object.links(multihash) - .then((links) => { - expect(node.links).to.deep.equal(links) - done() - }) - }) + ipfs.object.put(testObj).then((node) => { + ipfs.object.links(node.multihash).then((links) => { + expect(node.links).to.eql(links) + done() }) + }) }) describe('object.patch', () => { @@ -893,15 +817,13 @@ module.exports = (common) => { before((done) => { ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist - node.multihash((err, multihash) => { - expect(err).to.not.exist - testNodeMultihash = multihash - done() - }) + testNodeMultihash = node.multihash + done() }) }) it('.addLink', (done) => { + // TODO const node1 = new DAGNode(obj.Data, obj.Links) const node2 = new DAGNode(new Buffer('some other node')) let node1Multihash @@ -943,11 +865,8 @@ module.exports = (common) => { it('.rmLink', (done) => { ipfs.object.patch.rmLink(testNodeWithLinkMultihash, testLink) .then((node) => { - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeWithLinkMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeWithLinkMultihash) + done() }) .catch((err) => { expect(err).to.not.exist @@ -957,11 +876,8 @@ module.exports = (common) => { it('.appendData', (done) => { ipfs.object.patch.appendData(testNodeMultihash, new Buffer('append')) .then((node) => { - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeMultihash) + done() }) .catch((err) => { expect(err).to.not.exist @@ -971,11 +887,8 @@ module.exports = (common) => { it('.setData', (done) => { ipfs.object.patch.appendData(testNodeMultihash, new Buffer('set')) .then((node) => { - node.multihash((err, multihash) => { - expect(err).to.not.exist - expect(multihash).to.not.deep.equal(testNodeMultihash) - done() - }) + expect(node.multihash).to.not.deep.equal(testNodeMultihash) + done() }) .catch((err) => { expect(err).to.not.exist