Skip to content

multi-tasking download files and support continue transferring from breakpoint repeattly.

Notifications You must be signed in to change notification settings

chengang4505/repeat-down

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

repeat-down

multi-tasking download files and support continue transferring from breakpoint repeattly.

npm install repeat-down

##example

    var path = require('path');
    var RepeatDown = require('repeat-down');
    
    //{maxRun} max tasks in running, {repeatNum} the auto repeat times internally  if a task failed .
    //{timeout} the connection timeout.
    var downloader = new RepeatDown({maxRun: 6, timeout: 5000, repeatNum: 4});
    
    downloader.get({
        url:'http://download.sublimetext.com/Sublime%20Text%20Build%203103%20x64%20Setup.exe',
        dest:path.join(__dirname, 'temp', 'test.zip')
    }).get({
        url:'http://download.sublimetext.com/Sublime%20Text%20Build%203103%20x64%20Setup.exe',
        dest:path.join(__dirname, 'temp', 'test2.zip')
    }).run(function(percent,task){//percent callback {percent: 0-100}
    
        console.log('file:'+task.dest+' : '+percent);
        
    },function(err,task){//single task callback ,maybe completed or have a error.
    
        if(err){
            console.log('download err :'+task.url);
        }else{
            console.log('download complete :'+task.url);
        }
        
    },function(errtasks){//all tasks callback,errtasks are array of err task,
    
        if(errtasks.length > 1){
            console.log('failed tasks :'+errtasks.length);

            //you can call this to continue run the errtasks.
            //downloader.restartErrTasks(errtasks);
        }else{
            console.log('all complete.');
        }
        
    });

#methods

###RepeatDown(option)

    var RepeatDown = require('repeat-down');
    var downloader = new RepeatDown({maxRun: 6, timeout: 5000, repeatNum: 4});

create a downloader. option a obj with some propertys.

timeout [number] the connection timeout ,default 5000(milliscond).
maxRun [number] the max running tasks ,default 5.
repeatNum [number] max auto repeat times if a task failed ,default 10.
delayTime [number] a increasing time for every repeat ,default 500(milliscond).

###.get(option)

downloader.get({
        url:'http://download.sublimetext.com/Sublime%20Text%20Build%203103%20x64%20Setup.exe',
        dest:path.join(__dirname, 'temp', 'test.zip')
    })

add tasks.
option is a obj|array. propertys below.

url [string] the quest url of a file.
dest [string] the filepath.

###.run(processCallback,doneCallback,doneAllCallback)

    downloader.run(processCallback(percent, task),doneCallback(err, task),doneAllCallback(errtasks));

run the tasks.

processCallback the process callback. percent a percent for every task in range 0 - 100. task is a obj.
doneCallback the callback for every task when complete or failed.
doneAllCallback the global callback. errtasks is a array of failed tasks.
optionly you cal call downloader.restartErrTasks(errtasks) to continue the failed tasks.

###.restartErrTasks(errtasks)

restart the failed tasks. if need you can call many times until all tasks complete.

###Task

task has some propertys below:

start [number] the start point(byte) in download stream for breakpoint resume.
timeout [number] connection timeout.
url [string] quest url.
dest [string]filepath.
curChunk [obj] a chunk obj.
_isBreak [boolean] whether if surpport breakpoint resume for the task.
process [number]a percent of the process.
reclen [number] current received length. start+reclen is the total received length.
totalLen [number] the total lenght of the file will be received.

About

multi-tasking download files and support continue transferring from breakpoint repeattly.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published