Skip to content
This repository has been archived by the owner on Aug 11, 2021. It is now read-only.

Commit

Permalink
feat: return cid of last node traversed from ipld.get and friends (#181)
Browse files Browse the repository at this point in the history
We can pass a path to `ipld.get` that makes the resolver attempt to
do a graph traversal until it runs out of path segments to resolve.

At that point we don't know the `cid` of the node that will be returned,
which things like the `unixfs-exporter` need to know.
  • Loading branch information
achingbrain authored and vmx committed Nov 19, 2018
1 parent 62b0bf2 commit ebcc541
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ It may contain any of the following:

- `value` - the value that resulted from the get
- `remainderPath` - If it didn't manage to successfully resolve the whole path through or if simply the `localResolve` option was passed.
- `cid` - Where the graph traversal finished - if `remainderPath` has a value, this will be where it has its root

### `.getMany(cids, callback)`

Expand Down
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ class IPLDResolver {
}
callback(null, {
value: node,
remainderPath: ''
remainderPath: '',
cid
})
})
}
Expand All @@ -113,6 +114,7 @@ class IPLDResolver {
if (err) {
return cb(err)
}

format.resolver.resolve(block.data, path, (err, result) => {
if (err) {
return cb(err)
Expand All @@ -129,6 +131,8 @@ class IPLDResolver {
const isTerminal = value && !IPLDResolver._maybeCID(value)

if ((endReached && isTerminal) || options.localResolve) {
cid = IPLDResolver._maybeCID(value) || cid

return true
} else {
value = IPLDResolver._maybeCID(value)
Expand All @@ -145,7 +149,8 @@ class IPLDResolver {
}
return callback(null, {
value: value,
remainderPath: path
remainderPath: path,
cid
})
}
)
Expand Down
1 change: 1 addition & 0 deletions test/ipld-dag-cbor.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ module.exports = (repo) => {
expect(err).to.not.exist()
expect(result.value).to.eql('I am 1')
expect(result.remainderPath).to.eql('')
expect(result.cid).to.deep.eql(cid1)

done()
})
Expand Down
9 changes: 9 additions & 0 deletions test/ipld-dag-pb.js
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,15 @@ module.exports = (repo) => {
expect(result.value).to.eql({
'/': 'QmS149H7EbyMuZ2wtEF1sAd7gPwjj4rKAorweAjKMkxr8D'
})
expect(result.cid).to.deep.equal(cid2)
done()
})
})

it('resolver.get value within nested scope (1 level) returns cid of node traversed to', (done) => {
resolver.get(cid2, 'Links/0/Hash/Data', (err, result) => {
expect(err).to.not.exist()
expect(result.cid).to.deep.equal(cid1)
done()
})
})
Expand Down

0 comments on commit ebcc541

Please sign in to comment.