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

@particle/fetch-file

v3.0.2

Published

Download a file, report progress, retry when appropriate, and verify integrity of downloaded bits

Downloads

88

Readme

@particle/fetch-file

Download a file, report progress, retry when appropriate, and verify integrity of downloaded bits.

Installation

npm install @particle/fetch-file --save
const fetchFile = require('@particle/fetch-file');

API

@particle/fetch-file


module.exports(url, destination, [options], state) ⇒ Promise.<(DowloadedFile|Error)>

Tries really, really, really hard to download a file, verifying integrity of the downloaded bits and optionally reporting progress along the way. Also supports cancellation.

Kind: Exported function
Returns: Promise.<(DowloadedFile|Error)> - A promise for the downloaded file or an error

| Param | Type | Default | Description | | --- | --- | --- | --- | | url | string | | The URL for the file you'd like to download | | destination | string | | Path where downloaded bits will be saved | | [options] | object | | | | [options.signal] | object | | AbortSignal object as defined in https://dom.spec.whatwg.org/#interface-AbortSignal (optional) | | [options.headers] | object | | Request headers as key-value map object (optional) | | [options.maxRetries] | number | 3 | How many times to retry before giving up (optional) | | [options.onProgress] | onProgress | | Function to call with progess info (optional) | | [options.interval] | number | 100 | How often to report progress in milliseconds (optional) | | [options.algorithm] | string | "sha256" | Algorith to use when verifying checksum - supports whatever node's crypto.createHash() method does (optional) | | state | InternalState | | |

Example

// download a file
const result = await fetchFile(url, tmpFile.path);

// download a file and report progress
const onProgress = (progress) => console.log(progress);
const result = await fetchFile(url, tmpFile.path, { onProgress, interval: 250 });

// download a file but don't retry if the first attempt fails
const result = await fetchFile(url, tmpFile.path, { maxRetries: 0 });

// cancel downloading a file
const { AbortController } = fetchFile;
const controller = new AbortController();

try {
   setTimeout(() => controller.abort(), 50);
   await fetchFile(url, tmpFile.path, { signal });
} catch (error){
   error.type; // 'aborted'
   error.name; // 'AbortError'
   error.message; // 'The user aborted a request.'
}

module.exports~DowloadedFile : Object

Info about the downloaded file

Kind: inner typedef of module.exports
Properties

| Name | Type | Description | | --- | --- | --- | | filename | string | Filename of downloaded file | | hash | string | Checksum for downloaded file |


module.exports~Progress : Object

Progress data passed to onProgress callback

Kind: inner typedef of module.exports
Properties

| Name | Type | Description | | --- | --- | --- | | length | number | size in bytes of your file | | transferred | number | bytes processed | | remaining | number | bytes remaining to be processed | | percentage | number | percentage of bytes transferred (0-100) |


module.exports~onProgress : function

Kind: inner typedef of module.exports

| Param | Type | Description | | --- | --- | --- | | progress | Progress | progress info for file |


NOTE: Unfortunately, docs have a nasty habit of falling out of date. When in doubt, check usage in tests