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

Commit 02a158f

Browse files
committed
refactor: update to new IPLD API
This is part of the Awesome Endeavour: Async Iterators: ipfs/js-ipfs#1670
1 parent bb17a2d commit 02a158f

File tree

6 files changed

+91
-91
lines changed

6 files changed

+91
-91
lines changed

package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,20 @@
4040
"chai": "^4.2.0",
4141
"detect-node": "^2.0.4",
4242
"dirty-chai": "^2.0.1",
43-
"ipld": "~0.20.2",
43+
"ipld": "git+https://github.com/ipld/js-ipld.git#new-api-impl",
4444
"ipld-dag-pb": "~0.15.2",
4545
"ipld-in-memory": "^2.0.0",
46+
"multicodec": "~0.5.0",
4647
"pull-pushable": "^2.2.0",
4748
"pull-stream-to-stream": "^1.3.4",
48-
"pull-zip": "^2.0.1",
4949
"sinon": "^7.1.0",
5050
"stream-to-pull-stream": "^1.7.2"
5151
},
5252
"dependencies": {
5353
"async": "^2.6.1",
5454
"cids": "~0.5.5",
5555
"ipfs-unixfs": "~0.1.16",
56-
"ipfs-unixfs-importer": "~0.38.0",
56+
"ipfs-unixfs-importer": "git+https://github.com/ipfs/js-ipfs-unixfs-importer.git#new-ipld-api",
5757
"pull-cat": "^1.1.11",
5858
"pull-paramap": "^1.2.2",
5959
"pull-stream": "^3.6.9",

src/file.js

+19-17
Original file line numberDiff line numberDiff line change
@@ -150,23 +150,25 @@ function getChildren (dag, offset, end) {
150150

151151
return pull(
152152
once(filteredLinks),
153-
paramap((children, cb) => {
154-
dag.getMany(children.map(child => child.link.cid), (err, results) => {
155-
if (err) {
156-
return cb(err)
157-
}
158-
159-
cb(null, results.map((result, index) => {
160-
const child = children[index]
161-
162-
return {
163-
start: child.start,
164-
end: child.end,
165-
node: result,
166-
size: child.size
167-
}
168-
}))
169-
})
153+
paramap(async (children, cb) => {
154+
const results = dag.get(children.map(child => child.link.cid))
155+
const final = []
156+
for (
157+
let index = 0, result = await results.next();
158+
!result.done;
159+
index++, result = await results.next()
160+
) {
161+
const child = children[index]
162+
const node = result.value
163+
164+
final.push({
165+
start: child.start,
166+
end: child.end,
167+
node: node,
168+
size: child.size
169+
})
170+
}
171+
cb(null, final)
170172
}),
171173
flatten()
172174
)

src/resolve.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,8 @@ function createResolver (dag, options, depth, parent) {
4545
const cid = new CID(item.multihash)
4646

4747
waterfall([
48-
(done) => dag.get(cid, done),
49-
(node, done) => done(null, resolveItem(cid, node.value, item, options))
48+
async () => dag.get([cid]).first(),
49+
(node, done) => done(null, resolveItem(cid, node, item, options))
5050
], cb)
5151
}),
5252
flatten(),

test/exporter-sharded.spec.js

+17-14
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const {
2020
DAGLink,
2121
DAGNode
2222
} = require('ipld-dag-pb')
23+
const multicodec = require('multicodec')
2324

2425
const SHARD_SPLIT_THRESHOLD = 10
2526

@@ -88,18 +89,18 @@ describe('exporter sharded', function () {
8889
}),
8990
collect(cb)
9091
),
91-
(imported, cb) => {
92+
async (imported) => {
9293
directory = new CID(imported.pop().multihash)
9394

9495
// store the CIDs, we will validate them later
9596
imported.forEach(imported => {
9697
files[imported.path].cid = new CID(imported.multihash)
9798
})
9899

99-
ipld.get(directory, cb)
100+
return ipld.get([directory]).first()
100101
},
101-
({ value, cid }, cb) => {
102-
const dir = UnixFS.unmarshal(value.data)
102+
({ data }, cb) => {
103+
const dir = UnixFS.unmarshal(data)
103104

104105
expect(dir.type).to.equal('hamt-sharded-directory')
105106

@@ -374,24 +375,26 @@ describe('exporter sharded', function () {
374375
new DAGLink('shard', 5, dir)
375376
], cb)
376377
},
377-
(node, cb) => {
378-
ipld.put(node, {
378+
async (node) => {
379+
const result = ipld.put([node], {
379380
version: 0,
380-
format: 'dag-pb',
381-
hashAlg: 'sha2-256'
382-
}, cb)
381+
format: multicodec.DAG_PB,
382+
hashAlg: multicodec.SHA2_256
383+
})
384+
return result.first()
383385
},
384386
(cid, cb) => {
385387
DAGNode.create(new UnixFS('hamt-sharded-directory').marshal(), [
386388
new DAGLink('75normal-dir', 5, cid)
387389
], cb)
388390
},
389-
(node, cb) => {
390-
ipld.put(node, {
391+
async (node) => {
392+
const result = ipld.put([node], {
391393
version: 1,
392-
format: 'dag-pb',
393-
hashAlg: 'sha2-256'
394-
}, cb)
394+
format: multicodec.DAG_PB,
395+
hashAlg: multicodec.SHA_256
396+
})
397+
return result.first()
395398
},
396399
(dir, cb) => {
397400
pull(

test/exporter-subtree.spec.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const pull = require('pull-stream')
1111
const randomBytes = require('./helpers/random-bytes')
1212
const waterfall = require('async/waterfall')
1313
const importer = require('ipfs-unixfs-importer')
14+
const multicodec = require('multicodec')
1415

1516
const ONE_MEG = Math.pow(1024, 2)
1617

@@ -132,7 +133,11 @@ describe('exporter subtree', () => {
132133
),
133134
(files, cb) => cb(null, files.pop().multihash),
134135
(buf, cb) => cb(null, new CID(buf)),
135-
(cid, cb) => ipld.put({ a: { file: cid } }, { format: 'dag-cbor' }, cb),
136+
async (cid) => {
137+
return ipld.put([{ a: { file: cid } }], {
138+
format: multicodec.DAG_CBOR
139+
}).first()
140+
},
136141
(cborNodeCid, cb) => pull(
137142
exporter(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`, ipld),
138143
pull.collect(cb)

test/exporter.spec.js

+44-54
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const IPLD = require('ipld')
88
const inMemory = require('ipld-in-memory')
99
const UnixFS = require('ipfs-unixfs')
1010
const pull = require('pull-stream')
11-
const zip = require('pull-zip')
1211
const CID = require('cids')
1312
const doUntil = require('async/doUntil')
1413
const waterfall = require('async/waterfall')
@@ -25,6 +24,7 @@ const {
2524
} = require('ipld-dag-pb')
2625
const isNode = require('detect-node')
2726
const randomBytes = require('./helpers/random-bytes')
27+
const multicodec = require('multicodec')
2828

2929
const exporter = require('../src')
3030
const importer = require('ipfs-unixfs-importer')
@@ -51,13 +51,14 @@ describe('exporter', () => {
5151
DAGNode.create(file.marshal(), options.links, (err, node) => {
5252
expect(err).to.not.exist()
5353

54-
ipld.put(node, {
54+
const result = ipld.put([node], {
5555
version: 0,
56-
hashAlg: 'sha2-256',
57-
format: 'dag-pb'
58-
}, (err, cid) => {
59-
cb(err, { file: file, node: node, cid: cid })
56+
hashAlg: multicodec.SHA2_256,
57+
format: multicodec.DAG_PB
6058
})
59+
result.first()
60+
.then((cid) => cb(null, { file: file, node: node, cid: cid }))
61+
.catch((error) => cb(error))
6162
})
6263
}
6364

@@ -182,47 +183,41 @@ describe('exporter', () => {
182183
})
183184

184185
it('ensure hash inputs are sanitized', (done) => {
185-
dagPut((err, result) => {
186+
dagPut(async (err, result) => {
186187
expect(err).to.not.exist()
187188

188-
ipld.get(result.cid, (err, res) => {
189-
expect(err).to.not.exist()
190-
const unmarsh = UnixFS.unmarshal(result.node.data)
189+
const node = await ipld.get([result.cid]).first()
190+
const unmarsh = UnixFS.unmarshal(node.data)
191191

192-
expect(unmarsh.data).to.deep.equal(result.file.data)
192+
expect(unmarsh.data).to.deep.equal(result.file.data)
193193

194-
pull(
195-
exporter(result.cid, ipld),
196-
pull.collect(onFiles)
197-
)
194+
pull(
195+
exporter(result.cid, ipld),
196+
pull.collect(onFiles)
197+
)
198198

199-
function onFiles (err, files) {
200-
expect(err).to.equal(null)
201-
expect(files).to.have.length(1)
202-
expect(files[0]).to.have.property('hash')
203-
expect(files[0]).to.have.property('path', result.cid.toBaseEncodedString())
204-
fileEql(files[0], unmarsh.data, done)
205-
}
206-
})
199+
function onFiles (err, files) {
200+
expect(err).to.equal(null)
201+
expect(files).to.have.length(1)
202+
expect(files[0]).to.have.property('hash')
203+
expect(files[0]).to.have.property('path', result.cid.toBaseEncodedString())
204+
fileEql(files[0], unmarsh.data, done)
205+
}
207206
})
208207
})
209208

210209
it('exports a file with no links', (done) => {
211-
dagPut((err, result) => {
210+
dagPut(async (err, result) => {
212211
expect(err).to.not.exist()
213212

213+
const node = await ipld.get([result.cid]).first()
214+
const unmarsh = UnixFS.unmarshal(node.data)
215+
214216
pull(
215-
zip(
216-
pull(
217-
ipld.getStream(result.cid),
218-
pull.map((res) => UnixFS.unmarshal(res.value.data))
219-
),
220-
exporter(result.cid, ipld)
221-
),
217+
exporter(result.cid, ipld),
222218
pull.collect((err, values) => {
223219
expect(err).to.not.exist()
224-
const unmarsh = values[0][0]
225-
const file = values[0][1]
220+
const file = values[0]
226221

227222
fileEql(file, unmarsh.data, done)
228223
})
@@ -292,25 +287,20 @@ describe('exporter', () => {
292287

293288
dagPut({
294289
content: randomBytes(100)
295-
}, (err, result) => {
290+
}, async (err, result) => {
296291
expect(err).to.not.exist()
297292

293+
const node = await ipld.get([result.cid]).first()
294+
const unmarsh = UnixFS.unmarshal(node.data)
295+
298296
pull(
299-
zip(
300-
pull(
301-
ipld.getStream(result.cid),
302-
pull.map((res) => UnixFS.unmarshal(res.value.data))
303-
),
304-
exporter(result.cid, ipld, {
305-
offset,
306-
length
307-
})
308-
),
297+
exporter(result.cid, ipld, {
298+
offset,
299+
length
300+
}),
309301
pull.collect((err, values) => {
310302
expect(err).to.not.exist()
311-
312-
const unmarsh = values[0][0]
313-
const file = values[0][1]
303+
const file = values[0]
314304

315305
fileEql(file, unmarsh.data.slice(offset, offset + length), done)
316306
})
@@ -1118,13 +1108,13 @@ function createAndPersistNode (ipld, type, data, children, callback) {
11181108
return callback(error)
11191109
}
11201110

1121-
ipld.put(node, {
1111+
const result = ipld.put([node], {
11221112
version: 1,
1123-
hashAlg: 'sha2-256',
1124-
format: 'dag-pb'
1125-
}, (error, cid) => callback(error, {
1126-
node,
1127-
cid
1128-
}))
1113+
hashAlg: multicodec.SHA2_256,
1114+
format: multicodec.DAG_PB
1115+
})
1116+
result.first()
1117+
.then((cid) => callback(null, { node, cid }))
1118+
.catch((error) => callback(error))
11291119
})
11301120
}

0 commit comments

Comments
 (0)