-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-tracking.ts
More file actions
187 lines (178 loc) · 5.7 KB
/
Copy pathprogress-tracking.ts
File metadata and controls
187 lines (178 loc) · 5.7 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import fetch from '@root/index'
import { handleError } from './utils'
/** HTTPBin.org base URL for testing progress tracking */
const httpbinUrl: string = 'https://httpbin.org'
/** Test file size for progress demonstrations */
const testFileSize: number = 1 * 1024 * 1024
/**
* Tests upload progress tracking functionality.
* @description Demonstrates upload progress with rate limiting and progress callbacks.
* @returns Promise that resolves when test completes
*/
async function progressUpload(): Promise<void> {
try {
const largeFile: Blob = new Blob(['x'.repeat(testFileSize)], {
type: 'application/octet-stream'
})
console.log(
'-> Upload Progress Tracking',
await fetch.post(`${httpbinUrl}/post`, largeFile, {
maxRate: 100 * 1024,
onProgress: (percentage: number) => {
console.log(` 📤 Upload Progress: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'Upload Progress Tracking')
}
}
/**
* Tests download progress tracking functionality.
* @description Demonstrates download progress with rate limiting and progress callbacks.
* @returns Promise that resolves when test completes
*/
async function progressDownload(): Promise<void> {
try {
console.log(
'-> Download Progress Tracking',
await fetch.get(`${httpbinUrl}/bytes/${testFileSize}`, {
download: true,
filename: 'test-download.bin',
maxRate: 200 * 1024,
onProgress: (percentage: number) => {
console.log(` 📥 Download Progress: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'Download Progress Tracking')
}
}
/**
* Tests FormData upload progress tracking.
* @description Demonstrates FormData upload progress with mixed content types.
* @returns Promise that resolves when test completes
*/
async function progressFormData(): Promise<void> {
try {
const formData: FormData = new FormData()
formData.append('title', 'Progress Test File')
formData.append('description', 'Testing FormData upload progress')
const fileBlob: Blob = new Blob(['FormData file content for progress test'], {
type: 'text/plain'
})
formData.append('file', fileBlob, 'progress-test.txt')
console.log(
'-> FormData Upload Progress',
await fetch.post(`${httpbinUrl}/post`, formData, {
maxRate: 150 * 1024,
onProgress: (percentage: number) => {
console.log(` 📋 FormData Progress: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'FormData Upload Progress')
}
}
/**
* Tests progress tracking error handling.
* @description Demonstrates progress tracking with invalid configurations and error scenarios.
* @returns Promise that resolves when test completes
*/
async function progressErrorHandling(): Promise<void> {
try {
console.log(
'-> Progress Error Handling',
await fetch.get(`${httpbinUrl}/status/500`, {
download: true,
filename: 'error-test.bin',
onProgress: (percentage: number) => {
console.log(` ❌ Error Progress: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'Progress Error Handling')
}
}
/**
* Tests progress tracking with different rate limits.
* @description Demonstrates various rate limiting scenarios for uploads and downloads.
* @returns Promise that resolves when test completes
*/
async function progressRateLimiting(): Promise<void> {
try {
const testData: Blob = new Blob(['Rate limiting test data'], {
type: 'application/octet-stream'
})
console.log(
'-> Rate Limiting Progress',
await fetch.post(`${httpbinUrl}/post`, testData, {
maxRate: 50 * 1024,
onProgress: (percentage: number) => {
console.log(` 🐌 Slow Upload: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'Rate Limiting Progress')
}
}
/**
* Tests progress tracking with JSON data upload.
* @description Demonstrates progress tracking with JSON payloads and rate limiting.
* @returns Promise that resolves when test completes
*/
async function progressJsonUpload(): Promise<void> {
try {
const jsonData: Record<string, unknown> = {
title: 'Progress Test JSON',
content: 'x'.repeat(500 * 1024),
timestamp: Date.now(),
metadata: {
testType: 'progress-tracking',
fileSize: '500KB'
}
}
console.log(
'-> JSON Upload Progress',
await fetch.post(`${httpbinUrl}/post`, jsonData, {
maxRate: 75 * 1024,
onProgress: (percentage: number) => {
console.log(` 📄 JSON Progress: ${percentage.toFixed(1)}%`)
}
})
)
} catch (error: unknown) {
handleError(error, 'JSON Upload Progress')
}
}
/**
* Executes all progress tracking test functions in sequence.
* @description Runs all test functions with progress logging and error handling.
* @returns Promise that resolves when all tests complete
*/
async function runAllTests(): Promise<void> {
const listTests: Array<() => Promise<void>> = [
progressUpload,
progressDownload,
progressFormData,
progressJsonUpload,
progressRateLimiting,
progressErrorHandling
]
for (const [index, testTask] of listTests.entries()) {
console.log(`[${index + 1}] Working on "${testTask.name}" Function...`)
await testTask()
console.log('-\n-')
}
}
/**
* Main function to execute all tests when run directly.
* @description Checks if the script is being run directly and executes all tests.
*/
if (import.meta.url === `file://${process.argv[1]}`) {
runAllTests().catch(console.error)
}