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 pathimport-export-nested-dir.js
124 lines (112 loc) · 3.77 KB
/
import-export-nested-dir.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
/* eslint-env mocha */
'use strict'
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 map = require('async/map')
const CID = require('cids')
const unixFSEngine = require('./../')
module.exports = (repo) => {
describe('import and export: directory', () => {
const rootHash = 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK'
let ipld
before(() => {
const bs = new BlockService(repo)
ipld = new Ipld({blockService: bs})
})
it('imports', function (done) {
this.timeout(20 * 1000)
pull(
pull.values([
{ path: 'a/b/c/d/e', content: pull.values([Buffer.from('banana')]) },
{ path: 'a/b/c/d/f', content: pull.values([Buffer.from('strawberry')]) },
{ path: 'a/b/g', content: pull.values([Buffer.from('ice')]) },
{ path: 'a/b/h', content: pull.values([Buffer.from('cream')]) }
]),
unixFSEngine.importer(ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()
expect(files.map(normalizeNode).sort(byPath)).to.be.eql([
{ path: 'a/b/h',
multihash: 'QmWHMpCtdNjemT2F3SjyrmnBXQXwEohaZd4apcbFBhbFRC' },
{ path: 'a/b/g',
multihash: 'QmQGwYzzTPcbqTiy2Nbp88gqqBqCWY4QZGfen45LFZkD5n' },
{ path: 'a/b/c/d/f',
multihash: 'QmNVHs2dy7AjGUotsubWVncRsD3SpRXm8MgmCCQTVdVACz' },
{ path: 'a/b/c/d/e',
multihash: 'QmYPbDKwc7oneCcEc6BcRSN5GXthTGWUCd19bTCyP9u3vH' },
{ path: 'a/b/c/d',
multihash: 'QmQGDXr3ysARM38n7h79Tx7yD3YxuzcnZ1naG71WMojPoj' },
{ path: 'a/b/c',
multihash: 'QmYTVcjYpN3hQLtJstCPE8hhEacAYjWAuTmmAAXoonamuE' },
{ path: 'a/b',
multihash: 'QmWyWYxq1GD9fEyckf5LrJv8hMW35CwfWwzDBp8bTw3NQj' },
{ path: 'a',
multihash: rootHash }
])
done()
})
)
})
it('exports', function (done) {
this.timeout(20 * 1000)
pull(
unixFSEngine.exporter(rootHash, ipld),
pull.collect((err, files) => {
expect(err).to.not.exist()
map(
files,
(file, callback) => {
if (file.content) {
pull(
file.content,
pull.collect(mapFile(file, callback))
)
} else {
callback(null, { path: file.path })
}
},
(err, files) => {
expect(err).to.not.exist()
expect(files.filter(fileHasContent).sort(byPath)).to.eql([
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/h',
content: 'cream' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/g',
content: 'ice' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/c/d/f',
content: 'strawberry' },
{ path: 'QmdCrquDwd7RfZ6GCZFEVADwe8uyyw1YmF9mtAB7etDgmK/b/c/d/e',
content: 'banana' }
])
done()
})
})
)
function mapFile (file, callback) {
return (err, fileContent) => {
callback(err, fileContent && {
path: file.path,
content: fileContent.toString()
})
}
}
})
})
}
function normalizeNode (node) {
return {
path: node.path,
multihash: new CID(node.multihash).toBaseEncodedString()
}
}
function fileHasContent (file) {
return Boolean(file.content)
}
function byPath (a, b) {
if (a.path > b.path) return -1
if (a.path < b.path) return 1
return 0
}