-
Notifications
You must be signed in to change notification settings - Fork 296
[WIP] add missing dag.tree() #819
base: master
Are you sure you want to change the base?
Changes from 1 commit
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, null) | ||
|
||
let codec = ipfsBlock.cid.codec | ||
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. Nit-pick: This could be a |
||
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 | ||
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 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 commentThe 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) | ||
}) | ||
|
||
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) => { | ||
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. 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 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 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) => { | ||
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. 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.
In the error callbacks, the return value is normally
undefined
and notnull
. So this would then just bereturn callback(err)
.