forked from swiftcarrot/stream-progressbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
30 lines (25 loc) · 742 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
var PassThrough = require('stream').PassThrough;
var ProgressBar = require('progress');
module.exports = function(tokens, options) {
options = options || {};
var pt = new PassThrough(options);
var total = options.total;
pt.on('pipe', function(stream) {
if (total) {
var bar = new ProgressBar(tokens, options);
pt.on('data', function(chunk) {
bar.tick(chunk.length);
});
} else {
stream.on('response', function(res) {
var total = parseInt(res.headers['content-length'], 10);
options.total = total;
var bar = new ProgressBar(tokens, options);
pt.on('data', function(chunk) {
bar.tick(chunk.length);
});
});
}
});
return pt;
};