Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7af1081

Browse files
committedMar 22, 2016
convering non-string and non-buffer case
1 parent c2d0ec1 commit 7af1081

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed
 

‎src/dag-service.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,20 @@ function DAGService (blockService) {
2626

2727
// get retrieves a DAGNode, using the Block Service
2828
this.get = function (multihash, callback) {
29-
if (Buffer.isBuffer(multihash)) {
29+
const isBuf = Buffer.isBuffer(multihash)
30+
const isString = typeof multihash === 'string'
31+
32+
if (!isBuf && !isString) {
33+
return callback(new Error('Invalid Key'))
34+
}
35+
36+
if (isBuf) {
3037
var mhString = base58.encode(multihash)
3138
if (!isIPFS.multihash(mhString)) { return callback(new Error('Invalid Key')) }
3239
this.getWith(multihash, callback)
3340
}
3441

35-
if (typeof multihash === 'string') {
42+
if (isString) {
3643
var isMhash = isIPFS.multihash(multihash)
3744
var isPath = isIPFS.path(multihash)
3845
if (!isMhash && !isPath) {

‎tests/merkle-dag-tests.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,7 @@ module.exports = function (repo) {
176176
})
177177

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

207+
it('supply something weird', (done) => {
208+
var mh = 3
209+
dagService.get(mh, (err, fetchedNode) => {
210+
var error = 'Error: Invalid Key'
211+
expect(err.toString()).to.equal(error)
212+
done()
213+
})
214+
})
215+
208216
it('get a dag recursively', (done) => {
209217
// 1 -> 2 -> 3
210218
const node1 = new DAGNode(new Buffer('1'))

0 commit comments

Comments
 (0)
Please sign in to comment.