Skip to content

Commit 374fb6e

Browse files
committed
2 parents 2dd79f4 + 6e5dea8 commit 374fb6e

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

packages/file-transfer/download.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export function getDownloadBaseOptions<T extends DownloadBaseOptions>(options?:
2727
rangePolicy: options.rangePolicy,
2828
dispatcher: options.dispatcher,
2929
checkpointHandler: options.checkpointHandler,
30-
skipHead: options.skipHead,
3130
skipRevalidate: options.skipRevalidate,
3231
skipPrevalidate: options.skipPrevalidate,
3332
}
@@ -51,8 +50,6 @@ export interface DownloadBaseOptions {
5150
*/
5251
checkpointHandler?: CheckpointHandler
5352

54-
skipHead?: boolean
55-
5653
skipRevalidate?: boolean
5754

5855
skipPrevalidate?: boolean
@@ -87,6 +84,10 @@ export interface DownloadOptions extends DownloadBaseOptions {
8784
* Will first download to pending file and then rename to actual file
8885
*/
8986
pendingFile?: string
87+
/**
88+
* The expected total size of the file.
89+
*/
90+
expectedTotal?: number
9091
}
9192

9293
async function getWithRange(
@@ -151,7 +152,7 @@ async function getWithRange(
151152
onHeaderMetadata(metadata)
152153

153154
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
155156
const killRequest = isInitializeRequest && reachLimit
156157
write(fd, chunk, 0, chunk.length, range.start, (err) => {
157158
range.start += chunk.length
@@ -202,11 +203,14 @@ class DownloadJob {
202203
readonly url: string,
203204
readonly fd: number,
204205
readonly headers: Record<string, string>,
206+
readonly expectedTotal: number | undefined,
205207
readonly onProgress: (url: URL | string, chunkSize: number, progress: number, total: number) => void,
206208
readonly dispatcher: Dispatcher,
207209
readonly rangePolicy: RangePolicy,
208210
readonly signal?: AbortSignal,
209-
) { }
211+
) {
212+
this.contentLength = expectedTotal ?? 0
213+
}
210214

211215
readonly progress = [{
212216
start: 0,
@@ -275,6 +279,7 @@ export async function download(options: DownloadOptions) {
275279
const skipRevalidate = options.skipRevalidate
276280
const rangePolicy = options?.rangePolicy ?? new DefaultRangePolicy(2 * 1024 * 1024, 4)
277281
const dispatcher = options?.dispatcher ?? getDefaultAgent()
282+
const expectedTotal = options.expectedTotal
278283

279284
await mkdir(dirname(destination), { recursive: true }).catch(() => { })
280285

@@ -322,8 +327,8 @@ export async function download(options: DownloadOptions) {
322327
pendingFile,
323328
})
324329

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)
327332
}, dispatcher, rangePolicy, abortSignal)
328333

329334
const results = await job.run()

0 commit comments

Comments
 (0)