This repository was archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathimporter-flush.js
198 lines (176 loc) · 4.99 KB
/
importer-flush.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/* eslint-env mocha */
'use strict'
const createImporter = require('./../src').importer
const chai = require('chai')
chai.use(require('dirty-chai'))
const expect = chai.expect
const BlockService = require('ipfs-block-service')
const Ipld = require('ipld')
const pull = require('pull-stream')
const pushable = require('pull-pushable')
module.exports = (repo) => {
describe('importer: flush', () => {
let ipld
before(() => {
const bs = new BlockService(repo)
ipld = new Ipld({blockService: bs})
})
it('can push a single root file and flush yields no dirs', (done) => {
const source = pushable()
const importer = createImporter(ipld)
pull(
source,
importer,
pull.map(node => {
expect(node.path).to.be.eql('a')
return node
}),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(files.length).to.be.eql(1)
done()
})
)
source.push({
path: 'a',
content: pull.values([Buffer.from('hey')])
})
importer.flush((err, hash) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(hash)).to.be.true()
source.end()
})
})
it('can push a nested file and flush yields parent dir', (done) => {
const source = pushable()
const importer = createImporter(ipld)
let count = 0
pull(
source,
importer,
pull.map(function (node) {
count++
if (count === 1) {
expect(node.path).to.be.eql('b/c')
} else if (count === 2) {
expect(node.path).to.be.eql('b')
}
return node
}),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(count).to.be.eql(2)
done()
})
)
source.push({
path: 'b/c',
content: pull.values([Buffer.from('hey')])
})
importer.flush((err, hash) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(hash)).to.be.true()
source.end()
})
})
it('can flush many times, always coherent', (done) => {
const maxDepth = 4
const maxEntriesPerDir = 3
let count = 0
const tree = { children: {}, path: '', depth: 0, yielded: true }
let currentDir = tree
const source = pushable()
const importer = createImporter(ipld)
pull(
source,
importer,
pull.map((node) => {
count++
markDirAsYielded(node)
return node
}),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(count).to.be.eql(2)
done()
})
)
pushAndFlush()
function pushAndFlush () {
const childCount = Object.keys(currentDir.children).length
const newDirName = childCount.toString()
const dirPath = currentDir.path + (currentDir.depth > 0 ? '/' : '') + newDirName
const newDir = {
children: {},
path: dirPath,
depth: currentDir.depth + 1,
yielded: false,
parent: currentDir
}
currentDir.children[newDirName] = newDir
markAncestorsAsDirty(currentDir)
const filePath = dirPath + '/filename'
const file = {
path: filePath,
content: pull.values([Buffer.from('file with path ' + filePath)])
}
source.push(file)
if (currentDir.depth === 0 || childCount + 1 === maxEntriesPerDir) {
currentDir = newDir
}
importer.flush((err, hash) => {
expect(err).to.not.exist()
expect(Buffer.isBuffer(hash)).to.be.true()
testAllYielded(tree)
if (currentDir.depth < maxDepth) {
pushAndFlush()
} else {
expect(count).to.be.eql(38)
done()
}
})
}
function markDirAsYielded (node) {
const dir = findDir(tree, node.path)
if (node.path === dir.path) {
expect(dir.yielded).to.be.false()
dir.yielded = true
}
}
function findDir (tree, path) {
const pathElems = path.split('/').filter(notEmpty)
const child = tree.children[pathElems.shift()]
if (!child) {
return tree
}
if (pathElems.length) {
return findDir(child, pathElems.join('/'))
} else {
return child
}
}
function testAllYielded (tree) {
if (tree.depth) {
expect(tree.yielded).to.be.true()
}
const childrenNames = Object.keys(tree.children)
childrenNames.forEach((childName) => {
const child = tree.children[childName]
testAllYielded(child)
})
}
function markAncestorsAsDirty (dir) {
dir.yielded = false
while (dir) {
dir = dir.parent
if (dir) {
dir.yielded = false
}
}
}
})
})
}
function notEmpty (str) {
return Boolean(str)
}