This repository has been archived by the owner on Feb 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.js
222 lines (184 loc) · 5.42 KB
/
index.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
import { importer } from 'ipfs-unixfs-importer'
import { normaliseInput } from 'ipfs-core-utils/files/normalise-input-multiple'
import { parseChunkerString } from './utils.js'
import { pipe } from 'it-pipe'
import { withTimeoutOption } from 'ipfs-core-utils/with-timeout-option'
import mergeOpts from 'merge-options'
const mergeOptions = mergeOpts.bind({ ignoreUndefined: true })
/**
* @typedef {import('multiformats/cid').CID} CID
* @typedef {import('ipfs-unixfs-importer').ImportResult} ImportResult
* @typedef {import('multiformats/hashes/interface').MultihashHasher} MultihashHasher
* @typedef {import('ipfs-core-utils/multihashes').Multihashes} Multihashes
*/
/**
* @template T
*
* @typedef {import('it-stream-types').Source<T>} Source<T>
*/
/**
* @typedef {object} Context
* @property {import('ipfs-repo').IPFSRepo} repo
* @property {import('../../types').Preload} preload
* @property {Multihashes} hashers
* @property {import('ipfs-core-types/src/root').ShardingOptions} [options]
* @param {Context} context
*/
export function createAddAll ({ repo, preload, hashers, options }) {
const isShardingEnabled = options && options.sharding
/**
* @type {import('ipfs-core-types/src/root').API<{}>["addAll"]}
*/
async function * addAll (source, options = {}) {
const opts = mergeOptions({
shardSplitThreshold: isShardingEnabled ? 1000 : Infinity,
strategy: 'balanced'
}, options, {
...parseChunkerString(options.chunker)
})
// CID v0 is for multihashes encoded with sha2-256
if (opts.hashAlg && opts.hashAlg !== 'sha2-256' && opts.cidVersion !== 1) {
opts.cidVersion = 1
}
if (opts.trickle) {
opts.strategy = 'trickle'
}
if (opts.strategy === 'trickle') {
opts.leafType = 'raw'
opts.reduceSingleLeafToSelf = false
}
if (opts.cidVersion > 0 && opts.rawLeaves === undefined) {
// if the cid version is 1 or above, use raw leaves as this is
// what go does.
opts.rawLeaves = true
}
if (opts.hashAlg !== undefined && opts.rawLeaves === undefined) {
// if a non-default hash alg has been specified, use raw leaves as this is
// what go does.
opts.rawLeaves = true
}
delete opts.trickle
/** @type {Record<string, number>} */
const totals = {}
if (opts.progress) {
const prog = opts.progress
/**
* @param {number} bytes
* @param {string} path
*/
opts.progress = (bytes, path) => {
if (!totals[path]) {
totals[path] = 0
}
totals[path] += bytes
prog(totals[path], path)
}
}
/** @type {MultihashHasher | undefined} */
let hasher
if (opts.hashAlg != null) {
hasher = await hashers.getHasher(opts.hashAlg)
}
const iterator = pipe(
normaliseInput(source),
/**
* @param {Source<import('ipfs-unixfs-importer').ImportCandidate>} source
*/
source => importer(source, repo.blocks, {
...opts,
hasher,
pin: false
}),
transformFile(opts),
preloadFile(preload, opts),
pinFile(repo, opts)
)
const releaseLock = await repo.gcLock.readLock()
try {
for await (const added of iterator) {
const path = added.path ?? added.cid.toString()
// do not keep file totals around forever
delete totals[path]
yield {
...added,
path
}
}
} finally {
releaseLock()
}
}
return withTimeoutOption(addAll)
}
/**
* @param {import('ipfs-core-types/src/root').AddAllOptions} opts
*/
function transformFile (opts) {
/**
* @param {Source<ImportResult>} source
*/
async function * transformFile (source) {
for await (const file of source) {
let cid = file.cid
if (opts.cidVersion === 1) {
cid = cid.toV1()
}
let path = file.path ? file.path : cid.toString()
if (opts.wrapWithDirectory && !file.path) {
path = ''
}
yield {
path,
cid: cid,
size: file.size,
mode: file.unixfs && file.unixfs.mode,
mtime: file.unixfs && file.unixfs.mtime
}
}
}
return transformFile
}
/**
* @param {(cid: CID) => void} preload
* @param {import('ipfs-core-types/src/root').AddAllOptions} opts
*/
function preloadFile (preload, opts) {
/**
* @param {Source<ImportResult>} source
*/
async function * maybePreloadFile (source) {
for await (const file of source) {
const isRootFile = !file.path || opts.wrapWithDirectory
? file.path === ''
: !file.path.includes('/')
const shouldPreload = isRootFile && !opts.onlyHash && opts.preload !== false
if (shouldPreload) {
preload(file.cid)
}
yield file
}
}
return maybePreloadFile
}
/**
* @param {import('ipfs-repo').IPFSRepo} repo
* @param {import('ipfs-core-types/src/root').AddAllOptions} opts
*/
function pinFile (repo, opts) {
/**
* @param {Source<ImportResult>} source
*/
async function * maybePinFile (source) {
for await (const file of source) {
// Pin a file if it is the root dir of a recursive add or the single file
// of a direct add.
const isRootDir = !(file.path && file.path.includes('/'))
const shouldPin = (opts.pin == null ? true : opts.pin) && isRootDir && !opts.onlyHash
if (shouldPin) {
await repo.pins.pinRecursively(file.cid)
}
yield file
}
}
return maybePinFile
}