From 4daa0ad43f37acd84904ac22d12bba7a62c79a6b Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Thu, 19 Oct 2017 14:26:26 -0700 Subject: [PATCH 1/2] fix: fix progress bar flakiness --- src/cli/commands/files/add.js | 5 +---- src/core/components/files.js | 8 ++++++++ src/http/api/resources/files.js | 4 +--- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/src/cli/commands/files/add.js b/src/cli/commands/files/add.js index 805f80217f..e9946f5098 100644 --- a/src/cli/commands/files/add.js +++ b/src/cli/commands/files/add.js @@ -203,8 +203,6 @@ module.exports = { const ipfs = argv.ipfs let list = [] - let currentBytes = 0 - waterfall([ (next) => glob(path.join(inPath, '/**/*'), next), (globResult, next) => { @@ -216,8 +214,7 @@ module.exports = { if (argv.progress) { const bar = createProgressBar(totalBytes) options.progress = function (byteLength) { - currentBytes += byteLength - bar.tick(byteLength, {progress: byteman(currentBytes, 2, 'MB')}) + bar.update(byteLength / totalBytes, {progress: byteman(byteLength, 2, 'MB')}) } } diff --git a/src/core/components/files.js b/src/core/components/files.js index fa82b28080..e853b80ef6 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -20,6 +20,14 @@ module.exports = function files (self) { shardSplitThreshold: self._options.EXPERIMENTAL.sharding ? 1000 : Infinity }, options) + let total = 0 + let prog = opts.progress || (() => {}) + const progress = (bytes) => { + total += bytes + prog(total) + } + + opts.progress = progress return pull( pull.map(normalizeContent), pull.flatten(), diff --git a/src/http/api/resources/files.js b/src/http/api/resources/files.js index 64466b1622..1afbe2c6ef 100644 --- a/src/http/api/resources/files.js +++ b/src/http/api/resources/files.js @@ -209,10 +209,8 @@ exports.add = { }) const replyStream = pushable() - let total = 0 const progressHandler = (bytes) => { - total += bytes - replyStream.push({ Bytes: total }) + replyStream.push({ Bytes: bytes }) } const options = { From 3038452c6d34b8856b32ea602d6e3f5fc6daf2cd Mon Sep 17 00:00:00 2001 From: Dmitriy Ryajov Date: Thu, 19 Oct 2017 18:07:04 -0700 Subject: [PATCH 2/2] feat: reuse createAddPullStream to avoid code duplication --- src/core/components/files.js | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/src/core/components/files.js b/src/core/components/files.js index e853b80ef6..e2f6443350 100644 --- a/src/core/components/files.js +++ b/src/core/components/files.js @@ -73,18 +73,9 @@ module.exports = function files (self) { return callback(new Error('Invalid arguments, data must be an object, Buffer or readable stream')) } - let total = 0 - let prog = options.progress || (() => {}) - const progress = (bytes) => { - total += bytes - prog(total) - } - - options.progress = progress pull( - pull.values(normalizeContent(data)), - importer(self._ipldResolver, options), - pull.asyncMap(prepareFile.bind(null, self, options)), + pull.values([data]), + createAddPullStream(options), sort((a, b) => { if (a.path < b.path) return 1 if (a.path > b.path) return -1