A utility for fetching large files in chunks with support for parallel downloads and progress tracking.
npm install fetch-in-chunksA utility for fetching large files in chunks with support for parallel
downloads, progress tracking, and request abortion.
Install the package using npm:
``bash`
npm install fetch-in-chunks
`js`
import fetchInChunks from 'fetch-in-chunks';
`js`
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 downloadmaxParallelRequests
in bytes. Defaults to the total file size divided by .options.maxParallelRequests
- (number, default: '6'): The number of chunksoptions.progressCallback
to download in parallel.
- (function, optional): A callback function thatoptions.signal
will be called with the number of bytes downloaded and the total size of the
file.
- (AbortSignal, optional): An AbortSignal object that can
be used to abort the download.
#### Returns
- Promise: A promise that resolves to a Blob containing the downloaded
file.
`js
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();
`
`js
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();
`
`js
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);
}
`
This project is licensed under the Apache 2.0 License. See the LICENSE` file
for details.