Skip to content
This repository has been archived by the owner on Aug 12, 2020. It is now read-only.

Commit

Permalink
feat: adds call to progress bar function (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
bmordan authored and daviddias committed Sep 5, 2017
1 parent aa053d8 commit ac6f722
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -91,4 +92,4 @@
"jbenet <juan@benet.ai>",
"nginnever <ginneversource@gmail.com>"
]
}
}
7 changes: 6 additions & 1 deletion src/builder/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
18 changes: 18 additions & 0 deletions test/test-importer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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()
})
)
})
})
})
}

0 comments on commit ac6f722

Please sign in to comment.