Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
progress-stream
Advanced tools
The `progress-stream` npm package is used to monitor the progress of data being streamed. It is particularly useful for tracking the progress of file uploads, downloads, or any other data transfer operations. The package provides a simple interface to get updates on the amount of data transferred, the speed of transfer, and the estimated time remaining.
Basic Progress Tracking
This feature allows you to track the progress of a file being read and written. The `progress` event provides updates on the percentage completed, the speed of transfer, and the estimated time remaining.
const progress = require('progress-stream');
const fs = require('fs');
const stat = fs.statSync('file.txt');
const str = progress({ length: stat.size, time: 100 });
str.on('progress', function(progress) {
console.log(progress);
});
fs.createReadStream('file.txt').pipe(str).pipe(fs.createWriteStream('copy.txt'));
Custom Progress Handling
This feature allows for custom handling of progress updates. You can log specific details such as the number of bytes transferred, the speed of transfer, and the estimated time remaining.
const progress = require('progress-stream');
const fs = require('fs');
const stat = fs.statSync('file.txt');
const str = progress({ length: stat.size, time: 100 });
str.on('progress', function(progress) {
console.log(`Transferred: ${progress.transferred} bytes`);
console.log(`Speed: ${progress.speed} bytes/sec`);
console.log(`ETA: ${progress.eta} seconds`);
});
fs.createReadStream('file.txt').pipe(str).pipe(fs.createWriteStream('copy.txt'));
The `progress` package provides a simple way to create a progress bar in the terminal. It is useful for command-line applications that need to display progress updates. Unlike `progress-stream`, it does not provide detailed information about data transfer but focuses on visual representation.
The `node-fetch-progress` package is used to track the progress of HTTP requests made using the `node-fetch` library. It provides similar functionality to `progress-stream` but is specifically designed for HTTP requests. It is useful for tracking the progress of file downloads or uploads over HTTP.
The `stream-progressbar` package provides a progress bar for Node.js streams. It is similar to `progress-stream` in that it tracks the progress of data being streamed, but it focuses on providing a visual progress bar in the terminal.
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
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
});
str.on('progress', function(progress) {
console.log(Math.round(progress.percentage) + '% ' + progress.speed + 'b/s' + ' ' + progress.remaining + ' bytes left');
});
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 property.
var str = progress({time:100});
console.log(str.progress);
var str = progress({time:100});
str.on('progress', function(progress) { ... });
Sets how often progress events is emitted. If omitted then defaults to emit 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.
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.
FAQs
Read the progress of a stream
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
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.