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

Commit ac6f722

Browse files
bmordandaviddias
authored andcommitted
feat: adds call to progress bar function (#179)
1 parent aa053d8 commit ac6f722

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ The input's file paths and directory structure will be preserved in the [`dag-pb
138138
- `dirBuilder` (object): the options for the directory builder
139139
- `hamt` (object): the options for the HAMT sharded directory builder
140140
- bits (positive integer, defaults to `8`): the number of bits at each bucket of the HAMT
141+
- `progress` (function): a function that will be called with the byte length of chunks as a file is added to ipfs.
141142

142143
### Exporter
143144

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
"pull-generate": "^2.2.0",
5252
"pull-zip": "^2.0.1",
5353
"rimraf": "^2.6.1",
54+
"sinon": "^3.2.1",
5455
"split": "^1.0.1"
5556
},
5657
"dependencies": {
@@ -91,4 +92,4 @@
9192
"jbenet <[email protected]>",
9293
"nginnever <[email protected]>"
9394
]
94-
}
95+
}

src/builder/builder.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,12 @@ module.exports = function (createChunker, ipldResolver, createReducer, _options)
9393
pull(
9494
file.content,
9595
createChunker(options.chunkerOptions),
96-
pull.map(chunk => new Buffer(chunk)),
96+
pull.map(chunk => {
97+
if (options.progress && typeof options.progress === 'function') {
98+
options.progress(chunk.byteLength)
99+
}
100+
return new Buffer(chunk)
101+
}),
97102
pull.map(buffer => new UnixFS('file', buffer)),
98103
pull.asyncMap((fileNode, callback) => {
99104
DAGNode.create(fileNode.marshal(), [], options.hashAlg, (err, node) => {

test/test-importer.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const extend = require('deep-extend')
77
const chai = require('chai')
88
chai.use(require('dirty-chai'))
99
const expect = chai.expect
10+
const sinon = require('sinon')
1011
const BlockService = require('ipfs-block-service')
1112
const pull = require('pull-stream')
1213
const mh = require('multihashes')
@@ -417,6 +418,23 @@ module.exports = (repo) => {
417418
}
418419
}
419420
})
421+
422+
it('will call an optional progress function', (done) => {
423+
options.progress = sinon.spy()
424+
425+
pull(
426+
pull.values([{
427+
path: '1.2MiB.txt',
428+
content: pull.values([bigFile])
429+
}]),
430+
importer(ipldResolver, options),
431+
pull.collect(() => {
432+
expect(options.progress.called).to.equal(true)
433+
expect(options.progress.args[0][0]).to.equal(1024)
434+
done()
435+
})
436+
)
437+
})
420438
})
421439
})
422440
}

0 commit comments

Comments
 (0)