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

Commit 7e6604e

Browse files
committed
refactor: use new IPLD API
This is part of the Awesome Endeavour: Async Iterators: ipfs/js-ipfs#1670
1 parent 34cfd0d commit 7e6604e

15 files changed

+189
-172
lines changed

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"detect-node": "^2.0.4",
4343
"detect-webworker": "^1.0.0",
4444
"dirty-chai": "^2.0.1",
45-
"ipld": "~0.21.1",
45+
"ipld": "~0.22.0",
4646
"ipld-in-memory": "^2.0.0",
4747
"multihashes": "~0.4.14",
4848
"pull-buffer-stream": "^1.0.1",
@@ -58,14 +58,15 @@
5858
"interface-datastore": "~0.6.0",
5959
"ipfs-multipart": "~0.1.0",
6060
"ipfs-unixfs": "~0.1.16",
61-
"ipfs-unixfs-exporter": "~0.36.1",
62-
"ipfs-unixfs-importer": "~0.38.5",
61+
"ipfs-unixfs-exporter": "git+https://github.com/ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
62+
"ipfs-unixfs-importer": "git+https://github.com/ipfs/js-ipfs-unixfs-importer.git#new-ipld-api",
6363
"ipld-dag-pb": "~0.15.2",
6464
"is-pull-stream": "~0.0.0",
6565
"is-stream": "^1.1.0",
6666
"joi": "^14.3.0",
6767
"joi-browser": "^13.4.0",
6868
"mortice": "^1.2.1",
69+
"multicodec": "~0.5.0",
6970
"once": "^1.4.0",
7071
"promisify-es6": "^1.0.3",
7172
"pull-cat": "^1.1.11",

src/core/cp.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,12 @@ const copyToFile = (context, source, destination, destinationTrail, options, cal
8080
const child = sourceTrail[sourceTrail.length - 1]
8181

8282
waterfall([
83-
(next) => context.ipld.get(parent.cid, next),
84-
(result, next) => addLink(context, {
85-
parent: result.value,
83+
(next) => context.ipld.get(parent.cid).then(
84+
(node) => next(null, node),
85+
(error) => next(error)
86+
),
87+
(node, next) => addLink(context, {
88+
parent: node,
8689
parentCid: parent.cid,
8790
size: child.size,
8891
cid: child.cid,
@@ -165,8 +168,10 @@ const copyToDirectory = (context, sources, destination, destinationTrail, option
165168
const parent = destinationTrail[destinationTrail.length - 1]
166169

167170
waterfall([
168-
(next) => context.ipld.get(parent.cid, next),
169-
(result, next) => next(null, { cid: parent.cid, node: result.value })
171+
(next) => context.ipld.get(parent.cid).then(
172+
(node) => next(null, { cid: parent.cid, node }),
173+
(error) => next(error)
174+
)
170175
].concat(
171176
sourceTrails.map((sourceTrail, index) => {
172177
return (parent, done) => {

src/core/utils/add-link.js

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ const {
1515
generatePath,
1616
updateHamtDirectory
1717
} = require('./hamt-utils')
18+
const toMulticodecCode = require('./to-multicodec-code')
1819

1920
const defaultOptions = {
2021
parent: undefined,
@@ -43,8 +44,10 @@ const addLink = (context, options, callback) => {
4344
log('Loading parent node', options.parentCid.toBaseEncodedString())
4445

4546
return waterfall([
46-
(cb) => context.ipld.get(options.parentCid, cb),
47-
(result, cb) => cb(null, result.value),
47+
(cb) => context.ipld.get(options.parentCid).then(
48+
(node) => cb(null, node),
49+
(error) => cb(error)
50+
),
4851
(node, cb) => addLink(context, {
4952
...options,
5053
parent: node
@@ -111,15 +114,18 @@ const addToDirectory = (context, options, callback) => {
111114
(parent, done) => DAGNode.addLink(parent, new DAGLink(options.name, options.size, options.cid), done),
112115
(parent, done) => {
113116
// Persist the new parent DAGNode
114-
context.ipld.put(parent, {
115-
version: options.cidVersion,
116-
format: options.codec,
117-
hashAlg: options.hashAlg,
118-
hashOnly: !options.flush
119-
}, (error, cid) => done(error, {
120-
node: parent,
121-
cid
122-
}))
117+
context.ipld.put(
118+
parent,
119+
toMulticodecCode(options.codec),
120+
{
121+
cidVersion: options.cidVersion,
122+
hashAlg: toMulticodecCode(options.hashAlg),
123+
hashOnly: !options.flush
124+
}
125+
).then(
126+
(cid) => done(null, { node: parent, cid }),
127+
(error) => done(error)
128+
)
123129
}
124130
], callback)
125131
}
@@ -180,22 +186,22 @@ const updateShard = (context, positions, child, index, options, callback) => {
180186

181187
position++
182188

183-
context.ipld.get(shard.cid, (err, result) => {
184-
if (err) {
185-
return next(err)
186-
}
189+
const shardCid = shard.cid
190+
context.ipld.get(shardCid).then(
191+
(shardNode) => {
192+
if (position < positions.length) {
193+
const nextPrefix = positions[position].prefix
194+
const nextShard = shardNode.links.find(link => link.name.substring(0, 2) === nextPrefix)
187195

188-
if (position < positions.length) {
189-
const nextPrefix = positions[position].prefix
190-
const nextShard = result.value.links.find(link => link.name.substring(0, 2) === nextPrefix)
191-
192-
if (nextShard) {
193-
shard = nextShard
196+
if (nextShard) {
197+
shard = nextShard
198+
}
194199
}
195-
}
196200

197-
next(err, { cid: result && result.cid, node: result && result.value })
198-
})
201+
next(null, { cid: shardCid, node: shardNode })
202+
},
203+
(error) => next(error)
204+
)
199205
},
200206
done
201207
)

src/core/utils/create-node.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,22 @@ const UnixFS = require('ipfs-unixfs')
55
const {
66
DAGNode
77
} = require('ipld-dag-pb')
8+
const toMulticodecCode = require('./to-multicodec-code')
89

910
const createNode = (context, type, options, callback) => {
1011
waterfall([
1112
(done) => DAGNode.create(new UnixFS(type).marshal(), [], done),
12-
(node, done) => context.ipld.put(node, {
13-
version: options.cidVersion,
14-
format: options.format,
15-
hashAlg: options.hashAlg
16-
}, (err, cid) => done(err, {
17-
cid,
18-
node
19-
}))
13+
(node, done) => context.ipld.put(
14+
node,
15+
toMulticodecCode(options.format),
16+
{
17+
cidVersion: options.cidVersion,
18+
hashAlg: toMulticodecCode(options.hashAlg)
19+
}
20+
).then(
21+
(cid) => done(null, { cid, node }),
22+
(error) => done(error)
23+
)
2024
], callback)
2125
}
2226

src/core/utils/hamt-utils.js

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const Bucket = require('hamt-sharding/src/bucket')
99
const DirSharded = require('ipfs-unixfs-importer/src/importer/dir-sharded')
1010
const log = require('debug')('ipfs:mfs:core:utils:hamt-utils')
1111
const UnixFS = require('ipfs-unixfs')
12+
const toMulticodecCode = require('./to-multicodec-code')
1213

1314
const updateHamtDirectory = (context, links, bucket, options, callback) => {
1415
// update parent with new bit field
@@ -23,15 +24,18 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
2324
},
2425
(parent, done) => {
2526
// Persist the new parent DAGNode
26-
context.ipld.put(parent, {
27-
version: options.cidVersion,
28-
format: options.codec,
29-
hashAlg: options.hashAlg,
30-
hashOnly: !options.flush
31-
}, (error, cid) => done(error, {
32-
node: parent,
33-
cid
34-
}))
27+
context.ipld.put(
28+
parent,
29+
toMulticodecCode(options.codec),
30+
{
31+
cidVersion: options.cidVersion,
32+
hashAlg: toMulticodecCode(options.hashAlg),
33+
hashOnly: !options.flush
34+
}
35+
).then(
36+
(cid) => done(null, { cid, node: parent }),
37+
(error) => done(error)
38+
)
3539
}
3640
], callback)
3741
}
@@ -133,43 +137,41 @@ const generatePath = (context, fileName, rootNode, callback) => {
133137

134138
// found subshard
135139
log(`Found subshard ${segment.prefix}`)
136-
context.ipld.get(link.cid, (err, result) => {
137-
if (err) {
138-
return next(err)
139-
}
140-
141-
// subshard hasn't been loaded, descend to the next level of the HAMT
142-
if (!path[index - 1]) {
143-
log(`Loaded new subshard ${segment.prefix}`)
144-
const node = result.value
145-
146-
return recreateHamtLevel(node.links, rootBucket, segment.bucket, parseInt(segment.prefix, 16), async (err, bucket) => {
147-
if (err) {
148-
return next(err)
149-
}
150-
151-
const position = await rootBucket._findNewBucketAndPos(fileName)
152-
153-
index++
154-
path.unshift({
155-
bucket: position.bucket,
156-
prefix: toPrefix(position.pos),
157-
node: node
140+
context.ipld.get(link.cid).then(
141+
(node) => {
142+
// subshard hasn't been loaded, descend to the next level of the HAMT
143+
if (!path[index - 1]) {
144+
log(`Loaded new subshard ${segment.prefix}`)
145+
146+
return recreateHamtLevel(node.links, rootBucket, segment.bucket, parseInt(segment.prefix, 16), async (err, bucket) => {
147+
if (err) {
148+
return next(err)
149+
}
150+
151+
const position = await rootBucket._findNewBucketAndPos(fileName)
152+
153+
index++
154+
path.unshift({
155+
bucket: position.bucket,
156+
prefix: toPrefix(position.pos),
157+
node: node
158+
})
159+
160+
next()
158161
})
162+
}
159163

160-
next()
161-
})
162-
}
163-
164-
const nextSegment = path[index - 1]
164+
const nextSegment = path[index - 1]
165165

166-
// add intermediate links to bucket
167-
addLinksToHamtBucket(result.value.links, nextSegment.bucket, rootBucket, (error) => {
168-
nextSegment.node = result.value
166+
// add intermediate links to bucket
167+
addLinksToHamtBucket(node.links, nextSegment.bucket, rootBucket, (error) => {
168+
nextSegment.node = node
169169

170-
next(error)
171-
})
172-
})
170+
next(error)
171+
})
172+
},
173+
(error) => next(error)
174+
)
173175
},
174176
async (err, path) => {
175177
await rootBucket.put(fileName, true)

src/core/utils/load-node.js

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
'use strict'
22

3-
const waterfall = require('async/waterfall')
43
const CID = require('cids')
54
const log = require('debug')('ipfs:mfs:utils:load-node')
65

@@ -9,13 +8,10 @@ const loadNode = (context, dagLink, callback) => {
98

109
log(`Loading DAGNode for child ${cid.toBaseEncodedString()}`)
1110

12-
waterfall([
13-
(cb) => context.ipld.get(cid, cb),
14-
(result, cb) => cb(null, {
15-
node: result.value,
16-
cid
17-
})
18-
], callback)
11+
context.ipld.get(cid).then(
12+
(node) => callback(null, { cid, node }),
13+
(error) => callback(error)
14+
)
1915
}
2016

2117
module.exports = loadNode

src/core/utils/remove-link.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const {
1212
generatePath,
1313
updateHamtDirectory
1414
} = require('./hamt-utils')
15+
const toMulticodecCode = require('./to-multicodec-code')
1516

1617
const defaultOptions = {
1718
parent: undefined,
@@ -39,8 +40,10 @@ const removeLink = (context, options, callback) => {
3940
log('Loading parent node', options.parentCid.toBaseEncodedString())
4041

4142
return waterfall([
42-
(cb) => context.ipld.get(options.parentCid, cb),
43-
(result, cb) => cb(null, result.value),
43+
(cb) => context.ipld.get(options.parentCid).then(
44+
(node) => cb(null, node),
45+
(error) => cb(error)
46+
),
4447
(node, cb) => removeLink(context, {
4548
...options,
4649
parent: node
@@ -69,14 +72,17 @@ const removeFromDirectory = (context, options, callback) => {
6972
waterfall([
7073
(cb) => DAGNode.rmLink(options.parent, options.name, cb),
7174
(newParentNode, cb) => {
72-
context.ipld.put(newParentNode, {
73-
version: options.cidVersion,
74-
format: options.codec,
75-
hashAlg: options.hashAlg
76-
}, (error, cid) => cb(error, {
77-
node: newParentNode,
78-
cid
79-
}))
75+
context.ipld.put(
76+
newParentNode,
77+
toMulticodecCode(options.codec),
78+
{
79+
cidVersion: options.cidVersion,
80+
hashAlg: toMulticodecCode(options.hashAlg)
81+
}
82+
).then(
83+
(cid) => cb(null, { cid, node: newParentNode }),
84+
(error) => cb(error)
85+
)
8086
},
8187
(result, cb) => {
8288
log('Updated regular directory', result.cid.toBaseEncodedString())
@@ -126,15 +132,18 @@ const updateShard = (context, positions, child, options, callback) => {
126132
return waterfall([
127133
(done) => DAGNode.rmLink(node, link.name, done),
128134
(node, done) => {
129-
context.ipld.put(node, {
130-
version: options.cidVersion,
131-
format: options.codec,
132-
hashAlg: options.hashAlg,
133-
hashOnly: !options.flush
134-
}, (error, cid) => done(error, {
135+
context.ipld.put(
135136
node,
136-
cid
137-
}))
137+
toMulticodecCode(options.codec),
138+
{
139+
cidVersion: options.cidVersion,
140+
hashAlg: toMulticodecCode(options.hashAlg),
141+
hashOnly: !options.flush
142+
}
143+
).then(
144+
(cid) => done(null, { cid, node }),
145+
(error) => done(error)
146+
)
138147
},
139148
(result, done) => {
140149
bucket.del(child.name)

src/core/utils/to-multicodec-code.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict'
2+
3+
const multicodec = require('multicodec')
4+
5+
// Converts a multicodec name to the corresponding code if it isn't a code
6+
// already
7+
const toMulticodecCode = (name) => {
8+
if (typeof name === 'string') {
9+
const constantName = name.toUpperCase().replace(/-/g, '_')
10+
return multicodec[constantName]
11+
} else {
12+
return name
13+
}
14+
}
15+
16+
module.exports = toMulticodecCode

0 commit comments

Comments
 (0)