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

Commit 4b209d6

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 4b209d6

File tree

6 files changed

+103
-92
lines changed

6 files changed

+103
-92
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

+5-2
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ 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+
(cb) => dag.get([cid]).first().then(
49+
(node) => cb(null, node),
50+
(error) => cb(error)
51+
),
52+
(node, done) => done(null, resolveItem(cid, node, item, options))
5053
], cb)
5154
}),
5255
flatten(),

test/exporter-sharded.spec.js

+23-13
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

@@ -96,10 +97,13 @@ describe('exporter sharded', function () {
9697
files[imported.path].cid = new CID(imported.multihash)
9798
})
9899

99-
ipld.get(directory, cb)
100+
ipld.get([directory]).first().then(
101+
(data) => cb(null, data),
102+
(error) => cb(error)
103+
)
100104
},
101-
({ value, cid }, cb) => {
102-
const dir = UnixFS.unmarshal(value.data)
105+
({ data }, cb) => {
106+
const dir = UnixFS.unmarshal(data)
103107

104108
expect(dir.type).to.equal('hamt-sharded-directory')
105109

@@ -375,23 +379,29 @@ describe('exporter sharded', function () {
375379
], cb)
376380
},
377381
(node, cb) => {
378-
ipld.put(node, {
379-
version: 0,
380-
format: 'dag-pb',
381-
hashAlg: 'sha2-256'
382-
}, cb)
382+
const result = ipld.put([node], multicodec.DAG_PB, {
383+
cidVersion: 0,
384+
hashAlg: multicodec.SHA2_256
385+
})
386+
result.first().then(
387+
(cid) => cb(null, cid),
388+
(error) => cb(error)
389+
)
383390
},
384391
(cid, cb) => {
385392
DAGNode.create(new UnixFS('hamt-sharded-directory').marshal(), [
386393
new DAGLink('75normal-dir', 5, cid)
387394
], cb)
388395
},
389396
(node, cb) => {
390-
ipld.put(node, {
391-
version: 1,
392-
format: 'dag-pb',
393-
hashAlg: 'sha2-256'
394-
}, cb)
397+
const result = ipld.put([node], multicodec.DAG_PB, {
398+
cidVersion: 1,
399+
hashAlg: multicodec.SHA_256
400+
})
401+
result.first().then(
402+
(cid) => cb(null, cid),
403+
(error) => cb(error)
404+
)
395405
},
396406
(dir, cb) => {
397407
pull(

test/exporter-subtree.spec.js

+9-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,14 @@ 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+
(cid, cb) => {
137+
ipld.put(
138+
[{ a: { file: cid } }], multicodec.DAG_CBOR
139+
).first().then(
140+
(cborNodeCid) => cb(null, cborNodeCid),
141+
(error) => cb(error)
142+
)
143+
},
136144
(cborNodeCid, cb) => pull(
137145
exporter(`${cborNodeCid.toBaseEncodedString()}/a/file/level-1/200Bytes.txt`, ipld),
138146
pull.collect(cb)

test/exporter.spec.js

+44-56
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,13 @@ describe('exporter', () => {
5151
DAGNode.create(file.marshal(), options.links, (err, node) => {
5252
expect(err).to.not.exist()
5353

54-
ipld.put(node, {
55-
version: 0,
56-
hashAlg: 'sha2-256',
57-
format: 'dag-pb'
58-
}, (err, cid) => {
59-
cb(err, { file: file, node: node, cid: cid })
54+
const result = ipld.put([node], multicodec.DAG_PB, {
55+
cidVersion: 0,
56+
hashAlg: multicodec.SHA2_256,
6057
})
58+
result.first()
59+
.then((cid) => cb(null, { file: file, node: node, cid: cid }))
60+
.catch((error) => cb(error))
6161
})
6262
}
6363

@@ -182,47 +182,41 @@ describe('exporter', () => {
182182
})
183183

184184
it('ensure hash inputs are sanitized', (done) => {
185-
dagPut((err, result) => {
185+
dagPut(async (err, result) => {
186186
expect(err).to.not.exist()
187187

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

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

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

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-
})
198+
function onFiles (err, files) {
199+
expect(err).to.equal(null)
200+
expect(files).to.have.length(1)
201+
expect(files[0]).to.have.property('hash')
202+
expect(files[0]).to.have.property('path', result.cid.toBaseEncodedString())
203+
fileEql(files[0], unmarsh.data, done)
204+
}
207205
})
208206
})
209207

210208
it('exports a file with no links', (done) => {
211-
dagPut((err, result) => {
209+
dagPut(async (err, result) => {
212210
expect(err).to.not.exist()
213211

212+
const node = await ipld.get([result.cid]).first()
213+
const unmarsh = UnixFS.unmarshal(node.data)
214+
214215
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-
),
216+
exporter(result.cid, ipld),
222217
pull.collect((err, values) => {
223218
expect(err).to.not.exist()
224-
const unmarsh = values[0][0]
225-
const file = values[0][1]
219+
const file = values[0]
226220

227221
fileEql(file, unmarsh.data, done)
228222
})
@@ -292,25 +286,20 @@ describe('exporter', () => {
292286

293287
dagPut({
294288
content: randomBytes(100)
295-
}, (err, result) => {
289+
}, async (err, result) => {
296290
expect(err).to.not.exist()
297291

292+
const node = await ipld.get([result.cid]).first()
293+
const unmarsh = UnixFS.unmarshal(node.data)
294+
298295
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-
),
296+
exporter(result.cid, ipld, {
297+
offset,
298+
length
299+
}),
309300
pull.collect((err, values) => {
310301
expect(err).to.not.exist()
311-
312-
const unmarsh = values[0][0]
313-
const file = values[0][1]
302+
const file = values[0]
314303

315304
fileEql(file, unmarsh.data.slice(offset, offset + length), done)
316305
})
@@ -1118,13 +1107,12 @@ function createAndPersistNode (ipld, type, data, children, callback) {
11181107
return callback(error)
11191108
}
11201109

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

0 commit comments

Comments
 (0)