-
Notifications
You must be signed in to change notification settings - Fork 295
[WIP] add missing dag.tree() #819
base: master
Are you sure you want to change the base?
Changes from 2 commits
e1a6a59
046d305
96843ae
341609e
a2290d1
319f764
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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`), null) | ||
| } | ||
| }) | ||
| }) | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* eslint-env mocha */ | ||
| 'use strict' | ||
|
|
||
| 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 | ||
|
|
||
| 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) | ||
| }) | ||
|
|
||
| it('.dag.tree', (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('.dag.put', (done) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not know how to name this test. I looked into
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Next time this should be a separate commit, as it is unrelated to adding the |
||
| 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) => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| }) | ||
| }) | ||
| }) | ||
| }) | ||
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
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.