Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
progress-stream
Advanced tools
Readme
Read the progress of a stream. Supports speed and eta.
Gets the length 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 has a length property.
npm install progress-stream
This example copies a large file, and prints out the percentage, speed and remaining every 100ms.
var progress = require('progress-stream');
var fs = require('fs');
var stat = fs.statSync(filename);
var str = progress({
length: stat.size,
time: 100 /* ms */
});
str.on('progress', function(progress) {
console.log(progress);
/*
{
percentage: 9.05,
transferred: 949624,
length: 10485760,
remaining: 9536136,
eta: 42,
runtime: 3,
delta: 295396,
speed: 949624
}
*/
});
fs.createReadStream(filename)
.pipe(str)
.pipe(fs.createWriteStream(output));
You can instantiate in two ways:
var str = progress({time:100});
str.on('progress', function(progress) { ... });
or inline the progress listener
var str = progress({time:100}, function(progress) { ... });
You can get the progress from the progress function.
var str = progress({time:100});
console.log(str.progress());
/*
{
percentage: 9.05,
transferred: 949624,
length: 10485760,
remaining: 9536136,
eta: 10,
runtime: 0,
delta: 295396,
speed: 949624
}
*/
var str = progress({time:100});
str.on('progress', function(progress) { ... });
Sets how often progress events are emitted in ms. If omitted then the default is to do so every time a chunk is received.
Sets how long the speedometer needs to calculate the speed. Defaults to 5 sec.
If you already know the length of the stream, then you can set it. Defaults to 0.
In case you don't want to include a readstream after progress-stream, set to true to drain automatically. Defaults to false.
If you want to set the size of previously downloaded data. Useful for a resumed download.
This example uses request to download a 100 MB file, and writes out the percentage every second.
You can also find an example in test/request.js
.
var progress = require('progress-stream');
var req = require('request');
var fs = require('fs');
var str = progress({
time: 1000
});
str.on('progress', function(progress) {
console.log(Math.round(progress.percentage)+'%');
});
req('http://cachefly.cachefly.net/100mb.test', { headers: { 'user-agent': 'test' }})
.pipe(str)
.pipe(fs.createWriteStream('test.data'));
In test/http.js
it's shown how to do it with the http module.
setLength(newLength)
Sometimes, you don't know how big a stream is right away (e.g. multipart file uploads). You might find out after a few chunks have already passed through the stream, seconds or even minutes later. In this case, you can use the setLength
method to recalculate the relevant tracked progress data.
var str = progress({});
someFickleStreamInstance.pipe(str).pipe(fs.createWriteStream('test.data'));
someFickleStreamInstance.on('conviction', function nowIKnowMyLength (actualLength) {
str.setLength(actualLength);
});
FAQs
Read the progress of a stream
The npm package progress-stream receives a total of 376,007 weekly downloads. As such, progress-stream popularity was classified as popular.
We found that progress-stream demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.