diff --git a/README.md b/README.md index 178cde462..6a2b18851 100644 --- a/README.md +++ b/README.md @@ -62,9 +62,7 @@ A valid (read: that follows this interface) IPFS core implementation, must expos ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.new(layout, [callback]) - -`layout` is the MerkleDAG node type, it can be: `null`, `'unixfs-dir'`, `'unixfs-raw'`, `'unixfs-file'`, `'unixfs-metadata'`, `'unixfs-symlink'`. +##### `JavaScript` - ipfs.object.new([callback]) `callback` must follow `function (err, node) {}` signature, where `err` is an error if the operation was not successful and `node` is a MerkleDAG node of the type [DAGNode](https://github.com/vijayee/js-ipfs-merkle-dag/blob/master/src/dag-node.js) @@ -85,8 +83,8 @@ If no `callback` is passed, a promise is returned. `obj` is the MerkleDAG Node to be stored. Can of type: - Object, with format `{ Data: , Links: [] }` -- Buffer, requiring that the encoding is specified on the encoding -- [DAGNode](https://github.com/vijayee/js-ipfs-merkle-dag/blob/master/src/dag-node.js). If no encoding is specified, Buffer is treated as the Data field. +- Buffer, requiring that the encoding is specified on the options. If no encoding is specified, Buffer is treated as the Data field +- [DAGNode](https://github.com/vijayee/js-ipfs-merkle-dag/blob/master/src/dag-node.js). `options` is a optional argument of type object, that can contain the following properties: diff --git a/package.json b/package.json index bab4a2445..ee4fcc6ea 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,9 @@ }, "homepage": "https://github.com/ipfs/interface-ipfs-core#readme", "dependencies": { - "chai": "^3.5.0" + "bs58": "^3.0.0", + "chai": "^3.5.0", + "ipfs-merkle-dag": "^0.5.1", + "json2yaml": "^1.1.0" } } diff --git a/src/object.js b/src/object.js index 9b9d7af7f..c4108662c 100644 --- a/src/object.js +++ b/src/object.js @@ -3,6 +3,9 @@ 'use strict' const expect = require('chai').expect +const DAGNode = require('ipfs-merkle-dag').DAGNode +const bs58 = require('bs58') +const jsonToYaml = require('json2yaml') module.exports = (common) => { let ipfs @@ -20,233 +23,700 @@ module.exports = (common) => { }) describe('.object', () => { - const testObject = Buffer(JSON.stringify({Data: 'testdata', Links: []})) - const testObjectHash = 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD' - const testPatchObject = Buffer(JSON.stringify({Data: 'new test data'})) - const testPatchObjectHash = 'QmWJDtdQWQSajQPx1UVAGWKaSGrHVWdjnrNhbooHP7LuF2' + it('object.new', (done) => { + ipfs.object.new((err, node) => { + expect(err).to.not.exist + expect(node.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() + }) + }) - it('object.put', (done) => { - ipfs.object.put(testObject, 'json', (err, res) => { + it('object.put of object', (done) => { + const obj = { + Data: new Buffer('Some data'), + Links: [] + } + + ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist - expect(res).to.have.a.property('Hash', testObjectHash) - expect(res.Links).to.be.empty + const nodeJSON = node.toJSON() + expect(obj.Data).to.deep.equal(nodeJSON.Data) + expect(obj.Links).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') done() }) }) - it('object.get', (done) => { - ipfs.object.get(testObjectHash, (err, res) => { + it('object.put of json encoded buffer', (done) => { + const obj = { + Data: new Buffer('Some data').toString(), + Links: [] + } + + const buf = new Buffer(JSON.stringify(obj)) + + ipfs.object.put(buf, { enc: 'json' }, (err, node) => { expect(err).to.not.exist - expect(res).to.have.a.property('Data', 'testdata') - expect(res.Links).to.be.empty + const nodeJSON = node.toJSON() + expect(obj.Data).to.deep.equal(nodeJSON.Data) + expect(obj.Links).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') done() }) }) - it('object.data', (done) => { - ipfs.object.data(testObjectHash, (err, res) => { + // TODO verify that yaml encoded buffers still work in go-ipfs + it.skip('object.put of yaml encoded buffer', (done) => { + const obj = { + Data: new Buffer('Some data').toString(), + Links: [] + } + + const buf = new Buffer(jsonToYaml.stringify(obj)) + + ipfs.object.put(buf, { enc: 'yaml' }, (err, node) => { expect(err).to.not.exist + const nodeJSON = node.toJSON() + expect(obj.Data).to.deep.equal(nodeJSON.Data) + expect(obj.Links).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + done() + }) + }) - let buf = '' - res - .on('error', (err) => { - expect(err).to.not.exist - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.equal('testdata') - done() - }) + it('object.put of buffer treated as Data field', (done) => { + const data = new Buffer('Some data') + ipfs.object.put(data, (err, node) => { + expect(err).to.not.exist + const nodeJSON = node.toJSON() + expect(data).to.deep.equal(nodeJSON.Data) + expect([]).to.deep.equal(nodeJSON.Links) + expect(nodeJSON.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + done() }) }) - it('object.stat', (done) => { - ipfs.object.stat(testObjectHash, (err, res) => { + it('object.put of DAGNode', (done) => { + const dNode = new DAGNode(new Buffer('Some data')) + + ipfs.object.put(dNode, (err, node) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD', - NumLinks: 0, - BlockSize: 10, - LinksSize: 2, - DataSize: 8, - CumulativeSize: 10 - }) + expect(dNode.data).to.deep.equal(node.data) + expect(dNode.links).to.deep.equal(node.links) + expect(dNode.multihash()).to.deep.equal(node.multihash()) + done() + }) + }) + + it('object.put fails if String is passed', (done) => { + ipfs.object.put('aaa', (err) => { + expect(err).to.exist done() }) }) - it('object.links', (done) => { - ipfs.object.links(testObjectHash, (err, res) => { + it('object.put 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) + + ipfs.object.put(dNode1, (err, node) => { + expect(err).to.not.exist + expect(dNode1.data).to.deep.equal(node.data) + expect(dNode1.links).to.deep.equal(node.links) + expect(dNode1.multihash()).to.deep.equal(node.multihash()) + done() + }) + }) + + it('object.get with multihash', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node1) => { + expect(err).to.not.exist + + ipfs.object.get(node1.multihash(), (err, node2) => { + expect(err).to.not.exist + // 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.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) + done() + }) + }) + }) + + it('object.get with multihash (+ links)', (done) => { + const dNode1 = new DAGNode(new Buffer('Some data 1')) + const dNode2 = new DAGNode(new Buffer('Some data 2')) + dNode1.addNodeLink('some-link', dNode2) + + ipfs.object.put(dNode1, (err, node1) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD' + ipfs.object.get(node1.multihash(), (err, node2) => { + expect(err).to.not.exist + // 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.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) + done() + }) + }) + }) + + it('object.get with multihash base58 encoded', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node1) => { + expect(err).to.not.exist + + ipfs.object.get(bs58.encode(node1.multihash()), { enc: 'base58' }, (err, node2) => { + expect(err).to.not.exist + // 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.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) + done() + }) + }) + }) + + it('object.get with multihash base58 encoded toString', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node1) => { + expect(err).to.not.exist + + ipfs.object.get(bs58.encode(node1.multihash()).toString(), { enc: 'base58' }, (err, node2) => { + expect(err).to.not.exist + // 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.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) + done() + }) + }) + }) + + it('object.data with multihash', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.data(node.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() + }) + }) + }) + + it('object.data with multihash base58 encoded', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.data(bs58.encode(node.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() + }) + }) + }) + + it('object.data with multihash base58 encoded toString', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.data(bs58.encode(node.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() + }) + }) + }) + + it('object.links with multihash', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.links(node.multihash(), (err, links) => { + expect(err).to.not.exist + expect(node.links).to.deep.equal(links) + done() + }) + }) + }) + + it('object.links with multihash (+ links)', (done) => { + const dNode1 = new DAGNode(new Buffer('Some data 1')) + const dNode2 = new DAGNode(new Buffer('Some data 2')) + dNode1.addNodeLink('some-link', dNode2) + + ipfs.object.put(dNode1, (err, node) => { + expect(err).to.not.exist + + ipfs.object.links(node.multihash(), (err, links) => { + expect(err).to.not.exist + expect(node.links[0].toJSON()).to.deep.equal(links[0].toJSON()) + done() + }) + }) + }) + + it('object.links with multihash base58 encoded', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.links(bs58.encode(node.multihash()), { enc: 'base58' }, (err, links) => { + expect(err).to.not.exist + expect(node.links).to.deep.equal(links) + done() + }) + }) + }) + + it('object.links with multihash base58 encoded toString', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.links(bs58.encode(node.multihash()).toString(), { enc: 'base58' }, (err, links) => { + expect(err).to.not.exist + expect(node.links).to.deep.equal(links) + done() + }) + }) + }) + + it('object.stat with multihash', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.stat(node.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() + }) + }) + }) + + it('object.stat with multihash (+ Links)', (done) => { + const dNode1 = new DAGNode(new Buffer('Some data 1')) + const dNode2 = new DAGNode(new Buffer('Some data 2')) + dNode1.addNodeLink('some-link', dNode2) + + ipfs.object.put(dNode1, (err, node) => { + expect(err).to.not.exist + + ipfs.object.stat(node.multihash(), (err, stats) => { + expect(err).to.not.exist + const expected = { + Hash: 'QmPR7W4kaADkAo4GKEVVPQN81EDUFCHJtqejQZ5dEG7pBC', + NumLinks: 1, + BlockSize: 64, + LinksSize: 53, + DataSize: 11, + CumulativeSize: 77 + } + expect(expected).to.deep.equal(stats) + done() + }) + }) + }) + + it('object.stat with multihash base58 encoded', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.stat(bs58.encode(node.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() + }) + }) + }) + + it('object.stat with multihash base58 encoded toString', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.stat(bs58.encode(node.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() }) - done() }) }) describe('object.patch', () => { + let testNode + let testNodeWithLink + let testLink before((done) => { - ipfs.object.put(testPatchObject, 'json', (err, res) => { + const obj = { + Data: new Buffer('patch test object'), + Links: [] + } + + ipfs.object.put(obj, (err, node) => { expect(err).to.not.exist + testNode = node done() }) }) it('.addLink', (done) => { - ipfs.object.patch - .addLink(testObjectHash, 'next', testPatchObjectHash, (err, res) => { + const dNode1 = testNode.copy() + const dNode2 = new DAGNode(new Buffer('some other node')) + // note: we need to put the linked obj, otherwise IPFS won't timeout + // cause it needs the node to get its size + ipfs.object.put(dNode2, (err) => { + expect(err).to.not.exist + dNode1.addNodeLink('link-to-node', dNode2) + + ipfs.object.patch.addLink(testNode.multihash(), dNode1.links[0], (err, node3) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: 'QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd' - }) - ipfs.object.get(res.Hash, (err, res) => { - expect(err).to.not.exist - expect(res).to.be.eql({ - Data: 'testdata', - Links: [{ - Name: 'next', - Hash: 'QmWJDtdQWQSajQPx1UVAGWKaSGrHVWdjnrNhbooHP7LuF2', - Size: 15 - }] - }) - done() - }) + expect(dNode1.multihash()).to.deep.equal(node3.multihash()) + testNodeWithLink = node3 + testLink = dNode1.links[0] + done() }) + }) }) it('.rmLink', (done) => { - ipfs.object.patch - .rmLink('QmZFdJ3CQsY4kkyQtjoUo8oAzsEs5BNguxBhp8sjQMpgkd', 'next', (err, res) => { + ipfs.object.patch.rmLink(testNodeWithLink.multihash(), testLink, (err, node) => { + expect(err).to.not.exist + expect(node.multihash()).to.deep.equal(testNode.multihash()) + done() + }) + }) + + it('.appendData', (done) => { + ipfs.object.patch.appendData(testNode.multihash(), new Buffer('append'), (err, node) => { + expect(err).to.not.exist + expect(node.multihash()).to.not.deep.equal(testNode.multihash()) + done() + }) + }) + + it('.setData', (done) => { + ipfs.object.patch.appendData(testNode.multihash(), new Buffer('set'), (err, node) => { + expect(err).to.not.exist + expect(node.multihash()).to.not.deep.equal(testNode.multihash()) + done() + }) + }) + }) + + describe('promise', () => { + it('object.new', (done) => { + ipfs.object.new() + .then((node) => { + expect(node.toJSON().Hash).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() + }) + .catch((err) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: testObjectHash - }) - ipfs.object.get(res.Hash, (err, res) => { - expect(err).to.not.exist - expect(res).to.be.eql({ - Data: 'testdata', - Links: [] - }) - done() - }) }) }) - it('.appendData', (done) => { - ipfs.object.patch - .appendData(testObjectHash, new Buffer(' hello'), (err, res) => { + it('object.put', (done) => { + const obj = { + Data: new Buffer('Some data'), + Links: [] + } + + 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.Hash).to.equal('QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK') + done() + }).catch((err) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: 'Qmcjhr2QztQxCAoEf8tJPTGTVkTsUrTQ36JurH14DNYNsc' - }) - ipfs.object.get(res.Hash, (err, res) => { - expect(err).to.not.exist - expect(res).to.be.eql({ - Data: 'testdata hello', - Links: [] - }) + }) + }) + + it('object.get', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node1) => { + expect(err).to.not.exist + + 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.multihash()).to.deep.equal(node2.multihash()) + expect(node1.data).to.deep.equal(node2.data) + expect(node1.links).to.deep.equal(node2.links) done() }) + .catch((err) => { + expect(err).to.not.exist }) + }) }) - it('.setData', (done) => { - ipfs.object.patch - .setData(testObjectHash, new Buffer('hello world'), (err, res) => { - expect(err).to.not.exist - expect(res).to.be.eql({ - Hash: 'QmU1Sq1B7RPQD2XcQNLB58qJUyJffVJqihcxmmN1STPMxf' + + it('object.data', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + 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() }) - ipfs.object.get(res.Hash, (err, res) => { + .catch((err) => { expect(err).to.not.exist - expect(res).to.be.eql({ - Data: 'hello world', - Links: [] - }) - done() }) - }) + }) }) - }) - it('object.new', (done) => { - ipfs.object.new('unixfs-dir', (err, res) => { - expect(err).to.not.exist - expect(res).to.deep.equal({ - Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' + it('object.stat', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj) + .then((node) => { + ipfs.object.stat(node.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() + }) + }) + .catch((err) => { + expect(err).to.not.exist }) - done() }) - }) - describe('promise', () => { - it('object.put', () => { - return ipfs.object.put(testObject, 'json') - .then((res) => { - expect(res).to.have.a.property('Hash', testObjectHash) - expect(res.Links).to.be.empty + it('object.links', (done) => { + const testObj = { + Data: new Buffer('get test object'), + Links: [] + } + + ipfs.object.put(testObj, (err, node) => { + expect(err).to.not.exist + + ipfs.object.links(node.multihash()) + .then((links) => { + expect(node.links).to.deep.equal(links) + done() }) + .catch((err) => { + expect(err).to.not.exist + }) + }) }) - it('object.get', () => { - return ipfs.object.get(testObjectHash) - .then((res) => { - expect(res).to.have.a.property('Data', 'testdata') - expect(res.Links).to.be.empty + describe('object.patch', () => { + let testNode + let testNodeWithLink + let testLink + + before((done) => { + const obj = { + Data: new Buffer('patch test object'), + Links: [] + } + + ipfs.object.put(obj, (err, node) => { + expect(err).to.not.exist + testNode = node + done() }) - }) + }) - it('object.data', (done) => { - return ipfs.object.data(testObjectHash) - .then((res) => { - let buf = '' - res - .on('error', (err) => { - throw err - }) - .on('data', (data) => { - buf += data - }) - .on('end', () => { - expect(buf).to.equal('testdata') + it('.addLink', (done) => { + const dNode1 = testNode.copy() + const dNode2 = new DAGNode(new Buffer('some other node')) + // note: we need to put the linked obj, otherwise IPFS won't timeout + // cause it needs the node to get its size + ipfs.object.put(dNode2, (err) => { + expect(err).to.not.exist + dNode1.addNodeLink('link-to-node', dNode2) + + ipfs.object.patch.addLink(testNode.multihash(), dNode1.links[0]) + .then((node3) => { + expect(dNode1.multihash()).to.deep.equal(node3.multihash()) + testNodeWithLink = node3 + testLink = dNode1.links[0] done() }) + .catch((err) => { + expect(err).to.not.exist + }) }) - }) + }) - it('object.stat', () => { - return ipfs.object.stat(testObjectHash) - .then((res) => { - expect(res).to.be.eql({ - Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD', - NumLinks: 0, - BlockSize: 10, - LinksSize: 2, - DataSize: 8, - CumulativeSize: 10 + it('.rmLink', (done) => { + ipfs.object.patch.rmLink(testNodeWithLink.multihash(), testLink) + .then((node) => { + expect(node.multihash()).to.deep.equal(testNode.multihash()) + done() }) - }) - }) + .catch((err) => { + expect(err).to.not.exist + }) + }) - it('object.links', () => { - return ipfs.object.links(testObjectHash) - .then((res) => { - expect(res).to.be.eql({ - Hash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD' + it('.appendData', (done) => { + ipfs.object.patch.appendData(testNode.multihash(), new Buffer('append')) + .then((node) => { + expect(node.multihash()).to.not.deep.equal(testNode.multihash()) + done() }) - }) - }) + .catch((err) => { + expect(err).to.not.exist + }) + }) - it('object.new', () => { - return ipfs.object.new('unixfs-dir') - .then((res) => { - expect(res).to.deep.equal({ - Hash: 'QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn' + it('.setData', (done) => { + ipfs.object.patch.appendData(testNode.multihash(), new Buffer('set')) + .then((node) => { + expect(node.multihash()).to.not.deep.equal(testNode.multihash()) + done() }) + .catch((err) => { + expect(err).to.not.exist }) + }) }) }) })