From 7af1081f7139caf15cd23693d7509318e4eea982 Mon Sep 17 00:00:00 2001 From: nginnever Date: Tue, 22 Mar 2016 15:00:19 -0700 Subject: [PATCH] convering non-string and non-buffer case --- src/dag-service.js | 11 +++++++++-- tests/merkle-dag-tests.js | 12 ++++++++++-- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/src/dag-service.js b/src/dag-service.js index f2b2e3d..ae397b9 100644 --- a/src/dag-service.js +++ b/src/dag-service.js @@ -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) { diff --git a/tests/merkle-dag-tests.js b/tests/merkle-dag-tests.js index becb458..454c0fa 100644 --- a/tests/merkle-dag-tests.js +++ b/tests/merkle-dag-tests.js @@ -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'))) @@ -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'))