Harden Debrid-Link cooldown and quota handling

This commit is contained in:
Sucukdeluxe
2026-03-08 20:30:33 +01:00
parent fa0f85acb0
commit 6a0079f9d0
5 changed files with 274 additions and 127 deletions
+90
View File
@@ -424,6 +424,41 @@ describe("debrid service", () => {
expect(authHeaders).toEqual(["Bearer dl-key-one"]);
});
it("returns a cooldown marker when all Debrid-Link keys are temporarily cooling down", async () => {
const settings = {
...defaultSettings(),
debridLinkApiKeys: "dl-key-one\ndl-key-two",
providerOrder: ["debridlink"] as const,
providerPrimary: "debridlink" as const,
providerSecondary: "none" as const,
providerTertiary: "none" as const,
autoProviderFallback: true
};
let addCalls = 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("debrid-link.com/api/v2/downloader/add")) {
return new Response("not-found", { status: 404 });
}
addCalls += 1;
return new Response(JSON.stringify({
success: false,
error: "floodDetected",
error_description: "too many requests"
}), {
status: 403,
headers: { "Content-Type": "application/json" }
});
}) as typeof fetch;
const service = new DebridService(settings);
await expect(service.unrestrictLink("https://hoster.example/cooldown.bin")).rejects.toThrow("API-Rate-Limit erreicht");
await expect(service.unrestrictLink("https://hoster.example/cooldown.bin")).rejects.toThrow(/debrid_link_cooldown:\d+:/i);
expect(addCalls).toBe(2);
});
it("uses BestDebrid auth header without token query fallback", async () => {
const settings = {
...defaultSettings(),
@@ -659,6 +694,61 @@ describe("debrid service", () => {
expect(info[0].linksMax).toBe(500);
});
it("falls back from Debrid-Link limits/all to limits when the host is only present in limits", async () => {
const calledUrls: string[] = [];
globalThis.fetch = (async (input: RequestInfo | URL): Promise<Response> => {
const url = typeof input === "string" ? input : input instanceof URL ? input.toString() : input.url;
calledUrls.push(url);
if (url.includes("debrid-link.com/api/v2/downloader/limits/all")) {
return new Response(JSON.stringify({
success: true,
value: {
hosters: [
{
name: "uploaded",
daySize: { current: 1, value: 2 },
dayCount: { current: 3, value: 4 }
}
]
}
}), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
if (url.includes("debrid-link.com/api/v2/downloader/limits")) {
return new Response(JSON.stringify({
success: true,
value: {
hosters: [
{
name: "rapidgator",
displayName: "Rapidgator",
daySize: { current: 2147483648, value: 150323855360 },
dayCount: { current: 42, value: 500 }
}
]
}
}), {
status: 200,
headers: { "Content-Type": "application/json" }
});
}
return new Response("not-found", { status: 404 });
}) as typeof fetch;
const info = await fetchDebridLinkHostLimits("key-a", "rapidgator");
expect(info).toHaveLength(1);
expect(info[0].host).toBe("rapidgator");
expect(info[0].trafficCurrentBytes).toBe(2147483648);
expect(info[0].trafficMaxBytes).toBe(150323855360);
expect(info[0].linksCurrent).toBe(42);
expect(info[0].linksMax).toBe(500);
expect(calledUrls.some((url) => url.includes("/limits/all"))).toBe(true);
expect(calledUrls.some((url) => url.includes("/limits"))).toBe(true);
});
it("uses AllDebrid web path when enabled", async () => {
const settings = {
...defaultSettings(),
+53
View File
@@ -10,6 +10,7 @@ import { defaultSettings } from "../src/main/constants";
import { parseDebridLinkApiKeys } from "../src/shared/debrid-link-keys";
import { getProviderUsageDayKey } from "../src/shared/provider-daily-limits";
import { createStoragePaths, emptySession } from "../src/main/storage";
import { primeDebridLinkRuntimeCooldownForTests, resetDebridLinkRuntimeStateForTests } from "../src/main/debrid";
const tempDirs: string[] = [];
const originalFetch = globalThis.fetch;
@@ -42,6 +43,7 @@ async function removeDirWithRetries(dir: string): Promise<void> {
afterEach(async () => {
globalThis.fetch = originalFetch;
resetDebridLinkRuntimeStateForTests();
for (const dir of tempDirs.splice(0)) {
await removeDirWithRetries(dir);
}
@@ -762,6 +764,57 @@ describe("download manager", () => {
expect(fs.statSync(item.targetPath).size).toBe(binary.length);
});
it("queues Debrid-Link cooldown retries when wrapped unrestrict errors carry the cooldown marker", async () => {
const root = fs.mkdtempSync(path.join(os.tmpdir(), "rd-dm-"));
tempDirs.push(root);
let fetchCalls = 0;
globalThis.fetch = async (): Promise<Response> => {
fetchCalls += 1;
return new Response("not-found", { status: 404 });
};
const settings = {
...defaultSettings(),
debridLinkApiKeys: "dl-key-one\ndl-key-two",
providerOrder: ["debridlink"] as const,
providerPrimary: "debridlink" as const,
providerSecondary: "none" as const,
providerTertiary: "none" as const,
outputDir: path.join(root, "downloads"),
extractDir: path.join(root, "extract"),
retryLimit: 2,
autoExtract: false
};
const keys = parseDebridLinkApiKeys(settings.debridLinkApiKeys);
for (const key of keys) {
primeDebridLinkRuntimeCooldownForTests(key.id, 60_000, `${key.label} im Cooldown`);
}
const manager = new DownloadManager(
settings,
emptySession(),
createStoragePaths(path.join(root, "state"))
);
manager.addPackages([{ name: "debridlink-cooldown", links: ["https://rapidgator.net/file/example.part1.rar.html"] }]);
await manager.start();
await waitFor(() => {
const item = Object.values(manager.getSnapshot().session.items)[0];
return Boolean(item && item.status === "queued" && /debrid-link cooldown/i.test(item.fullStatus || ""));
}, 12000);
const item = Object.values(manager.getSnapshot().session.items)[0];
expect(item?.status).toBe("queued");
expect(item?.fullStatus).toContain("Debrid-Link Cooldown");
expect(item?.lastError).toContain("im Cooldown");
expect(item?.retries).toBe(1);
expect(fetchCalls).toBe(0);
await manager.stop();
});
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);