Skip to content

Commit 35dfc22

Browse files
committed
Switch to option object
1 parent 7949415 commit 35dfc22

File tree

3 files changed

+25
-29
lines changed

3 files changed

+25
-29
lines changed

README.md

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,19 @@ import fetchInChunks from 'fetch-in-chunks';
2222
### Function Signature
2323

2424
```js
25-
async function fetchInChunks(
26-
url,
27-
chunkSize = 5 * 1024 * 1024,
28-
maxParallelRequests = 6,
29-
progressCallback = null,
30-
signal = null
31-
)
25+
async function fetchInChunks(url, options = {})
3226
```
3327

28+
### Options Object
29+
30+
- `chunkSize` (`number`, optional): The size of each chunk in bytes. Defaults to
31+
5 MB (5 _ 1024 _ 1024).
32+
- `maxParallelRequests` (`number`, optional): The maximum number of parallel
33+
chunk requests. Defaults to 6. `progressCallback` (`function`, optional): A
34+
callback function that is called with the downloaded bytes and total file
35+
size. `signal` (`AbortSignal`, optional): An `AbortSignal` to allow aborting
36+
the request.
37+
3438
#### Parameters
3539

3640
- `url` (`string`): The URL of the file to download.
@@ -80,14 +84,11 @@ import fetchInChunks from 'fetch-in-chunks';
8084

8185
async function downloadFileWithProgress() {
8286
try {
83-
const blob = await fetchInChunks(
84-
'https://example.com/largefile.zip',
85-
5 * 1024 * 1024,
86-
6,
87-
(downloaded, total) => {
87+
const blob = await fetchInChunks('https://example.com/largefile.zip', {
88+
progressCallback: (downloaded, total) => {
8889
console.log(`Downloaded ${((downloaded / total) * 100).toFixed(2)}%`);
8990
},
90-
);
91+
});
9192
// Create a download link and trigger the download
9293
const url = URL.createObjectURL(blob);
9394
const a = document.createElement('a');
@@ -114,15 +115,9 @@ async function downloadFileWithAbort() {
114115
const signal = controller.signal;
115116

116117
try {
117-
const blob = await fetchInChunks(
118-
'https://example.com/largefile.zip',
119-
5 * 1024 * 1024,
120-
6,
121-
(downloaded, total) => {
122-
console.log(`Downloaded ${((downloaded / total) * 100).toFixed(2)}%`);
123-
},
118+
const blob = await fetchInChunks('https://example.com/largefile.zip', {
124119
signal,
125-
);
120+
});
126121
// Create a download link and trigger the download
127122
const url = URL.createObjectURL(blob);
128123
const a = document.createElement('a');

index.js

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
async function fetchInChunks(
2-
url,
3-
chunkSize = 5 * 1024 * 1024,
4-
maxParallelRequests = 6,
5-
progressCallback = null,
6-
signal = null,
7-
) {
1+
async function fetchInChunks(url, options = {}) {
2+
const {
3+
chunkSize = 5 * 1024 * 1024,
4+
maxParallelRequests = 6,
5+
progressCallback = null,
6+
signal = null,
7+
} = options;
8+
89
async function getFileSize(url, signal) {
910
const response = await fetch(url, { method: 'HEAD', signal });
1011
if (!response.ok) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "fetch-in-chunks",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "A utility for fetching large files in chunks with support for parallel downloads and progress tracking.",
55
"main": "index.js",
66
"type": "module",

0 commit comments

Comments
 (0)