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

Commit 2b3a577

Browse files
bmordandryajov
authored andcommitted
Adds wiring for a progress bar
Passes the progress bar option for tranfers over http. Progress is shown for your payload being streamed upto the server. There is a pause. Then you get your list of files.
1 parent 12802eb commit 2b3a577

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

src/files/add.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ module.exports = (send) => {
4141
qs.hash = opts.hashAlg
4242
}
4343

44-
const request = { path: 'add', files: files, qs: qs }
44+
const request = { path: 'add', files: files, qs: opts, progress: opts.progress }
4545

4646
// Transform the response stream to DAGNode values
4747
const transform = (res, callback) => DAGNodeStream.streamToValue(send, res, callback)

src/utils/request-api.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const getFilesStream = require('./get-files-stream')
1010
const streamToValue = require('./stream-to-value')
1111
const streamToJsonValue = require('./stream-to-json-value')
1212
const request = require('./request')
13+
const Transform = require('readable-stream').Transform
1314

1415
// -- Internal
1516

@@ -160,7 +161,17 @@ function requestAPI (config, options, callback) {
160161
})
161162

162163
if (options.files) {
163-
stream.pipe(req)
164+
if (options.progress && typeof options.progress === 'function') {
165+
const progressStream = new Transform({
166+
transform: (chunk, encoding, cb) => {
167+
options.progress(chunk.byteLength)
168+
cb(null, chunk)
169+
}
170+
})
171+
stream.pipe(progressStream).pipe(req)
172+
} else {
173+
stream.pipe(req)
174+
}
164175
} else {
165176
req.end()
166177
}

test/files.spec.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ describe('.files (the MFS API part)', function () {
8787
})
8888
})
8989

90+
it.only('files.add with progress options', (done) => {
91+
ipfs.files.add(testfile, {progress: false}, (err, res) => {
92+
expect(err).to.not.exist()
93+
94+
expect(res).to.have.length(1)
95+
done()
96+
})
97+
})
98+
9099
HASH_ALGS.forEach((name) => {
91100
it(`files.add with hash=${name} and raw-leaves=false`, (done) => {
92101
const content = String(Math.random() + Date.now())

0 commit comments

Comments
 (0)