This repository was archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 297
Ipfs file upload progress #604
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
2b3a577
Adds wiring for a progress bar
bmordan c749c36
feat: Support specify hash algorithm in files.add (#597)
alanshaw 10b0bde
feat: track progress events
dryajov 8885ddb
tesT: add more tests
daviddias 2ff8312
fix: tidy up tests
dryajov f6c0e21
feat: send errors as trailer headers
dryajov 843e699
fix: remove .only from tests
dryajov 1845f2b
feat: signal error on reponse stream if x-stream-error
dryajov 1891bbf
test: adding tests for trailer header errors
dryajov ecbf3e6
chore: update interface-ipfs-core
daviddias dca5925
Merge branch 'master' into ipfs-api-progress
daviddias 9a11a83
chore: upgrade to latest interface-ipfs-core
dryajov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
'use strict' | ||
|
||
const Transform = require('readable-stream').Transform | ||
|
||
/* | ||
A transform stream to track progress events on file upload | ||
|
||
When the progress flag is passed to the HTTP api, the stream | ||
emits progress events like such: | ||
|
||
{ | ||
Name string | ||
Hash string `json:",omitempty"` | ||
Bytes int64 `json:",omitempty"` | ||
Size string `json:",omitempty"` | ||
} | ||
|
||
This class will take care of detecting such | ||
events and calling the associated track method | ||
with the bytes sent so far as parameter. It will | ||
also skip them from the stream, emitting only | ||
when the final object has been uploaded and we | ||
got a hash. | ||
*/ | ||
class ProgressStream extends Transform { | ||
constructor (opts) { | ||
opts = Object.assign(opts || {}, { objectMode: true }) | ||
super(opts) | ||
this._track = opts.track || (() => {}) | ||
} | ||
|
||
static fromStream (track, stream) { | ||
const prog = new ProgressStream({ track }) | ||
return stream.pipe(prog) | ||
} | ||
|
||
_transform (chunk, encoding, callback) { | ||
if (chunk && | ||
typeof chunk.Bytes !== 'undefined' && | ||
typeof chunk.Hash === 'undefined') { | ||
this._track(chunk.Bytes) | ||
return callback() | ||
} | ||
|
||
callback(null, chunk) | ||
} | ||
} | ||
|
||
module.exports = ProgressStream |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,10 +7,14 @@ const expect = chai.expect | |
chai.use(dirtyChai) | ||
const isNode = require('detect-node') | ||
const ipfsAPI = require('../src/index.js') | ||
const ndjson = require('ndjson') | ||
const pump = require('pump') | ||
|
||
describe('\'deal with HTTP weirdness\' tests', () => { | ||
it('does not crash if no content-type header is provided', (done) => { | ||
if (!isNode) { return done() } | ||
if (!isNode) { | ||
return done() | ||
} | ||
|
||
// go-ipfs always (currently) adds a content-type header, even if no content is present, | ||
// the standard behaviour for an http-api is to omit this header if no content is present | ||
|
@@ -27,3 +31,34 @@ describe('\'deal with HTTP weirdness\' tests', () => { | |
}) | ||
}) | ||
}) | ||
|
||
describe('trailer headers', () => { | ||
it('should deal with trailer x-stream-error correctly', (done) => { | ||
if (!isNode) { | ||
return done() | ||
} | ||
|
||
const server = require('http').createServer((req, res) => { | ||
const resStream = pump(res, ndjson.stringify()) | ||
res.setHeader('x-chunked-output', '1') | ||
res.setHeader('content-type', 'application/json') | ||
res.setHeader('Trailer', 'X-Stream-Error') | ||
res.addTrailers({ 'X-Stream-Error': JSON.stringify({ Message: 'ups, something went wrong', Code: 500 }) }) | ||
resStream.write({ Bytes: 1 }) | ||
res.end() | ||
}) | ||
|
||
server.listen(6001, () => { | ||
const ipfs = ipfsAPI('/ip4/127.0.0.1/tcp/6001') | ||
/* eslint-disable */ | ||
ipfs.files.add(Buffer.from('Hello there!'), (err, res) => { | ||
// TODO: error's are not being correctly | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dryajov track this in an issue. |
||
// propagated with Trailer headers yet | ||
// expect(err).to.exist() | ||
expect(res).to.not.equal(0) | ||
server.close(done) | ||
}) | ||
/* eslint-enable */ | ||
}) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's an example in the examples directory. Just repointed it to use the current ipfs-api rather than a published version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, now it makes sense 👍