npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

progress-stream

v2.0.0

Published

Read the progress of a stream

Downloads

2,044,857

Readme

progress-stream

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

Usage

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));

Methods

progress([options], [onprogress])

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) { ... });

Properties

.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
}
*/

Events

on('progress', function(progress) { ... })

var str = progress({time:100});
str.on('progress', function(progress) { ... });

Options

time(integer)

Sets how often progress events are emitted in ms. If omitted then the default is to do so every time a chunk is received.

speed(integer)

Sets how long the speedometer needs to calculate the speed. Defaults to 5 sec.

length(integer)

If you already know the length of the stream, then you can set it. Defaults to 0.

drain(boolean)

In case you don't want to include a readstream after progress-stream, set to true to drain automatically. Defaults to false.

transferred(integer)

If you want to set the size of previously downloaded data. Useful for a resumed download.

Examples

Using the request module

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'));

Using the http module

In test/http.js it's shown how to do it with the http module.

Methods

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);
});