Skip to content
Open
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
43 changes: 26 additions & 17 deletions src/main/services/download/js-http-downloader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,17 @@ export class JsHttpDownloader {
}
}

private parseTotalSizeFrom416(response: Response): number | null {
const contentRange = response.headers.get("content-range");
if (!contentRange) return null;

const match = /bytes\s+\*\/(\d+)/i.exec(contentRange);
if (!match) return null;

const total = Number.parseInt(match[1], 10);
return Number.isFinite(total) && total > 0 ? total : null;
}

private async executeDownload(
url: string,
requestHeaders: Record<string, string>,
Expand All @@ -355,25 +366,23 @@ export class JsHttpDownloader {
);

if (response.status === 416 && startByte > 0) {
logger.log(
"[JsHttpDownloader] Range not satisfiable, deleting existing file and restarting"
);
if (fs.existsSync(filePath)) {
fs.unlinkSync(filePath);
}
this.bytesDownloaded = 0;
this.resetSpeedTracking();
const remoteTotalSize = this.parseTotalSizeFrom416(response);

const headersWithoutRange = { ...requestHeaders };
delete headersWithoutRange["Range"];
if (remoteTotalSize !== null && startByte === remoteTotalSize) {
this.fileSize = remoteTotalSize;
this.bytesDownloaded = remoteTotalSize;
this.status = "complete";
this.retryCount = 0;
this.downloadSpeed = 0;

return this.executeDownload(
url,
headersWithoutRange,
filePath,
0,
savePath,
usedFallback
logger.log(
"[JsHttpDownloader] Range not satisfiable but local file already complete"
);
return;
}

throw new Error(
`[JsHttpDownloader] Range not satisfiable for resumed download (local=${startByte}, remote=${remoteTotalSize ?? "unknown"}). Keeping local file and aborting to avoid restart from zero.`
);
}

Expand Down
1 change: 1 addition & 0 deletions tsconfig.node.tsbuildinfo

Large diffs are not rendered by default.

Loading