Skip to content

Commit

Permalink
convering non-string and non-buffer case
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Mar 22, 2016
1 parent c2d0ec1 commit 7af1081
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 4 deletions.
11 changes: 9 additions & 2 deletions src/dag-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,20 @@ function DAGService (blockService) {

// get retrieves a DAGNode, using the Block Service
this.get = function (multihash, callback) {
if (Buffer.isBuffer(multihash)) {
const isBuf = Buffer.isBuffer(multihash)
const isString = typeof multihash === 'string'

if (!isBuf && !isString) {
return callback(new Error('Invalid Key'))
}

if (isBuf) {
var mhString = base58.encode(multihash)
if (!isIPFS.multihash(mhString)) { return callback(new Error('Invalid Key')) }
this.getWith(multihash, callback)
}

if (typeof multihash === 'string') {
if (isString) {
var isMhash = isIPFS.multihash(multihash)
var isPath = isIPFS.path(multihash)
if (!isMhash && !isPath) {
Expand Down
12 changes: 10 additions & 2 deletions tests/merkle-dag-tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ module.exports = function (repo) {
})

it('get a mdag node from a /ipfs/ path', (done) => {
var encodedMh = 'QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
var ipfsPath = '/ipfs/' + encodedMh
var ipfsPath = '/ipfs/QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG'
dagService.get(ipfsPath, (err, fetchedNode) => {
expect(err).to.not.exist
expect(fetchedNode.data).to.deep.equal(new Buffer(bs58.decode('cL')))
Expand Down Expand Up @@ -205,6 +204,15 @@ module.exports = function (repo) {
})
})

it('supply something weird', (done) => {
var mh = 3
dagService.get(mh, (err, fetchedNode) => {
var error = 'Error: Invalid Key'
expect(err.toString()).to.equal(error)
done()
})
})

it('get a dag recursively', (done) => {
// 1 -> 2 -> 3
const node1 = new DAGNode(new Buffer('1'))
Expand Down

0 comments on commit 7af1081

Please sign in to comment.