Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: WIP: multi-range download progress #8697

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,7 @@ export abstract class DifferentialDownloader {

// Create our download info transformer if we have one
let downloadInfoTransform: ProgressDifferentialDownloadCallbackTransform | undefined = undefined
if (!this.options.isUseMultipleRangeRequest && this.options.onProgress) {
// TODO: Does not support multiple ranges (someone feel free to PR this!)
if (this.options.onProgress) {
const expectedByteCounts: Array<number> = []
let grandTotalBytes = 0

Expand Down Expand Up @@ -189,7 +188,7 @@ export abstract class DifferentialDownloader {

let w: any
if (this.options.isUseMultipleRangeRequest) {
w = executeTasksUsingMultipleRangeRequests(this, tasks, firstStream, oldFileFd, reject)
w = executeTasksUsingMultipleRangeRequests(this, tasks, firstStream, oldFileFd, reject, downloadInfoTransform)
w(0)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { Writable } from "stream"
import { copyData, DataSplitter, PartListDataTask } from "./DataSplitter"
import { DifferentialDownloader } from "./DifferentialDownloader"
import { Operation, OperationKind } from "./downloadPlanBuilder"
import { ProgressDifferentialDownloadCallbackTransform } from "./ProgressDifferentialDownloadCallbackTransform"

export function executeTasksUsingMultipleRangeRequests(
differentialDownloader: DifferentialDownloader,
tasks: Array<Operation>,
out: Writable,
oldFileFd: number,
reject: (error: Error) => void
reject: (error: Error) => void,
downloadInfoTransform?: ProgressDifferentialDownloadCallbackTransform
): (taskOffset: number) => void {
const w = (taskOffset: number): void => {
if (taskOffset >= tasks.length) {
Expand All @@ -32,13 +34,21 @@ export function executeTasksUsingMultipleRangeRequests(
},
out,
() => w(nextOffset),
reject
reject,
downloadInfoTransform
)
}
return w
}

function doExecuteTasks(differentialDownloader: DifferentialDownloader, options: PartListDataTask, out: Writable, resolve: () => void, reject: (error: Error) => void): void {
function doExecuteTasks(
differentialDownloader: DifferentialDownloader,
options: PartListDataTask,
out: Writable,
resolve: () => void,
reject: (error: Error) => void,
downloadInfoTransform?: ProgressDifferentialDownloadCallbackTransform
): void {
let ranges = "bytes="
let partCount = 0
const partIndexToTaskIndex = new Map<number, number>()
Expand All @@ -65,7 +75,8 @@ function doExecuteTasks(differentialDownloader: DifferentialDownloader, options:

if (task.kind === OperationKind.COPY) {
copyData(task, out, options.oldFileFd, reject, () => w(index))
} else {
}
else {
const requestOptions = differentialDownloader.createRequestOptions()
requestOptions.headers!.Range = `bytes=${task.start}-${task.end - 1}`
const request = differentialDownloader.httpExecutor.createRequest(requestOptions, response => {
Expand All @@ -89,6 +100,10 @@ function doExecuteTasks(differentialDownloader: DifferentialDownloader, options:

const requestOptions = differentialDownloader.createRequestOptions()
requestOptions.headers!.Range = ranges.substring(0, ranges.length - 2)

console.log("beginRangeDownload")
downloadInfoTransform?.beginRangeDownload()

const request = differentialDownloader.httpExecutor.createRequest(requestOptions, response => {
if (!checkIsRangesSupported(response, reject)) {
return
Expand All @@ -114,6 +129,7 @@ function doExecuteTasks(differentialDownloader: DifferentialDownloader, options:
})
differentialDownloader.httpExecutor.addErrorAndTimeoutHandlers(request, reject)
request.end()
downloadInfoTransform?.endRangeDownload()
}

export function checkIsRangesSupported(response: IncomingMessage, reject: (error: Error) => void): boolean {
Expand Down
Loading