-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwith-progress.ts
More file actions
151 lines (132 loc) · 4.46 KB
/
with-progress.ts
File metadata and controls
151 lines (132 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/**
* Progress tracking examples demonstrating download monitoring.
*
* Run with: npx tsx examples/with-progress.ts
* Or with Deno: deno run --allow-net examples/with-progress.ts
*/
import { fetchT } from '../src/mod.ts';
// A URL that returns a larger response for demonstrating progress
const LARGE_FILE_URL = 'https://jsonplaceholder.typicode.com/photos';
/**
* Example 1: Basic progress tracking
*/
async function trackProgress() {
console.log('--- Example 1: Basic Progress Tracking ---');
const result = await fetchT(LARGE_FILE_URL, {
responseType: 'json',
onProgress: (progressResult) => {
progressResult
.inspect((progress) => {
const percent = Math.round(
(progress.completedByteLength / progress.totalByteLength) * 100,
);
console.log(`Progress: ${percent}% (${progress.completedByteLength}/${progress.totalByteLength} bytes)`);
})
.inspectErr((err) => {
// Content-Length header not available
console.log('Progress unavailable:', err.message);
});
},
});
result
.inspect((data) => {
if (data == null) {
console.log('No body');
return;
}
console.log('Downloaded', (data as unknown[]).length, 'photos');
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 2: Chunk-by-chunk processing
*/
async function processChunks() {
console.log('\n--- Example 2: Chunk Processing ---');
let chunkCount = 0;
let totalBytes = 0;
const result = await fetchT(LARGE_FILE_URL, {
responseType: 'arraybuffer',
onChunk: (chunk) => {
chunkCount++;
totalBytes += chunk.byteLength;
console.log(`Chunk ${chunkCount}: ${chunk.byteLength} bytes`);
},
});
result
.inspect(() => {
console.log(`Total: ${chunkCount} chunks, ${totalBytes} bytes`);
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 3: Combined progress and chunk tracking
*/
async function combinedTracking() {
console.log('\n--- Example 3: Combined Progress + Chunks ---');
let lastPercent = 0;
const result = await fetchT(LARGE_FILE_URL, {
responseType: 'blob',
onProgress: (progressResult) => {
progressResult.inspect((progress) => {
const percent = Math.round(
(progress.completedByteLength / progress.totalByteLength) * 100,
);
// Only log on 10% increments
if (percent >= lastPercent + 10) {
lastPercent = percent;
console.log(`Downloaded: ${percent}%`);
}
});
},
onChunk: (chunk) => {
// Process each chunk as it arrives
// (e.g., calculate hash, write to disk, etc.)
void chunk; // Acknowledge receipt
},
});
result
.inspect((blob) => {
console.log('Download complete! Blob size:', blob.size, 'bytes');
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
/**
* Example 4: Progress with custom progress bar
*/
async function progressBar() {
console.log('\n--- Example 4: Progress Bar ---');
const BAR_LENGTH = 40;
const result = await fetchT(LARGE_FILE_URL, {
responseType: 'text',
onProgress: (progressResult) => {
progressResult.inspect((progress) => {
const ratio = progress.completedByteLength / progress.totalByteLength;
const filled = Math.round(BAR_LENGTH * ratio);
const empty = BAR_LENGTH - filled;
const bar = '█'.repeat(filled) + '░'.repeat(empty);
const percent = Math.round(ratio * 100);
console.log(`[${bar}] ${percent}%`);
});
},
});
result
.inspect((text) => {
console.log('Downloaded', text.length, 'characters');
})
.inspectErr((err) => {
console.error('Failed:', err.message);
});
}
// Run all examples
console.log('=== Progress Tracking Examples ===\n');
await trackProgress();
await processChunks();
await combinedTracking();
await progressBar();