From fd5e64e36e734d1caa035f02d2955c482bea9c73 Mon Sep 17 00:00:00 2001 From: Bernard Mordan Date: Fri, 1 Sep 2017 15:13:28 +0100 Subject: [PATCH 1/5] Adds call to progress bar function This change adds a call to an optional function that can be passed to `Importer`. This will be called with the byteLength of a file's buffered chunk. --- src/builder/builder.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/builder/builder.js b/src/builder/builder.js index 4569210a..1f74a105 100644 --- a/src/builder/builder.js +++ b/src/builder/builder.js @@ -93,7 +93,10 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options) pull( file.content, createChunker(options.chunkerOptions), - pull.map(chunk => new Buffer(chunk)), + pull.map(chunk => { + if (options.progress) options.progress(chunk.byteLength) + return new Buffer(chunk) + }), pull.map(buffer => new UnixFS('file', buffer)), pull.asyncMap((fileNode, callback) => { DAGNode.create(fileNode.marshal(), (err, node) => { From d12bdf5b0416d5b137fd1b6d2388189f36ec656e Mon Sep 17 00:00:00 2001 From: Bernard Mordan Date: Fri, 1 Sep 2017 16:02:58 +0100 Subject: [PATCH 2/5] Checks progress is a function This commit also updates the readme.md file with a discription for how to use this option. --- README.md | 1 + src/builder/builder.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 23992000..add7b5f0 100644 --- a/README.md +++ b/README.md @@ -138,6 +138,7 @@ The input's file paths and directory structure will be preserved in the [`dag-pb - `dirBuilder` (object): the options for the directory builder - `hamt` (object): the options for the HAMT sharded directory builder - bits (positive integer, defaults to `8`): the number of bits at each bucket of the HAMT +- `progress` (function): a function that will be called with the byte length of chunks as a file is added to ipfs. ### Exporter diff --git a/src/builder/builder.js b/src/builder/builder.js index 1f74a105..c88dd2e1 100644 --- a/src/builder/builder.js +++ b/src/builder/builder.js @@ -94,7 +94,9 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options) file.content, createChunker(options.chunkerOptions), pull.map(chunk => { - if (options.progress) options.progress(chunk.byteLength) + if (options.progress && typeof options.progress === 'function') { + options.progress(chunk.byteLength) + } return new Buffer(chunk) }), pull.map(buffer => new UnixFS('file', buffer)), From b24de1775aa300f014eebaf3bcf605c0ff84f717 Mon Sep 17 00:00:00 2001 From: Bernard Mordan Date: Mon, 4 Sep 2017 15:57:46 +0100 Subject: [PATCH 3/5] adds test for optional progress function --- package.json | 3 ++- test/test-importer.js | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 461e6867..dfbc443c 100644 --- a/package.json +++ b/package.json @@ -50,6 +50,7 @@ "pull-generate": "^2.2.0", "pull-zip": "^2.0.1", "rimraf": "^2.6.1", + "sinon": "^3.2.1", "split": "^1.0.0" }, "dependencies": { @@ -90,4 +91,4 @@ "jbenet ", "nginnever " ] -} \ No newline at end of file +} diff --git a/test/test-importer.js b/test/test-importer.js index 4fedead2..1106dfc4 100644 --- a/test/test-importer.js +++ b/test/test-importer.js @@ -7,6 +7,7 @@ const extend = require('deep-extend') const chai = require('chai') chai.use(require('dirty-chai')) const expect = chai.expect +const sinon = require('sinon') const BlockService = require('ipfs-block-service') const pull = require('pull-stream') const mh = require('multihashes') @@ -417,6 +418,37 @@ module.exports = (repo) => { } } }) + + it('will call a progress function', (done) => { + options.progress = sinon.spy() + pull( + pull.values([{ + path: '1.2MiB.txt', + content: pull.values([bigFile]) + }]), + importer(ipldResolver, options), + pull.collect(() => { + expect(options.progress.called).to.be.true + expect(options.progress.args[0][0]).to.equal(1024) + done() + }) + ) + }) + + it('only if it is passed', (done) => { + options.progress = false + pull( + pull.values([{ + path: '200Bytes.txt', + content: pull.values([smallFile]) + }]), + importer(ipldResolver, options), + pull.collect(() => { + expect(options.progress.notCalled).to.be.true + done() + }) + ) + }) }) }) } From ca6aa930fdf46f9653da7aa4410df562cb82bb79 Mon Sep 17 00:00:00 2001 From: Bernard Mordan Date: Mon, 4 Sep 2017 16:49:12 +0100 Subject: [PATCH 4/5] Linting workaround in some of my tests I was writing ``` expect(someTrueValue).to.be.true ``` but this is an `unused-expressions` in the eyes of eslint. So the work around is to just `expect(someTrueValue).to.equal(true)`. --- test/test-importer.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test-importer.js b/test/test-importer.js index 1106dfc4..e70235d0 100644 --- a/test/test-importer.js +++ b/test/test-importer.js @@ -428,7 +428,7 @@ module.exports = (repo) => { }]), importer(ipldResolver, options), pull.collect(() => { - expect(options.progress.called).to.be.true + expect(options.progress.called).to.equal(true) expect(options.progress.args[0][0]).to.equal(1024) done() }) @@ -444,7 +444,7 @@ module.exports = (repo) => { }]), importer(ipldResolver, options), pull.collect(() => { - expect(options.progress.notCalled).to.be.true + expect(options.progress.notCalled).to.equal(true) done() }) ) From 22a700e6d848ca30fdc8c55ca8d90570af27dddd Mon Sep 17 00:00:00 2001 From: Bernard Mordan Date: Mon, 4 Sep 2017 17:47:19 +0100 Subject: [PATCH 5/5] Fixes failing test --- test/test-importer.js | 18 ++---------------- 1 file changed, 2 insertions(+), 16 deletions(-) diff --git a/test/test-importer.js b/test/test-importer.js index e70235d0..3e40cc64 100644 --- a/test/test-importer.js +++ b/test/test-importer.js @@ -419,8 +419,9 @@ module.exports = (repo) => { } }) - it('will call a progress function', (done) => { + it('will call an optional progress function', (done) => { options.progress = sinon.spy() + pull( pull.values([{ path: '1.2MiB.txt', @@ -434,21 +435,6 @@ module.exports = (repo) => { }) ) }) - - it('only if it is passed', (done) => { - options.progress = false - pull( - pull.values([{ - path: '200Bytes.txt', - content: pull.values([smallFile]) - }]), - importer(ipldResolver, options), - pull.collect(() => { - expect(options.progress.notCalled).to.equal(true) - done() - }) - ) - }) }) }) }