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

Commit bc6ff32

Browse files
achingbrainAlan Shaw
authored and
Alan Shaw
committed
feat: add support for js-ipfs dag api and also some tests
1 parent 63103bd commit bc6ff32

File tree

3 files changed

+70
-7
lines changed

3 files changed

+70
-7
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
"ipfs-utils": "~0.0.3",
5151
"ipld-dag-cbor": "~0.15.0",
5252
"ipld-dag-pb": "~0.17.3",
53+
"ipld-raw": "^4.0.0",
5354
"is-ipfs": "~0.6.1",
5455
"is-pull-stream": "0.0.0",
5556
"is-stream": "^2.0.0",

src/dag/get.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
const dagPB = require('ipld-dag-pb')
44
const dagCBOR = require('ipld-dag-cbor')
5+
const raw = require('ipld-raw')
56
const promisify = require('promisify-es6')
67
const CID = require('cids')
78
const waterfall = require('async/waterfall')
89
const block = require('../block')
910

1011
const resolvers = {
1112
'dag-cbor': dagCBOR.resolver,
12-
'dag-pb': dagPB.resolver
13+
'dag-pb': dagPB.resolver,
14+
raw: raw.resolver
1315
}
1416

1517
module.exports = (send) => {
@@ -48,7 +50,7 @@ module.exports = (send) => {
4850
const dagResolver = resolvers[ipfsBlock.cid.codec]
4951

5052
if (!dagResolver) {
51-
const error = new Error('ipfs-http-client is missing DAG resolver for "' + ipfsBlock.cid.codec + '" multicodec')
53+
const error = new Error(`Missing IPLD format "${ipfsBlock.cid.codec}"`)
5254
error.missingMulticodec = ipfsBlock.cid.codec
5355
return cb(error)
5456
}
@@ -59,6 +61,7 @@ module.exports = (send) => {
5961
} catch (err) {
6062
return cb(err)
6163
}
64+
6265
cb(null, res)
6366
}
6467
], callback)

test/dag.spec.js

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,75 @@ describe('.dag', function () {
6868
})
6969
})
7070

71-
it('should callback with error when missing DAG resolver for raw multicodec', (done) => {
72-
ipfs.dag.put(Buffer.from([0, 1, 2, 3]), {
73-
// CIDv1 with multicodec = raw
74-
cid: new CID('bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy')
71+
it('should be able to get part of a dag-cbor node', (done) => {
72+
const cbor = {
73+
foo: 'dag-cbor-bar'
74+
}
75+
ipfs.dag.put(cbor, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid) => {
76+
expect(err).to.not.exist()
77+
expect(cid.codec).to.equal('dag-cbor')
78+
cid = cid.toBaseEncodedString('base32')
79+
expect(cid).to.equal('bafyreic6f672hnponukaacmk2mmt7vs324zkagvu4hcww6yba6kby25zce')
80+
ipfs.dag.get(cid, 'foo', (err, result) => {
81+
expect(err).to.not.exist()
82+
expect(result.value).to.equal('dag-cbor-bar')
83+
done()
84+
})
85+
})
86+
})
87+
88+
it('should be able to traverse from one dag-cbor node to another', (done) => {
89+
const cbor1 = {
90+
foo: 'dag-cbor-bar'
91+
}
92+
93+
ipfs.dag.put(cbor1, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid1) => {
94+
expect(err).to.not.exist()
95+
96+
const cbor2 = {
97+
other: {
98+
'/': cid1.toBaseEncodedString('base32')
99+
}
100+
}
101+
102+
ipfs.dag.put(cbor2, { format: 'dag-cbor', hashAlg: 'sha2-256' }, (err, cid2) => {
103+
expect(err).to.not.exist()
104+
105+
ipfs.dag.get(cid2, 'other/foo', (err, result) => {
106+
expect(err).to.not.exist()
107+
expect(result.value).to.equal('dag-cbor-bar')
108+
done()
109+
})
110+
})
111+
})
112+
})
113+
114+
it('should be able to put and get a DAG node with format raw', (done) => {
115+
const buf = Buffer.from([0, 1, 2, 3])
116+
117+
ipfs.dag.put(buf, {
118+
format: 'raw',
119+
hashAlg: 'sha2-256'
75120
}, (err, cid) => {
76121
expect(err).to.not.exist()
77122

78123
ipfs.dag.get(cid, (err, result) => {
124+
expect(err).to.not.exist()
125+
expect(result.value).to.deep.equal(buf)
126+
done()
127+
})
128+
})
129+
})
130+
131+
it('should callback with error when missing DAG resolver for multicodec from requested CID', (done) => {
132+
ipfs.block.put(Buffer.from([0, 1, 2, 3]), {
133+
cid: new CID('z8mWaJ1dZ9fH5EetPuRsj8jj26pXsgpsr')
134+
}, (err, block) => {
135+
expect(err).to.not.exist()
136+
137+
ipfs.dag.get(block.cid, (err, result) => {
79138
expect(result).to.not.exist()
80-
expect(err.message).to.equal('ipfs-http-client is missing DAG resolver for "raw" multicodec')
139+
expect(err.message).to.equal('Missing IPLD format "git-raw"')
81140
done()
82141
})
83142
})

0 commit comments

Comments
 (0)