• Stars
    star
    234
  • Rank 171,630 (Top 4 %)
  • Language
    JavaScript
  • License
    BSD 2-Clause "Sim...
  • Created almost 11 years ago
  • Updated over 7 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Read the progress of a stream

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

More Repositories

1

single-line-log

Output one line and move to beginning of line. Useful for progress bars and counters with no breaks in the terminal
JavaScript
209
star
2

currency-codes

Node.js module to work with currency codes based on ISO 4217
JavaScript
152
star
3

create-repository

Automatically creates and sets up a new github repository
JavaScript
75
star
4

redis-eventemitter

Redis pubsub using an event emitter for node.js
JavaScript
30
star
5

mtgtop8

Get events and decks data from mtgtop8.com
JavaScript
29
star
6

dombo

A very thin layer on top of the DOM to make working with the DOM much easier. Also include some extra jquery-like features to work with events and classes.
JavaScript
26
star
7

mtgjson

Node.js module that fetches AllSets.json from http://mtgjson.com and caches it so you are always sure to have the newest version.
JavaScript
15
star
8

github-with-auth

Get started using the github in no time! Instantiates github module by using ghauth.
JavaScript
14
star
9

dependency-hunter

Find node.js dependencies in github repositories
JavaScript
10
star
10

docx-to-pdf

Convert docx files to pdf using http://convertonlinefree.com
JavaScript
9
star
11

tvcom

A simple module for getting data from tv.com
JavaScript
9
star
12

mtgjson-render

Generates images from Magic: The Gathering card data based on the https://github.com/freeall/mtgjson format
JavaScript
9
star
13

trend

Find out how a chart (array of values) are trending. Compares the last X points to the previous Y points before them.
JavaScript
8
star
14

findcoronacenter.dk

Generate map to find corona test centers in Denmark
JavaScript
6
star
15

funcle

"We have the output, you have the input" - the algorithmic jeopardy game
JavaScript
5
star
16

mtg-printer

Nice shell interface for generating images from Magic: The Gathering card data based on the https://github.com/freeall/mtgjson format.
JavaScript
5
star
17

http-monitor

Check if a server is running. Both a module and an executable.
JavaScript
5
star
18

one-year-of-open-source

Celebrate that @mafintosh has a one year streak on Github
HTML
3
star
19

slack-team-watcher

Have all of your teams public chats listed in the CLI
JavaScript
2
star
20

td-combiner

JavaScript
2
star
21

sysinfo-flashy

A flashy sysinfo. Used for when terminal starts, just to be flashy ☯️
JavaScript
2
star
22

think

A small puzzle game
JavaScript
2
star
23

load-environment

Simply load your environment from name-of-your-environment.json. Takes the name from process.env.NODE_ENV.
JavaScript
2
star
24

mtg-aether-revolt

HTML
1
star
25

tower-defense

JavaScript
1
star
26

outdated

A node module that checks if the running process is outdated based on its package.json
JavaScript
1
star
27

flight-mode

Have you ever wanted to point to your screen and tell the flight attendant that "Nono, it really is in flight mode"?
JavaScript
1
star
28

freeall

1
star
29

timer

A timer with a basic web interface
1
star
30

mtg-card-detector

Python
1
star
31

playlister

1
star