Socket
Socket
Sign inDemoInstall

progress-stream

Package Overview
Dependencies
9
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.1 to 0.3.0

34

index.js
var through = require('through2');
var speedometer = require('speedometer');

@@ -10,10 +11,13 @@ module.exports = function(options, onprogress) {

var drain = options.drain || false;
var read = 0;
var transferred = 0;
var nextUpdate = Date.now()+time;
var delta = 0;
var speed = speedometer(options.speed || 5000);
var update = {
percentage: 0,
read: read,
length: length
transferred: transferred,
length: length,
remaining: length,
eta: 0
};

@@ -23,6 +27,7 @@

update.delta = delta;
update.read = read;
update.length = length;
update.percentage = ended ? 100 : (length ? read/length*100 : 0);
update.percentage = ended ? 100 : (length ? transferred/length*100 : 0);
update.speed = speed(delta);
update.eta = Math.round(update.remaining / update.speed);
nextUpdate += time;
delta = 0;

@@ -33,4 +38,6 @@

var write = function(chunk, enc, callback) {
read += chunk.length;
transferred += chunk.length;
delta += chunk.length;
update.transferred = transferred;
update.remaining = length - transferred;

@@ -48,2 +55,4 @@ if (Date.now() >= nextUpdate) emit(false);

length = newLength;
update.length = length;
update.remaining = length - update.transferred;
tr.emit('length', length);

@@ -57,2 +66,7 @@ };

// Support streams with a length property
if (typeof stream.length === 'number') {
return onlength(stream.length);
}
// Support request module

@@ -69,3 +83,9 @@ stream.on('response', function(res) {

tr.progress = function() {
update.speed = speed(0);
update.eta = Math.round(update.remaining / update.speed);
return update;
};
return tr;
};
{
"name": "progress-stream",
"version": "0.2.1",
"version": "0.3.0",
"description": "Read the progress of a stream",

@@ -23,7 +23,7 @@ "repository": {

"dependencies": {
"through2": "~0.2.3"
"through2": "~0.2.3",
"speedometer": "~0.1.2"
},
"devDependencies": {
"request": "~2.29.0",
"speedometer": "~0.1.2",
"single-line-log": "~0.1.2",

@@ -30,0 +30,0 @@ "numeral": "~1.5.2"

# progress-stream
Read the progress of a stream. You can either instantiate it with a specific length, or it will read the length automatically if you're using the request module or http module.
Read the progress of a stream. Supports speed and eta.
Gets the lengths of the stream automatically if you're using the request or http module. You can also pass the length on initiation. Progress-stream will also check to see if the stream already have a length property.
npm install progress-stream

@@ -9,3 +11,3 @@

This example copies a large file, and prints out the percentage every 100ms.
This example copies a large file, and prints out the percentage, speed and remaining every 100ms.

@@ -17,3 +19,3 @@ ```js

var stat = fs.statSync(filename);
var p = progress({
var str = progress({
length: stat.size,

@@ -23,10 +25,9 @@ time: 100

p.on('progress', function(progress) {
console.log(Math.round(progress.percentage)+'%');
str.on('progress', function(progress) {
console.log(Math.round(progress.percentage) + '% ' + progress.speed + 'b/s' + ' ' + progress.remaining + ' bytes left');
});
fs.createReadStream(filename)
.pipe(p)
.pipe(str)
.pipe(fs.createWriteStream(output));
```

@@ -36,3 +37,3 @@

# progress([options], [onprogress])
### progress([options], [onprogress])

@@ -42,18 +43,31 @@ You can instantiate in two ways:

``` js
var p = progress({time:100});
p.on('progress', function(progress) { ... });
var str = progress({time:100});
str.on('progress', function(progress) { ... });
```
or inline the onprogress event
or inline the progress listener
``` js
var p = progress({time:100}, function(progress) { ... });
var str = progress({time:100}, function(progress) { ... });
```
## Properties
### .progress
You can get the progress from the progress property.
``` js
var str = progress({time:100});
console.log(str.progress);
```
## Events
# on('progress', onprogress)
### on('progress', function(progress) { ... })
``` js
var p = progress({time:100});
p.on('progress', function(progress) { ... });
var str = progress({time:100});
str.on('progress', function(progress) { ... });
```

@@ -67,2 +81,6 @@

### speed(integer)
Sets how long the speedometer needs to calculate the speed. Defaults to 5 sec.
### length(integer)

@@ -89,7 +107,7 @@

var p = progress({
var str = progress({
time: 1000
});
p.on('progress', function(progress) {
str.on('progress', function(progress) {
console.log(Math.round(progress.percentage)+'%');

@@ -99,3 +117,3 @@ });

req('http://cachefly.cachefly.net/100mb.test', { headers: { 'user-agent': 'test' }})
.pipe(p)
.pipe(str)
.pipe(fs.createWriteStream('test.data'));

@@ -102,0 +120,0 @@ ```

@@ -5,10 +5,15 @@ var progress = require('../index');

var log = require('single-line-log');
var speedometer = require('speedometer');
var numeral = require('numeral');
var speed = speedometer();
var p = progress({drain:true, time:100});
p.on('progress', function(progress) {
log(Math.round(progress.percentage)+'%', numeral(speed(progress.delta)).format('0.00 b')+'/s');
var str = progress({
drain: true,
time: 100,
speed: 20
});
str.on('progress', function(progress) {
log(Math.round(progress.percentage)+'%',
numeral(progress.speed).format('0.00 b')+'/s',
numeral(progress.eta).format('00:00:00')+' left',
numeral(progress.remaining).format('0.00 b')+' remaining');
});

@@ -24,5 +29,5 @@ var options = {

http.request(options, function(response) {
response.pipe(p);
response.pipe(str);
}).end();
console.log('progress-stream using http module - downloading 100 MB file');
console.log('progress-stream using http module - downloading 10 MB file');

@@ -5,15 +5,18 @@ var progress = require('../index');

var log = require('single-line-log');
var speedometer = require('speedometer');
var numeral = require('numeral');
var speed = speedometer();
var p = progress({drain:true, time:100});
req('http://cachefly.cachefly.net/100mb.test', {
var str = progress({
drain: true,
time: 100
}, function(progress) {
log(Math.round(progress.percentage)+'%',
numeral(progress.speed).format('0.00 b')+'/s',
numeral(progress.eta).format('00:00:00')+' left',
numeral(progress.remaining).format('0.00 b')+' remaining');
});
req('http://cachefly.cachefly.net/10mb.test', {
headers: { 'user-agent': 'test' }
}).pipe(p);
}).pipe(str);
p.on('progress', function(progress) {
log(Math.round(progress.percentage)+'%', numeral(speed(progress.delta)).format('0.00 b')+'/s');
});
console.log('progress-stream using request module - downloading 100 MB file');
console.log('progress-stream using request module - downloading 10 MB file');
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc