get-down

WebJar for get-down

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

get-down
Last Version

Last Version

1.2.0
Release Date

Release Date

Type

Type

jar
Description

Description

get-down
WebJar for get-down
Project URL

Project URL

http://webjars.org
Source Code Management

Source Code Management

https://github.com/tschaub/get-down

Download get-down

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/get-down/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>get-down</artifactId>
    <version>1.2.0</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/get-down/
implementation 'org.webjars.npm:get-down:1.2.0'
// https://jarcasting.com/artifacts/org.webjars.npm/get-down/
implementation ("org.webjars.npm:get-down:1.2.0")
'org.webjars.npm:get-down:jar:1.2.0'
<dependency org="org.webjars.npm" name="get-down" rev="1.2.0">
  <artifact name="get-down" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='get-down', version='1.2.0')
)
libraryDependencies += "org.webjars.npm" % "get-down" % "1.2.0"
[org.webjars.npm/get-down "1.2.0"]

Dependencies

compile (10)

Group / Artifact Type Version
org.webjars.npm : tmp jar 0.0.31
org.webjars.npm : graceful-fs jar 4.1.11
org.webjars.npm : mout jar 1.0.0
org.webjars.npm : retry jar 0.10.1
org.webjars.npm : junk jar 2.1.0
org.webjars.npm : request jar 2.81.0
org.webjars.npm : request-progress jar 3.0.0
org.webjars.npm : decompress-zip jar 0.3.0
org.webjars.npm : q jar 1.5.0
org.webjars.npm : tar jar 3.1.5

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.2.0