Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
refactor: fix review
Browse files Browse the repository at this point in the history
  • Loading branch information
vasco-santos committed Nov 19, 2018
1 parent c2d26e1 commit ade2d1c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/core/ipns/republisher.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class IpnsRepublisher {
}

// keychain needs pass to get the cryptographic keys
if (this._keychain && Boolean(pass)) {
if (pass) {
this._keychain.listKeys((err, list) => {
if (err) {
log.error(err)
Expand Down
27 changes: 10 additions & 17 deletions src/core/ipns/resolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,34 +105,27 @@ class IpnsResolver {

this._routing.get(routingKey.toBuffer(), (err, res) => {
if (err) {
if (err.code !== 'ERR_NOT_FOUND') {
const errMsg = `unexpected error getting the ipns record ${peerId.id}`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_UNEXPECTED_ERROR_GETTING_RECORD'))
}
const errMsg = `record requested was not found for ${name} (${routingKey}) in the network`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_NO_RECORD_FOUND'))
}

if (!res) {
const errMsg = `record requested was empty for ${name} (${routingKey}) in the network`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_EMPTY_RECORD_FOUND'))
}

if (!Buffer.isBuffer(res)) {
const errMsg = `found ipns record that we couldn't convert to a value`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_RECEIVED'))
}

let ipnsEntry

try {
const record = Record.deserialize(res)
ipnsEntry = ipns.unmarshal(record.value)
} catch (err) {
log.error(err)
return callback(err)
const errMsg = `found ipns record that we couldn't convert to a value`

log.error(errMsg)
return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_RECEIVED'))
}

ipns.extractPublicKey(peerId, ipnsEntry, (err, pubKey) => {
Expand Down
2 changes: 1 addition & 1 deletion src/core/ipns/routing/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
const multibase = require('multibase')

module.exports.encodeBase32 = (buf) => {
return multibase.encode('base32', buf)
return multibase.encode('base32', buf).slice(1) // slice off multibase codec
}
8 changes: 4 additions & 4 deletions test/core/name.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,23 +322,23 @@ describe('name', function () {

node.name.resolve(nodeId, { nocache: true }, (err, res) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_NO_RECORD_FOUND')
expect(err.code).to.equal('ERR_UNEXPECTED_ERROR_GETTING_RECORD')
stub.restore()
done()
})
})
})

it('should publish and then fail to resolve if does not get any data', function (done) {
const stub = sinon.stub(node._ipns.resolver._routing, 'get').callsArgWith(1, undefined, undefined)
it('should publish and then fail to resolve if does not find the record', function (done) {
const stub = sinon.stub(node._ipns.resolver._routing, 'get').callsArgWith(1, { code: 'ERR_NOT_FOUND' })

node.name.publish(ipfsRef, { resolve: false }, (err, res) => {
expect(err).to.not.exist()
expect(res).to.exist()

node.name.resolve(nodeId, { nocache: true }, (err, res) => {
expect(err).to.exist()
expect(err.code).to.equal('ERR_EMPTY_RECORD_FOUND')
expect(err.code).to.equal('ERR_NO_RECORD_FOUND')
stub.restore()
done()
})
Expand Down

0 comments on commit ade2d1c

Please sign in to comment.