Compare commits
No commits in common. "8ab01f3da48131f074d04e4cbcd656748e0edd7f" and "6105a08728c8a8c31846df5978a1b25f4df55141" have entirely different histories.
8ab01f3da4
...
6105a08728
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "real-debrid-downloader",
|
"name": "real-debrid-downloader",
|
||||||
"version": "1.7.124",
|
"version": "1.7.123",
|
||||||
"description": "Desktop downloader",
|
"description": "Desktop downloader",
|
||||||
"main": "build/main/main/main.js",
|
"main": "build/main/main/main.js",
|
||||||
"author": "Sucukdeluxe",
|
"author": "Sucukdeluxe",
|
||||||
|
|||||||
@ -564,15 +564,11 @@ export class AppController {
|
|||||||
itemCount: Object.keys(this.manager.getSnapshot().session.items).length
|
itemCount: Object.keys(this.manager.getSnapshot().session.items).length
|
||||||
});
|
});
|
||||||
return {
|
return {
|
||||||
buffer: buildSupportBundle(this.manager, this.storagePaths.baseDir, { hostDiagnosticsMode: "cached" }),
|
buffer: buildSupportBundle(this.manager, this.storagePaths.baseDir),
|
||||||
defaultFileName: getSupportBundleDefaultFileName()
|
defaultFileName: getSupportBundleDefaultFileName()
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSupportBundleDefaultFileName(): string {
|
|
||||||
return getSupportBundleDefaultFileName();
|
|
||||||
}
|
|
||||||
|
|
||||||
public importBackup(data: Buffer): { restored: boolean; message: string } {
|
public importBackup(data: Buffer): { restored: boolean; message: string } {
|
||||||
let parsed: Record<string, unknown>;
|
let parsed: Record<string, unknown>;
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -127,8 +127,6 @@ const RESUME_REWIND_BYTES = 256 * 1024;
|
|||||||
|
|
||||||
const REALDEBRID_TOTAL_MISMATCH_TOLERANCE_BYTES = 64 * 1024;
|
const REALDEBRID_TOTAL_MISMATCH_TOLERANCE_BYTES = 64 * 1024;
|
||||||
|
|
||||||
const PREALLOC_RESUME_MISMATCH_THRESHOLD_BYTES = 1024 * 1024;
|
|
||||||
|
|
||||||
const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?:\.\d+)?|tar|gz|bz2|xz|iso|mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm|mp3|flac|aac|wav)$/i;
|
const LARGE_BINARY_FILE_RE = /\.(?:part\d+\.rar|rar|r\d{2,3}|zip(?:\.\d+)?|7z(?:\.\d+)?|tar|gz|bz2|xz|iso|mkv|mp4|avi|mov|wmv|m4v|ts|m2ts|webm|mp3|flac|aac|wav)$/i;
|
||||||
|
|
||||||
function expectedMinBytes(totalBytes: number | null | undefined, strict: boolean): number {
|
function expectedMinBytes(totalBytes: number | null | undefined, strict: boolean): number {
|
||||||
@ -143,12 +141,6 @@ function itemExpectedMinBytes(item: DownloadItem): number {
|
|||||||
return expectedMinBytes(item.totalBytes, strict);
|
return expectedMinBytes(item.totalBytes, strict);
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolvePreallocResumeMismatchThreshold(pathHint: string): number {
|
|
||||||
return isLargeBinaryLikePath(pathHint)
|
|
||||||
? 0
|
|
||||||
: PREALLOC_RESUME_MISMATCH_THRESHOLD_BYTES;
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolvePackageItemDiskPath(pkg: PackageEntry, item: DownloadItem): string | null {
|
function resolvePackageItemDiskPath(pkg: PackageEntry, item: DownloadItem): string | null {
|
||||||
if (item.targetPath) {
|
if (item.targetPath) {
|
||||||
return item.targetPath;
|
return item.targetPath;
|
||||||
@ -8487,37 +8479,15 @@ export class DownloadManager extends EventEmitter {
|
|||||||
} else if (resumeRewindBytesNextAttempt > 0) {
|
} else if (resumeRewindBytesNextAttempt > 0) {
|
||||||
resumeRewindBytesNextAttempt = 0;
|
resumeRewindBytesNextAttempt = 0;
|
||||||
}
|
}
|
||||||
const persistedBytes = Math.max(0, Math.floor(Number(item.downloadedBytes) || 0));
|
|
||||||
const preallocMismatchThreshold = resolvePreallocResumeMismatchThreshold(item.fileName || effectiveTargetPath || "");
|
|
||||||
// Guard against pre-allocated sparse files from a crashed session:
|
// Guard against pre-allocated sparse files from a crashed session:
|
||||||
// if file size exceeds persisted downloadedBytes beyond the allowed
|
// if file size exceeds persisted downloadedBytes by >1MB, the file was
|
||||||
// mismatch threshold, the file was likely pre-allocated but only
|
// likely pre-allocated but only partially written before a hard crash.
|
||||||
// partially written before a hard crash.
|
if (existingBytes > 0 && item.downloadedBytes > 0 && existingBytes > item.downloadedBytes + 1048576) {
|
||||||
// This must also run for persistedBytes=0, otherwise startup-resume can
|
|
||||||
// send Range=full-size and incorrectly accept HTTP 416 as "complete".
|
|
||||||
if (existingBytes > 0 && existingBytes > persistedBytes + preallocMismatchThreshold) {
|
|
||||||
try {
|
try {
|
||||||
const previousBytes = existingBytes;
|
await fs.promises.truncate(effectiveTargetPath, item.downloadedBytes);
|
||||||
await fs.promises.truncate(effectiveTargetPath, persistedBytes);
|
existingBytes = item.downloadedBytes;
|
||||||
existingBytes = persistedBytes;
|
} catch { /* best-effort */ }
|
||||||
logAttemptEvent("WARN", "Pre-alloc-Rest erkannt, Teil-Datei auf persistierte Bytes gekuerzt", {
|
|
||||||
attempt,
|
|
||||||
previousBytes,
|
|
||||||
persistedBytes
|
|
||||||
});
|
|
||||||
} catch {
|
|
||||||
if (persistedBytes === 0) {
|
|
||||||
try {
|
|
||||||
await fs.promises.rm(effectiveTargetPath, { force: true });
|
|
||||||
existingBytes = 0;
|
|
||||||
} catch {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const suspiciousResumeFootprint = existingBytes > 0
|
|
||||||
&& existingBytes > persistedBytes + preallocMismatchThreshold;
|
|
||||||
const headers: Record<string, string> = {};
|
const headers: Record<string, string> = {};
|
||||||
if (existingBytes > 0) {
|
if (existingBytes > 0) {
|
||||||
headers.Range = `bytes=${existingBytes}-`;
|
headers.Range = `bytes=${existingBytes}-`;
|
||||||
@ -8595,7 +8565,7 @@ export class DownloadManager extends EventEmitter {
|
|||||||
const sizeToleranceBytes = isLargeBinaryLikePath(item.fileName || effectiveTargetPath) ? 0 : ALLOCATION_UNIT_SIZE;
|
const sizeToleranceBytes = isLargeBinaryLikePath(item.fileName || effectiveTargetPath) ? 0 : ALLOCATION_UNIT_SIZE;
|
||||||
const closeEnoughToExpected = expectedTotal != null
|
const closeEnoughToExpected = expectedTotal != null
|
||||||
&& Math.abs(existingBytes - expectedTotal) <= sizeToleranceBytes;
|
&& Math.abs(existingBytes - expectedTotal) <= sizeToleranceBytes;
|
||||||
if (expectedTotal != null && closeEnoughToExpected && !suspiciousResumeFootprint) {
|
if (expectedTotal != null && closeEnoughToExpected) {
|
||||||
const finalizedTotal = Math.max(existingBytes, expectedTotal);
|
const finalizedTotal = Math.max(existingBytes, expectedTotal);
|
||||||
item.totalBytes = finalizedTotal;
|
item.totalBytes = finalizedTotal;
|
||||||
item.downloadedBytes = existingBytes;
|
item.downloadedBytes = existingBytes;
|
||||||
@ -8608,14 +8578,6 @@ export class DownloadManager extends EventEmitter {
|
|||||||
});
|
});
|
||||||
return { resumable: true };
|
return { resumable: true };
|
||||||
}
|
}
|
||||||
if (expectedTotal != null && closeEnoughToExpected && suspiciousResumeFootprint) {
|
|
||||||
logAttemptEvent("WARN", "HTTP 416 trotz Vollgroesse nicht als fertig gewertet (vermutlich pre-alloc)", {
|
|
||||||
attempt,
|
|
||||||
existingBytes,
|
|
||||||
persistedBytes,
|
|
||||||
expectedTotal
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await fs.promises.rm(effectiveTargetPath, { force: true });
|
await fs.promises.rm(effectiveTargetPath, { force: true });
|
||||||
@ -10501,12 +10463,6 @@ export class DownloadManager extends EventEmitter {
|
|||||||
// expected size. The old 50% threshold incorrectly recovered partial downloads
|
// expected size. The old 50% threshold incorrectly recovered partial downloads
|
||||||
// (e.g. 627 MB of 1001 MB) and triggered hybrid extraction on incomplete archives.
|
// (e.g. 627 MB of 1001 MB) and triggered hybrid extraction on incomplete archives.
|
||||||
const minSize = expectedMinBytes(item.totalBytes, isLargeBinaryLikePath(item.fileName || item.targetPath));
|
const minSize = expectedMinBytes(item.totalBytes, isLargeBinaryLikePath(item.fileName || item.targetPath));
|
||||||
const persistedBytes = Math.max(0, Math.floor(Number(item.downloadedBytes) || 0));
|
|
||||||
const preallocMismatchThreshold = resolvePreallocResumeMismatchThreshold(item.fileName || item.targetPath || "");
|
|
||||||
const suspiciousPreallocFootprint = item.totalBytes != null
|
|
||||||
&& item.totalBytes > 0
|
|
||||||
&& stat.size >= minSize
|
|
||||||
&& stat.size > persistedBytes + preallocMismatchThreshold;
|
|
||||||
if (stat.size >= minSize) {
|
if (stat.size >= minSize) {
|
||||||
// Re-check: another task may have started this item during the await
|
// Re-check: another task may have started this item during the await
|
||||||
const latestItem = this.session.items[item.id];
|
const latestItem = this.session.items[item.id];
|
||||||
@ -10514,31 +10470,6 @@ export class DownloadManager extends EventEmitter {
|
|||||||
|| latestItem.status === "validating" || latestItem.status === "integrity_check") {
|
|| latestItem.status === "validating" || latestItem.status === "integrity_check") {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if (suspiciousPreallocFootprint) {
|
|
||||||
logger.warn(
|
|
||||||
`Item-Recovery: ${item.fileName} uebersprungen – pre-alloc-Verdacht ` +
|
|
||||||
`(stat=${humanSize(stat.size)}, bytes=${humanSize(persistedBytes)}, total=${humanSize(item.totalBytes)})`
|
|
||||||
);
|
|
||||||
try {
|
|
||||||
if (persistedBytes > 0) {
|
|
||||||
fs.truncateSync(item.targetPath, persistedBytes);
|
|
||||||
} else {
|
|
||||||
fs.rmSync(item.targetPath, { force: true });
|
|
||||||
}
|
|
||||||
} catch {
|
|
||||||
// best-effort
|
|
||||||
}
|
|
||||||
item.status = "queued";
|
|
||||||
item.attempts = 0;
|
|
||||||
item.downloadedBytes = persistedBytes;
|
|
||||||
item.progressPercent = item.totalBytes > 0
|
|
||||||
? Math.max(0, Math.min(99, Math.floor((persistedBytes / item.totalBytes) * 100)))
|
|
||||||
: 0;
|
|
||||||
item.speedBps = 0;
|
|
||||||
item.fullStatus = "Wartet (Auto-Recovery: pre-alloc)";
|
|
||||||
item.updatedAt = nowMs();
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
// Guard against pre-allocated sparse files from a hard crash: file has
|
// Guard against pre-allocated sparse files from a hard crash: file has
|
||||||
// the full expected size but downloadedBytes is significantly behind.
|
// the full expected size but downloadedBytes is significantly behind.
|
||||||
if (item.downloadedBytes > 0 && item.totalBytes && item.totalBytes > 0
|
if (item.downloadedBytes > 0 && item.totalBytes && item.totalBytes > 0
|
||||||
|
|||||||
@ -525,15 +525,15 @@ function registerIpcHandlers(): void {
|
|||||||
});
|
});
|
||||||
|
|
||||||
ipcMain.handle(IPC_CHANNELS.EXPORT_SUPPORT_BUNDLE, async () => {
|
ipcMain.handle(IPC_CHANNELS.EXPORT_SUPPORT_BUNDLE, async () => {
|
||||||
|
const exported = controller.exportSupportBundle();
|
||||||
const options = {
|
const options = {
|
||||||
defaultPath: controller.getSupportBundleDefaultFileName(),
|
defaultPath: exported.defaultFileName,
|
||||||
filters: [{ name: "Support Bundle", extensions: ["zip"] }]
|
filters: [{ name: "Support Bundle", extensions: ["zip"] }]
|
||||||
};
|
};
|
||||||
const result = mainWindow ? await dialog.showSaveDialog(mainWindow, options) : await dialog.showSaveDialog(options);
|
const result = mainWindow ? await dialog.showSaveDialog(mainWindow, options) : await dialog.showSaveDialog(options);
|
||||||
if (result.canceled || !result.filePath) {
|
if (result.canceled || !result.filePath) {
|
||||||
return { saved: false };
|
return { saved: false };
|
||||||
}
|
}
|
||||||
const exported = controller.exportSupportBundle();
|
|
||||||
await fs.promises.writeFile(result.filePath, exported.buffer);
|
await fs.promises.writeFile(result.filePath, exported.buffer);
|
||||||
return { saved: true, filePath: result.filePath };
|
return { saved: true, filePath: result.filePath };
|
||||||
});
|
});
|
||||||
|
|||||||
@ -11,7 +11,7 @@ import { getSessionLogPath } from "./session-log";
|
|||||||
import { createStoragePaths, loadHistory, loadSettings } from "./storage";
|
import { createStoragePaths, loadHistory, loadSettings } from "./storage";
|
||||||
import { buildAccountSummary, buildRedactedSettingsPayload, buildStatsPayload, summarizeHistoryEntry } from "./support-data";
|
import { buildAccountSummary, buildRedactedSettingsPayload, buildStatsPayload, summarizeHistoryEntry } from "./support-data";
|
||||||
import { getTraceConfig, getTraceConfigPath, getTraceLogPath } from "./trace-log";
|
import { getTraceConfig, getTraceConfigPath, getTraceLogPath } from "./trace-log";
|
||||||
import { getCachedWindowsHostDiagnostics, getWindowsHostDiagnostics } from "./windows-host-diagnostics";
|
import { getWindowsHostDiagnostics } from "./windows-host-diagnostics";
|
||||||
import type { DownloadManager } from "./download-manager";
|
import type { DownloadManager } from "./download-manager";
|
||||||
|
|
||||||
const AI_MANIFEST_FILE = "debug_ai_manifest.json";
|
const AI_MANIFEST_FILE = "debug_ai_manifest.json";
|
||||||
@ -65,47 +65,8 @@ export function getSupportBundleDefaultFileName(): string {
|
|||||||
return `rd-support-bundle-${formatTimestampForFileName(new Date())}.zip`;
|
return `rd-support-bundle-${formatTimestampForFileName(new Date())}.zip`;
|
||||||
}
|
}
|
||||||
|
|
||||||
type HostDiagnosticsMode = "full" | "cached" | "none";
|
export function buildSupportBundle(manager: DownloadManager, baseDir: string): Buffer {
|
||||||
|
|
||||||
interface BuildSupportBundleOptions {
|
|
||||||
hostDiagnosticsMode?: HostDiagnosticsMode;
|
|
||||||
}
|
|
||||||
|
|
||||||
function createDeferredHostDiagnostics(reason: string): unknown {
|
|
||||||
return {
|
|
||||||
collectedAt: new Date().toISOString(),
|
|
||||||
supported: process.platform === "win32",
|
|
||||||
platform: process.platform,
|
|
||||||
crashControl: null,
|
|
||||||
recentKernelPower: [],
|
|
||||||
recentWerKernel: [],
|
|
||||||
recentKernelDump: [],
|
|
||||||
recentAppCrashes: [],
|
|
||||||
recentMinidumps: [],
|
|
||||||
assessmentHints: [
|
|
||||||
reason
|
|
||||||
],
|
|
||||||
errors: []
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
function resolveHostDiagnostics(mode: HostDiagnosticsMode): unknown {
|
|
||||||
if (mode === "none") {
|
|
||||||
return createDeferredHostDiagnostics("Host-Diagnose wurde fuer diesen Bundle-Export deaktiviert.");
|
|
||||||
}
|
|
||||||
if (mode === "cached") {
|
|
||||||
const cached = getCachedWindowsHostDiagnostics();
|
|
||||||
if (cached) {
|
|
||||||
return cached;
|
|
||||||
}
|
|
||||||
return createDeferredHostDiagnostics("Host-Diagnose wurde uebersprungen, um den Export nicht zu blockieren. Fuer eine Voll-Diagnose /host/diagnostics nutzen.");
|
|
||||||
}
|
|
||||||
return getWindowsHostDiagnostics();
|
|
||||||
}
|
|
||||||
|
|
||||||
export function buildSupportBundle(manager: DownloadManager, baseDir: string, options: BuildSupportBundleOptions = {}): Buffer {
|
|
||||||
const zip = new AdmZip();
|
const zip = new AdmZip();
|
||||||
const hostDiagnosticsMode = options.hostDiagnosticsMode || "full";
|
|
||||||
const storagePaths = createStoragePaths(baseDir);
|
const storagePaths = createStoragePaths(baseDir);
|
||||||
const settings = loadSettings(storagePaths);
|
const settings = loadSettings(storagePaths);
|
||||||
const history = loadHistory(storagePaths);
|
const history = loadHistory(storagePaths);
|
||||||
@ -146,7 +107,7 @@ export function buildSupportBundle(manager: DownloadManager, baseDir: string, op
|
|||||||
count: itemIds.length,
|
count: itemIds.length,
|
||||||
items: itemIds.map((itemId) => snapshot.session.items[itemId]).filter(Boolean)
|
items: itemIds.map((itemId) => snapshot.session.items[itemId]).filter(Boolean)
|
||||||
});
|
});
|
||||||
addJson(zip, "overview/host-diagnostics.json", resolveHostDiagnostics(hostDiagnosticsMode));
|
addJson(zip, "overview/host-diagnostics.json", getWindowsHostDiagnostics());
|
||||||
addJson(zip, "overview/trace-config.json", getTraceConfig());
|
addJson(zip, "overview/trace-config.json", getTraceConfig());
|
||||||
|
|
||||||
addFileIfExists(zip, path.join(baseDir, AI_MANIFEST_FILE), `runtime/${AI_MANIFEST_FILE}`);
|
addFileIfExists(zip, path.join(baseDir, AI_MANIFEST_FILE), `runtime/${AI_MANIFEST_FILE}`);
|
||||||
|
|||||||
@ -302,10 +302,6 @@ export function getWindowsHostDiagnostics(forceRefresh = false): WindowsHostDiag
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function getCachedWindowsHostDiagnostics(): WindowsHostDiagnostics | null {
|
|
||||||
return cachedValue;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resetWindowsHostDiagnosticsCache(): void {
|
export function resetWindowsHostDiagnosticsCache(): void {
|
||||||
cachedAt = 0;
|
cachedAt = 0;
|
||||||
cachedValue = null;
|
cachedValue = null;
|
||||||
|
|||||||
@ -4205,76 +4205,6 @@ describe("download manager", () => {
|
|||||||
expect(snapshot.session.packages[packageId]?.status).toBe("queued");
|
expect(snapshot.session.packages[packageId]?.status).toBe("queued");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not recover queued pre-allocated archive leftovers as completed during post-processing", async () => {
|
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
|
||||||
tempDirs.push(root);
|
|
||||||
|
|
||||||
const session = emptySession();
|
|
||||||
const packageId = "postproc-prealloc-pkg";
|
|
||||||
const itemId = "postproc-prealloc-item";
|
|
||||||
const createdAt = Date.now() - 20_000;
|
|
||||||
const outputDir = path.join(root, "downloads", "postproc-prealloc");
|
|
||||||
const targetPath = path.join(outputDir, "postproc-prealloc.part01.rar");
|
|
||||||
const totalBytes = 2 * 1024 * 1024;
|
|
||||||
fs.mkdirSync(outputDir, { recursive: true });
|
|
||||||
fs.writeFileSync(targetPath, Buffer.alloc(totalBytes, 0));
|
|
||||||
|
|
||||||
session.packageOrder = [packageId];
|
|
||||||
session.packages[packageId] = {
|
|
||||||
id: packageId,
|
|
||||||
name: "postproc-prealloc",
|
|
||||||
outputDir,
|
|
||||||
extractDir: path.join(root, "extract", "postproc-prealloc"),
|
|
||||||
status: "queued",
|
|
||||||
itemIds: [itemId],
|
|
||||||
cancelled: false,
|
|
||||||
enabled: true,
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
session.items[itemId] = {
|
|
||||||
id: itemId,
|
|
||||||
packageId,
|
|
||||||
url: "https://dummy/postproc-prealloc",
|
|
||||||
provider: "realdebrid",
|
|
||||||
status: "queued",
|
|
||||||
retries: 0,
|
|
||||||
speedBps: 0,
|
|
||||||
downloadedBytes: 0,
|
|
||||||
totalBytes,
|
|
||||||
progressPercent: 0,
|
|
||||||
fileName: "postproc-prealloc.part01.rar",
|
|
||||||
targetPath,
|
|
||||||
resumable: true,
|
|
||||||
attempts: 1,
|
|
||||||
lastError: "",
|
|
||||||
fullStatus: "Wartet",
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
|
|
||||||
const manager = new DownloadManager(
|
|
||||||
{
|
|
||||||
...defaultSettings(),
|
|
||||||
token: "rd-token",
|
|
||||||
outputDir: path.join(root, "downloads"),
|
|
||||||
extractDir: path.join(root, "extract"),
|
|
||||||
autoExtract: false
|
|
||||||
},
|
|
||||||
session,
|
|
||||||
createStoragePaths(path.join(root, "state"))
|
|
||||||
);
|
|
||||||
|
|
||||||
await (manager as any).handlePackagePostProcessing(packageId);
|
|
||||||
const snapshot = manager.getSnapshot();
|
|
||||||
const item = snapshot.session.items[itemId];
|
|
||||||
expect(item?.status).toBe("queued");
|
|
||||||
expect(item?.fullStatus).toContain("pre-alloc");
|
|
||||||
expect(item?.downloadedBytes).toBe(0);
|
|
||||||
expect(item?.progressPercent).toBe(0);
|
|
||||||
expect(fs.existsSync(targetPath)).toBe(false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("requeues completed archive parts after auto-recovery extraction failures", () => {
|
it("requeues completed archive parts after auto-recovery extraction failures", () => {
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||||
tempDirs.push(root);
|
tempDirs.push(root);
|
||||||
@ -7788,292 +7718,6 @@ describe("download manager", () => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
it("does not mark pre-allocated crash leftovers as 100% complete on resume", async () => {
|
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
|
||||||
tempDirs.push(root);
|
|
||||||
const binary = crypto.randomBytes(2 * 1024 * 1024);
|
|
||||||
const outputDir = path.join(root, "downloads", "resume-prealloc");
|
|
||||||
const targetPath = path.join(outputDir, "resume-prealloc.part01.rar");
|
|
||||||
fs.mkdirSync(outputDir, { recursive: true });
|
|
||||||
fs.writeFileSync(targetPath, Buffer.alloc(binary.length, 0));
|
|
||||||
|
|
||||||
const session = emptySession();
|
|
||||||
const packageId = "resume-prealloc-pkg";
|
|
||||||
const itemId = "resume-prealloc-item";
|
|
||||||
const createdAt = Date.now() - 20_000;
|
|
||||||
session.packageOrder = [packageId];
|
|
||||||
session.packages[packageId] = {
|
|
||||||
id: packageId,
|
|
||||||
name: "resume-prealloc",
|
|
||||||
outputDir,
|
|
||||||
extractDir: path.join(root, "extract", "resume-prealloc"),
|
|
||||||
status: "queued",
|
|
||||||
itemIds: [itemId],
|
|
||||||
cancelled: false,
|
|
||||||
enabled: true,
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
session.items[itemId] = {
|
|
||||||
id: itemId,
|
|
||||||
packageId,
|
|
||||||
url: "https://dummy/resume-prealloc",
|
|
||||||
provider: "realdebrid",
|
|
||||||
status: "queued",
|
|
||||||
retries: 0,
|
|
||||||
speedBps: 0,
|
|
||||||
downloadedBytes: 0,
|
|
||||||
totalBytes: binary.length,
|
|
||||||
progressPercent: 0,
|
|
||||||
fileName: "resume-prealloc.part01.rar",
|
|
||||||
targetPath,
|
|
||||||
resumable: true,
|
|
||||||
attempts: 1,
|
|
||||||
lastError: "",
|
|
||||||
fullStatus: "Wartet",
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
|
|
||||||
const rangeStarts: number[] = [];
|
|
||||||
let sawRangeAtFullSize = false;
|
|
||||||
const server = http.createServer((req, res) => {
|
|
||||||
if ((req.url || "") !== "/resume-prealloc") {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end("not-found");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rangeHeader = String(req.headers.range || "");
|
|
||||||
const match = /bytes=(\d+)-/i.exec(rangeHeader);
|
|
||||||
if (match) {
|
|
||||||
const start = Number(match[1] || 0);
|
|
||||||
rangeStarts.push(start);
|
|
||||||
if (start >= binary.length) {
|
|
||||||
sawRangeAtFullSize = true;
|
|
||||||
res.statusCode = 416;
|
|
||||||
res.setHeader("Content-Range", `bytes */${binary.length}`);
|
|
||||||
res.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const end = binary.length - 1;
|
|
||||||
res.statusCode = 206;
|
|
||||||
res.setHeader("Accept-Ranges", "bytes");
|
|
||||||
res.setHeader("Content-Range", `bytes ${start}-${end}/${binary.length}`);
|
|
||||||
res.setHeader("Content-Length", String(binary.length - start));
|
|
||||||
res.end(binary.subarray(start));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.statusCode = 200;
|
|
||||||
res.setHeader("Accept-Ranges", "bytes");
|
|
||||||
res.setHeader("Content-Length", String(binary.length));
|
|
||||||
res.end(binary);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(0, "127.0.0.1");
|
|
||||||
await once(server, "listening");
|
|
||||||
|
|
||||||
const address = server.address();
|
|
||||||
if (!address || typeof address === "string") {
|
|
||||||
throw new Error("server address unavailable");
|
|
||||||
}
|
|
||||||
const directUrl = `http://127.0.0.1:${address.port}/resume-prealloc`;
|
|
||||||
|
|
||||||
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")) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
download: directUrl,
|
|
||||||
filename: "resume-prealloc.part01.rar",
|
|
||||||
filesize: binary.length
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: { "Content-Type": "application/json" }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return originalFetch(input, init);
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const manager = new DownloadManager(
|
|
||||||
{
|
|
||||||
...defaultSettings(),
|
|
||||||
token: "rd-token",
|
|
||||||
outputDir: path.join(root, "downloads"),
|
|
||||||
extractDir: path.join(root, "extract"),
|
|
||||||
autoExtract: false,
|
|
||||||
maxParallel: 1
|
|
||||||
},
|
|
||||||
session,
|
|
||||||
createStoragePaths(path.join(root, "state"))
|
|
||||||
);
|
|
||||||
|
|
||||||
await manager.start();
|
|
||||||
await waitFor(() => !manager.getSnapshot().session.running, 30000);
|
|
||||||
|
|
||||||
const item = manager.getSnapshot().session.items[itemId];
|
|
||||||
expect(item?.status).toBe("completed");
|
|
||||||
expect(item?.downloadedBytes).toBe(binary.length);
|
|
||||||
expect(item?.progressPercent).toBe(100);
|
|
||||||
expect(sawRangeAtFullSize).toBe(false);
|
|
||||||
expect(rangeStarts).not.toContain(binary.length);
|
|
||||||
expect(fs.readFileSync(targetPath).equals(binary)).toBe(true);
|
|
||||||
} finally {
|
|
||||||
server.close();
|
|
||||||
await once(server, "close");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("resumes archive tails when persisted bytes lag slightly behind pre-allocated file size", async () => {
|
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
|
||||||
tempDirs.push(root);
|
|
||||||
const binary = crypto.randomBytes(2 * 1024 * 1024);
|
|
||||||
const persistedBytes = binary.length - (256 * 1024);
|
|
||||||
const outputDir = path.join(root, "downloads", "resume-prealloc-tail");
|
|
||||||
const targetPath = path.join(outputDir, "resume-prealloc-tail.part01.rar");
|
|
||||||
fs.mkdirSync(outputDir, { recursive: true });
|
|
||||||
const preallocated = Buffer.concat([
|
|
||||||
binary.subarray(0, persistedBytes),
|
|
||||||
Buffer.alloc(binary.length - persistedBytes, 0)
|
|
||||||
]);
|
|
||||||
fs.writeFileSync(targetPath, preallocated);
|
|
||||||
|
|
||||||
const session = emptySession();
|
|
||||||
const packageId = "resume-prealloc-tail-pkg";
|
|
||||||
const itemId = "resume-prealloc-tail-item";
|
|
||||||
const createdAt = Date.now() - 20_000;
|
|
||||||
session.packageOrder = [packageId];
|
|
||||||
session.packages[packageId] = {
|
|
||||||
id: packageId,
|
|
||||||
name: "resume-prealloc-tail",
|
|
||||||
outputDir,
|
|
||||||
extractDir: path.join(root, "extract", "resume-prealloc-tail"),
|
|
||||||
status: "queued",
|
|
||||||
itemIds: [itemId],
|
|
||||||
cancelled: false,
|
|
||||||
enabled: true,
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
session.items[itemId] = {
|
|
||||||
id: itemId,
|
|
||||||
packageId,
|
|
||||||
url: "https://dummy/resume-prealloc-tail",
|
|
||||||
provider: "realdebrid",
|
|
||||||
status: "queued",
|
|
||||||
retries: 0,
|
|
||||||
speedBps: 0,
|
|
||||||
downloadedBytes: persistedBytes,
|
|
||||||
totalBytes: binary.length,
|
|
||||||
progressPercent: Math.floor((persistedBytes / binary.length) * 100),
|
|
||||||
fileName: "resume-prealloc-tail.part01.rar",
|
|
||||||
targetPath,
|
|
||||||
resumable: true,
|
|
||||||
attempts: 1,
|
|
||||||
lastError: "",
|
|
||||||
fullStatus: "Wartet",
|
|
||||||
createdAt,
|
|
||||||
updatedAt: createdAt
|
|
||||||
};
|
|
||||||
|
|
||||||
const rangeStarts: number[] = [];
|
|
||||||
let sawRangeAtFullSize = false;
|
|
||||||
const server = http.createServer((req, res) => {
|
|
||||||
if ((req.url || "") !== "/resume-prealloc-tail") {
|
|
||||||
res.statusCode = 404;
|
|
||||||
res.end("not-found");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const rangeHeader = String(req.headers.range || "");
|
|
||||||
const match = /bytes=(\d+)-/i.exec(rangeHeader);
|
|
||||||
if (match) {
|
|
||||||
const start = Number(match[1] || 0);
|
|
||||||
rangeStarts.push(start);
|
|
||||||
if (start >= binary.length) {
|
|
||||||
sawRangeAtFullSize = true;
|
|
||||||
res.statusCode = 416;
|
|
||||||
res.setHeader("Content-Range", `bytes */${binary.length}`);
|
|
||||||
res.end();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const end = binary.length - 1;
|
|
||||||
res.statusCode = 206;
|
|
||||||
res.setHeader("Accept-Ranges", "bytes");
|
|
||||||
res.setHeader("Content-Range", `bytes ${start}-${end}/${binary.length}`);
|
|
||||||
res.setHeader("Content-Length", String(binary.length - start));
|
|
||||||
res.end(binary.subarray(start));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
res.statusCode = 200;
|
|
||||||
res.setHeader("Accept-Ranges", "bytes");
|
|
||||||
res.setHeader("Content-Length", String(binary.length));
|
|
||||||
res.end(binary);
|
|
||||||
});
|
|
||||||
|
|
||||||
server.listen(0, "127.0.0.1");
|
|
||||||
await once(server, "listening");
|
|
||||||
|
|
||||||
const address = server.address();
|
|
||||||
if (!address || typeof address === "string") {
|
|
||||||
throw new Error("server address unavailable");
|
|
||||||
}
|
|
||||||
const directUrl = `http://127.0.0.1:${address.port}/resume-prealloc-tail`;
|
|
||||||
|
|
||||||
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")) {
|
|
||||||
return new Response(
|
|
||||||
JSON.stringify({
|
|
||||||
download: directUrl,
|
|
||||||
filename: "resume-prealloc-tail.part01.rar",
|
|
||||||
filesize: binary.length
|
|
||||||
}),
|
|
||||||
{
|
|
||||||
status: 200,
|
|
||||||
headers: { "Content-Type": "application/json" }
|
|
||||||
}
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return originalFetch(input, init);
|
|
||||||
};
|
|
||||||
|
|
||||||
try {
|
|
||||||
const manager = new DownloadManager(
|
|
||||||
{
|
|
||||||
...defaultSettings(),
|
|
||||||
token: "rd-token",
|
|
||||||
outputDir: path.join(root, "downloads"),
|
|
||||||
extractDir: path.join(root, "extract"),
|
|
||||||
autoExtract: false,
|
|
||||||
maxParallel: 1
|
|
||||||
},
|
|
||||||
session,
|
|
||||||
createStoragePaths(path.join(root, "state"))
|
|
||||||
);
|
|
||||||
|
|
||||||
await manager.start();
|
|
||||||
await waitFor(() => !manager.getSnapshot().session.running, 30000);
|
|
||||||
|
|
||||||
const item = manager.getSnapshot().session.items[itemId];
|
|
||||||
expect(item?.status).toBe("completed");
|
|
||||||
expect(item?.downloadedBytes).toBe(binary.length);
|
|
||||||
expect(item?.progressPercent).toBe(100);
|
|
||||||
expect(sawRangeAtFullSize).toBe(false);
|
|
||||||
expect(rangeStarts).not.toContain(binary.length);
|
|
||||||
expect(rangeStarts.some((start) => start === persistedBytes)).toBe(true);
|
|
||||||
expect(fs.readFileSync(targetPath).equals(binary)).toBe(true);
|
|
||||||
} finally {
|
|
||||||
server.close();
|
|
||||||
await once(server, "close");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
it("marks extracting items as resumable extraction on shutdown", () => {
|
it("marks extracting items as resumable extraction on shutdown", () => {
|
||||||
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
|
||||||
tempDirs.push(root);
|
tempDirs.push(root);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user