Skip to content

Commit

Permalink
Safe JSON.parse call
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Nov 9, 2015
1 parent 8106ee5 commit 9d8e9ea
Showing 1 changed file with 24 additions and 20 deletions.
44 changes: 24 additions & 20 deletions lib/lookup.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,32 @@ var memoized_lookup
function _lookup (ipfs, hash, lookfor, cb) {
ipfs.object.get(hash, function (err, res) {
if (err) {
cb(err, null)
} else {
return cb(err, null)
}

try {
var obj = JSON.parse(res.Data)
} catch (err) {
return cb(err, null)
}

var child = 0
if (obj.type === 'Node') {
while (obj.mins[child] &&
obj.mins[child] <= lookfor) {
child++
}
return memoized_lookup(ipfs, res.Links[child - 1].Hash, lookfor, cb)
} else if (obj.type === 'Leaf') {
while (obj.data[child] &&
obj.data[child].min <= lookfor) {
child++
}

var child = 0
if (obj.type === 'Node') {
while (obj.mins[child] &&
obj.mins[child] <= lookfor) {
child++
}
return memoized_lookup(ipfs, res.Links[child - 1].Hash, lookfor, cb)
} else if (obj.type === 'Leaf') {
while (obj.data[child] &&
obj.data[child].min <= lookfor) {
child++
}

if (obj.data[child - 1].data) {
cb(null, formatData(obj.data[child - 1].data))
} else {
cb('Unmapped range', null)
}
if (obj.data[child - 1].data) {
cb(null, formatData(obj.data[child - 1].data))
} else {
cb('Unmapped range', null)
}
}
})
Expand Down

0 comments on commit 9d8e9ea

Please sign in to comment.