Skip to content
This repository was archived by the owner on Mar 10, 2020. It is now read-only.

Commit 0ba96bf

Browse files
committed
fix: dag.get return error on missing multicodec
Before this change it just failed silently for 'raw' DAGs such as `bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy` License: MIT Signed-off-by: Marcin Rataj <lidel@lidel.org>
1 parent 644276c commit 0ba96bf

File tree

2 files changed

+96
-5
lines changed

2 files changed

+96
-5
lines changed

src/dag/get.js

+12-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ const CID = require('cids')
77
const waterfall = require('async/waterfall')
88
const block = require('../block')
99

10+
const resolvers = {
11+
'dag-cbor': dagCBOR.resolver,
12+
'dag-pb': dagPB.resolver
13+
}
14+
1015
module.exports = (send) => {
1116
return promisify((cid, path, options, callback) => {
1217
if (typeof path === 'function') {
@@ -40,12 +45,14 @@ module.exports = (send) => {
4045
})
4146
},
4247
(ipfsBlock, path, cb) => {
43-
if (ipfsBlock.cid.codec === 'dag-cbor') {
44-
dagCBOR.resolver.resolve(ipfsBlock.data, path, cb)
45-
}
46-
if (ipfsBlock.cid.codec === 'dag-pb') {
47-
dagPB.resolver.resolve(ipfsBlock.data, path, cb)
48+
const dagResolver = resolvers[ipfsBlock.cid.codec]
49+
if (dagResolver) {
50+
dagResolver.resolve(ipfsBlock.data, path, cb)
51+
return
4852
}
53+
const error = new Error('ipfs-api is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
54+
error.missingMulticodec = ipfsBlock.cid.codec
55+
cb(error)
4956
}
5057
], callback)
5158
})

test/dag.spec.js

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/* eslint-env mocha */
2+
/* eslint max-nested-callbacks: ["error", 8] */
3+
4+
'use strict'
5+
6+
const chai = require('chai')
7+
const dirtyChai = require('dirty-chai')
8+
const expect = chai.expect
9+
chai.use(dirtyChai)
10+
const series = require('async/series')
11+
const CID = require('cids')
12+
const dagPB = require('ipld-dag-pb')
13+
const DAGNode = dagPB.DAGNode
14+
15+
const IPFSApi = require('../src')
16+
const f = require('./utils/factory')
17+
18+
let ipfsd
19+
let ipfs
20+
21+
describe('.dag', function () {
22+
this.timeout(20 * 1000)
23+
before(function (done) {
24+
series([
25+
(cb) => f.spawn({ initOptions: { bits: 1024 } }, (err, _ipfsd) => {
26+
expect(err).to.not.exist()
27+
ipfsd = _ipfsd
28+
ipfs = IPFSApi(_ipfsd.apiAddr)
29+
cb()
30+
})
31+
], done)
32+
})
33+
34+
after((done) => {
35+
if (!ipfsd) return done()
36+
ipfsd.stop(done)
37+
})
38+
39+
it('.dag.put+get dag-pb', (done) => {
40+
const data = Buffer.from('some data')
41+
DAGNode.create(data, (err, node) => {
42+
expect(err).to.not.exist()
43+
ipfs.dag.put(node, {format: 'dag-pb', hashAlg: 'sha2-256'}, (err, cid) => {
44+
expect(err).to.not.exist()
45+
cid = new CID(cid).toV0()
46+
expect(cid.codec).to.equal('dag-pb')
47+
cid = cid.toBaseEncodedString('base58btc')
48+
// expect(cid).to.equal('bafybeig3t3eugdchignsgkou3ly2mmy4ic4gtfor7inftnqn3yq4ws3a5u')
49+
expect(cid).to.equal('Qmd7xRhW5f29QuBFtqu3oSD27iVy35NRB91XFjmKFhtgMr')
50+
ipfs.dag.get(cid, (err, result) => {
51+
expect(err).to.not.exist()
52+
expect(result.value.data).to.deep.equal(data)
53+
done()
54+
})
55+
})
56+
})
57+
})
58+
59+
it('.dag.put+get dag-cbor', (done) => {
60+
const cbor = {foo: 'dag-cbor-bar'}
61+
ipfs.dag.put(cbor, {format: 'dag-cbor', hashAlg: 'sha2-256'}, (err, cid) => {
62+
expect(err).to.not.exist()
63+
cid = new CID(cid)
64+
expect(cid.codec).to.equal('dag-cbor')
65+
cid = cid.toBaseEncodedString('base32')
66+
expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
67+
ipfs.dag.get(cid, (err, result) => {
68+
expect(err).to.not.exist()
69+
expect(result.value).to.deep.equal(cbor)
70+
done()
71+
})
72+
})
73+
})
74+
75+
it('.dag.get missing raw multicodec returns error', (done) => {
76+
// CIDv1 with multicodec = raw
77+
const cid = 'bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy'
78+
ipfs.dag.get(cid, (err, result) => {
79+
expect(err.message).to.equal('ipfs-api is missing DAG resolver for "raw" multicodec')
80+
expect(result).to.not.exist()
81+
done()
82+
})
83+
})
84+
})

0 commit comments

Comments
 (0)