Skip to content

Commit

Permalink
refactored dagservice with new is-ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
nginnever committed Mar 25, 2016
1 parent 7af1081 commit 89fec70
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"dependencies": {
"ipfs-blocks": "^0.1.0",
"is-ipfs": "^0.1.0",
"is-ipfs": "github:nginnever/is-ipfs.git#0ca3ad3c10e4dfb549f0ca70824a9bb07ea6777b",
"multihashing": "^0.2.0",
"protocol-buffers": "^3.1.4",
"stable": "^0.1.5"
Expand Down
32 changes: 10 additions & 22 deletions src/dag-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const DAGNode = require('./dag-node').DAGNode
const Block = require('ipfs-blocks').Block
const isIPFS = require('is-ipfs')
const isIPFS = require('../node_modules/is-ipfs/src/index')
const base58 = require('bs58')

exports = module.exports = DAGService
Expand All @@ -26,38 +26,26 @@ function DAGService (blockService) {

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

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

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

if (isString) {
var isMhash = isIPFS.multihash(multihash)
var isPath = isIPFS.path(multihash)
if (!isMhash && !isPath) {
return callback(new Error('Invalid Key'))
}
if (isMhash) {
var mhBuffer = new Buffer(base58.decode(multihash))
this.getWith(mhBuffer, callback)
}
if (isPath) {
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
this.getWith(ipfsKey, callback)
}
if (isPath) {
var ipfsKey = new Buffer(base58.decode(multihash.replace('/ipfs/', '')))
this.getWith(ipfsKey, callback)
}
}

this.getWith = function (key, callback) {
this.bs.getBlock(key, (err, block) => {
const formatted = typeof key === 'string' ? new Buffer(base58.decode(key)) : key
this.bs.getBlock(formatted, (err, block) => {
if (err) { return callback(err) }
var node = new DAGNode()
node.unMarshal(block.data)
Expand Down

0 comments on commit 89fec70

Please sign in to comment.