Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

tomayac/fetch-in-chunks

Open more actions menu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

17 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

fetch-in-chunks

A utility for fetching large files in chunks with support for parallel downloads, progress tracking, and request abortion.

Installation

Install the package using npm:

npm install fetch-in-chunks

Usage

Importing the Module

import fetchInChunks from 'fetch-in-chunks';

Function Signature

async function fetchInchunks(url, options = {})

Parameters

  • url (string): The URL of the file to download.
  • options (object, optional): An object containing additional options.
    • options.chunkSize (number, optional): The size of each chunk to download in bytes. Defaults to the total file size divided by maxParallelRequests.
    • options.maxParallelRequests (number, default: '6'): The number of chunks to download in parallel.
    • options.progressCallback (function, optional): A callback function that will be called with the number of bytes downloaded and the total size of the file.
    • options.signal (AbortSignal, optional): An AbortSignal object that can be used to abort the download.

Returns

  • Promise<Blob>: A promise that resolves to a Blob containing the downloaded file.

Example

Basic Usage

import fetchInChunks from 'fetch-in-chunks';

async function downloadFile() {
  try {
    const blob = await fetchInChunks('https://example.com/largefile.zip');
    return blob;
  } catch (error) {
    console.error('Error fetching file:', error);
  }
}

downloadFile();

With Progress Callback

import fetchInChunks from 'fetch-in-chunks';

async function downloadFileWithProgress() {
  try {
    const blob = await fetchInChunks('https://example.com/largefile.zip', {
      progressCallback: (downloaded, total) => {
        console.log(`Downloaded ${((downloaded / total) * 100).toFixed(2)}%`);
      },
    });
    return blob;
  } catch (error) {
    console.error('Error fetching file:', error);
  }
}

downloadFileWithProgress();

With AbortController

import fetchInChunks from 'fetch-in-chunks';

async function downloadFileWithAbort() {
  const controller = new AbortController();
  const signal = controller.signal;

  try {
    const blob = await fetchInChunks('https://example.com/largefile.zip', {
      signal,
    });
    return blob;
  } catch (error) {
    if (error.name === 'AbortError') {
      console.log('Download aborted');
    } else {
      console.error('Error fetching file:', error);
    }
  }

  // You can abort the download at any time.
  setTimeout(() => {
    controller.abort();
  }, 10_000);
}

License

This project is licensed under the Apache 2.0 License. See the LICENSE file for details.

About

A utility for fetching large files in chunks with support for parallel downloads and progress tracking.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published
Morty Proxy This is a proxified and sanitized view of the page, visit original site.