Skip to content

Commit bfc0c3d

Browse files
committed
Allow object get/data to accept CID
1 parent b4c0785 commit bfc0c3d

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/object/data.js

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const promisify = require('promisify-es6')
44
const streamToValue = require('../utils/stream-to-value')
5-
const cleanMultihash = require('../utils/clean-multihash')
5+
const CID = require('cids')
66
const LRU = require('lru-cache')
77
const lruOptions = {
88
max: 128
@@ -11,7 +11,7 @@ const lruOptions = {
1111
const cache = LRU(lruOptions)
1212

1313
module.exports = (send) => {
14-
return promisify((multihash, options, callback) => {
14+
return promisify((hash, options, callback) => {
1515
if (typeof options === 'function') {
1616
callback = options
1717
options = {}
@@ -20,21 +20,24 @@ module.exports = (send) => {
2020
options = {}
2121
}
2222

23+
let cid, b58Hash
24+
2325
try {
24-
multihash = cleanMultihash(multihash, options)
26+
cid = new CID(hash)
27+
b58Hash = cid.toBaseEncodedString()
2528
} catch (err) {
2629
return callback(err)
2730
}
2831

29-
const node = cache.get(multihash)
32+
const node = cache.get(b58Hash)
3033

3134
if (node) {
3235
return callback(null, node.data)
3336
}
3437

3538
send({
3639
path: 'object/data',
37-
args: multihash
40+
args: b58Hash
3841
}, (err, result) => {
3942
if (err) {
4043
return callback(err)

src/object/get.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const dagPB = require('ipld-dag-pb')
55
const DAGNode = dagPB.DAGNode
66
const DAGLink = dagPB.DAGLink
77
const bs58 = require('bs58')
8-
const cleanMultihash = require('../utils/clean-multihash')
8+
const CID = require('cids')
99
const LRU = require('lru-cache')
1010
const lruOptions = {
1111
max: 128
@@ -14,7 +14,7 @@ const lruOptions = {
1414
const cache = LRU(lruOptions)
1515

1616
module.exports = (send) => {
17-
return promisify((multihash, options, callback) => {
17+
return promisify((hash, options, callback) => {
1818
if (typeof options === 'function') {
1919
callback = options
2020
options = {}
@@ -24,21 +24,24 @@ module.exports = (send) => {
2424
options = {}
2525
}
2626

27+
let cid, b58Hash
28+
2729
try {
28-
multihash = cleanMultihash(multihash, options)
30+
cid = new CID(hash)
31+
b58Hash = cid.toBaseEncodedString()
2932
} catch (err) {
3033
return callback(err)
3134
}
3235

33-
const node = cache.get(multihash)
36+
const node = cache.get(b58Hash)
3437

3538
if (node) {
3639
return callback(null, node)
3740
}
3841

3942
send({
4043
path: 'object/get',
41-
args: multihash
44+
args: b58Hash
4245
}, (err, result) => {
4346
if (err) {
4447
return callback(err)
@@ -52,7 +55,7 @@ module.exports = (send) => {
5255
if (err) {
5356
return callback(err)
5457
}
55-
cache.set(multihash, node)
58+
cache.set(b58Hash, node)
5659
callback(null, node)
5760
})
5861
})

src/utils/get-dagnode.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,25 @@
22

33
const DAGNode = require('ipld-dag-pb').DAGNode
44
const parallel = require('async/parallel')
5+
const CID = require('cids')
56
const streamToValue = require('./stream-to-value')
67

78
module.exports = function (send, hash, callback) {
9+
let cid
10+
11+
try {
12+
cid = new CID(hash)
13+
} catch (err) {
14+
return callback(err)
15+
}
16+
817
// Retrieve the object and its data in parallel, then produce a DAGNode
918
// instance using this information.
1019
parallel([
1120
function get (done) {
1221
send({
1322
path: 'object/get',
14-
args: hash
23+
args: cid.toBaseEncodedString()
1524
}, done)
1625
},
1726

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

0 commit comments

Comments
 (0)