Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

feat: dag.resolve API #113

Merged
merged 1 commit into from
Feb 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions API/dag/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <daviddias.p@gmail.com>",
Expand Down
153 changes: 153 additions & 0 deletions src/dag-resolve.js
Original file line number Diff line number Diff line change
@@ -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', () => {})
})
})
}
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')