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

Commit bffeaad

Browse files
committed
feat: migrate importer to use IPLD Resolver
1 parent 4fcdd51 commit bffeaad

File tree

5 files changed

+73
-43
lines changed

5 files changed

+73
-43
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"run-series": "^1.1.4"
5050
},
5151
"dependencies": {
52-
"ipfs-merkle-dag": "^0.7.0",
52+
"ipld-dag-pb": "^0.0.1",
5353
"ipfs-unixfs": "^0.1.4",
5454
"is-ipfs": "^0.2.0",
5555
"multihashes": "^0.2.2",

src/importer.js

Lines changed: 49 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,67 @@
11
'use strict'
22

3-
const merkleDAG = require('ipfs-merkle-dag')
43
const UnixFS = require('ipfs-unixfs')
54
const assert = require('assert')
65
const pull = require('pull-stream')
76
const pushable = require('pull-pushable')
87
const write = require('pull-write')
98
const parallel = require('run-parallel')
9+
const dagPB = require('ipld-dag-pb')
10+
const CID = require('cids')
1011

1112
const fsc = require('./chunker-fixed-size')
1213
const createAndStoreTree = require('./tree')
1314

14-
const DAGNode = merkleDAG.DAGNode
15+
const DAGNode = dagPB.DAGNode
16+
const DAGLink = dagPB.DAGLink
1517

1618
const CHUNK_SIZE = 262144
1719

18-
module.exports = (dagService, options) => {
19-
assert(dagService, 'Missing dagService')
20+
module.exports = (ipldResolver, options) => {
21+
assert(ipldResolver, 'Missing IPLD Resolver')
2022

2123
const files = []
2224

2325
const source = pushable()
2426
const sink = write(
25-
makeWriter(source, files, dagService),
27+
makeWriter(source, files, ipldResolver),
2628
null,
2729
100,
2830
(err) => {
29-
if (err) return source.end(err)
31+
if (err) {
32+
return source.end(err)
33+
}
3034

31-
createAndStoreTree(files, dagService, source, () => {
35+
createAndStoreTree(files, ipldResolver, source, () => {
3236
source.end()
3337
})
3438
}
3539
)
3640

37-
return {source, sink}
41+
return {
42+
source: source,
43+
sink: sink
44+
}
3845
}
3946

40-
function makeWriter (source, files, dagService) {
47+
function makeWriter (source, files, ipldResolver) {
4148
return (items, cb) => {
4249
parallel(items.map((item) => (cb) => {
4350
if (!item.content) {
44-
return createAndStoreDir(item, dagService, (err, node) => {
45-
if (err) return cb(err)
51+
return createAndStoreDir(item, ipldResolver, (err, node) => {
52+
if (err) {
53+
return cb(err)
54+
}
4655
source.push(node)
4756
files.push(node)
4857
cb()
4958
})
5059
}
5160

52-
createAndStoreFile(item, dagService, (err, node) => {
53-
if (err) return cb(err)
61+
createAndStoreFile(item, ipldResolver, (err, node) => {
62+
if (err) {
63+
return cb(err)
64+
}
5465
source.push(node)
5566
files.push(node)
5667
cb()
@@ -59,16 +70,21 @@ function makeWriter (source, files, dagService) {
5970
}
6071
}
6172

62-
function createAndStoreDir (item, ds, cb) {
73+
function createAndStoreDir (item, ipldResolver, cb) {
6374
// 1. create the empty dir dag node
6475
// 2. write it to the dag store
6576

6677
const d = new UnixFS('directory')
6778
const n = new DAGNode()
6879
n.data = d.marshal()
6980

70-
ds.put(n, (err) => {
71-
if (err) return cb(err)
81+
ipldResolver.put({
82+
node: n,
83+
cid: new CID(n.multihash())
84+
}, (err) => {
85+
if (err) {
86+
return cb(err)
87+
}
7288
cb(null, {
7389
path: item.path,
7490
multihash: n.multihash(),
@@ -78,7 +94,7 @@ function createAndStoreDir (item, ds, cb) {
7894
})
7995
}
8096

81-
function createAndStoreFile (file, ds, cb) {
97+
function createAndStoreFile (file, ipldResolver, cb) {
8298
if (Buffer.isBuffer(file.content)) {
8399
file.content = pull.values([file.content])
84100
}
@@ -102,7 +118,10 @@ function createAndStoreFile (file, ds, cb) {
102118
const l = new UnixFS('file', Buffer(chunk))
103119
const n = new DAGNode(l.marshal())
104120

105-
ds.put(n, (err) => {
121+
ipldResolver.put({
122+
node: n,
123+
cid: new CID(n.multihash())
124+
}, (err) => {
106125
if (err) {
107126
return cb(new Error('Failed to store chunk'))
108127
}
@@ -116,7 +135,9 @@ function createAndStoreFile (file, ds, cb) {
116135
})
117136
}),
118137
pull.collect((err, leaves) => {
119-
if (err) return cb(err)
138+
if (err) {
139+
return cb(err)
140+
}
120141

121142
if (leaves.length === 1) {
122143
return cb(null, {
@@ -130,18 +151,23 @@ function createAndStoreFile (file, ds, cb) {
130151
// create a parent node and add all the leafs
131152

132153
const f = new UnixFS('file')
133-
const n = new merkleDAG.DAGNode()
154+
const n = new DAGNode()
134155

135156
for (let leaf of leaves) {
136157
f.addBlockSize(leaf.leafSize)
137158
n.addRawLink(
138-
new merkleDAG.DAGLink(leaf.Name, leaf.Size, leaf.Hash)
159+
new DAGLink(leaf.Name, leaf.Size, leaf.Hash)
139160
)
140161
}
141162

142163
n.data = f.marshal()
143-
ds.put(n, (err) => {
144-
if (err) return cb(err)
164+
ipldResolver.put({
165+
node: n,
166+
cid: new CID(n.multihash())
167+
}, (err) => {
168+
if (err) {
169+
return cb(err)
170+
}
145171

146172
cb(null, {
147173
path: file.path,

src/tree.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
const mh = require('multihashes')
44
const UnixFS = require('ipfs-unixfs')
5-
const merkleDAG = require('ipfs-merkle-dag')
5+
const CID = require('cids')
6+
const dagPB = require('ipld-dag-pb')
67

7-
const DAGLink = merkleDAG.DAGLink
8-
const DAGNode = merkleDAG.DAGNode
8+
const DAGLink = dagPB.DAGLink
9+
const DAGNode = dagPB.DAGNode
910

10-
module.exports = (files, dagService, source, cb) => {
11+
module.exports = (files, ipldResolver, source, cb) => {
1112
// file struct
1213
// {
1314
// path: // full path
@@ -101,7 +102,10 @@ module.exports = (files, dagService, source, cb) => {
101102
n.data = d.marshal()
102103

103104
pendingWrites++
104-
dagService.put(n, (err) => {
105+
ipldResolver.put({
106+
node: n,
107+
cid: new CID(n.multihash())
108+
}, (err) => {
105109
pendingWrites--
106110
if (err) {
107111
source.push(new Error('failed to store dirNode'))

test/test-exporter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
const expect = require('chai').expect
55
const BlockService = require('ipfs-block-service')
6-
const DAGService = require('ipfs-merkle-dag').DAGService
6+
const DAGService = require('ipld-resolver')
77
const UnixFS = require('ipfs-unixfs')
88
const fs = require('fs')
99
const path = require('path')

test/test-importer.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
const importer = require('./../src').importer
55
const expect = require('chai').expect
66
const BlockService = require('ipfs-block-service')
7-
const DAGService = require('ipfs-merkle-dag').DAGService
87
const fs = require('fs')
98
const path = require('path')
109
const pull = require('pull-stream')
1110
const mh = require('multihashes')
11+
const IPLDResolver = require('ipld-resolver')
1212

1313
function stringifyMh (files) {
1414
return files.map((file) => {
@@ -18,8 +18,8 @@ function stringifyMh (files) {
1818
}
1919

2020
module.exports = function (repo) {
21-
describe('importer', function () {
22-
let ds
21+
describe.only('importer', function () {
22+
let ipldResolver
2323

2424
const bigFile = fs.readFileSync(path.join(__dirname, '/test-data/1.2MiB.txt'))
2525
const smallFile = fs.readFileSync(path.join(__dirname, '/test-data/200Bytes.txt'))
@@ -30,7 +30,7 @@ module.exports = function (repo) {
3030

3131
before(() => {
3232
const bs = new BlockService(repo)
33-
ds = new DAGService(bs)
33+
ipldResolver = new IPLDResolver(bs)
3434
})
3535

3636
it('bad input', (done) => {
@@ -39,7 +39,7 @@ module.exports = function (repo) {
3939
path: '200Bytes.txt',
4040
content: 'banana'
4141
}]),
42-
importer(ds),
42+
importer(ipldResolver),
4343
pull.onEnd((err) => {
4444
expect(err).to.exist
4545
done()
@@ -53,7 +53,7 @@ module.exports = function (repo) {
5353
path: '200Bytes.txt',
5454
content: pull.values([smallFile])
5555
}]),
56-
importer(ds),
56+
importer(ipldResolver),
5757
pull.collect((err, files) => {
5858
expect(err).to.not.exist
5959
expect(stringifyMh(files)).to.be.eql([{
@@ -72,7 +72,7 @@ module.exports = function (repo) {
7272
path: '200Bytes.txt',
7373
content: smallFile
7474
}]),
75-
importer(ds),
75+
importer(ipldResolver),
7676
pull.collect((err, files) => {
7777
expect(err).to.not.exist
7878
expect(stringifyMh(files)).to.be.eql([{
@@ -91,7 +91,7 @@ module.exports = function (repo) {
9191
path: 'foo/bar/200Bytes.txt',
9292
content: pull.values([smallFile])
9393
}]),
94-
importer(ds),
94+
importer(ipldResolver),
9595
pull.collect((err, files) => {
9696
expect(err).to.not.exist
9797
expect(files.length).to.equal(3)
@@ -129,7 +129,7 @@ module.exports = function (repo) {
129129
path: '1.2MiB.txt',
130130
content: pull.values([bigFile])
131131
}]),
132-
importer(ds),
132+
importer(ipldResolver),
133133
pull.collect((err, files) => {
134134
expect(err).to.not.exist
135135
expect(stringifyMh(files)).to.be.eql([{
@@ -148,7 +148,7 @@ module.exports = function (repo) {
148148
path: 'foo-big/1.2MiB.txt',
149149
content: pull.values([bigFile])
150150
}]),
151-
importer(ds),
151+
importer(ipldResolver),
152152
pull.collect((err, files) => {
153153
expect(err).to.not.exist
154154

@@ -176,7 +176,7 @@ module.exports = function (repo) {
176176
pull.values([{
177177
path: 'empty-dir'
178178
}]),
179-
importer(ds),
179+
importer(ipldResolver),
180180
pull.collect((err, files) => {
181181
expect(err).to.not.exist
182182

@@ -200,7 +200,7 @@ module.exports = function (repo) {
200200
path: 'pim/1.2MiB.txt',
201201
content: pull.values([bigFile])
202202
}]),
203-
importer(ds),
203+
importer(ipldResolver),
204204
pull.collect((err, files) => {
205205
expect(err).to.not.exist
206206

@@ -235,7 +235,7 @@ module.exports = function (repo) {
235235
path: 'pam/1.2MiB.txt',
236236
content: pull.values([bigFile])
237237
}]),
238-
importer(ds),
238+
importer(ipldResolver),
239239
pull.collect((err, files) => {
240240
expect(err).to.not.exist
241241

0 commit comments

Comments
 (0)