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

Commit dafbe63

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

15 files changed

+147
-147
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.20.0",
45+
"ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl",
4646
"ipld-in-memory": "^2.0.0",
4747
"multihashes": "~0.4.14",
4848
"pull-buffer-stream": "^1.0.0",
@@ -57,14 +57,15 @@
5757
"interface-datastore": "~0.6.0",
5858
"ipfs-multipart": "~0.1.0",
5959
"ipfs-unixfs": "~0.1.16",
60-
"ipfs-unixfs-importer": "~0.38.0",
61-
"ipfs-unixfs-exporter": "~0.35.5",
60+
"ipfs-unixfs-exporter": "git+https://github.com/ipfs/js-ipfs-unixfs-exporter.git#new-ipld-api",
61+
"ipfs-unixfs-importer": "git+https://github.com/ipfs/js-ipfs-unixfs-importer.git#new-ipld-api",
6262
"ipld-dag-pb": "~0.15.0",
6363
"is-pull-stream": "~0.0.0",
6464
"is-stream": "^1.1.0",
6565
"joi": "^14.0.4",
6666
"joi-browser": "^13.4.0",
6767
"mortice": "^1.2.1",
68+
"multicodec": "~0.5.0",
6869
"once": "^1.4.0",
6970
"promisify-es6": "^1.0.3",
7071
"pull-cat": "^1.1.11",

src/core/cp.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ 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+
async () => context.ipld.get([parent.cid]).first(),
84+
(node, next) => addLink(context, {
85+
parent: node,
8686
parentCid: parent.cid,
8787
size: child.size,
8888
cid: child.cid,
@@ -165,8 +165,8 @@ const copyToDirectory = (context, sources, destination, destinationTrail, option
165165
const parent = destinationTrail[destinationTrail.length - 1]
166166

167167
waterfall([
168-
(next) => context.ipld.get(parent.cid, next),
169-
(result, next) => next(null, { cid: parent.cid, node: result.value })
168+
async () => context.ipld.get([parent.cid]).first(),
169+
(node, next) => next(null, { cid: parent.cid, node })
170170
].concat(
171171
sourceTrails.map((sourceTrail, index) => {
172172
return (parent, done) => {

src/core/utils/add-link.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const {
1414
generatePath,
1515
updateHamtDirectory
1616
} = require('./hamt-utils')
17+
const toMulticodecCode = require('./to-multicodec-code')
1718

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

4445
return waterfall([
45-
(cb) => context.ipld.get(options.parentCid, cb),
46-
(result, cb) => cb(null, result.value),
46+
async () => context.ipld.get([options.parentCid]).first(),
47+
(node, cb) => cb(null, node),
4748
(node, cb) => addLink(context, {
4849
...options,
4950
parent: node
@@ -108,17 +109,18 @@ const addToDirectory = (context, options, callback) => {
108109
waterfall([
109110
(done) => DAGNode.rmLink(options.parent, options.name, done),
110111
(parent, done) => DAGNode.addLink(parent, new DAGLink(options.name, options.size, options.cid), done),
111-
(parent, done) => {
112+
async (parent) => {
112113
// Persist the new parent DAGNode
113-
context.ipld.put(parent, {
114+
const cid = await context.ipld.put([parent], {
114115
version: options.cidVersion,
115-
format: options.codec,
116-
hashAlg: options.hashAlg,
116+
format: toMulticodecCode(options.codec),
117+
hashAlg: toMulticodecCode(options.hashAlg),
117118
hashOnly: !options.flush
118-
}, (error, cid) => done(error, {
119+
}).first()
120+
return {
119121
node: parent,
120122
cid
121-
}))
123+
}
122124
}
123125
], callback)
124126
}
@@ -162,10 +164,9 @@ const updateShard = (context, positions, child, options, callback) => {
162164
size: child.size,
163165
multihash: child.cid.buffer
164166
}], {}, done),
165-
({ node: { links: [ shard ] } }, done) => {
166-
return context.ipld.get(shard.cid, (err, result) => {
167-
done(err, { cid: shard.cid, node: result && result.value })
168-
})
167+
async ({ node: { links: [ shard ] } }) => {
168+
const node = await context.ipld.get([shard.cid]).first()
169+
return { cid: shard.cid, node }
169170
},
170171
(result, cb) => updateShardParent(context, bucket, node, link.name, result.node, result.cid, prefix, options, cb)
171172
], cb)

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+
async (node) => {
14+
const cid = await context.ipld.put([node], {
15+
version: options.cidVersion,
16+
format: toMulticodecCode(options.format),
17+
hashAlg: toMulticodecCode(options.hashAlg)
18+
}).first()
19+
return {
20+
cid,
21+
node
22+
}
23+
}
2024
], callback)
2125
}
2226

src/core/utils/hamt-utils.js

Lines changed: 39 additions & 39 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
@@ -21,17 +22,18 @@ const updateHamtDirectory = (context, links, bucket, options, callback) => {
2122

2223
DAGNode.create(dir.marshal(), links, cb)
2324
},
24-
(parent, done) => {
25+
async (parent) => {
2526
// Persist the new parent DAGNode
26-
context.ipld.put(parent, {
27+
const cid = await context.ipld.put([parent], {
2728
version: options.cidVersion,
28-
format: options.codec,
29-
hashAlg: options.hashAlg,
29+
format: toMulticodecCode(options.codec),
30+
hashAlg: toMulticodecCode(options.hashAlg),
3031
hashOnly: !options.flush
31-
}, (error, cid) => done(error, {
32+
}).first()
33+
return {
3234
node: parent,
3335
cid
34-
}))
36+
}
3537
}
3638
], callback)
3739
}
@@ -137,43 +139,41 @@ const generatePath = (context, fileName, rootNode, callback) => {
137139

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

164-
next()
165-
})
166-
}
167-
168-
const nextSegment = path[index - 1]
166+
const nextSegment = path[index - 1]
169167

170-
// add intermediate links to bucket
171-
addLinksToHamtBucket(result.value.links, nextSegment.bucket, rootBucket, (error) => {
172-
nextSegment.node = result.value
168+
// add intermediate links to bucket
169+
addLinksToHamtBucket(node.links, nextSegment.bucket, rootBucket, (error) => {
170+
nextSegment.node = node
173171

174-
next(error)
175-
})
176-
})
172+
next(error)
173+
})
174+
},
175+
(error) => next(error)
176+
)
177177
},
178178
async (err, path) => {
179179
await rootBucket.put(fileName, true)

src/core/utils/load-node.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ const loadNode = (context, dagLink, callback) => {
1010
log(`Loading DAGNode for child ${cid.toBaseEncodedString()}`)
1111

1212
waterfall([
13-
(cb) => context.ipld.get(cid, cb),
14-
(result, cb) => cb(null, {
15-
node: result.value,
13+
async () => context.ipld.get([cid]).first(),
14+
(node, cb) => cb(null, {
15+
node,
1616
cid
1717
})
1818
], callback)

src/core/utils/remove-link.js

Lines changed: 17 additions & 14 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,8 @@ 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+
async () => context.ipld.get([options.parentCid]).first(),
44+
(node, cb) => cb(null, node),
4445
(node, cb) => removeLink(context, {
4546
...options,
4647
parent: node
@@ -68,15 +69,16 @@ const removeLink = (context, options, callback) => {
6869
const removeFromDirectory = (context, options, callback) => {
6970
waterfall([
7071
(cb) => DAGNode.rmLink(options.parent, options.name, cb),
71-
(newParentNode, cb) => {
72-
context.ipld.put(newParentNode, {
72+
async (newParentNode) => {
73+
const cid = await context.ipld.put([newParentNode], {
7374
version: options.cidVersion,
74-
format: options.codec,
75-
hashAlg: options.hashAlg
76-
}, (error, cid) => cb(error, {
75+
format: toMulticodecCode(options.codec),
76+
hashAlg: toMulticodecCode(options.hashAlg)
77+
}).first()
78+
return {
7779
node: newParentNode,
7880
cid
79-
}))
81+
}
8082
},
8183
(result, cb) => {
8284
log('Updated regular directory', result.cid.toBaseEncodedString())
@@ -129,16 +131,17 @@ const updateShard = (context, positions, child, options, callback) => {
129131

130132
return waterfall([
131133
(done) => DAGNode.rmLink(node, link.name, done),
132-
(node, done) => {
133-
context.ipld.put(node, {
134+
async (node) => {
135+
const cid = await context.ipld.put([node], {
134136
version: options.cidVersion,
135-
format: options.codec,
136-
hashAlg: options.hashAlg,
137+
format: toMulticodecCode(options.codec),
138+
hashAlg: toMulticodecCode(options.hashAlg),
137139
hashOnly: !options.flush
138-
}, (error, cid) => done(error, {
140+
}).first()
141+
return {
139142
node,
140143
cid
141-
}))
144+
}
142145
},
143146
(result, done) => {
144147
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

src/core/utils/update-tree.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ const updateTree = (context, trail, options, callback) => {
1212
options = Object.assign({}, defaultOptions, options)
1313

1414
waterfall([
15-
(cb) => context.ipld.getMany(trail.map(node => node.cid), cb),
15+
async () => {
16+
return context.ipld.get(trail.map(node => node.cid)).all()
17+
},
1618
(nodes, cb) => {
1719
let index = trail.length - 1
1820

src/core/utils/with-mfs-root.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const {
77
} = require('ipld-dag-pb')
88
const log = require('debug')('ipfs:mfs:utils:with-mfs-root')
99
const waterfall = require('async/waterfall')
10+
const multicodec = require('multicodec')
1011

1112
const {
1213
MFS_ROOT_KEY
@@ -26,11 +27,11 @@ const withMfsRoot = (context, callback) => {
2627
return waterfall([
2728
// Store an empty node as the root
2829
(next) => DAGNode.create(new UnixFs('directory').marshal(), next),
29-
(node, next) => context.ipld.put(node, {
30+
async (node) => context.ipld.put([node], {
3031
version: 0,
31-
hashAlg: 'sha2-256',
32-
format: 'dag-pb'
33-
}, next),
32+
hashAlg: multicodec.SHA2_256,
33+
format: multicodec.DAG_PB
34+
}).first(),
3435
// Store the Buffer in the datastore
3536
(cid, next) => context.repo.datastore.put(MFS_ROOT_KEY, cid.buffer, (error) => next(error, cid))
3637
], cb)

0 commit comments

Comments
 (0)