Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

bigdownloader

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bigdownloader - npm Package Compare versions

Comparing version 1.0.2 to 1.0.4

test/download.js

115

lib/BigDownloader.js

@@ -9,6 +9,7 @@ "use strict";

var jswget = require('jswget');
//var jswget = require('jswget');
var request = require('request');
function BigDownloader( opts ) {
this.opts = util._extend({

@@ -22,3 +23,3 @@ url : undefined,

}, opts);
this.debug = function(){

@@ -29,29 +30,48 @@ if( this.opts.debug ) {

}.bind(this);
this.size = undefined;
this.progressInterval = undefined;
this.download = function( retry ){
jswget({
url : this.opts.url,
downloadpath : path.dirname(this.opts.path) + '/',
downloadas : path.basename(this.opts.path),
downloadmode : true,
onsend: function(req, options){
this.download = function( retry ){
this.debug('this.download', 'retry:', retry === true)
var requestOpts = {
url: this.opts.url,
}
try {
var stats = fs.statSync(this.opts.path);
requestOpts.headers = {
'range': 'bytes=' + stats.size.toString() + '-'
}
} catch(e){}
var writeStream = fs.createWriteStream( this.opts.path, { flags: 'a+' } );
function errCallback(err){
this.debug(err);
this.download( true );
}
request(requestOpts)
.on('error', errCallback.bind(this))
.on('response', function(response) {
if( retry ) return;
this.emit('start');
if( this.progressInterval ) clearInterval(this.progressInterval);
if( response.headers['content-length'] ) {
this.size = parseInt(response.headers['content-length']);
this.debug('got size from headers', this.size);
}
if( this.progressInterval ) clearInterval(this.progressInterval);
this.progressInterval = setInterval(function(){
fs.stat( this.opts.path, function(err, stats){
if( err ) return this.debug(err);
var total = this.opts.size || this.size;
var done = stats.size;
var percent = done / total;
this.emit('progress', {

@@ -62,22 +82,17 @@ total : total,

})
}.bind(this));
}.bind(this), this.opts.progressInterval);
}.bind(this),
onhead: function(fstat, req, res){
if( retry ) return;
if( res.headers['content-length'] ) {
this.size = parseInt(res.headers['content-length']);
this.debug('got size from headers', this.size);
}
}.bind(this),
onsuccess: function(resp, req, res){
}.bind(this))
.pipe( writeStream )
.on('error', errCallback.bind(this))
.on('finish', function(){
this.debug('onFinish');
if( this.progressInterval ) clearInterval(this.progressInterval);
if( typeof this.opts.size == 'number' ) {
if( typeof this.opts.size == 'number' ) {
var stats = fs.statSync( this.opts.path );

@@ -93,9 +108,9 @@ if( stats.size < this.opts.size ) {

return this.download( true );
}
}
}
if( typeof this.opts.hash == 'string' ) {
var hasher = crypto.createHash('sha1');
hasher.setEncoding('hex');
var reader = fs.createReadStream( this.opts.path );

@@ -107,3 +122,3 @@ reader.pipe( hasher );

this.debug('calculated hash:', hash)
if( hash == this.opts.hash ) {

@@ -119,17 +134,11 @@ this.emit('finish');

}
}.bind(this),
onerror: function(err, req){
this.debug(err);
this.download( true );
}.bind(this)
});
}.bind(this))
}.bind(this);
this.start = function(){
this.download();
}.bind(this);
}

@@ -136,0 +145,0 @@

{
"name": "bigdownloader",
"version": "1.0.2",
"version": "1.0.4",
"description": "Download big files without worries. Included auto-resume",

@@ -12,4 +12,4 @@ "main": "index.js",

"dependencies": {
"jswget": "git+https://github.com/athombv/jswget.git"
"request": "^2.69.0"
}
}

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc