From bf2c76317950030ddca1150b1dac40e4d13d7b4c Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Mon, 26 Nov 2018 14:41:14 +0000 Subject: [PATCH 1/3] refactor: object API write methods return CIDs Since `ipld-dag-pb` removed `multihash` and `cid` properties, the Object API became less useful, returning the data you provided it when you call methods that write DAG nodes. This meant the user had to calculate the CID manually. This PR updates the Object API so that write methods return CID instances instead of DAG nodes. See discussion here for more context: https://github.com/ipfs/interface-ipfs-core/pull/388#pullrequestreview-173866270 License: MIT Signed-off-by: Alan Shaw --- SPEC/OBJECT.md | 70 ++++++------ SPEC/TYPES.md | 4 +- js/src/object/data.js | 74 +++++-------- js/src/object/get.js | 112 +++++--------------- js/src/object/links.js | 31 +++--- js/src/object/new.js | 32 +++--- js/src/object/patch/add-link.js | 69 +++++------- js/src/object/patch/append-data.js | 26 ++--- js/src/object/patch/rm-link.js | 65 ++++-------- js/src/object/patch/set-data.js | 28 +++-- js/src/object/put.js | 83 +++++++++------ js/src/object/stat.js | 101 +++++++----------- js/src/{utils/dag-pb.js => object/utils.js} | 19 +--- 13 files changed, 281 insertions(+), 433 deletions(-) rename js/src/{utils/dag-pb.js => object/utils.js} (65%) diff --git a/SPEC/OBJECT.md b/SPEC/OBJECT.md index 7dc9300e..062b5112 100644 --- a/SPEC/OBJECT.md +++ b/SPEC/OBJECT.md @@ -17,22 +17,22 @@ ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.new([template][, callback]) +##### `JavaScript` - ipfs.object.new([template], [callback]) `template` if defined, must be a string `unixfs-dir` and if that is passed, the created node will be an empty unixfs style directory. -`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][] +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][]. If no `callback` is passed, a [promise][] is returned. **Example:** ```JavaScript -ipfs.object.new('unixfs-dir', (err, node) => { +ipfs.object.new('unixfs-dir', (err, cid) => { if (err) { throw err } - console.log(node.toJSON().multihash) + console.log(cid.toString()) // Logs: // QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn }) @@ -46,7 +46,7 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.put(obj, [options, callback]) +##### `JavaScript` - ipfs.object.put(obj, [options], [callback]) `obj` is the MerkleDAG Node to be stored. Can of type: @@ -58,7 +58,7 @@ A great source of [examples][] can be found in the tests for this API. - `enc`, the encoding of the Buffer (json, yml, etc), if passed a Buffer. -`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][] +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][]. If no `callback` is passed, a [promise][] is returned. @@ -70,11 +70,11 @@ const obj = { Links: [] } -ipfs.object.put(obj, (err, node) => { +ipfs.object.put(obj, (err, cid) => { if (err) { throw err } - console.log(node.toJSON().multihash) + console.log(cid.toString()) // Logs: // QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK }) @@ -88,7 +88,7 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.get(multihash, [options, callback]) +##### `JavaScript` - ipfs.object.get(multihash, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -112,9 +112,9 @@ ipfs.object.get(multihash, (err, node) => { if (err) { throw err } - console.log(node.toJSON().multihash) + console.log(node.data) // Logs: - // QmPb5f92FxKPYdT3QNBd1GKiL4tZUXUrzF4Hkpdr3Gf1gK + // some data }) ``` @@ -126,7 +126,7 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.data(multihash, [options, callback]) +##### `JavaScript` - ipfs.object.data(multihash, [options], [callback]) `multihash` is a [multihash][] which can be passed as: - Buffer, the raw Buffer of the multihash (or of and encoded version) @@ -163,7 +163,7 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.links(multihash, [options, callback]) +##### `JavaScript` - ipfs.object.links(multihash, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -201,7 +201,7 @@ A great source of [examples][] can be found in the tests for this API. ##### `Go` **WIP** -##### `JavaScript` - ipfs.object.stat(multihash, [options, callback]) +##### `JavaScript` - ipfs.object.stat(multihash, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -261,7 +261,7 @@ A great source of [examples][] can be found in the tests for this API. ###### `Go` **WIP** -###### `JavaScript` - ipfs.object.patch.addLink(multihash, link, [options, callback]) +###### `JavaScript` - ipfs.object.patch.addLink(multihash, link, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -290,7 +290,7 @@ const link = new DAGLink(name, size, multihash) - `enc`, the encoding of multihash (base58, base64, etc), if any. -`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][] that resulted by the operation of adding a Link. +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][] - the CID of the new DAG node that was created due to the operation. If no `callback` is passed, a [promise][] is returned. @@ -301,11 +301,11 @@ ipfs.object.patch.addLink(node, { name: 'some-link' size: 10 multihash: 'QmPTkMuuL6PD8L2SwTwbcs1NPg14U8mRzerB1ZrrBrkSDD' -}, (err, newNode) => { +}, (err, cid) => { if (err) { throw err } - // newNode is node with the added link + // cid is CID of the DAG node created by adding a link }) ``` @@ -317,7 +317,7 @@ A great source of [examples][] can be found in the tests for this API. ###### `Go` **WIP** -###### `JavaScript` - ipfs.object.patch.rmLink(multihash, link, [options, callback]) +###### `JavaScript` - ipfs.object.patch.rmLink(multihash, link, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -325,26 +325,26 @@ A great source of [examples][] can be found in the tests for this API. - String, the toString version of the multihash (or of an encoded version) `link` is the link to be removed on the node that is identified by the `multihash`, can be passed as: + - `DAGLink` -- Object containing name property -```js -const link = { - name: 'Qmef7ScwzJUCg1zUSrCmPAz45m8uP5jU7SLgt2EffjBmbL' -}; -``` + ```js + const link = new DAGLink(name, size, multihash) + ``` -or +- Object containing a `name` property -```js -const link = new DAGLink(name, size, multihash) + ```js + const link = { + name: 'Qmef7ScwzJUCg1zUSrCmPAz45m8uP5jU7SLgt2EffjBmbL' + }; ``` `options` is a optional argument of type object, that can contain the following properties: - `enc`, the encoding of multihash (base58, base64, etc), if any. -`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][] that resulted by the operation of adding a Link. +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][] - the CID of the new DAG node that was created due to the operation. If no `callback` is passed, a [promise][] is returned. @@ -361,7 +361,7 @@ A great source of [examples][] can be found in the tests for this API. ###### `Go` **WIP** -###### `JavaScript` - ipfs.object.patch.appendData(multihash, data, [options, callback]) +###### `JavaScript` - ipfs.object.patch.appendData(multihash, data, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -374,7 +374,7 @@ A great source of [examples][] can be found in the tests for this API. - `enc`, the encoding of multihash (base58, base64, etc), if any. -`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][] that resulted by the operation of adding a Link. +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][] - the CID of the new DAG node that was created due to the operation. If no `callback` is passed, a [promise][] is returned. @@ -396,7 +396,7 @@ A great source of [examples][] can be found in the tests for this API. ###### `Go` **WIP** -###### `JavaScript` - ipfs.object.patch.setData(multihash, data, [options, callback]) +###### `JavaScript` - ipfs.object.patch.setData(multihash, data, [options], [callback]) `multihash` is a [multihash][] which can be passed as: @@ -409,23 +409,23 @@ A great source of [examples][] can be found in the tests for this API. - `enc`, the encoding of multihash (base58, base64, etc), if any. -`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][] that resulted by the operation of adding a Link. +`callback` must follow `function (err, cid) {}` signature, where `err` is an error if the operation was not successful and `cid` is an instance of [CID][] - the CID of the new DAG node that was created due to the operation. If no `callback` is passed, a [promise][] is returned. **Example:** ```JavaScript -ipfs.object.patch.setData(multihash, new Buffer('more data'), (err, node) => { +ipfs.object.patch.setData(multihash, new Buffer('more data'), (err, cid) => { if (err) { throw err } }) - ``` A great source of [examples][] can be found in the tests for this API. +[CID]: https://github.com/multiformats/js-cid [DAGNode]: https://github.com/ipld/js-ipld-dag-pb [multihash]: http://github.com/multiformats/multihash [promise]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise diff --git a/SPEC/TYPES.md b/SPEC/TYPES.md index bd76e4ac..0c587154 100644 --- a/SPEC/TYPES.md +++ b/SPEC/TYPES.md @@ -8,6 +8,4 @@ A set of data types are exposed directly from the IPFS instance under `ipfs.type - [`ipfs.types.multiaddr`](https://github.com/multiformats/js-multiaddr) - [`ipfs.types.multibase`](https://github.com/multiformats/multibase) - [`ipfs.types.multihash`](https://github.com/multiformats/js-multihash) -- [`ipfs.types.CID`](https://github.com/ipld/js-cid) -- [`ipfs.types.dagPB`](https://github.com/ipld/js-ipld-dag-pb) -- [`ipfs.types.dagCBOR`](https://github.com/ipld/js-ipld-dag-cbor) +- [`ipfs.types.CID`](https://github.com/multiformats/js-cid) diff --git a/js/src/object/data.js b/js/src/object/data.js index e0a58ba9..cd453216 100644 --- a/js/src/object/data.js +++ b/js/src/object/data.js @@ -5,9 +5,6 @@ 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) @@ -42,23 +39,19 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, nodeCid) => { expect(err).to.not.exist() - calculateCid(node, (err, nodeCid) => { + ipfs.object.data(nodeCid, (err, data) => { expect(err).to.not.exist() - 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() - }) + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(testObj.Data).to.eql(data) + done() }) }) }) @@ -69,8 +62,7 @@ module.exports = (createCommon, options) => { Links: [] } - const node = await ipfs.object.put(testObj) - const nodeCid = await calculateCid(node) + const nodeCid = await ipfs.object.put(testObj) let data = await ipfs.object.data(nodeCid) // because js-ipfs-api can't infer @@ -78,7 +70,7 @@ module.exports = (createCommon, options) => { if (typeof data === 'string') { data = Buffer.from(data) } - expect(node.data).to.deep.equal(data) + expect(testObj.Data).to.deep.equal(data) }) it('should get data by base58 encoded multihash', (done) => { @@ -87,23 +79,19 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, nodeCid) => { expect(err).to.not.exist() - calculateCid(node, (err, nodeCid) => { + ipfs.object.data(bs58.encode(nodeCid.buffer), { enc: 'base58' }, (err, data) => { expect(err).to.not.exist() - 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() - }) + // because js-ipfs-api can't infer + // if the returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(testObj.Data).to.eql(data) + done() }) }) }) @@ -114,23 +102,19 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, nodeCid) => { expect(err).to.not.exist() - calculateCid(node, (err, nodeCid) => { + ipfs.object.data(bs58.encode(nodeCid.buffer).toString(), { enc: 'base58' }, (err, data) => { expect(err).to.not.exist() - 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() - }) + // because js-ipfs-api can't infer if the + // returned Data is Buffer or String + if (typeof data === 'string') { + data = Buffer.from(data) + } + expect(testObj.Data).to.eql(data) + done() }) }) }) diff --git a/js/src/object/get.js b/js/src/object/get.js index 3566740a..0eaba527 100644 --- a/js/src/object/get.js +++ b/js/src/object/get.js @@ -8,10 +8,7 @@ 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') +const { asDAGLink } = require('./utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -52,15 +49,13 @@ module.exports = (createCommon, options) => { series([ (cb) => { - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - node1 = node + node1Cid = cid - calculateCid(node, (err, result) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - - node1Cid = result - + node1 = node cb() }) }) @@ -92,8 +87,8 @@ module.exports = (createCommon, options) => { Links: [] } - const node1 = await ipfs.object.put(testObj) - const node1Cid = await calculateCid(node1) + const node1Cid = await ipfs.object.put(testObj) + const node1 = await ipfs.object.get(node1Cid) const node2 = await ipfs.object.get(node1Cid) // because js-ipfs-api can't infer if the @@ -115,19 +110,16 @@ module.exports = (createCommon, options) => { let node1 let node1Cid let node2 - let node2Cid series([ (cb) => { - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - node1 = node + node1Cid = cid - calculateCid(node, (err, result) => { + ipfs.object.get(node1Cid, (err, node) => { expect(err).to.not.exist() - - node1Cid = result - + node1 = node cb() }) }) @@ -142,20 +134,12 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node2 = node - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node2Cid = result - - cb() - }) + cb() }) }, (cb) => { expect(node1.data).to.eql(node2.data) expect(node1.links).to.eql(node2.links) - expect(node1Cid).to.deep.eql(node2Cid) cb() } ], done) @@ -167,10 +151,10 @@ module.exports = (createCommon, options) => { Links: [] } - const node1 = await ipfs.object.put(obj) - const node1Cid = await calculateCid(node1) + const node1Cid = await ipfs.object.put(obj) + const node1 = await ipfs.object.get(node1Cid) 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') { @@ -179,7 +163,6 @@ module.exports = (createCommon, options) => { 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) => { @@ -187,7 +170,6 @@ module.exports = (createCommon, options) => { let node1b let node1bCid let node1c - let node1cCid let node2 series([ @@ -219,16 +201,10 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - ipfs.object.put(node1b, (err, node) => { + ipfs.object.put(node1b, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1bCid = result - - cb() - }) + node1bCid = cid + cb() }) }, (cb) => { @@ -242,19 +218,10 @@ module.exports = (createCommon, options) => { } node1c = node - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1cCid = result - - cb() - }) }) }, (cb) => { expect(node1a.data).to.eql(node1c.data) - expect(node1bCid).to.eql(node1cCid) cb() } ], done) @@ -269,19 +236,16 @@ module.exports = (createCommon, options) => { let node1a let node1aCid let node1b - let node1bCid series([ (cb) => { - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - node1a = node + node1aCid = cid - calculateCid(node, (err, result) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - - node1aCid = result - + node1a = node cb() }) }) @@ -295,18 +259,10 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node1b = node - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1bCid = result - - cb() - }) + cb() }) }, (cb) => { - expect(node1aCid).to.deep.eql(node1bCid) expect(node1a.data).to.eql(node1b.data) expect(node1a.links).to.eql(node1b.links) cb() @@ -323,19 +279,16 @@ module.exports = (createCommon, options) => { let node1a let node1aCid let node1b - let node1bCid series([ (cb) => { - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - node1a = node + node1aCid = cid - calculateCid(node, (err, result) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - - node1aCid = result - + node1a = node cb() }) }) @@ -349,18 +302,9 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node1b = node - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1bCid = result - - cb() - }) }) }, (cb) => { - expect(node1aCid).to.deep.eql(node1bCid) expect(node1a.data).to.eql(node1b.data) expect(node1a.links).to.eql(node1b.links) cb() @@ -368,7 +312,7 @@ module.exports = (createCommon, options) => { ], done) }) - it('supplies unadulterated data', () => { + it('should supply unaltered data', () => { // has to be big enough to span several DAGNodes let required = 1024 * 3000 diff --git a/js/src/object/links.js b/js/src/object/links.js index a86dbc5b..b1686662 100644 --- a/js/src/object/links.js +++ b/js/src/object/links.js @@ -7,10 +7,7 @@ const DAGNode = dagPB.DAGNode const series = require('async/series') const hat = require('hat') const { getDescribe, getIt, expect } = require('../utils/mocha') -const { - calculateCid, - asDAGLink -} = require('../utils/dag-pb') +const { asDAGLink } = require('./utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -45,10 +42,10 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() ipfs.object.links(cid, (err, links) => { @@ -66,8 +63,8 @@ module.exports = (createCommon, options) => { Links: [] } - const node = await ipfs.object.put(testObj) - const cid = await calculateCid(node) + const cid = await ipfs.object.put(testObj) + const node = await ipfs.object.get(cid) const links = await ipfs.object.links(cid) expect(node.links).to.eql(links) @@ -99,15 +96,13 @@ module.exports = (createCommon, options) => { asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - DAGNode.addLink(node1a, link, (err, node) => { + DAGNode.addLink(node1a, link, (err, cid) => { expect(err).to.not.exist() - node1b = node + node1bCid = cid - calculateCid(node, (err, cid) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - - node1bCid = cid - + node1b = node cb() }) }) @@ -132,10 +127,10 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() ipfs.object.links(cid.buffer, { enc: 'base58' }, (err, links) => { @@ -153,10 +148,10 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() ipfs.object.links(cid.toBaseEncodedString(), { enc: 'base58' }, (err, links) => { diff --git a/js/src/object/new.js b/js/src/object/new.js index c61b7276..bcd78c14 100644 --- a/js/src/object/new.js +++ b/js/src/object/new.js @@ -2,9 +2,6 @@ 'use strict' const { getDescribe, getIt, expect } = require('../utils/mocha') -const { - calculateCid -} = require('../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -34,34 +31,29 @@ module.exports = (createCommon, options) => { after((done) => common.teardown(done)) it('should create a new object with no template', (done) => { - ipfs.object.new((err, node) => { + ipfs.object.new((err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid.toBaseEncodedString()).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') - done() - }) + expect(cid.toBaseEncodedString()).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') + done() }) }) it('should create a new object with no template (promised)', async () => { - const node = await ipfs.object.new() - const cid = await calculateCid(node) - + const cid = await ipfs.object.new() expect(cid.toBaseEncodedString()).to.equal('QmdfTbBqBPQ7VNxZEYEj14VmRuZBkqFbiwReogJgS1zR1n') }) it('should create a new object with unixfs-dir template', (done) => { - ipfs.object.new('unixfs-dir', (err, node) => { + ipfs.object.new('unixfs-dir', (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, cid) => { - expect(err).to.not.exist() - expect(cid.toBaseEncodedString()).to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') - done() - }) + expect(cid.toBaseEncodedString()).to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + done() }) }) + + it('should create a new object with unixfs-dir template (promised)', async () => { + const cid = await ipfs.object.new('unixfs-dir') + expect(cid.toBaseEncodedString()).to.equal('QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn') + }) }) } diff --git a/js/src/object/patch/add-link.js b/js/src/object/patch/add-link.js index 8a8d3e80..6f7e8b72 100644 --- a/js/src/object/patch/add-link.js +++ b/js/src/object/patch/add-link.js @@ -6,10 +6,9 @@ const DAGNode = dagPB.DAGNode const series = require('async/series') const { getDescribe, getIt, expect } = require('../../utils/mocha') const { - calculateCid, createDAGNode, addLinkToDAGNode -} = require('../../utils/dag-pb') +} = require('../utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -39,11 +38,12 @@ module.exports = (createCommon, options) => { after((done) => common.teardown(done)) it('should add a link to an existing node', (done) => { - let testNodeMultihash - let node1bMultihash + let testNodeCid + let node1bCid let node1a let node1b let node2 + let node2Cid const obj = { Data: Buffer.from('patch test object'), @@ -52,14 +52,10 @@ module.exports = (createCommon, options) => { series([ (cb) => { - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - testNodeMultihash = result - cb() - }) + testNodeCid = cid + cb() }) }, (cb) => { @@ -79,37 +75,32 @@ module.exports = (createCommon, options) => { (cb) => { // note: we need to put the linked obj, otherwise IPFS won't // timeout. Reason: it needs the node to get its size - ipfs.object.put(node2, cb) + ipfs.object.put(node2, (err, cid) => { + expect(err).to.not.exist() + node2Cid = cid + cb() + }) }, (cb) => { - calculateCid(node2, (err, result) => { + DAGNode.addLink(node1a, { + name: 'link-to-node', + size: node2.toJSON().size, + cid: node2Cid + }, (err, cid) => { expect(err).to.not.exist() + node1bCid = cid - DAGNode.addLink(node1a, { - name: 'link-to-node', - size: node2.toJSON().size, - cid: result - }, (err, node) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() node1b = node - - calculateCid(node1b, (err, result) => { - expect(err).to.not.exist() - node1bMultihash = result - cb() - }) + cb() }) }) }, (cb) => { - ipfs.object.patch.addLink(testNodeMultihash, node1b.links[0], (err, node) => { + ipfs.object.patch.addLink(testNodeCid, node1b.links[0], (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - expect(node1bMultihash).to.eql(result) - cb() - }) + expect(node1bCid).to.eql(cid) }) } /* TODO: revisit this assertions. @@ -150,19 +141,17 @@ module.exports = (createCommon, options) => { Links: [] } - 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, { + 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 child = await ipfs.object.get(childCid) + const newParentCid = 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) + const newParent = await ipfs.object.get(newParentCid) + const nodeFromObjectPatchCid = await ipfs.object.patch.addLink(parentCid, newParent.links[0]) expect(newParentCid).to.eql(nodeFromObjectPatchCid) }) diff --git a/js/src/object/patch/append-data.js b/js/src/object/patch/append-data.js index dd4eda9e..6d313674 100644 --- a/js/src/object/patch/append-data.js +++ b/js/src/object/patch/append-data.js @@ -3,9 +3,6 @@ 'use strict' const { getDescribe, getIt, expect } = require('../../utils/mocha') -const { - calculateCid -} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -40,22 +37,13 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, nodeCid) => { expect(err).to.not.exist() - calculateCid(node, (err, nodeCid) => { + ipfs.object.patch.appendData(nodeCid, Buffer.from('append'), (err, patchedNodeCid) => { expect(err).to.not.exist() - - 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() - }) - }) + expect(patchedNodeCid).to.not.deep.equal(nodeCid) + done() }) }) }) @@ -66,10 +54,8 @@ module.exports = (createCommon, options) => { Links: [] } - 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) + const nodeCid = await ipfs.object.put(obj) + const patchedNodeCid = await ipfs.object.patch.appendData(nodeCid, Buffer.from('append')) 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 c0f2a48f..430db2ed 100644 --- a/js/src/object/patch/rm-link.js +++ b/js/src/object/patch/rm-link.js @@ -5,10 +5,7 @@ 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') +const { asDAGLink } = require('../utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -56,28 +53,20 @@ module.exports = (createCommon, options) => { series([ (cb) => { - ipfs.object.put(obj1, (err, node) => { + ipfs.object.put(obj1, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1aCid = result - - cb() - }) + node1aCid = cid + cb() }) }, (cb) => { - ipfs.object.put(obj2, (err, node) => { + ipfs.object.put(obj2, (err, cid) => { expect(err).to.not.exist() - node2 = node + node2Cid = cid - calculateCid(node, (err, result) => { + ipfs.object.get(cid, (err, node) => { expect(err).to.not.exist() - - node2Cid = result - + node2 = node cb() }) }) @@ -85,30 +74,18 @@ module.exports = (createCommon, options) => { (cb) => { testLink = new DAGLink('link-to-node', node2.size, node2Cid) - ipfs.object.patch.addLink(node1aCid, testLink, (err, node) => { + ipfs.object.patch.addLink(node1aCid, testLink, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - node1bCid = result - - cb() - }) + node1bCid = cid + cb() }) }, (cb) => { - ipfs.object.patch.rmLink(node1bCid, testLink, (err, node) => { + ipfs.object.patch.rmLink(node1bCid, testLink, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, result) => { - expect(err).to.not.exist() - - expect(result).to.not.deep.equal(node1bCid) - expect(result).to.deep.equal(node1aCid) - - cb() - }) + expect(cid).to.not.deep.equal(node1bCid) + expect(cid).to.deep.equal(node1aCid) + cb() }) } /* TODO: revisit this assertions. @@ -134,14 +111,12 @@ module.exports = (createCommon, options) => { Links: [] } - const node = await ipfs.object.put(obj1) - const nodeCid = await calculateCid(node) - const child = await ipfs.object.put(obj2) + const nodeCid = await ipfs.object.put(obj1) + const childCid = await ipfs.object.put(obj2) + const child = await ipfs.object.get(childCid) 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) + const parentCid = await ipfs.object.patch.addLink(nodeCid, childAsDAGLink) + const withoutChildCid = await ipfs.object.patch.rmLink(parentCid, childAsDAGLink) 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 cd9723d1..6f7c58e8 100644 --- a/js/src/object/patch/set-data.js +++ b/js/src/object/patch/set-data.js @@ -3,9 +3,6 @@ 'use strict' const { getDescribe, getIt, expect } = require('../../utils/mocha') -const { - calculateCid -} = require('../../utils/dag-pb') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -39,21 +36,19 @@ module.exports = (createCommon, options) => { Data: Buffer.from('patch test object'), Links: [] } + const patchData = Buffer.from('set') - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, nodeCid) => { expect(err).to.not.exist() - calculateCid(node, (err, nodeCid) => { + ipfs.object.patch.setData(nodeCid, patchData, (err, patchedNodeCid) => { expect(err).to.not.exist() + expect(nodeCid).to.not.deep.equal(patchedNodeCid) - ipfs.object.patch.setData(nodeCid, Buffer.from('set'), (err, patchedNode) => { + ipfs.object.get(patchedNodeCid, (err, patchedNode) => { expect(err).to.not.exist() - - calculateCid(patchedNode, (err, patchedNodeCid) => { - expect(err).to.not.exist() - - done() - }) + expect(patchedNode.data).to.eql(patchData) + done() }) }) }) @@ -64,13 +59,14 @@ module.exports = (createCommon, options) => { Data: Buffer.from('patch test object (promised)'), Links: [] } + const patchData = Buffer.from('set') - 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) + const nodeCid = await ipfs.object.put(obj) + const patchedNodeCid = await ipfs.object.patch.setData(nodeCid, patchData) + const patchedNode = await ipfs.object.get(patchedNodeCid) expect(nodeCid).to.not.deep.equal(patchedNodeCid) + expect(patchedNode.data).to.eql(patchData) }) }) } diff --git a/js/src/object/put.js b/js/src/object/put.js index f5addf21..b297e0cc 100644 --- a/js/src/object/put.js +++ b/js/src/object/put.js @@ -6,9 +6,7 @@ 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') +const { asDAGLink } = require('./utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -43,12 +41,16 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(obj, (err, node) => { + ipfs.object.put(obj, (err, cid) => { expect(err).to.not.exist() - const nodeJSON = node.toJSON() - expect(nodeJSON.data).to.eql(obj.Data) - expect(nodeJSON.links).to.eql(obj.Links) - done() + + ipfs.object.get(cid, (err, node) => { + expect(err).to.not.exist() + const nodeJSON = node.toJSON() + expect(nodeJSON.data).to.eql(obj.Data) + expect(nodeJSON.links).to.eql(obj.Links) + done() + }) }) }) @@ -58,7 +60,8 @@ module.exports = (createCommon, options) => { Links: [] } - const node = await ipfs.object.put(obj) + const cid = await ipfs.object.put(obj) + const node = await ipfs.object.get(cid) const nodeJSON = node.toJSON() expect(obj.Data).to.deep.equal(nodeJSON.data) @@ -78,12 +81,15 @@ module.exports = (createCommon, options) => { const buf = Buffer.from(JSON.stringify(obj2)) - ipfs.object.put(buf, { enc: 'json' }, (err, node) => { + ipfs.object.put(buf, { enc: 'json' }, (err, cid) => { expect(err).to.not.exist() - const nodeJSON = node.toJSON() - expect(nodeJSON.data).to.eql(node.data) - done() + ipfs.object.get(cid, (err, node) => { + expect(err).to.not.exist() + const nodeJSON = node.toJSON() + expect(nodeJSON.data).to.eql(node.data) + done() + }) }) }) @@ -107,11 +113,14 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - ipfs.object.put(serialized, { enc: 'protobuf' }, (err, storedNode) => { + ipfs.object.put(serialized, { enc: 'protobuf' }, (err, cid) => { expect(err).to.not.exist() - expect(node.data).to.deep.equal(node.data) - expect(node.links).to.deep.equal(node.links) - cb() + 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) @@ -119,23 +128,31 @@ module.exports = (createCommon, options) => { it('should put a Buffer as data', (done) => { const data = Buffer.from(hat()) - ipfs.object.put(data, (err, node) => { + ipfs.object.put(data, (err, cid) => { expect(err).to.not.exist() - const nodeJSON = node.toJSON() - expect(data).to.deep.equal(nodeJSON.data) - expect([]).to.deep.equal(nodeJSON.links) - done() + + ipfs.object.get(cid, (err, node) => { + expect(err).to.not.exist() + const nodeJSON = node.toJSON() + expect(data).to.deep.equal(nodeJSON.data) + expect([]).to.deep.equal(nodeJSON.links) + done() + }) }) }) it('should put a Protobuf DAGNode', (done) => { DAGNode.create(Buffer.from(hat()), (err, dNode) => { expect(err).to.not.exist() - ipfs.object.put(dNode, (err, node) => { + ipfs.object.put(dNode, (err, cid) => { 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() + }) }) }) }) @@ -179,12 +196,16 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - ipfs.object.put(node1b, (err, node) => { + ipfs.object.put(node1b, (err, cid) => { 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() + + 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())) + cb() + }) }) } ], done) diff --git a/js/src/object/stat.js b/js/src/object/stat.js index ef4942ae..6e1e867a 100644 --- a/js/src/object/stat.js +++ b/js/src/object/stat.js @@ -6,10 +6,7 @@ const dagPB = require('ipld-dag-pb') const DAGNode = dagPB.DAGNode const series = require('async/series') const { getDescribe, getIt, expect } = require('../utils/mocha') -const { - calculateCid, - asDAGLink -} = require('../utils/dag-pb') +const { asDAGLink } = require('./utils') module.exports = (createCommon, options) => { const describe = getDescribe(options) @@ -44,25 +41,21 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.stat(cid, (err, stats) => { expect(err).to.not.exist() - - ipfs.object.stat(cid, (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() }) }) }) @@ -123,16 +116,10 @@ module.exports = (createCommon, options) => { }) }, (cb) => { - ipfs.object.put(node1b, (err, node) => { + ipfs.object.put(node1b, (err, cid) => { expect(err).to.not.exist() - - calculateCid(node, (err, cid) => { - expect(err).to.not.exist() - - node1bCid = cid - - cb() - }) + node1bCid = cid + cb() }) }, (cb) => { @@ -159,25 +146,21 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.stat(cid.buffer, { enc: 'base58' }, (err, stats) => { expect(err).to.not.exist() - - 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() - }) + const expected = { + Hash: 'QmNggDXca24S6cMPEYHZjeuc4QRmofkRrAEqVL3Ms2sdJZ', + NumLinks: 0, + BlockSize: 17, + LinksSize: 2, + DataSize: 15, + CumulativeSize: 17 + } + expect(expected).to.deep.equal(stats) + done() }) }) }) @@ -188,25 +171,21 @@ module.exports = (createCommon, options) => { Links: [] } - ipfs.object.put(testObj, (err, node) => { + ipfs.object.put(testObj, (err, cid) => { expect(err).to.not.exist() - calculateCid(node, (err, cid) => { + ipfs.object.stat(cid.toBaseEncodedString(), { enc: 'base58' }, (err, stats) => { expect(err).to.not.exist() - - 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() - }) + 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/object/utils.js similarity index 65% rename from js/src/utils/dag-pb.js rename to js/src/object/utils.js index f199e063..d26af87e 100644 --- a/js/src/utils/dag-pb.js +++ b/js/src/object/utils.js @@ -1,19 +1,8 @@ '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) -}) +const { promisify } = require('es6-promisify') +const dagPb = require('ipld-dag-pb') +const { DAGNode, DAGLink } = dagPb module.exports.createDAGNode = promisify((data, links, cb) => { DAGNode.create(data, links, cb) @@ -29,7 +18,7 @@ module.exports.asDAGLink = promisify((node, name, cb) => { name = '' } - cid(node, (err, nodeCid) => { + dagPb.util.cid(node, (err, nodeCid) => { if (err) { return cb(err) } From 6b95a0d0d1b6eb73cdcfdf9a7789193e3fee016b Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 27 Nov 2018 10:50:21 +0000 Subject: [PATCH 2/3] fix: fixups License: MIT Signed-off-by: Alan Shaw --- js/src/object/get.js | 2 ++ js/src/object/links.js | 8 ++++---- js/src/object/patch/add-link.js | 14 ++++++++------ js/src/object/utils.js | 10 +++++++--- 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/js/src/object/get.js b/js/src/object/get.js index 0eaba527..14087be5 100644 --- a/js/src/object/get.js +++ b/js/src/object/get.js @@ -218,6 +218,7 @@ module.exports = (createCommon, options) => { } node1c = node + cb() }) }, (cb) => { @@ -302,6 +303,7 @@ module.exports = (createCommon, options) => { node.data = Buffer.from(node.data) } node1b = node + cb() }) }, (cb) => { diff --git a/js/src/object/links.js b/js/src/object/links.js index b1686662..04b75759 100644 --- a/js/src/object/links.js +++ b/js/src/object/links.js @@ -96,13 +96,13 @@ module.exports = (createCommon, options) => { asDAGLink(node2, 'some-link', (err, link) => { expect(err).to.not.exist() - DAGNode.addLink(node1a, link, (err, cid) => { + DAGNode.addLink(node1a, link, (err, node) => { expect(err).to.not.exist() - node1bCid = cid + node1b = node - ipfs.object.get(cid, (err, node) => { + dagPB.util.cid(node, (err, cid) => { expect(err).to.not.exist() - node1b = node + node1bCid = cid cb() }) }) diff --git a/js/src/object/patch/add-link.js b/js/src/object/patch/add-link.js index 6f7e8b72..5d8fc86a 100644 --- a/js/src/object/patch/add-link.js +++ b/js/src/object/patch/add-link.js @@ -6,6 +6,7 @@ const DAGNode = dagPB.DAGNode const series = require('async/series') const { getDescribe, getIt, expect } = require('../../utils/mocha') const { + calculateCid, createDAGNode, addLinkToDAGNode } = require('../utils') @@ -86,13 +87,13 @@ module.exports = (createCommon, options) => { name: 'link-to-node', size: node2.toJSON().size, cid: node2Cid - }, (err, cid) => { + }, (err, node) => { expect(err).to.not.exist() - node1bCid = cid + node1b = node - ipfs.object.get(cid, (err, node) => { + dagPB.util.cid(node, (err, cid) => { expect(err).to.not.exist() - node1b = node + node1bCid = cid cb() }) }) @@ -101,6 +102,7 @@ module.exports = (createCommon, options) => { ipfs.object.patch.addLink(testNodeCid, node1b.links[0], (err, cid) => { expect(err).to.not.exist() expect(node1bCid).to.eql(cid) + cb() }) } /* TODO: revisit this assertions. @@ -145,12 +147,12 @@ module.exports = (createCommon, options) => { const parent = await ipfs.object.get(parentCid) const childCid = await ipfs.object.put(await createDAGNode(Buffer.from('some other node'), [])) const child = await ipfs.object.get(childCid) - const newParentCid = await addLinkToDAGNode(parent, { + const newParent = await addLinkToDAGNode(parent, { name: 'link-to-node', size: child.size, cid: childCid }) - const newParent = await ipfs.object.get(newParentCid) + const newParentCid = await calculateCid(newParent) const nodeFromObjectPatchCid = await ipfs.object.patch.addLink(parentCid, newParent.links[0]) expect(newParentCid).to.eql(nodeFromObjectPatchCid) diff --git a/js/src/object/utils.js b/js/src/object/utils.js index d26af87e..fb122a7d 100644 --- a/js/src/object/utils.js +++ b/js/src/object/utils.js @@ -1,8 +1,12 @@ 'use strict' const { promisify } = require('es6-promisify') -const dagPb = require('ipld-dag-pb') -const { DAGNode, DAGLink } = dagPb +const dagPB = require('ipld-dag-pb') +const { DAGNode, DAGLink } = dagPB + +module.exports.calculateCid = promisify((node, cb) => { + dagPB.util.cid(node, cb) +}) module.exports.createDAGNode = promisify((data, links, cb) => { DAGNode.create(data, links, cb) @@ -18,7 +22,7 @@ module.exports.asDAGLink = promisify((node, name, cb) => { name = '' } - dagPb.util.cid(node, (err, nodeCid) => { + dagPB.util.cid(node, (err, nodeCid) => { if (err) { return cb(err) } From 3ad989acad7b70b9f3202534c21d4477b2b17012 Mon Sep 17 00:00:00 2001 From: Alan Shaw Date: Tue, 27 Nov 2018 11:10:57 +0000 Subject: [PATCH 3/3] fix: more fixes License: MIT Signed-off-by: Alan Shaw --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 172a9ca0..80603f96 100644 --- a/package.json +++ b/package.json @@ -48,16 +48,16 @@ "dirty-chai": "^2.0.1", "es6-promisify": "^6.0.1", "hat": "0.0.3", + "into-stream": "^4.0.0", "ipfs-block": "~0.8.0", "ipfs-unixfs": "~0.1.16", "ipld-dag-cbor": "~0.13.0", "ipld-dag-pb": "~0.15.0", "is-ipfs": "~0.4.2", - "libp2p-crypto": "~0.14.0", - "into-stream": "^4.0.0", "is-plain-object": "^2.0.4", + "libp2p-crypto": "~0.14.0", "multiaddr": "^5.0.0", - "multibase": "~0.5.0", + "multibase": "~0.6.0", "multihashes": "~0.4.14", "multihashing-async": "~0.5.1", "peer-id": "~0.12.0",