chore: include pending changes from original project
Pre-existing uncommitted changes carried over from the original repo: - download-manager.ts: HTTP 416 text check, resume preflight recovery - test updates for auto-rename and download-manager Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
efa0909e11
commit
566483a438
@ -395,6 +395,18 @@ function isFetchFailure(errorText: string): boolean {
|
||||
return text.includes("fetch failed") || text.includes("socket hang up") || text.includes("econnreset") || text.includes("network error");
|
||||
}
|
||||
|
||||
function isHttp416Text(errorText: string): boolean {
|
||||
return /(^|\D)416(\D|$)/.test(String(errorText || ""));
|
||||
}
|
||||
|
||||
function shouldPreflightFinalizeItemFromDisk(item: DownloadItem): boolean {
|
||||
const text = `${item.fullStatus || ""} ${item.lastError || ""}`.toLowerCase();
|
||||
return text.includes("resume-link erneuern")
|
||||
|| text.includes("resume link erneuern")
|
||||
|| text.includes("range_ignored_on_resume")
|
||||
|| text.includes("server ignorierte range");
|
||||
}
|
||||
|
||||
function isResumeHardResetReason(errorText: string): boolean {
|
||||
const text = String(errorText || "");
|
||||
return text.startsWith("resume_download_underflow:");
|
||||
@ -3883,16 +3895,24 @@ export class DownloadManager extends EventEmitter {
|
||||
|| item.status === "validating"
|
||||
|| item.status === "paused"
|
||||
|| item.status === "reconnect_wait") {
|
||||
const preserveRecoveryStatus = shouldPreflightFinalizeItemFromDisk(item);
|
||||
item.status = "queued";
|
||||
const itemPkg = this.session.packages[item.packageId];
|
||||
item.fullStatus = (itemPkg && itemPkg.enabled === false) ? "Paket gestoppt" : "Wartet";
|
||||
if (preserveRecoveryStatus) {
|
||||
item.fullStatus = (item.fullStatus || "").trim() || "Wartet";
|
||||
} else {
|
||||
const itemPkg = this.session.packages[item.packageId];
|
||||
item.fullStatus = (itemPkg && itemPkg.enabled === false) ? "Paket gestoppt" : "Wartet";
|
||||
}
|
||||
item.speedBps = 0;
|
||||
item.updatedAt = nowMs();
|
||||
}
|
||||
// Clear stale transient status texts from previous session
|
||||
if (item.status === "queued") {
|
||||
const statusText = (item.fullStatus || "").trim();
|
||||
if (statusText !== "Wartet" && statusText !== "Paket gestoppt" && statusText !== "Online") {
|
||||
if (statusText !== "Wartet"
|
||||
&& statusText !== "Paket gestoppt"
|
||||
&& statusText !== "Online"
|
||||
&& !shouldPreflightFinalizeItemFromDisk(item)) {
|
||||
item.fullStatus = "Wartet";
|
||||
}
|
||||
}
|
||||
@ -6035,6 +6055,37 @@ export class DownloadManager extends EventEmitter {
|
||||
this.retryAfterByItem.set(item.id, nowMs() + waitMs);
|
||||
}
|
||||
|
||||
private scheduleHttp416Retry(
|
||||
item: DownloadItem,
|
||||
active: ActiveTask,
|
||||
retryDisplayLimit: string,
|
||||
errorText: string,
|
||||
claimedTargetPath: string
|
||||
): void {
|
||||
active.genericErrorRetries += 1;
|
||||
item.retries += 1;
|
||||
if (claimedTargetPath) {
|
||||
try {
|
||||
fs.rmSync(claimedTargetPath, { force: true });
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
this.releaseTargetPath(item.id);
|
||||
this.dropItemContribution(item.id);
|
||||
item.lastError = errorText;
|
||||
item.downloadedBytes = 0;
|
||||
item.totalBytes = null;
|
||||
item.progressPercent = 0;
|
||||
item.speedBps = 0;
|
||||
const delayMs = retryDelayWithJitter(active.genericErrorRetries, 200);
|
||||
logger.warn(
|
||||
`HTTP 416 erkannt: item=${item.fileName || item.id}, ` +
|
||||
`retry=${active.genericErrorRetries}/${retryDisplayLimit}, error=${errorText}, provider=${item.provider || "?"}`
|
||||
);
|
||||
this.queueRetry(item, active, delayMs, `HTTP 416 erkannt, Retry ${active.genericErrorRetries}/${retryDisplayLimit}`);
|
||||
}
|
||||
|
||||
private startItem(packageId: string, itemId: string): void {
|
||||
const item = this.session.items[itemId];
|
||||
const pkg = this.session.packages[packageId];
|
||||
@ -6055,6 +6106,15 @@ export class DownloadManager extends EventEmitter {
|
||||
|
||||
this.retryAfterByItem.delete(itemId);
|
||||
|
||||
const preflightReason = `${item.fullStatus || ""} ${item.lastError || ""}`.trim();
|
||||
if (shouldPreflightFinalizeItemFromDisk(item)
|
||||
&& this.tryFinalizeItemFromDisk(pkg, item, "Start-Preflight", preflightReason)) {
|
||||
this.retryStateByItem.delete(item.id);
|
||||
this.refreshPackageStatus(pkg);
|
||||
this.persistSoon();
|
||||
return;
|
||||
}
|
||||
|
||||
item.status = "validating";
|
||||
item.fullStatus = "Link wird umgewandelt";
|
||||
item.speedBps = 0;
|
||||
@ -6126,8 +6186,15 @@ export class DownloadManager extends EventEmitter {
|
||||
const maxGenericErrorRetries = maxItemRetries;
|
||||
const maxUnrestrictRetries = maxItemRetries;
|
||||
const maxStallRetries = maxItemRetries;
|
||||
const maxHttp416Retries = configuredRetryLimit <= 0 ? 3 : Math.max(1, Math.min(maxItemRetries, 3));
|
||||
while (true) {
|
||||
try {
|
||||
const preflightReason = `${item.fullStatus || ""} ${item.lastError || ""}`.trim();
|
||||
if (shouldPreflightFinalizeItemFromDisk(item)
|
||||
&& this.tryFinalizeItemFromDisk(pkg, item, "Process-Preflight", preflightReason)) {
|
||||
this.retryStateByItem.delete(item.id);
|
||||
return;
|
||||
}
|
||||
this.logPackageForItem(item, "INFO", "Link-Umwandlung gestartet", {
|
||||
url: item.url,
|
||||
retryLimit: retryDisplayLimit
|
||||
@ -6562,6 +6629,12 @@ export class DownloadManager extends EventEmitter {
|
||||
const directLinkRetryMatch = errorText.match(/^(?:Error:\s*)?direct_link_retry_exhausted:(.+)$/);
|
||||
if (directLinkRetryMatch) {
|
||||
const exhaustedReason = compactErrorText(directLinkRetryMatch[1] || errorText).replace(/^Error:\s*/i, "");
|
||||
if (isHttp416Text(exhaustedReason) && active.genericErrorRetries < maxHttp416Retries) {
|
||||
this.scheduleHttp416Retry(item, active, retryDisplayLimit, exhaustedReason, claimedTargetPath);
|
||||
this.persistSoon();
|
||||
this.emitState();
|
||||
return;
|
||||
}
|
||||
if (isResumeHardResetReason(exhaustedReason) && !active.resumeHardResetUsed) {
|
||||
active.resumeHardResetUsed = true;
|
||||
item.retries += 1;
|
||||
@ -6608,20 +6681,14 @@ export class DownloadManager extends EventEmitter {
|
||||
return;
|
||||
}
|
||||
const shouldFreshRetry = !active.freshRetryUsed && isFetchFailure(errorText);
|
||||
const isHttp416 = /(^|\D)416(\D|$)/.test(errorText);
|
||||
const isHttp416 = isHttp416Text(errorText);
|
||||
if (isHttp416) {
|
||||
if (claimedTargetPath) {
|
||||
try {
|
||||
fs.rmSync(claimedTargetPath, { force: true });
|
||||
} catch {
|
||||
// ignore
|
||||
}
|
||||
if (active.genericErrorRetries < maxHttp416Retries) {
|
||||
this.scheduleHttp416Retry(item, active, retryDisplayLimit, errorText, claimedTargetPath);
|
||||
this.persistSoon();
|
||||
this.emitState();
|
||||
return;
|
||||
}
|
||||
this.releaseTargetPath(item.id);
|
||||
item.downloadedBytes = 0;
|
||||
item.totalBytes = null;
|
||||
item.progressPercent = 0;
|
||||
this.dropItemContribution(item.id);
|
||||
item.status = "failed";
|
||||
this.recordRunOutcome(item.id, "failed");
|
||||
item.lastError = errorText;
|
||||
@ -7633,6 +7700,7 @@ export class DownloadManager extends EventEmitter {
|
||||
|
||||
private async recoverRetryableItems(trigger: "startup" | "start"): Promise<number> {
|
||||
let recovered = 0;
|
||||
let finalized = 0;
|
||||
const touchedPackages = new Set<string>();
|
||||
const configuredRetryLimit = normalizeRetryLimit(this.settings.retryLimit);
|
||||
const maxAutoRetryFailures = retryLimitToMaxRetries(configuredRetryLimit);
|
||||
@ -7650,6 +7718,21 @@ export class DownloadManager extends EventEmitter {
|
||||
}
|
||||
// Only check failed or completed items — skip queued/cancelled to avoid
|
||||
// expensive fs.stat calls on hundreds of items (caused 5-10s freeze on start).
|
||||
const canFinalizeFromDisk = item.status === "failed"
|
||||
|| item.status === "completed"
|
||||
|| item.status === "queued"
|
||||
|| item.status === "reconnect_wait";
|
||||
if (canFinalizeFromDisk) {
|
||||
const recoveryReason = `${item.fullStatus || ""} ${item.lastError || ""}`.trim();
|
||||
if (shouldPreflightFinalizeItemFromDisk(item)
|
||||
&& this.tryFinalizeItemFromDisk(pkg, item, `Recovery-${trigger}`, recoveryReason)) {
|
||||
finalized += 1;
|
||||
touchedPackages.add(pkg.id);
|
||||
this.retryAfterByItem.delete(item.id);
|
||||
this.retryStateByItem.delete(item.id);
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (item.status !== "failed" && item.status !== "completed") {
|
||||
continue;
|
||||
}
|
||||
@ -7690,7 +7773,7 @@ export class DownloadManager extends EventEmitter {
|
||||
}
|
||||
}
|
||||
|
||||
if (recovered > 0) {
|
||||
if (recovered > 0 || finalized > 0) {
|
||||
for (const packageId of touchedPackages) {
|
||||
const pkg = this.session.packages[packageId];
|
||||
if (!pkg) {
|
||||
@ -7698,12 +7781,15 @@ export class DownloadManager extends EventEmitter {
|
||||
}
|
||||
this.refreshPackageStatus(pkg);
|
||||
}
|
||||
logger.warn(`Auto-Retry-Recovery (${trigger}): ${recovered} Item(s) wieder in Queue gesetzt`);
|
||||
logger.warn(
|
||||
`Auto-Retry-Recovery (${trigger}): ${recovered} Item(s) wieder in Queue gesetzt, ` +
|
||||
`${finalized} Item(s) direkt von Disk vervollstaendigt`
|
||||
);
|
||||
this.persistSoon();
|
||||
this.emitState();
|
||||
}
|
||||
|
||||
return recovered;
|
||||
return recovered + finalized;
|
||||
}
|
||||
|
||||
private queueItemForRetry(item: DownloadItem, options: { hardReset: boolean; reason: string }): void {
|
||||
|
||||
@ -726,4 +726,13 @@ describe("buildAutoRenameBaseNameFromFolders", () => {
|
||||
);
|
||||
expect(result).toBe("Carter.S02E01.GERMAN.DL.720p.HDTV.x264-MDGP");
|
||||
});
|
||||
|
||||
it("renames abbreviated source bupr.de.dl.web.7p-s01e03 via season folder", () => {
|
||||
const result = buildAutoRenameBaseNameFromFoldersWithOptions(
|
||||
["Burning.Promise.S01.GERMAN.DL.720p.WEB.H264-WvF"],
|
||||
"bupr.de.dl.web.7p-s01e03",
|
||||
{ forceEpisodeForSeasonFolder: true }
|
||||
);
|
||||
expect(result).toBe("Burning.Promise.S01E03.GERMAN.DL.720p.WEB.H264-WvF");
|
||||
});
|
||||
});
|
||||
|
||||
@ -534,6 +534,157 @@ describe("download manager", () => {
|
||||
expect(fs.statSync(item.targetPath).size).toBe(binary.length);
|
||||
});
|
||||
|
||||
it("completes queued full files during start preflight without unrestricting again", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
const binary = Buffer.alloc(192 * 1024, 17);
|
||||
const pkgDir = path.join(root, "downloads", "queued-complete");
|
||||
fs.mkdirSync(pkgDir, { recursive: true });
|
||||
const targetPath = path.join(pkgDir, "queued-complete.rar");
|
||||
fs.writeFileSync(targetPath, binary);
|
||||
let unrestrictCalls = 0;
|
||||
|
||||
globalThis.fetch = async (input: RequestInfo | URL, init?: RequestInit): Promise<Response> => {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
||||
if (url.includes("/unrestrict/link")) {
|
||||
unrestrictCalls += 1;
|
||||
throw new Error(`unexpected unrestrict ${url}`);
|
||||
}
|
||||
return originalFetch(input, init);
|
||||
};
|
||||
|
||||
const session = emptySession();
|
||||
const packageId = "queued-complete-pkg";
|
||||
const itemId = "queued-complete-item";
|
||||
const createdAt = Date.now() - 10_000;
|
||||
|
||||
session.packageOrder = [packageId];
|
||||
session.packages[packageId] = {
|
||||
id: packageId,
|
||||
name: "queued-complete",
|
||||
outputDir: pkgDir,
|
||||
extractDir: path.join(root, "extract", "queued-complete"),
|
||||
status: "queued",
|
||||
itemIds: [itemId],
|
||||
cancelled: false,
|
||||
enabled: true,
|
||||
createdAt,
|
||||
updatedAt: createdAt
|
||||
};
|
||||
session.items[itemId] = {
|
||||
id: itemId,
|
||||
packageId,
|
||||
url: "https://dummy/queued-complete",
|
||||
provider: "megadebrid-web",
|
||||
status: "queued",
|
||||
retries: 2,
|
||||
speedBps: 0,
|
||||
downloadedBytes: binary.length,
|
||||
totalBytes: binary.length,
|
||||
progressPercent: 100,
|
||||
fileName: "queued-complete.rar",
|
||||
targetPath,
|
||||
resumable: true,
|
||||
attempts: 0,
|
||||
lastError: "direct_link_retry_exhausted:HTTP 416",
|
||||
fullStatus: "Resume-Link erneuern, Retry 1/3",
|
||||
createdAt,
|
||||
updatedAt: createdAt
|
||||
};
|
||||
|
||||
const manager = new DownloadManager(
|
||||
{
|
||||
...defaultSettings(),
|
||||
token: "rd-token",
|
||||
outputDir: path.join(root, "downloads"),
|
||||
extractDir: path.join(root, "extract"),
|
||||
retryLimit: 2,
|
||||
autoExtract: false
|
||||
},
|
||||
session,
|
||||
createStoragePaths(path.join(root, "state"))
|
||||
);
|
||||
|
||||
await manager.start();
|
||||
await waitFor(() => !manager.getSnapshot().session.running, 12000);
|
||||
|
||||
const item = manager.getSnapshot().session.items[itemId];
|
||||
expect(item?.status).toBe("completed");
|
||||
expect(item?.progressPercent).toBe(100);
|
||||
expect(item?.downloadedBytes).toBe(binary.length);
|
||||
expect(item?.fullStatus).toContain("Fertig");
|
||||
expect(unrestrictCalls).toBe(0);
|
||||
});
|
||||
|
||||
it("retries direct-link exhaustion caused by HTTP 416 in-session and then completes", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
const binary = Buffer.alloc(160 * 1024, 41);
|
||||
let unrestrictCalls = 0;
|
||||
let downloadCalls = 0;
|
||||
|
||||
globalThis.fetch = async (input: RequestInfo | URL): Promise<Response> => {
|
||||
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
|
||||
if (url.includes("/unrestrict/link")) {
|
||||
unrestrictCalls += 1;
|
||||
return new Response(
|
||||
JSON.stringify({
|
||||
download: `https://dummy/direct-416-${unrestrictCalls}`,
|
||||
filename: "direct-416-retry.mkv",
|
||||
filesize: binary.length
|
||||
}),
|
||||
{
|
||||
status: 200,
|
||||
headers: { "Content-Type": "application/json" }
|
||||
}
|
||||
);
|
||||
}
|
||||
throw new Error(`unexpected fetch ${url}`);
|
||||
};
|
||||
|
||||
const manager = new DownloadManager(
|
||||
{
|
||||
...defaultSettings(),
|
||||
token: "rd-token",
|
||||
outputDir: path.join(root, "downloads"),
|
||||
extractDir: path.join(root, "extract"),
|
||||
retryLimit: 2,
|
||||
autoExtract: false
|
||||
},
|
||||
emptySession(),
|
||||
createStoragePaths(path.join(root, "state"))
|
||||
);
|
||||
|
||||
(manager as any).downloadToFile = async (_active: unknown, _directUrl: string, targetPath: string) => {
|
||||
downloadCalls += 1;
|
||||
if (downloadCalls === 1) {
|
||||
throw new Error("direct_link_retry_exhausted:HTTP 416");
|
||||
}
|
||||
fs.mkdirSync(path.dirname(targetPath), { recursive: true });
|
||||
fs.writeFileSync(targetPath, binary);
|
||||
const item = Object.values((manager as any).session.items)[0] as { downloadedBytes: number; totalBytes: number; progressPercent: number } | undefined;
|
||||
if (item) {
|
||||
item.downloadedBytes = binary.length;
|
||||
item.totalBytes = binary.length;
|
||||
item.progressPercent = 100;
|
||||
}
|
||||
return { resumable: true };
|
||||
};
|
||||
|
||||
manager.addPackages([{ name: "direct-416-retry", links: ["https://dummy/direct-416-retry"] }]);
|
||||
await manager.start();
|
||||
await waitFor(() => !manager.getSnapshot().session.running, 12000);
|
||||
|
||||
const item = Object.values(manager.getSnapshot().session.items)[0];
|
||||
expect(item?.status).toBe("completed");
|
||||
expect(item?.progressPercent).toBe(100);
|
||||
expect(item?.downloadedBytes).toBe(binary.length);
|
||||
expect(unrestrictCalls).toBe(2);
|
||||
expect(downloadCalls).toBe(2);
|
||||
expect(fs.existsSync(item.targetPath)).toBe(true);
|
||||
expect(fs.statSync(item.targetPath).size).toBe(binary.length);
|
||||
});
|
||||
|
||||
it("restarts from zero after repeated resume underflow on fresh direct links", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
@ -2214,7 +2365,7 @@ describe("download manager", () => {
|
||||
expect(snapshot.canStart).toBe(true);
|
||||
});
|
||||
|
||||
it("requeues failed HTTP 416 items automatically on startup", () => {
|
||||
it("requeues failed HTTP 416 items automatically on startup", async () => {
|
||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||
tempDirs.push(root);
|
||||
|
||||
@ -2273,6 +2424,8 @@ describe("download manager", () => {
|
||||
createStoragePaths(path.join(root, "state"))
|
||||
);
|
||||
|
||||
await waitFor(() => manager.getSnapshot().session.items[itemId]?.status === "queued", 12000);
|
||||
|
||||
const snapshot = manager.getSnapshot();
|
||||
const item = snapshot.session.items[itemId];
|
||||
expect(item?.status).toBe("queued");
|
||||
|
||||
Loading…
Reference in New Issue
Block a user