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

fix: add support for resolving links by name #78

Merged
merged 4 commits into from
Jul 20, 2018
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lib-cov

# Coverage directory used by tools like istanbul
coverage
.nyc_output

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
Expand Down
30 changes: 25 additions & 5 deletions src/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,11 @@ exports.resolve = (binaryBlob, path, callback) => {
// for the resolver
node.links.forEach((l, i) => {
const link = l.toJSON()
values[i] = {
values[i] = values[link.name] = {
hash: link.multihash,
name: link.name,
size: link.size
}
// TODO by enabling something to resolve through link name, we are
// applying a transformation (a view) to the data, confirm if this
// is exactly what we want
values[link.name] = link.multihash
})

let value = values[split[1]]
Expand All @@ -74,6 +70,30 @@ exports.resolve = (binaryBlob, path, callback) => {
} else if (split[0] === 'Data') {
cb(null, { value: node.data, remainderPath: '' })
} else {
// If split[0] is not 'Data' or 'Links' then we might be trying to refer
// to a named link from the Links array. This is because go-ipfs and
// js-ipfs have historically supported the ability to do
// `ipfs dag get CID/a` where a is a named link in a dag-pb.
const values = {}

node.links.forEach((l, i) => {
const link = l.toJSON()
values[link.name] = {
hash: link.multihash,
name: link.name,
size: link.size
}
})

const value = values[split[0]]

if (value) {
return cb(null, {
value: { '/': value.hash },
remainderPath: split.slice(1).join('/')
})
}

cb(new Error('path not available'))
}
}
Expand Down
38 changes: 38 additions & 0 deletions test/resolver.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,23 @@ describe('IPLD Format resolver (local)', () => {
})
})

it('links by name', (done) => {
resolver.resolve(linksNodeBlob, 'named link', (err, result) => {
expect(err).to.not.exist()
expect(result.value['/']).to.eql(links[1].multihash)
expect(result.remainderPath).to.eql('')
done()
})
})

Copy link
Member

Choose a reason for hiding this comment

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

Could you please add tests for:

  • named link and a remainder path
  • error case for non-existent named link

it('missing link by name', (done) => {
resolver.resolve(linksNodeBlob, 'missing link', (err, result) => {
expect(err).to.exist()
expect(err.message).to.equal('path not available')
done()
})
})

it('yield remainderPath if impossible to resolve through (a)', (done) => {
resolver.resolve(linksNodeBlob, 'Links/1/Hash/Data', (err, result) => {
expect(err).to.not.exist()
Expand All @@ -166,6 +183,27 @@ describe('IPLD Format resolver (local)', () => {
done()
})
})

it('yield remainderPath if impossible to resolve through named link (a)', (done) => {
resolver.resolve(linksNodeBlob, 'named link/Data', (err, result) => {
expect(err).to.not.exist()
expect(result.value['/']).to.exist()
expect(result.value['/']).to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V')
expect(result.remainderPath).to.equal('Data')
done()
})
})

it('yield remainderPath if impossible to resolve through named link (b)', (done) => {
resolver.resolve(linksNodeBlob, 'named link/Links/0/Hash/Data', (err, result) => {
expect(err).to.not.exist()
expect(result.value['/'])
.to.equal('QmXg9Pp2ytZ14xgmQjYEiHjVjMFXzCVVEcRTWJBmLgR39V')

expect(result.remainderPath).to.equal('Links/0/Hash/Data')
done()
})
})
})

it('resolver.tree', (done) => {
Expand Down