Skip to content
This repository was archived by the owner on Feb 12, 2024. It is now read-only.

Commit 652979c

Browse files
committed
fix: progress bar flakiness (#1042)
* fix: fix progress bar flakiness * feat: reuse createAddPullStream to avoid code duplication
1 parent 13b4d61 commit 652979c

File tree

3 files changed

+12
-18
lines changed

3 files changed

+12
-18
lines changed

src/cli/commands/files/add.js

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,6 @@ module.exports = {
203203
const ipfs = argv.ipfs
204204

205205
let list = []
206-
let currentBytes = 0
207-
208206
waterfall([
209207
(next) => glob(path.join(inPath, '/**/*'), next),
210208
(globResult, next) => {
@@ -216,8 +214,7 @@ module.exports = {
216214
if (argv.progress) {
217215
const bar = createProgressBar(totalBytes)
218216
options.progress = function (byteLength) {
219-
currentBytes += byteLength
220-
bar.tick(byteLength, {progress: byteman(currentBytes, 2, 'MB')})
217+
bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')})
221218
}
222219
}
223220

src/core/components/files.js

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ module.exports = function files (self) {
2020
shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity
2121
}, options)
2222

23+
let total = 0
24+
let prog = opts.progress || (() => {})
25+
const progress = (bytes) => {
26+
total += bytes
27+
prog(total)
28+
}
29+
30+
opts.progress = progress
2331
return pull(
2432
pull.map(normalizeContent),
2533
pull.flatten(),
@@ -65,18 +73,9 @@ module.exports = function files (self) {
6573
return callback(new Error('Invalid arguments, data must be an object, Buffer or readable stream'))
6674
}
6775

68-
let total = 0
69-
let prog = options.progress || (() => {})
70-
const progress = (bytes) => {
71-
total += bytes
72-
prog(total)
73-
}
74-
75-
options.progress = progress
7676
pull(
77-
pull.values(normalizeContent(data)),
78-
importer(self._ipldResolver, options),
79-
pull.asyncMap(prepareFile.bind(null, self, options)),
77+
pull.values([data]),
78+
createAddPullStream(options),
8079
sort((a, b) => {
8180
if (a.path < b.path) return 1
8281
if (a.path > b.path) return -1

src/http/api/resources/files.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,10 +209,8 @@ exports.add = {
209209
})
210210

211211
const replyStream = pushable()
212-
let total = 0
213212
const progressHandler = (bytes) => {
214-
total += bytes
215-
replyStream.push({ Bytes: total })
213+
replyStream.push({ Bytes: bytes })
216214
}
217215

218216
const options = {

0 commit comments

Comments
 (0)