npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

smart-downloader

v1.2.1

Published

HTTP(s) downloader, which resumes your downloads and supports download speed limits and download progress updates

Downloads

14

Readme

Smart Downloader

Description

HTTP(s) files downloader that can resume your downloads and supports download speed limits (throttle). It also supports file download progress callback to receive progress value in percentage.

Script ensures (creates if missing) that destination dir exists.

You can also extract downloaded archive file, just specify where to. It supports tarball (gzipped as well) and zip files.

If you already know md5 checksum value for the file to be downloaded, you can also validate downloaded file against it.

*Important - it currently uses wget to make file downloads resumable and throttled, so it might not work on all operating systems. There is a plan to add a fall back to just normal download for unsupported operating systems.

Install

npm install --save smart-downloader

Simple usage

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code-1.jpg',
    destinationDir: './downloads/'
}, (err, data) => {

    if (err) {

        console.log(err);
    }

    console.log(data);
});

Advanced usage

A file download with an option to resume and limit download speed to 125 kBps (125 KiloBytes = 1 Megabit) and a custom destination file name:

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code-1.jpg',
    destinationDir: './downloads/',
    destinationFileName: 'advanced-1.jpg',
    resumeDownload: true,
    downloadSpeedLimit: 125 //value in KiloBytes per second
}, (err, data) => {

    if (err) {

        console.log(err);
    }

    console.log(data);
});

You can also specify defaults in constructor and specify md5 checksum value for the downloaded file to be verified:

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader({
    resumeDownload: true,
    downloadSpeedLimit: 25 //value in KiloBytes per second
});

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code-1.jpg',
    destinationDir: './downloads/',
    destinationFileName: 'advanced-2.jpg',
    md5: 'd4a63031f57bdcafb86ca02100fdd6d2'
}, (err, data) => {

    if (err) {

        console.log(err);
    }

    console.log(data);
});

You can pass progress callback to get download progress updates:

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader({
    resumeDownload: true,
    downloadSpeedLimit: 250, //value in KiloBytes per second
    progressUpdateInterval: 2000 // value in milliseconds
});

downloader.download({
    uri: 'https://unsplash.com/photos/cvBBO4PzWPg/download?force=true',
    destinationDir: './downloads/',
    destinationFileName: 'advanced-3.jpg',
    md5: 'd94f347a514c051cff5a28814ddacb73'
}, (err, data) => {

    if (err) {

        console.log(err);
    }
    console.log(data);
}, (err, data) => {

    if (err) {

        console.log(err);
    }
    console.log(`Progress: ${data.progress}%`);
});

You can pass extract dir to extract (unzip) archived file contents:

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code.tar.gz',
    destinationDir: './downloads/',
    extractDir: './downloads/code'
}, (err, data) => {

    if (err) {

        console.log(err);
    }
    console.log(data);
});

Specify request Headers (it accepts the array of header strings):

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code.tar.gz',
    destinationDir: './downloads/',
    extractDir: './downloads/code',
    headers: ['Accept-Language: "en-us"', "Accept-Encoding: 'gzip, deflate'"]
}, (err, data) => {

    if (err) {

        console.log(err);
    }
    console.log(data);
});

Specify other wget related options (it accepts the array of options):

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

downloader.download({
    uri: 'https://github.com/gegis/smart-downloader/raw/master/test/fixtures/code.tar.gz',
    destinationDir: './downloads/',
    extractDir: './downloads/code',
    wgetOptions: ['--no-dns-cache', '--wait=1']
}, (err, data) => {

    if (err) {

        console.log(err);
    }
    console.log(data);
});

Example of how to stop ("pause") current download process:

const SmartDownloader = require('smart-downloader');

const downloader = new SmartDownloader();

// It returns child process instance
const cmd = downloader.download({
    uri: 'https://unsplash.com/photos/cvBBO4PzWPg/download?force=true',
    destinationDir: './downloads/',
    destinationFileName: 'test-stop.jpg,
    downloadSpeedLimit: 300
}, (err, data) => {

    if (err) {

        // Handling the known error
        if (err.message === 'Download error. Signal: SIGTERM') {

            console.log('Handled stop);
        } else {

            console.log(err);
        }
    }
    console.log(data);
});

// After 3 seconds we decide we want to stop it from downloading
setTimeout(() => {

    // You can kill it with custom signal, i.e. "SIGINT"
    // By default it's SIGTERM signal
    // It will cause an error in your callback, make sure to handle it
    cmd.kill();
}, 3000);

Options

Can be passed either to constructor or download function:

  • uri - url to download file from
  • destinationDir - destination directory, absolute or relative path
  • resumeDownload - resume download, default: true
  • downloadSpeedLimit - download speed limit integer value, default: null
  • downloadSpeedLimitUnit - download speed limit unit value, default 'k' (KiloBytes)
  • progressUpdateInterval - it tells how often to update with download progress, value in ms, default 1000
  • destinationFileName - downloaded file name in destination dir, otherwise it will use original file name
  • md5 - if md5 checksum specified, it will verify downloaded file md5 checksum against it
  • extractDir - if specified, it will extract downloaded archive to the specified dir
  • headers - (array) if specified, it will append headers to request
  • wgetOptions - (array) if specified, it will append all options related to wget command