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

Commit 0458b70

Browse files
committed
refactor: use the new IPLD API
This is part of the Awesome Endeavour: Async Iterators: ipfs/js-ipfs#1670
1 parent bc222c9 commit 0458b70

File tree

6 files changed

+65
-48
lines changed

6 files changed

+65
-48
lines changed

package.json

+4-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,12 @@
3939
"devDependencies": {
4040
"aegir": "^18.0.2",
4141
"chai": "^4.2.0",
42+
"chai-as-promised": "^7.1.1",
4243
"cids": "~0.5.5",
4344
"detect-node": "^2.0.4",
4445
"dirty-chai": "^2.0.1",
45-
"ipfs-unixfs-exporter": "~0.35.4",
46-
"ipld": "~0.20.2",
46+
"ipfs-unixfs-exporter": "git+https://github.com./ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
47+
"ipld": "git+https://github.com./ipld/js-ipld.git#new-api-impl",
4748
"ipld-in-memory": "^2.0.0",
4849
"multihashes": "~0.4.14",
4950
"pull-buffer-stream": "^1.0.1",
@@ -60,6 +61,7 @@
6061
"ipfs-unixfs": "~0.1.16",
6162
"ipld-dag-pb": "~0.15.2",
6263
"left-pad": "^1.3.0",
64+
"multicodec": "~0.5.0",
6365
"multihashing-async": "~0.5.1",
6466
"pull-batch": "^1.0.0",
6567
"pull-pair": "^1.1.0",

src/utils/persist.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict'
22

33
const {
4-
util: {
5-
cid
6-
}
4+
util
75
} = require('ipld-dag-pb')
6+
const multicodec = require('multicodec')
87

98
const defaultOptions = {
109
cidVersion: 0,
@@ -27,7 +26,7 @@ const persist = (node, ipld, options, callback) => {
2726
}
2827

2928
if (options.onlyHash) {
30-
return cid(node, {
29+
return util.cid(node, {
3130
version: cidVersion,
3231
hashAlg: hashAlg
3332
}, (err, cid) => {
@@ -38,16 +37,27 @@ const persist = (node, ipld, options, callback) => {
3837
})
3938
}
4039

41-
ipld.put(node, {
42-
version: cidVersion,
43-
hashAlg: hashAlg,
44-
format: codec
45-
}, (error, cid) => {
46-
callback(error, {
40+
// The IPLD expects the format and hashAlg as constants
41+
if (typeof codec === 'string') {
42+
const constantName = codec.toUpperCase().replace(/-/g, '_')
43+
codec = multicodec[constantName]
44+
}
45+
if (typeof hashAlg === 'string') {
46+
const constantName = hashAlg.toUpperCase().replace(/-/g, '_')
47+
hashAlg = multicodec[constantName]
48+
}
49+
50+
const result = ipld.put([node], codec, {
51+
cidVersion,
52+
hashAlg
53+
})
54+
result.first().then(
55+
(cid) => callback(null, {
4756
cid,
4857
node
49-
})
50-
})
58+
}),
59+
(error) => callback(error)
60+
)
5161
}
5262

5363
module.exports = persist

test/builder-only-hash.spec.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
'use strict'
33

44
const chai = require('chai')
5+
chai.use(require('chai-as-promised'))
56
chai.use(require('dirty-chai'))
67
const expect = chai.expect
78
const pull = require('pull-stream/pull')
@@ -27,16 +28,14 @@ describe('builder: onlyHash', () => {
2728
})
2829

2930
it('will only chunk and hash if passed an "onlyHash" option', (done) => {
30-
const onCollected = (err, nodes) => {
31+
const onCollected = async (err, nodes) => {
3132
if (err) return done(err)
3233

3334
const node = nodes[0]
3435
expect(node).to.exist()
3536

36-
ipld.get(new CID(node.multihash), (err, res) => {
37-
expect(err).to.exist()
38-
done()
39-
})
37+
const result = ipld.get([new CID(node.multihash)])
38+
expect(result.first()).to.be.rejectedWith('Not Found').notify(done)
4039
}
4140

4241
const content = String(Math.random() + Date.now())

test/builder.spec.js

+18-12
Original file line numberDiff line numberDiff line change
@@ -52,12 +52,15 @@ describe('builder', () => {
5252
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
5353

5454
// Fetch using hashAlg encoded multihash
55-
ipld.get(cid, (err, res) => {
56-
if (err) return cb(err)
57-
const content = UnixFS.unmarshal(res.value.data).data
58-
expect(content.equals(inputFile.content)).to.be.true()
59-
cb()
60-
})
55+
const result = ipld.get([cid])
56+
result.first().then(
57+
(node) => {
58+
const content = UnixFS.unmarshal(node.data).data
59+
expect(content.equals(inputFile.content)).to.be.true()
60+
cb()
61+
},
62+
(error) => cb(error)
63+
)
6164
}
6265

6366
pull(
@@ -124,12 +127,15 @@ describe('builder', () => {
124127
expect(mh.decode(cid.multihash).name).to.equal(hashAlg)
125128

126129
// Fetch using hashAlg encoded multihash
127-
ipld.get(cid, (err, res) => {
128-
if (err) return cb(err)
129-
const meta = UnixFS.unmarshal(res.value.data)
130-
expect(meta.type).to.equal('directory')
131-
cb()
132-
})
130+
const result = ipld.get([cid])
131+
result.first().then(
132+
(node) => {
133+
const meta = UnixFS.unmarshal(node.data)
134+
expect(meta.type).to.equal('directory')
135+
cb()
136+
},
137+
(error) => cb(error)
138+
)
133139
}
134140

135141
pull(

test/helpers/collect-leaf-cids.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ module.exports = (ipld, multihash, callback) => {
1515
return pull(
1616
values([cid]),
1717
asyncMap((cid, callback) => {
18-
ipld.get(cid, (error, result) => {
19-
callback(error, !error && result.value)
20-
})
18+
ipld.get([cid]).first().then(
19+
(node) => callback(null, node),
20+
(error) => callback(error)
21+
)
2122
}),
2223
asyncMap((node, callback) => {
2324
if (!node.links) {

test/importer.spec.js

+13-14
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,8 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => {
125125
importer(ipld, options),
126126
collect(cb)
127127
),
128-
(files, cb) => ipld.get(new CID(files[0].multihash), cb),
129-
(result, cb) => {
130-
const node = result.value
128+
async (files) => ipld.get([new CID(files[0].multihash)]).first(),
129+
(node, cb) => {
131130
const meta = UnixFs.unmarshal(node.data)
132131

133132
expect(meta.type).to.equal('file')
@@ -137,9 +136,8 @@ const checkLeafNodeTypes = (ipld, options, expected, done) => {
137136
node.links.map(link => {
138137
return (done) => {
139138
waterfall([
140-
(next) => ipld.get(link.cid, next),
141-
(result, next) => {
142-
const node = result.value
139+
async () => ipld.get([link.cid]).first(),
140+
(node, next) => {
143141
const meta = UnixFs.unmarshal(node.data)
144142

145143
expect(meta.type).to.equal(expected)
@@ -163,9 +161,8 @@ const checkNodeLinks = (ipld, options, expected, done) => {
163161
importer(ipld, options),
164162
collect(cb)
165163
),
166-
(files, cb) => ipld.get(new CID(files[0].multihash), cb),
167-
(result, cb) => {
168-
const node = result.value
164+
async (files) => ipld.get([new CID(files[0].multihash)]).first(),
165+
(node, cb) => {
169166
const meta = UnixFs.unmarshal(node.data)
170167

171168
expect(meta.type).to.equal('file')
@@ -587,10 +584,8 @@ strategies.forEach((strategy) => {
587584
const file = files[0]
588585
expect(file).to.exist()
589586

590-
ipld.get(new CID(file.multihash), (err) => {
591-
expect(err).to.exist()
592-
done()
593-
})
587+
const result = ipld.get([new CID(file.multihash)])
588+
expect(result.first()).to.be.rejectedWith('Not Found').notify(done)
594589
}
595590

596591
pull(
@@ -658,7 +653,11 @@ strategies.forEach((strategy) => {
658653

659654
// Just check the intermediate directory can be retrieved
660655
if (!inputFile) {
661-
return ipld.get(cid, cb)
656+
const result = ipld.get([new CID(file.multihash)])
657+
return result.first().then(
658+
(_cid) => cb(),
659+
(_error) => cb()
660+
)
662661
}
663662

664663
// Check the imported content is correct

0 commit comments

Comments
 (0)