This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
/
test-unixfs-engine.js
332 lines (296 loc) · 12 KB
/
test-unixfs-engine.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* eslint-env mocha */
'use strict'
const unixFSEngine = require('./../src')
const importer = unixFSEngine.importer
const exporter = unixFSEngine.exporter
const expect = require('chai').expect
const IPFSRepo = require('ipfs-repo')
const BlockService = require('ipfs-blocks').BlockService
const DAGService = require('ipfs-merkle-dag').DAGService
const DAGNode = require('ipfs-merkle-dag').DAGNode
const fsBlobStore = require('fs-blob-store')
const bs58 = require('bs58')
const fs = require('fs')
const UnixFS = require('ipfs-unixfs')
const path = require('path')
let ds
describe('layout: importer', function () {
const big = path.join(__dirname, '/test-data/1.2MiB.txt')
const small = path.join(__dirname, '/test-data/200Bytes.txt')
const dirSmall = path.join(__dirname, '/test-data/dir-small')
const dirBig = path.join(__dirname, '/test-data/dir-big')
const dirNested = path.join(__dirname, '/test-data/dir-nested')
// check to see if missing empty dirs need to be created
fs.stat(path.join(__dirname, '/test-data/dir-nested/dir-another'), function (err, exists) {
if (err) {
fs.mkdir(path.join(__dirname, '/test-data/dir-nested/dir-another'))
}
})
fs.stat(path.join(__dirname, '/test-data/dir-nested/level-1/level-2'), function (err, exists) {
if (err) {
fs.mkdir(path.join(__dirname, '/test-data/dir-nested/level-1/level-2'))
}
})
it('start dag service', function (done) {
const options = {
stores: {
keys: fsBlobStore,
config: fsBlobStore,
datastore: fsBlobStore,
// datastoreLegacy: needs https://github.com/ipfsBlobStore/js-ipfsBlobStore-repo/issues/6#issuecomment-164650642
logs: fsBlobStore,
locks: fsBlobStore,
version: fsBlobStore
}
}
const repo = new IPFSRepo(process.env.IPFS_PATH, options)
const bs = new BlockService(repo)
ds = new DAGService(bs)
expect(bs).to.exist
expect(ds).to.exist
done()
})
it('import a bad path', (done) => {
importer.import('/foo/bar/quux/a!wofjaeiwojfoiew', ds, function (err, stat) {
expect(err).to.exist
done()
})
})
it('import a small file', (done) => {
importer.import(small, ds, function (err, stat) {
expect(err).to.not.exist
ds.get(stat.Hash, (err, node) => {
expect(err).to.not.exist
const smallDAGNode = new DAGNode()
const buf = fs.readFileSync(small + '.block')
smallDAGNode.unMarshal(buf)
expect(node.size()).to.equal(smallDAGNode.size())
expect(node.multihash()).to.deep.equal(smallDAGNode.multihash())
done()
})
})
})
it('import a big file', (done) => {
importer.import(big, ds, function (err, stat) {
expect(err).to.not.exist
ds.get(stat.Hash, (err, node) => {
expect(err).to.not.exist
const bigDAGNode = new DAGNode()
const buf = fs.readFileSync(big + '.block')
bigDAGNode.unMarshal(buf)
expect(node.size()).to.equal(bigDAGNode.size())
expect(node.links).to.deep.equal(bigDAGNode.links)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const bigDAGNodeUnixFS = UnixFS.unmarshal(bigDAGNode.data)
expect(nodeUnixFS.type).to.equal(bigDAGNodeUnixFS.type)
expect(nodeUnixFS.data).to.deep.equal(bigDAGNodeUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(bigDAGNodeUnixFS.blockSizes)
expect(nodeUnixFS.fileSize()).to.equal(bigDAGNodeUnixFS.fileSize())
expect(node.data).to.deep.equal(bigDAGNode.data)
expect(node.multihash()).to.deep.equal(bigDAGNode.multihash())
ds.get(node.links[0].hash, (err, node) => {
expect(err).to.not.exist
const leaf = new DAGNode()
const buf2 = fs.readFileSync(big + '.link-block0')
leaf.unMarshal(buf2)
expect(node.links).to.deep.equal(leaf.links)
expect(node.links.length).to.equal(0)
expect(leaf.links.length).to.equal(0)
expect(leaf.marshal()).to.deep.equal(buf2)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const leafUnixFS = UnixFS.unmarshal(leaf.data)
expect(nodeUnixFS.type).to.equal(leafUnixFS.type)
expect(nodeUnixFS.fileSize()).to.equal(leafUnixFS.fileSize())
expect(nodeUnixFS.data).to.deep.equal(leafUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(leafUnixFS.blockSizes)
expect(node.data).to.deep.equal(leaf.data)
expect(node.marshal()).to.deep.equal(leaf.marshal())
done()
})
})
})
})
it('import a small directory', (done) => {
importer.import(dirSmall, ds, {
recursive: true
}, function (err, stats) {
expect(err).to.not.exist
ds.get(stats.Hash, (err, node) => {
expect(err).to.not.exist
const dirSmallNode = new DAGNode()
const buf = fs.readFileSync(dirSmall + '.block')
dirSmallNode.unMarshal(buf)
expect(node.links).to.deep.equal(dirSmallNode.links)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const dirUnixFS = UnixFS.unmarshal(dirSmallNode.data)
expect(nodeUnixFS.type).to.equal(dirUnixFS.type)
expect(nodeUnixFS.fileSize()).to.equal(dirUnixFS.fileSize())
expect(nodeUnixFS.data).to.deep.equal(dirUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(dirUnixFS.blockSizes)
expect(node.data).to.deep.equal(dirSmallNode.data)
expect(node.marshal()).to.deep.equal(dirSmallNode.marshal())
done()
})
})
})
it('import a big directory', (done) => {
importer.import(dirBig, ds, {
recursive: true
}, function (err, stats) {
expect(err).to.not.exist
ds.get(stats.Hash, (err, node) => {
expect(err).to.not.exist
const dirNode = new DAGNode()
const buf = fs.readFileSync(dirBig + '.block')
dirNode.unMarshal(buf)
expect(node.links).to.deep.equal(dirNode.links)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const dirUnixFS = UnixFS.unmarshal(dirNode.data)
expect(nodeUnixFS.type).to.equal(dirUnixFS.type)
expect(nodeUnixFS.fileSize()).to.equal(dirUnixFS.fileSize())
expect(nodeUnixFS.data).to.deep.equal(dirUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(dirUnixFS.blockSizes)
expect(node.data).to.deep.equal(dirNode.data)
expect(node.marshal()).to.deep.equal(dirNode.marshal())
done()
})
})
})
it('import a nested directory', (done) => {
importer.import(dirNested, ds, {
recursive: true
}, function (err, stats) {
expect(err).to.not.exist
expect(bs58.encode(stats.Hash).toString()).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN')
ds.get(stats.Hash, (err, node) => {
expect(err).to.not.exist
expect(node.links.length).to.equal(3)
const dirNode = new DAGNode()
const buf = fs.readFileSync(dirNested + '.block')
dirNode.unMarshal(buf)
expect(node.links).to.deep.equal(dirNode.links)
expect(node.data).to.deep.equal(dirNode.data)
done()
})
})
})
it('import a small buffer', (done) => {
// this is just like "import a small file"
const buf = fs.readFileSync(path.join(__dirname, '/test-data/200Bytes.txt'))
importer.import(buf, ds, function (err, stat) {
expect(err).to.not.exist
ds.get(stat.Hash, (err, node) => {
expect(err).to.not.exist
const smallDAGNode = new DAGNode()
const marbuf = fs.readFileSync(small + '.block')
smallDAGNode.unMarshal(marbuf)
expect(node.size()).to.equal(smallDAGNode.size())
expect(node.multihash()).to.deep.equal(smallDAGNode.multihash())
done()
})
})
})
it('import a big buffer', (done) => {
// this is just like "import a big file"
const buf = fs.readFileSync(path.join(__dirname, '/test-data/1.2MiB.txt'))
importer.import(buf, ds, function (err, stat) {
expect(err).to.not.exist
ds.get(stat.Hash, (err, node) => {
expect(err).to.not.exist
const bigDAGNode = new DAGNode()
const marbuf = fs.readFileSync(big + '.block')
bigDAGNode.unMarshal(marbuf)
expect(node.size()).to.equal(bigDAGNode.size())
expect(node.links).to.deep.equal(bigDAGNode.links)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const bigDAGNodeUnixFS = UnixFS.unmarshal(bigDAGNode.data)
expect(nodeUnixFS.type).to.equal(bigDAGNodeUnixFS.type)
expect(nodeUnixFS.data).to.deep.equal(bigDAGNodeUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(bigDAGNodeUnixFS.blockSizes)
expect(nodeUnixFS.fileSize()).to.equal(bigDAGNodeUnixFS.fileSize())
expect(node.data).to.deep.equal(bigDAGNode.data)
expect(node.multihash()).to.deep.equal(bigDAGNode.multihash())
ds.get(node.links[0].hash, (err, node) => {
expect(err).to.not.exist
const leaf = new DAGNode()
const marbuf2 = fs.readFileSync(big + '.link-block0')
leaf.unMarshal(marbuf2)
expect(node.links).to.deep.equal(leaf.links)
expect(node.links.length).to.equal(0)
expect(leaf.links.length).to.equal(0)
expect(leaf.marshal()).to.deep.equal(marbuf2)
const nodeUnixFS = UnixFS.unmarshal(node.data)
const leafUnixFS = UnixFS.unmarshal(leaf.data)
expect(nodeUnixFS.type).to.equal(leafUnixFS.type)
expect(nodeUnixFS.fileSize()).to.equal(leafUnixFS.fileSize())
expect(nodeUnixFS.data).to.deep.equal(leafUnixFS.data)
expect(nodeUnixFS.blockSizes).to.deep.equal(leafUnixFS.blockSizes)
expect(node.data).to.deep.equal(leaf.data)
expect(node.marshal()).to.deep.equal(leaf.marshal())
done()
})
})
})
})
it.skip('import from a readable stream', (done) => {
})
})
describe('layout: exporter', function () {
it('export a file with no links', (done) => {
const hash = 'QmQmZQxSKQppbsWfVzBvg59Cn3DKtsNVQ94bjAxg2h3Lb8'
const testExport = exporter(hash, ds)
testExport.on('file', (data) => {
ds.get(hash, (err, fetchedNode) => {
expect(err).to.not.exist
const unmarsh = UnixFS.unmarshal(fetchedNode.data)
expect(unmarsh.data).to.deep.equal(data.stream._readableState.buffer[0])
done()
})
})
})
it('export a small file with links', (done) => {
const hash = 'QmW7BDxEbGqxxSYVtn3peNPQgdDXbWkoQ6J1EFYAEuQV3Q'
const testExport = exporter(hash, ds)
testExport.on('file', (data) => {
var ws = fs.createWriteStream(path.join(process.cwd(), '/test', data.path))
data.stream.pipe(ws)
data.stream.on('end', () => {
const stats = fs.existsSync(path.join(process.cwd(), '/test', data.path))
expect(stats).to.equal(true)
fs.unlinkSync(path.join(process.cwd(), '/test', data.path))
done()
})
})
})
it('export a large file > 5mb', (done) => {
const hash = 'QmRQgufjp9vLE8XK2LGKZSsPCFCF6e4iynCQtNB5X2HBKE'
const testExport = exporter(hash, ds)
testExport.on('file', (data) => {
var ws = fs.createWriteStream(path.join(process.cwd(), '/test', data.path))
data.stream.pipe(ws)
data.stream.on('end', () => {
const stats = fs.existsSync(path.join(process.cwd(), '/test', data.path))
expect(stats).to.equal(true)
fs.unlinkSync(path.join(process.cwd(), '/test', data.path))
done()
})
})
})
it('export a directory', (done) => {
const hash = 'QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN'
var testExport = exporter(hash, ds)
var fs = []
var x = 0
testExport.on('file', (data) => {
fs.push(data)
x++
if (x === 4) {
expect(fs[0].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/200Bytes.txt')
expect(fs[1].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/dir-another')
expect(fs[2].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/level-1/200Bytes.txt')
expect(fs[3].path).to.equal('QmWChcSFMNcFkfeJtNd8Yru1rE6PhtCRfewi1tMwjkwKjN/level-1/level-2')
done()
}
})
})
})