@@ -27,7 +27,6 @@ export function getDownloadBaseOptions<T extends DownloadBaseOptions>(options?:
27
27
rangePolicy : options . rangePolicy ,
28
28
dispatcher : options . dispatcher ,
29
29
checkpointHandler : options . checkpointHandler ,
30
- skipHead : options . skipHead ,
31
30
skipRevalidate : options . skipRevalidate ,
32
31
skipPrevalidate : options . skipPrevalidate ,
33
32
}
@@ -51,8 +50,6 @@ export interface DownloadBaseOptions {
51
50
*/
52
51
checkpointHandler ?: CheckpointHandler
53
52
54
- skipHead ?: boolean
55
-
56
53
skipRevalidate ?: boolean
57
54
58
55
skipPrevalidate ?: boolean
@@ -87,6 +84,10 @@ export interface DownloadOptions extends DownloadBaseOptions {
87
84
* Will first download to pending file and then rename to actual file
88
85
*/
89
86
pendingFile ?: string
87
+ /**
88
+ * The expected total size of the file.
89
+ */
90
+ expectedTotal ?: number
90
91
}
91
92
92
93
async function getWithRange (
@@ -151,7 +152,7 @@ async function getWithRange(
151
152
onHeaderMetadata ( metadata )
152
153
153
154
function writeBuf ( chunk : Buffer , callback : ( err ?: Error | null ) => void ) {
154
- const reachLimit = ( range . start + chunk . length ) > range . end
155
+ const reachLimit = range . end !== - 1 && ( range . start + chunk . length ) > range . end
155
156
const killRequest = isInitializeRequest && reachLimit
156
157
write ( fd , chunk , 0 , chunk . length , range . start , ( err ) => {
157
158
range . start += chunk . length
@@ -202,11 +203,14 @@ class DownloadJob {
202
203
readonly url : string ,
203
204
readonly fd : number ,
204
205
readonly headers : Record < string , string > ,
206
+ readonly expectedTotal : number | undefined ,
205
207
readonly onProgress : ( url : URL | string , chunkSize : number , progress : number , total : number ) => void ,
206
208
readonly dispatcher : Dispatcher ,
207
209
readonly rangePolicy : RangePolicy ,
208
210
readonly signal ?: AbortSignal ,
209
- ) { }
211
+ ) {
212
+ this . contentLength = expectedTotal ?? 0
213
+ }
210
214
211
215
readonly progress = [ {
212
216
start : 0 ,
@@ -275,6 +279,7 @@ export async function download(options: DownloadOptions) {
275
279
const skipRevalidate = options . skipRevalidate
276
280
const rangePolicy = options ?. rangePolicy ?? new DefaultRangePolicy ( 2 * 1024 * 1024 , 4 )
277
281
const dispatcher = options ?. dispatcher ?? getDefaultAgent ( )
282
+ const expectedTotal = options . expectedTotal
278
283
279
284
await mkdir ( dirname ( destination ) , { recursive : true } ) . catch ( ( ) => { } )
280
285
@@ -322,8 +327,8 @@ export async function download(options: DownloadOptions) {
322
327
pendingFile,
323
328
} )
324
329
325
- const job = new DownloadJob ( url , fd , headers , ( url , chunkSize , progress , total ) => {
326
- progressController ( typeof url === 'string' ? new URL ( url ) : url , chunkSize , progress , total )
330
+ const job = new DownloadJob ( url , fd , headers , expectedTotal , ( url , chunkSize , progress , total ) => {
331
+ progressController ( typeof url === 'string' ? new URL ( url ) : url , chunkSize , progress , ! total && expectedTotal ? expectedTotal : total )
327
332
} , dispatcher , rangePolicy , abortSignal )
328
333
329
334
const results = await job . run ( )
0 commit comments