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

rapid-downloader

v1.2.6

Published

Multi connections rapid file downloading management package

Downloads

182

Readme

rapid-downloader

Ez to use accelerated downloader for Node.js enviroment. Greatly increase download speed by taking adavantage of node's non-io-blocking, divide the job into multiple network connections.

NPM

In a hurry? There is example in the end. Skip bellow. Go to last section.

What is inside the box

  • DownloadWorker: the core conception of downloading, and managing download connections. One worker mean one file that need to be downloaded.
  • utils: Cause it is not a class, I named it lowercase. This object contains many utilities functions to work with files/DownloadWorker's result.

P/s: The most useful one inside utils is utils.dynamicSpeedUnitDisplay(progress.bytesPerSecond, 2) That function is for making human-readable download speed string from raw progress result of DownloadWorker. For more details, see example bellow.

What happen inside DownloadWorker

DownloadWorker a.k.a worker, will first try to make a HEAD request. To determine if source server is supporting Range requests. After this point, there will be two cases:

  1. If it is advertised as ranges is supported, worker will then initilize multiples connection which is divided perfectly to download the file. Each connections will download it's part to seperated file. After all connections done it's jobs. Worker will then join all partial files into one. The download is completed.

  2. If worker decided that source server doesn't have ranges support, it will initilize only one connection. Source file will be then transfered byte by byte to single local file. After that single connection done it's job. The download is completed.

Available DownloadWorker options, and its default value

const options = {
    // Number of connections should be used
    // the bigger number, the faster download be
    // ... and the greater stress on your network also
    // default: 4
    maxConnections: 4, 

    // Force DownloadWorker to use multiple connections
    // even target server is saying that ranges is not support
    // (may rise an error, if target server is really doesn't support range requests)
    // default: false
    forceMultipleConnections : false,

    // Force single connection
    // oposite to forceMultipleConnections, force worker to download with
    // single connection, even target server accept range requests
    // Note: If both forceSingleConnection and forceMultipleConnections are true
    //       single connection will be used
    // default: false
    forceSingleConnection: false,

    // Number of miliseconds should wait for each progress calculation cycle
    // because it cost a little bit CPU to calculate the progress.
    // You should keep this updating cycle slow as posible, idle is 200ms
    // default: 200
    progressUpdateInterval: 200
};

How to use (show me the code!)

Simplest form, just download a file

In below example, Linode's server support Ranges request, so DownloadWorker will go with multiple connections.

const {DownloadWorker} = require("rapid-downloader");

const worker = new DownloadWorker("http://speedtest.tokyo2.linode.com/100MB-tokyo2.bin", "100MB-tokyo2.zip");
worker.on('ready', () => {
    worker.on('start', () => console.log('started'))
    worker.on('progress', (progress) => {
        console.log(`${progress.completedPercent}% - ${progress.bytesPerSecond} B/s`)
    });
    worker.on('finishing', () => console.log('Download is finishing'));
    worker.on('end', () => console.log('Download is done'));
    worker.start();
});

Specify number of desired connections, display human-readable download speed

const {DownloadWorker, utils} = require("rapid-downloader");

// Multi connections
const worker = new DownloadWorker("http://speedtest.tokyo2.linode.com/100MB-tokyo2.bin", "100MB-tokyo2.zip", {
    maxConnections: 8
});
worker.on('ready', () => {
    worker.on('start', () => console.log('started'))
    worker.on('progress', (progress) => {
        const speed = utils.dynamicSpeedUnitDisplay(progress.bytesPerSecond, 2);
        console.log(`${progress.completedPercent}% - ${speed}`)
    });
    worker.on('finishing', () => console.log('Download is finishing'));
    worker.on('end', () => console.log('Download is done'));
    worker.start();
});

Force DownloadWorker to download by using single connection

const {DownloadWorker, utils} = require("rapid-downloader");

const worker = new DownloadWorker("http://speedtest.tokyo2.linode.com/100MB-tokyo2.bin", "100MB.zip", {
    forceSingleConnection: true
});
worker.on('ready', () => {
    worker.on('start', () => console.log('started'))
    worker.on('progress', (progress) => {
        const speed = utils.dynamicSpeedUnitDisplay(progress.bytesPerSecond, 2);
        console.log(`${progress.completedPercent}% - ${speed}`)
    });
    worker.on('finishing', () => console.log('Download is finishing'));
    worker.on('end', () => console.log('Download is done'));
    worker.on('error', error => console.log(error));
    worker.start();
});