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

Commit

Permalink
feat: resolver guard (graceful failure) (#89)
Browse files Browse the repository at this point in the history
  • Loading branch information
kumavis authored and daviddias committed Aug 26, 2017
1 parent cac1e89 commit 62816a9
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 3 deletions.
30 changes: 27 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ class IPLDResolver {
return cb(err)
}
const r = this.resolvers[cid.codec]
if (!r) {
return cb(new Error('No resolver found for codec "' + cid.codec + '"'))
}
r.resolver.resolve(block, path, (err, result) => {
if (err) {
return cb(err)
Expand Down Expand Up @@ -199,6 +202,9 @@ class IPLDResolver {

options.hashAlg = options.hashAlg || 'sha2-256'
const r = this.resolvers[options.format]
if (!r) {
return callback(new Error('No resolver found for codec "' + options.format + '"'))
}
// TODO add support for different hash funcs in the utils of
// each format (just really needed for CBOR for now, really
// r.util.cid(node1, hashAlg, (err, cid) => {
Expand All @@ -224,13 +230,18 @@ class IPLDResolver {
if (!options.recursive) {
p = pullDeferSource()
const r = this.resolvers[cid.codec]
if (!r) {
p.abort(new Error('No resolver found for codec "' + cid.codec + '"'))
return p
}

waterfall([
(cb) => this.bs.get(cid, cb),
(block, cb) => r.resolver.tree(block, cb)
], (err, paths) => {
if (err) {
return p.abort(err)
p.abort(err)
return p
}
p.resolve(pull.values(paths))
})
Expand All @@ -252,7 +263,12 @@ class IPLDResolver {
}

const deferred = pullDeferSource()
const r = this.resolvers[el.cid.codec]
const cid = el.cid
const r = this.resolvers[cid.codec]
if (!r) {
deferred.abort(new Error('No resolver found for codec "' + cid.codec + '"'))
return deferred
}

waterfall([
(cb) => this.bs.get(el.cid, cb),
Expand All @@ -271,7 +287,8 @@ class IPLDResolver {
})
], (err, paths) => {
if (err) {
return deferred.abort(err)
deferred.abort(err)
return deferred
}

deferred.resolve(pull.values(paths.map((p) => {
Expand Down Expand Up @@ -324,6 +341,9 @@ class IPLDResolver {

_get (cid, callback) {
const r = this.resolvers[cid.codec]
if (!r) {
return callback(new Error('No resolver found for codec "' + cid.codec + '"'))
}

waterfall([
(cb) => this.bs.get(cid, cb),
Expand All @@ -346,6 +366,10 @@ class IPLDResolver {
callback = callback || noop

const r = this.resolvers[cid.codec]
if (!r) {
return callback(new Error('No resolver found for codec "' + cid.codec + '"'))
}

waterfall([
(cb) => r.util.serialize(node, cb),
(buf, cb) => this.bs.put(new Block(buf, cid), cb)
Expand Down
67 changes: 67 additions & 0 deletions test/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const dirtyChai = require('dirty-chai')
const expect = chai.expect
chai.use(dirtyChai)
const BlockService = require('ipfs-block-service')
const CID = require('cids')
const multihash = require('multihashes')
const pull = require('pull-stream')

const IPLDResolver = require('../src')

Expand All @@ -27,4 +30,68 @@ module.exports = (repo) => {
it.skip('add support to a new format', () => {})
it.skip('remove support to a new format', () => {})
})

describe('validation', () => {
it('get - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(new Buffer('abcd', 'hex'), 'sha1'))
r.get(cid, '/', {}, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
done()
})
})

it('_get - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(new Buffer('abcd', 'hex'), 'sha1'))
r.get(cid, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
done()
})
})

it('put - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
// choosing a format that is not supported
r.put(null, { format: 'base1' }, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
done()
})
})

it('_put - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(new Buffer('abcd', 'hex'), 'sha1'))
r._put(cid, null, (err, result) => {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
done()
})
})

it('treeStream - errors on unknown resolver', (done) => {
const bs = new BlockService(repo)
const r = new IPLDResolver(bs)
// choosing a format that is not supported
const cid = new CID(1, 'base1', multihash.encode(new Buffer('abcd', 'hex'), 'sha1'))
pull(
r.treeStream(cid, '/', {}),
pull.collect(function (err) {
expect(err).to.exist()
expect(err.message).to.eql('No resolver found for codec "base1"')
done()
})
)
})
})
}

0 comments on commit 62816a9

Please sign in to comment.