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

get-down

v1.2.0

Published

Download and optionally extract files

Downloads

10,842

Readme

get-down

Download and optionally extract files.

This draws heavily from Bower's download and extract utilities. Those utilities are copyright Twitter and carry the MIT license.

Example use

Download a file and save it to the working directory with the same name:

var download = require('get-down');
download('http://example.com/file.txt');

Same as above, but saving to a different directory:

var download = require('get-down');
download('http://example.com/file.txt', {dest: 'different/directory'});
// the provided directory must already exist

In addition to providing a dest directory, you can provide a new file name:

var download = require('get-down');
download('http://example.com/file.txt', {dest: 'different/name.txt'});
// the provided file must not already exist

The extract option can be used to extract tar, tgz, gz, or zip files:

var download = require('get-down');
download('http://example.com/file.zip', {dest: 'some/directory', extract: true});
// the dest directory must already exist

As you might expect, download is all async. You get an event emitter in return:

var download = require('get-down');
download('http://example.com/file.txt').on('end', function(dest) {
  console.log('downloaded', dest);
});

API Docs

download(url, [options])

  • url - string URL for the resource you want to download. E.g. 'http://example.com/file.txt' or 'https://example.com/archive.zip'.
  • options - Object An optional object with properties to configure the download operation. See the available options below.

Download a resource. Returns an EventEmitter that emits the events described below.

Options

options.dest

  • type: string
  • default: process.cwd()

The destination directory or file path for saving downloaded resources. If dest is a directory, it must already exist. If dest is a path to a file, the parent directory must already exist (existing files will not be overwritten). If the extract option is false and you provide a directory here, the filename will be derived from the provided url (e.g. a url of 'http://example.com/some/file.txt' and a dest of 'local/path' will result in 'local/path/file.txt'). Finally, if the extract option is true, you must provide the path to an existing directory.

options.extract

  • type: boolean
  • default: false

Extract the downloaded archive. If the url points to a .zip, .tar, .tgz, or .gz file, you can optionally extract/defalate it after download. If not obvious from the file extension, the archive type will be determined from the content type header of the response.

Events

progress event

Emitted periodically during the download. Listeners will receive a state object representing one of two types of progress. As more data is received, the state object will have the following properties:

  • received - number The cumulative number of bytes received.
  • total - number|null If the response headers provided it, this will be the total content size in bytes. If not provided, it will be null.
  • percent - number|null As a convenience, if total is available, this will be the percentage of the total file size received (otherwise it will be null).

If the download fails due to network issues, it will be retried (up to 5 times). In the event of a retry, progress listeners will receive a state object with the following properties:

  • retry - boolean This will always be true in the event of a retry.
  • timeout - number The number of milliseconds before the download is attempted again.
  • error - Error An error representing the failure.

A progress listener might handle both types of progress with code like this:

  download('http://example.com/big-file.txt').on('progress', function(state) {
    if (state.retry) {
      var delay = Math.round(state.timeout / 1000) + 's';
      console.log('Download failed, retrying again in ' + delay);
    } else {
      var progress = Math.floor(state.received / 1024) + 'K';
      if (state.percent) {
        progress = state.percent + '% (' + progress + ')';
      }
      console.log('Received ' + progress);
    }
  });

error event

Emitted in the event of an error during download or extraction. Listeners will receive an Error object with a message describing what went wrong. Temporary files created during a download will be removed when your program completes.

end event

Emitted in upon success. Listeners will be called with a (string) path to the saved resource. If the extract option is true, this will be a path to the directory where the resource was extracted. Otherwise, it will be a path to the saved file.

That's all

Please report any issues you find. To contribute, fork and start out by running the tests.

npm install
npm test

You can run npm start to have the linter and tests run continuously during development.

Build Status