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

[WIP] add missing dag.tree() #819

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/dag/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module.exports = (arg) => {

return {
get: require('./get')(send),
put: require('./put')(send)
put: require('./put')(send),
tree: require('./tree')(send)
}
}
37 changes: 37 additions & 0 deletions src/dag/tree.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
'use strict'
const promisify = require('promisify-es6')
const block = require('../block')

const DAGFormats = {
'dag-cbor': require('ipld-dag-cbor'),
'dag-pb': require('ipld-dag-pb')
}

module.exports = (send) => {
return promisify((cid, path, options, callback) => {
if (typeof path === 'function') {
callback = path
path = undefined
}

if (typeof options === 'function') {
callback = options
options = {}
}

options = options || {}
path = path || ''

// FIXME: handle case when options.recursive is true
block(send).get(cid, options, (err, ipfsBlock) => {
if (err) return callback(err)

const codec = ipfsBlock.cid.codec
if (codec in DAGFormats) {
DAGFormats[codec].resolver.tree(ipfsBlock.data, callback)
} else {
callback(new Error(`codec ${codec} is not valid`))
}
})
})
}
89 changes: 89 additions & 0 deletions test/dag.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/* eslint-env mocha */
/* eslint max-nested-callbacks: ["error", 8] */
'use strict'

const CID = require('cids')
const chai = require('chai')
const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)

const IPFSApi = require('../src')
const f = require('./utils/factory')

describe('.dag', function () {
this.timeout(50 * 1000) // slow CI
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that was copy&pasted. Generally try to stick to the default timeouts and see if they are long enough,

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried with the default timeout but it was not enough.


let ipfs
let ipfsd

const obj = {
a: 1,
b: [1, 2, 3],
c: {
ca: [5, 6, 7],
cb: 'foo'
}
}

before((done) => {
f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
expect(err).to.not.exist()
ipfsd = _ipfsd
ipfs = IPFSApi(_ipfsd.apiAddr)
done()
})
})

after((done) => {
if (!ipfsd) return done()
ipfsd.stop(done)
})

describe('.dag.tree', () => {
it('should return all the paths', (done) => {
const expectedPaths = [
'a', 'b', 'b/0', 'b/1', 'b/2', 'c', 'c/ca',
'c/ca/0', 'c/ca/1', 'c/ca/2', 'c/cb'
]
ipfs.dag.put(obj, (err, cid) => {
expect(err).to.not.exist()
ipfs.dag.tree(cid, (err, paths) => {
expect(err).to.not.exist()
expect(paths).deep.equal(expectedPaths)
done()
})
})
})

it('should return error when codec for cid is invalid', (done) => {
let cid = new CID('zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619')
cid.codec = 'invalid-codec'
ipfs.dag.tree(cid, (err, paths) => {
expect(err).to.exist()
done()
})
})
})

it('.dag.put', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a left-over from copy&pasting. Please remove it if that's the case.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not know how to name this test. I looked into log.spec.js and repo.spec.js and tried to follow the convention there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just wondering as this test doesn't have a call to tree().

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm sorry, my review was sloppy. I thought I was still looking at the file for the tree() tests. I missed that you've added another test file for testing put() and get(). Thanks for that.

Next time this should be a separate commit, as it is unrelated to adding the dag.tree(). Commits should be self contained. The reason is that it makes it easier if you look through the Git history, but also if you e.g. need to revert a change. If we would need to revert the commit where dag.tree() was added, suddenly unrelated tests would be removed as well.

let expectedCidStr = 'zdpuArMWc9Ee3B7zUDucRjvA1bDgYpWt8rpUXXjY3tbmBw619'
ipfs.dag.put(obj, (err, cid) => {
expect(err).to.not.exist()
let cidStr = cid.toBaseEncodedString()
expect(cidStr).to.be.equal(expectedCidStr)
done()
})
})

it('.dag.get', (done) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a left-over from copy&pasting. Please remove it if that's the case.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comment above.

ipfs.dag.put(obj, (err, cid) => {
expect(err).to.not.exist()
ipfs.dag.get(cid, (err, data) => {
expect(err).to.not.exist()
expect(data.value).deep.equal(obj)
done()
})
})
})
})