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

Commit ff7c7e5

Browse files
lidelalanshaw
authored andcommitted
fix: dag.get return error on missing multicodec (#831)
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 ff7c7e5

File tree

2 files changed

+94
-5
lines changed

2 files changed

+94
-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+
const error = new Error('ipfs-api is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
51+
error.missingMulticodec = ipfsBlock.cid.codec
52+
cb(error)
53+
return
4854
}
55+
dagResolver.resolve(ipfsBlock.data, path, cb)
4956
}
5057
], callback)
5158
})

test/dag.spec.js

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

0 commit comments

Comments
 (0)