diff --git a/README.md b/README.md index 79d553bf..76011e8a 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/package.json b/package.json index 843791d2..433303dd 100644 --- a/package.json +++ b/package.json @@ -51,6 +51,7 @@ "pull-generate": "^2.2.0", "pull-zip": "^2.0.1", "rimraf": "^2.6.1", + "sinon": "^3.2.1", "split": "^1.0.1" }, "dependencies": { @@ -91,4 +92,4 @@ "jbenet ", "nginnever " ] -} \ No newline at end of file +} diff --git a/src/builder/builder.js b/src/builder/builder.js index c9647bce..1a5852f8 100644 --- a/src/builder/builder.js +++ b/src/builder/builder.js @@ -93,7 +93,12 @@ 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 && 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) => { diff --git a/test/test-importer.js b/test/test-importer.js index 4fedead2..3e40cc64 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,23 @@ module.exports = (repo) => { } } }) + + it('will call an optional 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.equal(true) + expect(options.progress.args[0][0]).to.equal(1024) + done() + }) + ) + }) }) }) }