This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 299
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
66 additions
and
19 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,50 @@ | ||
'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 || (() => {}) | ||
this._res = [] | ||
} | ||
|
||
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 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 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