Skip to content

Commit

Permalink
Allow object get/data to accept CID
Browse files Browse the repository at this point in the history
  • Loading branch information
alanshaw committed Sep 7, 2017
1 parent b4c0785 commit bfc0c3d
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
13 changes: 8 additions & 5 deletions src/object/data.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const promisify = require('promisify-es6')
const streamToValue = require('../utils/stream-to-value')
const cleanMultihash = require('../utils/clean-multihash')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
max: 128
Expand All @@ -11,7 +11,7 @@ const lruOptions = {
const cache = LRU(lruOptions)

module.exports = (send) => {
return promisify((multihash, options, callback) => {
return promisify((hash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
Expand All @@ -20,21 +20,24 @@ module.exports = (send) => {
options = {}
}

let cid, b58Hash

try {
multihash = cleanMultihash(multihash, options)
cid = new CID(hash)
b58Hash = cid.toBaseEncodedString()
} catch (err) {
return callback(err)
}

const node = cache.get(multihash)
const node = cache.get(b58Hash)

if (node) {
return callback(null, node.data)
}

send({
path: 'object/data',
args: multihash
args: b58Hash
}, (err, result) => {
if (err) {
return callback(err)
Expand Down
15 changes: 9 additions & 6 deletions src/object/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const dagPB = require('ipld-dag-pb')
const DAGNode = dagPB.DAGNode
const DAGLink = dagPB.DAGLink
const bs58 = require('bs58')
const cleanMultihash = require('../utils/clean-multihash')
const CID = require('cids')
const LRU = require('lru-cache')
const lruOptions = {
max: 128
Expand All @@ -14,7 +14,7 @@ const lruOptions = {
const cache = LRU(lruOptions)

module.exports = (send) => {
return promisify((multihash, options, callback) => {
return promisify((hash, options, callback) => {
if (typeof options === 'function') {
callback = options
options = {}
Expand All @@ -24,21 +24,24 @@ module.exports = (send) => {
options = {}
}

let cid, b58Hash

try {
multihash = cleanMultihash(multihash, options)
cid = new CID(hash)
b58Hash = cid.toBaseEncodedString()
} catch (err) {
return callback(err)
}

const node = cache.get(multihash)
const node = cache.get(b58Hash)

if (node) {
return callback(null, node)
}

send({
path: 'object/get',
args: multihash
args: b58Hash
}, (err, result) => {
if (err) {
return callback(err)
Expand All @@ -52,7 +55,7 @@ module.exports = (send) => {
if (err) {
return callback(err)
}
cache.set(multihash, node)
cache.set(b58Hash, node)
callback(null, node)
})
})
Expand Down
13 changes: 11 additions & 2 deletions src/utils/get-dagnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,25 @@

const DAGNode = require('ipld-dag-pb').DAGNode
const parallel = require('async/parallel')
const CID = require('cids')
const streamToValue = require('./stream-to-value')

module.exports = function (send, hash, callback) {
let cid

try {
cid = new CID(hash)
} catch (err) {
return callback(err)
}

// Retrieve the object and its data in parallel, then produce a DAGNode
// instance using this information.
parallel([
function get (done) {
send({
path: 'object/get',
args: hash
args: cid.toBaseEncodedString()
}, done)
},

Expand All @@ -21,7 +30,7 @@ module.exports = function (send, hash, callback) {
// See https://github.com/ipfs/go-ipfs/issues/1582 for more details.
send({
path: 'object/data',
args: hash
args: cid.toBaseEncodedString()
}, done)
}],

Expand Down

0 comments on commit bfc0c3d

Please sign in to comment.