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

multipart-download

v1.2.5

Published

Speed up download of a single file with multiple HTTP GET connections running in parallel

Downloads

178

Readme

multipart-download Build Status

NPM

Speed up download of a single file with multiple HTTP GET connections running in parallel

Class: MultipartDownload

MultipartDownload is an EventEmitter.

start(url[, options])

  • url <string> Url of file to be downloaded
  • options <StartOptions> Download options (Optional)
    • numOfConnections <number> Number of HTTP GET connections to use for performing the download (Optional)
    • writeToBuffer <boolean> Store downloaded data to buffer (Optional)
    • saveDirectory <string> Directory to save the downloaded file (Optional)
    • fileName <string> Set name of the downloaded file (Optional)
    • headers <Object> Set custom HTTP headers (Optional)

Starts the download operation from the url.

Multiple HTTP GET connections will only be used if the target server supports partial requests. If the target server does not support partial requests, only a single HTTP GET connection will be used regardless of what the numOfConnections is set to.

If the numOfConnections parameter is not provided, a single connection will be used.

If the writeToBuffer parameter is set to true, the downloaded file will be written into a buffer.

If the saveDirectory parameter is provided, the downloaded file will be saved to the saveDirectory.

If the fileName parameter is provided, the downloaded file will be renamed to fileName. If the fileName parameter is not provided, the downloaded file will maintain its original file name.

If the headers parameter is provided, the headers will be included in the HTTP request.

Event: 'error'

  • err <Error> Emitted error

Event: 'data'

  • data <string> | <Buffer> Chunk of data received
  • offset <number> Offset for the chunk of data received

The file being downloaded can be manually constructed and manipulated using the data and offset received.

Event: 'end'

  • output <string> Downloaded file buffer or downloaded file saved path

output is the buffer of the downloaded file if the writeToBuffer parameter is set to true.

output is the location of the saved file if the saveDirectory parameter is provided.

output will be null if writeToBuffer is not set to true or saveDirectory parameter is not provided.

~~start(url[, numOfConnections, saveDirectory])~~ :exclamation: DEPRECATED and REMOVED in v1.0.0

Example

Download without writing to buffer or saving to file

const MultipartDownload = require('multipart-download');

new MultipartDownload()
  .start('https://homepages.cae.wisc.edu/~ece533/images/cat.png', {
    numOfConnections: 5
  })
  .on('error', (err) => {
    // handle error here
  })
  .on('data', (data, offset) => {
    // manipulate data here
  })
  .on('end', () => {

  });

Download and write to buffer

const MultipartDownload = require('multipart-download');

new MultipartDownload()
  .start('https://homepages.cae.wisc.edu/~ece533/images/cat.png', {
    numOfConnections: 5,
    writeToBuffer: true
  })
  .on('error', (err) => {
    // handle error here
  })
  .on('data', (data, offset) => {
    // manipulate data here
  })
  .on('end', (buffer) => {
    console.log(`Downloaded file buffer: ${buffer}`);
  });

Download and save to file

const os = require('os');

const MultipartDownload = require('multipart-download');

new MultipartDownload()
  .start('https://homepages.cae.wisc.edu/~ece533/images/cat.png', {
    numOfConnections: 5,
    saveDirectory: os.tmpdir(),
    fileName: 'kitty.png'
  })
  .on('error', (err) => {
    // handle error here
  })
  .on('data', (data, offset) => {
    // manipulate data here
  })
  .on('end', (filePath) => {
    console.log(`Downloaded file path: ${filePath}`);
  });