Socket
Socket
Sign inDemoInstall

request-progress

Package Overview
Dependencies
1
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 0.2.3 to 0.3.0

57

index.js

@@ -7,2 +7,3 @@ 'use strict';

var reporter;
var onResponse;
var delayTimer;

@@ -19,28 +20,30 @@ var delayCompleted;

request
.on('request', function () {
receivedSize = 0;
})
.on('response', function (response) {
state.total = totalSize = Number(response.headers['content-length']);
receivedSize = 0;
// Check if there's no total size or is invalid (NaN)
if (!totalSize) {
// Throttle the progress report function
reporter = throttle(function () {
// If the received size is the same, do not report
if (previousReceivedSize === receivedSize) {
return;
}
// Throttle the function
reporter = throttle(function () {
// If there received size is the same, abort
if (previousReceivedSize === receivedSize) {
return;
}
// Update received
previousReceivedSize = receivedSize;
state.received = receivedSize;
state.received = previousReceivedSize = receivedSize;
state.percent = Math.round(receivedSize / totalSize * 100);
request.emit('progress', state);
}, options.throttle);
// Update percentage
// Note that the totalSize might available
state.percent = totalSize ? Math.round(receivedSize / totalSize * 100) : null;
request.emit('progress', state);
}, options.throttle);
// On response handler
onResponse = function (response) {
totalSize = Number(response.headers['content-length']);
receivedSize = 0;
// Note that the totalSize might available
state.totalSize = totalSize || null;
// Delay the progress report
delayCompleted = false;
delayTimer = setTimeout(function () {

@@ -50,3 +53,9 @@ delayCompleted = true;

}, options.delay);
};
request
.on('request', function () {
receivedSize = 0;
})
.on('response', onResponse)
.on('data', function (data) {

@@ -60,7 +69,13 @@ receivedSize += data.length;

.on('end', function () {
if (!delayCompleted) {
if (delayTimer) {
clearTimeout(delayTimer);
delayTimer = null;
}
});
// If we already got a response, call the on response handler
if (request.response) {
onResponse(request.response);
}
return request;

@@ -67,0 +82,0 @@ }

{
"name": "request-progress",
"version": "0.2.3",
"version": "0.3.0",
"description": "Tracks the download progress of a request made with mikeal/request",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -24,4 +24,6 @@ # request-progress [![Build Status](https://secure.travis-ci.org/IndigoUnited/node-request-progress.png)](http://travis-ci.org/IndigoUnited/node-request-progress.png)

.on('progress', function (state) {
console.log('received size in bytes', state.received);
// The properties bellow can be null if response does not contain
// the content-length header
console.log('total size in bytes', state.total);
console.log('received size in bytes', state.received);
console.log('percent', state.percent);

@@ -28,0 +30,0 @@ })

Sorry, the diff of this file is not supported yet

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