fetch-in-chunks
Advanced tools
Comparing version 1.1.1 to 1.2.0
52
index.js
@@ -0,1 +1,23 @@ | ||
/** | ||
* @typedef {Object} FetchInChunksOptions | ||
* @property {number} [chunkSize=5 * 1024 * 1024] - The size of each chunk in | ||
* bytes. Default is `5 * 1024 * 1024` | ||
* @property {number} [maxParallelRequests=6] - The maximum number of parallel | ||
* chunk requests. Default is `6` | ||
* @property {function(number, number): void} [progressCallback=null] - A | ||
* callback function that is called with the downloaded bytes and total file | ||
* size. Default is `null` | ||
* @property {AbortSignal} [signal=null] - An AbortSignal to allow aborting the | ||
* request. Default is `null` | ||
*/ | ||
/** | ||
* Fetch a file in chunks with parallel requests and optional progress tracking. | ||
* | ||
* @param {string} url - The URL of the file to download. | ||
* @param {FetchInChunksOptions} [options={}] - The options for the download. | ||
* Default is `{}` | ||
* @returns {Promise<Blob>} A promise that resolves to a Blob containing the | ||
* downloaded file. | ||
*/ | ||
async function fetchInChunks(url, options = {}) { | ||
@@ -9,2 +31,9 @@ const { | ||
/** | ||
* Get the size of the remote file using a HEAD request. | ||
* | ||
* @param {string} url - The URL of the file. | ||
* @param {AbortSignal} signal - The abort signal. | ||
* @returns {Promise<number>} The size of the file in bytes. | ||
*/ | ||
async function getFileSize(url, signal) { | ||
@@ -22,2 +51,11 @@ const response = await fetch(url, { method: 'HEAD', signal }); | ||
/** | ||
* Fetch a chunk of the file. | ||
* | ||
* @param {string} url - The URL of the file. | ||
* @param {number} start - The start byte of the chunk. | ||
* @param {number} end - The end byte of the chunk. | ||
* @param {AbortSignal} signal - The abort signal. | ||
* @returns {Promise<ArrayBuffer>} The chunk data. | ||
*/ | ||
async function fetchChunk(url, start, end, signal) { | ||
@@ -34,2 +72,15 @@ const response = await fetch(url, { | ||
/** | ||
* Download the file in chunks with parallelism. | ||
* | ||
* @param {string} url - The URL of the file. | ||
* @param {number} fileSize - The size of the file in bytes. | ||
* @param {number} chunkSize - The size of each chunk in bytes. | ||
* @param {number} maxParallelRequests - The maximum number of parallel chunk | ||
* requests. | ||
* @param {function(number, number): void} progressCallback - The progress | ||
* callback function. | ||
* @param {AbortSignal} signal - The abort signal. | ||
* @returns {Promise<ArrayBuffer[]>} The downloaded chunks. | ||
*/ | ||
async function downloadChunks( | ||
@@ -48,2 +99,3 @@ url, | ||
// Function to process the queue | ||
async function processQueue() { | ||
@@ -50,0 +102,0 @@ while (start < fileSize) { |
{ | ||
"name": "fetch-in-chunks", | ||
"version": "1.1.1", | ||
"version": "1.2.0", | ||
"description": "A utility for fetching large files in chunks with support for parallel downloads and progress tracking.", | ||
"main": "index.js", | ||
"types": "index.d.ts", | ||
"type": "module", | ||
"files": [ | ||
"index.js" | ||
"index.js", | ||
"index.d.ts" | ||
], | ||
@@ -19,3 +21,4 @@ "exports": { | ||
"scripts": { | ||
"fix": "npx prettier . --write" | ||
"fix": "npx prettier . --write", | ||
"start": "npx http-server" | ||
}, | ||
@@ -41,2 +44,3 @@ "repository": { | ||
"devDependencies": { | ||
"http-server": "^14.1.1", | ||
"prettier": "^3.2.5", | ||
@@ -43,0 +47,0 @@ "prettier-plugin-jsdoc": "^1.3.0" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
20602
5
142
3