diff --git a/API/dag/README.md b/API/dag/README.md index 82a7cbfc6..71aaa76f4 100644 --- a/API/dag/README.md +++ b/API/dag/README.md @@ -30,4 +30,17 @@ If no `callback` is passed, a [promise][] is returned. `callback` must follow `function (err, dagNode) {}` signature, where `err` is an error if the operation was not successful and `dagNode` is the IPLD format DAG node retrieved. +#### `dag.resolve` + +> Resolves an IPLD path + +##### `Go` **WIP** + +##### `JavaScript` - ipfs.dag.resolve(cid, path, callback) + +- `cid` is a [CID][https://github.com/ipfs/js-cid] instance. +- `path` is a String that represents a valid path to be resolved + +`callback` must follow `function (err, value) {}` signature, where `err` is an error if the operation was not successful and `value` is the value it was retrieved. + If no `callback` is passed, a [promise][] is returned. diff --git a/package.json b/package.json index a2b81e0cb..5e2d37890 100644 --- a/package.json +++ b/package.json @@ -33,14 +33,14 @@ "concat-stream": "^1.6.0", "detect-node": "^2.0.3", "ipfs-block": "^0.5.4", - "ipld-dag-cbor": "^0.8.5", - "ipld-dag-pb": "^0.9.3", - "multiaddr": "^2.1.1", - "multihashes": "^0.3.1", + "ipld-dag-cbor": "^0.9.0", + "ipld-dag-pb": "^0.9.4", + "multiaddr": "^2.2.0", + "multihashes": "^0.3.2", "readable-stream": "2.2.2" }, "devDependencies": { - "aegir": "^9.3.0" + "aegir": "^9.4.0" }, "contributors": [ "David Dias ", diff --git a/src/dag-resolve.js b/src/dag-resolve.js new file mode 100644 index 000000000..903b9918e --- /dev/null +++ b/src/dag-resolve.js @@ -0,0 +1,153 @@ +/* eslint-env mocha */ +/* eslint max-nested-callbacks: ["error", 8] */ + +'use strict' + +const expect = require('chai').expect +const dagPB = require('ipld-dag-pb') +const dagCBOR = require('ipld-dag-cbor') +const series = require('async/series') +const pull = require('pull-stream') + +module.exports = (common) => { + describe.only('.dag.resolve', () => { + let ipfs + let nodePb + let nodeCbor + let cidPb + let cidCbor + + before((done) => { + common.setup((err, factory) => { + expect(err).to.not.exist + factory.spawnNode((err, node) => { + expect(err).to.not.exist + ipfs = node + done() + }) + }) + }) + + after((done) => { + common.teardown(done) + }) + + it('populate', (done) => { + series([ + (cb) => { + dagPB.DAGNode.create(new Buffer('I am inside a Protobuf'), (err, node) => { + expect(err).to.not.exist + nodePb = node + cb() + }) + }, + (cb) => { + dagPB.util.cid(nodePb, (err, cid) => { + expect(err).to.not.exist + cidPb = cid + cb() + }) + }, + (cb) => { + nodeCbor = { + someData: 'I am inside a Cbor object', + pb: { '/': cidPb.toBaseEncodedString() } + } + + dagCBOR.util.cid(nodeCbor, (err, cid) => { + expect(err).to.not.exist + cidCbor = cid + console.log(nodeCbor) + cb() + }) + } + ], store) + + function store () { + pull( + pull.values([ + { node: nodePb, multicodec: 'dag-pb', hashAlg: 'sha2-256' }, + { node: nodeCbor, multicodec: 'dag-cbor', hashAlg: 'sha2-256' } + ]), + pull.asyncMap((el, cb) => { + ipfs.dag.put(el.node, el.multicodec, el.hashAlg, (err) => { + if (err) { + console.log(err) + } + console.log('put', el.multicodec) + + cb() + }) + }), + pull.onEnd(done) + ) + } + }) + + describe('callback API', () => { + describe('.resolve', () => { + it('dag-pb get the node', (done) => { + ipfs.dag.resolve(cidPb, '/', (err, result) => { + expect(err).to.not.exist + + dagPB.util.cid(result, (err, cid) => { + expect(err).to.not.exist + expect(cid).to.eql(cidPb) + done() + }) + }) + }) + + it('dag-pb local scope', (done) => { + ipfs.dag.resolve(cidPb, 'data', (err, result) => { + expect(err).to.not.exist + expect(result).to.eql(new Buffer('I am inside a Protobuf')) + done() + }) + }) + + it.skip('dag-pb one level', (done) => {}) + it.skip('dag-pb two levels', (done) => {}) + + it('dag-cbor get the node', (done) => { + ipfs.dag.get(cidCbor, (err, result) => { + // ipfs.dag.resolve(cidCbor, '/', (err, result) => { + expect(err).to.not.exist + + console.log('get the node') + + dagCBOR.util.cid(result, (err, cid) => { + expect(err).to.not.exist + expect(cid).to.eql(cidCbor) + done() + }) + }) + }) + + it('dag-cbor local scope', (done) => { + ipfs.dag.resolve(cidCbor, 'someData', (err, result) => { + expect(err).to.not.exist + expect(result).to.eql('I am inside a Cbor object') + done() + }) + }) + + it.skip('dag-cbor one level', (done) => {}) + it.skip('dag-cbor two levels', (done) => {}) + it.skip('from dag-pb to dag-cbor', (done) => {}) + + it('from dag-cbor to dag-pb', (done) => { + ipfs.dag.resolve(cidCbor, 'pb/data', (err, result) => { + expect(err).to.not.exist + expect(result).to.eql(new Buffer('I am inside a Protobuf')) + done() + }) + }) + }) + }) + + describe('promise API', () => { + describe('.resolve', () => {}) + }) + }) +} diff --git a/src/index.js b/src/index.js index 4a4a4e131..19f45844c 100644 --- a/src/index.js +++ b/src/index.js @@ -9,4 +9,5 @@ exports.swarm = require('./swarm') exports.block = require('./block') exports.dht = require('./dht') exports.dag = require('./dag') +exports.dagResolve = require('./dag-resolve') exports.pubsub = require('./pubsub')