This repository has been 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
/
builder.js
163 lines (142 loc) · 3.97 KB
/
builder.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
'use strict'
const extend = require('deep-extend')
const UnixFS = require('ipfs-unixfs')
const pull = require('pull-stream')
const through = require('pull-through')
const parallel = require('async/parallel')
const waterfall = require('async/waterfall')
const dagPB = require('ipld-dag-pb')
const CID = require('cids')
const reduce = require('./reduce')
const DAGNode = dagPB.DAGNode
const defaultOptions = {
chunkerOptions: {
maxChunkSize: 262144
}
}
module.exports = function (createChunker, ipldResolver, createReducer, _options) {
const options = extend({}, defaultOptions, _options)
return function (source) {
return function (items, cb) {
parallel(items.map((item) => (cb) => {
if (!item.content) {
// item is a directory
return createAndStoreDir(item, (err, node) => {
if (err) {
return cb(err)
}
if (node) {
source.push(node)
}
cb()
})
}
// item is a file
createAndStoreFile(item, (err, node) => {
if (err) {
return cb(err)
}
if (node) {
source.push(node)
}
cb()
})
}), cb)
}
}
function createAndStoreDir (item, callback) {
// 1. create the empty dir dag node
// 2. write it to the dag store
const d = new UnixFS('directory')
waterfall([
(cb) => DAGNode.create(d.marshal(), [], options.hashAlg, cb),
(node, cb) => {
if (options.onlyHash) return cb(null, node)
let cid = new CID(node.multihash)
if (options.cidVersion === 1) {
cid = cid.toV1()
}
ipldResolver.put(node, { cid }, (err) => cb(err, node))
}
], (err, node) => {
if (err) {
return callback(err)
}
callback(null, {
path: item.path,
multihash: node.multihash,
size: node.size
})
})
}
function createAndStoreFile (file, callback) {
if (Buffer.isBuffer(file.content)) {
file.content = pull.values([file.content])
}
if (typeof file.content !== 'function') {
return callback(new Error('invalid content'))
}
const reducer = createReducer(reduce(file, ipldResolver, options), options)
let previous
let count = 0
pull(
file.content,
createChunker(options.chunkerOptions),
pull.map(chunk => {
if (options.progress && typeof options.progress === 'function') {
options.progress(chunk.byteLength)
}
return new Buffer(chunk)
}),
pull.map(buffer => new UnixFS('file', buffer)),
pull.asyncMap((fileNode, callback) => {
DAGNode.create(fileNode.marshal(), [], options.hashAlg, (err, node) => {
callback(err, { DAGNode: node, fileNode: fileNode })
})
}),
pull.asyncMap((leaf, callback) => {
if (options.onlyHash) return callback(null, leaf)
let cid = new CID(leaf.DAGNode.multihash)
if (options.cidVersion === 1) {
cid = cid.toV1()
}
ipldResolver.put(leaf.DAGNode, { cid }, (err) => callback(err, leaf))
}),
pull.map((leaf) => {
return {
path: file.path,
multihash: leaf.DAGNode.multihash,
size: leaf.DAGNode.size,
leafSize: leaf.fileNode.fileSize(),
name: ''
}
}),
through( // mark as single node if only one single node
function onData (data) {
count++
if (previous) {
this.queue(previous)
}
previous = data
},
function ended () {
if (previous) {
if (count === 1) {
previous.single = true
}
this.queue(previous)
}
this.queue(null)
}
),
reducer,
pull.collect((err, roots) => {
if (err) {
callback(err)
} else {
callback(null, roots[0])
}
})
)
}
}